blob: 9c8368a7149a4b2668d960087c6a567cd2064f46 [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 Smartf25e8e72015-04-07 15:07:28 -04004 * Copyright (C) 2004-2015 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010027#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050028
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040033#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040034#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040035#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050036
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_hw.h"
39#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040041#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_crtn.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050048#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040049#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050050
51/* There are only four IOCB completion types. */
52typedef enum _lpfc_iocb_type {
53 LPFC_UNKNOWN_IOCB,
54 LPFC_UNSOL_IOCB,
55 LPFC_SOL_IOCB,
56 LPFC_ABORT_IOCB
57} lpfc_iocb_type;
58
James Smart4f774512009-05-22 14:52:35 -040059
60/* Provide function prototypes local to this module. */
61static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
62 uint32_t);
63static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040064 uint8_t *, uint32_t *);
65static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
66 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040067static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
68 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040069static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
70 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040071static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
72 int);
James Smartba20c852012-08-03 12:36:52 -040073static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
74 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040075static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
76static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart05580562011-05-24 11:40:48 -040077
James Smart4f774512009-05-22 14:52:35 -040078static IOCB_t *
79lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
80{
81 return &iocbq->iocb;
82}
83
84/**
85 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
86 * @q: The Work Queue to operate on.
87 * @wqe: The work Queue Entry to put on the Work queue.
88 *
89 * This routine will copy the contents of @wqe to the next available entry on
90 * the @q. This function will then ring the Work Queue Doorbell to signal the
91 * HBA to start processing the Work Queue Entry. This function returns 0 if
92 * successful. If no entries are available on @q then this function will return
93 * -ENOMEM.
94 * The caller is expected to hold the hbalock when calling this routine.
95 **/
96static uint32_t
97lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
98{
James Smart2e90f4b2011-12-13 13:22:37 -050099 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400100 struct lpfc_register doorbell;
101 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400102 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400103
James Smart2e90f4b2011-12-13 13:22:37 -0500104 /* sanity check on queue memory */
105 if (unlikely(!q))
106 return -ENOMEM;
107 temp_wqe = q->qe[q->host_index].wqe;
108
James Smart4f774512009-05-22 14:52:35 -0400109 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400110 idx = ((q->host_index + 1) % q->entry_count);
111 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400112 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400113 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400114 }
115 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400116 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500117 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400118 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500119 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
120 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400121 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
122
123 /* Update the host index before invoking device */
124 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400125
126 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400127
128 /* Ring Doorbell */
129 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500130 if (q->db_format == LPFC_DB_LIST_FORMAT) {
131 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
132 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
133 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
134 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
135 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
136 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
137 } else {
138 return -EINVAL;
139 }
140 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400141
142 return 0;
143}
144
145/**
146 * lpfc_sli4_wq_release - Updates internal hba index for WQ
147 * @q: The Work Queue to operate on.
148 * @index: The index to advance the hba index to.
149 *
150 * This routine will update the HBA index of a queue to reflect consumption of
151 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
152 * an entry the host calls this function to update the queue's internal
153 * pointers. This routine returns the number of entries that were consumed by
154 * the HBA.
155 **/
156static uint32_t
157lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
158{
159 uint32_t released = 0;
160
James Smart2e90f4b2011-12-13 13:22:37 -0500161 /* sanity check on queue memory */
162 if (unlikely(!q))
163 return 0;
164
James Smart4f774512009-05-22 14:52:35 -0400165 if (q->hba_index == index)
166 return 0;
167 do {
168 q->hba_index = ((q->hba_index + 1) % q->entry_count);
169 released++;
170 } while (q->hba_index != index);
171 return released;
172}
173
174/**
175 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
176 * @q: The Mailbox Queue to operate on.
177 * @wqe: The Mailbox Queue Entry to put on the Work queue.
178 *
179 * This routine will copy the contents of @mqe to the next available entry on
180 * the @q. This function will then ring the Work Queue Doorbell to signal the
181 * HBA to start processing the Work Queue Entry. This function returns 0 if
182 * successful. If no entries are available on @q then this function will return
183 * -ENOMEM.
184 * The caller is expected to hold the hbalock when calling this routine.
185 **/
186static uint32_t
187lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
188{
James Smart2e90f4b2011-12-13 13:22:37 -0500189 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400190 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400191
James Smart2e90f4b2011-12-13 13:22:37 -0500192 /* sanity check on queue memory */
193 if (unlikely(!q))
194 return -ENOMEM;
195 temp_mqe = q->qe[q->host_index].mqe;
196
James Smart4f774512009-05-22 14:52:35 -0400197 /* If the host has not yet processed the next entry then we are done */
198 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
199 return -ENOMEM;
200 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
201 /* Save off the mailbox pointer for completion */
202 q->phba->mbox = (MAILBOX_t *)temp_mqe;
203
204 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400205 q->host_index = ((q->host_index + 1) % q->entry_count);
206
207 /* Ring Doorbell */
208 doorbell.word0 = 0;
209 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
210 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
211 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400212 return 0;
213}
214
215/**
216 * lpfc_sli4_mq_release - Updates internal hba index for MQ
217 * @q: The Mailbox Queue to operate on.
218 *
219 * This routine will update the HBA index of a queue to reflect consumption of
220 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
221 * an entry the host calls this function to update the queue's internal
222 * pointers. This routine returns the number of entries that were consumed by
223 * the HBA.
224 **/
225static uint32_t
226lpfc_sli4_mq_release(struct lpfc_queue *q)
227{
James Smart2e90f4b2011-12-13 13:22:37 -0500228 /* sanity check on queue memory */
229 if (unlikely(!q))
230 return 0;
231
James Smart4f774512009-05-22 14:52:35 -0400232 /* Clear the mailbox pointer for completion */
233 q->phba->mbox = NULL;
234 q->hba_index = ((q->hba_index + 1) % q->entry_count);
235 return 1;
236}
237
238/**
239 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
240 * @q: The Event Queue to get the first valid EQE from
241 *
242 * This routine will get the first valid Event Queue Entry from @q, update
243 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
244 * the Queue (no more work to do), or the Queue is full of EQEs that have been
245 * processed, but not popped back to the HBA then this routine will return NULL.
246 **/
247static struct lpfc_eqe *
248lpfc_sli4_eq_get(struct lpfc_queue *q)
249{
James Smart2e90f4b2011-12-13 13:22:37 -0500250 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400251 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500252
253 /* sanity check on queue memory */
254 if (unlikely(!q))
255 return NULL;
256 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400257
258 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400259 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400260 return NULL;
261 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400262 idx = ((q->hba_index + 1) % q->entry_count);
263 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400264 return NULL;
265
James Smart027140e2012-08-03 12:35:44 -0400266 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400267
268 /*
269 * insert barrier for instruction interlock : data from the hardware
270 * must have the valid bit checked before it can be copied and acted
271 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
272 * instructions allowing action on content before valid bit checked,
273 * add barrier here as well. May not be needed as "content" is a
274 * single 32-bit entity here (vs multi word structure for cq's).
275 */
276 mb();
James Smart4f774512009-05-22 14:52:35 -0400277 return eqe;
278}
279
280/**
James Smartba20c852012-08-03 12:36:52 -0400281 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
282 * @q: The Event Queue to disable interrupts
283 *
284 **/
285static inline void
286lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
287{
288 struct lpfc_register doorbell;
289
290 doorbell.word0 = 0;
291 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
292 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
293 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
294 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
295 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
296 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
297}
298
299/**
James Smart4f774512009-05-22 14:52:35 -0400300 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
301 * @q: The Event Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Event Queue Entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of EQEs that were popped.
313 **/
314uint32_t
315lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
316{
317 uint32_t released = 0;
318 struct lpfc_eqe *temp_eqe;
319 struct lpfc_register doorbell;
320
James Smart2e90f4b2011-12-13 13:22:37 -0500321 /* sanity check on queue memory */
322 if (unlikely(!q))
323 return 0;
324
James Smart4f774512009-05-22 14:52:35 -0400325 /* while there are valid entries */
326 while (q->hba_index != q->host_index) {
327 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400328 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400329 released++;
330 q->host_index = ((q->host_index + 1) % q->entry_count);
331 }
332 if (unlikely(released == 0 && !arm))
333 return 0;
334
335 /* ring doorbell for number popped */
336 doorbell.word0 = 0;
337 if (arm) {
338 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
340 }
341 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
342 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500343 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
344 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
345 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400346 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500347 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
348 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
349 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400350 return released;
351}
352
353/**
354 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
355 * @q: The Completion Queue to get the first valid CQE from
356 *
357 * This routine will get the first valid Completion Queue Entry from @q, update
358 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
359 * the Queue (no more work to do), or the Queue is full of CQEs that have been
360 * processed, but not popped back to the HBA then this routine will return NULL.
361 **/
362static struct lpfc_cqe *
363lpfc_sli4_cq_get(struct lpfc_queue *q)
364{
365 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400366 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400367
James Smart2e90f4b2011-12-13 13:22:37 -0500368 /* sanity check on queue memory */
369 if (unlikely(!q))
370 return NULL;
371
James Smart4f774512009-05-22 14:52:35 -0400372 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400373 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400374 return NULL;
375 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400376 idx = ((q->hba_index + 1) % q->entry_count);
377 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400378 return NULL;
379
380 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400381 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400382
383 /*
384 * insert barrier for instruction interlock : data from the hardware
385 * must have the valid bit checked before it can be copied and acted
386 * upon. Speculative instructions were allowing a bcopy at the start
387 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
388 * after our return, to copy data before the valid bit check above
389 * was done. As such, some of the copied data was stale. The barrier
390 * ensures the check is before any data is copied.
391 */
392 mb();
James Smart4f774512009-05-22 14:52:35 -0400393 return cqe;
394}
395
396/**
397 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
398 * @q: The Completion Queue that the host has completed processing for.
399 * @arm: Indicates whether the host wants to arms this CQ.
400 *
401 * This routine will mark all Completion queue entries on @q, from the last
402 * known completed entry to the last entry that was processed, as completed
403 * by clearing the valid bit for each completion queue entry. Then it will
404 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
405 * The internal host index in the @q will be updated by this routine to indicate
406 * that the host has finished processing the entries. The @arm parameter
407 * indicates that the queue should be rearmed when ringing the doorbell.
408 *
409 * This function will return the number of CQEs that were released.
410 **/
411uint32_t
412lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
413{
414 uint32_t released = 0;
415 struct lpfc_cqe *temp_qe;
416 struct lpfc_register doorbell;
417
James Smart2e90f4b2011-12-13 13:22:37 -0500418 /* sanity check on queue memory */
419 if (unlikely(!q))
420 return 0;
James Smart4f774512009-05-22 14:52:35 -0400421 /* while there are valid entries */
422 while (q->hba_index != q->host_index) {
423 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400424 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400425 released++;
426 q->host_index = ((q->host_index + 1) % q->entry_count);
427 }
428 if (unlikely(released == 0 && !arm))
429 return 0;
430
431 /* ring doorbell for number popped */
432 doorbell.word0 = 0;
433 if (arm)
434 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
435 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
436 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500437 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
438 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
439 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400440 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
441 return released;
442}
443
444/**
445 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
446 * @q: The Header Receive Queue to operate on.
447 * @wqe: The Receive Queue Entry to put on the Receive queue.
448 *
449 * This routine will copy the contents of @wqe to the next available entry on
450 * the @q. This function will then ring the Receive Queue Doorbell to signal the
451 * HBA to start processing the Receive Queue Entry. This function returns the
452 * index that the rqe was copied to if successful. If no entries are available
453 * on @q then this function will return -ENOMEM.
454 * The caller is expected to hold the hbalock when calling this routine.
455 **/
456static int
457lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
458 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
459{
James Smart2e90f4b2011-12-13 13:22:37 -0500460 struct lpfc_rqe *temp_hrqe;
461 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400462 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500463 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400464
James Smart2e90f4b2011-12-13 13:22:37 -0500465 /* sanity check on queue memory */
466 if (unlikely(!hq) || unlikely(!dq))
467 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500468 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500469 temp_hrqe = hq->qe[hq->host_index].rqe;
470 temp_drqe = dq->qe[dq->host_index].rqe;
471
James Smart4f774512009-05-22 14:52:35 -0400472 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
473 return -EINVAL;
474 if (hq->host_index != dq->host_index)
475 return -EINVAL;
476 /* If the host has not yet processed the next entry then we are done */
477 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
478 return -EBUSY;
479 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
480 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
481
482 /* Update the host index to point to the next slot */
483 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
484 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
485
486 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400487 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400488 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500489 if (hq->db_format == LPFC_DB_RING_FORMAT) {
490 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
491 hq->entry_repost);
492 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
493 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
494 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
495 hq->entry_repost);
496 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
497 hq->host_index);
498 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
499 } else {
500 return -EINVAL;
501 }
502 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400503 }
504 return put_index;
505}
506
507/**
508 * lpfc_sli4_rq_release - Updates internal hba index for RQ
509 * @q: The Header Receive Queue to operate on.
510 *
511 * This routine will update the HBA index of a queue to reflect consumption of
512 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
513 * consumed an entry the host calls this function to update the queue's
514 * internal pointers. This routine returns the number of entries that were
515 * consumed by the HBA.
516 **/
517static uint32_t
518lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
519{
James Smart2e90f4b2011-12-13 13:22:37 -0500520 /* sanity check on queue memory */
521 if (unlikely(!hq) || unlikely(!dq))
522 return 0;
523
James Smart4f774512009-05-22 14:52:35 -0400524 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
525 return 0;
526 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
527 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
528 return 1;
529}
530
James Smarte59058c2008-08-24 21:49:00 -0400531/**
James Smart3621a712009-04-06 18:47:14 -0400532 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400533 * @phba: Pointer to HBA context object.
534 * @pring: Pointer to driver SLI ring object.
535 *
536 * This function returns pointer to next command iocb entry
537 * in the command ring. The caller must hold hbalock to prevent
538 * other threads consume the next command iocb.
539 * SLI-2/SLI-3 provide different sized iocbs.
540 **/
James Smarted957682007-06-17 19:56:37 -0500541static inline IOCB_t *
542lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
543{
James Smart7e56aa22012-08-03 12:35:34 -0400544 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
545 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500546}
547
James Smarte59058c2008-08-24 21:49:00 -0400548/**
James Smart3621a712009-04-06 18:47:14 -0400549 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400550 * @phba: Pointer to HBA context object.
551 * @pring: Pointer to driver SLI ring object.
552 *
553 * This function returns pointer to next response iocb entry
554 * in the response ring. The caller must hold hbalock to make sure
555 * that no other thread consume the next response iocb.
556 * SLI-2/SLI-3 provide different sized iocbs.
557 **/
James Smarted957682007-06-17 19:56:37 -0500558static inline IOCB_t *
559lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
560{
James Smart7e56aa22012-08-03 12:35:34 -0400561 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
562 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500563}
564
James Smarte59058c2008-08-24 21:49:00 -0400565/**
James Smart3621a712009-04-06 18:47:14 -0400566 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400567 * @phba: Pointer to HBA context object.
568 *
569 * This function is called with hbalock held. This function
570 * allocates a new driver iocb object from the iocb pool. If the
571 * allocation is successful, it returns pointer to the newly
572 * allocated iocb object else it returns NULL.
573 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400574struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500575__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400576{
577 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
578 struct lpfc_iocbq * iocbq = NULL;
579
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100580 lockdep_assert_held(&phba->hbalock);
581
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400582 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400583 if (iocbq)
584 phba->iocb_cnt++;
585 if (phba->iocb_cnt > phba->iocb_max)
586 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400587 return iocbq;
588}
589
James Smarte59058c2008-08-24 21:49:00 -0400590/**
James Smartda0436e2009-05-22 14:51:39 -0400591 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
592 * @phba: Pointer to HBA context object.
593 * @xritag: XRI value.
594 *
595 * This function clears the sglq pointer from the array of acive
596 * sglq's. The xritag that is passed in is used to index into the
597 * array. Before the xritag can be used it needs to be adjusted
598 * by subtracting the xribase.
599 *
600 * Returns sglq ponter = success, NULL = Failure.
601 **/
602static struct lpfc_sglq *
603__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
604{
James Smartda0436e2009-05-22 14:51:39 -0400605 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400606
607 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
608 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400609 return sglq;
610}
611
612/**
613 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
614 * @phba: Pointer to HBA context object.
615 * @xritag: XRI value.
616 *
617 * This function returns the sglq pointer from the array of acive
618 * sglq's. The xritag that is passed in is used to index into the
619 * array. Before the xritag can be used it needs to be adjusted
620 * by subtracting the xribase.
621 *
622 * Returns sglq ponter = success, NULL = Failure.
623 **/
James Smart0f65ff62010-02-26 14:14:23 -0500624struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400625__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
626{
James Smartda0436e2009-05-22 14:51:39 -0400627 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400628
629 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400630 return sglq;
631}
632
633/**
James Smart1151e3e2011-02-16 12:39:35 -0500634 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500635 * @phba: Pointer to HBA context object.
636 * @xritag: xri used in this exchange.
637 * @rrq: The RRQ to be cleared.
638 *
James Smart19ca7602010-11-20 23:11:55 -0500639 **/
James Smart1151e3e2011-02-16 12:39:35 -0500640void
641lpfc_clr_rrq_active(struct lpfc_hba *phba,
642 uint16_t xritag,
643 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500644{
James Smart1151e3e2011-02-16 12:39:35 -0500645 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500646
James Smart1151e3e2011-02-16 12:39:35 -0500647 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
648 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500649
650 /* The target DID could have been swapped (cable swap)
651 * we should use the ndlp from the findnode if it is
652 * available.
653 */
James Smart1151e3e2011-02-16 12:39:35 -0500654 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500655 ndlp = rrq->ndlp;
656
James Smart1151e3e2011-02-16 12:39:35 -0500657 if (!ndlp)
658 goto out;
659
James Smartcff261f2013-12-17 20:29:47 -0500660 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500661 rrq->send_rrq = 0;
662 rrq->xritag = 0;
663 rrq->rrq_stop_time = 0;
664 }
James Smart1151e3e2011-02-16 12:39:35 -0500665out:
James Smart19ca7602010-11-20 23:11:55 -0500666 mempool_free(rrq, phba->rrq_pool);
667}
668
669/**
670 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
671 * @phba: Pointer to HBA context object.
672 *
673 * This function is called with hbalock held. This function
674 * Checks if stop_time (ratov from setting rrq active) has
675 * been reached, if it has and the send_rrq flag is set then
676 * it will call lpfc_send_rrq. If the send_rrq flag is not set
677 * then it will just call the routine to clear the rrq and
678 * free the rrq resource.
679 * The timer is set to the next rrq that is going to expire before
680 * leaving the routine.
681 *
682 **/
683void
684lpfc_handle_rrq_active(struct lpfc_hba *phba)
685{
686 struct lpfc_node_rrq *rrq;
687 struct lpfc_node_rrq *nextrrq;
688 unsigned long next_time;
689 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500690 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500691
692 spin_lock_irqsave(&phba->hbalock, iflags);
693 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400694 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500695 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500696 &phba->active_rrq_list, list) {
697 if (time_after(jiffies, rrq->rrq_stop_time))
698 list_move(&rrq->list, &send_rrq);
699 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500700 next_time = rrq->rrq_stop_time;
701 }
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500703 if ((!list_empty(&phba->active_rrq_list)) &&
704 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500705 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500706 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
707 list_del(&rrq->list);
708 if (!rrq->send_rrq)
709 /* this call will free the rrq */
710 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
711 else if (lpfc_send_rrq(phba, rrq)) {
712 /* if we send the rrq then the completion handler
713 * will clear the bit in the xribitmap.
714 */
715 lpfc_clr_rrq_active(phba, rrq->xritag,
716 rrq);
717 }
718 }
James Smart19ca7602010-11-20 23:11:55 -0500719}
720
721/**
722 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
723 * @vport: Pointer to vport context object.
724 * @xri: The xri used in the exchange.
725 * @did: The targets DID for this exchange.
726 *
727 * returns NULL = rrq not found in the phba->active_rrq_list.
728 * rrq = rrq for this xri and target.
729 **/
730struct lpfc_node_rrq *
731lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
732{
733 struct lpfc_hba *phba = vport->phba;
734 struct lpfc_node_rrq *rrq;
735 struct lpfc_node_rrq *nextrrq;
736 unsigned long iflags;
737
738 if (phba->sli_rev != LPFC_SLI_REV4)
739 return NULL;
740 spin_lock_irqsave(&phba->hbalock, iflags);
741 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
742 if (rrq->vport == vport && rrq->xritag == xri &&
743 rrq->nlp_DID == did){
744 list_del(&rrq->list);
745 spin_unlock_irqrestore(&phba->hbalock, iflags);
746 return rrq;
747 }
748 }
749 spin_unlock_irqrestore(&phba->hbalock, iflags);
750 return NULL;
751}
752
753/**
754 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
755 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500756 * @ndlp: Pointer to the lpfc_node_list structure.
757 * If ndlp is NULL Remove all active RRQs for this vport from the
758 * phba->active_rrq_list and clear the rrq.
759 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500760 **/
761void
James Smart1151e3e2011-02-16 12:39:35 -0500762lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500763
764{
765 struct lpfc_hba *phba = vport->phba;
766 struct lpfc_node_rrq *rrq;
767 struct lpfc_node_rrq *nextrrq;
768 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500769 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500770
771 if (phba->sli_rev != LPFC_SLI_REV4)
772 return;
James Smart1151e3e2011-02-16 12:39:35 -0500773 if (!ndlp) {
774 lpfc_sli4_vport_delete_els_xri_aborted(vport);
775 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500776 }
James Smart1151e3e2011-02-16 12:39:35 -0500777 spin_lock_irqsave(&phba->hbalock, iflags);
778 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
779 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
780 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500781 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500782
783 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
784 list_del(&rrq->list);
785 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
786 }
James Smart19ca7602010-11-20 23:11:55 -0500787}
788
789/**
James Smart1151e3e2011-02-16 12:39:35 -0500790 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500791 * @phba: Pointer to HBA context object.
792 * @ndlp: Targets nodelist pointer for this exchange.
793 * @xritag the xri in the bitmap to test.
794 *
795 * This function is called with hbalock held. This function
796 * returns 0 = rrq not active for this xri
797 * 1 = rrq is valid for this xri.
798 **/
James Smart1151e3e2011-02-16 12:39:35 -0500799int
800lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500801 uint16_t xritag)
802{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100803 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500804 if (!ndlp)
805 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500806 if (!ndlp->active_rrqs_xri_bitmap)
807 return 0;
808 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500809 return 1;
810 else
811 return 0;
812}
813
814/**
815 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
816 * @phba: Pointer to HBA context object.
817 * @ndlp: nodelist pointer for this target.
818 * @xritag: xri used in this exchange.
819 * @rxid: Remote Exchange ID.
820 * @send_rrq: Flag used to determine if we should send rrq els cmd.
821 *
822 * This function takes the hbalock.
823 * The active bit is always set in the active rrq xri_bitmap even
824 * if there is no slot avaiable for the other rrq information.
825 *
826 * returns 0 rrq actived for this xri
827 * < 0 No memory or invalid ndlp.
828 **/
829int
830lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500831 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500832{
James Smart19ca7602010-11-20 23:11:55 -0500833 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500834 struct lpfc_node_rrq *rrq;
835 int empty;
836
837 if (!ndlp)
838 return -EINVAL;
839
840 if (!phba->cfg_enable_rrq)
841 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500842
843 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500844 if (phba->pport->load_flag & FC_UNLOADING) {
845 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
846 goto out;
847 }
848
849 /*
850 * set the active bit even if there is no mem available.
851 */
852 if (NLP_CHK_FREE_REQ(ndlp))
853 goto out;
854
855 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
856 goto out;
857
James Smartcff261f2013-12-17 20:29:47 -0500858 if (!ndlp->active_rrqs_xri_bitmap)
859 goto out;
860
861 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500862 goto out;
863
James Smart19ca7602010-11-20 23:11:55 -0500864 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500865 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
866 if (!rrq) {
867 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
868 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
869 " DID:0x%x Send:%d\n",
870 xritag, rxid, ndlp->nlp_DID, send_rrq);
871 return -EINVAL;
872 }
James Smarte5771b42013-03-01 16:37:14 -0500873 if (phba->cfg_enable_rrq == 1)
874 rrq->send_rrq = send_rrq;
875 else
876 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500877 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400878 rrq->rrq_stop_time = jiffies +
879 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500880 rrq->ndlp = ndlp;
881 rrq->nlp_DID = ndlp->nlp_DID;
882 rrq->vport = ndlp->vport;
883 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500884 spin_lock_irqsave(&phba->hbalock, iflags);
885 empty = list_empty(&phba->active_rrq_list);
886 list_add_tail(&rrq->list, &phba->active_rrq_list);
887 phba->hba_flag |= HBA_RRQ_ACTIVE;
888 if (empty)
889 lpfc_worker_wake_up(phba);
890 spin_unlock_irqrestore(&phba->hbalock, iflags);
891 return 0;
892out:
893 spin_unlock_irqrestore(&phba->hbalock, iflags);
894 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
895 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
896 " DID:0x%x Send:%d\n",
897 xritag, rxid, ndlp->nlp_DID, send_rrq);
898 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500899}
900
901/**
James Smartda0436e2009-05-22 14:51:39 -0400902 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
903 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500904 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400905 *
James Smartdafe8ce2014-09-03 12:56:40 -0400906 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400907 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400908 * list is not empty then it is successful, it returns pointer to the newly
909 * allocated sglq object else it returns NULL.
910 **/
911static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500912__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400913{
914 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
915 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500916 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500917 struct lpfc_scsi_buf *lpfc_cmd;
918 struct lpfc_nodelist *ndlp;
919 int found = 0;
920
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100921 lockdep_assert_held(&phba->hbalock);
922
James Smart19ca7602010-11-20 23:11:55 -0500923 if (piocbq->iocb_flag & LPFC_IO_FCP) {
924 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
925 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500926 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400927 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500928 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400929 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
930 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
931 ndlp = NULL;
932 else
933 ndlp = piocbq->context_un.ndlp;
934 } else {
James Smart19ca7602010-11-20 23:11:55 -0500935 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400936 }
James Smart19ca7602010-11-20 23:11:55 -0500937
James Smartda0436e2009-05-22 14:51:39 -0400938 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500939 start_sglq = sglq;
940 while (!found) {
941 if (!sglq)
942 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400943 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500944 /* This xri has an rrq outstanding for this DID.
945 * put it back in the list and get another xri.
946 */
947 list_add_tail(&sglq->list, lpfc_sgl_list);
948 sglq = NULL;
949 list_remove_head(lpfc_sgl_list, sglq,
950 struct lpfc_sglq, list);
951 if (sglq == start_sglq) {
952 sglq = NULL;
953 break;
954 } else
955 continue;
956 }
957 sglq->ndlp = ndlp;
958 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400959 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500960 sglq->state = SGL_ALLOCATED;
961 }
James Smartda0436e2009-05-22 14:51:39 -0400962 return sglq;
963}
964
965/**
James Smart3621a712009-04-06 18:47:14 -0400966 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400967 * @phba: Pointer to HBA context object.
968 *
969 * This function is called with no lock held. This function
970 * allocates a new driver iocb object from the iocb pool. If the
971 * allocation is successful, it returns pointer to the newly
972 * allocated iocb object else it returns NULL.
973 **/
James Smart2e0fef82007-06-17 19:56:36 -0500974struct lpfc_iocbq *
975lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500976{
James Smart2e0fef82007-06-17 19:56:36 -0500977 struct lpfc_iocbq * iocbq = NULL;
978 unsigned long iflags;
979
980 spin_lock_irqsave(&phba->hbalock, iflags);
981 iocbq = __lpfc_sli_get_iocbq(phba);
982 spin_unlock_irqrestore(&phba->hbalock, iflags);
983 return iocbq;
984}
985
James Smarte59058c2008-08-24 21:49:00 -0400986/**
James Smart4f774512009-05-22 14:52:35 -0400987 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
988 * @phba: Pointer to HBA context object.
989 * @iocbq: Pointer to driver iocb object.
990 *
991 * This function is called with hbalock held to release driver
992 * iocb object to the iocb pool. The iotag in the iocb object
993 * does not change for each use of the iocb object. This function
994 * clears all other fields of the iocb object when it is freed.
995 * The sqlq structure that holds the xritag and phys and virtual
996 * mappings for the scatter gather list is retrieved from the
997 * active array of sglq. The get of the sglq pointer also clears
998 * the entry in the array. If the status of the IO indiactes that
999 * this IO was aborted then the sglq entry it put on the
1000 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1001 * IO has good status or fails for any other reason then the sglq
1002 * entry is added to the free list (lpfc_sgl_list).
1003 **/
1004static void
1005__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1006{
1007 struct lpfc_sglq *sglq;
1008 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001009 unsigned long iflag = 0;
1010 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001011
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001012 lockdep_assert_held(&phba->hbalock);
1013
James Smart4f774512009-05-22 14:52:35 -04001014 if (iocbq->sli4_xritag == NO_XRI)
1015 sglq = NULL;
1016 else
James Smart6d368e52011-05-24 11:44:12 -04001017 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1018
James Smart0e9bb8d2013-03-01 16:35:12 -05001019
James Smart4f774512009-05-22 14:52:35 -04001020 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001021 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1022 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001023 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1024 iflag);
1025 list_add(&sglq->list,
1026 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1027 spin_unlock_irqrestore(
1028 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001029 } else {
James Smartdafe8ce2014-09-03 12:56:40 -04001030 spin_lock_irqsave(&pring->ring_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001031 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001032 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001033 list_add_tail(&sglq->list,
1034 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04001035 spin_unlock_irqrestore(&pring->ring_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001036
1037 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001038 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001039 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001040 }
James Smart4f774512009-05-22 14:52:35 -04001041 }
1042
1043
1044 /*
1045 * Clean all volatile data fields, preserve iotag and node struct.
1046 */
1047 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001048 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001049 iocbq->sli4_xritag = NO_XRI;
1050 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1051}
1052
James Smart2a9bf3d2010-06-07 15:24:45 -04001053
James Smart4f774512009-05-22 14:52:35 -04001054/**
James Smart3772a992009-05-22 14:50:54 -04001055 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1056 * @phba: Pointer to HBA context object.
1057 * @iocbq: Pointer to driver iocb object.
1058 *
1059 * This function is called with hbalock held to release driver
1060 * iocb object to the iocb pool. The iotag in the iocb object
1061 * does not change for each use of the iocb object. This function
1062 * clears all other fields of the iocb object when it is freed.
1063 **/
1064static void
1065__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1066{
1067 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1068
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001069 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001070
1071 /*
James Smart3772a992009-05-22 14:50:54 -04001072 * Clean all volatile data fields, preserve iotag and node struct.
1073 */
1074 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1075 iocbq->sli4_xritag = NO_XRI;
1076 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1077}
1078
1079/**
James Smart3621a712009-04-06 18:47:14 -04001080 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001081 * @phba: Pointer to HBA context object.
1082 * @iocbq: Pointer to driver iocb object.
1083 *
1084 * This function is called with hbalock held to release driver
1085 * iocb object to the iocb pool. The iotag in the iocb object
1086 * does not change for each use of the iocb object. This function
1087 * clears all other fields of the iocb object when it is freed.
1088 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001089static void
James Smart2e0fef82007-06-17 19:56:36 -05001090__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1091{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001092 lockdep_assert_held(&phba->hbalock);
1093
James Smart3772a992009-05-22 14:50:54 -04001094 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001095 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001096}
1097
James Smarte59058c2008-08-24 21:49:00 -04001098/**
James Smart3621a712009-04-06 18:47:14 -04001099 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001100 * @phba: Pointer to HBA context object.
1101 * @iocbq: Pointer to driver iocb object.
1102 *
1103 * This function is called with no lock held to release the iocb to
1104 * iocb pool.
1105 **/
James Smart2e0fef82007-06-17 19:56:36 -05001106void
1107lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1108{
1109 unsigned long iflags;
1110
1111 /*
1112 * Clean all volatile data fields, preserve iotag and node struct.
1113 */
1114 spin_lock_irqsave(&phba->hbalock, iflags);
1115 __lpfc_sli_release_iocbq(phba, iocbq);
1116 spin_unlock_irqrestore(&phba->hbalock, iflags);
1117}
1118
James Smarte59058c2008-08-24 21:49:00 -04001119/**
James Smarta257bf92009-04-06 18:48:10 -04001120 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1121 * @phba: Pointer to HBA context object.
1122 * @iocblist: List of IOCBs.
1123 * @ulpstatus: ULP status in IOCB command field.
1124 * @ulpWord4: ULP word-4 in IOCB command field.
1125 *
1126 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1127 * on the list by invoking the complete callback function associated with the
1128 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1129 * fields.
1130 **/
1131void
1132lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1133 uint32_t ulpstatus, uint32_t ulpWord4)
1134{
1135 struct lpfc_iocbq *piocb;
1136
1137 while (!list_empty(iocblist)) {
1138 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001139 if (!piocb->iocb_cmpl)
1140 lpfc_sli_release_iocbq(phba, piocb);
1141 else {
1142 piocb->iocb.ulpStatus = ulpstatus;
1143 piocb->iocb.un.ulpWord[4] = ulpWord4;
1144 (piocb->iocb_cmpl) (phba, piocb, piocb);
1145 }
1146 }
1147 return;
1148}
1149
1150/**
James Smart3621a712009-04-06 18:47:14 -04001151 * lpfc_sli_iocb_cmd_type - Get the iocb type
1152 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001153 *
1154 * This function is called by ring event handler function to get the iocb type.
1155 * This function translates the iocb command to an iocb command type used to
1156 * decide the final disposition of each completed IOCB.
1157 * The function returns
1158 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1159 * LPFC_SOL_IOCB if it is a solicited iocb completion
1160 * LPFC_ABORT_IOCB if it is an abort iocb
1161 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1162 *
1163 * The caller is not required to hold any lock.
1164 **/
dea31012005-04-17 16:05:31 -05001165static lpfc_iocb_type
1166lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1167{
1168 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1169
1170 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1171 return 0;
1172
1173 switch (iocb_cmnd) {
1174 case CMD_XMIT_SEQUENCE_CR:
1175 case CMD_XMIT_SEQUENCE_CX:
1176 case CMD_XMIT_BCAST_CN:
1177 case CMD_XMIT_BCAST_CX:
1178 case CMD_ELS_REQUEST_CR:
1179 case CMD_ELS_REQUEST_CX:
1180 case CMD_CREATE_XRI_CR:
1181 case CMD_CREATE_XRI_CX:
1182 case CMD_GET_RPI_CN:
1183 case CMD_XMIT_ELS_RSP_CX:
1184 case CMD_GET_RPI_CR:
1185 case CMD_FCP_IWRITE_CR:
1186 case CMD_FCP_IWRITE_CX:
1187 case CMD_FCP_IREAD_CR:
1188 case CMD_FCP_IREAD_CX:
1189 case CMD_FCP_ICMND_CR:
1190 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001191 case CMD_FCP_TSEND_CX:
1192 case CMD_FCP_TRSP_CX:
1193 case CMD_FCP_TRECEIVE_CX:
1194 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001195 case CMD_ADAPTER_MSG:
1196 case CMD_ADAPTER_DUMP:
1197 case CMD_XMIT_SEQUENCE64_CR:
1198 case CMD_XMIT_SEQUENCE64_CX:
1199 case CMD_XMIT_BCAST64_CN:
1200 case CMD_XMIT_BCAST64_CX:
1201 case CMD_ELS_REQUEST64_CR:
1202 case CMD_ELS_REQUEST64_CX:
1203 case CMD_FCP_IWRITE64_CR:
1204 case CMD_FCP_IWRITE64_CX:
1205 case CMD_FCP_IREAD64_CR:
1206 case CMD_FCP_IREAD64_CX:
1207 case CMD_FCP_ICMND64_CR:
1208 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001209 case CMD_FCP_TSEND64_CX:
1210 case CMD_FCP_TRSP64_CX:
1211 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001212 case CMD_GEN_REQUEST64_CR:
1213 case CMD_GEN_REQUEST64_CX:
1214 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001215 case DSSCMD_IWRITE64_CR:
1216 case DSSCMD_IWRITE64_CX:
1217 case DSSCMD_IREAD64_CR:
1218 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001219 type = LPFC_SOL_IOCB;
1220 break;
1221 case CMD_ABORT_XRI_CN:
1222 case CMD_ABORT_XRI_CX:
1223 case CMD_CLOSE_XRI_CN:
1224 case CMD_CLOSE_XRI_CX:
1225 case CMD_XRI_ABORTED_CX:
1226 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001227 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001228 type = LPFC_ABORT_IOCB;
1229 break;
1230 case CMD_RCV_SEQUENCE_CX:
1231 case CMD_RCV_ELS_REQ_CX:
1232 case CMD_RCV_SEQUENCE64_CX:
1233 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001234 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001235 case CMD_IOCB_RCV_SEQ64_CX:
1236 case CMD_IOCB_RCV_ELS64_CX:
1237 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001238 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001239 type = LPFC_UNSOL_IOCB;
1240 break;
James Smart3163f722008-02-08 18:50:25 -05001241 case CMD_IOCB_XMIT_MSEQ64_CR:
1242 case CMD_IOCB_XMIT_MSEQ64_CX:
1243 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1244 case CMD_IOCB_RCV_ELS_LIST64_CX:
1245 case CMD_IOCB_CLOSE_EXTENDED_CN:
1246 case CMD_IOCB_ABORT_EXTENDED_CN:
1247 case CMD_IOCB_RET_HBQE64_CN:
1248 case CMD_IOCB_FCP_IBIDIR64_CR:
1249 case CMD_IOCB_FCP_IBIDIR64_CX:
1250 case CMD_IOCB_FCP_ITASKMGT64_CX:
1251 case CMD_IOCB_LOGENTRY_CN:
1252 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1253 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001254 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001255 type = LPFC_UNKNOWN_IOCB;
1256 break;
dea31012005-04-17 16:05:31 -05001257 default:
1258 type = LPFC_UNKNOWN_IOCB;
1259 break;
1260 }
1261
1262 return type;
1263}
1264
James Smarte59058c2008-08-24 21:49:00 -04001265/**
James Smart3621a712009-04-06 18:47:14 -04001266 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001267 * @phba: Pointer to HBA context object.
1268 *
1269 * This function is called from SLI initialization code
1270 * to configure every ring of the HBA's SLI interface. The
1271 * caller is not required to hold any lock. This function issues
1272 * a config_ring mailbox command for each ring.
1273 * This function returns zero if successful else returns a negative
1274 * error code.
1275 **/
dea31012005-04-17 16:05:31 -05001276static int
James Smarted957682007-06-17 19:56:37 -05001277lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001278{
1279 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001280 LPFC_MBOXQ_t *pmb;
1281 MAILBOX_t *pmbox;
1282 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001283
James Smarted957682007-06-17 19:56:37 -05001284 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1285 if (!pmb)
1286 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001287 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001288 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001289 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001290 lpfc_config_ring(phba, i, pmb);
1291 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1292 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001294 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001295 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1296 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001297 rc, pmbox->mbxCommand,
1298 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001299 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001300 ret = -ENXIO;
1301 break;
dea31012005-04-17 16:05:31 -05001302 }
1303 }
James Smarted957682007-06-17 19:56:37 -05001304 mempool_free(pmb, phba->mbox_mem_pool);
1305 return ret;
dea31012005-04-17 16:05:31 -05001306}
1307
James Smarte59058c2008-08-24 21:49:00 -04001308/**
James Smart3621a712009-04-06 18:47:14 -04001309 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001310 * @phba: Pointer to HBA context object.
1311 * @pring: Pointer to driver SLI ring object.
1312 * @piocb: Pointer to the driver iocb object.
1313 *
1314 * This function is called with hbalock held. The function adds the
1315 * new iocb to txcmplq of the given ring. This function always returns
1316 * 0. If this function is called for ELS ring, this function checks if
1317 * there is a vport associated with the ELS command. This function also
1318 * starts els_tmofunc timer if this is an ELS command.
1319 **/
dea31012005-04-17 16:05:31 -05001320static int
James Smart2e0fef82007-06-17 19:56:36 -05001321lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1322 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001323{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001324 lockdep_assert_held(&phba->hbalock);
1325
dea31012005-04-17 16:05:31 -05001326 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001327 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001328
James Smart92d7f7b2007-06-17 19:56:38 -05001329 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1330 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
James Smart06918ac2014-02-20 09:57:57 -05001331 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN) &&
1332 (!(piocb->vport->load_flag & FC_UNLOADING))) {
James Smart92d7f7b2007-06-17 19:56:38 -05001333 if (!piocb->vport)
1334 BUG();
1335 else
1336 mod_timer(&piocb->vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001337 jiffies +
1338 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
James Smart92d7f7b2007-06-17 19:56:38 -05001339 }
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 *
2003 * This function is called with hbalock held. This function searches
2004 * for the hbq buffer associated with the given tag in the hbq buffer
2005 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
2006 * it returns NULL.
2007 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002008static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002009lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2010{
James Smart92d7f7b2007-06-17 19:56:38 -05002011 struct lpfc_dmabuf *d_buf;
2012 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002013 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002014
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002015 lockdep_assert_held(&phba->hbalock);
2016
James Smart51ef4c22007-08-02 11:10:31 -04002017 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002018 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002019 return NULL;
2020
James Smart3772a992009-05-22 14:50:54 -04002021 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002022 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002023 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002024 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002025 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002026 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002027 }
2028 }
James Smart3772a992009-05-22 14:50:54 -04002029 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002030 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002031 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002032 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002033 return NULL;
James Smarted957682007-06-17 19:56:37 -05002034}
2035
James Smarte59058c2008-08-24 21:49:00 -04002036/**
James Smart3621a712009-04-06 18:47:14 -04002037 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002038 * @phba: Pointer to HBA context object.
2039 * @hbq_buffer: Pointer to HBQ buffer.
2040 *
2041 * This function is called with hbalock. This function gives back
2042 * the hbq buffer to firmware. If the HBQ does not have space to
2043 * post the buffer, it will free the buffer.
2044 **/
James Smarted957682007-06-17 19:56:37 -05002045void
James Smart51ef4c22007-08-02 11:10:31 -04002046lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002047{
2048 uint32_t hbqno;
2049
James Smart51ef4c22007-08-02 11:10:31 -04002050 if (hbq_buffer) {
2051 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002052 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002053 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002054 }
2055}
2056
James Smarte59058c2008-08-24 21:49:00 -04002057/**
James Smart3621a712009-04-06 18:47:14 -04002058 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002059 * @mbxCommand: mailbox command code.
2060 *
2061 * This function is called by the mailbox event handler function to verify
2062 * that the completed mailbox command is a legitimate mailbox command. If the
2063 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2064 * and the mailbox event handler will take the HBA offline.
2065 **/
dea31012005-04-17 16:05:31 -05002066static int
2067lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2068{
2069 uint8_t ret;
2070
2071 switch (mbxCommand) {
2072 case MBX_LOAD_SM:
2073 case MBX_READ_NV:
2074 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002075 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002076 case MBX_RUN_BIU_DIAG:
2077 case MBX_INIT_LINK:
2078 case MBX_DOWN_LINK:
2079 case MBX_CONFIG_LINK:
2080 case MBX_CONFIG_RING:
2081 case MBX_RESET_RING:
2082 case MBX_READ_CONFIG:
2083 case MBX_READ_RCONFIG:
2084 case MBX_READ_SPARM:
2085 case MBX_READ_STATUS:
2086 case MBX_READ_RPI:
2087 case MBX_READ_XRI:
2088 case MBX_READ_REV:
2089 case MBX_READ_LNK_STAT:
2090 case MBX_REG_LOGIN:
2091 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002092 case MBX_CLEAR_LA:
2093 case MBX_DUMP_MEMORY:
2094 case MBX_DUMP_CONTEXT:
2095 case MBX_RUN_DIAGS:
2096 case MBX_RESTART:
2097 case MBX_UPDATE_CFG:
2098 case MBX_DOWN_LOAD:
2099 case MBX_DEL_LD_ENTRY:
2100 case MBX_RUN_PROGRAM:
2101 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002102 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002103 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002104 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002105 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002106 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002107 case MBX_LOAD_AREA:
2108 case MBX_RUN_BIU_DIAG64:
2109 case MBX_CONFIG_PORT:
2110 case MBX_READ_SPARM64:
2111 case MBX_READ_RPI64:
2112 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002113 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002114 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002115 case MBX_SET_DEBUG:
2116 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002117 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002118 case MBX_REG_VPI:
2119 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002120 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002121 case MBX_PORT_CAPABILITIES:
2122 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002123 case MBX_SLI4_CONFIG:
2124 case MBX_SLI4_REQ_FTRS:
2125 case MBX_REG_FCFI:
2126 case MBX_UNREG_FCFI:
2127 case MBX_REG_VFI:
2128 case MBX_UNREG_VFI:
2129 case MBX_INIT_VPI:
2130 case MBX_INIT_VFI:
2131 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002132 case MBX_READ_EVENT_LOG_STATUS:
2133 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002134 case MBX_SECURITY_MGMT:
2135 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002136 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002137 ret = mbxCommand;
2138 break;
2139 default:
2140 ret = MBX_SHUTDOWN;
2141 break;
2142 }
James Smart2e0fef82007-06-17 19:56:36 -05002143 return ret;
dea31012005-04-17 16:05:31 -05002144}
James Smarte59058c2008-08-24 21:49:00 -04002145
2146/**
James Smart3621a712009-04-06 18:47:14 -04002147 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002148 * @phba: Pointer to HBA context object.
2149 * @pmboxq: Pointer to mailbox command.
2150 *
2151 * This is completion handler function for mailbox commands issued from
2152 * lpfc_sli_issue_mbox_wait function. This function is called by the
2153 * mailbox event handler function with no lock held. This function
2154 * will wake up thread waiting on the wait queue pointed by context1
2155 * of the mailbox.
2156 **/
James Smart04c68492009-05-22 14:52:52 -04002157void
James Smart2e0fef82007-06-17 19:56:36 -05002158lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002159{
2160 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002161 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002162
2163 /*
2164 * If pdone_q is empty, the driver thread gave up waiting and
2165 * continued running.
2166 */
James Smart7054a602007-04-25 09:52:34 -04002167 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002168 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002169 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2170 if (pdone_q)
2171 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002172 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002173 return;
2174}
2175
James Smarte59058c2008-08-24 21:49:00 -04002176
2177/**
James Smart3621a712009-04-06 18:47:14 -04002178 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002179 * @phba: Pointer to HBA context object.
2180 * @pmb: Pointer to mailbox object.
2181 *
2182 * This function is the default mailbox completion handler. It
2183 * frees the memory resources associated with the completed mailbox
2184 * command. If the completed command is a REG_LOGIN mailbox command,
2185 * this function will issue a UREG_LOGIN to re-claim the RPI.
2186 **/
dea31012005-04-17 16:05:31 -05002187void
James Smart2e0fef82007-06-17 19:56:36 -05002188lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002189{
James Smartd439d282010-09-29 11:18:45 -04002190 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002191 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002192 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002193 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002194 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002195 int rc;
2196
dea31012005-04-17 16:05:31 -05002197 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002198
dea31012005-04-17 16:05:31 -05002199 if (mp) {
2200 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2201 kfree(mp);
2202 }
James Smart7054a602007-04-25 09:52:34 -04002203
2204 /*
2205 * If a REG_LOGIN succeeded after node is destroyed or node
2206 * is in re-discovery driver need to cleanup the RPI.
2207 */
James Smart2e0fef82007-06-17 19:56:36 -05002208 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002209 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2210 !pmb->u.mb.mbxStatus) {
2211 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002212 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002213 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002214 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002215 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002216 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2217 if (rc != MBX_NOT_FINISHED)
2218 return;
2219 }
2220
James Smart695a8142010-01-26 23:08:03 -05002221 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2222 !(phba->pport->load_flag & FC_UNLOADING) &&
2223 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002224 shost = lpfc_shost_from_vport(vport);
2225 spin_lock_irq(shost->host_lock);
2226 vport->vpi_state |= LPFC_VPI_REGISTERED;
2227 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2228 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002229 }
2230
James Smartd439d282010-09-29 11:18:45 -04002231 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2232 ndlp = (struct lpfc_nodelist *)pmb->context2;
2233 lpfc_nlp_put(ndlp);
2234 pmb->context2 = NULL;
2235 }
2236
James Smartdcf2a4e2010-09-29 11:18:53 -04002237 /* Check security permission status on INIT_LINK mailbox command */
2238 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2239 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2241 "2860 SLI authentication is required "
2242 "for INIT_LINK but has not done yet\n");
2243
James Smart04c68492009-05-22 14:52:52 -04002244 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2245 lpfc_sli4_mbox_cmd_free(phba, pmb);
2246 else
2247 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002248}
James Smartbe6bb942015-04-07 15:07:22 -04002249 /**
2250 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2251 * @phba: Pointer to HBA context object.
2252 * @pmb: Pointer to mailbox object.
2253 *
2254 * This function is the unreg rpi mailbox completion handler. It
2255 * frees the memory resources associated with the completed mailbox
2256 * command. An additional refrenece is put on the ndlp to prevent
2257 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2258 * the unreg mailbox command completes, this routine puts the
2259 * reference back.
2260 *
2261 **/
2262void
2263lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2264{
2265 struct lpfc_vport *vport = pmb->vport;
2266 struct lpfc_nodelist *ndlp;
2267
2268 ndlp = pmb->context1;
2269 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2270 if (phba->sli_rev == LPFC_SLI_REV4 &&
2271 (bf_get(lpfc_sli_intf_if_type,
2272 &phba->sli4_hba.sli_intf) ==
2273 LPFC_SLI_INTF_IF_TYPE_2)) {
2274 if (ndlp) {
2275 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2276 "0010 UNREG_LOGIN vpi:%x "
2277 "rpi:%x DID:%x map:%x %p\n",
2278 vport->vpi, ndlp->nlp_rpi,
2279 ndlp->nlp_DID,
2280 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002281 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002282 lpfc_nlp_put(ndlp);
2283 }
2284 }
2285 }
2286
2287 mempool_free(pmb, phba->mbox_mem_pool);
2288}
dea31012005-04-17 16:05:31 -05002289
James Smarte59058c2008-08-24 21:49:00 -04002290/**
James Smart3621a712009-04-06 18:47:14 -04002291 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002292 * @phba: Pointer to HBA context object.
2293 *
2294 * This function is called with no lock held. This function processes all
2295 * the completed mailbox commands and gives it to upper layers. The interrupt
2296 * service routine processes mailbox completion interrupt and adds completed
2297 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2298 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2299 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2300 * function returns the mailbox commands to the upper layer by calling the
2301 * completion handler function of each mailbox.
2302 **/
dea31012005-04-17 16:05:31 -05002303int
James Smart2e0fef82007-06-17 19:56:36 -05002304lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002305{
James Smart92d7f7b2007-06-17 19:56:38 -05002306 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002307 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002308 int rc;
2309 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002310
2311 phba->sli.slistat.mbox_event++;
2312
James Smart92d7f7b2007-06-17 19:56:38 -05002313 /* Get all completed mailboxe buffers into the cmplq */
2314 spin_lock_irq(&phba->hbalock);
2315 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2316 spin_unlock_irq(&phba->hbalock);
2317
dea31012005-04-17 16:05:31 -05002318 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002319 do {
2320 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2321 if (pmb == NULL)
2322 break;
2323
James Smart04c68492009-05-22 14:52:52 -04002324 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002325
James Smart858c9f62007-06-17 19:56:39 -05002326 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2327 if (pmb->vport) {
2328 lpfc_debugfs_disc_trc(pmb->vport,
2329 LPFC_DISC_TRC_MBOX_VPORT,
2330 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2331 (uint32_t)pmbox->mbxCommand,
2332 pmbox->un.varWords[0],
2333 pmbox->un.varWords[1]);
2334 }
2335 else {
2336 lpfc_debugfs_disc_trc(phba->pport,
2337 LPFC_DISC_TRC_MBOX,
2338 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2339 (uint32_t)pmbox->mbxCommand,
2340 pmbox->un.varWords[0],
2341 pmbox->un.varWords[1]);
2342 }
2343 }
2344
dea31012005-04-17 16:05:31 -05002345 /*
2346 * It is a fatal error if unknown mbox command completion.
2347 */
2348 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2349 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002350 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002351 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002352 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002353 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002354 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002355 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002356 lpfc_sli_config_mbox_subsys_get(phba,
2357 pmb),
2358 lpfc_sli_config_mbox_opcode_get(phba,
2359 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002360 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002361 phba->work_hs = HS_FFER3;
2362 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002363 continue;
dea31012005-04-17 16:05:31 -05002364 }
2365
dea31012005-04-17 16:05:31 -05002366 if (pmbox->mbxStatus) {
2367 phba->sli.slistat.mbox_stat_err++;
2368 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2369 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002370 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002371 LOG_MBOX | LOG_SLI,
2372 "(%d):0305 Mbox cmd cmpl "
2373 "error - RETRYing Data: x%x "
2374 "(x%x/x%x) x%x x%x x%x\n",
2375 pmb->vport ? pmb->vport->vpi : 0,
2376 pmbox->mbxCommand,
2377 lpfc_sli_config_mbox_subsys_get(phba,
2378 pmb),
2379 lpfc_sli_config_mbox_opcode_get(phba,
2380 pmb),
2381 pmbox->mbxStatus,
2382 pmbox->un.varWords[0],
2383 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002384 pmbox->mbxStatus = 0;
2385 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002386 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002387 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002388 continue;
dea31012005-04-17 16:05:31 -05002389 }
2390 }
2391
2392 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002393 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002394 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002395 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2396 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002397 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002398 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002399 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2400 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002401 pmb->mbox_cmpl,
2402 *((uint32_t *) pmbox),
2403 pmbox->un.varWords[0],
2404 pmbox->un.varWords[1],
2405 pmbox->un.varWords[2],
2406 pmbox->un.varWords[3],
2407 pmbox->un.varWords[4],
2408 pmbox->un.varWords[5],
2409 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002410 pmbox->un.varWords[7],
2411 pmbox->un.varWords[8],
2412 pmbox->un.varWords[9],
2413 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002414
James Smart92d7f7b2007-06-17 19:56:38 -05002415 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002416 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002417 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002418 return 0;
dea31012005-04-17 16:05:31 -05002419}
James Smart92d7f7b2007-06-17 19:56:38 -05002420
James Smarte59058c2008-08-24 21:49:00 -04002421/**
James Smart3621a712009-04-06 18:47:14 -04002422 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002423 * @phba: Pointer to HBA context object.
2424 * @pring: Pointer to driver SLI ring object.
2425 * @tag: buffer tag.
2426 *
2427 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2428 * is set in the tag the buffer is posted for a particular exchange,
2429 * the function will return the buffer without replacing the buffer.
2430 * If the buffer is for unsolicited ELS or CT traffic, this function
2431 * returns the buffer and also posts another buffer to the firmware.
2432 **/
James Smart76bb24e2007-10-27 13:38:00 -04002433static struct lpfc_dmabuf *
2434lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002435 struct lpfc_sli_ring *pring,
2436 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002437{
James Smart9f1e1b52008-12-04 22:39:40 -05002438 struct hbq_dmabuf *hbq_entry;
2439
James Smart76bb24e2007-10-27 13:38:00 -04002440 if (tag & QUE_BUFTAG_BIT)
2441 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002442 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2443 if (!hbq_entry)
2444 return NULL;
2445 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002446}
James Smart57127f12007-10-27 13:37:05 -04002447
James Smart3772a992009-05-22 14:50:54 -04002448/**
2449 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2450 * @phba: Pointer to HBA context object.
2451 * @pring: Pointer to driver SLI ring object.
2452 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2453 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2454 * @fch_type: the type for the first frame of the sequence.
2455 *
2456 * This function is called with no lock held. This function uses the r_ctl and
2457 * type of the received sequence to find the correct callback function to call
2458 * to process the sequence.
2459 **/
2460static int
2461lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2462 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2463 uint32_t fch_type)
2464{
2465 int i;
2466
2467 /* unSolicited Responses */
2468 if (pring->prt[0].profile) {
2469 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2470 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2471 saveq);
2472 return 1;
2473 }
2474 /* We must search, based on rctl / type
2475 for the right routine */
2476 for (i = 0; i < pring->num_mask; i++) {
2477 if ((pring->prt[i].rctl == fch_r_ctl) &&
2478 (pring->prt[i].type == fch_type)) {
2479 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2480 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2481 (phba, pring, saveq);
2482 return 1;
2483 }
2484 }
2485 return 0;
2486}
James Smarte59058c2008-08-24 21:49:00 -04002487
2488/**
James Smart3621a712009-04-06 18:47:14 -04002489 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002490 * @phba: Pointer to HBA context object.
2491 * @pring: Pointer to driver SLI ring object.
2492 * @saveq: Pointer to the unsolicited iocb.
2493 *
2494 * This function is called with no lock held by the ring event handler
2495 * when there is an unsolicited iocb posted to the response ring by the
2496 * firmware. This function gets the buffer associated with the iocbs
2497 * and calls the event handler for the ring. This function handles both
2498 * qring buffers and hbq buffers.
2499 * When the function returns 1 the caller can free the iocb object otherwise
2500 * upper layer functions will free the iocb objects.
2501 **/
dea31012005-04-17 16:05:31 -05002502static int
2503lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2504 struct lpfc_iocbq *saveq)
2505{
2506 IOCB_t * irsp;
2507 WORD5 * w5p;
2508 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002509 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002510 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002511
dea31012005-04-17 16:05:31 -05002512 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002513
2514 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2515 if (pring->lpfc_sli_rcv_async_status)
2516 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2517 else
2518 lpfc_printf_log(phba,
2519 KERN_WARNING,
2520 LOG_SLI,
2521 "0316 Ring %d handler: unexpected "
2522 "ASYNC_STATUS iocb received evt_code "
2523 "0x%x\n",
2524 pring->ringno,
2525 irsp->un.asyncstat.evt_code);
2526 return 1;
2527 }
2528
James Smart3163f722008-02-08 18:50:25 -05002529 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2530 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2531 if (irsp->ulpBdeCount > 0) {
2532 dmzbuf = lpfc_sli_get_buff(phba, pring,
2533 irsp->un.ulpWord[3]);
2534 lpfc_in_buf_free(phba, dmzbuf);
2535 }
2536
2537 if (irsp->ulpBdeCount > 1) {
2538 dmzbuf = lpfc_sli_get_buff(phba, pring,
2539 irsp->unsli3.sli3Words[3]);
2540 lpfc_in_buf_free(phba, dmzbuf);
2541 }
2542
2543 if (irsp->ulpBdeCount > 2) {
2544 dmzbuf = lpfc_sli_get_buff(phba, pring,
2545 irsp->unsli3.sli3Words[7]);
2546 lpfc_in_buf_free(phba, dmzbuf);
2547 }
2548
2549 return 1;
2550 }
2551
James Smart92d7f7b2007-06-17 19:56:38 -05002552 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002553 if (irsp->ulpBdeCount != 0) {
2554 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002555 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002556 if (!saveq->context2)
2557 lpfc_printf_log(phba,
2558 KERN_ERR,
2559 LOG_SLI,
2560 "0341 Ring %d Cannot find buffer for "
2561 "an unsolicited iocb. tag 0x%x\n",
2562 pring->ringno,
2563 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002564 }
2565 if (irsp->ulpBdeCount == 2) {
2566 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002567 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002568 if (!saveq->context3)
2569 lpfc_printf_log(phba,
2570 KERN_ERR,
2571 LOG_SLI,
2572 "0342 Ring %d Cannot find buffer for an"
2573 " unsolicited iocb. tag 0x%x\n",
2574 pring->ringno,
2575 irsp->unsli3.sli3Words[7]);
2576 }
2577 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002578 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002579 if (irsp->ulpBdeCount != 0) {
2580 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2581 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002582 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002583 lpfc_printf_log(phba,
2584 KERN_ERR,
2585 LOG_SLI,
2586 "0343 Ring %d Cannot find "
2587 "buffer for an unsolicited iocb"
2588 ". tag 0x%x\n", pring->ringno,
2589 irsp->un.ulpWord[3]);
2590 }
2591 if (irsp->ulpBdeCount == 2) {
2592 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2593 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002594 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002595 lpfc_printf_log(phba,
2596 KERN_ERR,
2597 LOG_SLI,
2598 "0344 Ring %d Cannot find "
2599 "buffer for an unsolicited "
2600 "iocb. tag 0x%x\n",
2601 pring->ringno,
2602 irsp->unsli3.sli3Words[7]);
2603 }
2604 }
James Smart92d7f7b2007-06-17 19:56:38 -05002605 }
James Smart9c2face2008-01-11 01:53:18 -05002606 if (irsp->ulpBdeCount != 0 &&
2607 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2608 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2609 int found = 0;
2610
2611 /* search continue save q for same XRI */
2612 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002613 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2614 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002615 list_add_tail(&saveq->list, &iocbq->list);
2616 found = 1;
2617 break;
2618 }
2619 }
2620 if (!found)
2621 list_add_tail(&saveq->clist,
2622 &pring->iocb_continue_saveq);
2623 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2624 list_del_init(&iocbq->clist);
2625 saveq = iocbq;
2626 irsp = &(saveq->iocb);
2627 } else
2628 return 0;
2629 }
2630 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2631 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2632 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002633 Rctl = FC_RCTL_ELS_REQ;
2634 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002635 } else {
2636 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2637 Rctl = w5p->hcsw.Rctl;
2638 Type = w5p->hcsw.Type;
2639
2640 /* Firmware Workaround */
2641 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2642 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2643 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002644 Rctl = FC_RCTL_ELS_REQ;
2645 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002646 w5p->hcsw.Rctl = Rctl;
2647 w5p->hcsw.Type = Type;
2648 }
2649 }
James Smart92d7f7b2007-06-17 19:56:38 -05002650
James Smart3772a992009-05-22 14:50:54 -04002651 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002652 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002653 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002654 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002655 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002656
James Smart92d7f7b2007-06-17 19:56:38 -05002657 return 1;
dea31012005-04-17 16:05:31 -05002658}
2659
James Smarte59058c2008-08-24 21:49:00 -04002660/**
James Smart3621a712009-04-06 18:47:14 -04002661 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002662 * @phba: Pointer to HBA context object.
2663 * @pring: Pointer to driver SLI ring object.
2664 * @prspiocb: Pointer to response iocb object.
2665 *
2666 * This function looks up the iocb_lookup table to get the command iocb
2667 * corresponding to the given response iocb using the iotag of the
2668 * response iocb. This function is called with the hbalock held.
2669 * This function returns the command iocb object if it finds the command
2670 * iocb else returns NULL.
2671 **/
dea31012005-04-17 16:05:31 -05002672static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002673lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2674 struct lpfc_sli_ring *pring,
2675 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002676{
dea31012005-04-17 16:05:31 -05002677 struct lpfc_iocbq *cmd_iocb = NULL;
2678 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002679 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002680
James Bottomley604a3e32005-10-29 10:28:33 -05002681 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002682
James Bottomley604a3e32005-10-29 10:28:33 -05002683 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2684 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002685 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002686 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002687 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002688 }
James Bottomley604a3e32005-10-29 10:28:33 -05002689 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002690 }
2691
dea31012005-04-17 16:05:31 -05002692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002693 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002694 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002695 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002696 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002697 return NULL;
2698}
2699
James Smarte59058c2008-08-24 21:49:00 -04002700/**
James Smart3772a992009-05-22 14:50:54 -04002701 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2702 * @phba: Pointer to HBA context object.
2703 * @pring: Pointer to driver SLI ring object.
2704 * @iotag: IOCB tag.
2705 *
2706 * This function looks up the iocb_lookup table to get the command iocb
2707 * corresponding to the given iotag. This function is called with the
2708 * hbalock held.
2709 * This function returns the command iocb object if it finds the command
2710 * iocb else returns NULL.
2711 **/
2712static struct lpfc_iocbq *
2713lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2714 struct lpfc_sli_ring *pring, uint16_t iotag)
2715{
2716 struct lpfc_iocbq *cmd_iocb;
2717
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002718 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002719 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2720 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002721 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2722 /* remove from txcmpl queue list */
2723 list_del_init(&cmd_iocb->list);
2724 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002725 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002726 }
James Smart3772a992009-05-22 14:50:54 -04002727 }
James Smart3772a992009-05-22 14:50:54 -04002728 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2729 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2730 iotag, phba->sli.last_iotag);
2731 return NULL;
2732}
2733
2734/**
James Smart3621a712009-04-06 18:47:14 -04002735 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002736 * @phba: Pointer to HBA context object.
2737 * @pring: Pointer to driver SLI ring object.
2738 * @saveq: Pointer to the response iocb to be processed.
2739 *
2740 * This function is called by the ring event handler for non-fcp
2741 * rings when there is a new response iocb in the response ring.
2742 * The caller is not required to hold any locks. This function
2743 * gets the command iocb associated with the response iocb and
2744 * calls the completion handler for the command iocb. If there
2745 * is no completion handler, the function will free the resources
2746 * associated with command iocb. If the response iocb is for
2747 * an already aborted command iocb, the status of the completion
2748 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2749 * This function always returns 1.
2750 **/
dea31012005-04-17 16:05:31 -05002751static int
James Smart2e0fef82007-06-17 19:56:36 -05002752lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002753 struct lpfc_iocbq *saveq)
2754{
James Smart2e0fef82007-06-17 19:56:36 -05002755 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002756 int rc = 1;
2757 unsigned long iflag;
2758
2759 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002760 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002761 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002762 spin_unlock_irqrestore(&phba->hbalock, iflag);
2763
dea31012005-04-17 16:05:31 -05002764 if (cmdiocbp) {
2765 if (cmdiocbp->iocb_cmpl) {
2766 /*
James Smartea2151b2008-09-07 11:52:10 -04002767 * If an ELS command failed send an event to mgmt
2768 * application.
2769 */
2770 if (saveq->iocb.ulpStatus &&
2771 (pring->ringno == LPFC_ELS_RING) &&
2772 (cmdiocbp->iocb.ulpCommand ==
2773 CMD_ELS_REQUEST64_CR))
2774 lpfc_send_els_failure_event(phba,
2775 cmdiocbp, saveq);
2776
2777 /*
dea31012005-04-17 16:05:31 -05002778 * Post all ELS completions to the worker thread.
2779 * All other are passed to the completion callback.
2780 */
2781 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002782 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2783 (cmdiocbp->iocb_flag &
2784 LPFC_DRIVER_ABORTED)) {
2785 spin_lock_irqsave(&phba->hbalock,
2786 iflag);
James Smart07951072007-04-25 09:51:38 -04002787 cmdiocbp->iocb_flag &=
2788 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002789 spin_unlock_irqrestore(&phba->hbalock,
2790 iflag);
James Smart07951072007-04-25 09:51:38 -04002791 saveq->iocb.ulpStatus =
2792 IOSTAT_LOCAL_REJECT;
2793 saveq->iocb.un.ulpWord[4] =
2794 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002795
2796 /* Firmware could still be in progress
2797 * of DMAing payload, so don't free data
2798 * buffer till after a hbeat.
2799 */
James Smart341af102010-01-26 23:07:37 -05002800 spin_lock_irqsave(&phba->hbalock,
2801 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002802 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002803 spin_unlock_irqrestore(&phba->hbalock,
2804 iflag);
2805 }
James Smart0f65ff62010-02-26 14:14:23 -05002806 if (phba->sli_rev == LPFC_SLI_REV4) {
2807 if (saveq->iocb_flag &
2808 LPFC_EXCHANGE_BUSY) {
2809 /* Set cmdiocb flag for the
2810 * exchange busy so sgl (xri)
2811 * will not be released until
2812 * the abort xri is received
2813 * from hba.
2814 */
2815 spin_lock_irqsave(
2816 &phba->hbalock, iflag);
2817 cmdiocbp->iocb_flag |=
2818 LPFC_EXCHANGE_BUSY;
2819 spin_unlock_irqrestore(
2820 &phba->hbalock, iflag);
2821 }
2822 if (cmdiocbp->iocb_flag &
2823 LPFC_DRIVER_ABORTED) {
2824 /*
2825 * Clear LPFC_DRIVER_ABORTED
2826 * bit in case it was driver
2827 * initiated abort.
2828 */
2829 spin_lock_irqsave(
2830 &phba->hbalock, iflag);
2831 cmdiocbp->iocb_flag &=
2832 ~LPFC_DRIVER_ABORTED;
2833 spin_unlock_irqrestore(
2834 &phba->hbalock, iflag);
2835 cmdiocbp->iocb.ulpStatus =
2836 IOSTAT_LOCAL_REJECT;
2837 cmdiocbp->iocb.un.ulpWord[4] =
2838 IOERR_ABORT_REQUESTED;
2839 /*
2840 * For SLI4, irsiocb contains
2841 * NO_XRI in sli_xritag, it
2842 * shall not affect releasing
2843 * sgl (xri) process.
2844 */
2845 saveq->iocb.ulpStatus =
2846 IOSTAT_LOCAL_REJECT;
2847 saveq->iocb.un.ulpWord[4] =
2848 IOERR_SLI_ABORTED;
2849 spin_lock_irqsave(
2850 &phba->hbalock, iflag);
2851 saveq->iocb_flag |=
2852 LPFC_DELAY_MEM_FREE;
2853 spin_unlock_irqrestore(
2854 &phba->hbalock, iflag);
2855 }
James Smart07951072007-04-25 09:51:38 -04002856 }
dea31012005-04-17 16:05:31 -05002857 }
James Smart2e0fef82007-06-17 19:56:36 -05002858 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002859 } else
2860 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002861 } else {
2862 /*
2863 * Unknown initiating command based on the response iotag.
2864 * This could be the case on the ELS ring because of
2865 * lpfc_els_abort().
2866 */
2867 if (pring->ringno != LPFC_ELS_RING) {
2868 /*
2869 * Ring <ringno> handler: unexpected completion IoTag
2870 * <IoTag>
2871 */
James Smarta257bf92009-04-06 18:48:10 -04002872 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002873 "0322 Ring %d handler: "
2874 "unexpected completion IoTag x%x "
2875 "Data: x%x x%x x%x x%x\n",
2876 pring->ringno,
2877 saveq->iocb.ulpIoTag,
2878 saveq->iocb.ulpStatus,
2879 saveq->iocb.un.ulpWord[4],
2880 saveq->iocb.ulpCommand,
2881 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002882 }
2883 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002884
dea31012005-04-17 16:05:31 -05002885 return rc;
2886}
2887
James Smarte59058c2008-08-24 21:49:00 -04002888/**
James Smart3621a712009-04-06 18:47:14 -04002889 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002890 * @phba: Pointer to HBA context object.
2891 * @pring: Pointer to driver SLI ring object.
2892 *
2893 * This function is called from the iocb ring event handlers when
2894 * put pointer is ahead of the get pointer for a ring. This function signal
2895 * an error attention condition to the worker thread and the worker
2896 * thread will transition the HBA to offline state.
2897 **/
James Smart2e0fef82007-06-17 19:56:36 -05002898static void
2899lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002900{
James Smart34b02dc2008-08-24 21:49:55 -04002901 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002902 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002903 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002904 * rsp ring <portRspMax>
2905 */
2906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002907 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002908 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002909 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002910 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002911
James Smart2e0fef82007-06-17 19:56:36 -05002912 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002913
2914 /*
2915 * All error attention handlers are posted to
2916 * worker thread
2917 */
2918 phba->work_ha |= HA_ERATT;
2919 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002920
James Smart5e9d9b82008-06-14 22:52:53 -04002921 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002922
2923 return;
2924}
2925
James Smarte59058c2008-08-24 21:49:00 -04002926/**
James Smart3621a712009-04-06 18:47:14 -04002927 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002928 * @ptr: Pointer to address of HBA context object.
2929 *
2930 * This function is invoked by the Error Attention polling timer when the
2931 * timer times out. It will check the SLI Error Attention register for
2932 * possible attention events. If so, it will post an Error Attention event
2933 * and wake up worker thread to process it. Otherwise, it will set up the
2934 * Error Attention polling timer for the next poll.
2935 **/
2936void lpfc_poll_eratt(unsigned long ptr)
2937{
2938 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04002939 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04002940 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002941
2942 phba = (struct lpfc_hba *)ptr;
2943
James Smartaa6fbb72012-08-03 12:36:03 -04002944 /* Here we will also keep track of interrupts per sec of the hba */
2945 sli_intr = phba->sli.slistat.sli_intr;
2946
2947 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2948 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2949 sli_intr);
2950 else
2951 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2952
2953 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
James Smarteb016562014-09-03 12:58:06 -04002954 do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
James Smartaa6fbb72012-08-03 12:36:03 -04002955 phba->sli.slistat.sli_ips = cnt;
2956
2957 phba->sli.slistat.sli_prev_intr = sli_intr;
2958
James Smart93996272008-08-24 21:50:30 -04002959 /* Check chip HA register for error event */
2960 eratt = lpfc_sli_check_eratt(phba);
2961
2962 if (eratt)
2963 /* Tell the worker thread there is work to do */
2964 lpfc_worker_wake_up(phba);
2965 else
2966 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002967 mod_timer(&phba->eratt_poll,
2968 jiffies +
2969 msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smart93996272008-08-24 21:50:30 -04002970 return;
2971}
2972
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002973
James Smarte59058c2008-08-24 21:49:00 -04002974/**
James Smart3621a712009-04-06 18:47:14 -04002975 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002976 * @phba: Pointer to HBA context object.
2977 * @pring: Pointer to driver SLI ring object.
2978 * @mask: Host attention register mask for this ring.
2979 *
2980 * This function is called from the interrupt context when there is a ring
2981 * event for the fcp ring. The caller does not hold any lock.
2982 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002983 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002984 * LE bit set. The function will call the completion handler of the command iocb
2985 * if the response iocb indicates a completion for a command iocb or it is
2986 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2987 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002988 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002989 * to check it explicitly.
2990 */
2991int
James Smart2e0fef82007-06-17 19:56:36 -05002992lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2993 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002994{
James Smart34b02dc2008-08-24 21:49:55 -04002995 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002996 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002997 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002998 struct lpfc_iocbq *cmdiocbq = NULL;
2999 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003000 uint32_t status;
3001 uint32_t portRspPut, portRspMax;
3002 int rc = 1;
3003 lpfc_iocb_type type;
3004 unsigned long iflag;
3005 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003006
James Smart2e0fef82007-06-17 19:56:36 -05003007 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003008 pring->stats.iocb_event++;
3009
dea31012005-04-17 16:05:31 -05003010 /*
3011 * The next available response entry should never exceed the maximum
3012 * entries. If it does, treat it as an adapter hardware error.
3013 */
James Smart7e56aa22012-08-03 12:35:34 -04003014 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003015 portRspPut = le32_to_cpu(pgp->rspPutInx);
3016 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003017 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003018 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003019 return 1;
3020 }
James Smart45ed1192009-10-02 15:17:02 -04003021 if (phba->fcp_ring_in_use) {
3022 spin_unlock_irqrestore(&phba->hbalock, iflag);
3023 return 1;
3024 } else
3025 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003026
3027 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003028 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003029 /*
3030 * Fetch an entry off the ring and copy it into a local data
3031 * structure. The copy involves a byte-swap since the
3032 * network byte order and pci byte orders are different.
3033 */
James Smarted957682007-06-17 19:56:37 -05003034 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003035 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003036
James Smart7e56aa22012-08-03 12:35:34 -04003037 if (++pring->sli.sli3.rspidx >= portRspMax)
3038 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003039
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003040 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3041 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003042 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003043 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003044 irsp = &rspiocbq.iocb;
3045
dea31012005-04-17 16:05:31 -05003046 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3047 pring->stats.iocb_rsp++;
3048 rsp_cmpl++;
3049
3050 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003051 /*
3052 * If resource errors reported from HBA, reduce
3053 * queuedepths of the SCSI device.
3054 */
3055 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003056 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3057 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003058 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003059 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003060 spin_lock_irqsave(&phba->hbalock, iflag);
3061 }
3062
dea31012005-04-17 16:05:31 -05003063 /* Rsp ring <ringno> error: IOCB */
3064 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003065 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003066 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003067 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003068 irsp->un.ulpWord[0],
3069 irsp->un.ulpWord[1],
3070 irsp->un.ulpWord[2],
3071 irsp->un.ulpWord[3],
3072 irsp->un.ulpWord[4],
3073 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003074 *(uint32_t *)&irsp->un1,
3075 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003076 }
3077
3078 switch (type) {
3079 case LPFC_ABORT_IOCB:
3080 case LPFC_SOL_IOCB:
3081 /*
3082 * Idle exchange closed via ABTS from port. No iocb
3083 * resources need to be recovered.
3084 */
3085 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003086 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003087 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003088 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003089 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003090 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003091 break;
3092 }
3093
James Bottomley604a3e32005-10-29 10:28:33 -05003094 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3095 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003096 if (unlikely(!cmdiocbq))
3097 break;
3098 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3099 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3100 if (cmdiocbq->iocb_cmpl) {
3101 spin_unlock_irqrestore(&phba->hbalock, iflag);
3102 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3103 &rspiocbq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 }
dea31012005-04-17 16:05:31 -05003106 break;
James Smarta4bc3372006-12-02 13:34:16 -05003107 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003108 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003109 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003110 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003111 break;
dea31012005-04-17 16:05:31 -05003112 default:
3113 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3114 char adaptermsg[LPFC_MAX_ADPTMSG];
3115 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3116 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3117 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003118 dev_warn(&((phba->pcidev)->dev),
3119 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003120 phba->brd_no, adaptermsg);
3121 } else {
3122 /* Unknown IOCB command */
3123 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003124 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003125 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003126 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003127 irsp->ulpStatus,
3128 irsp->ulpIoTag,
3129 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003130 }
3131 break;
3132 }
3133
3134 /*
3135 * The response IOCB has been processed. Update the ring
3136 * pointer in SLIM. If the port response put pointer has not
3137 * been updated, sync the pgp->rspPutInx and fetch the new port
3138 * response put pointer.
3139 */
James Smart7e56aa22012-08-03 12:35:34 -04003140 writel(pring->sli.sli3.rspidx,
3141 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003142
James Smart7e56aa22012-08-03 12:35:34 -04003143 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003144 portRspPut = le32_to_cpu(pgp->rspPutInx);
3145 }
3146
3147 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3148 pring->stats.iocb_rsp_full++;
3149 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3150 writel(status, phba->CAregaddr);
3151 readl(phba->CAregaddr);
3152 }
3153 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3154 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3155 pring->stats.iocb_cmd_empty++;
3156
3157 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003158 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003159 lpfc_sli_resume_iocb(phba, pring);
3160
3161 if ((pring->lpfc_sli_cmd_available))
3162 (pring->lpfc_sli_cmd_available) (phba, pring);
3163
3164 }
3165
James Smart45ed1192009-10-02 15:17:02 -04003166 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003167 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003168 return rc;
3169}
3170
James Smarte59058c2008-08-24 21:49:00 -04003171/**
James Smart3772a992009-05-22 14:50:54 -04003172 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3173 * @phba: Pointer to HBA context object.
3174 * @pring: Pointer to driver SLI ring object.
3175 * @rspiocbp: Pointer to driver response IOCB object.
3176 *
3177 * This function is called from the worker thread when there is a slow-path
3178 * response IOCB to process. This function chains all the response iocbs until
3179 * seeing the iocb with the LE bit set. The function will call
3180 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3181 * completion of a command iocb. The function will call the
3182 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3183 * The function frees the resources or calls the completion handler if this
3184 * iocb is an abort completion. The function returns NULL when the response
3185 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3186 * this function shall chain the iocb on to the iocb_continueq and return the
3187 * response iocb passed in.
3188 **/
3189static struct lpfc_iocbq *
3190lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3191 struct lpfc_iocbq *rspiocbp)
3192{
3193 struct lpfc_iocbq *saveq;
3194 struct lpfc_iocbq *cmdiocbp;
3195 struct lpfc_iocbq *next_iocb;
3196 IOCB_t *irsp = NULL;
3197 uint32_t free_saveq;
3198 uint8_t iocb_cmd_type;
3199 lpfc_iocb_type type;
3200 unsigned long iflag;
3201 int rc;
3202
3203 spin_lock_irqsave(&phba->hbalock, iflag);
3204 /* First add the response iocb to the countinueq list */
3205 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3206 pring->iocb_continueq_cnt++;
3207
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003208 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003209 irsp = &rspiocbp->iocb;
3210 if (irsp->ulpLe) {
3211 /*
3212 * By default, the driver expects to free all resources
3213 * associated with this iocb completion.
3214 */
3215 free_saveq = 1;
3216 saveq = list_get_first(&pring->iocb_continueq,
3217 struct lpfc_iocbq, list);
3218 irsp = &(saveq->iocb);
3219 list_del_init(&pring->iocb_continueq);
3220 pring->iocb_continueq_cnt = 0;
3221
3222 pring->stats.iocb_rsp++;
3223
3224 /*
3225 * If resource errors reported from HBA, reduce
3226 * queuedepths of the SCSI device.
3227 */
3228 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003229 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3230 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003231 spin_unlock_irqrestore(&phba->hbalock, iflag);
3232 phba->lpfc_rampdown_queue_depth(phba);
3233 spin_lock_irqsave(&phba->hbalock, iflag);
3234 }
3235
3236 if (irsp->ulpStatus) {
3237 /* Rsp ring <ringno> error: IOCB */
3238 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3239 "0328 Rsp Ring %d error: "
3240 "IOCB Data: "
3241 "x%x x%x x%x x%x "
3242 "x%x x%x x%x x%x "
3243 "x%x x%x x%x x%x "
3244 "x%x x%x x%x x%x\n",
3245 pring->ringno,
3246 irsp->un.ulpWord[0],
3247 irsp->un.ulpWord[1],
3248 irsp->un.ulpWord[2],
3249 irsp->un.ulpWord[3],
3250 irsp->un.ulpWord[4],
3251 irsp->un.ulpWord[5],
3252 *(((uint32_t *) irsp) + 6),
3253 *(((uint32_t *) irsp) + 7),
3254 *(((uint32_t *) irsp) + 8),
3255 *(((uint32_t *) irsp) + 9),
3256 *(((uint32_t *) irsp) + 10),
3257 *(((uint32_t *) irsp) + 11),
3258 *(((uint32_t *) irsp) + 12),
3259 *(((uint32_t *) irsp) + 13),
3260 *(((uint32_t *) irsp) + 14),
3261 *(((uint32_t *) irsp) + 15));
3262 }
3263
3264 /*
3265 * Fetch the IOCB command type and call the correct completion
3266 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3267 * get freed back to the lpfc_iocb_list by the discovery
3268 * kernel thread.
3269 */
3270 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3271 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3272 switch (type) {
3273 case LPFC_SOL_IOCB:
3274 spin_unlock_irqrestore(&phba->hbalock, iflag);
3275 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3276 spin_lock_irqsave(&phba->hbalock, iflag);
3277 break;
3278
3279 case LPFC_UNSOL_IOCB:
3280 spin_unlock_irqrestore(&phba->hbalock, iflag);
3281 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3282 spin_lock_irqsave(&phba->hbalock, iflag);
3283 if (!rc)
3284 free_saveq = 0;
3285 break;
3286
3287 case LPFC_ABORT_IOCB:
3288 cmdiocbp = NULL;
3289 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3290 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3291 saveq);
3292 if (cmdiocbp) {
3293 /* Call the specified completion routine */
3294 if (cmdiocbp->iocb_cmpl) {
3295 spin_unlock_irqrestore(&phba->hbalock,
3296 iflag);
3297 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3298 saveq);
3299 spin_lock_irqsave(&phba->hbalock,
3300 iflag);
3301 } else
3302 __lpfc_sli_release_iocbq(phba,
3303 cmdiocbp);
3304 }
3305 break;
3306
3307 case LPFC_UNKNOWN_IOCB:
3308 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3309 char adaptermsg[LPFC_MAX_ADPTMSG];
3310 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3311 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3312 MAX_MSG_DATA);
3313 dev_warn(&((phba->pcidev)->dev),
3314 "lpfc%d: %s\n",
3315 phba->brd_no, adaptermsg);
3316 } else {
3317 /* Unknown IOCB command */
3318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3319 "0335 Unknown IOCB "
3320 "command Data: x%x "
3321 "x%x x%x x%x\n",
3322 irsp->ulpCommand,
3323 irsp->ulpStatus,
3324 irsp->ulpIoTag,
3325 irsp->ulpContext);
3326 }
3327 break;
3328 }
3329
3330 if (free_saveq) {
3331 list_for_each_entry_safe(rspiocbp, next_iocb,
3332 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003333 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003334 __lpfc_sli_release_iocbq(phba, rspiocbp);
3335 }
3336 __lpfc_sli_release_iocbq(phba, saveq);
3337 }
3338 rspiocbp = NULL;
3339 }
3340 spin_unlock_irqrestore(&phba->hbalock, iflag);
3341 return rspiocbp;
3342}
3343
3344/**
3345 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003346 * @phba: Pointer to HBA context object.
3347 * @pring: Pointer to driver SLI ring object.
3348 * @mask: Host attention register mask for this ring.
3349 *
James Smart3772a992009-05-22 14:50:54 -04003350 * This routine wraps the actual slow_ring event process routine from the
3351 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003352 **/
James Smart3772a992009-05-22 14:50:54 -04003353void
James Smart2e0fef82007-06-17 19:56:36 -05003354lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3355 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003356{
James Smart3772a992009-05-22 14:50:54 -04003357 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3358}
3359
3360/**
3361 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3362 * @phba: Pointer to HBA context object.
3363 * @pring: Pointer to driver SLI ring object.
3364 * @mask: Host attention register mask for this ring.
3365 *
3366 * This function is called from the worker thread when there is a ring event
3367 * for non-fcp rings. The caller does not hold any lock. The function will
3368 * remove each response iocb in the response ring and calls the handle
3369 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3370 **/
3371static void
3372lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3373 struct lpfc_sli_ring *pring, uint32_t mask)
3374{
James Smart34b02dc2008-08-24 21:49:55 -04003375 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003376 IOCB_t *entry;
3377 IOCB_t *irsp = NULL;
3378 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003379 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003380 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003381 uint32_t status;
dea31012005-04-17 16:05:31 -05003382
James Smart34b02dc2008-08-24 21:49:55 -04003383 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003384 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003385 pring->stats.iocb_event++;
3386
dea31012005-04-17 16:05:31 -05003387 /*
3388 * The next available response entry should never exceed the maximum
3389 * entries. If it does, treat it as an adapter hardware error.
3390 */
James Smart7e56aa22012-08-03 12:35:34 -04003391 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003392 portRspPut = le32_to_cpu(pgp->rspPutInx);
3393 if (portRspPut >= portRspMax) {
3394 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003395 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003396 * rsp ring <portRspMax>
3397 */
James Smarted957682007-06-17 19:56:37 -05003398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003399 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003400 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003401 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003402
James Smart2e0fef82007-06-17 19:56:36 -05003403 phba->link_state = LPFC_HBA_ERROR;
3404 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003405
3406 phba->work_hs = HS_FFER3;
3407 lpfc_handle_eratt(phba);
3408
James Smart3772a992009-05-22 14:50:54 -04003409 return;
dea31012005-04-17 16:05:31 -05003410 }
3411
3412 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003413 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003414 /*
3415 * Build a completion list and call the appropriate handler.
3416 * The process is to get the next available response iocb, get
3417 * a free iocb from the list, copy the response data into the
3418 * free iocb, insert to the continuation list, and update the
3419 * next response index to slim. This process makes response
3420 * iocb's in the ring available to DMA as fast as possible but
3421 * pays a penalty for a copy operation. Since the iocb is
3422 * only 32 bytes, this penalty is considered small relative to
3423 * the PCI reads for register values and a slim write. When
3424 * the ulpLe field is set, the entire Command has been
3425 * received.
3426 */
James Smarted957682007-06-17 19:56:37 -05003427 entry = lpfc_resp_iocb(phba, pring);
3428
James Smart858c9f62007-06-17 19:56:39 -05003429 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003430 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003431 if (rspiocbp == NULL) {
3432 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003433 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003434 break;
3435 }
3436
James Smarted957682007-06-17 19:56:37 -05003437 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3438 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003439 irsp = &rspiocbp->iocb;
3440
James Smart7e56aa22012-08-03 12:35:34 -04003441 if (++pring->sli.sli3.rspidx >= portRspMax)
3442 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003443
James Smarta58cbd52007-08-02 11:09:43 -04003444 if (pring->ringno == LPFC_ELS_RING) {
3445 lpfc_debugfs_slow_ring_trc(phba,
3446 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3447 *(((uint32_t *) irsp) + 4),
3448 *(((uint32_t *) irsp) + 6),
3449 *(((uint32_t *) irsp) + 7));
3450 }
3451
James Smart7e56aa22012-08-03 12:35:34 -04003452 writel(pring->sli.sli3.rspidx,
3453 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003454
James Smart3772a992009-05-22 14:50:54 -04003455 spin_unlock_irqrestore(&phba->hbalock, iflag);
3456 /* Handle the response IOCB */
3457 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3458 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003459
3460 /*
3461 * If the port response put pointer has not been updated, sync
3462 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3463 * response put pointer.
3464 */
James Smart7e56aa22012-08-03 12:35:34 -04003465 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003466 portRspPut = le32_to_cpu(pgp->rspPutInx);
3467 }
James Smart7e56aa22012-08-03 12:35:34 -04003468 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003469
James Smart92d7f7b2007-06-17 19:56:38 -05003470 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003471 /* At least one response entry has been freed */
3472 pring->stats.iocb_rsp_full++;
3473 /* SET RxRE_RSP in Chip Att register */
3474 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3475 writel(status, phba->CAregaddr);
3476 readl(phba->CAregaddr); /* flush */
3477 }
3478 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3479 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3480 pring->stats.iocb_cmd_empty++;
3481
3482 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003483 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003484 lpfc_sli_resume_iocb(phba, pring);
3485
3486 if ((pring->lpfc_sli_cmd_available))
3487 (pring->lpfc_sli_cmd_available) (phba, pring);
3488
3489 }
3490
James Smart2e0fef82007-06-17 19:56:36 -05003491 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003492 return;
dea31012005-04-17 16:05:31 -05003493}
3494
James Smarte59058c2008-08-24 21:49:00 -04003495/**
James Smart4f774512009-05-22 14:52:35 -04003496 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3497 * @phba: Pointer to HBA context object.
3498 * @pring: Pointer to driver SLI ring object.
3499 * @mask: Host attention register mask for this ring.
3500 *
3501 * This function is called from the worker thread when there is a pending
3502 * ELS response iocb on the driver internal slow-path response iocb worker
3503 * queue. The caller does not hold any lock. The function will remove each
3504 * response iocb from the response worker queue and calls the handle
3505 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3506 **/
3507static void
3508lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3509 struct lpfc_sli_ring *pring, uint32_t mask)
3510{
3511 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003512 struct hbq_dmabuf *dmabuf;
3513 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003514 unsigned long iflag;
3515
James Smart45ed1192009-10-02 15:17:02 -04003516 spin_lock_irqsave(&phba->hbalock, iflag);
3517 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3518 spin_unlock_irqrestore(&phba->hbalock, iflag);
3519 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003520 /* Get the response iocb from the head of work queue */
3521 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003522 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003523 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003524 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003525
3526 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3527 case CQE_CODE_COMPL_WQE:
3528 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3529 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003530 /* Translate ELS WCQE to response IOCBQ */
3531 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3532 irspiocbq);
3533 if (irspiocbq)
3534 lpfc_sli_sp_handle_rspiocb(phba, pring,
3535 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003536 break;
3537 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003538 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003539 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3540 cq_event);
3541 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3542 break;
3543 default:
3544 break;
3545 }
James Smart4f774512009-05-22 14:52:35 -04003546 }
3547}
3548
3549/**
James Smart3621a712009-04-06 18:47:14 -04003550 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003551 * @phba: Pointer to HBA context object.
3552 * @pring: Pointer to driver SLI ring object.
3553 *
3554 * This function aborts all iocbs in the given ring and frees all the iocb
3555 * objects in txq. This function issues an abort iocb for all the iocb commands
3556 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3557 * the return of this function. The caller is not required to hold any locks.
3558 **/
James Smart2e0fef82007-06-17 19:56:36 -05003559void
dea31012005-04-17 16:05:31 -05003560lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3561{
James Smart2534ba72007-04-25 09:52:20 -04003562 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003563 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003564
James Smart92d7f7b2007-06-17 19:56:38 -05003565 if (pring->ringno == LPFC_ELS_RING) {
3566 lpfc_fabric_abort_hba(phba);
3567 }
3568
dea31012005-04-17 16:05:31 -05003569 /* Error everything on txq and txcmplq
3570 * First do the txq.
3571 */
James Smartdb55fba2014-04-04 13:52:02 -04003572 if (phba->sli_rev >= LPFC_SLI_REV4) {
3573 spin_lock_irq(&pring->ring_lock);
3574 list_splice_init(&pring->txq, &completions);
3575 pring->txq_cnt = 0;
3576 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003577
James Smartdb55fba2014-04-04 13:52:02 -04003578 spin_lock_irq(&phba->hbalock);
3579 /* Next issue ABTS for everything on the txcmplq */
3580 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3581 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3582 spin_unlock_irq(&phba->hbalock);
3583 } else {
3584 spin_lock_irq(&phba->hbalock);
3585 list_splice_init(&pring->txq, &completions);
3586 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003587
James Smartdb55fba2014-04-04 13:52:02 -04003588 /* Next issue ABTS for everything on the txcmplq */
3589 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3590 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3591 spin_unlock_irq(&phba->hbalock);
3592 }
James Smart2534ba72007-04-25 09:52:20 -04003593
James Smarta257bf92009-04-06 18:48:10 -04003594 /* Cancel all the IOCBs from the completions list */
3595 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3596 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003597}
3598
James Smarte59058c2008-08-24 21:49:00 -04003599/**
James Smartdb55fba2014-04-04 13:52:02 -04003600 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3601 * @phba: Pointer to HBA context object.
3602 * @pring: Pointer to driver SLI ring object.
3603 *
3604 * This function aborts all iocbs in FCP rings and frees all the iocb
3605 * objects in txq. This function issues an abort iocb for all the iocb commands
3606 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3607 * the return of this function. The caller is not required to hold any locks.
3608 **/
3609void
3610lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3611{
3612 struct lpfc_sli *psli = &phba->sli;
3613 struct lpfc_sli_ring *pring;
3614 uint32_t i;
3615
3616 /* Look on all the FCP Rings for the iotag */
3617 if (phba->sli_rev >= LPFC_SLI_REV4) {
3618 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3619 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3620 lpfc_sli_abort_iocb_ring(phba, pring);
3621 }
3622 } else {
3623 pring = &psli->ring[psli->fcp_ring];
3624 lpfc_sli_abort_iocb_ring(phba, pring);
3625 }
3626}
3627
3628
3629/**
James Smart3621a712009-04-06 18:47:14 -04003630 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003631 * @phba: Pointer to HBA context object.
3632 *
3633 * This function flushes all iocbs in the fcp ring and frees all the iocb
3634 * objects in txq and txcmplq. This function will not issue abort iocbs
3635 * for all the iocb commands in txcmplq, they will just be returned with
3636 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3637 * slot has been permanently disabled.
3638 **/
3639void
3640lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3641{
3642 LIST_HEAD(txq);
3643 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003644 struct lpfc_sli *psli = &phba->sli;
3645 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003646 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003647
3648 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003649 /* Indicate the I/O queues are flushed */
3650 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003651 spin_unlock_irq(&phba->hbalock);
3652
James Smartdb55fba2014-04-04 13:52:02 -04003653 /* Look on all the FCP Rings for the iotag */
3654 if (phba->sli_rev >= LPFC_SLI_REV4) {
3655 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3656 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
James Smarta8e497d2008-08-24 21:50:11 -04003657
James Smartdb55fba2014-04-04 13:52:02 -04003658 spin_lock_irq(&pring->ring_lock);
3659 /* Retrieve everything on txq */
3660 list_splice_init(&pring->txq, &txq);
3661 /* Retrieve everything on the txcmplq */
3662 list_splice_init(&pring->txcmplq, &txcmplq);
3663 pring->txq_cnt = 0;
3664 pring->txcmplq_cnt = 0;
3665 spin_unlock_irq(&pring->ring_lock);
3666
3667 /* Flush the txq */
3668 lpfc_sli_cancel_iocbs(phba, &txq,
3669 IOSTAT_LOCAL_REJECT,
3670 IOERR_SLI_DOWN);
3671 /* Flush the txcmpq */
3672 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3673 IOSTAT_LOCAL_REJECT,
3674 IOERR_SLI_DOWN);
3675 }
3676 } else {
3677 pring = &psli->ring[psli->fcp_ring];
3678
3679 spin_lock_irq(&phba->hbalock);
3680 /* Retrieve everything on txq */
3681 list_splice_init(&pring->txq, &txq);
3682 /* Retrieve everything on the txcmplq */
3683 list_splice_init(&pring->txcmplq, &txcmplq);
3684 pring->txq_cnt = 0;
3685 pring->txcmplq_cnt = 0;
3686 spin_unlock_irq(&phba->hbalock);
3687
3688 /* Flush the txq */
3689 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3690 IOERR_SLI_DOWN);
3691 /* Flush the txcmpq */
3692 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3693 IOERR_SLI_DOWN);
3694 }
James Smarta8e497d2008-08-24 21:50:11 -04003695}
3696
3697/**
James Smart3772a992009-05-22 14:50:54 -04003698 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003699 * @phba: Pointer to HBA context object.
3700 * @mask: Bit mask to be checked.
3701 *
3702 * This function reads the host status register and compares
3703 * with the provided bit mask to check if HBA completed
3704 * the restart. This function will wait in a loop for the
3705 * HBA to complete restart. If the HBA does not restart within
3706 * 15 iterations, the function will reset the HBA again. The
3707 * function returns 1 when HBA fail to restart otherwise returns
3708 * zero.
3709 **/
James Smart3772a992009-05-22 14:50:54 -04003710static int
3711lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003712{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003713 uint32_t status;
3714 int i = 0;
3715 int retval = 0;
dea31012005-04-17 16:05:31 -05003716
Jamie Wellnitz41415862006-02-28 19:25:27 -05003717 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003718 if (lpfc_readl(phba->HSregaddr, &status))
3719 return 1;
dea31012005-04-17 16:05:31 -05003720
Jamie Wellnitz41415862006-02-28 19:25:27 -05003721 /*
3722 * Check status register every 100ms for 5 retries, then every
3723 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3724 * every 2.5 sec for 4.
3725 * Break our of the loop if errors occurred during init.
3726 */
3727 while (((status & mask) != mask) &&
3728 !(status & HS_FFERM) &&
3729 i++ < 20) {
dea31012005-04-17 16:05:31 -05003730
Jamie Wellnitz41415862006-02-28 19:25:27 -05003731 if (i <= 5)
3732 msleep(10);
3733 else if (i <= 10)
3734 msleep(500);
3735 else
3736 msleep(2500);
dea31012005-04-17 16:05:31 -05003737
Jamie Wellnitz41415862006-02-28 19:25:27 -05003738 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003739 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003740 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003741 lpfc_sli_brdrestart(phba);
3742 }
3743 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003744 if (lpfc_readl(phba->HSregaddr, &status)) {
3745 retval = 1;
3746 break;
3747 }
dea31012005-04-17 16:05:31 -05003748 }
dea31012005-04-17 16:05:31 -05003749
Jamie Wellnitz41415862006-02-28 19:25:27 -05003750 /* Check to see if any errors occurred during init */
3751 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3753 "2751 Adapter failed to restart, "
3754 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3755 status,
3756 readl(phba->MBslimaddr + 0xa8),
3757 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003758 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003759 retval = 1;
3760 }
dea31012005-04-17 16:05:31 -05003761
Jamie Wellnitz41415862006-02-28 19:25:27 -05003762 return retval;
dea31012005-04-17 16:05:31 -05003763}
3764
James Smartda0436e2009-05-22 14:51:39 -04003765/**
3766 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3767 * @phba: Pointer to HBA context object.
3768 * @mask: Bit mask to be checked.
3769 *
3770 * This function checks the host status register to check if HBA is
3771 * ready. This function will wait in a loop for the HBA to be ready
3772 * If the HBA is not ready , the function will will reset the HBA PCI
3773 * function again. The function returns 1 when HBA fail to be ready
3774 * otherwise returns zero.
3775 **/
3776static int
3777lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3778{
3779 uint32_t status;
3780 int retval = 0;
3781
3782 /* Read the HBA Host Status Register */
3783 status = lpfc_sli4_post_status_check(phba);
3784
3785 if (status) {
3786 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3787 lpfc_sli_brdrestart(phba);
3788 status = lpfc_sli4_post_status_check(phba);
3789 }
3790
3791 /* Check to see if any errors occurred during init */
3792 if (status) {
3793 phba->link_state = LPFC_HBA_ERROR;
3794 retval = 1;
3795 } else
3796 phba->sli4_hba.intr_enable = 0;
3797
3798 return retval;
3799}
3800
3801/**
3802 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3803 * @phba: Pointer to HBA context object.
3804 * @mask: Bit mask to be checked.
3805 *
3806 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3807 * from the API jump table function pointer from the lpfc_hba struct.
3808 **/
3809int
3810lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3811{
3812 return phba->lpfc_sli_brdready(phba, mask);
3813}
3814
James Smart92908312006-03-07 15:04:13 -05003815#define BARRIER_TEST_PATTERN (0xdeadbeef)
3816
James Smarte59058c2008-08-24 21:49:00 -04003817/**
James Smart3621a712009-04-06 18:47:14 -04003818 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003819 * @phba: Pointer to HBA context object.
3820 *
James Smart1b511972011-12-13 13:23:09 -05003821 * This function is called before resetting an HBA. This function is called
3822 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003823 **/
James Smart2e0fef82007-06-17 19:56:36 -05003824void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003825{
James Smart65a29c12006-07-06 15:50:50 -04003826 uint32_t __iomem *resp_buf;
3827 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003828 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003829 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003830 int i;
3831 uint8_t hdrtype;
3832
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003833 lockdep_assert_held(&phba->hbalock);
3834
James Smart92908312006-03-07 15:04:13 -05003835 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3836 if (hdrtype != 0x80 ||
3837 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3838 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3839 return;
3840
3841 /*
3842 * Tell the other part of the chip to suspend temporarily all
3843 * its DMA activity.
3844 */
James Smart65a29c12006-07-06 15:50:50 -04003845 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003846
3847 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003848 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3849 return;
James Smart92908312006-03-07 15:04:13 -05003850 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3851 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003852 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003853
James Smart9940b972011-03-11 16:06:12 -05003854 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3855 return;
3856 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003857 /* Clear Chip error bit */
3858 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003859 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003860 }
3861
3862 mbox = 0;
3863 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3864 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3865
3866 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003867 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003868 writel(mbox, mbox_buf);
3869
James Smart9940b972011-03-11 16:06:12 -05003870 for (i = 0; i < 50; i++) {
3871 if (lpfc_readl((resp_buf + 1), &resp_data))
3872 return;
3873 if (resp_data != ~(BARRIER_TEST_PATTERN))
3874 mdelay(1);
3875 else
3876 break;
3877 }
3878 resp_data = 0;
3879 if (lpfc_readl((resp_buf + 1), &resp_data))
3880 return;
3881 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003882 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003883 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003884 goto restore_hc;
3885 else
3886 goto clear_errat;
3887 }
3888
3889 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003890 resp_data = 0;
3891 for (i = 0; i < 500; i++) {
3892 if (lpfc_readl(resp_buf, &resp_data))
3893 return;
3894 if (resp_data != mbox)
3895 mdelay(1);
3896 else
3897 break;
3898 }
James Smart92908312006-03-07 15:04:13 -05003899
3900clear_errat:
3901
James Smart9940b972011-03-11 16:06:12 -05003902 while (++i < 500) {
3903 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3904 return;
3905 if (!(ha_copy & HA_ERATT))
3906 mdelay(1);
3907 else
3908 break;
3909 }
James Smart92908312006-03-07 15:04:13 -05003910
3911 if (readl(phba->HAregaddr) & HA_ERATT) {
3912 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003913 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003914 }
3915
3916restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003917 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003918 writel(hc_copy, phba->HCregaddr);
3919 readl(phba->HCregaddr); /* flush */
3920}
3921
James Smarte59058c2008-08-24 21:49:00 -04003922/**
James Smart3621a712009-04-06 18:47:14 -04003923 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003924 * @phba: Pointer to HBA context object.
3925 *
3926 * This function issues a kill_board mailbox command and waits for
3927 * the error attention interrupt. This function is called for stopping
3928 * the firmware processing. The caller is not required to hold any
3929 * locks. This function calls lpfc_hba_down_post function to free
3930 * any pending commands after the kill. The function will return 1 when it
3931 * fails to kill the board else will return 0.
3932 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003933int
James Smart2e0fef82007-06-17 19:56:36 -05003934lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003935{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936 struct lpfc_sli *psli;
3937 LPFC_MBOXQ_t *pmb;
3938 uint32_t status;
3939 uint32_t ha_copy;
3940 int retval;
3941 int i = 0;
3942
3943 psli = &phba->sli;
3944
3945 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003946 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003947 "0329 Kill HBA Data: x%x x%x\n",
3948 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003949
James Smart98c9ea52007-10-27 13:37:33 -04003950 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3951 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003953
3954 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003955 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003956 if (lpfc_readl(phba->HCregaddr, &status)) {
3957 spin_unlock_irq(&phba->hbalock);
3958 mempool_free(pmb, phba->mbox_mem_pool);
3959 return 1;
3960 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003961 status &= ~HC_ERINT_ENA;
3962 writel(status, phba->HCregaddr);
3963 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003964 phba->link_flag |= LS_IGNORE_ERATT;
3965 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003966
3967 lpfc_kill_board(phba, pmb);
3968 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3969 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3970
3971 if (retval != MBX_SUCCESS) {
3972 if (retval != MBX_BUSY)
3973 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3975 "2752 KILL_BOARD command failed retval %d\n",
3976 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003977 spin_lock_irq(&phba->hbalock);
3978 phba->link_flag &= ~LS_IGNORE_ERATT;
3979 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003980 return 1;
3981 }
3982
James Smartf4b4c682009-05-22 14:53:12 -04003983 spin_lock_irq(&phba->hbalock);
3984 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3985 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003986
Jamie Wellnitz41415862006-02-28 19:25:27 -05003987 mempool_free(pmb, phba->mbox_mem_pool);
3988
3989 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3990 * attention every 100ms for 3 seconds. If we don't get ERATT after
3991 * 3 seconds we still set HBA_ERROR state because the status of the
3992 * board is now undefined.
3993 */
James Smart9940b972011-03-11 16:06:12 -05003994 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3995 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003996 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3997 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003998 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3999 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004000 }
4001
4002 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004003 if (ha_copy & HA_ERATT) {
4004 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004005 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004006 }
James Smart2e0fef82007-06-17 19:56:36 -05004007 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004008 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004009 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004010 phba->link_flag &= ~LS_IGNORE_ERATT;
4011 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004012
Jamie Wellnitz41415862006-02-28 19:25:27 -05004013 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004014 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004015
James Smart2e0fef82007-06-17 19:56:36 -05004016 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004017}
4018
James Smarte59058c2008-08-24 21:49:00 -04004019/**
James Smart3772a992009-05-22 14:50:54 -04004020 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004021 * @phba: Pointer to HBA context object.
4022 *
4023 * This function resets the HBA by writing HC_INITFF to the control
4024 * register. After the HBA resets, this function resets all the iocb ring
4025 * indices. This function disables PCI layer parity checking during
4026 * the reset.
4027 * This function returns 0 always.
4028 * The caller is not required to hold any locks.
4029 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004030int
James Smart2e0fef82007-06-17 19:56:36 -05004031lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004032{
4033 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004034 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004035 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004036 int i;
dea31012005-04-17 16:05:31 -05004037
Jamie Wellnitz41415862006-02-28 19:25:27 -05004038 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004039
Jamie Wellnitz41415862006-02-28 19:25:27 -05004040 /* Reset HBA */
4041 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004042 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004043 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004044
4045 /* perform board reset */
4046 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004047 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004048 phba->pport->fc_myDID = 0;
4049 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004050
Jamie Wellnitz41415862006-02-28 19:25:27 -05004051 /* Turn off parity checking and serr during the physical reset */
4052 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4053 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4054 (cfg_value &
4055 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4056
James Smart3772a992009-05-22 14:50:54 -04004057 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4058
Jamie Wellnitz41415862006-02-28 19:25:27 -05004059 /* Now toggle INITFF bit in the Host Control Register */
4060 writel(HC_INITFF, phba->HCregaddr);
4061 mdelay(1);
4062 readl(phba->HCregaddr); /* flush */
4063 writel(0, phba->HCregaddr);
4064 readl(phba->HCregaddr); /* flush */
4065
4066 /* Restore PCI cmd register */
4067 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004068
4069 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004070 for (i = 0; i < psli->num_rings; i++) {
4071 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05004072 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004073 pring->sli.sli3.rspidx = 0;
4074 pring->sli.sli3.next_cmdidx = 0;
4075 pring->sli.sli3.local_getidx = 0;
4076 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004077 pring->missbufcnt = 0;
4078 }
dea31012005-04-17 16:05:31 -05004079
James Smart2e0fef82007-06-17 19:56:36 -05004080 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004081 return 0;
4082}
4083
James Smarte59058c2008-08-24 21:49:00 -04004084/**
James Smartda0436e2009-05-22 14:51:39 -04004085 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4086 * @phba: Pointer to HBA context object.
4087 *
4088 * This function resets a SLI4 HBA. This function disables PCI layer parity
4089 * checking during resets the device. The caller is not required to hold
4090 * any locks.
4091 *
4092 * This function returns 0 always.
4093 **/
4094int
4095lpfc_sli4_brdreset(struct lpfc_hba *phba)
4096{
4097 struct lpfc_sli *psli = &phba->sli;
4098 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004099 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004100
4101 /* Reset HBA */
4102 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004103 "0295 Reset HBA Data: x%x x%x x%x\n",
4104 phba->pport->port_state, psli->sli_flag,
4105 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004106
4107 /* perform board reset */
4108 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004109 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004110 phba->pport->fc_myDID = 0;
4111 phba->pport->fc_prevDID = 0;
4112
James Smartda0436e2009-05-22 14:51:39 -04004113 spin_lock_irq(&phba->hbalock);
4114 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4115 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004116 spin_unlock_irq(&phba->hbalock);
4117
James Smart02936352014-04-04 13:52:12 -04004118 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4119 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4120 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4121 return rc;
4122 }
4123
James Smartda0436e2009-05-22 14:51:39 -04004124 /* Now physically reset the device */
4125 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4126 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004127
4128 /* Turn off parity checking and serr during the physical reset */
4129 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4130 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4131 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4132
James Smart88318812012-09-29 11:29:29 -04004133 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004134 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004135 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004136
James Smartbe858b62010-12-15 17:57:20 -05004137 /* Restore PCI cmd register */
4138 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4139
James Smart27b01b82012-05-09 21:19:44 -04004140 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004141}
4142
4143/**
4144 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004145 * @phba: Pointer to HBA context object.
4146 *
4147 * This function is called in the SLI initialization code path to
4148 * restart the HBA. The caller is not required to hold any lock.
4149 * This function writes MBX_RESTART mailbox command to the SLIM and
4150 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4151 * function to free any pending commands. The function enables
4152 * POST only during the first initialization. The function returns zero.
4153 * The function does not guarantee completion of MBX_RESTART mailbox
4154 * command before the return of this function.
4155 **/
James Smartda0436e2009-05-22 14:51:39 -04004156static int
4157lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004158{
4159 MAILBOX_t *mb;
4160 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004161 volatile uint32_t word0;
4162 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004163 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004164
James Smart2e0fef82007-06-17 19:56:36 -05004165 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004166
James Smart0d878412009-10-02 15:16:56 -04004167 /* Take PCIe device Advanced Error Reporting (AER) state */
4168 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4169
Jamie Wellnitz41415862006-02-28 19:25:27 -05004170 psli = &phba->sli;
4171
4172 /* Restart HBA */
4173 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004174 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004175 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004176
4177 word0 = 0;
4178 mb = (MAILBOX_t *) &word0;
4179 mb->mbxCommand = MBX_RESTART;
4180 mb->mbxHc = 1;
4181
James Smart92908312006-03-07 15:04:13 -05004182 lpfc_reset_barrier(phba);
4183
Jamie Wellnitz41415862006-02-28 19:25:27 -05004184 to_slim = phba->MBslimaddr;
4185 writel(*(uint32_t *) mb, to_slim);
4186 readl(to_slim); /* flush */
4187
4188 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004189 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004190 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004191 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004192 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004193 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004194 writel(*(uint32_t *) mb, to_slim);
4195 readl(to_slim); /* flush */
4196
4197 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004198 phba->pport->stopped = 0;
4199 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004200 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004201 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004202
James Smart64ba8812006-08-02 15:24:34 -04004203 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4204 psli->stats_start = get_seconds();
4205
James Smarteaf15d52008-12-04 22:39:29 -05004206 /* Give the INITFF and Post time to settle. */
4207 mdelay(100);
dea31012005-04-17 16:05:31 -05004208
James Smart0d878412009-10-02 15:16:56 -04004209 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4210 if (hba_aer_enabled)
4211 pci_disable_pcie_error_reporting(phba->pcidev);
4212
Jamie Wellnitz41415862006-02-28 19:25:27 -05004213 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004214
4215 return 0;
4216}
4217
James Smarte59058c2008-08-24 21:49:00 -04004218/**
James Smartda0436e2009-05-22 14:51:39 -04004219 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4220 * @phba: Pointer to HBA context object.
4221 *
4222 * This function is called in the SLI initialization code path to restart
4223 * a SLI4 HBA. The caller is not required to hold any lock.
4224 * At the end of the function, it calls lpfc_hba_down_post function to
4225 * free any pending commands.
4226 **/
4227static int
4228lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4229{
4230 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004231 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004232 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004233
4234 /* Restart HBA */
4235 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4236 "0296 Restart HBA Data: x%x x%x\n",
4237 phba->pport->port_state, psli->sli_flag);
4238
James Smart75baf692010-06-08 18:31:21 -04004239 /* Take PCIe device Advanced Error Reporting (AER) state */
4240 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4241
James Smart27b01b82012-05-09 21:19:44 -04004242 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004243
4244 spin_lock_irq(&phba->hbalock);
4245 phba->pport->stopped = 0;
4246 phba->link_state = LPFC_INIT_START;
4247 phba->hba_flag = 0;
4248 spin_unlock_irq(&phba->hbalock);
4249
4250 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4251 psli->stats_start = get_seconds();
4252
James Smart75baf692010-06-08 18:31:21 -04004253 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4254 if (hba_aer_enabled)
4255 pci_disable_pcie_error_reporting(phba->pcidev);
4256
James Smartda0436e2009-05-22 14:51:39 -04004257 lpfc_hba_down_post(phba);
4258
James Smart27b01b82012-05-09 21:19:44 -04004259 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004260}
4261
4262/**
4263 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4264 * @phba: Pointer to HBA context object.
4265 *
4266 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4267 * API jump table function pointer from the lpfc_hba struct.
4268**/
4269int
4270lpfc_sli_brdrestart(struct lpfc_hba *phba)
4271{
4272 return phba->lpfc_sli_brdrestart(phba);
4273}
4274
4275/**
James Smart3621a712009-04-06 18:47:14 -04004276 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004277 * @phba: Pointer to HBA context object.
4278 *
4279 * This function is called after a HBA restart to wait for successful
4280 * restart of the HBA. Successful restart of the HBA is indicated by
4281 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4282 * iteration, the function will restart the HBA again. The function returns
4283 * zero if HBA successfully restarted else returns negative error code.
4284 **/
dea31012005-04-17 16:05:31 -05004285static int
4286lpfc_sli_chipset_init(struct lpfc_hba *phba)
4287{
4288 uint32_t status, i = 0;
4289
4290 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004291 if (lpfc_readl(phba->HSregaddr, &status))
4292 return -EIO;
dea31012005-04-17 16:05:31 -05004293
4294 /* Check status register to see what current state is */
4295 i = 0;
4296 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4297
James Smartdcf2a4e2010-09-29 11:18:53 -04004298 /* Check every 10ms for 10 retries, then every 100ms for 90
4299 * retries, then every 1 sec for 50 retires for a total of
4300 * ~60 seconds before reset the board again and check every
4301 * 1 sec for 50 retries. The up to 60 seconds before the
4302 * board ready is required by the Falcon FIPS zeroization
4303 * complete, and any reset the board in between shall cause
4304 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004305 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004306 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004307 /* Adapter failed to init, timeout, status reg
4308 <status> */
James Smarted957682007-06-17 19:56:37 -05004309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004310 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004311 "timeout, status reg x%x, "
4312 "FW Data: A8 x%x AC x%x\n", status,
4313 readl(phba->MBslimaddr + 0xa8),
4314 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004315 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004316 return -ETIMEDOUT;
4317 }
4318
4319 /* Check to see if any errors occurred during init */
4320 if (status & HS_FFERM) {
4321 /* ERROR: During chipset initialization */
4322 /* Adapter failed to init, chipset, status reg
4323 <status> */
James Smarted957682007-06-17 19:56:37 -05004324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004325 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004326 "chipset, status reg x%x, "
4327 "FW Data: A8 x%x AC x%x\n", status,
4328 readl(phba->MBslimaddr + 0xa8),
4329 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004330 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004331 return -EIO;
4332 }
4333
James Smartdcf2a4e2010-09-29 11:18:53 -04004334 if (i <= 10)
dea31012005-04-17 16:05:31 -05004335 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004336 else if (i <= 100)
4337 msleep(100);
4338 else
4339 msleep(1000);
dea31012005-04-17 16:05:31 -05004340
James Smartdcf2a4e2010-09-29 11:18:53 -04004341 if (i == 150) {
4342 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004343 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004344 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004345 }
4346 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004347 if (lpfc_readl(phba->HSregaddr, &status))
4348 return -EIO;
dea31012005-04-17 16:05:31 -05004349 }
4350
4351 /* Check to see if any errors occurred during init */
4352 if (status & HS_FFERM) {
4353 /* ERROR: During chipset initialization */
4354 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004356 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004357 "status reg x%x, "
4358 "FW Data: A8 x%x AC x%x\n", status,
4359 readl(phba->MBslimaddr + 0xa8),
4360 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004361 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004362 return -EIO;
4363 }
4364
4365 /* Clear all interrupt enable conditions */
4366 writel(0, phba->HCregaddr);
4367 readl(phba->HCregaddr); /* flush */
4368
4369 /* setup host attn register */
4370 writel(0xffffffff, phba->HAregaddr);
4371 readl(phba->HAregaddr); /* flush */
4372 return 0;
4373}
4374
James Smarte59058c2008-08-24 21:49:00 -04004375/**
James Smart3621a712009-04-06 18:47:14 -04004376 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004377 *
4378 * This function calculates and returns the number of HBQs required to be
4379 * configured.
4380 **/
James Smart78b2d852007-08-02 11:10:21 -04004381int
James Smarted957682007-06-17 19:56:37 -05004382lpfc_sli_hbq_count(void)
4383{
James Smart92d7f7b2007-06-17 19:56:38 -05004384 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004385}
4386
James Smarte59058c2008-08-24 21:49:00 -04004387/**
James Smart3621a712009-04-06 18:47:14 -04004388 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004389 *
4390 * This function adds the number of hbq entries in every HBQ to get
4391 * the total number of hbq entries required for the HBA and returns
4392 * the total count.
4393 **/
James Smarted957682007-06-17 19:56:37 -05004394static int
4395lpfc_sli_hbq_entry_count(void)
4396{
4397 int hbq_count = lpfc_sli_hbq_count();
4398 int count = 0;
4399 int i;
4400
4401 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004402 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004403 return count;
4404}
4405
James Smarte59058c2008-08-24 21:49:00 -04004406/**
James Smart3621a712009-04-06 18:47:14 -04004407 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004408 *
4409 * This function calculates amount of memory required for all hbq entries
4410 * to be configured and returns the total memory required.
4411 **/
dea31012005-04-17 16:05:31 -05004412int
James Smarted957682007-06-17 19:56:37 -05004413lpfc_sli_hbq_size(void)
4414{
4415 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4416}
4417
James Smarte59058c2008-08-24 21:49:00 -04004418/**
James Smart3621a712009-04-06 18:47:14 -04004419 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004420 * @phba: Pointer to HBA context object.
4421 *
4422 * This function is called during the SLI initialization to configure
4423 * all the HBQs and post buffers to the HBQ. The caller is not
4424 * required to hold any locks. This function will return zero if successful
4425 * else it will return negative error code.
4426 **/
James Smarted957682007-06-17 19:56:37 -05004427static int
4428lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4429{
4430 int hbq_count = lpfc_sli_hbq_count();
4431 LPFC_MBOXQ_t *pmb;
4432 MAILBOX_t *pmbox;
4433 uint32_t hbqno;
4434 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004435
James Smart92d7f7b2007-06-17 19:56:38 -05004436 /* Get a Mailbox buffer to setup mailbox
4437 * commands for HBA initialization
4438 */
James Smarted957682007-06-17 19:56:37 -05004439 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4440
4441 if (!pmb)
4442 return -ENOMEM;
4443
James Smart04c68492009-05-22 14:52:52 -04004444 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004445
4446 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4447 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004448 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004449
4450 hbq_entry_index = 0;
4451 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4452 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4453 phba->hbqs[hbqno].hbqPutIdx = 0;
4454 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4455 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004456 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004457 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4458 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004459 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4460
4461 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4462 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4463 mbxStatus <status>, ring <num> */
4464
4465 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004466 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004467 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004468 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004469 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004470 pmbox->mbxStatus, hbqno);
4471
4472 phba->link_state = LPFC_HBA_ERROR;
4473 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004474 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004475 }
4476 }
4477 phba->hbq_count = hbq_count;
4478
James Smarted957682007-06-17 19:56:37 -05004479 mempool_free(pmb, phba->mbox_mem_pool);
4480
James Smart92d7f7b2007-06-17 19:56:38 -05004481 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004482 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4483 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004484 return 0;
4485}
4486
James Smarte59058c2008-08-24 21:49:00 -04004487/**
James Smart4f774512009-05-22 14:52:35 -04004488 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4489 * @phba: Pointer to HBA context object.
4490 *
4491 * This function is called during the SLI initialization to configure
4492 * all the HBQs and post buffers to the HBQ. The caller is not
4493 * required to hold any locks. This function will return zero if successful
4494 * else it will return negative error code.
4495 **/
4496static int
4497lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4498{
4499 phba->hbq_in_use = 1;
4500 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4501 phba->hbq_count = 1;
4502 /* Initially populate or replenish the HBQs */
4503 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4504 return 0;
4505}
4506
4507/**
James Smart3621a712009-04-06 18:47:14 -04004508 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004509 * @phba: Pointer to HBA context object.
4510 * @sli_mode: sli mode - 2/3
4511 *
4512 * This function is called by the sli intialization code path
4513 * to issue config_port mailbox command. This function restarts the
4514 * HBA firmware and issues a config_port mailbox command to configure
4515 * the SLI interface in the sli mode specified by sli_mode
4516 * variable. The caller is not required to hold any locks.
4517 * The function returns 0 if successful, else returns negative error
4518 * code.
4519 **/
James Smart93996272008-08-24 21:50:30 -04004520int
4521lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004522{
4523 LPFC_MBOXQ_t *pmb;
4524 uint32_t resetcount = 0, rc = 0, done = 0;
4525
4526 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4527 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004528 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004529 return -ENOMEM;
4530 }
4531
James Smarted957682007-06-17 19:56:37 -05004532 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004533 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004534 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004535 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004536 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004537 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004538 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004539 rc = lpfc_sli_chipset_init(phba);
4540 if (rc)
4541 break;
4542
James Smart2e0fef82007-06-17 19:56:36 -05004543 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004544 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004545 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004546 resetcount++;
4547
James Smarted957682007-06-17 19:56:37 -05004548 /* Call pre CONFIG_PORT mailbox command initialization. A
4549 * value of 0 means the call was successful. Any other
4550 * nonzero value is a failure, but if ERESTART is returned,
4551 * the driver may reset the HBA and try again.
4552 */
dea31012005-04-17 16:05:31 -05004553 rc = lpfc_config_port_prep(phba);
4554 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004555 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004556 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004557 } else if (rc)
dea31012005-04-17 16:05:31 -05004558 break;
James Smart6d368e52011-05-24 11:44:12 -04004559
James Smart2e0fef82007-06-17 19:56:36 -05004560 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004561 lpfc_config_port(phba, pmb);
4562 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004563 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4564 LPFC_SLI3_HBQ_ENABLED |
4565 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004566 LPFC_SLI3_BG_ENABLED |
4567 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004568 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004570 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004571 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004572 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004573 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004574 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004575 spin_unlock_irq(&phba->hbalock);
4576 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004577 } else {
4578 /* Allow asynchronous mailbox command to go through */
4579 spin_lock_irq(&phba->hbalock);
4580 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4581 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004582 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004583
4584 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4585 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4586 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4587 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004588 }
dea31012005-04-17 16:05:31 -05004589 }
James Smarted957682007-06-17 19:56:37 -05004590 if (!done) {
4591 rc = -EINVAL;
4592 goto do_prep_failed;
4593 }
James Smart04c68492009-05-22 14:52:52 -04004594 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4595 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004596 rc = -ENXIO;
4597 goto do_prep_failed;
4598 }
James Smart04c68492009-05-22 14:52:52 -04004599 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004600 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004601 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4602 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4603 phba->max_vpi : phba->max_vports;
4604
James Smart34b02dc2008-08-24 21:49:55 -04004605 } else
4606 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004607 phba->fips_level = 0;
4608 phba->fips_spec_rev = 0;
4609 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004610 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004611 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4612 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4613 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4614 "2850 Security Crypto Active. FIPS x%d "
4615 "(Spec Rev: x%d)",
4616 phba->fips_level, phba->fips_spec_rev);
4617 }
4618 if (pmb->u.mb.un.varCfgPort.sec_err) {
4619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4620 "2856 Config Port Security Crypto "
4621 "Error: x%x ",
4622 pmb->u.mb.un.varCfgPort.sec_err);
4623 }
James Smart04c68492009-05-22 14:52:52 -04004624 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004625 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004626 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004627 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004628
4629 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4630 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004631
4632 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004633 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004634 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4635 else
4636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4637 "0443 Adapter did not grant "
4638 "BlockGuard\n");
4639 }
James Smart34b02dc2008-08-24 21:49:55 -04004640 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004641 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004642 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004643 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004644 }
James Smart92d7f7b2007-06-17 19:56:38 -05004645do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004646 mempool_free(pmb, phba->mbox_mem_pool);
4647 return rc;
4648}
4649
James Smarte59058c2008-08-24 21:49:00 -04004650
4651/**
James Smart3621a712009-04-06 18:47:14 -04004652 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004653 * @phba: Pointer to HBA context object.
4654 *
4655 * This function is the main SLI intialization function. This function
4656 * is called by the HBA intialization code, HBA reset code and HBA
4657 * error attention handler code. Caller is not required to hold any
4658 * locks. This function issues config_port mailbox command to configure
4659 * the SLI, setup iocb rings and HBQ rings. In the end the function
4660 * calls the config_port_post function to issue init_link mailbox
4661 * command and to start the discovery. The function will return zero
4662 * if successful, else it will return negative error code.
4663 **/
James Smarted957682007-06-17 19:56:37 -05004664int
4665lpfc_sli_hba_setup(struct lpfc_hba *phba)
4666{
4667 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004668 int mode = 3, i;
4669 int longs;
James Smarted957682007-06-17 19:56:37 -05004670
4671 switch (lpfc_sli_mode) {
4672 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004673 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004675 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004676 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004677 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004678 break;
4679 }
James Smarted957682007-06-17 19:56:37 -05004680 mode = 2;
4681 break;
4682 case 0:
4683 case 3:
4684 break;
4685 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004687 "1819 Unrecognized lpfc_sli_mode "
4688 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004689
4690 break;
4691 }
James Smartb5c53952016-03-31 14:12:30 -07004692 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004693
James Smart93996272008-08-24 21:50:30 -04004694 rc = lpfc_sli_config_port(phba, mode);
4695
James Smarted957682007-06-17 19:56:37 -05004696 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004697 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004698 "1820 Unable to select SLI-3. "
4699 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004700 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004701 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004702 if (rc)
dea31012005-04-17 16:05:31 -05004703 goto lpfc_sli_hba_setup_error;
4704
James Smart0d878412009-10-02 15:16:56 -04004705 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4706 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4707 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4708 if (!rc) {
4709 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4710 "2709 This device supports "
4711 "Advanced Error Reporting (AER)\n");
4712 spin_lock_irq(&phba->hbalock);
4713 phba->hba_flag |= HBA_AER_ENABLED;
4714 spin_unlock_irq(&phba->hbalock);
4715 } else {
4716 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4717 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004718 "Advanced Error Reporting (AER): %d\n",
4719 rc);
James Smart0d878412009-10-02 15:16:56 -04004720 phba->cfg_aer_support = 0;
4721 }
4722 }
4723
James Smarted957682007-06-17 19:56:37 -05004724 if (phba->sli_rev == 3) {
4725 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4726 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004727 } else {
4728 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4729 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004730 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004731 }
4732
4733 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004734 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4735 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004736 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004737
4738 if (rc)
4739 goto lpfc_sli_hba_setup_error;
4740
James Smart6d368e52011-05-24 11:44:12 -04004741 /* Initialize VPIs. */
4742 if (phba->sli_rev == LPFC_SLI_REV3) {
4743 /*
4744 * The VPI bitmask and physical ID array are allocated
4745 * and initialized once only - at driver load. A port
4746 * reset doesn't need to reinitialize this memory.
4747 */
4748 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4749 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4750 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4751 GFP_KERNEL);
4752 if (!phba->vpi_bmask) {
4753 rc = -ENOMEM;
4754 goto lpfc_sli_hba_setup_error;
4755 }
4756
4757 phba->vpi_ids = kzalloc(
4758 (phba->max_vpi+1) * sizeof(uint16_t),
4759 GFP_KERNEL);
4760 if (!phba->vpi_ids) {
4761 kfree(phba->vpi_bmask);
4762 rc = -ENOMEM;
4763 goto lpfc_sli_hba_setup_error;
4764 }
4765 for (i = 0; i < phba->max_vpi; i++)
4766 phba->vpi_ids[i] = i;
4767 }
4768 }
4769
James Smart93996272008-08-24 21:50:30 -04004770 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004771 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4772 rc = lpfc_sli_hbq_setup(phba);
4773 if (rc)
4774 goto lpfc_sli_hba_setup_error;
4775 }
James Smart04c68492009-05-22 14:52:52 -04004776 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004777 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004778 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004779
4780 rc = lpfc_config_port_post(phba);
4781 if (rc)
4782 goto lpfc_sli_hba_setup_error;
4783
James Smarted957682007-06-17 19:56:37 -05004784 return rc;
4785
James Smart92d7f7b2007-06-17 19:56:38 -05004786lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004787 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004789 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004790 return rc;
4791}
4792
James Smartda0436e2009-05-22 14:51:39 -04004793/**
4794 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4795 * @phba: Pointer to HBA context object.
4796 * @mboxq: mailbox pointer.
4797 * This function issue a dump mailbox command to read config region
4798 * 23 and parse the records in the region and populate driver
4799 * data structure.
4800 **/
4801static int
James Smartff78d8f2011-12-13 13:21:35 -05004802lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004803{
James Smartff78d8f2011-12-13 13:21:35 -05004804 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004805 struct lpfc_dmabuf *mp;
4806 struct lpfc_mqe *mqe;
4807 uint32_t data_length;
4808 int rc;
4809
4810 /* Program the default value of vlan_id and fc_map */
4811 phba->valid_vlan = 0;
4812 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4813 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4814 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4815
James Smartff78d8f2011-12-13 13:21:35 -05004816 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4817 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004818 return -ENOMEM;
4819
James Smartff78d8f2011-12-13 13:21:35 -05004820 mqe = &mboxq->u.mqe;
4821 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4822 rc = -ENOMEM;
4823 goto out_free_mboxq;
4824 }
4825
James Smartda0436e2009-05-22 14:51:39 -04004826 mp = (struct lpfc_dmabuf *) mboxq->context1;
4827 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4828
4829 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4830 "(%d):2571 Mailbox cmd x%x Status x%x "
4831 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4832 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4833 "CQ: x%x x%x x%x x%x\n",
4834 mboxq->vport ? mboxq->vport->vpi : 0,
4835 bf_get(lpfc_mqe_command, mqe),
4836 bf_get(lpfc_mqe_status, mqe),
4837 mqe->un.mb_words[0], mqe->un.mb_words[1],
4838 mqe->un.mb_words[2], mqe->un.mb_words[3],
4839 mqe->un.mb_words[4], mqe->un.mb_words[5],
4840 mqe->un.mb_words[6], mqe->un.mb_words[7],
4841 mqe->un.mb_words[8], mqe->un.mb_words[9],
4842 mqe->un.mb_words[10], mqe->un.mb_words[11],
4843 mqe->un.mb_words[12], mqe->un.mb_words[13],
4844 mqe->un.mb_words[14], mqe->un.mb_words[15],
4845 mqe->un.mb_words[16], mqe->un.mb_words[50],
4846 mboxq->mcqe.word0,
4847 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4848 mboxq->mcqe.trailer);
4849
4850 if (rc) {
4851 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4852 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004853 rc = -EIO;
4854 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004855 }
4856 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004857 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004858 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4859 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004860 rc = -EIO;
4861 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004862 }
James Smartda0436e2009-05-22 14:51:39 -04004863
4864 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4865 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4866 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004867 rc = 0;
4868
4869out_free_mboxq:
4870 mempool_free(mboxq, phba->mbox_mem_pool);
4871 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004872}
4873
4874/**
4875 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4876 * @phba: pointer to lpfc hba data structure.
4877 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4878 * @vpd: pointer to the memory to hold resulting port vpd data.
4879 * @vpd_size: On input, the number of bytes allocated to @vpd.
4880 * On output, the number of data bytes in @vpd.
4881 *
4882 * This routine executes a READ_REV SLI4 mailbox command. In
4883 * addition, this routine gets the port vpd data.
4884 *
4885 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004886 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004887 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004888 **/
4889static int
4890lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4891 uint8_t *vpd, uint32_t *vpd_size)
4892{
4893 int rc = 0;
4894 uint32_t dma_size;
4895 struct lpfc_dmabuf *dmabuf;
4896 struct lpfc_mqe *mqe;
4897
4898 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4899 if (!dmabuf)
4900 return -ENOMEM;
4901
4902 /*
4903 * Get a DMA buffer for the vpd data resulting from the READ_REV
4904 * mailbox command.
4905 */
4906 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004907 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4908 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004909 if (!dmabuf->virt) {
4910 kfree(dmabuf);
4911 return -ENOMEM;
4912 }
James Smartda0436e2009-05-22 14:51:39 -04004913
4914 /*
4915 * The SLI4 implementation of READ_REV conflicts at word1,
4916 * bits 31:16 and SLI4 adds vpd functionality not present
4917 * in SLI3. This code corrects the conflicts.
4918 */
4919 lpfc_read_rev(phba, mboxq);
4920 mqe = &mboxq->u.mqe;
4921 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4922 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4923 mqe->un.read_rev.word1 &= 0x0000FFFF;
4924 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4925 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4926
4927 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4928 if (rc) {
4929 dma_free_coherent(&phba->pcidev->dev, dma_size,
4930 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004931 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004932 return -EIO;
4933 }
4934
James Smartda0436e2009-05-22 14:51:39 -04004935 /*
4936 * The available vpd length cannot be bigger than the
4937 * DMA buffer passed to the port. Catch the less than
4938 * case and update the caller's size.
4939 */
4940 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4941 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4942
James Smartd7c47992010-06-08 18:31:54 -04004943 memcpy(vpd, dmabuf->virt, *vpd_size);
4944
James Smartda0436e2009-05-22 14:51:39 -04004945 dma_free_coherent(&phba->pcidev->dev, dma_size,
4946 dmabuf->virt, dmabuf->phys);
4947 kfree(dmabuf);
4948 return 0;
4949}
4950
4951/**
James Smartcd1c8302011-10-10 21:33:25 -04004952 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4953 * @phba: pointer to lpfc hba data structure.
4954 *
4955 * This routine retrieves SLI4 device physical port name this PCI function
4956 * is attached to.
4957 *
4958 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004959 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004960 * otherwise - failed to retrieve physical port name
4961 **/
4962static int
4963lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4964{
4965 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004966 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4967 struct lpfc_controller_attribute *cntl_attr;
4968 struct lpfc_mbx_get_port_name *get_port_name;
4969 void *virtaddr = NULL;
4970 uint32_t alloclen, reqlen;
4971 uint32_t shdr_status, shdr_add_status;
4972 union lpfc_sli4_cfg_shdr *shdr;
4973 char cport_name = 0;
4974 int rc;
4975
4976 /* We assume nothing at this point */
4977 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4978 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4979
4980 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4981 if (!mboxq)
4982 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004983 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004984 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4985 lpfc_sli4_read_config(phba);
4986 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4987 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004988
4989 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4990 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4991 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4992 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4993 LPFC_SLI4_MBX_NEMBED);
4994 if (alloclen < reqlen) {
4995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4996 "3084 Allocated DMA memory size (%d) is "
4997 "less than the requested DMA memory size "
4998 "(%d)\n", alloclen, reqlen);
4999 rc = -ENOMEM;
5000 goto out_free_mboxq;
5001 }
5002 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5003 virtaddr = mboxq->sge_array->addr[0];
5004 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5005 shdr = &mbx_cntl_attr->cfg_shdr;
5006 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5007 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5008 if (shdr_status || shdr_add_status || rc) {
5009 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5010 "3085 Mailbox x%x (x%x/x%x) failed, "
5011 "rc:x%x, status:x%x, add_status:x%x\n",
5012 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5013 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5014 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5015 rc, shdr_status, shdr_add_status);
5016 rc = -ENXIO;
5017 goto out_free_mboxq;
5018 }
5019 cntl_attr = &mbx_cntl_attr->cntl_attr;
5020 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5021 phba->sli4_hba.lnk_info.lnk_tp =
5022 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5023 phba->sli4_hba.lnk_info.lnk_no =
5024 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5025 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5026 "3086 lnk_type:%d, lnk_numb:%d\n",
5027 phba->sli4_hba.lnk_info.lnk_tp,
5028 phba->sli4_hba.lnk_info.lnk_no);
5029
5030retrieve_ppname:
5031 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5032 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5033 sizeof(struct lpfc_mbx_get_port_name) -
5034 sizeof(struct lpfc_sli4_cfg_mhdr),
5035 LPFC_SLI4_MBX_EMBED);
5036 get_port_name = &mboxq->u.mqe.un.get_port_name;
5037 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5038 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5039 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5040 phba->sli4_hba.lnk_info.lnk_tp);
5041 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5042 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5043 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5044 if (shdr_status || shdr_add_status || rc) {
5045 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5046 "3087 Mailbox x%x (x%x/x%x) failed: "
5047 "rc:x%x, status:x%x, add_status:x%x\n",
5048 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5049 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5050 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5051 rc, shdr_status, shdr_add_status);
5052 rc = -ENXIO;
5053 goto out_free_mboxq;
5054 }
5055 switch (phba->sli4_hba.lnk_info.lnk_no) {
5056 case LPFC_LINK_NUMBER_0:
5057 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5058 &get_port_name->u.response);
5059 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5060 break;
5061 case LPFC_LINK_NUMBER_1:
5062 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5063 &get_port_name->u.response);
5064 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5065 break;
5066 case LPFC_LINK_NUMBER_2:
5067 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5068 &get_port_name->u.response);
5069 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5070 break;
5071 case LPFC_LINK_NUMBER_3:
5072 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5073 &get_port_name->u.response);
5074 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5075 break;
5076 default:
5077 break;
5078 }
5079
5080 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5081 phba->Port[0] = cport_name;
5082 phba->Port[1] = '\0';
5083 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5084 "3091 SLI get port name: %s\n", phba->Port);
5085 }
5086
5087out_free_mboxq:
5088 if (rc != MBX_TIMEOUT) {
5089 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5090 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5091 else
5092 mempool_free(mboxq, phba->mbox_mem_pool);
5093 }
5094 return rc;
5095}
5096
5097/**
James Smartda0436e2009-05-22 14:51:39 -04005098 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5099 * @phba: pointer to lpfc hba data structure.
5100 *
5101 * This routine is called to explicitly arm the SLI4 device's completion and
5102 * event queues
5103 **/
5104static void
5105lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5106{
James Smart962bc512013-01-03 15:44:00 -05005107 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04005108
5109 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5110 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04005111 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05005112 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04005113 do {
James Smart2e90f4b2011-12-13 13:22:37 -05005114 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5115 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04005116 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05005117 }
James Smart1ba981f2014-02-20 09:56:45 -05005118
James Smartf38fa0b2014-04-04 13:52:21 -04005119 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005120 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5121
James Smart67d12732012-08-03 12:36:13 -04005122 if (phba->sli4_hba.hba_eq) {
5123 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05005124 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04005125 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05005126 LPFC_QUEUE_REARM);
5127 }
James Smart1ba981f2014-02-20 09:56:45 -05005128
5129 if (phba->cfg_fof)
5130 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005131}
5132
5133/**
James Smart6d368e52011-05-24 11:44:12 -04005134 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5135 * @phba: Pointer to HBA context object.
5136 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005137 * @extnt_count: buffer to hold port available extent count.
5138 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005139 *
James Smartb76f2dc2011-07-22 18:37:42 -04005140 * This function calls the port and retrievs the number of available
5141 * extents and their size for a particular extent type.
5142 *
5143 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005144 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005145int
James Smart6d368e52011-05-24 11:44:12 -04005146lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5147 uint16_t *extnt_count, uint16_t *extnt_size)
5148{
5149 int rc = 0;
5150 uint32_t length;
5151 uint32_t mbox_tmo;
5152 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5153 LPFC_MBOXQ_t *mbox;
5154
5155 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5156 if (!mbox)
5157 return -ENOMEM;
5158
5159 /* Find out how many extents are available for this resource type */
5160 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5161 sizeof(struct lpfc_sli4_cfg_mhdr));
5162 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5163 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5164 length, LPFC_SLI4_MBX_EMBED);
5165
5166 /* Send an extents count of 0 - the GET doesn't use it. */
5167 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5168 LPFC_SLI4_MBX_EMBED);
5169 if (unlikely(rc)) {
5170 rc = -EIO;
5171 goto err_exit;
5172 }
5173
5174 if (!phba->sli4_hba.intr_enable)
5175 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5176 else {
James Smarta183a152011-10-10 21:32:43 -04005177 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005178 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5179 }
5180 if (unlikely(rc)) {
5181 rc = -EIO;
5182 goto err_exit;
5183 }
5184
5185 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5186 if (bf_get(lpfc_mbox_hdr_status,
5187 &rsrc_info->header.cfg_shdr.response)) {
5188 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5189 "2930 Failed to get resource extents "
5190 "Status 0x%x Add'l Status 0x%x\n",
5191 bf_get(lpfc_mbox_hdr_status,
5192 &rsrc_info->header.cfg_shdr.response),
5193 bf_get(lpfc_mbox_hdr_add_status,
5194 &rsrc_info->header.cfg_shdr.response));
5195 rc = -EIO;
5196 goto err_exit;
5197 }
5198
5199 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5200 &rsrc_info->u.rsp);
5201 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5202 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005203
5204 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5205 "3162 Retrieved extents type-%d from port: count:%d, "
5206 "size:%d\n", type, *extnt_count, *extnt_size);
5207
5208err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005209 mempool_free(mbox, phba->mbox_mem_pool);
5210 return rc;
5211}
5212
5213/**
5214 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5215 * @phba: Pointer to HBA context object.
5216 * @type: The extent type to check.
5217 *
5218 * This function reads the current available extents from the port and checks
5219 * if the extent count or extent size has changed since the last access.
5220 * Callers use this routine post port reset to understand if there is a
5221 * extent reprovisioning requirement.
5222 *
5223 * Returns:
5224 * -Error: error indicates problem.
5225 * 1: Extent count or size has changed.
5226 * 0: No changes.
5227 **/
5228static int
5229lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5230{
5231 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5232 uint16_t size_diff, rsrc_ext_size;
5233 int rc = 0;
5234 struct lpfc_rsrc_blks *rsrc_entry;
5235 struct list_head *rsrc_blk_list = NULL;
5236
5237 size_diff = 0;
5238 curr_ext_cnt = 0;
5239 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5240 &rsrc_ext_cnt,
5241 &rsrc_ext_size);
5242 if (unlikely(rc))
5243 return -EIO;
5244
5245 switch (type) {
5246 case LPFC_RSC_TYPE_FCOE_RPI:
5247 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5248 break;
5249 case LPFC_RSC_TYPE_FCOE_VPI:
5250 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5251 break;
5252 case LPFC_RSC_TYPE_FCOE_XRI:
5253 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5254 break;
5255 case LPFC_RSC_TYPE_FCOE_VFI:
5256 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5257 break;
5258 default:
5259 break;
5260 }
5261
5262 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5263 curr_ext_cnt++;
5264 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5265 size_diff++;
5266 }
5267
5268 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5269 rc = 1;
5270
5271 return rc;
5272}
5273
5274/**
5275 * lpfc_sli4_cfg_post_extnts -
5276 * @phba: Pointer to HBA context object.
5277 * @extnt_cnt - number of available extents.
5278 * @type - the extent type (rpi, xri, vfi, vpi).
5279 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5280 * @mbox - pointer to the caller's allocated mailbox structure.
5281 *
5282 * This function executes the extents allocation request. It also
5283 * takes care of the amount of memory needed to allocate or get the
5284 * allocated extents. It is the caller's responsibility to evaluate
5285 * the response.
5286 *
5287 * Returns:
5288 * -Error: Error value describes the condition found.
5289 * 0: if successful
5290 **/
5291static int
James Smart8a9d2e82012-05-09 21:16:12 -04005292lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005293 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5294{
5295 int rc = 0;
5296 uint32_t req_len;
5297 uint32_t emb_len;
5298 uint32_t alloc_len, mbox_tmo;
5299
5300 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005301 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005302
5303 /*
5304 * Calculate the size of an embedded mailbox. The uint32_t
5305 * accounts for extents-specific word.
5306 */
5307 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5308 sizeof(uint32_t);
5309
5310 /*
5311 * Presume the allocation and response will fit into an embedded
5312 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5313 */
5314 *emb = LPFC_SLI4_MBX_EMBED;
5315 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005316 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005317 sizeof(union lpfc_sli4_cfg_shdr) +
5318 sizeof(uint32_t);
5319 *emb = LPFC_SLI4_MBX_NEMBED;
5320 }
5321
5322 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5323 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5324 req_len, *emb);
5325 if (alloc_len < req_len) {
5326 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005327 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005328 "less than the requested DMA memory "
5329 "size (x%x)\n", alloc_len, req_len);
5330 return -ENOMEM;
5331 }
James Smart8a9d2e82012-05-09 21:16:12 -04005332 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005333 if (unlikely(rc))
5334 return -EIO;
5335
5336 if (!phba->sli4_hba.intr_enable)
5337 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5338 else {
James Smarta183a152011-10-10 21:32:43 -04005339 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005340 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5341 }
5342
5343 if (unlikely(rc))
5344 rc = -EIO;
5345 return rc;
5346}
5347
5348/**
5349 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5350 * @phba: Pointer to HBA context object.
5351 * @type: The resource extent type to allocate.
5352 *
5353 * This function allocates the number of elements for the specified
5354 * resource type.
5355 **/
5356static int
5357lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5358{
5359 bool emb = false;
5360 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5361 uint16_t rsrc_id, rsrc_start, j, k;
5362 uint16_t *ids;
5363 int i, rc;
5364 unsigned long longs;
5365 unsigned long *bmask;
5366 struct lpfc_rsrc_blks *rsrc_blks;
5367 LPFC_MBOXQ_t *mbox;
5368 uint32_t length;
5369 struct lpfc_id_range *id_array = NULL;
5370 void *virtaddr = NULL;
5371 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5372 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5373 struct list_head *ext_blk_list;
5374
5375 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5376 &rsrc_cnt,
5377 &rsrc_size);
5378 if (unlikely(rc))
5379 return -EIO;
5380
5381 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5382 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5383 "3009 No available Resource Extents "
5384 "for resource type 0x%x: Count: 0x%x, "
5385 "Size 0x%x\n", type, rsrc_cnt,
5386 rsrc_size);
5387 return -ENOMEM;
5388 }
5389
James Smart8a9d2e82012-05-09 21:16:12 -04005390 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5391 "2903 Post resource extents type-0x%x: "
5392 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005393
5394 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5395 if (!mbox)
5396 return -ENOMEM;
5397
James Smart8a9d2e82012-05-09 21:16:12 -04005398 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005399 if (unlikely(rc)) {
5400 rc = -EIO;
5401 goto err_exit;
5402 }
5403
5404 /*
5405 * Figure out where the response is located. Then get local pointers
5406 * to the response data. The port does not guarantee to respond to
5407 * all extents counts request so update the local variable with the
5408 * allocated count from the port.
5409 */
5410 if (emb == LPFC_SLI4_MBX_EMBED) {
5411 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5412 id_array = &rsrc_ext->u.rsp.id[0];
5413 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5414 } else {
5415 virtaddr = mbox->sge_array->addr[0];
5416 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5417 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5418 id_array = &n_rsrc->id;
5419 }
5420
5421 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5422 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5423
5424 /*
5425 * Based on the resource size and count, correct the base and max
5426 * resource values.
5427 */
5428 length = sizeof(struct lpfc_rsrc_blks);
5429 switch (type) {
5430 case LPFC_RSC_TYPE_FCOE_RPI:
5431 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5432 sizeof(unsigned long),
5433 GFP_KERNEL);
5434 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5435 rc = -ENOMEM;
5436 goto err_exit;
5437 }
5438 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5439 sizeof(uint16_t),
5440 GFP_KERNEL);
5441 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5442 kfree(phba->sli4_hba.rpi_bmask);
5443 rc = -ENOMEM;
5444 goto err_exit;
5445 }
5446
5447 /*
5448 * The next_rpi was initialized with the maximum available
5449 * count but the port may allocate a smaller number. Catch
5450 * that case and update the next_rpi.
5451 */
5452 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5453
5454 /* Initialize local ptrs for common extent processing later. */
5455 bmask = phba->sli4_hba.rpi_bmask;
5456 ids = phba->sli4_hba.rpi_ids;
5457 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5458 break;
5459 case LPFC_RSC_TYPE_FCOE_VPI:
5460 phba->vpi_bmask = kzalloc(longs *
5461 sizeof(unsigned long),
5462 GFP_KERNEL);
5463 if (unlikely(!phba->vpi_bmask)) {
5464 rc = -ENOMEM;
5465 goto err_exit;
5466 }
5467 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5468 sizeof(uint16_t),
5469 GFP_KERNEL);
5470 if (unlikely(!phba->vpi_ids)) {
5471 kfree(phba->vpi_bmask);
5472 rc = -ENOMEM;
5473 goto err_exit;
5474 }
5475
5476 /* Initialize local ptrs for common extent processing later. */
5477 bmask = phba->vpi_bmask;
5478 ids = phba->vpi_ids;
5479 ext_blk_list = &phba->lpfc_vpi_blk_list;
5480 break;
5481 case LPFC_RSC_TYPE_FCOE_XRI:
5482 phba->sli4_hba.xri_bmask = kzalloc(longs *
5483 sizeof(unsigned long),
5484 GFP_KERNEL);
5485 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5486 rc = -ENOMEM;
5487 goto err_exit;
5488 }
James Smart8a9d2e82012-05-09 21:16:12 -04005489 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005490 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5491 sizeof(uint16_t),
5492 GFP_KERNEL);
5493 if (unlikely(!phba->sli4_hba.xri_ids)) {
5494 kfree(phba->sli4_hba.xri_bmask);
5495 rc = -ENOMEM;
5496 goto err_exit;
5497 }
5498
5499 /* Initialize local ptrs for common extent processing later. */
5500 bmask = phba->sli4_hba.xri_bmask;
5501 ids = phba->sli4_hba.xri_ids;
5502 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5503 break;
5504 case LPFC_RSC_TYPE_FCOE_VFI:
5505 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5506 sizeof(unsigned long),
5507 GFP_KERNEL);
5508 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5509 rc = -ENOMEM;
5510 goto err_exit;
5511 }
5512 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5513 sizeof(uint16_t),
5514 GFP_KERNEL);
5515 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5516 kfree(phba->sli4_hba.vfi_bmask);
5517 rc = -ENOMEM;
5518 goto err_exit;
5519 }
5520
5521 /* Initialize local ptrs for common extent processing later. */
5522 bmask = phba->sli4_hba.vfi_bmask;
5523 ids = phba->sli4_hba.vfi_ids;
5524 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5525 break;
5526 default:
5527 /* Unsupported Opcode. Fail call. */
5528 id_array = NULL;
5529 bmask = NULL;
5530 ids = NULL;
5531 ext_blk_list = NULL;
5532 goto err_exit;
5533 }
5534
5535 /*
5536 * Complete initializing the extent configuration with the
5537 * allocated ids assigned to this function. The bitmask serves
5538 * as an index into the array and manages the available ids. The
5539 * array just stores the ids communicated to the port via the wqes.
5540 */
5541 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5542 if ((i % 2) == 0)
5543 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5544 &id_array[k]);
5545 else
5546 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5547 &id_array[k]);
5548
5549 rsrc_blks = kzalloc(length, GFP_KERNEL);
5550 if (unlikely(!rsrc_blks)) {
5551 rc = -ENOMEM;
5552 kfree(bmask);
5553 kfree(ids);
5554 goto err_exit;
5555 }
5556 rsrc_blks->rsrc_start = rsrc_id;
5557 rsrc_blks->rsrc_size = rsrc_size;
5558 list_add_tail(&rsrc_blks->list, ext_blk_list);
5559 rsrc_start = rsrc_id;
5560 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5561 phba->sli4_hba.scsi_xri_start = rsrc_start +
5562 lpfc_sli4_get_els_iocb_cnt(phba);
5563
5564 while (rsrc_id < (rsrc_start + rsrc_size)) {
5565 ids[j] = rsrc_id;
5566 rsrc_id++;
5567 j++;
5568 }
5569 /* Entire word processed. Get next word.*/
5570 if ((i % 2) == 1)
5571 k++;
5572 }
5573 err_exit:
5574 lpfc_sli4_mbox_cmd_free(phba, mbox);
5575 return rc;
5576}
5577
5578/**
5579 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5580 * @phba: Pointer to HBA context object.
5581 * @type: the extent's type.
5582 *
5583 * This function deallocates all extents of a particular resource type.
5584 * SLI4 does not allow for deallocating a particular extent range. It
5585 * is the caller's responsibility to release all kernel memory resources.
5586 **/
5587static int
5588lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5589{
5590 int rc;
5591 uint32_t length, mbox_tmo = 0;
5592 LPFC_MBOXQ_t *mbox;
5593 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5594 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5595
5596 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5597 if (!mbox)
5598 return -ENOMEM;
5599
5600 /*
5601 * This function sends an embedded mailbox because it only sends the
5602 * the resource type. All extents of this type are released by the
5603 * port.
5604 */
5605 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5606 sizeof(struct lpfc_sli4_cfg_mhdr));
5607 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5608 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5609 length, LPFC_SLI4_MBX_EMBED);
5610
5611 /* Send an extents count of 0 - the dealloc doesn't use it. */
5612 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5613 LPFC_SLI4_MBX_EMBED);
5614 if (unlikely(rc)) {
5615 rc = -EIO;
5616 goto out_free_mbox;
5617 }
5618 if (!phba->sli4_hba.intr_enable)
5619 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5620 else {
James Smarta183a152011-10-10 21:32:43 -04005621 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005622 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5623 }
5624 if (unlikely(rc)) {
5625 rc = -EIO;
5626 goto out_free_mbox;
5627 }
5628
5629 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5630 if (bf_get(lpfc_mbox_hdr_status,
5631 &dealloc_rsrc->header.cfg_shdr.response)) {
5632 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5633 "2919 Failed to release resource extents "
5634 "for type %d - Status 0x%x Add'l Status 0x%x. "
5635 "Resource memory not released.\n",
5636 type,
5637 bf_get(lpfc_mbox_hdr_status,
5638 &dealloc_rsrc->header.cfg_shdr.response),
5639 bf_get(lpfc_mbox_hdr_add_status,
5640 &dealloc_rsrc->header.cfg_shdr.response));
5641 rc = -EIO;
5642 goto out_free_mbox;
5643 }
5644
5645 /* Release kernel memory resources for the specific type. */
5646 switch (type) {
5647 case LPFC_RSC_TYPE_FCOE_VPI:
5648 kfree(phba->vpi_bmask);
5649 kfree(phba->vpi_ids);
5650 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5651 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5652 &phba->lpfc_vpi_blk_list, list) {
5653 list_del_init(&rsrc_blk->list);
5654 kfree(rsrc_blk);
5655 }
James Smart16a3a202013-04-17 20:14:38 -04005656 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005657 break;
5658 case LPFC_RSC_TYPE_FCOE_XRI:
5659 kfree(phba->sli4_hba.xri_bmask);
5660 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005661 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5662 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5663 list_del_init(&rsrc_blk->list);
5664 kfree(rsrc_blk);
5665 }
5666 break;
5667 case LPFC_RSC_TYPE_FCOE_VFI:
5668 kfree(phba->sli4_hba.vfi_bmask);
5669 kfree(phba->sli4_hba.vfi_ids);
5670 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5671 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5672 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5673 list_del_init(&rsrc_blk->list);
5674 kfree(rsrc_blk);
5675 }
5676 break;
5677 case LPFC_RSC_TYPE_FCOE_RPI:
5678 /* RPI bitmask and physical id array are cleaned up earlier. */
5679 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5680 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5681 list_del_init(&rsrc_blk->list);
5682 kfree(rsrc_blk);
5683 }
5684 break;
5685 default:
5686 break;
5687 }
5688
5689 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5690
5691 out_free_mbox:
5692 mempool_free(mbox, phba->mbox_mem_pool);
5693 return rc;
5694}
5695
5696/**
5697 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5698 * @phba: Pointer to HBA context object.
5699 *
5700 * This function allocates all SLI4 resource identifiers.
5701 **/
5702int
5703lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5704{
5705 int i, rc, error = 0;
5706 uint16_t count, base;
5707 unsigned long longs;
5708
James Smartff78d8f2011-12-13 13:21:35 -05005709 if (!phba->sli4_hba.rpi_hdrs_in_use)
5710 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005711 if (phba->sli4_hba.extents_in_use) {
5712 /*
5713 * The port supports resource extents. The XRI, VPI, VFI, RPI
5714 * resource extent count must be read and allocated before
5715 * provisioning the resource id arrays.
5716 */
5717 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5718 LPFC_IDX_RSRC_RDY) {
5719 /*
5720 * Extent-based resources are set - the driver could
5721 * be in a port reset. Figure out if any corrective
5722 * actions need to be taken.
5723 */
5724 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5725 LPFC_RSC_TYPE_FCOE_VFI);
5726 if (rc != 0)
5727 error++;
5728 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5729 LPFC_RSC_TYPE_FCOE_VPI);
5730 if (rc != 0)
5731 error++;
5732 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5733 LPFC_RSC_TYPE_FCOE_XRI);
5734 if (rc != 0)
5735 error++;
5736 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5737 LPFC_RSC_TYPE_FCOE_RPI);
5738 if (rc != 0)
5739 error++;
5740
5741 /*
5742 * It's possible that the number of resources
5743 * provided to this port instance changed between
5744 * resets. Detect this condition and reallocate
5745 * resources. Otherwise, there is no action.
5746 */
5747 if (error) {
5748 lpfc_printf_log(phba, KERN_INFO,
5749 LOG_MBOX | LOG_INIT,
5750 "2931 Detected extent resource "
5751 "change. Reallocating all "
5752 "extents.\n");
5753 rc = lpfc_sli4_dealloc_extent(phba,
5754 LPFC_RSC_TYPE_FCOE_VFI);
5755 rc = lpfc_sli4_dealloc_extent(phba,
5756 LPFC_RSC_TYPE_FCOE_VPI);
5757 rc = lpfc_sli4_dealloc_extent(phba,
5758 LPFC_RSC_TYPE_FCOE_XRI);
5759 rc = lpfc_sli4_dealloc_extent(phba,
5760 LPFC_RSC_TYPE_FCOE_RPI);
5761 } else
5762 return 0;
5763 }
5764
5765 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5766 if (unlikely(rc))
5767 goto err_exit;
5768
5769 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5770 if (unlikely(rc))
5771 goto err_exit;
5772
5773 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5774 if (unlikely(rc))
5775 goto err_exit;
5776
5777 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5778 if (unlikely(rc))
5779 goto err_exit;
5780 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5781 LPFC_IDX_RSRC_RDY);
5782 return rc;
5783 } else {
5784 /*
5785 * The port does not support resource extents. The XRI, VPI,
5786 * VFI, RPI resource ids were determined from READ_CONFIG.
5787 * Just allocate the bitmasks and provision the resource id
5788 * arrays. If a port reset is active, the resources don't
5789 * need any action - just exit.
5790 */
5791 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005792 LPFC_IDX_RSRC_RDY) {
5793 lpfc_sli4_dealloc_resource_identifiers(phba);
5794 lpfc_sli4_remove_rpis(phba);
5795 }
James Smart6d368e52011-05-24 11:44:12 -04005796 /* RPIs. */
5797 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005798 if (count <= 0) {
5799 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5800 "3279 Invalid provisioning of "
5801 "rpi:%d\n", count);
5802 rc = -EINVAL;
5803 goto err_exit;
5804 }
James Smart6d368e52011-05-24 11:44:12 -04005805 base = phba->sli4_hba.max_cfg_param.rpi_base;
5806 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5807 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5808 sizeof(unsigned long),
5809 GFP_KERNEL);
5810 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5811 rc = -ENOMEM;
5812 goto err_exit;
5813 }
5814 phba->sli4_hba.rpi_ids = kzalloc(count *
5815 sizeof(uint16_t),
5816 GFP_KERNEL);
5817 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5818 rc = -ENOMEM;
5819 goto free_rpi_bmask;
5820 }
5821
5822 for (i = 0; i < count; i++)
5823 phba->sli4_hba.rpi_ids[i] = base + i;
5824
5825 /* VPIs. */
5826 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005827 if (count <= 0) {
5828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5829 "3280 Invalid provisioning of "
5830 "vpi:%d\n", count);
5831 rc = -EINVAL;
5832 goto free_rpi_ids;
5833 }
James Smart6d368e52011-05-24 11:44:12 -04005834 base = phba->sli4_hba.max_cfg_param.vpi_base;
5835 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5836 phba->vpi_bmask = kzalloc(longs *
5837 sizeof(unsigned long),
5838 GFP_KERNEL);
5839 if (unlikely(!phba->vpi_bmask)) {
5840 rc = -ENOMEM;
5841 goto free_rpi_ids;
5842 }
5843 phba->vpi_ids = kzalloc(count *
5844 sizeof(uint16_t),
5845 GFP_KERNEL);
5846 if (unlikely(!phba->vpi_ids)) {
5847 rc = -ENOMEM;
5848 goto free_vpi_bmask;
5849 }
5850
5851 for (i = 0; i < count; i++)
5852 phba->vpi_ids[i] = base + i;
5853
5854 /* XRIs. */
5855 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005856 if (count <= 0) {
5857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5858 "3281 Invalid provisioning of "
5859 "xri:%d\n", count);
5860 rc = -EINVAL;
5861 goto free_vpi_ids;
5862 }
James Smart6d368e52011-05-24 11:44:12 -04005863 base = phba->sli4_hba.max_cfg_param.xri_base;
5864 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5865 phba->sli4_hba.xri_bmask = kzalloc(longs *
5866 sizeof(unsigned long),
5867 GFP_KERNEL);
5868 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5869 rc = -ENOMEM;
5870 goto free_vpi_ids;
5871 }
James Smart41899be2012-03-01 22:34:19 -05005872 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005873 phba->sli4_hba.xri_ids = kzalloc(count *
5874 sizeof(uint16_t),
5875 GFP_KERNEL);
5876 if (unlikely(!phba->sli4_hba.xri_ids)) {
5877 rc = -ENOMEM;
5878 goto free_xri_bmask;
5879 }
5880
5881 for (i = 0; i < count; i++)
5882 phba->sli4_hba.xri_ids[i] = base + i;
5883
5884 /* VFIs. */
5885 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005886 if (count <= 0) {
5887 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5888 "3282 Invalid provisioning of "
5889 "vfi:%d\n", count);
5890 rc = -EINVAL;
5891 goto free_xri_ids;
5892 }
James Smart6d368e52011-05-24 11:44:12 -04005893 base = phba->sli4_hba.max_cfg_param.vfi_base;
5894 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5895 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5896 sizeof(unsigned long),
5897 GFP_KERNEL);
5898 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5899 rc = -ENOMEM;
5900 goto free_xri_ids;
5901 }
5902 phba->sli4_hba.vfi_ids = kzalloc(count *
5903 sizeof(uint16_t),
5904 GFP_KERNEL);
5905 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5906 rc = -ENOMEM;
5907 goto free_vfi_bmask;
5908 }
5909
5910 for (i = 0; i < count; i++)
5911 phba->sli4_hba.vfi_ids[i] = base + i;
5912
5913 /*
5914 * Mark all resources ready. An HBA reset doesn't need
5915 * to reset the initialization.
5916 */
5917 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5918 LPFC_IDX_RSRC_RDY);
5919 return 0;
5920 }
5921
5922 free_vfi_bmask:
5923 kfree(phba->sli4_hba.vfi_bmask);
5924 free_xri_ids:
5925 kfree(phba->sli4_hba.xri_ids);
5926 free_xri_bmask:
5927 kfree(phba->sli4_hba.xri_bmask);
5928 free_vpi_ids:
5929 kfree(phba->vpi_ids);
5930 free_vpi_bmask:
5931 kfree(phba->vpi_bmask);
5932 free_rpi_ids:
5933 kfree(phba->sli4_hba.rpi_ids);
5934 free_rpi_bmask:
5935 kfree(phba->sli4_hba.rpi_bmask);
5936 err_exit:
5937 return rc;
5938}
5939
5940/**
5941 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5942 * @phba: Pointer to HBA context object.
5943 *
5944 * This function allocates the number of elements for the specified
5945 * resource type.
5946 **/
5947int
5948lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5949{
5950 if (phba->sli4_hba.extents_in_use) {
5951 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5952 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5953 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5954 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5955 } else {
5956 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005957 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005958 kfree(phba->vpi_ids);
5959 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5960 kfree(phba->sli4_hba.xri_bmask);
5961 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005962 kfree(phba->sli4_hba.vfi_bmask);
5963 kfree(phba->sli4_hba.vfi_ids);
5964 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5965 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5966 }
5967
5968 return 0;
5969}
5970
5971/**
James Smartb76f2dc2011-07-22 18:37:42 -04005972 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5973 * @phba: Pointer to HBA context object.
5974 * @type: The resource extent type.
5975 * @extnt_count: buffer to hold port extent count response
5976 * @extnt_size: buffer to hold port extent size response.
5977 *
5978 * This function calls the port to read the host allocated extents
5979 * for a particular type.
5980 **/
5981int
5982lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5983 uint16_t *extnt_cnt, uint16_t *extnt_size)
5984{
5985 bool emb;
5986 int rc = 0;
5987 uint16_t curr_blks = 0;
5988 uint32_t req_len, emb_len;
5989 uint32_t alloc_len, mbox_tmo;
5990 struct list_head *blk_list_head;
5991 struct lpfc_rsrc_blks *rsrc_blk;
5992 LPFC_MBOXQ_t *mbox;
5993 void *virtaddr = NULL;
5994 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5995 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5996 union lpfc_sli4_cfg_shdr *shdr;
5997
5998 switch (type) {
5999 case LPFC_RSC_TYPE_FCOE_VPI:
6000 blk_list_head = &phba->lpfc_vpi_blk_list;
6001 break;
6002 case LPFC_RSC_TYPE_FCOE_XRI:
6003 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6004 break;
6005 case LPFC_RSC_TYPE_FCOE_VFI:
6006 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6007 break;
6008 case LPFC_RSC_TYPE_FCOE_RPI:
6009 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6010 break;
6011 default:
6012 return -EIO;
6013 }
6014
6015 /* Count the number of extents currently allocatd for this type. */
6016 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6017 if (curr_blks == 0) {
6018 /*
6019 * The GET_ALLOCATED mailbox does not return the size,
6020 * just the count. The size should be just the size
6021 * stored in the current allocated block and all sizes
6022 * for an extent type are the same so set the return
6023 * value now.
6024 */
6025 *extnt_size = rsrc_blk->rsrc_size;
6026 }
6027 curr_blks++;
6028 }
6029
James Smartb76f2dc2011-07-22 18:37:42 -04006030 /*
6031 * Calculate the size of an embedded mailbox. The uint32_t
6032 * accounts for extents-specific word.
6033 */
6034 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6035 sizeof(uint32_t);
6036
6037 /*
6038 * Presume the allocation and response will fit into an embedded
6039 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6040 */
6041 emb = LPFC_SLI4_MBX_EMBED;
6042 req_len = emb_len;
6043 if (req_len > emb_len) {
6044 req_len = curr_blks * sizeof(uint16_t) +
6045 sizeof(union lpfc_sli4_cfg_shdr) +
6046 sizeof(uint32_t);
6047 emb = LPFC_SLI4_MBX_NEMBED;
6048 }
6049
6050 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6051 if (!mbox)
6052 return -ENOMEM;
6053 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6054
6055 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6056 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6057 req_len, emb);
6058 if (alloc_len < req_len) {
6059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6060 "2983 Allocated DMA memory size (x%x) is "
6061 "less than the requested DMA memory "
6062 "size (x%x)\n", alloc_len, req_len);
6063 rc = -ENOMEM;
6064 goto err_exit;
6065 }
6066 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6067 if (unlikely(rc)) {
6068 rc = -EIO;
6069 goto err_exit;
6070 }
6071
6072 if (!phba->sli4_hba.intr_enable)
6073 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6074 else {
James Smarta183a152011-10-10 21:32:43 -04006075 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006076 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6077 }
6078
6079 if (unlikely(rc)) {
6080 rc = -EIO;
6081 goto err_exit;
6082 }
6083
6084 /*
6085 * Figure out where the response is located. Then get local pointers
6086 * to the response data. The port does not guarantee to respond to
6087 * all extents counts request so update the local variable with the
6088 * allocated count from the port.
6089 */
6090 if (emb == LPFC_SLI4_MBX_EMBED) {
6091 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6092 shdr = &rsrc_ext->header.cfg_shdr;
6093 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6094 } else {
6095 virtaddr = mbox->sge_array->addr[0];
6096 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6097 shdr = &n_rsrc->cfg_shdr;
6098 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6099 }
6100
6101 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6102 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6103 "2984 Failed to read allocated resources "
6104 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6105 type,
6106 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6107 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6108 rc = -EIO;
6109 goto err_exit;
6110 }
6111 err_exit:
6112 lpfc_sli4_mbox_cmd_free(phba, mbox);
6113 return rc;
6114}
6115
6116/**
James Smart8a9d2e82012-05-09 21:16:12 -04006117 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6118 * @phba: pointer to lpfc hba data structure.
6119 *
6120 * This routine walks the list of els buffers that have been allocated and
6121 * repost them to the port by using SGL block post. This is needed after a
6122 * pci_function_reset/warm_start or start. It attempts to construct blocks
6123 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6124 * SGL block post mailbox commands to post them to the port. For single els
6125 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6126 * mailbox command for posting.
6127 *
6128 * Returns: 0 = success, non-zero failure.
6129 **/
6130static int
6131lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6132{
6133 struct lpfc_sglq *sglq_entry = NULL;
6134 struct lpfc_sglq *sglq_entry_next = NULL;
6135 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04006136 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006137 int last_xritag = NO_XRI;
James Smartdafe8ce2014-09-03 12:56:40 -04006138 struct lpfc_sli_ring *pring;
James Smart8a9d2e82012-05-09 21:16:12 -04006139 LIST_HEAD(prep_sgl_list);
6140 LIST_HEAD(blck_sgl_list);
6141 LIST_HEAD(allc_sgl_list);
6142 LIST_HEAD(post_sgl_list);
6143 LIST_HEAD(free_sgl_list);
6144
James Smartdafe8ce2014-09-03 12:56:40 -04006145 pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart38c20672013-03-01 16:37:44 -05006146 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006147 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006148 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006149 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006150 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006151
James Smart711ea882013-04-17 20:18:29 -04006152 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006153 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6154 &allc_sgl_list, list) {
6155 list_del_init(&sglq_entry->list);
6156 block_cnt++;
6157 if ((last_xritag != NO_XRI) &&
6158 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6159 /* a hole in xri block, form a sgl posting block */
6160 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6161 post_cnt = block_cnt - 1;
6162 /* prepare list for next posting block */
6163 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6164 block_cnt = 1;
6165 } else {
6166 /* prepare list for next posting block */
6167 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6168 /* enough sgls for non-embed sgl mbox command */
6169 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6170 list_splice_init(&prep_sgl_list,
6171 &blck_sgl_list);
6172 post_cnt = block_cnt;
6173 block_cnt = 0;
6174 }
6175 }
6176 num_posted++;
6177
6178 /* keep track of last sgl's xritag */
6179 last_xritag = sglq_entry->sli4_xritag;
6180
6181 /* end of repost sgl list condition for els buffers */
6182 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6183 if (post_cnt == 0) {
6184 list_splice_init(&prep_sgl_list,
6185 &blck_sgl_list);
6186 post_cnt = block_cnt;
6187 } else if (block_cnt == 1) {
6188 status = lpfc_sli4_post_sgl(phba,
6189 sglq_entry->phys, 0,
6190 sglq_entry->sli4_xritag);
6191 if (!status) {
6192 /* successful, put sgl to posted list */
6193 list_add_tail(&sglq_entry->list,
6194 &post_sgl_list);
6195 } else {
6196 /* Failure, put sgl to free list */
6197 lpfc_printf_log(phba, KERN_WARNING,
6198 LOG_SLI,
6199 "3159 Failed to post els "
6200 "sgl, xritag:x%x\n",
6201 sglq_entry->sli4_xritag);
6202 list_add_tail(&sglq_entry->list,
6203 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006204 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006205 }
6206 }
6207 }
6208
6209 /* continue until a nembed page worth of sgls */
6210 if (post_cnt == 0)
6211 continue;
6212
6213 /* post the els buffer list sgls as a block */
6214 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6215 post_cnt);
6216
6217 if (!status) {
6218 /* success, put sgl list to posted sgl list */
6219 list_splice_init(&blck_sgl_list, &post_sgl_list);
6220 } else {
6221 /* Failure, put sgl list to free sgl list */
6222 sglq_entry_first = list_first_entry(&blck_sgl_list,
6223 struct lpfc_sglq,
6224 list);
6225 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6226 "3160 Failed to post els sgl-list, "
6227 "xritag:x%x-x%x\n",
6228 sglq_entry_first->sli4_xritag,
6229 (sglq_entry_first->sli4_xritag +
6230 post_cnt - 1));
6231 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006232 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006233 }
6234
6235 /* don't reset xirtag due to hole in xri block */
6236 if (block_cnt == 0)
6237 last_xritag = NO_XRI;
6238
6239 /* reset els sgl post count for next round of posting */
6240 post_cnt = 0;
6241 }
James Smart711ea882013-04-17 20:18:29 -04006242 /* update the number of XRIs posted for ELS */
6243 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006244
6245 /* free the els sgls failed to post */
6246 lpfc_free_sgl_list(phba, &free_sgl_list);
6247
6248 /* push els sgls posted to the availble list */
6249 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006250 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006251 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006252 list_splice_init(&post_sgl_list,
6253 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006254 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006255 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006256 } else {
6257 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6258 "3161 Failure to post els sgl to port.\n");
6259 return -EIO;
6260 }
6261 return 0;
6262}
6263
6264/**
James Smartda0436e2009-05-22 14:51:39 -04006265 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6266 * @phba: Pointer to HBA context object.
6267 *
6268 * This function is the main SLI4 device intialization PCI function. This
6269 * function is called by the HBA intialization code, HBA reset code and
6270 * HBA error attention handler code. Caller is not required to hold any
6271 * locks.
6272 **/
6273int
6274lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6275{
6276 int rc;
6277 LPFC_MBOXQ_t *mboxq;
6278 struct lpfc_mqe *mqe;
6279 uint8_t *vpd;
6280 uint32_t vpd_size;
6281 uint32_t ftr_rsp = 0;
6282 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6283 struct lpfc_vport *vport = phba->pport;
6284 struct lpfc_dmabuf *mp;
6285
6286 /* Perform a PCI function reset to start from clean */
6287 rc = lpfc_pci_function_reset(phba);
6288 if (unlikely(rc))
6289 return -ENODEV;
6290
6291 /* Check the HBA Host Status Register for readyness */
6292 rc = lpfc_sli4_post_status_check(phba);
6293 if (unlikely(rc))
6294 return -ENODEV;
6295 else {
6296 spin_lock_irq(&phba->hbalock);
6297 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6298 spin_unlock_irq(&phba->hbalock);
6299 }
6300
6301 /*
6302 * Allocate a single mailbox container for initializing the
6303 * port.
6304 */
6305 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6306 if (!mboxq)
6307 return -ENOMEM;
6308
James Smartda0436e2009-05-22 14:51:39 -04006309 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006310 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006311 vpd = kzalloc(vpd_size, GFP_KERNEL);
6312 if (!vpd) {
6313 rc = -ENOMEM;
6314 goto out_free_mbox;
6315 }
6316
6317 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006318 if (unlikely(rc)) {
6319 kfree(vpd);
6320 goto out_free_mbox;
6321 }
James Smart572709e2013-07-15 18:32:43 -04006322
James Smartda0436e2009-05-22 14:51:39 -04006323 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006324 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006325 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006326 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006327 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6328 } else {
James Smart76a95d72010-11-20 23:11:48 -05006329 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006330 }
James Smart45ed1192009-10-02 15:17:02 -04006331
6332 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6333 LPFC_DCBX_CEE_MODE)
6334 phba->hba_flag |= HBA_FIP_SUPPORT;
6335 else
6336 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6337
James Smart4f2e66c2012-05-09 21:17:07 -04006338 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6339
James Smartc31098c2011-04-16 11:03:33 -04006340 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006341 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6342 "0376 READ_REV Error. SLI Level %d "
6343 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006344 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006345 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006346 kfree(vpd);
6347 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006348 }
James Smartcd1c8302011-10-10 21:33:25 -04006349
6350 /*
James Smartff78d8f2011-12-13 13:21:35 -05006351 * Continue initialization with default values even if driver failed
6352 * to read FCoE param config regions, only read parameters if the
6353 * board is FCoE
6354 */
6355 if (phba->hba_flag & HBA_FCOE_MODE &&
6356 lpfc_sli4_read_fcoe_params(phba))
6357 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6358 "2570 Failed to read FCoE parameters\n");
6359
6360 /*
James Smartcd1c8302011-10-10 21:33:25 -04006361 * Retrieve sli4 device physical port name, failure of doing it
6362 * is considered as non-fatal.
6363 */
6364 rc = lpfc_sli4_retrieve_pport_name(phba);
6365 if (!rc)
6366 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6367 "3080 Successful retrieving SLI4 device "
6368 "physical port name: %s.\n", phba->Port);
6369
James Smartda0436e2009-05-22 14:51:39 -04006370 /*
6371 * Evaluate the read rev and vpd data. Populate the driver
6372 * state with the results. If this routine fails, the failure
6373 * is not fatal as the driver will use generic values.
6374 */
6375 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6376 if (unlikely(!rc)) {
6377 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6378 "0377 Error %d parsing vpd. "
6379 "Using defaults.\n", rc);
6380 rc = 0;
6381 }
James Smart76a95d72010-11-20 23:11:48 -05006382 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006383
James Smartf1126682009-06-10 17:22:44 -04006384 /* Save information as VPD data */
6385 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6386 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6387 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6388 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6389 &mqe->un.read_rev);
6390 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6391 &mqe->un.read_rev);
6392 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6393 &mqe->un.read_rev);
6394 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6395 &mqe->un.read_rev);
6396 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6397 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6398 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6399 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6400 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6401 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6402 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6403 "(%d):0380 READ_REV Status x%x "
6404 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6405 mboxq->vport ? mboxq->vport->vpi : 0,
6406 bf_get(lpfc_mqe_status, mqe),
6407 phba->vpd.rev.opFwName,
6408 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6409 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006410
James Smart572709e2013-07-15 18:32:43 -04006411 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6412 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6413 if (phba->pport->cfg_lun_queue_depth > rc) {
6414 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6415 "3362 LUN queue depth changed from %d to %d\n",
6416 phba->pport->cfg_lun_queue_depth, rc);
6417 phba->pport->cfg_lun_queue_depth = rc;
6418 }
6419
6420
James Smartda0436e2009-05-22 14:51:39 -04006421 /*
6422 * Discover the port's supported feature set and match it against the
6423 * hosts requests.
6424 */
6425 lpfc_request_features(phba, mboxq);
6426 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6427 if (unlikely(rc)) {
6428 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006429 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006430 }
6431
6432 /*
6433 * The port must support FCP initiator mode as this is the
6434 * only mode running in the host.
6435 */
6436 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6437 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6438 "0378 No support for fcpi mode.\n");
6439 ftr_rsp++;
6440 }
James Smartfedd3b72011-02-16 12:39:24 -05006441 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6442 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6443 else
6444 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006445 /*
6446 * If the port cannot support the host's requested features
6447 * then turn off the global config parameters to disable the
6448 * feature in the driver. This is not a fatal error.
6449 */
James Smartbf086112011-08-21 21:48:13 -04006450 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6451 if (phba->cfg_enable_bg) {
6452 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6453 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6454 else
6455 ftr_rsp++;
6456 }
James Smartda0436e2009-05-22 14:51:39 -04006457
6458 if (phba->max_vpi && phba->cfg_enable_npiv &&
6459 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6460 ftr_rsp++;
6461
6462 if (ftr_rsp) {
6463 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6464 "0379 Feature Mismatch Data: x%08x %08x "
6465 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6466 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6467 phba->cfg_enable_npiv, phba->max_vpi);
6468 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6469 phba->cfg_enable_bg = 0;
6470 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6471 phba->cfg_enable_npiv = 0;
6472 }
6473
6474 /* These SLI3 features are assumed in SLI4 */
6475 spin_lock_irq(&phba->hbalock);
6476 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6477 spin_unlock_irq(&phba->hbalock);
6478
James Smart6d368e52011-05-24 11:44:12 -04006479 /*
6480 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6481 * calls depends on these resources to complete port setup.
6482 */
6483 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6484 if (rc) {
6485 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6486 "2920 Failed to alloc Resource IDs "
6487 "rc = x%x\n", rc);
6488 goto out_free_mbox;
6489 }
6490
James Smartda0436e2009-05-22 14:51:39 -04006491 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006492 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6493 if (rc) {
6494 phba->link_state = LPFC_HBA_ERROR;
6495 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006496 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006497 }
6498
James Smartda0436e2009-05-22 14:51:39 -04006499 mboxq->vport = vport;
6500 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6501 mp = (struct lpfc_dmabuf *) mboxq->context1;
6502 if (rc == MBX_SUCCESS) {
6503 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6504 rc = 0;
6505 }
6506
6507 /*
6508 * This memory was allocated by the lpfc_read_sparam routine. Release
6509 * it to the mbuf pool.
6510 */
6511 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6512 kfree(mp);
6513 mboxq->context1 = NULL;
6514 if (unlikely(rc)) {
6515 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6516 "0382 READ_SPARAM command failed "
6517 "status %d, mbxStatus x%x\n",
6518 rc, bf_get(lpfc_mqe_status, mqe));
6519 phba->link_state = LPFC_HBA_ERROR;
6520 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006521 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006522 }
6523
James Smart05580562011-05-24 11:40:48 -04006524 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006525
6526 /* Update the fc_host data structures with new wwn. */
6527 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6528 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6529
James Smart8a9d2e82012-05-09 21:16:12 -04006530 /* update host els and scsi xri-sgl sizes and mappings */
6531 rc = lpfc_sli4_xri_sgl_update(phba);
6532 if (unlikely(rc)) {
6533 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6534 "1400 Failed to update xri-sgl size and "
6535 "mapping: %d\n", rc);
6536 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006537 }
6538
James Smart8a9d2e82012-05-09 21:16:12 -04006539 /* register the els sgl pool to the port */
6540 rc = lpfc_sli4_repost_els_sgl_list(phba);
6541 if (unlikely(rc)) {
6542 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6543 "0582 Error %d during els sgl post "
6544 "operation\n", rc);
6545 rc = -ENODEV;
6546 goto out_free_mbox;
6547 }
6548
6549 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006550 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6551 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006552 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006553 "0383 Error %d during scsi sgl post "
6554 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006555 /* Some Scsi buffers were moved to the abort scsi list */
6556 /* A pci function reset will repost them */
6557 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006558 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006559 }
6560
6561 /* Post the rpi header region to the device. */
6562 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6563 if (unlikely(rc)) {
6564 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6565 "0393 Error %d during rpi post operation\n",
6566 rc);
6567 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006568 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006569 }
James Smart97f2ecf2012-03-01 22:35:23 -05006570 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006571
James Smart5350d872011-10-10 21:33:49 -04006572 /* Create all the SLI4 queues */
6573 rc = lpfc_sli4_queue_create(phba);
6574 if (rc) {
6575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6576 "3089 Failed to allocate queues\n");
6577 rc = -ENODEV;
6578 goto out_stop_timers;
6579 }
James Smartda0436e2009-05-22 14:51:39 -04006580 /* Set up all the queues to the device */
6581 rc = lpfc_sli4_queue_setup(phba);
6582 if (unlikely(rc)) {
6583 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6584 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006585 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006586 }
6587
6588 /* Arm the CQs and then EQs on device */
6589 lpfc_sli4_arm_cqeq_intr(phba);
6590
6591 /* Indicate device interrupt mode */
6592 phba->sli4_hba.intr_enable = 1;
6593
6594 /* Allow asynchronous mailbox command to go through */
6595 spin_lock_irq(&phba->hbalock);
6596 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6597 spin_unlock_irq(&phba->hbalock);
6598
6599 /* Post receive buffers to the device */
6600 lpfc_sli4_rb_setup(phba);
6601
James Smartfc2b9892010-02-26 14:15:29 -05006602 /* Reset HBA FCF states after HBA reset */
6603 phba->fcf.fcf_flag = 0;
6604 phba->fcf.current_rec.flag = 0;
6605
James Smartda0436e2009-05-22 14:51:39 -04006606 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006607 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006608 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006609
6610 /* Start heart beat timer */
6611 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006612 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006613 phba->hb_outstanding = 0;
6614 phba->last_completion_time = jiffies;
6615
6616 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006617 mod_timer(&phba->eratt_poll,
6618 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006619
James Smart75baf692010-06-08 18:31:21 -04006620 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6621 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6622 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6623 if (!rc) {
6624 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6625 "2829 This device supports "
6626 "Advanced Error Reporting (AER)\n");
6627 spin_lock_irq(&phba->hbalock);
6628 phba->hba_flag |= HBA_AER_ENABLED;
6629 spin_unlock_irq(&phba->hbalock);
6630 } else {
6631 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6632 "2830 This device does not support "
6633 "Advanced Error Reporting (AER)\n");
6634 phba->cfg_aer_support = 0;
6635 }
James Smart0a96e972011-07-22 18:37:28 -04006636 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006637 }
6638
James Smart76a95d72010-11-20 23:11:48 -05006639 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6640 /*
6641 * The FC Port needs to register FCFI (index 0)
6642 */
6643 lpfc_reg_fcfi(phba, mboxq);
6644 mboxq->vport = phba->pport;
6645 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006646 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006647 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006648 rc = 0;
6649 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6650 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006651
6652 /* Check if the port is configured to be disabled */
6653 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006654 }
James Smart026abb82011-12-13 13:20:45 -05006655
James Smartda0436e2009-05-22 14:51:39 -04006656 /*
6657 * The port is ready, set the host's link state to LINK_DOWN
6658 * in preparation for link interrupts.
6659 */
James Smartda0436e2009-05-22 14:51:39 -04006660 spin_lock_irq(&phba->hbalock);
6661 phba->link_state = LPFC_LINK_DOWN;
6662 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006663 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6664 (phba->hba_flag & LINK_DISABLED)) {
6665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6666 "3103 Adapter Link is disabled.\n");
6667 lpfc_down_link(phba, mboxq);
6668 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6669 if (rc != MBX_SUCCESS) {
6670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6671 "3104 Adapter failed to issue "
6672 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6673 goto out_unset_queue;
6674 }
6675 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006676 /* don't perform init_link on SLI4 FC port loopback test */
6677 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6678 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6679 if (rc)
6680 goto out_unset_queue;
6681 }
James Smart5350d872011-10-10 21:33:49 -04006682 }
6683 mempool_free(mboxq, phba->mbox_mem_pool);
6684 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006685out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006686 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006687 lpfc_sli4_queue_unset(phba);
6688out_destroy_queue:
6689 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006690out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006691 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006692out_free_mbox:
6693 mempool_free(mboxq, phba->mbox_mem_pool);
6694 return rc;
6695}
James Smarte59058c2008-08-24 21:49:00 -04006696
6697/**
James Smart3621a712009-04-06 18:47:14 -04006698 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006699 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006700 *
James Smarte59058c2008-08-24 21:49:00 -04006701 * This is the callback function for mailbox timer. The mailbox
6702 * timer is armed when a new mailbox command is issued and the timer
6703 * is deleted when the mailbox complete. The function is called by
6704 * the kernel timer code when a mailbox does not complete within
6705 * expected time. This function wakes up the worker thread to
6706 * process the mailbox timeout and returns. All the processing is
6707 * done by the worker thread function lpfc_mbox_timeout_handler.
6708 **/
dea31012005-04-17 16:05:31 -05006709void
6710lpfc_mbox_timeout(unsigned long ptr)
6711{
James Smart92d7f7b2007-06-17 19:56:38 -05006712 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006713 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006714 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006715
James Smart2e0fef82007-06-17 19:56:36 -05006716 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006717 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006718 if (!tmo_posted)
6719 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6720 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6721
James Smart5e9d9b82008-06-14 22:52:53 -04006722 if (!tmo_posted)
6723 lpfc_worker_wake_up(phba);
6724 return;
dea31012005-04-17 16:05:31 -05006725}
6726
James Smarte8d3c3b2013-10-10 12:21:30 -04006727/**
6728 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6729 * are pending
6730 * @phba: Pointer to HBA context object.
6731 *
6732 * This function checks if any mailbox completions are present on the mailbox
6733 * completion queue.
6734 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006735static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006736lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6737{
6738
6739 uint32_t idx;
6740 struct lpfc_queue *mcq;
6741 struct lpfc_mcqe *mcqe;
6742 bool pending_completions = false;
6743
6744 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6745 return false;
6746
6747 /* Check for completions on mailbox completion queue */
6748
6749 mcq = phba->sli4_hba.mbx_cq;
6750 idx = mcq->hba_index;
6751 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6752 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6753 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6754 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6755 pending_completions = true;
6756 break;
6757 }
6758 idx = (idx + 1) % mcq->entry_count;
6759 if (mcq->hba_index == idx)
6760 break;
6761 }
6762 return pending_completions;
6763
6764}
6765
6766/**
6767 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6768 * that were missed.
6769 * @phba: Pointer to HBA context object.
6770 *
6771 * For sli4, it is possible to miss an interrupt. As such mbox completions
6772 * maybe missed causing erroneous mailbox timeouts to occur. This function
6773 * checks to see if mbox completions are on the mailbox completion queue
6774 * and will process all the completions associated with the eq for the
6775 * mailbox completion queue.
6776 **/
6777bool
6778lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6779{
6780
6781 uint32_t eqidx;
6782 struct lpfc_queue *fpeq = NULL;
6783 struct lpfc_eqe *eqe;
6784 bool mbox_pending;
6785
6786 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6787 return false;
6788
6789 /* Find the eq associated with the mcq */
6790
6791 if (phba->sli4_hba.hba_eq)
6792 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6793 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6794 phba->sli4_hba.mbx_cq->assoc_qid) {
6795 fpeq = phba->sli4_hba.hba_eq[eqidx];
6796 break;
6797 }
6798 if (!fpeq)
6799 return false;
6800
6801 /* Turn off interrupts from this EQ */
6802
6803 lpfc_sli4_eq_clr_intr(fpeq);
6804
6805 /* Check to see if a mbox completion is pending */
6806
6807 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6808
6809 /*
6810 * If a mbox completion is pending, process all the events on EQ
6811 * associated with the mbox completion queue (this could include
6812 * mailbox commands, async events, els commands, receive queue data
6813 * and fcp commands)
6814 */
6815
6816 if (mbox_pending)
6817 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6818 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6819 fpeq->EQ_processed++;
6820 }
6821
6822 /* Always clear and re-arm the EQ */
6823
6824 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6825
6826 return mbox_pending;
6827
6828}
James Smarte59058c2008-08-24 21:49:00 -04006829
6830/**
James Smart3621a712009-04-06 18:47:14 -04006831 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006832 * @phba: Pointer to HBA context object.
6833 *
6834 * This function is called from worker thread when a mailbox command times out.
6835 * The caller is not required to hold any locks. This function will reset the
6836 * HBA and recover all the pending commands.
6837 **/
dea31012005-04-17 16:05:31 -05006838void
6839lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6840{
James Smart2e0fef82007-06-17 19:56:36 -05006841 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04006842 MAILBOX_t *mb = NULL;
6843
James Smart1dcb58e2007-04-25 09:51:30 -04006844 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006845
James Smarte8d3c3b2013-10-10 12:21:30 -04006846 /* If the mailbox completed, process the completion and return */
6847 if (lpfc_sli4_process_missed_mbox_completions(phba))
6848 return;
6849
James Smarteb016562014-09-03 12:58:06 -04006850 if (pmbox != NULL)
6851 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04006852 /* Check the pmbox pointer first. There is a race condition
6853 * between the mbox timeout handler getting executed in the
6854 * worklist and the mailbox actually completing. When this
6855 * race condition occurs, the mbox_active will be NULL.
6856 */
6857 spin_lock_irq(&phba->hbalock);
6858 if (pmbox == NULL) {
6859 lpfc_printf_log(phba, KERN_WARNING,
6860 LOG_MBOX | LOG_SLI,
6861 "0353 Active Mailbox cleared - mailbox timeout "
6862 "exiting\n");
6863 spin_unlock_irq(&phba->hbalock);
6864 return;
6865 }
6866
dea31012005-04-17 16:05:31 -05006867 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006868 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006869 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006870 mb->mbxCommand,
6871 phba->pport->port_state,
6872 phba->sli.sli_flag,
6873 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006874 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006875
James Smart1dcb58e2007-04-25 09:51:30 -04006876 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6877 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006878 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006879 */
James Smart2e0fef82007-06-17 19:56:36 -05006880 spin_lock_irq(&phba->pport->work_port_lock);
6881 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6882 spin_unlock_irq(&phba->pport->work_port_lock);
6883 spin_lock_irq(&phba->hbalock);
6884 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006885 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006886 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006887
James Smartdb55fba2014-04-04 13:52:02 -04006888 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04006889
6890 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006891 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006892
6893 /* Reset the HBA device */
6894 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006895}
6896
James Smarte59058c2008-08-24 21:49:00 -04006897/**
James Smart3772a992009-05-22 14:50:54 -04006898 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006899 * @phba: Pointer to HBA context object.
6900 * @pmbox: Pointer to mailbox object.
6901 * @flag: Flag indicating how the mailbox need to be processed.
6902 *
6903 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006904 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6905 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006906 * The mailbox command can be submitted in polling mode, in which case
6907 * this function will wait in a polling loop for the completion of the
6908 * mailbox.
6909 * If the mailbox is submitted in no_wait mode (not polling) the
6910 * function will submit the command and returns immediately without waiting
6911 * for the mailbox completion. The no_wait is supported only when HBA
6912 * is in SLI2/SLI3 mode - interrupts are enabled.
6913 * The SLI interface allows only one mailbox pending at a time. If the
6914 * mailbox is issued in polling mode and there is already a mailbox
6915 * pending, then the function will return an error. If the mailbox is issued
6916 * in NO_WAIT mode and there is a mailbox pending already, the function
6917 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6918 * The sli layer owns the mailbox object until the completion of mailbox
6919 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6920 * return codes the caller owns the mailbox command after the return of
6921 * the function.
6922 **/
James Smart3772a992009-05-22 14:50:54 -04006923static int
6924lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6925 uint32_t flag)
dea31012005-04-17 16:05:31 -05006926{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006927 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006928 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006929 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006930 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006931 int i;
James Smart09372822008-01-11 01:52:54 -05006932 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006933 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006934 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006935 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006936 int processing_queue = 0;
6937
6938 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6939 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006940 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006941 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006942 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6943 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6944 return MBX_SUCCESS;
6945 }
James Smart58da1ff2008-04-07 10:15:56 -04006946 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006947 pmbox = lpfc_mbox_get(phba);
6948 if (!pmbox) {
6949 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6950 return MBX_SUCCESS;
6951 }
6952 }
dea31012005-04-17 16:05:31 -05006953
James Smarted957682007-06-17 19:56:37 -05006954 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006955 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006956 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006957 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006958 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006959 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006960 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006961 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006962 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006963 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006964 }
6965 }
6966
Linas Vepstas8d63f372007-02-14 14:28:36 -06006967 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006968 if (unlikely(pci_channel_offline(phba->pcidev))) {
6969 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6970 goto out_not_finished;
6971 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006972
James Smarta257bf92009-04-06 18:48:10 -04006973 /* If HBA has a deferred error attention, fail the iocb. */
6974 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6975 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6976 goto out_not_finished;
6977 }
6978
dea31012005-04-17 16:05:31 -05006979 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006980
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006981 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006982 status = MBX_SUCCESS;
6983
James Smart2e0fef82007-06-17 19:56:36 -05006984 if (phba->link_state == LPFC_HBA_ERROR) {
6985 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006986
6987 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006988 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6989 "(%d):0311 Mailbox command x%x cannot "
6990 "issue Data: x%x x%x\n",
6991 pmbox->vport ? pmbox->vport->vpi : 0,
6992 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006993 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006994 }
6995
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006996 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05006997 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6998 !(hc_copy & HC_MBINT_ENA)) {
6999 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7000 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007001 "(%d):2528 Mailbox command x%x cannot "
7002 "issue Data: x%x x%x\n",
7003 pmbox->vport ? pmbox->vport->vpi : 0,
7004 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007005 goto out_not_finished;
7006 }
James Smart92908312006-03-07 15:04:13 -05007007 }
7008
dea31012005-04-17 16:05:31 -05007009 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7010 /* Polling for a mbox command when another one is already active
7011 * is not allowed in SLI. Also, the driver must have established
7012 * SLI2 mode to queue and process multiple mbox commands.
7013 */
7014
7015 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007016 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007017
7018 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007019 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7020 "(%d):2529 Mailbox command x%x "
7021 "cannot issue Data: x%x x%x\n",
7022 pmbox->vport ? pmbox->vport->vpi : 0,
7023 pmbox->u.mb.mbxCommand,
7024 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007025 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007026 }
7027
James Smart3772a992009-05-22 14:50:54 -04007028 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007029 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007030 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007031 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7032 "(%d):2530 Mailbox command x%x "
7033 "cannot issue Data: x%x x%x\n",
7034 pmbox->vport ? pmbox->vport->vpi : 0,
7035 pmbox->u.mb.mbxCommand,
7036 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007037 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007038 }
7039
dea31012005-04-17 16:05:31 -05007040 /* Another mailbox command is still being processed, queue this
7041 * command to be processed later.
7042 */
7043 lpfc_mbox_put(phba, pmbox);
7044
7045 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007046 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007047 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007048 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007049 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007050 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007051 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007052
7053 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007054 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007055
James Smart858c9f62007-06-17 19:56:39 -05007056 if (pmbox->vport) {
7057 lpfc_debugfs_disc_trc(pmbox->vport,
7058 LPFC_DISC_TRC_MBOX_VPORT,
7059 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007060 (uint32_t)mbx->mbxCommand,
7061 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007062 }
7063 else {
7064 lpfc_debugfs_disc_trc(phba->pport,
7065 LPFC_DISC_TRC_MBOX,
7066 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007067 (uint32_t)mbx->mbxCommand,
7068 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007069 }
7070
James Smart2e0fef82007-06-17 19:56:36 -05007071 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007072 }
7073
dea31012005-04-17 16:05:31 -05007074 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7075
7076 /* If we are not polling, we MUST be in SLI2 mode */
7077 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007078 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007079 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007080 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007081 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007082 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007083 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7084 "(%d):2531 Mailbox command x%x "
7085 "cannot issue Data: x%x x%x\n",
7086 pmbox->vport ? pmbox->vport->vpi : 0,
7087 pmbox->u.mb.mbxCommand,
7088 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007089 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007090 }
7091 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007092 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7093 1000);
7094 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007095 }
7096
7097 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007098 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007099 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007100 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007101 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007102 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007103 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007104
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007105 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007106 if (pmbox->vport) {
7107 lpfc_debugfs_disc_trc(pmbox->vport,
7108 LPFC_DISC_TRC_MBOX_VPORT,
7109 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007110 (uint32_t)mbx->mbxCommand,
7111 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007112 }
7113 else {
7114 lpfc_debugfs_disc_trc(phba->pport,
7115 LPFC_DISC_TRC_MBOX,
7116 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007117 (uint32_t)mbx->mbxCommand,
7118 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007119 }
7120 }
7121
dea31012005-04-17 16:05:31 -05007122 psli->slistat.mbox_cmd++;
7123 evtctr = psli->slistat.mbox_event;
7124
7125 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007126 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007127
James Smart3772a992009-05-22 14:50:54 -04007128 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007129 /* Populate mbox extension offset word. */
7130 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007131 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007132 = (uint8_t *)phba->mbox_ext
7133 - (uint8_t *)phba->mbox;
7134 }
7135
7136 /* Copy the mailbox extension data */
7137 if (pmbox->in_ext_byte_len && pmbox->context2) {
7138 lpfc_sli_pcimem_bcopy(pmbox->context2,
7139 (uint8_t *)phba->mbox_ext,
7140 pmbox->in_ext_byte_len);
7141 }
7142 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007143 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007144 } else {
James Smart7a470272010-03-15 11:25:20 -04007145 /* Populate mbox extension offset word. */
7146 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007147 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007148 = MAILBOX_HBA_EXT_OFFSET;
7149
7150 /* Copy the mailbox extension data */
7151 if (pmbox->in_ext_byte_len && pmbox->context2) {
7152 lpfc_memcpy_to_slim(phba->MBslimaddr +
7153 MAILBOX_HBA_EXT_OFFSET,
7154 pmbox->context2, pmbox->in_ext_byte_len);
7155
7156 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007157 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007158 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007159 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007160 }
7161
7162 /* First copy mbox command data to HBA SLIM, skip past first
7163 word */
7164 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007165 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007166 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7167
7168 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007169 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007170 to_slim = phba->MBslimaddr;
7171 writel(ldata, to_slim);
7172 readl(to_slim); /* flush */
7173
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007174 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007175 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007176 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007177 }
7178 }
7179
7180 wmb();
dea31012005-04-17 16:05:31 -05007181
7182 switch (flag) {
7183 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007184 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007185 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007186 /* Interrupt board to do it */
7187 writel(CA_MBATT, phba->CAregaddr);
7188 readl(phba->CAregaddr); /* flush */
7189 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007190 break;
7191
7192 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007193 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007194 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007195 /* Interrupt board to do it */
7196 writel(CA_MBATT, phba->CAregaddr);
7197 readl(phba->CAregaddr); /* flush */
7198
James Smart3772a992009-05-22 14:50:54 -04007199 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007200 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007201 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007202 word0 = le32_to_cpu(word0);
7203 } else {
7204 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007205 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7206 spin_unlock_irqrestore(&phba->hbalock,
7207 drvr_flag);
7208 goto out_not_finished;
7209 }
dea31012005-04-17 16:05:31 -05007210 }
7211
7212 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007213 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7214 spin_unlock_irqrestore(&phba->hbalock,
7215 drvr_flag);
7216 goto out_not_finished;
7217 }
James Smarta183a152011-10-10 21:32:43 -04007218 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7219 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007220 i = 0;
dea31012005-04-17 16:05:31 -05007221 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007222 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7223 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007224 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007225 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007226 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007227 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007228 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007229 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007230 }
7231
7232 /* Check if we took a mbox interrupt while we were
7233 polling */
7234 if (((word0 & OWN_CHIP) != OWN_CHIP)
7235 && (evtctr != psli->slistat.mbox_event))
7236 break;
7237
James Smart09372822008-01-11 01:52:54 -05007238 if (i++ > 10) {
7239 spin_unlock_irqrestore(&phba->hbalock,
7240 drvr_flag);
7241 msleep(1);
7242 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7243 }
dea31012005-04-17 16:05:31 -05007244
James Smart3772a992009-05-22 14:50:54 -04007245 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007246 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007247 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007248 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007249 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007250 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007251 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007252 /* Check real SLIM for any errors */
7253 slimword0 = readl(phba->MBslimaddr);
7254 slimmb = (MAILBOX_t *) & slimword0;
7255 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7256 && slimmb->mbxStatus) {
7257 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007258 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007259 word0 = slimword0;
7260 }
7261 }
7262 } else {
7263 /* First copy command data */
7264 word0 = readl(phba->MBslimaddr);
7265 }
7266 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007267 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7268 spin_unlock_irqrestore(&phba->hbalock,
7269 drvr_flag);
7270 goto out_not_finished;
7271 }
dea31012005-04-17 16:05:31 -05007272 }
7273
James Smart3772a992009-05-22 14:50:54 -04007274 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007275 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007276 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007277 /* Copy the mailbox extension data */
7278 if (pmbox->out_ext_byte_len && pmbox->context2) {
7279 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7280 pmbox->context2,
7281 pmbox->out_ext_byte_len);
7282 }
dea31012005-04-17 16:05:31 -05007283 } else {
7284 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007285 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007286 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007287 /* Copy the mailbox extension data */
7288 if (pmbox->out_ext_byte_len && pmbox->context2) {
7289 lpfc_memcpy_from_slim(pmbox->context2,
7290 phba->MBslimaddr +
7291 MAILBOX_HBA_EXT_OFFSET,
7292 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007293 }
7294 }
7295
7296 writel(HA_MBATT, phba->HAregaddr);
7297 readl(phba->HAregaddr); /* flush */
7298
7299 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007300 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007301 }
7302
James Smart2e0fef82007-06-17 19:56:36 -05007303 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7304 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007305
7306out_not_finished:
7307 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007308 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007309 lpfc_mbox_cmpl_put(phba, pmbox);
7310 }
7311 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007312}
7313
James Smarte59058c2008-08-24 21:49:00 -04007314/**
James Smartf1126682009-06-10 17:22:44 -04007315 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7316 * @phba: Pointer to HBA context object.
7317 *
7318 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7319 * the driver internal pending mailbox queue. It will then try to wait out the
7320 * possible outstanding mailbox command before return.
7321 *
7322 * Returns:
7323 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7324 * the outstanding mailbox command timed out.
7325 **/
7326static int
7327lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7328{
7329 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007330 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007331 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007332
7333 /* Mark the asynchronous mailbox command posting as blocked */
7334 spin_lock_irq(&phba->hbalock);
7335 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007336 /* Determine how long we might wait for the active mailbox
7337 * command to be gracefully completed by firmware.
7338 */
James Smarta183a152011-10-10 21:32:43 -04007339 if (phba->sli.mbox_active)
7340 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7341 phba->sli.mbox_active) *
7342 1000) + jiffies;
7343 spin_unlock_irq(&phba->hbalock);
7344
James Smarte8d3c3b2013-10-10 12:21:30 -04007345 /* Make sure the mailbox is really active */
7346 if (timeout)
7347 lpfc_sli4_process_missed_mbox_completions(phba);
7348
James Smartf1126682009-06-10 17:22:44 -04007349 /* Wait for the outstnading mailbox command to complete */
7350 while (phba->sli.mbox_active) {
7351 /* Check active mailbox complete status every 2ms */
7352 msleep(2);
7353 if (time_after(jiffies, timeout)) {
7354 /* Timeout, marked the outstanding cmd not complete */
7355 rc = 1;
7356 break;
7357 }
7358 }
7359
7360 /* Can not cleanly block async mailbox command, fails it */
7361 if (rc) {
7362 spin_lock_irq(&phba->hbalock);
7363 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7364 spin_unlock_irq(&phba->hbalock);
7365 }
7366 return rc;
7367}
7368
7369/**
7370 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7371 * @phba: Pointer to HBA context object.
7372 *
7373 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7374 * commands from the driver internal pending mailbox queue. It makes sure
7375 * that there is no outstanding mailbox command before resuming posting
7376 * asynchronous mailbox commands. If, for any reason, there is outstanding
7377 * mailbox command, it will try to wait it out before resuming asynchronous
7378 * mailbox command posting.
7379 **/
7380static void
7381lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7382{
7383 struct lpfc_sli *psli = &phba->sli;
7384
7385 spin_lock_irq(&phba->hbalock);
7386 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7387 /* Asynchronous mailbox posting is not blocked, do nothing */
7388 spin_unlock_irq(&phba->hbalock);
7389 return;
7390 }
7391
7392 /* Outstanding synchronous mailbox command is guaranteed to be done,
7393 * successful or timeout, after timing-out the outstanding mailbox
7394 * command shall always be removed, so just unblock posting async
7395 * mailbox command and resume
7396 */
7397 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7398 spin_unlock_irq(&phba->hbalock);
7399
7400 /* wake up worker thread to post asynchronlous mailbox command */
7401 lpfc_worker_wake_up(phba);
7402}
7403
7404/**
James Smart2d843ed2012-09-29 11:29:06 -04007405 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7406 * @phba: Pointer to HBA context object.
7407 * @mboxq: Pointer to mailbox object.
7408 *
7409 * The function waits for the bootstrap mailbox register ready bit from
7410 * port for twice the regular mailbox command timeout value.
7411 *
7412 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7413 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7414 **/
7415static int
7416lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7417{
7418 uint32_t db_ready;
7419 unsigned long timeout;
7420 struct lpfc_register bmbx_reg;
7421
7422 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7423 * 1000) + jiffies;
7424
7425 do {
7426 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7427 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7428 if (!db_ready)
7429 msleep(2);
7430
7431 if (time_after(jiffies, timeout))
7432 return MBXERR_ERROR;
7433 } while (!db_ready);
7434
7435 return 0;
7436}
7437
7438/**
James Smartda0436e2009-05-22 14:51:39 -04007439 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7440 * @phba: Pointer to HBA context object.
7441 * @mboxq: Pointer to mailbox object.
7442 *
7443 * The function posts a mailbox to the port. The mailbox is expected
7444 * to be comletely filled in and ready for the port to operate on it.
7445 * This routine executes a synchronous completion operation on the
7446 * mailbox by polling for its completion.
7447 *
7448 * The caller must not be holding any locks when calling this routine.
7449 *
7450 * Returns:
7451 * MBX_SUCCESS - mailbox posted successfully
7452 * Any of the MBX error values.
7453 **/
7454static int
7455lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7456{
7457 int rc = MBX_SUCCESS;
7458 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007459 uint32_t mcqe_status;
7460 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007461 struct lpfc_sli *psli = &phba->sli;
7462 struct lpfc_mqe *mb = &mboxq->u.mqe;
7463 struct lpfc_bmbx_create *mbox_rgn;
7464 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007465
7466 /*
7467 * Only one mailbox can be active to the bootstrap mailbox region
7468 * at a time and there is no queueing provided.
7469 */
7470 spin_lock_irqsave(&phba->hbalock, iflag);
7471 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7472 spin_unlock_irqrestore(&phba->hbalock, iflag);
7473 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007474 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007475 "cannot issue Data: x%x x%x\n",
7476 mboxq->vport ? mboxq->vport->vpi : 0,
7477 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007478 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7479 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007480 psli->sli_flag, MBX_POLL);
7481 return MBXERR_ERROR;
7482 }
7483 /* The server grabs the token and owns it until release */
7484 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7485 phba->sli.mbox_active = mboxq;
7486 spin_unlock_irqrestore(&phba->hbalock, iflag);
7487
James Smart2d843ed2012-09-29 11:29:06 -04007488 /* wait for bootstrap mbox register for readyness */
7489 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7490 if (rc)
7491 goto exit;
7492
James Smartda0436e2009-05-22 14:51:39 -04007493 /*
7494 * Initialize the bootstrap memory region to avoid stale data areas
7495 * in the mailbox post. Then copy the caller's mailbox contents to
7496 * the bmbx mailbox region.
7497 */
7498 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7499 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7500 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7501 sizeof(struct lpfc_mqe));
7502
7503 /* Post the high mailbox dma address to the port and wait for ready. */
7504 dma_address = &phba->sli4_hba.bmbx.dma_address;
7505 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7506
James Smart2d843ed2012-09-29 11:29:06 -04007507 /* wait for bootstrap mbox register for hi-address write done */
7508 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7509 if (rc)
7510 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007511
7512 /* Post the low mailbox dma address to the port. */
7513 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007514
James Smart2d843ed2012-09-29 11:29:06 -04007515 /* wait for bootstrap mbox register for low address write done */
7516 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7517 if (rc)
7518 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007519
7520 /*
7521 * Read the CQ to ensure the mailbox has completed.
7522 * If so, update the mailbox status so that the upper layers
7523 * can complete the request normally.
7524 */
7525 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7526 sizeof(struct lpfc_mqe));
7527 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7528 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7529 sizeof(struct lpfc_mcqe));
7530 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007531 /*
7532 * When the CQE status indicates a failure and the mailbox status
7533 * indicates success then copy the CQE status into the mailbox status
7534 * (and prefix it with x4000).
7535 */
James Smartda0436e2009-05-22 14:51:39 -04007536 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007537 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7538 bf_set(lpfc_mqe_status, mb,
7539 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007540 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007541 } else
7542 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007543
7544 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007545 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007546 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7547 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007548 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7549 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7550 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007551 bf_get(lpfc_mqe_status, mb),
7552 mb->un.mb_words[0], mb->un.mb_words[1],
7553 mb->un.mb_words[2], mb->un.mb_words[3],
7554 mb->un.mb_words[4], mb->un.mb_words[5],
7555 mb->un.mb_words[6], mb->un.mb_words[7],
7556 mb->un.mb_words[8], mb->un.mb_words[9],
7557 mb->un.mb_words[10], mb->un.mb_words[11],
7558 mb->un.mb_words[12], mboxq->mcqe.word0,
7559 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7560 mboxq->mcqe.trailer);
7561exit:
7562 /* We are holding the token, no needed for lock when release */
7563 spin_lock_irqsave(&phba->hbalock, iflag);
7564 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7565 phba->sli.mbox_active = NULL;
7566 spin_unlock_irqrestore(&phba->hbalock, iflag);
7567 return rc;
7568}
7569
7570/**
7571 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7572 * @phba: Pointer to HBA context object.
7573 * @pmbox: Pointer to mailbox object.
7574 * @flag: Flag indicating how the mailbox need to be processed.
7575 *
7576 * This function is called by discovery code and HBA management code to submit
7577 * a mailbox command to firmware with SLI-4 interface spec.
7578 *
7579 * Return codes the caller owns the mailbox command after the return of the
7580 * function.
7581 **/
7582static int
7583lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7584 uint32_t flag)
7585{
7586 struct lpfc_sli *psli = &phba->sli;
7587 unsigned long iflags;
7588 int rc;
7589
James Smartb76f2dc2011-07-22 18:37:42 -04007590 /* dump from issue mailbox command if setup */
7591 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7592
James Smart8fa38512009-07-19 10:01:03 -04007593 rc = lpfc_mbox_dev_check(phba);
7594 if (unlikely(rc)) {
7595 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007596 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007597 "cannot issue Data: x%x x%x\n",
7598 mboxq->vport ? mboxq->vport->vpi : 0,
7599 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007600 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7601 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007602 psli->sli_flag, flag);
7603 goto out_not_finished;
7604 }
7605
James Smartda0436e2009-05-22 14:51:39 -04007606 /* Detect polling mode and jump to a handler */
7607 if (!phba->sli4_hba.intr_enable) {
7608 if (flag == MBX_POLL)
7609 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7610 else
7611 rc = -EIO;
7612 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007613 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007614 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007615 "(x%x/x%x) failure: "
7616 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7617 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007618 mboxq->vport ? mboxq->vport->vpi : 0,
7619 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007620 lpfc_sli_config_mbox_subsys_get(phba,
7621 mboxq),
7622 lpfc_sli_config_mbox_opcode_get(phba,
7623 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007624 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7625 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7626 bf_get(lpfc_mcqe_ext_status,
7627 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007628 psli->sli_flag, flag);
7629 return rc;
7630 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007631 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7632 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007633 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007634 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007635 mboxq->vport ? mboxq->vport->vpi : 0,
7636 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007637 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7638 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007639 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007640 /* Try to block the asynchronous mailbox posting */
7641 rc = lpfc_sli4_async_mbox_block(phba);
7642 if (!rc) {
7643 /* Successfully blocked, now issue sync mbox cmd */
7644 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7645 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007646 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007647 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007648 "(%d):2597 Sync Mailbox command "
7649 "x%x (x%x/x%x) failure: "
7650 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7651 "Data: x%x x%x\n,",
7652 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007653 mboxq->u.mb.mbxCommand,
7654 lpfc_sli_config_mbox_subsys_get(phba,
7655 mboxq),
7656 lpfc_sli_config_mbox_opcode_get(phba,
7657 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007658 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7659 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7660 bf_get(lpfc_mcqe_ext_status,
7661 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007662 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007663 /* Unblock the async mailbox posting afterward */
7664 lpfc_sli4_async_mbox_unblock(phba);
7665 }
7666 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007667 }
7668
7669 /* Now, interrupt mode asynchrous mailbox command */
7670 rc = lpfc_mbox_cmd_check(phba, mboxq);
7671 if (rc) {
7672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007673 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007674 "cannot issue Data: x%x x%x\n",
7675 mboxq->vport ? mboxq->vport->vpi : 0,
7676 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007677 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7678 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007679 psli->sli_flag, flag);
7680 goto out_not_finished;
7681 }
James Smartda0436e2009-05-22 14:51:39 -04007682
7683 /* Put the mailbox command to the driver internal FIFO */
7684 psli->slistat.mbox_busy++;
7685 spin_lock_irqsave(&phba->hbalock, iflags);
7686 lpfc_mbox_put(phba, mboxq);
7687 spin_unlock_irqrestore(&phba->hbalock, iflags);
7688 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7689 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007690 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007691 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7692 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007693 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7694 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007695 phba->pport->port_state,
7696 psli->sli_flag, MBX_NOWAIT);
7697 /* Wake up worker thread to transport mailbox command from head */
7698 lpfc_worker_wake_up(phba);
7699
7700 return MBX_BUSY;
7701
7702out_not_finished:
7703 return MBX_NOT_FINISHED;
7704}
7705
7706/**
7707 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7708 * @phba: Pointer to HBA context object.
7709 *
7710 * This function is called by worker thread to send a mailbox command to
7711 * SLI4 HBA firmware.
7712 *
7713 **/
7714int
7715lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7716{
7717 struct lpfc_sli *psli = &phba->sli;
7718 LPFC_MBOXQ_t *mboxq;
7719 int rc = MBX_SUCCESS;
7720 unsigned long iflags;
7721 struct lpfc_mqe *mqe;
7722 uint32_t mbx_cmnd;
7723
7724 /* Check interrupt mode before post async mailbox command */
7725 if (unlikely(!phba->sli4_hba.intr_enable))
7726 return MBX_NOT_FINISHED;
7727
7728 /* Check for mailbox command service token */
7729 spin_lock_irqsave(&phba->hbalock, iflags);
7730 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7731 spin_unlock_irqrestore(&phba->hbalock, iflags);
7732 return MBX_NOT_FINISHED;
7733 }
7734 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7735 spin_unlock_irqrestore(&phba->hbalock, iflags);
7736 return MBX_NOT_FINISHED;
7737 }
7738 if (unlikely(phba->sli.mbox_active)) {
7739 spin_unlock_irqrestore(&phba->hbalock, iflags);
7740 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7741 "0384 There is pending active mailbox cmd\n");
7742 return MBX_NOT_FINISHED;
7743 }
7744 /* Take the mailbox command service token */
7745 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7746
7747 /* Get the next mailbox command from head of queue */
7748 mboxq = lpfc_mbox_get(phba);
7749
7750 /* If no more mailbox command waiting for post, we're done */
7751 if (!mboxq) {
7752 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7753 spin_unlock_irqrestore(&phba->hbalock, iflags);
7754 return MBX_SUCCESS;
7755 }
7756 phba->sli.mbox_active = mboxq;
7757 spin_unlock_irqrestore(&phba->hbalock, iflags);
7758
7759 /* Check device readiness for posting mailbox command */
7760 rc = lpfc_mbox_dev_check(phba);
7761 if (unlikely(rc))
7762 /* Driver clean routine will clean up pending mailbox */
7763 goto out_not_finished;
7764
7765 /* Prepare the mbox command to be posted */
7766 mqe = &mboxq->u.mqe;
7767 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7768
7769 /* Start timer for the mbox_tmo and log some mailbox post messages */
7770 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007771 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007772
7773 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007774 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007775 "x%x x%x\n",
7776 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007777 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7778 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007779 phba->pport->port_state, psli->sli_flag);
7780
7781 if (mbx_cmnd != MBX_HEARTBEAT) {
7782 if (mboxq->vport) {
7783 lpfc_debugfs_disc_trc(mboxq->vport,
7784 LPFC_DISC_TRC_MBOX_VPORT,
7785 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7786 mbx_cmnd, mqe->un.mb_words[0],
7787 mqe->un.mb_words[1]);
7788 } else {
7789 lpfc_debugfs_disc_trc(phba->pport,
7790 LPFC_DISC_TRC_MBOX,
7791 "MBOX Send: cmd:x%x mb:x%x x%x",
7792 mbx_cmnd, mqe->un.mb_words[0],
7793 mqe->un.mb_words[1]);
7794 }
7795 }
7796 psli->slistat.mbox_cmd++;
7797
7798 /* Post the mailbox command to the port */
7799 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7800 if (rc != MBX_SUCCESS) {
7801 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007802 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007803 "cannot issue Data: x%x x%x\n",
7804 mboxq->vport ? mboxq->vport->vpi : 0,
7805 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007806 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7807 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007808 psli->sli_flag, MBX_NOWAIT);
7809 goto out_not_finished;
7810 }
7811
7812 return rc;
7813
7814out_not_finished:
7815 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007816 if (phba->sli.mbox_active) {
7817 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7818 __lpfc_mbox_cmpl_put(phba, mboxq);
7819 /* Release the token */
7820 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7821 phba->sli.mbox_active = NULL;
7822 }
James Smartda0436e2009-05-22 14:51:39 -04007823 spin_unlock_irqrestore(&phba->hbalock, iflags);
7824
7825 return MBX_NOT_FINISHED;
7826}
7827
7828/**
7829 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7830 * @phba: Pointer to HBA context object.
7831 * @pmbox: Pointer to mailbox object.
7832 * @flag: Flag indicating how the mailbox need to be processed.
7833 *
7834 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7835 * the API jump table function pointer from the lpfc_hba struct.
7836 *
7837 * Return codes the caller owns the mailbox command after the return of the
7838 * function.
7839 **/
7840int
7841lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7842{
7843 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7844}
7845
7846/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007847 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007848 * @phba: The hba struct for which this call is being executed.
7849 * @dev_grp: The HBA PCI-Device group number.
7850 *
7851 * This routine sets up the mbox interface API function jump table in @phba
7852 * struct.
7853 * Returns: 0 - success, -ENODEV - failure.
7854 **/
7855int
7856lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7857{
7858
7859 switch (dev_grp) {
7860 case LPFC_PCI_DEV_LP:
7861 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7862 phba->lpfc_sli_handle_slow_ring_event =
7863 lpfc_sli_handle_slow_ring_event_s3;
7864 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7865 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7866 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7867 break;
7868 case LPFC_PCI_DEV_OC:
7869 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7870 phba->lpfc_sli_handle_slow_ring_event =
7871 lpfc_sli_handle_slow_ring_event_s4;
7872 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7873 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7874 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7875 break;
7876 default:
7877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7878 "1420 Invalid HBA PCI-device group: 0x%x\n",
7879 dev_grp);
7880 return -ENODEV;
7881 break;
7882 }
7883 return 0;
7884}
7885
7886/**
James Smart3621a712009-04-06 18:47:14 -04007887 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007888 * @phba: Pointer to HBA context object.
7889 * @pring: Pointer to driver SLI ring object.
7890 * @piocb: Pointer to address of newly added command iocb.
7891 *
7892 * This function is called with hbalock held to add a command
7893 * iocb to the txq when SLI layer cannot submit the command iocb
7894 * to the ring.
7895 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007896void
James Smart92d7f7b2007-06-17 19:56:38 -05007897__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007898 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007899{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007900 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007901 /* Insert the caller's iocb in the txq tail for later processing. */
7902 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007903}
7904
James Smarte59058c2008-08-24 21:49:00 -04007905/**
James Smart3621a712009-04-06 18:47:14 -04007906 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007907 * @phba: Pointer to HBA context object.
7908 * @pring: Pointer to driver SLI ring object.
7909 * @piocb: Pointer to address of newly added command iocb.
7910 *
7911 * This function is called with hbalock held before a new
7912 * iocb is submitted to the firmware. This function checks
7913 * txq to flush the iocbs in txq to Firmware before
7914 * submitting new iocbs to the Firmware.
7915 * If there are iocbs in the txq which need to be submitted
7916 * to firmware, lpfc_sli_next_iocb returns the first element
7917 * of the txq after dequeuing it from txq.
7918 * If there is no iocb in the txq then the function will return
7919 * *piocb and *piocb is set to NULL. Caller needs to check
7920 * *piocb to find if there are more commands in the txq.
7921 **/
dea31012005-04-17 16:05:31 -05007922static struct lpfc_iocbq *
7923lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007924 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007925{
7926 struct lpfc_iocbq * nextiocb;
7927
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007928 lockdep_assert_held(&phba->hbalock);
7929
dea31012005-04-17 16:05:31 -05007930 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7931 if (!nextiocb) {
7932 nextiocb = *piocb;
7933 *piocb = NULL;
7934 }
7935
7936 return nextiocb;
7937}
7938
James Smarte59058c2008-08-24 21:49:00 -04007939/**
James Smart3772a992009-05-22 14:50:54 -04007940 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007941 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007942 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007943 * @piocb: Pointer to command iocb.
7944 * @flag: Flag indicating if this command can be put into txq.
7945 *
James Smart3772a992009-05-22 14:50:54 -04007946 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7947 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7948 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7949 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7950 * this function allows only iocbs for posting buffers. This function finds
7951 * next available slot in the command ring and posts the command to the
7952 * available slot and writes the port attention register to request HBA start
7953 * processing new iocb. If there is no slot available in the ring and
7954 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7955 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007956 *
James Smart3772a992009-05-22 14:50:54 -04007957 * This function is called with hbalock held. The function will return success
7958 * after it successfully submit the iocb to firmware or after adding to the
7959 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007960 **/
James Smart98c9ea52007-10-27 13:37:33 -04007961static int
James Smart3772a992009-05-22 14:50:54 -04007962__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007963 struct lpfc_iocbq *piocb, uint32_t flag)
7964{
7965 struct lpfc_iocbq *nextiocb;
7966 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007967 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007968
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007969 lockdep_assert_held(&phba->hbalock);
7970
James Smart92d7f7b2007-06-17 19:56:38 -05007971 if (piocb->iocb_cmpl && (!piocb->vport) &&
7972 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7973 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7974 lpfc_printf_log(phba, KERN_ERR,
7975 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007976 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007977 piocb->iocb.ulpCommand);
7978 dump_stack();
7979 return IOCB_ERROR;
7980 }
7981
7982
Linas Vepstas8d63f372007-02-14 14:28:36 -06007983 /* If the PCI channel is in offline state, do not post iocbs. */
7984 if (unlikely(pci_channel_offline(phba->pcidev)))
7985 return IOCB_ERROR;
7986
James Smarta257bf92009-04-06 18:48:10 -04007987 /* If HBA has a deferred error attention, fail the iocb. */
7988 if (unlikely(phba->hba_flag & DEFER_ERATT))
7989 return IOCB_ERROR;
7990
dea31012005-04-17 16:05:31 -05007991 /*
7992 * We should never get an IOCB if we are in a < LINK_DOWN state
7993 */
James Smart2e0fef82007-06-17 19:56:36 -05007994 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007995 return IOCB_ERROR;
7996
7997 /*
7998 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007999 * outstanding event.
dea31012005-04-17 16:05:31 -05008000 */
James Smart0b727fe2007-10-27 13:37:25 -04008001 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008002 goto iocb_busy;
8003
James Smart2e0fef82007-06-17 19:56:36 -05008004 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008005 /*
James Smart2680eea2007-04-25 09:52:55 -04008006 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008007 * can be issued if the link is not up.
8008 */
8009 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008010 case CMD_GEN_REQUEST64_CR:
8011 case CMD_GEN_REQUEST64_CX:
8012 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8013 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008014 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008015 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8016 MENLO_TRANSPORT_TYPE))
8017
8018 goto iocb_busy;
8019 break;
dea31012005-04-17 16:05:31 -05008020 case CMD_QUE_RING_BUF_CN:
8021 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008022 /*
8023 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8024 * completion, iocb_cmpl MUST be 0.
8025 */
8026 if (piocb->iocb_cmpl)
8027 piocb->iocb_cmpl = NULL;
8028 /*FALLTHROUGH*/
8029 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008030 case CMD_CLOSE_XRI_CN:
8031 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008032 break;
8033 default:
8034 goto iocb_busy;
8035 }
8036
8037 /*
8038 * For FCP commands, we must be in a state where we can process link
8039 * attention events.
8040 */
8041 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05008042 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008043 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008044 }
dea31012005-04-17 16:05:31 -05008045
dea31012005-04-17 16:05:31 -05008046 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8047 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8048 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8049
8050 if (iocb)
8051 lpfc_sli_update_ring(phba, pring);
8052 else
8053 lpfc_sli_update_full_ring(phba, pring);
8054
8055 if (!piocb)
8056 return IOCB_SUCCESS;
8057
8058 goto out_busy;
8059
8060 iocb_busy:
8061 pring->stats.iocb_cmd_delay++;
8062
8063 out_busy:
8064
8065 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008066 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008067 return IOCB_SUCCESS;
8068 }
8069
8070 return IOCB_BUSY;
8071}
8072
James Smart3772a992009-05-22 14:50:54 -04008073/**
James Smart4f774512009-05-22 14:52:35 -04008074 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8075 * @phba: Pointer to HBA context object.
8076 * @piocb: Pointer to command iocb.
8077 * @sglq: Pointer to the scatter gather queue object.
8078 *
8079 * This routine converts the bpl or bde that is in the IOCB
8080 * to a sgl list for the sli4 hardware. The physical address
8081 * of the bpl/bde is converted back to a virtual address.
8082 * If the IOCB contains a BPL then the list of BDE's is
8083 * converted to sli4_sge's. If the IOCB contains a single
8084 * BDE then it is converted to a single sli_sge.
8085 * The IOCB is still in cpu endianess so the contents of
8086 * the bpl can be used without byte swapping.
8087 *
8088 * Returns valid XRI = Success, NO_XRI = Failure.
8089**/
8090static uint16_t
8091lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8092 struct lpfc_sglq *sglq)
8093{
8094 uint16_t xritag = NO_XRI;
8095 struct ulp_bde64 *bpl = NULL;
8096 struct ulp_bde64 bde;
8097 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008098 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008099 IOCB_t *icmd;
8100 int numBdes = 0;
8101 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008102 uint32_t offset = 0; /* accumulated offset in the sg request list */
8103 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008104
8105 if (!piocbq || !sglq)
8106 return xritag;
8107
8108 sgl = (struct sli4_sge *)sglq->sgl;
8109 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008110 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8111 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008112 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8113 numBdes = icmd->un.genreq64.bdl.bdeSize /
8114 sizeof(struct ulp_bde64);
8115 /* The addrHigh and addrLow fields within the IOCB
8116 * have not been byteswapped yet so there is no
8117 * need to swap them back.
8118 */
James Smart1b511972011-12-13 13:23:09 -05008119 if (piocbq->context3)
8120 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8121 else
8122 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008123
James Smart1b511972011-12-13 13:23:09 -05008124 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008125 if (!bpl)
8126 return xritag;
8127
8128 for (i = 0; i < numBdes; i++) {
8129 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008130 sgl->addr_hi = bpl->addrHigh;
8131 sgl->addr_lo = bpl->addrLow;
8132
James Smart05580562011-05-24 11:40:48 -04008133 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008134 if ((i+1) == numBdes)
8135 bf_set(lpfc_sli4_sge_last, sgl, 1);
8136 else
8137 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008138 /* swap the size field back to the cpu so we
8139 * can assign it to the sgl.
8140 */
8141 bde.tus.w = le32_to_cpu(bpl->tus.w);
8142 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008143 /* The offsets in the sgl need to be accumulated
8144 * separately for the request and reply lists.
8145 * The request is always first, the reply follows.
8146 */
8147 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8148 /* add up the reply sg entries */
8149 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8150 inbound++;
8151 /* first inbound? reset the offset */
8152 if (inbound == 1)
8153 offset = 0;
8154 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008155 bf_set(lpfc_sli4_sge_type, sgl,
8156 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008157 offset += bde.tus.f.bdeSize;
8158 }
James Smart546fc852011-03-11 16:06:29 -05008159 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008160 bpl++;
8161 sgl++;
8162 }
8163 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8164 /* The addrHigh and addrLow fields of the BDE have not
8165 * been byteswapped yet so they need to be swapped
8166 * before putting them in the sgl.
8167 */
8168 sgl->addr_hi =
8169 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8170 sgl->addr_lo =
8171 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008172 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008173 bf_set(lpfc_sli4_sge_last, sgl, 1);
8174 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008175 sgl->sge_len =
8176 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008177 }
8178 return sglq->sli4_xritag;
8179}
8180
8181/**
James Smart4f774512009-05-22 14:52:35 -04008182 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8183 * @phba: Pointer to HBA context object.
8184 * @piocb: Pointer to command iocb.
8185 * @wqe: Pointer to the work queue entry.
8186 *
8187 * This routine converts the iocb command to its Work Queue Entry
8188 * equivalent. The wqe pointer should not have any fields set when
8189 * this routine is called because it will memcpy over them.
8190 * This routine does not set the CQ_ID or the WQEC bits in the
8191 * wqe.
8192 *
8193 * Returns: 0 = Success, IOCB_ERROR = Failure.
8194 **/
8195static int
8196lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8197 union lpfc_wqe *wqe)
8198{
James Smart5ffc2662009-11-18 15:39:44 -05008199 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008200 uint8_t ct = 0;
8201 uint32_t fip;
8202 uint32_t abort_tag;
8203 uint8_t command_type = ELS_COMMAND_NON_FIP;
8204 uint8_t cmnd;
8205 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008206 uint16_t abrt_iotag;
8207 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008208 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008209 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008210 int numBdes, i;
8211 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008212 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008213 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008214 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008215
James Smart45ed1192009-10-02 15:17:02 -04008216 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008217 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008218 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008219 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008220 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008221 command_type = ELS_COMMAND_FIP;
8222 else
8223 command_type = ELS_COMMAND_NON_FIP;
8224
James Smartb5c53952016-03-31 14:12:30 -07008225 if (phba->fcp_embed_io)
8226 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008227 /* Some of the fields are in the right position already */
8228 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008229 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008230 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008231
8232 abort_tag = (uint32_t) iocbq->iotag;
8233 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008234 /* words0-2 bpl convert bde */
8235 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008236 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8237 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008238 bpl = (struct ulp_bde64 *)
8239 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8240 if (!bpl)
8241 return IOCB_ERROR;
8242
8243 /* Should already be byte swapped. */
8244 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8245 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8246 /* swap the size field back to the cpu so we
8247 * can assign it to the sgl.
8248 */
8249 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008250 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8251 total_len = 0;
8252 for (i = 0; i < numBdes; i++) {
8253 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8254 total_len += bde.tus.f.bdeSize;
8255 }
James Smart4f774512009-05-22 14:52:35 -04008256 } else
James Smart5ffc2662009-11-18 15:39:44 -05008257 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008258
8259 iocbq->iocb.ulpIoTag = iocbq->iotag;
8260 cmnd = iocbq->iocb.ulpCommand;
8261
8262 switch (iocbq->iocb.ulpCommand) {
8263 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008264 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8265 ndlp = iocbq->context_un.ndlp;
8266 else
8267 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008268 if (!iocbq->iocb.ulpLe) {
8269 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8270 "2007 Only Limited Edition cmd Format"
8271 " supported 0x%x\n",
8272 iocbq->iocb.ulpCommand);
8273 return IOCB_ERROR;
8274 }
James Smartff78d8f2011-12-13 13:21:35 -05008275
James Smart5ffc2662009-11-18 15:39:44 -05008276 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008277 /* Els_reguest64 has a TMO */
8278 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8279 iocbq->iocb.ulpTimeout);
8280 /* Need a VF for word 4 set the vf bit*/
8281 bf_set(els_req64_vf, &wqe->els_req, 0);
8282 /* And a VFID for word 12 */
8283 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008284 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008285 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8286 iocbq->iocb.ulpContext);
8287 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8288 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008289 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008290 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008291 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8292 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008293 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8294 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008295 if_type = bf_get(lpfc_sli_intf_if_type,
8296 &phba->sli4_hba.sli_intf);
8297 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008298 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008299 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008300 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008301 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008302 *pcmd == ELS_CMD_PLOGI)) {
8303 bf_set(els_req64_sp, &wqe->els_req, 1);
8304 bf_set(els_req64_sid, &wqe->els_req,
8305 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008306 if ((*pcmd == ELS_CMD_FLOGI) &&
8307 !(phba->fc_topology ==
8308 LPFC_TOPOLOGY_LOOP))
8309 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008310 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8311 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008312 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008313 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008314 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8315 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8316 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8317 }
James Smartc8685952009-11-18 15:39:16 -05008318 }
James Smart6d368e52011-05-24 11:44:12 -04008319 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8320 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008321 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8322 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8323 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8324 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8325 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8326 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008327 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008328 break;
James Smart5ffc2662009-11-18 15:39:44 -05008329 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008330 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8331 iocbq->iocb.un.ulpWord[3]);
8332 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008333 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008334 /* The entire sequence is transmitted for this IOCB */
8335 xmit_len = total_len;
8336 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008337 if (phba->link_flag & LS_LOOPBACK_MODE)
8338 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008339 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008340 /* word3 iocb=io_tag32 wqe=reserved */
8341 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008342 /* word4 relative_offset memcpy */
8343 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008344 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8345 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8346 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8347 LPFC_WQE_IOD_WRITE);
8348 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8349 LPFC_WQE_LENLOC_WORD12);
8350 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008351 wqe->xmit_sequence.xmit_len = xmit_len;
8352 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008353 break;
James Smart4f774512009-05-22 14:52:35 -04008354 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008355 /* word3 iocb=iotag32 wqe=seq_payload_len */
8356 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008357 /* word4 iocb=rsvd wqe=rsvd */
8358 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8359 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008360 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008361 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008362 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8363 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8364 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8365 LPFC_WQE_LENLOC_WORD3);
8366 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008367 break;
James Smart4f774512009-05-22 14:52:35 -04008368 case CMD_FCP_IWRITE64_CR:
8369 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008370 /* word3 iocb=iotag wqe=payload_offset_len */
8371 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008372 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8373 xmit_len + sizeof(struct fcp_rsp));
8374 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8375 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008376 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8377 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8378 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8379 iocbq->iocb.ulpFCP2Rcvy);
8380 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8381 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008382 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8383 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8384 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008385 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008386 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008387 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8388 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8389 if (phba->cfg_XLanePriority) {
8390 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8391 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8392 (phba->cfg_XLanePriority << 1));
8393 }
8394 }
James Smartb5c53952016-03-31 14:12:30 -07008395 /* Note, word 10 is already initialized to 0 */
8396
8397 if (phba->fcp_embed_io) {
8398 struct lpfc_scsi_buf *lpfc_cmd;
8399 struct sli4_sge *sgl;
8400 union lpfc_wqe128 *wqe128;
8401 struct fcp_cmnd *fcp_cmnd;
8402 uint32_t *ptr;
8403
8404 /* 128 byte wqe support here */
8405 wqe128 = (union lpfc_wqe128 *)wqe;
8406
8407 lpfc_cmd = iocbq->context1;
8408 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8409 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8410
8411 /* Word 0-2 - FCP_CMND */
8412 wqe128->generic.bde.tus.f.bdeFlags =
8413 BUFF_TYPE_BDE_IMMED;
8414 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8415 wqe128->generic.bde.addrHigh = 0;
8416 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8417
8418 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8419
8420 /* Word 22-29 FCP CMND Payload */
8421 ptr = &wqe128->words[22];
8422 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8423 }
James Smart7851fe22011-07-22 18:36:52 -04008424 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008425 case CMD_FCP_IREAD64_CR:
8426 /* word3 iocb=iotag wqe=payload_offset_len */
8427 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008428 bf_set(payload_offset_len, &wqe->fcp_iread,
8429 xmit_len + sizeof(struct fcp_rsp));
8430 bf_set(cmd_buff_len, &wqe->fcp_iread,
8431 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008432 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8433 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8434 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8435 iocbq->iocb.ulpFCP2Rcvy);
8436 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008437 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008438 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8439 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8440 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008441 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008442 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008443 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8444 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8445 if (phba->cfg_XLanePriority) {
8446 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8447 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8448 (phba->cfg_XLanePriority << 1));
8449 }
8450 }
James Smartb5c53952016-03-31 14:12:30 -07008451 /* Note, word 10 is already initialized to 0 */
8452
8453 if (phba->fcp_embed_io) {
8454 struct lpfc_scsi_buf *lpfc_cmd;
8455 struct sli4_sge *sgl;
8456 union lpfc_wqe128 *wqe128;
8457 struct fcp_cmnd *fcp_cmnd;
8458 uint32_t *ptr;
8459
8460 /* 128 byte wqe support here */
8461 wqe128 = (union lpfc_wqe128 *)wqe;
8462
8463 lpfc_cmd = iocbq->context1;
8464 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8465 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8466
8467 /* Word 0-2 - FCP_CMND */
8468 wqe128->generic.bde.tus.f.bdeFlags =
8469 BUFF_TYPE_BDE_IMMED;
8470 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8471 wqe128->generic.bde.addrHigh = 0;
8472 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8473
8474 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8475
8476 /* Word 22-29 FCP CMND Payload */
8477 ptr = &wqe128->words[22];
8478 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8479 }
James Smart7851fe22011-07-22 18:36:52 -04008480 break;
James Smartf1126682009-06-10 17:22:44 -04008481 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008482 /* word3 iocb=iotag wqe=payload_offset_len */
8483 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8484 bf_set(payload_offset_len, &wqe->fcp_icmd,
8485 xmit_len + sizeof(struct fcp_rsp));
8486 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8487 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008488 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008489 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008490 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008491 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8492 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8493 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8494 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8495 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008496 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8497 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008498 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8499 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8500 if (phba->cfg_XLanePriority) {
8501 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8502 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8503 (phba->cfg_XLanePriority << 1));
8504 }
8505 }
James Smartb5c53952016-03-31 14:12:30 -07008506 /* Note, word 10 is already initialized to 0 */
8507
8508 if (phba->fcp_embed_io) {
8509 struct lpfc_scsi_buf *lpfc_cmd;
8510 struct sli4_sge *sgl;
8511 union lpfc_wqe128 *wqe128;
8512 struct fcp_cmnd *fcp_cmnd;
8513 uint32_t *ptr;
8514
8515 /* 128 byte wqe support here */
8516 wqe128 = (union lpfc_wqe128 *)wqe;
8517
8518 lpfc_cmd = iocbq->context1;
8519 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8520 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8521
8522 /* Word 0-2 - FCP_CMND */
8523 wqe128->generic.bde.tus.f.bdeFlags =
8524 BUFF_TYPE_BDE_IMMED;
8525 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8526 wqe128->generic.bde.addrHigh = 0;
8527 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8528
8529 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8530
8531 /* Word 22-29 FCP CMND Payload */
8532 ptr = &wqe128->words[22];
8533 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8534 }
James Smart7851fe22011-07-22 18:36:52 -04008535 break;
James Smart4f774512009-05-22 14:52:35 -04008536 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008537 /* For this command calculate the xmit length of the
8538 * request bde.
8539 */
8540 xmit_len = 0;
8541 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8542 sizeof(struct ulp_bde64);
8543 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008544 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008545 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8546 break;
James Smart63e801c2010-11-20 23:14:19 -05008547 xmit_len += bde.tus.f.bdeSize;
8548 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008549 /* word3 iocb=IO_TAG wqe=request_payload_len */
8550 wqe->gen_req.request_payload_len = xmit_len;
8551 /* word4 iocb=parameter wqe=relative_offset memcpy */
8552 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008553 /* word6 context tag copied in memcpy */
8554 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8555 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8556 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8557 "2015 Invalid CT %x command 0x%x\n",
8558 ct, iocbq->iocb.ulpCommand);
8559 return IOCB_ERROR;
8560 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008561 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8562 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8563 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8564 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8565 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8566 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8567 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8568 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008569 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008570 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008571 break;
James Smart4f774512009-05-22 14:52:35 -04008572 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008573 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008574 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008575 /* word3 iocb=iotag32 wqe=response_payload_len */
8576 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008577 /* word4 */
8578 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008579 /* word5 iocb=rsvd wge=did */
8580 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008581 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8582
8583 if_type = bf_get(lpfc_sli_intf_if_type,
8584 &phba->sli4_hba.sli_intf);
8585 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8586 if (iocbq->vport->fc_flag & FC_PT2PT) {
8587 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8588 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8589 iocbq->vport->fc_myDID);
8590 if (iocbq->vport->fc_myDID == Fabric_DID) {
8591 bf_set(wqe_els_did,
8592 &wqe->xmit_els_rsp.wqe_dest, 0);
8593 }
8594 }
8595 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008596 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8597 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8598 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8599 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008600 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008601 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008602 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008603 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008604 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8605 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8606 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8607 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8608 LPFC_WQE_LENLOC_WORD3);
8609 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008610 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8611 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008612 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8613 iocbq->context2)->virt);
8614 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008615 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8616 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008617 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008618 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8619 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008620 phba->vpi_ids[phba->pport->vpi]);
8621 }
James Smart4f774512009-05-22 14:52:35 -04008622 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008623 break;
James Smart4f774512009-05-22 14:52:35 -04008624 case CMD_CLOSE_XRI_CN:
8625 case CMD_ABORT_XRI_CN:
8626 case CMD_ABORT_XRI_CX:
8627 /* words 0-2 memcpy should be 0 rserved */
8628 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008629 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8630 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8631 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8632 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8633 } else
8634 fip = 0;
8635
8636 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008637 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008638 * The link is down, or the command was ELS_FIP
8639 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008640 * on the wire.
8641 */
8642 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8643 else
8644 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8645 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008646 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8647 wqe->abort_cmd.rsrvd5 = 0;
8648 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008649 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8650 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008651 /*
8652 * The abort handler will send us CMD_ABORT_XRI_CN or
8653 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8654 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008655 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8656 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8657 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8658 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008659 cmnd = CMD_ABORT_XRI_CX;
8660 command_type = OTHER_COMMAND;
8661 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008662 break;
James Smart6669f9b2009-10-02 15:16:45 -04008663 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008664 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008665 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008666 * we re-construct this WQE here based on information in
8667 * iocbq from scratch.
8668 */
8669 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008670 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008671 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008672 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8673 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008674 LPFC_ABTS_UNSOL_INT) {
8675 /* ABTS sent by initiator to CT exchange, the
8676 * RX_ID field will be filled with the newly
8677 * allocated responder XRI.
8678 */
8679 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8680 iocbq->sli4_xritag);
8681 } else {
8682 /* ABTS sent by responder to CT exchange, the
8683 * RX_ID field will be filled with the responder
8684 * RX_ID from ABTS.
8685 */
8686 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008687 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008688 }
James Smart6669f9b2009-10-02 15:16:45 -04008689 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8690 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008691
8692 /* Use CT=VPI */
8693 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8694 ndlp->nlp_DID);
8695 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8696 iocbq->iocb.ulpContext);
8697 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008698 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008699 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008700 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8701 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8702 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008703 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8704 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008705 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8706 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8707 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8708 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8709 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8710 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8711 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8712 }
8713
James Smart7851fe22011-07-22 18:36:52 -04008714 break;
James Smart4f774512009-05-22 14:52:35 -04008715 case CMD_XRI_ABORTED_CX:
8716 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008717 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8718 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8719 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8720 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8721 default:
8722 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8723 "2014 Invalid command 0x%x\n",
8724 iocbq->iocb.ulpCommand);
8725 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008726 break;
James Smart4f774512009-05-22 14:52:35 -04008727 }
James Smart6d368e52011-05-24 11:44:12 -04008728
James Smart8012cc32012-10-31 14:44:49 -04008729 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8730 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8731 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8732 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8733 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8734 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8735 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8736 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008737 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8738 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8739 wqe->generic.wqe_com.abort_tag = abort_tag;
8740 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8741 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8742 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8743 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008744 return 0;
8745}
8746
8747/**
8748 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8749 * @phba: Pointer to HBA context object.
8750 * @ring_number: SLI ring number to issue iocb on.
8751 * @piocb: Pointer to command iocb.
8752 * @flag: Flag indicating if this command can be put into txq.
8753 *
8754 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8755 * an iocb command to an HBA with SLI-4 interface spec.
8756 *
8757 * This function is called with hbalock held. The function will return success
8758 * after it successfully submit the iocb to firmware or after adding to the
8759 * txq.
8760 **/
8761static int
8762__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8763 struct lpfc_iocbq *piocb, uint32_t flag)
8764{
8765 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008766 union lpfc_wqe *wqe;
8767 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008768 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008769 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008770
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008771 lockdep_assert_held(&phba->hbalock);
8772
James Smartb5c53952016-03-31 14:12:30 -07008773 /*
8774 * The WQE can be either 64 or 128 bytes,
8775 * so allocate space on the stack assuming the largest.
8776 */
8777 wqe = (union lpfc_wqe *)&wqe128;
8778
James Smart4f774512009-05-22 14:52:35 -04008779 if (piocb->sli4_xritag == NO_XRI) {
8780 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008781 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008782 sglq = NULL;
8783 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008784 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008785 if (!(flag & SLI_IOCB_RET_IOCB)) {
8786 __lpfc_sli_ringtx_put(phba,
8787 pring, piocb);
8788 return IOCB_SUCCESS;
8789 } else {
8790 return IOCB_BUSY;
8791 }
8792 } else {
James Smart6d368e52011-05-24 11:44:12 -04008793 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008794 if (!sglq) {
8795 if (!(flag & SLI_IOCB_RET_IOCB)) {
8796 __lpfc_sli_ringtx_put(phba,
8797 pring,
8798 piocb);
8799 return IOCB_SUCCESS;
8800 } else
8801 return IOCB_BUSY;
8802 }
8803 }
James Smart4f774512009-05-22 14:52:35 -04008804 }
8805 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008806 /* These IO's already have an XRI and a mapped sgl. */
8807 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008808 } else {
James Smart6d368e52011-05-24 11:44:12 -04008809 /*
8810 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008811 * sglq is on the active list
8812 */
James Smartedccdc12013-01-03 15:43:45 -05008813 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008814 if (!sglq)
8815 return IOCB_ERROR;
8816 }
8817
8818 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008819 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008820 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008821 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008822 return IOCB_ERROR;
8823 }
8824
James Smartb5c53952016-03-31 14:12:30 -07008825 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008826 return IOCB_ERROR;
8827
James Smart341af102010-01-26 23:07:37 -05008828 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008829 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008830 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008831 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8832 } else {
8833 wq = phba->sli4_hba.oas_wq;
8834 }
James Smartb5c53952016-03-31 14:12:30 -07008835 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008836 return IOCB_ERROR;
8837 } else {
James Smartea714f32013-04-17 20:18:47 -04008838 if (unlikely(!phba->sli4_hba.els_wq))
8839 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008840 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008841 return IOCB_ERROR;
8842 }
8843 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8844
8845 return 0;
8846}
8847
8848/**
James Smart3772a992009-05-22 14:50:54 -04008849 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8850 *
8851 * This routine wraps the actual lockless version for issusing IOCB function
8852 * pointer from the lpfc_hba struct.
8853 *
8854 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008855 * IOCB_ERROR - Error
8856 * IOCB_SUCCESS - Success
8857 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008858 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008859int
James Smart3772a992009-05-22 14:50:54 -04008860__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8861 struct lpfc_iocbq *piocb, uint32_t flag)
8862{
8863 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8864}
8865
8866/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008867 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008868 * @phba: The hba struct for which this call is being executed.
8869 * @dev_grp: The HBA PCI-Device group number.
8870 *
8871 * This routine sets up the SLI interface API function jump table in @phba
8872 * struct.
8873 * Returns: 0 - success, -ENODEV - failure.
8874 **/
8875int
8876lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8877{
8878
8879 switch (dev_grp) {
8880 case LPFC_PCI_DEV_LP:
8881 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8882 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8883 break;
James Smart4f774512009-05-22 14:52:35 -04008884 case LPFC_PCI_DEV_OC:
8885 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8886 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8887 break;
James Smart3772a992009-05-22 14:50:54 -04008888 default:
8889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8890 "1419 Invalid HBA PCI-device group: 0x%x\n",
8891 dev_grp);
8892 return -ENODEV;
8893 break;
8894 }
8895 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8896 return 0;
8897}
James Smart92d7f7b2007-06-17 19:56:38 -05008898
James Smarta1efe162015-05-21 13:55:20 -04008899/**
8900 * lpfc_sli_calc_ring - Calculates which ring to use
8901 * @phba: Pointer to HBA context object.
8902 * @ring_number: Initial ring
8903 * @piocb: Pointer to command iocb.
8904 *
8905 * For SLI4, FCP IO can deferred to one fo many WQs, based on
8906 * fcp_wqidx, thus we need to calculate the corresponding ring.
8907 * Since ABORTS must go on the same WQ of the command they are
8908 * aborting, we use command's fcp_wqidx.
8909 */
James Smart9bd2bff52014-09-03 12:57:30 -04008910int
8911lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
8912 struct lpfc_iocbq *piocb)
8913{
James Smart8b0dff12015-05-22 10:42:38 -04008914 if (phba->sli_rev < LPFC_SLI_REV4)
8915 return ring_number;
8916
8917 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8918 if (!(phba->cfg_fof) ||
8919 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8920 if (unlikely(!phba->sli4_hba.fcp_wq))
8921 return LPFC_HBA_ERROR;
8922 /*
8923 * for abort iocb fcp_wqidx should already
8924 * be setup based on what work queue we used.
8925 */
8926 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
8927 piocb->fcp_wqidx =
8928 lpfc_sli4_scmd_to_wqidx_distr(phba,
8929 piocb->context1);
8930 ring_number = MAX_SLI3_CONFIGURED_RINGS +
8931 piocb->fcp_wqidx;
8932 } else {
8933 if (unlikely(!phba->sli4_hba.oas_wq))
8934 return LPFC_HBA_ERROR;
8935 piocb->fcp_wqidx = 0;
8936 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04008937 }
8938 }
8939 return ring_number;
8940}
8941
James Smarte59058c2008-08-24 21:49:00 -04008942/**
James Smart3621a712009-04-06 18:47:14 -04008943 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008944 * @phba: Pointer to HBA context object.
8945 * @pring: Pointer to driver SLI ring object.
8946 * @piocb: Pointer to command iocb.
8947 * @flag: Flag indicating if this command can be put into txq.
8948 *
8949 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8950 * function. This function gets the hbalock and calls
8951 * __lpfc_sli_issue_iocb function and will return the error returned
8952 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8953 * functions which do not hold hbalock.
8954 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008955int
James Smart3772a992009-05-22 14:50:54 -04008956lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008957 struct lpfc_iocbq *piocb, uint32_t flag)
8958{
James Smartba20c852012-08-03 12:36:52 -04008959 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008960 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008961 struct lpfc_queue *fpeq;
8962 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008963 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008964 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008965
James Smart7e56aa22012-08-03 12:35:34 -04008966 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04008967 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
8968 if (unlikely(ring_number == LPFC_HBA_ERROR))
8969 return IOCB_ERROR;
8970 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04008971
James Smart9bd2bff52014-09-03 12:57:30 -04008972 pring = &phba->sli.ring[ring_number];
8973 spin_lock_irqsave(&pring->ring_lock, iflags);
8974 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8975 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04008976
James Smart9bd2bff52014-09-03 12:57:30 -04008977 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
8978 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04008979
James Smart9bd2bff52014-09-03 12:57:30 -04008980 if (atomic_dec_and_test(&fcp_eq_hdl->
8981 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04008982
James Smart9bd2bff52014-09-03 12:57:30 -04008983 /* Get associated EQ with this index */
8984 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04008985
James Smart9bd2bff52014-09-03 12:57:30 -04008986 /* Turn off interrupts from this EQ */
8987 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04008988
James Smart9bd2bff52014-09-03 12:57:30 -04008989 /*
8990 * Process all the events on FCP EQ
8991 */
8992 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8993 lpfc_sli4_hba_handle_eqe(phba,
8994 eqe, idx);
8995 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04008996 }
James Smartba20c852012-08-03 12:36:52 -04008997
James Smart9bd2bff52014-09-03 12:57:30 -04008998 /* Always clear and re-arm the EQ */
8999 lpfc_sli4_eq_release(fpeq,
9000 LPFC_QUEUE_REARM);
9001 }
9002 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009003 }
James Smart7e56aa22012-08-03 12:35:34 -04009004 } else {
9005 /* For now, SLI2/3 will still use hbalock */
9006 spin_lock_irqsave(&phba->hbalock, iflags);
9007 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9008 spin_unlock_irqrestore(&phba->hbalock, iflags);
9009 }
James Smart92d7f7b2007-06-17 19:56:38 -05009010 return rc;
9011}
9012
James Smarte59058c2008-08-24 21:49:00 -04009013/**
James Smart3621a712009-04-06 18:47:14 -04009014 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009015 * @phba: Pointer to HBA context object.
9016 *
9017 * This function is called while driver attaches with the
9018 * HBA to setup the extra ring. The extra ring is used
9019 * only when driver needs to support target mode functionality
9020 * or IP over FC functionalities.
9021 *
9022 * This function is called with no lock held.
9023 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009024static int
9025lpfc_extra_ring_setup( struct lpfc_hba *phba)
9026{
9027 struct lpfc_sli *psli;
9028 struct lpfc_sli_ring *pring;
9029
9030 psli = &phba->sli;
9031
9032 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009033
9034 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009035 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009036 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9037 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9038 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9039 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009040
James Smarta4bc3372006-12-02 13:34:16 -05009041 /* and give them to the extra ring */
9042 pring = &psli->ring[psli->extra_ring];
9043
James Smart7e56aa22012-08-03 12:35:34 -04009044 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9045 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9046 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9047 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009048
9049 /* Setup default profile for this ring */
9050 pring->iotag_max = 4096;
9051 pring->num_mask = 1;
9052 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009053 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9054 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009055 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9056 return 0;
9057}
9058
James Smartcb69f7d2011-12-13 13:21:57 -05009059/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9060 * @phba: Pointer to HBA context object.
9061 * @iocbq: Pointer to iocb object.
9062 *
9063 * The async_event handler calls this routine when it receives
9064 * an ASYNC_STATUS_CN event from the port. The port generates
9065 * this event when an Abort Sequence request to an rport fails
9066 * twice in succession. The abort could be originated by the
9067 * driver or by the port. The ABTS could have been for an ELS
9068 * or FCP IO. The port only generates this event when an ABTS
9069 * fails to complete after one retry.
9070 */
9071static void
9072lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9073 struct lpfc_iocbq *iocbq)
9074{
9075 struct lpfc_nodelist *ndlp = NULL;
9076 uint16_t rpi = 0, vpi = 0;
9077 struct lpfc_vport *vport = NULL;
9078
9079 /* The rpi in the ulpContext is vport-sensitive. */
9080 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9081 rpi = iocbq->iocb.ulpContext;
9082
9083 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9084 "3092 Port generated ABTS async event "
9085 "on vpi %d rpi %d status 0x%x\n",
9086 vpi, rpi, iocbq->iocb.ulpStatus);
9087
9088 vport = lpfc_find_vport_by_vpid(phba, vpi);
9089 if (!vport)
9090 goto err_exit;
9091 ndlp = lpfc_findnode_rpi(vport, rpi);
9092 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9093 goto err_exit;
9094
9095 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9096 lpfc_sli_abts_recover_port(vport, ndlp);
9097 return;
9098
9099 err_exit:
9100 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9101 "3095 Event Context not found, no "
9102 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9103 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9104 vpi, rpi);
9105}
9106
9107/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9108 * @phba: pointer to HBA context object.
9109 * @ndlp: nodelist pointer for the impacted rport.
9110 * @axri: pointer to the wcqe containing the failed exchange.
9111 *
9112 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9113 * port. The port generates this event when an abort exchange request to an
9114 * rport fails twice in succession with no reply. The abort could be originated
9115 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9116 */
9117void
9118lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9119 struct lpfc_nodelist *ndlp,
9120 struct sli4_wcqe_xri_aborted *axri)
9121{
9122 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009123 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009124
James Smart6b5151f2012-01-18 16:24:06 -05009125 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009126 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9127 "3115 Node Context not found, driver "
9128 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009129 return;
9130 }
9131
James Smartcb69f7d2011-12-13 13:21:57 -05009132 vport = ndlp->vport;
9133 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9134 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009135 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009136 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009137 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009138 bf_get(lpfc_wcqe_xa_status, axri),
9139 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009140
James Smart5c1db2a2012-03-01 22:34:36 -05009141 /*
9142 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9143 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9144 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9145 */
James Smarte3d2b802012-08-14 14:25:43 -04009146 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009147 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9148 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009149 lpfc_sli_abts_recover_port(vport, ndlp);
9150}
9151
James Smarte59058c2008-08-24 21:49:00 -04009152/**
James Smart3621a712009-04-06 18:47:14 -04009153 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009154 * @phba: Pointer to HBA context object.
9155 * @pring: Pointer to driver SLI ring object.
9156 * @iocbq: Pointer to iocb object.
9157 *
9158 * This function is called by the slow ring event handler
9159 * function when there is an ASYNC event iocb in the ring.
9160 * This function is called with no lock held.
9161 * Currently this function handles only temperature related
9162 * ASYNC events. The function decodes the temperature sensor
9163 * event message and posts events for the management applications.
9164 **/
James Smart98c9ea52007-10-27 13:37:33 -04009165static void
James Smart57127f12007-10-27 13:37:05 -04009166lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9167 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9168{
9169 IOCB_t *icmd;
9170 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009171 struct temp_event temp_event_data;
9172 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009173 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009174
9175 icmd = &iocbq->iocb;
9176 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009177
James Smartcb69f7d2011-12-13 13:21:57 -05009178 switch (evt_code) {
9179 case ASYNC_TEMP_WARN:
9180 case ASYNC_TEMP_SAFE:
9181 temp_event_data.data = (uint32_t) icmd->ulpContext;
9182 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9183 if (evt_code == ASYNC_TEMP_WARN) {
9184 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9185 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9186 "0347 Adapter is very hot, please take "
9187 "corrective action. temperature : %d Celsius\n",
9188 (uint32_t) icmd->ulpContext);
9189 } else {
9190 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9191 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9192 "0340 Adapter temperature is OK now. "
9193 "temperature : %d Celsius\n",
9194 (uint32_t) icmd->ulpContext);
9195 }
9196
9197 /* Send temperature change event to applications */
9198 shost = lpfc_shost_from_vport(phba->pport);
9199 fc_host_post_vendor_event(shost, fc_get_event_number(),
9200 sizeof(temp_event_data), (char *) &temp_event_data,
9201 LPFC_NL_VENDOR_ID);
9202 break;
9203 case ASYNC_STATUS_CN:
9204 lpfc_sli_abts_err_handler(phba, iocbq);
9205 break;
9206 default:
James Smarta257bf92009-04-06 18:48:10 -04009207 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009209 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009210 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009211 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9212 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9213 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9214 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009215 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009216 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9217 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9218 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9219 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9220
James Smartcb69f7d2011-12-13 13:21:57 -05009221 break;
James Smart57127f12007-10-27 13:37:05 -04009222 }
James Smart57127f12007-10-27 13:37:05 -04009223}
9224
9225
James Smarte59058c2008-08-24 21:49:00 -04009226/**
James Smart3621a712009-04-06 18:47:14 -04009227 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009228 * @phba: Pointer to HBA context object.
9229 *
9230 * lpfc_sli_setup sets up rings of the SLI interface with
9231 * number of iocbs per ring and iotags. This function is
9232 * called while driver attach to the HBA and before the
9233 * interrupts are enabled. So there is no need for locking.
9234 *
9235 * This function always returns 0.
9236 **/
dea31012005-04-17 16:05:31 -05009237int
9238lpfc_sli_setup(struct lpfc_hba *phba)
9239{
James Smarted957682007-06-17 19:56:37 -05009240 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009241 struct lpfc_sli *psli = &phba->sli;
9242 struct lpfc_sli_ring *pring;
9243
James Smart2a76a282012-08-03 12:35:54 -04009244 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9245 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009246 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009247 psli->sli_flag = 0;
9248 psli->fcp_ring = LPFC_FCP_RING;
9249 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009250 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009251
James Bottomley604a3e32005-10-29 10:28:33 -05009252 psli->iocbq_lookup = NULL;
9253 psli->iocbq_lookup_len = 0;
9254 psli->last_iotag = 0;
9255
dea31012005-04-17 16:05:31 -05009256 for (i = 0; i < psli->num_rings; i++) {
9257 pring = &psli->ring[i];
9258 switch (i) {
9259 case LPFC_FCP_RING: /* ring 0 - FCP */
9260 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009261 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9262 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9263 pring->sli.sli3.numCiocb +=
9264 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9265 pring->sli.sli3.numRiocb +=
9266 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9267 pring->sli.sli3.numCiocb +=
9268 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9269 pring->sli.sli3.numRiocb +=
9270 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9271 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009272 SLI3_IOCB_CMD_SIZE :
9273 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009274 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009275 SLI3_IOCB_RSP_SIZE :
9276 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009277 pring->iotag_ctr = 0;
9278 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009279 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009280 pring->fast_iotag = pring->iotag_max;
9281 pring->num_mask = 0;
9282 break;
James Smarta4bc3372006-12-02 13:34:16 -05009283 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009284 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009285 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9286 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9287 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009288 SLI3_IOCB_CMD_SIZE :
9289 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009290 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009291 SLI3_IOCB_RSP_SIZE :
9292 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009293 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009294 pring->num_mask = 0;
9295 break;
9296 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9297 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009298 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9299 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9300 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009301 SLI3_IOCB_CMD_SIZE :
9302 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009303 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009304 SLI3_IOCB_RSP_SIZE :
9305 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009306 pring->fast_iotag = 0;
9307 pring->iotag_ctr = 0;
9308 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009309 pring->lpfc_sli_rcv_async_status =
9310 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009311 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009312 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009313 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9314 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009315 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009316 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009317 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009318 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9319 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009320 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009321 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009322 pring->prt[2].profile = 0; /* Mask 2 */
9323 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009324 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009325 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009326 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009327 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009328 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009329 pring->prt[3].profile = 0; /* Mask 3 */
9330 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009331 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009332 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009333 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009334 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009335 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009336 break;
9337 }
James Smart7e56aa22012-08-03 12:35:34 -04009338 totiocbsize += (pring->sli.sli3.numCiocb *
9339 pring->sli.sli3.sizeCiocb) +
9340 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009341 }
James Smarted957682007-06-17 19:56:37 -05009342 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009343 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009344 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9345 "SLI2 SLIM Data: x%x x%lx\n",
9346 phba->brd_no, totiocbsize,
9347 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009348 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009349 if (phba->cfg_multi_ring_support == 2)
9350 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009351
9352 return 0;
9353}
9354
James Smarte59058c2008-08-24 21:49:00 -04009355/**
James Smart3621a712009-04-06 18:47:14 -04009356 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009357 * @phba: Pointer to HBA context object.
9358 *
9359 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9360 * ring. This function also initializes ring indices of each ring.
9361 * This function is called during the initialization of the SLI
9362 * interface of an HBA.
9363 * This function is called with no lock held and always returns
9364 * 1.
9365 **/
dea31012005-04-17 16:05:31 -05009366int
James Smart2e0fef82007-06-17 19:56:36 -05009367lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009368{
9369 struct lpfc_sli *psli;
9370 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009371 int i;
dea31012005-04-17 16:05:31 -05009372
9373 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009374 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009375 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009376 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009377 /* Initialize list headers for txq and txcmplq as double linked lists */
9378 for (i = 0; i < psli->num_rings; i++) {
9379 pring = &psli->ring[i];
9380 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009381 pring->sli.sli3.next_cmdidx = 0;
9382 pring->sli.sli3.local_getidx = 0;
9383 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009384 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009385 INIT_LIST_HEAD(&pring->txq);
9386 INIT_LIST_HEAD(&pring->txcmplq);
9387 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009388 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009389 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009390 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009391 }
James Smart2e0fef82007-06-17 19:56:36 -05009392 spin_unlock_irq(&phba->hbalock);
9393 return 1;
dea31012005-04-17 16:05:31 -05009394}
9395
James Smarte59058c2008-08-24 21:49:00 -04009396/**
James Smart04c68492009-05-22 14:52:52 -04009397 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9398 * @phba: Pointer to HBA context object.
9399 *
9400 * This routine flushes the mailbox command subsystem. It will unconditionally
9401 * flush all the mailbox commands in the three possible stages in the mailbox
9402 * command sub-system: pending mailbox command queue; the outstanding mailbox
9403 * command; and completed mailbox command queue. It is caller's responsibility
9404 * to make sure that the driver is in the proper state to flush the mailbox
9405 * command sub-system. Namely, the posting of mailbox commands into the
9406 * pending mailbox command queue from the various clients must be stopped;
9407 * either the HBA is in a state that it will never works on the outstanding
9408 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9409 * mailbox command has been completed.
9410 **/
9411static void
9412lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9413{
9414 LIST_HEAD(completions);
9415 struct lpfc_sli *psli = &phba->sli;
9416 LPFC_MBOXQ_t *pmb;
9417 unsigned long iflag;
9418
9419 /* Flush all the mailbox commands in the mbox system */
9420 spin_lock_irqsave(&phba->hbalock, iflag);
9421 /* The pending mailbox command queue */
9422 list_splice_init(&phba->sli.mboxq, &completions);
9423 /* The outstanding active mailbox command */
9424 if (psli->mbox_active) {
9425 list_add_tail(&psli->mbox_active->list, &completions);
9426 psli->mbox_active = NULL;
9427 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9428 }
9429 /* The completed mailbox command queue */
9430 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9431 spin_unlock_irqrestore(&phba->hbalock, iflag);
9432
9433 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9434 while (!list_empty(&completions)) {
9435 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9436 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9437 if (pmb->mbox_cmpl)
9438 pmb->mbox_cmpl(phba, pmb);
9439 }
9440}
9441
9442/**
James Smart3621a712009-04-06 18:47:14 -04009443 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009444 * @vport: Pointer to virtual port object.
9445 *
9446 * lpfc_sli_host_down is called to clean up the resources
9447 * associated with a vport before destroying virtual
9448 * port data structures.
9449 * This function does following operations:
9450 * - Free discovery resources associated with this virtual
9451 * port.
9452 * - Free iocbs associated with this virtual port in
9453 * the txq.
9454 * - Send abort for all iocb commands associated with this
9455 * vport in txcmplq.
9456 *
9457 * This function is called with no lock held and always returns 1.
9458 **/
dea31012005-04-17 16:05:31 -05009459int
James Smart92d7f7b2007-06-17 19:56:38 -05009460lpfc_sli_host_down(struct lpfc_vport *vport)
9461{
James Smart858c9f62007-06-17 19:56:39 -05009462 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009463 struct lpfc_hba *phba = vport->phba;
9464 struct lpfc_sli *psli = &phba->sli;
9465 struct lpfc_sli_ring *pring;
9466 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009467 int i;
9468 unsigned long flags = 0;
9469 uint16_t prev_pring_flag;
9470
9471 lpfc_cleanup_discovery_resources(vport);
9472
9473 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009474 for (i = 0; i < psli->num_rings; i++) {
9475 pring = &psli->ring[i];
9476 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009477 /* Only slow rings */
9478 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009479 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009480 /* Set the lpfc data pending flag */
9481 set_bit(LPFC_DATA_READY, &phba->data_flags);
9482 }
James Smart92d7f7b2007-06-17 19:56:38 -05009483 /*
9484 * Error everything on the txq since these iocbs have not been
9485 * given to the FW yet.
9486 */
James Smart92d7f7b2007-06-17 19:56:38 -05009487 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9488 if (iocb->vport != vport)
9489 continue;
James Smart858c9f62007-06-17 19:56:39 -05009490 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009491 }
9492
9493 /* Next issue ABTS for everything on the txcmplq */
9494 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9495 list) {
9496 if (iocb->vport != vport)
9497 continue;
9498 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9499 }
9500
9501 pring->flag = prev_pring_flag;
9502 }
9503
9504 spin_unlock_irqrestore(&phba->hbalock, flags);
9505
James Smarta257bf92009-04-06 18:48:10 -04009506 /* Cancel all the IOCBs from the completions list */
9507 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9508 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009509 return 1;
9510}
9511
James Smarte59058c2008-08-24 21:49:00 -04009512/**
James Smart3621a712009-04-06 18:47:14 -04009513 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009514 * @phba: Pointer to HBA context object.
9515 *
9516 * This function cleans up all iocb, buffers, mailbox commands
9517 * while shutting down the HBA. This function is called with no
9518 * lock held and always returns 1.
9519 * This function does the following to cleanup driver resources:
9520 * - Free discovery resources for each virtual port
9521 * - Cleanup any pending fabric iocbs
9522 * - Iterate through the iocb txq and free each entry
9523 * in the list.
9524 * - Free up any buffer posted to the HBA
9525 * - Free mailbox commands in the mailbox queue.
9526 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009527int
James Smart2e0fef82007-06-17 19:56:36 -05009528lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009529{
James Smart2534ba72007-04-25 09:52:20 -04009530 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009531 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009532 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009533 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009534 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009535 int i;
9536
9537 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009538 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009539
dea31012005-04-17 16:05:31 -05009540 lpfc_hba_down_prep(phba);
9541
James Smart92d7f7b2007-06-17 19:56:38 -05009542 lpfc_fabric_abort_hba(phba);
9543
James Smart2e0fef82007-06-17 19:56:36 -05009544 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009545 for (i = 0; i < psli->num_rings; i++) {
9546 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009547 /* Only slow rings */
9548 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009549 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009550 /* Set the lpfc data pending flag */
9551 set_bit(LPFC_DATA_READY, &phba->data_flags);
9552 }
dea31012005-04-17 16:05:31 -05009553
9554 /*
9555 * Error everything on the txq since these iocbs have not been
9556 * given to the FW yet.
9557 */
James Smart2534ba72007-04-25 09:52:20 -04009558 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009559 }
James Smart2e0fef82007-06-17 19:56:36 -05009560 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009561
James Smarta257bf92009-04-06 18:48:10 -04009562 /* Cancel all the IOCBs from the completions list */
9563 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9564 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009565
James Smart0ff10d42008-01-11 01:52:36 -05009566 spin_lock_irqsave(&phba->hbalock, flags);
9567 list_splice_init(&phba->elsbuf, &completions);
9568 phba->elsbuf_cnt = 0;
9569 phba->elsbuf_prev_cnt = 0;
9570 spin_unlock_irqrestore(&phba->hbalock, flags);
9571
9572 while (!list_empty(&completions)) {
9573 list_remove_head(&completions, buf_ptr,
9574 struct lpfc_dmabuf, list);
9575 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9576 kfree(buf_ptr);
9577 }
9578
dea31012005-04-17 16:05:31 -05009579 /* Return any active mbox cmds */
9580 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009581
James Smartda0436e2009-05-22 14:51:39 -04009582 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009583 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009584 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009585
James Smartda0436e2009-05-22 14:51:39 -04009586 return 1;
9587}
James Smart92d7f7b2007-06-17 19:56:38 -05009588
James Smartda0436e2009-05-22 14:51:39 -04009589/**
James Smart3621a712009-04-06 18:47:14 -04009590 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009591 * @srcp: Source memory pointer.
9592 * @destp: Destination memory pointer.
9593 * @cnt: Number of words required to be copied.
9594 *
9595 * This function is used for copying data between driver memory
9596 * and the SLI memory. This function also changes the endianness
9597 * of each word if native endianness is different from SLI
9598 * endianness. This function can be called with or without
9599 * lock.
9600 **/
dea31012005-04-17 16:05:31 -05009601void
9602lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9603{
9604 uint32_t *src = srcp;
9605 uint32_t *dest = destp;
9606 uint32_t ldata;
9607 int i;
9608
9609 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9610 ldata = *src;
9611 ldata = le32_to_cpu(ldata);
9612 *dest = ldata;
9613 src++;
9614 dest++;
9615 }
9616}
9617
James Smarte59058c2008-08-24 21:49:00 -04009618
9619/**
James Smarta0c87cb2009-07-19 10:01:10 -04009620 * lpfc_sli_bemem_bcopy - SLI memory copy function
9621 * @srcp: Source memory pointer.
9622 * @destp: Destination memory pointer.
9623 * @cnt: Number of words required to be copied.
9624 *
9625 * This function is used for copying data between a data structure
9626 * with big endian representation to local endianness.
9627 * This function can be called with or without lock.
9628 **/
9629void
9630lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9631{
9632 uint32_t *src = srcp;
9633 uint32_t *dest = destp;
9634 uint32_t ldata;
9635 int i;
9636
9637 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9638 ldata = *src;
9639 ldata = be32_to_cpu(ldata);
9640 *dest = ldata;
9641 src++;
9642 dest++;
9643 }
9644}
9645
9646/**
James Smart3621a712009-04-06 18:47:14 -04009647 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009648 * @phba: Pointer to HBA context object.
9649 * @pring: Pointer to driver SLI ring object.
9650 * @mp: Pointer to driver buffer object.
9651 *
9652 * This function is called with no lock held.
9653 * It always return zero after adding the buffer to the postbufq
9654 * buffer list.
9655 **/
dea31012005-04-17 16:05:31 -05009656int
James Smart2e0fef82007-06-17 19:56:36 -05009657lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9658 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009659{
9660 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9661 later */
James Smart2e0fef82007-06-17 19:56:36 -05009662 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009663 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009664 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009665 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009666 return 0;
9667}
9668
James Smarte59058c2008-08-24 21:49:00 -04009669/**
James Smart3621a712009-04-06 18:47:14 -04009670 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009671 * @phba: Pointer to HBA context object.
9672 *
9673 * When HBQ is enabled, buffers are searched based on tags. This function
9674 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9675 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9676 * does not conflict with tags of buffer posted for unsolicited events.
9677 * The function returns the allocated tag. The function is called with
9678 * no locks held.
9679 **/
James Smart76bb24e2007-10-27 13:38:00 -04009680uint32_t
9681lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9682{
9683 spin_lock_irq(&phba->hbalock);
9684 phba->buffer_tag_count++;
9685 /*
9686 * Always set the QUE_BUFTAG_BIT to distiguish between
9687 * a tag assigned by HBQ.
9688 */
9689 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9690 spin_unlock_irq(&phba->hbalock);
9691 return phba->buffer_tag_count;
9692}
9693
James Smarte59058c2008-08-24 21:49:00 -04009694/**
James Smart3621a712009-04-06 18:47:14 -04009695 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009696 * @phba: Pointer to HBA context object.
9697 * @pring: Pointer to driver SLI ring object.
9698 * @tag: Buffer tag.
9699 *
9700 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9701 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9702 * iocb is posted to the response ring with the tag of the buffer.
9703 * This function searches the pring->postbufq list using the tag
9704 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9705 * iocb. If the buffer is found then lpfc_dmabuf object of the
9706 * buffer is returned to the caller else NULL is returned.
9707 * This function is called with no lock held.
9708 **/
James Smart76bb24e2007-10-27 13:38:00 -04009709struct lpfc_dmabuf *
9710lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9711 uint32_t tag)
9712{
9713 struct lpfc_dmabuf *mp, *next_mp;
9714 struct list_head *slp = &pring->postbufq;
9715
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009716 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009717 spin_lock_irq(&phba->hbalock);
9718 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9719 if (mp->buffer_tag == tag) {
9720 list_del_init(&mp->list);
9721 pring->postbufq_cnt--;
9722 spin_unlock_irq(&phba->hbalock);
9723 return mp;
9724 }
9725 }
9726
9727 spin_unlock_irq(&phba->hbalock);
9728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009729 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009730 "ring %d Data x%lx x%p x%p x%x\n",
9731 pring->ringno, (unsigned long) tag,
9732 slp->next, slp->prev, pring->postbufq_cnt);
9733
9734 return NULL;
9735}
dea31012005-04-17 16:05:31 -05009736
James Smarte59058c2008-08-24 21:49:00 -04009737/**
James Smart3621a712009-04-06 18:47:14 -04009738 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009739 * @phba: Pointer to HBA context object.
9740 * @pring: Pointer to driver SLI ring object.
9741 * @phys: DMA address of the buffer.
9742 *
9743 * This function searches the buffer list using the dma_address
9744 * of unsolicited event to find the driver's lpfc_dmabuf object
9745 * corresponding to the dma_address. The function returns the
9746 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9747 * This function is called by the ct and els unsolicited event
9748 * handlers to get the buffer associated with the unsolicited
9749 * event.
9750 *
9751 * This function is called with no lock held.
9752 **/
dea31012005-04-17 16:05:31 -05009753struct lpfc_dmabuf *
9754lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9755 dma_addr_t phys)
9756{
9757 struct lpfc_dmabuf *mp, *next_mp;
9758 struct list_head *slp = &pring->postbufq;
9759
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009760 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009761 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009762 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9763 if (mp->phys == phys) {
9764 list_del_init(&mp->list);
9765 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009766 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009767 return mp;
9768 }
9769 }
9770
James Smart2e0fef82007-06-17 19:56:36 -05009771 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009773 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009774 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009775 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009776 slp->next, slp->prev, pring->postbufq_cnt);
9777 return NULL;
9778}
9779
James Smarte59058c2008-08-24 21:49:00 -04009780/**
James Smart3621a712009-04-06 18:47:14 -04009781 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009782 * @phba: Pointer to HBA context object.
9783 * @cmdiocb: Pointer to driver command iocb object.
9784 * @rspiocb: Pointer to driver response iocb object.
9785 *
9786 * This function is the completion handler for the abort iocbs for
9787 * ELS commands. This function is called from the ELS ring event
9788 * handler with no lock held. This function frees memory resources
9789 * associated with the abort iocb.
9790 **/
dea31012005-04-17 16:05:31 -05009791static void
James Smart2e0fef82007-06-17 19:56:36 -05009792lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9793 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009794{
James Smart2e0fef82007-06-17 19:56:36 -05009795 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009796 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009797 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009798
9799 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009800
9801 /*
9802 * Assume that the port already completed and returned, or
9803 * will return the iocb. Just Log the message.
9804 */
James Smart2680eea2007-04-25 09:52:55 -04009805 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9806 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9807
James Smart2e0fef82007-06-17 19:56:36 -05009808 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009809 if (phba->sli_rev < LPFC_SLI_REV4) {
9810 if (abort_iotag != 0 &&
9811 abort_iotag <= phba->sli.last_iotag)
9812 abort_iocb =
9813 phba->sli.iocbq_lookup[abort_iotag];
9814 } else
9815 /* For sli4 the abort_tag is the XRI,
9816 * so the abort routine puts the iotag of the iocb
9817 * being aborted in the context field of the abort
9818 * IOCB.
9819 */
9820 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009821
James Smart2a9bf3d2010-06-07 15:24:45 -04009822 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9823 "0327 Cannot abort els iocb %p "
9824 "with tag %x context %x, abort status %x, "
9825 "abort code %x\n",
9826 abort_iocb, abort_iotag, abort_context,
9827 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009828
James Smartff78d8f2011-12-13 13:21:35 -05009829 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009830 }
James Bottomley604a3e32005-10-29 10:28:33 -05009831 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009832 return;
9833}
9834
James Smarte59058c2008-08-24 21:49:00 -04009835/**
James Smart3621a712009-04-06 18:47:14 -04009836 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009837 * @phba: Pointer to HBA context object.
9838 * @cmdiocb: Pointer to driver command iocb object.
9839 * @rspiocb: Pointer to driver response iocb object.
9840 *
9841 * The function is called from SLI ring event handler with no
9842 * lock held. This function is the completion handler for ELS commands
9843 * which are aborted. The function frees memory resources used for
9844 * the aborted ELS commands.
9845 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009846static void
9847lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9848 struct lpfc_iocbq *rspiocb)
9849{
9850 IOCB_t *irsp = &rspiocb->iocb;
9851
9852 /* ELS cmd tag <ulpIoTag> completes */
9853 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009854 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009855 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009856 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009857 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009858 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9859 lpfc_ct_free_iocb(phba, cmdiocb);
9860 else
9861 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009862 return;
9863}
9864
James Smarte59058c2008-08-24 21:49:00 -04009865/**
James Smart5af5eee2010-10-22 11:06:38 -04009866 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009867 * @phba: Pointer to HBA context object.
9868 * @pring: Pointer to driver SLI ring object.
9869 * @cmdiocb: Pointer to driver command iocb object.
9870 *
James Smart5af5eee2010-10-22 11:06:38 -04009871 * This function issues an abort iocb for the provided command iocb down to
9872 * the port. Other than the case the outstanding command iocb is an abort
9873 * request, this function issues abort out unconditionally. This function is
9874 * called with hbalock held. The function returns 0 when it fails due to
9875 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009876 **/
James Smart5af5eee2010-10-22 11:06:38 -04009877static int
9878lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009879 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009880{
James Smart2e0fef82007-06-17 19:56:36 -05009881 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009882 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009883 IOCB_t *icmd = NULL;
9884 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009885 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009886 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009887 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009888
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009889 lockdep_assert_held(&phba->hbalock);
9890
James Smart92d7f7b2007-06-17 19:56:38 -05009891 /*
9892 * There are certain command types we don't want to abort. And we
9893 * don't want to abort commands that are already in the process of
9894 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009895 */
9896 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009897 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009898 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9899 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009900 return 0;
9901
dea31012005-04-17 16:05:31 -05009902 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009903 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009904 if (abtsiocbp == NULL)
9905 return 0;
dea31012005-04-17 16:05:31 -05009906
James Smart07951072007-04-25 09:51:38 -04009907 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009908 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009909 */
9910 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9911
dea31012005-04-17 16:05:31 -05009912 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009913 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9914 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009915 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009916 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009917 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9918 }
James Smartda0436e2009-05-22 14:51:39 -04009919 else
9920 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009921 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009922 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009923
James Smart5ffc2662009-11-18 15:39:44 -05009924 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9925 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009926 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9927 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04009928 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
9929 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05009930
James Smart2e0fef82007-06-17 19:56:36 -05009931 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009932 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9933 else
9934 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9935
9936 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009937
James Smarte8b62012007-08-02 11:10:09 -04009938 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9939 "0339 Abort xri x%x, original iotag x%x, "
9940 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009941 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009942 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009943 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009944
9945 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009946 ring_number =
9947 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
9948 if (unlikely(ring_number == LPFC_HBA_ERROR))
9949 return 0;
9950 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -04009951 /* Note: both hbalock and ring_lock need to be set here */
9952 spin_lock_irqsave(&pring->ring_lock, iflags);
9953 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9954 abtsiocbp, 0);
9955 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9956 } else {
9957 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9958 abtsiocbp, 0);
9959 }
James Smart07951072007-04-25 09:51:38 -04009960
James Smartd7c255b2008-08-24 21:50:00 -04009961 if (retval)
9962 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009963
9964 /*
9965 * Caller to this routine should check for IOCB_ERROR
9966 * and handle it properly. This routine no longer removes
9967 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9968 */
9969 return retval;
9970}
9971
9972/**
9973 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9974 * @phba: Pointer to HBA context object.
9975 * @pring: Pointer to driver SLI ring object.
9976 * @cmdiocb: Pointer to driver command iocb object.
9977 *
9978 * This function issues an abort iocb for the provided command iocb. In case
9979 * of unloading, the abort iocb will not be issued to commands on the ELS
9980 * ring. Instead, the callback function shall be changed to those commands
9981 * so that nothing happens when them finishes. This function is called with
9982 * hbalock held. The function returns 0 when the command iocb is an abort
9983 * request.
9984 **/
9985int
9986lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9987 struct lpfc_iocbq *cmdiocb)
9988{
9989 struct lpfc_vport *vport = cmdiocb->vport;
9990 int retval = IOCB_ERROR;
9991 IOCB_t *icmd = NULL;
9992
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009993 lockdep_assert_held(&phba->hbalock);
9994
James Smart5af5eee2010-10-22 11:06:38 -04009995 /*
9996 * There are certain command types we don't want to abort. And we
9997 * don't want to abort commands that are already in the process of
9998 * being aborted.
9999 */
10000 icmd = &cmdiocb->iocb;
10001 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10002 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10003 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10004 return 0;
10005
10006 /*
10007 * If we're unloading, don't abort iocb on the ELS ring, but change
10008 * the callback so that nothing happens when it finishes.
10009 */
10010 if ((vport->load_flag & FC_UNLOADING) &&
10011 (pring->ringno == LPFC_ELS_RING)) {
10012 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10013 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10014 else
10015 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10016 goto abort_iotag_exit;
10017 }
10018
10019 /* Now, we try to issue the abort to the cmdiocb out */
10020 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10021
James Smart07951072007-04-25 09:51:38 -040010022abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010023 /*
10024 * Caller to this routine should check for IOCB_ERROR
10025 * and handle it properly. This routine no longer removes
10026 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010027 */
James Smart2e0fef82007-06-17 19:56:36 -050010028 return retval;
dea31012005-04-17 16:05:31 -050010029}
10030
James Smarte59058c2008-08-24 21:49:00 -040010031/**
James Smart5af5eee2010-10-22 11:06:38 -040010032 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10033 * @phba: pointer to lpfc HBA data structure.
10034 *
10035 * This routine will abort all pending and outstanding iocbs to an HBA.
10036 **/
10037void
10038lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10039{
10040 struct lpfc_sli *psli = &phba->sli;
10041 struct lpfc_sli_ring *pring;
10042 int i;
10043
10044 for (i = 0; i < psli->num_rings; i++) {
10045 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010046 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010047 }
10048}
10049
10050/**
James Smart3621a712009-04-06 18:47:14 -040010051 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010052 * @iocbq: Pointer to driver iocb object.
10053 * @vport: Pointer to driver virtual port object.
10054 * @tgt_id: SCSI ID of the target.
10055 * @lun_id: LUN ID of the scsi device.
10056 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10057 *
James Smart3621a712009-04-06 18:47:14 -040010058 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010059 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10060 * 0 if the filtering criteria is met for the given iocb and will return
10061 * 1 if the filtering criteria is not met.
10062 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10063 * given iocb is for the SCSI device specified by vport, tgt_id and
10064 * lun_id parameter.
10065 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10066 * given iocb is for the SCSI target specified by vport and tgt_id
10067 * parameters.
10068 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10069 * given iocb is for the SCSI host associated with the given vport.
10070 * This function is called with no locks held.
10071 **/
dea31012005-04-17 16:05:31 -050010072static int
James Smart51ef4c22007-08-02 11:10:31 -040010073lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10074 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010075 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010076{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010077 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010078 int rc = 1;
10079
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010080 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10081 return rc;
10082
James Smart51ef4c22007-08-02 11:10:31 -040010083 if (iocbq->vport != vport)
10084 return rc;
10085
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010086 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010087
James Smart495a7142008-06-14 22:52:59 -040010088 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010089 return rc;
10090
10091 switch (ctx_cmd) {
10092 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010093 if ((lpfc_cmd->rdata->pnode) &&
10094 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10095 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010096 rc = 0;
10097 break;
10098 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010099 if ((lpfc_cmd->rdata->pnode) &&
10100 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010101 rc = 0;
10102 break;
dea31012005-04-17 16:05:31 -050010103 case LPFC_CTX_HOST:
10104 rc = 0;
10105 break;
10106 default:
10107 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010108 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010109 break;
10110 }
10111
10112 return rc;
10113}
10114
James Smarte59058c2008-08-24 21:49:00 -040010115/**
James Smart3621a712009-04-06 18:47:14 -040010116 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010117 * @vport: Pointer to virtual port.
10118 * @tgt_id: SCSI ID of the target.
10119 * @lun_id: LUN ID of the scsi device.
10120 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10121 *
10122 * This function returns number of FCP commands pending for the vport.
10123 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10124 * commands pending on the vport associated with SCSI device specified
10125 * by tgt_id and lun_id parameters.
10126 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10127 * commands pending on the vport associated with SCSI target specified
10128 * by tgt_id parameter.
10129 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10130 * commands pending on the vport.
10131 * This function returns the number of iocbs which satisfy the filter.
10132 * This function is called without any lock held.
10133 **/
dea31012005-04-17 16:05:31 -050010134int
James Smart51ef4c22007-08-02 11:10:31 -040010135lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10136 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010137{
James Smart51ef4c22007-08-02 11:10:31 -040010138 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010139 struct lpfc_iocbq *iocbq;
10140 int sum, i;
dea31012005-04-17 16:05:31 -050010141
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010142 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10143 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010144
James Smart51ef4c22007-08-02 11:10:31 -040010145 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10146 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010147 sum++;
dea31012005-04-17 16:05:31 -050010148 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010149
dea31012005-04-17 16:05:31 -050010150 return sum;
10151}
10152
James Smarte59058c2008-08-24 21:49:00 -040010153/**
James Smart3621a712009-04-06 18:47:14 -040010154 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010155 * @phba: Pointer to HBA context object
10156 * @cmdiocb: Pointer to command iocb object.
10157 * @rspiocb: Pointer to response iocb object.
10158 *
10159 * This function is called when an aborted FCP iocb completes. This
10160 * function is called by the ring event handler with no lock held.
10161 * This function frees the iocb.
10162 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010163void
James Smart2e0fef82007-06-17 19:56:36 -050010164lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10165 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010166{
James Smartcb69f7d2011-12-13 13:21:57 -050010167 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010168 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010169 "original iotag x%x, abort cmd iotag x%x "
10170 "status 0x%x, reason 0x%x\n",
10171 cmdiocb->iocb.un.acxri.abortContextTag,
10172 cmdiocb->iocb.un.acxri.abortIoTag,
10173 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10174 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010175 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010176 return;
10177}
10178
James Smarte59058c2008-08-24 21:49:00 -040010179/**
James Smart3621a712009-04-06 18:47:14 -040010180 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010181 * @vport: Pointer to virtual port.
10182 * @pring: Pointer to driver SLI ring object.
10183 * @tgt_id: SCSI ID of the target.
10184 * @lun_id: LUN ID of the scsi device.
10185 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10186 *
10187 * This function sends an abort command for every SCSI command
10188 * associated with the given virtual port pending on the ring
10189 * filtered by lpfc_sli_validate_fcp_iocb function.
10190 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10191 * FCP iocbs associated with lun specified by tgt_id and lun_id
10192 * parameters
10193 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10194 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10195 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10196 * FCP iocbs associated with virtual port.
10197 * This function returns number of iocbs it failed to abort.
10198 * This function is called with no locks held.
10199 **/
dea31012005-04-17 16:05:31 -050010200int
James Smart51ef4c22007-08-02 11:10:31 -040010201lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10202 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010203{
James Smart51ef4c22007-08-02 11:10:31 -040010204 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010205 struct lpfc_iocbq *iocbq;
10206 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010207 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010208 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010209 int i;
dea31012005-04-17 16:05:31 -050010210
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010211 for (i = 1; i <= phba->sli.last_iotag; i++) {
10212 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010213
James Smart51ef4c22007-08-02 11:10:31 -040010214 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010215 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010216 continue;
10217
James Smartafbd8d82013-09-06 12:22:13 -040010218 /*
10219 * If the iocbq is already being aborted, don't take a second
10220 * action, but do count it.
10221 */
10222 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10223 continue;
10224
dea31012005-04-17 16:05:31 -050010225 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010226 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010227 if (abtsiocb == NULL) {
10228 errcnt++;
10229 continue;
10230 }
dea31012005-04-17 16:05:31 -050010231
James Smartafbd8d82013-09-06 12:22:13 -040010232 /* indicate the IO is being aborted by the driver. */
10233 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10234
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010235 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010236 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10237 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010238 if (phba->sli_rev == LPFC_SLI_REV4)
10239 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10240 else
10241 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010242 abtsiocb->iocb.ulpLe = 1;
10243 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010244 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010245
James Smart5ffc2662009-11-18 15:39:44 -050010246 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10247 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010248 if (iocbq->iocb_flag & LPFC_IO_FCP)
10249 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010250 if (iocbq->iocb_flag & LPFC_IO_FOF)
10251 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010252
James Smart2e0fef82007-06-17 19:56:36 -050010253 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010254 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10255 else
10256 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10257
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010258 /* Setup callback routine and issue the command. */
10259 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010260 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10261 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010262 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010263 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010264 errcnt++;
10265 continue;
10266 }
10267 }
10268
10269 return errcnt;
10270}
10271
James Smarte59058c2008-08-24 21:49:00 -040010272/**
James Smart98912dda2014-04-04 13:52:31 -040010273 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10274 * @vport: Pointer to virtual port.
10275 * @pring: Pointer to driver SLI ring object.
10276 * @tgt_id: SCSI ID of the target.
10277 * @lun_id: LUN ID of the scsi device.
10278 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10279 *
10280 * This function sends an abort command for every SCSI command
10281 * associated with the given virtual port pending on the ring
10282 * filtered by lpfc_sli_validate_fcp_iocb function.
10283 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10284 * FCP iocbs associated with lun specified by tgt_id and lun_id
10285 * parameters
10286 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10287 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10288 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10289 * FCP iocbs associated with virtual port.
10290 * This function returns number of iocbs it aborted .
10291 * This function is called with no locks held right after a taskmgmt
10292 * command is sent.
10293 **/
10294int
10295lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10296 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10297{
10298 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010299 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010300 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010301 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010302 struct lpfc_iocbq *iocbq;
10303 IOCB_t *icmd;
10304 int sum, i, ret_val;
10305 unsigned long iflags;
10306 struct lpfc_sli_ring *pring_s4;
10307 uint32_t ring_number;
10308
10309 spin_lock_irq(&phba->hbalock);
10310
10311 /* all I/Os are in process of being flushed */
10312 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10313 spin_unlock_irq(&phba->hbalock);
10314 return 0;
10315 }
10316 sum = 0;
10317
10318 for (i = 1; i <= phba->sli.last_iotag; i++) {
10319 iocbq = phba->sli.iocbq_lookup[i];
10320
10321 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10322 cmd) != 0)
10323 continue;
10324
10325 /*
10326 * If the iocbq is already being aborted, don't take a second
10327 * action, but do count it.
10328 */
10329 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10330 continue;
10331
10332 /* issue ABTS for this IOCB based on iotag */
10333 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10334 if (abtsiocbq == NULL)
10335 continue;
10336
10337 icmd = &iocbq->iocb;
10338 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10339 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10340 if (phba->sli_rev == LPFC_SLI_REV4)
10341 abtsiocbq->iocb.un.acxri.abortIoTag =
10342 iocbq->sli4_xritag;
10343 else
10344 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10345 abtsiocbq->iocb.ulpLe = 1;
10346 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10347 abtsiocbq->vport = vport;
10348
10349 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10350 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10351 if (iocbq->iocb_flag & LPFC_IO_FCP)
10352 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010353 if (iocbq->iocb_flag & LPFC_IO_FOF)
10354 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010355
James Smart8c50d252014-09-03 12:58:16 -040010356 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10357 ndlp = lpfc_cmd->rdata->pnode;
10358
10359 if (lpfc_is_link_up(phba) &&
10360 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010361 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10362 else
10363 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10364
10365 /* Setup callback routine and issue the command. */
10366 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10367
10368 /*
10369 * Indicate the IO is being aborted by the driver and set
10370 * the caller's flag into the aborted IO.
10371 */
10372 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10373
10374 if (phba->sli_rev == LPFC_SLI_REV4) {
10375 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10376 iocbq->fcp_wqidx;
10377 pring_s4 = &phba->sli.ring[ring_number];
10378 /* Note: both hbalock and ring_lock must be set here */
10379 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10380 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10381 abtsiocbq, 0);
10382 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10383 } else {
10384 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10385 abtsiocbq, 0);
10386 }
10387
10388
10389 if (ret_val == IOCB_ERROR)
10390 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10391 else
10392 sum++;
10393 }
10394 spin_unlock_irq(&phba->hbalock);
10395 return sum;
10396}
10397
10398/**
James Smart3621a712009-04-06 18:47:14 -040010399 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010400 * @phba: Pointer to HBA context object.
10401 * @cmdiocbq: Pointer to command iocb.
10402 * @rspiocbq: Pointer to response iocb.
10403 *
10404 * This function is the completion handler for iocbs issued using
10405 * lpfc_sli_issue_iocb_wait function. This function is called by the
10406 * ring event handler function without any lock held. This function
10407 * can be called from both worker thread context and interrupt
10408 * context. This function also can be called from other thread which
10409 * cleans up the SLI layer objects.
10410 * This function copy the contents of the response iocb to the
10411 * response iocb memory object provided by the caller of
10412 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10413 * sleeps for the iocb completion.
10414 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010415static void
10416lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10417 struct lpfc_iocbq *cmdiocbq,
10418 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010419{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010420 wait_queue_head_t *pdone_q;
10421 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010422 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010423
James Smart2e0fef82007-06-17 19:56:36 -050010424 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010425 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10426
10427 /*
10428 * A time out has occurred for the iocb. If a time out
10429 * completion handler has been supplied, call it. Otherwise,
10430 * just free the iocbq.
10431 */
10432
10433 spin_unlock_irqrestore(&phba->hbalock, iflags);
10434 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10435 cmdiocbq->wait_iocb_cmpl = NULL;
10436 if (cmdiocbq->iocb_cmpl)
10437 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10438 else
10439 lpfc_sli_release_iocbq(phba, cmdiocbq);
10440 return;
10441 }
10442
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010443 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10444 if (cmdiocbq->context2 && rspiocbq)
10445 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10446 &rspiocbq->iocb, sizeof(IOCB_t));
10447
James Smart0f65ff62010-02-26 14:14:23 -050010448 /* Set the exchange busy flag for task management commands */
10449 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10450 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10451 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10452 cur_iocbq);
10453 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10454 }
10455
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010456 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010457 if (pdone_q)
10458 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010459 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010460 return;
10461}
10462
James Smarte59058c2008-08-24 21:49:00 -040010463/**
James Smartd11e31d2009-06-10 17:23:06 -040010464 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10465 * @phba: Pointer to HBA context object..
10466 * @piocbq: Pointer to command iocb.
10467 * @flag: Flag to test.
10468 *
10469 * This routine grabs the hbalock and then test the iocb_flag to
10470 * see if the passed in flag is set.
10471 * Returns:
10472 * 1 if flag is set.
10473 * 0 if flag is not set.
10474 **/
10475static int
10476lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10477 struct lpfc_iocbq *piocbq, uint32_t flag)
10478{
10479 unsigned long iflags;
10480 int ret;
10481
10482 spin_lock_irqsave(&phba->hbalock, iflags);
10483 ret = piocbq->iocb_flag & flag;
10484 spin_unlock_irqrestore(&phba->hbalock, iflags);
10485 return ret;
10486
10487}
10488
10489/**
James Smart3621a712009-04-06 18:47:14 -040010490 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010491 * @phba: Pointer to HBA context object..
10492 * @pring: Pointer to sli ring.
10493 * @piocb: Pointer to command iocb.
10494 * @prspiocbq: Pointer to response iocb.
10495 * @timeout: Timeout in number of seconds.
10496 *
10497 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010498 * iocb to complete. The iocb_cmpl field of the shall be used
10499 * to handle iocbs which time out. If the field is NULL, the
10500 * function shall free the iocbq structure. If more clean up is
10501 * needed, the caller is expected to provide a completion function
10502 * that will provide the needed clean up. If the iocb command is
10503 * not completed within timeout seconds, the function will either
10504 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10505 * completion function set in the iocb_cmpl field and then return
10506 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10507 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010508 * The function waits for the iocb completion using an
10509 * non-interruptible wait.
10510 * This function will sleep while waiting for iocb completion.
10511 * So, this function should not be called from any context which
10512 * does not allow sleeping. Due to the same reason, this function
10513 * cannot be called with interrupt disabled.
10514 * This function assumes that the iocb completions occur while
10515 * this function sleep. So, this function cannot be called from
10516 * the thread which process iocb completion for this ring.
10517 * This function clears the iocb_flag of the iocb object before
10518 * issuing the iocb and the iocb completion handler sets this
10519 * flag and wakes this thread when the iocb completes.
10520 * The contents of the response iocb will be copied to prspiocbq
10521 * by the completion handler when the command completes.
10522 * This function returns IOCB_SUCCESS when success.
10523 * This function is called with no lock held.
10524 **/
dea31012005-04-17 16:05:31 -050010525int
James Smart2e0fef82007-06-17 19:56:36 -050010526lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010527 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010528 struct lpfc_iocbq *piocb,
10529 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010530 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010531{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010532 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010533 long timeleft, timeout_req = 0;
10534 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010535 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010536 struct lpfc_iocbq *iocb;
10537 int txq_cnt = 0;
10538 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010539 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010540 unsigned long iflags;
10541 bool iocb_completed = true;
10542
dea31012005-04-17 16:05:31 -050010543 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010544 * If the caller has provided a response iocbq buffer, then context2
10545 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010546 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010547 if (prspiocbq) {
10548 if (piocb->context2)
10549 return IOCB_ERROR;
10550 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010551 }
10552
James Smart5a0916b2013-07-15 18:31:42 -040010553 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010554 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10555 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010556 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010557
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010558 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010559 if (lpfc_readl(phba->HCregaddr, &creg_val))
10560 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010561 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10562 writel(creg_val, phba->HCregaddr);
10563 readl(phba->HCregaddr); /* flush */
10564 }
10565
James Smart2a9bf3d2010-06-07 15:24:45 -040010566 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10567 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010568 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010569 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010570 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010571 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010572 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010573 spin_lock_irqsave(&phba->hbalock, iflags);
10574 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010575
James Smart5a0916b2013-07-15 18:31:42 -040010576 /*
10577 * IOCB timed out. Inform the wake iocb wait
10578 * completion function and set local status
10579 */
10580
10581 iocb_completed = false;
10582 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10583 }
10584 spin_unlock_irqrestore(&phba->hbalock, iflags);
10585 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010586 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010587 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010588 /* Note: we are not indicating if the IOCB has a success
10589 * status or not - that's for the caller to check.
10590 * IOCB_SUCCESS means just that the command was sent and
10591 * completed. Not that it completed successfully.
10592 * */
James Smart7054a602007-04-25 09:52:34 -040010593 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010594 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010595 "0338 IOCB wait timeout error - no "
10596 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010597 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010598 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010600 "0330 IOCB wake NOT set, "
10601 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010602 timeout, (timeleft / jiffies));
10603 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010604 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010605 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010606 if (phba->cfg_log_verbose & LOG_SLI) {
10607 list_for_each_entry(iocb, &pring->txq, list) {
10608 txq_cnt++;
10609 }
10610 list_for_each_entry(iocb, &pring->txcmplq, list) {
10611 txcmplq_cnt++;
10612 }
10613 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10614 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10615 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10616 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010617 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010618 } else {
10619 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010620 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010621 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010622 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010623 }
10624
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010625 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010626 if (lpfc_readl(phba->HCregaddr, &creg_val))
10627 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010628 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10629 writel(creg_val, phba->HCregaddr);
10630 readl(phba->HCregaddr); /* flush */
10631 }
10632
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010633 if (prspiocbq)
10634 piocb->context2 = NULL;
10635
10636 piocb->context_un.wait_queue = NULL;
10637 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010638 return retval;
10639}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010640
James Smarte59058c2008-08-24 21:49:00 -040010641/**
James Smart3621a712009-04-06 18:47:14 -040010642 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010643 * @phba: Pointer to HBA context object.
10644 * @pmboxq: Pointer to driver mailbox object.
10645 * @timeout: Timeout in number of seconds.
10646 *
10647 * This function issues the mailbox to firmware and waits for the
10648 * mailbox command to complete. If the mailbox command is not
10649 * completed within timeout seconds, it returns MBX_TIMEOUT.
10650 * The function waits for the mailbox completion using an
10651 * interruptible wait. If the thread is woken up due to a
10652 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10653 * should not free the mailbox resources, if this function returns
10654 * MBX_TIMEOUT.
10655 * This function will sleep while waiting for mailbox completion.
10656 * So, this function should not be called from any context which
10657 * does not allow sleeping. Due to the same reason, this function
10658 * cannot be called with interrupt disabled.
10659 * This function assumes that the mailbox completion occurs while
10660 * this function sleep. So, this function cannot be called from
10661 * the worker thread which processes mailbox completion.
10662 * This function is called in the context of HBA management
10663 * applications.
10664 * This function returns MBX_SUCCESS when successful.
10665 * This function is called with no lock held.
10666 **/
dea31012005-04-17 16:05:31 -050010667int
James Smart2e0fef82007-06-17 19:56:36 -050010668lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010669 uint32_t timeout)
10670{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010671 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a22013-05-31 17:05:27 -040010672 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010673 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010674 unsigned long flag;
dea31012005-04-17 16:05:31 -050010675
James Smartb230b8a22013-05-31 17:05:27 -040010676 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010677 if (pmboxq->context1)
James Smartb230b8a22013-05-31 17:05:27 -040010678 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010679
James Smart495a7142008-06-14 22:52:59 -040010680 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010681 /* setup wake call as IOCB callback */
10682 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10683 /* setup context field to pass wait_queue pointer to wake function */
10684 pmboxq->context1 = &done_q;
10685
dea31012005-04-17 16:05:31 -050010686 /* now issue the command */
10687 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010688 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010689 wait_event_interruptible_timeout(done_q,
10690 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010691 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010692
James Smart858c9f62007-06-17 19:56:39 -050010693 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040010694 /* restore the possible extended buffer for free resource */
10695 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010696 /*
10697 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10698 * else do not free the resources.
10699 */
James Smartd7c47992010-06-08 18:31:54 -040010700 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010701 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010702 } else {
James Smart7054a602007-04-25 09:52:34 -040010703 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010704 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10705 }
10706 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040010707 } else {
10708 /* restore the possible extended buffer for free resource */
10709 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010710 }
10711
dea31012005-04-17 16:05:31 -050010712 return retval;
10713}
10714
James Smarte59058c2008-08-24 21:49:00 -040010715/**
James Smart3772a992009-05-22 14:50:54 -040010716 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010717 * @phba: Pointer to HBA context.
10718 *
James Smart3772a992009-05-22 14:50:54 -040010719 * This function is called to shutdown the driver's mailbox sub-system.
10720 * It first marks the mailbox sub-system is in a block state to prevent
10721 * the asynchronous mailbox command from issued off the pending mailbox
10722 * command queue. If the mailbox command sub-system shutdown is due to
10723 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10724 * the mailbox sub-system flush routine to forcefully bring down the
10725 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10726 * as with offline or HBA function reset), this routine will wait for the
10727 * outstanding mailbox command to complete before invoking the mailbox
10728 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010729 **/
James Smart3772a992009-05-22 14:50:54 -040010730void
James Smart618a5232012-06-12 13:54:36 -040010731lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010732{
James Smart3772a992009-05-22 14:50:54 -040010733 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010734 unsigned long timeout;
10735
James Smart618a5232012-06-12 13:54:36 -040010736 if (mbx_action == LPFC_MBX_NO_WAIT) {
10737 /* delay 100ms for port state */
10738 msleep(100);
10739 lpfc_sli_mbox_sys_flush(phba);
10740 return;
10741 }
James Smarta183a152011-10-10 21:32:43 -040010742 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010743
James Smart3772a992009-05-22 14:50:54 -040010744 spin_lock_irq(&phba->hbalock);
10745 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010746
10747 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010748 /* Determine how long we might wait for the active mailbox
10749 * command to be gracefully completed by firmware.
10750 */
James Smarta183a152011-10-10 21:32:43 -040010751 if (phba->sli.mbox_active)
10752 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10753 phba->sli.mbox_active) *
10754 1000) + jiffies;
10755 spin_unlock_irq(&phba->hbalock);
10756
James Smart3772a992009-05-22 14:50:54 -040010757 while (phba->sli.mbox_active) {
10758 /* Check active mailbox complete status every 2ms */
10759 msleep(2);
10760 if (time_after(jiffies, timeout))
10761 /* Timeout, let the mailbox flush routine to
10762 * forcefully release active mailbox command
10763 */
10764 break;
10765 }
James Smartd7069f02012-03-01 22:36:29 -050010766 } else
10767 spin_unlock_irq(&phba->hbalock);
10768
James Smart3772a992009-05-22 14:50:54 -040010769 lpfc_sli_mbox_sys_flush(phba);
10770}
10771
10772/**
10773 * lpfc_sli_eratt_read - read sli-3 error attention events
10774 * @phba: Pointer to HBA context.
10775 *
10776 * This function is called to read the SLI3 device error attention registers
10777 * for possible error attention events. The caller must hold the hostlock
10778 * with spin_lock_irq().
10779 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010780 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010781 * Register and returns 0 otherwise.
10782 **/
10783static int
10784lpfc_sli_eratt_read(struct lpfc_hba *phba)
10785{
James Smarted957682007-06-17 19:56:37 -050010786 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010787
James Smart3772a992009-05-22 14:50:54 -040010788 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010789 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10790 goto unplug_err;
10791
James Smart3772a992009-05-22 14:50:54 -040010792 if (ha_copy & HA_ERATT) {
10793 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010794 if (lpfc_sli_read_hs(phba))
10795 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010796
James Smart3772a992009-05-22 14:50:54 -040010797 /* Check if there is a deferred error condition is active */
10798 if ((HS_FFER1 & phba->work_hs) &&
10799 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010800 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010801 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010802 /* Clear all interrupt enable conditions */
10803 writel(0, phba->HCregaddr);
10804 readl(phba->HCregaddr);
10805 }
10806
10807 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010808 phba->work_ha |= HA_ERATT;
10809 /* Indicate polling handles this ERATT */
10810 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010811 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010812 }
James Smart3772a992009-05-22 14:50:54 -040010813 return 0;
James Smart9940b972011-03-11 16:06:12 -050010814
10815unplug_err:
10816 /* Set the driver HS work bitmap */
10817 phba->work_hs |= UNPLUG_ERR;
10818 /* Set the driver HA work bitmap */
10819 phba->work_ha |= HA_ERATT;
10820 /* Indicate polling handles this ERATT */
10821 phba->hba_flag |= HBA_ERATT_HANDLED;
10822 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010823}
10824
James Smarte59058c2008-08-24 21:49:00 -040010825/**
James Smartda0436e2009-05-22 14:51:39 -040010826 * lpfc_sli4_eratt_read - read sli-4 error attention events
10827 * @phba: Pointer to HBA context.
10828 *
10829 * This function is called to read the SLI4 device error attention registers
10830 * for possible error attention events. The caller must hold the hostlock
10831 * with spin_lock_irq().
10832 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010833 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010834 * Register and returns 0 otherwise.
10835 **/
10836static int
10837lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10838{
10839 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010840 uint32_t if_type, portsmphr;
10841 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010842
James Smart2fcee4b2010-12-15 17:57:46 -050010843 /*
10844 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010845 * registers for error attention. This can be changed later.
10846 */
James Smart2fcee4b2010-12-15 17:57:46 -050010847 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10848 switch (if_type) {
10849 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010850 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10851 &uerr_sta_lo) ||
10852 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10853 &uerr_sta_hi)) {
10854 phba->work_hs |= UNPLUG_ERR;
10855 phba->work_ha |= HA_ERATT;
10856 phba->hba_flag |= HBA_ERATT_HANDLED;
10857 return 1;
10858 }
James Smart2fcee4b2010-12-15 17:57:46 -050010859 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10860 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10862 "1423 HBA Unrecoverable error: "
10863 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10864 "ue_mask_lo_reg=0x%x, "
10865 "ue_mask_hi_reg=0x%x\n",
10866 uerr_sta_lo, uerr_sta_hi,
10867 phba->sli4_hba.ue_mask_lo,
10868 phba->sli4_hba.ue_mask_hi);
10869 phba->work_status[0] = uerr_sta_lo;
10870 phba->work_status[1] = uerr_sta_hi;
10871 phba->work_ha |= HA_ERATT;
10872 phba->hba_flag |= HBA_ERATT_HANDLED;
10873 return 1;
10874 }
10875 break;
10876 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010877 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10878 &portstat_reg.word0) ||
10879 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10880 &portsmphr)){
10881 phba->work_hs |= UNPLUG_ERR;
10882 phba->work_ha |= HA_ERATT;
10883 phba->hba_flag |= HBA_ERATT_HANDLED;
10884 return 1;
10885 }
James Smart2fcee4b2010-12-15 17:57:46 -050010886 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10887 phba->work_status[0] =
10888 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10889 phba->work_status[1] =
10890 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010892 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010893 "port status reg 0x%x, "
10894 "port smphr reg 0x%x, "
10895 "error 1=0x%x, error 2=0x%x\n",
10896 portstat_reg.word0,
10897 portsmphr,
10898 phba->work_status[0],
10899 phba->work_status[1]);
10900 phba->work_ha |= HA_ERATT;
10901 phba->hba_flag |= HBA_ERATT_HANDLED;
10902 return 1;
10903 }
10904 break;
10905 case LPFC_SLI_INTF_IF_TYPE_1:
10906 default:
James Smarta747c9c2009-11-18 15:41:10 -050010907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010908 "2886 HBA Error Attention on unsupported "
10909 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010910 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010911 }
James Smart2fcee4b2010-12-15 17:57:46 -050010912
James Smartda0436e2009-05-22 14:51:39 -040010913 return 0;
10914}
10915
10916/**
James Smart3621a712009-04-06 18:47:14 -040010917 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010918 * @phba: Pointer to HBA context.
10919 *
James Smart3772a992009-05-22 14:50:54 -040010920 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010921 * error attention register bit for error attention events.
10922 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010923 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010924 * Register and returns 0 otherwise.
10925 **/
10926int
10927lpfc_sli_check_eratt(struct lpfc_hba *phba)
10928{
10929 uint32_t ha_copy;
10930
10931 /* If somebody is waiting to handle an eratt, don't process it
10932 * here. The brdkill function will do this.
10933 */
10934 if (phba->link_flag & LS_IGNORE_ERATT)
10935 return 0;
10936
10937 /* Check if interrupt handler handles this ERATT */
10938 spin_lock_irq(&phba->hbalock);
10939 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10940 /* Interrupt handler has handled ERATT */
10941 spin_unlock_irq(&phba->hbalock);
10942 return 0;
10943 }
10944
James Smarta257bf92009-04-06 18:48:10 -040010945 /*
10946 * If there is deferred error attention, do not check for error
10947 * attention
10948 */
10949 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10950 spin_unlock_irq(&phba->hbalock);
10951 return 0;
10952 }
10953
James Smart3772a992009-05-22 14:50:54 -040010954 /* If PCI channel is offline, don't process it */
10955 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010956 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010957 return 0;
10958 }
10959
10960 switch (phba->sli_rev) {
10961 case LPFC_SLI_REV2:
10962 case LPFC_SLI_REV3:
10963 /* Read chip Host Attention (HA) register */
10964 ha_copy = lpfc_sli_eratt_read(phba);
10965 break;
James Smartda0436e2009-05-22 14:51:39 -040010966 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010967 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010968 ha_copy = lpfc_sli4_eratt_read(phba);
10969 break;
James Smart3772a992009-05-22 14:50:54 -040010970 default:
10971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10972 "0299 Invalid SLI revision (%d)\n",
10973 phba->sli_rev);
10974 ha_copy = 0;
10975 break;
James Smart93996272008-08-24 21:50:30 -040010976 }
10977 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010978
10979 return ha_copy;
10980}
10981
10982/**
10983 * lpfc_intr_state_check - Check device state for interrupt handling
10984 * @phba: Pointer to HBA context.
10985 *
10986 * This inline routine checks whether a device or its PCI slot is in a state
10987 * that the interrupt should be handled.
10988 *
10989 * This function returns 0 if the device or the PCI slot is in a state that
10990 * interrupt should be handled, otherwise -EIO.
10991 */
10992static inline int
10993lpfc_intr_state_check(struct lpfc_hba *phba)
10994{
10995 /* If the pci channel is offline, ignore all the interrupts */
10996 if (unlikely(pci_channel_offline(phba->pcidev)))
10997 return -EIO;
10998
10999 /* Update device level interrupt statistics */
11000 phba->sli.slistat.sli_intr++;
11001
11002 /* Ignore all interrupts during initialization. */
11003 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11004 return -EIO;
11005
James Smart93996272008-08-24 21:50:30 -040011006 return 0;
11007}
11008
11009/**
James Smart3772a992009-05-22 14:50:54 -040011010 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011011 * @irq: Interrupt number.
11012 * @dev_id: The device context pointer.
11013 *
James Smart93996272008-08-24 21:50:30 -040011014 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011015 * service routine when device with SLI-3 interface spec is enabled with
11016 * MSI-X multi-message interrupt mode and there are slow-path events in
11017 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11018 * interrupt mode, this function is called as part of the device-level
11019 * interrupt handler. When the PCI slot is in error recovery or the HBA
11020 * is undergoing initialization, the interrupt handler will not process
11021 * the interrupt. The link attention and ELS ring attention events are
11022 * handled by the worker thread. The interrupt handler signals the worker
11023 * thread and returns for these events. This function is called without
11024 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011025 * structures.
11026 *
11027 * This function returns IRQ_HANDLED when interrupt is handled else it
11028 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011029 **/
dea31012005-04-17 16:05:31 -050011030irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011031lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011032{
James Smart2e0fef82007-06-17 19:56:36 -050011033 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011034 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011035 uint32_t work_ha_copy;
11036 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011037 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011038 uint32_t control;
11039
James Smart92d7f7b2007-06-17 19:56:38 -050011040 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011041 struct lpfc_vport *vport;
11042 struct lpfc_nodelist *ndlp;
11043 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011044 LPFC_MBOXQ_t *pmb;
11045 int rc;
11046
dea31012005-04-17 16:05:31 -050011047 /*
11048 * Get the driver's phba structure from the dev_id and
11049 * assume the HBA is not interrupting.
11050 */
James Smart93996272008-08-24 21:50:30 -040011051 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011052
11053 if (unlikely(!phba))
11054 return IRQ_NONE;
11055
dea31012005-04-17 16:05:31 -050011056 /*
James Smart93996272008-08-24 21:50:30 -040011057 * Stuff needs to be attented to when this function is invoked as an
11058 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011059 */
James Smart93996272008-08-24 21:50:30 -040011060 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011061 /* Check device state for handling interrupt */
11062 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011063 return IRQ_NONE;
11064 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011065 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011066 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11067 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011068 /* If somebody is waiting to handle an eratt don't process it
11069 * here. The brdkill function will do this.
11070 */
11071 if (phba->link_flag & LS_IGNORE_ERATT)
11072 ha_copy &= ~HA_ERATT;
11073 /* Check the need for handling ERATT in interrupt handler */
11074 if (ha_copy & HA_ERATT) {
11075 if (phba->hba_flag & HBA_ERATT_HANDLED)
11076 /* ERATT polling has handled ERATT */
11077 ha_copy &= ~HA_ERATT;
11078 else
11079 /* Indicate interrupt handler handles ERATT */
11080 phba->hba_flag |= HBA_ERATT_HANDLED;
11081 }
James Smarta257bf92009-04-06 18:48:10 -040011082
11083 /*
11084 * If there is deferred error attention, do not check for any
11085 * interrupt.
11086 */
11087 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011088 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011089 return IRQ_NONE;
11090 }
11091
James Smart93996272008-08-24 21:50:30 -040011092 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011093 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11094 goto unplug_error;
11095
James Smarta747c9c2009-11-18 15:41:10 -050011096 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11097 HC_LAINT_ENA | HC_ERINT_ENA),
11098 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011099 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11100 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011101 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011102 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011103 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011104 } else
11105 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011106
dea31012005-04-17 16:05:31 -050011107 work_ha_copy = ha_copy & phba->work_ha_mask;
11108
James Smart93996272008-08-24 21:50:30 -040011109 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011110 if (work_ha_copy & HA_LATT) {
11111 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11112 /*
11113 * Turn off Link Attention interrupts
11114 * until CLEAR_LA done
11115 */
James Smart5b75da22008-12-04 22:39:35 -050011116 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011117 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011118 if (lpfc_readl(phba->HCregaddr, &control))
11119 goto unplug_error;
dea31012005-04-17 16:05:31 -050011120 control &= ~HC_LAINT_ENA;
11121 writel(control, phba->HCregaddr);
11122 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011123 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011124 }
11125 else
11126 work_ha_copy &= ~HA_LATT;
11127 }
11128
James Smart93996272008-08-24 21:50:30 -040011129 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011130 /*
11131 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11132 * the only slow ring.
11133 */
11134 status = (work_ha_copy &
11135 (HA_RXMASK << (4*LPFC_ELS_RING)));
11136 status >>= (4*LPFC_ELS_RING);
11137 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011138 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011139 if (lpfc_readl(phba->HCregaddr, &control))
11140 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011141
11142 lpfc_debugfs_slow_ring_trc(phba,
11143 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11144 control, status,
11145 (uint32_t)phba->sli.slistat.sli_intr);
11146
James Smart858c9f62007-06-17 19:56:39 -050011147 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011148 lpfc_debugfs_slow_ring_trc(phba,
11149 "ISR Disable ring:"
11150 "pwork:x%x hawork:x%x wait:x%x",
11151 phba->work_ha, work_ha_copy,
11152 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011153 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011154
James Smart858c9f62007-06-17 19:56:39 -050011155 control &=
11156 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011157 writel(control, phba->HCregaddr);
11158 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011159 }
James Smarta58cbd52007-08-02 11:09:43 -040011160 else {
11161 lpfc_debugfs_slow_ring_trc(phba,
11162 "ISR slow ring: pwork:"
11163 "x%x hawork:x%x wait:x%x",
11164 phba->work_ha, work_ha_copy,
11165 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011166 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011167 }
James Smart5b75da22008-12-04 22:39:35 -050011168 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011169 }
11170 }
James Smart5b75da22008-12-04 22:39:35 -050011171 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011172 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011173 if (lpfc_sli_read_hs(phba))
11174 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011175 /*
11176 * Check if there is a deferred error condition
11177 * is active
11178 */
11179 if ((HS_FFER1 & phba->work_hs) &&
11180 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011181 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11182 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011183 phba->hba_flag |= DEFER_ERATT;
11184 /* Clear all interrupt enable conditions */
11185 writel(0, phba->HCregaddr);
11186 readl(phba->HCregaddr);
11187 }
11188 }
11189
James Smart93996272008-08-24 21:50:30 -040011190 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011191 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011192 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011193 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011194 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011195
11196 /* First check out the status word */
11197 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11198 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011199 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011200 /*
11201 * Stray Mailbox Interrupt, mbxCommand <cmd>
11202 * mbxStatus <status>
11203 */
James Smart09372822008-01-11 01:52:54 -050011204 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011205 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011206 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011207 "Interrupt mbxCommand x%x "
11208 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011209 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011210 pmbox->mbxCommand,
11211 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011212 /* clear mailbox attention bit */
11213 work_ha_copy &= ~HA_MBATT;
11214 } else {
James Smart97eab632008-04-07 10:16:05 -040011215 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011216 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011217 phba->last_completion_time = jiffies;
11218 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011219 if (pmb->mbox_cmpl) {
11220 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11221 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011222 if (pmb->out_ext_byte_len &&
11223 pmb->context2)
11224 lpfc_sli_pcimem_bcopy(
11225 phba->mbox_ext,
11226 pmb->context2,
11227 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011228 }
James Smart09372822008-01-11 01:52:54 -050011229 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11230 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11231
11232 lpfc_debugfs_disc_trc(vport,
11233 LPFC_DISC_TRC_MBOX_VPORT,
11234 "MBOX dflt rpi: : "
11235 "status:x%x rpi:x%x",
11236 (uint32_t)pmbox->mbxStatus,
11237 pmbox->un.varWords[0], 0);
11238
11239 if (!pmbox->mbxStatus) {
11240 mp = (struct lpfc_dmabuf *)
11241 (pmb->context1);
11242 ndlp = (struct lpfc_nodelist *)
11243 pmb->context2;
11244
11245 /* Reg_LOGIN of dflt RPI was
11246 * successful. new lets get
11247 * rid of the RPI using the
11248 * same mbox buffer.
11249 */
11250 lpfc_unreg_login(phba,
11251 vport->vpi,
11252 pmbox->un.varWords[0],
11253 pmb);
11254 pmb->mbox_cmpl =
11255 lpfc_mbx_cmpl_dflt_rpi;
11256 pmb->context1 = mp;
11257 pmb->context2 = ndlp;
11258 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011259 rc = lpfc_sli_issue_mbox(phba,
11260 pmb,
11261 MBX_NOWAIT);
11262 if (rc != MBX_BUSY)
11263 lpfc_printf_log(phba,
11264 KERN_ERR,
11265 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011266 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011267 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011268 if (rc != MBX_NOT_FINISHED)
11269 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011270 }
11271 }
James Smart5b75da22008-12-04 22:39:35 -050011272 spin_lock_irqsave(
11273 &phba->pport->work_port_lock,
11274 iflag);
James Smart09372822008-01-11 01:52:54 -050011275 phba->pport->work_port_events &=
11276 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011277 spin_unlock_irqrestore(
11278 &phba->pport->work_port_lock,
11279 iflag);
James Smart09372822008-01-11 01:52:54 -050011280 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011281 }
James Smart97eab632008-04-07 10:16:05 -040011282 } else
James Smart5b75da22008-12-04 22:39:35 -050011283 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011284
James Smart92d7f7b2007-06-17 19:56:38 -050011285 if ((work_ha_copy & HA_MBATT) &&
11286 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011287send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011288 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011289 do {
11290 rc = lpfc_sli_issue_mbox(phba, NULL,
11291 MBX_NOWAIT);
11292 } while (rc == MBX_NOT_FINISHED);
11293 if (rc != MBX_SUCCESS)
11294 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11295 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011296 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011297 }
11298
James Smart5b75da22008-12-04 22:39:35 -050011299 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011300 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011301 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011302 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011303 }
James Smart93996272008-08-24 21:50:30 -040011304 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011305unplug_error:
11306 spin_unlock_irqrestore(&phba->hbalock, iflag);
11307 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011308
James Smart3772a992009-05-22 14:50:54 -040011309} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011310
11311/**
James Smart3772a992009-05-22 14:50:54 -040011312 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011313 * @irq: Interrupt number.
11314 * @dev_id: The device context pointer.
11315 *
11316 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011317 * service routine when device with SLI-3 interface spec is enabled with
11318 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11319 * ring event in the HBA. However, when the device is enabled with either
11320 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11321 * device-level interrupt handler. When the PCI slot is in error recovery
11322 * or the HBA is undergoing initialization, the interrupt handler will not
11323 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11324 * the intrrupt context. This function is called without any lock held.
11325 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011326 *
11327 * This function returns IRQ_HANDLED when interrupt is handled else it
11328 * returns IRQ_NONE.
11329 **/
11330irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011331lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011332{
11333 struct lpfc_hba *phba;
11334 uint32_t ha_copy;
11335 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011336 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011337
11338 /* Get the driver's phba structure from the dev_id and
11339 * assume the HBA is not interrupting.
11340 */
11341 phba = (struct lpfc_hba *) dev_id;
11342
11343 if (unlikely(!phba))
11344 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011345
11346 /*
James Smart93996272008-08-24 21:50:30 -040011347 * Stuff needs to be attented to when this function is invoked as an
11348 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011349 */
James Smart93996272008-08-24 21:50:30 -040011350 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011351 /* Check device state for handling interrupt */
11352 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011353 return IRQ_NONE;
11354 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011355 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11356 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011357 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011358 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011359 /*
11360 * If there is deferred error attention, do not check for
11361 * any interrupt.
11362 */
11363 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011364 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011365 return IRQ_NONE;
11366 }
James Smart93996272008-08-24 21:50:30 -040011367 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11368 phba->HAregaddr);
11369 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011370 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011371 } else
11372 ha_copy = phba->ha_copy;
11373
11374 /*
11375 * Process all events on FCP ring. Take the optimized path for FCP IO.
11376 */
11377 ha_copy &= ~(phba->work_ha_mask);
11378
11379 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011380 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011381 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011382 lpfc_sli_handle_fast_ring_event(phba,
11383 &phba->sli.ring[LPFC_FCP_RING],
11384 status);
James Smarta4bc3372006-12-02 13:34:16 -050011385
11386 if (phba->cfg_multi_ring_support == 2) {
11387 /*
James Smart93996272008-08-24 21:50:30 -040011388 * Process all events on extra ring. Take the optimized path
11389 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011390 */
James Smart93996272008-08-24 21:50:30 -040011391 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011392 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011393 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011394 lpfc_sli_handle_fast_ring_event(phba,
11395 &phba->sli.ring[LPFC_EXTRA_RING],
11396 status);
11397 }
11398 }
dea31012005-04-17 16:05:31 -050011399 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011400} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011401
James Smart93996272008-08-24 21:50:30 -040011402/**
James Smart3772a992009-05-22 14:50:54 -040011403 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011404 * @irq: Interrupt number.
11405 * @dev_id: The device context pointer.
11406 *
James Smart3772a992009-05-22 14:50:54 -040011407 * This function is the HBA device-level interrupt handler to device with
11408 * SLI-3 interface spec, called from the PCI layer when either MSI or
11409 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11410 * requires driver attention. This function invokes the slow-path interrupt
11411 * attention handling function and fast-path interrupt attention handling
11412 * function in turn to process the relevant HBA attention events. This
11413 * function is called without any lock held. It gets the hbalock to access
11414 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011415 *
11416 * This function returns IRQ_HANDLED when interrupt is handled, else it
11417 * returns IRQ_NONE.
11418 **/
11419irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011420lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011421{
11422 struct lpfc_hba *phba;
11423 irqreturn_t sp_irq_rc, fp_irq_rc;
11424 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011425 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011426
11427 /*
11428 * Get the driver's phba structure from the dev_id and
11429 * assume the HBA is not interrupting.
11430 */
11431 phba = (struct lpfc_hba *) dev_id;
11432
11433 if (unlikely(!phba))
11434 return IRQ_NONE;
11435
James Smart3772a992009-05-22 14:50:54 -040011436 /* Check device state for handling interrupt */
11437 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011438 return IRQ_NONE;
11439
11440 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011441 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11442 spin_unlock(&phba->hbalock);
11443 return IRQ_HANDLED;
11444 }
11445
James Smart93996272008-08-24 21:50:30 -040011446 if (unlikely(!phba->ha_copy)) {
11447 spin_unlock(&phba->hbalock);
11448 return IRQ_NONE;
11449 } else if (phba->ha_copy & HA_ERATT) {
11450 if (phba->hba_flag & HBA_ERATT_HANDLED)
11451 /* ERATT polling has handled ERATT */
11452 phba->ha_copy &= ~HA_ERATT;
11453 else
11454 /* Indicate interrupt handler handles ERATT */
11455 phba->hba_flag |= HBA_ERATT_HANDLED;
11456 }
11457
James Smarta257bf92009-04-06 18:48:10 -040011458 /*
11459 * If there is deferred error attention, do not check for any interrupt.
11460 */
11461 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011462 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011463 return IRQ_NONE;
11464 }
11465
James Smart93996272008-08-24 21:50:30 -040011466 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011467 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11468 spin_unlock(&phba->hbalock);
11469 return IRQ_HANDLED;
11470 }
James Smarta747c9c2009-11-18 15:41:10 -050011471 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11472 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11473 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011474 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011475 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011476 readl(phba->HAregaddr); /* flush */
11477 spin_unlock(&phba->hbalock);
11478
11479 /*
11480 * Invokes slow-path host attention interrupt handling as appropriate.
11481 */
11482
11483 /* status of events with mailbox and link attention */
11484 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11485
11486 /* status of events with ELS ring */
11487 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11488 status2 >>= (4*LPFC_ELS_RING);
11489
11490 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011491 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011492 else
11493 sp_irq_rc = IRQ_NONE;
11494
11495 /*
11496 * Invoke fast-path host attention interrupt handling as appropriate.
11497 */
11498
11499 /* status of events with FCP ring */
11500 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11501 status1 >>= (4*LPFC_FCP_RING);
11502
11503 /* status of events with extra ring */
11504 if (phba->cfg_multi_ring_support == 2) {
11505 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11506 status2 >>= (4*LPFC_EXTRA_RING);
11507 } else
11508 status2 = 0;
11509
11510 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011511 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011512 else
11513 fp_irq_rc = IRQ_NONE;
11514
11515 /* Return device-level interrupt handling status */
11516 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011517} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011518
11519/**
11520 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11521 * @phba: pointer to lpfc hba data structure.
11522 *
11523 * This routine is invoked by the worker thread to process all the pending
11524 * SLI4 FCP abort XRI events.
11525 **/
11526void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11527{
11528 struct lpfc_cq_event *cq_event;
11529
11530 /* First, declare the fcp xri abort event has been handled */
11531 spin_lock_irq(&phba->hbalock);
11532 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11533 spin_unlock_irq(&phba->hbalock);
11534 /* Now, handle all the fcp xri abort events */
11535 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11536 /* Get the first event from the head of the event queue */
11537 spin_lock_irq(&phba->hbalock);
11538 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11539 cq_event, struct lpfc_cq_event, list);
11540 spin_unlock_irq(&phba->hbalock);
11541 /* Notify aborted XRI for FCP work queue */
11542 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11543 /* Free the event processed back to the free pool */
11544 lpfc_sli4_cq_event_release(phba, cq_event);
11545 }
11546}
11547
11548/**
11549 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11550 * @phba: pointer to lpfc hba data structure.
11551 *
11552 * This routine is invoked by the worker thread to process all the pending
11553 * SLI4 els abort xri events.
11554 **/
11555void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11556{
11557 struct lpfc_cq_event *cq_event;
11558
11559 /* First, declare the els xri abort event has been handled */
11560 spin_lock_irq(&phba->hbalock);
11561 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11562 spin_unlock_irq(&phba->hbalock);
11563 /* Now, handle all the els xri abort events */
11564 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11565 /* Get the first event from the head of the event queue */
11566 spin_lock_irq(&phba->hbalock);
11567 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11568 cq_event, struct lpfc_cq_event, list);
11569 spin_unlock_irq(&phba->hbalock);
11570 /* Notify aborted XRI for ELS work queue */
11571 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11572 /* Free the event processed back to the free pool */
11573 lpfc_sli4_cq_event_release(phba, cq_event);
11574 }
11575}
11576
James Smart341af102010-01-26 23:07:37 -050011577/**
11578 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11579 * @phba: pointer to lpfc hba data structure
11580 * @pIocbIn: pointer to the rspiocbq
11581 * @pIocbOut: pointer to the cmdiocbq
11582 * @wcqe: pointer to the complete wcqe
11583 *
11584 * This routine transfers the fields of a command iocbq to a response iocbq
11585 * by copying all the IOCB fields from command iocbq and transferring the
11586 * completion status information from the complete wcqe.
11587 **/
James Smart4f774512009-05-22 14:52:35 -040011588static void
James Smart341af102010-01-26 23:07:37 -050011589lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11590 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011591 struct lpfc_iocbq *pIocbOut,
11592 struct lpfc_wcqe_complete *wcqe)
11593{
James Smartaf227412013-10-10 12:23:10 -040011594 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011595 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011596 uint32_t status, max_response;
11597 struct lpfc_dmabuf *dmabuf;
11598 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011599 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11600
11601 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11602 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011603 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011604 status = bf_get(lpfc_wcqe_c_status, wcqe);
11605 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011606 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11607 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11608 pIocbIn->iocb.un.fcpi.fcpi_parm =
11609 pIocbOut->iocb.un.fcpi.fcpi_parm -
11610 wcqe->total_data_placed;
11611 else
11612 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011613 else {
James Smart4f774512009-05-22 14:52:35 -040011614 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011615 switch (pIocbOut->iocb.ulpCommand) {
11616 case CMD_ELS_REQUEST64_CR:
11617 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11618 bpl = (struct ulp_bde64 *)dmabuf->virt;
11619 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11620 max_response = bde.tus.f.bdeSize;
11621 break;
11622 case CMD_GEN_REQUEST64_CR:
11623 max_response = 0;
11624 if (!pIocbOut->context3)
11625 break;
11626 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11627 sizeof(struct ulp_bde64);
11628 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11629 bpl = (struct ulp_bde64 *)dmabuf->virt;
11630 for (i = 0; i < numBdes; i++) {
11631 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11632 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11633 max_response += bde.tus.f.bdeSize;
11634 }
11635 break;
11636 default:
11637 max_response = wcqe->total_data_placed;
11638 break;
11639 }
11640 if (max_response < wcqe->total_data_placed)
11641 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11642 else
11643 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11644 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011645 }
James Smart341af102010-01-26 23:07:37 -050011646
James Smartacd68592012-01-18 16:25:09 -050011647 /* Convert BG errors for completion status */
11648 if (status == CQE_STATUS_DI_ERROR) {
11649 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11650
11651 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11652 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11653 else
11654 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11655
11656 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11657 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11658 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11659 BGS_GUARD_ERR_MASK;
11660 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11661 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11662 BGS_APPTAG_ERR_MASK;
11663 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11664 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11665 BGS_REFTAG_ERR_MASK;
11666
11667 /* Check to see if there was any good data before the error */
11668 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11669 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11670 BGS_HI_WATER_MARK_PRESENT_MASK;
11671 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11672 wcqe->total_data_placed;
11673 }
11674
11675 /*
11676 * Set ALL the error bits to indicate we don't know what
11677 * type of error it is.
11678 */
11679 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11680 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11681 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11682 BGS_GUARD_ERR_MASK);
11683 }
11684
James Smart341af102010-01-26 23:07:37 -050011685 /* Pick up HBA exchange busy condition */
11686 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11687 spin_lock_irqsave(&phba->hbalock, iflags);
11688 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11689 spin_unlock_irqrestore(&phba->hbalock, iflags);
11690 }
James Smart4f774512009-05-22 14:52:35 -040011691}
11692
11693/**
James Smart45ed1192009-10-02 15:17:02 -040011694 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11695 * @phba: Pointer to HBA context object.
11696 * @wcqe: Pointer to work-queue completion queue entry.
11697 *
11698 * This routine handles an ELS work-queue completion event and construct
11699 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11700 * discovery engine to handle.
11701 *
11702 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11703 **/
11704static struct lpfc_iocbq *
11705lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11706 struct lpfc_iocbq *irspiocbq)
11707{
11708 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11709 struct lpfc_iocbq *cmdiocbq;
11710 struct lpfc_wcqe_complete *wcqe;
11711 unsigned long iflags;
11712
11713 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011714 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011715 pring->stats.iocb_event++;
11716 /* Look up the ELS command IOCB and create pseudo response IOCB */
11717 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11718 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011719 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011720
11721 if (unlikely(!cmdiocbq)) {
11722 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11723 "0386 ELS complete with no corresponding "
11724 "cmdiocb: iotag (%d)\n",
11725 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11726 lpfc_sli_release_iocbq(phba, irspiocbq);
11727 return NULL;
11728 }
11729
11730 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011731 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011732
11733 return irspiocbq;
11734}
11735
11736/**
James Smart04c68492009-05-22 14:52:52 -040011737 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11738 * @phba: Pointer to HBA context object.
11739 * @cqe: Pointer to mailbox completion queue entry.
11740 *
11741 * This routine process a mailbox completion queue entry with asynchrous
11742 * event.
11743 *
11744 * Return: true if work posted to worker thread, otherwise false.
11745 **/
11746static bool
11747lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11748{
11749 struct lpfc_cq_event *cq_event;
11750 unsigned long iflags;
11751
11752 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11753 "0392 Async Event: word0:x%x, word1:x%x, "
11754 "word2:x%x, word3:x%x\n", mcqe->word0,
11755 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11756
11757 /* Allocate a new internal CQ_EVENT entry */
11758 cq_event = lpfc_sli4_cq_event_alloc(phba);
11759 if (!cq_event) {
11760 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11761 "0394 Failed to allocate CQ_EVENT entry\n");
11762 return false;
11763 }
11764
11765 /* Move the CQE into an asynchronous event entry */
11766 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11767 spin_lock_irqsave(&phba->hbalock, iflags);
11768 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11769 /* Set the async event flag */
11770 phba->hba_flag |= ASYNC_EVENT;
11771 spin_unlock_irqrestore(&phba->hbalock, iflags);
11772
11773 return true;
11774}
11775
11776/**
11777 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11778 * @phba: Pointer to HBA context object.
11779 * @cqe: Pointer to mailbox completion queue entry.
11780 *
11781 * This routine process a mailbox completion queue entry with mailbox
11782 * completion event.
11783 *
11784 * Return: true if work posted to worker thread, otherwise false.
11785 **/
11786static bool
11787lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11788{
11789 uint32_t mcqe_status;
11790 MAILBOX_t *mbox, *pmbox;
11791 struct lpfc_mqe *mqe;
11792 struct lpfc_vport *vport;
11793 struct lpfc_nodelist *ndlp;
11794 struct lpfc_dmabuf *mp;
11795 unsigned long iflags;
11796 LPFC_MBOXQ_t *pmb;
11797 bool workposted = false;
11798 int rc;
11799
11800 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11801 if (!bf_get(lpfc_trailer_completed, mcqe))
11802 goto out_no_mqe_complete;
11803
11804 /* Get the reference to the active mbox command */
11805 spin_lock_irqsave(&phba->hbalock, iflags);
11806 pmb = phba->sli.mbox_active;
11807 if (unlikely(!pmb)) {
11808 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11809 "1832 No pending MBOX command to handle\n");
11810 spin_unlock_irqrestore(&phba->hbalock, iflags);
11811 goto out_no_mqe_complete;
11812 }
11813 spin_unlock_irqrestore(&phba->hbalock, iflags);
11814 mqe = &pmb->u.mqe;
11815 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11816 mbox = phba->mbox;
11817 vport = pmb->vport;
11818
11819 /* Reset heartbeat timer */
11820 phba->last_completion_time = jiffies;
11821 del_timer(&phba->sli.mbox_tmo);
11822
11823 /* Move mbox data to caller's mailbox region, do endian swapping */
11824 if (pmb->mbox_cmpl && mbox)
11825 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011826
James Smart73d91e52011-10-10 21:32:10 -040011827 /*
11828 * For mcqe errors, conditionally move a modified error code to
11829 * the mbox so that the error will not be missed.
11830 */
11831 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11832 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11833 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11834 bf_set(lpfc_mqe_status, mqe,
11835 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11836 }
James Smart04c68492009-05-22 14:52:52 -040011837 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11838 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11839 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11840 "MBOX dflt rpi: status:x%x rpi:x%x",
11841 mcqe_status,
11842 pmbox->un.varWords[0], 0);
11843 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11844 mp = (struct lpfc_dmabuf *)(pmb->context1);
11845 ndlp = (struct lpfc_nodelist *)pmb->context2;
11846 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11847 * RID of the PPI using the same mbox buffer.
11848 */
11849 lpfc_unreg_login(phba, vport->vpi,
11850 pmbox->un.varWords[0], pmb);
11851 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11852 pmb->context1 = mp;
11853 pmb->context2 = ndlp;
11854 pmb->vport = vport;
11855 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11856 if (rc != MBX_BUSY)
11857 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11858 LOG_SLI, "0385 rc should "
11859 "have been MBX_BUSY\n");
11860 if (rc != MBX_NOT_FINISHED)
11861 goto send_current_mbox;
11862 }
11863 }
11864 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11865 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11866 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11867
11868 /* There is mailbox completion work to do */
11869 spin_lock_irqsave(&phba->hbalock, iflags);
11870 __lpfc_mbox_cmpl_put(phba, pmb);
11871 phba->work_ha |= HA_MBATT;
11872 spin_unlock_irqrestore(&phba->hbalock, iflags);
11873 workposted = true;
11874
11875send_current_mbox:
11876 spin_lock_irqsave(&phba->hbalock, iflags);
11877 /* Release the mailbox command posting token */
11878 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11879 /* Setting active mailbox pointer need to be in sync to flag clear */
11880 phba->sli.mbox_active = NULL;
11881 spin_unlock_irqrestore(&phba->hbalock, iflags);
11882 /* Wake up worker thread to post the next pending mailbox command */
11883 lpfc_worker_wake_up(phba);
11884out_no_mqe_complete:
11885 if (bf_get(lpfc_trailer_consumed, mcqe))
11886 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11887 return workposted;
11888}
11889
11890/**
11891 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11892 * @phba: Pointer to HBA context object.
11893 * @cqe: Pointer to mailbox completion queue entry.
11894 *
11895 * This routine process a mailbox completion queue entry, it invokes the
11896 * proper mailbox complete handling or asynchrous event handling routine
11897 * according to the MCQE's async bit.
11898 *
11899 * Return: true if work posted to worker thread, otherwise false.
11900 **/
11901static bool
11902lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11903{
11904 struct lpfc_mcqe mcqe;
11905 bool workposted;
11906
11907 /* Copy the mailbox MCQE and convert endian order as needed */
11908 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11909
11910 /* Invoke the proper event handling routine */
11911 if (!bf_get(lpfc_trailer_async, &mcqe))
11912 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11913 else
11914 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11915 return workposted;
11916}
11917
11918/**
James Smart4f774512009-05-22 14:52:35 -040011919 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11920 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011921 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011922 * @wcqe: Pointer to work-queue completion queue entry.
11923 *
11924 * This routine handles an ELS work-queue completion event.
11925 *
11926 * Return: true if work posted to worker thread, otherwise false.
11927 **/
11928static bool
James Smart2a76a282012-08-03 12:35:54 -040011929lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011930 struct lpfc_wcqe_complete *wcqe)
11931{
James Smart4f774512009-05-22 14:52:35 -040011932 struct lpfc_iocbq *irspiocbq;
11933 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011934 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011935 int txq_cnt = 0;
11936 int txcmplq_cnt = 0;
11937 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040011938
James Smart45ed1192009-10-02 15:17:02 -040011939 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011940 irspiocbq = lpfc_sli_get_iocbq(phba);
11941 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011942 if (!list_empty(&pring->txq))
11943 txq_cnt++;
11944 if (!list_empty(&pring->txcmplq))
11945 txcmplq_cnt++;
11946 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11947 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040011948 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011949 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11950 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050011951 txq_cnt, phba->iocb_cnt,
11952 fcp_txcmplq_cnt,
11953 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011954 return false;
James Smart4f774512009-05-22 14:52:35 -040011955 }
James Smart4f774512009-05-22 14:52:35 -040011956
James Smart45ed1192009-10-02 15:17:02 -040011957 /* Save off the slow-path queue event for work thread to process */
11958 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011959 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011960 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011961 &phba->sli4_hba.sp_queue_event);
11962 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011963 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011964
James Smart45ed1192009-10-02 15:17:02 -040011965 return true;
James Smart4f774512009-05-22 14:52:35 -040011966}
11967
11968/**
11969 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11970 * @phba: Pointer to HBA context object.
11971 * @wcqe: Pointer to work-queue completion queue entry.
11972 *
11973 * This routine handles slow-path WQ entry comsumed event by invoking the
11974 * proper WQ release routine to the slow-path WQ.
11975 **/
11976static void
11977lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11978 struct lpfc_wcqe_release *wcqe)
11979{
James Smart2e90f4b2011-12-13 13:22:37 -050011980 /* sanity check on queue memory */
11981 if (unlikely(!phba->sli4_hba.els_wq))
11982 return;
James Smart4f774512009-05-22 14:52:35 -040011983 /* Check for the slow-path ELS work queue */
11984 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11985 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11986 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11987 else
11988 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11989 "2579 Slow-path wqe consume event carries "
11990 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11991 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11992 phba->sli4_hba.els_wq->queue_id);
11993}
11994
11995/**
11996 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11997 * @phba: Pointer to HBA context object.
11998 * @cq: Pointer to a WQ completion queue.
11999 * @wcqe: Pointer to work-queue completion queue entry.
12000 *
12001 * This routine handles an XRI abort event.
12002 *
12003 * Return: true if work posted to worker thread, otherwise false.
12004 **/
12005static bool
12006lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12007 struct lpfc_queue *cq,
12008 struct sli4_wcqe_xri_aborted *wcqe)
12009{
12010 bool workposted = false;
12011 struct lpfc_cq_event *cq_event;
12012 unsigned long iflags;
12013
12014 /* Allocate a new internal CQ_EVENT entry */
12015 cq_event = lpfc_sli4_cq_event_alloc(phba);
12016 if (!cq_event) {
12017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12018 "0602 Failed to allocate CQ_EVENT entry\n");
12019 return false;
12020 }
12021
12022 /* Move the CQE into the proper xri abort event list */
12023 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12024 switch (cq->subtype) {
12025 case LPFC_FCP:
12026 spin_lock_irqsave(&phba->hbalock, iflags);
12027 list_add_tail(&cq_event->list,
12028 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12029 /* Set the fcp xri abort event flag */
12030 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12031 spin_unlock_irqrestore(&phba->hbalock, iflags);
12032 workposted = true;
12033 break;
12034 case LPFC_ELS:
12035 spin_lock_irqsave(&phba->hbalock, iflags);
12036 list_add_tail(&cq_event->list,
12037 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12038 /* Set the els xri abort event flag */
12039 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12040 spin_unlock_irqrestore(&phba->hbalock, iflags);
12041 workposted = true;
12042 break;
12043 default:
12044 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12045 "0603 Invalid work queue CQE subtype (x%x)\n",
12046 cq->subtype);
12047 workposted = false;
12048 break;
12049 }
12050 return workposted;
12051}
12052
12053/**
James Smart4d9ab992009-10-02 15:16:39 -040012054 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012055 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012056 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012057 *
James Smart4d9ab992009-10-02 15:16:39 -040012058 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012059 *
12060 * Return: true if work posted to worker thread, otherwise false.
12061 **/
12062static bool
James Smart4d9ab992009-10-02 15:16:39 -040012063lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12064{
12065 bool workposted = false;
12066 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12067 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12068 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012069 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012070 unsigned long iflags;
12071
James Smart2e90f4b2011-12-13 13:22:37 -050012072 /* sanity check on queue memory */
12073 if (unlikely(!hrq) || unlikely(!drq))
12074 return workposted;
12075
James Smart7851fe22011-07-22 18:36:52 -040012076 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12077 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12078 else
12079 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12080 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012081 goto out;
12082
12083 status = bf_get(lpfc_rcqe_status, rcqe);
12084 switch (status) {
12085 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12087 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012088 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012089 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012090 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012091 spin_lock_irqsave(&phba->hbalock, iflags);
12092 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12093 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012094 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012095 spin_unlock_irqrestore(&phba->hbalock, iflags);
12096 goto out;
12097 }
James Smartb84daac2012-08-03 12:35:13 -040012098 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012099 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12100 /* save off the frame for the word thread to process */
12101 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012102 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012103 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012104 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012105 spin_unlock_irqrestore(&phba->hbalock, iflags);
12106 workposted = true;
12107 break;
12108 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12109 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012110 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012111 /* Post more buffers if possible */
12112 spin_lock_irqsave(&phba->hbalock, iflags);
12113 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12114 spin_unlock_irqrestore(&phba->hbalock, iflags);
12115 workposted = true;
12116 break;
12117 }
12118out:
12119 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012120}
12121
12122/**
12123 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12124 * @phba: Pointer to HBA context object.
12125 * @cq: Pointer to the completion queue.
12126 * @wcqe: Pointer to a completion queue entry.
12127 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012128 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012129 * entry.
12130 *
12131 * Return: true if work posted to worker thread, otherwise false.
12132 **/
12133static bool
12134lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012135 struct lpfc_cqe *cqe)
12136{
James Smart45ed1192009-10-02 15:17:02 -040012137 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012138 bool workposted = false;
12139
12140 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012141 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012142
12143 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012144 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012145 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012146 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012147 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012148 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012149 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012150 break;
12151 case CQE_CODE_RELEASE_WQE:
12152 /* Process the WQ release event */
12153 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012154 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012155 break;
12156 case CQE_CODE_XRI_ABORTED:
12157 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012158 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012159 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012160 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012161 break;
James Smart4d9ab992009-10-02 15:16:39 -040012162 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012163 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012164 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012165 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012166 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012167 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012168 break;
James Smart4f774512009-05-22 14:52:35 -040012169 default:
12170 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12171 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012172 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012173 break;
12174 }
12175 return workposted;
12176}
12177
12178/**
James Smart4f774512009-05-22 14:52:35 -040012179 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12180 * @phba: Pointer to HBA context object.
12181 * @eqe: Pointer to fast-path event queue entry.
12182 *
12183 * This routine process a event queue entry from the slow-path event queue.
12184 * It will check the MajorCode and MinorCode to determine this is for a
12185 * completion event on a completion queue, if not, an error shall be logged
12186 * and just return. Otherwise, it will get to the corresponding completion
12187 * queue and process all the entries on that completion queue, rearm the
12188 * completion queue, and then return.
12189 *
12190 **/
12191static void
James Smart67d12732012-08-03 12:36:13 -040012192lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12193 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012194{
James Smart67d12732012-08-03 12:36:13 -040012195 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012196 struct lpfc_cqe *cqe;
12197 bool workposted = false;
12198 int ecount = 0;
12199 uint16_t cqid;
12200
James Smart4f774512009-05-22 14:52:35 -040012201 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012202 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012203
James Smart4f774512009-05-22 14:52:35 -040012204 list_for_each_entry(childq, &speq->child_list, list) {
12205 if (childq->queue_id == cqid) {
12206 cq = childq;
12207 break;
12208 }
12209 }
12210 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012211 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12212 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12213 "0365 Slow-path CQ identifier "
12214 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012215 return;
12216 }
12217
12218 /* Process all the entries to the CQ */
12219 switch (cq->type) {
12220 case LPFC_MCQ:
12221 while ((cqe = lpfc_sli4_cq_get(cq))) {
12222 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012223 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012224 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012225 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012226 }
12227 break;
12228 case LPFC_WCQ:
12229 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012230 if (cq->subtype == LPFC_FCP)
12231 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12232 cqe);
12233 else
12234 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12235 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012236 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012237 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12238 }
James Smartb84daac2012-08-03 12:35:13 -040012239
12240 /* Track the max number of CQEs processed in 1 EQ */
12241 if (ecount > cq->CQ_max_cqe)
12242 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012243 break;
12244 default:
12245 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12246 "0370 Invalid completion queue type (%d)\n",
12247 cq->type);
12248 return;
12249 }
12250
12251 /* Catch the no cq entry condition, log an error */
12252 if (unlikely(ecount == 0))
12253 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12254 "0371 No entry from the CQ: identifier "
12255 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12256
12257 /* In any case, flash and re-arm the RCQ */
12258 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12259
12260 /* wake up worker thread if there are works to be done */
12261 if (workposted)
12262 lpfc_worker_wake_up(phba);
12263}
12264
12265/**
12266 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012267 * @phba: Pointer to HBA context object.
12268 * @cq: Pointer to associated CQ
12269 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012270 *
12271 * This routine process a fast-path work queue completion entry from fast-path
12272 * event queue for FCP command response completion.
12273 **/
12274static void
James Smart2a76a282012-08-03 12:35:54 -040012275lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012276 struct lpfc_wcqe_complete *wcqe)
12277{
James Smart2a76a282012-08-03 12:35:54 -040012278 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012279 struct lpfc_iocbq *cmdiocbq;
12280 struct lpfc_iocbq irspiocbq;
12281 unsigned long iflags;
12282
James Smart4f774512009-05-22 14:52:35 -040012283 /* Check for response status */
12284 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12285 /* If resource errors reported from HBA, reduce queue
12286 * depth of the SCSI device.
12287 */
James Smarte3d2b802012-08-14 14:25:43 -040012288 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12289 IOSTAT_LOCAL_REJECT)) &&
12290 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12291 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012292 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012293
James Smart4f774512009-05-22 14:52:35 -040012294 /* Log the error status */
12295 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12296 "0373 FCP complete error: status=x%x, "
12297 "hw_status=x%x, total_data_specified=%d, "
12298 "parameter=x%x, word3=x%x\n",
12299 bf_get(lpfc_wcqe_c_status, wcqe),
12300 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12301 wcqe->total_data_placed, wcqe->parameter,
12302 wcqe->word3);
12303 }
12304
12305 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012306 spin_lock_irqsave(&pring->ring_lock, iflags);
12307 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012308 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12309 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012310 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012311 if (unlikely(!cmdiocbq)) {
12312 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12313 "0374 FCP complete with no corresponding "
12314 "cmdiocb: iotag (%d)\n",
12315 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12316 return;
12317 }
12318 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12319 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12320 "0375 FCP cmdiocb not callback function "
12321 "iotag: (%d)\n",
12322 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12323 return;
12324 }
12325
12326 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012327 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012328
James Smart0f65ff62010-02-26 14:14:23 -050012329 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12330 spin_lock_irqsave(&phba->hbalock, iflags);
12331 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12332 spin_unlock_irqrestore(&phba->hbalock, iflags);
12333 }
12334
James Smart4f774512009-05-22 14:52:35 -040012335 /* Pass the cmd_iocb and the rsp state to the upper layer */
12336 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12337}
12338
12339/**
12340 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12341 * @phba: Pointer to HBA context object.
12342 * @cq: Pointer to completion queue.
12343 * @wcqe: Pointer to work-queue completion queue entry.
12344 *
12345 * This routine handles an fast-path WQ entry comsumed event by invoking the
12346 * proper WQ release routine to the slow-path WQ.
12347 **/
12348static void
12349lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12350 struct lpfc_wcqe_release *wcqe)
12351{
12352 struct lpfc_queue *childwq;
12353 bool wqid_matched = false;
12354 uint16_t fcp_wqid;
12355
12356 /* Check for fast-path FCP work queue release */
12357 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12358 list_for_each_entry(childwq, &cq->child_list, list) {
12359 if (childwq->queue_id == fcp_wqid) {
12360 lpfc_sli4_wq_release(childwq,
12361 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12362 wqid_matched = true;
12363 break;
12364 }
12365 }
12366 /* Report warning log message if no match found */
12367 if (wqid_matched != true)
12368 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12369 "2580 Fast-path wqe consume event carries "
12370 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12371}
12372
12373/**
12374 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12375 * @cq: Pointer to the completion queue.
12376 * @eqe: Pointer to fast-path completion queue entry.
12377 *
12378 * This routine process a fast-path work queue completion entry from fast-path
12379 * event queue for FCP command response completion.
12380 **/
12381static int
12382lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12383 struct lpfc_cqe *cqe)
12384{
12385 struct lpfc_wcqe_release wcqe;
12386 bool workposted = false;
12387
12388 /* Copy the work queue CQE and convert endian order if needed */
12389 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12390
12391 /* Check and process for different type of WCQE and dispatch */
12392 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12393 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012394 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012395 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012396 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012397 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012398 (struct lpfc_wcqe_complete *)&wcqe);
12399 break;
12400 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012401 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012402 /* Process the WQ release event */
12403 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12404 (struct lpfc_wcqe_release *)&wcqe);
12405 break;
12406 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012407 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012408 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012409 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012410 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12411 (struct sli4_wcqe_xri_aborted *)&wcqe);
12412 break;
12413 default:
12414 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12415 "0144 Not a valid WCQE code: x%x\n",
12416 bf_get(lpfc_wcqe_c_code, &wcqe));
12417 break;
12418 }
12419 return workposted;
12420}
12421
12422/**
James Smart67d12732012-08-03 12:36:13 -040012423 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012424 * @phba: Pointer to HBA context object.
12425 * @eqe: Pointer to fast-path event queue entry.
12426 *
12427 * This routine process a event queue entry from the fast-path event queue.
12428 * It will check the MajorCode and MinorCode to determine this is for a
12429 * completion event on a completion queue, if not, an error shall be logged
12430 * and just return. Otherwise, it will get to the corresponding completion
12431 * queue and process all the entries on the completion queue, rearm the
12432 * completion queue, and then return.
12433 **/
12434static void
James Smart67d12732012-08-03 12:36:13 -040012435lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12436 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012437{
12438 struct lpfc_queue *cq;
12439 struct lpfc_cqe *cqe;
12440 bool workposted = false;
12441 uint16_t cqid;
12442 int ecount = 0;
12443
James Smartcb5172e2010-03-15 11:25:07 -040012444 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012445 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012446 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012447 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012448 bf_get_le32(lpfc_eqe_major_code, eqe),
12449 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012450 return;
12451 }
12452
James Smart67d12732012-08-03 12:36:13 -040012453 /* Get the reference to the corresponding CQ */
12454 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12455
12456 /* Check if this is a Slow path event */
12457 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12458 lpfc_sli4_sp_handle_eqe(phba, eqe,
12459 phba->sli4_hba.hba_eq[qidx]);
12460 return;
12461 }
12462
James Smart2e90f4b2011-12-13 13:22:37 -050012463 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12464 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12465 "3146 Fast-path completion queues "
12466 "does not exist\n");
12467 return;
12468 }
James Smart67d12732012-08-03 12:36:13 -040012469 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012470 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012471 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12472 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12473 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012474 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012475 return;
12476 }
12477
James Smart4f774512009-05-22 14:52:35 -040012478 if (unlikely(cqid != cq->queue_id)) {
12479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12480 "0368 Miss-matched fast-path completion "
12481 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12482 cqid, cq->queue_id);
12483 return;
12484 }
12485
12486 /* Process all the entries to the CQ */
12487 while ((cqe = lpfc_sli4_cq_get(cq))) {
12488 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012489 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012490 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12491 }
12492
James Smartb84daac2012-08-03 12:35:13 -040012493 /* Track the max number of CQEs processed in 1 EQ */
12494 if (ecount > cq->CQ_max_cqe)
12495 cq->CQ_max_cqe = ecount;
12496
James Smart4f774512009-05-22 14:52:35 -040012497 /* Catch the no cq entry condition */
12498 if (unlikely(ecount == 0))
12499 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12500 "0369 No entry from fast-path completion "
12501 "queue fcpcqid=%d\n", cq->queue_id);
12502
12503 /* In any case, flash and re-arm the CQ */
12504 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12505
12506 /* wake up worker thread if there are works to be done */
12507 if (workposted)
12508 lpfc_worker_wake_up(phba);
12509}
12510
12511static void
12512lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12513{
12514 struct lpfc_eqe *eqe;
12515
12516 /* walk all the EQ entries and drop on the floor */
12517 while ((eqe = lpfc_sli4_eq_get(eq)))
12518 ;
12519
12520 /* Clear and re-arm the EQ */
12521 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12522}
12523
James Smart1ba981f2014-02-20 09:56:45 -050012524
12525/**
12526 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12527 * entry
12528 * @phba: Pointer to HBA context object.
12529 * @eqe: Pointer to fast-path event queue entry.
12530 *
12531 * This routine process a event queue entry from the Flash Optimized Fabric
12532 * event queue. It will check the MajorCode and MinorCode to determine this
12533 * is for a completion event on a completion queue, if not, an error shall be
12534 * logged and just return. Otherwise, it will get to the corresponding
12535 * completion queue and process all the entries on the completion queue, rearm
12536 * the completion queue, and then return.
12537 **/
12538static void
12539lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12540{
12541 struct lpfc_queue *cq;
12542 struct lpfc_cqe *cqe;
12543 bool workposted = false;
12544 uint16_t cqid;
12545 int ecount = 0;
12546
12547 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12549 "9147 Not a valid completion "
12550 "event: majorcode=x%x, minorcode=x%x\n",
12551 bf_get_le32(lpfc_eqe_major_code, eqe),
12552 bf_get_le32(lpfc_eqe_minor_code, eqe));
12553 return;
12554 }
12555
12556 /* Get the reference to the corresponding CQ */
12557 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12558
12559 /* Next check for OAS */
12560 cq = phba->sli4_hba.oas_cq;
12561 if (unlikely(!cq)) {
12562 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12563 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12564 "9148 OAS completion queue "
12565 "does not exist\n");
12566 return;
12567 }
12568
12569 if (unlikely(cqid != cq->queue_id)) {
12570 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12571 "9149 Miss-matched fast-path compl "
12572 "queue id: eqcqid=%d, fcpcqid=%d\n",
12573 cqid, cq->queue_id);
12574 return;
12575 }
12576
12577 /* Process all the entries to the OAS CQ */
12578 while ((cqe = lpfc_sli4_cq_get(cq))) {
12579 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12580 if (!(++ecount % cq->entry_repost))
12581 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12582 }
12583
12584 /* Track the max number of CQEs processed in 1 EQ */
12585 if (ecount > cq->CQ_max_cqe)
12586 cq->CQ_max_cqe = ecount;
12587
12588 /* Catch the no cq entry condition */
12589 if (unlikely(ecount == 0))
12590 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12591 "9153 No entry from fast-path completion "
12592 "queue fcpcqid=%d\n", cq->queue_id);
12593
12594 /* In any case, flash and re-arm the CQ */
12595 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12596
12597 /* wake up worker thread if there are works to be done */
12598 if (workposted)
12599 lpfc_worker_wake_up(phba);
12600}
12601
12602/**
12603 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12604 * @irq: Interrupt number.
12605 * @dev_id: The device context pointer.
12606 *
12607 * This function is directly called from the PCI layer as an interrupt
12608 * service routine when device with SLI-4 interface spec is enabled with
12609 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12610 * IOCB ring event in the HBA. However, when the device is enabled with either
12611 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12612 * device-level interrupt handler. When the PCI slot is in error recovery
12613 * or the HBA is undergoing initialization, the interrupt handler will not
12614 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12615 * the intrrupt context. This function is called without any lock held.
12616 * It gets the hbalock to access and update SLI data structures. Note that,
12617 * the EQ to CQ are one-to-one map such that the EQ index is
12618 * equal to that of CQ index.
12619 *
12620 * This function returns IRQ_HANDLED when interrupt is handled else it
12621 * returns IRQ_NONE.
12622 **/
12623irqreturn_t
12624lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12625{
12626 struct lpfc_hba *phba;
12627 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12628 struct lpfc_queue *eq;
12629 struct lpfc_eqe *eqe;
12630 unsigned long iflag;
12631 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012632
12633 /* Get the driver's phba structure from the dev_id */
12634 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12635 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012636
12637 if (unlikely(!phba))
12638 return IRQ_NONE;
12639
12640 /* Get to the EQ struct associated with this vector */
12641 eq = phba->sli4_hba.fof_eq;
12642 if (unlikely(!eq))
12643 return IRQ_NONE;
12644
12645 /* Check device state for handling interrupt */
12646 if (unlikely(lpfc_intr_state_check(phba))) {
12647 eq->EQ_badstate++;
12648 /* Check again for link_state with lock held */
12649 spin_lock_irqsave(&phba->hbalock, iflag);
12650 if (phba->link_state < LPFC_LINK_DOWN)
12651 /* Flush, clear interrupt, and rearm the EQ */
12652 lpfc_sli4_eq_flush(phba, eq);
12653 spin_unlock_irqrestore(&phba->hbalock, iflag);
12654 return IRQ_NONE;
12655 }
12656
12657 /*
12658 * Process all the event on FCP fast-path EQ
12659 */
12660 while ((eqe = lpfc_sli4_eq_get(eq))) {
12661 lpfc_sli4_fof_handle_eqe(phba, eqe);
12662 if (!(++ecount % eq->entry_repost))
12663 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12664 eq->EQ_processed++;
12665 }
12666
12667 /* Track the max number of EQEs processed in 1 intr */
12668 if (ecount > eq->EQ_max_eqe)
12669 eq->EQ_max_eqe = ecount;
12670
12671
12672 if (unlikely(ecount == 0)) {
12673 eq->EQ_no_entry++;
12674
12675 if (phba->intr_type == MSIX)
12676 /* MSI-X treated interrupt served as no EQ share INT */
12677 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12678 "9145 MSI-X interrupt with no EQE\n");
12679 else {
12680 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12681 "9146 ISR interrupt with no EQE\n");
12682 /* Non MSI-X treated on interrupt as EQ share INT */
12683 return IRQ_NONE;
12684 }
12685 }
12686 /* Always clear and re-arm the fast-path EQ */
12687 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12688 return IRQ_HANDLED;
12689}
12690
James Smart4f774512009-05-22 14:52:35 -040012691/**
James Smart67d12732012-08-03 12:36:13 -040012692 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012693 * @irq: Interrupt number.
12694 * @dev_id: The device context pointer.
12695 *
12696 * This function is directly called from the PCI layer as an interrupt
12697 * service routine when device with SLI-4 interface spec is enabled with
12698 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12699 * ring event in the HBA. However, when the device is enabled with either
12700 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12701 * device-level interrupt handler. When the PCI slot is in error recovery
12702 * or the HBA is undergoing initialization, the interrupt handler will not
12703 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12704 * the intrrupt context. This function is called without any lock held.
12705 * It gets the hbalock to access and update SLI data structures. Note that,
12706 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12707 * equal to that of FCP CQ index.
12708 *
James Smart67d12732012-08-03 12:36:13 -040012709 * The link attention and ELS ring attention events are handled
12710 * by the worker thread. The interrupt handler signals the worker thread
12711 * and returns for these events. This function is called without any lock
12712 * held. It gets the hbalock to access and update SLI data structures.
12713 *
James Smart4f774512009-05-22 14:52:35 -040012714 * This function returns IRQ_HANDLED when interrupt is handled else it
12715 * returns IRQ_NONE.
12716 **/
12717irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012718lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012719{
12720 struct lpfc_hba *phba;
12721 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12722 struct lpfc_queue *fpeq;
12723 struct lpfc_eqe *eqe;
12724 unsigned long iflag;
12725 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012726 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012727
12728 /* Get the driver's phba structure from the dev_id */
12729 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12730 phba = fcp_eq_hdl->phba;
12731 fcp_eqidx = fcp_eq_hdl->idx;
12732
12733 if (unlikely(!phba))
12734 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012735 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012736 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012737
12738 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012739 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012740 if (unlikely(!fpeq))
12741 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012742
James Smartba20c852012-08-03 12:36:52 -040012743 if (lpfc_fcp_look_ahead) {
12744 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12745 lpfc_sli4_eq_clr_intr(fpeq);
12746 else {
12747 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12748 return IRQ_NONE;
12749 }
12750 }
12751
James Smart4f774512009-05-22 14:52:35 -040012752 /* Check device state for handling interrupt */
12753 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012754 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012755 /* Check again for link_state with lock held */
12756 spin_lock_irqsave(&phba->hbalock, iflag);
12757 if (phba->link_state < LPFC_LINK_DOWN)
12758 /* Flush, clear interrupt, and rearm the EQ */
12759 lpfc_sli4_eq_flush(phba, fpeq);
12760 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012761 if (lpfc_fcp_look_ahead)
12762 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012763 return IRQ_NONE;
12764 }
12765
12766 /*
12767 * Process all the event on FCP fast-path EQ
12768 */
12769 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012770 if (eqe == NULL)
12771 break;
12772
James Smart67d12732012-08-03 12:36:13 -040012773 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012774 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012775 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012776 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012777 }
12778
James Smartb84daac2012-08-03 12:35:13 -040012779 /* Track the max number of EQEs processed in 1 intr */
12780 if (ecount > fpeq->EQ_max_eqe)
12781 fpeq->EQ_max_eqe = ecount;
12782
James Smart4f774512009-05-22 14:52:35 -040012783 /* Always clear and re-arm the fast-path EQ */
12784 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12785
12786 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012787 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012788
12789 if (lpfc_fcp_look_ahead) {
12790 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12791 return IRQ_NONE;
12792 }
12793
James Smart4f774512009-05-22 14:52:35 -040012794 if (phba->intr_type == MSIX)
12795 /* MSI-X treated interrupt served as no EQ share INT */
12796 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12797 "0358 MSI-X interrupt with no EQE\n");
12798 else
12799 /* Non MSI-X treated on interrupt as EQ share INT */
12800 return IRQ_NONE;
12801 }
12802
James Smartba20c852012-08-03 12:36:52 -040012803 if (lpfc_fcp_look_ahead)
12804 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012805 return IRQ_HANDLED;
12806} /* lpfc_sli4_fp_intr_handler */
12807
12808/**
12809 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12810 * @irq: Interrupt number.
12811 * @dev_id: The device context pointer.
12812 *
12813 * This function is the device-level interrupt handler to device with SLI-4
12814 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12815 * interrupt mode is enabled and there is an event in the HBA which requires
12816 * driver attention. This function invokes the slow-path interrupt attention
12817 * handling function and fast-path interrupt attention handling function in
12818 * turn to process the relevant HBA attention events. This function is called
12819 * without any lock held. It gets the hbalock to access and update SLI data
12820 * structures.
12821 *
12822 * This function returns IRQ_HANDLED when interrupt is handled, else it
12823 * returns IRQ_NONE.
12824 **/
12825irqreturn_t
12826lpfc_sli4_intr_handler(int irq, void *dev_id)
12827{
12828 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012829 irqreturn_t hba_irq_rc;
12830 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012831 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012832
12833 /* Get the driver's phba structure from the dev_id */
12834 phba = (struct lpfc_hba *)dev_id;
12835
12836 if (unlikely(!phba))
12837 return IRQ_NONE;
12838
12839 /*
James Smart4f774512009-05-22 14:52:35 -040012840 * Invoke fast-path host attention interrupt handling as appropriate.
12841 */
James Smart67d12732012-08-03 12:36:13 -040012842 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12843 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012844 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012845 if (hba_irq_rc == IRQ_HANDLED)
12846 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012847 }
12848
James Smart1ba981f2014-02-20 09:56:45 -050012849 if (phba->cfg_fof) {
12850 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12851 &phba->sli4_hba.fcp_eq_hdl[0]);
12852 if (hba_irq_rc == IRQ_HANDLED)
12853 hba_handled |= true;
12854 }
12855
James Smart67d12732012-08-03 12:36:13 -040012856 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012857} /* lpfc_sli4_intr_handler */
12858
12859/**
12860 * lpfc_sli4_queue_free - free a queue structure and associated memory
12861 * @queue: The queue structure to free.
12862 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012863 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012864 * the host resident queue. This function must be called after destroying the
12865 * queue on the HBA.
12866 **/
12867void
12868lpfc_sli4_queue_free(struct lpfc_queue *queue)
12869{
12870 struct lpfc_dmabuf *dmabuf;
12871
12872 if (!queue)
12873 return;
12874
12875 while (!list_empty(&queue->page_list)) {
12876 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12877 list);
James Smart49198b32010-04-06 15:04:33 -040012878 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012879 dmabuf->virt, dmabuf->phys);
12880 kfree(dmabuf);
12881 }
12882 kfree(queue);
12883 return;
12884}
12885
12886/**
12887 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12888 * @phba: The HBA that this queue is being created on.
12889 * @entry_size: The size of each queue entry for this queue.
12890 * @entry count: The number of entries that this queue will handle.
12891 *
12892 * This function allocates a queue structure and the DMAable memory used for
12893 * the host resident queue. This function must be called before creating the
12894 * queue on the HBA.
12895 **/
12896struct lpfc_queue *
12897lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12898 uint32_t entry_count)
12899{
12900 struct lpfc_queue *queue;
12901 struct lpfc_dmabuf *dmabuf;
12902 int x, total_qe_count;
12903 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012904 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012905
James Smartcb5172e2010-03-15 11:25:07 -040012906 if (!phba->sli4_hba.pc_sli4_params.supported)
12907 hw_page_size = SLI4_PAGE_SIZE;
12908
James Smart4f774512009-05-22 14:52:35 -040012909 queue = kzalloc(sizeof(struct lpfc_queue) +
12910 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12911 if (!queue)
12912 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012913 queue->page_count = (ALIGN(entry_size * entry_count,
12914 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012915 INIT_LIST_HEAD(&queue->list);
12916 INIT_LIST_HEAD(&queue->page_list);
12917 INIT_LIST_HEAD(&queue->child_list);
12918 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12919 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12920 if (!dmabuf)
12921 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040012922 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
12923 hw_page_size, &dmabuf->phys,
12924 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040012925 if (!dmabuf->virt) {
12926 kfree(dmabuf);
12927 goto out_fail;
12928 }
12929 dmabuf->buffer_tag = x;
12930 list_add_tail(&dmabuf->list, &queue->page_list);
12931 /* initialize queue's entry array */
12932 dma_pointer = dmabuf->virt;
12933 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012934 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012935 total_qe_count++, dma_pointer += entry_size) {
12936 queue->qe[total_qe_count].address = dma_pointer;
12937 }
12938 }
12939 queue->entry_size = entry_size;
12940 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012941
12942 /*
12943 * entry_repost is calculated based on the number of entries in the
12944 * queue. This works out except for RQs. If buffers are NOT initially
12945 * posted for every RQE, entry_repost should be adjusted accordingly.
12946 */
12947 queue->entry_repost = (entry_count >> 3);
12948 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12949 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012950 queue->phba = phba;
12951
12952 return queue;
12953out_fail:
12954 lpfc_sli4_queue_free(queue);
12955 return NULL;
12956}
12957
12958/**
James Smart962bc512013-01-03 15:44:00 -050012959 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12960 * @phba: HBA structure that indicates port to create a queue on.
12961 * @pci_barset: PCI BAR set flag.
12962 *
12963 * This function shall perform iomap of the specified PCI BAR address to host
12964 * memory address if not already done so and return it. The returned host
12965 * memory address can be NULL.
12966 */
12967static void __iomem *
12968lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12969{
James Smart962bc512013-01-03 15:44:00 -050012970 if (!phba->pcidev)
12971 return NULL;
James Smart962bc512013-01-03 15:44:00 -050012972
12973 switch (pci_barset) {
12974 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050012975 return phba->pci_bar0_memmap_p;
12976 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050012977 return phba->pci_bar2_memmap_p;
12978 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050012979 return phba->pci_bar4_memmap_p;
12980 default:
12981 break;
12982 }
12983 return NULL;
12984}
12985
12986/**
James Smart173edbb2012-06-12 13:54:50 -040012987 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12988 * @phba: HBA structure that indicates port to create a queue on.
12989 * @startq: The starting FCP EQ to modify
12990 *
12991 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12992 *
12993 * The @phba struct is used to send mailbox command to HBA. The @startq
12994 * is used to get the starting FCP EQ to change.
12995 * This function is asynchronous and will wait for the mailbox
12996 * command to finish before continuing.
12997 *
12998 * On success this function will return a zero. If unable to allocate enough
12999 * memory this function will return -ENOMEM. If the queue create mailbox command
13000 * fails this function will return -ENXIO.
13001 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013002int
James Smart2c9c5a02015-04-07 15:07:15 -040013003lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013004{
13005 struct lpfc_mbx_modify_eq_delay *eq_delay;
13006 LPFC_MBOXQ_t *mbox;
13007 struct lpfc_queue *eq;
13008 int cnt, rc, length, status = 0;
13009 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013010 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013011 int fcp_eqidx;
13012 union lpfc_sli4_cfg_shdr *shdr;
13013 uint16_t dmult;
13014
James Smart67d12732012-08-03 12:36:13 -040013015 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013016 return 0;
13017
13018 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13019 if (!mbox)
13020 return -ENOMEM;
13021 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13022 sizeof(struct lpfc_sli4_cfg_mhdr));
13023 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13024 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13025 length, LPFC_SLI4_MBX_EMBED);
13026 eq_delay = &mbox->u.mqe.un.eq_delay;
13027
13028 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013029 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13030 if (result > LPFC_DMULT_CONST)
13031 dmult = 0;
13032 else
13033 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013034
13035 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013036 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013037 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013038 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013039 if (!eq)
13040 continue;
13041 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13042 eq_delay->u.request.eq[cnt].phase = 0;
13043 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13044 cnt++;
13045 if (cnt >= LPFC_MAX_EQ_DELAY)
13046 break;
13047 }
13048 eq_delay->u.request.num_eq = cnt;
13049
13050 mbox->vport = phba->pport;
13051 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13052 mbox->context1 = NULL;
13053 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13054 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13055 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13056 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13057 if (shdr_status || shdr_add_status || rc) {
13058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13059 "2512 MODIFY_EQ_DELAY mailbox failed with "
13060 "status x%x add_status x%x, mbx status x%x\n",
13061 shdr_status, shdr_add_status, rc);
13062 status = -ENXIO;
13063 }
13064 mempool_free(mbox, phba->mbox_mem_pool);
13065 return status;
13066}
13067
13068/**
James Smart4f774512009-05-22 14:52:35 -040013069 * lpfc_eq_create - Create an Event Queue on the HBA
13070 * @phba: HBA structure that indicates port to create a queue on.
13071 * @eq: The queue structure to use to create the event queue.
13072 * @imax: The maximum interrupt per second limit.
13073 *
13074 * This function creates an event queue, as detailed in @eq, on a port,
13075 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13076 *
13077 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13078 * is used to get the entry count and entry size that are necessary to
13079 * determine the number of pages to allocate and use for this queue. This
13080 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13081 * event queue. This function is asynchronous and will wait for the mailbox
13082 * command to finish before continuing.
13083 *
13084 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013085 * memory this function will return -ENOMEM. If the queue create mailbox command
13086 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013087 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013088int
James Smartee020062012-09-29 11:28:52 -040013089lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013090{
13091 struct lpfc_mbx_eq_create *eq_create;
13092 LPFC_MBOXQ_t *mbox;
13093 int rc, length, status = 0;
13094 struct lpfc_dmabuf *dmabuf;
13095 uint32_t shdr_status, shdr_add_status;
13096 union lpfc_sli4_cfg_shdr *shdr;
13097 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013098 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13099
James Smart2e90f4b2011-12-13 13:22:37 -050013100 /* sanity check on queue memory */
13101 if (!eq)
13102 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013103 if (!phba->sli4_hba.pc_sli4_params.supported)
13104 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013105
13106 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13107 if (!mbox)
13108 return -ENOMEM;
13109 length = (sizeof(struct lpfc_mbx_eq_create) -
13110 sizeof(struct lpfc_sli4_cfg_mhdr));
13111 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13112 LPFC_MBOX_OPCODE_EQ_CREATE,
13113 length, LPFC_SLI4_MBX_EMBED);
13114 eq_create = &mbox->u.mqe.un.eq_create;
13115 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13116 eq->page_count);
13117 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13118 LPFC_EQE_SIZE);
13119 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013120 /* don't setup delay multiplier using EQ_CREATE */
13121 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013122 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13123 dmult);
13124 switch (eq->entry_count) {
13125 default:
13126 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13127 "0360 Unsupported EQ count. (%d)\n",
13128 eq->entry_count);
13129 if (eq->entry_count < 256)
13130 return -EINVAL;
13131 /* otherwise default to smallest count (drop through) */
13132 case 256:
13133 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13134 LPFC_EQ_CNT_256);
13135 break;
13136 case 512:
13137 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13138 LPFC_EQ_CNT_512);
13139 break;
13140 case 1024:
13141 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13142 LPFC_EQ_CNT_1024);
13143 break;
13144 case 2048:
13145 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13146 LPFC_EQ_CNT_2048);
13147 break;
13148 case 4096:
13149 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13150 LPFC_EQ_CNT_4096);
13151 break;
13152 }
13153 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013154 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013155 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13156 putPaddrLow(dmabuf->phys);
13157 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13158 putPaddrHigh(dmabuf->phys);
13159 }
13160 mbox->vport = phba->pport;
13161 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13162 mbox->context1 = NULL;
13163 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13164 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13165 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13166 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13167 if (shdr_status || shdr_add_status || rc) {
13168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13169 "2500 EQ_CREATE mailbox failed with "
13170 "status x%x add_status x%x, mbx status x%x\n",
13171 shdr_status, shdr_add_status, rc);
13172 status = -ENXIO;
13173 }
13174 eq->type = LPFC_EQ;
13175 eq->subtype = LPFC_NONE;
13176 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13177 if (eq->queue_id == 0xFFFF)
13178 status = -ENXIO;
13179 eq->host_index = 0;
13180 eq->hba_index = 0;
13181
James Smart8fa38512009-07-19 10:01:03 -040013182 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013183 return status;
13184}
13185
13186/**
13187 * lpfc_cq_create - Create a Completion Queue on the HBA
13188 * @phba: HBA structure that indicates port to create a queue on.
13189 * @cq: The queue structure to use to create the completion queue.
13190 * @eq: The event queue to bind this completion queue to.
13191 *
13192 * This function creates a completion queue, as detailed in @wq, on a port,
13193 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13194 *
13195 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13196 * is used to get the entry count and entry size that are necessary to
13197 * determine the number of pages to allocate and use for this queue. The @eq
13198 * is used to indicate which event queue to bind this completion queue to. This
13199 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13200 * completion queue. This function is asynchronous and will wait for the mailbox
13201 * command to finish before continuing.
13202 *
13203 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013204 * memory this function will return -ENOMEM. If the queue create mailbox command
13205 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013206 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013207int
James Smart4f774512009-05-22 14:52:35 -040013208lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13209 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13210{
13211 struct lpfc_mbx_cq_create *cq_create;
13212 struct lpfc_dmabuf *dmabuf;
13213 LPFC_MBOXQ_t *mbox;
13214 int rc, length, status = 0;
13215 uint32_t shdr_status, shdr_add_status;
13216 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013217 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13218
James Smart2e90f4b2011-12-13 13:22:37 -050013219 /* sanity check on queue memory */
13220 if (!cq || !eq)
13221 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013222 if (!phba->sli4_hba.pc_sli4_params.supported)
13223 hw_page_size = SLI4_PAGE_SIZE;
13224
James Smart4f774512009-05-22 14:52:35 -040013225 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13226 if (!mbox)
13227 return -ENOMEM;
13228 length = (sizeof(struct lpfc_mbx_cq_create) -
13229 sizeof(struct lpfc_sli4_cfg_mhdr));
13230 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13231 LPFC_MBOX_OPCODE_CQ_CREATE,
13232 length, LPFC_SLI4_MBX_EMBED);
13233 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013234 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013235 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13236 cq->page_count);
13237 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13238 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013239 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13240 phba->sli4_hba.pc_sli4_params.cqv);
13241 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013242 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13243 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013244 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13245 eq->queue_id);
13246 } else {
13247 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13248 eq->queue_id);
13249 }
James Smart4f774512009-05-22 14:52:35 -040013250 switch (cq->entry_count) {
13251 default:
13252 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13253 "0361 Unsupported CQ count. (%d)\n",
13254 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013255 if (cq->entry_count < 256) {
13256 status = -EINVAL;
13257 goto out;
13258 }
James Smart4f774512009-05-22 14:52:35 -040013259 /* otherwise default to smallest count (drop through) */
13260 case 256:
13261 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13262 LPFC_CQ_CNT_256);
13263 break;
13264 case 512:
13265 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13266 LPFC_CQ_CNT_512);
13267 break;
13268 case 1024:
13269 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13270 LPFC_CQ_CNT_1024);
13271 break;
13272 }
13273 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013274 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013275 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13276 putPaddrLow(dmabuf->phys);
13277 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13278 putPaddrHigh(dmabuf->phys);
13279 }
13280 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13281
13282 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013283 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13284 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13285 if (shdr_status || shdr_add_status || rc) {
13286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13287 "2501 CQ_CREATE mailbox failed with "
13288 "status x%x add_status x%x, mbx status x%x\n",
13289 shdr_status, shdr_add_status, rc);
13290 status = -ENXIO;
13291 goto out;
13292 }
13293 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13294 if (cq->queue_id == 0xFFFF) {
13295 status = -ENXIO;
13296 goto out;
13297 }
13298 /* link the cq onto the parent eq child list */
13299 list_add_tail(&cq->list, &eq->child_list);
13300 /* Set up completion queue's type and subtype */
13301 cq->type = type;
13302 cq->subtype = subtype;
13303 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013304 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013305 cq->host_index = 0;
13306 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013307
James Smart8fa38512009-07-19 10:01:03 -040013308out:
13309 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013310 return status;
13311}
13312
13313/**
James Smartb19a0612010-04-06 14:48:51 -040013314 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013315 * @phba: HBA structure that indicates port to create a queue on.
13316 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013317 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13318 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013319 *
James Smartb19a0612010-04-06 14:48:51 -040013320 * This function provides failback (fb) functionality when the
13321 * mq_create_ext fails on older FW generations. It's purpose is identical
13322 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013323 *
James Smartb19a0612010-04-06 14:48:51 -040013324 * This routine cannot fail as all attributes were previously accessed and
13325 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013326 **/
James Smartb19a0612010-04-06 14:48:51 -040013327static void
13328lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13329 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013330{
13331 struct lpfc_mbx_mq_create *mq_create;
13332 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013333 int length;
James Smart04c68492009-05-22 14:52:52 -040013334
James Smart04c68492009-05-22 14:52:52 -040013335 length = (sizeof(struct lpfc_mbx_mq_create) -
13336 sizeof(struct lpfc_sli4_cfg_mhdr));
13337 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13338 LPFC_MBOX_OPCODE_MQ_CREATE,
13339 length, LPFC_SLI4_MBX_EMBED);
13340 mq_create = &mbox->u.mqe.un.mq_create;
13341 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013342 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013343 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013344 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013345 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13346 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013347 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013348 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13349 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013350 break;
13351 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013352 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13353 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013354 break;
13355 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013356 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13357 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013358 break;
13359 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013360 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13361 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013362 break;
13363 }
13364 list_for_each_entry(dmabuf, &mq->page_list, list) {
13365 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013366 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013367 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013368 putPaddrHigh(dmabuf->phys);
13369 }
13370}
13371
13372/**
13373 * lpfc_mq_create - Create a mailbox Queue on the HBA
13374 * @phba: HBA structure that indicates port to create a queue on.
13375 * @mq: The queue structure to use to create the mailbox queue.
13376 * @cq: The completion queue to associate with this cq.
13377 * @subtype: The queue's subtype.
13378 *
13379 * This function creates a mailbox queue, as detailed in @mq, on a port,
13380 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13381 *
13382 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13383 * is used to get the entry count and entry size that are necessary to
13384 * determine the number of pages to allocate and use for this queue. This
13385 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13386 * mailbox queue. This function is asynchronous and will wait for the mailbox
13387 * command to finish before continuing.
13388 *
13389 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013390 * memory this function will return -ENOMEM. If the queue create mailbox command
13391 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013392 **/
13393int32_t
13394lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13395 struct lpfc_queue *cq, uint32_t subtype)
13396{
13397 struct lpfc_mbx_mq_create *mq_create;
13398 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13399 struct lpfc_dmabuf *dmabuf;
13400 LPFC_MBOXQ_t *mbox;
13401 int rc, length, status = 0;
13402 uint32_t shdr_status, shdr_add_status;
13403 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013404 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013405
James Smart2e90f4b2011-12-13 13:22:37 -050013406 /* sanity check on queue memory */
13407 if (!mq || !cq)
13408 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013409 if (!phba->sli4_hba.pc_sli4_params.supported)
13410 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013411
13412 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13413 if (!mbox)
13414 return -ENOMEM;
13415 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13416 sizeof(struct lpfc_sli4_cfg_mhdr));
13417 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13418 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13419 length, LPFC_SLI4_MBX_EMBED);
13420
13421 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013422 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013423 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13424 &mq_create_ext->u.request, mq->page_count);
13425 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13426 &mq_create_ext->u.request, 1);
13427 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013428 &mq_create_ext->u.request, 1);
13429 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13430 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013431 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13432 &mq_create_ext->u.request, 1);
13433 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13434 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013435 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013436 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13437 phba->sli4_hba.pc_sli4_params.mqv);
13438 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13439 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13440 cq->queue_id);
13441 else
13442 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13443 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013444 switch (mq->entry_count) {
13445 default:
13446 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13447 "0362 Unsupported MQ count. (%d)\n",
13448 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013449 if (mq->entry_count < 16) {
13450 status = -EINVAL;
13451 goto out;
13452 }
James Smartb19a0612010-04-06 14:48:51 -040013453 /* otherwise default to smallest count (drop through) */
13454 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013455 bf_set(lpfc_mq_context_ring_size,
13456 &mq_create_ext->u.request.context,
13457 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013458 break;
13459 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013460 bf_set(lpfc_mq_context_ring_size,
13461 &mq_create_ext->u.request.context,
13462 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013463 break;
13464 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013465 bf_set(lpfc_mq_context_ring_size,
13466 &mq_create_ext->u.request.context,
13467 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013468 break;
13469 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013470 bf_set(lpfc_mq_context_ring_size,
13471 &mq_create_ext->u.request.context,
13472 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013473 break;
13474 }
13475 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013476 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013477 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13478 putPaddrLow(dmabuf->phys);
13479 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013480 putPaddrHigh(dmabuf->phys);
13481 }
13482 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013483 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13484 &mq_create_ext->u.response);
13485 if (rc != MBX_SUCCESS) {
13486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13487 "2795 MQ_CREATE_EXT failed with "
13488 "status x%x. Failback to MQ_CREATE.\n",
13489 rc);
13490 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13491 mq_create = &mbox->u.mqe.un.mq_create;
13492 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13493 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13494 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13495 &mq_create->u.response);
13496 }
13497
James Smart04c68492009-05-22 14:52:52 -040013498 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013499 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13500 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13501 if (shdr_status || shdr_add_status || rc) {
13502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13503 "2502 MQ_CREATE mailbox failed with "
13504 "status x%x add_status x%x, mbx status x%x\n",
13505 shdr_status, shdr_add_status, rc);
13506 status = -ENXIO;
13507 goto out;
13508 }
James Smart04c68492009-05-22 14:52:52 -040013509 if (mq->queue_id == 0xFFFF) {
13510 status = -ENXIO;
13511 goto out;
13512 }
13513 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013514 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013515 mq->subtype = subtype;
13516 mq->host_index = 0;
13517 mq->hba_index = 0;
13518
13519 /* link the mq onto the parent cq child list */
13520 list_add_tail(&mq->list, &cq->child_list);
13521out:
James Smart8fa38512009-07-19 10:01:03 -040013522 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013523 return status;
13524}
13525
13526/**
James Smart4f774512009-05-22 14:52:35 -040013527 * lpfc_wq_create - Create a Work Queue on the HBA
13528 * @phba: HBA structure that indicates port to create a queue on.
13529 * @wq: The queue structure to use to create the work queue.
13530 * @cq: The completion queue to bind this work queue to.
13531 * @subtype: The subtype of the work queue indicating its functionality.
13532 *
13533 * This function creates a work queue, as detailed in @wq, on a port, described
13534 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13535 *
13536 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13537 * is used to get the entry count and entry size that are necessary to
13538 * determine the number of pages to allocate and use for this queue. The @cq
13539 * is used to indicate which completion queue to bind this work queue to. This
13540 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13541 * work queue. This function is asynchronous and will wait for the mailbox
13542 * command to finish before continuing.
13543 *
13544 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013545 * memory this function will return -ENOMEM. If the queue create mailbox command
13546 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013547 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013548int
James Smart4f774512009-05-22 14:52:35 -040013549lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13550 struct lpfc_queue *cq, uint32_t subtype)
13551{
13552 struct lpfc_mbx_wq_create *wq_create;
13553 struct lpfc_dmabuf *dmabuf;
13554 LPFC_MBOXQ_t *mbox;
13555 int rc, length, status = 0;
13556 uint32_t shdr_status, shdr_add_status;
13557 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013558 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013559 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013560 void __iomem *bar_memmap_p;
13561 uint32_t db_offset;
13562 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013563
James Smart2e90f4b2011-12-13 13:22:37 -050013564 /* sanity check on queue memory */
13565 if (!wq || !cq)
13566 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013567 if (!phba->sli4_hba.pc_sli4_params.supported)
13568 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013569
13570 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13571 if (!mbox)
13572 return -ENOMEM;
13573 length = (sizeof(struct lpfc_mbx_wq_create) -
13574 sizeof(struct lpfc_sli4_cfg_mhdr));
13575 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13576 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13577 length, LPFC_SLI4_MBX_EMBED);
13578 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013579 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013580 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13581 wq->page_count);
13582 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13583 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013584
13585 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013586 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13587 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013588
James Smart0c651872013-07-15 18:33:23 -040013589 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13590 case LPFC_Q_CREATE_VERSION_0:
13591 switch (wq->entry_size) {
13592 default:
13593 case 64:
13594 /* Nothing to do, version 0 ONLY supports 64 byte */
13595 page = wq_create->u.request.page;
13596 break;
13597 case 128:
13598 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13599 LPFC_WQ_SZ128_SUPPORT)) {
13600 status = -ERANGE;
13601 goto out;
13602 }
13603 /* If we get here the HBA MUST also support V1 and
13604 * we MUST use it
13605 */
13606 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13607 LPFC_Q_CREATE_VERSION_1);
13608
13609 bf_set(lpfc_mbx_wq_create_wqe_count,
13610 &wq_create->u.request_1, wq->entry_count);
13611 bf_set(lpfc_mbx_wq_create_wqe_size,
13612 &wq_create->u.request_1,
13613 LPFC_WQ_WQE_SIZE_128);
13614 bf_set(lpfc_mbx_wq_create_page_size,
13615 &wq_create->u.request_1,
13616 (PAGE_SIZE/SLI4_PAGE_SIZE));
13617 page = wq_create->u.request_1.page;
13618 break;
13619 }
13620 break;
13621 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013622 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13623 wq->entry_count);
13624 switch (wq->entry_size) {
13625 default:
13626 case 64:
13627 bf_set(lpfc_mbx_wq_create_wqe_size,
13628 &wq_create->u.request_1,
13629 LPFC_WQ_WQE_SIZE_64);
13630 break;
13631 case 128:
James Smart0c651872013-07-15 18:33:23 -040013632 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13633 LPFC_WQ_SZ128_SUPPORT)) {
13634 status = -ERANGE;
13635 goto out;
13636 }
James Smart5a6f1332011-03-11 16:05:35 -050013637 bf_set(lpfc_mbx_wq_create_wqe_size,
13638 &wq_create->u.request_1,
13639 LPFC_WQ_WQE_SIZE_128);
13640 break;
13641 }
13642 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13643 (PAGE_SIZE/SLI4_PAGE_SIZE));
13644 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013645 break;
13646 default:
13647 status = -ERANGE;
13648 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013649 }
James Smart0c651872013-07-15 18:33:23 -040013650
James Smart4f774512009-05-22 14:52:35 -040013651 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013652 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013653 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13654 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013655 }
James Smart962bc512013-01-03 15:44:00 -050013656
13657 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13658 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13659
James Smart4f774512009-05-22 14:52:35 -040013660 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13661 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013662 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13663 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13664 if (shdr_status || shdr_add_status || rc) {
13665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13666 "2503 WQ_CREATE mailbox failed with "
13667 "status x%x add_status x%x, mbx status x%x\n",
13668 shdr_status, shdr_add_status, rc);
13669 status = -ENXIO;
13670 goto out;
13671 }
13672 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13673 if (wq->queue_id == 0xFFFF) {
13674 status = -ENXIO;
13675 goto out;
13676 }
James Smart962bc512013-01-03 15:44:00 -050013677 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13678 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13679 &wq_create->u.response);
13680 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13681 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13683 "3265 WQ[%d] doorbell format not "
13684 "supported: x%x\n", wq->queue_id,
13685 wq->db_format);
13686 status = -EINVAL;
13687 goto out;
13688 }
13689 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13690 &wq_create->u.response);
13691 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13692 if (!bar_memmap_p) {
13693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13694 "3263 WQ[%d] failed to memmap pci "
13695 "barset:x%x\n", wq->queue_id,
13696 pci_barset);
13697 status = -ENOMEM;
13698 goto out;
13699 }
13700 db_offset = wq_create->u.response.doorbell_offset;
13701 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13702 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13704 "3252 WQ[%d] doorbell offset not "
13705 "supported: x%x\n", wq->queue_id,
13706 db_offset);
13707 status = -EINVAL;
13708 goto out;
13709 }
13710 wq->db_regaddr = bar_memmap_p + db_offset;
13711 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013712 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13713 "format:x%x\n", wq->queue_id, pci_barset,
13714 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013715 } else {
13716 wq->db_format = LPFC_DB_LIST_FORMAT;
13717 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13718 }
James Smart4f774512009-05-22 14:52:35 -040013719 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013720 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013721 wq->subtype = subtype;
13722 wq->host_index = 0;
13723 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013724 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013725
13726 /* link the wq onto the parent cq child list */
13727 list_add_tail(&wq->list, &cq->child_list);
13728out:
James Smart8fa38512009-07-19 10:01:03 -040013729 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013730 return status;
13731}
13732
13733/**
James Smart73d91e52011-10-10 21:32:10 -040013734 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13735 * @phba: HBA structure that indicates port to create a queue on.
13736 * @rq: The queue structure to use for the receive queue.
13737 * @qno: The associated HBQ number
13738 *
13739 *
13740 * For SLI4 we need to adjust the RQ repost value based on
13741 * the number of buffers that are initially posted to the RQ.
13742 */
13743void
13744lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13745{
13746 uint32_t cnt;
13747
James Smart2e90f4b2011-12-13 13:22:37 -050013748 /* sanity check on queue memory */
13749 if (!rq)
13750 return;
James Smart73d91e52011-10-10 21:32:10 -040013751 cnt = lpfc_hbq_defs[qno]->entry_count;
13752
13753 /* Recalc repost for RQs based on buffers initially posted */
13754 cnt = (cnt >> 3);
13755 if (cnt < LPFC_QUEUE_MIN_REPOST)
13756 cnt = LPFC_QUEUE_MIN_REPOST;
13757
13758 rq->entry_repost = cnt;
13759}
13760
13761/**
James Smart4f774512009-05-22 14:52:35 -040013762 * lpfc_rq_create - Create a Receive Queue on the HBA
13763 * @phba: HBA structure that indicates port to create a queue on.
13764 * @hrq: The queue structure to use to create the header receive queue.
13765 * @drq: The queue structure to use to create the data receive queue.
13766 * @cq: The completion queue to bind this work queue to.
13767 *
13768 * This function creates a receive buffer queue pair , as detailed in @hrq and
13769 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13770 * to the HBA.
13771 *
13772 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13773 * struct is used to get the entry count that is necessary to determine the
13774 * number of pages to use for this queue. The @cq is used to indicate which
13775 * completion queue to bind received buffers that are posted to these queues to.
13776 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13777 * receive queue pair. This function is asynchronous and will wait for the
13778 * mailbox command to finish before continuing.
13779 *
13780 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013781 * memory this function will return -ENOMEM. If the queue create mailbox command
13782 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013783 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013784int
James Smart4f774512009-05-22 14:52:35 -040013785lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13786 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13787{
13788 struct lpfc_mbx_rq_create *rq_create;
13789 struct lpfc_dmabuf *dmabuf;
13790 LPFC_MBOXQ_t *mbox;
13791 int rc, length, status = 0;
13792 uint32_t shdr_status, shdr_add_status;
13793 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013794 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013795 void __iomem *bar_memmap_p;
13796 uint32_t db_offset;
13797 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013798
James Smart2e90f4b2011-12-13 13:22:37 -050013799 /* sanity check on queue memory */
13800 if (!hrq || !drq || !cq)
13801 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013802 if (!phba->sli4_hba.pc_sli4_params.supported)
13803 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013804
13805 if (hrq->entry_count != drq->entry_count)
13806 return -EINVAL;
13807 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13808 if (!mbox)
13809 return -ENOMEM;
13810 length = (sizeof(struct lpfc_mbx_rq_create) -
13811 sizeof(struct lpfc_sli4_cfg_mhdr));
13812 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13813 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13814 length, LPFC_SLI4_MBX_EMBED);
13815 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013816 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13817 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13818 phba->sli4_hba.pc_sli4_params.rqv);
13819 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13820 bf_set(lpfc_rq_context_rqe_count_1,
13821 &rq_create->u.request.context,
13822 hrq->entry_count);
13823 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013824 bf_set(lpfc_rq_context_rqe_size,
13825 &rq_create->u.request.context,
13826 LPFC_RQE_SIZE_8);
13827 bf_set(lpfc_rq_context_page_size,
13828 &rq_create->u.request.context,
13829 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013830 } else {
13831 switch (hrq->entry_count) {
13832 default:
13833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13834 "2535 Unsupported RQ count. (%d)\n",
13835 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013836 if (hrq->entry_count < 512) {
13837 status = -EINVAL;
13838 goto out;
13839 }
James Smart5a6f1332011-03-11 16:05:35 -050013840 /* otherwise default to smallest count (drop through) */
13841 case 512:
13842 bf_set(lpfc_rq_context_rqe_count,
13843 &rq_create->u.request.context,
13844 LPFC_RQ_RING_SIZE_512);
13845 break;
13846 case 1024:
13847 bf_set(lpfc_rq_context_rqe_count,
13848 &rq_create->u.request.context,
13849 LPFC_RQ_RING_SIZE_1024);
13850 break;
13851 case 2048:
13852 bf_set(lpfc_rq_context_rqe_count,
13853 &rq_create->u.request.context,
13854 LPFC_RQ_RING_SIZE_2048);
13855 break;
13856 case 4096:
13857 bf_set(lpfc_rq_context_rqe_count,
13858 &rq_create->u.request.context,
13859 LPFC_RQ_RING_SIZE_4096);
13860 break;
13861 }
13862 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13863 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013864 }
13865 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13866 cq->queue_id);
13867 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13868 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013869 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013870 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013871 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13872 putPaddrLow(dmabuf->phys);
13873 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13874 putPaddrHigh(dmabuf->phys);
13875 }
James Smart962bc512013-01-03 15:44:00 -050013876 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13877 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13878
James Smart4f774512009-05-22 14:52:35 -040013879 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13880 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013881 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13882 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13883 if (shdr_status || shdr_add_status || rc) {
13884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13885 "2504 RQ_CREATE mailbox failed with "
13886 "status x%x add_status x%x, mbx status x%x\n",
13887 shdr_status, shdr_add_status, rc);
13888 status = -ENXIO;
13889 goto out;
13890 }
13891 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13892 if (hrq->queue_id == 0xFFFF) {
13893 status = -ENXIO;
13894 goto out;
13895 }
James Smart962bc512013-01-03 15:44:00 -050013896
13897 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13898 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13899 &rq_create->u.response);
13900 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13901 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13903 "3262 RQ [%d] doorbell format not "
13904 "supported: x%x\n", hrq->queue_id,
13905 hrq->db_format);
13906 status = -EINVAL;
13907 goto out;
13908 }
13909
13910 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13911 &rq_create->u.response);
13912 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13913 if (!bar_memmap_p) {
13914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13915 "3269 RQ[%d] failed to memmap pci "
13916 "barset:x%x\n", hrq->queue_id,
13917 pci_barset);
13918 status = -ENOMEM;
13919 goto out;
13920 }
13921
13922 db_offset = rq_create->u.response.doorbell_offset;
13923 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13924 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13926 "3270 RQ[%d] doorbell offset not "
13927 "supported: x%x\n", hrq->queue_id,
13928 db_offset);
13929 status = -EINVAL;
13930 goto out;
13931 }
13932 hrq->db_regaddr = bar_memmap_p + db_offset;
13933 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013934 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13935 "format:x%x\n", hrq->queue_id, pci_barset,
13936 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013937 } else {
13938 hrq->db_format = LPFC_DB_RING_FORMAT;
13939 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13940 }
James Smart4f774512009-05-22 14:52:35 -040013941 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013942 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013943 hrq->subtype = subtype;
13944 hrq->host_index = 0;
13945 hrq->hba_index = 0;
13946
13947 /* now create the data queue */
13948 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13949 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13950 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050013951 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13952 phba->sli4_hba.pc_sli4_params.rqv);
13953 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13954 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040013955 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050013956 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013957 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13958 LPFC_RQE_SIZE_8);
13959 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13960 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013961 } else {
13962 switch (drq->entry_count) {
13963 default:
13964 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13965 "2536 Unsupported RQ count. (%d)\n",
13966 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013967 if (drq->entry_count < 512) {
13968 status = -EINVAL;
13969 goto out;
13970 }
James Smart5a6f1332011-03-11 16:05:35 -050013971 /* otherwise default to smallest count (drop through) */
13972 case 512:
13973 bf_set(lpfc_rq_context_rqe_count,
13974 &rq_create->u.request.context,
13975 LPFC_RQ_RING_SIZE_512);
13976 break;
13977 case 1024:
13978 bf_set(lpfc_rq_context_rqe_count,
13979 &rq_create->u.request.context,
13980 LPFC_RQ_RING_SIZE_1024);
13981 break;
13982 case 2048:
13983 bf_set(lpfc_rq_context_rqe_count,
13984 &rq_create->u.request.context,
13985 LPFC_RQ_RING_SIZE_2048);
13986 break;
13987 case 4096:
13988 bf_set(lpfc_rq_context_rqe_count,
13989 &rq_create->u.request.context,
13990 LPFC_RQ_RING_SIZE_4096);
13991 break;
13992 }
13993 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13994 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013995 }
13996 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13997 cq->queue_id);
13998 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13999 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014000 list_for_each_entry(dmabuf, &drq->page_list, list) {
14001 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14002 putPaddrLow(dmabuf->phys);
14003 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14004 putPaddrHigh(dmabuf->phys);
14005 }
James Smart962bc512013-01-03 15:44:00 -050014006 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14007 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014008 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14009 /* The IOCTL status is embedded in the mailbox subheader. */
14010 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14011 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14012 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14013 if (shdr_status || shdr_add_status || rc) {
14014 status = -ENXIO;
14015 goto out;
14016 }
14017 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14018 if (drq->queue_id == 0xFFFF) {
14019 status = -ENXIO;
14020 goto out;
14021 }
14022 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014023 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014024 drq->subtype = subtype;
14025 drq->host_index = 0;
14026 drq->hba_index = 0;
14027
14028 /* link the header and data RQs onto the parent cq child list */
14029 list_add_tail(&hrq->list, &cq->child_list);
14030 list_add_tail(&drq->list, &cq->child_list);
14031
14032out:
James Smart8fa38512009-07-19 10:01:03 -040014033 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014034 return status;
14035}
14036
14037/**
14038 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14039 * @eq: The queue structure associated with the queue to destroy.
14040 *
14041 * This function destroys a queue, as detailed in @eq by sending an mailbox
14042 * command, specific to the type of queue, to the HBA.
14043 *
14044 * The @eq struct is used to get the queue ID of the queue to destroy.
14045 *
14046 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014047 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014048 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014049int
James Smart4f774512009-05-22 14:52:35 -040014050lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14051{
14052 LPFC_MBOXQ_t *mbox;
14053 int rc, length, status = 0;
14054 uint32_t shdr_status, shdr_add_status;
14055 union lpfc_sli4_cfg_shdr *shdr;
14056
James Smart2e90f4b2011-12-13 13:22:37 -050014057 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014058 if (!eq)
14059 return -ENODEV;
14060 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14061 if (!mbox)
14062 return -ENOMEM;
14063 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14064 sizeof(struct lpfc_sli4_cfg_mhdr));
14065 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14066 LPFC_MBOX_OPCODE_EQ_DESTROY,
14067 length, LPFC_SLI4_MBX_EMBED);
14068 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14069 eq->queue_id);
14070 mbox->vport = eq->phba->pport;
14071 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14072
14073 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14074 /* The IOCTL status is embedded in the mailbox subheader. */
14075 shdr = (union lpfc_sli4_cfg_shdr *)
14076 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14077 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14078 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14079 if (shdr_status || shdr_add_status || rc) {
14080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14081 "2505 EQ_DESTROY mailbox failed with "
14082 "status x%x add_status x%x, mbx status x%x\n",
14083 shdr_status, shdr_add_status, rc);
14084 status = -ENXIO;
14085 }
14086
14087 /* Remove eq from any list */
14088 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014089 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014090 return status;
14091}
14092
14093/**
14094 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14095 * @cq: The queue structure associated with the queue to destroy.
14096 *
14097 * This function destroys a queue, as detailed in @cq by sending an mailbox
14098 * command, specific to the type of queue, to the HBA.
14099 *
14100 * The @cq struct is used to get the queue ID of the queue to destroy.
14101 *
14102 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014103 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014104 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014105int
James Smart4f774512009-05-22 14:52:35 -040014106lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14107{
14108 LPFC_MBOXQ_t *mbox;
14109 int rc, length, status = 0;
14110 uint32_t shdr_status, shdr_add_status;
14111 union lpfc_sli4_cfg_shdr *shdr;
14112
James Smart2e90f4b2011-12-13 13:22:37 -050014113 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014114 if (!cq)
14115 return -ENODEV;
14116 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14117 if (!mbox)
14118 return -ENOMEM;
14119 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14120 sizeof(struct lpfc_sli4_cfg_mhdr));
14121 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14122 LPFC_MBOX_OPCODE_CQ_DESTROY,
14123 length, LPFC_SLI4_MBX_EMBED);
14124 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14125 cq->queue_id);
14126 mbox->vport = cq->phba->pport;
14127 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14128 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14129 /* The IOCTL status is embedded in the mailbox subheader. */
14130 shdr = (union lpfc_sli4_cfg_shdr *)
14131 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14132 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14133 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14134 if (shdr_status || shdr_add_status || rc) {
14135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14136 "2506 CQ_DESTROY mailbox failed with "
14137 "status x%x add_status x%x, mbx status x%x\n",
14138 shdr_status, shdr_add_status, rc);
14139 status = -ENXIO;
14140 }
14141 /* Remove cq from any list */
14142 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014143 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014144 return status;
14145}
14146
14147/**
James Smart04c68492009-05-22 14:52:52 -040014148 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14149 * @qm: The queue structure associated with the queue to destroy.
14150 *
14151 * This function destroys a queue, as detailed in @mq by sending an mailbox
14152 * command, specific to the type of queue, to the HBA.
14153 *
14154 * The @mq struct is used to get the queue ID of the queue to destroy.
14155 *
14156 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014157 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014158 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014159int
James Smart04c68492009-05-22 14:52:52 -040014160lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14161{
14162 LPFC_MBOXQ_t *mbox;
14163 int rc, length, status = 0;
14164 uint32_t shdr_status, shdr_add_status;
14165 union lpfc_sli4_cfg_shdr *shdr;
14166
James Smart2e90f4b2011-12-13 13:22:37 -050014167 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014168 if (!mq)
14169 return -ENODEV;
14170 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14171 if (!mbox)
14172 return -ENOMEM;
14173 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14174 sizeof(struct lpfc_sli4_cfg_mhdr));
14175 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14176 LPFC_MBOX_OPCODE_MQ_DESTROY,
14177 length, LPFC_SLI4_MBX_EMBED);
14178 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14179 mq->queue_id);
14180 mbox->vport = mq->phba->pport;
14181 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14182 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14183 /* The IOCTL status is embedded in the mailbox subheader. */
14184 shdr = (union lpfc_sli4_cfg_shdr *)
14185 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14186 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14187 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14188 if (shdr_status || shdr_add_status || rc) {
14189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14190 "2507 MQ_DESTROY mailbox failed with "
14191 "status x%x add_status x%x, mbx status x%x\n",
14192 shdr_status, shdr_add_status, rc);
14193 status = -ENXIO;
14194 }
14195 /* Remove mq from any list */
14196 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014197 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014198 return status;
14199}
14200
14201/**
James Smart4f774512009-05-22 14:52:35 -040014202 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14203 * @wq: The queue structure associated with the queue to destroy.
14204 *
14205 * This function destroys a queue, as detailed in @wq by sending an mailbox
14206 * command, specific to the type of queue, to the HBA.
14207 *
14208 * The @wq struct is used to get the queue ID of the queue to destroy.
14209 *
14210 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014211 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014212 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014213int
James Smart4f774512009-05-22 14:52:35 -040014214lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14215{
14216 LPFC_MBOXQ_t *mbox;
14217 int rc, length, status = 0;
14218 uint32_t shdr_status, shdr_add_status;
14219 union lpfc_sli4_cfg_shdr *shdr;
14220
James Smart2e90f4b2011-12-13 13:22:37 -050014221 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014222 if (!wq)
14223 return -ENODEV;
14224 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14225 if (!mbox)
14226 return -ENOMEM;
14227 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14228 sizeof(struct lpfc_sli4_cfg_mhdr));
14229 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14230 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14231 length, LPFC_SLI4_MBX_EMBED);
14232 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14233 wq->queue_id);
14234 mbox->vport = wq->phba->pport;
14235 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14236 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14237 shdr = (union lpfc_sli4_cfg_shdr *)
14238 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14239 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14240 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14241 if (shdr_status || shdr_add_status || rc) {
14242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14243 "2508 WQ_DESTROY mailbox failed with "
14244 "status x%x add_status x%x, mbx status x%x\n",
14245 shdr_status, shdr_add_status, rc);
14246 status = -ENXIO;
14247 }
14248 /* Remove wq from any list */
14249 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014250 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014251 return status;
14252}
14253
14254/**
14255 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14256 * @rq: The queue structure associated with the queue to destroy.
14257 *
14258 * This function destroys a queue, as detailed in @rq by sending an mailbox
14259 * command, specific to the type of queue, to the HBA.
14260 *
14261 * The @rq struct is used to get the queue ID of the queue to destroy.
14262 *
14263 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014264 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014265 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014266int
James Smart4f774512009-05-22 14:52:35 -040014267lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14268 struct lpfc_queue *drq)
14269{
14270 LPFC_MBOXQ_t *mbox;
14271 int rc, length, status = 0;
14272 uint32_t shdr_status, shdr_add_status;
14273 union lpfc_sli4_cfg_shdr *shdr;
14274
James Smart2e90f4b2011-12-13 13:22:37 -050014275 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014276 if (!hrq || !drq)
14277 return -ENODEV;
14278 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14279 if (!mbox)
14280 return -ENOMEM;
14281 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014282 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014283 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14284 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14285 length, LPFC_SLI4_MBX_EMBED);
14286 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14287 hrq->queue_id);
14288 mbox->vport = hrq->phba->pport;
14289 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14290 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14291 /* The IOCTL status is embedded in the mailbox subheader. */
14292 shdr = (union lpfc_sli4_cfg_shdr *)
14293 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14296 if (shdr_status || shdr_add_status || rc) {
14297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14298 "2509 RQ_DESTROY mailbox failed with "
14299 "status x%x add_status x%x, mbx status x%x\n",
14300 shdr_status, shdr_add_status, rc);
14301 if (rc != MBX_TIMEOUT)
14302 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14303 return -ENXIO;
14304 }
14305 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14306 drq->queue_id);
14307 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14308 shdr = (union lpfc_sli4_cfg_shdr *)
14309 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14310 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14311 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14312 if (shdr_status || shdr_add_status || rc) {
14313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14314 "2510 RQ_DESTROY mailbox failed with "
14315 "status x%x add_status x%x, mbx status x%x\n",
14316 shdr_status, shdr_add_status, rc);
14317 status = -ENXIO;
14318 }
14319 list_del_init(&hrq->list);
14320 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014321 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014322 return status;
14323}
14324
14325/**
14326 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14327 * @phba: The virtual port for which this call being executed.
14328 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14329 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14330 * @xritag: the xritag that ties this io to the SGL pages.
14331 *
14332 * This routine will post the sgl pages for the IO that has the xritag
14333 * that is in the iocbq structure. The xritag is assigned during iocbq
14334 * creation and persists for as long as the driver is loaded.
14335 * if the caller has fewer than 256 scatter gather segments to map then
14336 * pdma_phys_addr1 should be 0.
14337 * If the caller needs to map more than 256 scatter gather segment then
14338 * pdma_phys_addr1 should be a valid physical address.
14339 * physical address for SGLs must be 64 byte aligned.
14340 * If you are going to map 2 SGL's then the first one must have 256 entries
14341 * the second sgl can have between 1 and 256 entries.
14342 *
14343 * Return codes:
14344 * 0 - Success
14345 * -ENXIO, -ENOMEM - Failure
14346 **/
14347int
14348lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14349 dma_addr_t pdma_phys_addr0,
14350 dma_addr_t pdma_phys_addr1,
14351 uint16_t xritag)
14352{
14353 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14354 LPFC_MBOXQ_t *mbox;
14355 int rc;
14356 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014357 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014358 union lpfc_sli4_cfg_shdr *shdr;
14359
14360 if (xritag == NO_XRI) {
14361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14362 "0364 Invalid param:\n");
14363 return -EINVAL;
14364 }
14365
14366 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14367 if (!mbox)
14368 return -ENOMEM;
14369
14370 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14371 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14372 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014373 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014374
14375 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14376 &mbox->u.mqe.un.post_sgl_pages;
14377 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14378 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14379
14380 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14381 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14382 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14383 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14384
14385 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14386 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14387 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14388 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14389 if (!phba->sli4_hba.intr_enable)
14390 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014391 else {
James Smarta183a152011-10-10 21:32:43 -040014392 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014393 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14394 }
James Smart4f774512009-05-22 14:52:35 -040014395 /* The IOCTL status is embedded in the mailbox subheader. */
14396 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14397 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14398 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14399 if (rc != MBX_TIMEOUT)
14400 mempool_free(mbox, phba->mbox_mem_pool);
14401 if (shdr_status || shdr_add_status || rc) {
14402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14403 "2511 POST_SGL mailbox failed with "
14404 "status x%x add_status x%x, mbx status x%x\n",
14405 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014406 }
14407 return 0;
14408}
James Smart4f774512009-05-22 14:52:35 -040014409
14410/**
James Smart88a2cfb2011-07-22 18:36:33 -040014411 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014412 * @phba: pointer to lpfc hba data structure.
14413 *
14414 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014415 * HBA consistent with the SLI-4 interface spec. This routine
14416 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14417 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014418 *
James Smart88a2cfb2011-07-22 18:36:33 -040014419 * Returns
14420 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14421 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14422 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014423static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014424lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14425{
14426 unsigned long xri;
14427
14428 /*
14429 * Fetch the next logical xri. Because this index is logical,
14430 * the driver starts at 0 each time.
14431 */
14432 spin_lock_irq(&phba->hbalock);
14433 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14434 phba->sli4_hba.max_cfg_param.max_xri, 0);
14435 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14436 spin_unlock_irq(&phba->hbalock);
14437 return NO_XRI;
14438 } else {
14439 set_bit(xri, phba->sli4_hba.xri_bmask);
14440 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014441 }
James Smart6d368e52011-05-24 11:44:12 -040014442 spin_unlock_irq(&phba->hbalock);
14443 return xri;
14444}
14445
14446/**
14447 * lpfc_sli4_free_xri - Release an xri for reuse.
14448 * @phba: pointer to lpfc hba data structure.
14449 *
14450 * This routine is invoked to release an xri to the pool of
14451 * available rpis maintained by the driver.
14452 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014453static void
James Smart6d368e52011-05-24 11:44:12 -040014454__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14455{
14456 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014457 phba->sli4_hba.max_cfg_param.xri_used--;
14458 }
14459}
14460
14461/**
14462 * lpfc_sli4_free_xri - Release an xri for reuse.
14463 * @phba: pointer to lpfc hba data structure.
14464 *
14465 * This routine is invoked to release an xri to the pool of
14466 * available rpis maintained by the driver.
14467 **/
14468void
14469lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14470{
14471 spin_lock_irq(&phba->hbalock);
14472 __lpfc_sli4_free_xri(phba, xri);
14473 spin_unlock_irq(&phba->hbalock);
14474}
14475
14476/**
James Smart4f774512009-05-22 14:52:35 -040014477 * lpfc_sli4_next_xritag - Get an xritag for the io
14478 * @phba: Pointer to HBA context object.
14479 *
14480 * This function gets an xritag for the iocb. If there is no unused xritag
14481 * it will return 0xffff.
14482 * The function returns the allocated xritag if successful, else returns zero.
14483 * Zero is not a valid xritag.
14484 * The caller is not required to hold any lock.
14485 **/
14486uint16_t
14487lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14488{
James Smart6d368e52011-05-24 11:44:12 -040014489 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014490
James Smart6d368e52011-05-24 11:44:12 -040014491 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014492 if (xri_index == NO_XRI)
14493 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14494 "2004 Failed to allocate XRI.last XRITAG is %d"
14495 " Max XRI is %d, Used XRI is %d\n",
14496 xri_index,
14497 phba->sli4_hba.max_cfg_param.max_xri,
14498 phba->sli4_hba.max_cfg_param.xri_used);
14499 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014500}
14501
14502/**
James Smart6d368e52011-05-24 11:44:12 -040014503 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014504 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014505 * @post_sgl_list: pointer to els sgl entry list.
14506 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014507 *
14508 * This routine is invoked to post a block of driver's sgl pages to the
14509 * HBA using non-embedded mailbox command. No Lock is held. This routine
14510 * is only called when the driver is loading and after all IO has been
14511 * stopped.
14512 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014513static int
14514lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14515 struct list_head *post_sgl_list,
14516 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014517{
James Smart8a9d2e82012-05-09 21:16:12 -040014518 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014519 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14520 struct sgl_page_pairs *sgl_pg_pairs;
14521 void *viraddr;
14522 LPFC_MBOXQ_t *mbox;
14523 uint32_t reqlen, alloclen, pg_pairs;
14524 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014525 uint16_t xritag_start = 0;
14526 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014527 uint32_t shdr_status, shdr_add_status;
14528 union lpfc_sli4_cfg_shdr *shdr;
14529
James Smart8a9d2e82012-05-09 21:16:12 -040014530 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014531 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014532 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014533 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14534 "2559 Block sgl registration required DMA "
14535 "size (%d) great than a page\n", reqlen);
14536 return -ENOMEM;
14537 }
14538 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014539 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014540 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014541
14542 /* Allocate DMA memory and set up the non-embedded mailbox command */
14543 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14544 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14545 LPFC_SLI4_MBX_NEMBED);
14546
14547 if (alloclen < reqlen) {
14548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14549 "0285 Allocated DMA memory size (%d) is "
14550 "less than the requested DMA memory "
14551 "size (%d)\n", alloclen, reqlen);
14552 lpfc_sli4_mbox_cmd_free(phba, mbox);
14553 return -ENOMEM;
14554 }
James Smart4f774512009-05-22 14:52:35 -040014555 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014556 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014557 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14558 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14559
James Smart8a9d2e82012-05-09 21:16:12 -040014560 pg_pairs = 0;
14561 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014562 /* Set up the sge entry */
14563 sgl_pg_pairs->sgl_pg0_addr_lo =
14564 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14565 sgl_pg_pairs->sgl_pg0_addr_hi =
14566 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14567 sgl_pg_pairs->sgl_pg1_addr_lo =
14568 cpu_to_le32(putPaddrLow(0));
14569 sgl_pg_pairs->sgl_pg1_addr_hi =
14570 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014571
James Smart4f774512009-05-22 14:52:35 -040014572 /* Keep the first xritag on the list */
14573 if (pg_pairs == 0)
14574 xritag_start = sglq_entry->sli4_xritag;
14575 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014576 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014577 }
James Smart6d368e52011-05-24 11:44:12 -040014578
14579 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014580 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014581 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014582 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014583 if (!phba->sli4_hba.intr_enable)
14584 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14585 else {
James Smarta183a152011-10-10 21:32:43 -040014586 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014587 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14588 }
14589 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14590 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14591 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14592 if (rc != MBX_TIMEOUT)
14593 lpfc_sli4_mbox_cmd_free(phba, mbox);
14594 if (shdr_status || shdr_add_status || rc) {
14595 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14596 "2513 POST_SGL_BLOCK mailbox command failed "
14597 "status x%x add_status x%x mbx status x%x\n",
14598 shdr_status, shdr_add_status, rc);
14599 rc = -ENXIO;
14600 }
14601 return rc;
14602}
14603
14604/**
14605 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14606 * @phba: pointer to lpfc hba data structure.
14607 * @sblist: pointer to scsi buffer list.
14608 * @count: number of scsi buffers on the list.
14609 *
14610 * This routine is invoked to post a block of @count scsi sgl pages from a
14611 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14612 * No Lock is held.
14613 *
14614 **/
14615int
James Smart8a9d2e82012-05-09 21:16:12 -040014616lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14617 struct list_head *sblist,
14618 int count)
James Smart4f774512009-05-22 14:52:35 -040014619{
14620 struct lpfc_scsi_buf *psb;
14621 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14622 struct sgl_page_pairs *sgl_pg_pairs;
14623 void *viraddr;
14624 LPFC_MBOXQ_t *mbox;
14625 uint32_t reqlen, alloclen, pg_pairs;
14626 uint32_t mbox_tmo;
14627 uint16_t xritag_start = 0;
14628 int rc = 0;
14629 uint32_t shdr_status, shdr_add_status;
14630 dma_addr_t pdma_phys_bpl1;
14631 union lpfc_sli4_cfg_shdr *shdr;
14632
14633 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014634 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014635 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014636 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014637 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14638 "0217 Block sgl registration required DMA "
14639 "size (%d) great than a page\n", reqlen);
14640 return -ENOMEM;
14641 }
14642 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14643 if (!mbox) {
14644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14645 "0283 Failed to allocate mbox cmd memory\n");
14646 return -ENOMEM;
14647 }
14648
14649 /* Allocate DMA memory and set up the non-embedded mailbox command */
14650 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14651 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14652 LPFC_SLI4_MBX_NEMBED);
14653
14654 if (alloclen < reqlen) {
14655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14656 "2561 Allocated DMA memory size (%d) is "
14657 "less than the requested DMA memory "
14658 "size (%d)\n", alloclen, reqlen);
14659 lpfc_sli4_mbox_cmd_free(phba, mbox);
14660 return -ENOMEM;
14661 }
James Smart6d368e52011-05-24 11:44:12 -040014662
James Smart4f774512009-05-22 14:52:35 -040014663 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014664 viraddr = mbox->sge_array->addr[0];
14665
14666 /* Set up the SGL pages in the non-embedded DMA pages */
14667 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14668 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14669
14670 pg_pairs = 0;
14671 list_for_each_entry(psb, sblist, list) {
14672 /* Set up the sge entry */
14673 sgl_pg_pairs->sgl_pg0_addr_lo =
14674 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14675 sgl_pg_pairs->sgl_pg0_addr_hi =
14676 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14677 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14678 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14679 else
14680 pdma_phys_bpl1 = 0;
14681 sgl_pg_pairs->sgl_pg1_addr_lo =
14682 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14683 sgl_pg_pairs->sgl_pg1_addr_hi =
14684 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14685 /* Keep the first xritag on the list */
14686 if (pg_pairs == 0)
14687 xritag_start = psb->cur_iocbq.sli4_xritag;
14688 sgl_pg_pairs++;
14689 pg_pairs++;
14690 }
14691 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14692 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14693 /* Perform endian conversion if necessary */
14694 sgl->word0 = cpu_to_le32(sgl->word0);
14695
14696 if (!phba->sli4_hba.intr_enable)
14697 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14698 else {
James Smarta183a152011-10-10 21:32:43 -040014699 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014700 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14701 }
14702 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14703 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14704 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14705 if (rc != MBX_TIMEOUT)
14706 lpfc_sli4_mbox_cmd_free(phba, mbox);
14707 if (shdr_status || shdr_add_status || rc) {
14708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14709 "2564 POST_SGL_BLOCK mailbox command failed "
14710 "status x%x add_status x%x mbx status x%x\n",
14711 shdr_status, shdr_add_status, rc);
14712 rc = -ENXIO;
14713 }
14714 return rc;
14715}
14716
14717/**
14718 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14719 * @phba: pointer to lpfc_hba struct that the frame was received on
14720 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14721 *
14722 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14723 * valid type of frame that the LPFC driver will handle. This function will
14724 * return a zero if the frame is a valid frame or a non zero value when the
14725 * frame does not pass the check.
14726 **/
14727static int
14728lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14729{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014730 /* make rctl_names static to save stack space */
14731 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014732 char *type_names[] = FC_TYPE_NAMES_INIT;
14733 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014734 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014735
14736 switch (fc_hdr->fh_r_ctl) {
14737 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14738 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14739 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14740 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14741 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14742 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14743 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14744 case FC_RCTL_DD_CMD_STATUS: /* command status */
14745 case FC_RCTL_ELS_REQ: /* extended link services request */
14746 case FC_RCTL_ELS_REP: /* extended link services reply */
14747 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14748 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14749 case FC_RCTL_BA_NOP: /* basic link service NOP */
14750 case FC_RCTL_BA_ABTS: /* basic link service abort */
14751 case FC_RCTL_BA_RMC: /* remove connection */
14752 case FC_RCTL_BA_ACC: /* basic accept */
14753 case FC_RCTL_BA_RJT: /* basic reject */
14754 case FC_RCTL_BA_PRMT:
14755 case FC_RCTL_ACK_1: /* acknowledge_1 */
14756 case FC_RCTL_ACK_0: /* acknowledge_0 */
14757 case FC_RCTL_P_RJT: /* port reject */
14758 case FC_RCTL_F_RJT: /* fabric reject */
14759 case FC_RCTL_P_BSY: /* port busy */
14760 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14761 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14762 case FC_RCTL_LCR: /* link credit reset */
14763 case FC_RCTL_END: /* end */
14764 break;
14765 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14766 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14767 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14768 return lpfc_fc_frame_check(phba, fc_hdr);
14769 default:
14770 goto drop;
14771 }
14772 switch (fc_hdr->fh_type) {
14773 case FC_TYPE_BLS:
14774 case FC_TYPE_ELS:
14775 case FC_TYPE_FCP:
14776 case FC_TYPE_CT:
14777 break;
14778 case FC_TYPE_IP:
14779 case FC_TYPE_ILS:
14780 default:
14781 goto drop;
14782 }
James Smart546fc852011-03-11 16:06:29 -050014783
James Smart4f774512009-05-22 14:52:35 -040014784 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014785 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14786 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14787 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14788 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014789 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14790 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014791 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14792 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014793 return 0;
14794drop:
14795 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14796 "2539 Dropped frame rctl:%s type:%s\n",
14797 rctl_names[fc_hdr->fh_r_ctl],
14798 type_names[fc_hdr->fh_type]);
14799 return 1;
14800}
14801
14802/**
14803 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14804 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14805 *
14806 * This function processes the FC header to retrieve the VFI from the VF
14807 * header, if one exists. This function will return the VFI if one exists
14808 * or 0 if no VSAN Header exists.
14809 **/
14810static uint32_t
14811lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14812{
14813 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14814
14815 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14816 return 0;
14817 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14818}
14819
14820/**
14821 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14822 * @phba: Pointer to the HBA structure to search for the vport on
14823 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14824 * @fcfi: The FC Fabric ID that the frame came from
14825 *
14826 * This function searches the @phba for a vport that matches the content of the
14827 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14828 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14829 * returns the matching vport pointer or NULL if unable to match frame to a
14830 * vport.
14831 **/
14832static struct lpfc_vport *
14833lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14834 uint16_t fcfi)
14835{
14836 struct lpfc_vport **vports;
14837 struct lpfc_vport *vport = NULL;
14838 int i;
14839 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14840 fc_hdr->fh_d_id[1] << 8 |
14841 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014842
James Smartbf086112011-08-21 21:48:13 -040014843 if (did == Fabric_DID)
14844 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014845 if ((phba->pport->fc_flag & FC_PT2PT) &&
14846 !(phba->link_state == LPFC_HBA_READY))
14847 return phba->pport;
14848
James Smart4f774512009-05-22 14:52:35 -040014849 vports = lpfc_create_vport_work_array(phba);
14850 if (vports != NULL)
14851 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14852 if (phba->fcf.fcfi == fcfi &&
14853 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14854 vports[i]->fc_myDID == did) {
14855 vport = vports[i];
14856 break;
14857 }
14858 }
14859 lpfc_destroy_vport_work_array(phba, vports);
14860 return vport;
14861}
14862
14863/**
James Smart45ed1192009-10-02 15:17:02 -040014864 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14865 * @vport: The vport to work on.
14866 *
14867 * This function updates the receive sequence time stamp for this vport. The
14868 * receive sequence time stamp indicates the time that the last frame of the
14869 * the sequence that has been idle for the longest amount of time was received.
14870 * the driver uses this time stamp to indicate if any received sequences have
14871 * timed out.
14872 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014873static void
James Smart45ed1192009-10-02 15:17:02 -040014874lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14875{
14876 struct lpfc_dmabuf *h_buf;
14877 struct hbq_dmabuf *dmabuf = NULL;
14878
14879 /* get the oldest sequence on the rcv list */
14880 h_buf = list_get_first(&vport->rcv_buffer_list,
14881 struct lpfc_dmabuf, list);
14882 if (!h_buf)
14883 return;
14884 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14885 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14886}
14887
14888/**
14889 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14890 * @vport: The vport that the received sequences were sent to.
14891 *
14892 * This function cleans up all outstanding received sequences. This is called
14893 * by the driver when a link event or user action invalidates all the received
14894 * sequences.
14895 **/
14896void
14897lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14898{
14899 struct lpfc_dmabuf *h_buf, *hnext;
14900 struct lpfc_dmabuf *d_buf, *dnext;
14901 struct hbq_dmabuf *dmabuf = NULL;
14902
14903 /* start with the oldest sequence on the rcv list */
14904 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14905 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14906 list_del_init(&dmabuf->hbuf.list);
14907 list_for_each_entry_safe(d_buf, dnext,
14908 &dmabuf->dbuf.list, list) {
14909 list_del_init(&d_buf->list);
14910 lpfc_in_buf_free(vport->phba, d_buf);
14911 }
14912 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14913 }
14914}
14915
14916/**
14917 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14918 * @vport: The vport that the received sequences were sent to.
14919 *
14920 * This function determines whether any received sequences have timed out by
14921 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14922 * indicates that there is at least one timed out sequence this routine will
14923 * go through the received sequences one at a time from most inactive to most
14924 * active to determine which ones need to be cleaned up. Once it has determined
14925 * that a sequence needs to be cleaned up it will simply free up the resources
14926 * without sending an abort.
14927 **/
14928void
14929lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14930{
14931 struct lpfc_dmabuf *h_buf, *hnext;
14932 struct lpfc_dmabuf *d_buf, *dnext;
14933 struct hbq_dmabuf *dmabuf = NULL;
14934 unsigned long timeout;
14935 int abort_count = 0;
14936
14937 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14938 vport->rcv_buffer_time_stamp);
14939 if (list_empty(&vport->rcv_buffer_list) ||
14940 time_before(jiffies, timeout))
14941 return;
14942 /* start with the oldest sequence on the rcv list */
14943 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14944 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14945 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14946 dmabuf->time_stamp);
14947 if (time_before(jiffies, timeout))
14948 break;
14949 abort_count++;
14950 list_del_init(&dmabuf->hbuf.list);
14951 list_for_each_entry_safe(d_buf, dnext,
14952 &dmabuf->dbuf.list, list) {
14953 list_del_init(&d_buf->list);
14954 lpfc_in_buf_free(vport->phba, d_buf);
14955 }
14956 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14957 }
14958 if (abort_count)
14959 lpfc_update_rcv_time_stamp(vport);
14960}
14961
14962/**
James Smart4f774512009-05-22 14:52:35 -040014963 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14964 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14965 *
14966 * This function searches through the existing incomplete sequences that have
14967 * been sent to this @vport. If the frame matches one of the incomplete
14968 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14969 * make up that sequence. If no sequence is found that matches this frame then
14970 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14971 * This function returns a pointer to the first dmabuf in the sequence list that
14972 * the frame was linked to.
14973 **/
14974static struct hbq_dmabuf *
14975lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14976{
14977 struct fc_frame_header *new_hdr;
14978 struct fc_frame_header *temp_hdr;
14979 struct lpfc_dmabuf *d_buf;
14980 struct lpfc_dmabuf *h_buf;
14981 struct hbq_dmabuf *seq_dmabuf = NULL;
14982 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050014983 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040014984
James Smart4d9ab992009-10-02 15:16:39 -040014985 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014986 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014987 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050014988
James Smart4f774512009-05-22 14:52:35 -040014989 /* Use the hdr_buf to find the sequence that this frame belongs to */
14990 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14991 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14992 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14993 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14994 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14995 continue;
14996 /* found a pending sequence that matches this frame */
14997 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14998 break;
14999 }
15000 if (!seq_dmabuf) {
15001 /*
15002 * This indicates first frame received for this sequence.
15003 * Queue the buffer on the vport's rcv_buffer_list.
15004 */
15005 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015006 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015007 return dmabuf;
15008 }
15009 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015010 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15011 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015012 list_del_init(&seq_dmabuf->hbuf.list);
15013 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15014 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015015 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015016 return dmabuf;
15017 }
James Smart45ed1192009-10-02 15:17:02 -040015018 /* move this sequence to the tail to indicate a young sequence */
15019 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15020 seq_dmabuf->time_stamp = jiffies;
15021 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015022 if (list_empty(&seq_dmabuf->dbuf.list)) {
15023 temp_hdr = dmabuf->hbuf.virt;
15024 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15025 return seq_dmabuf;
15026 }
James Smart4f774512009-05-22 14:52:35 -040015027 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015028 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15029 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015030 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15031 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15032 /*
15033 * If the frame's sequence count is greater than the frame on
15034 * the list then insert the frame right after this frame
15035 */
James Smarteeead812009-12-21 17:01:23 -050015036 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15037 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015038 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015039 found = 1;
15040 break;
James Smart4f774512009-05-22 14:52:35 -040015041 }
James Smart4360ca92015-12-16 18:12:04 -050015042
15043 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15044 break;
15045 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015046 }
James Smart4360ca92015-12-16 18:12:04 -050015047
15048 if (found)
15049 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015050 return NULL;
15051}
15052
15053/**
James Smart6669f9b2009-10-02 15:16:45 -040015054 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15055 * @vport: pointer to a vitural port
15056 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15057 *
15058 * This function tries to abort from the partially assembed sequence, described
15059 * by the information from basic abbort @dmabuf. It checks to see whether such
15060 * partially assembled sequence held by the driver. If so, it shall free up all
15061 * the frames from the partially assembled sequence.
15062 *
15063 * Return
15064 * true -- if there is matching partially assembled sequence present and all
15065 * the frames freed with the sequence;
15066 * false -- if there is no matching partially assembled sequence present so
15067 * nothing got aborted in the lower layer driver
15068 **/
15069static bool
15070lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15071 struct hbq_dmabuf *dmabuf)
15072{
15073 struct fc_frame_header *new_hdr;
15074 struct fc_frame_header *temp_hdr;
15075 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15076 struct hbq_dmabuf *seq_dmabuf = NULL;
15077
15078 /* Use the hdr_buf to find the sequence that matches this frame */
15079 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15080 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15081 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15082 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15083 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15084 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15085 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15086 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15087 continue;
15088 /* found a pending sequence that matches this frame */
15089 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15090 break;
15091 }
15092
15093 /* Free up all the frames from the partially assembled sequence */
15094 if (seq_dmabuf) {
15095 list_for_each_entry_safe(d_buf, n_buf,
15096 &seq_dmabuf->dbuf.list, list) {
15097 list_del_init(&d_buf->list);
15098 lpfc_in_buf_free(vport->phba, d_buf);
15099 }
15100 return true;
15101 }
15102 return false;
15103}
15104
15105/**
James Smart6dd9e312013-01-03 15:43:37 -050015106 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15107 * @vport: pointer to a vitural port
15108 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15109 *
15110 * This function tries to abort from the assembed sequence from upper level
15111 * protocol, described by the information from basic abbort @dmabuf. It
15112 * checks to see whether such pending context exists at upper level protocol.
15113 * If so, it shall clean up the pending context.
15114 *
15115 * Return
15116 * true -- if there is matching pending context of the sequence cleaned
15117 * at ulp;
15118 * false -- if there is no matching pending context of the sequence present
15119 * at ulp.
15120 **/
15121static bool
15122lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15123{
15124 struct lpfc_hba *phba = vport->phba;
15125 int handled;
15126
15127 /* Accepting abort at ulp with SLI4 only */
15128 if (phba->sli_rev < LPFC_SLI_REV4)
15129 return false;
15130
15131 /* Register all caring upper level protocols to attend abort */
15132 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15133 if (handled)
15134 return true;
15135
15136 return false;
15137}
15138
15139/**
James Smart546fc852011-03-11 16:06:29 -050015140 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015141 * @phba: Pointer to HBA context object.
15142 * @cmd_iocbq: pointer to the command iocbq structure.
15143 * @rsp_iocbq: pointer to the response iocbq structure.
15144 *
James Smart546fc852011-03-11 16:06:29 -050015145 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015146 * event. It properly releases the memory allocated to the sequence abort
15147 * accept iocb.
15148 **/
15149static void
James Smart546fc852011-03-11 16:06:29 -050015150lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015151 struct lpfc_iocbq *cmd_iocbq,
15152 struct lpfc_iocbq *rsp_iocbq)
15153{
James Smart6dd9e312013-01-03 15:43:37 -050015154 struct lpfc_nodelist *ndlp;
15155
15156 if (cmd_iocbq) {
15157 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15158 lpfc_nlp_put(ndlp);
15159 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015160 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015161 }
James Smart6b5151f2012-01-18 16:24:06 -050015162
15163 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15164 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15166 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15167 rsp_iocbq->iocb.ulpStatus,
15168 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015169}
15170
15171/**
James Smart6d368e52011-05-24 11:44:12 -040015172 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15173 * @phba: Pointer to HBA context object.
15174 * @xri: xri id in transaction.
15175 *
15176 * This function validates the xri maps to the known range of XRIs allocated an
15177 * used by the driver.
15178 **/
James Smart7851fe22011-07-22 18:36:52 -040015179uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015180lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15181 uint16_t xri)
15182{
James Smarta2fc4aef2014-09-03 12:57:55 -040015183 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015184
15185 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15186 if (xri == phba->sli4_hba.xri_ids[i])
15187 return i;
15188 }
15189 return NO_XRI;
15190}
15191
James Smart6d368e52011-05-24 11:44:12 -040015192/**
James Smart546fc852011-03-11 16:06:29 -050015193 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015194 * @phba: Pointer to HBA context object.
15195 * @fc_hdr: pointer to a FC frame header.
15196 *
James Smart546fc852011-03-11 16:06:29 -050015197 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015198 * event after aborting the sequence handling.
15199 **/
15200static void
James Smart6dd9e312013-01-03 15:43:37 -050015201lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15202 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015203{
James Smart6dd9e312013-01-03 15:43:37 -050015204 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015205 struct lpfc_iocbq *ctiocb = NULL;
15206 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015207 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015208 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015209 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015210 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015211
15212 if (!lpfc_is_link_up(phba))
15213 return;
15214
15215 sid = sli4_sid_from_fc_hdr(fc_hdr);
15216 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015217 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015218
James Smart6dd9e312013-01-03 15:43:37 -050015219 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015220 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015221 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15222 if (!ndlp) {
15223 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15224 "1268 Failed to allocate ndlp for "
15225 "oxid:x%x SID:x%x\n", oxid, sid);
15226 return;
15227 }
15228 lpfc_nlp_init(vport, ndlp, sid);
15229 /* Put ndlp onto pport node list */
15230 lpfc_enqueue_node(vport, ndlp);
15231 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15232 /* re-setup ndlp without removing from node list */
15233 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15234 if (!ndlp) {
15235 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15236 "3275 Failed to active ndlp found "
15237 "for oxid:x%x SID:x%x\n", oxid, sid);
15238 return;
15239 }
James Smart6669f9b2009-10-02 15:16:45 -040015240 }
15241
James Smart546fc852011-03-11 16:06:29 -050015242 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015243 ctiocb = lpfc_sli_get_iocbq(phba);
15244 if (!ctiocb)
15245 return;
15246
James Smart5ffc2662009-11-18 15:39:44 -050015247 /* Extract the F_CTL field from FC_HDR */
15248 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15249
James Smart6669f9b2009-10-02 15:16:45 -040015250 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015251 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015252 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015253 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15254 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15255 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15256
15257 /* Fill in the rest of iocb fields */
15258 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15259 icmd->ulpBdeCount = 0;
15260 icmd->ulpLe = 1;
15261 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015262 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015263 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015264
James Smart6669f9b2009-10-02 15:16:45 -040015265 ctiocb->iocb_cmpl = NULL;
15266 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015267 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015268 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015269 ctiocb->sli4_xritag = NO_XRI;
15270
James Smartee0f4fe2012-05-09 21:19:14 -040015271 if (fctl & FC_FC_EX_CTX)
15272 /* Exchange responder sent the abort so we
15273 * own the oxid.
15274 */
15275 xri = oxid;
15276 else
15277 xri = rxid;
15278 lxri = lpfc_sli4_xri_inrange(phba, xri);
15279 if (lxri != NO_XRI)
15280 lpfc_set_rrq_active(phba, ndlp, lxri,
15281 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015282 /* For BA_ABTS from exchange responder, if the logical xri with
15283 * the oxid maps to the FCP XRI range, the port no longer has
15284 * that exchange context, send a BLS_RJT. Override the IOCB for
15285 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015286 */
James Smart6dd9e312013-01-03 15:43:37 -050015287 if ((fctl & FC_FC_EX_CTX) &&
15288 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15289 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15290 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15291 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15292 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15293 }
15294
15295 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15296 * the driver no longer has that exchange, send a BLS_RJT. Override
15297 * the IOCB for a BA_RJT.
15298 */
15299 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015300 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15301 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15302 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15303 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15304 }
James Smart6669f9b2009-10-02 15:16:45 -040015305
James Smart5ffc2662009-11-18 15:39:44 -050015306 if (fctl & FC_FC_EX_CTX) {
15307 /* ABTS sent by responder to CT exchange, construction
15308 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15309 * field and RX_ID from ABTS for RX_ID field.
15310 */
James Smart546fc852011-03-11 16:06:29 -050015311 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015312 } else {
15313 /* ABTS sent by initiator to CT exchange, construction
15314 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015315 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015316 */
James Smart546fc852011-03-11 16:06:29 -050015317 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015318 }
James Smartf09c3ac2012-03-01 22:33:29 -050015319 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015320 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015321
James Smart546fc852011-03-11 16:06:29 -050015322 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015323 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15324 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15325 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015326
15327 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15328 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015329 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15330 "2925 Failed to issue CT ABTS RSP x%x on "
15331 "xri x%x, Data x%x\n",
15332 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15333 phba->link_state);
15334 lpfc_nlp_put(ndlp);
15335 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015336 lpfc_sli_release_iocbq(phba, ctiocb);
15337 }
James Smart6669f9b2009-10-02 15:16:45 -040015338}
15339
15340/**
15341 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15342 * @vport: Pointer to the vport on which this sequence was received
15343 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15344 *
15345 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15346 * receive sequence is only partially assembed by the driver, it shall abort
15347 * the partially assembled frames for the sequence. Otherwise, if the
15348 * unsolicited receive sequence has been completely assembled and passed to
15349 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15350 * unsolicited sequence has been aborted. After that, it will issue a basic
15351 * accept to accept the abort.
15352 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015353static void
James Smart6669f9b2009-10-02 15:16:45 -040015354lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15355 struct hbq_dmabuf *dmabuf)
15356{
15357 struct lpfc_hba *phba = vport->phba;
15358 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015359 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015360 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015361
James Smart6669f9b2009-10-02 15:16:45 -040015362 /* Make a copy of fc_hdr before the dmabuf being released */
15363 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015364 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015365
James Smart5ffc2662009-11-18 15:39:44 -050015366 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015367 /* ABTS by responder to exchange, no cleanup needed */
15368 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015369 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015370 /* ABTS by initiator to exchange, need to do cleanup */
15371 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15372 if (aborted == false)
15373 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015374 }
James Smart6dd9e312013-01-03 15:43:37 -050015375 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15376
15377 /* Respond with BA_ACC or BA_RJT accordingly */
15378 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015379}
15380
15381/**
James Smart4f774512009-05-22 14:52:35 -040015382 * lpfc_seq_complete - Indicates if a sequence is complete
15383 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15384 *
15385 * This function checks the sequence, starting with the frame described by
15386 * @dmabuf, to see if all the frames associated with this sequence are present.
15387 * the frames associated with this sequence are linked to the @dmabuf using the
15388 * dbuf list. This function looks for two major things. 1) That the first frame
15389 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15390 * set. 3) That there are no holes in the sequence count. The function will
15391 * return 1 when the sequence is complete, otherwise it will return 0.
15392 **/
15393static int
15394lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15395{
15396 struct fc_frame_header *hdr;
15397 struct lpfc_dmabuf *d_buf;
15398 struct hbq_dmabuf *seq_dmabuf;
15399 uint32_t fctl;
15400 int seq_count = 0;
15401
15402 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15403 /* make sure first fame of sequence has a sequence count of zero */
15404 if (hdr->fh_seq_cnt != seq_count)
15405 return 0;
15406 fctl = (hdr->fh_f_ctl[0] << 16 |
15407 hdr->fh_f_ctl[1] << 8 |
15408 hdr->fh_f_ctl[2]);
15409 /* If last frame of sequence we can return success. */
15410 if (fctl & FC_FC_END_SEQ)
15411 return 1;
15412 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15413 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15414 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15415 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015416 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015417 return 0;
15418 fctl = (hdr->fh_f_ctl[0] << 16 |
15419 hdr->fh_f_ctl[1] << 8 |
15420 hdr->fh_f_ctl[2]);
15421 /* If last frame of sequence we can return success. */
15422 if (fctl & FC_FC_END_SEQ)
15423 return 1;
15424 }
15425 return 0;
15426}
15427
15428/**
15429 * lpfc_prep_seq - Prep sequence for ULP processing
15430 * @vport: Pointer to the vport on which this sequence was received
15431 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15432 *
15433 * This function takes a sequence, described by a list of frames, and creates
15434 * a list of iocbq structures to describe the sequence. This iocbq list will be
15435 * used to issue to the generic unsolicited sequence handler. This routine
15436 * returns a pointer to the first iocbq in the list. If the function is unable
15437 * to allocate an iocbq then it throw out the received frames that were not
15438 * able to be described and return a pointer to the first iocbq. If unable to
15439 * allocate any iocbqs (including the first) this function will return NULL.
15440 **/
15441static struct lpfc_iocbq *
15442lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15443{
James Smart7851fe22011-07-22 18:36:52 -040015444 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015445 struct lpfc_dmabuf *d_buf, *n_buf;
15446 struct lpfc_iocbq *first_iocbq, *iocbq;
15447 struct fc_frame_header *fc_hdr;
15448 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015449 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015450 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015451
15452 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15453 /* remove from receive buffer list */
15454 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015455 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015456 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015457 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015458 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015459 /* Get an iocbq struct to fill in. */
15460 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15461 if (first_iocbq) {
15462 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015463 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015464 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015465
15466 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15467 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15468 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15469 first_iocbq->iocb.un.rcvels.parmRo =
15470 sli4_did_from_fc_hdr(fc_hdr);
15471 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15472 } else
15473 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015474 first_iocbq->iocb.ulpContext = NO_XRI;
15475 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15476 be16_to_cpu(fc_hdr->fh_ox_id);
15477 /* iocbq is prepped for internal consumption. Physical vpi. */
15478 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15479 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015480 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015481 tot_len = bf_get(lpfc_rcqe_length,
15482 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15483
James Smart4f774512009-05-22 14:52:35 -040015484 first_iocbq->context2 = &seq_dmabuf->dbuf;
15485 first_iocbq->context3 = NULL;
15486 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015487 if (tot_len > LPFC_DATA_BUF_SIZE)
15488 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015489 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015490 else
15491 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15492
James Smart4f774512009-05-22 14:52:35 -040015493 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015494
James Smart7851fe22011-07-22 18:36:52 -040015495 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015496 }
15497 iocbq = first_iocbq;
15498 /*
15499 * Each IOCBq can have two Buffers assigned, so go through the list
15500 * of buffers for this sequence and save two buffers in each IOCBq
15501 */
15502 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15503 if (!iocbq) {
15504 lpfc_in_buf_free(vport->phba, d_buf);
15505 continue;
15506 }
15507 if (!iocbq->context3) {
15508 iocbq->context3 = d_buf;
15509 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015510 /* We need to get the size out of the right CQE */
15511 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15512 len = bf_get(lpfc_rcqe_length,
15513 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015514 pbde = (struct ulp_bde64 *)
15515 &iocbq->iocb.unsli3.sli3Words[4];
15516 if (len > LPFC_DATA_BUF_SIZE)
15517 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15518 else
15519 pbde->tus.f.bdeSize = len;
15520
James Smart7851fe22011-07-22 18:36:52 -040015521 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15522 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015523 } else {
15524 iocbq = lpfc_sli_get_iocbq(vport->phba);
15525 if (!iocbq) {
15526 if (first_iocbq) {
15527 first_iocbq->iocb.ulpStatus =
15528 IOSTAT_FCP_RSP_ERROR;
15529 first_iocbq->iocb.un.ulpWord[4] =
15530 IOERR_NO_RESOURCES;
15531 }
15532 lpfc_in_buf_free(vport->phba, d_buf);
15533 continue;
15534 }
James Smart7851fe22011-07-22 18:36:52 -040015535 /* We need to get the size out of the right CQE */
15536 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15537 len = bf_get(lpfc_rcqe_length,
15538 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015539 iocbq->context2 = d_buf;
15540 iocbq->context3 = NULL;
15541 iocbq->iocb.ulpBdeCount = 1;
15542 if (len > LPFC_DATA_BUF_SIZE)
15543 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15544 LPFC_DATA_BUF_SIZE;
15545 else
15546 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15547
James Smart7851fe22011-07-22 18:36:52 -040015548 tot_len += len;
15549 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15550
James Smart4f774512009-05-22 14:52:35 -040015551 iocbq->iocb.un.rcvels.remoteID = sid;
15552 list_add_tail(&iocbq->list, &first_iocbq->list);
15553 }
15554 }
15555 return first_iocbq;
15556}
15557
James Smart6669f9b2009-10-02 15:16:45 -040015558static void
15559lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15560 struct hbq_dmabuf *seq_dmabuf)
15561{
15562 struct fc_frame_header *fc_hdr;
15563 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15564 struct lpfc_hba *phba = vport->phba;
15565
15566 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15567 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15568 if (!iocbq) {
15569 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15570 "2707 Ring %d handler: Failed to allocate "
15571 "iocb Rctl x%x Type x%x received\n",
15572 LPFC_ELS_RING,
15573 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15574 return;
15575 }
15576 if (!lpfc_complete_unsol_iocb(phba,
15577 &phba->sli.ring[LPFC_ELS_RING],
15578 iocbq, fc_hdr->fh_r_ctl,
15579 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015580 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015581 "2540 Ring %d handler: unexpected Rctl "
15582 "x%x Type x%x received\n",
15583 LPFC_ELS_RING,
15584 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15585
15586 /* Free iocb created in lpfc_prep_seq */
15587 list_for_each_entry_safe(curr_iocb, next_iocb,
15588 &iocbq->list, list) {
15589 list_del_init(&curr_iocb->list);
15590 lpfc_sli_release_iocbq(phba, curr_iocb);
15591 }
15592 lpfc_sli_release_iocbq(phba, iocbq);
15593}
15594
James Smart4f774512009-05-22 14:52:35 -040015595/**
15596 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15597 * @phba: Pointer to HBA context object.
15598 *
15599 * This function is called with no lock held. This function processes all
15600 * the received buffers and gives it to upper layers when a received buffer
15601 * indicates that it is the final frame in the sequence. The interrupt
15602 * service routine processes received buffers at interrupt contexts and adds
15603 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15604 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15605 * appropriate receive function when the final frame in a sequence is received.
15606 **/
James Smart4d9ab992009-10-02 15:16:39 -040015607void
15608lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15609 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015610{
James Smart4d9ab992009-10-02 15:16:39 -040015611 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015612 struct fc_frame_header *fc_hdr;
15613 struct lpfc_vport *vport;
15614 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015615 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015616
James Smart4f774512009-05-22 14:52:35 -040015617 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015618 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15619 /* check to see if this a valid type of frame */
15620 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15621 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15622 return;
15623 }
James Smart7851fe22011-07-22 18:36:52 -040015624 if ((bf_get(lpfc_cqe_code,
15625 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15626 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15627 &dmabuf->cq_event.cqe.rcqe_cmpl);
15628 else
15629 fcfi = bf_get(lpfc_rcqe_fcf_id,
15630 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015631
James Smart4d9ab992009-10-02 15:16:39 -040015632 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015633 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015634 /* throw out the frame */
15635 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15636 return;
15637 }
James Smart939723a2012-05-09 21:19:03 -040015638
15639 /* d_id this frame is directed to */
15640 did = sli4_did_from_fc_hdr(fc_hdr);
15641
15642 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15643 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15644 (did != Fabric_DID)) {
15645 /*
15646 * Throw out the frame if we are not pt2pt.
15647 * The pt2pt protocol allows for discovery frames
15648 * to be received without a registered VPI.
15649 */
15650 if (!(vport->fc_flag & FC_PT2PT) ||
15651 (phba->link_state == LPFC_HBA_READY)) {
15652 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15653 return;
15654 }
15655 }
15656
James Smart6669f9b2009-10-02 15:16:45 -040015657 /* Handle the basic abort sequence (BA_ABTS) event */
15658 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15659 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15660 return;
15661 }
15662
James Smart4d9ab992009-10-02 15:16:39 -040015663 /* Link this frame */
15664 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15665 if (!seq_dmabuf) {
15666 /* unable to add frame to vport - throw it out */
15667 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15668 return;
15669 }
15670 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015671 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015672 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015673
James Smart6669f9b2009-10-02 15:16:45 -040015674 /* Send the complete sequence to the upper layer protocol */
15675 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015676}
James Smart6fb120a2009-05-22 14:52:59 -040015677
15678/**
15679 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15680 * @phba: pointer to lpfc hba data structure.
15681 *
15682 * This routine is invoked to post rpi header templates to the
15683 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015684 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15685 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015686 *
15687 * This routine does not require any locks. It's usage is expected
15688 * to be driver load or reset recovery when the driver is
15689 * sequential.
15690 *
15691 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015692 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015693 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015694 * When this error occurs, the driver is not guaranteed
15695 * to have any rpi regions posted to the device and
15696 * must either attempt to repost the regions or take a
15697 * fatal error.
15698 **/
15699int
15700lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15701{
15702 struct lpfc_rpi_hdr *rpi_page;
15703 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015704 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015705
James Smart6d368e52011-05-24 11:44:12 -040015706 /* SLI4 ports that support extents do not require RPI headers. */
15707 if (!phba->sli4_hba.rpi_hdrs_in_use)
15708 goto exit;
15709 if (phba->sli4_hba.extents_in_use)
15710 return -EIO;
15711
James Smart6fb120a2009-05-22 14:52:59 -040015712 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015713 /*
15714 * Assign the rpi headers a physical rpi only if the driver
15715 * has not initialized those resources. A port reset only
15716 * needs the headers posted.
15717 */
15718 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15719 LPFC_RPI_RSRC_RDY)
15720 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15721
James Smart6fb120a2009-05-22 14:52:59 -040015722 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15723 if (rc != MBX_SUCCESS) {
15724 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15725 "2008 Error %d posting all rpi "
15726 "headers\n", rc);
15727 rc = -EIO;
15728 break;
15729 }
15730 }
15731
James Smart6d368e52011-05-24 11:44:12 -040015732 exit:
15733 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15734 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015735 return rc;
15736}
15737
15738/**
15739 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15740 * @phba: pointer to lpfc hba data structure.
15741 * @rpi_page: pointer to the rpi memory region.
15742 *
15743 * This routine is invoked to post a single rpi header to the
15744 * HBA consistent with the SLI-4 interface spec. This memory region
15745 * maps up to 64 rpi context regions.
15746 *
15747 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015748 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015749 * -ENOMEM - No available memory
15750 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015751 **/
15752int
15753lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15754{
15755 LPFC_MBOXQ_t *mboxq;
15756 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15757 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015758 uint32_t shdr_status, shdr_add_status;
15759 union lpfc_sli4_cfg_shdr *shdr;
15760
James Smart6d368e52011-05-24 11:44:12 -040015761 /* SLI4 ports that support extents do not require RPI headers. */
15762 if (!phba->sli4_hba.rpi_hdrs_in_use)
15763 return rc;
15764 if (phba->sli4_hba.extents_in_use)
15765 return -EIO;
15766
James Smart6fb120a2009-05-22 14:52:59 -040015767 /* The port is notified of the header region via a mailbox command. */
15768 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15769 if (!mboxq) {
15770 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15771 "2001 Unable to allocate memory for issuing "
15772 "SLI_CONFIG_SPECIAL mailbox command\n");
15773 return -ENOMEM;
15774 }
15775
15776 /* Post all rpi memory regions to the port. */
15777 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015778 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15779 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15780 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015781 sizeof(struct lpfc_sli4_cfg_mhdr),
15782 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015783
15784
15785 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015786 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15787 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015788 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15789 hdr_tmpl, rpi_page->page_count);
15790
James Smart6fb120a2009-05-22 14:52:59 -040015791 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15792 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015793 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015794 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15795 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15796 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15797 if (rc != MBX_TIMEOUT)
15798 mempool_free(mboxq, phba->mbox_mem_pool);
15799 if (shdr_status || shdr_add_status || rc) {
15800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15801 "2514 POST_RPI_HDR mailbox failed with "
15802 "status x%x add_status x%x, mbx status x%x\n",
15803 shdr_status, shdr_add_status, rc);
15804 rc = -ENXIO;
15805 }
15806 return rc;
15807}
15808
15809/**
15810 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15811 * @phba: pointer to lpfc hba data structure.
15812 *
15813 * This routine is invoked to post rpi header templates to the
15814 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015815 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15816 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015817 *
15818 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015819 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015820 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15821 **/
15822int
15823lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15824{
James Smart6d368e52011-05-24 11:44:12 -040015825 unsigned long rpi;
15826 uint16_t max_rpi, rpi_limit;
15827 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015828 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015829 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015830
James Smart6fb120a2009-05-22 14:52:59 -040015831 /*
James Smart6d368e52011-05-24 11:44:12 -040015832 * Fetch the next logical rpi. Because this index is logical,
15833 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015834 */
James Smart4902b382013-10-10 12:20:35 -040015835 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015836 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15837 rpi_limit = phba->sli4_hba.next_rpi;
15838
James Smart6d368e52011-05-24 11:44:12 -040015839 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15840 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015841 rpi = LPFC_RPI_ALLOC_ERROR;
15842 else {
15843 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15844 phba->sli4_hba.max_cfg_param.rpi_used++;
15845 phba->sli4_hba.rpi_count++;
15846 }
James Smartbe6bb942015-04-07 15:07:22 -040015847 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15848 "0001 rpi:%x max:%x lim:%x\n",
15849 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015850
15851 /*
15852 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015853 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015854 */
15855 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15856 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015857 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015858 return rpi;
15859 }
15860
15861 /*
James Smart6d368e52011-05-24 11:44:12 -040015862 * RPI header postings are not required for SLI4 ports capable of
15863 * extents.
15864 */
15865 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015866 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015867 return rpi;
15868 }
15869
15870 /*
James Smart6fb120a2009-05-22 14:52:59 -040015871 * If the driver is running low on rpi resources, allocate another
15872 * page now. Note that the next_rpi value is used because
15873 * it represents how many are actually in use whereas max_rpi notes
15874 * how many are supported max by the device.
15875 */
James Smart6d368e52011-05-24 11:44:12 -040015876 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015877 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015878 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15879 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15880 if (!rpi_hdr) {
15881 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15882 "2002 Error Could not grow rpi "
15883 "count\n");
15884 } else {
James Smart6d368e52011-05-24 11:44:12 -040015885 lrpi = rpi_hdr->start_rpi;
15886 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015887 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15888 }
15889 }
15890
15891 return rpi;
15892}
15893
15894/**
15895 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15896 * @phba: pointer to lpfc hba data structure.
15897 *
15898 * This routine is invoked to release an rpi to the pool of
15899 * available rpis maintained by the driver.
15900 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015901static void
James Smartd7c47992010-06-08 18:31:54 -040015902__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15903{
15904 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15905 phba->sli4_hba.rpi_count--;
15906 phba->sli4_hba.max_cfg_param.rpi_used--;
15907 }
15908}
15909
15910/**
15911 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15912 * @phba: pointer to lpfc hba data structure.
15913 *
15914 * This routine is invoked to release an rpi to the pool of
15915 * available rpis maintained by the driver.
15916 **/
15917void
James Smart6fb120a2009-05-22 14:52:59 -040015918lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15919{
15920 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015921 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015922 spin_unlock_irq(&phba->hbalock);
15923}
15924
15925/**
15926 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15927 * @phba: pointer to lpfc hba data structure.
15928 *
15929 * This routine is invoked to remove the memory region that
15930 * provided rpi via a bitmask.
15931 **/
15932void
15933lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15934{
15935 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040015936 kfree(phba->sli4_hba.rpi_ids);
15937 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040015938}
15939
15940/**
15941 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15942 * @phba: pointer to lpfc hba data structure.
15943 *
15944 * This routine is invoked to remove the memory region that
15945 * provided rpi via a bitmask.
15946 **/
15947int
James Smart6b5151f2012-01-18 16:24:06 -050015948lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15949 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040015950{
15951 LPFC_MBOXQ_t *mboxq;
15952 struct lpfc_hba *phba = ndlp->phba;
15953 int rc;
15954
15955 /* The port is notified of the header region via a mailbox command. */
15956 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15957 if (!mboxq)
15958 return -ENOMEM;
15959
15960 /* Post all rpi memory regions to the port. */
15961 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050015962 if (cmpl) {
15963 mboxq->mbox_cmpl = cmpl;
15964 mboxq->context1 = arg;
15965 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050015966 } else
15967 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050015968 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040015969 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15970 if (rc == MBX_NOT_FINISHED) {
15971 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15972 "2010 Resume RPI Mailbox failed "
15973 "status %d, mbxStatus x%x\n", rc,
15974 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15975 mempool_free(mboxq, phba->mbox_mem_pool);
15976 return -EIO;
15977 }
15978 return 0;
15979}
15980
15981/**
15982 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050015983 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040015984 *
James Smart76a95d72010-11-20 23:11:48 -050015985 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040015986 *
15987 * Returns:
15988 * 0 success
15989 * -Evalue otherwise
15990 **/
15991int
James Smart76a95d72010-11-20 23:11:48 -050015992lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040015993{
15994 LPFC_MBOXQ_t *mboxq;
15995 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040015996 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040015997 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050015998 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040015999 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16000 if (!mboxq)
16001 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016002 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016003 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016004 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016005 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016006 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016007 "2022 INIT VPI Mailbox failed "
16008 "status %d, mbxStatus x%x\n", rc,
16009 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016010 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016011 }
James Smart6a9c52c2009-10-02 15:16:51 -040016012 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016013 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016014
16015 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016016}
16017
16018/**
16019 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16020 * @phba: pointer to lpfc hba data structure.
16021 * @mboxq: Pointer to mailbox object.
16022 *
16023 * This routine is invoked to manually add a single FCF record. The caller
16024 * must pass a completely initialized FCF_Record. This routine takes
16025 * care of the nonembedded mailbox operations.
16026 **/
16027static void
16028lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16029{
16030 void *virt_addr;
16031 union lpfc_sli4_cfg_shdr *shdr;
16032 uint32_t shdr_status, shdr_add_status;
16033
16034 virt_addr = mboxq->sge_array->addr[0];
16035 /* The IOCTL status is embedded in the mailbox subheader. */
16036 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16037 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16038 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16039
16040 if ((shdr_status || shdr_add_status) &&
16041 (shdr_status != STATUS_FCF_IN_USE))
16042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16043 "2558 ADD_FCF_RECORD mailbox failed with "
16044 "status x%x add_status x%x\n",
16045 shdr_status, shdr_add_status);
16046
16047 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16048}
16049
16050/**
16051 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16052 * @phba: pointer to lpfc hba data structure.
16053 * @fcf_record: pointer to the initialized fcf record to add.
16054 *
16055 * This routine is invoked to manually add a single FCF record. The caller
16056 * must pass a completely initialized FCF_Record. This routine takes
16057 * care of the nonembedded mailbox operations.
16058 **/
16059int
16060lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16061{
16062 int rc = 0;
16063 LPFC_MBOXQ_t *mboxq;
16064 uint8_t *bytep;
16065 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016066 struct lpfc_mbx_sge sge;
16067 uint32_t alloc_len, req_len;
16068 uint32_t fcfindex;
16069
16070 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16071 if (!mboxq) {
16072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16073 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16074 return -ENOMEM;
16075 }
16076
16077 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16078 sizeof(uint32_t);
16079
16080 /* Allocate DMA memory and set up the non-embedded mailbox command */
16081 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16082 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16083 req_len, LPFC_SLI4_MBX_NEMBED);
16084 if (alloc_len < req_len) {
16085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16086 "2523 Allocated DMA memory size (x%x) is "
16087 "less than the requested DMA memory "
16088 "size (x%x)\n", alloc_len, req_len);
16089 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16090 return -ENOMEM;
16091 }
16092
16093 /*
16094 * Get the first SGE entry from the non-embedded DMA memory. This
16095 * routine only uses a single SGE.
16096 */
16097 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016098 virt_addr = mboxq->sge_array->addr[0];
16099 /*
16100 * Configure the FCF record for FCFI 0. This is the driver's
16101 * hardcoded default and gets used in nonFIP mode.
16102 */
16103 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16104 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16105 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16106
16107 /*
16108 * Copy the fcf_index and the FCF Record Data. The data starts after
16109 * the FCoE header plus word10. The data copy needs to be endian
16110 * correct.
16111 */
16112 bytep += sizeof(uint32_t);
16113 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16114 mboxq->vport = phba->pport;
16115 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16116 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16117 if (rc == MBX_NOT_FINISHED) {
16118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16119 "2515 ADD_FCF_RECORD mailbox failed with "
16120 "status 0x%x\n", rc);
16121 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16122 rc = -EIO;
16123 } else
16124 rc = 0;
16125
16126 return rc;
16127}
16128
16129/**
16130 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16131 * @phba: pointer to lpfc hba data structure.
16132 * @fcf_record: pointer to the fcf record to write the default data.
16133 * @fcf_index: FCF table entry index.
16134 *
16135 * This routine is invoked to build the driver's default FCF record. The
16136 * values used are hardcoded. This routine handles memory initialization.
16137 *
16138 **/
16139void
16140lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16141 struct fcf_record *fcf_record,
16142 uint16_t fcf_index)
16143{
16144 memset(fcf_record, 0, sizeof(struct fcf_record));
16145 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16146 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16147 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16148 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16149 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16150 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16151 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16152 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16153 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16154 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16155 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16156 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16157 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016158 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016159 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16160 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16161 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16162 /* Set the VLAN bit map */
16163 if (phba->valid_vlan) {
16164 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16165 = 1 << (phba->vlan_id % 8);
16166 }
16167}
16168
16169/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016170 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016171 * @phba: pointer to lpfc hba data structure.
16172 * @fcf_index: FCF table entry offset.
16173 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016174 * This routine is invoked to scan the entire FCF table by reading FCF
16175 * record and processing it one at a time starting from the @fcf_index
16176 * for initial FCF discovery or fast FCF failover rediscovery.
16177 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016178 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016179 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016180 **/
16181int
James Smart0c9ab6f2010-02-26 14:15:57 -050016182lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016183{
16184 int rc = 0, error;
16185 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016186
James Smart32b97932009-07-19 10:01:21 -040016187 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016188 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016189 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16190 if (!mboxq) {
16191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16192 "2000 Failed to allocate mbox for "
16193 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016194 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016195 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016196 }
James Smartecfd03c2010-02-12 14:41:27 -050016197 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016198 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016199 if (rc) {
16200 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016201 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016202 }
James Smartecfd03c2010-02-12 14:41:27 -050016203 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016204 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016205 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016206
16207 spin_lock_irq(&phba->hbalock);
16208 phba->hba_flag |= FCF_TS_INPROG;
16209 spin_unlock_irq(&phba->hbalock);
16210
James Smart6fb120a2009-05-22 14:52:59 -040016211 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016212 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016213 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016214 else {
James Smart38b92ef2010-08-04 16:11:39 -040016215 /* Reset eligible FCF count for new scan */
16216 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016217 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016218 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016219 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016220fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016221 if (error) {
16222 if (mboxq)
16223 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016224 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016225 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016226 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016227 spin_unlock_irq(&phba->hbalock);
16228 }
James Smart6fb120a2009-05-22 14:52:59 -040016229 return error;
16230}
James Smarta0c87cb2009-07-19 10:01:10 -040016231
16232/**
James Smarta93ff372010-10-22 11:06:08 -040016233 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016234 * @phba: pointer to lpfc hba data structure.
16235 * @fcf_index: FCF table entry offset.
16236 *
16237 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016238 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016239 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016240 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016241 * otherwise.
16242 **/
16243int
16244lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16245{
16246 int rc = 0, error;
16247 LPFC_MBOXQ_t *mboxq;
16248
16249 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16250 if (!mboxq) {
16251 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16252 "2763 Failed to allocate mbox for "
16253 "READ_FCF cmd\n");
16254 error = -ENOMEM;
16255 goto fail_fcf_read;
16256 }
16257 /* Construct the read FCF record mailbox command */
16258 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16259 if (rc) {
16260 error = -EINVAL;
16261 goto fail_fcf_read;
16262 }
16263 /* Issue the mailbox command asynchronously */
16264 mboxq->vport = phba->pport;
16265 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16266 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16267 if (rc == MBX_NOT_FINISHED)
16268 error = -EIO;
16269 else
16270 error = 0;
16271
16272fail_fcf_read:
16273 if (error && mboxq)
16274 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16275 return error;
16276}
16277
16278/**
16279 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16280 * @phba: pointer to lpfc hba data structure.
16281 * @fcf_index: FCF table entry offset.
16282 *
16283 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016284 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016285 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016286 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016287 * otherwise.
16288 **/
16289int
16290lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16291{
16292 int rc = 0, error;
16293 LPFC_MBOXQ_t *mboxq;
16294
16295 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16296 if (!mboxq) {
16297 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16298 "2758 Failed to allocate mbox for "
16299 "READ_FCF cmd\n");
16300 error = -ENOMEM;
16301 goto fail_fcf_read;
16302 }
16303 /* Construct the read FCF record mailbox command */
16304 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16305 if (rc) {
16306 error = -EINVAL;
16307 goto fail_fcf_read;
16308 }
16309 /* Issue the mailbox command asynchronously */
16310 mboxq->vport = phba->pport;
16311 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16312 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16313 if (rc == MBX_NOT_FINISHED)
16314 error = -EIO;
16315 else
16316 error = 0;
16317
16318fail_fcf_read:
16319 if (error && mboxq)
16320 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16321 return error;
16322}
16323
16324/**
James Smartf5cb5302015-12-16 18:11:52 -050016325 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016326 * phba pointer to the lpfc_hba struct for this port.
16327 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16328 * routine when the rr_bmask is empty. The FCF indecies are put into the
16329 * rr_bmask based on their priority level. Starting from the highest priority
16330 * to the lowest. The most likely FCF candidate will be in the highest
16331 * priority group. When this routine is called it searches the fcf_pri list for
16332 * next lowest priority group and repopulates the rr_bmask with only those
16333 * fcf_indexes.
16334 * returns:
16335 * 1=success 0=failure
16336 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016337static int
James Smart7d791df2011-07-22 18:37:52 -040016338lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16339{
16340 uint16_t next_fcf_pri;
16341 uint16_t last_index;
16342 struct lpfc_fcf_pri *fcf_pri;
16343 int rc;
16344 int ret = 0;
16345
16346 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16347 LPFC_SLI4_FCF_TBL_INDX_MAX);
16348 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16349 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016350
16351 /* Verify the priority list has 2 or more entries */
16352 spin_lock_irq(&phba->hbalock);
16353 if (list_empty(&phba->fcf.fcf_pri_list) ||
16354 list_is_singular(&phba->fcf.fcf_pri_list)) {
16355 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016356 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16357 "3061 Last IDX %d\n", last_index);
16358 return 0; /* Empty rr list */
16359 }
James Smart25626692013-03-01 16:36:54 -050016360 spin_unlock_irq(&phba->hbalock);
16361
James Smart7d791df2011-07-22 18:37:52 -040016362 next_fcf_pri = 0;
16363 /*
16364 * Clear the rr_bmask and set all of the bits that are at this
16365 * priority.
16366 */
16367 memset(phba->fcf.fcf_rr_bmask, 0,
16368 sizeof(*phba->fcf.fcf_rr_bmask));
16369 spin_lock_irq(&phba->hbalock);
16370 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16371 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16372 continue;
16373 /*
16374 * the 1st priority that has not FLOGI failed
16375 * will be the highest.
16376 */
16377 if (!next_fcf_pri)
16378 next_fcf_pri = fcf_pri->fcf_rec.priority;
16379 spin_unlock_irq(&phba->hbalock);
16380 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16381 rc = lpfc_sli4_fcf_rr_index_set(phba,
16382 fcf_pri->fcf_rec.fcf_index);
16383 if (rc)
16384 return 0;
16385 }
16386 spin_lock_irq(&phba->hbalock);
16387 }
16388 /*
16389 * if next_fcf_pri was not set above and the list is not empty then
16390 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016391 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016392 */
16393 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16394 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16395 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16396 /*
16397 * the 1st priority that has not FLOGI failed
16398 * will be the highest.
16399 */
16400 if (!next_fcf_pri)
16401 next_fcf_pri = fcf_pri->fcf_rec.priority;
16402 spin_unlock_irq(&phba->hbalock);
16403 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16404 rc = lpfc_sli4_fcf_rr_index_set(phba,
16405 fcf_pri->fcf_rec.fcf_index);
16406 if (rc)
16407 return 0;
16408 }
16409 spin_lock_irq(&phba->hbalock);
16410 }
16411 } else
16412 ret = 1;
16413 spin_unlock_irq(&phba->hbalock);
16414
16415 return ret;
16416}
16417/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016418 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16419 * @phba: pointer to lpfc hba data structure.
16420 *
16421 * This routine is to get the next eligible FCF record index in a round
16422 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016423 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016424 * shall be returned, otherwise, the next eligible FCF record's index
16425 * shall be returned.
16426 **/
16427uint16_t
16428lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16429{
16430 uint16_t next_fcf_index;
16431
James Smart421c6622013-01-03 15:44:16 -050016432initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016433 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016434 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16435
James Smart7d791df2011-07-22 18:37:52 -040016436next_priority:
James Smart421c6622013-01-03 15:44:16 -050016437 /* Determine the next fcf index to check */
16438 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016439 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16440 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016441 next_fcf_index);
16442
James Smart0c9ab6f2010-02-26 14:15:57 -050016443 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016444 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16445 /*
16446 * If we have wrapped then we need to clear the bits that
16447 * have been tested so that we can detect when we should
16448 * change the priority level.
16449 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016450 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16451 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016452 }
16453
James Smart0c9ab6f2010-02-26 14:15:57 -050016454
James Smart3804dc82010-07-14 15:31:37 -040016455 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016456 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16457 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16458 /*
16459 * If next fcf index is not found check if there are lower
16460 * Priority level fcf's in the fcf_priority list.
16461 * Set up the rr_bmask with all of the avaiable fcf bits
16462 * at that level and continue the selection process.
16463 */
16464 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016465 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016466 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16467 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016468 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16469 return LPFC_FCOE_FCF_NEXT_NONE;
16470 else {
16471 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16472 "3063 Only FCF available idx %d, flag %x\n",
16473 next_fcf_index,
16474 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16475 return next_fcf_index;
16476 }
James Smart3804dc82010-07-14 15:31:37 -040016477 }
16478
James Smart7d791df2011-07-22 18:37:52 -040016479 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16480 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016481 LPFC_FCF_FLOGI_FAILED) {
16482 if (list_is_singular(&phba->fcf.fcf_pri_list))
16483 return LPFC_FCOE_FCF_NEXT_NONE;
16484
James Smart7d791df2011-07-22 18:37:52 -040016485 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016486 }
James Smart7d791df2011-07-22 18:37:52 -040016487
James Smart3804dc82010-07-14 15:31:37 -040016488 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016489 "2845 Get next roundrobin failover FCF (x%x)\n",
16490 next_fcf_index);
16491
James Smart0c9ab6f2010-02-26 14:15:57 -050016492 return next_fcf_index;
16493}
16494
16495/**
16496 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16497 * @phba: pointer to lpfc hba data structure.
16498 *
16499 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016500 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016501 * does not go beyond the range of the driver allocated bmask dimension
16502 * before setting the bit.
16503 *
16504 * Returns 0 if the index bit successfully set, otherwise, it returns
16505 * -EINVAL.
16506 **/
16507int
16508lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16509{
16510 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16511 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016512 "2610 FCF (x%x) reached driver's book "
16513 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016514 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16515 return -EINVAL;
16516 }
16517 /* Set the eligible FCF record index bmask */
16518 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16519
James Smart3804dc82010-07-14 15:31:37 -040016520 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016521 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016522 "bmask\n", fcf_index);
16523
James Smart0c9ab6f2010-02-26 14:15:57 -050016524 return 0;
16525}
16526
16527/**
James Smart3804dc82010-07-14 15:31:37 -040016528 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016529 * @phba: pointer to lpfc hba data structure.
16530 *
16531 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016532 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016533 * does not go beyond the range of the driver allocated bmask dimension
16534 * before clearing the bit.
16535 **/
16536void
16537lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16538{
James Smart9a803a72013-09-06 12:17:56 -040016539 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016540 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16541 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016542 "2762 FCF (x%x) reached driver's book "
16543 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016544 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16545 return;
16546 }
16547 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016548 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016549 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16550 list) {
James Smart7d791df2011-07-22 18:37:52 -040016551 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16552 list_del_init(&fcf_pri->list);
16553 break;
16554 }
16555 }
16556 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016557 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016558
16559 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016560 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016561 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016562}
16563
16564/**
James Smartecfd03c2010-02-12 14:41:27 -050016565 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16566 * @phba: pointer to lpfc hba data structure.
16567 *
16568 * This routine is the completion routine for the rediscover FCF table mailbox
16569 * command. If the mailbox command returned failure, it will try to stop the
16570 * FCF rediscover wait timer.
16571 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016572static void
James Smartecfd03c2010-02-12 14:41:27 -050016573lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16574{
16575 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16576 uint32_t shdr_status, shdr_add_status;
16577
16578 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16579
16580 shdr_status = bf_get(lpfc_mbox_hdr_status,
16581 &redisc_fcf->header.cfg_shdr.response);
16582 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16583 &redisc_fcf->header.cfg_shdr.response);
16584 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016585 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016586 "2746 Requesting for FCF rediscovery failed "
16587 "status x%x add_status x%x\n",
16588 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016589 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016590 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016591 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016592 spin_unlock_irq(&phba->hbalock);
16593 /*
16594 * CVL event triggered FCF rediscover request failed,
16595 * last resort to re-try current registered FCF entry.
16596 */
16597 lpfc_retry_pport_discovery(phba);
16598 } else {
16599 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016600 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016601 spin_unlock_irq(&phba->hbalock);
16602 /*
16603 * DEAD FCF event triggered FCF rediscover request
16604 * failed, last resort to fail over as a link down
16605 * to FCF registration.
16606 */
16607 lpfc_sli4_fcf_dead_failthrough(phba);
16608 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016609 } else {
16610 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016611 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016612 /*
16613 * Start FCF rediscovery wait timer for pending FCF
16614 * before rescan FCF record table.
16615 */
16616 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016617 }
James Smartecfd03c2010-02-12 14:41:27 -050016618
16619 mempool_free(mbox, phba->mbox_mem_pool);
16620}
16621
16622/**
James Smart3804dc82010-07-14 15:31:37 -040016623 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016624 * @phba: pointer to lpfc hba data structure.
16625 *
16626 * This routine is invoked to request for rediscovery of the entire FCF table
16627 * by the port.
16628 **/
16629int
16630lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16631{
16632 LPFC_MBOXQ_t *mbox;
16633 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16634 int rc, length;
16635
James Smart0c9ab6f2010-02-26 14:15:57 -050016636 /* Cancel retry delay timers to all vports before FCF rediscover */
16637 lpfc_cancel_all_vport_retry_delay_timer(phba);
16638
James Smartecfd03c2010-02-12 14:41:27 -050016639 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16640 if (!mbox) {
16641 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16642 "2745 Failed to allocate mbox for "
16643 "requesting FCF rediscover.\n");
16644 return -ENOMEM;
16645 }
16646
16647 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16648 sizeof(struct lpfc_sli4_cfg_mhdr));
16649 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16650 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16651 length, LPFC_SLI4_MBX_EMBED);
16652
16653 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16654 /* Set count to 0 for invalidating the entire FCF database */
16655 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16656
16657 /* Issue the mailbox command asynchronously */
16658 mbox->vport = phba->pport;
16659 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16660 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16661
16662 if (rc == MBX_NOT_FINISHED) {
16663 mempool_free(mbox, phba->mbox_mem_pool);
16664 return -EIO;
16665 }
16666 return 0;
16667}
16668
16669/**
James Smartfc2b9892010-02-26 14:15:29 -050016670 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16671 * @phba: pointer to lpfc hba data structure.
16672 *
16673 * This function is the failover routine as a last resort to the FCF DEAD
16674 * event when driver failed to perform fast FCF failover.
16675 **/
16676void
16677lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16678{
16679 uint32_t link_state;
16680
16681 /*
16682 * Last resort as FCF DEAD event failover will treat this as
16683 * a link down, but save the link state because we don't want
16684 * it to be changed to Link Down unless it is already down.
16685 */
16686 link_state = phba->link_state;
16687 lpfc_linkdown(phba);
16688 phba->link_state = link_state;
16689
16690 /* Unregister FCF if no devices connected to it */
16691 lpfc_unregister_unused_fcf(phba);
16692}
16693
16694/**
James Smart026abb82011-12-13 13:20:45 -050016695 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016696 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016697 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016698 *
James Smart026abb82011-12-13 13:20:45 -050016699 * This function gets SLI3 port configure region 23 data through memory dump
16700 * mailbox command. When it successfully retrieves data, the size of the data
16701 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016702 **/
James Smart026abb82011-12-13 13:20:45 -050016703static uint32_t
16704lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016705{
16706 LPFC_MBOXQ_t *pmb = NULL;
16707 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016708 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016709 int rc;
16710
James Smart026abb82011-12-13 13:20:45 -050016711 if (!rgn23_data)
16712 return 0;
16713
James Smarta0c87cb2009-07-19 10:01:10 -040016714 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16715 if (!pmb) {
16716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016717 "2600 failed to allocate mailbox memory\n");
16718 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016719 }
16720 mb = &pmb->u.mb;
16721
James Smarta0c87cb2009-07-19 10:01:10 -040016722 do {
16723 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16724 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16725
16726 if (rc != MBX_SUCCESS) {
16727 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016728 "2601 failed to read config "
16729 "region 23, rc 0x%x Status 0x%x\n",
16730 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016731 mb->un.varDmp.word_cnt = 0;
16732 }
16733 /*
16734 * dump mem may return a zero when finished or we got a
16735 * mailbox error, either way we are done.
16736 */
16737 if (mb->un.varDmp.word_cnt == 0)
16738 break;
16739 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16740 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16741
16742 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016743 rgn23_data + offset,
16744 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016745 offset += mb->un.varDmp.word_cnt;
16746 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16747
James Smart026abb82011-12-13 13:20:45 -050016748 mempool_free(pmb, phba->mbox_mem_pool);
16749 return offset;
16750}
16751
16752/**
16753 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16754 * @phba: pointer to lpfc hba data structure.
16755 * @rgn23_data: pointer to configure region 23 data.
16756 *
16757 * This function gets SLI4 port configure region 23 data through memory dump
16758 * mailbox command. When it successfully retrieves data, the size of the data
16759 * will be returned, otherwise, 0 will be returned.
16760 **/
16761static uint32_t
16762lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16763{
16764 LPFC_MBOXQ_t *mboxq = NULL;
16765 struct lpfc_dmabuf *mp = NULL;
16766 struct lpfc_mqe *mqe;
16767 uint32_t data_length = 0;
16768 int rc;
16769
16770 if (!rgn23_data)
16771 return 0;
16772
16773 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16774 if (!mboxq) {
16775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16776 "3105 failed to allocate mailbox memory\n");
16777 return 0;
16778 }
16779
16780 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16781 goto out;
16782 mqe = &mboxq->u.mqe;
16783 mp = (struct lpfc_dmabuf *) mboxq->context1;
16784 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16785 if (rc)
16786 goto out;
16787 data_length = mqe->un.mb_words[5];
16788 if (data_length == 0)
16789 goto out;
16790 if (data_length > DMP_RGN23_SIZE) {
16791 data_length = 0;
16792 goto out;
16793 }
16794 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16795out:
16796 mempool_free(mboxq, phba->mbox_mem_pool);
16797 if (mp) {
16798 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16799 kfree(mp);
16800 }
16801 return data_length;
16802}
16803
16804/**
16805 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16806 * @phba: pointer to lpfc hba data structure.
16807 *
16808 * This function read region 23 and parse TLV for port status to
16809 * decide if the user disaled the port. If the TLV indicates the
16810 * port is disabled, the hba_flag is set accordingly.
16811 **/
16812void
16813lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16814{
16815 uint8_t *rgn23_data = NULL;
16816 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16817 uint32_t offset = 0;
16818
16819 /* Get adapter Region 23 data */
16820 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16821 if (!rgn23_data)
16822 goto out;
16823
16824 if (phba->sli_rev < LPFC_SLI_REV4)
16825 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16826 else {
16827 if_type = bf_get(lpfc_sli_intf_if_type,
16828 &phba->sli4_hba.sli_intf);
16829 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16830 goto out;
16831 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16832 }
James Smarta0c87cb2009-07-19 10:01:10 -040016833
16834 if (!data_size)
16835 goto out;
16836
16837 /* Check the region signature first */
16838 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16840 "2619 Config region 23 has bad signature\n");
16841 goto out;
16842 }
16843 offset += 4;
16844
16845 /* Check the data structure version */
16846 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16848 "2620 Config region 23 has bad version\n");
16849 goto out;
16850 }
16851 offset += 4;
16852
16853 /* Parse TLV entries in the region */
16854 while (offset < data_size) {
16855 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16856 break;
16857 /*
16858 * If the TLV is not driver specific TLV or driver id is
16859 * not linux driver id, skip the record.
16860 */
16861 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16862 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16863 (rgn23_data[offset + 3] != 0)) {
16864 offset += rgn23_data[offset + 1] * 4 + 4;
16865 continue;
16866 }
16867
16868 /* Driver found a driver specific TLV in the config region */
16869 sub_tlv_len = rgn23_data[offset + 1] * 4;
16870 offset += 4;
16871 tlv_offset = 0;
16872
16873 /*
16874 * Search for configured port state sub-TLV.
16875 */
16876 while ((offset < data_size) &&
16877 (tlv_offset < sub_tlv_len)) {
16878 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16879 offset += 4;
16880 tlv_offset += 4;
16881 break;
16882 }
16883 if (rgn23_data[offset] != PORT_STE_TYPE) {
16884 offset += rgn23_data[offset + 1] * 4 + 4;
16885 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16886 continue;
16887 }
16888
16889 /* This HBA contains PORT_STE configured */
16890 if (!rgn23_data[offset + 2])
16891 phba->hba_flag |= LINK_DISABLED;
16892
16893 goto out;
16894 }
16895 }
James Smart026abb82011-12-13 13:20:45 -050016896
James Smarta0c87cb2009-07-19 10:01:10 -040016897out:
James Smarta0c87cb2009-07-19 10:01:10 -040016898 kfree(rgn23_data);
16899 return;
16900}
James Smart695a8142010-01-26 23:08:03 -050016901
16902/**
James Smart52d52442011-05-24 11:42:45 -040016903 * lpfc_wr_object - write an object to the firmware
16904 * @phba: HBA structure that indicates port to create a queue on.
16905 * @dmabuf_list: list of dmabufs to write to the port.
16906 * @size: the total byte value of the objects to write to the port.
16907 * @offset: the current offset to be used to start the transfer.
16908 *
16909 * This routine will create a wr_object mailbox command to send to the port.
16910 * the mailbox command will be constructed using the dma buffers described in
16911 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16912 * BDEs that the imbedded mailbox can support. The @offset variable will be
16913 * used to indicate the starting offset of the transfer and will also return
16914 * the offset after the write object mailbox has completed. @size is used to
16915 * determine the end of the object and whether the eof bit should be set.
16916 *
16917 * Return 0 is successful and offset will contain the the new offset to use
16918 * for the next write.
16919 * Return negative value for error cases.
16920 **/
16921int
16922lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16923 uint32_t size, uint32_t *offset)
16924{
16925 struct lpfc_mbx_wr_object *wr_object;
16926 LPFC_MBOXQ_t *mbox;
16927 int rc = 0, i = 0;
16928 uint32_t shdr_status, shdr_add_status;
16929 uint32_t mbox_tmo;
16930 union lpfc_sli4_cfg_shdr *shdr;
16931 struct lpfc_dmabuf *dmabuf;
16932 uint32_t written = 0;
16933
16934 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16935 if (!mbox)
16936 return -ENOMEM;
16937
16938 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16939 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16940 sizeof(struct lpfc_mbx_wr_object) -
16941 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16942
16943 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16944 wr_object->u.request.write_offset = *offset;
16945 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16946 wr_object->u.request.object_name[0] =
16947 cpu_to_le32(wr_object->u.request.object_name[0]);
16948 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16949 list_for_each_entry(dmabuf, dmabuf_list, list) {
16950 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16951 break;
16952 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16953 wr_object->u.request.bde[i].addrHigh =
16954 putPaddrHigh(dmabuf->phys);
16955 if (written + SLI4_PAGE_SIZE >= size) {
16956 wr_object->u.request.bde[i].tus.f.bdeSize =
16957 (size - written);
16958 written += (size - written);
16959 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16960 } else {
16961 wr_object->u.request.bde[i].tus.f.bdeSize =
16962 SLI4_PAGE_SIZE;
16963 written += SLI4_PAGE_SIZE;
16964 }
16965 i++;
16966 }
16967 wr_object->u.request.bde_count = i;
16968 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16969 if (!phba->sli4_hba.intr_enable)
16970 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16971 else {
James Smarta183a152011-10-10 21:32:43 -040016972 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040016973 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16974 }
16975 /* The IOCTL status is embedded in the mailbox subheader. */
16976 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16977 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16978 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16979 if (rc != MBX_TIMEOUT)
16980 mempool_free(mbox, phba->mbox_mem_pool);
16981 if (shdr_status || shdr_add_status || rc) {
16982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16983 "3025 Write Object mailbox failed with "
16984 "status x%x add_status x%x, mbx status x%x\n",
16985 shdr_status, shdr_add_status, rc);
16986 rc = -ENXIO;
16987 } else
16988 *offset += wr_object->u.response.actual_write_length;
16989 return rc;
16990}
16991
16992/**
James Smart695a8142010-01-26 23:08:03 -050016993 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16994 * @vport: pointer to vport data structure.
16995 *
16996 * This function iterate through the mailboxq and clean up all REG_LOGIN
16997 * and REG_VPI mailbox commands associated with the vport. This function
16998 * is called when driver want to restart discovery of the vport due to
16999 * a Clear Virtual Link event.
17000 **/
17001void
17002lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17003{
17004 struct lpfc_hba *phba = vport->phba;
17005 LPFC_MBOXQ_t *mb, *nextmb;
17006 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017007 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017008 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017009 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017010 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017011 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017012
James Smartd439d282010-09-29 11:18:45 -040017013 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017014 spin_lock_irq(&phba->hbalock);
17015 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17016 if (mb->vport != vport)
17017 continue;
17018
17019 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17020 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17021 continue;
17022
James Smartd439d282010-09-29 11:18:45 -040017023 list_del(&mb->list);
17024 list_add_tail(&mb->list, &mbox_cmd_list);
17025 }
17026 /* Clean up active mailbox command with the vport */
17027 mb = phba->sli.mbox_active;
17028 if (mb && (mb->vport == vport)) {
17029 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17030 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17031 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17032 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17033 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17034 /* Put reference count for delayed processing */
17035 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17036 /* Unregister the RPI when mailbox complete */
17037 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17038 }
17039 }
James Smart63e801c2010-11-20 23:14:19 -050017040 /* Cleanup any mailbox completions which are not yet processed */
17041 do {
17042 restart_loop = 0;
17043 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17044 /*
17045 * If this mailox is already processed or it is
17046 * for another vport ignore it.
17047 */
17048 if ((mb->vport != vport) ||
17049 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17050 continue;
17051
17052 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17053 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17054 continue;
17055
17056 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17057 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17058 ndlp = (struct lpfc_nodelist *)mb->context2;
17059 /* Unregister the RPI when mailbox complete */
17060 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17061 restart_loop = 1;
17062 spin_unlock_irq(&phba->hbalock);
17063 spin_lock(shost->host_lock);
17064 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17065 spin_unlock(shost->host_lock);
17066 spin_lock_irq(&phba->hbalock);
17067 break;
17068 }
17069 }
17070 } while (restart_loop);
17071
James Smartd439d282010-09-29 11:18:45 -040017072 spin_unlock_irq(&phba->hbalock);
17073
17074 /* Release the cleaned-up mailbox commands */
17075 while (!list_empty(&mbox_cmd_list)) {
17076 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017077 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17078 mp = (struct lpfc_dmabuf *) (mb->context1);
17079 if (mp) {
17080 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17081 kfree(mp);
17082 }
James Smart78730cf2010-04-06 15:06:30 -040017083 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017084 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017085 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017086 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017087 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017088 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017089 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017090 }
James Smart695a8142010-01-26 23:08:03 -050017091 }
James Smart695a8142010-01-26 23:08:03 -050017092 mempool_free(mb, phba->mbox_mem_pool);
17093 }
James Smartd439d282010-09-29 11:18:45 -040017094
17095 /* Release the ndlp with the cleaned-up active mailbox command */
17096 if (act_mbx_ndlp) {
17097 spin_lock(shost->host_lock);
17098 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17099 spin_unlock(shost->host_lock);
17100 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017101 }
James Smart695a8142010-01-26 23:08:03 -050017102}
17103
James Smart2a9bf3d2010-06-07 15:24:45 -040017104/**
17105 * lpfc_drain_txq - Drain the txq
17106 * @phba: Pointer to HBA context object.
17107 *
17108 * This function attempt to submit IOCBs on the txq
17109 * to the adapter. For SLI4 adapters, the txq contains
17110 * ELS IOCBs that have been deferred because the there
17111 * are no SGLs. This congestion can occur with large
17112 * vport counts during node discovery.
17113 **/
17114
17115uint32_t
17116lpfc_drain_txq(struct lpfc_hba *phba)
17117{
17118 LIST_HEAD(completions);
17119 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017120 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017121 unsigned long iflags = 0;
17122 char *fail_msg = NULL;
17123 struct lpfc_sglq *sglq;
17124 union lpfc_wqe wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040017125 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017126
James Smart398d81c2013-05-31 17:04:19 -040017127 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017128 list_for_each_entry(piocbq, &pring->txq, list) {
17129 txq_cnt++;
17130 }
17131
17132 if (txq_cnt > pring->txq_max)
17133 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017134
James Smart398d81c2013-05-31 17:04:19 -040017135 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017136
James Smart0e9bb8d2013-03-01 16:35:12 -050017137 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017138 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017139
James Smart19ca7602010-11-20 23:11:55 -050017140 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017141 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017142 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017143 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17144 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017145 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017146 break;
17147 }
James Smart19ca7602010-11-20 23:11:55 -050017148 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017149 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017150 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017151 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017152 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017153 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017154 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017155
17156 /* The xri and iocb resources secured,
17157 * attempt to issue request
17158 */
James Smart6d368e52011-05-24 11:44:12 -040017159 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017160 piocbq->sli4_xritag = sglq->sli4_xritag;
17161 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17162 fail_msg = "to convert bpl to sgl";
17163 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17164 fail_msg = "to convert iocb to wqe";
17165 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17166 fail_msg = " - Wq is full";
17167 else
17168 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17169
17170 if (fail_msg) {
17171 /* Failed means we can't issue and need to cancel */
17172 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17173 "2822 IOCB failed %s iotag 0x%x "
17174 "xri 0x%x\n",
17175 fail_msg,
17176 piocbq->iotag, piocbq->sli4_xritag);
17177 list_add_tail(&piocbq->list, &completions);
17178 }
James Smart398d81c2013-05-31 17:04:19 -040017179 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017180 }
17181
James Smart2a9bf3d2010-06-07 15:24:45 -040017182 /* Cancel all the IOCBs that cannot be issued */
17183 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17184 IOERR_SLI_ABORTED);
17185
James Smart0e9bb8d2013-03-01 16:35:12 -050017186 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017187}