blob: f895cb9c89a3a788c00b8ecefb15e9edffd2d63a [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
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001327 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001328
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) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001334 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1335 BUG_ON(!piocb->vport);
1336 if (!(piocb->vport->load_flag & FC_UNLOADING))
1337 mod_timer(&piocb->vport->els_tmofunc,
1338 jiffies +
1339 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1340 }
dea31012005-04-17 16:05:31 -05001341
James Smart2e0fef82007-06-17 19:56:36 -05001342 return 0;
dea31012005-04-17 16:05:31 -05001343}
1344
James Smarte59058c2008-08-24 21:49:00 -04001345/**
James Smart3621a712009-04-06 18:47:14 -04001346 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001347 * @phba: Pointer to HBA context object.
1348 * @pring: Pointer to driver SLI ring object.
1349 *
1350 * This function is called with hbalock held to get next
1351 * iocb in txq of the given ring. If there is any iocb in
1352 * the txq, the function returns first iocb in the list after
1353 * removing the iocb from the list, else it returns NULL.
1354 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001355struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001356lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001357{
dea31012005-04-17 16:05:31 -05001358 struct lpfc_iocbq *cmd_iocb;
1359
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001360 lockdep_assert_held(&phba->hbalock);
1361
James Smart858c9f62007-06-17 19:56:39 -05001362 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001363 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001364}
1365
James Smarte59058c2008-08-24 21:49:00 -04001366/**
James Smart3621a712009-04-06 18:47:14 -04001367 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001368 * @phba: Pointer to HBA context object.
1369 * @pring: Pointer to driver SLI ring object.
1370 *
1371 * This function is called with hbalock held and the caller must post the
1372 * iocb without releasing the lock. If the caller releases the lock,
1373 * iocb slot returned by the function is not guaranteed to be available.
1374 * The function returns pointer to the next available iocb slot if there
1375 * is available slot in the ring, else it returns NULL.
1376 * If the get index of the ring is ahead of the put index, the function
1377 * will post an error attention event to the worker thread to take the
1378 * HBA to offline state.
1379 **/
dea31012005-04-17 16:05:31 -05001380static IOCB_t *
1381lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1382{
James Smart34b02dc2008-08-24 21:49:55 -04001383 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001384 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001385
1386 lockdep_assert_held(&phba->hbalock);
1387
James Smart7e56aa22012-08-03 12:35:34 -04001388 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1389 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1390 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001391
James Smart7e56aa22012-08-03 12:35:34 -04001392 if (unlikely(pring->sli.sli3.local_getidx ==
1393 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001394
James Smart7e56aa22012-08-03 12:35:34 -04001395 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001396
James Smart7e56aa22012-08-03 12:35:34 -04001397 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001399 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001400 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001401 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001402 pring->sli.sli3.local_getidx,
1403 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001404
James Smart2e0fef82007-06-17 19:56:36 -05001405 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001406 /*
1407 * All error attention handlers are posted to
1408 * worker thread
1409 */
1410 phba->work_ha |= HA_ERATT;
1411 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001412
James Smart5e9d9b82008-06-14 22:52:53 -04001413 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001414
1415 return NULL;
1416 }
1417
James Smart7e56aa22012-08-03 12:35:34 -04001418 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001419 return NULL;
1420 }
1421
James Smarted957682007-06-17 19:56:37 -05001422 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001423}
1424
James Smarte59058c2008-08-24 21:49:00 -04001425/**
James Smart3621a712009-04-06 18:47:14 -04001426 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001427 * @phba: Pointer to HBA context object.
1428 * @iocbq: Pointer to driver iocb object.
1429 *
1430 * This function gets an iotag for the iocb. If there is no unused iotag and
1431 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1432 * array and assigns a new iotag.
1433 * The function returns the allocated iotag if successful, else returns zero.
1434 * Zero is not a valid iotag.
1435 * The caller is not required to hold any lock.
1436 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001437uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001438lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001439{
James Smart2e0fef82007-06-17 19:56:36 -05001440 struct lpfc_iocbq **new_arr;
1441 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001442 size_t new_len;
1443 struct lpfc_sli *psli = &phba->sli;
1444 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001445
James Smart2e0fef82007-06-17 19:56:36 -05001446 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001447 iotag = psli->last_iotag;
1448 if(++iotag < psli->iocbq_lookup_len) {
1449 psli->last_iotag = iotag;
1450 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001451 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001452 iocbq->iotag = iotag;
1453 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001454 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001455 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1456 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001457 spin_unlock_irq(&phba->hbalock);
1458 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001459 GFP_KERNEL);
1460 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001461 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001462 old_arr = psli->iocbq_lookup;
1463 if (new_len <= psli->iocbq_lookup_len) {
1464 /* highly unprobable case */
1465 kfree(new_arr);
1466 iotag = psli->last_iotag;
1467 if(++iotag < psli->iocbq_lookup_len) {
1468 psli->last_iotag = iotag;
1469 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001470 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001471 iocbq->iotag = iotag;
1472 return iotag;
1473 }
James Smart2e0fef82007-06-17 19:56:36 -05001474 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001475 return 0;
1476 }
1477 if (psli->iocbq_lookup)
1478 memcpy(new_arr, old_arr,
1479 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001480 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001481 psli->iocbq_lookup = new_arr;
1482 psli->iocbq_lookup_len = new_len;
1483 psli->last_iotag = iotag;
1484 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001485 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001486 iocbq->iotag = iotag;
1487 kfree(old_arr);
1488 return iotag;
1489 }
James Smart8f6d98d2006-08-01 07:34:00 -04001490 } else
James Smart2e0fef82007-06-17 19:56:36 -05001491 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001492
James Smartbc739052010-08-04 16:11:18 -04001493 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001494 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1495 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001496
James Bottomley604a3e32005-10-29 10:28:33 -05001497 return 0;
dea31012005-04-17 16:05:31 -05001498}
1499
James Smarte59058c2008-08-24 21:49:00 -04001500/**
James Smart3621a712009-04-06 18:47:14 -04001501 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001502 * @phba: Pointer to HBA context object.
1503 * @pring: Pointer to driver SLI ring object.
1504 * @iocb: Pointer to iocb slot in the ring.
1505 * @nextiocb: Pointer to driver iocb object which need to be
1506 * posted to firmware.
1507 *
1508 * This function is called with hbalock held to post a new iocb to
1509 * the firmware. This function copies the new iocb to ring iocb slot and
1510 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1511 * a completion call back for this iocb else the function will free the
1512 * iocb object.
1513 **/
dea31012005-04-17 16:05:31 -05001514static void
1515lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1516 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1517{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001518 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001519 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001520 * Set up an iotag
dea31012005-04-17 16:05:31 -05001521 */
James Bottomley604a3e32005-10-29 10:28:33 -05001522 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001523
James Smarte2a0a9d2008-12-04 22:40:02 -05001524
James Smarta58cbd52007-08-02 11:09:43 -04001525 if (pring->ringno == LPFC_ELS_RING) {
1526 lpfc_debugfs_slow_ring_trc(phba,
1527 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1528 *(((uint32_t *) &nextiocb->iocb) + 4),
1529 *(((uint32_t *) &nextiocb->iocb) + 6),
1530 *(((uint32_t *) &nextiocb->iocb) + 7));
1531 }
1532
dea31012005-04-17 16:05:31 -05001533 /*
1534 * Issue iocb command to adapter
1535 */
James Smart92d7f7b2007-06-17 19:56:38 -05001536 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001537 wmb();
1538 pring->stats.iocb_cmd++;
1539
1540 /*
1541 * If there is no completion routine to call, we can release the
1542 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1543 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1544 */
1545 if (nextiocb->iocb_cmpl)
1546 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001547 else
James Smart2e0fef82007-06-17 19:56:36 -05001548 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001549
1550 /*
1551 * Let the HBA know what IOCB slot will be the next one the
1552 * driver will put a command into.
1553 */
James Smart7e56aa22012-08-03 12:35:34 -04001554 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1555 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001556}
1557
James Smarte59058c2008-08-24 21:49:00 -04001558/**
James Smart3621a712009-04-06 18:47:14 -04001559 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001560 * @phba: Pointer to HBA context object.
1561 * @pring: Pointer to driver SLI ring object.
1562 *
1563 * The caller is not required to hold any lock for calling this function.
1564 * This function updates the chip attention bits for the ring to inform firmware
1565 * that there are pending work to be done for this ring and requests an
1566 * interrupt when there is space available in the ring. This function is
1567 * called when the driver is unable to post more iocbs to the ring due
1568 * to unavailability of space in the ring.
1569 **/
dea31012005-04-17 16:05:31 -05001570static void
James Smart2e0fef82007-06-17 19:56:36 -05001571lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001572{
1573 int ringno = pring->ringno;
1574
1575 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1576
1577 wmb();
1578
1579 /*
1580 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1581 * The HBA will tell us when an IOCB entry is available.
1582 */
1583 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1584 readl(phba->CAregaddr); /* flush */
1585
1586 pring->stats.iocb_cmd_full++;
1587}
1588
James Smarte59058c2008-08-24 21:49:00 -04001589/**
James Smart3621a712009-04-06 18:47:14 -04001590 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001591 * @phba: Pointer to HBA context object.
1592 * @pring: Pointer to driver SLI ring object.
1593 *
1594 * This function updates the chip attention register bit for the
1595 * given ring to inform HBA that there is more work to be done
1596 * in this ring. The caller is not required to hold any lock.
1597 **/
dea31012005-04-17 16:05:31 -05001598static void
James Smart2e0fef82007-06-17 19:56:36 -05001599lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001600{
1601 int ringno = pring->ringno;
1602
1603 /*
1604 * Tell the HBA that there is work to do in this ring.
1605 */
James Smart34b02dc2008-08-24 21:49:55 -04001606 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1607 wmb();
1608 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1609 readl(phba->CAregaddr); /* flush */
1610 }
dea31012005-04-17 16:05:31 -05001611}
1612
James Smarte59058c2008-08-24 21:49:00 -04001613/**
James Smart3621a712009-04-06 18:47:14 -04001614 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001615 * @phba: Pointer to HBA context object.
1616 * @pring: Pointer to driver SLI ring object.
1617 *
1618 * This function is called with hbalock held to post pending iocbs
1619 * in the txq to the firmware. This function is called when driver
1620 * detects space available in the ring.
1621 **/
dea31012005-04-17 16:05:31 -05001622static void
James Smart2e0fef82007-06-17 19:56:36 -05001623lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001624{
1625 IOCB_t *iocb;
1626 struct lpfc_iocbq *nextiocb;
1627
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001628 lockdep_assert_held(&phba->hbalock);
1629
dea31012005-04-17 16:05:31 -05001630 /*
1631 * Check to see if:
1632 * (a) there is anything on the txq to send
1633 * (b) link is up
1634 * (c) link attention events can be processed (fcp ring only)
1635 * (d) IOCB processing is not blocked by the outstanding mbox command.
1636 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001637
1638 if (lpfc_is_link_up(phba) &&
1639 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001640 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001641 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001642
1643 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1644 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1645 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1646
1647 if (iocb)
1648 lpfc_sli_update_ring(phba, pring);
1649 else
1650 lpfc_sli_update_full_ring(phba, pring);
1651 }
1652
1653 return;
1654}
1655
James Smarte59058c2008-08-24 21:49:00 -04001656/**
James Smart3621a712009-04-06 18:47:14 -04001657 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001658 * @phba: Pointer to HBA context object.
1659 * @hbqno: HBQ number.
1660 *
1661 * This function is called with hbalock held to get the next
1662 * available slot for the given HBQ. If there is free slot
1663 * available for the HBQ it will return pointer to the next available
1664 * HBQ entry else it will return NULL.
1665 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001666static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001667lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1668{
1669 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1670
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001671 lockdep_assert_held(&phba->hbalock);
1672
James Smarted957682007-06-17 19:56:37 -05001673 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1674 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1675 hbqp->next_hbqPutIdx = 0;
1676
1677 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001678 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001679 uint32_t getidx = le32_to_cpu(raw_index);
1680
1681 hbqp->local_hbqGetIdx = getidx;
1682
1683 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1684 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001685 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001686 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001687 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001688 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001689 hbqp->entry_count);
1690
1691 phba->link_state = LPFC_HBA_ERROR;
1692 return NULL;
1693 }
1694
1695 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1696 return NULL;
1697 }
1698
James Smart51ef4c22007-08-02 11:10:31 -04001699 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1700 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001701}
1702
James Smarte59058c2008-08-24 21:49:00 -04001703/**
James Smart3621a712009-04-06 18:47:14 -04001704 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001705 * @phba: Pointer to HBA context object.
1706 *
1707 * This function is called with no lock held to free all the
1708 * hbq buffers while uninitializing the SLI interface. It also
1709 * frees the HBQ buffers returned by the firmware but not yet
1710 * processed by the upper layers.
1711 **/
James Smarted957682007-06-17 19:56:37 -05001712void
1713lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1714{
James Smart92d7f7b2007-06-17 19:56:38 -05001715 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1716 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001717 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001718 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001719 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001720
James Smart51ef4c22007-08-02 11:10:31 -04001721 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001722 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001723 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001724 for (i = 0; i < hbq_count; ++i) {
1725 list_for_each_entry_safe(dmabuf, next_dmabuf,
1726 &phba->hbqs[i].hbq_buffer_list, list) {
1727 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1728 list_del(&hbq_buf->dbuf.list);
1729 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1730 }
James Smarta8adb832007-10-27 13:37:53 -04001731 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001732 }
James Smart3163f722008-02-08 18:50:25 -05001733 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001734 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1735 list) {
James Smart3163f722008-02-08 18:50:25 -05001736 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1737 list_del(&hbq_buf->dbuf.list);
1738 if (hbq_buf->tag == -1) {
1739 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1740 (phba, hbq_buf);
1741 } else {
1742 hbqno = hbq_buf->tag >> 16;
1743 if (hbqno >= LPFC_MAX_HBQS)
1744 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1745 (phba, hbq_buf);
1746 else
1747 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1748 hbq_buf);
1749 }
1750 }
1751
1752 /* Mark the HBQs not in use */
1753 phba->hbq_in_use = 0;
1754 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001755}
1756
James Smarte59058c2008-08-24 21:49:00 -04001757/**
James Smart3621a712009-04-06 18:47:14 -04001758 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001759 * @phba: Pointer to HBA context object.
1760 * @hbqno: HBQ number.
1761 * @hbq_buf: Pointer to HBQ buffer.
1762 *
1763 * This function is called with the hbalock held to post a
1764 * hbq buffer to the firmware. If the function finds an empty
1765 * slot in the HBQ, it will post the buffer. The function will return
1766 * pointer to the hbq entry if it successfully post the buffer
1767 * else it will return NULL.
1768 **/
James Smart3772a992009-05-22 14:50:54 -04001769static int
James Smarted957682007-06-17 19:56:37 -05001770lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001771 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001772{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001773 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001774 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1775}
1776
1777/**
1778 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1779 * @phba: Pointer to HBA context object.
1780 * @hbqno: HBQ number.
1781 * @hbq_buf: Pointer to HBQ buffer.
1782 *
1783 * This function is called with the hbalock held to post a hbq buffer to the
1784 * firmware. If the function finds an empty slot in the HBQ, it will post the
1785 * buffer and place it on the hbq_buffer_list. The function will return zero if
1786 * it successfully post the buffer else it will return an error.
1787 **/
1788static int
1789lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1790 struct hbq_dmabuf *hbq_buf)
1791{
James Smarted957682007-06-17 19:56:37 -05001792 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001793 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001794
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001795 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001796 /* Get next HBQ entry slot to use */
1797 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1798 if (hbqe) {
1799 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1800
James Smart92d7f7b2007-06-17 19:56:38 -05001801 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1802 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001803 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001804 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001805 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1806 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1807 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001808 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1809 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001810 /* flush */
James Smarted957682007-06-17 19:56:37 -05001811 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001812 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001813 return 0;
1814 } else
1815 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001816}
1817
James Smart4f774512009-05-22 14:52:35 -04001818/**
1819 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1820 * @phba: Pointer to HBA context object.
1821 * @hbqno: HBQ number.
1822 * @hbq_buf: Pointer to HBQ buffer.
1823 *
1824 * This function is called with the hbalock held to post an RQE to the SLI4
1825 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1826 * the hbq_buffer_list and return zero, otherwise it will return an error.
1827 **/
1828static int
1829lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1830 struct hbq_dmabuf *hbq_buf)
1831{
1832 int rc;
1833 struct lpfc_rqe hrqe;
1834 struct lpfc_rqe drqe;
1835
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001836 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001837 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1838 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1839 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1840 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1841 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1842 &hrqe, &drqe);
1843 if (rc < 0)
1844 return rc;
1845 hbq_buf->tag = rc;
1846 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1847 return 0;
1848}
1849
James Smarte59058c2008-08-24 21:49:00 -04001850/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001851static struct lpfc_hbq_init lpfc_els_hbq = {
1852 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001853 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001854 .mask_count = 0,
1855 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001856 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001857 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001858 .init_count = 40,
1859 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001860};
James Smarted957682007-06-17 19:56:37 -05001861
James Smarte59058c2008-08-24 21:49:00 -04001862/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001863static struct lpfc_hbq_init lpfc_extra_hbq = {
1864 .rn = 1,
1865 .entry_count = 200,
1866 .mask_count = 0,
1867 .profile = 0,
1868 .ring_mask = (1 << LPFC_EXTRA_RING),
1869 .buffer_count = 0,
1870 .init_count = 0,
1871 .add_count = 5,
1872};
1873
James Smarte59058c2008-08-24 21:49:00 -04001874/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001875struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001876 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001877 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001878};
1879
James Smarte59058c2008-08-24 21:49:00 -04001880/**
James Smart3621a712009-04-06 18:47:14 -04001881 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001882 * @phba: Pointer to HBA context object.
1883 * @hbqno: HBQ number.
1884 * @count: Number of HBQ buffers to be posted.
1885 *
James Smartd7c255b2008-08-24 21:50:00 -04001886 * This function is called with no lock held to post more hbq buffers to the
1887 * given HBQ. The function returns the number of HBQ buffers successfully
1888 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001889 **/
James Smart311464e2007-08-02 11:10:37 -04001890static int
James Smart92d7f7b2007-06-17 19:56:38 -05001891lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1892{
James Smartd7c255b2008-08-24 21:50:00 -04001893 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001894 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001895 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001896 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001897 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001898 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001899
James Smartd7c255b2008-08-24 21:50:00 -04001900 if ((phba->hbqs[hbqno].buffer_count + count) >
1901 lpfc_hbq_defs[hbqno]->entry_count)
1902 count = lpfc_hbq_defs[hbqno]->entry_count -
1903 phba->hbqs[hbqno].buffer_count;
1904 if (!count)
1905 return 0;
1906 /* Allocate HBQ entries */
1907 for (i = 0; i < count; i++) {
1908 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1909 if (!hbq_buffer)
1910 break;
1911 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1912 }
James Smart3163f722008-02-08 18:50:25 -05001913 /* Check whether HBQ is still in use */
1914 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001915 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001916 goto err;
1917 while (!list_empty(&hbq_buf_list)) {
1918 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1919 dbuf.list);
1920 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1921 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001922 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001923 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001924 posted++;
1925 } else
James Smart51ef4c22007-08-02 11:10:31 -04001926 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001927 }
James Smart3163f722008-02-08 18:50:25 -05001928 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001929 return posted;
1930err:
1931 spin_unlock_irqrestore(&phba->hbalock, flags);
1932 while (!list_empty(&hbq_buf_list)) {
1933 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1934 dbuf.list);
1935 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1936 }
James Smart92d7f7b2007-06-17 19:56:38 -05001937 return 0;
James Smarted957682007-06-17 19:56:37 -05001938}
1939
James Smarte59058c2008-08-24 21:49:00 -04001940/**
James Smart3621a712009-04-06 18:47:14 -04001941 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001942 * @phba: Pointer to HBA context object.
1943 * @qno: HBQ number.
1944 *
1945 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001946 * is called with no lock held. The function returns the number of HBQ entries
1947 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001948 **/
James Smarted957682007-06-17 19:56:37 -05001949int
James Smart92d7f7b2007-06-17 19:56:38 -05001950lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001951{
James Smartdef9c7a2009-12-21 17:02:28 -05001952 if (phba->sli_rev == LPFC_SLI_REV4)
1953 return 0;
1954 else
1955 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1956 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001957}
1958
James Smarte59058c2008-08-24 21:49:00 -04001959/**
James Smart3621a712009-04-06 18:47:14 -04001960 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001961 * @phba: Pointer to HBA context object.
1962 * @qno: HBQ queue number.
1963 *
1964 * This function is called from SLI initialization code path with
1965 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001966 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001967 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001968static int
James Smart92d7f7b2007-06-17 19:56:38 -05001969lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001970{
James Smartdef9c7a2009-12-21 17:02:28 -05001971 if (phba->sli_rev == LPFC_SLI_REV4)
1972 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001973 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001974 else
1975 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1976 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001977}
1978
James Smarte59058c2008-08-24 21:49:00 -04001979/**
James Smart3772a992009-05-22 14:50:54 -04001980 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1981 * @phba: Pointer to HBA context object.
1982 * @hbqno: HBQ number.
1983 *
1984 * This function removes the first hbq buffer on an hbq list and returns a
1985 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1986 **/
1987static struct hbq_dmabuf *
1988lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1989{
1990 struct lpfc_dmabuf *d_buf;
1991
1992 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1993 if (!d_buf)
1994 return NULL;
1995 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1996}
1997
1998/**
James Smart3621a712009-04-06 18:47:14 -04001999 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002000 * @phba: Pointer to HBA context object.
2001 * @tag: Tag of the hbq buffer.
2002 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002003 * This function searches for the hbq buffer associated with the given tag in
2004 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2005 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002006 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002007static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002008lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2009{
James Smart92d7f7b2007-06-17 19:56:38 -05002010 struct lpfc_dmabuf *d_buf;
2011 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002012 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002013
James Smart51ef4c22007-08-02 11:10:31 -04002014 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002015 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002016 return NULL;
2017
James Smart3772a992009-05-22 14:50:54 -04002018 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002019 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002020 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002021 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002022 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002023 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002024 }
2025 }
James Smart3772a992009-05-22 14:50:54 -04002026 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002028 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002029 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002030 return NULL;
James Smarted957682007-06-17 19:56:37 -05002031}
2032
James Smarte59058c2008-08-24 21:49:00 -04002033/**
James Smart3621a712009-04-06 18:47:14 -04002034 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002035 * @phba: Pointer to HBA context object.
2036 * @hbq_buffer: Pointer to HBQ buffer.
2037 *
2038 * This function is called with hbalock. This function gives back
2039 * the hbq buffer to firmware. If the HBQ does not have space to
2040 * post the buffer, it will free the buffer.
2041 **/
James Smarted957682007-06-17 19:56:37 -05002042void
James Smart51ef4c22007-08-02 11:10:31 -04002043lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002044{
2045 uint32_t hbqno;
2046
James Smart51ef4c22007-08-02 11:10:31 -04002047 if (hbq_buffer) {
2048 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002049 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002050 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002051 }
2052}
2053
James Smarte59058c2008-08-24 21:49:00 -04002054/**
James Smart3621a712009-04-06 18:47:14 -04002055 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002056 * @mbxCommand: mailbox command code.
2057 *
2058 * This function is called by the mailbox event handler function to verify
2059 * that the completed mailbox command is a legitimate mailbox command. If the
2060 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2061 * and the mailbox event handler will take the HBA offline.
2062 **/
dea31012005-04-17 16:05:31 -05002063static int
2064lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2065{
2066 uint8_t ret;
2067
2068 switch (mbxCommand) {
2069 case MBX_LOAD_SM:
2070 case MBX_READ_NV:
2071 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002072 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002073 case MBX_RUN_BIU_DIAG:
2074 case MBX_INIT_LINK:
2075 case MBX_DOWN_LINK:
2076 case MBX_CONFIG_LINK:
2077 case MBX_CONFIG_RING:
2078 case MBX_RESET_RING:
2079 case MBX_READ_CONFIG:
2080 case MBX_READ_RCONFIG:
2081 case MBX_READ_SPARM:
2082 case MBX_READ_STATUS:
2083 case MBX_READ_RPI:
2084 case MBX_READ_XRI:
2085 case MBX_READ_REV:
2086 case MBX_READ_LNK_STAT:
2087 case MBX_REG_LOGIN:
2088 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002089 case MBX_CLEAR_LA:
2090 case MBX_DUMP_MEMORY:
2091 case MBX_DUMP_CONTEXT:
2092 case MBX_RUN_DIAGS:
2093 case MBX_RESTART:
2094 case MBX_UPDATE_CFG:
2095 case MBX_DOWN_LOAD:
2096 case MBX_DEL_LD_ENTRY:
2097 case MBX_RUN_PROGRAM:
2098 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002099 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002100 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002101 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002102 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002103 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002104 case MBX_LOAD_AREA:
2105 case MBX_RUN_BIU_DIAG64:
2106 case MBX_CONFIG_PORT:
2107 case MBX_READ_SPARM64:
2108 case MBX_READ_RPI64:
2109 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002110 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002111 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002112 case MBX_SET_DEBUG:
2113 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002114 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002115 case MBX_REG_VPI:
2116 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002117 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002118 case MBX_PORT_CAPABILITIES:
2119 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002120 case MBX_SLI4_CONFIG:
2121 case MBX_SLI4_REQ_FTRS:
2122 case MBX_REG_FCFI:
2123 case MBX_UNREG_FCFI:
2124 case MBX_REG_VFI:
2125 case MBX_UNREG_VFI:
2126 case MBX_INIT_VPI:
2127 case MBX_INIT_VFI:
2128 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002129 case MBX_READ_EVENT_LOG_STATUS:
2130 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002131 case MBX_SECURITY_MGMT:
2132 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002133 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002134 ret = mbxCommand;
2135 break;
2136 default:
2137 ret = MBX_SHUTDOWN;
2138 break;
2139 }
James Smart2e0fef82007-06-17 19:56:36 -05002140 return ret;
dea31012005-04-17 16:05:31 -05002141}
James Smarte59058c2008-08-24 21:49:00 -04002142
2143/**
James Smart3621a712009-04-06 18:47:14 -04002144 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002145 * @phba: Pointer to HBA context object.
2146 * @pmboxq: Pointer to mailbox command.
2147 *
2148 * This is completion handler function for mailbox commands issued from
2149 * lpfc_sli_issue_mbox_wait function. This function is called by the
2150 * mailbox event handler function with no lock held. This function
2151 * will wake up thread waiting on the wait queue pointed by context1
2152 * of the mailbox.
2153 **/
James Smart04c68492009-05-22 14:52:52 -04002154void
James Smart2e0fef82007-06-17 19:56:36 -05002155lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002156{
2157 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002158 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002159
2160 /*
2161 * If pdone_q is empty, the driver thread gave up waiting and
2162 * continued running.
2163 */
James Smart7054a602007-04-25 09:52:34 -04002164 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002165 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002166 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2167 if (pdone_q)
2168 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002169 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002170 return;
2171}
2172
James Smarte59058c2008-08-24 21:49:00 -04002173
2174/**
James Smart3621a712009-04-06 18:47:14 -04002175 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002176 * @phba: Pointer to HBA context object.
2177 * @pmb: Pointer to mailbox object.
2178 *
2179 * This function is the default mailbox completion handler. It
2180 * frees the memory resources associated with the completed mailbox
2181 * command. If the completed command is a REG_LOGIN mailbox command,
2182 * this function will issue a UREG_LOGIN to re-claim the RPI.
2183 **/
dea31012005-04-17 16:05:31 -05002184void
James Smart2e0fef82007-06-17 19:56:36 -05002185lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002186{
James Smartd439d282010-09-29 11:18:45 -04002187 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002188 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002189 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002190 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002191 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002192 int rc;
2193
dea31012005-04-17 16:05:31 -05002194 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002195
dea31012005-04-17 16:05:31 -05002196 if (mp) {
2197 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2198 kfree(mp);
2199 }
James Smart7054a602007-04-25 09:52:34 -04002200
2201 /*
2202 * If a REG_LOGIN succeeded after node is destroyed or node
2203 * is in re-discovery driver need to cleanup the RPI.
2204 */
James Smart2e0fef82007-06-17 19:56:36 -05002205 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002206 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2207 !pmb->u.mb.mbxStatus) {
2208 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002209 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002210 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002211 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002212 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002213 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2214 if (rc != MBX_NOT_FINISHED)
2215 return;
2216 }
2217
James Smart695a8142010-01-26 23:08:03 -05002218 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2219 !(phba->pport->load_flag & FC_UNLOADING) &&
2220 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002221 shost = lpfc_shost_from_vport(vport);
2222 spin_lock_irq(shost->host_lock);
2223 vport->vpi_state |= LPFC_VPI_REGISTERED;
2224 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2225 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002226 }
2227
James Smartd439d282010-09-29 11:18:45 -04002228 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2229 ndlp = (struct lpfc_nodelist *)pmb->context2;
2230 lpfc_nlp_put(ndlp);
2231 pmb->context2 = NULL;
2232 }
2233
James Smartdcf2a4e2010-09-29 11:18:53 -04002234 /* Check security permission status on INIT_LINK mailbox command */
2235 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2236 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2237 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2238 "2860 SLI authentication is required "
2239 "for INIT_LINK but has not done yet\n");
2240
James Smart04c68492009-05-22 14:52:52 -04002241 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2242 lpfc_sli4_mbox_cmd_free(phba, pmb);
2243 else
2244 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002245}
James Smartbe6bb942015-04-07 15:07:22 -04002246 /**
2247 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2248 * @phba: Pointer to HBA context object.
2249 * @pmb: Pointer to mailbox object.
2250 *
2251 * This function is the unreg rpi mailbox completion handler. It
2252 * frees the memory resources associated with the completed mailbox
2253 * command. An additional refrenece is put on the ndlp to prevent
2254 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2255 * the unreg mailbox command completes, this routine puts the
2256 * reference back.
2257 *
2258 **/
2259void
2260lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2261{
2262 struct lpfc_vport *vport = pmb->vport;
2263 struct lpfc_nodelist *ndlp;
2264
2265 ndlp = pmb->context1;
2266 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2267 if (phba->sli_rev == LPFC_SLI_REV4 &&
2268 (bf_get(lpfc_sli_intf_if_type,
2269 &phba->sli4_hba.sli_intf) ==
2270 LPFC_SLI_INTF_IF_TYPE_2)) {
2271 if (ndlp) {
2272 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2273 "0010 UNREG_LOGIN vpi:%x "
2274 "rpi:%x DID:%x map:%x %p\n",
2275 vport->vpi, ndlp->nlp_rpi,
2276 ndlp->nlp_DID,
2277 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002278 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002279 lpfc_nlp_put(ndlp);
2280 }
2281 }
2282 }
2283
2284 mempool_free(pmb, phba->mbox_mem_pool);
2285}
dea31012005-04-17 16:05:31 -05002286
James Smarte59058c2008-08-24 21:49:00 -04002287/**
James Smart3621a712009-04-06 18:47:14 -04002288 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002289 * @phba: Pointer to HBA context object.
2290 *
2291 * This function is called with no lock held. This function processes all
2292 * the completed mailbox commands and gives it to upper layers. The interrupt
2293 * service routine processes mailbox completion interrupt and adds completed
2294 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2295 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2296 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2297 * function returns the mailbox commands to the upper layer by calling the
2298 * completion handler function of each mailbox.
2299 **/
dea31012005-04-17 16:05:31 -05002300int
James Smart2e0fef82007-06-17 19:56:36 -05002301lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002302{
James Smart92d7f7b2007-06-17 19:56:38 -05002303 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002304 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002305 int rc;
2306 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002307
2308 phba->sli.slistat.mbox_event++;
2309
James Smart92d7f7b2007-06-17 19:56:38 -05002310 /* Get all completed mailboxe buffers into the cmplq */
2311 spin_lock_irq(&phba->hbalock);
2312 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2313 spin_unlock_irq(&phba->hbalock);
2314
dea31012005-04-17 16:05:31 -05002315 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002316 do {
2317 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2318 if (pmb == NULL)
2319 break;
2320
James Smart04c68492009-05-22 14:52:52 -04002321 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002322
James Smart858c9f62007-06-17 19:56:39 -05002323 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2324 if (pmb->vport) {
2325 lpfc_debugfs_disc_trc(pmb->vport,
2326 LPFC_DISC_TRC_MBOX_VPORT,
2327 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2328 (uint32_t)pmbox->mbxCommand,
2329 pmbox->un.varWords[0],
2330 pmbox->un.varWords[1]);
2331 }
2332 else {
2333 lpfc_debugfs_disc_trc(phba->pport,
2334 LPFC_DISC_TRC_MBOX,
2335 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2336 (uint32_t)pmbox->mbxCommand,
2337 pmbox->un.varWords[0],
2338 pmbox->un.varWords[1]);
2339 }
2340 }
2341
dea31012005-04-17 16:05:31 -05002342 /*
2343 * It is a fatal error if unknown mbox command completion.
2344 */
2345 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2346 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002347 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002349 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002350 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002351 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002352 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002353 lpfc_sli_config_mbox_subsys_get(phba,
2354 pmb),
2355 lpfc_sli_config_mbox_opcode_get(phba,
2356 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002357 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002358 phba->work_hs = HS_FFER3;
2359 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002360 continue;
dea31012005-04-17 16:05:31 -05002361 }
2362
dea31012005-04-17 16:05:31 -05002363 if (pmbox->mbxStatus) {
2364 phba->sli.slistat.mbox_stat_err++;
2365 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2366 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002367 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002368 LOG_MBOX | LOG_SLI,
2369 "(%d):0305 Mbox cmd cmpl "
2370 "error - RETRYing Data: x%x "
2371 "(x%x/x%x) x%x x%x x%x\n",
2372 pmb->vport ? pmb->vport->vpi : 0,
2373 pmbox->mbxCommand,
2374 lpfc_sli_config_mbox_subsys_get(phba,
2375 pmb),
2376 lpfc_sli_config_mbox_opcode_get(phba,
2377 pmb),
2378 pmbox->mbxStatus,
2379 pmbox->un.varWords[0],
2380 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002381 pmbox->mbxStatus = 0;
2382 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002383 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002384 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002385 continue;
dea31012005-04-17 16:05:31 -05002386 }
2387 }
2388
2389 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002390 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002391 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002392 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2393 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002394 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002395 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002396 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2397 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002398 pmb->mbox_cmpl,
2399 *((uint32_t *) pmbox),
2400 pmbox->un.varWords[0],
2401 pmbox->un.varWords[1],
2402 pmbox->un.varWords[2],
2403 pmbox->un.varWords[3],
2404 pmbox->un.varWords[4],
2405 pmbox->un.varWords[5],
2406 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002407 pmbox->un.varWords[7],
2408 pmbox->un.varWords[8],
2409 pmbox->un.varWords[9],
2410 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002411
James Smart92d7f7b2007-06-17 19:56:38 -05002412 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002413 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002414 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002415 return 0;
dea31012005-04-17 16:05:31 -05002416}
James Smart92d7f7b2007-06-17 19:56:38 -05002417
James Smarte59058c2008-08-24 21:49:00 -04002418/**
James Smart3621a712009-04-06 18:47:14 -04002419 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002420 * @phba: Pointer to HBA context object.
2421 * @pring: Pointer to driver SLI ring object.
2422 * @tag: buffer tag.
2423 *
2424 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2425 * is set in the tag the buffer is posted for a particular exchange,
2426 * the function will return the buffer without replacing the buffer.
2427 * If the buffer is for unsolicited ELS or CT traffic, this function
2428 * returns the buffer and also posts another buffer to the firmware.
2429 **/
James Smart76bb24e2007-10-27 13:38:00 -04002430static struct lpfc_dmabuf *
2431lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002432 struct lpfc_sli_ring *pring,
2433 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002434{
James Smart9f1e1b52008-12-04 22:39:40 -05002435 struct hbq_dmabuf *hbq_entry;
2436
James Smart76bb24e2007-10-27 13:38:00 -04002437 if (tag & QUE_BUFTAG_BIT)
2438 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002439 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2440 if (!hbq_entry)
2441 return NULL;
2442 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002443}
James Smart57127f12007-10-27 13:37:05 -04002444
James Smart3772a992009-05-22 14:50:54 -04002445/**
2446 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2447 * @phba: Pointer to HBA context object.
2448 * @pring: Pointer to driver SLI ring object.
2449 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2450 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2451 * @fch_type: the type for the first frame of the sequence.
2452 *
2453 * This function is called with no lock held. This function uses the r_ctl and
2454 * type of the received sequence to find the correct callback function to call
2455 * to process the sequence.
2456 **/
2457static int
2458lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2459 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2460 uint32_t fch_type)
2461{
2462 int i;
2463
2464 /* unSolicited Responses */
2465 if (pring->prt[0].profile) {
2466 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2467 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2468 saveq);
2469 return 1;
2470 }
2471 /* We must search, based on rctl / type
2472 for the right routine */
2473 for (i = 0; i < pring->num_mask; i++) {
2474 if ((pring->prt[i].rctl == fch_r_ctl) &&
2475 (pring->prt[i].type == fch_type)) {
2476 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2477 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2478 (phba, pring, saveq);
2479 return 1;
2480 }
2481 }
2482 return 0;
2483}
James Smarte59058c2008-08-24 21:49:00 -04002484
2485/**
James Smart3621a712009-04-06 18:47:14 -04002486 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002487 * @phba: Pointer to HBA context object.
2488 * @pring: Pointer to driver SLI ring object.
2489 * @saveq: Pointer to the unsolicited iocb.
2490 *
2491 * This function is called with no lock held by the ring event handler
2492 * when there is an unsolicited iocb posted to the response ring by the
2493 * firmware. This function gets the buffer associated with the iocbs
2494 * and calls the event handler for the ring. This function handles both
2495 * qring buffers and hbq buffers.
2496 * When the function returns 1 the caller can free the iocb object otherwise
2497 * upper layer functions will free the iocb objects.
2498 **/
dea31012005-04-17 16:05:31 -05002499static int
2500lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2501 struct lpfc_iocbq *saveq)
2502{
2503 IOCB_t * irsp;
2504 WORD5 * w5p;
2505 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002506 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002507 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002508
dea31012005-04-17 16:05:31 -05002509 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002510
2511 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2512 if (pring->lpfc_sli_rcv_async_status)
2513 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2514 else
2515 lpfc_printf_log(phba,
2516 KERN_WARNING,
2517 LOG_SLI,
2518 "0316 Ring %d handler: unexpected "
2519 "ASYNC_STATUS iocb received evt_code "
2520 "0x%x\n",
2521 pring->ringno,
2522 irsp->un.asyncstat.evt_code);
2523 return 1;
2524 }
2525
James Smart3163f722008-02-08 18:50:25 -05002526 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2527 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2528 if (irsp->ulpBdeCount > 0) {
2529 dmzbuf = lpfc_sli_get_buff(phba, pring,
2530 irsp->un.ulpWord[3]);
2531 lpfc_in_buf_free(phba, dmzbuf);
2532 }
2533
2534 if (irsp->ulpBdeCount > 1) {
2535 dmzbuf = lpfc_sli_get_buff(phba, pring,
2536 irsp->unsli3.sli3Words[3]);
2537 lpfc_in_buf_free(phba, dmzbuf);
2538 }
2539
2540 if (irsp->ulpBdeCount > 2) {
2541 dmzbuf = lpfc_sli_get_buff(phba, pring,
2542 irsp->unsli3.sli3Words[7]);
2543 lpfc_in_buf_free(phba, dmzbuf);
2544 }
2545
2546 return 1;
2547 }
2548
James Smart92d7f7b2007-06-17 19:56:38 -05002549 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002550 if (irsp->ulpBdeCount != 0) {
2551 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002552 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002553 if (!saveq->context2)
2554 lpfc_printf_log(phba,
2555 KERN_ERR,
2556 LOG_SLI,
2557 "0341 Ring %d Cannot find buffer for "
2558 "an unsolicited iocb. tag 0x%x\n",
2559 pring->ringno,
2560 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002561 }
2562 if (irsp->ulpBdeCount == 2) {
2563 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002564 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002565 if (!saveq->context3)
2566 lpfc_printf_log(phba,
2567 KERN_ERR,
2568 LOG_SLI,
2569 "0342 Ring %d Cannot find buffer for an"
2570 " unsolicited iocb. tag 0x%x\n",
2571 pring->ringno,
2572 irsp->unsli3.sli3Words[7]);
2573 }
2574 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002575 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002576 if (irsp->ulpBdeCount != 0) {
2577 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2578 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002579 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002580 lpfc_printf_log(phba,
2581 KERN_ERR,
2582 LOG_SLI,
2583 "0343 Ring %d Cannot find "
2584 "buffer for an unsolicited iocb"
2585 ". tag 0x%x\n", pring->ringno,
2586 irsp->un.ulpWord[3]);
2587 }
2588 if (irsp->ulpBdeCount == 2) {
2589 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2590 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002591 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002592 lpfc_printf_log(phba,
2593 KERN_ERR,
2594 LOG_SLI,
2595 "0344 Ring %d Cannot find "
2596 "buffer for an unsolicited "
2597 "iocb. tag 0x%x\n",
2598 pring->ringno,
2599 irsp->unsli3.sli3Words[7]);
2600 }
2601 }
James Smart92d7f7b2007-06-17 19:56:38 -05002602 }
James Smart9c2face2008-01-11 01:53:18 -05002603 if (irsp->ulpBdeCount != 0 &&
2604 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2605 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2606 int found = 0;
2607
2608 /* search continue save q for same XRI */
2609 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002610 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2611 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002612 list_add_tail(&saveq->list, &iocbq->list);
2613 found = 1;
2614 break;
2615 }
2616 }
2617 if (!found)
2618 list_add_tail(&saveq->clist,
2619 &pring->iocb_continue_saveq);
2620 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2621 list_del_init(&iocbq->clist);
2622 saveq = iocbq;
2623 irsp = &(saveq->iocb);
2624 } else
2625 return 0;
2626 }
2627 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2628 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2629 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002630 Rctl = FC_RCTL_ELS_REQ;
2631 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002632 } else {
2633 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2634 Rctl = w5p->hcsw.Rctl;
2635 Type = w5p->hcsw.Type;
2636
2637 /* Firmware Workaround */
2638 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2639 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2640 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002641 Rctl = FC_RCTL_ELS_REQ;
2642 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002643 w5p->hcsw.Rctl = Rctl;
2644 w5p->hcsw.Type = Type;
2645 }
2646 }
James Smart92d7f7b2007-06-17 19:56:38 -05002647
James Smart3772a992009-05-22 14:50:54 -04002648 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002649 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002650 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002651 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002652 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002653
James Smart92d7f7b2007-06-17 19:56:38 -05002654 return 1;
dea31012005-04-17 16:05:31 -05002655}
2656
James Smarte59058c2008-08-24 21:49:00 -04002657/**
James Smart3621a712009-04-06 18:47:14 -04002658 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002659 * @phba: Pointer to HBA context object.
2660 * @pring: Pointer to driver SLI ring object.
2661 * @prspiocb: Pointer to response iocb object.
2662 *
2663 * This function looks up the iocb_lookup table to get the command iocb
2664 * corresponding to the given response iocb using the iotag of the
2665 * response iocb. This function is called with the hbalock held.
2666 * This function returns the command iocb object if it finds the command
2667 * iocb else returns NULL.
2668 **/
dea31012005-04-17 16:05:31 -05002669static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002670lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2671 struct lpfc_sli_ring *pring,
2672 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002673{
dea31012005-04-17 16:05:31 -05002674 struct lpfc_iocbq *cmd_iocb = NULL;
2675 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002676 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002677
James Bottomley604a3e32005-10-29 10:28:33 -05002678 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002679
James Bottomley604a3e32005-10-29 10:28:33 -05002680 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2681 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002682 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002683 /* remove from txcmpl queue list */
2684 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002685 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002686 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002687 }
dea31012005-04-17 16:05:31 -05002688 }
2689
dea31012005-04-17 16:05:31 -05002690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002691 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002692 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002693 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002694 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002695 return NULL;
2696}
2697
James Smarte59058c2008-08-24 21:49:00 -04002698/**
James Smart3772a992009-05-22 14:50:54 -04002699 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2700 * @phba: Pointer to HBA context object.
2701 * @pring: Pointer to driver SLI ring object.
2702 * @iotag: IOCB tag.
2703 *
2704 * This function looks up the iocb_lookup table to get the command iocb
2705 * corresponding to the given iotag. This function is called with the
2706 * hbalock held.
2707 * This function returns the command iocb object if it finds the command
2708 * iocb else returns NULL.
2709 **/
2710static struct lpfc_iocbq *
2711lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2712 struct lpfc_sli_ring *pring, uint16_t iotag)
2713{
2714 struct lpfc_iocbq *cmd_iocb;
2715
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002716 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002717 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2718 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002719 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2720 /* remove from txcmpl queue list */
2721 list_del_init(&cmd_iocb->list);
2722 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002723 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002724 }
James Smart3772a992009-05-22 14:50:54 -04002725 }
James Smart89533e92016-10-13 15:06:15 -07002726
James Smart3772a992009-05-22 14:50:54 -04002727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002728 "0372 iotag x%x is out of range: max iotag (x%x)\n",
James Smart3772a992009-05-22 14:50:54 -04002729 iotag, phba->sli.last_iotag);
2730 return NULL;
2731}
2732
2733/**
James Smart3621a712009-04-06 18:47:14 -04002734 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002735 * @phba: Pointer to HBA context object.
2736 * @pring: Pointer to driver SLI ring object.
2737 * @saveq: Pointer to the response iocb to be processed.
2738 *
2739 * This function is called by the ring event handler for non-fcp
2740 * rings when there is a new response iocb in the response ring.
2741 * The caller is not required to hold any locks. This function
2742 * gets the command iocb associated with the response iocb and
2743 * calls the completion handler for the command iocb. If there
2744 * is no completion handler, the function will free the resources
2745 * associated with command iocb. If the response iocb is for
2746 * an already aborted command iocb, the status of the completion
2747 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2748 * This function always returns 1.
2749 **/
dea31012005-04-17 16:05:31 -05002750static int
James Smart2e0fef82007-06-17 19:56:36 -05002751lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002752 struct lpfc_iocbq *saveq)
2753{
James Smart2e0fef82007-06-17 19:56:36 -05002754 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002755 int rc = 1;
2756 unsigned long iflag;
2757
2758 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002759 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002760 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002761 spin_unlock_irqrestore(&phba->hbalock, iflag);
2762
dea31012005-04-17 16:05:31 -05002763 if (cmdiocbp) {
2764 if (cmdiocbp->iocb_cmpl) {
2765 /*
James Smartea2151b2008-09-07 11:52:10 -04002766 * If an ELS command failed send an event to mgmt
2767 * application.
2768 */
2769 if (saveq->iocb.ulpStatus &&
2770 (pring->ringno == LPFC_ELS_RING) &&
2771 (cmdiocbp->iocb.ulpCommand ==
2772 CMD_ELS_REQUEST64_CR))
2773 lpfc_send_els_failure_event(phba,
2774 cmdiocbp, saveq);
2775
2776 /*
dea31012005-04-17 16:05:31 -05002777 * Post all ELS completions to the worker thread.
2778 * All other are passed to the completion callback.
2779 */
2780 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002781 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2782 (cmdiocbp->iocb_flag &
2783 LPFC_DRIVER_ABORTED)) {
2784 spin_lock_irqsave(&phba->hbalock,
2785 iflag);
James Smart07951072007-04-25 09:51:38 -04002786 cmdiocbp->iocb_flag &=
2787 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002788 spin_unlock_irqrestore(&phba->hbalock,
2789 iflag);
James Smart07951072007-04-25 09:51:38 -04002790 saveq->iocb.ulpStatus =
2791 IOSTAT_LOCAL_REJECT;
2792 saveq->iocb.un.ulpWord[4] =
2793 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002794
2795 /* Firmware could still be in progress
2796 * of DMAing payload, so don't free data
2797 * buffer till after a hbeat.
2798 */
James Smart341af102010-01-26 23:07:37 -05002799 spin_lock_irqsave(&phba->hbalock,
2800 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002801 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002802 spin_unlock_irqrestore(&phba->hbalock,
2803 iflag);
2804 }
James Smart0f65ff62010-02-26 14:14:23 -05002805 if (phba->sli_rev == LPFC_SLI_REV4) {
2806 if (saveq->iocb_flag &
2807 LPFC_EXCHANGE_BUSY) {
2808 /* Set cmdiocb flag for the
2809 * exchange busy so sgl (xri)
2810 * will not be released until
2811 * the abort xri is received
2812 * from hba.
2813 */
2814 spin_lock_irqsave(
2815 &phba->hbalock, iflag);
2816 cmdiocbp->iocb_flag |=
2817 LPFC_EXCHANGE_BUSY;
2818 spin_unlock_irqrestore(
2819 &phba->hbalock, iflag);
2820 }
2821 if (cmdiocbp->iocb_flag &
2822 LPFC_DRIVER_ABORTED) {
2823 /*
2824 * Clear LPFC_DRIVER_ABORTED
2825 * bit in case it was driver
2826 * initiated abort.
2827 */
2828 spin_lock_irqsave(
2829 &phba->hbalock, iflag);
2830 cmdiocbp->iocb_flag &=
2831 ~LPFC_DRIVER_ABORTED;
2832 spin_unlock_irqrestore(
2833 &phba->hbalock, iflag);
2834 cmdiocbp->iocb.ulpStatus =
2835 IOSTAT_LOCAL_REJECT;
2836 cmdiocbp->iocb.un.ulpWord[4] =
2837 IOERR_ABORT_REQUESTED;
2838 /*
2839 * For SLI4, irsiocb contains
2840 * NO_XRI in sli_xritag, it
2841 * shall not affect releasing
2842 * sgl (xri) process.
2843 */
2844 saveq->iocb.ulpStatus =
2845 IOSTAT_LOCAL_REJECT;
2846 saveq->iocb.un.ulpWord[4] =
2847 IOERR_SLI_ABORTED;
2848 spin_lock_irqsave(
2849 &phba->hbalock, iflag);
2850 saveq->iocb_flag |=
2851 LPFC_DELAY_MEM_FREE;
2852 spin_unlock_irqrestore(
2853 &phba->hbalock, iflag);
2854 }
James Smart07951072007-04-25 09:51:38 -04002855 }
dea31012005-04-17 16:05:31 -05002856 }
James Smart2e0fef82007-06-17 19:56:36 -05002857 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002858 } else
2859 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002860 } else {
2861 /*
2862 * Unknown initiating command based on the response iotag.
2863 * This could be the case on the ELS ring because of
2864 * lpfc_els_abort().
2865 */
2866 if (pring->ringno != LPFC_ELS_RING) {
2867 /*
2868 * Ring <ringno> handler: unexpected completion IoTag
2869 * <IoTag>
2870 */
James Smarta257bf92009-04-06 18:48:10 -04002871 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002872 "0322 Ring %d handler: "
2873 "unexpected completion IoTag x%x "
2874 "Data: x%x x%x x%x x%x\n",
2875 pring->ringno,
2876 saveq->iocb.ulpIoTag,
2877 saveq->iocb.ulpStatus,
2878 saveq->iocb.un.ulpWord[4],
2879 saveq->iocb.ulpCommand,
2880 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002881 }
2882 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002883
dea31012005-04-17 16:05:31 -05002884 return rc;
2885}
2886
James Smarte59058c2008-08-24 21:49:00 -04002887/**
James Smart3621a712009-04-06 18:47:14 -04002888 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002889 * @phba: Pointer to HBA context object.
2890 * @pring: Pointer to driver SLI ring object.
2891 *
2892 * This function is called from the iocb ring event handlers when
2893 * put pointer is ahead of the get pointer for a ring. This function signal
2894 * an error attention condition to the worker thread and the worker
2895 * thread will transition the HBA to offline state.
2896 **/
James Smart2e0fef82007-06-17 19:56:36 -05002897static void
2898lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002899{
James Smart34b02dc2008-08-24 21:49:55 -04002900 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002901 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002902 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002903 * rsp ring <portRspMax>
2904 */
2905 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002906 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002907 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002908 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002909 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002910
James Smart2e0fef82007-06-17 19:56:36 -05002911 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002912
2913 /*
2914 * All error attention handlers are posted to
2915 * worker thread
2916 */
2917 phba->work_ha |= HA_ERATT;
2918 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002919
James Smart5e9d9b82008-06-14 22:52:53 -04002920 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002921
2922 return;
2923}
2924
James Smarte59058c2008-08-24 21:49:00 -04002925/**
James Smart3621a712009-04-06 18:47:14 -04002926 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002927 * @ptr: Pointer to address of HBA context object.
2928 *
2929 * This function is invoked by the Error Attention polling timer when the
2930 * timer times out. It will check the SLI Error Attention register for
2931 * possible attention events. If so, it will post an Error Attention event
2932 * and wake up worker thread to process it. Otherwise, it will set up the
2933 * Error Attention polling timer for the next poll.
2934 **/
2935void lpfc_poll_eratt(unsigned long ptr)
2936{
2937 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04002938 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04002939 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002940
2941 phba = (struct lpfc_hba *)ptr;
2942
James Smartaa6fbb72012-08-03 12:36:03 -04002943 /* Here we will also keep track of interrupts per sec of the hba */
2944 sli_intr = phba->sli.slistat.sli_intr;
2945
2946 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2947 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2948 sli_intr);
2949 else
2950 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2951
James Smart65791f12016-07-06 12:35:56 -07002952 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2953 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04002954 phba->sli.slistat.sli_ips = cnt;
2955
2956 phba->sli.slistat.sli_prev_intr = sli_intr;
2957
James Smart93996272008-08-24 21:50:30 -04002958 /* Check chip HA register for error event */
2959 eratt = lpfc_sli_check_eratt(phba);
2960
2961 if (eratt)
2962 /* Tell the worker thread there is work to do */
2963 lpfc_worker_wake_up(phba);
2964 else
2965 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002966 mod_timer(&phba->eratt_poll,
2967 jiffies +
James Smart65791f12016-07-06 12:35:56 -07002968 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04002969 return;
2970}
2971
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002972
James Smarte59058c2008-08-24 21:49:00 -04002973/**
James Smart3621a712009-04-06 18:47:14 -04002974 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002975 * @phba: Pointer to HBA context object.
2976 * @pring: Pointer to driver SLI ring object.
2977 * @mask: Host attention register mask for this ring.
2978 *
2979 * This function is called from the interrupt context when there is a ring
2980 * event for the fcp ring. The caller does not hold any lock.
2981 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002982 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002983 * LE bit set. The function will call the completion handler of the command iocb
2984 * if the response iocb indicates a completion for a command iocb or it is
2985 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2986 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002987 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002988 * to check it explicitly.
2989 */
2990int
James Smart2e0fef82007-06-17 19:56:36 -05002991lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2992 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002993{
James Smart34b02dc2008-08-24 21:49:55 -04002994 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002995 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002996 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002997 struct lpfc_iocbq *cmdiocbq = NULL;
2998 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002999 uint32_t status;
3000 uint32_t portRspPut, portRspMax;
3001 int rc = 1;
3002 lpfc_iocb_type type;
3003 unsigned long iflag;
3004 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003005
James Smart2e0fef82007-06-17 19:56:36 -05003006 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003007 pring->stats.iocb_event++;
3008
dea31012005-04-17 16:05:31 -05003009 /*
3010 * The next available response entry should never exceed the maximum
3011 * entries. If it does, treat it as an adapter hardware error.
3012 */
James Smart7e56aa22012-08-03 12:35:34 -04003013 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003014 portRspPut = le32_to_cpu(pgp->rspPutInx);
3015 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003016 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003017 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003018 return 1;
3019 }
James Smart45ed1192009-10-02 15:17:02 -04003020 if (phba->fcp_ring_in_use) {
3021 spin_unlock_irqrestore(&phba->hbalock, iflag);
3022 return 1;
3023 } else
3024 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003025
3026 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003027 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003028 /*
3029 * Fetch an entry off the ring and copy it into a local data
3030 * structure. The copy involves a byte-swap since the
3031 * network byte order and pci byte orders are different.
3032 */
James Smarted957682007-06-17 19:56:37 -05003033 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003034 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003035
James Smart7e56aa22012-08-03 12:35:34 -04003036 if (++pring->sli.sli3.rspidx >= portRspMax)
3037 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003038
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003039 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3040 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003041 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003042 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003043 irsp = &rspiocbq.iocb;
3044
dea31012005-04-17 16:05:31 -05003045 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3046 pring->stats.iocb_rsp++;
3047 rsp_cmpl++;
3048
3049 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003050 /*
3051 * If resource errors reported from HBA, reduce
3052 * queuedepths of the SCSI device.
3053 */
3054 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003055 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3056 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003057 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003058 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003059 spin_lock_irqsave(&phba->hbalock, iflag);
3060 }
3061
dea31012005-04-17 16:05:31 -05003062 /* Rsp ring <ringno> error: IOCB */
3063 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003064 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003065 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003066 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003067 irsp->un.ulpWord[0],
3068 irsp->un.ulpWord[1],
3069 irsp->un.ulpWord[2],
3070 irsp->un.ulpWord[3],
3071 irsp->un.ulpWord[4],
3072 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003073 *(uint32_t *)&irsp->un1,
3074 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003075 }
3076
3077 switch (type) {
3078 case LPFC_ABORT_IOCB:
3079 case LPFC_SOL_IOCB:
3080 /*
3081 * Idle exchange closed via ABTS from port. No iocb
3082 * resources need to be recovered.
3083 */
3084 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003085 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003086 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003087 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003088 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003089 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003090 break;
3091 }
3092
James Bottomley604a3e32005-10-29 10:28:33 -05003093 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3094 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003095 if (unlikely(!cmdiocbq))
3096 break;
3097 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3098 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3099 if (cmdiocbq->iocb_cmpl) {
3100 spin_unlock_irqrestore(&phba->hbalock, iflag);
3101 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3102 &rspiocbq);
3103 spin_lock_irqsave(&phba->hbalock, iflag);
3104 }
dea31012005-04-17 16:05:31 -05003105 break;
James Smarta4bc3372006-12-02 13:34:16 -05003106 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003107 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003108 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003109 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003110 break;
dea31012005-04-17 16:05:31 -05003111 default:
3112 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3113 char adaptermsg[LPFC_MAX_ADPTMSG];
3114 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3115 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3116 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003117 dev_warn(&((phba->pcidev)->dev),
3118 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003119 phba->brd_no, adaptermsg);
3120 } else {
3121 /* Unknown IOCB command */
3122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003123 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003124 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003125 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003126 irsp->ulpStatus,
3127 irsp->ulpIoTag,
3128 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003129 }
3130 break;
3131 }
3132
3133 /*
3134 * The response IOCB has been processed. Update the ring
3135 * pointer in SLIM. If the port response put pointer has not
3136 * been updated, sync the pgp->rspPutInx and fetch the new port
3137 * response put pointer.
3138 */
James Smart7e56aa22012-08-03 12:35:34 -04003139 writel(pring->sli.sli3.rspidx,
3140 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003141
James Smart7e56aa22012-08-03 12:35:34 -04003142 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003143 portRspPut = le32_to_cpu(pgp->rspPutInx);
3144 }
3145
3146 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3147 pring->stats.iocb_rsp_full++;
3148 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3149 writel(status, phba->CAregaddr);
3150 readl(phba->CAregaddr);
3151 }
3152 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3153 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3154 pring->stats.iocb_cmd_empty++;
3155
3156 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003157 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003158 lpfc_sli_resume_iocb(phba, pring);
3159
3160 if ((pring->lpfc_sli_cmd_available))
3161 (pring->lpfc_sli_cmd_available) (phba, pring);
3162
3163 }
3164
James Smart45ed1192009-10-02 15:17:02 -04003165 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003166 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003167 return rc;
3168}
3169
James Smarte59058c2008-08-24 21:49:00 -04003170/**
James Smart3772a992009-05-22 14:50:54 -04003171 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3172 * @phba: Pointer to HBA context object.
3173 * @pring: Pointer to driver SLI ring object.
3174 * @rspiocbp: Pointer to driver response IOCB object.
3175 *
3176 * This function is called from the worker thread when there is a slow-path
3177 * response IOCB to process. This function chains all the response iocbs until
3178 * seeing the iocb with the LE bit set. The function will call
3179 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3180 * completion of a command iocb. The function will call the
3181 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3182 * The function frees the resources or calls the completion handler if this
3183 * iocb is an abort completion. The function returns NULL when the response
3184 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3185 * this function shall chain the iocb on to the iocb_continueq and return the
3186 * response iocb passed in.
3187 **/
3188static struct lpfc_iocbq *
3189lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3190 struct lpfc_iocbq *rspiocbp)
3191{
3192 struct lpfc_iocbq *saveq;
3193 struct lpfc_iocbq *cmdiocbp;
3194 struct lpfc_iocbq *next_iocb;
3195 IOCB_t *irsp = NULL;
3196 uint32_t free_saveq;
3197 uint8_t iocb_cmd_type;
3198 lpfc_iocb_type type;
3199 unsigned long iflag;
3200 int rc;
3201
3202 spin_lock_irqsave(&phba->hbalock, iflag);
3203 /* First add the response iocb to the countinueq list */
3204 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3205 pring->iocb_continueq_cnt++;
3206
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003207 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003208 irsp = &rspiocbp->iocb;
3209 if (irsp->ulpLe) {
3210 /*
3211 * By default, the driver expects to free all resources
3212 * associated with this iocb completion.
3213 */
3214 free_saveq = 1;
3215 saveq = list_get_first(&pring->iocb_continueq,
3216 struct lpfc_iocbq, list);
3217 irsp = &(saveq->iocb);
3218 list_del_init(&pring->iocb_continueq);
3219 pring->iocb_continueq_cnt = 0;
3220
3221 pring->stats.iocb_rsp++;
3222
3223 /*
3224 * If resource errors reported from HBA, reduce
3225 * queuedepths of the SCSI device.
3226 */
3227 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003228 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3229 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003230 spin_unlock_irqrestore(&phba->hbalock, iflag);
3231 phba->lpfc_rampdown_queue_depth(phba);
3232 spin_lock_irqsave(&phba->hbalock, iflag);
3233 }
3234
3235 if (irsp->ulpStatus) {
3236 /* Rsp ring <ringno> error: IOCB */
3237 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3238 "0328 Rsp Ring %d error: "
3239 "IOCB Data: "
3240 "x%x x%x x%x x%x "
3241 "x%x x%x x%x x%x "
3242 "x%x x%x x%x x%x "
3243 "x%x x%x x%x x%x\n",
3244 pring->ringno,
3245 irsp->un.ulpWord[0],
3246 irsp->un.ulpWord[1],
3247 irsp->un.ulpWord[2],
3248 irsp->un.ulpWord[3],
3249 irsp->un.ulpWord[4],
3250 irsp->un.ulpWord[5],
3251 *(((uint32_t *) irsp) + 6),
3252 *(((uint32_t *) irsp) + 7),
3253 *(((uint32_t *) irsp) + 8),
3254 *(((uint32_t *) irsp) + 9),
3255 *(((uint32_t *) irsp) + 10),
3256 *(((uint32_t *) irsp) + 11),
3257 *(((uint32_t *) irsp) + 12),
3258 *(((uint32_t *) irsp) + 13),
3259 *(((uint32_t *) irsp) + 14),
3260 *(((uint32_t *) irsp) + 15));
3261 }
3262
3263 /*
3264 * Fetch the IOCB command type and call the correct completion
3265 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3266 * get freed back to the lpfc_iocb_list by the discovery
3267 * kernel thread.
3268 */
3269 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3270 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3271 switch (type) {
3272 case LPFC_SOL_IOCB:
3273 spin_unlock_irqrestore(&phba->hbalock, iflag);
3274 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3275 spin_lock_irqsave(&phba->hbalock, iflag);
3276 break;
3277
3278 case LPFC_UNSOL_IOCB:
3279 spin_unlock_irqrestore(&phba->hbalock, iflag);
3280 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3281 spin_lock_irqsave(&phba->hbalock, iflag);
3282 if (!rc)
3283 free_saveq = 0;
3284 break;
3285
3286 case LPFC_ABORT_IOCB:
3287 cmdiocbp = NULL;
3288 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3289 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3290 saveq);
3291 if (cmdiocbp) {
3292 /* Call the specified completion routine */
3293 if (cmdiocbp->iocb_cmpl) {
3294 spin_unlock_irqrestore(&phba->hbalock,
3295 iflag);
3296 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3297 saveq);
3298 spin_lock_irqsave(&phba->hbalock,
3299 iflag);
3300 } else
3301 __lpfc_sli_release_iocbq(phba,
3302 cmdiocbp);
3303 }
3304 break;
3305
3306 case LPFC_UNKNOWN_IOCB:
3307 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3308 char adaptermsg[LPFC_MAX_ADPTMSG];
3309 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3310 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3311 MAX_MSG_DATA);
3312 dev_warn(&((phba->pcidev)->dev),
3313 "lpfc%d: %s\n",
3314 phba->brd_no, adaptermsg);
3315 } else {
3316 /* Unknown IOCB command */
3317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3318 "0335 Unknown IOCB "
3319 "command Data: x%x "
3320 "x%x x%x x%x\n",
3321 irsp->ulpCommand,
3322 irsp->ulpStatus,
3323 irsp->ulpIoTag,
3324 irsp->ulpContext);
3325 }
3326 break;
3327 }
3328
3329 if (free_saveq) {
3330 list_for_each_entry_safe(rspiocbp, next_iocb,
3331 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003332 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003333 __lpfc_sli_release_iocbq(phba, rspiocbp);
3334 }
3335 __lpfc_sli_release_iocbq(phba, saveq);
3336 }
3337 rspiocbp = NULL;
3338 }
3339 spin_unlock_irqrestore(&phba->hbalock, iflag);
3340 return rspiocbp;
3341}
3342
3343/**
3344 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003345 * @phba: Pointer to HBA context object.
3346 * @pring: Pointer to driver SLI ring object.
3347 * @mask: Host attention register mask for this ring.
3348 *
James Smart3772a992009-05-22 14:50:54 -04003349 * This routine wraps the actual slow_ring event process routine from the
3350 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003351 **/
James Smart3772a992009-05-22 14:50:54 -04003352void
James Smart2e0fef82007-06-17 19:56:36 -05003353lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3354 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003355{
James Smart3772a992009-05-22 14:50:54 -04003356 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3357}
3358
3359/**
3360 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3361 * @phba: Pointer to HBA context object.
3362 * @pring: Pointer to driver SLI ring object.
3363 * @mask: Host attention register mask for this ring.
3364 *
3365 * This function is called from the worker thread when there is a ring event
3366 * for non-fcp rings. The caller does not hold any lock. The function will
3367 * remove each response iocb in the response ring and calls the handle
3368 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3369 **/
3370static void
3371lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3372 struct lpfc_sli_ring *pring, uint32_t mask)
3373{
James Smart34b02dc2008-08-24 21:49:55 -04003374 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003375 IOCB_t *entry;
3376 IOCB_t *irsp = NULL;
3377 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003378 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003379 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003380 uint32_t status;
dea31012005-04-17 16:05:31 -05003381
James Smart34b02dc2008-08-24 21:49:55 -04003382 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003383 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003384 pring->stats.iocb_event++;
3385
dea31012005-04-17 16:05:31 -05003386 /*
3387 * The next available response entry should never exceed the maximum
3388 * entries. If it does, treat it as an adapter hardware error.
3389 */
James Smart7e56aa22012-08-03 12:35:34 -04003390 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003391 portRspPut = le32_to_cpu(pgp->rspPutInx);
3392 if (portRspPut >= portRspMax) {
3393 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003394 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003395 * rsp ring <portRspMax>
3396 */
James Smarted957682007-06-17 19:56:37 -05003397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003398 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003399 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003400 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003401
James Smart2e0fef82007-06-17 19:56:36 -05003402 phba->link_state = LPFC_HBA_ERROR;
3403 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003404
3405 phba->work_hs = HS_FFER3;
3406 lpfc_handle_eratt(phba);
3407
James Smart3772a992009-05-22 14:50:54 -04003408 return;
dea31012005-04-17 16:05:31 -05003409 }
3410
3411 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003412 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003413 /*
3414 * Build a completion list and call the appropriate handler.
3415 * The process is to get the next available response iocb, get
3416 * a free iocb from the list, copy the response data into the
3417 * free iocb, insert to the continuation list, and update the
3418 * next response index to slim. This process makes response
3419 * iocb's in the ring available to DMA as fast as possible but
3420 * pays a penalty for a copy operation. Since the iocb is
3421 * only 32 bytes, this penalty is considered small relative to
3422 * the PCI reads for register values and a slim write. When
3423 * the ulpLe field is set, the entire Command has been
3424 * received.
3425 */
James Smarted957682007-06-17 19:56:37 -05003426 entry = lpfc_resp_iocb(phba, pring);
3427
James Smart858c9f62007-06-17 19:56:39 -05003428 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003429 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003430 if (rspiocbp == NULL) {
3431 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003432 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003433 break;
3434 }
3435
James Smarted957682007-06-17 19:56:37 -05003436 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3437 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003438 irsp = &rspiocbp->iocb;
3439
James Smart7e56aa22012-08-03 12:35:34 -04003440 if (++pring->sli.sli3.rspidx >= portRspMax)
3441 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003442
James Smarta58cbd52007-08-02 11:09:43 -04003443 if (pring->ringno == LPFC_ELS_RING) {
3444 lpfc_debugfs_slow_ring_trc(phba,
3445 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3446 *(((uint32_t *) irsp) + 4),
3447 *(((uint32_t *) irsp) + 6),
3448 *(((uint32_t *) irsp) + 7));
3449 }
3450
James Smart7e56aa22012-08-03 12:35:34 -04003451 writel(pring->sli.sli3.rspidx,
3452 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003453
James Smart3772a992009-05-22 14:50:54 -04003454 spin_unlock_irqrestore(&phba->hbalock, iflag);
3455 /* Handle the response IOCB */
3456 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3457 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003458
3459 /*
3460 * If the port response put pointer has not been updated, sync
3461 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3462 * response put pointer.
3463 */
James Smart7e56aa22012-08-03 12:35:34 -04003464 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003465 portRspPut = le32_to_cpu(pgp->rspPutInx);
3466 }
James Smart7e56aa22012-08-03 12:35:34 -04003467 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003468
James Smart92d7f7b2007-06-17 19:56:38 -05003469 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003470 /* At least one response entry has been freed */
3471 pring->stats.iocb_rsp_full++;
3472 /* SET RxRE_RSP in Chip Att register */
3473 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3474 writel(status, phba->CAregaddr);
3475 readl(phba->CAregaddr); /* flush */
3476 }
3477 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3478 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3479 pring->stats.iocb_cmd_empty++;
3480
3481 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003482 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003483 lpfc_sli_resume_iocb(phba, pring);
3484
3485 if ((pring->lpfc_sli_cmd_available))
3486 (pring->lpfc_sli_cmd_available) (phba, pring);
3487
3488 }
3489
James Smart2e0fef82007-06-17 19:56:36 -05003490 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003491 return;
dea31012005-04-17 16:05:31 -05003492}
3493
James Smarte59058c2008-08-24 21:49:00 -04003494/**
James Smart4f774512009-05-22 14:52:35 -04003495 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3496 * @phba: Pointer to HBA context object.
3497 * @pring: Pointer to driver SLI ring object.
3498 * @mask: Host attention register mask for this ring.
3499 *
3500 * This function is called from the worker thread when there is a pending
3501 * ELS response iocb on the driver internal slow-path response iocb worker
3502 * queue. The caller does not hold any lock. The function will remove each
3503 * response iocb from the response worker queue and calls the handle
3504 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3505 **/
3506static void
3507lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3508 struct lpfc_sli_ring *pring, uint32_t mask)
3509{
3510 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003511 struct hbq_dmabuf *dmabuf;
3512 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003513 unsigned long iflag;
3514
James Smart45ed1192009-10-02 15:17:02 -04003515 spin_lock_irqsave(&phba->hbalock, iflag);
3516 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3517 spin_unlock_irqrestore(&phba->hbalock, iflag);
3518 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003519 /* Get the response iocb from the head of work queue */
3520 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003521 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003522 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003523 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003524
3525 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3526 case CQE_CODE_COMPL_WQE:
3527 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3528 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003529 /* Translate ELS WCQE to response IOCBQ */
3530 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3531 irspiocbq);
3532 if (irspiocbq)
3533 lpfc_sli_sp_handle_rspiocb(phba, pring,
3534 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003535 break;
3536 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003537 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003538 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3539 cq_event);
3540 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3541 break;
3542 default:
3543 break;
3544 }
James Smart4f774512009-05-22 14:52:35 -04003545 }
3546}
3547
3548/**
James Smart3621a712009-04-06 18:47:14 -04003549 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003550 * @phba: Pointer to HBA context object.
3551 * @pring: Pointer to driver SLI ring object.
3552 *
3553 * This function aborts all iocbs in the given ring and frees all the iocb
3554 * objects in txq. This function issues an abort iocb for all the iocb commands
3555 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3556 * the return of this function. The caller is not required to hold any locks.
3557 **/
James Smart2e0fef82007-06-17 19:56:36 -05003558void
dea31012005-04-17 16:05:31 -05003559lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3560{
James Smart2534ba72007-04-25 09:52:20 -04003561 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003562 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003563
James Smart92d7f7b2007-06-17 19:56:38 -05003564 if (pring->ringno == LPFC_ELS_RING) {
3565 lpfc_fabric_abort_hba(phba);
3566 }
3567
dea31012005-04-17 16:05:31 -05003568 /* Error everything on txq and txcmplq
3569 * First do the txq.
3570 */
James Smartdb55fba2014-04-04 13:52:02 -04003571 if (phba->sli_rev >= LPFC_SLI_REV4) {
3572 spin_lock_irq(&pring->ring_lock);
3573 list_splice_init(&pring->txq, &completions);
3574 pring->txq_cnt = 0;
3575 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003576
James Smartdb55fba2014-04-04 13:52:02 -04003577 spin_lock_irq(&phba->hbalock);
3578 /* Next issue ABTS for everything on the txcmplq */
3579 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3580 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3581 spin_unlock_irq(&phba->hbalock);
3582 } else {
3583 spin_lock_irq(&phba->hbalock);
3584 list_splice_init(&pring->txq, &completions);
3585 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003586
James Smartdb55fba2014-04-04 13:52:02 -04003587 /* Next issue ABTS for everything on the txcmplq */
3588 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3589 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3590 spin_unlock_irq(&phba->hbalock);
3591 }
James Smart2534ba72007-04-25 09:52:20 -04003592
James Smarta257bf92009-04-06 18:48:10 -04003593 /* Cancel all the IOCBs from the completions list */
3594 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3595 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003596}
3597
James Smarte59058c2008-08-24 21:49:00 -04003598/**
James Smartdb55fba2014-04-04 13:52:02 -04003599 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3600 * @phba: Pointer to HBA context object.
3601 * @pring: Pointer to driver SLI ring object.
3602 *
3603 * This function aborts all iocbs in FCP rings and frees all the iocb
3604 * objects in txq. This function issues an abort iocb for all the iocb commands
3605 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3606 * the return of this function. The caller is not required to hold any locks.
3607 **/
3608void
3609lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3610{
3611 struct lpfc_sli *psli = &phba->sli;
3612 struct lpfc_sli_ring *pring;
3613 uint32_t i;
3614
3615 /* Look on all the FCP Rings for the iotag */
3616 if (phba->sli_rev >= LPFC_SLI_REV4) {
3617 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3618 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3619 lpfc_sli_abort_iocb_ring(phba, pring);
3620 }
3621 } else {
3622 pring = &psli->ring[psli->fcp_ring];
3623 lpfc_sli_abort_iocb_ring(phba, pring);
3624 }
3625}
3626
3627
3628/**
James Smart3621a712009-04-06 18:47:14 -04003629 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003630 * @phba: Pointer to HBA context object.
3631 *
3632 * This function flushes all iocbs in the fcp ring and frees all the iocb
3633 * objects in txq and txcmplq. This function will not issue abort iocbs
3634 * for all the iocb commands in txcmplq, they will just be returned with
3635 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3636 * slot has been permanently disabled.
3637 **/
3638void
3639lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3640{
3641 LIST_HEAD(txq);
3642 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003643 struct lpfc_sli *psli = &phba->sli;
3644 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003645 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003646
3647 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003648 /* Indicate the I/O queues are flushed */
3649 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003650 spin_unlock_irq(&phba->hbalock);
3651
James Smartdb55fba2014-04-04 13:52:02 -04003652 /* Look on all the FCP Rings for the iotag */
3653 if (phba->sli_rev >= LPFC_SLI_REV4) {
3654 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3655 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
James Smarta8e497d2008-08-24 21:50:11 -04003656
James Smartdb55fba2014-04-04 13:52:02 -04003657 spin_lock_irq(&pring->ring_lock);
3658 /* Retrieve everything on txq */
3659 list_splice_init(&pring->txq, &txq);
3660 /* Retrieve everything on the txcmplq */
3661 list_splice_init(&pring->txcmplq, &txcmplq);
3662 pring->txq_cnt = 0;
3663 pring->txcmplq_cnt = 0;
3664 spin_unlock_irq(&pring->ring_lock);
3665
3666 /* Flush the txq */
3667 lpfc_sli_cancel_iocbs(phba, &txq,
3668 IOSTAT_LOCAL_REJECT,
3669 IOERR_SLI_DOWN);
3670 /* Flush the txcmpq */
3671 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3672 IOSTAT_LOCAL_REJECT,
3673 IOERR_SLI_DOWN);
3674 }
3675 } else {
3676 pring = &psli->ring[psli->fcp_ring];
3677
3678 spin_lock_irq(&phba->hbalock);
3679 /* Retrieve everything on txq */
3680 list_splice_init(&pring->txq, &txq);
3681 /* Retrieve everything on the txcmplq */
3682 list_splice_init(&pring->txcmplq, &txcmplq);
3683 pring->txq_cnt = 0;
3684 pring->txcmplq_cnt = 0;
3685 spin_unlock_irq(&phba->hbalock);
3686
3687 /* Flush the txq */
3688 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3689 IOERR_SLI_DOWN);
3690 /* Flush the txcmpq */
3691 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3692 IOERR_SLI_DOWN);
3693 }
James Smarta8e497d2008-08-24 21:50:11 -04003694}
3695
3696/**
James Smart3772a992009-05-22 14:50:54 -04003697 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003698 * @phba: Pointer to HBA context object.
3699 * @mask: Bit mask to be checked.
3700 *
3701 * This function reads the host status register and compares
3702 * with the provided bit mask to check if HBA completed
3703 * the restart. This function will wait in a loop for the
3704 * HBA to complete restart. If the HBA does not restart within
3705 * 15 iterations, the function will reset the HBA again. The
3706 * function returns 1 when HBA fail to restart otherwise returns
3707 * zero.
3708 **/
James Smart3772a992009-05-22 14:50:54 -04003709static int
3710lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003711{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003712 uint32_t status;
3713 int i = 0;
3714 int retval = 0;
dea31012005-04-17 16:05:31 -05003715
Jamie Wellnitz41415862006-02-28 19:25:27 -05003716 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003717 if (lpfc_readl(phba->HSregaddr, &status))
3718 return 1;
dea31012005-04-17 16:05:31 -05003719
Jamie Wellnitz41415862006-02-28 19:25:27 -05003720 /*
3721 * Check status register every 100ms for 5 retries, then every
3722 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3723 * every 2.5 sec for 4.
3724 * Break our of the loop if errors occurred during init.
3725 */
3726 while (((status & mask) != mask) &&
3727 !(status & HS_FFERM) &&
3728 i++ < 20) {
dea31012005-04-17 16:05:31 -05003729
Jamie Wellnitz41415862006-02-28 19:25:27 -05003730 if (i <= 5)
3731 msleep(10);
3732 else if (i <= 10)
3733 msleep(500);
3734 else
3735 msleep(2500);
dea31012005-04-17 16:05:31 -05003736
Jamie Wellnitz41415862006-02-28 19:25:27 -05003737 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003738 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003739 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003740 lpfc_sli_brdrestart(phba);
3741 }
3742 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003743 if (lpfc_readl(phba->HSregaddr, &status)) {
3744 retval = 1;
3745 break;
3746 }
dea31012005-04-17 16:05:31 -05003747 }
dea31012005-04-17 16:05:31 -05003748
Jamie Wellnitz41415862006-02-28 19:25:27 -05003749 /* Check to see if any errors occurred during init */
3750 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3752 "2751 Adapter failed to restart, "
3753 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3754 status,
3755 readl(phba->MBslimaddr + 0xa8),
3756 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003757 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003758 retval = 1;
3759 }
dea31012005-04-17 16:05:31 -05003760
Jamie Wellnitz41415862006-02-28 19:25:27 -05003761 return retval;
dea31012005-04-17 16:05:31 -05003762}
3763
James Smartda0436e2009-05-22 14:51:39 -04003764/**
3765 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3766 * @phba: Pointer to HBA context object.
3767 * @mask: Bit mask to be checked.
3768 *
3769 * This function checks the host status register to check if HBA is
3770 * ready. This function will wait in a loop for the HBA to be ready
3771 * If the HBA is not ready , the function will will reset the HBA PCI
3772 * function again. The function returns 1 when HBA fail to be ready
3773 * otherwise returns zero.
3774 **/
3775static int
3776lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3777{
3778 uint32_t status;
3779 int retval = 0;
3780
3781 /* Read the HBA Host Status Register */
3782 status = lpfc_sli4_post_status_check(phba);
3783
3784 if (status) {
3785 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3786 lpfc_sli_brdrestart(phba);
3787 status = lpfc_sli4_post_status_check(phba);
3788 }
3789
3790 /* Check to see if any errors occurred during init */
3791 if (status) {
3792 phba->link_state = LPFC_HBA_ERROR;
3793 retval = 1;
3794 } else
3795 phba->sli4_hba.intr_enable = 0;
3796
3797 return retval;
3798}
3799
3800/**
3801 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3802 * @phba: Pointer to HBA context object.
3803 * @mask: Bit mask to be checked.
3804 *
3805 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3806 * from the API jump table function pointer from the lpfc_hba struct.
3807 **/
3808int
3809lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3810{
3811 return phba->lpfc_sli_brdready(phba, mask);
3812}
3813
James Smart92908312006-03-07 15:04:13 -05003814#define BARRIER_TEST_PATTERN (0xdeadbeef)
3815
James Smarte59058c2008-08-24 21:49:00 -04003816/**
James Smart3621a712009-04-06 18:47:14 -04003817 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003818 * @phba: Pointer to HBA context object.
3819 *
James Smart1b511972011-12-13 13:23:09 -05003820 * This function is called before resetting an HBA. This function is called
3821 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003822 **/
James Smart2e0fef82007-06-17 19:56:36 -05003823void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003824{
James Smart65a29c12006-07-06 15:50:50 -04003825 uint32_t __iomem *resp_buf;
3826 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003827 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003828 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003829 int i;
3830 uint8_t hdrtype;
3831
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003832 lockdep_assert_held(&phba->hbalock);
3833
James Smart92908312006-03-07 15:04:13 -05003834 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3835 if (hdrtype != 0x80 ||
3836 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3837 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3838 return;
3839
3840 /*
3841 * Tell the other part of the chip to suspend temporarily all
3842 * its DMA activity.
3843 */
James Smart65a29c12006-07-06 15:50:50 -04003844 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003845
3846 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003847 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3848 return;
James Smart92908312006-03-07 15:04:13 -05003849 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3850 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003851 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003852
James Smart9940b972011-03-11 16:06:12 -05003853 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3854 return;
3855 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003856 /* Clear Chip error bit */
3857 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003858 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003859 }
3860
3861 mbox = 0;
3862 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3863 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3864
3865 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003866 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003867 writel(mbox, mbox_buf);
3868
James Smart9940b972011-03-11 16:06:12 -05003869 for (i = 0; i < 50; i++) {
3870 if (lpfc_readl((resp_buf + 1), &resp_data))
3871 return;
3872 if (resp_data != ~(BARRIER_TEST_PATTERN))
3873 mdelay(1);
3874 else
3875 break;
3876 }
3877 resp_data = 0;
3878 if (lpfc_readl((resp_buf + 1), &resp_data))
3879 return;
3880 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003881 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003882 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003883 goto restore_hc;
3884 else
3885 goto clear_errat;
3886 }
3887
3888 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003889 resp_data = 0;
3890 for (i = 0; i < 500; i++) {
3891 if (lpfc_readl(resp_buf, &resp_data))
3892 return;
3893 if (resp_data != mbox)
3894 mdelay(1);
3895 else
3896 break;
3897 }
James Smart92908312006-03-07 15:04:13 -05003898
3899clear_errat:
3900
James Smart9940b972011-03-11 16:06:12 -05003901 while (++i < 500) {
3902 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3903 return;
3904 if (!(ha_copy & HA_ERATT))
3905 mdelay(1);
3906 else
3907 break;
3908 }
James Smart92908312006-03-07 15:04:13 -05003909
3910 if (readl(phba->HAregaddr) & HA_ERATT) {
3911 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003912 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003913 }
3914
3915restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003916 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003917 writel(hc_copy, phba->HCregaddr);
3918 readl(phba->HCregaddr); /* flush */
3919}
3920
James Smarte59058c2008-08-24 21:49:00 -04003921/**
James Smart3621a712009-04-06 18:47:14 -04003922 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003923 * @phba: Pointer to HBA context object.
3924 *
3925 * This function issues a kill_board mailbox command and waits for
3926 * the error attention interrupt. This function is called for stopping
3927 * the firmware processing. The caller is not required to hold any
3928 * locks. This function calls lpfc_hba_down_post function to free
3929 * any pending commands after the kill. The function will return 1 when it
3930 * fails to kill the board else will return 0.
3931 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003932int
James Smart2e0fef82007-06-17 19:56:36 -05003933lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003934{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003935 struct lpfc_sli *psli;
3936 LPFC_MBOXQ_t *pmb;
3937 uint32_t status;
3938 uint32_t ha_copy;
3939 int retval;
3940 int i = 0;
3941
3942 psli = &phba->sli;
3943
3944 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003945 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003946 "0329 Kill HBA Data: x%x x%x\n",
3947 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003948
James Smart98c9ea52007-10-27 13:37:33 -04003949 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3950 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003951 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952
3953 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003954 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003955 if (lpfc_readl(phba->HCregaddr, &status)) {
3956 spin_unlock_irq(&phba->hbalock);
3957 mempool_free(pmb, phba->mbox_mem_pool);
3958 return 1;
3959 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003960 status &= ~HC_ERINT_ENA;
3961 writel(status, phba->HCregaddr);
3962 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003963 phba->link_flag |= LS_IGNORE_ERATT;
3964 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003965
3966 lpfc_kill_board(phba, pmb);
3967 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3968 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3969
3970 if (retval != MBX_SUCCESS) {
3971 if (retval != MBX_BUSY)
3972 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003973 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3974 "2752 KILL_BOARD command failed retval %d\n",
3975 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003976 spin_lock_irq(&phba->hbalock);
3977 phba->link_flag &= ~LS_IGNORE_ERATT;
3978 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003979 return 1;
3980 }
3981
James Smartf4b4c682009-05-22 14:53:12 -04003982 spin_lock_irq(&phba->hbalock);
3983 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3984 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003985
Jamie Wellnitz41415862006-02-28 19:25:27 -05003986 mempool_free(pmb, phba->mbox_mem_pool);
3987
3988 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3989 * attention every 100ms for 3 seconds. If we don't get ERATT after
3990 * 3 seconds we still set HBA_ERROR state because the status of the
3991 * board is now undefined.
3992 */
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 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3996 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003997 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3998 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003999 }
4000
4001 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004002 if (ha_copy & HA_ERATT) {
4003 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004004 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004005 }
James Smart2e0fef82007-06-17 19:56:36 -05004006 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004007 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004008 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004009 phba->link_flag &= ~LS_IGNORE_ERATT;
4010 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004011
Jamie Wellnitz41415862006-02-28 19:25:27 -05004012 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004013 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004014
James Smart2e0fef82007-06-17 19:56:36 -05004015 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004016}
4017
James Smarte59058c2008-08-24 21:49:00 -04004018/**
James Smart3772a992009-05-22 14:50:54 -04004019 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004020 * @phba: Pointer to HBA context object.
4021 *
4022 * This function resets the HBA by writing HC_INITFF to the control
4023 * register. After the HBA resets, this function resets all the iocb ring
4024 * indices. This function disables PCI layer parity checking during
4025 * the reset.
4026 * This function returns 0 always.
4027 * The caller is not required to hold any locks.
4028 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004029int
James Smart2e0fef82007-06-17 19:56:36 -05004030lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004031{
4032 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004033 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004034 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004035 int i;
dea31012005-04-17 16:05:31 -05004036
Jamie Wellnitz41415862006-02-28 19:25:27 -05004037 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004038
Jamie Wellnitz41415862006-02-28 19:25:27 -05004039 /* Reset HBA */
4040 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004041 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004042 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004043
4044 /* perform board reset */
4045 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004046 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004047 phba->pport->fc_myDID = 0;
4048 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004049
Jamie Wellnitz41415862006-02-28 19:25:27 -05004050 /* Turn off parity checking and serr during the physical reset */
4051 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4052 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4053 (cfg_value &
4054 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4055
James Smart3772a992009-05-22 14:50:54 -04004056 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4057
Jamie Wellnitz41415862006-02-28 19:25:27 -05004058 /* Now toggle INITFF bit in the Host Control Register */
4059 writel(HC_INITFF, phba->HCregaddr);
4060 mdelay(1);
4061 readl(phba->HCregaddr); /* flush */
4062 writel(0, phba->HCregaddr);
4063 readl(phba->HCregaddr); /* flush */
4064
4065 /* Restore PCI cmd register */
4066 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004067
4068 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004069 for (i = 0; i < psli->num_rings; i++) {
4070 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05004071 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004072 pring->sli.sli3.rspidx = 0;
4073 pring->sli.sli3.next_cmdidx = 0;
4074 pring->sli.sli3.local_getidx = 0;
4075 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004076 pring->missbufcnt = 0;
4077 }
dea31012005-04-17 16:05:31 -05004078
James Smart2e0fef82007-06-17 19:56:36 -05004079 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004080 return 0;
4081}
4082
James Smarte59058c2008-08-24 21:49:00 -04004083/**
James Smartda0436e2009-05-22 14:51:39 -04004084 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4085 * @phba: Pointer to HBA context object.
4086 *
4087 * This function resets a SLI4 HBA. This function disables PCI layer parity
4088 * checking during resets the device. The caller is not required to hold
4089 * any locks.
4090 *
4091 * This function returns 0 always.
4092 **/
4093int
4094lpfc_sli4_brdreset(struct lpfc_hba *phba)
4095{
4096 struct lpfc_sli *psli = &phba->sli;
4097 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004098 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004099
4100 /* Reset HBA */
4101 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004102 "0295 Reset HBA Data: x%x x%x x%x\n",
4103 phba->pport->port_state, psli->sli_flag,
4104 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004105
4106 /* perform board reset */
4107 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004108 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004109 phba->pport->fc_myDID = 0;
4110 phba->pport->fc_prevDID = 0;
4111
James Smartda0436e2009-05-22 14:51:39 -04004112 spin_lock_irq(&phba->hbalock);
4113 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4114 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004115 spin_unlock_irq(&phba->hbalock);
4116
James Smart02936352014-04-04 13:52:12 -04004117 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4118 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4119 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4120 return rc;
4121 }
4122
James Smartda0436e2009-05-22 14:51:39 -04004123 /* Now physically reset the device */
4124 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4125 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004126
4127 /* Turn off parity checking and serr during the physical reset */
4128 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4129 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4130 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4131
James Smart88318812012-09-29 11:29:29 -04004132 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004133 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004134 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004135
James Smartbe858b62010-12-15 17:57:20 -05004136 /* Restore PCI cmd register */
4137 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4138
James Smart27b01b82012-05-09 21:19:44 -04004139 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004140}
4141
4142/**
4143 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004144 * @phba: Pointer to HBA context object.
4145 *
4146 * This function is called in the SLI initialization code path to
4147 * restart the HBA. The caller is not required to hold any lock.
4148 * This function writes MBX_RESTART mailbox command to the SLIM and
4149 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4150 * function to free any pending commands. The function enables
4151 * POST only during the first initialization. The function returns zero.
4152 * The function does not guarantee completion of MBX_RESTART mailbox
4153 * command before the return of this function.
4154 **/
James Smartda0436e2009-05-22 14:51:39 -04004155static int
4156lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004157{
4158 MAILBOX_t *mb;
4159 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004160 volatile uint32_t word0;
4161 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004162 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004163
James Smart2e0fef82007-06-17 19:56:36 -05004164 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004165
James Smart0d878412009-10-02 15:16:56 -04004166 /* Take PCIe device Advanced Error Reporting (AER) state */
4167 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4168
Jamie Wellnitz41415862006-02-28 19:25:27 -05004169 psli = &phba->sli;
4170
4171 /* Restart HBA */
4172 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004173 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004174 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004175
4176 word0 = 0;
4177 mb = (MAILBOX_t *) &word0;
4178 mb->mbxCommand = MBX_RESTART;
4179 mb->mbxHc = 1;
4180
James Smart92908312006-03-07 15:04:13 -05004181 lpfc_reset_barrier(phba);
4182
Jamie Wellnitz41415862006-02-28 19:25:27 -05004183 to_slim = phba->MBslimaddr;
4184 writel(*(uint32_t *) mb, to_slim);
4185 readl(to_slim); /* flush */
4186
4187 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004188 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004189 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004190 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004191 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004192 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004193 writel(*(uint32_t *) mb, to_slim);
4194 readl(to_slim); /* flush */
4195
4196 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004197 phba->pport->stopped = 0;
4198 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004199 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004200 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004201
James Smart64ba8812006-08-02 15:24:34 -04004202 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4203 psli->stats_start = get_seconds();
4204
James Smarteaf15d52008-12-04 22:39:29 -05004205 /* Give the INITFF and Post time to settle. */
4206 mdelay(100);
dea31012005-04-17 16:05:31 -05004207
James Smart0d878412009-10-02 15:16:56 -04004208 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4209 if (hba_aer_enabled)
4210 pci_disable_pcie_error_reporting(phba->pcidev);
4211
Jamie Wellnitz41415862006-02-28 19:25:27 -05004212 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004213
4214 return 0;
4215}
4216
James Smarte59058c2008-08-24 21:49:00 -04004217/**
James Smartda0436e2009-05-22 14:51:39 -04004218 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4219 * @phba: Pointer to HBA context object.
4220 *
4221 * This function is called in the SLI initialization code path to restart
4222 * a SLI4 HBA. The caller is not required to hold any lock.
4223 * At the end of the function, it calls lpfc_hba_down_post function to
4224 * free any pending commands.
4225 **/
4226static int
4227lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4228{
4229 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004230 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004231 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004232
4233 /* Restart HBA */
4234 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4235 "0296 Restart HBA Data: x%x x%x\n",
4236 phba->pport->port_state, psli->sli_flag);
4237
James Smart75baf692010-06-08 18:31:21 -04004238 /* Take PCIe device Advanced Error Reporting (AER) state */
4239 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4240
James Smart27b01b82012-05-09 21:19:44 -04004241 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004242
4243 spin_lock_irq(&phba->hbalock);
4244 phba->pport->stopped = 0;
4245 phba->link_state = LPFC_INIT_START;
4246 phba->hba_flag = 0;
4247 spin_unlock_irq(&phba->hbalock);
4248
4249 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4250 psli->stats_start = get_seconds();
4251
James Smart75baf692010-06-08 18:31:21 -04004252 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4253 if (hba_aer_enabled)
4254 pci_disable_pcie_error_reporting(phba->pcidev);
4255
James Smartda0436e2009-05-22 14:51:39 -04004256 lpfc_hba_down_post(phba);
4257
James Smart27b01b82012-05-09 21:19:44 -04004258 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004259}
4260
4261/**
4262 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4263 * @phba: Pointer to HBA context object.
4264 *
4265 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4266 * API jump table function pointer from the lpfc_hba struct.
4267**/
4268int
4269lpfc_sli_brdrestart(struct lpfc_hba *phba)
4270{
4271 return phba->lpfc_sli_brdrestart(phba);
4272}
4273
4274/**
James Smart3621a712009-04-06 18:47:14 -04004275 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004276 * @phba: Pointer to HBA context object.
4277 *
4278 * This function is called after a HBA restart to wait for successful
4279 * restart of the HBA. Successful restart of the HBA is indicated by
4280 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4281 * iteration, the function will restart the HBA again. The function returns
4282 * zero if HBA successfully restarted else returns negative error code.
4283 **/
dea31012005-04-17 16:05:31 -05004284static int
4285lpfc_sli_chipset_init(struct lpfc_hba *phba)
4286{
4287 uint32_t status, i = 0;
4288
4289 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004290 if (lpfc_readl(phba->HSregaddr, &status))
4291 return -EIO;
dea31012005-04-17 16:05:31 -05004292
4293 /* Check status register to see what current state is */
4294 i = 0;
4295 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4296
James Smartdcf2a4e2010-09-29 11:18:53 -04004297 /* Check every 10ms for 10 retries, then every 100ms for 90
4298 * retries, then every 1 sec for 50 retires for a total of
4299 * ~60 seconds before reset the board again and check every
4300 * 1 sec for 50 retries. The up to 60 seconds before the
4301 * board ready is required by the Falcon FIPS zeroization
4302 * complete, and any reset the board in between shall cause
4303 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004304 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004305 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004306 /* Adapter failed to init, timeout, status reg
4307 <status> */
James Smarted957682007-06-17 19:56:37 -05004308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004309 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004310 "timeout, status reg x%x, "
4311 "FW Data: A8 x%x AC x%x\n", status,
4312 readl(phba->MBslimaddr + 0xa8),
4313 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004314 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004315 return -ETIMEDOUT;
4316 }
4317
4318 /* Check to see if any errors occurred during init */
4319 if (status & HS_FFERM) {
4320 /* ERROR: During chipset initialization */
4321 /* Adapter failed to init, chipset, status reg
4322 <status> */
James Smarted957682007-06-17 19:56:37 -05004323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004324 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004325 "chipset, status reg x%x, "
4326 "FW Data: A8 x%x AC x%x\n", status,
4327 readl(phba->MBslimaddr + 0xa8),
4328 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004329 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004330 return -EIO;
4331 }
4332
James Smartdcf2a4e2010-09-29 11:18:53 -04004333 if (i <= 10)
dea31012005-04-17 16:05:31 -05004334 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004335 else if (i <= 100)
4336 msleep(100);
4337 else
4338 msleep(1000);
dea31012005-04-17 16:05:31 -05004339
James Smartdcf2a4e2010-09-29 11:18:53 -04004340 if (i == 150) {
4341 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004342 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004343 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004344 }
4345 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004346 if (lpfc_readl(phba->HSregaddr, &status))
4347 return -EIO;
dea31012005-04-17 16:05:31 -05004348 }
4349
4350 /* Check to see if any errors occurred during init */
4351 if (status & HS_FFERM) {
4352 /* ERROR: During chipset initialization */
4353 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004355 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004356 "status reg x%x, "
4357 "FW Data: A8 x%x AC x%x\n", status,
4358 readl(phba->MBslimaddr + 0xa8),
4359 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004360 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004361 return -EIO;
4362 }
4363
4364 /* Clear all interrupt enable conditions */
4365 writel(0, phba->HCregaddr);
4366 readl(phba->HCregaddr); /* flush */
4367
4368 /* setup host attn register */
4369 writel(0xffffffff, phba->HAregaddr);
4370 readl(phba->HAregaddr); /* flush */
4371 return 0;
4372}
4373
James Smarte59058c2008-08-24 21:49:00 -04004374/**
James Smart3621a712009-04-06 18:47:14 -04004375 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004376 *
4377 * This function calculates and returns the number of HBQs required to be
4378 * configured.
4379 **/
James Smart78b2d852007-08-02 11:10:21 -04004380int
James Smarted957682007-06-17 19:56:37 -05004381lpfc_sli_hbq_count(void)
4382{
James Smart92d7f7b2007-06-17 19:56:38 -05004383 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004384}
4385
James Smarte59058c2008-08-24 21:49:00 -04004386/**
James Smart3621a712009-04-06 18:47:14 -04004387 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004388 *
4389 * This function adds the number of hbq entries in every HBQ to get
4390 * the total number of hbq entries required for the HBA and returns
4391 * the total count.
4392 **/
James Smarted957682007-06-17 19:56:37 -05004393static int
4394lpfc_sli_hbq_entry_count(void)
4395{
4396 int hbq_count = lpfc_sli_hbq_count();
4397 int count = 0;
4398 int i;
4399
4400 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004401 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004402 return count;
4403}
4404
James Smarte59058c2008-08-24 21:49:00 -04004405/**
James Smart3621a712009-04-06 18:47:14 -04004406 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004407 *
4408 * This function calculates amount of memory required for all hbq entries
4409 * to be configured and returns the total memory required.
4410 **/
dea31012005-04-17 16:05:31 -05004411int
James Smarted957682007-06-17 19:56:37 -05004412lpfc_sli_hbq_size(void)
4413{
4414 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4415}
4416
James Smarte59058c2008-08-24 21:49:00 -04004417/**
James Smart3621a712009-04-06 18:47:14 -04004418 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004419 * @phba: Pointer to HBA context object.
4420 *
4421 * This function is called during the SLI initialization to configure
4422 * all the HBQs and post buffers to the HBQ. The caller is not
4423 * required to hold any locks. This function will return zero if successful
4424 * else it will return negative error code.
4425 **/
James Smarted957682007-06-17 19:56:37 -05004426static int
4427lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4428{
4429 int hbq_count = lpfc_sli_hbq_count();
4430 LPFC_MBOXQ_t *pmb;
4431 MAILBOX_t *pmbox;
4432 uint32_t hbqno;
4433 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004434
James Smart92d7f7b2007-06-17 19:56:38 -05004435 /* Get a Mailbox buffer to setup mailbox
4436 * commands for HBA initialization
4437 */
James Smarted957682007-06-17 19:56:37 -05004438 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4439
4440 if (!pmb)
4441 return -ENOMEM;
4442
James Smart04c68492009-05-22 14:52:52 -04004443 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004444
4445 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4446 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004447 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004448
4449 hbq_entry_index = 0;
4450 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4451 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4452 phba->hbqs[hbqno].hbqPutIdx = 0;
4453 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4454 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004455 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004456 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4457 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004458 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4459
4460 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4461 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4462 mbxStatus <status>, ring <num> */
4463
4464 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004465 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004466 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004467 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004468 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004469 pmbox->mbxStatus, hbqno);
4470
4471 phba->link_state = LPFC_HBA_ERROR;
4472 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004473 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004474 }
4475 }
4476 phba->hbq_count = hbq_count;
4477
James Smarted957682007-06-17 19:56:37 -05004478 mempool_free(pmb, phba->mbox_mem_pool);
4479
James Smart92d7f7b2007-06-17 19:56:38 -05004480 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004481 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4482 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004483 return 0;
4484}
4485
James Smarte59058c2008-08-24 21:49:00 -04004486/**
James Smart4f774512009-05-22 14:52:35 -04004487 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4488 * @phba: Pointer to HBA context object.
4489 *
4490 * This function is called during the SLI initialization to configure
4491 * all the HBQs and post buffers to the HBQ. The caller is not
4492 * required to hold any locks. This function will return zero if successful
4493 * else it will return negative error code.
4494 **/
4495static int
4496lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4497{
4498 phba->hbq_in_use = 1;
4499 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4500 phba->hbq_count = 1;
4501 /* Initially populate or replenish the HBQs */
4502 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4503 return 0;
4504}
4505
4506/**
James Smart3621a712009-04-06 18:47:14 -04004507 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004508 * @phba: Pointer to HBA context object.
4509 * @sli_mode: sli mode - 2/3
4510 *
4511 * This function is called by the sli intialization code path
4512 * to issue config_port mailbox command. This function restarts the
4513 * HBA firmware and issues a config_port mailbox command to configure
4514 * the SLI interface in the sli mode specified by sli_mode
4515 * variable. The caller is not required to hold any locks.
4516 * The function returns 0 if successful, else returns negative error
4517 * code.
4518 **/
James Smart93996272008-08-24 21:50:30 -04004519int
4520lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004521{
4522 LPFC_MBOXQ_t *pmb;
4523 uint32_t resetcount = 0, rc = 0, done = 0;
4524
4525 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4526 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004527 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004528 return -ENOMEM;
4529 }
4530
James Smarted957682007-06-17 19:56:37 -05004531 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004532 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004533 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004534 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004535 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004536 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004537 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004538 rc = lpfc_sli_chipset_init(phba);
4539 if (rc)
4540 break;
4541
James Smart2e0fef82007-06-17 19:56:36 -05004542 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004543 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004544 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004545 resetcount++;
4546
James Smarted957682007-06-17 19:56:37 -05004547 /* Call pre CONFIG_PORT mailbox command initialization. A
4548 * value of 0 means the call was successful. Any other
4549 * nonzero value is a failure, but if ERESTART is returned,
4550 * the driver may reset the HBA and try again.
4551 */
dea31012005-04-17 16:05:31 -05004552 rc = lpfc_config_port_prep(phba);
4553 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004554 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004555 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004556 } else if (rc)
dea31012005-04-17 16:05:31 -05004557 break;
James Smart6d368e52011-05-24 11:44:12 -04004558
James Smart2e0fef82007-06-17 19:56:36 -05004559 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004560 lpfc_config_port(phba, pmb);
4561 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004562 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4563 LPFC_SLI3_HBQ_ENABLED |
4564 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004565 LPFC_SLI3_BG_ENABLED |
4566 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004567 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004569 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004570 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004571 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004572 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004573 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004574 spin_unlock_irq(&phba->hbalock);
4575 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004576 } else {
4577 /* Allow asynchronous mailbox command to go through */
4578 spin_lock_irq(&phba->hbalock);
4579 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4580 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004581 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004582
4583 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4584 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4585 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4586 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004587 }
dea31012005-04-17 16:05:31 -05004588 }
James Smarted957682007-06-17 19:56:37 -05004589 if (!done) {
4590 rc = -EINVAL;
4591 goto do_prep_failed;
4592 }
James Smart04c68492009-05-22 14:52:52 -04004593 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4594 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004595 rc = -ENXIO;
4596 goto do_prep_failed;
4597 }
James Smart04c68492009-05-22 14:52:52 -04004598 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004599 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004600 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4601 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4602 phba->max_vpi : phba->max_vports;
4603
James Smart34b02dc2008-08-24 21:49:55 -04004604 } else
4605 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004606 phba->fips_level = 0;
4607 phba->fips_spec_rev = 0;
4608 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004609 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004610 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4611 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4612 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4613 "2850 Security Crypto Active. FIPS x%d "
4614 "(Spec Rev: x%d)",
4615 phba->fips_level, phba->fips_spec_rev);
4616 }
4617 if (pmb->u.mb.un.varCfgPort.sec_err) {
4618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4619 "2856 Config Port Security Crypto "
4620 "Error: x%x ",
4621 pmb->u.mb.un.varCfgPort.sec_err);
4622 }
James Smart04c68492009-05-22 14:52:52 -04004623 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004624 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004625 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004626 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004627
4628 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4629 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004630
4631 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004632 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004633 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4634 else
4635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4636 "0443 Adapter did not grant "
4637 "BlockGuard\n");
4638 }
James Smart34b02dc2008-08-24 21:49:55 -04004639 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004640 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004641 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004642 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004643 }
James Smart92d7f7b2007-06-17 19:56:38 -05004644do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004645 mempool_free(pmb, phba->mbox_mem_pool);
4646 return rc;
4647}
4648
James Smarte59058c2008-08-24 21:49:00 -04004649
4650/**
James Smart3621a712009-04-06 18:47:14 -04004651 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004652 * @phba: Pointer to HBA context object.
4653 *
4654 * This function is the main SLI intialization function. This function
4655 * is called by the HBA intialization code, HBA reset code and HBA
4656 * error attention handler code. Caller is not required to hold any
4657 * locks. This function issues config_port mailbox command to configure
4658 * the SLI, setup iocb rings and HBQ rings. In the end the function
4659 * calls the config_port_post function to issue init_link mailbox
4660 * command and to start the discovery. The function will return zero
4661 * if successful, else it will return negative error code.
4662 **/
James Smarted957682007-06-17 19:56:37 -05004663int
4664lpfc_sli_hba_setup(struct lpfc_hba *phba)
4665{
4666 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004667 int mode = 3, i;
4668 int longs;
James Smarted957682007-06-17 19:56:37 -05004669
James Smart12247e82016-07-06 12:36:09 -07004670 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004671 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004672 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004674 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004675 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004676 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004677 break;
4678 }
James Smarted957682007-06-17 19:56:37 -05004679 mode = 2;
4680 break;
4681 case 0:
4682 case 3:
4683 break;
4684 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004685 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004686 "1819 Unrecognized sli_mode parameter: %d.\n",
4687 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004688
4689 break;
4690 }
James Smartb5c53952016-03-31 14:12:30 -07004691 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004692
James Smart93996272008-08-24 21:50:30 -04004693 rc = lpfc_sli_config_port(phba, mode);
4694
James Smart12247e82016-07-06 12:36:09 -07004695 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004697 "1820 Unable to select SLI-3. "
4698 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004699 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004700 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004701 else if (rc && mode == 2)
4702 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004703 if (rc)
dea31012005-04-17 16:05:31 -05004704 goto lpfc_sli_hba_setup_error;
4705
James Smart0d878412009-10-02 15:16:56 -04004706 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4707 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4708 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4709 if (!rc) {
4710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4711 "2709 This device supports "
4712 "Advanced Error Reporting (AER)\n");
4713 spin_lock_irq(&phba->hbalock);
4714 phba->hba_flag |= HBA_AER_ENABLED;
4715 spin_unlock_irq(&phba->hbalock);
4716 } else {
4717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4718 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004719 "Advanced Error Reporting (AER): %d\n",
4720 rc);
James Smart0d878412009-10-02 15:16:56 -04004721 phba->cfg_aer_support = 0;
4722 }
4723 }
4724
James Smarted957682007-06-17 19:56:37 -05004725 if (phba->sli_rev == 3) {
4726 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4727 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004728 } else {
4729 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4730 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004731 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004732 }
4733
4734 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004735 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4736 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004737 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004738
4739 if (rc)
4740 goto lpfc_sli_hba_setup_error;
4741
James Smart6d368e52011-05-24 11:44:12 -04004742 /* Initialize VPIs. */
4743 if (phba->sli_rev == LPFC_SLI_REV3) {
4744 /*
4745 * The VPI bitmask and physical ID array are allocated
4746 * and initialized once only - at driver load. A port
4747 * reset doesn't need to reinitialize this memory.
4748 */
4749 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4750 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4751 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4752 GFP_KERNEL);
4753 if (!phba->vpi_bmask) {
4754 rc = -ENOMEM;
4755 goto lpfc_sli_hba_setup_error;
4756 }
4757
4758 phba->vpi_ids = kzalloc(
4759 (phba->max_vpi+1) * sizeof(uint16_t),
4760 GFP_KERNEL);
4761 if (!phba->vpi_ids) {
4762 kfree(phba->vpi_bmask);
4763 rc = -ENOMEM;
4764 goto lpfc_sli_hba_setup_error;
4765 }
4766 for (i = 0; i < phba->max_vpi; i++)
4767 phba->vpi_ids[i] = i;
4768 }
4769 }
4770
James Smart93996272008-08-24 21:50:30 -04004771 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004772 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4773 rc = lpfc_sli_hbq_setup(phba);
4774 if (rc)
4775 goto lpfc_sli_hba_setup_error;
4776 }
James Smart04c68492009-05-22 14:52:52 -04004777 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004778 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004779 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004780
4781 rc = lpfc_config_port_post(phba);
4782 if (rc)
4783 goto lpfc_sli_hba_setup_error;
4784
James Smarted957682007-06-17 19:56:37 -05004785 return rc;
4786
James Smart92d7f7b2007-06-17 19:56:38 -05004787lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004788 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004790 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004791 return rc;
4792}
4793
James Smartda0436e2009-05-22 14:51:39 -04004794/**
4795 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4796 * @phba: Pointer to HBA context object.
4797 * @mboxq: mailbox pointer.
4798 * This function issue a dump mailbox command to read config region
4799 * 23 and parse the records in the region and populate driver
4800 * data structure.
4801 **/
4802static int
James Smartff78d8f2011-12-13 13:21:35 -05004803lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004804{
James Smartff78d8f2011-12-13 13:21:35 -05004805 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004806 struct lpfc_dmabuf *mp;
4807 struct lpfc_mqe *mqe;
4808 uint32_t data_length;
4809 int rc;
4810
4811 /* Program the default value of vlan_id and fc_map */
4812 phba->valid_vlan = 0;
4813 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4814 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4815 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4816
James Smartff78d8f2011-12-13 13:21:35 -05004817 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4818 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004819 return -ENOMEM;
4820
James Smartff78d8f2011-12-13 13:21:35 -05004821 mqe = &mboxq->u.mqe;
4822 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4823 rc = -ENOMEM;
4824 goto out_free_mboxq;
4825 }
4826
James Smartda0436e2009-05-22 14:51:39 -04004827 mp = (struct lpfc_dmabuf *) mboxq->context1;
4828 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4829
4830 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4831 "(%d):2571 Mailbox cmd x%x Status x%x "
4832 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4833 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4834 "CQ: x%x x%x x%x x%x\n",
4835 mboxq->vport ? mboxq->vport->vpi : 0,
4836 bf_get(lpfc_mqe_command, mqe),
4837 bf_get(lpfc_mqe_status, mqe),
4838 mqe->un.mb_words[0], mqe->un.mb_words[1],
4839 mqe->un.mb_words[2], mqe->un.mb_words[3],
4840 mqe->un.mb_words[4], mqe->un.mb_words[5],
4841 mqe->un.mb_words[6], mqe->un.mb_words[7],
4842 mqe->un.mb_words[8], mqe->un.mb_words[9],
4843 mqe->un.mb_words[10], mqe->un.mb_words[11],
4844 mqe->un.mb_words[12], mqe->un.mb_words[13],
4845 mqe->un.mb_words[14], mqe->un.mb_words[15],
4846 mqe->un.mb_words[16], mqe->un.mb_words[50],
4847 mboxq->mcqe.word0,
4848 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4849 mboxq->mcqe.trailer);
4850
4851 if (rc) {
4852 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4853 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004854 rc = -EIO;
4855 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004856 }
4857 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004858 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004859 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4860 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004861 rc = -EIO;
4862 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004863 }
James Smartda0436e2009-05-22 14:51:39 -04004864
4865 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4866 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4867 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004868 rc = 0;
4869
4870out_free_mboxq:
4871 mempool_free(mboxq, phba->mbox_mem_pool);
4872 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004873}
4874
4875/**
4876 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4877 * @phba: pointer to lpfc hba data structure.
4878 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4879 * @vpd: pointer to the memory to hold resulting port vpd data.
4880 * @vpd_size: On input, the number of bytes allocated to @vpd.
4881 * On output, the number of data bytes in @vpd.
4882 *
4883 * This routine executes a READ_REV SLI4 mailbox command. In
4884 * addition, this routine gets the port vpd data.
4885 *
4886 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004887 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004888 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004889 **/
4890static int
4891lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4892 uint8_t *vpd, uint32_t *vpd_size)
4893{
4894 int rc = 0;
4895 uint32_t dma_size;
4896 struct lpfc_dmabuf *dmabuf;
4897 struct lpfc_mqe *mqe;
4898
4899 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4900 if (!dmabuf)
4901 return -ENOMEM;
4902
4903 /*
4904 * Get a DMA buffer for the vpd data resulting from the READ_REV
4905 * mailbox command.
4906 */
4907 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004908 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4909 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004910 if (!dmabuf->virt) {
4911 kfree(dmabuf);
4912 return -ENOMEM;
4913 }
James Smartda0436e2009-05-22 14:51:39 -04004914
4915 /*
4916 * The SLI4 implementation of READ_REV conflicts at word1,
4917 * bits 31:16 and SLI4 adds vpd functionality not present
4918 * in SLI3. This code corrects the conflicts.
4919 */
4920 lpfc_read_rev(phba, mboxq);
4921 mqe = &mboxq->u.mqe;
4922 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4923 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4924 mqe->un.read_rev.word1 &= 0x0000FFFF;
4925 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4926 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4927
4928 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4929 if (rc) {
4930 dma_free_coherent(&phba->pcidev->dev, dma_size,
4931 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004932 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004933 return -EIO;
4934 }
4935
James Smartda0436e2009-05-22 14:51:39 -04004936 /*
4937 * The available vpd length cannot be bigger than the
4938 * DMA buffer passed to the port. Catch the less than
4939 * case and update the caller's size.
4940 */
4941 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4942 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4943
James Smartd7c47992010-06-08 18:31:54 -04004944 memcpy(vpd, dmabuf->virt, *vpd_size);
4945
James Smartda0436e2009-05-22 14:51:39 -04004946 dma_free_coherent(&phba->pcidev->dev, dma_size,
4947 dmabuf->virt, dmabuf->phys);
4948 kfree(dmabuf);
4949 return 0;
4950}
4951
4952/**
James Smartcd1c8302011-10-10 21:33:25 -04004953 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4954 * @phba: pointer to lpfc hba data structure.
4955 *
4956 * This routine retrieves SLI4 device physical port name this PCI function
4957 * is attached to.
4958 *
4959 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004960 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004961 * otherwise - failed to retrieve physical port name
4962 **/
4963static int
4964lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4965{
4966 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004967 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4968 struct lpfc_controller_attribute *cntl_attr;
4969 struct lpfc_mbx_get_port_name *get_port_name;
4970 void *virtaddr = NULL;
4971 uint32_t alloclen, reqlen;
4972 uint32_t shdr_status, shdr_add_status;
4973 union lpfc_sli4_cfg_shdr *shdr;
4974 char cport_name = 0;
4975 int rc;
4976
4977 /* We assume nothing at this point */
4978 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4979 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4980
4981 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4982 if (!mboxq)
4983 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004984 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004985 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4986 lpfc_sli4_read_config(phba);
4987 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4988 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004989
4990 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4991 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4992 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4993 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4994 LPFC_SLI4_MBX_NEMBED);
4995 if (alloclen < reqlen) {
4996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4997 "3084 Allocated DMA memory size (%d) is "
4998 "less than the requested DMA memory size "
4999 "(%d)\n", alloclen, reqlen);
5000 rc = -ENOMEM;
5001 goto out_free_mboxq;
5002 }
5003 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5004 virtaddr = mboxq->sge_array->addr[0];
5005 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5006 shdr = &mbx_cntl_attr->cfg_shdr;
5007 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5008 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5009 if (shdr_status || shdr_add_status || rc) {
5010 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5011 "3085 Mailbox x%x (x%x/x%x) failed, "
5012 "rc:x%x, status:x%x, add_status:x%x\n",
5013 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5014 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5015 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5016 rc, shdr_status, shdr_add_status);
5017 rc = -ENXIO;
5018 goto out_free_mboxq;
5019 }
5020 cntl_attr = &mbx_cntl_attr->cntl_attr;
5021 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5022 phba->sli4_hba.lnk_info.lnk_tp =
5023 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5024 phba->sli4_hba.lnk_info.lnk_no =
5025 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5026 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5027 "3086 lnk_type:%d, lnk_numb:%d\n",
5028 phba->sli4_hba.lnk_info.lnk_tp,
5029 phba->sli4_hba.lnk_info.lnk_no);
5030
5031retrieve_ppname:
5032 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5033 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5034 sizeof(struct lpfc_mbx_get_port_name) -
5035 sizeof(struct lpfc_sli4_cfg_mhdr),
5036 LPFC_SLI4_MBX_EMBED);
5037 get_port_name = &mboxq->u.mqe.un.get_port_name;
5038 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5039 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5040 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5041 phba->sli4_hba.lnk_info.lnk_tp);
5042 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5043 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5044 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5045 if (shdr_status || shdr_add_status || rc) {
5046 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5047 "3087 Mailbox x%x (x%x/x%x) failed: "
5048 "rc:x%x, status:x%x, add_status:x%x\n",
5049 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5050 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5051 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5052 rc, shdr_status, shdr_add_status);
5053 rc = -ENXIO;
5054 goto out_free_mboxq;
5055 }
5056 switch (phba->sli4_hba.lnk_info.lnk_no) {
5057 case LPFC_LINK_NUMBER_0:
5058 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5059 &get_port_name->u.response);
5060 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5061 break;
5062 case LPFC_LINK_NUMBER_1:
5063 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5064 &get_port_name->u.response);
5065 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5066 break;
5067 case LPFC_LINK_NUMBER_2:
5068 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5069 &get_port_name->u.response);
5070 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5071 break;
5072 case LPFC_LINK_NUMBER_3:
5073 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5074 &get_port_name->u.response);
5075 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5076 break;
5077 default:
5078 break;
5079 }
5080
5081 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5082 phba->Port[0] = cport_name;
5083 phba->Port[1] = '\0';
5084 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5085 "3091 SLI get port name: %s\n", phba->Port);
5086 }
5087
5088out_free_mboxq:
5089 if (rc != MBX_TIMEOUT) {
5090 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5091 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5092 else
5093 mempool_free(mboxq, phba->mbox_mem_pool);
5094 }
5095 return rc;
5096}
5097
5098/**
James Smartda0436e2009-05-22 14:51:39 -04005099 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5100 * @phba: pointer to lpfc hba data structure.
5101 *
5102 * This routine is called to explicitly arm the SLI4 device's completion and
5103 * event queues
5104 **/
5105static void
5106lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5107{
James Smart962bc512013-01-03 15:44:00 -05005108 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04005109
5110 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5111 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04005112 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05005113 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04005114 do {
James Smart2e90f4b2011-12-13 13:22:37 -05005115 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5116 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04005117 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05005118 }
James Smart1ba981f2014-02-20 09:56:45 -05005119
James Smartf38fa0b2014-04-04 13:52:21 -04005120 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005121 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5122
James Smart67d12732012-08-03 12:36:13 -04005123 if (phba->sli4_hba.hba_eq) {
5124 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05005125 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04005126 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05005127 LPFC_QUEUE_REARM);
5128 }
James Smart1ba981f2014-02-20 09:56:45 -05005129
5130 if (phba->cfg_fof)
5131 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005132}
5133
5134/**
James Smart6d368e52011-05-24 11:44:12 -04005135 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5136 * @phba: Pointer to HBA context object.
5137 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005138 * @extnt_count: buffer to hold port available extent count.
5139 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005140 *
James Smartb76f2dc2011-07-22 18:37:42 -04005141 * This function calls the port and retrievs the number of available
5142 * extents and their size for a particular extent type.
5143 *
5144 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005145 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005146int
James Smart6d368e52011-05-24 11:44:12 -04005147lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5148 uint16_t *extnt_count, uint16_t *extnt_size)
5149{
5150 int rc = 0;
5151 uint32_t length;
5152 uint32_t mbox_tmo;
5153 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5154 LPFC_MBOXQ_t *mbox;
5155
5156 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5157 if (!mbox)
5158 return -ENOMEM;
5159
5160 /* Find out how many extents are available for this resource type */
5161 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5162 sizeof(struct lpfc_sli4_cfg_mhdr));
5163 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5164 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5165 length, LPFC_SLI4_MBX_EMBED);
5166
5167 /* Send an extents count of 0 - the GET doesn't use it. */
5168 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5169 LPFC_SLI4_MBX_EMBED);
5170 if (unlikely(rc)) {
5171 rc = -EIO;
5172 goto err_exit;
5173 }
5174
5175 if (!phba->sli4_hba.intr_enable)
5176 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5177 else {
James Smarta183a152011-10-10 21:32:43 -04005178 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005179 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5180 }
5181 if (unlikely(rc)) {
5182 rc = -EIO;
5183 goto err_exit;
5184 }
5185
5186 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5187 if (bf_get(lpfc_mbox_hdr_status,
5188 &rsrc_info->header.cfg_shdr.response)) {
5189 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5190 "2930 Failed to get resource extents "
5191 "Status 0x%x Add'l Status 0x%x\n",
5192 bf_get(lpfc_mbox_hdr_status,
5193 &rsrc_info->header.cfg_shdr.response),
5194 bf_get(lpfc_mbox_hdr_add_status,
5195 &rsrc_info->header.cfg_shdr.response));
5196 rc = -EIO;
5197 goto err_exit;
5198 }
5199
5200 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5201 &rsrc_info->u.rsp);
5202 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5203 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005204
5205 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5206 "3162 Retrieved extents type-%d from port: count:%d, "
5207 "size:%d\n", type, *extnt_count, *extnt_size);
5208
5209err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005210 mempool_free(mbox, phba->mbox_mem_pool);
5211 return rc;
5212}
5213
5214/**
5215 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5216 * @phba: Pointer to HBA context object.
5217 * @type: The extent type to check.
5218 *
5219 * This function reads the current available extents from the port and checks
5220 * if the extent count or extent size has changed since the last access.
5221 * Callers use this routine post port reset to understand if there is a
5222 * extent reprovisioning requirement.
5223 *
5224 * Returns:
5225 * -Error: error indicates problem.
5226 * 1: Extent count or size has changed.
5227 * 0: No changes.
5228 **/
5229static int
5230lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5231{
5232 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5233 uint16_t size_diff, rsrc_ext_size;
5234 int rc = 0;
5235 struct lpfc_rsrc_blks *rsrc_entry;
5236 struct list_head *rsrc_blk_list = NULL;
5237
5238 size_diff = 0;
5239 curr_ext_cnt = 0;
5240 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5241 &rsrc_ext_cnt,
5242 &rsrc_ext_size);
5243 if (unlikely(rc))
5244 return -EIO;
5245
5246 switch (type) {
5247 case LPFC_RSC_TYPE_FCOE_RPI:
5248 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5249 break;
5250 case LPFC_RSC_TYPE_FCOE_VPI:
5251 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5252 break;
5253 case LPFC_RSC_TYPE_FCOE_XRI:
5254 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5255 break;
5256 case LPFC_RSC_TYPE_FCOE_VFI:
5257 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5258 break;
5259 default:
5260 break;
5261 }
5262
5263 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5264 curr_ext_cnt++;
5265 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5266 size_diff++;
5267 }
5268
5269 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5270 rc = 1;
5271
5272 return rc;
5273}
5274
5275/**
5276 * lpfc_sli4_cfg_post_extnts -
5277 * @phba: Pointer to HBA context object.
5278 * @extnt_cnt - number of available extents.
5279 * @type - the extent type (rpi, xri, vfi, vpi).
5280 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5281 * @mbox - pointer to the caller's allocated mailbox structure.
5282 *
5283 * This function executes the extents allocation request. It also
5284 * takes care of the amount of memory needed to allocate or get the
5285 * allocated extents. It is the caller's responsibility to evaluate
5286 * the response.
5287 *
5288 * Returns:
5289 * -Error: Error value describes the condition found.
5290 * 0: if successful
5291 **/
5292static int
James Smart8a9d2e82012-05-09 21:16:12 -04005293lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005294 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5295{
5296 int rc = 0;
5297 uint32_t req_len;
5298 uint32_t emb_len;
5299 uint32_t alloc_len, mbox_tmo;
5300
5301 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005302 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005303
5304 /*
5305 * Calculate the size of an embedded mailbox. The uint32_t
5306 * accounts for extents-specific word.
5307 */
5308 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5309 sizeof(uint32_t);
5310
5311 /*
5312 * Presume the allocation and response will fit into an embedded
5313 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5314 */
5315 *emb = LPFC_SLI4_MBX_EMBED;
5316 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005317 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005318 sizeof(union lpfc_sli4_cfg_shdr) +
5319 sizeof(uint32_t);
5320 *emb = LPFC_SLI4_MBX_NEMBED;
5321 }
5322
5323 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5324 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5325 req_len, *emb);
5326 if (alloc_len < req_len) {
5327 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005328 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005329 "less than the requested DMA memory "
5330 "size (x%x)\n", alloc_len, req_len);
5331 return -ENOMEM;
5332 }
James Smart8a9d2e82012-05-09 21:16:12 -04005333 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005334 if (unlikely(rc))
5335 return -EIO;
5336
5337 if (!phba->sli4_hba.intr_enable)
5338 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5339 else {
James Smarta183a152011-10-10 21:32:43 -04005340 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005341 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5342 }
5343
5344 if (unlikely(rc))
5345 rc = -EIO;
5346 return rc;
5347}
5348
5349/**
5350 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5351 * @phba: Pointer to HBA context object.
5352 * @type: The resource extent type to allocate.
5353 *
5354 * This function allocates the number of elements for the specified
5355 * resource type.
5356 **/
5357static int
5358lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5359{
5360 bool emb = false;
5361 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5362 uint16_t rsrc_id, rsrc_start, j, k;
5363 uint16_t *ids;
5364 int i, rc;
5365 unsigned long longs;
5366 unsigned long *bmask;
5367 struct lpfc_rsrc_blks *rsrc_blks;
5368 LPFC_MBOXQ_t *mbox;
5369 uint32_t length;
5370 struct lpfc_id_range *id_array = NULL;
5371 void *virtaddr = NULL;
5372 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5373 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5374 struct list_head *ext_blk_list;
5375
5376 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5377 &rsrc_cnt,
5378 &rsrc_size);
5379 if (unlikely(rc))
5380 return -EIO;
5381
5382 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5384 "3009 No available Resource Extents "
5385 "for resource type 0x%x: Count: 0x%x, "
5386 "Size 0x%x\n", type, rsrc_cnt,
5387 rsrc_size);
5388 return -ENOMEM;
5389 }
5390
James Smart8a9d2e82012-05-09 21:16:12 -04005391 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5392 "2903 Post resource extents type-0x%x: "
5393 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005394
5395 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5396 if (!mbox)
5397 return -ENOMEM;
5398
James Smart8a9d2e82012-05-09 21:16:12 -04005399 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005400 if (unlikely(rc)) {
5401 rc = -EIO;
5402 goto err_exit;
5403 }
5404
5405 /*
5406 * Figure out where the response is located. Then get local pointers
5407 * to the response data. The port does not guarantee to respond to
5408 * all extents counts request so update the local variable with the
5409 * allocated count from the port.
5410 */
5411 if (emb == LPFC_SLI4_MBX_EMBED) {
5412 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5413 id_array = &rsrc_ext->u.rsp.id[0];
5414 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5415 } else {
5416 virtaddr = mbox->sge_array->addr[0];
5417 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5418 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5419 id_array = &n_rsrc->id;
5420 }
5421
5422 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5423 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5424
5425 /*
5426 * Based on the resource size and count, correct the base and max
5427 * resource values.
5428 */
5429 length = sizeof(struct lpfc_rsrc_blks);
5430 switch (type) {
5431 case LPFC_RSC_TYPE_FCOE_RPI:
5432 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5433 sizeof(unsigned long),
5434 GFP_KERNEL);
5435 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5436 rc = -ENOMEM;
5437 goto err_exit;
5438 }
5439 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5440 sizeof(uint16_t),
5441 GFP_KERNEL);
5442 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5443 kfree(phba->sli4_hba.rpi_bmask);
5444 rc = -ENOMEM;
5445 goto err_exit;
5446 }
5447
5448 /*
5449 * The next_rpi was initialized with the maximum available
5450 * count but the port may allocate a smaller number. Catch
5451 * that case and update the next_rpi.
5452 */
5453 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5454
5455 /* Initialize local ptrs for common extent processing later. */
5456 bmask = phba->sli4_hba.rpi_bmask;
5457 ids = phba->sli4_hba.rpi_ids;
5458 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5459 break;
5460 case LPFC_RSC_TYPE_FCOE_VPI:
5461 phba->vpi_bmask = kzalloc(longs *
5462 sizeof(unsigned long),
5463 GFP_KERNEL);
5464 if (unlikely(!phba->vpi_bmask)) {
5465 rc = -ENOMEM;
5466 goto err_exit;
5467 }
5468 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5469 sizeof(uint16_t),
5470 GFP_KERNEL);
5471 if (unlikely(!phba->vpi_ids)) {
5472 kfree(phba->vpi_bmask);
5473 rc = -ENOMEM;
5474 goto err_exit;
5475 }
5476
5477 /* Initialize local ptrs for common extent processing later. */
5478 bmask = phba->vpi_bmask;
5479 ids = phba->vpi_ids;
5480 ext_blk_list = &phba->lpfc_vpi_blk_list;
5481 break;
5482 case LPFC_RSC_TYPE_FCOE_XRI:
5483 phba->sli4_hba.xri_bmask = kzalloc(longs *
5484 sizeof(unsigned long),
5485 GFP_KERNEL);
5486 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5487 rc = -ENOMEM;
5488 goto err_exit;
5489 }
James Smart8a9d2e82012-05-09 21:16:12 -04005490 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005491 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5492 sizeof(uint16_t),
5493 GFP_KERNEL);
5494 if (unlikely(!phba->sli4_hba.xri_ids)) {
5495 kfree(phba->sli4_hba.xri_bmask);
5496 rc = -ENOMEM;
5497 goto err_exit;
5498 }
5499
5500 /* Initialize local ptrs for common extent processing later. */
5501 bmask = phba->sli4_hba.xri_bmask;
5502 ids = phba->sli4_hba.xri_ids;
5503 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5504 break;
5505 case LPFC_RSC_TYPE_FCOE_VFI:
5506 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5507 sizeof(unsigned long),
5508 GFP_KERNEL);
5509 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5510 rc = -ENOMEM;
5511 goto err_exit;
5512 }
5513 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5514 sizeof(uint16_t),
5515 GFP_KERNEL);
5516 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5517 kfree(phba->sli4_hba.vfi_bmask);
5518 rc = -ENOMEM;
5519 goto err_exit;
5520 }
5521
5522 /* Initialize local ptrs for common extent processing later. */
5523 bmask = phba->sli4_hba.vfi_bmask;
5524 ids = phba->sli4_hba.vfi_ids;
5525 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5526 break;
5527 default:
5528 /* Unsupported Opcode. Fail call. */
5529 id_array = NULL;
5530 bmask = NULL;
5531 ids = NULL;
5532 ext_blk_list = NULL;
5533 goto err_exit;
5534 }
5535
5536 /*
5537 * Complete initializing the extent configuration with the
5538 * allocated ids assigned to this function. The bitmask serves
5539 * as an index into the array and manages the available ids. The
5540 * array just stores the ids communicated to the port via the wqes.
5541 */
5542 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5543 if ((i % 2) == 0)
5544 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5545 &id_array[k]);
5546 else
5547 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5548 &id_array[k]);
5549
5550 rsrc_blks = kzalloc(length, GFP_KERNEL);
5551 if (unlikely(!rsrc_blks)) {
5552 rc = -ENOMEM;
5553 kfree(bmask);
5554 kfree(ids);
5555 goto err_exit;
5556 }
5557 rsrc_blks->rsrc_start = rsrc_id;
5558 rsrc_blks->rsrc_size = rsrc_size;
5559 list_add_tail(&rsrc_blks->list, ext_blk_list);
5560 rsrc_start = rsrc_id;
5561 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5562 phba->sli4_hba.scsi_xri_start = rsrc_start +
5563 lpfc_sli4_get_els_iocb_cnt(phba);
5564
5565 while (rsrc_id < (rsrc_start + rsrc_size)) {
5566 ids[j] = rsrc_id;
5567 rsrc_id++;
5568 j++;
5569 }
5570 /* Entire word processed. Get next word.*/
5571 if ((i % 2) == 1)
5572 k++;
5573 }
5574 err_exit:
5575 lpfc_sli4_mbox_cmd_free(phba, mbox);
5576 return rc;
5577}
5578
5579/**
5580 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5581 * @phba: Pointer to HBA context object.
5582 * @type: the extent's type.
5583 *
5584 * This function deallocates all extents of a particular resource type.
5585 * SLI4 does not allow for deallocating a particular extent range. It
5586 * is the caller's responsibility to release all kernel memory resources.
5587 **/
5588static int
5589lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5590{
5591 int rc;
5592 uint32_t length, mbox_tmo = 0;
5593 LPFC_MBOXQ_t *mbox;
5594 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5595 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5596
5597 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5598 if (!mbox)
5599 return -ENOMEM;
5600
5601 /*
5602 * This function sends an embedded mailbox because it only sends the
5603 * the resource type. All extents of this type are released by the
5604 * port.
5605 */
5606 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5607 sizeof(struct lpfc_sli4_cfg_mhdr));
5608 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5609 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5610 length, LPFC_SLI4_MBX_EMBED);
5611
5612 /* Send an extents count of 0 - the dealloc doesn't use it. */
5613 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5614 LPFC_SLI4_MBX_EMBED);
5615 if (unlikely(rc)) {
5616 rc = -EIO;
5617 goto out_free_mbox;
5618 }
5619 if (!phba->sli4_hba.intr_enable)
5620 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5621 else {
James Smarta183a152011-10-10 21:32:43 -04005622 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005623 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5624 }
5625 if (unlikely(rc)) {
5626 rc = -EIO;
5627 goto out_free_mbox;
5628 }
5629
5630 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5631 if (bf_get(lpfc_mbox_hdr_status,
5632 &dealloc_rsrc->header.cfg_shdr.response)) {
5633 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5634 "2919 Failed to release resource extents "
5635 "for type %d - Status 0x%x Add'l Status 0x%x. "
5636 "Resource memory not released.\n",
5637 type,
5638 bf_get(lpfc_mbox_hdr_status,
5639 &dealloc_rsrc->header.cfg_shdr.response),
5640 bf_get(lpfc_mbox_hdr_add_status,
5641 &dealloc_rsrc->header.cfg_shdr.response));
5642 rc = -EIO;
5643 goto out_free_mbox;
5644 }
5645
5646 /* Release kernel memory resources for the specific type. */
5647 switch (type) {
5648 case LPFC_RSC_TYPE_FCOE_VPI:
5649 kfree(phba->vpi_bmask);
5650 kfree(phba->vpi_ids);
5651 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5652 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5653 &phba->lpfc_vpi_blk_list, list) {
5654 list_del_init(&rsrc_blk->list);
5655 kfree(rsrc_blk);
5656 }
James Smart16a3a202013-04-17 20:14:38 -04005657 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005658 break;
5659 case LPFC_RSC_TYPE_FCOE_XRI:
5660 kfree(phba->sli4_hba.xri_bmask);
5661 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005662 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5663 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5664 list_del_init(&rsrc_blk->list);
5665 kfree(rsrc_blk);
5666 }
5667 break;
5668 case LPFC_RSC_TYPE_FCOE_VFI:
5669 kfree(phba->sli4_hba.vfi_bmask);
5670 kfree(phba->sli4_hba.vfi_ids);
5671 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5672 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5673 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5674 list_del_init(&rsrc_blk->list);
5675 kfree(rsrc_blk);
5676 }
5677 break;
5678 case LPFC_RSC_TYPE_FCOE_RPI:
5679 /* RPI bitmask and physical id array are cleaned up earlier. */
5680 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5681 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5682 list_del_init(&rsrc_blk->list);
5683 kfree(rsrc_blk);
5684 }
5685 break;
5686 default:
5687 break;
5688 }
5689
5690 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5691
5692 out_free_mbox:
5693 mempool_free(mbox, phba->mbox_mem_pool);
5694 return rc;
5695}
5696
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005697static void
James Smart7bdedb32016-07-06 12:36:00 -07005698lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5699 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005700{
James Smart65791f12016-07-06 12:35:56 -07005701 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005702
James Smart65791f12016-07-06 12:35:56 -07005703 len = sizeof(struct lpfc_mbx_set_feature) -
5704 sizeof(struct lpfc_sli4_cfg_mhdr);
5705 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5706 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5707 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005708
James Smart7bdedb32016-07-06 12:36:00 -07005709 switch (feature) {
5710 case LPFC_SET_UE_RECOVERY:
5711 bf_set(lpfc_mbx_set_feature_UER,
5712 &mbox->u.mqe.un.set_feature, 1);
5713 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5714 mbox->u.mqe.un.set_feature.param_len = 8;
5715 break;
5716 case LPFC_SET_MDS_DIAGS:
5717 bf_set(lpfc_mbx_set_feature_mds,
5718 &mbox->u.mqe.un.set_feature, 1);
5719 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5720 &mbox->u.mqe.un.set_feature, 0);
5721 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5722 mbox->u.mqe.un.set_feature.param_len = 8;
5723 break;
James Smart65791f12016-07-06 12:35:56 -07005724 }
James Smart7bdedb32016-07-06 12:36:00 -07005725
5726 return;
James Smart65791f12016-07-06 12:35:56 -07005727}
5728
James Smart6d368e52011-05-24 11:44:12 -04005729/**
5730 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5731 * @phba: Pointer to HBA context object.
5732 *
5733 * This function allocates all SLI4 resource identifiers.
5734 **/
5735int
5736lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5737{
5738 int i, rc, error = 0;
5739 uint16_t count, base;
5740 unsigned long longs;
5741
James Smartff78d8f2011-12-13 13:21:35 -05005742 if (!phba->sli4_hba.rpi_hdrs_in_use)
5743 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005744 if (phba->sli4_hba.extents_in_use) {
5745 /*
5746 * The port supports resource extents. The XRI, VPI, VFI, RPI
5747 * resource extent count must be read and allocated before
5748 * provisioning the resource id arrays.
5749 */
5750 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5751 LPFC_IDX_RSRC_RDY) {
5752 /*
5753 * Extent-based resources are set - the driver could
5754 * be in a port reset. Figure out if any corrective
5755 * actions need to be taken.
5756 */
5757 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5758 LPFC_RSC_TYPE_FCOE_VFI);
5759 if (rc != 0)
5760 error++;
5761 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5762 LPFC_RSC_TYPE_FCOE_VPI);
5763 if (rc != 0)
5764 error++;
5765 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5766 LPFC_RSC_TYPE_FCOE_XRI);
5767 if (rc != 0)
5768 error++;
5769 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5770 LPFC_RSC_TYPE_FCOE_RPI);
5771 if (rc != 0)
5772 error++;
5773
5774 /*
5775 * It's possible that the number of resources
5776 * provided to this port instance changed between
5777 * resets. Detect this condition and reallocate
5778 * resources. Otherwise, there is no action.
5779 */
5780 if (error) {
5781 lpfc_printf_log(phba, KERN_INFO,
5782 LOG_MBOX | LOG_INIT,
5783 "2931 Detected extent resource "
5784 "change. Reallocating all "
5785 "extents.\n");
5786 rc = lpfc_sli4_dealloc_extent(phba,
5787 LPFC_RSC_TYPE_FCOE_VFI);
5788 rc = lpfc_sli4_dealloc_extent(phba,
5789 LPFC_RSC_TYPE_FCOE_VPI);
5790 rc = lpfc_sli4_dealloc_extent(phba,
5791 LPFC_RSC_TYPE_FCOE_XRI);
5792 rc = lpfc_sli4_dealloc_extent(phba,
5793 LPFC_RSC_TYPE_FCOE_RPI);
5794 } else
5795 return 0;
5796 }
5797
5798 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5799 if (unlikely(rc))
5800 goto err_exit;
5801
5802 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5803 if (unlikely(rc))
5804 goto err_exit;
5805
5806 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5807 if (unlikely(rc))
5808 goto err_exit;
5809
5810 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5811 if (unlikely(rc))
5812 goto err_exit;
5813 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5814 LPFC_IDX_RSRC_RDY);
5815 return rc;
5816 } else {
5817 /*
5818 * The port does not support resource extents. The XRI, VPI,
5819 * VFI, RPI resource ids were determined from READ_CONFIG.
5820 * Just allocate the bitmasks and provision the resource id
5821 * arrays. If a port reset is active, the resources don't
5822 * need any action - just exit.
5823 */
5824 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005825 LPFC_IDX_RSRC_RDY) {
5826 lpfc_sli4_dealloc_resource_identifiers(phba);
5827 lpfc_sli4_remove_rpis(phba);
5828 }
James Smart6d368e52011-05-24 11:44:12 -04005829 /* RPIs. */
5830 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005831 if (count <= 0) {
5832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5833 "3279 Invalid provisioning of "
5834 "rpi:%d\n", count);
5835 rc = -EINVAL;
5836 goto err_exit;
5837 }
James Smart6d368e52011-05-24 11:44:12 -04005838 base = phba->sli4_hba.max_cfg_param.rpi_base;
5839 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5840 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5841 sizeof(unsigned long),
5842 GFP_KERNEL);
5843 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5844 rc = -ENOMEM;
5845 goto err_exit;
5846 }
5847 phba->sli4_hba.rpi_ids = kzalloc(count *
5848 sizeof(uint16_t),
5849 GFP_KERNEL);
5850 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5851 rc = -ENOMEM;
5852 goto free_rpi_bmask;
5853 }
5854
5855 for (i = 0; i < count; i++)
5856 phba->sli4_hba.rpi_ids[i] = base + i;
5857
5858 /* VPIs. */
5859 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005860 if (count <= 0) {
5861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5862 "3280 Invalid provisioning of "
5863 "vpi:%d\n", count);
5864 rc = -EINVAL;
5865 goto free_rpi_ids;
5866 }
James Smart6d368e52011-05-24 11:44:12 -04005867 base = phba->sli4_hba.max_cfg_param.vpi_base;
5868 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5869 phba->vpi_bmask = kzalloc(longs *
5870 sizeof(unsigned long),
5871 GFP_KERNEL);
5872 if (unlikely(!phba->vpi_bmask)) {
5873 rc = -ENOMEM;
5874 goto free_rpi_ids;
5875 }
5876 phba->vpi_ids = kzalloc(count *
5877 sizeof(uint16_t),
5878 GFP_KERNEL);
5879 if (unlikely(!phba->vpi_ids)) {
5880 rc = -ENOMEM;
5881 goto free_vpi_bmask;
5882 }
5883
5884 for (i = 0; i < count; i++)
5885 phba->vpi_ids[i] = base + i;
5886
5887 /* XRIs. */
5888 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005889 if (count <= 0) {
5890 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5891 "3281 Invalid provisioning of "
5892 "xri:%d\n", count);
5893 rc = -EINVAL;
5894 goto free_vpi_ids;
5895 }
James Smart6d368e52011-05-24 11:44:12 -04005896 base = phba->sli4_hba.max_cfg_param.xri_base;
5897 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5898 phba->sli4_hba.xri_bmask = kzalloc(longs *
5899 sizeof(unsigned long),
5900 GFP_KERNEL);
5901 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5902 rc = -ENOMEM;
5903 goto free_vpi_ids;
5904 }
James Smart41899be2012-03-01 22:34:19 -05005905 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005906 phba->sli4_hba.xri_ids = kzalloc(count *
5907 sizeof(uint16_t),
5908 GFP_KERNEL);
5909 if (unlikely(!phba->sli4_hba.xri_ids)) {
5910 rc = -ENOMEM;
5911 goto free_xri_bmask;
5912 }
5913
5914 for (i = 0; i < count; i++)
5915 phba->sli4_hba.xri_ids[i] = base + i;
5916
5917 /* VFIs. */
5918 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005919 if (count <= 0) {
5920 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5921 "3282 Invalid provisioning of "
5922 "vfi:%d\n", count);
5923 rc = -EINVAL;
5924 goto free_xri_ids;
5925 }
James Smart6d368e52011-05-24 11:44:12 -04005926 base = phba->sli4_hba.max_cfg_param.vfi_base;
5927 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5928 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5929 sizeof(unsigned long),
5930 GFP_KERNEL);
5931 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5932 rc = -ENOMEM;
5933 goto free_xri_ids;
5934 }
5935 phba->sli4_hba.vfi_ids = kzalloc(count *
5936 sizeof(uint16_t),
5937 GFP_KERNEL);
5938 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5939 rc = -ENOMEM;
5940 goto free_vfi_bmask;
5941 }
5942
5943 for (i = 0; i < count; i++)
5944 phba->sli4_hba.vfi_ids[i] = base + i;
5945
5946 /*
5947 * Mark all resources ready. An HBA reset doesn't need
5948 * to reset the initialization.
5949 */
5950 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5951 LPFC_IDX_RSRC_RDY);
5952 return 0;
5953 }
5954
5955 free_vfi_bmask:
5956 kfree(phba->sli4_hba.vfi_bmask);
5957 free_xri_ids:
5958 kfree(phba->sli4_hba.xri_ids);
5959 free_xri_bmask:
5960 kfree(phba->sli4_hba.xri_bmask);
5961 free_vpi_ids:
5962 kfree(phba->vpi_ids);
5963 free_vpi_bmask:
5964 kfree(phba->vpi_bmask);
5965 free_rpi_ids:
5966 kfree(phba->sli4_hba.rpi_ids);
5967 free_rpi_bmask:
5968 kfree(phba->sli4_hba.rpi_bmask);
5969 err_exit:
5970 return rc;
5971}
5972
5973/**
5974 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5975 * @phba: Pointer to HBA context object.
5976 *
5977 * This function allocates the number of elements for the specified
5978 * resource type.
5979 **/
5980int
5981lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5982{
5983 if (phba->sli4_hba.extents_in_use) {
5984 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5985 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5986 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5987 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5988 } else {
5989 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005990 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005991 kfree(phba->vpi_ids);
5992 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5993 kfree(phba->sli4_hba.xri_bmask);
5994 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005995 kfree(phba->sli4_hba.vfi_bmask);
5996 kfree(phba->sli4_hba.vfi_ids);
5997 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5998 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5999 }
6000
6001 return 0;
6002}
6003
6004/**
James Smartb76f2dc2011-07-22 18:37:42 -04006005 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6006 * @phba: Pointer to HBA context object.
6007 * @type: The resource extent type.
6008 * @extnt_count: buffer to hold port extent count response
6009 * @extnt_size: buffer to hold port extent size response.
6010 *
6011 * This function calls the port to read the host allocated extents
6012 * for a particular type.
6013 **/
6014int
6015lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6016 uint16_t *extnt_cnt, uint16_t *extnt_size)
6017{
6018 bool emb;
6019 int rc = 0;
6020 uint16_t curr_blks = 0;
6021 uint32_t req_len, emb_len;
6022 uint32_t alloc_len, mbox_tmo;
6023 struct list_head *blk_list_head;
6024 struct lpfc_rsrc_blks *rsrc_blk;
6025 LPFC_MBOXQ_t *mbox;
6026 void *virtaddr = NULL;
6027 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6028 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6029 union lpfc_sli4_cfg_shdr *shdr;
6030
6031 switch (type) {
6032 case LPFC_RSC_TYPE_FCOE_VPI:
6033 blk_list_head = &phba->lpfc_vpi_blk_list;
6034 break;
6035 case LPFC_RSC_TYPE_FCOE_XRI:
6036 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6037 break;
6038 case LPFC_RSC_TYPE_FCOE_VFI:
6039 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6040 break;
6041 case LPFC_RSC_TYPE_FCOE_RPI:
6042 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6043 break;
6044 default:
6045 return -EIO;
6046 }
6047
6048 /* Count the number of extents currently allocatd for this type. */
6049 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6050 if (curr_blks == 0) {
6051 /*
6052 * The GET_ALLOCATED mailbox does not return the size,
6053 * just the count. The size should be just the size
6054 * stored in the current allocated block and all sizes
6055 * for an extent type are the same so set the return
6056 * value now.
6057 */
6058 *extnt_size = rsrc_blk->rsrc_size;
6059 }
6060 curr_blks++;
6061 }
6062
James Smartb76f2dc2011-07-22 18:37:42 -04006063 /*
6064 * Calculate the size of an embedded mailbox. The uint32_t
6065 * accounts for extents-specific word.
6066 */
6067 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6068 sizeof(uint32_t);
6069
6070 /*
6071 * Presume the allocation and response will fit into an embedded
6072 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6073 */
6074 emb = LPFC_SLI4_MBX_EMBED;
6075 req_len = emb_len;
6076 if (req_len > emb_len) {
6077 req_len = curr_blks * sizeof(uint16_t) +
6078 sizeof(union lpfc_sli4_cfg_shdr) +
6079 sizeof(uint32_t);
6080 emb = LPFC_SLI4_MBX_NEMBED;
6081 }
6082
6083 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6084 if (!mbox)
6085 return -ENOMEM;
6086 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6087
6088 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6089 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6090 req_len, emb);
6091 if (alloc_len < req_len) {
6092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6093 "2983 Allocated DMA memory size (x%x) is "
6094 "less than the requested DMA memory "
6095 "size (x%x)\n", alloc_len, req_len);
6096 rc = -ENOMEM;
6097 goto err_exit;
6098 }
6099 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6100 if (unlikely(rc)) {
6101 rc = -EIO;
6102 goto err_exit;
6103 }
6104
6105 if (!phba->sli4_hba.intr_enable)
6106 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6107 else {
James Smarta183a152011-10-10 21:32:43 -04006108 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006109 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6110 }
6111
6112 if (unlikely(rc)) {
6113 rc = -EIO;
6114 goto err_exit;
6115 }
6116
6117 /*
6118 * Figure out where the response is located. Then get local pointers
6119 * to the response data. The port does not guarantee to respond to
6120 * all extents counts request so update the local variable with the
6121 * allocated count from the port.
6122 */
6123 if (emb == LPFC_SLI4_MBX_EMBED) {
6124 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6125 shdr = &rsrc_ext->header.cfg_shdr;
6126 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6127 } else {
6128 virtaddr = mbox->sge_array->addr[0];
6129 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6130 shdr = &n_rsrc->cfg_shdr;
6131 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6132 }
6133
6134 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6135 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6136 "2984 Failed to read allocated resources "
6137 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6138 type,
6139 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6140 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6141 rc = -EIO;
6142 goto err_exit;
6143 }
6144 err_exit:
6145 lpfc_sli4_mbox_cmd_free(phba, mbox);
6146 return rc;
6147}
6148
6149/**
James Smart8a9d2e82012-05-09 21:16:12 -04006150 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6151 * @phba: pointer to lpfc hba data structure.
6152 *
6153 * This routine walks the list of els buffers that have been allocated and
6154 * repost them to the port by using SGL block post. This is needed after a
6155 * pci_function_reset/warm_start or start. It attempts to construct blocks
6156 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6157 * SGL block post mailbox commands to post them to the port. For single els
6158 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6159 * mailbox command for posting.
6160 *
6161 * Returns: 0 = success, non-zero failure.
6162 **/
6163static int
6164lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6165{
6166 struct lpfc_sglq *sglq_entry = NULL;
6167 struct lpfc_sglq *sglq_entry_next = NULL;
6168 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04006169 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006170 int last_xritag = NO_XRI;
James Smartdafe8ce2014-09-03 12:56:40 -04006171 struct lpfc_sli_ring *pring;
James Smart8a9d2e82012-05-09 21:16:12 -04006172 LIST_HEAD(prep_sgl_list);
6173 LIST_HEAD(blck_sgl_list);
6174 LIST_HEAD(allc_sgl_list);
6175 LIST_HEAD(post_sgl_list);
6176 LIST_HEAD(free_sgl_list);
6177
James Smartdafe8ce2014-09-03 12:56:40 -04006178 pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart38c20672013-03-01 16:37:44 -05006179 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006180 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006181 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006182 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006183 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006184
James Smart711ea882013-04-17 20:18:29 -04006185 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006186 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6187 &allc_sgl_list, list) {
6188 list_del_init(&sglq_entry->list);
6189 block_cnt++;
6190 if ((last_xritag != NO_XRI) &&
6191 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6192 /* a hole in xri block, form a sgl posting block */
6193 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6194 post_cnt = block_cnt - 1;
6195 /* prepare list for next posting block */
6196 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6197 block_cnt = 1;
6198 } else {
6199 /* prepare list for next posting block */
6200 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6201 /* enough sgls for non-embed sgl mbox command */
6202 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6203 list_splice_init(&prep_sgl_list,
6204 &blck_sgl_list);
6205 post_cnt = block_cnt;
6206 block_cnt = 0;
6207 }
6208 }
6209 num_posted++;
6210
6211 /* keep track of last sgl's xritag */
6212 last_xritag = sglq_entry->sli4_xritag;
6213
6214 /* end of repost sgl list condition for els buffers */
6215 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6216 if (post_cnt == 0) {
6217 list_splice_init(&prep_sgl_list,
6218 &blck_sgl_list);
6219 post_cnt = block_cnt;
6220 } else if (block_cnt == 1) {
6221 status = lpfc_sli4_post_sgl(phba,
6222 sglq_entry->phys, 0,
6223 sglq_entry->sli4_xritag);
6224 if (!status) {
6225 /* successful, put sgl to posted list */
6226 list_add_tail(&sglq_entry->list,
6227 &post_sgl_list);
6228 } else {
6229 /* Failure, put sgl to free list */
6230 lpfc_printf_log(phba, KERN_WARNING,
6231 LOG_SLI,
6232 "3159 Failed to post els "
6233 "sgl, xritag:x%x\n",
6234 sglq_entry->sli4_xritag);
6235 list_add_tail(&sglq_entry->list,
6236 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006237 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006238 }
6239 }
6240 }
6241
6242 /* continue until a nembed page worth of sgls */
6243 if (post_cnt == 0)
6244 continue;
6245
6246 /* post the els buffer list sgls as a block */
6247 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6248 post_cnt);
6249
6250 if (!status) {
6251 /* success, put sgl list to posted sgl list */
6252 list_splice_init(&blck_sgl_list, &post_sgl_list);
6253 } else {
6254 /* Failure, put sgl list to free sgl list */
6255 sglq_entry_first = list_first_entry(&blck_sgl_list,
6256 struct lpfc_sglq,
6257 list);
6258 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6259 "3160 Failed to post els sgl-list, "
6260 "xritag:x%x-x%x\n",
6261 sglq_entry_first->sli4_xritag,
6262 (sglq_entry_first->sli4_xritag +
6263 post_cnt - 1));
6264 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006265 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006266 }
6267
6268 /* don't reset xirtag due to hole in xri block */
6269 if (block_cnt == 0)
6270 last_xritag = NO_XRI;
6271
6272 /* reset els sgl post count for next round of posting */
6273 post_cnt = 0;
6274 }
James Smart711ea882013-04-17 20:18:29 -04006275 /* update the number of XRIs posted for ELS */
6276 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006277
6278 /* free the els sgls failed to post */
6279 lpfc_free_sgl_list(phba, &free_sgl_list);
6280
6281 /* push els sgls posted to the availble list */
6282 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006283 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006284 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006285 list_splice_init(&post_sgl_list,
6286 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006287 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006288 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006289 } else {
6290 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6291 "3161 Failure to post els sgl to port.\n");
6292 return -EIO;
6293 }
6294 return 0;
6295}
6296
James Smart61bda8f2016-10-13 15:06:05 -07006297void
6298lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6299{
6300 uint32_t len;
6301
6302 len = sizeof(struct lpfc_mbx_set_host_data) -
6303 sizeof(struct lpfc_sli4_cfg_mhdr);
6304 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6305 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6306 LPFC_SLI4_MBX_EMBED);
6307
6308 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006309 mbox->u.mqe.un.set_host_data.param_len =
6310 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006311 snprintf(mbox->u.mqe.un.set_host_data.data,
6312 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6313 "Linux %s v"LPFC_DRIVER_VERSION,
6314 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6315}
6316
James Smart8a9d2e82012-05-09 21:16:12 -04006317/**
James Smartda0436e2009-05-22 14:51:39 -04006318 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6319 * @phba: Pointer to HBA context object.
6320 *
6321 * This function is the main SLI4 device intialization PCI function. This
6322 * function is called by the HBA intialization code, HBA reset code and
6323 * HBA error attention handler code. Caller is not required to hold any
6324 * locks.
6325 **/
6326int
6327lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6328{
6329 int rc;
6330 LPFC_MBOXQ_t *mboxq;
6331 struct lpfc_mqe *mqe;
6332 uint8_t *vpd;
6333 uint32_t vpd_size;
6334 uint32_t ftr_rsp = 0;
6335 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6336 struct lpfc_vport *vport = phba->pport;
6337 struct lpfc_dmabuf *mp;
6338
6339 /* Perform a PCI function reset to start from clean */
6340 rc = lpfc_pci_function_reset(phba);
6341 if (unlikely(rc))
6342 return -ENODEV;
6343
6344 /* Check the HBA Host Status Register for readyness */
6345 rc = lpfc_sli4_post_status_check(phba);
6346 if (unlikely(rc))
6347 return -ENODEV;
6348 else {
6349 spin_lock_irq(&phba->hbalock);
6350 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6351 spin_unlock_irq(&phba->hbalock);
6352 }
6353
6354 /*
6355 * Allocate a single mailbox container for initializing the
6356 * port.
6357 */
6358 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6359 if (!mboxq)
6360 return -ENOMEM;
6361
James Smartda0436e2009-05-22 14:51:39 -04006362 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006363 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006364 vpd = kzalloc(vpd_size, GFP_KERNEL);
6365 if (!vpd) {
6366 rc = -ENOMEM;
6367 goto out_free_mbox;
6368 }
6369
6370 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006371 if (unlikely(rc)) {
6372 kfree(vpd);
6373 goto out_free_mbox;
6374 }
James Smart572709e2013-07-15 18:32:43 -04006375
James Smartda0436e2009-05-22 14:51:39 -04006376 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006377 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006378 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006379 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006380 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6381 } else {
James Smart76a95d72010-11-20 23:11:48 -05006382 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006383 }
James Smart45ed1192009-10-02 15:17:02 -04006384
6385 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6386 LPFC_DCBX_CEE_MODE)
6387 phba->hba_flag |= HBA_FIP_SUPPORT;
6388 else
6389 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6390
James Smart4f2e66c2012-05-09 21:17:07 -04006391 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6392
James Smartc31098c2011-04-16 11:03:33 -04006393 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006394 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6395 "0376 READ_REV Error. SLI Level %d "
6396 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006397 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006398 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006399 kfree(vpd);
6400 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006401 }
James Smartcd1c8302011-10-10 21:33:25 -04006402
6403 /*
James Smartff78d8f2011-12-13 13:21:35 -05006404 * Continue initialization with default values even if driver failed
6405 * to read FCoE param config regions, only read parameters if the
6406 * board is FCoE
6407 */
6408 if (phba->hba_flag & HBA_FCOE_MODE &&
6409 lpfc_sli4_read_fcoe_params(phba))
6410 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6411 "2570 Failed to read FCoE parameters\n");
6412
6413 /*
James Smartcd1c8302011-10-10 21:33:25 -04006414 * Retrieve sli4 device physical port name, failure of doing it
6415 * is considered as non-fatal.
6416 */
6417 rc = lpfc_sli4_retrieve_pport_name(phba);
6418 if (!rc)
6419 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6420 "3080 Successful retrieving SLI4 device "
6421 "physical port name: %s.\n", phba->Port);
6422
James Smartda0436e2009-05-22 14:51:39 -04006423 /*
6424 * Evaluate the read rev and vpd data. Populate the driver
6425 * state with the results. If this routine fails, the failure
6426 * is not fatal as the driver will use generic values.
6427 */
6428 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6429 if (unlikely(!rc)) {
6430 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6431 "0377 Error %d parsing vpd. "
6432 "Using defaults.\n", rc);
6433 rc = 0;
6434 }
James Smart76a95d72010-11-20 23:11:48 -05006435 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006436
James Smartf1126682009-06-10 17:22:44 -04006437 /* Save information as VPD data */
6438 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6439 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6440 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6441 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6442 &mqe->un.read_rev);
6443 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6444 &mqe->un.read_rev);
6445 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6446 &mqe->un.read_rev);
6447 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6448 &mqe->un.read_rev);
6449 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6450 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6451 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6452 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6453 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6454 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6455 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6456 "(%d):0380 READ_REV Status x%x "
6457 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6458 mboxq->vport ? mboxq->vport->vpi : 0,
6459 bf_get(lpfc_mqe_status, mqe),
6460 phba->vpd.rev.opFwName,
6461 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6462 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006463
James Smart572709e2013-07-15 18:32:43 -04006464 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6465 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6466 if (phba->pport->cfg_lun_queue_depth > rc) {
6467 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6468 "3362 LUN queue depth changed from %d to %d\n",
6469 phba->pport->cfg_lun_queue_depth, rc);
6470 phba->pport->cfg_lun_queue_depth = rc;
6471 }
6472
James Smart65791f12016-07-06 12:35:56 -07006473 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006474 LPFC_SLI_INTF_IF_TYPE_0) {
6475 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6476 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6477 if (rc == MBX_SUCCESS) {
6478 phba->hba_flag |= HBA_RECOVERABLE_UE;
6479 /* Set 1Sec interval to detect UE */
6480 phba->eratt_poll_interval = 1;
6481 phba->sli4_hba.ue_to_sr = bf_get(
6482 lpfc_mbx_set_feature_UESR,
6483 &mboxq->u.mqe.un.set_feature);
6484 phba->sli4_hba.ue_to_rp = bf_get(
6485 lpfc_mbx_set_feature_UERP,
6486 &mboxq->u.mqe.un.set_feature);
6487 }
6488 }
6489
6490 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6491 /* Enable MDS Diagnostics only if the SLI Port supports it */
6492 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6493 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6494 if (rc != MBX_SUCCESS)
6495 phba->mds_diags_support = 0;
6496 }
James Smart572709e2013-07-15 18:32:43 -04006497
James Smartda0436e2009-05-22 14:51:39 -04006498 /*
6499 * Discover the port's supported feature set and match it against the
6500 * hosts requests.
6501 */
6502 lpfc_request_features(phba, mboxq);
6503 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6504 if (unlikely(rc)) {
6505 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006506 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006507 }
6508
6509 /*
6510 * The port must support FCP initiator mode as this is the
6511 * only mode running in the host.
6512 */
6513 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6514 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6515 "0378 No support for fcpi mode.\n");
6516 ftr_rsp++;
6517 }
James Smartfedd3b72011-02-16 12:39:24 -05006518 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6519 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6520 else
6521 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006522 /*
6523 * If the port cannot support the host's requested features
6524 * then turn off the global config parameters to disable the
6525 * feature in the driver. This is not a fatal error.
6526 */
James Smartbf086112011-08-21 21:48:13 -04006527 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6528 if (phba->cfg_enable_bg) {
6529 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6530 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6531 else
6532 ftr_rsp++;
6533 }
James Smartda0436e2009-05-22 14:51:39 -04006534
6535 if (phba->max_vpi && phba->cfg_enable_npiv &&
6536 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6537 ftr_rsp++;
6538
6539 if (ftr_rsp) {
6540 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6541 "0379 Feature Mismatch Data: x%08x %08x "
6542 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6543 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6544 phba->cfg_enable_npiv, phba->max_vpi);
6545 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6546 phba->cfg_enable_bg = 0;
6547 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6548 phba->cfg_enable_npiv = 0;
6549 }
6550
6551 /* These SLI3 features are assumed in SLI4 */
6552 spin_lock_irq(&phba->hbalock);
6553 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6554 spin_unlock_irq(&phba->hbalock);
6555
James Smart6d368e52011-05-24 11:44:12 -04006556 /*
6557 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6558 * calls depends on these resources to complete port setup.
6559 */
6560 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6561 if (rc) {
6562 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6563 "2920 Failed to alloc Resource IDs "
6564 "rc = x%x\n", rc);
6565 goto out_free_mbox;
6566 }
6567
James Smart61bda8f2016-10-13 15:06:05 -07006568 lpfc_set_host_data(phba, mboxq);
6569
6570 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6571 if (rc) {
6572 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6573 "2134 Failed to set host os driver version %x",
6574 rc);
6575 }
6576
James Smartda0436e2009-05-22 14:51:39 -04006577 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006578 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6579 if (rc) {
6580 phba->link_state = LPFC_HBA_ERROR;
6581 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006582 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006583 }
6584
James Smartda0436e2009-05-22 14:51:39 -04006585 mboxq->vport = vport;
6586 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6587 mp = (struct lpfc_dmabuf *) mboxq->context1;
6588 if (rc == MBX_SUCCESS) {
6589 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6590 rc = 0;
6591 }
6592
6593 /*
6594 * This memory was allocated by the lpfc_read_sparam routine. Release
6595 * it to the mbuf pool.
6596 */
6597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6598 kfree(mp);
6599 mboxq->context1 = NULL;
6600 if (unlikely(rc)) {
6601 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6602 "0382 READ_SPARAM command failed "
6603 "status %d, mbxStatus x%x\n",
6604 rc, bf_get(lpfc_mqe_status, mqe));
6605 phba->link_state = LPFC_HBA_ERROR;
6606 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006607 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006608 }
6609
James Smart05580562011-05-24 11:40:48 -04006610 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006611
6612 /* Update the fc_host data structures with new wwn. */
6613 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6614 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6615
James Smart8a9d2e82012-05-09 21:16:12 -04006616 /* update host els and scsi xri-sgl sizes and mappings */
6617 rc = lpfc_sli4_xri_sgl_update(phba);
6618 if (unlikely(rc)) {
6619 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6620 "1400 Failed to update xri-sgl size and "
6621 "mapping: %d\n", rc);
6622 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006623 }
6624
James Smart8a9d2e82012-05-09 21:16:12 -04006625 /* register the els sgl pool to the port */
6626 rc = lpfc_sli4_repost_els_sgl_list(phba);
6627 if (unlikely(rc)) {
6628 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6629 "0582 Error %d during els sgl post "
6630 "operation\n", rc);
6631 rc = -ENODEV;
6632 goto out_free_mbox;
6633 }
6634
6635 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006636 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6637 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006638 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006639 "0383 Error %d during scsi sgl post "
6640 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006641 /* Some Scsi buffers were moved to the abort scsi list */
6642 /* A pci function reset will repost them */
6643 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006644 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006645 }
6646
6647 /* Post the rpi header region to the device. */
6648 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6649 if (unlikely(rc)) {
6650 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6651 "0393 Error %d during rpi post operation\n",
6652 rc);
6653 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006654 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006655 }
James Smart97f2ecf2012-03-01 22:35:23 -05006656 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006657
James Smart5350d872011-10-10 21:33:49 -04006658 /* Create all the SLI4 queues */
6659 rc = lpfc_sli4_queue_create(phba);
6660 if (rc) {
6661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6662 "3089 Failed to allocate queues\n");
6663 rc = -ENODEV;
6664 goto out_stop_timers;
6665 }
James Smartda0436e2009-05-22 14:51:39 -04006666 /* Set up all the queues to the device */
6667 rc = lpfc_sli4_queue_setup(phba);
6668 if (unlikely(rc)) {
6669 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6670 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006671 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006672 }
6673
6674 /* Arm the CQs and then EQs on device */
6675 lpfc_sli4_arm_cqeq_intr(phba);
6676
6677 /* Indicate device interrupt mode */
6678 phba->sli4_hba.intr_enable = 1;
6679
6680 /* Allow asynchronous mailbox command to go through */
6681 spin_lock_irq(&phba->hbalock);
6682 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6683 spin_unlock_irq(&phba->hbalock);
6684
6685 /* Post receive buffers to the device */
6686 lpfc_sli4_rb_setup(phba);
6687
James Smartfc2b9892010-02-26 14:15:29 -05006688 /* Reset HBA FCF states after HBA reset */
6689 phba->fcf.fcf_flag = 0;
6690 phba->fcf.current_rec.flag = 0;
6691
James Smartda0436e2009-05-22 14:51:39 -04006692 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006693 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006694 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006695
6696 /* Start heart beat timer */
6697 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006698 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006699 phba->hb_outstanding = 0;
6700 phba->last_completion_time = jiffies;
6701
6702 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006703 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006704 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006705
James Smart75baf692010-06-08 18:31:21 -04006706 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6707 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6708 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6709 if (!rc) {
6710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6711 "2829 This device supports "
6712 "Advanced Error Reporting (AER)\n");
6713 spin_lock_irq(&phba->hbalock);
6714 phba->hba_flag |= HBA_AER_ENABLED;
6715 spin_unlock_irq(&phba->hbalock);
6716 } else {
6717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6718 "2830 This device does not support "
6719 "Advanced Error Reporting (AER)\n");
6720 phba->cfg_aer_support = 0;
6721 }
James Smart0a96e972011-07-22 18:37:28 -04006722 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006723 }
6724
James Smart76a95d72010-11-20 23:11:48 -05006725 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6726 /*
6727 * The FC Port needs to register FCFI (index 0)
6728 */
6729 lpfc_reg_fcfi(phba, mboxq);
6730 mboxq->vport = phba->pport;
6731 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006732 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006733 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006734 rc = 0;
6735 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6736 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006737
6738 /* Check if the port is configured to be disabled */
6739 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006740 }
James Smart026abb82011-12-13 13:20:45 -05006741
James Smartda0436e2009-05-22 14:51:39 -04006742 /*
6743 * The port is ready, set the host's link state to LINK_DOWN
6744 * in preparation for link interrupts.
6745 */
James Smartda0436e2009-05-22 14:51:39 -04006746 spin_lock_irq(&phba->hbalock);
6747 phba->link_state = LPFC_LINK_DOWN;
6748 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006749 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6750 (phba->hba_flag & LINK_DISABLED)) {
6751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6752 "3103 Adapter Link is disabled.\n");
6753 lpfc_down_link(phba, mboxq);
6754 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6755 if (rc != MBX_SUCCESS) {
6756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6757 "3104 Adapter failed to issue "
6758 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6759 goto out_unset_queue;
6760 }
6761 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006762 /* don't perform init_link on SLI4 FC port loopback test */
6763 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6764 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6765 if (rc)
6766 goto out_unset_queue;
6767 }
James Smart5350d872011-10-10 21:33:49 -04006768 }
6769 mempool_free(mboxq, phba->mbox_mem_pool);
6770 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006771out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006772 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006773 lpfc_sli4_queue_unset(phba);
6774out_destroy_queue:
6775 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006776out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006777 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006778out_free_mbox:
6779 mempool_free(mboxq, phba->mbox_mem_pool);
6780 return rc;
6781}
James Smarte59058c2008-08-24 21:49:00 -04006782
6783/**
James Smart3621a712009-04-06 18:47:14 -04006784 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006785 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006786 *
James Smarte59058c2008-08-24 21:49:00 -04006787 * This is the callback function for mailbox timer. The mailbox
6788 * timer is armed when a new mailbox command is issued and the timer
6789 * is deleted when the mailbox complete. The function is called by
6790 * the kernel timer code when a mailbox does not complete within
6791 * expected time. This function wakes up the worker thread to
6792 * process the mailbox timeout and returns. All the processing is
6793 * done by the worker thread function lpfc_mbox_timeout_handler.
6794 **/
dea31012005-04-17 16:05:31 -05006795void
6796lpfc_mbox_timeout(unsigned long ptr)
6797{
James Smart92d7f7b2007-06-17 19:56:38 -05006798 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006799 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006800 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006801
James Smart2e0fef82007-06-17 19:56:36 -05006802 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006803 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006804 if (!tmo_posted)
6805 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6806 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6807
James Smart5e9d9b82008-06-14 22:52:53 -04006808 if (!tmo_posted)
6809 lpfc_worker_wake_up(phba);
6810 return;
dea31012005-04-17 16:05:31 -05006811}
6812
James Smarte8d3c3b2013-10-10 12:21:30 -04006813/**
6814 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6815 * are pending
6816 * @phba: Pointer to HBA context object.
6817 *
6818 * This function checks if any mailbox completions are present on the mailbox
6819 * completion queue.
6820 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006821static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006822lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6823{
6824
6825 uint32_t idx;
6826 struct lpfc_queue *mcq;
6827 struct lpfc_mcqe *mcqe;
6828 bool pending_completions = false;
6829
6830 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6831 return false;
6832
6833 /* Check for completions on mailbox completion queue */
6834
6835 mcq = phba->sli4_hba.mbx_cq;
6836 idx = mcq->hba_index;
6837 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6838 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6839 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6840 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6841 pending_completions = true;
6842 break;
6843 }
6844 idx = (idx + 1) % mcq->entry_count;
6845 if (mcq->hba_index == idx)
6846 break;
6847 }
6848 return pending_completions;
6849
6850}
6851
6852/**
6853 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6854 * that were missed.
6855 * @phba: Pointer to HBA context object.
6856 *
6857 * For sli4, it is possible to miss an interrupt. As such mbox completions
6858 * maybe missed causing erroneous mailbox timeouts to occur. This function
6859 * checks to see if mbox completions are on the mailbox completion queue
6860 * and will process all the completions associated with the eq for the
6861 * mailbox completion queue.
6862 **/
6863bool
6864lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6865{
6866
6867 uint32_t eqidx;
6868 struct lpfc_queue *fpeq = NULL;
6869 struct lpfc_eqe *eqe;
6870 bool mbox_pending;
6871
6872 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6873 return false;
6874
6875 /* Find the eq associated with the mcq */
6876
6877 if (phba->sli4_hba.hba_eq)
6878 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6879 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6880 phba->sli4_hba.mbx_cq->assoc_qid) {
6881 fpeq = phba->sli4_hba.hba_eq[eqidx];
6882 break;
6883 }
6884 if (!fpeq)
6885 return false;
6886
6887 /* Turn off interrupts from this EQ */
6888
6889 lpfc_sli4_eq_clr_intr(fpeq);
6890
6891 /* Check to see if a mbox completion is pending */
6892
6893 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6894
6895 /*
6896 * If a mbox completion is pending, process all the events on EQ
6897 * associated with the mbox completion queue (this could include
6898 * mailbox commands, async events, els commands, receive queue data
6899 * and fcp commands)
6900 */
6901
6902 if (mbox_pending)
6903 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6904 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6905 fpeq->EQ_processed++;
6906 }
6907
6908 /* Always clear and re-arm the EQ */
6909
6910 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6911
6912 return mbox_pending;
6913
6914}
James Smarte59058c2008-08-24 21:49:00 -04006915
6916/**
James Smart3621a712009-04-06 18:47:14 -04006917 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006918 * @phba: Pointer to HBA context object.
6919 *
6920 * This function is called from worker thread when a mailbox command times out.
6921 * The caller is not required to hold any locks. This function will reset the
6922 * HBA and recover all the pending commands.
6923 **/
dea31012005-04-17 16:05:31 -05006924void
6925lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6926{
James Smart2e0fef82007-06-17 19:56:36 -05006927 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04006928 MAILBOX_t *mb = NULL;
6929
James Smart1dcb58e2007-04-25 09:51:30 -04006930 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006931
James Smarte8d3c3b2013-10-10 12:21:30 -04006932 /* If the mailbox completed, process the completion and return */
6933 if (lpfc_sli4_process_missed_mbox_completions(phba))
6934 return;
6935
James Smarteb016562014-09-03 12:58:06 -04006936 if (pmbox != NULL)
6937 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04006938 /* Check the pmbox pointer first. There is a race condition
6939 * between the mbox timeout handler getting executed in the
6940 * worklist and the mailbox actually completing. When this
6941 * race condition occurs, the mbox_active will be NULL.
6942 */
6943 spin_lock_irq(&phba->hbalock);
6944 if (pmbox == NULL) {
6945 lpfc_printf_log(phba, KERN_WARNING,
6946 LOG_MBOX | LOG_SLI,
6947 "0353 Active Mailbox cleared - mailbox timeout "
6948 "exiting\n");
6949 spin_unlock_irq(&phba->hbalock);
6950 return;
6951 }
6952
dea31012005-04-17 16:05:31 -05006953 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006955 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006956 mb->mbxCommand,
6957 phba->pport->port_state,
6958 phba->sli.sli_flag,
6959 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006960 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006961
James Smart1dcb58e2007-04-25 09:51:30 -04006962 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6963 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006964 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006965 */
James Smart2e0fef82007-06-17 19:56:36 -05006966 spin_lock_irq(&phba->pport->work_port_lock);
6967 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6968 spin_unlock_irq(&phba->pport->work_port_lock);
6969 spin_lock_irq(&phba->hbalock);
6970 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006971 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006972 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006973
James Smartdb55fba2014-04-04 13:52:02 -04006974 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04006975
6976 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006977 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006978
6979 /* Reset the HBA device */
6980 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006981}
6982
James Smarte59058c2008-08-24 21:49:00 -04006983/**
James Smart3772a992009-05-22 14:50:54 -04006984 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006985 * @phba: Pointer to HBA context object.
6986 * @pmbox: Pointer to mailbox object.
6987 * @flag: Flag indicating how the mailbox need to be processed.
6988 *
6989 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006990 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6991 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006992 * The mailbox command can be submitted in polling mode, in which case
6993 * this function will wait in a polling loop for the completion of the
6994 * mailbox.
6995 * If the mailbox is submitted in no_wait mode (not polling) the
6996 * function will submit the command and returns immediately without waiting
6997 * for the mailbox completion. The no_wait is supported only when HBA
6998 * is in SLI2/SLI3 mode - interrupts are enabled.
6999 * The SLI interface allows only one mailbox pending at a time. If the
7000 * mailbox is issued in polling mode and there is already a mailbox
7001 * pending, then the function will return an error. If the mailbox is issued
7002 * in NO_WAIT mode and there is a mailbox pending already, the function
7003 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7004 * The sli layer owns the mailbox object until the completion of mailbox
7005 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7006 * return codes the caller owns the mailbox command after the return of
7007 * the function.
7008 **/
James Smart3772a992009-05-22 14:50:54 -04007009static int
7010lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7011 uint32_t flag)
dea31012005-04-17 16:05:31 -05007012{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007013 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007014 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007015 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007016 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007017 int i;
James Smart09372822008-01-11 01:52:54 -05007018 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007019 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007020 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007021 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007022 int processing_queue = 0;
7023
7024 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7025 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007026 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007027 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007028 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7029 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7030 return MBX_SUCCESS;
7031 }
James Smart58da1ff2008-04-07 10:15:56 -04007032 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007033 pmbox = lpfc_mbox_get(phba);
7034 if (!pmbox) {
7035 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7036 return MBX_SUCCESS;
7037 }
7038 }
dea31012005-04-17 16:05:31 -05007039
James Smarted957682007-06-17 19:56:37 -05007040 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007041 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007042 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007043 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007044 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007045 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007046 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007047 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007048 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007049 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007050 }
7051 }
7052
Linas Vepstas8d63f372007-02-14 14:28:36 -06007053 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007054 if (unlikely(pci_channel_offline(phba->pcidev))) {
7055 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7056 goto out_not_finished;
7057 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007058
James Smarta257bf92009-04-06 18:48:10 -04007059 /* If HBA has a deferred error attention, fail the iocb. */
7060 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7061 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7062 goto out_not_finished;
7063 }
7064
dea31012005-04-17 16:05:31 -05007065 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007066
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007067 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007068 status = MBX_SUCCESS;
7069
James Smart2e0fef82007-06-17 19:56:36 -05007070 if (phba->link_state == LPFC_HBA_ERROR) {
7071 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007072
7073 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007074 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7075 "(%d):0311 Mailbox command x%x cannot "
7076 "issue Data: x%x x%x\n",
7077 pmbox->vport ? pmbox->vport->vpi : 0,
7078 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007079 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007080 }
7081
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007082 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007083 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7084 !(hc_copy & HC_MBINT_ENA)) {
7085 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7086 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007087 "(%d):2528 Mailbox command x%x cannot "
7088 "issue Data: x%x x%x\n",
7089 pmbox->vport ? pmbox->vport->vpi : 0,
7090 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007091 goto out_not_finished;
7092 }
James Smart92908312006-03-07 15:04:13 -05007093 }
7094
dea31012005-04-17 16:05:31 -05007095 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7096 /* Polling for a mbox command when another one is already active
7097 * is not allowed in SLI. Also, the driver must have established
7098 * SLI2 mode to queue and process multiple mbox commands.
7099 */
7100
7101 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007102 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007103
7104 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007105 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7106 "(%d):2529 Mailbox command x%x "
7107 "cannot issue Data: x%x x%x\n",
7108 pmbox->vport ? pmbox->vport->vpi : 0,
7109 pmbox->u.mb.mbxCommand,
7110 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007111 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007112 }
7113
James Smart3772a992009-05-22 14:50:54 -04007114 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007115 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007116 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007117 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7118 "(%d):2530 Mailbox command x%x "
7119 "cannot issue Data: x%x x%x\n",
7120 pmbox->vport ? pmbox->vport->vpi : 0,
7121 pmbox->u.mb.mbxCommand,
7122 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007123 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007124 }
7125
dea31012005-04-17 16:05:31 -05007126 /* Another mailbox command is still being processed, queue this
7127 * command to be processed later.
7128 */
7129 lpfc_mbox_put(phba, pmbox);
7130
7131 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007132 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007133 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007134 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007135 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007136 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007137 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007138
7139 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007140 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007141
James Smart858c9f62007-06-17 19:56:39 -05007142 if (pmbox->vport) {
7143 lpfc_debugfs_disc_trc(pmbox->vport,
7144 LPFC_DISC_TRC_MBOX_VPORT,
7145 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007146 (uint32_t)mbx->mbxCommand,
7147 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007148 }
7149 else {
7150 lpfc_debugfs_disc_trc(phba->pport,
7151 LPFC_DISC_TRC_MBOX,
7152 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007153 (uint32_t)mbx->mbxCommand,
7154 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007155 }
7156
James Smart2e0fef82007-06-17 19:56:36 -05007157 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007158 }
7159
dea31012005-04-17 16:05:31 -05007160 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7161
7162 /* If we are not polling, we MUST be in SLI2 mode */
7163 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007164 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007165 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007166 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007167 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007168 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007169 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7170 "(%d):2531 Mailbox command x%x "
7171 "cannot issue Data: x%x x%x\n",
7172 pmbox->vport ? pmbox->vport->vpi : 0,
7173 pmbox->u.mb.mbxCommand,
7174 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007175 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007176 }
7177 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007178 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7179 1000);
7180 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007181 }
7182
7183 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007184 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007185 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007186 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007187 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007188 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007189 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007190
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007191 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007192 if (pmbox->vport) {
7193 lpfc_debugfs_disc_trc(pmbox->vport,
7194 LPFC_DISC_TRC_MBOX_VPORT,
7195 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007196 (uint32_t)mbx->mbxCommand,
7197 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007198 }
7199 else {
7200 lpfc_debugfs_disc_trc(phba->pport,
7201 LPFC_DISC_TRC_MBOX,
7202 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007203 (uint32_t)mbx->mbxCommand,
7204 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007205 }
7206 }
7207
dea31012005-04-17 16:05:31 -05007208 psli->slistat.mbox_cmd++;
7209 evtctr = psli->slistat.mbox_event;
7210
7211 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007212 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007213
James Smart3772a992009-05-22 14:50:54 -04007214 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007215 /* Populate mbox extension offset word. */
7216 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007217 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007218 = (uint8_t *)phba->mbox_ext
7219 - (uint8_t *)phba->mbox;
7220 }
7221
7222 /* Copy the mailbox extension data */
7223 if (pmbox->in_ext_byte_len && pmbox->context2) {
7224 lpfc_sli_pcimem_bcopy(pmbox->context2,
7225 (uint8_t *)phba->mbox_ext,
7226 pmbox->in_ext_byte_len);
7227 }
7228 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007229 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007230 } else {
James Smart7a470272010-03-15 11:25:20 -04007231 /* Populate mbox extension offset word. */
7232 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007233 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007234 = MAILBOX_HBA_EXT_OFFSET;
7235
7236 /* Copy the mailbox extension data */
7237 if (pmbox->in_ext_byte_len && pmbox->context2) {
7238 lpfc_memcpy_to_slim(phba->MBslimaddr +
7239 MAILBOX_HBA_EXT_OFFSET,
7240 pmbox->context2, pmbox->in_ext_byte_len);
7241
7242 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007243 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007244 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007245 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007246 }
7247
7248 /* First copy mbox command data to HBA SLIM, skip past first
7249 word */
7250 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007251 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007252 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7253
7254 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007255 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007256 to_slim = phba->MBslimaddr;
7257 writel(ldata, to_slim);
7258 readl(to_slim); /* flush */
7259
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007260 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007261 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007262 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007263 }
7264 }
7265
7266 wmb();
dea31012005-04-17 16:05:31 -05007267
7268 switch (flag) {
7269 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007270 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007271 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007272 /* Interrupt board to do it */
7273 writel(CA_MBATT, phba->CAregaddr);
7274 readl(phba->CAregaddr); /* flush */
7275 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007276 break;
7277
7278 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007279 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007280 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007281 /* Interrupt board to do it */
7282 writel(CA_MBATT, phba->CAregaddr);
7283 readl(phba->CAregaddr); /* flush */
7284
James Smart3772a992009-05-22 14:50:54 -04007285 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007286 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007287 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007288 word0 = le32_to_cpu(word0);
7289 } else {
7290 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007291 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7292 spin_unlock_irqrestore(&phba->hbalock,
7293 drvr_flag);
7294 goto out_not_finished;
7295 }
dea31012005-04-17 16:05:31 -05007296 }
7297
7298 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007299 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7300 spin_unlock_irqrestore(&phba->hbalock,
7301 drvr_flag);
7302 goto out_not_finished;
7303 }
James Smarta183a152011-10-10 21:32:43 -04007304 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7305 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007306 i = 0;
dea31012005-04-17 16:05:31 -05007307 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007308 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7309 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007310 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007311 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007312 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007313 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007314 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007315 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007316 }
7317
7318 /* Check if we took a mbox interrupt while we were
7319 polling */
7320 if (((word0 & OWN_CHIP) != OWN_CHIP)
7321 && (evtctr != psli->slistat.mbox_event))
7322 break;
7323
James Smart09372822008-01-11 01:52:54 -05007324 if (i++ > 10) {
7325 spin_unlock_irqrestore(&phba->hbalock,
7326 drvr_flag);
7327 msleep(1);
7328 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7329 }
dea31012005-04-17 16:05:31 -05007330
James Smart3772a992009-05-22 14:50:54 -04007331 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007332 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007333 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007334 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007335 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007336 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007337 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007338 /* Check real SLIM for any errors */
7339 slimword0 = readl(phba->MBslimaddr);
7340 slimmb = (MAILBOX_t *) & slimword0;
7341 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7342 && slimmb->mbxStatus) {
7343 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007344 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007345 word0 = slimword0;
7346 }
7347 }
7348 } else {
7349 /* First copy command data */
7350 word0 = readl(phba->MBslimaddr);
7351 }
7352 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007353 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7354 spin_unlock_irqrestore(&phba->hbalock,
7355 drvr_flag);
7356 goto out_not_finished;
7357 }
dea31012005-04-17 16:05:31 -05007358 }
7359
James Smart3772a992009-05-22 14:50:54 -04007360 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007361 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007362 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007363 /* Copy the mailbox extension data */
7364 if (pmbox->out_ext_byte_len && pmbox->context2) {
7365 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7366 pmbox->context2,
7367 pmbox->out_ext_byte_len);
7368 }
dea31012005-04-17 16:05:31 -05007369 } else {
7370 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007371 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007372 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007373 /* Copy the mailbox extension data */
7374 if (pmbox->out_ext_byte_len && pmbox->context2) {
7375 lpfc_memcpy_from_slim(pmbox->context2,
7376 phba->MBslimaddr +
7377 MAILBOX_HBA_EXT_OFFSET,
7378 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007379 }
7380 }
7381
7382 writel(HA_MBATT, phba->HAregaddr);
7383 readl(phba->HAregaddr); /* flush */
7384
7385 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007386 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007387 }
7388
James Smart2e0fef82007-06-17 19:56:36 -05007389 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7390 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007391
7392out_not_finished:
7393 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007394 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007395 lpfc_mbox_cmpl_put(phba, pmbox);
7396 }
7397 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007398}
7399
James Smarte59058c2008-08-24 21:49:00 -04007400/**
James Smartf1126682009-06-10 17:22:44 -04007401 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7402 * @phba: Pointer to HBA context object.
7403 *
7404 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7405 * the driver internal pending mailbox queue. It will then try to wait out the
7406 * possible outstanding mailbox command before return.
7407 *
7408 * Returns:
7409 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7410 * the outstanding mailbox command timed out.
7411 **/
7412static int
7413lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7414{
7415 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007416 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007417 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007418
7419 /* Mark the asynchronous mailbox command posting as blocked */
7420 spin_lock_irq(&phba->hbalock);
7421 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007422 /* Determine how long we might wait for the active mailbox
7423 * command to be gracefully completed by firmware.
7424 */
James Smarta183a152011-10-10 21:32:43 -04007425 if (phba->sli.mbox_active)
7426 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7427 phba->sli.mbox_active) *
7428 1000) + jiffies;
7429 spin_unlock_irq(&phba->hbalock);
7430
James Smarte8d3c3b2013-10-10 12:21:30 -04007431 /* Make sure the mailbox is really active */
7432 if (timeout)
7433 lpfc_sli4_process_missed_mbox_completions(phba);
7434
James Smartf1126682009-06-10 17:22:44 -04007435 /* Wait for the outstnading mailbox command to complete */
7436 while (phba->sli.mbox_active) {
7437 /* Check active mailbox complete status every 2ms */
7438 msleep(2);
7439 if (time_after(jiffies, timeout)) {
7440 /* Timeout, marked the outstanding cmd not complete */
7441 rc = 1;
7442 break;
7443 }
7444 }
7445
7446 /* Can not cleanly block async mailbox command, fails it */
7447 if (rc) {
7448 spin_lock_irq(&phba->hbalock);
7449 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7450 spin_unlock_irq(&phba->hbalock);
7451 }
7452 return rc;
7453}
7454
7455/**
7456 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7457 * @phba: Pointer to HBA context object.
7458 *
7459 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7460 * commands from the driver internal pending mailbox queue. It makes sure
7461 * that there is no outstanding mailbox command before resuming posting
7462 * asynchronous mailbox commands. If, for any reason, there is outstanding
7463 * mailbox command, it will try to wait it out before resuming asynchronous
7464 * mailbox command posting.
7465 **/
7466static void
7467lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7468{
7469 struct lpfc_sli *psli = &phba->sli;
7470
7471 spin_lock_irq(&phba->hbalock);
7472 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7473 /* Asynchronous mailbox posting is not blocked, do nothing */
7474 spin_unlock_irq(&phba->hbalock);
7475 return;
7476 }
7477
7478 /* Outstanding synchronous mailbox command is guaranteed to be done,
7479 * successful or timeout, after timing-out the outstanding mailbox
7480 * command shall always be removed, so just unblock posting async
7481 * mailbox command and resume
7482 */
7483 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7484 spin_unlock_irq(&phba->hbalock);
7485
7486 /* wake up worker thread to post asynchronlous mailbox command */
7487 lpfc_worker_wake_up(phba);
7488}
7489
7490/**
James Smart2d843ed2012-09-29 11:29:06 -04007491 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7492 * @phba: Pointer to HBA context object.
7493 * @mboxq: Pointer to mailbox object.
7494 *
7495 * The function waits for the bootstrap mailbox register ready bit from
7496 * port for twice the regular mailbox command timeout value.
7497 *
7498 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7499 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7500 **/
7501static int
7502lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7503{
7504 uint32_t db_ready;
7505 unsigned long timeout;
7506 struct lpfc_register bmbx_reg;
7507
7508 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7509 * 1000) + jiffies;
7510
7511 do {
7512 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7513 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7514 if (!db_ready)
7515 msleep(2);
7516
7517 if (time_after(jiffies, timeout))
7518 return MBXERR_ERROR;
7519 } while (!db_ready);
7520
7521 return 0;
7522}
7523
7524/**
James Smartda0436e2009-05-22 14:51:39 -04007525 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7526 * @phba: Pointer to HBA context object.
7527 * @mboxq: Pointer to mailbox object.
7528 *
7529 * The function posts a mailbox to the port. The mailbox is expected
7530 * to be comletely filled in and ready for the port to operate on it.
7531 * This routine executes a synchronous completion operation on the
7532 * mailbox by polling for its completion.
7533 *
7534 * The caller must not be holding any locks when calling this routine.
7535 *
7536 * Returns:
7537 * MBX_SUCCESS - mailbox posted successfully
7538 * Any of the MBX error values.
7539 **/
7540static int
7541lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7542{
7543 int rc = MBX_SUCCESS;
7544 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007545 uint32_t mcqe_status;
7546 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007547 struct lpfc_sli *psli = &phba->sli;
7548 struct lpfc_mqe *mb = &mboxq->u.mqe;
7549 struct lpfc_bmbx_create *mbox_rgn;
7550 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007551
7552 /*
7553 * Only one mailbox can be active to the bootstrap mailbox region
7554 * at a time and there is no queueing provided.
7555 */
7556 spin_lock_irqsave(&phba->hbalock, iflag);
7557 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7558 spin_unlock_irqrestore(&phba->hbalock, iflag);
7559 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007560 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007561 "cannot issue Data: x%x x%x\n",
7562 mboxq->vport ? mboxq->vport->vpi : 0,
7563 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007564 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7565 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007566 psli->sli_flag, MBX_POLL);
7567 return MBXERR_ERROR;
7568 }
7569 /* The server grabs the token and owns it until release */
7570 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7571 phba->sli.mbox_active = mboxq;
7572 spin_unlock_irqrestore(&phba->hbalock, iflag);
7573
James Smart2d843ed2012-09-29 11:29:06 -04007574 /* wait for bootstrap mbox register for readyness */
7575 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7576 if (rc)
7577 goto exit;
7578
James Smartda0436e2009-05-22 14:51:39 -04007579 /*
7580 * Initialize the bootstrap memory region to avoid stale data areas
7581 * in the mailbox post. Then copy the caller's mailbox contents to
7582 * the bmbx mailbox region.
7583 */
7584 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7585 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7586 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7587 sizeof(struct lpfc_mqe));
7588
7589 /* Post the high mailbox dma address to the port and wait for ready. */
7590 dma_address = &phba->sli4_hba.bmbx.dma_address;
7591 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7592
James Smart2d843ed2012-09-29 11:29:06 -04007593 /* wait for bootstrap mbox register for hi-address write done */
7594 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7595 if (rc)
7596 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007597
7598 /* Post the low mailbox dma address to the port. */
7599 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007600
James Smart2d843ed2012-09-29 11:29:06 -04007601 /* wait for bootstrap mbox register for low address write done */
7602 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7603 if (rc)
7604 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007605
7606 /*
7607 * Read the CQ to ensure the mailbox has completed.
7608 * If so, update the mailbox status so that the upper layers
7609 * can complete the request normally.
7610 */
7611 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7612 sizeof(struct lpfc_mqe));
7613 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7614 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7615 sizeof(struct lpfc_mcqe));
7616 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007617 /*
7618 * When the CQE status indicates a failure and the mailbox status
7619 * indicates success then copy the CQE status into the mailbox status
7620 * (and prefix it with x4000).
7621 */
James Smartda0436e2009-05-22 14:51:39 -04007622 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007623 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7624 bf_set(lpfc_mqe_status, mb,
7625 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007626 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007627 } else
7628 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007629
7630 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007631 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007632 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7633 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007634 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7635 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7636 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007637 bf_get(lpfc_mqe_status, mb),
7638 mb->un.mb_words[0], mb->un.mb_words[1],
7639 mb->un.mb_words[2], mb->un.mb_words[3],
7640 mb->un.mb_words[4], mb->un.mb_words[5],
7641 mb->un.mb_words[6], mb->un.mb_words[7],
7642 mb->un.mb_words[8], mb->un.mb_words[9],
7643 mb->un.mb_words[10], mb->un.mb_words[11],
7644 mb->un.mb_words[12], mboxq->mcqe.word0,
7645 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7646 mboxq->mcqe.trailer);
7647exit:
7648 /* We are holding the token, no needed for lock when release */
7649 spin_lock_irqsave(&phba->hbalock, iflag);
7650 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7651 phba->sli.mbox_active = NULL;
7652 spin_unlock_irqrestore(&phba->hbalock, iflag);
7653 return rc;
7654}
7655
7656/**
7657 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7658 * @phba: Pointer to HBA context object.
7659 * @pmbox: Pointer to mailbox object.
7660 * @flag: Flag indicating how the mailbox need to be processed.
7661 *
7662 * This function is called by discovery code and HBA management code to submit
7663 * a mailbox command to firmware with SLI-4 interface spec.
7664 *
7665 * Return codes the caller owns the mailbox command after the return of the
7666 * function.
7667 **/
7668static int
7669lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7670 uint32_t flag)
7671{
7672 struct lpfc_sli *psli = &phba->sli;
7673 unsigned long iflags;
7674 int rc;
7675
James Smartb76f2dc2011-07-22 18:37:42 -04007676 /* dump from issue mailbox command if setup */
7677 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7678
James Smart8fa38512009-07-19 10:01:03 -04007679 rc = lpfc_mbox_dev_check(phba);
7680 if (unlikely(rc)) {
7681 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007682 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007683 "cannot issue Data: x%x x%x\n",
7684 mboxq->vport ? mboxq->vport->vpi : 0,
7685 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007686 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7687 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007688 psli->sli_flag, flag);
7689 goto out_not_finished;
7690 }
7691
James Smartda0436e2009-05-22 14:51:39 -04007692 /* Detect polling mode and jump to a handler */
7693 if (!phba->sli4_hba.intr_enable) {
7694 if (flag == MBX_POLL)
7695 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7696 else
7697 rc = -EIO;
7698 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007699 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007700 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007701 "(x%x/x%x) failure: "
7702 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7703 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007704 mboxq->vport ? mboxq->vport->vpi : 0,
7705 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007706 lpfc_sli_config_mbox_subsys_get(phba,
7707 mboxq),
7708 lpfc_sli_config_mbox_opcode_get(phba,
7709 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007710 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7711 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7712 bf_get(lpfc_mcqe_ext_status,
7713 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007714 psli->sli_flag, flag);
7715 return rc;
7716 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007717 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7718 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007719 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007720 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007721 mboxq->vport ? mboxq->vport->vpi : 0,
7722 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007723 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7724 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007725 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007726 /* Try to block the asynchronous mailbox posting */
7727 rc = lpfc_sli4_async_mbox_block(phba);
7728 if (!rc) {
7729 /* Successfully blocked, now issue sync mbox cmd */
7730 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7731 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007732 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007733 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007734 "(%d):2597 Sync Mailbox command "
7735 "x%x (x%x/x%x) failure: "
7736 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7737 "Data: x%x x%x\n,",
7738 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007739 mboxq->u.mb.mbxCommand,
7740 lpfc_sli_config_mbox_subsys_get(phba,
7741 mboxq),
7742 lpfc_sli_config_mbox_opcode_get(phba,
7743 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007744 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7745 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7746 bf_get(lpfc_mcqe_ext_status,
7747 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007748 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007749 /* Unblock the async mailbox posting afterward */
7750 lpfc_sli4_async_mbox_unblock(phba);
7751 }
7752 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007753 }
7754
7755 /* Now, interrupt mode asynchrous mailbox command */
7756 rc = lpfc_mbox_cmd_check(phba, mboxq);
7757 if (rc) {
7758 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007759 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007760 "cannot issue Data: x%x x%x\n",
7761 mboxq->vport ? mboxq->vport->vpi : 0,
7762 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007763 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7764 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007765 psli->sli_flag, flag);
7766 goto out_not_finished;
7767 }
James Smartda0436e2009-05-22 14:51:39 -04007768
7769 /* Put the mailbox command to the driver internal FIFO */
7770 psli->slistat.mbox_busy++;
7771 spin_lock_irqsave(&phba->hbalock, iflags);
7772 lpfc_mbox_put(phba, mboxq);
7773 spin_unlock_irqrestore(&phba->hbalock, iflags);
7774 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7775 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007776 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007777 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7778 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007779 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7780 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007781 phba->pport->port_state,
7782 psli->sli_flag, MBX_NOWAIT);
7783 /* Wake up worker thread to transport mailbox command from head */
7784 lpfc_worker_wake_up(phba);
7785
7786 return MBX_BUSY;
7787
7788out_not_finished:
7789 return MBX_NOT_FINISHED;
7790}
7791
7792/**
7793 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7794 * @phba: Pointer to HBA context object.
7795 *
7796 * This function is called by worker thread to send a mailbox command to
7797 * SLI4 HBA firmware.
7798 *
7799 **/
7800int
7801lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7802{
7803 struct lpfc_sli *psli = &phba->sli;
7804 LPFC_MBOXQ_t *mboxq;
7805 int rc = MBX_SUCCESS;
7806 unsigned long iflags;
7807 struct lpfc_mqe *mqe;
7808 uint32_t mbx_cmnd;
7809
7810 /* Check interrupt mode before post async mailbox command */
7811 if (unlikely(!phba->sli4_hba.intr_enable))
7812 return MBX_NOT_FINISHED;
7813
7814 /* Check for mailbox command service token */
7815 spin_lock_irqsave(&phba->hbalock, iflags);
7816 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7817 spin_unlock_irqrestore(&phba->hbalock, iflags);
7818 return MBX_NOT_FINISHED;
7819 }
7820 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7821 spin_unlock_irqrestore(&phba->hbalock, iflags);
7822 return MBX_NOT_FINISHED;
7823 }
7824 if (unlikely(phba->sli.mbox_active)) {
7825 spin_unlock_irqrestore(&phba->hbalock, iflags);
7826 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7827 "0384 There is pending active mailbox cmd\n");
7828 return MBX_NOT_FINISHED;
7829 }
7830 /* Take the mailbox command service token */
7831 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7832
7833 /* Get the next mailbox command from head of queue */
7834 mboxq = lpfc_mbox_get(phba);
7835
7836 /* If no more mailbox command waiting for post, we're done */
7837 if (!mboxq) {
7838 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7839 spin_unlock_irqrestore(&phba->hbalock, iflags);
7840 return MBX_SUCCESS;
7841 }
7842 phba->sli.mbox_active = mboxq;
7843 spin_unlock_irqrestore(&phba->hbalock, iflags);
7844
7845 /* Check device readiness for posting mailbox command */
7846 rc = lpfc_mbox_dev_check(phba);
7847 if (unlikely(rc))
7848 /* Driver clean routine will clean up pending mailbox */
7849 goto out_not_finished;
7850
7851 /* Prepare the mbox command to be posted */
7852 mqe = &mboxq->u.mqe;
7853 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7854
7855 /* Start timer for the mbox_tmo and log some mailbox post messages */
7856 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007857 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007858
7859 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007860 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007861 "x%x x%x\n",
7862 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007863 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7864 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007865 phba->pport->port_state, psli->sli_flag);
7866
7867 if (mbx_cmnd != MBX_HEARTBEAT) {
7868 if (mboxq->vport) {
7869 lpfc_debugfs_disc_trc(mboxq->vport,
7870 LPFC_DISC_TRC_MBOX_VPORT,
7871 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7872 mbx_cmnd, mqe->un.mb_words[0],
7873 mqe->un.mb_words[1]);
7874 } else {
7875 lpfc_debugfs_disc_trc(phba->pport,
7876 LPFC_DISC_TRC_MBOX,
7877 "MBOX Send: cmd:x%x mb:x%x x%x",
7878 mbx_cmnd, mqe->un.mb_words[0],
7879 mqe->un.mb_words[1]);
7880 }
7881 }
7882 psli->slistat.mbox_cmd++;
7883
7884 /* Post the mailbox command to the port */
7885 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7886 if (rc != MBX_SUCCESS) {
7887 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007888 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007889 "cannot issue Data: x%x x%x\n",
7890 mboxq->vport ? mboxq->vport->vpi : 0,
7891 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007892 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7893 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007894 psli->sli_flag, MBX_NOWAIT);
7895 goto out_not_finished;
7896 }
7897
7898 return rc;
7899
7900out_not_finished:
7901 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007902 if (phba->sli.mbox_active) {
7903 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7904 __lpfc_mbox_cmpl_put(phba, mboxq);
7905 /* Release the token */
7906 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7907 phba->sli.mbox_active = NULL;
7908 }
James Smartda0436e2009-05-22 14:51:39 -04007909 spin_unlock_irqrestore(&phba->hbalock, iflags);
7910
7911 return MBX_NOT_FINISHED;
7912}
7913
7914/**
7915 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7916 * @phba: Pointer to HBA context object.
7917 * @pmbox: Pointer to mailbox object.
7918 * @flag: Flag indicating how the mailbox need to be processed.
7919 *
7920 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7921 * the API jump table function pointer from the lpfc_hba struct.
7922 *
7923 * Return codes the caller owns the mailbox command after the return of the
7924 * function.
7925 **/
7926int
7927lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7928{
7929 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7930}
7931
7932/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007933 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007934 * @phba: The hba struct for which this call is being executed.
7935 * @dev_grp: The HBA PCI-Device group number.
7936 *
7937 * This routine sets up the mbox interface API function jump table in @phba
7938 * struct.
7939 * Returns: 0 - success, -ENODEV - failure.
7940 **/
7941int
7942lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7943{
7944
7945 switch (dev_grp) {
7946 case LPFC_PCI_DEV_LP:
7947 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7948 phba->lpfc_sli_handle_slow_ring_event =
7949 lpfc_sli_handle_slow_ring_event_s3;
7950 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7951 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7952 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7953 break;
7954 case LPFC_PCI_DEV_OC:
7955 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7956 phba->lpfc_sli_handle_slow_ring_event =
7957 lpfc_sli_handle_slow_ring_event_s4;
7958 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7959 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7960 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7961 break;
7962 default:
7963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7964 "1420 Invalid HBA PCI-device group: 0x%x\n",
7965 dev_grp);
7966 return -ENODEV;
7967 break;
7968 }
7969 return 0;
7970}
7971
7972/**
James Smart3621a712009-04-06 18:47:14 -04007973 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007974 * @phba: Pointer to HBA context object.
7975 * @pring: Pointer to driver SLI ring object.
7976 * @piocb: Pointer to address of newly added command iocb.
7977 *
7978 * This function is called with hbalock held to add a command
7979 * iocb to the txq when SLI layer cannot submit the command iocb
7980 * to the ring.
7981 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007982void
James Smart92d7f7b2007-06-17 19:56:38 -05007983__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007984 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007985{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007986 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007987 /* Insert the caller's iocb in the txq tail for later processing. */
7988 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007989}
7990
James Smarte59058c2008-08-24 21:49:00 -04007991/**
James Smart3621a712009-04-06 18:47:14 -04007992 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007993 * @phba: Pointer to HBA context object.
7994 * @pring: Pointer to driver SLI ring object.
7995 * @piocb: Pointer to address of newly added command iocb.
7996 *
7997 * This function is called with hbalock held before a new
7998 * iocb is submitted to the firmware. This function checks
7999 * txq to flush the iocbs in txq to Firmware before
8000 * submitting new iocbs to the Firmware.
8001 * If there are iocbs in the txq which need to be submitted
8002 * to firmware, lpfc_sli_next_iocb returns the first element
8003 * of the txq after dequeuing it from txq.
8004 * If there is no iocb in the txq then the function will return
8005 * *piocb and *piocb is set to NULL. Caller needs to check
8006 * *piocb to find if there are more commands in the txq.
8007 **/
dea31012005-04-17 16:05:31 -05008008static struct lpfc_iocbq *
8009lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008010 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008011{
8012 struct lpfc_iocbq * nextiocb;
8013
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008014 lockdep_assert_held(&phba->hbalock);
8015
dea31012005-04-17 16:05:31 -05008016 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8017 if (!nextiocb) {
8018 nextiocb = *piocb;
8019 *piocb = NULL;
8020 }
8021
8022 return nextiocb;
8023}
8024
James Smarte59058c2008-08-24 21:49:00 -04008025/**
James Smart3772a992009-05-22 14:50:54 -04008026 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008027 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008028 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008029 * @piocb: Pointer to command iocb.
8030 * @flag: Flag indicating if this command can be put into txq.
8031 *
James Smart3772a992009-05-22 14:50:54 -04008032 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8033 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8034 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8035 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8036 * this function allows only iocbs for posting buffers. This function finds
8037 * next available slot in the command ring and posts the command to the
8038 * available slot and writes the port attention register to request HBA start
8039 * processing new iocb. If there is no slot available in the ring and
8040 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8041 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008042 *
James Smart3772a992009-05-22 14:50:54 -04008043 * This function is called with hbalock held. The function will return success
8044 * after it successfully submit the iocb to firmware or after adding to the
8045 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008046 **/
James Smart98c9ea52007-10-27 13:37:33 -04008047static int
James Smart3772a992009-05-22 14:50:54 -04008048__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008049 struct lpfc_iocbq *piocb, uint32_t flag)
8050{
8051 struct lpfc_iocbq *nextiocb;
8052 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04008053 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05008054
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008055 lockdep_assert_held(&phba->hbalock);
8056
James Smart92d7f7b2007-06-17 19:56:38 -05008057 if (piocb->iocb_cmpl && (!piocb->vport) &&
8058 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8059 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8060 lpfc_printf_log(phba, KERN_ERR,
8061 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008062 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008063 piocb->iocb.ulpCommand);
8064 dump_stack();
8065 return IOCB_ERROR;
8066 }
8067
8068
Linas Vepstas8d63f372007-02-14 14:28:36 -06008069 /* If the PCI channel is in offline state, do not post iocbs. */
8070 if (unlikely(pci_channel_offline(phba->pcidev)))
8071 return IOCB_ERROR;
8072
James Smarta257bf92009-04-06 18:48:10 -04008073 /* If HBA has a deferred error attention, fail the iocb. */
8074 if (unlikely(phba->hba_flag & DEFER_ERATT))
8075 return IOCB_ERROR;
8076
dea31012005-04-17 16:05:31 -05008077 /*
8078 * We should never get an IOCB if we are in a < LINK_DOWN state
8079 */
James Smart2e0fef82007-06-17 19:56:36 -05008080 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008081 return IOCB_ERROR;
8082
8083 /*
8084 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008085 * outstanding event.
dea31012005-04-17 16:05:31 -05008086 */
James Smart0b727fe2007-10-27 13:37:25 -04008087 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008088 goto iocb_busy;
8089
James Smart2e0fef82007-06-17 19:56:36 -05008090 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008091 /*
James Smart2680eea2007-04-25 09:52:55 -04008092 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008093 * can be issued if the link is not up.
8094 */
8095 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008096 case CMD_GEN_REQUEST64_CR:
8097 case CMD_GEN_REQUEST64_CX:
8098 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8099 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008100 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008101 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8102 MENLO_TRANSPORT_TYPE))
8103
8104 goto iocb_busy;
8105 break;
dea31012005-04-17 16:05:31 -05008106 case CMD_QUE_RING_BUF_CN:
8107 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008108 /*
8109 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8110 * completion, iocb_cmpl MUST be 0.
8111 */
8112 if (piocb->iocb_cmpl)
8113 piocb->iocb_cmpl = NULL;
8114 /*FALLTHROUGH*/
8115 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008116 case CMD_CLOSE_XRI_CN:
8117 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008118 break;
8119 default:
8120 goto iocb_busy;
8121 }
8122
8123 /*
8124 * For FCP commands, we must be in a state where we can process link
8125 * attention events.
8126 */
8127 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05008128 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008129 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008130 }
dea31012005-04-17 16:05:31 -05008131
dea31012005-04-17 16:05:31 -05008132 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8133 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8134 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8135
8136 if (iocb)
8137 lpfc_sli_update_ring(phba, pring);
8138 else
8139 lpfc_sli_update_full_ring(phba, pring);
8140
8141 if (!piocb)
8142 return IOCB_SUCCESS;
8143
8144 goto out_busy;
8145
8146 iocb_busy:
8147 pring->stats.iocb_cmd_delay++;
8148
8149 out_busy:
8150
8151 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008152 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008153 return IOCB_SUCCESS;
8154 }
8155
8156 return IOCB_BUSY;
8157}
8158
James Smart3772a992009-05-22 14:50:54 -04008159/**
James Smart4f774512009-05-22 14:52:35 -04008160 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8161 * @phba: Pointer to HBA context object.
8162 * @piocb: Pointer to command iocb.
8163 * @sglq: Pointer to the scatter gather queue object.
8164 *
8165 * This routine converts the bpl or bde that is in the IOCB
8166 * to a sgl list for the sli4 hardware. The physical address
8167 * of the bpl/bde is converted back to a virtual address.
8168 * If the IOCB contains a BPL then the list of BDE's is
8169 * converted to sli4_sge's. If the IOCB contains a single
8170 * BDE then it is converted to a single sli_sge.
8171 * The IOCB is still in cpu endianess so the contents of
8172 * the bpl can be used without byte swapping.
8173 *
8174 * Returns valid XRI = Success, NO_XRI = Failure.
8175**/
8176static uint16_t
8177lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8178 struct lpfc_sglq *sglq)
8179{
8180 uint16_t xritag = NO_XRI;
8181 struct ulp_bde64 *bpl = NULL;
8182 struct ulp_bde64 bde;
8183 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008184 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008185 IOCB_t *icmd;
8186 int numBdes = 0;
8187 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008188 uint32_t offset = 0; /* accumulated offset in the sg request list */
8189 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008190
8191 if (!piocbq || !sglq)
8192 return xritag;
8193
8194 sgl = (struct sli4_sge *)sglq->sgl;
8195 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008196 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8197 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008198 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8199 numBdes = icmd->un.genreq64.bdl.bdeSize /
8200 sizeof(struct ulp_bde64);
8201 /* The addrHigh and addrLow fields within the IOCB
8202 * have not been byteswapped yet so there is no
8203 * need to swap them back.
8204 */
James Smart1b511972011-12-13 13:23:09 -05008205 if (piocbq->context3)
8206 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8207 else
8208 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008209
James Smart1b511972011-12-13 13:23:09 -05008210 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008211 if (!bpl)
8212 return xritag;
8213
8214 for (i = 0; i < numBdes; i++) {
8215 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008216 sgl->addr_hi = bpl->addrHigh;
8217 sgl->addr_lo = bpl->addrLow;
8218
James Smart05580562011-05-24 11:40:48 -04008219 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008220 if ((i+1) == numBdes)
8221 bf_set(lpfc_sli4_sge_last, sgl, 1);
8222 else
8223 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008224 /* swap the size field back to the cpu so we
8225 * can assign it to the sgl.
8226 */
8227 bde.tus.w = le32_to_cpu(bpl->tus.w);
8228 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008229 /* The offsets in the sgl need to be accumulated
8230 * separately for the request and reply lists.
8231 * The request is always first, the reply follows.
8232 */
8233 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8234 /* add up the reply sg entries */
8235 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8236 inbound++;
8237 /* first inbound? reset the offset */
8238 if (inbound == 1)
8239 offset = 0;
8240 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008241 bf_set(lpfc_sli4_sge_type, sgl,
8242 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008243 offset += bde.tus.f.bdeSize;
8244 }
James Smart546fc852011-03-11 16:06:29 -05008245 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008246 bpl++;
8247 sgl++;
8248 }
8249 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8250 /* The addrHigh and addrLow fields of the BDE have not
8251 * been byteswapped yet so they need to be swapped
8252 * before putting them in the sgl.
8253 */
8254 sgl->addr_hi =
8255 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8256 sgl->addr_lo =
8257 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008258 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008259 bf_set(lpfc_sli4_sge_last, sgl, 1);
8260 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008261 sgl->sge_len =
8262 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008263 }
8264 return sglq->sli4_xritag;
8265}
8266
8267/**
James Smart4f774512009-05-22 14:52:35 -04008268 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8269 * @phba: Pointer to HBA context object.
8270 * @piocb: Pointer to command iocb.
8271 * @wqe: Pointer to the work queue entry.
8272 *
8273 * This routine converts the iocb command to its Work Queue Entry
8274 * equivalent. The wqe pointer should not have any fields set when
8275 * this routine is called because it will memcpy over them.
8276 * This routine does not set the CQ_ID or the WQEC bits in the
8277 * wqe.
8278 *
8279 * Returns: 0 = Success, IOCB_ERROR = Failure.
8280 **/
8281static int
8282lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8283 union lpfc_wqe *wqe)
8284{
James Smart5ffc2662009-11-18 15:39:44 -05008285 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008286 uint8_t ct = 0;
8287 uint32_t fip;
8288 uint32_t abort_tag;
8289 uint8_t command_type = ELS_COMMAND_NON_FIP;
8290 uint8_t cmnd;
8291 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008292 uint16_t abrt_iotag;
8293 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008294 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008295 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008296 int numBdes, i;
8297 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008298 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008299 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008300 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008301
James Smart45ed1192009-10-02 15:17:02 -04008302 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008303 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008304 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008305 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008306 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008307 command_type = ELS_COMMAND_FIP;
8308 else
8309 command_type = ELS_COMMAND_NON_FIP;
8310
James Smartb5c53952016-03-31 14:12:30 -07008311 if (phba->fcp_embed_io)
8312 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008313 /* Some of the fields are in the right position already */
8314 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008315 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008316 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008317
8318 abort_tag = (uint32_t) iocbq->iotag;
8319 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008320 /* words0-2 bpl convert bde */
8321 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008322 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8323 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008324 bpl = (struct ulp_bde64 *)
8325 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8326 if (!bpl)
8327 return IOCB_ERROR;
8328
8329 /* Should already be byte swapped. */
8330 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8331 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8332 /* swap the size field back to the cpu so we
8333 * can assign it to the sgl.
8334 */
8335 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008336 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8337 total_len = 0;
8338 for (i = 0; i < numBdes; i++) {
8339 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8340 total_len += bde.tus.f.bdeSize;
8341 }
James Smart4f774512009-05-22 14:52:35 -04008342 } else
James Smart5ffc2662009-11-18 15:39:44 -05008343 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008344
8345 iocbq->iocb.ulpIoTag = iocbq->iotag;
8346 cmnd = iocbq->iocb.ulpCommand;
8347
8348 switch (iocbq->iocb.ulpCommand) {
8349 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008350 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8351 ndlp = iocbq->context_un.ndlp;
8352 else
8353 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008354 if (!iocbq->iocb.ulpLe) {
8355 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8356 "2007 Only Limited Edition cmd Format"
8357 " supported 0x%x\n",
8358 iocbq->iocb.ulpCommand);
8359 return IOCB_ERROR;
8360 }
James Smartff78d8f2011-12-13 13:21:35 -05008361
James Smart5ffc2662009-11-18 15:39:44 -05008362 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008363 /* Els_reguest64 has a TMO */
8364 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8365 iocbq->iocb.ulpTimeout);
8366 /* Need a VF for word 4 set the vf bit*/
8367 bf_set(els_req64_vf, &wqe->els_req, 0);
8368 /* And a VFID for word 12 */
8369 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008370 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008371 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8372 iocbq->iocb.ulpContext);
8373 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8374 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008375 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008376 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008377 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8378 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008379 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8380 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008381 if_type = bf_get(lpfc_sli_intf_if_type,
8382 &phba->sli4_hba.sli_intf);
8383 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008384 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008385 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008386 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008387 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008388 *pcmd == ELS_CMD_PLOGI)) {
8389 bf_set(els_req64_sp, &wqe->els_req, 1);
8390 bf_set(els_req64_sid, &wqe->els_req,
8391 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008392 if ((*pcmd == ELS_CMD_FLOGI) &&
8393 !(phba->fc_topology ==
8394 LPFC_TOPOLOGY_LOOP))
8395 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008396 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8397 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008398 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008399 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008400 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8401 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8402 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8403 }
James Smartc8685952009-11-18 15:39:16 -05008404 }
James Smart6d368e52011-05-24 11:44:12 -04008405 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8406 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008407 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8408 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8409 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8410 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8411 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8412 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008413 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008414 break;
James Smart5ffc2662009-11-18 15:39:44 -05008415 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008416 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8417 iocbq->iocb.un.ulpWord[3]);
8418 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008419 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008420 /* The entire sequence is transmitted for this IOCB */
8421 xmit_len = total_len;
8422 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008423 if (phba->link_flag & LS_LOOPBACK_MODE)
8424 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008425 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008426 /* word3 iocb=io_tag32 wqe=reserved */
8427 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008428 /* word4 relative_offset memcpy */
8429 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008430 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8431 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8432 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8433 LPFC_WQE_IOD_WRITE);
8434 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8435 LPFC_WQE_LENLOC_WORD12);
8436 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008437 wqe->xmit_sequence.xmit_len = xmit_len;
8438 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008439 break;
James Smart4f774512009-05-22 14:52:35 -04008440 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008441 /* word3 iocb=iotag32 wqe=seq_payload_len */
8442 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008443 /* word4 iocb=rsvd wqe=rsvd */
8444 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8445 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008446 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008447 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008448 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8449 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8450 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8451 LPFC_WQE_LENLOC_WORD3);
8452 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008453 break;
James Smart4f774512009-05-22 14:52:35 -04008454 case CMD_FCP_IWRITE64_CR:
8455 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008456 /* word3 iocb=iotag wqe=payload_offset_len */
8457 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008458 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8459 xmit_len + sizeof(struct fcp_rsp));
8460 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8461 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008462 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8463 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8464 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8465 iocbq->iocb.ulpFCP2Rcvy);
8466 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8467 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008468 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8469 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8470 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008471 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008472 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008473 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8474 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008475 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8476 if (iocbq->priority) {
8477 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8478 (iocbq->priority << 1));
8479 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008480 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8481 (phba->cfg_XLanePriority << 1));
8482 }
8483 }
James Smartb5c53952016-03-31 14:12:30 -07008484 /* Note, word 10 is already initialized to 0 */
8485
8486 if (phba->fcp_embed_io) {
8487 struct lpfc_scsi_buf *lpfc_cmd;
8488 struct sli4_sge *sgl;
8489 union lpfc_wqe128 *wqe128;
8490 struct fcp_cmnd *fcp_cmnd;
8491 uint32_t *ptr;
8492
8493 /* 128 byte wqe support here */
8494 wqe128 = (union lpfc_wqe128 *)wqe;
8495
8496 lpfc_cmd = iocbq->context1;
8497 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8498 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8499
8500 /* Word 0-2 - FCP_CMND */
8501 wqe128->generic.bde.tus.f.bdeFlags =
8502 BUFF_TYPE_BDE_IMMED;
8503 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8504 wqe128->generic.bde.addrHigh = 0;
8505 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8506
8507 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8508
8509 /* Word 22-29 FCP CMND Payload */
8510 ptr = &wqe128->words[22];
8511 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8512 }
James Smart7851fe22011-07-22 18:36:52 -04008513 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008514 case CMD_FCP_IREAD64_CR:
8515 /* word3 iocb=iotag wqe=payload_offset_len */
8516 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008517 bf_set(payload_offset_len, &wqe->fcp_iread,
8518 xmit_len + sizeof(struct fcp_rsp));
8519 bf_set(cmd_buff_len, &wqe->fcp_iread,
8520 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008521 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8522 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8523 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8524 iocbq->iocb.ulpFCP2Rcvy);
8525 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008526 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008527 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8528 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8529 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008530 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008531 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008532 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8533 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008534 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8535 if (iocbq->priority) {
8536 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8537 (iocbq->priority << 1));
8538 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008539 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8540 (phba->cfg_XLanePriority << 1));
8541 }
8542 }
James Smartb5c53952016-03-31 14:12:30 -07008543 /* Note, word 10 is already initialized to 0 */
8544
8545 if (phba->fcp_embed_io) {
8546 struct lpfc_scsi_buf *lpfc_cmd;
8547 struct sli4_sge *sgl;
8548 union lpfc_wqe128 *wqe128;
8549 struct fcp_cmnd *fcp_cmnd;
8550 uint32_t *ptr;
8551
8552 /* 128 byte wqe support here */
8553 wqe128 = (union lpfc_wqe128 *)wqe;
8554
8555 lpfc_cmd = iocbq->context1;
8556 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8557 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8558
8559 /* Word 0-2 - FCP_CMND */
8560 wqe128->generic.bde.tus.f.bdeFlags =
8561 BUFF_TYPE_BDE_IMMED;
8562 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8563 wqe128->generic.bde.addrHigh = 0;
8564 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8565
8566 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8567
8568 /* Word 22-29 FCP CMND Payload */
8569 ptr = &wqe128->words[22];
8570 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8571 }
James Smart7851fe22011-07-22 18:36:52 -04008572 break;
James Smartf1126682009-06-10 17:22:44 -04008573 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008574 /* word3 iocb=iotag wqe=payload_offset_len */
8575 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8576 bf_set(payload_offset_len, &wqe->fcp_icmd,
8577 xmit_len + sizeof(struct fcp_rsp));
8578 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8579 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008580 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008581 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008582 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008583 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8584 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8585 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8586 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8587 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008588 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8589 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008590 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8591 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008592 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8593 if (iocbq->priority) {
8594 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8595 (iocbq->priority << 1));
8596 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008597 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8598 (phba->cfg_XLanePriority << 1));
8599 }
8600 }
James Smartb5c53952016-03-31 14:12:30 -07008601 /* Note, word 10 is already initialized to 0 */
8602
8603 if (phba->fcp_embed_io) {
8604 struct lpfc_scsi_buf *lpfc_cmd;
8605 struct sli4_sge *sgl;
8606 union lpfc_wqe128 *wqe128;
8607 struct fcp_cmnd *fcp_cmnd;
8608 uint32_t *ptr;
8609
8610 /* 128 byte wqe support here */
8611 wqe128 = (union lpfc_wqe128 *)wqe;
8612
8613 lpfc_cmd = iocbq->context1;
8614 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8615 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8616
8617 /* Word 0-2 - FCP_CMND */
8618 wqe128->generic.bde.tus.f.bdeFlags =
8619 BUFF_TYPE_BDE_IMMED;
8620 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8621 wqe128->generic.bde.addrHigh = 0;
8622 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8623
8624 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8625
8626 /* Word 22-29 FCP CMND Payload */
8627 ptr = &wqe128->words[22];
8628 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8629 }
James Smart7851fe22011-07-22 18:36:52 -04008630 break;
James Smart4f774512009-05-22 14:52:35 -04008631 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008632 /* For this command calculate the xmit length of the
8633 * request bde.
8634 */
8635 xmit_len = 0;
8636 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8637 sizeof(struct ulp_bde64);
8638 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008639 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008640 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8641 break;
James Smart63e801c2010-11-20 23:14:19 -05008642 xmit_len += bde.tus.f.bdeSize;
8643 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008644 /* word3 iocb=IO_TAG wqe=request_payload_len */
8645 wqe->gen_req.request_payload_len = xmit_len;
8646 /* word4 iocb=parameter wqe=relative_offset memcpy */
8647 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008648 /* word6 context tag copied in memcpy */
8649 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8650 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8652 "2015 Invalid CT %x command 0x%x\n",
8653 ct, iocbq->iocb.ulpCommand);
8654 return IOCB_ERROR;
8655 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008656 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8657 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8658 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8659 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8660 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8661 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8662 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8663 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008664 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008665 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008666 break;
James Smart4f774512009-05-22 14:52:35 -04008667 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008668 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008669 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008670 /* word3 iocb=iotag32 wqe=response_payload_len */
8671 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008672 /* word4 */
8673 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008674 /* word5 iocb=rsvd wge=did */
8675 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008676 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8677
8678 if_type = bf_get(lpfc_sli_intf_if_type,
8679 &phba->sli4_hba.sli_intf);
8680 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8681 if (iocbq->vport->fc_flag & FC_PT2PT) {
8682 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8683 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8684 iocbq->vport->fc_myDID);
8685 if (iocbq->vport->fc_myDID == Fabric_DID) {
8686 bf_set(wqe_els_did,
8687 &wqe->xmit_els_rsp.wqe_dest, 0);
8688 }
8689 }
8690 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008691 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8692 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8693 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8694 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008695 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008696 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008697 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008698 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008699 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8700 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8701 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8702 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8703 LPFC_WQE_LENLOC_WORD3);
8704 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008705 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8706 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008707 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8708 iocbq->context2)->virt);
8709 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008710 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8711 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008712 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008713 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8714 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008715 phba->vpi_ids[phba->pport->vpi]);
8716 }
James Smart4f774512009-05-22 14:52:35 -04008717 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008718 break;
James Smart4f774512009-05-22 14:52:35 -04008719 case CMD_CLOSE_XRI_CN:
8720 case CMD_ABORT_XRI_CN:
8721 case CMD_ABORT_XRI_CX:
8722 /* words 0-2 memcpy should be 0 rserved */
8723 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008724 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8725 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8726 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8727 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8728 } else
8729 fip = 0;
8730
8731 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008732 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008733 * The link is down, or the command was ELS_FIP
8734 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008735 * on the wire.
8736 */
8737 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8738 else
8739 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8740 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008741 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8742 wqe->abort_cmd.rsrvd5 = 0;
8743 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008744 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8745 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008746 /*
8747 * The abort handler will send us CMD_ABORT_XRI_CN or
8748 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8749 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008750 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8751 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8752 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8753 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008754 cmnd = CMD_ABORT_XRI_CX;
8755 command_type = OTHER_COMMAND;
8756 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008757 break;
James Smart6669f9b2009-10-02 15:16:45 -04008758 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008759 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008760 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008761 * we re-construct this WQE here based on information in
8762 * iocbq from scratch.
8763 */
8764 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008765 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008766 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008767 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8768 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008769 LPFC_ABTS_UNSOL_INT) {
8770 /* ABTS sent by initiator to CT exchange, the
8771 * RX_ID field will be filled with the newly
8772 * allocated responder XRI.
8773 */
8774 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8775 iocbq->sli4_xritag);
8776 } else {
8777 /* ABTS sent by responder to CT exchange, the
8778 * RX_ID field will be filled with the responder
8779 * RX_ID from ABTS.
8780 */
8781 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008782 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008783 }
James Smart6669f9b2009-10-02 15:16:45 -04008784 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8785 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008786
8787 /* Use CT=VPI */
8788 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8789 ndlp->nlp_DID);
8790 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8791 iocbq->iocb.ulpContext);
8792 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008793 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008794 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008795 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8796 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8797 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008798 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8799 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008800 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8801 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8802 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8803 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8804 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8805 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8806 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8807 }
8808
James Smart7851fe22011-07-22 18:36:52 -04008809 break;
James Smart4f774512009-05-22 14:52:35 -04008810 case CMD_XRI_ABORTED_CX:
8811 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008812 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8813 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8814 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8815 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8816 default:
8817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8818 "2014 Invalid command 0x%x\n",
8819 iocbq->iocb.ulpCommand);
8820 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008821 break;
James Smart4f774512009-05-22 14:52:35 -04008822 }
James Smart6d368e52011-05-24 11:44:12 -04008823
James Smart8012cc32012-10-31 14:44:49 -04008824 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8825 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8826 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8827 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8828 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8829 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8830 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8831 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008832 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8833 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8834 wqe->generic.wqe_com.abort_tag = abort_tag;
8835 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8836 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8837 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8838 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008839 return 0;
8840}
8841
8842/**
8843 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8844 * @phba: Pointer to HBA context object.
8845 * @ring_number: SLI ring number to issue iocb on.
8846 * @piocb: Pointer to command iocb.
8847 * @flag: Flag indicating if this command can be put into txq.
8848 *
8849 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8850 * an iocb command to an HBA with SLI-4 interface spec.
8851 *
8852 * This function is called with hbalock held. The function will return success
8853 * after it successfully submit the iocb to firmware or after adding to the
8854 * txq.
8855 **/
8856static int
8857__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8858 struct lpfc_iocbq *piocb, uint32_t flag)
8859{
8860 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008861 union lpfc_wqe *wqe;
8862 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008863 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008864 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008865
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008866 lockdep_assert_held(&phba->hbalock);
8867
James Smartb5c53952016-03-31 14:12:30 -07008868 /*
8869 * The WQE can be either 64 or 128 bytes,
8870 * so allocate space on the stack assuming the largest.
8871 */
8872 wqe = (union lpfc_wqe *)&wqe128;
8873
James Smart4f774512009-05-22 14:52:35 -04008874 if (piocb->sli4_xritag == NO_XRI) {
8875 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008876 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008877 sglq = NULL;
8878 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008879 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008880 if (!(flag & SLI_IOCB_RET_IOCB)) {
8881 __lpfc_sli_ringtx_put(phba,
8882 pring, piocb);
8883 return IOCB_SUCCESS;
8884 } else {
8885 return IOCB_BUSY;
8886 }
8887 } else {
James Smart6d368e52011-05-24 11:44:12 -04008888 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008889 if (!sglq) {
8890 if (!(flag & SLI_IOCB_RET_IOCB)) {
8891 __lpfc_sli_ringtx_put(phba,
8892 pring,
8893 piocb);
8894 return IOCB_SUCCESS;
8895 } else
8896 return IOCB_BUSY;
8897 }
8898 }
James Smart4f774512009-05-22 14:52:35 -04008899 }
8900 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008901 /* These IO's already have an XRI and a mapped sgl. */
8902 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008903 } else {
James Smart6d368e52011-05-24 11:44:12 -04008904 /*
8905 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008906 * sglq is on the active list
8907 */
James Smartedccdc12013-01-03 15:43:45 -05008908 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008909 if (!sglq)
8910 return IOCB_ERROR;
8911 }
8912
8913 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008914 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008915 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008916 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008917 return IOCB_ERROR;
8918 }
8919
James Smartb5c53952016-03-31 14:12:30 -07008920 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008921 return IOCB_ERROR;
8922
James Smart341af102010-01-26 23:07:37 -05008923 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008924 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008925 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008926 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8927 } else {
8928 wq = phba->sli4_hba.oas_wq;
8929 }
James Smartb5c53952016-03-31 14:12:30 -07008930 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008931 return IOCB_ERROR;
8932 } else {
James Smartea714f32013-04-17 20:18:47 -04008933 if (unlikely(!phba->sli4_hba.els_wq))
8934 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008935 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008936 return IOCB_ERROR;
8937 }
8938 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8939
8940 return 0;
8941}
8942
8943/**
James Smart3772a992009-05-22 14:50:54 -04008944 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8945 *
8946 * This routine wraps the actual lockless version for issusing IOCB function
8947 * pointer from the lpfc_hba struct.
8948 *
8949 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008950 * IOCB_ERROR - Error
8951 * IOCB_SUCCESS - Success
8952 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008953 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008954int
James Smart3772a992009-05-22 14:50:54 -04008955__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8956 struct lpfc_iocbq *piocb, uint32_t flag)
8957{
8958 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8959}
8960
8961/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008962 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008963 * @phba: The hba struct for which this call is being executed.
8964 * @dev_grp: The HBA PCI-Device group number.
8965 *
8966 * This routine sets up the SLI interface API function jump table in @phba
8967 * struct.
8968 * Returns: 0 - success, -ENODEV - failure.
8969 **/
8970int
8971lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8972{
8973
8974 switch (dev_grp) {
8975 case LPFC_PCI_DEV_LP:
8976 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8977 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8978 break;
James Smart4f774512009-05-22 14:52:35 -04008979 case LPFC_PCI_DEV_OC:
8980 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8981 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8982 break;
James Smart3772a992009-05-22 14:50:54 -04008983 default:
8984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8985 "1419 Invalid HBA PCI-device group: 0x%x\n",
8986 dev_grp);
8987 return -ENODEV;
8988 break;
8989 }
8990 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8991 return 0;
8992}
James Smart92d7f7b2007-06-17 19:56:38 -05008993
James Smarta1efe162015-05-21 13:55:20 -04008994/**
8995 * lpfc_sli_calc_ring - Calculates which ring to use
8996 * @phba: Pointer to HBA context object.
8997 * @ring_number: Initial ring
8998 * @piocb: Pointer to command iocb.
8999 *
9000 * For SLI4, FCP IO can deferred to one fo many WQs, based on
9001 * fcp_wqidx, thus we need to calculate the corresponding ring.
9002 * Since ABORTS must go on the same WQ of the command they are
9003 * aborting, we use command's fcp_wqidx.
9004 */
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08009005static int
James Smart9bd2bff52014-09-03 12:57:30 -04009006lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
9007 struct lpfc_iocbq *piocb)
9008{
James Smart8b0dff12015-05-22 10:42:38 -04009009 if (phba->sli_rev < LPFC_SLI_REV4)
9010 return ring_number;
9011
9012 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9013 if (!(phba->cfg_fof) ||
9014 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9015 if (unlikely(!phba->sli4_hba.fcp_wq))
9016 return LPFC_HBA_ERROR;
9017 /*
9018 * for abort iocb fcp_wqidx should already
9019 * be setup based on what work queue we used.
9020 */
9021 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9022 piocb->fcp_wqidx =
9023 lpfc_sli4_scmd_to_wqidx_distr(phba,
9024 piocb->context1);
9025 ring_number = MAX_SLI3_CONFIGURED_RINGS +
9026 piocb->fcp_wqidx;
9027 } else {
9028 if (unlikely(!phba->sli4_hba.oas_wq))
9029 return LPFC_HBA_ERROR;
9030 piocb->fcp_wqidx = 0;
9031 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04009032 }
9033 }
9034 return ring_number;
9035}
9036
James Smarte59058c2008-08-24 21:49:00 -04009037/**
James Smart3621a712009-04-06 18:47:14 -04009038 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009039 * @phba: Pointer to HBA context object.
9040 * @pring: Pointer to driver SLI ring object.
9041 * @piocb: Pointer to command iocb.
9042 * @flag: Flag indicating if this command can be put into txq.
9043 *
9044 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9045 * function. This function gets the hbalock and calls
9046 * __lpfc_sli_issue_iocb function and will return the error returned
9047 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9048 * functions which do not hold hbalock.
9049 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009050int
James Smart3772a992009-05-22 14:50:54 -04009051lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009052 struct lpfc_iocbq *piocb, uint32_t flag)
9053{
James Smartba20c852012-08-03 12:36:52 -04009054 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009055 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009056 struct lpfc_queue *fpeq;
9057 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009058 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009059 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009060
James Smart7e56aa22012-08-03 12:35:34 -04009061 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009062 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9063 if (unlikely(ring_number == LPFC_HBA_ERROR))
9064 return IOCB_ERROR;
9065 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04009066
James Smart9bd2bff52014-09-03 12:57:30 -04009067 pring = &phba->sli.ring[ring_number];
9068 spin_lock_irqsave(&pring->ring_lock, iflags);
9069 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9070 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009071
James Smart9bd2bff52014-09-03 12:57:30 -04009072 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9073 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009074
James Smart9bd2bff52014-09-03 12:57:30 -04009075 if (atomic_dec_and_test(&fcp_eq_hdl->
9076 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009077
James Smart9bd2bff52014-09-03 12:57:30 -04009078 /* Get associated EQ with this index */
9079 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009080
James Smart9bd2bff52014-09-03 12:57:30 -04009081 /* Turn off interrupts from this EQ */
9082 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009083
James Smart9bd2bff52014-09-03 12:57:30 -04009084 /*
9085 * Process all the events on FCP EQ
9086 */
9087 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9088 lpfc_sli4_hba_handle_eqe(phba,
9089 eqe, idx);
9090 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009091 }
James Smartba20c852012-08-03 12:36:52 -04009092
James Smart9bd2bff52014-09-03 12:57:30 -04009093 /* Always clear and re-arm the EQ */
9094 lpfc_sli4_eq_release(fpeq,
9095 LPFC_QUEUE_REARM);
9096 }
9097 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009098 }
James Smart7e56aa22012-08-03 12:35:34 -04009099 } else {
9100 /* For now, SLI2/3 will still use hbalock */
9101 spin_lock_irqsave(&phba->hbalock, iflags);
9102 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9103 spin_unlock_irqrestore(&phba->hbalock, iflags);
9104 }
James Smart92d7f7b2007-06-17 19:56:38 -05009105 return rc;
9106}
9107
James Smarte59058c2008-08-24 21:49:00 -04009108/**
James Smart3621a712009-04-06 18:47:14 -04009109 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009110 * @phba: Pointer to HBA context object.
9111 *
9112 * This function is called while driver attaches with the
9113 * HBA to setup the extra ring. The extra ring is used
9114 * only when driver needs to support target mode functionality
9115 * or IP over FC functionalities.
9116 *
9117 * This function is called with no lock held.
9118 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009119static int
9120lpfc_extra_ring_setup( struct lpfc_hba *phba)
9121{
9122 struct lpfc_sli *psli;
9123 struct lpfc_sli_ring *pring;
9124
9125 psli = &phba->sli;
9126
9127 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009128
9129 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009130 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009131 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9132 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9133 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9134 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009135
James Smarta4bc3372006-12-02 13:34:16 -05009136 /* and give them to the extra ring */
9137 pring = &psli->ring[psli->extra_ring];
9138
James Smart7e56aa22012-08-03 12:35:34 -04009139 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9140 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9141 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9142 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009143
9144 /* Setup default profile for this ring */
9145 pring->iotag_max = 4096;
9146 pring->num_mask = 1;
9147 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009148 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9149 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009150 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9151 return 0;
9152}
9153
James Smartcb69f7d2011-12-13 13:21:57 -05009154/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9155 * @phba: Pointer to HBA context object.
9156 * @iocbq: Pointer to iocb object.
9157 *
9158 * The async_event handler calls this routine when it receives
9159 * an ASYNC_STATUS_CN event from the port. The port generates
9160 * this event when an Abort Sequence request to an rport fails
9161 * twice in succession. The abort could be originated by the
9162 * driver or by the port. The ABTS could have been for an ELS
9163 * or FCP IO. The port only generates this event when an ABTS
9164 * fails to complete after one retry.
9165 */
9166static void
9167lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9168 struct lpfc_iocbq *iocbq)
9169{
9170 struct lpfc_nodelist *ndlp = NULL;
9171 uint16_t rpi = 0, vpi = 0;
9172 struct lpfc_vport *vport = NULL;
9173
9174 /* The rpi in the ulpContext is vport-sensitive. */
9175 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9176 rpi = iocbq->iocb.ulpContext;
9177
9178 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9179 "3092 Port generated ABTS async event "
9180 "on vpi %d rpi %d status 0x%x\n",
9181 vpi, rpi, iocbq->iocb.ulpStatus);
9182
9183 vport = lpfc_find_vport_by_vpid(phba, vpi);
9184 if (!vport)
9185 goto err_exit;
9186 ndlp = lpfc_findnode_rpi(vport, rpi);
9187 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9188 goto err_exit;
9189
9190 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9191 lpfc_sli_abts_recover_port(vport, ndlp);
9192 return;
9193
9194 err_exit:
9195 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9196 "3095 Event Context not found, no "
9197 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9198 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9199 vpi, rpi);
9200}
9201
9202/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9203 * @phba: pointer to HBA context object.
9204 * @ndlp: nodelist pointer for the impacted rport.
9205 * @axri: pointer to the wcqe containing the failed exchange.
9206 *
9207 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9208 * port. The port generates this event when an abort exchange request to an
9209 * rport fails twice in succession with no reply. The abort could be originated
9210 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9211 */
9212void
9213lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9214 struct lpfc_nodelist *ndlp,
9215 struct sli4_wcqe_xri_aborted *axri)
9216{
9217 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009218 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009219
James Smart6b5151f2012-01-18 16:24:06 -05009220 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009221 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9222 "3115 Node Context not found, driver "
9223 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009224 return;
9225 }
9226
James Smartcb69f7d2011-12-13 13:21:57 -05009227 vport = ndlp->vport;
9228 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9229 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009230 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009231 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009232 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009233 bf_get(lpfc_wcqe_xa_status, axri),
9234 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009235
James Smart5c1db2a2012-03-01 22:34:36 -05009236 /*
9237 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9238 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9239 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9240 */
James Smarte3d2b802012-08-14 14:25:43 -04009241 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009242 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9243 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009244 lpfc_sli_abts_recover_port(vport, ndlp);
9245}
9246
James Smarte59058c2008-08-24 21:49:00 -04009247/**
James Smart3621a712009-04-06 18:47:14 -04009248 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009249 * @phba: Pointer to HBA context object.
9250 * @pring: Pointer to driver SLI ring object.
9251 * @iocbq: Pointer to iocb object.
9252 *
9253 * This function is called by the slow ring event handler
9254 * function when there is an ASYNC event iocb in the ring.
9255 * This function is called with no lock held.
9256 * Currently this function handles only temperature related
9257 * ASYNC events. The function decodes the temperature sensor
9258 * event message and posts events for the management applications.
9259 **/
James Smart98c9ea52007-10-27 13:37:33 -04009260static void
James Smart57127f12007-10-27 13:37:05 -04009261lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9262 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9263{
9264 IOCB_t *icmd;
9265 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009266 struct temp_event temp_event_data;
9267 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009268 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009269
9270 icmd = &iocbq->iocb;
9271 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009272
James Smartcb69f7d2011-12-13 13:21:57 -05009273 switch (evt_code) {
9274 case ASYNC_TEMP_WARN:
9275 case ASYNC_TEMP_SAFE:
9276 temp_event_data.data = (uint32_t) icmd->ulpContext;
9277 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9278 if (evt_code == ASYNC_TEMP_WARN) {
9279 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9280 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9281 "0347 Adapter is very hot, please take "
9282 "corrective action. temperature : %d Celsius\n",
9283 (uint32_t) icmd->ulpContext);
9284 } else {
9285 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9286 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9287 "0340 Adapter temperature is OK now. "
9288 "temperature : %d Celsius\n",
9289 (uint32_t) icmd->ulpContext);
9290 }
9291
9292 /* Send temperature change event to applications */
9293 shost = lpfc_shost_from_vport(phba->pport);
9294 fc_host_post_vendor_event(shost, fc_get_event_number(),
9295 sizeof(temp_event_data), (char *) &temp_event_data,
9296 LPFC_NL_VENDOR_ID);
9297 break;
9298 case ASYNC_STATUS_CN:
9299 lpfc_sli_abts_err_handler(phba, iocbq);
9300 break;
9301 default:
James Smarta257bf92009-04-06 18:48:10 -04009302 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009304 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009305 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009306 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9307 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9308 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9309 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009310 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009311 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9312 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9313 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9314 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9315
James Smartcb69f7d2011-12-13 13:21:57 -05009316 break;
James Smart57127f12007-10-27 13:37:05 -04009317 }
James Smart57127f12007-10-27 13:37:05 -04009318}
9319
9320
James Smarte59058c2008-08-24 21:49:00 -04009321/**
James Smart3621a712009-04-06 18:47:14 -04009322 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009323 * @phba: Pointer to HBA context object.
9324 *
9325 * lpfc_sli_setup sets up rings of the SLI interface with
9326 * number of iocbs per ring and iotags. This function is
9327 * called while driver attach to the HBA and before the
9328 * interrupts are enabled. So there is no need for locking.
9329 *
9330 * This function always returns 0.
9331 **/
dea31012005-04-17 16:05:31 -05009332int
9333lpfc_sli_setup(struct lpfc_hba *phba)
9334{
James Smarted957682007-06-17 19:56:37 -05009335 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009336 struct lpfc_sli *psli = &phba->sli;
9337 struct lpfc_sli_ring *pring;
9338
James Smart2a76a282012-08-03 12:35:54 -04009339 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9340 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009341 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009342 psli->sli_flag = 0;
9343 psli->fcp_ring = LPFC_FCP_RING;
9344 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009345 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009346
James Bottomley604a3e32005-10-29 10:28:33 -05009347 psli->iocbq_lookup = NULL;
9348 psli->iocbq_lookup_len = 0;
9349 psli->last_iotag = 0;
9350
dea31012005-04-17 16:05:31 -05009351 for (i = 0; i < psli->num_rings; i++) {
9352 pring = &psli->ring[i];
9353 switch (i) {
9354 case LPFC_FCP_RING: /* ring 0 - FCP */
9355 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009356 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9357 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9358 pring->sli.sli3.numCiocb +=
9359 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9360 pring->sli.sli3.numRiocb +=
9361 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9362 pring->sli.sli3.numCiocb +=
9363 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9364 pring->sli.sli3.numRiocb +=
9365 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9366 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009367 SLI3_IOCB_CMD_SIZE :
9368 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009369 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009370 SLI3_IOCB_RSP_SIZE :
9371 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009372 pring->iotag_ctr = 0;
9373 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009374 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009375 pring->fast_iotag = pring->iotag_max;
9376 pring->num_mask = 0;
9377 break;
James Smarta4bc3372006-12-02 13:34:16 -05009378 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009379 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009380 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9381 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9382 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009383 SLI3_IOCB_CMD_SIZE :
9384 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009385 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009386 SLI3_IOCB_RSP_SIZE :
9387 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009388 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009389 pring->num_mask = 0;
9390 break;
9391 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9392 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009393 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9394 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9395 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009396 SLI3_IOCB_CMD_SIZE :
9397 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009398 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009399 SLI3_IOCB_RSP_SIZE :
9400 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009401 pring->fast_iotag = 0;
9402 pring->iotag_ctr = 0;
9403 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009404 pring->lpfc_sli_rcv_async_status =
9405 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009406 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009407 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009408 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9409 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009410 pring->prt[0].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[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009413 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9414 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009415 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009416 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009417 pring->prt[2].profile = 0; /* Mask 2 */
9418 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009419 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009420 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009421 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009422 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009423 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009424 pring->prt[3].profile = 0; /* Mask 3 */
9425 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009426 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009427 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009428 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009429 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009430 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009431 break;
9432 }
James Smart7e56aa22012-08-03 12:35:34 -04009433 totiocbsize += (pring->sli.sli3.numCiocb *
9434 pring->sli.sli3.sizeCiocb) +
9435 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009436 }
James Smarted957682007-06-17 19:56:37 -05009437 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009438 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009439 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9440 "SLI2 SLIM Data: x%x x%lx\n",
9441 phba->brd_no, totiocbsize,
9442 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009443 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009444 if (phba->cfg_multi_ring_support == 2)
9445 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009446
9447 return 0;
9448}
9449
James Smarte59058c2008-08-24 21:49:00 -04009450/**
James Smart3621a712009-04-06 18:47:14 -04009451 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009452 * @phba: Pointer to HBA context object.
9453 *
9454 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9455 * ring. This function also initializes ring indices of each ring.
9456 * This function is called during the initialization of the SLI
9457 * interface of an HBA.
9458 * This function is called with no lock held and always returns
9459 * 1.
9460 **/
dea31012005-04-17 16:05:31 -05009461int
James Smart2e0fef82007-06-17 19:56:36 -05009462lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009463{
9464 struct lpfc_sli *psli;
9465 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009466 int i;
dea31012005-04-17 16:05:31 -05009467
9468 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009469 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009470 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009471 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009472 /* Initialize list headers for txq and txcmplq as double linked lists */
9473 for (i = 0; i < psli->num_rings; i++) {
9474 pring = &psli->ring[i];
9475 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009476 pring->sli.sli3.next_cmdidx = 0;
9477 pring->sli.sli3.local_getidx = 0;
9478 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009479 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009480 INIT_LIST_HEAD(&pring->txq);
9481 INIT_LIST_HEAD(&pring->txcmplq);
9482 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009483 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009484 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009485 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009486 }
James Smart2e0fef82007-06-17 19:56:36 -05009487 spin_unlock_irq(&phba->hbalock);
9488 return 1;
dea31012005-04-17 16:05:31 -05009489}
9490
James Smarte59058c2008-08-24 21:49:00 -04009491/**
James Smart04c68492009-05-22 14:52:52 -04009492 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9493 * @phba: Pointer to HBA context object.
9494 *
9495 * This routine flushes the mailbox command subsystem. It will unconditionally
9496 * flush all the mailbox commands in the three possible stages in the mailbox
9497 * command sub-system: pending mailbox command queue; the outstanding mailbox
9498 * command; and completed mailbox command queue. It is caller's responsibility
9499 * to make sure that the driver is in the proper state to flush the mailbox
9500 * command sub-system. Namely, the posting of mailbox commands into the
9501 * pending mailbox command queue from the various clients must be stopped;
9502 * either the HBA is in a state that it will never works on the outstanding
9503 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9504 * mailbox command has been completed.
9505 **/
9506static void
9507lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9508{
9509 LIST_HEAD(completions);
9510 struct lpfc_sli *psli = &phba->sli;
9511 LPFC_MBOXQ_t *pmb;
9512 unsigned long iflag;
9513
9514 /* Flush all the mailbox commands in the mbox system */
9515 spin_lock_irqsave(&phba->hbalock, iflag);
9516 /* The pending mailbox command queue */
9517 list_splice_init(&phba->sli.mboxq, &completions);
9518 /* The outstanding active mailbox command */
9519 if (psli->mbox_active) {
9520 list_add_tail(&psli->mbox_active->list, &completions);
9521 psli->mbox_active = NULL;
9522 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9523 }
9524 /* The completed mailbox command queue */
9525 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9526 spin_unlock_irqrestore(&phba->hbalock, iflag);
9527
9528 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9529 while (!list_empty(&completions)) {
9530 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9531 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9532 if (pmb->mbox_cmpl)
9533 pmb->mbox_cmpl(phba, pmb);
9534 }
9535}
9536
9537/**
James Smart3621a712009-04-06 18:47:14 -04009538 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009539 * @vport: Pointer to virtual port object.
9540 *
9541 * lpfc_sli_host_down is called to clean up the resources
9542 * associated with a vport before destroying virtual
9543 * port data structures.
9544 * This function does following operations:
9545 * - Free discovery resources associated with this virtual
9546 * port.
9547 * - Free iocbs associated with this virtual port in
9548 * the txq.
9549 * - Send abort for all iocb commands associated with this
9550 * vport in txcmplq.
9551 *
9552 * This function is called with no lock held and always returns 1.
9553 **/
dea31012005-04-17 16:05:31 -05009554int
James Smart92d7f7b2007-06-17 19:56:38 -05009555lpfc_sli_host_down(struct lpfc_vport *vport)
9556{
James Smart858c9f62007-06-17 19:56:39 -05009557 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009558 struct lpfc_hba *phba = vport->phba;
9559 struct lpfc_sli *psli = &phba->sli;
9560 struct lpfc_sli_ring *pring;
9561 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009562 int i;
9563 unsigned long flags = 0;
9564 uint16_t prev_pring_flag;
9565
9566 lpfc_cleanup_discovery_resources(vport);
9567
9568 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009569 for (i = 0; i < psli->num_rings; i++) {
9570 pring = &psli->ring[i];
9571 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009572 /* Only slow rings */
9573 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009574 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009575 /* Set the lpfc data pending flag */
9576 set_bit(LPFC_DATA_READY, &phba->data_flags);
9577 }
James Smart92d7f7b2007-06-17 19:56:38 -05009578 /*
9579 * Error everything on the txq since these iocbs have not been
9580 * given to the FW yet.
9581 */
James Smart92d7f7b2007-06-17 19:56:38 -05009582 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9583 if (iocb->vport != vport)
9584 continue;
James Smart858c9f62007-06-17 19:56:39 -05009585 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009586 }
9587
9588 /* Next issue ABTS for everything on the txcmplq */
9589 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9590 list) {
9591 if (iocb->vport != vport)
9592 continue;
9593 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9594 }
9595
9596 pring->flag = prev_pring_flag;
9597 }
9598
9599 spin_unlock_irqrestore(&phba->hbalock, flags);
9600
James Smarta257bf92009-04-06 18:48:10 -04009601 /* Cancel all the IOCBs from the completions list */
9602 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9603 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009604 return 1;
9605}
9606
James Smarte59058c2008-08-24 21:49:00 -04009607/**
James Smart3621a712009-04-06 18:47:14 -04009608 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009609 * @phba: Pointer to HBA context object.
9610 *
9611 * This function cleans up all iocb, buffers, mailbox commands
9612 * while shutting down the HBA. This function is called with no
9613 * lock held and always returns 1.
9614 * This function does the following to cleanup driver resources:
9615 * - Free discovery resources for each virtual port
9616 * - Cleanup any pending fabric iocbs
9617 * - Iterate through the iocb txq and free each entry
9618 * in the list.
9619 * - Free up any buffer posted to the HBA
9620 * - Free mailbox commands in the mailbox queue.
9621 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009622int
James Smart2e0fef82007-06-17 19:56:36 -05009623lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009624{
James Smart2534ba72007-04-25 09:52:20 -04009625 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009626 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009627 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009628 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009629 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009630 int i;
9631
9632 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009633 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009634
dea31012005-04-17 16:05:31 -05009635 lpfc_hba_down_prep(phba);
9636
James Smart92d7f7b2007-06-17 19:56:38 -05009637 lpfc_fabric_abort_hba(phba);
9638
James Smart2e0fef82007-06-17 19:56:36 -05009639 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009640 for (i = 0; i < psli->num_rings; i++) {
9641 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009642 /* Only slow rings */
9643 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009644 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009645 /* Set the lpfc data pending flag */
9646 set_bit(LPFC_DATA_READY, &phba->data_flags);
9647 }
dea31012005-04-17 16:05:31 -05009648
9649 /*
9650 * Error everything on the txq since these iocbs have not been
9651 * given to the FW yet.
9652 */
James Smart2534ba72007-04-25 09:52:20 -04009653 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009654 }
James Smart2e0fef82007-06-17 19:56:36 -05009655 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009656
James Smarta257bf92009-04-06 18:48:10 -04009657 /* Cancel all the IOCBs from the completions list */
9658 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9659 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009660
James Smart0ff10d42008-01-11 01:52:36 -05009661 spin_lock_irqsave(&phba->hbalock, flags);
9662 list_splice_init(&phba->elsbuf, &completions);
9663 phba->elsbuf_cnt = 0;
9664 phba->elsbuf_prev_cnt = 0;
9665 spin_unlock_irqrestore(&phba->hbalock, flags);
9666
9667 while (!list_empty(&completions)) {
9668 list_remove_head(&completions, buf_ptr,
9669 struct lpfc_dmabuf, list);
9670 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9671 kfree(buf_ptr);
9672 }
9673
dea31012005-04-17 16:05:31 -05009674 /* Return any active mbox cmds */
9675 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009676
James Smartda0436e2009-05-22 14:51:39 -04009677 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009678 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009679 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009680
James Smartda0436e2009-05-22 14:51:39 -04009681 return 1;
9682}
James Smart92d7f7b2007-06-17 19:56:38 -05009683
James Smartda0436e2009-05-22 14:51:39 -04009684/**
James Smart3621a712009-04-06 18:47:14 -04009685 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009686 * @srcp: Source memory pointer.
9687 * @destp: Destination memory pointer.
9688 * @cnt: Number of words required to be copied.
9689 *
9690 * This function is used for copying data between driver memory
9691 * and the SLI memory. This function also changes the endianness
9692 * of each word if native endianness is different from SLI
9693 * endianness. This function can be called with or without
9694 * lock.
9695 **/
dea31012005-04-17 16:05:31 -05009696void
9697lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9698{
9699 uint32_t *src = srcp;
9700 uint32_t *dest = destp;
9701 uint32_t ldata;
9702 int i;
9703
9704 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9705 ldata = *src;
9706 ldata = le32_to_cpu(ldata);
9707 *dest = ldata;
9708 src++;
9709 dest++;
9710 }
9711}
9712
James Smarte59058c2008-08-24 21:49:00 -04009713
9714/**
James Smarta0c87cb2009-07-19 10:01:10 -04009715 * lpfc_sli_bemem_bcopy - SLI memory copy function
9716 * @srcp: Source memory pointer.
9717 * @destp: Destination memory pointer.
9718 * @cnt: Number of words required to be copied.
9719 *
9720 * This function is used for copying data between a data structure
9721 * with big endian representation to local endianness.
9722 * This function can be called with or without lock.
9723 **/
9724void
9725lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9726{
9727 uint32_t *src = srcp;
9728 uint32_t *dest = destp;
9729 uint32_t ldata;
9730 int i;
9731
9732 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9733 ldata = *src;
9734 ldata = be32_to_cpu(ldata);
9735 *dest = ldata;
9736 src++;
9737 dest++;
9738 }
9739}
9740
9741/**
James Smart3621a712009-04-06 18:47:14 -04009742 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009743 * @phba: Pointer to HBA context object.
9744 * @pring: Pointer to driver SLI ring object.
9745 * @mp: Pointer to driver buffer object.
9746 *
9747 * This function is called with no lock held.
9748 * It always return zero after adding the buffer to the postbufq
9749 * buffer list.
9750 **/
dea31012005-04-17 16:05:31 -05009751int
James Smart2e0fef82007-06-17 19:56:36 -05009752lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9753 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009754{
9755 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9756 later */
James Smart2e0fef82007-06-17 19:56:36 -05009757 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009758 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009759 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009760 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009761 return 0;
9762}
9763
James Smarte59058c2008-08-24 21:49:00 -04009764/**
James Smart3621a712009-04-06 18:47:14 -04009765 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009766 * @phba: Pointer to HBA context object.
9767 *
9768 * When HBQ is enabled, buffers are searched based on tags. This function
9769 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9770 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9771 * does not conflict with tags of buffer posted for unsolicited events.
9772 * The function returns the allocated tag. The function is called with
9773 * no locks held.
9774 **/
James Smart76bb24e2007-10-27 13:38:00 -04009775uint32_t
9776lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9777{
9778 spin_lock_irq(&phba->hbalock);
9779 phba->buffer_tag_count++;
9780 /*
9781 * Always set the QUE_BUFTAG_BIT to distiguish between
9782 * a tag assigned by HBQ.
9783 */
9784 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9785 spin_unlock_irq(&phba->hbalock);
9786 return phba->buffer_tag_count;
9787}
9788
James Smarte59058c2008-08-24 21:49:00 -04009789/**
James Smart3621a712009-04-06 18:47:14 -04009790 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009791 * @phba: Pointer to HBA context object.
9792 * @pring: Pointer to driver SLI ring object.
9793 * @tag: Buffer tag.
9794 *
9795 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9796 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9797 * iocb is posted to the response ring with the tag of the buffer.
9798 * This function searches the pring->postbufq list using the tag
9799 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9800 * iocb. If the buffer is found then lpfc_dmabuf object of the
9801 * buffer is returned to the caller else NULL is returned.
9802 * This function is called with no lock held.
9803 **/
James Smart76bb24e2007-10-27 13:38:00 -04009804struct lpfc_dmabuf *
9805lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9806 uint32_t tag)
9807{
9808 struct lpfc_dmabuf *mp, *next_mp;
9809 struct list_head *slp = &pring->postbufq;
9810
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009811 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009812 spin_lock_irq(&phba->hbalock);
9813 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9814 if (mp->buffer_tag == tag) {
9815 list_del_init(&mp->list);
9816 pring->postbufq_cnt--;
9817 spin_unlock_irq(&phba->hbalock);
9818 return mp;
9819 }
9820 }
9821
9822 spin_unlock_irq(&phba->hbalock);
9823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009824 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009825 "ring %d Data x%lx x%p x%p x%x\n",
9826 pring->ringno, (unsigned long) tag,
9827 slp->next, slp->prev, pring->postbufq_cnt);
9828
9829 return NULL;
9830}
dea31012005-04-17 16:05:31 -05009831
James Smarte59058c2008-08-24 21:49:00 -04009832/**
James Smart3621a712009-04-06 18:47:14 -04009833 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009834 * @phba: Pointer to HBA context object.
9835 * @pring: Pointer to driver SLI ring object.
9836 * @phys: DMA address of the buffer.
9837 *
9838 * This function searches the buffer list using the dma_address
9839 * of unsolicited event to find the driver's lpfc_dmabuf object
9840 * corresponding to the dma_address. The function returns the
9841 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9842 * This function is called by the ct and els unsolicited event
9843 * handlers to get the buffer associated with the unsolicited
9844 * event.
9845 *
9846 * This function is called with no lock held.
9847 **/
dea31012005-04-17 16:05:31 -05009848struct lpfc_dmabuf *
9849lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9850 dma_addr_t phys)
9851{
9852 struct lpfc_dmabuf *mp, *next_mp;
9853 struct list_head *slp = &pring->postbufq;
9854
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009855 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009856 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009857 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9858 if (mp->phys == phys) {
9859 list_del_init(&mp->list);
9860 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009861 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009862 return mp;
9863 }
9864 }
9865
James Smart2e0fef82007-06-17 19:56:36 -05009866 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009868 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009869 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009870 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009871 slp->next, slp->prev, pring->postbufq_cnt);
9872 return NULL;
9873}
9874
James Smarte59058c2008-08-24 21:49:00 -04009875/**
James Smart3621a712009-04-06 18:47:14 -04009876 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009877 * @phba: Pointer to HBA context object.
9878 * @cmdiocb: Pointer to driver command iocb object.
9879 * @rspiocb: Pointer to driver response iocb object.
9880 *
9881 * This function is the completion handler for the abort iocbs for
9882 * ELS commands. This function is called from the ELS ring event
9883 * handler with no lock held. This function frees memory resources
9884 * associated with the abort iocb.
9885 **/
dea31012005-04-17 16:05:31 -05009886static void
James Smart2e0fef82007-06-17 19:56:36 -05009887lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9888 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009889{
James Smart2e0fef82007-06-17 19:56:36 -05009890 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009891 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009892 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009893
9894 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009895
9896 /*
9897 * Assume that the port already completed and returned, or
9898 * will return the iocb. Just Log the message.
9899 */
James Smart2680eea2007-04-25 09:52:55 -04009900 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9901 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9902
James Smart2e0fef82007-06-17 19:56:36 -05009903 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009904 if (phba->sli_rev < LPFC_SLI_REV4) {
9905 if (abort_iotag != 0 &&
9906 abort_iotag <= phba->sli.last_iotag)
9907 abort_iocb =
9908 phba->sli.iocbq_lookup[abort_iotag];
9909 } else
9910 /* For sli4 the abort_tag is the XRI,
9911 * so the abort routine puts the iotag of the iocb
9912 * being aborted in the context field of the abort
9913 * IOCB.
9914 */
9915 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009916
James Smart2a9bf3d2010-06-07 15:24:45 -04009917 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9918 "0327 Cannot abort els iocb %p "
9919 "with tag %x context %x, abort status %x, "
9920 "abort code %x\n",
9921 abort_iocb, abort_iotag, abort_context,
9922 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009923
James Smartff78d8f2011-12-13 13:21:35 -05009924 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009925 }
James Bottomley604a3e32005-10-29 10:28:33 -05009926 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009927 return;
9928}
9929
James Smarte59058c2008-08-24 21:49:00 -04009930/**
James Smart3621a712009-04-06 18:47:14 -04009931 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009932 * @phba: Pointer to HBA context object.
9933 * @cmdiocb: Pointer to driver command iocb object.
9934 * @rspiocb: Pointer to driver response iocb object.
9935 *
9936 * The function is called from SLI ring event handler with no
9937 * lock held. This function is the completion handler for ELS commands
9938 * which are aborted. The function frees memory resources used for
9939 * the aborted ELS commands.
9940 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009941static void
9942lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9943 struct lpfc_iocbq *rspiocb)
9944{
9945 IOCB_t *irsp = &rspiocb->iocb;
9946
9947 /* ELS cmd tag <ulpIoTag> completes */
9948 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009949 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009950 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009951 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009952 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009953 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9954 lpfc_ct_free_iocb(phba, cmdiocb);
9955 else
9956 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009957 return;
9958}
9959
James Smarte59058c2008-08-24 21:49:00 -04009960/**
James Smart5af5eee2010-10-22 11:06:38 -04009961 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009962 * @phba: Pointer to HBA context object.
9963 * @pring: Pointer to driver SLI ring object.
9964 * @cmdiocb: Pointer to driver command iocb object.
9965 *
James Smart5af5eee2010-10-22 11:06:38 -04009966 * This function issues an abort iocb for the provided command iocb down to
9967 * the port. Other than the case the outstanding command iocb is an abort
9968 * request, this function issues abort out unconditionally. This function is
9969 * called with hbalock held. The function returns 0 when it fails due to
9970 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009971 **/
James Smart5af5eee2010-10-22 11:06:38 -04009972static int
9973lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009974 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009975{
James Smart2e0fef82007-06-17 19:56:36 -05009976 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009977 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009978 IOCB_t *icmd = NULL;
9979 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009980 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009981 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009982 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009983
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009984 lockdep_assert_held(&phba->hbalock);
9985
James Smart92d7f7b2007-06-17 19:56:38 -05009986 /*
9987 * There are certain command types we don't want to abort. And we
9988 * don't want to abort commands that are already in the process of
9989 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009990 */
9991 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009992 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009993 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9994 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009995 return 0;
9996
dea31012005-04-17 16:05:31 -05009997 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009998 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009999 if (abtsiocbp == NULL)
10000 return 0;
dea31012005-04-17 16:05:31 -050010001
James Smart07951072007-04-25 09:51:38 -040010002 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010003 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010004 */
10005 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10006
dea31012005-04-17 16:05:31 -050010007 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010008 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10009 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010010 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010011 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010012 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10013 }
James Smartda0436e2009-05-22 14:51:39 -040010014 else
10015 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010016 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010017 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010018
James Smart5ffc2662009-11-18 15:39:44 -050010019 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10020 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010021 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10022 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010023 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10024 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010025
James Smart2e0fef82007-06-17 19:56:36 -050010026 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010027 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10028 else
10029 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10030
10031 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -040010032
James Smarte8b62012007-08-02 11:10:09 -040010033 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10034 "0339 Abort xri x%x, original iotag x%x, "
10035 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010036 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010037 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010038 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010039
10040 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -040010041 ring_number =
10042 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10043 if (unlikely(ring_number == LPFC_HBA_ERROR))
10044 return 0;
10045 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -040010046 /* Note: both hbalock and ring_lock need to be set here */
10047 spin_lock_irqsave(&pring->ring_lock, iflags);
10048 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10049 abtsiocbp, 0);
10050 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10051 } else {
10052 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10053 abtsiocbp, 0);
10054 }
James Smart07951072007-04-25 09:51:38 -040010055
James Smartd7c255b2008-08-24 21:50:00 -040010056 if (retval)
10057 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010058
10059 /*
10060 * Caller to this routine should check for IOCB_ERROR
10061 * and handle it properly. This routine no longer removes
10062 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10063 */
10064 return retval;
10065}
10066
10067/**
10068 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10069 * @phba: Pointer to HBA context object.
10070 * @pring: Pointer to driver SLI ring object.
10071 * @cmdiocb: Pointer to driver command iocb object.
10072 *
10073 * This function issues an abort iocb for the provided command iocb. In case
10074 * of unloading, the abort iocb will not be issued to commands on the ELS
10075 * ring. Instead, the callback function shall be changed to those commands
10076 * so that nothing happens when them finishes. This function is called with
10077 * hbalock held. The function returns 0 when the command iocb is an abort
10078 * request.
10079 **/
10080int
10081lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10082 struct lpfc_iocbq *cmdiocb)
10083{
10084 struct lpfc_vport *vport = cmdiocb->vport;
10085 int retval = IOCB_ERROR;
10086 IOCB_t *icmd = NULL;
10087
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010088 lockdep_assert_held(&phba->hbalock);
10089
James Smart5af5eee2010-10-22 11:06:38 -040010090 /*
10091 * There are certain command types we don't want to abort. And we
10092 * don't want to abort commands that are already in the process of
10093 * being aborted.
10094 */
10095 icmd = &cmdiocb->iocb;
10096 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10097 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10098 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10099 return 0;
10100
10101 /*
10102 * If we're unloading, don't abort iocb on the ELS ring, but change
10103 * the callback so that nothing happens when it finishes.
10104 */
10105 if ((vport->load_flag & FC_UNLOADING) &&
10106 (pring->ringno == LPFC_ELS_RING)) {
10107 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10108 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10109 else
10110 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10111 goto abort_iotag_exit;
10112 }
10113
10114 /* Now, we try to issue the abort to the cmdiocb out */
10115 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10116
James Smart07951072007-04-25 09:51:38 -040010117abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010118 /*
10119 * Caller to this routine should check for IOCB_ERROR
10120 * and handle it properly. This routine no longer removes
10121 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010122 */
James Smart2e0fef82007-06-17 19:56:36 -050010123 return retval;
dea31012005-04-17 16:05:31 -050010124}
10125
James Smarte59058c2008-08-24 21:49:00 -040010126/**
James Smart5af5eee2010-10-22 11:06:38 -040010127 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10128 * @phba: pointer to lpfc HBA data structure.
10129 *
10130 * This routine will abort all pending and outstanding iocbs to an HBA.
10131 **/
10132void
10133lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10134{
10135 struct lpfc_sli *psli = &phba->sli;
10136 struct lpfc_sli_ring *pring;
10137 int i;
10138
10139 for (i = 0; i < psli->num_rings; i++) {
10140 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010141 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010142 }
10143}
10144
10145/**
James Smart3621a712009-04-06 18:47:14 -040010146 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010147 * @iocbq: Pointer to driver iocb object.
10148 * @vport: Pointer to driver virtual port object.
10149 * @tgt_id: SCSI ID of the target.
10150 * @lun_id: LUN ID of the scsi device.
10151 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10152 *
James Smart3621a712009-04-06 18:47:14 -040010153 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010154 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10155 * 0 if the filtering criteria is met for the given iocb and will return
10156 * 1 if the filtering criteria is not met.
10157 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10158 * given iocb is for the SCSI device specified by vport, tgt_id and
10159 * lun_id parameter.
10160 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10161 * given iocb is for the SCSI target specified by vport and tgt_id
10162 * parameters.
10163 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10164 * given iocb is for the SCSI host associated with the given vport.
10165 * This function is called with no locks held.
10166 **/
dea31012005-04-17 16:05:31 -050010167static int
James Smart51ef4c22007-08-02 11:10:31 -040010168lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10169 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010170 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010171{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010172 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010173 int rc = 1;
10174
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010175 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10176 return rc;
10177
James Smart51ef4c22007-08-02 11:10:31 -040010178 if (iocbq->vport != vport)
10179 return rc;
10180
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010181 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010182
James Smart495a7142008-06-14 22:52:59 -040010183 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010184 return rc;
10185
10186 switch (ctx_cmd) {
10187 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010188 if ((lpfc_cmd->rdata->pnode) &&
10189 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10190 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010191 rc = 0;
10192 break;
10193 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010194 if ((lpfc_cmd->rdata->pnode) &&
10195 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010196 rc = 0;
10197 break;
dea31012005-04-17 16:05:31 -050010198 case LPFC_CTX_HOST:
10199 rc = 0;
10200 break;
10201 default:
10202 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010203 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010204 break;
10205 }
10206
10207 return rc;
10208}
10209
James Smarte59058c2008-08-24 21:49:00 -040010210/**
James Smart3621a712009-04-06 18:47:14 -040010211 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010212 * @vport: Pointer to virtual port.
10213 * @tgt_id: SCSI ID of the target.
10214 * @lun_id: LUN ID of the scsi device.
10215 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10216 *
10217 * This function returns number of FCP commands pending for the vport.
10218 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10219 * commands pending on the vport associated with SCSI device specified
10220 * by tgt_id and lun_id parameters.
10221 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10222 * commands pending on the vport associated with SCSI target specified
10223 * by tgt_id parameter.
10224 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10225 * commands pending on the vport.
10226 * This function returns the number of iocbs which satisfy the filter.
10227 * This function is called without any lock held.
10228 **/
dea31012005-04-17 16:05:31 -050010229int
James Smart51ef4c22007-08-02 11:10:31 -040010230lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10231 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010232{
James Smart51ef4c22007-08-02 11:10:31 -040010233 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010234 struct lpfc_iocbq *iocbq;
10235 int sum, i;
dea31012005-04-17 16:05:31 -050010236
Johannes Thumshirn31979002016-07-18 16:06:03 +020010237 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010238 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10239 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010240
James Smart51ef4c22007-08-02 11:10:31 -040010241 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10242 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010243 sum++;
dea31012005-04-17 16:05:31 -050010244 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010245 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010246
dea31012005-04-17 16:05:31 -050010247 return sum;
10248}
10249
James Smarte59058c2008-08-24 21:49:00 -040010250/**
James Smart3621a712009-04-06 18:47:14 -040010251 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010252 * @phba: Pointer to HBA context object
10253 * @cmdiocb: Pointer to command iocb object.
10254 * @rspiocb: Pointer to response iocb object.
10255 *
10256 * This function is called when an aborted FCP iocb completes. This
10257 * function is called by the ring event handler with no lock held.
10258 * This function frees the iocb.
10259 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010260void
James Smart2e0fef82007-06-17 19:56:36 -050010261lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10262 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010263{
James Smartcb69f7d2011-12-13 13:21:57 -050010264 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010265 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010266 "original iotag x%x, abort cmd iotag x%x "
10267 "status 0x%x, reason 0x%x\n",
10268 cmdiocb->iocb.un.acxri.abortContextTag,
10269 cmdiocb->iocb.un.acxri.abortIoTag,
10270 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10271 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010272 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010273 return;
10274}
10275
James Smarte59058c2008-08-24 21:49:00 -040010276/**
James Smart3621a712009-04-06 18:47:14 -040010277 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010278 * @vport: Pointer to virtual port.
10279 * @pring: Pointer to driver SLI ring object.
10280 * @tgt_id: SCSI ID of the target.
10281 * @lun_id: LUN ID of the scsi device.
10282 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10283 *
10284 * This function sends an abort command for every SCSI command
10285 * associated with the given virtual port pending on the ring
10286 * filtered by lpfc_sli_validate_fcp_iocb function.
10287 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10288 * FCP iocbs associated with lun specified by tgt_id and lun_id
10289 * parameters
10290 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10291 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10292 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10293 * FCP iocbs associated with virtual port.
10294 * This function returns number of iocbs it failed to abort.
10295 * This function is called with no locks held.
10296 **/
dea31012005-04-17 16:05:31 -050010297int
James Smart51ef4c22007-08-02 11:10:31 -040010298lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10299 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010300{
James Smart51ef4c22007-08-02 11:10:31 -040010301 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010302 struct lpfc_iocbq *iocbq;
10303 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010304 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010305 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010306 int i;
dea31012005-04-17 16:05:31 -050010307
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010308 for (i = 1; i <= phba->sli.last_iotag; i++) {
10309 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010310
James Smart51ef4c22007-08-02 11:10:31 -040010311 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010312 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010313 continue;
10314
James Smartafbd8d82013-09-06 12:22:13 -040010315 /*
10316 * If the iocbq is already being aborted, don't take a second
10317 * action, but do count it.
10318 */
10319 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10320 continue;
10321
dea31012005-04-17 16:05:31 -050010322 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010323 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010324 if (abtsiocb == NULL) {
10325 errcnt++;
10326 continue;
10327 }
dea31012005-04-17 16:05:31 -050010328
James Smartafbd8d82013-09-06 12:22:13 -040010329 /* indicate the IO is being aborted by the driver. */
10330 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10331
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010332 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010333 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10334 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010335 if (phba->sli_rev == LPFC_SLI_REV4)
10336 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10337 else
10338 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010339 abtsiocb->iocb.ulpLe = 1;
10340 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010341 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010342
James Smart5ffc2662009-11-18 15:39:44 -050010343 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10344 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010345 if (iocbq->iocb_flag & LPFC_IO_FCP)
10346 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010347 if (iocbq->iocb_flag & LPFC_IO_FOF)
10348 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010349
James Smart2e0fef82007-06-17 19:56:36 -050010350 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010351 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10352 else
10353 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10354
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010355 /* Setup callback routine and issue the command. */
10356 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010357 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10358 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010359 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010360 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010361 errcnt++;
10362 continue;
10363 }
10364 }
10365
10366 return errcnt;
10367}
10368
James Smarte59058c2008-08-24 21:49:00 -040010369/**
James Smart98912dda2014-04-04 13:52:31 -040010370 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10371 * @vport: Pointer to virtual port.
10372 * @pring: Pointer to driver SLI ring object.
10373 * @tgt_id: SCSI ID of the target.
10374 * @lun_id: LUN ID of the scsi device.
10375 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10376 *
10377 * This function sends an abort command for every SCSI command
10378 * associated with the given virtual port pending on the ring
10379 * filtered by lpfc_sli_validate_fcp_iocb function.
10380 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10381 * FCP iocbs associated with lun specified by tgt_id and lun_id
10382 * parameters
10383 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10384 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10385 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10386 * FCP iocbs associated with virtual port.
10387 * This function returns number of iocbs it aborted .
10388 * This function is called with no locks held right after a taskmgmt
10389 * command is sent.
10390 **/
10391int
10392lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10393 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10394{
10395 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010396 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010397 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010398 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010399 struct lpfc_iocbq *iocbq;
10400 IOCB_t *icmd;
10401 int sum, i, ret_val;
10402 unsigned long iflags;
10403 struct lpfc_sli_ring *pring_s4;
10404 uint32_t ring_number;
10405
10406 spin_lock_irq(&phba->hbalock);
10407
10408 /* all I/Os are in process of being flushed */
10409 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10410 spin_unlock_irq(&phba->hbalock);
10411 return 0;
10412 }
10413 sum = 0;
10414
10415 for (i = 1; i <= phba->sli.last_iotag; i++) {
10416 iocbq = phba->sli.iocbq_lookup[i];
10417
10418 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10419 cmd) != 0)
10420 continue;
10421
10422 /*
10423 * If the iocbq is already being aborted, don't take a second
10424 * action, but do count it.
10425 */
10426 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10427 continue;
10428
10429 /* issue ABTS for this IOCB based on iotag */
10430 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10431 if (abtsiocbq == NULL)
10432 continue;
10433
10434 icmd = &iocbq->iocb;
10435 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10436 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10437 if (phba->sli_rev == LPFC_SLI_REV4)
10438 abtsiocbq->iocb.un.acxri.abortIoTag =
10439 iocbq->sli4_xritag;
10440 else
10441 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10442 abtsiocbq->iocb.ulpLe = 1;
10443 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10444 abtsiocbq->vport = vport;
10445
10446 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10447 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10448 if (iocbq->iocb_flag & LPFC_IO_FCP)
10449 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010450 if (iocbq->iocb_flag & LPFC_IO_FOF)
10451 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010452
James Smart8c50d252014-09-03 12:58:16 -040010453 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10454 ndlp = lpfc_cmd->rdata->pnode;
10455
10456 if (lpfc_is_link_up(phba) &&
10457 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010458 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10459 else
10460 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10461
10462 /* Setup callback routine and issue the command. */
10463 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10464
10465 /*
10466 * Indicate the IO is being aborted by the driver and set
10467 * the caller's flag into the aborted IO.
10468 */
10469 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10470
10471 if (phba->sli_rev == LPFC_SLI_REV4) {
10472 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10473 iocbq->fcp_wqidx;
10474 pring_s4 = &phba->sli.ring[ring_number];
10475 /* Note: both hbalock and ring_lock must be set here */
10476 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10477 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10478 abtsiocbq, 0);
10479 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10480 } else {
10481 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10482 abtsiocbq, 0);
10483 }
10484
10485
10486 if (ret_val == IOCB_ERROR)
10487 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10488 else
10489 sum++;
10490 }
10491 spin_unlock_irq(&phba->hbalock);
10492 return sum;
10493}
10494
10495/**
James Smart3621a712009-04-06 18:47:14 -040010496 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010497 * @phba: Pointer to HBA context object.
10498 * @cmdiocbq: Pointer to command iocb.
10499 * @rspiocbq: Pointer to response iocb.
10500 *
10501 * This function is the completion handler for iocbs issued using
10502 * lpfc_sli_issue_iocb_wait function. This function is called by the
10503 * ring event handler function without any lock held. This function
10504 * can be called from both worker thread context and interrupt
10505 * context. This function also can be called from other thread which
10506 * cleans up the SLI layer objects.
10507 * This function copy the contents of the response iocb to the
10508 * response iocb memory object provided by the caller of
10509 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10510 * sleeps for the iocb completion.
10511 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010512static void
10513lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10514 struct lpfc_iocbq *cmdiocbq,
10515 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010516{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010517 wait_queue_head_t *pdone_q;
10518 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010519 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010520
James Smart2e0fef82007-06-17 19:56:36 -050010521 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010522 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10523
10524 /*
10525 * A time out has occurred for the iocb. If a time out
10526 * completion handler has been supplied, call it. Otherwise,
10527 * just free the iocbq.
10528 */
10529
10530 spin_unlock_irqrestore(&phba->hbalock, iflags);
10531 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10532 cmdiocbq->wait_iocb_cmpl = NULL;
10533 if (cmdiocbq->iocb_cmpl)
10534 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10535 else
10536 lpfc_sli_release_iocbq(phba, cmdiocbq);
10537 return;
10538 }
10539
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010540 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10541 if (cmdiocbq->context2 && rspiocbq)
10542 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10543 &rspiocbq->iocb, sizeof(IOCB_t));
10544
James Smart0f65ff62010-02-26 14:14:23 -050010545 /* Set the exchange busy flag for task management commands */
10546 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10547 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10548 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10549 cur_iocbq);
10550 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10551 }
10552
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010553 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010554 if (pdone_q)
10555 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010556 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010557 return;
10558}
10559
James Smarte59058c2008-08-24 21:49:00 -040010560/**
James Smartd11e31d2009-06-10 17:23:06 -040010561 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10562 * @phba: Pointer to HBA context object..
10563 * @piocbq: Pointer to command iocb.
10564 * @flag: Flag to test.
10565 *
10566 * This routine grabs the hbalock and then test the iocb_flag to
10567 * see if the passed in flag is set.
10568 * Returns:
10569 * 1 if flag is set.
10570 * 0 if flag is not set.
10571 **/
10572static int
10573lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10574 struct lpfc_iocbq *piocbq, uint32_t flag)
10575{
10576 unsigned long iflags;
10577 int ret;
10578
10579 spin_lock_irqsave(&phba->hbalock, iflags);
10580 ret = piocbq->iocb_flag & flag;
10581 spin_unlock_irqrestore(&phba->hbalock, iflags);
10582 return ret;
10583
10584}
10585
10586/**
James Smart3621a712009-04-06 18:47:14 -040010587 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010588 * @phba: Pointer to HBA context object..
10589 * @pring: Pointer to sli ring.
10590 * @piocb: Pointer to command iocb.
10591 * @prspiocbq: Pointer to response iocb.
10592 * @timeout: Timeout in number of seconds.
10593 *
10594 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010595 * iocb to complete. The iocb_cmpl field of the shall be used
10596 * to handle iocbs which time out. If the field is NULL, the
10597 * function shall free the iocbq structure. If more clean up is
10598 * needed, the caller is expected to provide a completion function
10599 * that will provide the needed clean up. If the iocb command is
10600 * not completed within timeout seconds, the function will either
10601 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10602 * completion function set in the iocb_cmpl field and then return
10603 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10604 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010605 * The function waits for the iocb completion using an
10606 * non-interruptible wait.
10607 * This function will sleep while waiting for iocb completion.
10608 * So, this function should not be called from any context which
10609 * does not allow sleeping. Due to the same reason, this function
10610 * cannot be called with interrupt disabled.
10611 * This function assumes that the iocb completions occur while
10612 * this function sleep. So, this function cannot be called from
10613 * the thread which process iocb completion for this ring.
10614 * This function clears the iocb_flag of the iocb object before
10615 * issuing the iocb and the iocb completion handler sets this
10616 * flag and wakes this thread when the iocb completes.
10617 * The contents of the response iocb will be copied to prspiocbq
10618 * by the completion handler when the command completes.
10619 * This function returns IOCB_SUCCESS when success.
10620 * This function is called with no lock held.
10621 **/
dea31012005-04-17 16:05:31 -050010622int
James Smart2e0fef82007-06-17 19:56:36 -050010623lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010624 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010625 struct lpfc_iocbq *piocb,
10626 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010627 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010628{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010629 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010630 long timeleft, timeout_req = 0;
10631 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010632 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010633 struct lpfc_iocbq *iocb;
10634 int txq_cnt = 0;
10635 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010636 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010637 unsigned long iflags;
10638 bool iocb_completed = true;
10639
dea31012005-04-17 16:05:31 -050010640 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010641 * If the caller has provided a response iocbq buffer, then context2
10642 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010643 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010644 if (prspiocbq) {
10645 if (piocb->context2)
10646 return IOCB_ERROR;
10647 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010648 }
10649
James Smart5a0916b2013-07-15 18:31:42 -040010650 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010651 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10652 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010653 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010654
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010655 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010656 if (lpfc_readl(phba->HCregaddr, &creg_val))
10657 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010658 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10659 writel(creg_val, phba->HCregaddr);
10660 readl(phba->HCregaddr); /* flush */
10661 }
10662
James Smart2a9bf3d2010-06-07 15:24:45 -040010663 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10664 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010665 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010666 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010667 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010668 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010669 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010670 spin_lock_irqsave(&phba->hbalock, iflags);
10671 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010672
James Smart5a0916b2013-07-15 18:31:42 -040010673 /*
10674 * IOCB timed out. Inform the wake iocb wait
10675 * completion function and set local status
10676 */
10677
10678 iocb_completed = false;
10679 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10680 }
10681 spin_unlock_irqrestore(&phba->hbalock, iflags);
10682 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010683 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010684 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010685 /* Note: we are not indicating if the IOCB has a success
10686 * status or not - that's for the caller to check.
10687 * IOCB_SUCCESS means just that the command was sent and
10688 * completed. Not that it completed successfully.
10689 * */
James Smart7054a602007-04-25 09:52:34 -040010690 } else if (timeleft == 0) {
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 "0338 IOCB wait timeout error - no "
10693 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010694 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010695 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010696 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010697 "0330 IOCB wake NOT set, "
10698 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010699 timeout, (timeleft / jiffies));
10700 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010701 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010702 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010703 if (phba->cfg_log_verbose & LOG_SLI) {
10704 list_for_each_entry(iocb, &pring->txq, list) {
10705 txq_cnt++;
10706 }
10707 list_for_each_entry(iocb, &pring->txcmplq, list) {
10708 txcmplq_cnt++;
10709 }
10710 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10711 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10712 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10713 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010714 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010715 } else {
10716 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010717 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010718 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010719 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010720 }
10721
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010722 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010723 if (lpfc_readl(phba->HCregaddr, &creg_val))
10724 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010725 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10726 writel(creg_val, phba->HCregaddr);
10727 readl(phba->HCregaddr); /* flush */
10728 }
10729
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010730 if (prspiocbq)
10731 piocb->context2 = NULL;
10732
10733 piocb->context_un.wait_queue = NULL;
10734 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010735 return retval;
10736}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010737
James Smarte59058c2008-08-24 21:49:00 -040010738/**
James Smart3621a712009-04-06 18:47:14 -040010739 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010740 * @phba: Pointer to HBA context object.
10741 * @pmboxq: Pointer to driver mailbox object.
10742 * @timeout: Timeout in number of seconds.
10743 *
10744 * This function issues the mailbox to firmware and waits for the
10745 * mailbox command to complete. If the mailbox command is not
10746 * completed within timeout seconds, it returns MBX_TIMEOUT.
10747 * The function waits for the mailbox completion using an
10748 * interruptible wait. If the thread is woken up due to a
10749 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10750 * should not free the mailbox resources, if this function returns
10751 * MBX_TIMEOUT.
10752 * This function will sleep while waiting for mailbox completion.
10753 * So, this function should not be called from any context which
10754 * does not allow sleeping. Due to the same reason, this function
10755 * cannot be called with interrupt disabled.
10756 * This function assumes that the mailbox completion occurs while
10757 * this function sleep. So, this function cannot be called from
10758 * the worker thread which processes mailbox completion.
10759 * This function is called in the context of HBA management
10760 * applications.
10761 * This function returns MBX_SUCCESS when successful.
10762 * This function is called with no lock held.
10763 **/
dea31012005-04-17 16:05:31 -050010764int
James Smart2e0fef82007-06-17 19:56:36 -050010765lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010766 uint32_t timeout)
10767{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010768 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010769 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010770 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010771 unsigned long flag;
dea31012005-04-17 16:05:31 -050010772
James Smartb230b8a2013-05-31 17:05:27 -040010773 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010774 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010775 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010776
James Smart495a7142008-06-14 22:52:59 -040010777 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010778 /* setup wake call as IOCB callback */
10779 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10780 /* setup context field to pass wait_queue pointer to wake function */
10781 pmboxq->context1 = &done_q;
10782
dea31012005-04-17 16:05:31 -050010783 /* now issue the command */
10784 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010785 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010786 wait_event_interruptible_timeout(done_q,
10787 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010788 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010789
James Smart858c9f62007-06-17 19:56:39 -050010790 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010791 /* restore the possible extended buffer for free resource */
10792 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010793 /*
10794 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10795 * else do not free the resources.
10796 */
James Smartd7c47992010-06-08 18:31:54 -040010797 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010798 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010799 } else {
James Smart7054a602007-04-25 09:52:34 -040010800 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010801 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10802 }
10803 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010804 } else {
10805 /* restore the possible extended buffer for free resource */
10806 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010807 }
10808
dea31012005-04-17 16:05:31 -050010809 return retval;
10810}
10811
James Smarte59058c2008-08-24 21:49:00 -040010812/**
James Smart3772a992009-05-22 14:50:54 -040010813 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010814 * @phba: Pointer to HBA context.
10815 *
James Smart3772a992009-05-22 14:50:54 -040010816 * This function is called to shutdown the driver's mailbox sub-system.
10817 * It first marks the mailbox sub-system is in a block state to prevent
10818 * the asynchronous mailbox command from issued off the pending mailbox
10819 * command queue. If the mailbox command sub-system shutdown is due to
10820 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10821 * the mailbox sub-system flush routine to forcefully bring down the
10822 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10823 * as with offline or HBA function reset), this routine will wait for the
10824 * outstanding mailbox command to complete before invoking the mailbox
10825 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010826 **/
James Smart3772a992009-05-22 14:50:54 -040010827void
James Smart618a5232012-06-12 13:54:36 -040010828lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010829{
James Smart3772a992009-05-22 14:50:54 -040010830 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010831 unsigned long timeout;
10832
James Smart618a5232012-06-12 13:54:36 -040010833 if (mbx_action == LPFC_MBX_NO_WAIT) {
10834 /* delay 100ms for port state */
10835 msleep(100);
10836 lpfc_sli_mbox_sys_flush(phba);
10837 return;
10838 }
James Smarta183a152011-10-10 21:32:43 -040010839 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010840
James Smart3772a992009-05-22 14:50:54 -040010841 spin_lock_irq(&phba->hbalock);
10842 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010843
10844 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010845 /* Determine how long we might wait for the active mailbox
10846 * command to be gracefully completed by firmware.
10847 */
James Smarta183a152011-10-10 21:32:43 -040010848 if (phba->sli.mbox_active)
10849 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10850 phba->sli.mbox_active) *
10851 1000) + jiffies;
10852 spin_unlock_irq(&phba->hbalock);
10853
James Smart3772a992009-05-22 14:50:54 -040010854 while (phba->sli.mbox_active) {
10855 /* Check active mailbox complete status every 2ms */
10856 msleep(2);
10857 if (time_after(jiffies, timeout))
10858 /* Timeout, let the mailbox flush routine to
10859 * forcefully release active mailbox command
10860 */
10861 break;
10862 }
James Smartd7069f02012-03-01 22:36:29 -050010863 } else
10864 spin_unlock_irq(&phba->hbalock);
10865
James Smart3772a992009-05-22 14:50:54 -040010866 lpfc_sli_mbox_sys_flush(phba);
10867}
10868
10869/**
10870 * lpfc_sli_eratt_read - read sli-3 error attention events
10871 * @phba: Pointer to HBA context.
10872 *
10873 * This function is called to read the SLI3 device error attention registers
10874 * for possible error attention events. The caller must hold the hostlock
10875 * with spin_lock_irq().
10876 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010877 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010878 * Register and returns 0 otherwise.
10879 **/
10880static int
10881lpfc_sli_eratt_read(struct lpfc_hba *phba)
10882{
James Smarted957682007-06-17 19:56:37 -050010883 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010884
James Smart3772a992009-05-22 14:50:54 -040010885 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010886 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10887 goto unplug_err;
10888
James Smart3772a992009-05-22 14:50:54 -040010889 if (ha_copy & HA_ERATT) {
10890 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010891 if (lpfc_sli_read_hs(phba))
10892 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010893
James Smart3772a992009-05-22 14:50:54 -040010894 /* Check if there is a deferred error condition is active */
10895 if ((HS_FFER1 & phba->work_hs) &&
10896 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010897 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010898 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010899 /* Clear all interrupt enable conditions */
10900 writel(0, phba->HCregaddr);
10901 readl(phba->HCregaddr);
10902 }
10903
10904 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010905 phba->work_ha |= HA_ERATT;
10906 /* Indicate polling handles this ERATT */
10907 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010908 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010909 }
James Smart3772a992009-05-22 14:50:54 -040010910 return 0;
James Smart9940b972011-03-11 16:06:12 -050010911
10912unplug_err:
10913 /* Set the driver HS work bitmap */
10914 phba->work_hs |= UNPLUG_ERR;
10915 /* Set the driver HA work bitmap */
10916 phba->work_ha |= HA_ERATT;
10917 /* Indicate polling handles this ERATT */
10918 phba->hba_flag |= HBA_ERATT_HANDLED;
10919 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010920}
10921
James Smarte59058c2008-08-24 21:49:00 -040010922/**
James Smartda0436e2009-05-22 14:51:39 -040010923 * lpfc_sli4_eratt_read - read sli-4 error attention events
10924 * @phba: Pointer to HBA context.
10925 *
10926 * This function is called to read the SLI4 device error attention registers
10927 * for possible error attention events. The caller must hold the hostlock
10928 * with spin_lock_irq().
10929 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010930 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010931 * Register and returns 0 otherwise.
10932 **/
10933static int
10934lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10935{
10936 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010937 uint32_t if_type, portsmphr;
10938 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010939
James Smart2fcee4b2010-12-15 17:57:46 -050010940 /*
10941 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010942 * registers for error attention. This can be changed later.
10943 */
James Smart2fcee4b2010-12-15 17:57:46 -050010944 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10945 switch (if_type) {
10946 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010947 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10948 &uerr_sta_lo) ||
10949 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10950 &uerr_sta_hi)) {
10951 phba->work_hs |= UNPLUG_ERR;
10952 phba->work_ha |= HA_ERATT;
10953 phba->hba_flag |= HBA_ERATT_HANDLED;
10954 return 1;
10955 }
James Smart2fcee4b2010-12-15 17:57:46 -050010956 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10957 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10959 "1423 HBA Unrecoverable error: "
10960 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10961 "ue_mask_lo_reg=0x%x, "
10962 "ue_mask_hi_reg=0x%x\n",
10963 uerr_sta_lo, uerr_sta_hi,
10964 phba->sli4_hba.ue_mask_lo,
10965 phba->sli4_hba.ue_mask_hi);
10966 phba->work_status[0] = uerr_sta_lo;
10967 phba->work_status[1] = uerr_sta_hi;
10968 phba->work_ha |= HA_ERATT;
10969 phba->hba_flag |= HBA_ERATT_HANDLED;
10970 return 1;
10971 }
10972 break;
10973 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010974 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10975 &portstat_reg.word0) ||
10976 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10977 &portsmphr)){
10978 phba->work_hs |= UNPLUG_ERR;
10979 phba->work_ha |= HA_ERATT;
10980 phba->hba_flag |= HBA_ERATT_HANDLED;
10981 return 1;
10982 }
James Smart2fcee4b2010-12-15 17:57:46 -050010983 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10984 phba->work_status[0] =
10985 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10986 phba->work_status[1] =
10987 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010989 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010990 "port status reg 0x%x, "
10991 "port smphr reg 0x%x, "
10992 "error 1=0x%x, error 2=0x%x\n",
10993 portstat_reg.word0,
10994 portsmphr,
10995 phba->work_status[0],
10996 phba->work_status[1]);
10997 phba->work_ha |= HA_ERATT;
10998 phba->hba_flag |= HBA_ERATT_HANDLED;
10999 return 1;
11000 }
11001 break;
11002 case LPFC_SLI_INTF_IF_TYPE_1:
11003 default:
James Smarta747c9c2009-11-18 15:41:10 -050011004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011005 "2886 HBA Error Attention on unsupported "
11006 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011007 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011008 }
James Smart2fcee4b2010-12-15 17:57:46 -050011009
James Smartda0436e2009-05-22 14:51:39 -040011010 return 0;
11011}
11012
11013/**
James Smart3621a712009-04-06 18:47:14 -040011014 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011015 * @phba: Pointer to HBA context.
11016 *
James Smart3772a992009-05-22 14:50:54 -040011017 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011018 * error attention register bit for error attention events.
11019 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011020 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011021 * Register and returns 0 otherwise.
11022 **/
11023int
11024lpfc_sli_check_eratt(struct lpfc_hba *phba)
11025{
11026 uint32_t ha_copy;
11027
11028 /* If somebody is waiting to handle an eratt, don't process it
11029 * here. The brdkill function will do this.
11030 */
11031 if (phba->link_flag & LS_IGNORE_ERATT)
11032 return 0;
11033
11034 /* Check if interrupt handler handles this ERATT */
11035 spin_lock_irq(&phba->hbalock);
11036 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11037 /* Interrupt handler has handled ERATT */
11038 spin_unlock_irq(&phba->hbalock);
11039 return 0;
11040 }
11041
James Smarta257bf92009-04-06 18:48:10 -040011042 /*
11043 * If there is deferred error attention, do not check for error
11044 * attention
11045 */
11046 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11047 spin_unlock_irq(&phba->hbalock);
11048 return 0;
11049 }
11050
James Smart3772a992009-05-22 14:50:54 -040011051 /* If PCI channel is offline, don't process it */
11052 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011053 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011054 return 0;
11055 }
11056
11057 switch (phba->sli_rev) {
11058 case LPFC_SLI_REV2:
11059 case LPFC_SLI_REV3:
11060 /* Read chip Host Attention (HA) register */
11061 ha_copy = lpfc_sli_eratt_read(phba);
11062 break;
James Smartda0436e2009-05-22 14:51:39 -040011063 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011064 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011065 ha_copy = lpfc_sli4_eratt_read(phba);
11066 break;
James Smart3772a992009-05-22 14:50:54 -040011067 default:
11068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11069 "0299 Invalid SLI revision (%d)\n",
11070 phba->sli_rev);
11071 ha_copy = 0;
11072 break;
James Smart93996272008-08-24 21:50:30 -040011073 }
11074 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011075
11076 return ha_copy;
11077}
11078
11079/**
11080 * lpfc_intr_state_check - Check device state for interrupt handling
11081 * @phba: Pointer to HBA context.
11082 *
11083 * This inline routine checks whether a device or its PCI slot is in a state
11084 * that the interrupt should be handled.
11085 *
11086 * This function returns 0 if the device or the PCI slot is in a state that
11087 * interrupt should be handled, otherwise -EIO.
11088 */
11089static inline int
11090lpfc_intr_state_check(struct lpfc_hba *phba)
11091{
11092 /* If the pci channel is offline, ignore all the interrupts */
11093 if (unlikely(pci_channel_offline(phba->pcidev)))
11094 return -EIO;
11095
11096 /* Update device level interrupt statistics */
11097 phba->sli.slistat.sli_intr++;
11098
11099 /* Ignore all interrupts during initialization. */
11100 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11101 return -EIO;
11102
James Smart93996272008-08-24 21:50:30 -040011103 return 0;
11104}
11105
11106/**
James Smart3772a992009-05-22 14:50:54 -040011107 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011108 * @irq: Interrupt number.
11109 * @dev_id: The device context pointer.
11110 *
James Smart93996272008-08-24 21:50:30 -040011111 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011112 * service routine when device with SLI-3 interface spec is enabled with
11113 * MSI-X multi-message interrupt mode and there are slow-path events in
11114 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11115 * interrupt mode, this function is called as part of the device-level
11116 * interrupt handler. When the PCI slot is in error recovery or the HBA
11117 * is undergoing initialization, the interrupt handler will not process
11118 * the interrupt. The link attention and ELS ring attention events are
11119 * handled by the worker thread. The interrupt handler signals the worker
11120 * thread and returns for these events. This function is called without
11121 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011122 * structures.
11123 *
11124 * This function returns IRQ_HANDLED when interrupt is handled else it
11125 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011126 **/
dea31012005-04-17 16:05:31 -050011127irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011128lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011129{
James Smart2e0fef82007-06-17 19:56:36 -050011130 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011131 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011132 uint32_t work_ha_copy;
11133 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011134 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011135 uint32_t control;
11136
James Smart92d7f7b2007-06-17 19:56:38 -050011137 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011138 struct lpfc_vport *vport;
11139 struct lpfc_nodelist *ndlp;
11140 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011141 LPFC_MBOXQ_t *pmb;
11142 int rc;
11143
dea31012005-04-17 16:05:31 -050011144 /*
11145 * Get the driver's phba structure from the dev_id and
11146 * assume the HBA is not interrupting.
11147 */
James Smart93996272008-08-24 21:50:30 -040011148 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011149
11150 if (unlikely(!phba))
11151 return IRQ_NONE;
11152
dea31012005-04-17 16:05:31 -050011153 /*
James Smart93996272008-08-24 21:50:30 -040011154 * Stuff needs to be attented to when this function is invoked as an
11155 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011156 */
James Smart93996272008-08-24 21:50:30 -040011157 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011158 /* Check device state for handling interrupt */
11159 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011160 return IRQ_NONE;
11161 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011162 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011163 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11164 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011165 /* If somebody is waiting to handle an eratt don't process it
11166 * here. The brdkill function will do this.
11167 */
11168 if (phba->link_flag & LS_IGNORE_ERATT)
11169 ha_copy &= ~HA_ERATT;
11170 /* Check the need for handling ERATT in interrupt handler */
11171 if (ha_copy & HA_ERATT) {
11172 if (phba->hba_flag & HBA_ERATT_HANDLED)
11173 /* ERATT polling has handled ERATT */
11174 ha_copy &= ~HA_ERATT;
11175 else
11176 /* Indicate interrupt handler handles ERATT */
11177 phba->hba_flag |= HBA_ERATT_HANDLED;
11178 }
James Smarta257bf92009-04-06 18:48:10 -040011179
11180 /*
11181 * If there is deferred error attention, do not check for any
11182 * interrupt.
11183 */
11184 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011185 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011186 return IRQ_NONE;
11187 }
11188
James Smart93996272008-08-24 21:50:30 -040011189 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011190 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11191 goto unplug_error;
11192
James Smarta747c9c2009-11-18 15:41:10 -050011193 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11194 HC_LAINT_ENA | HC_ERINT_ENA),
11195 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011196 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11197 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011198 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011199 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011200 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011201 } else
11202 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011203
dea31012005-04-17 16:05:31 -050011204 work_ha_copy = ha_copy & phba->work_ha_mask;
11205
James Smart93996272008-08-24 21:50:30 -040011206 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011207 if (work_ha_copy & HA_LATT) {
11208 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11209 /*
11210 * Turn off Link Attention interrupts
11211 * until CLEAR_LA done
11212 */
James Smart5b75da22008-12-04 22:39:35 -050011213 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011214 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011215 if (lpfc_readl(phba->HCregaddr, &control))
11216 goto unplug_error;
dea31012005-04-17 16:05:31 -050011217 control &= ~HC_LAINT_ENA;
11218 writel(control, phba->HCregaddr);
11219 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011220 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011221 }
11222 else
11223 work_ha_copy &= ~HA_LATT;
11224 }
11225
James Smart93996272008-08-24 21:50:30 -040011226 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011227 /*
11228 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11229 * the only slow ring.
11230 */
11231 status = (work_ha_copy &
11232 (HA_RXMASK << (4*LPFC_ELS_RING)));
11233 status >>= (4*LPFC_ELS_RING);
11234 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011235 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011236 if (lpfc_readl(phba->HCregaddr, &control))
11237 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011238
11239 lpfc_debugfs_slow_ring_trc(phba,
11240 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11241 control, status,
11242 (uint32_t)phba->sli.slistat.sli_intr);
11243
James Smart858c9f62007-06-17 19:56:39 -050011244 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011245 lpfc_debugfs_slow_ring_trc(phba,
11246 "ISR Disable ring:"
11247 "pwork:x%x hawork:x%x wait:x%x",
11248 phba->work_ha, work_ha_copy,
11249 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011250 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011251
James Smart858c9f62007-06-17 19:56:39 -050011252 control &=
11253 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011254 writel(control, phba->HCregaddr);
11255 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011256 }
James Smarta58cbd52007-08-02 11:09:43 -040011257 else {
11258 lpfc_debugfs_slow_ring_trc(phba,
11259 "ISR slow ring: pwork:"
11260 "x%x hawork:x%x wait:x%x",
11261 phba->work_ha, work_ha_copy,
11262 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011263 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011264 }
James Smart5b75da22008-12-04 22:39:35 -050011265 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011266 }
11267 }
James Smart5b75da22008-12-04 22:39:35 -050011268 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011269 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011270 if (lpfc_sli_read_hs(phba))
11271 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011272 /*
11273 * Check if there is a deferred error condition
11274 * is active
11275 */
11276 if ((HS_FFER1 & phba->work_hs) &&
11277 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011278 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11279 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011280 phba->hba_flag |= DEFER_ERATT;
11281 /* Clear all interrupt enable conditions */
11282 writel(0, phba->HCregaddr);
11283 readl(phba->HCregaddr);
11284 }
11285 }
11286
James Smart93996272008-08-24 21:50:30 -040011287 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011288 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011289 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011290 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011291 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011292
11293 /* First check out the status word */
11294 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11295 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011296 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011297 /*
11298 * Stray Mailbox Interrupt, mbxCommand <cmd>
11299 * mbxStatus <status>
11300 */
James Smart09372822008-01-11 01:52:54 -050011301 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011302 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011303 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011304 "Interrupt mbxCommand x%x "
11305 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011306 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011307 pmbox->mbxCommand,
11308 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011309 /* clear mailbox attention bit */
11310 work_ha_copy &= ~HA_MBATT;
11311 } else {
James Smart97eab632008-04-07 10:16:05 -040011312 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011313 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011314 phba->last_completion_time = jiffies;
11315 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011316 if (pmb->mbox_cmpl) {
11317 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11318 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011319 if (pmb->out_ext_byte_len &&
11320 pmb->context2)
11321 lpfc_sli_pcimem_bcopy(
11322 phba->mbox_ext,
11323 pmb->context2,
11324 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011325 }
James Smart09372822008-01-11 01:52:54 -050011326 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11327 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11328
11329 lpfc_debugfs_disc_trc(vport,
11330 LPFC_DISC_TRC_MBOX_VPORT,
11331 "MBOX dflt rpi: : "
11332 "status:x%x rpi:x%x",
11333 (uint32_t)pmbox->mbxStatus,
11334 pmbox->un.varWords[0], 0);
11335
11336 if (!pmbox->mbxStatus) {
11337 mp = (struct lpfc_dmabuf *)
11338 (pmb->context1);
11339 ndlp = (struct lpfc_nodelist *)
11340 pmb->context2;
11341
11342 /* Reg_LOGIN of dflt RPI was
11343 * successful. new lets get
11344 * rid of the RPI using the
11345 * same mbox buffer.
11346 */
11347 lpfc_unreg_login(phba,
11348 vport->vpi,
11349 pmbox->un.varWords[0],
11350 pmb);
11351 pmb->mbox_cmpl =
11352 lpfc_mbx_cmpl_dflt_rpi;
11353 pmb->context1 = mp;
11354 pmb->context2 = ndlp;
11355 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011356 rc = lpfc_sli_issue_mbox(phba,
11357 pmb,
11358 MBX_NOWAIT);
11359 if (rc != MBX_BUSY)
11360 lpfc_printf_log(phba,
11361 KERN_ERR,
11362 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011363 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011364 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011365 if (rc != MBX_NOT_FINISHED)
11366 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011367 }
11368 }
James Smart5b75da22008-12-04 22:39:35 -050011369 spin_lock_irqsave(
11370 &phba->pport->work_port_lock,
11371 iflag);
James Smart09372822008-01-11 01:52:54 -050011372 phba->pport->work_port_events &=
11373 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011374 spin_unlock_irqrestore(
11375 &phba->pport->work_port_lock,
11376 iflag);
James Smart09372822008-01-11 01:52:54 -050011377 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011378 }
James Smart97eab632008-04-07 10:16:05 -040011379 } else
James Smart5b75da22008-12-04 22:39:35 -050011380 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011381
James Smart92d7f7b2007-06-17 19:56:38 -050011382 if ((work_ha_copy & HA_MBATT) &&
11383 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011384send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011385 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011386 do {
11387 rc = lpfc_sli_issue_mbox(phba, NULL,
11388 MBX_NOWAIT);
11389 } while (rc == MBX_NOT_FINISHED);
11390 if (rc != MBX_SUCCESS)
11391 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11392 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011393 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011394 }
11395
James Smart5b75da22008-12-04 22:39:35 -050011396 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011397 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011398 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011399 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011400 }
James Smart93996272008-08-24 21:50:30 -040011401 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011402unplug_error:
11403 spin_unlock_irqrestore(&phba->hbalock, iflag);
11404 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011405
James Smart3772a992009-05-22 14:50:54 -040011406} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011407
11408/**
James Smart3772a992009-05-22 14:50:54 -040011409 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011410 * @irq: Interrupt number.
11411 * @dev_id: The device context pointer.
11412 *
11413 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011414 * service routine when device with SLI-3 interface spec is enabled with
11415 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11416 * ring event in the HBA. However, when the device is enabled with either
11417 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11418 * device-level interrupt handler. When the PCI slot is in error recovery
11419 * or the HBA is undergoing initialization, the interrupt handler will not
11420 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11421 * the intrrupt context. This function is called without any lock held.
11422 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011423 *
11424 * This function returns IRQ_HANDLED when interrupt is handled else it
11425 * returns IRQ_NONE.
11426 **/
11427irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011428lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011429{
11430 struct lpfc_hba *phba;
11431 uint32_t ha_copy;
11432 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011433 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011434
11435 /* Get the driver's phba structure from the dev_id and
11436 * assume the HBA is not interrupting.
11437 */
11438 phba = (struct lpfc_hba *) dev_id;
11439
11440 if (unlikely(!phba))
11441 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011442
11443 /*
James Smart93996272008-08-24 21:50:30 -040011444 * Stuff needs to be attented to when this function is invoked as an
11445 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011446 */
James Smart93996272008-08-24 21:50:30 -040011447 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011448 /* Check device state for handling interrupt */
11449 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011450 return IRQ_NONE;
11451 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011452 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11453 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011454 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011455 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011456 /*
11457 * If there is deferred error attention, do not check for
11458 * any interrupt.
11459 */
11460 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011461 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011462 return IRQ_NONE;
11463 }
James Smart93996272008-08-24 21:50:30 -040011464 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11465 phba->HAregaddr);
11466 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011467 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011468 } else
11469 ha_copy = phba->ha_copy;
11470
11471 /*
11472 * Process all events on FCP ring. Take the optimized path for FCP IO.
11473 */
11474 ha_copy &= ~(phba->work_ha_mask);
11475
11476 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011477 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011478 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011479 lpfc_sli_handle_fast_ring_event(phba,
11480 &phba->sli.ring[LPFC_FCP_RING],
11481 status);
James Smarta4bc3372006-12-02 13:34:16 -050011482
11483 if (phba->cfg_multi_ring_support == 2) {
11484 /*
James Smart93996272008-08-24 21:50:30 -040011485 * Process all events on extra ring. Take the optimized path
11486 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011487 */
James Smart93996272008-08-24 21:50:30 -040011488 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011489 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011490 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011491 lpfc_sli_handle_fast_ring_event(phba,
11492 &phba->sli.ring[LPFC_EXTRA_RING],
11493 status);
11494 }
11495 }
dea31012005-04-17 16:05:31 -050011496 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011497} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011498
James Smart93996272008-08-24 21:50:30 -040011499/**
James Smart3772a992009-05-22 14:50:54 -040011500 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011501 * @irq: Interrupt number.
11502 * @dev_id: The device context pointer.
11503 *
James Smart3772a992009-05-22 14:50:54 -040011504 * This function is the HBA device-level interrupt handler to device with
11505 * SLI-3 interface spec, called from the PCI layer when either MSI or
11506 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11507 * requires driver attention. This function invokes the slow-path interrupt
11508 * attention handling function and fast-path interrupt attention handling
11509 * function in turn to process the relevant HBA attention events. This
11510 * function is called without any lock held. It gets the hbalock to access
11511 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011512 *
11513 * This function returns IRQ_HANDLED when interrupt is handled, else it
11514 * returns IRQ_NONE.
11515 **/
11516irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011517lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011518{
11519 struct lpfc_hba *phba;
11520 irqreturn_t sp_irq_rc, fp_irq_rc;
11521 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011522 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011523
11524 /*
11525 * Get the driver's phba structure from the dev_id and
11526 * assume the HBA is not interrupting.
11527 */
11528 phba = (struct lpfc_hba *) dev_id;
11529
11530 if (unlikely(!phba))
11531 return IRQ_NONE;
11532
James Smart3772a992009-05-22 14:50:54 -040011533 /* Check device state for handling interrupt */
11534 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011535 return IRQ_NONE;
11536
11537 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011538 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11539 spin_unlock(&phba->hbalock);
11540 return IRQ_HANDLED;
11541 }
11542
James Smart93996272008-08-24 21:50:30 -040011543 if (unlikely(!phba->ha_copy)) {
11544 spin_unlock(&phba->hbalock);
11545 return IRQ_NONE;
11546 } else if (phba->ha_copy & HA_ERATT) {
11547 if (phba->hba_flag & HBA_ERATT_HANDLED)
11548 /* ERATT polling has handled ERATT */
11549 phba->ha_copy &= ~HA_ERATT;
11550 else
11551 /* Indicate interrupt handler handles ERATT */
11552 phba->hba_flag |= HBA_ERATT_HANDLED;
11553 }
11554
James Smarta257bf92009-04-06 18:48:10 -040011555 /*
11556 * If there is deferred error attention, do not check for any interrupt.
11557 */
11558 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011559 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011560 return IRQ_NONE;
11561 }
11562
James Smart93996272008-08-24 21:50:30 -040011563 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011564 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11565 spin_unlock(&phba->hbalock);
11566 return IRQ_HANDLED;
11567 }
James Smarta747c9c2009-11-18 15:41:10 -050011568 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11569 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11570 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011571 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011572 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011573 readl(phba->HAregaddr); /* flush */
11574 spin_unlock(&phba->hbalock);
11575
11576 /*
11577 * Invokes slow-path host attention interrupt handling as appropriate.
11578 */
11579
11580 /* status of events with mailbox and link attention */
11581 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11582
11583 /* status of events with ELS ring */
11584 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11585 status2 >>= (4*LPFC_ELS_RING);
11586
11587 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011588 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011589 else
11590 sp_irq_rc = IRQ_NONE;
11591
11592 /*
11593 * Invoke fast-path host attention interrupt handling as appropriate.
11594 */
11595
11596 /* status of events with FCP ring */
11597 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11598 status1 >>= (4*LPFC_FCP_RING);
11599
11600 /* status of events with extra ring */
11601 if (phba->cfg_multi_ring_support == 2) {
11602 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11603 status2 >>= (4*LPFC_EXTRA_RING);
11604 } else
11605 status2 = 0;
11606
11607 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011608 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011609 else
11610 fp_irq_rc = IRQ_NONE;
11611
11612 /* Return device-level interrupt handling status */
11613 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011614} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011615
11616/**
11617 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11618 * @phba: pointer to lpfc hba data structure.
11619 *
11620 * This routine is invoked by the worker thread to process all the pending
11621 * SLI4 FCP abort XRI events.
11622 **/
11623void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11624{
11625 struct lpfc_cq_event *cq_event;
11626
11627 /* First, declare the fcp xri abort event has been handled */
11628 spin_lock_irq(&phba->hbalock);
11629 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11630 spin_unlock_irq(&phba->hbalock);
11631 /* Now, handle all the fcp xri abort events */
11632 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11633 /* Get the first event from the head of the event queue */
11634 spin_lock_irq(&phba->hbalock);
11635 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11636 cq_event, struct lpfc_cq_event, list);
11637 spin_unlock_irq(&phba->hbalock);
11638 /* Notify aborted XRI for FCP work queue */
11639 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11640 /* Free the event processed back to the free pool */
11641 lpfc_sli4_cq_event_release(phba, cq_event);
11642 }
11643}
11644
11645/**
11646 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11647 * @phba: pointer to lpfc hba data structure.
11648 *
11649 * This routine is invoked by the worker thread to process all the pending
11650 * SLI4 els abort xri events.
11651 **/
11652void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11653{
11654 struct lpfc_cq_event *cq_event;
11655
11656 /* First, declare the els xri abort event has been handled */
11657 spin_lock_irq(&phba->hbalock);
11658 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11659 spin_unlock_irq(&phba->hbalock);
11660 /* Now, handle all the els xri abort events */
11661 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11662 /* Get the first event from the head of the event queue */
11663 spin_lock_irq(&phba->hbalock);
11664 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11665 cq_event, struct lpfc_cq_event, list);
11666 spin_unlock_irq(&phba->hbalock);
11667 /* Notify aborted XRI for ELS work queue */
11668 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11669 /* Free the event processed back to the free pool */
11670 lpfc_sli4_cq_event_release(phba, cq_event);
11671 }
11672}
11673
James Smart341af102010-01-26 23:07:37 -050011674/**
11675 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11676 * @phba: pointer to lpfc hba data structure
11677 * @pIocbIn: pointer to the rspiocbq
11678 * @pIocbOut: pointer to the cmdiocbq
11679 * @wcqe: pointer to the complete wcqe
11680 *
11681 * This routine transfers the fields of a command iocbq to a response iocbq
11682 * by copying all the IOCB fields from command iocbq and transferring the
11683 * completion status information from the complete wcqe.
11684 **/
James Smart4f774512009-05-22 14:52:35 -040011685static void
James Smart341af102010-01-26 23:07:37 -050011686lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11687 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011688 struct lpfc_iocbq *pIocbOut,
11689 struct lpfc_wcqe_complete *wcqe)
11690{
James Smartaf227412013-10-10 12:23:10 -040011691 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011692 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011693 uint32_t status, max_response;
11694 struct lpfc_dmabuf *dmabuf;
11695 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011696 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11697
11698 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11699 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011700 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011701 status = bf_get(lpfc_wcqe_c_status, wcqe);
11702 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011703 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11704 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11705 pIocbIn->iocb.un.fcpi.fcpi_parm =
11706 pIocbOut->iocb.un.fcpi.fcpi_parm -
11707 wcqe->total_data_placed;
11708 else
11709 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011710 else {
James Smart4f774512009-05-22 14:52:35 -040011711 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011712 switch (pIocbOut->iocb.ulpCommand) {
11713 case CMD_ELS_REQUEST64_CR:
11714 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11715 bpl = (struct ulp_bde64 *)dmabuf->virt;
11716 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11717 max_response = bde.tus.f.bdeSize;
11718 break;
11719 case CMD_GEN_REQUEST64_CR:
11720 max_response = 0;
11721 if (!pIocbOut->context3)
11722 break;
11723 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11724 sizeof(struct ulp_bde64);
11725 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11726 bpl = (struct ulp_bde64 *)dmabuf->virt;
11727 for (i = 0; i < numBdes; i++) {
11728 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11729 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11730 max_response += bde.tus.f.bdeSize;
11731 }
11732 break;
11733 default:
11734 max_response = wcqe->total_data_placed;
11735 break;
11736 }
11737 if (max_response < wcqe->total_data_placed)
11738 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11739 else
11740 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11741 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011742 }
James Smart341af102010-01-26 23:07:37 -050011743
James Smartacd68592012-01-18 16:25:09 -050011744 /* Convert BG errors for completion status */
11745 if (status == CQE_STATUS_DI_ERROR) {
11746 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11747
11748 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11749 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11750 else
11751 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11752
11753 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11754 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11755 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11756 BGS_GUARD_ERR_MASK;
11757 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11758 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11759 BGS_APPTAG_ERR_MASK;
11760 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11761 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11762 BGS_REFTAG_ERR_MASK;
11763
11764 /* Check to see if there was any good data before the error */
11765 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11766 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11767 BGS_HI_WATER_MARK_PRESENT_MASK;
11768 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11769 wcqe->total_data_placed;
11770 }
11771
11772 /*
11773 * Set ALL the error bits to indicate we don't know what
11774 * type of error it is.
11775 */
11776 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11777 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11778 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11779 BGS_GUARD_ERR_MASK);
11780 }
11781
James Smart341af102010-01-26 23:07:37 -050011782 /* Pick up HBA exchange busy condition */
11783 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11784 spin_lock_irqsave(&phba->hbalock, iflags);
11785 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11786 spin_unlock_irqrestore(&phba->hbalock, iflags);
11787 }
James Smart4f774512009-05-22 14:52:35 -040011788}
11789
11790/**
James Smart45ed1192009-10-02 15:17:02 -040011791 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11792 * @phba: Pointer to HBA context object.
11793 * @wcqe: Pointer to work-queue completion queue entry.
11794 *
11795 * This routine handles an ELS work-queue completion event and construct
11796 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11797 * discovery engine to handle.
11798 *
11799 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11800 **/
11801static struct lpfc_iocbq *
11802lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11803 struct lpfc_iocbq *irspiocbq)
11804{
11805 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11806 struct lpfc_iocbq *cmdiocbq;
11807 struct lpfc_wcqe_complete *wcqe;
11808 unsigned long iflags;
11809
11810 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011811 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011812 pring->stats.iocb_event++;
11813 /* Look up the ELS command IOCB and create pseudo response IOCB */
11814 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11815 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070011816 /* Put the iocb back on the txcmplq */
11817 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040011818 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011819
11820 if (unlikely(!cmdiocbq)) {
11821 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11822 "0386 ELS complete with no corresponding "
11823 "cmdiocb: iotag (%d)\n",
11824 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11825 lpfc_sli_release_iocbq(phba, irspiocbq);
11826 return NULL;
11827 }
11828
11829 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011830 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011831
11832 return irspiocbq;
11833}
11834
11835/**
James Smart04c68492009-05-22 14:52:52 -040011836 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11837 * @phba: Pointer to HBA context object.
11838 * @cqe: Pointer to mailbox completion queue entry.
11839 *
11840 * This routine process a mailbox completion queue entry with asynchrous
11841 * event.
11842 *
11843 * Return: true if work posted to worker thread, otherwise false.
11844 **/
11845static bool
11846lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11847{
11848 struct lpfc_cq_event *cq_event;
11849 unsigned long iflags;
11850
11851 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11852 "0392 Async Event: word0:x%x, word1:x%x, "
11853 "word2:x%x, word3:x%x\n", mcqe->word0,
11854 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11855
11856 /* Allocate a new internal CQ_EVENT entry */
11857 cq_event = lpfc_sli4_cq_event_alloc(phba);
11858 if (!cq_event) {
11859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11860 "0394 Failed to allocate CQ_EVENT entry\n");
11861 return false;
11862 }
11863
11864 /* Move the CQE into an asynchronous event entry */
11865 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11866 spin_lock_irqsave(&phba->hbalock, iflags);
11867 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11868 /* Set the async event flag */
11869 phba->hba_flag |= ASYNC_EVENT;
11870 spin_unlock_irqrestore(&phba->hbalock, iflags);
11871
11872 return true;
11873}
11874
11875/**
11876 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11877 * @phba: Pointer to HBA context object.
11878 * @cqe: Pointer to mailbox completion queue entry.
11879 *
11880 * This routine process a mailbox completion queue entry with mailbox
11881 * completion event.
11882 *
11883 * Return: true if work posted to worker thread, otherwise false.
11884 **/
11885static bool
11886lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11887{
11888 uint32_t mcqe_status;
11889 MAILBOX_t *mbox, *pmbox;
11890 struct lpfc_mqe *mqe;
11891 struct lpfc_vport *vport;
11892 struct lpfc_nodelist *ndlp;
11893 struct lpfc_dmabuf *mp;
11894 unsigned long iflags;
11895 LPFC_MBOXQ_t *pmb;
11896 bool workposted = false;
11897 int rc;
11898
11899 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11900 if (!bf_get(lpfc_trailer_completed, mcqe))
11901 goto out_no_mqe_complete;
11902
11903 /* Get the reference to the active mbox command */
11904 spin_lock_irqsave(&phba->hbalock, iflags);
11905 pmb = phba->sli.mbox_active;
11906 if (unlikely(!pmb)) {
11907 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11908 "1832 No pending MBOX command to handle\n");
11909 spin_unlock_irqrestore(&phba->hbalock, iflags);
11910 goto out_no_mqe_complete;
11911 }
11912 spin_unlock_irqrestore(&phba->hbalock, iflags);
11913 mqe = &pmb->u.mqe;
11914 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11915 mbox = phba->mbox;
11916 vport = pmb->vport;
11917
11918 /* Reset heartbeat timer */
11919 phba->last_completion_time = jiffies;
11920 del_timer(&phba->sli.mbox_tmo);
11921
11922 /* Move mbox data to caller's mailbox region, do endian swapping */
11923 if (pmb->mbox_cmpl && mbox)
11924 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011925
James Smart73d91e52011-10-10 21:32:10 -040011926 /*
11927 * For mcqe errors, conditionally move a modified error code to
11928 * the mbox so that the error will not be missed.
11929 */
11930 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11931 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11932 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11933 bf_set(lpfc_mqe_status, mqe,
11934 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11935 }
James Smart04c68492009-05-22 14:52:52 -040011936 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11937 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11938 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11939 "MBOX dflt rpi: status:x%x rpi:x%x",
11940 mcqe_status,
11941 pmbox->un.varWords[0], 0);
11942 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11943 mp = (struct lpfc_dmabuf *)(pmb->context1);
11944 ndlp = (struct lpfc_nodelist *)pmb->context2;
11945 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11946 * RID of the PPI using the same mbox buffer.
11947 */
11948 lpfc_unreg_login(phba, vport->vpi,
11949 pmbox->un.varWords[0], pmb);
11950 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11951 pmb->context1 = mp;
11952 pmb->context2 = ndlp;
11953 pmb->vport = vport;
11954 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11955 if (rc != MBX_BUSY)
11956 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11957 LOG_SLI, "0385 rc should "
11958 "have been MBX_BUSY\n");
11959 if (rc != MBX_NOT_FINISHED)
11960 goto send_current_mbox;
11961 }
11962 }
11963 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11964 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11965 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11966
11967 /* There is mailbox completion work to do */
11968 spin_lock_irqsave(&phba->hbalock, iflags);
11969 __lpfc_mbox_cmpl_put(phba, pmb);
11970 phba->work_ha |= HA_MBATT;
11971 spin_unlock_irqrestore(&phba->hbalock, iflags);
11972 workposted = true;
11973
11974send_current_mbox:
11975 spin_lock_irqsave(&phba->hbalock, iflags);
11976 /* Release the mailbox command posting token */
11977 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11978 /* Setting active mailbox pointer need to be in sync to flag clear */
11979 phba->sli.mbox_active = NULL;
11980 spin_unlock_irqrestore(&phba->hbalock, iflags);
11981 /* Wake up worker thread to post the next pending mailbox command */
11982 lpfc_worker_wake_up(phba);
11983out_no_mqe_complete:
11984 if (bf_get(lpfc_trailer_consumed, mcqe))
11985 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11986 return workposted;
11987}
11988
11989/**
11990 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11991 * @phba: Pointer to HBA context object.
11992 * @cqe: Pointer to mailbox completion queue entry.
11993 *
11994 * This routine process a mailbox completion queue entry, it invokes the
11995 * proper mailbox complete handling or asynchrous event handling routine
11996 * according to the MCQE's async bit.
11997 *
11998 * Return: true if work posted to worker thread, otherwise false.
11999 **/
12000static bool
12001lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12002{
12003 struct lpfc_mcqe mcqe;
12004 bool workposted;
12005
12006 /* Copy the mailbox MCQE and convert endian order as needed */
12007 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12008
12009 /* Invoke the proper event handling routine */
12010 if (!bf_get(lpfc_trailer_async, &mcqe))
12011 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12012 else
12013 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12014 return workposted;
12015}
12016
12017/**
James Smart4f774512009-05-22 14:52:35 -040012018 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12019 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012020 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012021 * @wcqe: Pointer to work-queue completion queue entry.
12022 *
12023 * This routine handles an ELS work-queue completion event.
12024 *
12025 * Return: true if work posted to worker thread, otherwise false.
12026 **/
12027static bool
James Smart2a76a282012-08-03 12:35:54 -040012028lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012029 struct lpfc_wcqe_complete *wcqe)
12030{
James Smart4f774512009-05-22 14:52:35 -040012031 struct lpfc_iocbq *irspiocbq;
12032 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012033 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012034 int txq_cnt = 0;
12035 int txcmplq_cnt = 0;
12036 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012037
James Smart45ed1192009-10-02 15:17:02 -040012038 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012039 irspiocbq = lpfc_sli_get_iocbq(phba);
12040 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012041 if (!list_empty(&pring->txq))
12042 txq_cnt++;
12043 if (!list_empty(&pring->txcmplq))
12044 txcmplq_cnt++;
12045 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12046 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012047 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012048 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12049 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012050 txq_cnt, phba->iocb_cnt,
12051 fcp_txcmplq_cnt,
12052 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012053 return false;
James Smart4f774512009-05-22 14:52:35 -040012054 }
James Smart4f774512009-05-22 14:52:35 -040012055
James Smart45ed1192009-10-02 15:17:02 -040012056 /* Save off the slow-path queue event for work thread to process */
12057 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012058 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012059 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012060 &phba->sli4_hba.sp_queue_event);
12061 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012062 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012063
James Smart45ed1192009-10-02 15:17:02 -040012064 return true;
James Smart4f774512009-05-22 14:52:35 -040012065}
12066
12067/**
12068 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12069 * @phba: Pointer to HBA context object.
12070 * @wcqe: Pointer to work-queue completion queue entry.
12071 *
12072 * This routine handles slow-path WQ entry comsumed event by invoking the
12073 * proper WQ release routine to the slow-path WQ.
12074 **/
12075static void
12076lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12077 struct lpfc_wcqe_release *wcqe)
12078{
James Smart2e90f4b2011-12-13 13:22:37 -050012079 /* sanity check on queue memory */
12080 if (unlikely(!phba->sli4_hba.els_wq))
12081 return;
James Smart4f774512009-05-22 14:52:35 -040012082 /* Check for the slow-path ELS work queue */
12083 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12084 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12085 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12086 else
12087 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12088 "2579 Slow-path wqe consume event carries "
12089 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12090 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12091 phba->sli4_hba.els_wq->queue_id);
12092}
12093
12094/**
12095 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12096 * @phba: Pointer to HBA context object.
12097 * @cq: Pointer to a WQ completion queue.
12098 * @wcqe: Pointer to work-queue completion queue entry.
12099 *
12100 * This routine handles an XRI abort event.
12101 *
12102 * Return: true if work posted to worker thread, otherwise false.
12103 **/
12104static bool
12105lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12106 struct lpfc_queue *cq,
12107 struct sli4_wcqe_xri_aborted *wcqe)
12108{
12109 bool workposted = false;
12110 struct lpfc_cq_event *cq_event;
12111 unsigned long iflags;
12112
12113 /* Allocate a new internal CQ_EVENT entry */
12114 cq_event = lpfc_sli4_cq_event_alloc(phba);
12115 if (!cq_event) {
12116 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12117 "0602 Failed to allocate CQ_EVENT entry\n");
12118 return false;
12119 }
12120
12121 /* Move the CQE into the proper xri abort event list */
12122 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12123 switch (cq->subtype) {
12124 case LPFC_FCP:
12125 spin_lock_irqsave(&phba->hbalock, iflags);
12126 list_add_tail(&cq_event->list,
12127 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12128 /* Set the fcp xri abort event flag */
12129 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12130 spin_unlock_irqrestore(&phba->hbalock, iflags);
12131 workposted = true;
12132 break;
12133 case LPFC_ELS:
12134 spin_lock_irqsave(&phba->hbalock, iflags);
12135 list_add_tail(&cq_event->list,
12136 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12137 /* Set the els xri abort event flag */
12138 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12139 spin_unlock_irqrestore(&phba->hbalock, iflags);
12140 workposted = true;
12141 break;
12142 default:
12143 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12144 "0603 Invalid work queue CQE subtype (x%x)\n",
12145 cq->subtype);
12146 workposted = false;
12147 break;
12148 }
12149 return workposted;
12150}
12151
12152/**
James Smart4d9ab992009-10-02 15:16:39 -040012153 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012154 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012155 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012156 *
James Smart4d9ab992009-10-02 15:16:39 -040012157 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012158 *
12159 * Return: true if work posted to worker thread, otherwise false.
12160 **/
12161static bool
James Smart4d9ab992009-10-02 15:16:39 -040012162lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12163{
12164 bool workposted = false;
12165 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12166 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12167 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012168 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012169 unsigned long iflags;
12170
James Smart2e90f4b2011-12-13 13:22:37 -050012171 /* sanity check on queue memory */
12172 if (unlikely(!hrq) || unlikely(!drq))
12173 return workposted;
12174
James Smart7851fe22011-07-22 18:36:52 -040012175 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12176 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12177 else
12178 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12179 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012180 goto out;
12181
12182 status = bf_get(lpfc_rcqe_status, rcqe);
12183 switch (status) {
12184 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12185 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12186 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012187 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012188 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012189 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012190 spin_lock_irqsave(&phba->hbalock, iflags);
12191 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12192 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012193 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012194 spin_unlock_irqrestore(&phba->hbalock, iflags);
12195 goto out;
12196 }
James Smartb84daac2012-08-03 12:35:13 -040012197 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012198 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12199 /* save off the frame for the word thread to process */
12200 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012201 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012202 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012203 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012204 spin_unlock_irqrestore(&phba->hbalock, iflags);
12205 workposted = true;
12206 break;
12207 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12208 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012209 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012210 /* Post more buffers if possible */
12211 spin_lock_irqsave(&phba->hbalock, iflags);
12212 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12213 spin_unlock_irqrestore(&phba->hbalock, iflags);
12214 workposted = true;
12215 break;
12216 }
12217out:
12218 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012219}
12220
12221/**
12222 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12223 * @phba: Pointer to HBA context object.
12224 * @cq: Pointer to the completion queue.
12225 * @wcqe: Pointer to a completion queue entry.
12226 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012227 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012228 * entry.
12229 *
12230 * Return: true if work posted to worker thread, otherwise false.
12231 **/
12232static bool
12233lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012234 struct lpfc_cqe *cqe)
12235{
James Smart45ed1192009-10-02 15:17:02 -040012236 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012237 bool workposted = false;
12238
12239 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012240 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012241
12242 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012243 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012244 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012245 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012246 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012247 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012248 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012249 break;
12250 case CQE_CODE_RELEASE_WQE:
12251 /* Process the WQ release event */
12252 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012253 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012254 break;
12255 case CQE_CODE_XRI_ABORTED:
12256 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012257 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012258 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012259 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012260 break;
James Smart4d9ab992009-10-02 15:16:39 -040012261 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012262 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012263 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012264 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012265 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012266 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012267 break;
James Smart4f774512009-05-22 14:52:35 -040012268 default:
12269 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12270 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012271 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012272 break;
12273 }
12274 return workposted;
12275}
12276
12277/**
James Smart4f774512009-05-22 14:52:35 -040012278 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12279 * @phba: Pointer to HBA context object.
12280 * @eqe: Pointer to fast-path event queue entry.
12281 *
12282 * This routine process a event queue entry from the slow-path event queue.
12283 * It will check the MajorCode and MinorCode to determine this is for a
12284 * completion event on a completion queue, if not, an error shall be logged
12285 * and just return. Otherwise, it will get to the corresponding completion
12286 * queue and process all the entries on that completion queue, rearm the
12287 * completion queue, and then return.
12288 *
12289 **/
12290static void
James Smart67d12732012-08-03 12:36:13 -040012291lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12292 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012293{
James Smart67d12732012-08-03 12:36:13 -040012294 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012295 struct lpfc_cqe *cqe;
12296 bool workposted = false;
12297 int ecount = 0;
12298 uint16_t cqid;
12299
James Smart4f774512009-05-22 14:52:35 -040012300 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012301 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012302
James Smart4f774512009-05-22 14:52:35 -040012303 list_for_each_entry(childq, &speq->child_list, list) {
12304 if (childq->queue_id == cqid) {
12305 cq = childq;
12306 break;
12307 }
12308 }
12309 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012310 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12312 "0365 Slow-path CQ identifier "
12313 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012314 return;
12315 }
12316
12317 /* Process all the entries to the CQ */
12318 switch (cq->type) {
12319 case LPFC_MCQ:
12320 while ((cqe = lpfc_sli4_cq_get(cq))) {
12321 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012322 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012323 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012324 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012325 }
12326 break;
12327 case LPFC_WCQ:
12328 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012329 if (cq->subtype == LPFC_FCP)
12330 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12331 cqe);
12332 else
12333 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12334 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012335 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012336 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12337 }
James Smartb84daac2012-08-03 12:35:13 -040012338
12339 /* Track the max number of CQEs processed in 1 EQ */
12340 if (ecount > cq->CQ_max_cqe)
12341 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012342 break;
12343 default:
12344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12345 "0370 Invalid completion queue type (%d)\n",
12346 cq->type);
12347 return;
12348 }
12349
12350 /* Catch the no cq entry condition, log an error */
12351 if (unlikely(ecount == 0))
12352 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12353 "0371 No entry from the CQ: identifier "
12354 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12355
12356 /* In any case, flash and re-arm the RCQ */
12357 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12358
12359 /* wake up worker thread if there are works to be done */
12360 if (workposted)
12361 lpfc_worker_wake_up(phba);
12362}
12363
12364/**
12365 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012366 * @phba: Pointer to HBA context object.
12367 * @cq: Pointer to associated CQ
12368 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012369 *
12370 * This routine process a fast-path work queue completion entry from fast-path
12371 * event queue for FCP command response completion.
12372 **/
12373static void
James Smart2a76a282012-08-03 12:35:54 -040012374lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012375 struct lpfc_wcqe_complete *wcqe)
12376{
James Smart2a76a282012-08-03 12:35:54 -040012377 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012378 struct lpfc_iocbq *cmdiocbq;
12379 struct lpfc_iocbq irspiocbq;
12380 unsigned long iflags;
12381
James Smart4f774512009-05-22 14:52:35 -040012382 /* Check for response status */
12383 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12384 /* If resource errors reported from HBA, reduce queue
12385 * depth of the SCSI device.
12386 */
James Smarte3d2b802012-08-14 14:25:43 -040012387 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12388 IOSTAT_LOCAL_REJECT)) &&
12389 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12390 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012391 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012392
James Smart4f774512009-05-22 14:52:35 -040012393 /* Log the error status */
12394 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12395 "0373 FCP complete error: status=x%x, "
12396 "hw_status=x%x, total_data_specified=%d, "
12397 "parameter=x%x, word3=x%x\n",
12398 bf_get(lpfc_wcqe_c_status, wcqe),
12399 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12400 wcqe->total_data_placed, wcqe->parameter,
12401 wcqe->word3);
12402 }
12403
12404 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012405 spin_lock_irqsave(&pring->ring_lock, iflags);
12406 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012407 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12408 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012409 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012410 if (unlikely(!cmdiocbq)) {
12411 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12412 "0374 FCP complete with no corresponding "
12413 "cmdiocb: iotag (%d)\n",
12414 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12415 return;
12416 }
12417 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12418 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12419 "0375 FCP cmdiocb not callback function "
12420 "iotag: (%d)\n",
12421 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12422 return;
12423 }
12424
12425 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012426 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012427
James Smart0f65ff62010-02-26 14:14:23 -050012428 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12429 spin_lock_irqsave(&phba->hbalock, iflags);
12430 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12431 spin_unlock_irqrestore(&phba->hbalock, iflags);
12432 }
12433
James Smart4f774512009-05-22 14:52:35 -040012434 /* Pass the cmd_iocb and the rsp state to the upper layer */
12435 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12436}
12437
12438/**
12439 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12440 * @phba: Pointer to HBA context object.
12441 * @cq: Pointer to completion queue.
12442 * @wcqe: Pointer to work-queue completion queue entry.
12443 *
12444 * This routine handles an fast-path WQ entry comsumed event by invoking the
12445 * proper WQ release routine to the slow-path WQ.
12446 **/
12447static void
12448lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12449 struct lpfc_wcqe_release *wcqe)
12450{
12451 struct lpfc_queue *childwq;
12452 bool wqid_matched = false;
12453 uint16_t fcp_wqid;
12454
12455 /* Check for fast-path FCP work queue release */
12456 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12457 list_for_each_entry(childwq, &cq->child_list, list) {
12458 if (childwq->queue_id == fcp_wqid) {
12459 lpfc_sli4_wq_release(childwq,
12460 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12461 wqid_matched = true;
12462 break;
12463 }
12464 }
12465 /* Report warning log message if no match found */
12466 if (wqid_matched != true)
12467 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12468 "2580 Fast-path wqe consume event carries "
12469 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12470}
12471
12472/**
12473 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12474 * @cq: Pointer to the completion queue.
12475 * @eqe: Pointer to fast-path completion queue entry.
12476 *
12477 * This routine process a fast-path work queue completion entry from fast-path
12478 * event queue for FCP command response completion.
12479 **/
12480static int
12481lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12482 struct lpfc_cqe *cqe)
12483{
12484 struct lpfc_wcqe_release wcqe;
12485 bool workposted = false;
12486
12487 /* Copy the work queue CQE and convert endian order if needed */
12488 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12489
12490 /* Check and process for different type of WCQE and dispatch */
12491 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12492 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012493 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012494 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012495 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012496 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012497 (struct lpfc_wcqe_complete *)&wcqe);
12498 break;
12499 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012500 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012501 /* Process the WQ release event */
12502 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12503 (struct lpfc_wcqe_release *)&wcqe);
12504 break;
12505 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012506 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012507 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012508 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012509 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12510 (struct sli4_wcqe_xri_aborted *)&wcqe);
12511 break;
12512 default:
12513 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12514 "0144 Not a valid WCQE code: x%x\n",
12515 bf_get(lpfc_wcqe_c_code, &wcqe));
12516 break;
12517 }
12518 return workposted;
12519}
12520
12521/**
James Smart67d12732012-08-03 12:36:13 -040012522 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012523 * @phba: Pointer to HBA context object.
12524 * @eqe: Pointer to fast-path event queue entry.
12525 *
12526 * This routine process a event queue entry from the fast-path event queue.
12527 * It will check the MajorCode and MinorCode to determine this is for a
12528 * completion event on a completion queue, if not, an error shall be logged
12529 * and just return. Otherwise, it will get to the corresponding completion
12530 * queue and process all the entries on the completion queue, rearm the
12531 * completion queue, and then return.
12532 **/
12533static void
James Smart67d12732012-08-03 12:36:13 -040012534lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12535 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012536{
12537 struct lpfc_queue *cq;
12538 struct lpfc_cqe *cqe;
12539 bool workposted = false;
12540 uint16_t cqid;
12541 int ecount = 0;
12542
James Smartcb5172e2010-03-15 11:25:07 -040012543 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012545 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012546 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012547 bf_get_le32(lpfc_eqe_major_code, eqe),
12548 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012549 return;
12550 }
12551
James Smart67d12732012-08-03 12:36:13 -040012552 /* Get the reference to the corresponding CQ */
12553 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12554
12555 /* Check if this is a Slow path event */
12556 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12557 lpfc_sli4_sp_handle_eqe(phba, eqe,
12558 phba->sli4_hba.hba_eq[qidx]);
12559 return;
12560 }
12561
James Smart2e90f4b2011-12-13 13:22:37 -050012562 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12563 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12564 "3146 Fast-path completion queues "
12565 "does not exist\n");
12566 return;
12567 }
James Smart67d12732012-08-03 12:36:13 -040012568 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012569 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012570 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12572 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012573 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012574 return;
12575 }
12576
James Smart4f774512009-05-22 14:52:35 -040012577 if (unlikely(cqid != cq->queue_id)) {
12578 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12579 "0368 Miss-matched fast-path completion "
12580 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12581 cqid, cq->queue_id);
12582 return;
12583 }
12584
12585 /* Process all the entries to the CQ */
12586 while ((cqe = lpfc_sli4_cq_get(cq))) {
12587 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012588 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012589 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12590 }
12591
James Smartb84daac2012-08-03 12:35:13 -040012592 /* Track the max number of CQEs processed in 1 EQ */
12593 if (ecount > cq->CQ_max_cqe)
12594 cq->CQ_max_cqe = ecount;
12595
James Smart4f774512009-05-22 14:52:35 -040012596 /* Catch the no cq entry condition */
12597 if (unlikely(ecount == 0))
12598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12599 "0369 No entry from fast-path completion "
12600 "queue fcpcqid=%d\n", cq->queue_id);
12601
12602 /* In any case, flash and re-arm the CQ */
12603 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12604
12605 /* wake up worker thread if there are works to be done */
12606 if (workposted)
12607 lpfc_worker_wake_up(phba);
12608}
12609
12610static void
12611lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12612{
12613 struct lpfc_eqe *eqe;
12614
12615 /* walk all the EQ entries and drop on the floor */
12616 while ((eqe = lpfc_sli4_eq_get(eq)))
12617 ;
12618
12619 /* Clear and re-arm the EQ */
12620 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12621}
12622
James Smart1ba981f2014-02-20 09:56:45 -050012623
12624/**
12625 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12626 * entry
12627 * @phba: Pointer to HBA context object.
12628 * @eqe: Pointer to fast-path event queue entry.
12629 *
12630 * This routine process a event queue entry from the Flash Optimized Fabric
12631 * event queue. It will check the MajorCode and MinorCode to determine this
12632 * is for a completion event on a completion queue, if not, an error shall be
12633 * logged and just return. Otherwise, it will get to the corresponding
12634 * completion queue and process all the entries on the completion queue, rearm
12635 * the completion queue, and then return.
12636 **/
12637static void
12638lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12639{
12640 struct lpfc_queue *cq;
12641 struct lpfc_cqe *cqe;
12642 bool workposted = false;
12643 uint16_t cqid;
12644 int ecount = 0;
12645
12646 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12647 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12648 "9147 Not a valid completion "
12649 "event: majorcode=x%x, minorcode=x%x\n",
12650 bf_get_le32(lpfc_eqe_major_code, eqe),
12651 bf_get_le32(lpfc_eqe_minor_code, eqe));
12652 return;
12653 }
12654
12655 /* Get the reference to the corresponding CQ */
12656 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12657
12658 /* Next check for OAS */
12659 cq = phba->sli4_hba.oas_cq;
12660 if (unlikely(!cq)) {
12661 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12663 "9148 OAS completion queue "
12664 "does not exist\n");
12665 return;
12666 }
12667
12668 if (unlikely(cqid != cq->queue_id)) {
12669 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12670 "9149 Miss-matched fast-path compl "
12671 "queue id: eqcqid=%d, fcpcqid=%d\n",
12672 cqid, cq->queue_id);
12673 return;
12674 }
12675
12676 /* Process all the entries to the OAS CQ */
12677 while ((cqe = lpfc_sli4_cq_get(cq))) {
12678 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12679 if (!(++ecount % cq->entry_repost))
12680 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12681 }
12682
12683 /* Track the max number of CQEs processed in 1 EQ */
12684 if (ecount > cq->CQ_max_cqe)
12685 cq->CQ_max_cqe = ecount;
12686
12687 /* Catch the no cq entry condition */
12688 if (unlikely(ecount == 0))
12689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12690 "9153 No entry from fast-path completion "
12691 "queue fcpcqid=%d\n", cq->queue_id);
12692
12693 /* In any case, flash and re-arm the CQ */
12694 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12695
12696 /* wake up worker thread if there are works to be done */
12697 if (workposted)
12698 lpfc_worker_wake_up(phba);
12699}
12700
12701/**
12702 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12703 * @irq: Interrupt number.
12704 * @dev_id: The device context pointer.
12705 *
12706 * This function is directly called from the PCI layer as an interrupt
12707 * service routine when device with SLI-4 interface spec is enabled with
12708 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12709 * IOCB ring event in the HBA. However, when the device is enabled with either
12710 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12711 * device-level interrupt handler. When the PCI slot is in error recovery
12712 * or the HBA is undergoing initialization, the interrupt handler will not
12713 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12714 * the intrrupt context. This function is called without any lock held.
12715 * It gets the hbalock to access and update SLI data structures. Note that,
12716 * the EQ to CQ are one-to-one map such that the EQ index is
12717 * equal to that of CQ index.
12718 *
12719 * This function returns IRQ_HANDLED when interrupt is handled else it
12720 * returns IRQ_NONE.
12721 **/
12722irqreturn_t
12723lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12724{
12725 struct lpfc_hba *phba;
12726 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12727 struct lpfc_queue *eq;
12728 struct lpfc_eqe *eqe;
12729 unsigned long iflag;
12730 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012731
12732 /* Get the driver's phba structure from the dev_id */
12733 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12734 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012735
12736 if (unlikely(!phba))
12737 return IRQ_NONE;
12738
12739 /* Get to the EQ struct associated with this vector */
12740 eq = phba->sli4_hba.fof_eq;
12741 if (unlikely(!eq))
12742 return IRQ_NONE;
12743
12744 /* Check device state for handling interrupt */
12745 if (unlikely(lpfc_intr_state_check(phba))) {
12746 eq->EQ_badstate++;
12747 /* Check again for link_state with lock held */
12748 spin_lock_irqsave(&phba->hbalock, iflag);
12749 if (phba->link_state < LPFC_LINK_DOWN)
12750 /* Flush, clear interrupt, and rearm the EQ */
12751 lpfc_sli4_eq_flush(phba, eq);
12752 spin_unlock_irqrestore(&phba->hbalock, iflag);
12753 return IRQ_NONE;
12754 }
12755
12756 /*
12757 * Process all the event on FCP fast-path EQ
12758 */
12759 while ((eqe = lpfc_sli4_eq_get(eq))) {
12760 lpfc_sli4_fof_handle_eqe(phba, eqe);
12761 if (!(++ecount % eq->entry_repost))
12762 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12763 eq->EQ_processed++;
12764 }
12765
12766 /* Track the max number of EQEs processed in 1 intr */
12767 if (ecount > eq->EQ_max_eqe)
12768 eq->EQ_max_eqe = ecount;
12769
12770
12771 if (unlikely(ecount == 0)) {
12772 eq->EQ_no_entry++;
12773
12774 if (phba->intr_type == MSIX)
12775 /* MSI-X treated interrupt served as no EQ share INT */
12776 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12777 "9145 MSI-X interrupt with no EQE\n");
12778 else {
12779 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12780 "9146 ISR interrupt with no EQE\n");
12781 /* Non MSI-X treated on interrupt as EQ share INT */
12782 return IRQ_NONE;
12783 }
12784 }
12785 /* Always clear and re-arm the fast-path EQ */
12786 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12787 return IRQ_HANDLED;
12788}
12789
James Smart4f774512009-05-22 14:52:35 -040012790/**
James Smart67d12732012-08-03 12:36:13 -040012791 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012792 * @irq: Interrupt number.
12793 * @dev_id: The device context pointer.
12794 *
12795 * This function is directly called from the PCI layer as an interrupt
12796 * service routine when device with SLI-4 interface spec is enabled with
12797 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12798 * ring event in the HBA. However, when the device is enabled with either
12799 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12800 * device-level interrupt handler. When the PCI slot is in error recovery
12801 * or the HBA is undergoing initialization, the interrupt handler will not
12802 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12803 * the intrrupt context. This function is called without any lock held.
12804 * It gets the hbalock to access and update SLI data structures. Note that,
12805 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12806 * equal to that of FCP CQ index.
12807 *
James Smart67d12732012-08-03 12:36:13 -040012808 * The link attention and ELS ring attention events are handled
12809 * by the worker thread. The interrupt handler signals the worker thread
12810 * and returns for these events. This function is called without any lock
12811 * held. It gets the hbalock to access and update SLI data structures.
12812 *
James Smart4f774512009-05-22 14:52:35 -040012813 * This function returns IRQ_HANDLED when interrupt is handled else it
12814 * returns IRQ_NONE.
12815 **/
12816irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012817lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012818{
12819 struct lpfc_hba *phba;
12820 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12821 struct lpfc_queue *fpeq;
12822 struct lpfc_eqe *eqe;
12823 unsigned long iflag;
12824 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012825 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012826
12827 /* Get the driver's phba structure from the dev_id */
12828 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12829 phba = fcp_eq_hdl->phba;
12830 fcp_eqidx = fcp_eq_hdl->idx;
12831
12832 if (unlikely(!phba))
12833 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012834 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012835 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012836
12837 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012838 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012839 if (unlikely(!fpeq))
12840 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012841
James Smartba20c852012-08-03 12:36:52 -040012842 if (lpfc_fcp_look_ahead) {
12843 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12844 lpfc_sli4_eq_clr_intr(fpeq);
12845 else {
12846 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12847 return IRQ_NONE;
12848 }
12849 }
12850
James Smart4f774512009-05-22 14:52:35 -040012851 /* Check device state for handling interrupt */
12852 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012853 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012854 /* Check again for link_state with lock held */
12855 spin_lock_irqsave(&phba->hbalock, iflag);
12856 if (phba->link_state < LPFC_LINK_DOWN)
12857 /* Flush, clear interrupt, and rearm the EQ */
12858 lpfc_sli4_eq_flush(phba, fpeq);
12859 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012860 if (lpfc_fcp_look_ahead)
12861 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012862 return IRQ_NONE;
12863 }
12864
12865 /*
12866 * Process all the event on FCP fast-path EQ
12867 */
12868 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012869 if (eqe == NULL)
12870 break;
12871
James Smart67d12732012-08-03 12:36:13 -040012872 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012873 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012874 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012875 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012876 }
12877
James Smartb84daac2012-08-03 12:35:13 -040012878 /* Track the max number of EQEs processed in 1 intr */
12879 if (ecount > fpeq->EQ_max_eqe)
12880 fpeq->EQ_max_eqe = ecount;
12881
James Smart4f774512009-05-22 14:52:35 -040012882 /* Always clear and re-arm the fast-path EQ */
12883 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12884
12885 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012886 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012887
12888 if (lpfc_fcp_look_ahead) {
12889 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12890 return IRQ_NONE;
12891 }
12892
James Smart4f774512009-05-22 14:52:35 -040012893 if (phba->intr_type == MSIX)
12894 /* MSI-X treated interrupt served as no EQ share INT */
12895 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12896 "0358 MSI-X interrupt with no EQE\n");
12897 else
12898 /* Non MSI-X treated on interrupt as EQ share INT */
12899 return IRQ_NONE;
12900 }
12901
James Smartba20c852012-08-03 12:36:52 -040012902 if (lpfc_fcp_look_ahead)
12903 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012904 return IRQ_HANDLED;
12905} /* lpfc_sli4_fp_intr_handler */
12906
12907/**
12908 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12909 * @irq: Interrupt number.
12910 * @dev_id: The device context pointer.
12911 *
12912 * This function is the device-level interrupt handler to device with SLI-4
12913 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12914 * interrupt mode is enabled and there is an event in the HBA which requires
12915 * driver attention. This function invokes the slow-path interrupt attention
12916 * handling function and fast-path interrupt attention handling function in
12917 * turn to process the relevant HBA attention events. This function is called
12918 * without any lock held. It gets the hbalock to access and update SLI data
12919 * structures.
12920 *
12921 * This function returns IRQ_HANDLED when interrupt is handled, else it
12922 * returns IRQ_NONE.
12923 **/
12924irqreturn_t
12925lpfc_sli4_intr_handler(int irq, void *dev_id)
12926{
12927 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012928 irqreturn_t hba_irq_rc;
12929 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012930 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012931
12932 /* Get the driver's phba structure from the dev_id */
12933 phba = (struct lpfc_hba *)dev_id;
12934
12935 if (unlikely(!phba))
12936 return IRQ_NONE;
12937
12938 /*
James Smart4f774512009-05-22 14:52:35 -040012939 * Invoke fast-path host attention interrupt handling as appropriate.
12940 */
James Smart67d12732012-08-03 12:36:13 -040012941 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12942 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012943 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012944 if (hba_irq_rc == IRQ_HANDLED)
12945 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012946 }
12947
James Smart1ba981f2014-02-20 09:56:45 -050012948 if (phba->cfg_fof) {
12949 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12950 &phba->sli4_hba.fcp_eq_hdl[0]);
12951 if (hba_irq_rc == IRQ_HANDLED)
12952 hba_handled |= true;
12953 }
12954
James Smart67d12732012-08-03 12:36:13 -040012955 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012956} /* lpfc_sli4_intr_handler */
12957
12958/**
12959 * lpfc_sli4_queue_free - free a queue structure and associated memory
12960 * @queue: The queue structure to free.
12961 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012962 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012963 * the host resident queue. This function must be called after destroying the
12964 * queue on the HBA.
12965 **/
12966void
12967lpfc_sli4_queue_free(struct lpfc_queue *queue)
12968{
12969 struct lpfc_dmabuf *dmabuf;
12970
12971 if (!queue)
12972 return;
12973
12974 while (!list_empty(&queue->page_list)) {
12975 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12976 list);
James Smart49198b32010-04-06 15:04:33 -040012977 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012978 dmabuf->virt, dmabuf->phys);
12979 kfree(dmabuf);
12980 }
12981 kfree(queue);
12982 return;
12983}
12984
12985/**
12986 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12987 * @phba: The HBA that this queue is being created on.
12988 * @entry_size: The size of each queue entry for this queue.
12989 * @entry count: The number of entries that this queue will handle.
12990 *
12991 * This function allocates a queue structure and the DMAable memory used for
12992 * the host resident queue. This function must be called before creating the
12993 * queue on the HBA.
12994 **/
12995struct lpfc_queue *
12996lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12997 uint32_t entry_count)
12998{
12999 struct lpfc_queue *queue;
13000 struct lpfc_dmabuf *dmabuf;
13001 int x, total_qe_count;
13002 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013003 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013004
James Smartcb5172e2010-03-15 11:25:07 -040013005 if (!phba->sli4_hba.pc_sli4_params.supported)
13006 hw_page_size = SLI4_PAGE_SIZE;
13007
James Smart4f774512009-05-22 14:52:35 -040013008 queue = kzalloc(sizeof(struct lpfc_queue) +
13009 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13010 if (!queue)
13011 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013012 queue->page_count = (ALIGN(entry_size * entry_count,
13013 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040013014 INIT_LIST_HEAD(&queue->list);
13015 INIT_LIST_HEAD(&queue->page_list);
13016 INIT_LIST_HEAD(&queue->child_list);
13017 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13018 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13019 if (!dmabuf)
13020 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013021 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13022 hw_page_size, &dmabuf->phys,
13023 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013024 if (!dmabuf->virt) {
13025 kfree(dmabuf);
13026 goto out_fail;
13027 }
13028 dmabuf->buffer_tag = x;
13029 list_add_tail(&dmabuf->list, &queue->page_list);
13030 /* initialize queue's entry array */
13031 dma_pointer = dmabuf->virt;
13032 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013033 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013034 total_qe_count++, dma_pointer += entry_size) {
13035 queue->qe[total_qe_count].address = dma_pointer;
13036 }
13037 }
13038 queue->entry_size = entry_size;
13039 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013040
13041 /*
13042 * entry_repost is calculated based on the number of entries in the
13043 * queue. This works out except for RQs. If buffers are NOT initially
13044 * posted for every RQE, entry_repost should be adjusted accordingly.
13045 */
13046 queue->entry_repost = (entry_count >> 3);
13047 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13048 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013049 queue->phba = phba;
13050
13051 return queue;
13052out_fail:
13053 lpfc_sli4_queue_free(queue);
13054 return NULL;
13055}
13056
13057/**
James Smart962bc512013-01-03 15:44:00 -050013058 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13059 * @phba: HBA structure that indicates port to create a queue on.
13060 * @pci_barset: PCI BAR set flag.
13061 *
13062 * This function shall perform iomap of the specified PCI BAR address to host
13063 * memory address if not already done so and return it. The returned host
13064 * memory address can be NULL.
13065 */
13066static void __iomem *
13067lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13068{
James Smart962bc512013-01-03 15:44:00 -050013069 if (!phba->pcidev)
13070 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013071
13072 switch (pci_barset) {
13073 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013074 return phba->pci_bar0_memmap_p;
13075 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013076 return phba->pci_bar2_memmap_p;
13077 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013078 return phba->pci_bar4_memmap_p;
13079 default:
13080 break;
13081 }
13082 return NULL;
13083}
13084
13085/**
James Smart173edbb2012-06-12 13:54:50 -040013086 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13087 * @phba: HBA structure that indicates port to create a queue on.
13088 * @startq: The starting FCP EQ to modify
13089 *
13090 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13091 *
13092 * The @phba struct is used to send mailbox command to HBA. The @startq
13093 * is used to get the starting FCP EQ to change.
13094 * This function is asynchronous and will wait for the mailbox
13095 * command to finish before continuing.
13096 *
13097 * On success this function will return a zero. If unable to allocate enough
13098 * memory this function will return -ENOMEM. If the queue create mailbox command
13099 * fails this function will return -ENXIO.
13100 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013101int
James Smart2c9c5a02015-04-07 15:07:15 -040013102lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013103{
13104 struct lpfc_mbx_modify_eq_delay *eq_delay;
13105 LPFC_MBOXQ_t *mbox;
13106 struct lpfc_queue *eq;
13107 int cnt, rc, length, status = 0;
13108 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013109 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013110 int fcp_eqidx;
13111 union lpfc_sli4_cfg_shdr *shdr;
13112 uint16_t dmult;
13113
James Smart67d12732012-08-03 12:36:13 -040013114 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013115 return 0;
13116
13117 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13118 if (!mbox)
13119 return -ENOMEM;
13120 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13121 sizeof(struct lpfc_sli4_cfg_mhdr));
13122 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13123 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13124 length, LPFC_SLI4_MBX_EMBED);
13125 eq_delay = &mbox->u.mqe.un.eq_delay;
13126
13127 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013128 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13129 if (result > LPFC_DMULT_CONST)
13130 dmult = 0;
13131 else
13132 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013133
13134 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013135 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013136 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013137 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013138 if (!eq)
13139 continue;
13140 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13141 eq_delay->u.request.eq[cnt].phase = 0;
13142 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13143 cnt++;
13144 if (cnt >= LPFC_MAX_EQ_DELAY)
13145 break;
13146 }
13147 eq_delay->u.request.num_eq = cnt;
13148
13149 mbox->vport = phba->pport;
13150 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13151 mbox->context1 = NULL;
13152 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13153 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13154 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13155 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13156 if (shdr_status || shdr_add_status || rc) {
13157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13158 "2512 MODIFY_EQ_DELAY mailbox failed with "
13159 "status x%x add_status x%x, mbx status x%x\n",
13160 shdr_status, shdr_add_status, rc);
13161 status = -ENXIO;
13162 }
13163 mempool_free(mbox, phba->mbox_mem_pool);
13164 return status;
13165}
13166
13167/**
James Smart4f774512009-05-22 14:52:35 -040013168 * lpfc_eq_create - Create an Event Queue on the HBA
13169 * @phba: HBA structure that indicates port to create a queue on.
13170 * @eq: The queue structure to use to create the event queue.
13171 * @imax: The maximum interrupt per second limit.
13172 *
13173 * This function creates an event queue, as detailed in @eq, on a port,
13174 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13175 *
13176 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13177 * is used to get the entry count and entry size that are necessary to
13178 * determine the number of pages to allocate and use for this queue. This
13179 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13180 * event queue. This function is asynchronous and will wait for the mailbox
13181 * command to finish before continuing.
13182 *
13183 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013184 * memory this function will return -ENOMEM. If the queue create mailbox command
13185 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013186 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013187int
James Smartee020062012-09-29 11:28:52 -040013188lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013189{
13190 struct lpfc_mbx_eq_create *eq_create;
13191 LPFC_MBOXQ_t *mbox;
13192 int rc, length, status = 0;
13193 struct lpfc_dmabuf *dmabuf;
13194 uint32_t shdr_status, shdr_add_status;
13195 union lpfc_sli4_cfg_shdr *shdr;
13196 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013197 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13198
James Smart2e90f4b2011-12-13 13:22:37 -050013199 /* sanity check on queue memory */
13200 if (!eq)
13201 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013202 if (!phba->sli4_hba.pc_sli4_params.supported)
13203 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013204
13205 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13206 if (!mbox)
13207 return -ENOMEM;
13208 length = (sizeof(struct lpfc_mbx_eq_create) -
13209 sizeof(struct lpfc_sli4_cfg_mhdr));
13210 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13211 LPFC_MBOX_OPCODE_EQ_CREATE,
13212 length, LPFC_SLI4_MBX_EMBED);
13213 eq_create = &mbox->u.mqe.un.eq_create;
13214 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13215 eq->page_count);
13216 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13217 LPFC_EQE_SIZE);
13218 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013219 /* don't setup delay multiplier using EQ_CREATE */
13220 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013221 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13222 dmult);
13223 switch (eq->entry_count) {
13224 default:
13225 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13226 "0360 Unsupported EQ count. (%d)\n",
13227 eq->entry_count);
13228 if (eq->entry_count < 256)
13229 return -EINVAL;
13230 /* otherwise default to smallest count (drop through) */
13231 case 256:
13232 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13233 LPFC_EQ_CNT_256);
13234 break;
13235 case 512:
13236 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13237 LPFC_EQ_CNT_512);
13238 break;
13239 case 1024:
13240 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13241 LPFC_EQ_CNT_1024);
13242 break;
13243 case 2048:
13244 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13245 LPFC_EQ_CNT_2048);
13246 break;
13247 case 4096:
13248 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13249 LPFC_EQ_CNT_4096);
13250 break;
13251 }
13252 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013253 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013254 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13255 putPaddrLow(dmabuf->phys);
13256 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13257 putPaddrHigh(dmabuf->phys);
13258 }
13259 mbox->vport = phba->pport;
13260 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13261 mbox->context1 = NULL;
13262 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13263 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13264 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13265 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13266 if (shdr_status || shdr_add_status || rc) {
13267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13268 "2500 EQ_CREATE mailbox failed with "
13269 "status x%x add_status x%x, mbx status x%x\n",
13270 shdr_status, shdr_add_status, rc);
13271 status = -ENXIO;
13272 }
13273 eq->type = LPFC_EQ;
13274 eq->subtype = LPFC_NONE;
13275 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13276 if (eq->queue_id == 0xFFFF)
13277 status = -ENXIO;
13278 eq->host_index = 0;
13279 eq->hba_index = 0;
13280
James Smart8fa38512009-07-19 10:01:03 -040013281 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013282 return status;
13283}
13284
13285/**
13286 * lpfc_cq_create - Create a Completion Queue on the HBA
13287 * @phba: HBA structure that indicates port to create a queue on.
13288 * @cq: The queue structure to use to create the completion queue.
13289 * @eq: The event queue to bind this completion queue to.
13290 *
13291 * This function creates a completion queue, as detailed in @wq, on a port,
13292 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13293 *
13294 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13295 * is used to get the entry count and entry size that are necessary to
13296 * determine the number of pages to allocate and use for this queue. The @eq
13297 * is used to indicate which event queue to bind this completion queue to. This
13298 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13299 * completion queue. This function is asynchronous and will wait for the mailbox
13300 * command to finish before continuing.
13301 *
13302 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013303 * memory this function will return -ENOMEM. If the queue create mailbox command
13304 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013305 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013306int
James Smart4f774512009-05-22 14:52:35 -040013307lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13308 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13309{
13310 struct lpfc_mbx_cq_create *cq_create;
13311 struct lpfc_dmabuf *dmabuf;
13312 LPFC_MBOXQ_t *mbox;
13313 int rc, length, status = 0;
13314 uint32_t shdr_status, shdr_add_status;
13315 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013316 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13317
James Smart2e90f4b2011-12-13 13:22:37 -050013318 /* sanity check on queue memory */
13319 if (!cq || !eq)
13320 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013321 if (!phba->sli4_hba.pc_sli4_params.supported)
13322 hw_page_size = SLI4_PAGE_SIZE;
13323
James Smart4f774512009-05-22 14:52:35 -040013324 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13325 if (!mbox)
13326 return -ENOMEM;
13327 length = (sizeof(struct lpfc_mbx_cq_create) -
13328 sizeof(struct lpfc_sli4_cfg_mhdr));
13329 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13330 LPFC_MBOX_OPCODE_CQ_CREATE,
13331 length, LPFC_SLI4_MBX_EMBED);
13332 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013333 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013334 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13335 cq->page_count);
13336 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13337 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013338 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13339 phba->sli4_hba.pc_sli4_params.cqv);
13340 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013341 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13342 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013343 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13344 eq->queue_id);
13345 } else {
13346 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13347 eq->queue_id);
13348 }
James Smart4f774512009-05-22 14:52:35 -040013349 switch (cq->entry_count) {
13350 default:
13351 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13352 "0361 Unsupported CQ count. (%d)\n",
13353 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013354 if (cq->entry_count < 256) {
13355 status = -EINVAL;
13356 goto out;
13357 }
James Smart4f774512009-05-22 14:52:35 -040013358 /* otherwise default to smallest count (drop through) */
13359 case 256:
13360 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13361 LPFC_CQ_CNT_256);
13362 break;
13363 case 512:
13364 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13365 LPFC_CQ_CNT_512);
13366 break;
13367 case 1024:
13368 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13369 LPFC_CQ_CNT_1024);
13370 break;
13371 }
13372 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013373 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013374 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13375 putPaddrLow(dmabuf->phys);
13376 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13377 putPaddrHigh(dmabuf->phys);
13378 }
13379 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13380
13381 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013382 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13383 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13384 if (shdr_status || shdr_add_status || rc) {
13385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13386 "2501 CQ_CREATE mailbox failed with "
13387 "status x%x add_status x%x, mbx status x%x\n",
13388 shdr_status, shdr_add_status, rc);
13389 status = -ENXIO;
13390 goto out;
13391 }
13392 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13393 if (cq->queue_id == 0xFFFF) {
13394 status = -ENXIO;
13395 goto out;
13396 }
13397 /* link the cq onto the parent eq child list */
13398 list_add_tail(&cq->list, &eq->child_list);
13399 /* Set up completion queue's type and subtype */
13400 cq->type = type;
13401 cq->subtype = subtype;
13402 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013403 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013404 cq->host_index = 0;
13405 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013406
James Smart8fa38512009-07-19 10:01:03 -040013407out:
13408 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013409 return status;
13410}
13411
13412/**
James Smartb19a0612010-04-06 14:48:51 -040013413 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013414 * @phba: HBA structure that indicates port to create a queue on.
13415 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013416 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13417 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013418 *
James Smartb19a0612010-04-06 14:48:51 -040013419 * This function provides failback (fb) functionality when the
13420 * mq_create_ext fails on older FW generations. It's purpose is identical
13421 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013422 *
James Smartb19a0612010-04-06 14:48:51 -040013423 * This routine cannot fail as all attributes were previously accessed and
13424 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013425 **/
James Smartb19a0612010-04-06 14:48:51 -040013426static void
13427lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13428 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013429{
13430 struct lpfc_mbx_mq_create *mq_create;
13431 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013432 int length;
James Smart04c68492009-05-22 14:52:52 -040013433
James Smart04c68492009-05-22 14:52:52 -040013434 length = (sizeof(struct lpfc_mbx_mq_create) -
13435 sizeof(struct lpfc_sli4_cfg_mhdr));
13436 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13437 LPFC_MBOX_OPCODE_MQ_CREATE,
13438 length, LPFC_SLI4_MBX_EMBED);
13439 mq_create = &mbox->u.mqe.un.mq_create;
13440 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013441 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013442 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013443 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013444 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13445 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013446 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013447 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13448 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013449 break;
13450 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013451 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13452 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013453 break;
13454 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013455 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13456 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013457 break;
13458 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013459 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13460 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013461 break;
13462 }
13463 list_for_each_entry(dmabuf, &mq->page_list, list) {
13464 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013465 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013466 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013467 putPaddrHigh(dmabuf->phys);
13468 }
13469}
13470
13471/**
13472 * lpfc_mq_create - Create a mailbox Queue on the HBA
13473 * @phba: HBA structure that indicates port to create a queue on.
13474 * @mq: The queue structure to use to create the mailbox queue.
13475 * @cq: The completion queue to associate with this cq.
13476 * @subtype: The queue's subtype.
13477 *
13478 * This function creates a mailbox queue, as detailed in @mq, on a port,
13479 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13480 *
13481 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13482 * is used to get the entry count and entry size that are necessary to
13483 * determine the number of pages to allocate and use for this queue. This
13484 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13485 * mailbox queue. This function is asynchronous and will wait for the mailbox
13486 * command to finish before continuing.
13487 *
13488 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013489 * memory this function will return -ENOMEM. If the queue create mailbox command
13490 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013491 **/
13492int32_t
13493lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13494 struct lpfc_queue *cq, uint32_t subtype)
13495{
13496 struct lpfc_mbx_mq_create *mq_create;
13497 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13498 struct lpfc_dmabuf *dmabuf;
13499 LPFC_MBOXQ_t *mbox;
13500 int rc, length, status = 0;
13501 uint32_t shdr_status, shdr_add_status;
13502 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013503 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013504
James Smart2e90f4b2011-12-13 13:22:37 -050013505 /* sanity check on queue memory */
13506 if (!mq || !cq)
13507 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013508 if (!phba->sli4_hba.pc_sli4_params.supported)
13509 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013510
13511 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13512 if (!mbox)
13513 return -ENOMEM;
13514 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13515 sizeof(struct lpfc_sli4_cfg_mhdr));
13516 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13517 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13518 length, LPFC_SLI4_MBX_EMBED);
13519
13520 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013521 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013522 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13523 &mq_create_ext->u.request, mq->page_count);
13524 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13525 &mq_create_ext->u.request, 1);
13526 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013527 &mq_create_ext->u.request, 1);
13528 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13529 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013530 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13531 &mq_create_ext->u.request, 1);
13532 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13533 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013534 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013535 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13536 phba->sli4_hba.pc_sli4_params.mqv);
13537 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13538 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13539 cq->queue_id);
13540 else
13541 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13542 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013543 switch (mq->entry_count) {
13544 default:
13545 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13546 "0362 Unsupported MQ count. (%d)\n",
13547 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013548 if (mq->entry_count < 16) {
13549 status = -EINVAL;
13550 goto out;
13551 }
James Smartb19a0612010-04-06 14:48:51 -040013552 /* otherwise default to smallest count (drop through) */
13553 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013554 bf_set(lpfc_mq_context_ring_size,
13555 &mq_create_ext->u.request.context,
13556 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013557 break;
13558 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013559 bf_set(lpfc_mq_context_ring_size,
13560 &mq_create_ext->u.request.context,
13561 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013562 break;
13563 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013564 bf_set(lpfc_mq_context_ring_size,
13565 &mq_create_ext->u.request.context,
13566 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013567 break;
13568 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013569 bf_set(lpfc_mq_context_ring_size,
13570 &mq_create_ext->u.request.context,
13571 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013572 break;
13573 }
13574 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013575 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013576 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13577 putPaddrLow(dmabuf->phys);
13578 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013579 putPaddrHigh(dmabuf->phys);
13580 }
13581 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013582 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13583 &mq_create_ext->u.response);
13584 if (rc != MBX_SUCCESS) {
13585 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13586 "2795 MQ_CREATE_EXT failed with "
13587 "status x%x. Failback to MQ_CREATE.\n",
13588 rc);
13589 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13590 mq_create = &mbox->u.mqe.un.mq_create;
13591 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13592 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13593 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13594 &mq_create->u.response);
13595 }
13596
James Smart04c68492009-05-22 14:52:52 -040013597 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013598 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13599 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13600 if (shdr_status || shdr_add_status || rc) {
13601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13602 "2502 MQ_CREATE mailbox failed with "
13603 "status x%x add_status x%x, mbx status x%x\n",
13604 shdr_status, shdr_add_status, rc);
13605 status = -ENXIO;
13606 goto out;
13607 }
James Smart04c68492009-05-22 14:52:52 -040013608 if (mq->queue_id == 0xFFFF) {
13609 status = -ENXIO;
13610 goto out;
13611 }
13612 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013613 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013614 mq->subtype = subtype;
13615 mq->host_index = 0;
13616 mq->hba_index = 0;
13617
13618 /* link the mq onto the parent cq child list */
13619 list_add_tail(&mq->list, &cq->child_list);
13620out:
James Smart8fa38512009-07-19 10:01:03 -040013621 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013622 return status;
13623}
13624
13625/**
James Smart4f774512009-05-22 14:52:35 -040013626 * lpfc_wq_create - Create a Work Queue on the HBA
13627 * @phba: HBA structure that indicates port to create a queue on.
13628 * @wq: The queue structure to use to create the work queue.
13629 * @cq: The completion queue to bind this work queue to.
13630 * @subtype: The subtype of the work queue indicating its functionality.
13631 *
13632 * This function creates a work queue, as detailed in @wq, on a port, described
13633 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13634 *
13635 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13636 * is used to get the entry count and entry size that are necessary to
13637 * determine the number of pages to allocate and use for this queue. The @cq
13638 * is used to indicate which completion queue to bind this work queue to. This
13639 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13640 * work queue. This function is asynchronous and will wait for the mailbox
13641 * command to finish before continuing.
13642 *
13643 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013644 * memory this function will return -ENOMEM. If the queue create mailbox command
13645 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013646 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013647int
James Smart4f774512009-05-22 14:52:35 -040013648lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13649 struct lpfc_queue *cq, uint32_t subtype)
13650{
13651 struct lpfc_mbx_wq_create *wq_create;
13652 struct lpfc_dmabuf *dmabuf;
13653 LPFC_MBOXQ_t *mbox;
13654 int rc, length, status = 0;
13655 uint32_t shdr_status, shdr_add_status;
13656 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013657 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013658 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013659 void __iomem *bar_memmap_p;
13660 uint32_t db_offset;
13661 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013662
James Smart2e90f4b2011-12-13 13:22:37 -050013663 /* sanity check on queue memory */
13664 if (!wq || !cq)
13665 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013666 if (!phba->sli4_hba.pc_sli4_params.supported)
13667 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013668
13669 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13670 if (!mbox)
13671 return -ENOMEM;
13672 length = (sizeof(struct lpfc_mbx_wq_create) -
13673 sizeof(struct lpfc_sli4_cfg_mhdr));
13674 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13675 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13676 length, LPFC_SLI4_MBX_EMBED);
13677 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013678 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013679 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13680 wq->page_count);
13681 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13682 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013683
13684 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013685 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13686 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013687
James Smart0c651872013-07-15 18:33:23 -040013688 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13689 case LPFC_Q_CREATE_VERSION_0:
13690 switch (wq->entry_size) {
13691 default:
13692 case 64:
13693 /* Nothing to do, version 0 ONLY supports 64 byte */
13694 page = wq_create->u.request.page;
13695 break;
13696 case 128:
13697 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13698 LPFC_WQ_SZ128_SUPPORT)) {
13699 status = -ERANGE;
13700 goto out;
13701 }
13702 /* If we get here the HBA MUST also support V1 and
13703 * we MUST use it
13704 */
13705 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13706 LPFC_Q_CREATE_VERSION_1);
13707
13708 bf_set(lpfc_mbx_wq_create_wqe_count,
13709 &wq_create->u.request_1, wq->entry_count);
13710 bf_set(lpfc_mbx_wq_create_wqe_size,
13711 &wq_create->u.request_1,
13712 LPFC_WQ_WQE_SIZE_128);
13713 bf_set(lpfc_mbx_wq_create_page_size,
13714 &wq_create->u.request_1,
13715 (PAGE_SIZE/SLI4_PAGE_SIZE));
13716 page = wq_create->u.request_1.page;
13717 break;
13718 }
13719 break;
13720 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013721 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13722 wq->entry_count);
13723 switch (wq->entry_size) {
13724 default:
13725 case 64:
13726 bf_set(lpfc_mbx_wq_create_wqe_size,
13727 &wq_create->u.request_1,
13728 LPFC_WQ_WQE_SIZE_64);
13729 break;
13730 case 128:
James Smart0c651872013-07-15 18:33:23 -040013731 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13732 LPFC_WQ_SZ128_SUPPORT)) {
13733 status = -ERANGE;
13734 goto out;
13735 }
James Smart5a6f1332011-03-11 16:05:35 -050013736 bf_set(lpfc_mbx_wq_create_wqe_size,
13737 &wq_create->u.request_1,
13738 LPFC_WQ_WQE_SIZE_128);
13739 break;
13740 }
13741 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13742 (PAGE_SIZE/SLI4_PAGE_SIZE));
13743 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013744 break;
13745 default:
13746 status = -ERANGE;
13747 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013748 }
James Smart0c651872013-07-15 18:33:23 -040013749
James Smart4f774512009-05-22 14:52:35 -040013750 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013751 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013752 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13753 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013754 }
James Smart962bc512013-01-03 15:44:00 -050013755
13756 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13757 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13758
James Smart4f774512009-05-22 14:52:35 -040013759 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13760 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013761 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13762 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13763 if (shdr_status || shdr_add_status || rc) {
13764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13765 "2503 WQ_CREATE mailbox failed with "
13766 "status x%x add_status x%x, mbx status x%x\n",
13767 shdr_status, shdr_add_status, rc);
13768 status = -ENXIO;
13769 goto out;
13770 }
13771 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13772 if (wq->queue_id == 0xFFFF) {
13773 status = -ENXIO;
13774 goto out;
13775 }
James Smart962bc512013-01-03 15:44:00 -050013776 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13777 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13778 &wq_create->u.response);
13779 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13780 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13782 "3265 WQ[%d] doorbell format not "
13783 "supported: x%x\n", wq->queue_id,
13784 wq->db_format);
13785 status = -EINVAL;
13786 goto out;
13787 }
13788 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13789 &wq_create->u.response);
13790 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13791 if (!bar_memmap_p) {
13792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13793 "3263 WQ[%d] failed to memmap pci "
13794 "barset:x%x\n", wq->queue_id,
13795 pci_barset);
13796 status = -ENOMEM;
13797 goto out;
13798 }
13799 db_offset = wq_create->u.response.doorbell_offset;
13800 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13801 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13803 "3252 WQ[%d] doorbell offset not "
13804 "supported: x%x\n", wq->queue_id,
13805 db_offset);
13806 status = -EINVAL;
13807 goto out;
13808 }
13809 wq->db_regaddr = bar_memmap_p + db_offset;
13810 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013811 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13812 "format:x%x\n", wq->queue_id, pci_barset,
13813 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013814 } else {
13815 wq->db_format = LPFC_DB_LIST_FORMAT;
13816 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13817 }
James Smart4f774512009-05-22 14:52:35 -040013818 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013819 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013820 wq->subtype = subtype;
13821 wq->host_index = 0;
13822 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013823 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013824
13825 /* link the wq onto the parent cq child list */
13826 list_add_tail(&wq->list, &cq->child_list);
13827out:
James Smart8fa38512009-07-19 10:01:03 -040013828 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013829 return status;
13830}
13831
13832/**
James Smart73d91e52011-10-10 21:32:10 -040013833 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13834 * @phba: HBA structure that indicates port to create a queue on.
13835 * @rq: The queue structure to use for the receive queue.
13836 * @qno: The associated HBQ number
13837 *
13838 *
13839 * For SLI4 we need to adjust the RQ repost value based on
13840 * the number of buffers that are initially posted to the RQ.
13841 */
13842void
13843lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13844{
13845 uint32_t cnt;
13846
James Smart2e90f4b2011-12-13 13:22:37 -050013847 /* sanity check on queue memory */
13848 if (!rq)
13849 return;
James Smart73d91e52011-10-10 21:32:10 -040013850 cnt = lpfc_hbq_defs[qno]->entry_count;
13851
13852 /* Recalc repost for RQs based on buffers initially posted */
13853 cnt = (cnt >> 3);
13854 if (cnt < LPFC_QUEUE_MIN_REPOST)
13855 cnt = LPFC_QUEUE_MIN_REPOST;
13856
13857 rq->entry_repost = cnt;
13858}
13859
13860/**
James Smart4f774512009-05-22 14:52:35 -040013861 * lpfc_rq_create - Create a Receive Queue on the HBA
13862 * @phba: HBA structure that indicates port to create a queue on.
13863 * @hrq: The queue structure to use to create the header receive queue.
13864 * @drq: The queue structure to use to create the data receive queue.
13865 * @cq: The completion queue to bind this work queue to.
13866 *
13867 * This function creates a receive buffer queue pair , as detailed in @hrq and
13868 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13869 * to the HBA.
13870 *
13871 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13872 * struct is used to get the entry count that is necessary to determine the
13873 * number of pages to use for this queue. The @cq is used to indicate which
13874 * completion queue to bind received buffers that are posted to these queues to.
13875 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13876 * receive queue pair. This function is asynchronous and will wait for the
13877 * mailbox command to finish before continuing.
13878 *
13879 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013880 * memory this function will return -ENOMEM. If the queue create mailbox command
13881 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013882 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013883int
James Smart4f774512009-05-22 14:52:35 -040013884lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13885 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13886{
13887 struct lpfc_mbx_rq_create *rq_create;
13888 struct lpfc_dmabuf *dmabuf;
13889 LPFC_MBOXQ_t *mbox;
13890 int rc, length, status = 0;
13891 uint32_t shdr_status, shdr_add_status;
13892 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013893 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013894 void __iomem *bar_memmap_p;
13895 uint32_t db_offset;
13896 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013897
James Smart2e90f4b2011-12-13 13:22:37 -050013898 /* sanity check on queue memory */
13899 if (!hrq || !drq || !cq)
13900 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013901 if (!phba->sli4_hba.pc_sli4_params.supported)
13902 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013903
13904 if (hrq->entry_count != drq->entry_count)
13905 return -EINVAL;
13906 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13907 if (!mbox)
13908 return -ENOMEM;
13909 length = (sizeof(struct lpfc_mbx_rq_create) -
13910 sizeof(struct lpfc_sli4_cfg_mhdr));
13911 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13912 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13913 length, LPFC_SLI4_MBX_EMBED);
13914 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013915 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13916 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13917 phba->sli4_hba.pc_sli4_params.rqv);
13918 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13919 bf_set(lpfc_rq_context_rqe_count_1,
13920 &rq_create->u.request.context,
13921 hrq->entry_count);
13922 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013923 bf_set(lpfc_rq_context_rqe_size,
13924 &rq_create->u.request.context,
13925 LPFC_RQE_SIZE_8);
13926 bf_set(lpfc_rq_context_page_size,
13927 &rq_create->u.request.context,
13928 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013929 } else {
13930 switch (hrq->entry_count) {
13931 default:
13932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13933 "2535 Unsupported RQ count. (%d)\n",
13934 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013935 if (hrq->entry_count < 512) {
13936 status = -EINVAL;
13937 goto out;
13938 }
James Smart5a6f1332011-03-11 16:05:35 -050013939 /* otherwise default to smallest count (drop through) */
13940 case 512:
13941 bf_set(lpfc_rq_context_rqe_count,
13942 &rq_create->u.request.context,
13943 LPFC_RQ_RING_SIZE_512);
13944 break;
13945 case 1024:
13946 bf_set(lpfc_rq_context_rqe_count,
13947 &rq_create->u.request.context,
13948 LPFC_RQ_RING_SIZE_1024);
13949 break;
13950 case 2048:
13951 bf_set(lpfc_rq_context_rqe_count,
13952 &rq_create->u.request.context,
13953 LPFC_RQ_RING_SIZE_2048);
13954 break;
13955 case 4096:
13956 bf_set(lpfc_rq_context_rqe_count,
13957 &rq_create->u.request.context,
13958 LPFC_RQ_RING_SIZE_4096);
13959 break;
13960 }
13961 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13962 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013963 }
13964 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13965 cq->queue_id);
13966 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13967 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013968 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013969 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013970 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13971 putPaddrLow(dmabuf->phys);
13972 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13973 putPaddrHigh(dmabuf->phys);
13974 }
James Smart962bc512013-01-03 15:44:00 -050013975 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13976 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13977
James Smart4f774512009-05-22 14:52:35 -040013978 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13979 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013980 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13981 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13982 if (shdr_status || shdr_add_status || rc) {
13983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13984 "2504 RQ_CREATE mailbox failed with "
13985 "status x%x add_status x%x, mbx status x%x\n",
13986 shdr_status, shdr_add_status, rc);
13987 status = -ENXIO;
13988 goto out;
13989 }
13990 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13991 if (hrq->queue_id == 0xFFFF) {
13992 status = -ENXIO;
13993 goto out;
13994 }
James Smart962bc512013-01-03 15:44:00 -050013995
13996 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13997 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13998 &rq_create->u.response);
13999 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14000 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14002 "3262 RQ [%d] doorbell format not "
14003 "supported: x%x\n", hrq->queue_id,
14004 hrq->db_format);
14005 status = -EINVAL;
14006 goto out;
14007 }
14008
14009 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14010 &rq_create->u.response);
14011 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14012 if (!bar_memmap_p) {
14013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14014 "3269 RQ[%d] failed to memmap pci "
14015 "barset:x%x\n", hrq->queue_id,
14016 pci_barset);
14017 status = -ENOMEM;
14018 goto out;
14019 }
14020
14021 db_offset = rq_create->u.response.doorbell_offset;
14022 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14023 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14025 "3270 RQ[%d] doorbell offset not "
14026 "supported: x%x\n", hrq->queue_id,
14027 db_offset);
14028 status = -EINVAL;
14029 goto out;
14030 }
14031 hrq->db_regaddr = bar_memmap_p + db_offset;
14032 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014033 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14034 "format:x%x\n", hrq->queue_id, pci_barset,
14035 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014036 } else {
14037 hrq->db_format = LPFC_DB_RING_FORMAT;
14038 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14039 }
James Smart4f774512009-05-22 14:52:35 -040014040 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014041 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014042 hrq->subtype = subtype;
14043 hrq->host_index = 0;
14044 hrq->hba_index = 0;
14045
14046 /* now create the data queue */
14047 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14048 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14049 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014050 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14051 phba->sli4_hba.pc_sli4_params.rqv);
14052 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14053 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014054 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014055 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014056 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14057 LPFC_RQE_SIZE_8);
14058 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14059 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014060 } else {
14061 switch (drq->entry_count) {
14062 default:
14063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14064 "2536 Unsupported RQ count. (%d)\n",
14065 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014066 if (drq->entry_count < 512) {
14067 status = -EINVAL;
14068 goto out;
14069 }
James Smart5a6f1332011-03-11 16:05:35 -050014070 /* otherwise default to smallest count (drop through) */
14071 case 512:
14072 bf_set(lpfc_rq_context_rqe_count,
14073 &rq_create->u.request.context,
14074 LPFC_RQ_RING_SIZE_512);
14075 break;
14076 case 1024:
14077 bf_set(lpfc_rq_context_rqe_count,
14078 &rq_create->u.request.context,
14079 LPFC_RQ_RING_SIZE_1024);
14080 break;
14081 case 2048:
14082 bf_set(lpfc_rq_context_rqe_count,
14083 &rq_create->u.request.context,
14084 LPFC_RQ_RING_SIZE_2048);
14085 break;
14086 case 4096:
14087 bf_set(lpfc_rq_context_rqe_count,
14088 &rq_create->u.request.context,
14089 LPFC_RQ_RING_SIZE_4096);
14090 break;
14091 }
14092 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14093 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014094 }
14095 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14096 cq->queue_id);
14097 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14098 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014099 list_for_each_entry(dmabuf, &drq->page_list, list) {
14100 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14101 putPaddrLow(dmabuf->phys);
14102 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14103 putPaddrHigh(dmabuf->phys);
14104 }
James Smart962bc512013-01-03 15:44:00 -050014105 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14106 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014107 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14108 /* The IOCTL status is embedded in the mailbox subheader. */
14109 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14110 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14111 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14112 if (shdr_status || shdr_add_status || rc) {
14113 status = -ENXIO;
14114 goto out;
14115 }
14116 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14117 if (drq->queue_id == 0xFFFF) {
14118 status = -ENXIO;
14119 goto out;
14120 }
14121 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014122 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014123 drq->subtype = subtype;
14124 drq->host_index = 0;
14125 drq->hba_index = 0;
14126
14127 /* link the header and data RQs onto the parent cq child list */
14128 list_add_tail(&hrq->list, &cq->child_list);
14129 list_add_tail(&drq->list, &cq->child_list);
14130
14131out:
James Smart8fa38512009-07-19 10:01:03 -040014132 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014133 return status;
14134}
14135
14136/**
14137 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14138 * @eq: The queue structure associated with the queue to destroy.
14139 *
14140 * This function destroys a queue, as detailed in @eq by sending an mailbox
14141 * command, specific to the type of queue, to the HBA.
14142 *
14143 * The @eq struct is used to get the queue ID of the queue to destroy.
14144 *
14145 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014146 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014147 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014148int
James Smart4f774512009-05-22 14:52:35 -040014149lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14150{
14151 LPFC_MBOXQ_t *mbox;
14152 int rc, length, status = 0;
14153 uint32_t shdr_status, shdr_add_status;
14154 union lpfc_sli4_cfg_shdr *shdr;
14155
James Smart2e90f4b2011-12-13 13:22:37 -050014156 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014157 if (!eq)
14158 return -ENODEV;
14159 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14160 if (!mbox)
14161 return -ENOMEM;
14162 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14163 sizeof(struct lpfc_sli4_cfg_mhdr));
14164 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14165 LPFC_MBOX_OPCODE_EQ_DESTROY,
14166 length, LPFC_SLI4_MBX_EMBED);
14167 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14168 eq->queue_id);
14169 mbox->vport = eq->phba->pport;
14170 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14171
14172 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14173 /* The IOCTL status is embedded in the mailbox subheader. */
14174 shdr = (union lpfc_sli4_cfg_shdr *)
14175 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14176 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14177 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14178 if (shdr_status || shdr_add_status || rc) {
14179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14180 "2505 EQ_DESTROY mailbox failed with "
14181 "status x%x add_status x%x, mbx status x%x\n",
14182 shdr_status, shdr_add_status, rc);
14183 status = -ENXIO;
14184 }
14185
14186 /* Remove eq from any list */
14187 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014188 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014189 return status;
14190}
14191
14192/**
14193 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14194 * @cq: The queue structure associated with the queue to destroy.
14195 *
14196 * This function destroys a queue, as detailed in @cq by sending an mailbox
14197 * command, specific to the type of queue, to the HBA.
14198 *
14199 * The @cq struct is used to get the queue ID of the queue to destroy.
14200 *
14201 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014202 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014203 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014204int
James Smart4f774512009-05-22 14:52:35 -040014205lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14206{
14207 LPFC_MBOXQ_t *mbox;
14208 int rc, length, status = 0;
14209 uint32_t shdr_status, shdr_add_status;
14210 union lpfc_sli4_cfg_shdr *shdr;
14211
James Smart2e90f4b2011-12-13 13:22:37 -050014212 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014213 if (!cq)
14214 return -ENODEV;
14215 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14216 if (!mbox)
14217 return -ENOMEM;
14218 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14219 sizeof(struct lpfc_sli4_cfg_mhdr));
14220 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14221 LPFC_MBOX_OPCODE_CQ_DESTROY,
14222 length, LPFC_SLI4_MBX_EMBED);
14223 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14224 cq->queue_id);
14225 mbox->vport = cq->phba->pport;
14226 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14227 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14228 /* The IOCTL status is embedded in the mailbox subheader. */
14229 shdr = (union lpfc_sli4_cfg_shdr *)
14230 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14231 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14232 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14233 if (shdr_status || shdr_add_status || rc) {
14234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14235 "2506 CQ_DESTROY mailbox failed with "
14236 "status x%x add_status x%x, mbx status x%x\n",
14237 shdr_status, shdr_add_status, rc);
14238 status = -ENXIO;
14239 }
14240 /* Remove cq from any list */
14241 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014242 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014243 return status;
14244}
14245
14246/**
James Smart04c68492009-05-22 14:52:52 -040014247 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14248 * @qm: The queue structure associated with the queue to destroy.
14249 *
14250 * This function destroys a queue, as detailed in @mq by sending an mailbox
14251 * command, specific to the type of queue, to the HBA.
14252 *
14253 * The @mq struct is used to get the queue ID of the queue to destroy.
14254 *
14255 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014256 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014257 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014258int
James Smart04c68492009-05-22 14:52:52 -040014259lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14260{
14261 LPFC_MBOXQ_t *mbox;
14262 int rc, length, status = 0;
14263 uint32_t shdr_status, shdr_add_status;
14264 union lpfc_sli4_cfg_shdr *shdr;
14265
James Smart2e90f4b2011-12-13 13:22:37 -050014266 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014267 if (!mq)
14268 return -ENODEV;
14269 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14270 if (!mbox)
14271 return -ENOMEM;
14272 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14273 sizeof(struct lpfc_sli4_cfg_mhdr));
14274 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14275 LPFC_MBOX_OPCODE_MQ_DESTROY,
14276 length, LPFC_SLI4_MBX_EMBED);
14277 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14278 mq->queue_id);
14279 mbox->vport = mq->phba->pport;
14280 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14281 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14282 /* The IOCTL status is embedded in the mailbox subheader. */
14283 shdr = (union lpfc_sli4_cfg_shdr *)
14284 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14285 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14286 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14287 if (shdr_status || shdr_add_status || rc) {
14288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14289 "2507 MQ_DESTROY mailbox failed with "
14290 "status x%x add_status x%x, mbx status x%x\n",
14291 shdr_status, shdr_add_status, rc);
14292 status = -ENXIO;
14293 }
14294 /* Remove mq from any list */
14295 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014296 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014297 return status;
14298}
14299
14300/**
James Smart4f774512009-05-22 14:52:35 -040014301 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14302 * @wq: The queue structure associated with the queue to destroy.
14303 *
14304 * This function destroys a queue, as detailed in @wq by sending an mailbox
14305 * command, specific to the type of queue, to the HBA.
14306 *
14307 * The @wq struct is used to get the queue ID of the queue to destroy.
14308 *
14309 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014310 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014311 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014312int
James Smart4f774512009-05-22 14:52:35 -040014313lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14314{
14315 LPFC_MBOXQ_t *mbox;
14316 int rc, length, status = 0;
14317 uint32_t shdr_status, shdr_add_status;
14318 union lpfc_sli4_cfg_shdr *shdr;
14319
James Smart2e90f4b2011-12-13 13:22:37 -050014320 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014321 if (!wq)
14322 return -ENODEV;
14323 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14324 if (!mbox)
14325 return -ENOMEM;
14326 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14327 sizeof(struct lpfc_sli4_cfg_mhdr));
14328 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14329 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14330 length, LPFC_SLI4_MBX_EMBED);
14331 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14332 wq->queue_id);
14333 mbox->vport = wq->phba->pport;
14334 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14335 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14336 shdr = (union lpfc_sli4_cfg_shdr *)
14337 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14338 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14339 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14340 if (shdr_status || shdr_add_status || rc) {
14341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14342 "2508 WQ_DESTROY mailbox failed with "
14343 "status x%x add_status x%x, mbx status x%x\n",
14344 shdr_status, shdr_add_status, rc);
14345 status = -ENXIO;
14346 }
14347 /* Remove wq from any list */
14348 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014349 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014350 return status;
14351}
14352
14353/**
14354 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14355 * @rq: The queue structure associated with the queue to destroy.
14356 *
14357 * This function destroys a queue, as detailed in @rq by sending an mailbox
14358 * command, specific to the type of queue, to the HBA.
14359 *
14360 * The @rq struct is used to get the queue ID of the queue to destroy.
14361 *
14362 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014363 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014364 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014365int
James Smart4f774512009-05-22 14:52:35 -040014366lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14367 struct lpfc_queue *drq)
14368{
14369 LPFC_MBOXQ_t *mbox;
14370 int rc, length, status = 0;
14371 uint32_t shdr_status, shdr_add_status;
14372 union lpfc_sli4_cfg_shdr *shdr;
14373
James Smart2e90f4b2011-12-13 13:22:37 -050014374 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014375 if (!hrq || !drq)
14376 return -ENODEV;
14377 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14378 if (!mbox)
14379 return -ENOMEM;
14380 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014381 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014382 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14383 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14384 length, LPFC_SLI4_MBX_EMBED);
14385 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14386 hrq->queue_id);
14387 mbox->vport = hrq->phba->pport;
14388 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14389 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14390 /* The IOCTL status is embedded in the mailbox subheader. */
14391 shdr = (union lpfc_sli4_cfg_shdr *)
14392 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14393 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14394 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14395 if (shdr_status || shdr_add_status || rc) {
14396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14397 "2509 RQ_DESTROY mailbox failed with "
14398 "status x%x add_status x%x, mbx status x%x\n",
14399 shdr_status, shdr_add_status, rc);
14400 if (rc != MBX_TIMEOUT)
14401 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14402 return -ENXIO;
14403 }
14404 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14405 drq->queue_id);
14406 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14407 shdr = (union lpfc_sli4_cfg_shdr *)
14408 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14409 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14410 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14411 if (shdr_status || shdr_add_status || rc) {
14412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14413 "2510 RQ_DESTROY mailbox failed with "
14414 "status x%x add_status x%x, mbx status x%x\n",
14415 shdr_status, shdr_add_status, rc);
14416 status = -ENXIO;
14417 }
14418 list_del_init(&hrq->list);
14419 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014420 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014421 return status;
14422}
14423
14424/**
14425 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14426 * @phba: The virtual port for which this call being executed.
14427 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14428 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14429 * @xritag: the xritag that ties this io to the SGL pages.
14430 *
14431 * This routine will post the sgl pages for the IO that has the xritag
14432 * that is in the iocbq structure. The xritag is assigned during iocbq
14433 * creation and persists for as long as the driver is loaded.
14434 * if the caller has fewer than 256 scatter gather segments to map then
14435 * pdma_phys_addr1 should be 0.
14436 * If the caller needs to map more than 256 scatter gather segment then
14437 * pdma_phys_addr1 should be a valid physical address.
14438 * physical address for SGLs must be 64 byte aligned.
14439 * If you are going to map 2 SGL's then the first one must have 256 entries
14440 * the second sgl can have between 1 and 256 entries.
14441 *
14442 * Return codes:
14443 * 0 - Success
14444 * -ENXIO, -ENOMEM - Failure
14445 **/
14446int
14447lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14448 dma_addr_t pdma_phys_addr0,
14449 dma_addr_t pdma_phys_addr1,
14450 uint16_t xritag)
14451{
14452 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14453 LPFC_MBOXQ_t *mbox;
14454 int rc;
14455 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014456 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014457 union lpfc_sli4_cfg_shdr *shdr;
14458
14459 if (xritag == NO_XRI) {
14460 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14461 "0364 Invalid param:\n");
14462 return -EINVAL;
14463 }
14464
14465 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14466 if (!mbox)
14467 return -ENOMEM;
14468
14469 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14470 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14471 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014472 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014473
14474 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14475 &mbox->u.mqe.un.post_sgl_pages;
14476 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14477 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14478
14479 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14480 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14481 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14482 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14483
14484 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14485 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14486 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14487 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14488 if (!phba->sli4_hba.intr_enable)
14489 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014490 else {
James Smarta183a152011-10-10 21:32:43 -040014491 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014492 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14493 }
James Smart4f774512009-05-22 14:52:35 -040014494 /* The IOCTL status is embedded in the mailbox subheader. */
14495 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14496 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14497 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14498 if (rc != MBX_TIMEOUT)
14499 mempool_free(mbox, phba->mbox_mem_pool);
14500 if (shdr_status || shdr_add_status || rc) {
14501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14502 "2511 POST_SGL mailbox failed with "
14503 "status x%x add_status x%x, mbx status x%x\n",
14504 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014505 }
14506 return 0;
14507}
James Smart4f774512009-05-22 14:52:35 -040014508
14509/**
James Smart88a2cfb2011-07-22 18:36:33 -040014510 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014511 * @phba: pointer to lpfc hba data structure.
14512 *
14513 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014514 * HBA consistent with the SLI-4 interface spec. This routine
14515 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14516 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014517 *
James Smart88a2cfb2011-07-22 18:36:33 -040014518 * Returns
14519 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14520 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14521 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014522static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014523lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14524{
14525 unsigned long xri;
14526
14527 /*
14528 * Fetch the next logical xri. Because this index is logical,
14529 * the driver starts at 0 each time.
14530 */
14531 spin_lock_irq(&phba->hbalock);
14532 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14533 phba->sli4_hba.max_cfg_param.max_xri, 0);
14534 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14535 spin_unlock_irq(&phba->hbalock);
14536 return NO_XRI;
14537 } else {
14538 set_bit(xri, phba->sli4_hba.xri_bmask);
14539 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014540 }
James Smart6d368e52011-05-24 11:44:12 -040014541 spin_unlock_irq(&phba->hbalock);
14542 return xri;
14543}
14544
14545/**
14546 * lpfc_sli4_free_xri - Release an xri for reuse.
14547 * @phba: pointer to lpfc hba data structure.
14548 *
14549 * This routine is invoked to release an xri to the pool of
14550 * available rpis maintained by the driver.
14551 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014552static void
James Smart6d368e52011-05-24 11:44:12 -040014553__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14554{
14555 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014556 phba->sli4_hba.max_cfg_param.xri_used--;
14557 }
14558}
14559
14560/**
14561 * lpfc_sli4_free_xri - Release an xri for reuse.
14562 * @phba: pointer to lpfc hba data structure.
14563 *
14564 * This routine is invoked to release an xri to the pool of
14565 * available rpis maintained by the driver.
14566 **/
14567void
14568lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14569{
14570 spin_lock_irq(&phba->hbalock);
14571 __lpfc_sli4_free_xri(phba, xri);
14572 spin_unlock_irq(&phba->hbalock);
14573}
14574
14575/**
James Smart4f774512009-05-22 14:52:35 -040014576 * lpfc_sli4_next_xritag - Get an xritag for the io
14577 * @phba: Pointer to HBA context object.
14578 *
14579 * This function gets an xritag for the iocb. If there is no unused xritag
14580 * it will return 0xffff.
14581 * The function returns the allocated xritag if successful, else returns zero.
14582 * Zero is not a valid xritag.
14583 * The caller is not required to hold any lock.
14584 **/
14585uint16_t
14586lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14587{
James Smart6d368e52011-05-24 11:44:12 -040014588 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014589
James Smart6d368e52011-05-24 11:44:12 -040014590 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014591 if (xri_index == NO_XRI)
14592 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14593 "2004 Failed to allocate XRI.last XRITAG is %d"
14594 " Max XRI is %d, Used XRI is %d\n",
14595 xri_index,
14596 phba->sli4_hba.max_cfg_param.max_xri,
14597 phba->sli4_hba.max_cfg_param.xri_used);
14598 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014599}
14600
14601/**
James Smart6d368e52011-05-24 11:44:12 -040014602 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014603 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014604 * @post_sgl_list: pointer to els sgl entry list.
14605 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014606 *
14607 * This routine is invoked to post a block of driver's sgl pages to the
14608 * HBA using non-embedded mailbox command. No Lock is held. This routine
14609 * is only called when the driver is loading and after all IO has been
14610 * stopped.
14611 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014612static int
14613lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14614 struct list_head *post_sgl_list,
14615 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014616{
James Smart8a9d2e82012-05-09 21:16:12 -040014617 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014618 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14619 struct sgl_page_pairs *sgl_pg_pairs;
14620 void *viraddr;
14621 LPFC_MBOXQ_t *mbox;
14622 uint32_t reqlen, alloclen, pg_pairs;
14623 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014624 uint16_t xritag_start = 0;
14625 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014626 uint32_t shdr_status, shdr_add_status;
14627 union lpfc_sli4_cfg_shdr *shdr;
14628
James Smart8a9d2e82012-05-09 21:16:12 -040014629 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014630 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014631 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014632 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14633 "2559 Block sgl registration required DMA "
14634 "size (%d) great than a page\n", reqlen);
14635 return -ENOMEM;
14636 }
14637 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014638 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014639 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014640
14641 /* Allocate DMA memory and set up the non-embedded mailbox command */
14642 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14643 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14644 LPFC_SLI4_MBX_NEMBED);
14645
14646 if (alloclen < reqlen) {
14647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14648 "0285 Allocated DMA memory size (%d) is "
14649 "less than the requested DMA memory "
14650 "size (%d)\n", alloclen, reqlen);
14651 lpfc_sli4_mbox_cmd_free(phba, mbox);
14652 return -ENOMEM;
14653 }
James Smart4f774512009-05-22 14:52:35 -040014654 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014655 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014656 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14657 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14658
James Smart8a9d2e82012-05-09 21:16:12 -040014659 pg_pairs = 0;
14660 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014661 /* Set up the sge entry */
14662 sgl_pg_pairs->sgl_pg0_addr_lo =
14663 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14664 sgl_pg_pairs->sgl_pg0_addr_hi =
14665 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14666 sgl_pg_pairs->sgl_pg1_addr_lo =
14667 cpu_to_le32(putPaddrLow(0));
14668 sgl_pg_pairs->sgl_pg1_addr_hi =
14669 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014670
James Smart4f774512009-05-22 14:52:35 -040014671 /* Keep the first xritag on the list */
14672 if (pg_pairs == 0)
14673 xritag_start = sglq_entry->sli4_xritag;
14674 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014675 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014676 }
James Smart6d368e52011-05-24 11:44:12 -040014677
14678 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014679 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014680 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014681 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014682 if (!phba->sli4_hba.intr_enable)
14683 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14684 else {
James Smarta183a152011-10-10 21:32:43 -040014685 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014686 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14687 }
14688 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14689 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14691 if (rc != MBX_TIMEOUT)
14692 lpfc_sli4_mbox_cmd_free(phba, mbox);
14693 if (shdr_status || shdr_add_status || rc) {
14694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14695 "2513 POST_SGL_BLOCK mailbox command failed "
14696 "status x%x add_status x%x mbx status x%x\n",
14697 shdr_status, shdr_add_status, rc);
14698 rc = -ENXIO;
14699 }
14700 return rc;
14701}
14702
14703/**
14704 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14705 * @phba: pointer to lpfc hba data structure.
14706 * @sblist: pointer to scsi buffer list.
14707 * @count: number of scsi buffers on the list.
14708 *
14709 * This routine is invoked to post a block of @count scsi sgl pages from a
14710 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14711 * No Lock is held.
14712 *
14713 **/
14714int
James Smart8a9d2e82012-05-09 21:16:12 -040014715lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14716 struct list_head *sblist,
14717 int count)
James Smart4f774512009-05-22 14:52:35 -040014718{
14719 struct lpfc_scsi_buf *psb;
14720 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14721 struct sgl_page_pairs *sgl_pg_pairs;
14722 void *viraddr;
14723 LPFC_MBOXQ_t *mbox;
14724 uint32_t reqlen, alloclen, pg_pairs;
14725 uint32_t mbox_tmo;
14726 uint16_t xritag_start = 0;
14727 int rc = 0;
14728 uint32_t shdr_status, shdr_add_status;
14729 dma_addr_t pdma_phys_bpl1;
14730 union lpfc_sli4_cfg_shdr *shdr;
14731
14732 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014733 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014734 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014735 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014736 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14737 "0217 Block sgl registration required DMA "
14738 "size (%d) great than a page\n", reqlen);
14739 return -ENOMEM;
14740 }
14741 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14742 if (!mbox) {
14743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14744 "0283 Failed to allocate mbox cmd memory\n");
14745 return -ENOMEM;
14746 }
14747
14748 /* Allocate DMA memory and set up the non-embedded mailbox command */
14749 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14750 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14751 LPFC_SLI4_MBX_NEMBED);
14752
14753 if (alloclen < reqlen) {
14754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14755 "2561 Allocated DMA memory size (%d) is "
14756 "less than the requested DMA memory "
14757 "size (%d)\n", alloclen, reqlen);
14758 lpfc_sli4_mbox_cmd_free(phba, mbox);
14759 return -ENOMEM;
14760 }
James Smart6d368e52011-05-24 11:44:12 -040014761
James Smart4f774512009-05-22 14:52:35 -040014762 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014763 viraddr = mbox->sge_array->addr[0];
14764
14765 /* Set up the SGL pages in the non-embedded DMA pages */
14766 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14767 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14768
14769 pg_pairs = 0;
14770 list_for_each_entry(psb, sblist, list) {
14771 /* Set up the sge entry */
14772 sgl_pg_pairs->sgl_pg0_addr_lo =
14773 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14774 sgl_pg_pairs->sgl_pg0_addr_hi =
14775 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14776 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14777 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14778 else
14779 pdma_phys_bpl1 = 0;
14780 sgl_pg_pairs->sgl_pg1_addr_lo =
14781 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14782 sgl_pg_pairs->sgl_pg1_addr_hi =
14783 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14784 /* Keep the first xritag on the list */
14785 if (pg_pairs == 0)
14786 xritag_start = psb->cur_iocbq.sli4_xritag;
14787 sgl_pg_pairs++;
14788 pg_pairs++;
14789 }
14790 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14791 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14792 /* Perform endian conversion if necessary */
14793 sgl->word0 = cpu_to_le32(sgl->word0);
14794
14795 if (!phba->sli4_hba.intr_enable)
14796 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14797 else {
James Smarta183a152011-10-10 21:32:43 -040014798 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014799 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14800 }
14801 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14802 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14803 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14804 if (rc != MBX_TIMEOUT)
14805 lpfc_sli4_mbox_cmd_free(phba, mbox);
14806 if (shdr_status || shdr_add_status || rc) {
14807 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14808 "2564 POST_SGL_BLOCK mailbox command failed "
14809 "status x%x add_status x%x mbx status x%x\n",
14810 shdr_status, shdr_add_status, rc);
14811 rc = -ENXIO;
14812 }
14813 return rc;
14814}
14815
14816/**
14817 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14818 * @phba: pointer to lpfc_hba struct that the frame was received on
14819 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14820 *
14821 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14822 * valid type of frame that the LPFC driver will handle. This function will
14823 * return a zero if the frame is a valid frame or a non zero value when the
14824 * frame does not pass the check.
14825 **/
14826static int
14827lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14828{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014829 /* make rctl_names static to save stack space */
14830 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014831 char *type_names[] = FC_TYPE_NAMES_INIT;
14832 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014833 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014834
14835 switch (fc_hdr->fh_r_ctl) {
14836 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14837 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14838 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14839 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14840 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14841 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14842 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14843 case FC_RCTL_DD_CMD_STATUS: /* command status */
14844 case FC_RCTL_ELS_REQ: /* extended link services request */
14845 case FC_RCTL_ELS_REP: /* extended link services reply */
14846 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14847 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14848 case FC_RCTL_BA_NOP: /* basic link service NOP */
14849 case FC_RCTL_BA_ABTS: /* basic link service abort */
14850 case FC_RCTL_BA_RMC: /* remove connection */
14851 case FC_RCTL_BA_ACC: /* basic accept */
14852 case FC_RCTL_BA_RJT: /* basic reject */
14853 case FC_RCTL_BA_PRMT:
14854 case FC_RCTL_ACK_1: /* acknowledge_1 */
14855 case FC_RCTL_ACK_0: /* acknowledge_0 */
14856 case FC_RCTL_P_RJT: /* port reject */
14857 case FC_RCTL_F_RJT: /* fabric reject */
14858 case FC_RCTL_P_BSY: /* port busy */
14859 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14860 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14861 case FC_RCTL_LCR: /* link credit reset */
14862 case FC_RCTL_END: /* end */
14863 break;
14864 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14865 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14866 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14867 return lpfc_fc_frame_check(phba, fc_hdr);
14868 default:
14869 goto drop;
14870 }
14871 switch (fc_hdr->fh_type) {
14872 case FC_TYPE_BLS:
14873 case FC_TYPE_ELS:
14874 case FC_TYPE_FCP:
14875 case FC_TYPE_CT:
14876 break;
14877 case FC_TYPE_IP:
14878 case FC_TYPE_ILS:
14879 default:
14880 goto drop;
14881 }
James Smart546fc852011-03-11 16:06:29 -050014882
James Smart4f774512009-05-22 14:52:35 -040014883 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014884 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14885 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14886 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14887 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014888 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14889 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014890 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14891 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014892 return 0;
14893drop:
14894 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14895 "2539 Dropped frame rctl:%s type:%s\n",
14896 rctl_names[fc_hdr->fh_r_ctl],
14897 type_names[fc_hdr->fh_type]);
14898 return 1;
14899}
14900
14901/**
14902 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14903 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14904 *
14905 * This function processes the FC header to retrieve the VFI from the VF
14906 * header, if one exists. This function will return the VFI if one exists
14907 * or 0 if no VSAN Header exists.
14908 **/
14909static uint32_t
14910lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14911{
14912 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14913
14914 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14915 return 0;
14916 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14917}
14918
14919/**
14920 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14921 * @phba: Pointer to the HBA structure to search for the vport on
14922 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14923 * @fcfi: The FC Fabric ID that the frame came from
14924 *
14925 * This function searches the @phba for a vport that matches the content of the
14926 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14927 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14928 * returns the matching vport pointer or NULL if unable to match frame to a
14929 * vport.
14930 **/
14931static struct lpfc_vport *
14932lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14933 uint16_t fcfi)
14934{
14935 struct lpfc_vport **vports;
14936 struct lpfc_vport *vport = NULL;
14937 int i;
14938 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14939 fc_hdr->fh_d_id[1] << 8 |
14940 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014941
James Smartbf086112011-08-21 21:48:13 -040014942 if (did == Fabric_DID)
14943 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014944 if ((phba->pport->fc_flag & FC_PT2PT) &&
14945 !(phba->link_state == LPFC_HBA_READY))
14946 return phba->pport;
14947
James Smart4f774512009-05-22 14:52:35 -040014948 vports = lpfc_create_vport_work_array(phba);
14949 if (vports != NULL)
14950 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14951 if (phba->fcf.fcfi == fcfi &&
14952 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14953 vports[i]->fc_myDID == did) {
14954 vport = vports[i];
14955 break;
14956 }
14957 }
14958 lpfc_destroy_vport_work_array(phba, vports);
14959 return vport;
14960}
14961
14962/**
James Smart45ed1192009-10-02 15:17:02 -040014963 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14964 * @vport: The vport to work on.
14965 *
14966 * This function updates the receive sequence time stamp for this vport. The
14967 * receive sequence time stamp indicates the time that the last frame of the
14968 * the sequence that has been idle for the longest amount of time was received.
14969 * the driver uses this time stamp to indicate if any received sequences have
14970 * timed out.
14971 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014972static void
James Smart45ed1192009-10-02 15:17:02 -040014973lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14974{
14975 struct lpfc_dmabuf *h_buf;
14976 struct hbq_dmabuf *dmabuf = NULL;
14977
14978 /* get the oldest sequence on the rcv list */
14979 h_buf = list_get_first(&vport->rcv_buffer_list,
14980 struct lpfc_dmabuf, list);
14981 if (!h_buf)
14982 return;
14983 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14984 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14985}
14986
14987/**
14988 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14989 * @vport: The vport that the received sequences were sent to.
14990 *
14991 * This function cleans up all outstanding received sequences. This is called
14992 * by the driver when a link event or user action invalidates all the received
14993 * sequences.
14994 **/
14995void
14996lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14997{
14998 struct lpfc_dmabuf *h_buf, *hnext;
14999 struct lpfc_dmabuf *d_buf, *dnext;
15000 struct hbq_dmabuf *dmabuf = NULL;
15001
15002 /* start with the oldest sequence on the rcv list */
15003 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15004 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15005 list_del_init(&dmabuf->hbuf.list);
15006 list_for_each_entry_safe(d_buf, dnext,
15007 &dmabuf->dbuf.list, list) {
15008 list_del_init(&d_buf->list);
15009 lpfc_in_buf_free(vport->phba, d_buf);
15010 }
15011 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15012 }
15013}
15014
15015/**
15016 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
15017 * @vport: The vport that the received sequences were sent to.
15018 *
15019 * This function determines whether any received sequences have timed out by
15020 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15021 * indicates that there is at least one timed out sequence this routine will
15022 * go through the received sequences one at a time from most inactive to most
15023 * active to determine which ones need to be cleaned up. Once it has determined
15024 * that a sequence needs to be cleaned up it will simply free up the resources
15025 * without sending an abort.
15026 **/
15027void
15028lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15029{
15030 struct lpfc_dmabuf *h_buf, *hnext;
15031 struct lpfc_dmabuf *d_buf, *dnext;
15032 struct hbq_dmabuf *dmabuf = NULL;
15033 unsigned long timeout;
15034 int abort_count = 0;
15035
15036 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15037 vport->rcv_buffer_time_stamp);
15038 if (list_empty(&vport->rcv_buffer_list) ||
15039 time_before(jiffies, timeout))
15040 return;
15041 /* start with the oldest sequence on the rcv list */
15042 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15043 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15044 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15045 dmabuf->time_stamp);
15046 if (time_before(jiffies, timeout))
15047 break;
15048 abort_count++;
15049 list_del_init(&dmabuf->hbuf.list);
15050 list_for_each_entry_safe(d_buf, dnext,
15051 &dmabuf->dbuf.list, list) {
15052 list_del_init(&d_buf->list);
15053 lpfc_in_buf_free(vport->phba, d_buf);
15054 }
15055 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15056 }
15057 if (abort_count)
15058 lpfc_update_rcv_time_stamp(vport);
15059}
15060
15061/**
James Smart4f774512009-05-22 14:52:35 -040015062 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15063 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15064 *
15065 * This function searches through the existing incomplete sequences that have
15066 * been sent to this @vport. If the frame matches one of the incomplete
15067 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15068 * make up that sequence. If no sequence is found that matches this frame then
15069 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15070 * This function returns a pointer to the first dmabuf in the sequence list that
15071 * the frame was linked to.
15072 **/
15073static struct hbq_dmabuf *
15074lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15075{
15076 struct fc_frame_header *new_hdr;
15077 struct fc_frame_header *temp_hdr;
15078 struct lpfc_dmabuf *d_buf;
15079 struct lpfc_dmabuf *h_buf;
15080 struct hbq_dmabuf *seq_dmabuf = NULL;
15081 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015082 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015083
James Smart4d9ab992009-10-02 15:16:39 -040015084 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015085 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015086 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015087
James Smart4f774512009-05-22 14:52:35 -040015088 /* Use the hdr_buf to find the sequence that this frame belongs to */
15089 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15090 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15091 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15092 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15093 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15094 continue;
15095 /* found a pending sequence that matches this frame */
15096 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15097 break;
15098 }
15099 if (!seq_dmabuf) {
15100 /*
15101 * This indicates first frame received for this sequence.
15102 * Queue the buffer on the vport's rcv_buffer_list.
15103 */
15104 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015105 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015106 return dmabuf;
15107 }
15108 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015109 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15110 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015111 list_del_init(&seq_dmabuf->hbuf.list);
15112 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15113 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015114 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015115 return dmabuf;
15116 }
James Smart45ed1192009-10-02 15:17:02 -040015117 /* move this sequence to the tail to indicate a young sequence */
15118 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15119 seq_dmabuf->time_stamp = jiffies;
15120 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015121 if (list_empty(&seq_dmabuf->dbuf.list)) {
15122 temp_hdr = dmabuf->hbuf.virt;
15123 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15124 return seq_dmabuf;
15125 }
James Smart4f774512009-05-22 14:52:35 -040015126 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015127 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15128 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015129 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15130 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15131 /*
15132 * If the frame's sequence count is greater than the frame on
15133 * the list then insert the frame right after this frame
15134 */
James Smarteeead812009-12-21 17:01:23 -050015135 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15136 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015137 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015138 found = 1;
15139 break;
James Smart4f774512009-05-22 14:52:35 -040015140 }
James Smart4360ca92015-12-16 18:12:04 -050015141
15142 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15143 break;
15144 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015145 }
James Smart4360ca92015-12-16 18:12:04 -050015146
15147 if (found)
15148 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015149 return NULL;
15150}
15151
15152/**
James Smart6669f9b2009-10-02 15:16:45 -040015153 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15154 * @vport: pointer to a vitural port
15155 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15156 *
15157 * This function tries to abort from the partially assembed sequence, described
15158 * by the information from basic abbort @dmabuf. It checks to see whether such
15159 * partially assembled sequence held by the driver. If so, it shall free up all
15160 * the frames from the partially assembled sequence.
15161 *
15162 * Return
15163 * true -- if there is matching partially assembled sequence present and all
15164 * the frames freed with the sequence;
15165 * false -- if there is no matching partially assembled sequence present so
15166 * nothing got aborted in the lower layer driver
15167 **/
15168static bool
15169lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15170 struct hbq_dmabuf *dmabuf)
15171{
15172 struct fc_frame_header *new_hdr;
15173 struct fc_frame_header *temp_hdr;
15174 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15175 struct hbq_dmabuf *seq_dmabuf = NULL;
15176
15177 /* Use the hdr_buf to find the sequence that matches this frame */
15178 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15179 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15180 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15181 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15182 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15183 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15184 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15185 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15186 continue;
15187 /* found a pending sequence that matches this frame */
15188 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15189 break;
15190 }
15191
15192 /* Free up all the frames from the partially assembled sequence */
15193 if (seq_dmabuf) {
15194 list_for_each_entry_safe(d_buf, n_buf,
15195 &seq_dmabuf->dbuf.list, list) {
15196 list_del_init(&d_buf->list);
15197 lpfc_in_buf_free(vport->phba, d_buf);
15198 }
15199 return true;
15200 }
15201 return false;
15202}
15203
15204/**
James Smart6dd9e312013-01-03 15:43:37 -050015205 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15206 * @vport: pointer to a vitural port
15207 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15208 *
15209 * This function tries to abort from the assembed sequence from upper level
15210 * protocol, described by the information from basic abbort @dmabuf. It
15211 * checks to see whether such pending context exists at upper level protocol.
15212 * If so, it shall clean up the pending context.
15213 *
15214 * Return
15215 * true -- if there is matching pending context of the sequence cleaned
15216 * at ulp;
15217 * false -- if there is no matching pending context of the sequence present
15218 * at ulp.
15219 **/
15220static bool
15221lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15222{
15223 struct lpfc_hba *phba = vport->phba;
15224 int handled;
15225
15226 /* Accepting abort at ulp with SLI4 only */
15227 if (phba->sli_rev < LPFC_SLI_REV4)
15228 return false;
15229
15230 /* Register all caring upper level protocols to attend abort */
15231 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15232 if (handled)
15233 return true;
15234
15235 return false;
15236}
15237
15238/**
James Smart546fc852011-03-11 16:06:29 -050015239 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015240 * @phba: Pointer to HBA context object.
15241 * @cmd_iocbq: pointer to the command iocbq structure.
15242 * @rsp_iocbq: pointer to the response iocbq structure.
15243 *
James Smart546fc852011-03-11 16:06:29 -050015244 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015245 * event. It properly releases the memory allocated to the sequence abort
15246 * accept iocb.
15247 **/
15248static void
James Smart546fc852011-03-11 16:06:29 -050015249lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015250 struct lpfc_iocbq *cmd_iocbq,
15251 struct lpfc_iocbq *rsp_iocbq)
15252{
James Smart6dd9e312013-01-03 15:43:37 -050015253 struct lpfc_nodelist *ndlp;
15254
15255 if (cmd_iocbq) {
15256 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15257 lpfc_nlp_put(ndlp);
15258 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015259 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015260 }
James Smart6b5151f2012-01-18 16:24:06 -050015261
15262 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15263 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15264 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15265 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15266 rsp_iocbq->iocb.ulpStatus,
15267 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015268}
15269
15270/**
James Smart6d368e52011-05-24 11:44:12 -040015271 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15272 * @phba: Pointer to HBA context object.
15273 * @xri: xri id in transaction.
15274 *
15275 * This function validates the xri maps to the known range of XRIs allocated an
15276 * used by the driver.
15277 **/
James Smart7851fe22011-07-22 18:36:52 -040015278uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015279lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15280 uint16_t xri)
15281{
James Smarta2fc4aef2014-09-03 12:57:55 -040015282 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015283
15284 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15285 if (xri == phba->sli4_hba.xri_ids[i])
15286 return i;
15287 }
15288 return NO_XRI;
15289}
15290
James Smart6d368e52011-05-24 11:44:12 -040015291/**
James Smart546fc852011-03-11 16:06:29 -050015292 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015293 * @phba: Pointer to HBA context object.
15294 * @fc_hdr: pointer to a FC frame header.
15295 *
James Smart546fc852011-03-11 16:06:29 -050015296 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015297 * event after aborting the sequence handling.
15298 **/
15299static void
James Smart6dd9e312013-01-03 15:43:37 -050015300lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15301 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015302{
James Smart6dd9e312013-01-03 15:43:37 -050015303 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015304 struct lpfc_iocbq *ctiocb = NULL;
15305 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015306 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015307 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015308 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015309 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015310
15311 if (!lpfc_is_link_up(phba))
15312 return;
15313
15314 sid = sli4_sid_from_fc_hdr(fc_hdr);
15315 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015316 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015317
James Smart6dd9e312013-01-03 15:43:37 -050015318 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015319 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015320 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15321 if (!ndlp) {
15322 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15323 "1268 Failed to allocate ndlp for "
15324 "oxid:x%x SID:x%x\n", oxid, sid);
15325 return;
15326 }
15327 lpfc_nlp_init(vport, ndlp, sid);
15328 /* Put ndlp onto pport node list */
15329 lpfc_enqueue_node(vport, ndlp);
15330 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15331 /* re-setup ndlp without removing from node list */
15332 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15333 if (!ndlp) {
15334 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15335 "3275 Failed to active ndlp found "
15336 "for oxid:x%x SID:x%x\n", oxid, sid);
15337 return;
15338 }
James Smart6669f9b2009-10-02 15:16:45 -040015339 }
15340
James Smart546fc852011-03-11 16:06:29 -050015341 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015342 ctiocb = lpfc_sli_get_iocbq(phba);
15343 if (!ctiocb)
15344 return;
15345
James Smart5ffc2662009-11-18 15:39:44 -050015346 /* Extract the F_CTL field from FC_HDR */
15347 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15348
James Smart6669f9b2009-10-02 15:16:45 -040015349 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015350 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015351 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015352 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15353 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15354 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15355
15356 /* Fill in the rest of iocb fields */
15357 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15358 icmd->ulpBdeCount = 0;
15359 icmd->ulpLe = 1;
15360 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015361 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015362 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015363
James Smart6669f9b2009-10-02 15:16:45 -040015364 ctiocb->iocb_cmpl = NULL;
15365 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015366 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015367 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015368 ctiocb->sli4_xritag = NO_XRI;
15369
James Smartee0f4fe2012-05-09 21:19:14 -040015370 if (fctl & FC_FC_EX_CTX)
15371 /* Exchange responder sent the abort so we
15372 * own the oxid.
15373 */
15374 xri = oxid;
15375 else
15376 xri = rxid;
15377 lxri = lpfc_sli4_xri_inrange(phba, xri);
15378 if (lxri != NO_XRI)
15379 lpfc_set_rrq_active(phba, ndlp, lxri,
15380 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015381 /* For BA_ABTS from exchange responder, if the logical xri with
15382 * the oxid maps to the FCP XRI range, the port no longer has
15383 * that exchange context, send a BLS_RJT. Override the IOCB for
15384 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015385 */
James Smart6dd9e312013-01-03 15:43:37 -050015386 if ((fctl & FC_FC_EX_CTX) &&
15387 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15388 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15389 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15390 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15391 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15392 }
15393
15394 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15395 * the driver no longer has that exchange, send a BLS_RJT. Override
15396 * the IOCB for a BA_RJT.
15397 */
15398 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015399 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15400 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15401 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15402 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15403 }
James Smart6669f9b2009-10-02 15:16:45 -040015404
James Smart5ffc2662009-11-18 15:39:44 -050015405 if (fctl & FC_FC_EX_CTX) {
15406 /* ABTS sent by responder to CT exchange, construction
15407 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15408 * field and RX_ID from ABTS for RX_ID field.
15409 */
James Smart546fc852011-03-11 16:06:29 -050015410 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015411 } else {
15412 /* ABTS sent by initiator to CT exchange, construction
15413 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015414 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015415 */
James Smart546fc852011-03-11 16:06:29 -050015416 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015417 }
James Smartf09c3ac2012-03-01 22:33:29 -050015418 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015419 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015420
James Smart546fc852011-03-11 16:06:29 -050015421 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015422 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15423 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15424 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015425
15426 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15427 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015428 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15429 "2925 Failed to issue CT ABTS RSP x%x on "
15430 "xri x%x, Data x%x\n",
15431 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15432 phba->link_state);
15433 lpfc_nlp_put(ndlp);
15434 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015435 lpfc_sli_release_iocbq(phba, ctiocb);
15436 }
James Smart6669f9b2009-10-02 15:16:45 -040015437}
15438
15439/**
15440 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15441 * @vport: Pointer to the vport on which this sequence was received
15442 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15443 *
15444 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15445 * receive sequence is only partially assembed by the driver, it shall abort
15446 * the partially assembled frames for the sequence. Otherwise, if the
15447 * unsolicited receive sequence has been completely assembled and passed to
15448 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15449 * unsolicited sequence has been aborted. After that, it will issue a basic
15450 * accept to accept the abort.
15451 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015452static void
James Smart6669f9b2009-10-02 15:16:45 -040015453lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15454 struct hbq_dmabuf *dmabuf)
15455{
15456 struct lpfc_hba *phba = vport->phba;
15457 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015458 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015459 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015460
James Smart6669f9b2009-10-02 15:16:45 -040015461 /* Make a copy of fc_hdr before the dmabuf being released */
15462 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015463 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015464
James Smart5ffc2662009-11-18 15:39:44 -050015465 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015466 /* ABTS by responder to exchange, no cleanup needed */
15467 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015468 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015469 /* ABTS by initiator to exchange, need to do cleanup */
15470 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15471 if (aborted == false)
15472 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015473 }
James Smart6dd9e312013-01-03 15:43:37 -050015474 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15475
15476 /* Respond with BA_ACC or BA_RJT accordingly */
15477 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015478}
15479
15480/**
James Smart4f774512009-05-22 14:52:35 -040015481 * lpfc_seq_complete - Indicates if a sequence is complete
15482 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15483 *
15484 * This function checks the sequence, starting with the frame described by
15485 * @dmabuf, to see if all the frames associated with this sequence are present.
15486 * the frames associated with this sequence are linked to the @dmabuf using the
15487 * dbuf list. This function looks for two major things. 1) That the first frame
15488 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15489 * set. 3) That there are no holes in the sequence count. The function will
15490 * return 1 when the sequence is complete, otherwise it will return 0.
15491 **/
15492static int
15493lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15494{
15495 struct fc_frame_header *hdr;
15496 struct lpfc_dmabuf *d_buf;
15497 struct hbq_dmabuf *seq_dmabuf;
15498 uint32_t fctl;
15499 int seq_count = 0;
15500
15501 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15502 /* make sure first fame of sequence has a sequence count of zero */
15503 if (hdr->fh_seq_cnt != seq_count)
15504 return 0;
15505 fctl = (hdr->fh_f_ctl[0] << 16 |
15506 hdr->fh_f_ctl[1] << 8 |
15507 hdr->fh_f_ctl[2]);
15508 /* If last frame of sequence we can return success. */
15509 if (fctl & FC_FC_END_SEQ)
15510 return 1;
15511 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15512 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15513 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15514 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015515 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015516 return 0;
15517 fctl = (hdr->fh_f_ctl[0] << 16 |
15518 hdr->fh_f_ctl[1] << 8 |
15519 hdr->fh_f_ctl[2]);
15520 /* If last frame of sequence we can return success. */
15521 if (fctl & FC_FC_END_SEQ)
15522 return 1;
15523 }
15524 return 0;
15525}
15526
15527/**
15528 * lpfc_prep_seq - Prep sequence for ULP processing
15529 * @vport: Pointer to the vport on which this sequence was received
15530 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15531 *
15532 * This function takes a sequence, described by a list of frames, and creates
15533 * a list of iocbq structures to describe the sequence. This iocbq list will be
15534 * used to issue to the generic unsolicited sequence handler. This routine
15535 * returns a pointer to the first iocbq in the list. If the function is unable
15536 * to allocate an iocbq then it throw out the received frames that were not
15537 * able to be described and return a pointer to the first iocbq. If unable to
15538 * allocate any iocbqs (including the first) this function will return NULL.
15539 **/
15540static struct lpfc_iocbq *
15541lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15542{
James Smart7851fe22011-07-22 18:36:52 -040015543 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015544 struct lpfc_dmabuf *d_buf, *n_buf;
15545 struct lpfc_iocbq *first_iocbq, *iocbq;
15546 struct fc_frame_header *fc_hdr;
15547 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015548 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015549 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015550
15551 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15552 /* remove from receive buffer list */
15553 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015554 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015555 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015556 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015557 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015558 /* Get an iocbq struct to fill in. */
15559 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15560 if (first_iocbq) {
15561 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015562 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015563 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015564
15565 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15566 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15567 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15568 first_iocbq->iocb.un.rcvels.parmRo =
15569 sli4_did_from_fc_hdr(fc_hdr);
15570 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15571 } else
15572 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015573 first_iocbq->iocb.ulpContext = NO_XRI;
15574 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15575 be16_to_cpu(fc_hdr->fh_ox_id);
15576 /* iocbq is prepped for internal consumption. Physical vpi. */
15577 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15578 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015579 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015580 tot_len = bf_get(lpfc_rcqe_length,
15581 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15582
James Smart4f774512009-05-22 14:52:35 -040015583 first_iocbq->context2 = &seq_dmabuf->dbuf;
15584 first_iocbq->context3 = NULL;
15585 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015586 if (tot_len > LPFC_DATA_BUF_SIZE)
15587 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015588 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015589 else
15590 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15591
James Smart4f774512009-05-22 14:52:35 -040015592 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015593
James Smart7851fe22011-07-22 18:36:52 -040015594 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015595 }
15596 iocbq = first_iocbq;
15597 /*
15598 * Each IOCBq can have two Buffers assigned, so go through the list
15599 * of buffers for this sequence and save two buffers in each IOCBq
15600 */
15601 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15602 if (!iocbq) {
15603 lpfc_in_buf_free(vport->phba, d_buf);
15604 continue;
15605 }
15606 if (!iocbq->context3) {
15607 iocbq->context3 = d_buf;
15608 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015609 /* We need to get the size out of the right CQE */
15610 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15611 len = bf_get(lpfc_rcqe_length,
15612 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015613 pbde = (struct ulp_bde64 *)
15614 &iocbq->iocb.unsli3.sli3Words[4];
15615 if (len > LPFC_DATA_BUF_SIZE)
15616 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15617 else
15618 pbde->tus.f.bdeSize = len;
15619
James Smart7851fe22011-07-22 18:36:52 -040015620 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15621 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015622 } else {
15623 iocbq = lpfc_sli_get_iocbq(vport->phba);
15624 if (!iocbq) {
15625 if (first_iocbq) {
15626 first_iocbq->iocb.ulpStatus =
15627 IOSTAT_FCP_RSP_ERROR;
15628 first_iocbq->iocb.un.ulpWord[4] =
15629 IOERR_NO_RESOURCES;
15630 }
15631 lpfc_in_buf_free(vport->phba, d_buf);
15632 continue;
15633 }
James Smart7851fe22011-07-22 18:36:52 -040015634 /* We need to get the size out of the right CQE */
15635 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15636 len = bf_get(lpfc_rcqe_length,
15637 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015638 iocbq->context2 = d_buf;
15639 iocbq->context3 = NULL;
15640 iocbq->iocb.ulpBdeCount = 1;
15641 if (len > LPFC_DATA_BUF_SIZE)
15642 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15643 LPFC_DATA_BUF_SIZE;
15644 else
15645 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15646
James Smart7851fe22011-07-22 18:36:52 -040015647 tot_len += len;
15648 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15649
James Smart4f774512009-05-22 14:52:35 -040015650 iocbq->iocb.un.rcvels.remoteID = sid;
15651 list_add_tail(&iocbq->list, &first_iocbq->list);
15652 }
15653 }
15654 return first_iocbq;
15655}
15656
James Smart6669f9b2009-10-02 15:16:45 -040015657static void
15658lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15659 struct hbq_dmabuf *seq_dmabuf)
15660{
15661 struct fc_frame_header *fc_hdr;
15662 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15663 struct lpfc_hba *phba = vport->phba;
15664
15665 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15666 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15667 if (!iocbq) {
15668 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15669 "2707 Ring %d handler: Failed to allocate "
15670 "iocb Rctl x%x Type x%x received\n",
15671 LPFC_ELS_RING,
15672 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15673 return;
15674 }
15675 if (!lpfc_complete_unsol_iocb(phba,
15676 &phba->sli.ring[LPFC_ELS_RING],
15677 iocbq, fc_hdr->fh_r_ctl,
15678 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015680 "2540 Ring %d handler: unexpected Rctl "
15681 "x%x Type x%x received\n",
15682 LPFC_ELS_RING,
15683 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15684
15685 /* Free iocb created in lpfc_prep_seq */
15686 list_for_each_entry_safe(curr_iocb, next_iocb,
15687 &iocbq->list, list) {
15688 list_del_init(&curr_iocb->list);
15689 lpfc_sli_release_iocbq(phba, curr_iocb);
15690 }
15691 lpfc_sli_release_iocbq(phba, iocbq);
15692}
15693
James Smart4f774512009-05-22 14:52:35 -040015694/**
15695 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15696 * @phba: Pointer to HBA context object.
15697 *
15698 * This function is called with no lock held. This function processes all
15699 * the received buffers and gives it to upper layers when a received buffer
15700 * indicates that it is the final frame in the sequence. The interrupt
15701 * service routine processes received buffers at interrupt contexts and adds
15702 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15703 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15704 * appropriate receive function when the final frame in a sequence is received.
15705 **/
James Smart4d9ab992009-10-02 15:16:39 -040015706void
15707lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15708 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015709{
James Smart4d9ab992009-10-02 15:16:39 -040015710 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015711 struct fc_frame_header *fc_hdr;
15712 struct lpfc_vport *vport;
15713 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015714 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015715
James Smart4f774512009-05-22 14:52:35 -040015716 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015717 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15718 /* check to see if this a valid type of frame */
15719 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15720 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15721 return;
15722 }
James Smart7851fe22011-07-22 18:36:52 -040015723 if ((bf_get(lpfc_cqe_code,
15724 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15725 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15726 &dmabuf->cq_event.cqe.rcqe_cmpl);
15727 else
15728 fcfi = bf_get(lpfc_rcqe_fcf_id,
15729 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015730
James Smart4d9ab992009-10-02 15:16:39 -040015731 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015732 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015733 /* throw out the frame */
15734 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15735 return;
15736 }
James Smart939723a2012-05-09 21:19:03 -040015737
15738 /* d_id this frame is directed to */
15739 did = sli4_did_from_fc_hdr(fc_hdr);
15740
15741 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15742 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15743 (did != Fabric_DID)) {
15744 /*
15745 * Throw out the frame if we are not pt2pt.
15746 * The pt2pt protocol allows for discovery frames
15747 * to be received without a registered VPI.
15748 */
15749 if (!(vport->fc_flag & FC_PT2PT) ||
15750 (phba->link_state == LPFC_HBA_READY)) {
15751 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15752 return;
15753 }
15754 }
15755
James Smart6669f9b2009-10-02 15:16:45 -040015756 /* Handle the basic abort sequence (BA_ABTS) event */
15757 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15758 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15759 return;
15760 }
15761
James Smart4d9ab992009-10-02 15:16:39 -040015762 /* Link this frame */
15763 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15764 if (!seq_dmabuf) {
15765 /* unable to add frame to vport - throw it out */
15766 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15767 return;
15768 }
15769 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015770 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015771 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015772
James Smart6669f9b2009-10-02 15:16:45 -040015773 /* Send the complete sequence to the upper layer protocol */
15774 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015775}
James Smart6fb120a2009-05-22 14:52:59 -040015776
15777/**
15778 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15779 * @phba: pointer to lpfc hba data structure.
15780 *
15781 * This routine is invoked to post rpi header templates to the
15782 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015783 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15784 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015785 *
15786 * This routine does not require any locks. It's usage is expected
15787 * to be driver load or reset recovery when the driver is
15788 * sequential.
15789 *
15790 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015791 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015792 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015793 * When this error occurs, the driver is not guaranteed
15794 * to have any rpi regions posted to the device and
15795 * must either attempt to repost the regions or take a
15796 * fatal error.
15797 **/
15798int
15799lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15800{
15801 struct lpfc_rpi_hdr *rpi_page;
15802 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015803 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015804
James Smart6d368e52011-05-24 11:44:12 -040015805 /* SLI4 ports that support extents do not require RPI headers. */
15806 if (!phba->sli4_hba.rpi_hdrs_in_use)
15807 goto exit;
15808 if (phba->sli4_hba.extents_in_use)
15809 return -EIO;
15810
James Smart6fb120a2009-05-22 14:52:59 -040015811 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015812 /*
15813 * Assign the rpi headers a physical rpi only if the driver
15814 * has not initialized those resources. A port reset only
15815 * needs the headers posted.
15816 */
15817 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15818 LPFC_RPI_RSRC_RDY)
15819 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15820
James Smart6fb120a2009-05-22 14:52:59 -040015821 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15822 if (rc != MBX_SUCCESS) {
15823 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15824 "2008 Error %d posting all rpi "
15825 "headers\n", rc);
15826 rc = -EIO;
15827 break;
15828 }
15829 }
15830
James Smart6d368e52011-05-24 11:44:12 -040015831 exit:
15832 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15833 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015834 return rc;
15835}
15836
15837/**
15838 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15839 * @phba: pointer to lpfc hba data structure.
15840 * @rpi_page: pointer to the rpi memory region.
15841 *
15842 * This routine is invoked to post a single rpi header to the
15843 * HBA consistent with the SLI-4 interface spec. This memory region
15844 * maps up to 64 rpi context regions.
15845 *
15846 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015847 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015848 * -ENOMEM - No available memory
15849 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015850 **/
15851int
15852lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15853{
15854 LPFC_MBOXQ_t *mboxq;
15855 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15856 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015857 uint32_t shdr_status, shdr_add_status;
15858 union lpfc_sli4_cfg_shdr *shdr;
15859
James Smart6d368e52011-05-24 11:44:12 -040015860 /* SLI4 ports that support extents do not require RPI headers. */
15861 if (!phba->sli4_hba.rpi_hdrs_in_use)
15862 return rc;
15863 if (phba->sli4_hba.extents_in_use)
15864 return -EIO;
15865
James Smart6fb120a2009-05-22 14:52:59 -040015866 /* The port is notified of the header region via a mailbox command. */
15867 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15868 if (!mboxq) {
15869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15870 "2001 Unable to allocate memory for issuing "
15871 "SLI_CONFIG_SPECIAL mailbox command\n");
15872 return -ENOMEM;
15873 }
15874
15875 /* Post all rpi memory regions to the port. */
15876 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015877 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15878 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15879 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015880 sizeof(struct lpfc_sli4_cfg_mhdr),
15881 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015882
15883
15884 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015885 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15886 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015887 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15888 hdr_tmpl, rpi_page->page_count);
15889
James Smart6fb120a2009-05-22 14:52:59 -040015890 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15891 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015892 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015893 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15894 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15895 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15896 if (rc != MBX_TIMEOUT)
15897 mempool_free(mboxq, phba->mbox_mem_pool);
15898 if (shdr_status || shdr_add_status || rc) {
15899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15900 "2514 POST_RPI_HDR mailbox failed with "
15901 "status x%x add_status x%x, mbx status x%x\n",
15902 shdr_status, shdr_add_status, rc);
15903 rc = -ENXIO;
15904 }
15905 return rc;
15906}
15907
15908/**
15909 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15910 * @phba: pointer to lpfc hba data structure.
15911 *
15912 * This routine is invoked to post rpi header templates to the
15913 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015914 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15915 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015916 *
15917 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015918 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015919 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15920 **/
15921int
15922lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15923{
James Smart6d368e52011-05-24 11:44:12 -040015924 unsigned long rpi;
15925 uint16_t max_rpi, rpi_limit;
15926 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015927 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015928 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015929
James Smart6fb120a2009-05-22 14:52:59 -040015930 /*
James Smart6d368e52011-05-24 11:44:12 -040015931 * Fetch the next logical rpi. Because this index is logical,
15932 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015933 */
James Smart4902b382013-10-10 12:20:35 -040015934 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015935 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15936 rpi_limit = phba->sli4_hba.next_rpi;
15937
James Smart6d368e52011-05-24 11:44:12 -040015938 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15939 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015940 rpi = LPFC_RPI_ALLOC_ERROR;
15941 else {
15942 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15943 phba->sli4_hba.max_cfg_param.rpi_used++;
15944 phba->sli4_hba.rpi_count++;
15945 }
James Smartbe6bb942015-04-07 15:07:22 -040015946 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15947 "0001 rpi:%x max:%x lim:%x\n",
15948 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015949
15950 /*
15951 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015952 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015953 */
15954 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15955 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015956 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015957 return rpi;
15958 }
15959
15960 /*
James Smart6d368e52011-05-24 11:44:12 -040015961 * RPI header postings are not required for SLI4 ports capable of
15962 * extents.
15963 */
15964 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015965 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015966 return rpi;
15967 }
15968
15969 /*
James Smart6fb120a2009-05-22 14:52:59 -040015970 * If the driver is running low on rpi resources, allocate another
15971 * page now. Note that the next_rpi value is used because
15972 * it represents how many are actually in use whereas max_rpi notes
15973 * how many are supported max by the device.
15974 */
James Smart6d368e52011-05-24 11:44:12 -040015975 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015976 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015977 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15978 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15979 if (!rpi_hdr) {
15980 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15981 "2002 Error Could not grow rpi "
15982 "count\n");
15983 } else {
James Smart6d368e52011-05-24 11:44:12 -040015984 lrpi = rpi_hdr->start_rpi;
15985 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015986 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15987 }
15988 }
15989
15990 return rpi;
15991}
15992
15993/**
15994 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15995 * @phba: pointer to lpfc hba data structure.
15996 *
15997 * This routine is invoked to release an rpi to the pool of
15998 * available rpis maintained by the driver.
15999 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016000static void
James Smartd7c47992010-06-08 18:31:54 -040016001__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16002{
16003 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
16004 phba->sli4_hba.rpi_count--;
16005 phba->sli4_hba.max_cfg_param.rpi_used--;
16006 }
16007}
16008
16009/**
16010 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16011 * @phba: pointer to lpfc hba data structure.
16012 *
16013 * This routine is invoked to release an rpi to the pool of
16014 * available rpis maintained by the driver.
16015 **/
16016void
James Smart6fb120a2009-05-22 14:52:59 -040016017lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16018{
16019 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040016020 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040016021 spin_unlock_irq(&phba->hbalock);
16022}
16023
16024/**
16025 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16026 * @phba: pointer to lpfc hba data structure.
16027 *
16028 * This routine is invoked to remove the memory region that
16029 * provided rpi via a bitmask.
16030 **/
16031void
16032lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16033{
16034 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040016035 kfree(phba->sli4_hba.rpi_ids);
16036 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040016037}
16038
16039/**
16040 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16041 * @phba: pointer to lpfc hba data structure.
16042 *
16043 * This routine is invoked to remove the memory region that
16044 * provided rpi via a bitmask.
16045 **/
16046int
James Smart6b5151f2012-01-18 16:24:06 -050016047lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16048 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016049{
16050 LPFC_MBOXQ_t *mboxq;
16051 struct lpfc_hba *phba = ndlp->phba;
16052 int rc;
16053
16054 /* The port is notified of the header region via a mailbox command. */
16055 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16056 if (!mboxq)
16057 return -ENOMEM;
16058
16059 /* Post all rpi memory regions to the port. */
16060 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016061 if (cmpl) {
16062 mboxq->mbox_cmpl = cmpl;
16063 mboxq->context1 = arg;
16064 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016065 } else
16066 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016067 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016068 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16069 if (rc == MBX_NOT_FINISHED) {
16070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16071 "2010 Resume RPI Mailbox failed "
16072 "status %d, mbxStatus x%x\n", rc,
16073 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16074 mempool_free(mboxq, phba->mbox_mem_pool);
16075 return -EIO;
16076 }
16077 return 0;
16078}
16079
16080/**
16081 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016082 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016083 *
James Smart76a95d72010-11-20 23:11:48 -050016084 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016085 *
16086 * Returns:
16087 * 0 success
16088 * -Evalue otherwise
16089 **/
16090int
James Smart76a95d72010-11-20 23:11:48 -050016091lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016092{
16093 LPFC_MBOXQ_t *mboxq;
16094 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016095 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016096 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016097 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016098 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16099 if (!mboxq)
16100 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016101 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016102 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016103 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016104 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016105 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016106 "2022 INIT VPI Mailbox failed "
16107 "status %d, mbxStatus x%x\n", rc,
16108 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016109 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016110 }
James Smart6a9c52c2009-10-02 15:16:51 -040016111 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016112 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016113
16114 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016115}
16116
16117/**
16118 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16119 * @phba: pointer to lpfc hba data structure.
16120 * @mboxq: Pointer to mailbox object.
16121 *
16122 * This routine is invoked to manually add a single FCF record. The caller
16123 * must pass a completely initialized FCF_Record. This routine takes
16124 * care of the nonembedded mailbox operations.
16125 **/
16126static void
16127lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16128{
16129 void *virt_addr;
16130 union lpfc_sli4_cfg_shdr *shdr;
16131 uint32_t shdr_status, shdr_add_status;
16132
16133 virt_addr = mboxq->sge_array->addr[0];
16134 /* The IOCTL status is embedded in the mailbox subheader. */
16135 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16136 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16137 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16138
16139 if ((shdr_status || shdr_add_status) &&
16140 (shdr_status != STATUS_FCF_IN_USE))
16141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16142 "2558 ADD_FCF_RECORD mailbox failed with "
16143 "status x%x add_status x%x\n",
16144 shdr_status, shdr_add_status);
16145
16146 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16147}
16148
16149/**
16150 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16151 * @phba: pointer to lpfc hba data structure.
16152 * @fcf_record: pointer to the initialized fcf record to add.
16153 *
16154 * This routine is invoked to manually add a single FCF record. The caller
16155 * must pass a completely initialized FCF_Record. This routine takes
16156 * care of the nonembedded mailbox operations.
16157 **/
16158int
16159lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16160{
16161 int rc = 0;
16162 LPFC_MBOXQ_t *mboxq;
16163 uint8_t *bytep;
16164 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016165 struct lpfc_mbx_sge sge;
16166 uint32_t alloc_len, req_len;
16167 uint32_t fcfindex;
16168
16169 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16170 if (!mboxq) {
16171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16172 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16173 return -ENOMEM;
16174 }
16175
16176 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16177 sizeof(uint32_t);
16178
16179 /* Allocate DMA memory and set up the non-embedded mailbox command */
16180 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16181 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16182 req_len, LPFC_SLI4_MBX_NEMBED);
16183 if (alloc_len < req_len) {
16184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16185 "2523 Allocated DMA memory size (x%x) is "
16186 "less than the requested DMA memory "
16187 "size (x%x)\n", alloc_len, req_len);
16188 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16189 return -ENOMEM;
16190 }
16191
16192 /*
16193 * Get the first SGE entry from the non-embedded DMA memory. This
16194 * routine only uses a single SGE.
16195 */
16196 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016197 virt_addr = mboxq->sge_array->addr[0];
16198 /*
16199 * Configure the FCF record for FCFI 0. This is the driver's
16200 * hardcoded default and gets used in nonFIP mode.
16201 */
16202 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16203 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16204 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16205
16206 /*
16207 * Copy the fcf_index and the FCF Record Data. The data starts after
16208 * the FCoE header plus word10. The data copy needs to be endian
16209 * correct.
16210 */
16211 bytep += sizeof(uint32_t);
16212 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16213 mboxq->vport = phba->pport;
16214 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16215 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16216 if (rc == MBX_NOT_FINISHED) {
16217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16218 "2515 ADD_FCF_RECORD mailbox failed with "
16219 "status 0x%x\n", rc);
16220 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16221 rc = -EIO;
16222 } else
16223 rc = 0;
16224
16225 return rc;
16226}
16227
16228/**
16229 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16230 * @phba: pointer to lpfc hba data structure.
16231 * @fcf_record: pointer to the fcf record to write the default data.
16232 * @fcf_index: FCF table entry index.
16233 *
16234 * This routine is invoked to build the driver's default FCF record. The
16235 * values used are hardcoded. This routine handles memory initialization.
16236 *
16237 **/
16238void
16239lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16240 struct fcf_record *fcf_record,
16241 uint16_t fcf_index)
16242{
16243 memset(fcf_record, 0, sizeof(struct fcf_record));
16244 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16245 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16246 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16247 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16248 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16249 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16250 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16251 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16252 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16253 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16254 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16255 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16256 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016257 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016258 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16259 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16260 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16261 /* Set the VLAN bit map */
16262 if (phba->valid_vlan) {
16263 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16264 = 1 << (phba->vlan_id % 8);
16265 }
16266}
16267
16268/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016269 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016270 * @phba: pointer to lpfc hba data structure.
16271 * @fcf_index: FCF table entry offset.
16272 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016273 * This routine is invoked to scan the entire FCF table by reading FCF
16274 * record and processing it one at a time starting from the @fcf_index
16275 * for initial FCF discovery or fast FCF failover rediscovery.
16276 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016277 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016278 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016279 **/
16280int
James Smart0c9ab6f2010-02-26 14:15:57 -050016281lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016282{
16283 int rc = 0, error;
16284 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016285
James Smart32b97932009-07-19 10:01:21 -040016286 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016287 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016288 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16289 if (!mboxq) {
16290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16291 "2000 Failed to allocate mbox for "
16292 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016293 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016294 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016295 }
James Smartecfd03c2010-02-12 14:41:27 -050016296 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016297 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016298 if (rc) {
16299 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016300 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016301 }
James Smartecfd03c2010-02-12 14:41:27 -050016302 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016303 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016304 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016305
16306 spin_lock_irq(&phba->hbalock);
16307 phba->hba_flag |= FCF_TS_INPROG;
16308 spin_unlock_irq(&phba->hbalock);
16309
James Smart6fb120a2009-05-22 14:52:59 -040016310 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016311 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016312 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016313 else {
James Smart38b92ef2010-08-04 16:11:39 -040016314 /* Reset eligible FCF count for new scan */
16315 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016316 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016317 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016318 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016319fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016320 if (error) {
16321 if (mboxq)
16322 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016323 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016324 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016325 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016326 spin_unlock_irq(&phba->hbalock);
16327 }
James Smart6fb120a2009-05-22 14:52:59 -040016328 return error;
16329}
James Smarta0c87cb2009-07-19 10:01:10 -040016330
16331/**
James Smarta93ff372010-10-22 11:06:08 -040016332 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016333 * @phba: pointer to lpfc hba data structure.
16334 * @fcf_index: FCF table entry offset.
16335 *
16336 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016337 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016338 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016339 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016340 * otherwise.
16341 **/
16342int
16343lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16344{
16345 int rc = 0, error;
16346 LPFC_MBOXQ_t *mboxq;
16347
16348 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16349 if (!mboxq) {
16350 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16351 "2763 Failed to allocate mbox for "
16352 "READ_FCF cmd\n");
16353 error = -ENOMEM;
16354 goto fail_fcf_read;
16355 }
16356 /* Construct the read FCF record mailbox command */
16357 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16358 if (rc) {
16359 error = -EINVAL;
16360 goto fail_fcf_read;
16361 }
16362 /* Issue the mailbox command asynchronously */
16363 mboxq->vport = phba->pport;
16364 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16365 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16366 if (rc == MBX_NOT_FINISHED)
16367 error = -EIO;
16368 else
16369 error = 0;
16370
16371fail_fcf_read:
16372 if (error && mboxq)
16373 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16374 return error;
16375}
16376
16377/**
16378 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16379 * @phba: pointer to lpfc hba data structure.
16380 * @fcf_index: FCF table entry offset.
16381 *
16382 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016383 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016384 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016385 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016386 * otherwise.
16387 **/
16388int
16389lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16390{
16391 int rc = 0, error;
16392 LPFC_MBOXQ_t *mboxq;
16393
16394 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16395 if (!mboxq) {
16396 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16397 "2758 Failed to allocate mbox for "
16398 "READ_FCF cmd\n");
16399 error = -ENOMEM;
16400 goto fail_fcf_read;
16401 }
16402 /* Construct the read FCF record mailbox command */
16403 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16404 if (rc) {
16405 error = -EINVAL;
16406 goto fail_fcf_read;
16407 }
16408 /* Issue the mailbox command asynchronously */
16409 mboxq->vport = phba->pport;
16410 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16411 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16412 if (rc == MBX_NOT_FINISHED)
16413 error = -EIO;
16414 else
16415 error = 0;
16416
16417fail_fcf_read:
16418 if (error && mboxq)
16419 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16420 return error;
16421}
16422
16423/**
James Smartf5cb5302015-12-16 18:11:52 -050016424 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016425 * phba pointer to the lpfc_hba struct for this port.
16426 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16427 * routine when the rr_bmask is empty. The FCF indecies are put into the
16428 * rr_bmask based on their priority level. Starting from the highest priority
16429 * to the lowest. The most likely FCF candidate will be in the highest
16430 * priority group. When this routine is called it searches the fcf_pri list for
16431 * next lowest priority group and repopulates the rr_bmask with only those
16432 * fcf_indexes.
16433 * returns:
16434 * 1=success 0=failure
16435 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016436static int
James Smart7d791df2011-07-22 18:37:52 -040016437lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16438{
16439 uint16_t next_fcf_pri;
16440 uint16_t last_index;
16441 struct lpfc_fcf_pri *fcf_pri;
16442 int rc;
16443 int ret = 0;
16444
16445 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16446 LPFC_SLI4_FCF_TBL_INDX_MAX);
16447 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16448 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016449
16450 /* Verify the priority list has 2 or more entries */
16451 spin_lock_irq(&phba->hbalock);
16452 if (list_empty(&phba->fcf.fcf_pri_list) ||
16453 list_is_singular(&phba->fcf.fcf_pri_list)) {
16454 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016455 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16456 "3061 Last IDX %d\n", last_index);
16457 return 0; /* Empty rr list */
16458 }
James Smart25626692013-03-01 16:36:54 -050016459 spin_unlock_irq(&phba->hbalock);
16460
James Smart7d791df2011-07-22 18:37:52 -040016461 next_fcf_pri = 0;
16462 /*
16463 * Clear the rr_bmask and set all of the bits that are at this
16464 * priority.
16465 */
16466 memset(phba->fcf.fcf_rr_bmask, 0,
16467 sizeof(*phba->fcf.fcf_rr_bmask));
16468 spin_lock_irq(&phba->hbalock);
16469 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16470 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16471 continue;
16472 /*
16473 * the 1st priority that has not FLOGI failed
16474 * will be the highest.
16475 */
16476 if (!next_fcf_pri)
16477 next_fcf_pri = fcf_pri->fcf_rec.priority;
16478 spin_unlock_irq(&phba->hbalock);
16479 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16480 rc = lpfc_sli4_fcf_rr_index_set(phba,
16481 fcf_pri->fcf_rec.fcf_index);
16482 if (rc)
16483 return 0;
16484 }
16485 spin_lock_irq(&phba->hbalock);
16486 }
16487 /*
16488 * if next_fcf_pri was not set above and the list is not empty then
16489 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016490 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016491 */
16492 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16493 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16494 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16495 /*
16496 * the 1st priority that has not FLOGI failed
16497 * will be the highest.
16498 */
16499 if (!next_fcf_pri)
16500 next_fcf_pri = fcf_pri->fcf_rec.priority;
16501 spin_unlock_irq(&phba->hbalock);
16502 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16503 rc = lpfc_sli4_fcf_rr_index_set(phba,
16504 fcf_pri->fcf_rec.fcf_index);
16505 if (rc)
16506 return 0;
16507 }
16508 spin_lock_irq(&phba->hbalock);
16509 }
16510 } else
16511 ret = 1;
16512 spin_unlock_irq(&phba->hbalock);
16513
16514 return ret;
16515}
16516/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016517 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16518 * @phba: pointer to lpfc hba data structure.
16519 *
16520 * This routine is to get the next eligible FCF record index in a round
16521 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016522 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016523 * shall be returned, otherwise, the next eligible FCF record's index
16524 * shall be returned.
16525 **/
16526uint16_t
16527lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16528{
16529 uint16_t next_fcf_index;
16530
James Smart421c6622013-01-03 15:44:16 -050016531initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016532 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016533 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16534
James Smart7d791df2011-07-22 18:37:52 -040016535next_priority:
James Smart421c6622013-01-03 15:44:16 -050016536 /* Determine the next fcf index to check */
16537 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016538 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16539 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016540 next_fcf_index);
16541
James Smart0c9ab6f2010-02-26 14:15:57 -050016542 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016543 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16544 /*
16545 * If we have wrapped then we need to clear the bits that
16546 * have been tested so that we can detect when we should
16547 * change the priority level.
16548 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016549 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16550 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016551 }
16552
James Smart0c9ab6f2010-02-26 14:15:57 -050016553
James Smart3804dc82010-07-14 15:31:37 -040016554 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016555 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16556 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16557 /*
16558 * If next fcf index is not found check if there are lower
16559 * Priority level fcf's in the fcf_priority list.
16560 * Set up the rr_bmask with all of the avaiable fcf bits
16561 * at that level and continue the selection process.
16562 */
16563 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016564 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016565 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16566 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016567 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16568 return LPFC_FCOE_FCF_NEXT_NONE;
16569 else {
16570 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16571 "3063 Only FCF available idx %d, flag %x\n",
16572 next_fcf_index,
16573 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16574 return next_fcf_index;
16575 }
James Smart3804dc82010-07-14 15:31:37 -040016576 }
16577
James Smart7d791df2011-07-22 18:37:52 -040016578 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16579 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016580 LPFC_FCF_FLOGI_FAILED) {
16581 if (list_is_singular(&phba->fcf.fcf_pri_list))
16582 return LPFC_FCOE_FCF_NEXT_NONE;
16583
James Smart7d791df2011-07-22 18:37:52 -040016584 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016585 }
James Smart7d791df2011-07-22 18:37:52 -040016586
James Smart3804dc82010-07-14 15:31:37 -040016587 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016588 "2845 Get next roundrobin failover FCF (x%x)\n",
16589 next_fcf_index);
16590
James Smart0c9ab6f2010-02-26 14:15:57 -050016591 return next_fcf_index;
16592}
16593
16594/**
16595 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16596 * @phba: pointer to lpfc hba data structure.
16597 *
16598 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016599 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016600 * does not go beyond the range of the driver allocated bmask dimension
16601 * before setting the bit.
16602 *
16603 * Returns 0 if the index bit successfully set, otherwise, it returns
16604 * -EINVAL.
16605 **/
16606int
16607lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16608{
16609 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16610 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016611 "2610 FCF (x%x) reached driver's book "
16612 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016613 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16614 return -EINVAL;
16615 }
16616 /* Set the eligible FCF record index bmask */
16617 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16618
James Smart3804dc82010-07-14 15:31:37 -040016619 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016620 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016621 "bmask\n", fcf_index);
16622
James Smart0c9ab6f2010-02-26 14:15:57 -050016623 return 0;
16624}
16625
16626/**
James Smart3804dc82010-07-14 15:31:37 -040016627 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016628 * @phba: pointer to lpfc hba data structure.
16629 *
16630 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016631 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016632 * does not go beyond the range of the driver allocated bmask dimension
16633 * before clearing the bit.
16634 **/
16635void
16636lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16637{
James Smart9a803a72013-09-06 12:17:56 -040016638 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016639 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16640 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016641 "2762 FCF (x%x) reached driver's book "
16642 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016643 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16644 return;
16645 }
16646 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016647 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016648 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16649 list) {
James Smart7d791df2011-07-22 18:37:52 -040016650 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16651 list_del_init(&fcf_pri->list);
16652 break;
16653 }
16654 }
16655 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016656 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016657
16658 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016659 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016660 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016661}
16662
16663/**
James Smartecfd03c2010-02-12 14:41:27 -050016664 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16665 * @phba: pointer to lpfc hba data structure.
16666 *
16667 * This routine is the completion routine for the rediscover FCF table mailbox
16668 * command. If the mailbox command returned failure, it will try to stop the
16669 * FCF rediscover wait timer.
16670 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016671static void
James Smartecfd03c2010-02-12 14:41:27 -050016672lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16673{
16674 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16675 uint32_t shdr_status, shdr_add_status;
16676
16677 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16678
16679 shdr_status = bf_get(lpfc_mbox_hdr_status,
16680 &redisc_fcf->header.cfg_shdr.response);
16681 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16682 &redisc_fcf->header.cfg_shdr.response);
16683 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016684 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016685 "2746 Requesting for FCF rediscovery failed "
16686 "status x%x add_status x%x\n",
16687 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016688 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016689 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016690 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016691 spin_unlock_irq(&phba->hbalock);
16692 /*
16693 * CVL event triggered FCF rediscover request failed,
16694 * last resort to re-try current registered FCF entry.
16695 */
16696 lpfc_retry_pport_discovery(phba);
16697 } else {
16698 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016699 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016700 spin_unlock_irq(&phba->hbalock);
16701 /*
16702 * DEAD FCF event triggered FCF rediscover request
16703 * failed, last resort to fail over as a link down
16704 * to FCF registration.
16705 */
16706 lpfc_sli4_fcf_dead_failthrough(phba);
16707 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016708 } else {
16709 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016710 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016711 /*
16712 * Start FCF rediscovery wait timer for pending FCF
16713 * before rescan FCF record table.
16714 */
16715 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016716 }
James Smartecfd03c2010-02-12 14:41:27 -050016717
16718 mempool_free(mbox, phba->mbox_mem_pool);
16719}
16720
16721/**
James Smart3804dc82010-07-14 15:31:37 -040016722 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016723 * @phba: pointer to lpfc hba data structure.
16724 *
16725 * This routine is invoked to request for rediscovery of the entire FCF table
16726 * by the port.
16727 **/
16728int
16729lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16730{
16731 LPFC_MBOXQ_t *mbox;
16732 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16733 int rc, length;
16734
James Smart0c9ab6f2010-02-26 14:15:57 -050016735 /* Cancel retry delay timers to all vports before FCF rediscover */
16736 lpfc_cancel_all_vport_retry_delay_timer(phba);
16737
James Smartecfd03c2010-02-12 14:41:27 -050016738 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16739 if (!mbox) {
16740 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16741 "2745 Failed to allocate mbox for "
16742 "requesting FCF rediscover.\n");
16743 return -ENOMEM;
16744 }
16745
16746 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16747 sizeof(struct lpfc_sli4_cfg_mhdr));
16748 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16749 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16750 length, LPFC_SLI4_MBX_EMBED);
16751
16752 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16753 /* Set count to 0 for invalidating the entire FCF database */
16754 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16755
16756 /* Issue the mailbox command asynchronously */
16757 mbox->vport = phba->pport;
16758 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16759 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16760
16761 if (rc == MBX_NOT_FINISHED) {
16762 mempool_free(mbox, phba->mbox_mem_pool);
16763 return -EIO;
16764 }
16765 return 0;
16766}
16767
16768/**
James Smartfc2b9892010-02-26 14:15:29 -050016769 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16770 * @phba: pointer to lpfc hba data structure.
16771 *
16772 * This function is the failover routine as a last resort to the FCF DEAD
16773 * event when driver failed to perform fast FCF failover.
16774 **/
16775void
16776lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16777{
16778 uint32_t link_state;
16779
16780 /*
16781 * Last resort as FCF DEAD event failover will treat this as
16782 * a link down, but save the link state because we don't want
16783 * it to be changed to Link Down unless it is already down.
16784 */
16785 link_state = phba->link_state;
16786 lpfc_linkdown(phba);
16787 phba->link_state = link_state;
16788
16789 /* Unregister FCF if no devices connected to it */
16790 lpfc_unregister_unused_fcf(phba);
16791}
16792
16793/**
James Smart026abb82011-12-13 13:20:45 -050016794 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016795 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016796 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016797 *
James Smart026abb82011-12-13 13:20:45 -050016798 * This function gets SLI3 port configure region 23 data through memory dump
16799 * mailbox command. When it successfully retrieves data, the size of the data
16800 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016801 **/
James Smart026abb82011-12-13 13:20:45 -050016802static uint32_t
16803lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016804{
16805 LPFC_MBOXQ_t *pmb = NULL;
16806 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016807 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016808 int rc;
16809
James Smart026abb82011-12-13 13:20:45 -050016810 if (!rgn23_data)
16811 return 0;
16812
James Smarta0c87cb2009-07-19 10:01:10 -040016813 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16814 if (!pmb) {
16815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016816 "2600 failed to allocate mailbox memory\n");
16817 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016818 }
16819 mb = &pmb->u.mb;
16820
James Smarta0c87cb2009-07-19 10:01:10 -040016821 do {
16822 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16823 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16824
16825 if (rc != MBX_SUCCESS) {
16826 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016827 "2601 failed to read config "
16828 "region 23, rc 0x%x Status 0x%x\n",
16829 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016830 mb->un.varDmp.word_cnt = 0;
16831 }
16832 /*
16833 * dump mem may return a zero when finished or we got a
16834 * mailbox error, either way we are done.
16835 */
16836 if (mb->un.varDmp.word_cnt == 0)
16837 break;
16838 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16839 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16840
16841 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016842 rgn23_data + offset,
16843 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016844 offset += mb->un.varDmp.word_cnt;
16845 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16846
James Smart026abb82011-12-13 13:20:45 -050016847 mempool_free(pmb, phba->mbox_mem_pool);
16848 return offset;
16849}
16850
16851/**
16852 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16853 * @phba: pointer to lpfc hba data structure.
16854 * @rgn23_data: pointer to configure region 23 data.
16855 *
16856 * This function gets SLI4 port configure region 23 data through memory dump
16857 * mailbox command. When it successfully retrieves data, the size of the data
16858 * will be returned, otherwise, 0 will be returned.
16859 **/
16860static uint32_t
16861lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16862{
16863 LPFC_MBOXQ_t *mboxq = NULL;
16864 struct lpfc_dmabuf *mp = NULL;
16865 struct lpfc_mqe *mqe;
16866 uint32_t data_length = 0;
16867 int rc;
16868
16869 if (!rgn23_data)
16870 return 0;
16871
16872 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16873 if (!mboxq) {
16874 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16875 "3105 failed to allocate mailbox memory\n");
16876 return 0;
16877 }
16878
16879 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16880 goto out;
16881 mqe = &mboxq->u.mqe;
16882 mp = (struct lpfc_dmabuf *) mboxq->context1;
16883 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16884 if (rc)
16885 goto out;
16886 data_length = mqe->un.mb_words[5];
16887 if (data_length == 0)
16888 goto out;
16889 if (data_length > DMP_RGN23_SIZE) {
16890 data_length = 0;
16891 goto out;
16892 }
16893 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16894out:
16895 mempool_free(mboxq, phba->mbox_mem_pool);
16896 if (mp) {
16897 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16898 kfree(mp);
16899 }
16900 return data_length;
16901}
16902
16903/**
16904 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16905 * @phba: pointer to lpfc hba data structure.
16906 *
16907 * This function read region 23 and parse TLV for port status to
16908 * decide if the user disaled the port. If the TLV indicates the
16909 * port is disabled, the hba_flag is set accordingly.
16910 **/
16911void
16912lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16913{
16914 uint8_t *rgn23_data = NULL;
16915 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16916 uint32_t offset = 0;
16917
16918 /* Get adapter Region 23 data */
16919 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16920 if (!rgn23_data)
16921 goto out;
16922
16923 if (phba->sli_rev < LPFC_SLI_REV4)
16924 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16925 else {
16926 if_type = bf_get(lpfc_sli_intf_if_type,
16927 &phba->sli4_hba.sli_intf);
16928 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16929 goto out;
16930 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16931 }
James Smarta0c87cb2009-07-19 10:01:10 -040016932
16933 if (!data_size)
16934 goto out;
16935
16936 /* Check the region signature first */
16937 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16939 "2619 Config region 23 has bad signature\n");
16940 goto out;
16941 }
16942 offset += 4;
16943
16944 /* Check the data structure version */
16945 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16947 "2620 Config region 23 has bad version\n");
16948 goto out;
16949 }
16950 offset += 4;
16951
16952 /* Parse TLV entries in the region */
16953 while (offset < data_size) {
16954 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16955 break;
16956 /*
16957 * If the TLV is not driver specific TLV or driver id is
16958 * not linux driver id, skip the record.
16959 */
16960 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16961 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16962 (rgn23_data[offset + 3] != 0)) {
16963 offset += rgn23_data[offset + 1] * 4 + 4;
16964 continue;
16965 }
16966
16967 /* Driver found a driver specific TLV in the config region */
16968 sub_tlv_len = rgn23_data[offset + 1] * 4;
16969 offset += 4;
16970 tlv_offset = 0;
16971
16972 /*
16973 * Search for configured port state sub-TLV.
16974 */
16975 while ((offset < data_size) &&
16976 (tlv_offset < sub_tlv_len)) {
16977 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16978 offset += 4;
16979 tlv_offset += 4;
16980 break;
16981 }
16982 if (rgn23_data[offset] != PORT_STE_TYPE) {
16983 offset += rgn23_data[offset + 1] * 4 + 4;
16984 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16985 continue;
16986 }
16987
16988 /* This HBA contains PORT_STE configured */
16989 if (!rgn23_data[offset + 2])
16990 phba->hba_flag |= LINK_DISABLED;
16991
16992 goto out;
16993 }
16994 }
James Smart026abb82011-12-13 13:20:45 -050016995
James Smarta0c87cb2009-07-19 10:01:10 -040016996out:
James Smarta0c87cb2009-07-19 10:01:10 -040016997 kfree(rgn23_data);
16998 return;
16999}
James Smart695a8142010-01-26 23:08:03 -050017000
17001/**
James Smart52d52442011-05-24 11:42:45 -040017002 * lpfc_wr_object - write an object to the firmware
17003 * @phba: HBA structure that indicates port to create a queue on.
17004 * @dmabuf_list: list of dmabufs to write to the port.
17005 * @size: the total byte value of the objects to write to the port.
17006 * @offset: the current offset to be used to start the transfer.
17007 *
17008 * This routine will create a wr_object mailbox command to send to the port.
17009 * the mailbox command will be constructed using the dma buffers described in
17010 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
17011 * BDEs that the imbedded mailbox can support. The @offset variable will be
17012 * used to indicate the starting offset of the transfer and will also return
17013 * the offset after the write object mailbox has completed. @size is used to
17014 * determine the end of the object and whether the eof bit should be set.
17015 *
17016 * Return 0 is successful and offset will contain the the new offset to use
17017 * for the next write.
17018 * Return negative value for error cases.
17019 **/
17020int
17021lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17022 uint32_t size, uint32_t *offset)
17023{
17024 struct lpfc_mbx_wr_object *wr_object;
17025 LPFC_MBOXQ_t *mbox;
17026 int rc = 0, i = 0;
17027 uint32_t shdr_status, shdr_add_status;
17028 uint32_t mbox_tmo;
17029 union lpfc_sli4_cfg_shdr *shdr;
17030 struct lpfc_dmabuf *dmabuf;
17031 uint32_t written = 0;
17032
17033 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17034 if (!mbox)
17035 return -ENOMEM;
17036
17037 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17038 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17039 sizeof(struct lpfc_mbx_wr_object) -
17040 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17041
17042 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17043 wr_object->u.request.write_offset = *offset;
17044 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17045 wr_object->u.request.object_name[0] =
17046 cpu_to_le32(wr_object->u.request.object_name[0]);
17047 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17048 list_for_each_entry(dmabuf, dmabuf_list, list) {
17049 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17050 break;
17051 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17052 wr_object->u.request.bde[i].addrHigh =
17053 putPaddrHigh(dmabuf->phys);
17054 if (written + SLI4_PAGE_SIZE >= size) {
17055 wr_object->u.request.bde[i].tus.f.bdeSize =
17056 (size - written);
17057 written += (size - written);
17058 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17059 } else {
17060 wr_object->u.request.bde[i].tus.f.bdeSize =
17061 SLI4_PAGE_SIZE;
17062 written += SLI4_PAGE_SIZE;
17063 }
17064 i++;
17065 }
17066 wr_object->u.request.bde_count = i;
17067 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17068 if (!phba->sli4_hba.intr_enable)
17069 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17070 else {
James Smarta183a152011-10-10 21:32:43 -040017071 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017072 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17073 }
17074 /* The IOCTL status is embedded in the mailbox subheader. */
17075 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17076 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17077 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17078 if (rc != MBX_TIMEOUT)
17079 mempool_free(mbox, phba->mbox_mem_pool);
17080 if (shdr_status || shdr_add_status || rc) {
17081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17082 "3025 Write Object mailbox failed with "
17083 "status x%x add_status x%x, mbx status x%x\n",
17084 shdr_status, shdr_add_status, rc);
17085 rc = -ENXIO;
17086 } else
17087 *offset += wr_object->u.response.actual_write_length;
17088 return rc;
17089}
17090
17091/**
James Smart695a8142010-01-26 23:08:03 -050017092 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17093 * @vport: pointer to vport data structure.
17094 *
17095 * This function iterate through the mailboxq and clean up all REG_LOGIN
17096 * and REG_VPI mailbox commands associated with the vport. This function
17097 * is called when driver want to restart discovery of the vport due to
17098 * a Clear Virtual Link event.
17099 **/
17100void
17101lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17102{
17103 struct lpfc_hba *phba = vport->phba;
17104 LPFC_MBOXQ_t *mb, *nextmb;
17105 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017106 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017107 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017108 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017109 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017110 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017111
James Smartd439d282010-09-29 11:18:45 -040017112 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017113 spin_lock_irq(&phba->hbalock);
17114 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17115 if (mb->vport != vport)
17116 continue;
17117
17118 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17119 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17120 continue;
17121
James Smartd439d282010-09-29 11:18:45 -040017122 list_del(&mb->list);
17123 list_add_tail(&mb->list, &mbox_cmd_list);
17124 }
17125 /* Clean up active mailbox command with the vport */
17126 mb = phba->sli.mbox_active;
17127 if (mb && (mb->vport == vport)) {
17128 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17129 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17130 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17131 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17132 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17133 /* Put reference count for delayed processing */
17134 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17135 /* Unregister the RPI when mailbox complete */
17136 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17137 }
17138 }
James Smart63e801c2010-11-20 23:14:19 -050017139 /* Cleanup any mailbox completions which are not yet processed */
17140 do {
17141 restart_loop = 0;
17142 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17143 /*
17144 * If this mailox is already processed or it is
17145 * for another vport ignore it.
17146 */
17147 if ((mb->vport != vport) ||
17148 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17149 continue;
17150
17151 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17152 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17153 continue;
17154
17155 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17156 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17157 ndlp = (struct lpfc_nodelist *)mb->context2;
17158 /* Unregister the RPI when mailbox complete */
17159 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17160 restart_loop = 1;
17161 spin_unlock_irq(&phba->hbalock);
17162 spin_lock(shost->host_lock);
17163 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17164 spin_unlock(shost->host_lock);
17165 spin_lock_irq(&phba->hbalock);
17166 break;
17167 }
17168 }
17169 } while (restart_loop);
17170
James Smartd439d282010-09-29 11:18:45 -040017171 spin_unlock_irq(&phba->hbalock);
17172
17173 /* Release the cleaned-up mailbox commands */
17174 while (!list_empty(&mbox_cmd_list)) {
17175 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017176 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17177 mp = (struct lpfc_dmabuf *) (mb->context1);
17178 if (mp) {
17179 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17180 kfree(mp);
17181 }
James Smart78730cf2010-04-06 15:06:30 -040017182 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017183 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017184 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017185 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017186 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017187 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017188 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017189 }
James Smart695a8142010-01-26 23:08:03 -050017190 }
James Smart695a8142010-01-26 23:08:03 -050017191 mempool_free(mb, phba->mbox_mem_pool);
17192 }
James Smartd439d282010-09-29 11:18:45 -040017193
17194 /* Release the ndlp with the cleaned-up active mailbox command */
17195 if (act_mbx_ndlp) {
17196 spin_lock(shost->host_lock);
17197 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17198 spin_unlock(shost->host_lock);
17199 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017200 }
James Smart695a8142010-01-26 23:08:03 -050017201}
17202
James Smart2a9bf3d2010-06-07 15:24:45 -040017203/**
17204 * lpfc_drain_txq - Drain the txq
17205 * @phba: Pointer to HBA context object.
17206 *
17207 * This function attempt to submit IOCBs on the txq
17208 * to the adapter. For SLI4 adapters, the txq contains
17209 * ELS IOCBs that have been deferred because the there
17210 * are no SGLs. This congestion can occur with large
17211 * vport counts during node discovery.
17212 **/
17213
17214uint32_t
17215lpfc_drain_txq(struct lpfc_hba *phba)
17216{
17217 LIST_HEAD(completions);
17218 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017219 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017220 unsigned long iflags = 0;
17221 char *fail_msg = NULL;
17222 struct lpfc_sglq *sglq;
17223 union lpfc_wqe wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040017224 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017225
James Smart398d81c2013-05-31 17:04:19 -040017226 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017227 list_for_each_entry(piocbq, &pring->txq, list) {
17228 txq_cnt++;
17229 }
17230
17231 if (txq_cnt > pring->txq_max)
17232 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017233
James Smart398d81c2013-05-31 17:04:19 -040017234 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017235
James Smart0e9bb8d2013-03-01 16:35:12 -050017236 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017237 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017238
James Smart19ca7602010-11-20 23:11:55 -050017239 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017240 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017241 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017242 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17243 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017244 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017245 break;
17246 }
James Smart19ca7602010-11-20 23:11:55 -050017247 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017248 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017249 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017250 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017251 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017252 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017253 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017254
17255 /* The xri and iocb resources secured,
17256 * attempt to issue request
17257 */
James Smart6d368e52011-05-24 11:44:12 -040017258 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017259 piocbq->sli4_xritag = sglq->sli4_xritag;
17260 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17261 fail_msg = "to convert bpl to sgl";
17262 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17263 fail_msg = "to convert iocb to wqe";
17264 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17265 fail_msg = " - Wq is full";
17266 else
17267 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17268
17269 if (fail_msg) {
17270 /* Failed means we can't issue and need to cancel */
17271 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17272 "2822 IOCB failed %s iotag 0x%x "
17273 "xri 0x%x\n",
17274 fail_msg,
17275 piocbq->iotag, piocbq->sli4_xritag);
17276 list_add_tail(&piocbq->list, &completions);
17277 }
James Smart398d81c2013-05-31 17:04:19 -040017278 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017279 }
17280
James Smart2a9bf3d2010-06-07 15:24:45 -040017281 /* Cancel all the IOCBs that cannot be issued */
17282 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17283 IOERR_SLI_ABORTED);
17284
James Smart0e9bb8d2013-03-01 16:35:12 -050017285 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017286}