blob: b3a781d38fa815a09ce129c02e751d004ef50fd0 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart50611572016-03-31 14:12:34 -07004 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010027#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050028
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040033#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040034#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040035#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050036
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_hw.h"
39#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040041#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_crtn.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050048#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040049#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050050
51/* There are only four IOCB completion types. */
52typedef enum _lpfc_iocb_type {
53 LPFC_UNKNOWN_IOCB,
54 LPFC_UNSOL_IOCB,
55 LPFC_SOL_IOCB,
56 LPFC_ABORT_IOCB
57} lpfc_iocb_type;
58
James Smart4f774512009-05-22 14:52:35 -040059
60/* Provide function prototypes local to this module. */
61static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
62 uint32_t);
63static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040064 uint8_t *, uint32_t *);
65static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
66 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040067static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
68 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040069static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
70 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040071static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
72 int);
James Smartba20c852012-08-03 12:36:52 -040073static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
74 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040075static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
76static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart05580562011-05-24 11:40:48 -040077
James Smart4f774512009-05-22 14:52:35 -040078static IOCB_t *
79lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
80{
81 return &iocbq->iocb;
82}
83
84/**
85 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
86 * @q: The Work Queue to operate on.
87 * @wqe: The work Queue Entry to put on the Work queue.
88 *
89 * This routine will copy the contents of @wqe to the next available entry on
90 * the @q. This function will then ring the Work Queue Doorbell to signal the
91 * HBA to start processing the Work Queue Entry. This function returns 0 if
92 * successful. If no entries are available on @q then this function will return
93 * -ENOMEM.
94 * The caller is expected to hold the hbalock when calling this routine.
95 **/
96static uint32_t
97lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
98{
James Smart2e90f4b2011-12-13 13:22:37 -050099 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400100 struct lpfc_register doorbell;
101 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400102 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400103
James Smart2e90f4b2011-12-13 13:22:37 -0500104 /* sanity check on queue memory */
105 if (unlikely(!q))
106 return -ENOMEM;
107 temp_wqe = q->qe[q->host_index].wqe;
108
James Smart4f774512009-05-22 14:52:35 -0400109 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400110 idx = ((q->host_index + 1) % q->entry_count);
111 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400112 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400113 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400114 }
115 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400116 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500117 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400118 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James 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 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002003 * This function searches for the hbq buffer associated with the given tag in
2004 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2005 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002006 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002007static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002008lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2009{
James Smart92d7f7b2007-06-17 19:56:38 -05002010 struct lpfc_dmabuf *d_buf;
2011 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002012 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002013
James Smart51ef4c22007-08-02 11:10:31 -04002014 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002015 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002016 return NULL;
2017
James Smart3772a992009-05-22 14:50:54 -04002018 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002019 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002020 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002021 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002022 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002023 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002024 }
2025 }
James Smart3772a992009-05-22 14:50:54 -04002026 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002028 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002029 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002030 return NULL;
James Smarted957682007-06-17 19:56:37 -05002031}
2032
James Smarte59058c2008-08-24 21:49:00 -04002033/**
James Smart3621a712009-04-06 18:47:14 -04002034 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002035 * @phba: Pointer to HBA context object.
2036 * @hbq_buffer: Pointer to HBQ buffer.
2037 *
2038 * This function is called with hbalock. This function gives back
2039 * the hbq buffer to firmware. If the HBQ does not have space to
2040 * post the buffer, it will free the buffer.
2041 **/
James Smarted957682007-06-17 19:56:37 -05002042void
James Smart51ef4c22007-08-02 11:10:31 -04002043lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002044{
2045 uint32_t hbqno;
2046
James Smart51ef4c22007-08-02 11:10:31 -04002047 if (hbq_buffer) {
2048 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002049 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002050 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002051 }
2052}
2053
James Smarte59058c2008-08-24 21:49:00 -04002054/**
James Smart3621a712009-04-06 18:47:14 -04002055 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002056 * @mbxCommand: mailbox command code.
2057 *
2058 * This function is called by the mailbox event handler function to verify
2059 * that the completed mailbox command is a legitimate mailbox command. If the
2060 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2061 * and the mailbox event handler will take the HBA offline.
2062 **/
dea31012005-04-17 16:05:31 -05002063static int
2064lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2065{
2066 uint8_t ret;
2067
2068 switch (mbxCommand) {
2069 case MBX_LOAD_SM:
2070 case MBX_READ_NV:
2071 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002072 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002073 case MBX_RUN_BIU_DIAG:
2074 case MBX_INIT_LINK:
2075 case MBX_DOWN_LINK:
2076 case MBX_CONFIG_LINK:
2077 case MBX_CONFIG_RING:
2078 case MBX_RESET_RING:
2079 case MBX_READ_CONFIG:
2080 case MBX_READ_RCONFIG:
2081 case MBX_READ_SPARM:
2082 case MBX_READ_STATUS:
2083 case MBX_READ_RPI:
2084 case MBX_READ_XRI:
2085 case MBX_READ_REV:
2086 case MBX_READ_LNK_STAT:
2087 case MBX_REG_LOGIN:
2088 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002089 case MBX_CLEAR_LA:
2090 case MBX_DUMP_MEMORY:
2091 case MBX_DUMP_CONTEXT:
2092 case MBX_RUN_DIAGS:
2093 case MBX_RESTART:
2094 case MBX_UPDATE_CFG:
2095 case MBX_DOWN_LOAD:
2096 case MBX_DEL_LD_ENTRY:
2097 case MBX_RUN_PROGRAM:
2098 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002099 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002100 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002101 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002102 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002103 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002104 case MBX_LOAD_AREA:
2105 case MBX_RUN_BIU_DIAG64:
2106 case MBX_CONFIG_PORT:
2107 case MBX_READ_SPARM64:
2108 case MBX_READ_RPI64:
2109 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002110 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002111 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002112 case MBX_SET_DEBUG:
2113 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002114 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002115 case MBX_REG_VPI:
2116 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002117 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002118 case MBX_PORT_CAPABILITIES:
2119 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002120 case MBX_SLI4_CONFIG:
2121 case MBX_SLI4_REQ_FTRS:
2122 case MBX_REG_FCFI:
2123 case MBX_UNREG_FCFI:
2124 case MBX_REG_VFI:
2125 case MBX_UNREG_VFI:
2126 case MBX_INIT_VPI:
2127 case MBX_INIT_VFI:
2128 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002129 case MBX_READ_EVENT_LOG_STATUS:
2130 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002131 case MBX_SECURITY_MGMT:
2132 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002133 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002134 ret = mbxCommand;
2135 break;
2136 default:
2137 ret = MBX_SHUTDOWN;
2138 break;
2139 }
James Smart2e0fef82007-06-17 19:56:36 -05002140 return ret;
dea31012005-04-17 16:05:31 -05002141}
James Smarte59058c2008-08-24 21:49:00 -04002142
2143/**
James Smart3621a712009-04-06 18:47:14 -04002144 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002145 * @phba: Pointer to HBA context object.
2146 * @pmboxq: Pointer to mailbox command.
2147 *
2148 * This is completion handler function for mailbox commands issued from
2149 * lpfc_sli_issue_mbox_wait function. This function is called by the
2150 * mailbox event handler function with no lock held. This function
2151 * will wake up thread waiting on the wait queue pointed by context1
2152 * of the mailbox.
2153 **/
James Smart04c68492009-05-22 14:52:52 -04002154void
James Smart2e0fef82007-06-17 19:56:36 -05002155lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002156{
2157 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002158 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002159
2160 /*
2161 * If pdone_q is empty, the driver thread gave up waiting and
2162 * continued running.
2163 */
James Smart7054a602007-04-25 09:52:34 -04002164 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002165 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002166 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2167 if (pdone_q)
2168 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002169 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002170 return;
2171}
2172
James Smarte59058c2008-08-24 21:49:00 -04002173
2174/**
James Smart3621a712009-04-06 18:47:14 -04002175 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002176 * @phba: Pointer to HBA context object.
2177 * @pmb: Pointer to mailbox object.
2178 *
2179 * This function is the default mailbox completion handler. It
2180 * frees the memory resources associated with the completed mailbox
2181 * command. If the completed command is a REG_LOGIN mailbox command,
2182 * this function will issue a UREG_LOGIN to re-claim the RPI.
2183 **/
dea31012005-04-17 16:05:31 -05002184void
James Smart2e0fef82007-06-17 19:56:36 -05002185lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002186{
James Smartd439d282010-09-29 11:18:45 -04002187 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002188 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002189 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002190 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002191 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002192 int rc;
2193
dea31012005-04-17 16:05:31 -05002194 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002195
dea31012005-04-17 16:05:31 -05002196 if (mp) {
2197 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2198 kfree(mp);
2199 }
James Smart7054a602007-04-25 09:52:34 -04002200
2201 /*
2202 * If a REG_LOGIN succeeded after node is destroyed or node
2203 * is in re-discovery driver need to cleanup the RPI.
2204 */
James Smart2e0fef82007-06-17 19:56:36 -05002205 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002206 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2207 !pmb->u.mb.mbxStatus) {
2208 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002209 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002210 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002211 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002212 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002213 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2214 if (rc != MBX_NOT_FINISHED)
2215 return;
2216 }
2217
James Smart695a8142010-01-26 23:08:03 -05002218 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2219 !(phba->pport->load_flag & FC_UNLOADING) &&
2220 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002221 shost = lpfc_shost_from_vport(vport);
2222 spin_lock_irq(shost->host_lock);
2223 vport->vpi_state |= LPFC_VPI_REGISTERED;
2224 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2225 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002226 }
2227
James Smartd439d282010-09-29 11:18:45 -04002228 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2229 ndlp = (struct lpfc_nodelist *)pmb->context2;
2230 lpfc_nlp_put(ndlp);
2231 pmb->context2 = NULL;
2232 }
2233
James Smartdcf2a4e2010-09-29 11:18:53 -04002234 /* Check security permission status on INIT_LINK mailbox command */
2235 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2236 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2237 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2238 "2860 SLI authentication is required "
2239 "for INIT_LINK but has not done yet\n");
2240
James Smart04c68492009-05-22 14:52:52 -04002241 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2242 lpfc_sli4_mbox_cmd_free(phba, pmb);
2243 else
2244 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002245}
James Smartbe6bb942015-04-07 15:07:22 -04002246 /**
2247 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2248 * @phba: Pointer to HBA context object.
2249 * @pmb: Pointer to mailbox object.
2250 *
2251 * This function is the unreg rpi mailbox completion handler. It
2252 * frees the memory resources associated with the completed mailbox
2253 * command. An additional refrenece is put on the ndlp to prevent
2254 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2255 * the unreg mailbox command completes, this routine puts the
2256 * reference back.
2257 *
2258 **/
2259void
2260lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2261{
2262 struct lpfc_vport *vport = pmb->vport;
2263 struct lpfc_nodelist *ndlp;
2264
2265 ndlp = pmb->context1;
2266 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2267 if (phba->sli_rev == LPFC_SLI_REV4 &&
2268 (bf_get(lpfc_sli_intf_if_type,
2269 &phba->sli4_hba.sli_intf) ==
2270 LPFC_SLI_INTF_IF_TYPE_2)) {
2271 if (ndlp) {
2272 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2273 "0010 UNREG_LOGIN vpi:%x "
2274 "rpi:%x DID:%x map:%x %p\n",
2275 vport->vpi, ndlp->nlp_rpi,
2276 ndlp->nlp_DID,
2277 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002278 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002279 lpfc_nlp_put(ndlp);
2280 }
2281 }
2282 }
2283
2284 mempool_free(pmb, phba->mbox_mem_pool);
2285}
dea31012005-04-17 16:05:31 -05002286
James Smarte59058c2008-08-24 21:49:00 -04002287/**
James Smart3621a712009-04-06 18:47:14 -04002288 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002289 * @phba: Pointer to HBA context object.
2290 *
2291 * This function is called with no lock held. This function processes all
2292 * the completed mailbox commands and gives it to upper layers. The interrupt
2293 * service routine processes mailbox completion interrupt and adds completed
2294 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2295 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2296 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2297 * function returns the mailbox commands to the upper layer by calling the
2298 * completion handler function of each mailbox.
2299 **/
dea31012005-04-17 16:05:31 -05002300int
James Smart2e0fef82007-06-17 19:56:36 -05002301lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002302{
James Smart92d7f7b2007-06-17 19:56:38 -05002303 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002304 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002305 int rc;
2306 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002307
2308 phba->sli.slistat.mbox_event++;
2309
James Smart92d7f7b2007-06-17 19:56:38 -05002310 /* Get all completed mailboxe buffers into the cmplq */
2311 spin_lock_irq(&phba->hbalock);
2312 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2313 spin_unlock_irq(&phba->hbalock);
2314
dea31012005-04-17 16:05:31 -05002315 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002316 do {
2317 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2318 if (pmb == NULL)
2319 break;
2320
James Smart04c68492009-05-22 14:52:52 -04002321 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002322
James Smart858c9f62007-06-17 19:56:39 -05002323 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2324 if (pmb->vport) {
2325 lpfc_debugfs_disc_trc(pmb->vport,
2326 LPFC_DISC_TRC_MBOX_VPORT,
2327 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2328 (uint32_t)pmbox->mbxCommand,
2329 pmbox->un.varWords[0],
2330 pmbox->un.varWords[1]);
2331 }
2332 else {
2333 lpfc_debugfs_disc_trc(phba->pport,
2334 LPFC_DISC_TRC_MBOX,
2335 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2336 (uint32_t)pmbox->mbxCommand,
2337 pmbox->un.varWords[0],
2338 pmbox->un.varWords[1]);
2339 }
2340 }
2341
dea31012005-04-17 16:05:31 -05002342 /*
2343 * It is a fatal error if unknown mbox command completion.
2344 */
2345 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2346 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002347 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002349 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002350 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002351 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002352 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002353 lpfc_sli_config_mbox_subsys_get(phba,
2354 pmb),
2355 lpfc_sli_config_mbox_opcode_get(phba,
2356 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002357 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002358 phba->work_hs = HS_FFER3;
2359 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002360 continue;
dea31012005-04-17 16:05:31 -05002361 }
2362
dea31012005-04-17 16:05:31 -05002363 if (pmbox->mbxStatus) {
2364 phba->sli.slistat.mbox_stat_err++;
2365 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2366 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002367 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002368 LOG_MBOX | LOG_SLI,
2369 "(%d):0305 Mbox cmd cmpl "
2370 "error - RETRYing Data: x%x "
2371 "(x%x/x%x) x%x x%x x%x\n",
2372 pmb->vport ? pmb->vport->vpi : 0,
2373 pmbox->mbxCommand,
2374 lpfc_sli_config_mbox_subsys_get(phba,
2375 pmb),
2376 lpfc_sli_config_mbox_opcode_get(phba,
2377 pmb),
2378 pmbox->mbxStatus,
2379 pmbox->un.varWords[0],
2380 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002381 pmbox->mbxStatus = 0;
2382 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002383 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002384 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002385 continue;
dea31012005-04-17 16:05:31 -05002386 }
2387 }
2388
2389 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002390 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002391 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002392 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2393 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002394 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002395 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002396 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2397 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002398 pmb->mbox_cmpl,
2399 *((uint32_t *) pmbox),
2400 pmbox->un.varWords[0],
2401 pmbox->un.varWords[1],
2402 pmbox->un.varWords[2],
2403 pmbox->un.varWords[3],
2404 pmbox->un.varWords[4],
2405 pmbox->un.varWords[5],
2406 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002407 pmbox->un.varWords[7],
2408 pmbox->un.varWords[8],
2409 pmbox->un.varWords[9],
2410 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002411
James Smart92d7f7b2007-06-17 19:56:38 -05002412 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002413 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002414 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002415 return 0;
dea31012005-04-17 16:05:31 -05002416}
James Smart92d7f7b2007-06-17 19:56:38 -05002417
James Smarte59058c2008-08-24 21:49:00 -04002418/**
James Smart3621a712009-04-06 18:47:14 -04002419 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002420 * @phba: Pointer to HBA context object.
2421 * @pring: Pointer to driver SLI ring object.
2422 * @tag: buffer tag.
2423 *
2424 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2425 * is set in the tag the buffer is posted for a particular exchange,
2426 * the function will return the buffer without replacing the buffer.
2427 * If the buffer is for unsolicited ELS or CT traffic, this function
2428 * returns the buffer and also posts another buffer to the firmware.
2429 **/
James Smart76bb24e2007-10-27 13:38:00 -04002430static struct lpfc_dmabuf *
2431lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002432 struct lpfc_sli_ring *pring,
2433 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002434{
James Smart9f1e1b52008-12-04 22:39:40 -05002435 struct hbq_dmabuf *hbq_entry;
2436
James Smart76bb24e2007-10-27 13:38:00 -04002437 if (tag & QUE_BUFTAG_BIT)
2438 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002439 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2440 if (!hbq_entry)
2441 return NULL;
2442 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002443}
James Smart57127f12007-10-27 13:37:05 -04002444
James Smart3772a992009-05-22 14:50:54 -04002445/**
2446 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2447 * @phba: Pointer to HBA context object.
2448 * @pring: Pointer to driver SLI ring object.
2449 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2450 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2451 * @fch_type: the type for the first frame of the sequence.
2452 *
2453 * This function is called with no lock held. This function uses the r_ctl and
2454 * type of the received sequence to find the correct callback function to call
2455 * to process the sequence.
2456 **/
2457static int
2458lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2459 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2460 uint32_t fch_type)
2461{
2462 int i;
2463
2464 /* unSolicited Responses */
2465 if (pring->prt[0].profile) {
2466 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2467 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2468 saveq);
2469 return 1;
2470 }
2471 /* We must search, based on rctl / type
2472 for the right routine */
2473 for (i = 0; i < pring->num_mask; i++) {
2474 if ((pring->prt[i].rctl == fch_r_ctl) &&
2475 (pring->prt[i].type == fch_type)) {
2476 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2477 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2478 (phba, pring, saveq);
2479 return 1;
2480 }
2481 }
2482 return 0;
2483}
James Smarte59058c2008-08-24 21:49:00 -04002484
2485/**
James Smart3621a712009-04-06 18:47:14 -04002486 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002487 * @phba: Pointer to HBA context object.
2488 * @pring: Pointer to driver SLI ring object.
2489 * @saveq: Pointer to the unsolicited iocb.
2490 *
2491 * This function is called with no lock held by the ring event handler
2492 * when there is an unsolicited iocb posted to the response ring by the
2493 * firmware. This function gets the buffer associated with the iocbs
2494 * and calls the event handler for the ring. This function handles both
2495 * qring buffers and hbq buffers.
2496 * When the function returns 1 the caller can free the iocb object otherwise
2497 * upper layer functions will free the iocb objects.
2498 **/
dea31012005-04-17 16:05:31 -05002499static int
2500lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2501 struct lpfc_iocbq *saveq)
2502{
2503 IOCB_t * irsp;
2504 WORD5 * w5p;
2505 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002506 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002507 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002508
dea31012005-04-17 16:05:31 -05002509 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002510
2511 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2512 if (pring->lpfc_sli_rcv_async_status)
2513 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2514 else
2515 lpfc_printf_log(phba,
2516 KERN_WARNING,
2517 LOG_SLI,
2518 "0316 Ring %d handler: unexpected "
2519 "ASYNC_STATUS iocb received evt_code "
2520 "0x%x\n",
2521 pring->ringno,
2522 irsp->un.asyncstat.evt_code);
2523 return 1;
2524 }
2525
James Smart3163f722008-02-08 18:50:25 -05002526 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2527 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2528 if (irsp->ulpBdeCount > 0) {
2529 dmzbuf = lpfc_sli_get_buff(phba, pring,
2530 irsp->un.ulpWord[3]);
2531 lpfc_in_buf_free(phba, dmzbuf);
2532 }
2533
2534 if (irsp->ulpBdeCount > 1) {
2535 dmzbuf = lpfc_sli_get_buff(phba, pring,
2536 irsp->unsli3.sli3Words[3]);
2537 lpfc_in_buf_free(phba, dmzbuf);
2538 }
2539
2540 if (irsp->ulpBdeCount > 2) {
2541 dmzbuf = lpfc_sli_get_buff(phba, pring,
2542 irsp->unsli3.sli3Words[7]);
2543 lpfc_in_buf_free(phba, dmzbuf);
2544 }
2545
2546 return 1;
2547 }
2548
James Smart92d7f7b2007-06-17 19:56:38 -05002549 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002550 if (irsp->ulpBdeCount != 0) {
2551 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002552 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002553 if (!saveq->context2)
2554 lpfc_printf_log(phba,
2555 KERN_ERR,
2556 LOG_SLI,
2557 "0341 Ring %d Cannot find buffer for "
2558 "an unsolicited iocb. tag 0x%x\n",
2559 pring->ringno,
2560 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002561 }
2562 if (irsp->ulpBdeCount == 2) {
2563 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002564 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002565 if (!saveq->context3)
2566 lpfc_printf_log(phba,
2567 KERN_ERR,
2568 LOG_SLI,
2569 "0342 Ring %d Cannot find buffer for an"
2570 " unsolicited iocb. tag 0x%x\n",
2571 pring->ringno,
2572 irsp->unsli3.sli3Words[7]);
2573 }
2574 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002575 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002576 if (irsp->ulpBdeCount != 0) {
2577 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2578 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002579 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002580 lpfc_printf_log(phba,
2581 KERN_ERR,
2582 LOG_SLI,
2583 "0343 Ring %d Cannot find "
2584 "buffer for an unsolicited iocb"
2585 ". tag 0x%x\n", pring->ringno,
2586 irsp->un.ulpWord[3]);
2587 }
2588 if (irsp->ulpBdeCount == 2) {
2589 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2590 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002591 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002592 lpfc_printf_log(phba,
2593 KERN_ERR,
2594 LOG_SLI,
2595 "0344 Ring %d Cannot find "
2596 "buffer for an unsolicited "
2597 "iocb. tag 0x%x\n",
2598 pring->ringno,
2599 irsp->unsli3.sli3Words[7]);
2600 }
2601 }
James Smart92d7f7b2007-06-17 19:56:38 -05002602 }
James Smart9c2face2008-01-11 01:53:18 -05002603 if (irsp->ulpBdeCount != 0 &&
2604 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2605 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2606 int found = 0;
2607
2608 /* search continue save q for same XRI */
2609 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002610 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2611 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002612 list_add_tail(&saveq->list, &iocbq->list);
2613 found = 1;
2614 break;
2615 }
2616 }
2617 if (!found)
2618 list_add_tail(&saveq->clist,
2619 &pring->iocb_continue_saveq);
2620 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2621 list_del_init(&iocbq->clist);
2622 saveq = iocbq;
2623 irsp = &(saveq->iocb);
2624 } else
2625 return 0;
2626 }
2627 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2628 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2629 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002630 Rctl = FC_RCTL_ELS_REQ;
2631 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002632 } else {
2633 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2634 Rctl = w5p->hcsw.Rctl;
2635 Type = w5p->hcsw.Type;
2636
2637 /* Firmware Workaround */
2638 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2639 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2640 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002641 Rctl = FC_RCTL_ELS_REQ;
2642 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002643 w5p->hcsw.Rctl = Rctl;
2644 w5p->hcsw.Type = Type;
2645 }
2646 }
James Smart92d7f7b2007-06-17 19:56:38 -05002647
James Smart3772a992009-05-22 14:50:54 -04002648 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002649 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002650 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002651 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002652 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002653
James Smart92d7f7b2007-06-17 19:56:38 -05002654 return 1;
dea31012005-04-17 16:05:31 -05002655}
2656
James Smarte59058c2008-08-24 21:49:00 -04002657/**
James Smart3621a712009-04-06 18:47:14 -04002658 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002659 * @phba: Pointer to HBA context object.
2660 * @pring: Pointer to driver SLI ring object.
2661 * @prspiocb: Pointer to response iocb object.
2662 *
2663 * This function looks up the iocb_lookup table to get the command iocb
2664 * corresponding to the given response iocb using the iotag of the
2665 * response iocb. This function is called with the hbalock held.
2666 * This function returns the command iocb object if it finds the command
2667 * iocb else returns NULL.
2668 **/
dea31012005-04-17 16:05:31 -05002669static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002670lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2671 struct lpfc_sli_ring *pring,
2672 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002673{
dea31012005-04-17 16:05:31 -05002674 struct lpfc_iocbq *cmd_iocb = NULL;
2675 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002676 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002677
James Bottomley604a3e32005-10-29 10:28:33 -05002678 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002679
James Bottomley604a3e32005-10-29 10:28:33 -05002680 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2681 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002682 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002683 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002684 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002685 }
James Bottomley604a3e32005-10-29 10:28:33 -05002686 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002687 }
2688
dea31012005-04-17 16:05:31 -05002689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002690 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002691 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002692 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002693 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002694 return NULL;
2695}
2696
James Smarte59058c2008-08-24 21:49:00 -04002697/**
James Smart3772a992009-05-22 14:50:54 -04002698 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2699 * @phba: Pointer to HBA context object.
2700 * @pring: Pointer to driver SLI ring object.
2701 * @iotag: IOCB tag.
2702 *
2703 * This function looks up the iocb_lookup table to get the command iocb
2704 * corresponding to the given iotag. This function is called with the
2705 * hbalock held.
2706 * This function returns the command iocb object if it finds the command
2707 * iocb else returns NULL.
2708 **/
2709static struct lpfc_iocbq *
2710lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2711 struct lpfc_sli_ring *pring, uint16_t iotag)
2712{
2713 struct lpfc_iocbq *cmd_iocb;
2714
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002715 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002716 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2717 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002718 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2719 /* remove from txcmpl queue list */
2720 list_del_init(&cmd_iocb->list);
2721 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002722 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002723 }
James Smart3772a992009-05-22 14:50:54 -04002724 }
James Smart3772a992009-05-22 14:50:54 -04002725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2726 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2727 iotag, phba->sli.last_iotag);
2728 return NULL;
2729}
2730
2731/**
James Smart3621a712009-04-06 18:47:14 -04002732 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002733 * @phba: Pointer to HBA context object.
2734 * @pring: Pointer to driver SLI ring object.
2735 * @saveq: Pointer to the response iocb to be processed.
2736 *
2737 * This function is called by the ring event handler for non-fcp
2738 * rings when there is a new response iocb in the response ring.
2739 * The caller is not required to hold any locks. This function
2740 * gets the command iocb associated with the response iocb and
2741 * calls the completion handler for the command iocb. If there
2742 * is no completion handler, the function will free the resources
2743 * associated with command iocb. If the response iocb is for
2744 * an already aborted command iocb, the status of the completion
2745 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2746 * This function always returns 1.
2747 **/
dea31012005-04-17 16:05:31 -05002748static int
James Smart2e0fef82007-06-17 19:56:36 -05002749lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002750 struct lpfc_iocbq *saveq)
2751{
James Smart2e0fef82007-06-17 19:56:36 -05002752 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002753 int rc = 1;
2754 unsigned long iflag;
2755
2756 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002757 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002758 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002759 spin_unlock_irqrestore(&phba->hbalock, iflag);
2760
dea31012005-04-17 16:05:31 -05002761 if (cmdiocbp) {
2762 if (cmdiocbp->iocb_cmpl) {
2763 /*
James Smartea2151b2008-09-07 11:52:10 -04002764 * If an ELS command failed send an event to mgmt
2765 * application.
2766 */
2767 if (saveq->iocb.ulpStatus &&
2768 (pring->ringno == LPFC_ELS_RING) &&
2769 (cmdiocbp->iocb.ulpCommand ==
2770 CMD_ELS_REQUEST64_CR))
2771 lpfc_send_els_failure_event(phba,
2772 cmdiocbp, saveq);
2773
2774 /*
dea31012005-04-17 16:05:31 -05002775 * Post all ELS completions to the worker thread.
2776 * All other are passed to the completion callback.
2777 */
2778 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002779 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2780 (cmdiocbp->iocb_flag &
2781 LPFC_DRIVER_ABORTED)) {
2782 spin_lock_irqsave(&phba->hbalock,
2783 iflag);
James Smart07951072007-04-25 09:51:38 -04002784 cmdiocbp->iocb_flag &=
2785 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002786 spin_unlock_irqrestore(&phba->hbalock,
2787 iflag);
James Smart07951072007-04-25 09:51:38 -04002788 saveq->iocb.ulpStatus =
2789 IOSTAT_LOCAL_REJECT;
2790 saveq->iocb.un.ulpWord[4] =
2791 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002792
2793 /* Firmware could still be in progress
2794 * of DMAing payload, so don't free data
2795 * buffer till after a hbeat.
2796 */
James Smart341af102010-01-26 23:07:37 -05002797 spin_lock_irqsave(&phba->hbalock,
2798 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002799 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002800 spin_unlock_irqrestore(&phba->hbalock,
2801 iflag);
2802 }
James Smart0f65ff62010-02-26 14:14:23 -05002803 if (phba->sli_rev == LPFC_SLI_REV4) {
2804 if (saveq->iocb_flag &
2805 LPFC_EXCHANGE_BUSY) {
2806 /* Set cmdiocb flag for the
2807 * exchange busy so sgl (xri)
2808 * will not be released until
2809 * the abort xri is received
2810 * from hba.
2811 */
2812 spin_lock_irqsave(
2813 &phba->hbalock, iflag);
2814 cmdiocbp->iocb_flag |=
2815 LPFC_EXCHANGE_BUSY;
2816 spin_unlock_irqrestore(
2817 &phba->hbalock, iflag);
2818 }
2819 if (cmdiocbp->iocb_flag &
2820 LPFC_DRIVER_ABORTED) {
2821 /*
2822 * Clear LPFC_DRIVER_ABORTED
2823 * bit in case it was driver
2824 * initiated abort.
2825 */
2826 spin_lock_irqsave(
2827 &phba->hbalock, iflag);
2828 cmdiocbp->iocb_flag &=
2829 ~LPFC_DRIVER_ABORTED;
2830 spin_unlock_irqrestore(
2831 &phba->hbalock, iflag);
2832 cmdiocbp->iocb.ulpStatus =
2833 IOSTAT_LOCAL_REJECT;
2834 cmdiocbp->iocb.un.ulpWord[4] =
2835 IOERR_ABORT_REQUESTED;
2836 /*
2837 * For SLI4, irsiocb contains
2838 * NO_XRI in sli_xritag, it
2839 * shall not affect releasing
2840 * sgl (xri) process.
2841 */
2842 saveq->iocb.ulpStatus =
2843 IOSTAT_LOCAL_REJECT;
2844 saveq->iocb.un.ulpWord[4] =
2845 IOERR_SLI_ABORTED;
2846 spin_lock_irqsave(
2847 &phba->hbalock, iflag);
2848 saveq->iocb_flag |=
2849 LPFC_DELAY_MEM_FREE;
2850 spin_unlock_irqrestore(
2851 &phba->hbalock, iflag);
2852 }
James Smart07951072007-04-25 09:51:38 -04002853 }
dea31012005-04-17 16:05:31 -05002854 }
James Smart2e0fef82007-06-17 19:56:36 -05002855 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002856 } else
2857 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002858 } else {
2859 /*
2860 * Unknown initiating command based on the response iotag.
2861 * This could be the case on the ELS ring because of
2862 * lpfc_els_abort().
2863 */
2864 if (pring->ringno != LPFC_ELS_RING) {
2865 /*
2866 * Ring <ringno> handler: unexpected completion IoTag
2867 * <IoTag>
2868 */
James Smarta257bf92009-04-06 18:48:10 -04002869 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002870 "0322 Ring %d handler: "
2871 "unexpected completion IoTag x%x "
2872 "Data: x%x x%x x%x x%x\n",
2873 pring->ringno,
2874 saveq->iocb.ulpIoTag,
2875 saveq->iocb.ulpStatus,
2876 saveq->iocb.un.ulpWord[4],
2877 saveq->iocb.ulpCommand,
2878 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002879 }
2880 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002881
dea31012005-04-17 16:05:31 -05002882 return rc;
2883}
2884
James Smarte59058c2008-08-24 21:49:00 -04002885/**
James Smart3621a712009-04-06 18:47:14 -04002886 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002887 * @phba: Pointer to HBA context object.
2888 * @pring: Pointer to driver SLI ring object.
2889 *
2890 * This function is called from the iocb ring event handlers when
2891 * put pointer is ahead of the get pointer for a ring. This function signal
2892 * an error attention condition to the worker thread and the worker
2893 * thread will transition the HBA to offline state.
2894 **/
James Smart2e0fef82007-06-17 19:56:36 -05002895static void
2896lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002897{
James Smart34b02dc2008-08-24 21:49:55 -04002898 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002899 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002900 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002901 * rsp ring <portRspMax>
2902 */
2903 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002904 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002905 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002906 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002907 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002908
James Smart2e0fef82007-06-17 19:56:36 -05002909 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002910
2911 /*
2912 * All error attention handlers are posted to
2913 * worker thread
2914 */
2915 phba->work_ha |= HA_ERATT;
2916 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002917
James Smart5e9d9b82008-06-14 22:52:53 -04002918 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002919
2920 return;
2921}
2922
James Smarte59058c2008-08-24 21:49:00 -04002923/**
James Smart3621a712009-04-06 18:47:14 -04002924 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002925 * @ptr: Pointer to address of HBA context object.
2926 *
2927 * This function is invoked by the Error Attention polling timer when the
2928 * timer times out. It will check the SLI Error Attention register for
2929 * possible attention events. If so, it will post an Error Attention event
2930 * and wake up worker thread to process it. Otherwise, it will set up the
2931 * Error Attention polling timer for the next poll.
2932 **/
2933void lpfc_poll_eratt(unsigned long ptr)
2934{
2935 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04002936 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04002937 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002938
2939 phba = (struct lpfc_hba *)ptr;
2940
James Smartaa6fbb72012-08-03 12:36:03 -04002941 /* Here we will also keep track of interrupts per sec of the hba */
2942 sli_intr = phba->sli.slistat.sli_intr;
2943
2944 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2945 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2946 sli_intr);
2947 else
2948 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2949
James Smart65791f12016-07-06 12:35:56 -07002950 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2951 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04002952 phba->sli.slistat.sli_ips = cnt;
2953
2954 phba->sli.slistat.sli_prev_intr = sli_intr;
2955
James Smart93996272008-08-24 21:50:30 -04002956 /* Check chip HA register for error event */
2957 eratt = lpfc_sli_check_eratt(phba);
2958
2959 if (eratt)
2960 /* Tell the worker thread there is work to do */
2961 lpfc_worker_wake_up(phba);
2962 else
2963 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002964 mod_timer(&phba->eratt_poll,
2965 jiffies +
James Smart65791f12016-07-06 12:35:56 -07002966 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04002967 return;
2968}
2969
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002970
James Smarte59058c2008-08-24 21:49:00 -04002971/**
James Smart3621a712009-04-06 18:47:14 -04002972 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002973 * @phba: Pointer to HBA context object.
2974 * @pring: Pointer to driver SLI ring object.
2975 * @mask: Host attention register mask for this ring.
2976 *
2977 * This function is called from the interrupt context when there is a ring
2978 * event for the fcp ring. The caller does not hold any lock.
2979 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002980 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002981 * LE bit set. The function will call the completion handler of the command iocb
2982 * if the response iocb indicates a completion for a command iocb or it is
2983 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2984 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002985 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002986 * to check it explicitly.
2987 */
2988int
James Smart2e0fef82007-06-17 19:56:36 -05002989lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2990 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002991{
James Smart34b02dc2008-08-24 21:49:55 -04002992 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002993 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002994 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002995 struct lpfc_iocbq *cmdiocbq = NULL;
2996 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002997 uint32_t status;
2998 uint32_t portRspPut, portRspMax;
2999 int rc = 1;
3000 lpfc_iocb_type type;
3001 unsigned long iflag;
3002 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003003
James Smart2e0fef82007-06-17 19:56:36 -05003004 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003005 pring->stats.iocb_event++;
3006
dea31012005-04-17 16:05:31 -05003007 /*
3008 * The next available response entry should never exceed the maximum
3009 * entries. If it does, treat it as an adapter hardware error.
3010 */
James Smart7e56aa22012-08-03 12:35:34 -04003011 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003012 portRspPut = le32_to_cpu(pgp->rspPutInx);
3013 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003014 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003015 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003016 return 1;
3017 }
James Smart45ed1192009-10-02 15:17:02 -04003018 if (phba->fcp_ring_in_use) {
3019 spin_unlock_irqrestore(&phba->hbalock, iflag);
3020 return 1;
3021 } else
3022 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003023
3024 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003025 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003026 /*
3027 * Fetch an entry off the ring and copy it into a local data
3028 * structure. The copy involves a byte-swap since the
3029 * network byte order and pci byte orders are different.
3030 */
James Smarted957682007-06-17 19:56:37 -05003031 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003032 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003033
James Smart7e56aa22012-08-03 12:35:34 -04003034 if (++pring->sli.sli3.rspidx >= portRspMax)
3035 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003036
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003037 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3038 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003039 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003040 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003041 irsp = &rspiocbq.iocb;
3042
dea31012005-04-17 16:05:31 -05003043 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3044 pring->stats.iocb_rsp++;
3045 rsp_cmpl++;
3046
3047 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003048 /*
3049 * If resource errors reported from HBA, reduce
3050 * queuedepths of the SCSI device.
3051 */
3052 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003053 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3054 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003055 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003056 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003057 spin_lock_irqsave(&phba->hbalock, iflag);
3058 }
3059
dea31012005-04-17 16:05:31 -05003060 /* Rsp ring <ringno> error: IOCB */
3061 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003062 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003063 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003064 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003065 irsp->un.ulpWord[0],
3066 irsp->un.ulpWord[1],
3067 irsp->un.ulpWord[2],
3068 irsp->un.ulpWord[3],
3069 irsp->un.ulpWord[4],
3070 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003071 *(uint32_t *)&irsp->un1,
3072 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003073 }
3074
3075 switch (type) {
3076 case LPFC_ABORT_IOCB:
3077 case LPFC_SOL_IOCB:
3078 /*
3079 * Idle exchange closed via ABTS from port. No iocb
3080 * resources need to be recovered.
3081 */
3082 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003083 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003084 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003085 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003086 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003087 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003088 break;
3089 }
3090
James Bottomley604a3e32005-10-29 10:28:33 -05003091 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3092 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003093 if (unlikely(!cmdiocbq))
3094 break;
3095 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3096 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3097 if (cmdiocbq->iocb_cmpl) {
3098 spin_unlock_irqrestore(&phba->hbalock, iflag);
3099 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3100 &rspiocbq);
3101 spin_lock_irqsave(&phba->hbalock, iflag);
3102 }
dea31012005-04-17 16:05:31 -05003103 break;
James Smarta4bc3372006-12-02 13:34:16 -05003104 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003105 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003106 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003107 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003108 break;
dea31012005-04-17 16:05:31 -05003109 default:
3110 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3111 char adaptermsg[LPFC_MAX_ADPTMSG];
3112 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3113 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3114 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003115 dev_warn(&((phba->pcidev)->dev),
3116 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003117 phba->brd_no, adaptermsg);
3118 } else {
3119 /* Unknown IOCB command */
3120 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003121 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003122 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003123 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003124 irsp->ulpStatus,
3125 irsp->ulpIoTag,
3126 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003127 }
3128 break;
3129 }
3130
3131 /*
3132 * The response IOCB has been processed. Update the ring
3133 * pointer in SLIM. If the port response put pointer has not
3134 * been updated, sync the pgp->rspPutInx and fetch the new port
3135 * response put pointer.
3136 */
James Smart7e56aa22012-08-03 12:35:34 -04003137 writel(pring->sli.sli3.rspidx,
3138 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003139
James Smart7e56aa22012-08-03 12:35:34 -04003140 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003141 portRspPut = le32_to_cpu(pgp->rspPutInx);
3142 }
3143
3144 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3145 pring->stats.iocb_rsp_full++;
3146 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3147 writel(status, phba->CAregaddr);
3148 readl(phba->CAregaddr);
3149 }
3150 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3151 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3152 pring->stats.iocb_cmd_empty++;
3153
3154 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003155 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003156 lpfc_sli_resume_iocb(phba, pring);
3157
3158 if ((pring->lpfc_sli_cmd_available))
3159 (pring->lpfc_sli_cmd_available) (phba, pring);
3160
3161 }
3162
James Smart45ed1192009-10-02 15:17:02 -04003163 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003164 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003165 return rc;
3166}
3167
James Smarte59058c2008-08-24 21:49:00 -04003168/**
James Smart3772a992009-05-22 14:50:54 -04003169 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3170 * @phba: Pointer to HBA context object.
3171 * @pring: Pointer to driver SLI ring object.
3172 * @rspiocbp: Pointer to driver response IOCB object.
3173 *
3174 * This function is called from the worker thread when there is a slow-path
3175 * response IOCB to process. This function chains all the response iocbs until
3176 * seeing the iocb with the LE bit set. The function will call
3177 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3178 * completion of a command iocb. The function will call the
3179 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3180 * The function frees the resources or calls the completion handler if this
3181 * iocb is an abort completion. The function returns NULL when the response
3182 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3183 * this function shall chain the iocb on to the iocb_continueq and return the
3184 * response iocb passed in.
3185 **/
3186static struct lpfc_iocbq *
3187lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3188 struct lpfc_iocbq *rspiocbp)
3189{
3190 struct lpfc_iocbq *saveq;
3191 struct lpfc_iocbq *cmdiocbp;
3192 struct lpfc_iocbq *next_iocb;
3193 IOCB_t *irsp = NULL;
3194 uint32_t free_saveq;
3195 uint8_t iocb_cmd_type;
3196 lpfc_iocb_type type;
3197 unsigned long iflag;
3198 int rc;
3199
3200 spin_lock_irqsave(&phba->hbalock, iflag);
3201 /* First add the response iocb to the countinueq list */
3202 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3203 pring->iocb_continueq_cnt++;
3204
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003205 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003206 irsp = &rspiocbp->iocb;
3207 if (irsp->ulpLe) {
3208 /*
3209 * By default, the driver expects to free all resources
3210 * associated with this iocb completion.
3211 */
3212 free_saveq = 1;
3213 saveq = list_get_first(&pring->iocb_continueq,
3214 struct lpfc_iocbq, list);
3215 irsp = &(saveq->iocb);
3216 list_del_init(&pring->iocb_continueq);
3217 pring->iocb_continueq_cnt = 0;
3218
3219 pring->stats.iocb_rsp++;
3220
3221 /*
3222 * If resource errors reported from HBA, reduce
3223 * queuedepths of the SCSI device.
3224 */
3225 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003226 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3227 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003228 spin_unlock_irqrestore(&phba->hbalock, iflag);
3229 phba->lpfc_rampdown_queue_depth(phba);
3230 spin_lock_irqsave(&phba->hbalock, iflag);
3231 }
3232
3233 if (irsp->ulpStatus) {
3234 /* Rsp ring <ringno> error: IOCB */
3235 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3236 "0328 Rsp Ring %d error: "
3237 "IOCB Data: "
3238 "x%x x%x x%x x%x "
3239 "x%x x%x x%x x%x "
3240 "x%x x%x x%x x%x "
3241 "x%x x%x x%x x%x\n",
3242 pring->ringno,
3243 irsp->un.ulpWord[0],
3244 irsp->un.ulpWord[1],
3245 irsp->un.ulpWord[2],
3246 irsp->un.ulpWord[3],
3247 irsp->un.ulpWord[4],
3248 irsp->un.ulpWord[5],
3249 *(((uint32_t *) irsp) + 6),
3250 *(((uint32_t *) irsp) + 7),
3251 *(((uint32_t *) irsp) + 8),
3252 *(((uint32_t *) irsp) + 9),
3253 *(((uint32_t *) irsp) + 10),
3254 *(((uint32_t *) irsp) + 11),
3255 *(((uint32_t *) irsp) + 12),
3256 *(((uint32_t *) irsp) + 13),
3257 *(((uint32_t *) irsp) + 14),
3258 *(((uint32_t *) irsp) + 15));
3259 }
3260
3261 /*
3262 * Fetch the IOCB command type and call the correct completion
3263 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3264 * get freed back to the lpfc_iocb_list by the discovery
3265 * kernel thread.
3266 */
3267 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3268 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3269 switch (type) {
3270 case LPFC_SOL_IOCB:
3271 spin_unlock_irqrestore(&phba->hbalock, iflag);
3272 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3273 spin_lock_irqsave(&phba->hbalock, iflag);
3274 break;
3275
3276 case LPFC_UNSOL_IOCB:
3277 spin_unlock_irqrestore(&phba->hbalock, iflag);
3278 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3279 spin_lock_irqsave(&phba->hbalock, iflag);
3280 if (!rc)
3281 free_saveq = 0;
3282 break;
3283
3284 case LPFC_ABORT_IOCB:
3285 cmdiocbp = NULL;
3286 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3287 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3288 saveq);
3289 if (cmdiocbp) {
3290 /* Call the specified completion routine */
3291 if (cmdiocbp->iocb_cmpl) {
3292 spin_unlock_irqrestore(&phba->hbalock,
3293 iflag);
3294 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3295 saveq);
3296 spin_lock_irqsave(&phba->hbalock,
3297 iflag);
3298 } else
3299 __lpfc_sli_release_iocbq(phba,
3300 cmdiocbp);
3301 }
3302 break;
3303
3304 case LPFC_UNKNOWN_IOCB:
3305 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3306 char adaptermsg[LPFC_MAX_ADPTMSG];
3307 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3308 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3309 MAX_MSG_DATA);
3310 dev_warn(&((phba->pcidev)->dev),
3311 "lpfc%d: %s\n",
3312 phba->brd_no, adaptermsg);
3313 } else {
3314 /* Unknown IOCB command */
3315 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3316 "0335 Unknown IOCB "
3317 "command Data: x%x "
3318 "x%x x%x x%x\n",
3319 irsp->ulpCommand,
3320 irsp->ulpStatus,
3321 irsp->ulpIoTag,
3322 irsp->ulpContext);
3323 }
3324 break;
3325 }
3326
3327 if (free_saveq) {
3328 list_for_each_entry_safe(rspiocbp, next_iocb,
3329 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003330 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003331 __lpfc_sli_release_iocbq(phba, rspiocbp);
3332 }
3333 __lpfc_sli_release_iocbq(phba, saveq);
3334 }
3335 rspiocbp = NULL;
3336 }
3337 spin_unlock_irqrestore(&phba->hbalock, iflag);
3338 return rspiocbp;
3339}
3340
3341/**
3342 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003343 * @phba: Pointer to HBA context object.
3344 * @pring: Pointer to driver SLI ring object.
3345 * @mask: Host attention register mask for this ring.
3346 *
James Smart3772a992009-05-22 14:50:54 -04003347 * This routine wraps the actual slow_ring event process routine from the
3348 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003349 **/
James Smart3772a992009-05-22 14:50:54 -04003350void
James Smart2e0fef82007-06-17 19:56:36 -05003351lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3352 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003353{
James Smart3772a992009-05-22 14:50:54 -04003354 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3355}
3356
3357/**
3358 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3359 * @phba: Pointer to HBA context object.
3360 * @pring: Pointer to driver SLI ring object.
3361 * @mask: Host attention register mask for this ring.
3362 *
3363 * This function is called from the worker thread when there is a ring event
3364 * for non-fcp rings. The caller does not hold any lock. The function will
3365 * remove each response iocb in the response ring and calls the handle
3366 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3367 **/
3368static void
3369lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3370 struct lpfc_sli_ring *pring, uint32_t mask)
3371{
James Smart34b02dc2008-08-24 21:49:55 -04003372 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003373 IOCB_t *entry;
3374 IOCB_t *irsp = NULL;
3375 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003376 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003377 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003378 uint32_t status;
dea31012005-04-17 16:05:31 -05003379
James Smart34b02dc2008-08-24 21:49:55 -04003380 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003381 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003382 pring->stats.iocb_event++;
3383
dea31012005-04-17 16:05:31 -05003384 /*
3385 * The next available response entry should never exceed the maximum
3386 * entries. If it does, treat it as an adapter hardware error.
3387 */
James Smart7e56aa22012-08-03 12:35:34 -04003388 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003389 portRspPut = le32_to_cpu(pgp->rspPutInx);
3390 if (portRspPut >= portRspMax) {
3391 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003392 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003393 * rsp ring <portRspMax>
3394 */
James Smarted957682007-06-17 19:56:37 -05003395 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003396 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003397 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003398 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003399
James Smart2e0fef82007-06-17 19:56:36 -05003400 phba->link_state = LPFC_HBA_ERROR;
3401 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003402
3403 phba->work_hs = HS_FFER3;
3404 lpfc_handle_eratt(phba);
3405
James Smart3772a992009-05-22 14:50:54 -04003406 return;
dea31012005-04-17 16:05:31 -05003407 }
3408
3409 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003410 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003411 /*
3412 * Build a completion list and call the appropriate handler.
3413 * The process is to get the next available response iocb, get
3414 * a free iocb from the list, copy the response data into the
3415 * free iocb, insert to the continuation list, and update the
3416 * next response index to slim. This process makes response
3417 * iocb's in the ring available to DMA as fast as possible but
3418 * pays a penalty for a copy operation. Since the iocb is
3419 * only 32 bytes, this penalty is considered small relative to
3420 * the PCI reads for register values and a slim write. When
3421 * the ulpLe field is set, the entire Command has been
3422 * received.
3423 */
James Smarted957682007-06-17 19:56:37 -05003424 entry = lpfc_resp_iocb(phba, pring);
3425
James Smart858c9f62007-06-17 19:56:39 -05003426 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003427 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003428 if (rspiocbp == NULL) {
3429 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003430 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003431 break;
3432 }
3433
James Smarted957682007-06-17 19:56:37 -05003434 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3435 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003436 irsp = &rspiocbp->iocb;
3437
James Smart7e56aa22012-08-03 12:35:34 -04003438 if (++pring->sli.sli3.rspidx >= portRspMax)
3439 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003440
James Smarta58cbd52007-08-02 11:09:43 -04003441 if (pring->ringno == LPFC_ELS_RING) {
3442 lpfc_debugfs_slow_ring_trc(phba,
3443 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3444 *(((uint32_t *) irsp) + 4),
3445 *(((uint32_t *) irsp) + 6),
3446 *(((uint32_t *) irsp) + 7));
3447 }
3448
James Smart7e56aa22012-08-03 12:35:34 -04003449 writel(pring->sli.sli3.rspidx,
3450 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003451
James Smart3772a992009-05-22 14:50:54 -04003452 spin_unlock_irqrestore(&phba->hbalock, iflag);
3453 /* Handle the response IOCB */
3454 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3455 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003456
3457 /*
3458 * If the port response put pointer has not been updated, sync
3459 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3460 * response put pointer.
3461 */
James Smart7e56aa22012-08-03 12:35:34 -04003462 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003463 portRspPut = le32_to_cpu(pgp->rspPutInx);
3464 }
James Smart7e56aa22012-08-03 12:35:34 -04003465 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003466
James Smart92d7f7b2007-06-17 19:56:38 -05003467 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003468 /* At least one response entry has been freed */
3469 pring->stats.iocb_rsp_full++;
3470 /* SET RxRE_RSP in Chip Att register */
3471 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3472 writel(status, phba->CAregaddr);
3473 readl(phba->CAregaddr); /* flush */
3474 }
3475 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3476 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3477 pring->stats.iocb_cmd_empty++;
3478
3479 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003480 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003481 lpfc_sli_resume_iocb(phba, pring);
3482
3483 if ((pring->lpfc_sli_cmd_available))
3484 (pring->lpfc_sli_cmd_available) (phba, pring);
3485
3486 }
3487
James Smart2e0fef82007-06-17 19:56:36 -05003488 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003489 return;
dea31012005-04-17 16:05:31 -05003490}
3491
James Smarte59058c2008-08-24 21:49:00 -04003492/**
James Smart4f774512009-05-22 14:52:35 -04003493 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3494 * @phba: Pointer to HBA context object.
3495 * @pring: Pointer to driver SLI ring object.
3496 * @mask: Host attention register mask for this ring.
3497 *
3498 * This function is called from the worker thread when there is a pending
3499 * ELS response iocb on the driver internal slow-path response iocb worker
3500 * queue. The caller does not hold any lock. The function will remove each
3501 * response iocb from the response worker queue and calls the handle
3502 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3503 **/
3504static void
3505lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3506 struct lpfc_sli_ring *pring, uint32_t mask)
3507{
3508 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003509 struct hbq_dmabuf *dmabuf;
3510 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003511 unsigned long iflag;
3512
James Smart45ed1192009-10-02 15:17:02 -04003513 spin_lock_irqsave(&phba->hbalock, iflag);
3514 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3515 spin_unlock_irqrestore(&phba->hbalock, iflag);
3516 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003517 /* Get the response iocb from the head of work queue */
3518 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003519 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003520 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003521 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003522
3523 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3524 case CQE_CODE_COMPL_WQE:
3525 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3526 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003527 /* Translate ELS WCQE to response IOCBQ */
3528 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3529 irspiocbq);
3530 if (irspiocbq)
3531 lpfc_sli_sp_handle_rspiocb(phba, pring,
3532 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003533 break;
3534 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003535 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003536 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3537 cq_event);
3538 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3539 break;
3540 default:
3541 break;
3542 }
James Smart4f774512009-05-22 14:52:35 -04003543 }
3544}
3545
3546/**
James Smart3621a712009-04-06 18:47:14 -04003547 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003548 * @phba: Pointer to HBA context object.
3549 * @pring: Pointer to driver SLI ring object.
3550 *
3551 * This function aborts all iocbs in the given ring and frees all the iocb
3552 * objects in txq. This function issues an abort iocb for all the iocb commands
3553 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3554 * the return of this function. The caller is not required to hold any locks.
3555 **/
James Smart2e0fef82007-06-17 19:56:36 -05003556void
dea31012005-04-17 16:05:31 -05003557lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3558{
James Smart2534ba72007-04-25 09:52:20 -04003559 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003560 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003561
James Smart92d7f7b2007-06-17 19:56:38 -05003562 if (pring->ringno == LPFC_ELS_RING) {
3563 lpfc_fabric_abort_hba(phba);
3564 }
3565
dea31012005-04-17 16:05:31 -05003566 /* Error everything on txq and txcmplq
3567 * First do the txq.
3568 */
James Smartdb55fba2014-04-04 13:52:02 -04003569 if (phba->sli_rev >= LPFC_SLI_REV4) {
3570 spin_lock_irq(&pring->ring_lock);
3571 list_splice_init(&pring->txq, &completions);
3572 pring->txq_cnt = 0;
3573 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003574
James Smartdb55fba2014-04-04 13:52:02 -04003575 spin_lock_irq(&phba->hbalock);
3576 /* Next issue ABTS for everything on the txcmplq */
3577 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3578 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3579 spin_unlock_irq(&phba->hbalock);
3580 } else {
3581 spin_lock_irq(&phba->hbalock);
3582 list_splice_init(&pring->txq, &completions);
3583 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003584
James Smartdb55fba2014-04-04 13:52:02 -04003585 /* Next issue ABTS for everything on the txcmplq */
3586 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3587 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3588 spin_unlock_irq(&phba->hbalock);
3589 }
James Smart2534ba72007-04-25 09:52:20 -04003590
James Smarta257bf92009-04-06 18:48:10 -04003591 /* Cancel all the IOCBs from the completions list */
3592 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3593 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003594}
3595
James Smarte59058c2008-08-24 21:49:00 -04003596/**
James Smartdb55fba2014-04-04 13:52:02 -04003597 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3598 * @phba: Pointer to HBA context object.
3599 * @pring: Pointer to driver SLI ring object.
3600 *
3601 * This function aborts all iocbs in FCP rings and frees all the iocb
3602 * objects in txq. This function issues an abort iocb for all the iocb commands
3603 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3604 * the return of this function. The caller is not required to hold any locks.
3605 **/
3606void
3607lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3608{
3609 struct lpfc_sli *psli = &phba->sli;
3610 struct lpfc_sli_ring *pring;
3611 uint32_t i;
3612
3613 /* Look on all the FCP Rings for the iotag */
3614 if (phba->sli_rev >= LPFC_SLI_REV4) {
3615 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3616 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3617 lpfc_sli_abort_iocb_ring(phba, pring);
3618 }
3619 } else {
3620 pring = &psli->ring[psli->fcp_ring];
3621 lpfc_sli_abort_iocb_ring(phba, pring);
3622 }
3623}
3624
3625
3626/**
James Smart3621a712009-04-06 18:47:14 -04003627 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003628 * @phba: Pointer to HBA context object.
3629 *
3630 * This function flushes all iocbs in the fcp ring and frees all the iocb
3631 * objects in txq and txcmplq. This function will not issue abort iocbs
3632 * for all the iocb commands in txcmplq, they will just be returned with
3633 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3634 * slot has been permanently disabled.
3635 **/
3636void
3637lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3638{
3639 LIST_HEAD(txq);
3640 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003641 struct lpfc_sli *psli = &phba->sli;
3642 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003643 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003644
3645 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003646 /* Indicate the I/O queues are flushed */
3647 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003648 spin_unlock_irq(&phba->hbalock);
3649
James Smartdb55fba2014-04-04 13:52:02 -04003650 /* Look on all the FCP Rings for the iotag */
3651 if (phba->sli_rev >= LPFC_SLI_REV4) {
3652 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3653 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
James Smarta8e497d2008-08-24 21:50:11 -04003654
James Smartdb55fba2014-04-04 13:52:02 -04003655 spin_lock_irq(&pring->ring_lock);
3656 /* Retrieve everything on txq */
3657 list_splice_init(&pring->txq, &txq);
3658 /* Retrieve everything on the txcmplq */
3659 list_splice_init(&pring->txcmplq, &txcmplq);
3660 pring->txq_cnt = 0;
3661 pring->txcmplq_cnt = 0;
3662 spin_unlock_irq(&pring->ring_lock);
3663
3664 /* Flush the txq */
3665 lpfc_sli_cancel_iocbs(phba, &txq,
3666 IOSTAT_LOCAL_REJECT,
3667 IOERR_SLI_DOWN);
3668 /* Flush the txcmpq */
3669 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3670 IOSTAT_LOCAL_REJECT,
3671 IOERR_SLI_DOWN);
3672 }
3673 } else {
3674 pring = &psli->ring[psli->fcp_ring];
3675
3676 spin_lock_irq(&phba->hbalock);
3677 /* Retrieve everything on txq */
3678 list_splice_init(&pring->txq, &txq);
3679 /* Retrieve everything on the txcmplq */
3680 list_splice_init(&pring->txcmplq, &txcmplq);
3681 pring->txq_cnt = 0;
3682 pring->txcmplq_cnt = 0;
3683 spin_unlock_irq(&phba->hbalock);
3684
3685 /* Flush the txq */
3686 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3687 IOERR_SLI_DOWN);
3688 /* Flush the txcmpq */
3689 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3690 IOERR_SLI_DOWN);
3691 }
James Smarta8e497d2008-08-24 21:50:11 -04003692}
3693
3694/**
James Smart3772a992009-05-22 14:50:54 -04003695 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003696 * @phba: Pointer to HBA context object.
3697 * @mask: Bit mask to be checked.
3698 *
3699 * This function reads the host status register and compares
3700 * with the provided bit mask to check if HBA completed
3701 * the restart. This function will wait in a loop for the
3702 * HBA to complete restart. If the HBA does not restart within
3703 * 15 iterations, the function will reset the HBA again. The
3704 * function returns 1 when HBA fail to restart otherwise returns
3705 * zero.
3706 **/
James Smart3772a992009-05-22 14:50:54 -04003707static int
3708lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003709{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003710 uint32_t status;
3711 int i = 0;
3712 int retval = 0;
dea31012005-04-17 16:05:31 -05003713
Jamie Wellnitz41415862006-02-28 19:25:27 -05003714 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003715 if (lpfc_readl(phba->HSregaddr, &status))
3716 return 1;
dea31012005-04-17 16:05:31 -05003717
Jamie Wellnitz41415862006-02-28 19:25:27 -05003718 /*
3719 * Check status register every 100ms for 5 retries, then every
3720 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3721 * every 2.5 sec for 4.
3722 * Break our of the loop if errors occurred during init.
3723 */
3724 while (((status & mask) != mask) &&
3725 !(status & HS_FFERM) &&
3726 i++ < 20) {
dea31012005-04-17 16:05:31 -05003727
Jamie Wellnitz41415862006-02-28 19:25:27 -05003728 if (i <= 5)
3729 msleep(10);
3730 else if (i <= 10)
3731 msleep(500);
3732 else
3733 msleep(2500);
dea31012005-04-17 16:05:31 -05003734
Jamie Wellnitz41415862006-02-28 19:25:27 -05003735 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003736 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003737 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003738 lpfc_sli_brdrestart(phba);
3739 }
3740 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003741 if (lpfc_readl(phba->HSregaddr, &status)) {
3742 retval = 1;
3743 break;
3744 }
dea31012005-04-17 16:05:31 -05003745 }
dea31012005-04-17 16:05:31 -05003746
Jamie Wellnitz41415862006-02-28 19:25:27 -05003747 /* Check to see if any errors occurred during init */
3748 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3750 "2751 Adapter failed to restart, "
3751 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3752 status,
3753 readl(phba->MBslimaddr + 0xa8),
3754 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003755 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003756 retval = 1;
3757 }
dea31012005-04-17 16:05:31 -05003758
Jamie Wellnitz41415862006-02-28 19:25:27 -05003759 return retval;
dea31012005-04-17 16:05:31 -05003760}
3761
James Smartda0436e2009-05-22 14:51:39 -04003762/**
3763 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3764 * @phba: Pointer to HBA context object.
3765 * @mask: Bit mask to be checked.
3766 *
3767 * This function checks the host status register to check if HBA is
3768 * ready. This function will wait in a loop for the HBA to be ready
3769 * If the HBA is not ready , the function will will reset the HBA PCI
3770 * function again. The function returns 1 when HBA fail to be ready
3771 * otherwise returns zero.
3772 **/
3773static int
3774lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3775{
3776 uint32_t status;
3777 int retval = 0;
3778
3779 /* Read the HBA Host Status Register */
3780 status = lpfc_sli4_post_status_check(phba);
3781
3782 if (status) {
3783 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3784 lpfc_sli_brdrestart(phba);
3785 status = lpfc_sli4_post_status_check(phba);
3786 }
3787
3788 /* Check to see if any errors occurred during init */
3789 if (status) {
3790 phba->link_state = LPFC_HBA_ERROR;
3791 retval = 1;
3792 } else
3793 phba->sli4_hba.intr_enable = 0;
3794
3795 return retval;
3796}
3797
3798/**
3799 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3800 * @phba: Pointer to HBA context object.
3801 * @mask: Bit mask to be checked.
3802 *
3803 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3804 * from the API jump table function pointer from the lpfc_hba struct.
3805 **/
3806int
3807lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3808{
3809 return phba->lpfc_sli_brdready(phba, mask);
3810}
3811
James Smart92908312006-03-07 15:04:13 -05003812#define BARRIER_TEST_PATTERN (0xdeadbeef)
3813
James Smarte59058c2008-08-24 21:49:00 -04003814/**
James Smart3621a712009-04-06 18:47:14 -04003815 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003816 * @phba: Pointer to HBA context object.
3817 *
James Smart1b511972011-12-13 13:23:09 -05003818 * This function is called before resetting an HBA. This function is called
3819 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003820 **/
James Smart2e0fef82007-06-17 19:56:36 -05003821void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003822{
James Smart65a29c12006-07-06 15:50:50 -04003823 uint32_t __iomem *resp_buf;
3824 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003825 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003826 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003827 int i;
3828 uint8_t hdrtype;
3829
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003830 lockdep_assert_held(&phba->hbalock);
3831
James Smart92908312006-03-07 15:04:13 -05003832 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3833 if (hdrtype != 0x80 ||
3834 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3835 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3836 return;
3837
3838 /*
3839 * Tell the other part of the chip to suspend temporarily all
3840 * its DMA activity.
3841 */
James Smart65a29c12006-07-06 15:50:50 -04003842 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003843
3844 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003845 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3846 return;
James Smart92908312006-03-07 15:04:13 -05003847 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3848 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003849 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003850
James Smart9940b972011-03-11 16:06:12 -05003851 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3852 return;
3853 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003854 /* Clear Chip error bit */
3855 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003856 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003857 }
3858
3859 mbox = 0;
3860 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3861 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3862
3863 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003864 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003865 writel(mbox, mbox_buf);
3866
James Smart9940b972011-03-11 16:06:12 -05003867 for (i = 0; i < 50; i++) {
3868 if (lpfc_readl((resp_buf + 1), &resp_data))
3869 return;
3870 if (resp_data != ~(BARRIER_TEST_PATTERN))
3871 mdelay(1);
3872 else
3873 break;
3874 }
3875 resp_data = 0;
3876 if (lpfc_readl((resp_buf + 1), &resp_data))
3877 return;
3878 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003879 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003880 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003881 goto restore_hc;
3882 else
3883 goto clear_errat;
3884 }
3885
3886 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003887 resp_data = 0;
3888 for (i = 0; i < 500; i++) {
3889 if (lpfc_readl(resp_buf, &resp_data))
3890 return;
3891 if (resp_data != mbox)
3892 mdelay(1);
3893 else
3894 break;
3895 }
James Smart92908312006-03-07 15:04:13 -05003896
3897clear_errat:
3898
James Smart9940b972011-03-11 16:06:12 -05003899 while (++i < 500) {
3900 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3901 return;
3902 if (!(ha_copy & HA_ERATT))
3903 mdelay(1);
3904 else
3905 break;
3906 }
James Smart92908312006-03-07 15:04:13 -05003907
3908 if (readl(phba->HAregaddr) & HA_ERATT) {
3909 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003910 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003911 }
3912
3913restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003914 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003915 writel(hc_copy, phba->HCregaddr);
3916 readl(phba->HCregaddr); /* flush */
3917}
3918
James Smarte59058c2008-08-24 21:49:00 -04003919/**
James Smart3621a712009-04-06 18:47:14 -04003920 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003921 * @phba: Pointer to HBA context object.
3922 *
3923 * This function issues a kill_board mailbox command and waits for
3924 * the error attention interrupt. This function is called for stopping
3925 * the firmware processing. The caller is not required to hold any
3926 * locks. This function calls lpfc_hba_down_post function to free
3927 * any pending commands after the kill. The function will return 1 when it
3928 * fails to kill the board else will return 0.
3929 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003930int
James Smart2e0fef82007-06-17 19:56:36 -05003931lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003932{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003933 struct lpfc_sli *psli;
3934 LPFC_MBOXQ_t *pmb;
3935 uint32_t status;
3936 uint32_t ha_copy;
3937 int retval;
3938 int i = 0;
3939
3940 psli = &phba->sli;
3941
3942 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003943 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003944 "0329 Kill HBA Data: x%x x%x\n",
3945 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003946
James Smart98c9ea52007-10-27 13:37:33 -04003947 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3948 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003949 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003950
3951 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003952 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003953 if (lpfc_readl(phba->HCregaddr, &status)) {
3954 spin_unlock_irq(&phba->hbalock);
3955 mempool_free(pmb, phba->mbox_mem_pool);
3956 return 1;
3957 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003958 status &= ~HC_ERINT_ENA;
3959 writel(status, phba->HCregaddr);
3960 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003961 phba->link_flag |= LS_IGNORE_ERATT;
3962 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003963
3964 lpfc_kill_board(phba, pmb);
3965 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3966 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3967
3968 if (retval != MBX_SUCCESS) {
3969 if (retval != MBX_BUSY)
3970 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003971 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3972 "2752 KILL_BOARD command failed retval %d\n",
3973 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003974 spin_lock_irq(&phba->hbalock);
3975 phba->link_flag &= ~LS_IGNORE_ERATT;
3976 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003977 return 1;
3978 }
3979
James Smartf4b4c682009-05-22 14:53:12 -04003980 spin_lock_irq(&phba->hbalock);
3981 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3982 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003983
Jamie Wellnitz41415862006-02-28 19:25:27 -05003984 mempool_free(pmb, phba->mbox_mem_pool);
3985
3986 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3987 * attention every 100ms for 3 seconds. If we don't get ERATT after
3988 * 3 seconds we still set HBA_ERROR state because the status of the
3989 * board is now undefined.
3990 */
James Smart9940b972011-03-11 16:06:12 -05003991 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3992 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003993 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3994 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003995 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3996 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003997 }
3998
3999 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004000 if (ha_copy & HA_ERATT) {
4001 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004002 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004003 }
James Smart2e0fef82007-06-17 19:56:36 -05004004 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004005 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004006 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004007 phba->link_flag &= ~LS_IGNORE_ERATT;
4008 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004009
Jamie Wellnitz41415862006-02-28 19:25:27 -05004010 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004011 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004012
James Smart2e0fef82007-06-17 19:56:36 -05004013 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004014}
4015
James Smarte59058c2008-08-24 21:49:00 -04004016/**
James Smart3772a992009-05-22 14:50:54 -04004017 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004018 * @phba: Pointer to HBA context object.
4019 *
4020 * This function resets the HBA by writing HC_INITFF to the control
4021 * register. After the HBA resets, this function resets all the iocb ring
4022 * indices. This function disables PCI layer parity checking during
4023 * the reset.
4024 * This function returns 0 always.
4025 * The caller is not required to hold any locks.
4026 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004027int
James Smart2e0fef82007-06-17 19:56:36 -05004028lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004029{
4030 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004031 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004032 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004033 int i;
dea31012005-04-17 16:05:31 -05004034
Jamie Wellnitz41415862006-02-28 19:25:27 -05004035 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004036
Jamie Wellnitz41415862006-02-28 19:25:27 -05004037 /* Reset HBA */
4038 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004039 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004040 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004041
4042 /* perform board reset */
4043 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004044 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004045 phba->pport->fc_myDID = 0;
4046 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004047
Jamie Wellnitz41415862006-02-28 19:25:27 -05004048 /* Turn off parity checking and serr during the physical reset */
4049 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4050 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4051 (cfg_value &
4052 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4053
James Smart3772a992009-05-22 14:50:54 -04004054 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4055
Jamie Wellnitz41415862006-02-28 19:25:27 -05004056 /* Now toggle INITFF bit in the Host Control Register */
4057 writel(HC_INITFF, phba->HCregaddr);
4058 mdelay(1);
4059 readl(phba->HCregaddr); /* flush */
4060 writel(0, phba->HCregaddr);
4061 readl(phba->HCregaddr); /* flush */
4062
4063 /* Restore PCI cmd register */
4064 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004065
4066 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004067 for (i = 0; i < psli->num_rings; i++) {
4068 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05004069 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004070 pring->sli.sli3.rspidx = 0;
4071 pring->sli.sli3.next_cmdidx = 0;
4072 pring->sli.sli3.local_getidx = 0;
4073 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004074 pring->missbufcnt = 0;
4075 }
dea31012005-04-17 16:05:31 -05004076
James Smart2e0fef82007-06-17 19:56:36 -05004077 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004078 return 0;
4079}
4080
James Smarte59058c2008-08-24 21:49:00 -04004081/**
James Smartda0436e2009-05-22 14:51:39 -04004082 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4083 * @phba: Pointer to HBA context object.
4084 *
4085 * This function resets a SLI4 HBA. This function disables PCI layer parity
4086 * checking during resets the device. The caller is not required to hold
4087 * any locks.
4088 *
4089 * This function returns 0 always.
4090 **/
4091int
4092lpfc_sli4_brdreset(struct lpfc_hba *phba)
4093{
4094 struct lpfc_sli *psli = &phba->sli;
4095 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004096 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004097
4098 /* Reset HBA */
4099 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004100 "0295 Reset HBA Data: x%x x%x x%x\n",
4101 phba->pport->port_state, psli->sli_flag,
4102 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004103
4104 /* perform board reset */
4105 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004106 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004107 phba->pport->fc_myDID = 0;
4108 phba->pport->fc_prevDID = 0;
4109
James Smartda0436e2009-05-22 14:51:39 -04004110 spin_lock_irq(&phba->hbalock);
4111 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4112 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004113 spin_unlock_irq(&phba->hbalock);
4114
James Smart02936352014-04-04 13:52:12 -04004115 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4116 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4117 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4118 return rc;
4119 }
4120
James Smartda0436e2009-05-22 14:51:39 -04004121 /* Now physically reset the device */
4122 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4123 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004124
4125 /* Turn off parity checking and serr during the physical reset */
4126 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4127 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4128 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4129
James Smart88318812012-09-29 11:29:29 -04004130 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004131 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004132 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004133
James Smartbe858b62010-12-15 17:57:20 -05004134 /* Restore PCI cmd register */
4135 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4136
James Smart27b01b82012-05-09 21:19:44 -04004137 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004138}
4139
4140/**
4141 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004142 * @phba: Pointer to HBA context object.
4143 *
4144 * This function is called in the SLI initialization code path to
4145 * restart the HBA. The caller is not required to hold any lock.
4146 * This function writes MBX_RESTART mailbox command to the SLIM and
4147 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4148 * function to free any pending commands. The function enables
4149 * POST only during the first initialization. The function returns zero.
4150 * The function does not guarantee completion of MBX_RESTART mailbox
4151 * command before the return of this function.
4152 **/
James Smartda0436e2009-05-22 14:51:39 -04004153static int
4154lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004155{
4156 MAILBOX_t *mb;
4157 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004158 volatile uint32_t word0;
4159 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004160 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004161
James Smart2e0fef82007-06-17 19:56:36 -05004162 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004163
James Smart0d878412009-10-02 15:16:56 -04004164 /* Take PCIe device Advanced Error Reporting (AER) state */
4165 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4166
Jamie Wellnitz41415862006-02-28 19:25:27 -05004167 psli = &phba->sli;
4168
4169 /* Restart HBA */
4170 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004171 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004172 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004173
4174 word0 = 0;
4175 mb = (MAILBOX_t *) &word0;
4176 mb->mbxCommand = MBX_RESTART;
4177 mb->mbxHc = 1;
4178
James Smart92908312006-03-07 15:04:13 -05004179 lpfc_reset_barrier(phba);
4180
Jamie Wellnitz41415862006-02-28 19:25:27 -05004181 to_slim = phba->MBslimaddr;
4182 writel(*(uint32_t *) mb, to_slim);
4183 readl(to_slim); /* flush */
4184
4185 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004186 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004187 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004188 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004189 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004190 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004191 writel(*(uint32_t *) mb, to_slim);
4192 readl(to_slim); /* flush */
4193
4194 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004195 phba->pport->stopped = 0;
4196 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004197 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004198 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004199
James Smart64ba8812006-08-02 15:24:34 -04004200 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4201 psli->stats_start = get_seconds();
4202
James Smarteaf15d52008-12-04 22:39:29 -05004203 /* Give the INITFF and Post time to settle. */
4204 mdelay(100);
dea31012005-04-17 16:05:31 -05004205
James Smart0d878412009-10-02 15:16:56 -04004206 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4207 if (hba_aer_enabled)
4208 pci_disable_pcie_error_reporting(phba->pcidev);
4209
Jamie Wellnitz41415862006-02-28 19:25:27 -05004210 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004211
4212 return 0;
4213}
4214
James Smarte59058c2008-08-24 21:49:00 -04004215/**
James Smartda0436e2009-05-22 14:51:39 -04004216 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4217 * @phba: Pointer to HBA context object.
4218 *
4219 * This function is called in the SLI initialization code path to restart
4220 * a SLI4 HBA. The caller is not required to hold any lock.
4221 * At the end of the function, it calls lpfc_hba_down_post function to
4222 * free any pending commands.
4223 **/
4224static int
4225lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4226{
4227 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004228 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004229 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004230
4231 /* Restart HBA */
4232 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4233 "0296 Restart HBA Data: x%x x%x\n",
4234 phba->pport->port_state, psli->sli_flag);
4235
James Smart75baf692010-06-08 18:31:21 -04004236 /* Take PCIe device Advanced Error Reporting (AER) state */
4237 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4238
James Smart27b01b82012-05-09 21:19:44 -04004239 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004240
4241 spin_lock_irq(&phba->hbalock);
4242 phba->pport->stopped = 0;
4243 phba->link_state = LPFC_INIT_START;
4244 phba->hba_flag = 0;
4245 spin_unlock_irq(&phba->hbalock);
4246
4247 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4248 psli->stats_start = get_seconds();
4249
James Smart75baf692010-06-08 18:31:21 -04004250 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4251 if (hba_aer_enabled)
4252 pci_disable_pcie_error_reporting(phba->pcidev);
4253
James Smartda0436e2009-05-22 14:51:39 -04004254 lpfc_hba_down_post(phba);
4255
James Smart27b01b82012-05-09 21:19:44 -04004256 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004257}
4258
4259/**
4260 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4261 * @phba: Pointer to HBA context object.
4262 *
4263 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4264 * API jump table function pointer from the lpfc_hba struct.
4265**/
4266int
4267lpfc_sli_brdrestart(struct lpfc_hba *phba)
4268{
4269 return phba->lpfc_sli_brdrestart(phba);
4270}
4271
4272/**
James Smart3621a712009-04-06 18:47:14 -04004273 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004274 * @phba: Pointer to HBA context object.
4275 *
4276 * This function is called after a HBA restart to wait for successful
4277 * restart of the HBA. Successful restart of the HBA is indicated by
4278 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4279 * iteration, the function will restart the HBA again. The function returns
4280 * zero if HBA successfully restarted else returns negative error code.
4281 **/
dea31012005-04-17 16:05:31 -05004282static int
4283lpfc_sli_chipset_init(struct lpfc_hba *phba)
4284{
4285 uint32_t status, i = 0;
4286
4287 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004288 if (lpfc_readl(phba->HSregaddr, &status))
4289 return -EIO;
dea31012005-04-17 16:05:31 -05004290
4291 /* Check status register to see what current state is */
4292 i = 0;
4293 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4294
James Smartdcf2a4e2010-09-29 11:18:53 -04004295 /* Check every 10ms for 10 retries, then every 100ms for 90
4296 * retries, then every 1 sec for 50 retires for a total of
4297 * ~60 seconds before reset the board again and check every
4298 * 1 sec for 50 retries. The up to 60 seconds before the
4299 * board ready is required by the Falcon FIPS zeroization
4300 * complete, and any reset the board in between shall cause
4301 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004302 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004303 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004304 /* Adapter failed to init, timeout, status reg
4305 <status> */
James Smarted957682007-06-17 19:56:37 -05004306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004307 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004308 "timeout, status reg x%x, "
4309 "FW Data: A8 x%x AC x%x\n", status,
4310 readl(phba->MBslimaddr + 0xa8),
4311 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004312 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004313 return -ETIMEDOUT;
4314 }
4315
4316 /* Check to see if any errors occurred during init */
4317 if (status & HS_FFERM) {
4318 /* ERROR: During chipset initialization */
4319 /* Adapter failed to init, chipset, status reg
4320 <status> */
James Smarted957682007-06-17 19:56:37 -05004321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004322 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004323 "chipset, status reg x%x, "
4324 "FW Data: A8 x%x AC x%x\n", status,
4325 readl(phba->MBslimaddr + 0xa8),
4326 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004327 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004328 return -EIO;
4329 }
4330
James Smartdcf2a4e2010-09-29 11:18:53 -04004331 if (i <= 10)
dea31012005-04-17 16:05:31 -05004332 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004333 else if (i <= 100)
4334 msleep(100);
4335 else
4336 msleep(1000);
dea31012005-04-17 16:05:31 -05004337
James Smartdcf2a4e2010-09-29 11:18:53 -04004338 if (i == 150) {
4339 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004340 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004341 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004342 }
4343 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004344 if (lpfc_readl(phba->HSregaddr, &status))
4345 return -EIO;
dea31012005-04-17 16:05:31 -05004346 }
4347
4348 /* Check to see if any errors occurred during init */
4349 if (status & HS_FFERM) {
4350 /* ERROR: During chipset initialization */
4351 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004353 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004354 "status reg x%x, "
4355 "FW Data: A8 x%x AC x%x\n", status,
4356 readl(phba->MBslimaddr + 0xa8),
4357 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004358 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004359 return -EIO;
4360 }
4361
4362 /* Clear all interrupt enable conditions */
4363 writel(0, phba->HCregaddr);
4364 readl(phba->HCregaddr); /* flush */
4365
4366 /* setup host attn register */
4367 writel(0xffffffff, phba->HAregaddr);
4368 readl(phba->HAregaddr); /* flush */
4369 return 0;
4370}
4371
James Smarte59058c2008-08-24 21:49:00 -04004372/**
James Smart3621a712009-04-06 18:47:14 -04004373 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004374 *
4375 * This function calculates and returns the number of HBQs required to be
4376 * configured.
4377 **/
James Smart78b2d852007-08-02 11:10:21 -04004378int
James Smarted957682007-06-17 19:56:37 -05004379lpfc_sli_hbq_count(void)
4380{
James Smart92d7f7b2007-06-17 19:56:38 -05004381 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004382}
4383
James Smarte59058c2008-08-24 21:49:00 -04004384/**
James Smart3621a712009-04-06 18:47:14 -04004385 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004386 *
4387 * This function adds the number of hbq entries in every HBQ to get
4388 * the total number of hbq entries required for the HBA and returns
4389 * the total count.
4390 **/
James Smarted957682007-06-17 19:56:37 -05004391static int
4392lpfc_sli_hbq_entry_count(void)
4393{
4394 int hbq_count = lpfc_sli_hbq_count();
4395 int count = 0;
4396 int i;
4397
4398 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004399 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004400 return count;
4401}
4402
James Smarte59058c2008-08-24 21:49:00 -04004403/**
James Smart3621a712009-04-06 18:47:14 -04004404 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004405 *
4406 * This function calculates amount of memory required for all hbq entries
4407 * to be configured and returns the total memory required.
4408 **/
dea31012005-04-17 16:05:31 -05004409int
James Smarted957682007-06-17 19:56:37 -05004410lpfc_sli_hbq_size(void)
4411{
4412 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4413}
4414
James Smarte59058c2008-08-24 21:49:00 -04004415/**
James Smart3621a712009-04-06 18:47:14 -04004416 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004417 * @phba: Pointer to HBA context object.
4418 *
4419 * This function is called during the SLI initialization to configure
4420 * all the HBQs and post buffers to the HBQ. The caller is not
4421 * required to hold any locks. This function will return zero if successful
4422 * else it will return negative error code.
4423 **/
James Smarted957682007-06-17 19:56:37 -05004424static int
4425lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4426{
4427 int hbq_count = lpfc_sli_hbq_count();
4428 LPFC_MBOXQ_t *pmb;
4429 MAILBOX_t *pmbox;
4430 uint32_t hbqno;
4431 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004432
James Smart92d7f7b2007-06-17 19:56:38 -05004433 /* Get a Mailbox buffer to setup mailbox
4434 * commands for HBA initialization
4435 */
James Smarted957682007-06-17 19:56:37 -05004436 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4437
4438 if (!pmb)
4439 return -ENOMEM;
4440
James Smart04c68492009-05-22 14:52:52 -04004441 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004442
4443 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4444 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004445 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004446
4447 hbq_entry_index = 0;
4448 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4449 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4450 phba->hbqs[hbqno].hbqPutIdx = 0;
4451 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4452 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004453 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004454 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4455 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004456 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4457
4458 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4459 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4460 mbxStatus <status>, ring <num> */
4461
4462 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004463 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004464 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004465 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004466 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004467 pmbox->mbxStatus, hbqno);
4468
4469 phba->link_state = LPFC_HBA_ERROR;
4470 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004471 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004472 }
4473 }
4474 phba->hbq_count = hbq_count;
4475
James Smarted957682007-06-17 19:56:37 -05004476 mempool_free(pmb, phba->mbox_mem_pool);
4477
James Smart92d7f7b2007-06-17 19:56:38 -05004478 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004479 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4480 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004481 return 0;
4482}
4483
James Smarte59058c2008-08-24 21:49:00 -04004484/**
James Smart4f774512009-05-22 14:52:35 -04004485 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4486 * @phba: Pointer to HBA context object.
4487 *
4488 * This function is called during the SLI initialization to configure
4489 * all the HBQs and post buffers to the HBQ. The caller is not
4490 * required to hold any locks. This function will return zero if successful
4491 * else it will return negative error code.
4492 **/
4493static int
4494lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4495{
4496 phba->hbq_in_use = 1;
4497 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4498 phba->hbq_count = 1;
4499 /* Initially populate or replenish the HBQs */
4500 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4501 return 0;
4502}
4503
4504/**
James Smart3621a712009-04-06 18:47:14 -04004505 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004506 * @phba: Pointer to HBA context object.
4507 * @sli_mode: sli mode - 2/3
4508 *
4509 * This function is called by the sli intialization code path
4510 * to issue config_port mailbox command. This function restarts the
4511 * HBA firmware and issues a config_port mailbox command to configure
4512 * the SLI interface in the sli mode specified by sli_mode
4513 * variable. The caller is not required to hold any locks.
4514 * The function returns 0 if successful, else returns negative error
4515 * code.
4516 **/
James Smart93996272008-08-24 21:50:30 -04004517int
4518lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004519{
4520 LPFC_MBOXQ_t *pmb;
4521 uint32_t resetcount = 0, rc = 0, done = 0;
4522
4523 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4524 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004525 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004526 return -ENOMEM;
4527 }
4528
James Smarted957682007-06-17 19:56:37 -05004529 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004530 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004531 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004532 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004533 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004534 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004535 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004536 rc = lpfc_sli_chipset_init(phba);
4537 if (rc)
4538 break;
4539
James Smart2e0fef82007-06-17 19:56:36 -05004540 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004541 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004542 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004543 resetcount++;
4544
James Smarted957682007-06-17 19:56:37 -05004545 /* Call pre CONFIG_PORT mailbox command initialization. A
4546 * value of 0 means the call was successful. Any other
4547 * nonzero value is a failure, but if ERESTART is returned,
4548 * the driver may reset the HBA and try again.
4549 */
dea31012005-04-17 16:05:31 -05004550 rc = lpfc_config_port_prep(phba);
4551 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004552 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004553 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004554 } else if (rc)
dea31012005-04-17 16:05:31 -05004555 break;
James Smart6d368e52011-05-24 11:44:12 -04004556
James Smart2e0fef82007-06-17 19:56:36 -05004557 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004558 lpfc_config_port(phba, pmb);
4559 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004560 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4561 LPFC_SLI3_HBQ_ENABLED |
4562 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004563 LPFC_SLI3_BG_ENABLED |
4564 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004565 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004567 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004568 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004569 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004570 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004571 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004572 spin_unlock_irq(&phba->hbalock);
4573 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004574 } else {
4575 /* Allow asynchronous mailbox command to go through */
4576 spin_lock_irq(&phba->hbalock);
4577 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4578 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004579 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004580
4581 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4582 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4583 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4584 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004585 }
dea31012005-04-17 16:05:31 -05004586 }
James Smarted957682007-06-17 19:56:37 -05004587 if (!done) {
4588 rc = -EINVAL;
4589 goto do_prep_failed;
4590 }
James Smart04c68492009-05-22 14:52:52 -04004591 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4592 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004593 rc = -ENXIO;
4594 goto do_prep_failed;
4595 }
James Smart04c68492009-05-22 14:52:52 -04004596 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004597 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004598 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4599 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4600 phba->max_vpi : phba->max_vports;
4601
James Smart34b02dc2008-08-24 21:49:55 -04004602 } else
4603 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004604 phba->fips_level = 0;
4605 phba->fips_spec_rev = 0;
4606 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004607 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004608 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4609 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4610 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4611 "2850 Security Crypto Active. FIPS x%d "
4612 "(Spec Rev: x%d)",
4613 phba->fips_level, phba->fips_spec_rev);
4614 }
4615 if (pmb->u.mb.un.varCfgPort.sec_err) {
4616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4617 "2856 Config Port Security Crypto "
4618 "Error: x%x ",
4619 pmb->u.mb.un.varCfgPort.sec_err);
4620 }
James Smart04c68492009-05-22 14:52:52 -04004621 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004622 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004623 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004624 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004625
4626 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4627 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004628
4629 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004630 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004631 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4632 else
4633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4634 "0443 Adapter did not grant "
4635 "BlockGuard\n");
4636 }
James Smart34b02dc2008-08-24 21:49:55 -04004637 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004638 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004639 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004640 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004641 }
James Smart92d7f7b2007-06-17 19:56:38 -05004642do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004643 mempool_free(pmb, phba->mbox_mem_pool);
4644 return rc;
4645}
4646
James Smarte59058c2008-08-24 21:49:00 -04004647
4648/**
James Smart3621a712009-04-06 18:47:14 -04004649 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004650 * @phba: Pointer to HBA context object.
4651 *
4652 * This function is the main SLI intialization function. This function
4653 * is called by the HBA intialization code, HBA reset code and HBA
4654 * error attention handler code. Caller is not required to hold any
4655 * locks. This function issues config_port mailbox command to configure
4656 * the SLI, setup iocb rings and HBQ rings. In the end the function
4657 * calls the config_port_post function to issue init_link mailbox
4658 * command and to start the discovery. The function will return zero
4659 * if successful, else it will return negative error code.
4660 **/
James Smarted957682007-06-17 19:56:37 -05004661int
4662lpfc_sli_hba_setup(struct lpfc_hba *phba)
4663{
4664 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004665 int mode = 3, i;
4666 int longs;
James Smarted957682007-06-17 19:56:37 -05004667
4668 switch (lpfc_sli_mode) {
4669 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004670 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004672 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004673 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004674 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004675 break;
4676 }
James Smarted957682007-06-17 19:56:37 -05004677 mode = 2;
4678 break;
4679 case 0:
4680 case 3:
4681 break;
4682 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004684 "1819 Unrecognized lpfc_sli_mode "
4685 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004686
4687 break;
4688 }
James Smartb5c53952016-03-31 14:12:30 -07004689 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004690
James Smart93996272008-08-24 21:50:30 -04004691 rc = lpfc_sli_config_port(phba, mode);
4692
James Smarted957682007-06-17 19:56:37 -05004693 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004695 "1820 Unable to select SLI-3. "
4696 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004697 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004698 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004699 if (rc)
dea31012005-04-17 16:05:31 -05004700 goto lpfc_sli_hba_setup_error;
4701
James Smart0d878412009-10-02 15:16:56 -04004702 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4703 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4704 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4705 if (!rc) {
4706 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4707 "2709 This device supports "
4708 "Advanced Error Reporting (AER)\n");
4709 spin_lock_irq(&phba->hbalock);
4710 phba->hba_flag |= HBA_AER_ENABLED;
4711 spin_unlock_irq(&phba->hbalock);
4712 } else {
4713 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4714 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004715 "Advanced Error Reporting (AER): %d\n",
4716 rc);
James Smart0d878412009-10-02 15:16:56 -04004717 phba->cfg_aer_support = 0;
4718 }
4719 }
4720
James Smarted957682007-06-17 19:56:37 -05004721 if (phba->sli_rev == 3) {
4722 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4723 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004724 } else {
4725 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4726 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004727 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004728 }
4729
4730 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004731 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4732 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004733 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004734
4735 if (rc)
4736 goto lpfc_sli_hba_setup_error;
4737
James Smart6d368e52011-05-24 11:44:12 -04004738 /* Initialize VPIs. */
4739 if (phba->sli_rev == LPFC_SLI_REV3) {
4740 /*
4741 * The VPI bitmask and physical ID array are allocated
4742 * and initialized once only - at driver load. A port
4743 * reset doesn't need to reinitialize this memory.
4744 */
4745 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4746 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4747 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4748 GFP_KERNEL);
4749 if (!phba->vpi_bmask) {
4750 rc = -ENOMEM;
4751 goto lpfc_sli_hba_setup_error;
4752 }
4753
4754 phba->vpi_ids = kzalloc(
4755 (phba->max_vpi+1) * sizeof(uint16_t),
4756 GFP_KERNEL);
4757 if (!phba->vpi_ids) {
4758 kfree(phba->vpi_bmask);
4759 rc = -ENOMEM;
4760 goto lpfc_sli_hba_setup_error;
4761 }
4762 for (i = 0; i < phba->max_vpi; i++)
4763 phba->vpi_ids[i] = i;
4764 }
4765 }
4766
James Smart93996272008-08-24 21:50:30 -04004767 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004768 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4769 rc = lpfc_sli_hbq_setup(phba);
4770 if (rc)
4771 goto lpfc_sli_hba_setup_error;
4772 }
James Smart04c68492009-05-22 14:52:52 -04004773 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004774 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004775 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004776
4777 rc = lpfc_config_port_post(phba);
4778 if (rc)
4779 goto lpfc_sli_hba_setup_error;
4780
James Smarted957682007-06-17 19:56:37 -05004781 return rc;
4782
James Smart92d7f7b2007-06-17 19:56:38 -05004783lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004784 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004786 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004787 return rc;
4788}
4789
James Smartda0436e2009-05-22 14:51:39 -04004790/**
4791 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4792 * @phba: Pointer to HBA context object.
4793 * @mboxq: mailbox pointer.
4794 * This function issue a dump mailbox command to read config region
4795 * 23 and parse the records in the region and populate driver
4796 * data structure.
4797 **/
4798static int
James Smartff78d8f2011-12-13 13:21:35 -05004799lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004800{
James Smartff78d8f2011-12-13 13:21:35 -05004801 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004802 struct lpfc_dmabuf *mp;
4803 struct lpfc_mqe *mqe;
4804 uint32_t data_length;
4805 int rc;
4806
4807 /* Program the default value of vlan_id and fc_map */
4808 phba->valid_vlan = 0;
4809 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4810 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4811 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4812
James Smartff78d8f2011-12-13 13:21:35 -05004813 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4814 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004815 return -ENOMEM;
4816
James Smartff78d8f2011-12-13 13:21:35 -05004817 mqe = &mboxq->u.mqe;
4818 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4819 rc = -ENOMEM;
4820 goto out_free_mboxq;
4821 }
4822
James Smartda0436e2009-05-22 14:51:39 -04004823 mp = (struct lpfc_dmabuf *) mboxq->context1;
4824 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4825
4826 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4827 "(%d):2571 Mailbox cmd x%x Status x%x "
4828 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4829 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4830 "CQ: x%x x%x x%x x%x\n",
4831 mboxq->vport ? mboxq->vport->vpi : 0,
4832 bf_get(lpfc_mqe_command, mqe),
4833 bf_get(lpfc_mqe_status, mqe),
4834 mqe->un.mb_words[0], mqe->un.mb_words[1],
4835 mqe->un.mb_words[2], mqe->un.mb_words[3],
4836 mqe->un.mb_words[4], mqe->un.mb_words[5],
4837 mqe->un.mb_words[6], mqe->un.mb_words[7],
4838 mqe->un.mb_words[8], mqe->un.mb_words[9],
4839 mqe->un.mb_words[10], mqe->un.mb_words[11],
4840 mqe->un.mb_words[12], mqe->un.mb_words[13],
4841 mqe->un.mb_words[14], mqe->un.mb_words[15],
4842 mqe->un.mb_words[16], mqe->un.mb_words[50],
4843 mboxq->mcqe.word0,
4844 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4845 mboxq->mcqe.trailer);
4846
4847 if (rc) {
4848 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4849 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004850 rc = -EIO;
4851 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004852 }
4853 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004854 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004855 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4856 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004857 rc = -EIO;
4858 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004859 }
James Smartda0436e2009-05-22 14:51:39 -04004860
4861 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4862 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4863 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004864 rc = 0;
4865
4866out_free_mboxq:
4867 mempool_free(mboxq, phba->mbox_mem_pool);
4868 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004869}
4870
4871/**
4872 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4873 * @phba: pointer to lpfc hba data structure.
4874 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4875 * @vpd: pointer to the memory to hold resulting port vpd data.
4876 * @vpd_size: On input, the number of bytes allocated to @vpd.
4877 * On output, the number of data bytes in @vpd.
4878 *
4879 * This routine executes a READ_REV SLI4 mailbox command. In
4880 * addition, this routine gets the port vpd data.
4881 *
4882 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004883 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004884 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004885 **/
4886static int
4887lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4888 uint8_t *vpd, uint32_t *vpd_size)
4889{
4890 int rc = 0;
4891 uint32_t dma_size;
4892 struct lpfc_dmabuf *dmabuf;
4893 struct lpfc_mqe *mqe;
4894
4895 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4896 if (!dmabuf)
4897 return -ENOMEM;
4898
4899 /*
4900 * Get a DMA buffer for the vpd data resulting from the READ_REV
4901 * mailbox command.
4902 */
4903 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004904 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4905 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004906 if (!dmabuf->virt) {
4907 kfree(dmabuf);
4908 return -ENOMEM;
4909 }
James Smartda0436e2009-05-22 14:51:39 -04004910
4911 /*
4912 * The SLI4 implementation of READ_REV conflicts at word1,
4913 * bits 31:16 and SLI4 adds vpd functionality not present
4914 * in SLI3. This code corrects the conflicts.
4915 */
4916 lpfc_read_rev(phba, mboxq);
4917 mqe = &mboxq->u.mqe;
4918 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4919 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4920 mqe->un.read_rev.word1 &= 0x0000FFFF;
4921 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4922 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4923
4924 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4925 if (rc) {
4926 dma_free_coherent(&phba->pcidev->dev, dma_size,
4927 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004928 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004929 return -EIO;
4930 }
4931
James Smartda0436e2009-05-22 14:51:39 -04004932 /*
4933 * The available vpd length cannot be bigger than the
4934 * DMA buffer passed to the port. Catch the less than
4935 * case and update the caller's size.
4936 */
4937 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4938 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4939
James Smartd7c47992010-06-08 18:31:54 -04004940 memcpy(vpd, dmabuf->virt, *vpd_size);
4941
James Smartda0436e2009-05-22 14:51:39 -04004942 dma_free_coherent(&phba->pcidev->dev, dma_size,
4943 dmabuf->virt, dmabuf->phys);
4944 kfree(dmabuf);
4945 return 0;
4946}
4947
4948/**
James Smartcd1c8302011-10-10 21:33:25 -04004949 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4950 * @phba: pointer to lpfc hba data structure.
4951 *
4952 * This routine retrieves SLI4 device physical port name this PCI function
4953 * is attached to.
4954 *
4955 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004956 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004957 * otherwise - failed to retrieve physical port name
4958 **/
4959static int
4960lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4961{
4962 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004963 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4964 struct lpfc_controller_attribute *cntl_attr;
4965 struct lpfc_mbx_get_port_name *get_port_name;
4966 void *virtaddr = NULL;
4967 uint32_t alloclen, reqlen;
4968 uint32_t shdr_status, shdr_add_status;
4969 union lpfc_sli4_cfg_shdr *shdr;
4970 char cport_name = 0;
4971 int rc;
4972
4973 /* We assume nothing at this point */
4974 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4975 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4976
4977 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4978 if (!mboxq)
4979 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004980 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004981 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4982 lpfc_sli4_read_config(phba);
4983 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4984 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004985
4986 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4987 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4988 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4989 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4990 LPFC_SLI4_MBX_NEMBED);
4991 if (alloclen < reqlen) {
4992 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4993 "3084 Allocated DMA memory size (%d) is "
4994 "less than the requested DMA memory size "
4995 "(%d)\n", alloclen, reqlen);
4996 rc = -ENOMEM;
4997 goto out_free_mboxq;
4998 }
4999 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5000 virtaddr = mboxq->sge_array->addr[0];
5001 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5002 shdr = &mbx_cntl_attr->cfg_shdr;
5003 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5004 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5005 if (shdr_status || shdr_add_status || rc) {
5006 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5007 "3085 Mailbox x%x (x%x/x%x) failed, "
5008 "rc:x%x, status:x%x, add_status:x%x\n",
5009 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5010 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5011 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5012 rc, shdr_status, shdr_add_status);
5013 rc = -ENXIO;
5014 goto out_free_mboxq;
5015 }
5016 cntl_attr = &mbx_cntl_attr->cntl_attr;
5017 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5018 phba->sli4_hba.lnk_info.lnk_tp =
5019 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5020 phba->sli4_hba.lnk_info.lnk_no =
5021 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5022 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5023 "3086 lnk_type:%d, lnk_numb:%d\n",
5024 phba->sli4_hba.lnk_info.lnk_tp,
5025 phba->sli4_hba.lnk_info.lnk_no);
5026
5027retrieve_ppname:
5028 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5029 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5030 sizeof(struct lpfc_mbx_get_port_name) -
5031 sizeof(struct lpfc_sli4_cfg_mhdr),
5032 LPFC_SLI4_MBX_EMBED);
5033 get_port_name = &mboxq->u.mqe.un.get_port_name;
5034 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5035 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5036 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5037 phba->sli4_hba.lnk_info.lnk_tp);
5038 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5039 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5040 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5041 if (shdr_status || shdr_add_status || rc) {
5042 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5043 "3087 Mailbox x%x (x%x/x%x) failed: "
5044 "rc:x%x, status:x%x, add_status:x%x\n",
5045 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5046 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5047 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5048 rc, shdr_status, shdr_add_status);
5049 rc = -ENXIO;
5050 goto out_free_mboxq;
5051 }
5052 switch (phba->sli4_hba.lnk_info.lnk_no) {
5053 case LPFC_LINK_NUMBER_0:
5054 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5055 &get_port_name->u.response);
5056 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5057 break;
5058 case LPFC_LINK_NUMBER_1:
5059 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5060 &get_port_name->u.response);
5061 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5062 break;
5063 case LPFC_LINK_NUMBER_2:
5064 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5065 &get_port_name->u.response);
5066 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5067 break;
5068 case LPFC_LINK_NUMBER_3:
5069 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5070 &get_port_name->u.response);
5071 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5072 break;
5073 default:
5074 break;
5075 }
5076
5077 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5078 phba->Port[0] = cport_name;
5079 phba->Port[1] = '\0';
5080 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5081 "3091 SLI get port name: %s\n", phba->Port);
5082 }
5083
5084out_free_mboxq:
5085 if (rc != MBX_TIMEOUT) {
5086 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5087 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5088 else
5089 mempool_free(mboxq, phba->mbox_mem_pool);
5090 }
5091 return rc;
5092}
5093
5094/**
James Smartda0436e2009-05-22 14:51:39 -04005095 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5096 * @phba: pointer to lpfc hba data structure.
5097 *
5098 * This routine is called to explicitly arm the SLI4 device's completion and
5099 * event queues
5100 **/
5101static void
5102lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5103{
James Smart962bc512013-01-03 15:44:00 -05005104 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04005105
5106 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5107 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04005108 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05005109 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04005110 do {
James Smart2e90f4b2011-12-13 13:22:37 -05005111 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5112 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04005113 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05005114 }
James Smart1ba981f2014-02-20 09:56:45 -05005115
James Smartf38fa0b2014-04-04 13:52:21 -04005116 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005117 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5118
James Smart67d12732012-08-03 12:36:13 -04005119 if (phba->sli4_hba.hba_eq) {
5120 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05005121 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04005122 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05005123 LPFC_QUEUE_REARM);
5124 }
James Smart1ba981f2014-02-20 09:56:45 -05005125
5126 if (phba->cfg_fof)
5127 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005128}
5129
5130/**
James Smart6d368e52011-05-24 11:44:12 -04005131 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5132 * @phba: Pointer to HBA context object.
5133 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005134 * @extnt_count: buffer to hold port available extent count.
5135 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005136 *
James Smartb76f2dc2011-07-22 18:37:42 -04005137 * This function calls the port and retrievs the number of available
5138 * extents and their size for a particular extent type.
5139 *
5140 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005141 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005142int
James Smart6d368e52011-05-24 11:44:12 -04005143lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5144 uint16_t *extnt_count, uint16_t *extnt_size)
5145{
5146 int rc = 0;
5147 uint32_t length;
5148 uint32_t mbox_tmo;
5149 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5150 LPFC_MBOXQ_t *mbox;
5151
5152 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5153 if (!mbox)
5154 return -ENOMEM;
5155
5156 /* Find out how many extents are available for this resource type */
5157 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5158 sizeof(struct lpfc_sli4_cfg_mhdr));
5159 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5160 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5161 length, LPFC_SLI4_MBX_EMBED);
5162
5163 /* Send an extents count of 0 - the GET doesn't use it. */
5164 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5165 LPFC_SLI4_MBX_EMBED);
5166 if (unlikely(rc)) {
5167 rc = -EIO;
5168 goto err_exit;
5169 }
5170
5171 if (!phba->sli4_hba.intr_enable)
5172 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5173 else {
James Smarta183a152011-10-10 21:32:43 -04005174 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005175 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5176 }
5177 if (unlikely(rc)) {
5178 rc = -EIO;
5179 goto err_exit;
5180 }
5181
5182 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5183 if (bf_get(lpfc_mbox_hdr_status,
5184 &rsrc_info->header.cfg_shdr.response)) {
5185 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5186 "2930 Failed to get resource extents "
5187 "Status 0x%x Add'l Status 0x%x\n",
5188 bf_get(lpfc_mbox_hdr_status,
5189 &rsrc_info->header.cfg_shdr.response),
5190 bf_get(lpfc_mbox_hdr_add_status,
5191 &rsrc_info->header.cfg_shdr.response));
5192 rc = -EIO;
5193 goto err_exit;
5194 }
5195
5196 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5197 &rsrc_info->u.rsp);
5198 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5199 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005200
5201 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5202 "3162 Retrieved extents type-%d from port: count:%d, "
5203 "size:%d\n", type, *extnt_count, *extnt_size);
5204
5205err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005206 mempool_free(mbox, phba->mbox_mem_pool);
5207 return rc;
5208}
5209
5210/**
5211 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5212 * @phba: Pointer to HBA context object.
5213 * @type: The extent type to check.
5214 *
5215 * This function reads the current available extents from the port and checks
5216 * if the extent count or extent size has changed since the last access.
5217 * Callers use this routine post port reset to understand if there is a
5218 * extent reprovisioning requirement.
5219 *
5220 * Returns:
5221 * -Error: error indicates problem.
5222 * 1: Extent count or size has changed.
5223 * 0: No changes.
5224 **/
5225static int
5226lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5227{
5228 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5229 uint16_t size_diff, rsrc_ext_size;
5230 int rc = 0;
5231 struct lpfc_rsrc_blks *rsrc_entry;
5232 struct list_head *rsrc_blk_list = NULL;
5233
5234 size_diff = 0;
5235 curr_ext_cnt = 0;
5236 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5237 &rsrc_ext_cnt,
5238 &rsrc_ext_size);
5239 if (unlikely(rc))
5240 return -EIO;
5241
5242 switch (type) {
5243 case LPFC_RSC_TYPE_FCOE_RPI:
5244 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5245 break;
5246 case LPFC_RSC_TYPE_FCOE_VPI:
5247 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5248 break;
5249 case LPFC_RSC_TYPE_FCOE_XRI:
5250 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5251 break;
5252 case LPFC_RSC_TYPE_FCOE_VFI:
5253 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5254 break;
5255 default:
5256 break;
5257 }
5258
5259 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5260 curr_ext_cnt++;
5261 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5262 size_diff++;
5263 }
5264
5265 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5266 rc = 1;
5267
5268 return rc;
5269}
5270
5271/**
5272 * lpfc_sli4_cfg_post_extnts -
5273 * @phba: Pointer to HBA context object.
5274 * @extnt_cnt - number of available extents.
5275 * @type - the extent type (rpi, xri, vfi, vpi).
5276 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5277 * @mbox - pointer to the caller's allocated mailbox structure.
5278 *
5279 * This function executes the extents allocation request. It also
5280 * takes care of the amount of memory needed to allocate or get the
5281 * allocated extents. It is the caller's responsibility to evaluate
5282 * the response.
5283 *
5284 * Returns:
5285 * -Error: Error value describes the condition found.
5286 * 0: if successful
5287 **/
5288static int
James Smart8a9d2e82012-05-09 21:16:12 -04005289lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005290 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5291{
5292 int rc = 0;
5293 uint32_t req_len;
5294 uint32_t emb_len;
5295 uint32_t alloc_len, mbox_tmo;
5296
5297 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005298 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005299
5300 /*
5301 * Calculate the size of an embedded mailbox. The uint32_t
5302 * accounts for extents-specific word.
5303 */
5304 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5305 sizeof(uint32_t);
5306
5307 /*
5308 * Presume the allocation and response will fit into an embedded
5309 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5310 */
5311 *emb = LPFC_SLI4_MBX_EMBED;
5312 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005313 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005314 sizeof(union lpfc_sli4_cfg_shdr) +
5315 sizeof(uint32_t);
5316 *emb = LPFC_SLI4_MBX_NEMBED;
5317 }
5318
5319 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5320 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5321 req_len, *emb);
5322 if (alloc_len < req_len) {
5323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005324 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005325 "less than the requested DMA memory "
5326 "size (x%x)\n", alloc_len, req_len);
5327 return -ENOMEM;
5328 }
James Smart8a9d2e82012-05-09 21:16:12 -04005329 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005330 if (unlikely(rc))
5331 return -EIO;
5332
5333 if (!phba->sli4_hba.intr_enable)
5334 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5335 else {
James Smarta183a152011-10-10 21:32:43 -04005336 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005337 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5338 }
5339
5340 if (unlikely(rc))
5341 rc = -EIO;
5342 return rc;
5343}
5344
5345/**
5346 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5347 * @phba: Pointer to HBA context object.
5348 * @type: The resource extent type to allocate.
5349 *
5350 * This function allocates the number of elements for the specified
5351 * resource type.
5352 **/
5353static int
5354lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5355{
5356 bool emb = false;
5357 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5358 uint16_t rsrc_id, rsrc_start, j, k;
5359 uint16_t *ids;
5360 int i, rc;
5361 unsigned long longs;
5362 unsigned long *bmask;
5363 struct lpfc_rsrc_blks *rsrc_blks;
5364 LPFC_MBOXQ_t *mbox;
5365 uint32_t length;
5366 struct lpfc_id_range *id_array = NULL;
5367 void *virtaddr = NULL;
5368 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5369 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5370 struct list_head *ext_blk_list;
5371
5372 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5373 &rsrc_cnt,
5374 &rsrc_size);
5375 if (unlikely(rc))
5376 return -EIO;
5377
5378 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5379 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5380 "3009 No available Resource Extents "
5381 "for resource type 0x%x: Count: 0x%x, "
5382 "Size 0x%x\n", type, rsrc_cnt,
5383 rsrc_size);
5384 return -ENOMEM;
5385 }
5386
James Smart8a9d2e82012-05-09 21:16:12 -04005387 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5388 "2903 Post resource extents type-0x%x: "
5389 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005390
5391 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5392 if (!mbox)
5393 return -ENOMEM;
5394
James Smart8a9d2e82012-05-09 21:16:12 -04005395 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005396 if (unlikely(rc)) {
5397 rc = -EIO;
5398 goto err_exit;
5399 }
5400
5401 /*
5402 * Figure out where the response is located. Then get local pointers
5403 * to the response data. The port does not guarantee to respond to
5404 * all extents counts request so update the local variable with the
5405 * allocated count from the port.
5406 */
5407 if (emb == LPFC_SLI4_MBX_EMBED) {
5408 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5409 id_array = &rsrc_ext->u.rsp.id[0];
5410 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5411 } else {
5412 virtaddr = mbox->sge_array->addr[0];
5413 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5414 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5415 id_array = &n_rsrc->id;
5416 }
5417
5418 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5419 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5420
5421 /*
5422 * Based on the resource size and count, correct the base and max
5423 * resource values.
5424 */
5425 length = sizeof(struct lpfc_rsrc_blks);
5426 switch (type) {
5427 case LPFC_RSC_TYPE_FCOE_RPI:
5428 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5429 sizeof(unsigned long),
5430 GFP_KERNEL);
5431 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5432 rc = -ENOMEM;
5433 goto err_exit;
5434 }
5435 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5436 sizeof(uint16_t),
5437 GFP_KERNEL);
5438 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5439 kfree(phba->sli4_hba.rpi_bmask);
5440 rc = -ENOMEM;
5441 goto err_exit;
5442 }
5443
5444 /*
5445 * The next_rpi was initialized with the maximum available
5446 * count but the port may allocate a smaller number. Catch
5447 * that case and update the next_rpi.
5448 */
5449 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5450
5451 /* Initialize local ptrs for common extent processing later. */
5452 bmask = phba->sli4_hba.rpi_bmask;
5453 ids = phba->sli4_hba.rpi_ids;
5454 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5455 break;
5456 case LPFC_RSC_TYPE_FCOE_VPI:
5457 phba->vpi_bmask = kzalloc(longs *
5458 sizeof(unsigned long),
5459 GFP_KERNEL);
5460 if (unlikely(!phba->vpi_bmask)) {
5461 rc = -ENOMEM;
5462 goto err_exit;
5463 }
5464 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5465 sizeof(uint16_t),
5466 GFP_KERNEL);
5467 if (unlikely(!phba->vpi_ids)) {
5468 kfree(phba->vpi_bmask);
5469 rc = -ENOMEM;
5470 goto err_exit;
5471 }
5472
5473 /* Initialize local ptrs for common extent processing later. */
5474 bmask = phba->vpi_bmask;
5475 ids = phba->vpi_ids;
5476 ext_blk_list = &phba->lpfc_vpi_blk_list;
5477 break;
5478 case LPFC_RSC_TYPE_FCOE_XRI:
5479 phba->sli4_hba.xri_bmask = kzalloc(longs *
5480 sizeof(unsigned long),
5481 GFP_KERNEL);
5482 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5483 rc = -ENOMEM;
5484 goto err_exit;
5485 }
James Smart8a9d2e82012-05-09 21:16:12 -04005486 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005487 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5488 sizeof(uint16_t),
5489 GFP_KERNEL);
5490 if (unlikely(!phba->sli4_hba.xri_ids)) {
5491 kfree(phba->sli4_hba.xri_bmask);
5492 rc = -ENOMEM;
5493 goto err_exit;
5494 }
5495
5496 /* Initialize local ptrs for common extent processing later. */
5497 bmask = phba->sli4_hba.xri_bmask;
5498 ids = phba->sli4_hba.xri_ids;
5499 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5500 break;
5501 case LPFC_RSC_TYPE_FCOE_VFI:
5502 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5503 sizeof(unsigned long),
5504 GFP_KERNEL);
5505 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5506 rc = -ENOMEM;
5507 goto err_exit;
5508 }
5509 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5510 sizeof(uint16_t),
5511 GFP_KERNEL);
5512 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5513 kfree(phba->sli4_hba.vfi_bmask);
5514 rc = -ENOMEM;
5515 goto err_exit;
5516 }
5517
5518 /* Initialize local ptrs for common extent processing later. */
5519 bmask = phba->sli4_hba.vfi_bmask;
5520 ids = phba->sli4_hba.vfi_ids;
5521 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5522 break;
5523 default:
5524 /* Unsupported Opcode. Fail call. */
5525 id_array = NULL;
5526 bmask = NULL;
5527 ids = NULL;
5528 ext_blk_list = NULL;
5529 goto err_exit;
5530 }
5531
5532 /*
5533 * Complete initializing the extent configuration with the
5534 * allocated ids assigned to this function. The bitmask serves
5535 * as an index into the array and manages the available ids. The
5536 * array just stores the ids communicated to the port via the wqes.
5537 */
5538 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5539 if ((i % 2) == 0)
5540 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5541 &id_array[k]);
5542 else
5543 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5544 &id_array[k]);
5545
5546 rsrc_blks = kzalloc(length, GFP_KERNEL);
5547 if (unlikely(!rsrc_blks)) {
5548 rc = -ENOMEM;
5549 kfree(bmask);
5550 kfree(ids);
5551 goto err_exit;
5552 }
5553 rsrc_blks->rsrc_start = rsrc_id;
5554 rsrc_blks->rsrc_size = rsrc_size;
5555 list_add_tail(&rsrc_blks->list, ext_blk_list);
5556 rsrc_start = rsrc_id;
5557 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5558 phba->sli4_hba.scsi_xri_start = rsrc_start +
5559 lpfc_sli4_get_els_iocb_cnt(phba);
5560
5561 while (rsrc_id < (rsrc_start + rsrc_size)) {
5562 ids[j] = rsrc_id;
5563 rsrc_id++;
5564 j++;
5565 }
5566 /* Entire word processed. Get next word.*/
5567 if ((i % 2) == 1)
5568 k++;
5569 }
5570 err_exit:
5571 lpfc_sli4_mbox_cmd_free(phba, mbox);
5572 return rc;
5573}
5574
5575/**
5576 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5577 * @phba: Pointer to HBA context object.
5578 * @type: the extent's type.
5579 *
5580 * This function deallocates all extents of a particular resource type.
5581 * SLI4 does not allow for deallocating a particular extent range. It
5582 * is the caller's responsibility to release all kernel memory resources.
5583 **/
5584static int
5585lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5586{
5587 int rc;
5588 uint32_t length, mbox_tmo = 0;
5589 LPFC_MBOXQ_t *mbox;
5590 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5591 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5592
5593 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5594 if (!mbox)
5595 return -ENOMEM;
5596
5597 /*
5598 * This function sends an embedded mailbox because it only sends the
5599 * the resource type. All extents of this type are released by the
5600 * port.
5601 */
5602 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5603 sizeof(struct lpfc_sli4_cfg_mhdr));
5604 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5605 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5606 length, LPFC_SLI4_MBX_EMBED);
5607
5608 /* Send an extents count of 0 - the dealloc doesn't use it. */
5609 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5610 LPFC_SLI4_MBX_EMBED);
5611 if (unlikely(rc)) {
5612 rc = -EIO;
5613 goto out_free_mbox;
5614 }
5615 if (!phba->sli4_hba.intr_enable)
5616 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5617 else {
James Smarta183a152011-10-10 21:32:43 -04005618 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005619 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5620 }
5621 if (unlikely(rc)) {
5622 rc = -EIO;
5623 goto out_free_mbox;
5624 }
5625
5626 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5627 if (bf_get(lpfc_mbox_hdr_status,
5628 &dealloc_rsrc->header.cfg_shdr.response)) {
5629 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5630 "2919 Failed to release resource extents "
5631 "for type %d - Status 0x%x Add'l Status 0x%x. "
5632 "Resource memory not released.\n",
5633 type,
5634 bf_get(lpfc_mbox_hdr_status,
5635 &dealloc_rsrc->header.cfg_shdr.response),
5636 bf_get(lpfc_mbox_hdr_add_status,
5637 &dealloc_rsrc->header.cfg_shdr.response));
5638 rc = -EIO;
5639 goto out_free_mbox;
5640 }
5641
5642 /* Release kernel memory resources for the specific type. */
5643 switch (type) {
5644 case LPFC_RSC_TYPE_FCOE_VPI:
5645 kfree(phba->vpi_bmask);
5646 kfree(phba->vpi_ids);
5647 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5648 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5649 &phba->lpfc_vpi_blk_list, list) {
5650 list_del_init(&rsrc_blk->list);
5651 kfree(rsrc_blk);
5652 }
James Smart16a3a202013-04-17 20:14:38 -04005653 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005654 break;
5655 case LPFC_RSC_TYPE_FCOE_XRI:
5656 kfree(phba->sli4_hba.xri_bmask);
5657 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005658 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5659 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5660 list_del_init(&rsrc_blk->list);
5661 kfree(rsrc_blk);
5662 }
5663 break;
5664 case LPFC_RSC_TYPE_FCOE_VFI:
5665 kfree(phba->sli4_hba.vfi_bmask);
5666 kfree(phba->sli4_hba.vfi_ids);
5667 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5668 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5669 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5670 list_del_init(&rsrc_blk->list);
5671 kfree(rsrc_blk);
5672 }
5673 break;
5674 case LPFC_RSC_TYPE_FCOE_RPI:
5675 /* RPI bitmask and physical id array are cleaned up earlier. */
5676 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5677 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5678 list_del_init(&rsrc_blk->list);
5679 kfree(rsrc_blk);
5680 }
5681 break;
5682 default:
5683 break;
5684 }
5685
5686 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5687
5688 out_free_mbox:
5689 mempool_free(mbox, phba->mbox_mem_pool);
5690 return rc;
5691}
5692
James Smart65791f12016-07-06 12:35:56 -07005693void
5694lpfc_set_features(struct lpfc_hba *phba)
5695{
5696 LPFC_MBOXQ_t *mbox = NULL;
5697 uint32_t len;
5698 int rc;
5699
5700 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5701 if (!mbox)
5702 return;
5703 len = sizeof(struct lpfc_mbx_set_feature) -
5704 sizeof(struct lpfc_sli4_cfg_mhdr);
5705 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5706 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5707 LPFC_SLI4_MBX_EMBED);
5708 bf_set(lpfc_mbx_set_feature_UER,
5709 &mbox->u.mqe.un.set_feature, 1);
5710 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5711 mbox->u.mqe.un.set_feature.param_len = 8;
5712 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5713
5714 if (rc != MBX_SUCCESS) {
5715 mempool_free(mbox, phba->mbox_mem_pool);
5716 return;
5717 }
5718 phba->hba_flag |= HBA_RECOVERABLE_UE;
5719 phba->eratt_poll_interval = 1; /* Set 1Sec interval to detect UE */
5720 phba->sli4_hba.ue_to_sr = bf_get(lpfc_mbx_set_feature_UESR,
5721 &mbox->u.mqe.un.set_feature);
5722 phba->sli4_hba.ue_to_rp = bf_get(lpfc_mbx_set_feature_UERP,
5723 &mbox->u.mqe.un.set_feature);
5724 mempool_free(mbox, phba->mbox_mem_pool);
5725}
5726
James Smart6d368e52011-05-24 11:44:12 -04005727/**
5728 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5729 * @phba: Pointer to HBA context object.
5730 *
5731 * This function allocates all SLI4 resource identifiers.
5732 **/
5733int
5734lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5735{
5736 int i, rc, error = 0;
5737 uint16_t count, base;
5738 unsigned long longs;
5739
James Smartff78d8f2011-12-13 13:21:35 -05005740 if (!phba->sli4_hba.rpi_hdrs_in_use)
5741 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005742 if (phba->sli4_hba.extents_in_use) {
5743 /*
5744 * The port supports resource extents. The XRI, VPI, VFI, RPI
5745 * resource extent count must be read and allocated before
5746 * provisioning the resource id arrays.
5747 */
5748 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5749 LPFC_IDX_RSRC_RDY) {
5750 /*
5751 * Extent-based resources are set - the driver could
5752 * be in a port reset. Figure out if any corrective
5753 * actions need to be taken.
5754 */
5755 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5756 LPFC_RSC_TYPE_FCOE_VFI);
5757 if (rc != 0)
5758 error++;
5759 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5760 LPFC_RSC_TYPE_FCOE_VPI);
5761 if (rc != 0)
5762 error++;
5763 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5764 LPFC_RSC_TYPE_FCOE_XRI);
5765 if (rc != 0)
5766 error++;
5767 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5768 LPFC_RSC_TYPE_FCOE_RPI);
5769 if (rc != 0)
5770 error++;
5771
5772 /*
5773 * It's possible that the number of resources
5774 * provided to this port instance changed between
5775 * resets. Detect this condition and reallocate
5776 * resources. Otherwise, there is no action.
5777 */
5778 if (error) {
5779 lpfc_printf_log(phba, KERN_INFO,
5780 LOG_MBOX | LOG_INIT,
5781 "2931 Detected extent resource "
5782 "change. Reallocating all "
5783 "extents.\n");
5784 rc = lpfc_sli4_dealloc_extent(phba,
5785 LPFC_RSC_TYPE_FCOE_VFI);
5786 rc = lpfc_sli4_dealloc_extent(phba,
5787 LPFC_RSC_TYPE_FCOE_VPI);
5788 rc = lpfc_sli4_dealloc_extent(phba,
5789 LPFC_RSC_TYPE_FCOE_XRI);
5790 rc = lpfc_sli4_dealloc_extent(phba,
5791 LPFC_RSC_TYPE_FCOE_RPI);
5792 } else
5793 return 0;
5794 }
5795
5796 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5797 if (unlikely(rc))
5798 goto err_exit;
5799
5800 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5801 if (unlikely(rc))
5802 goto err_exit;
5803
5804 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5805 if (unlikely(rc))
5806 goto err_exit;
5807
5808 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5809 if (unlikely(rc))
5810 goto err_exit;
5811 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5812 LPFC_IDX_RSRC_RDY);
5813 return rc;
5814 } else {
5815 /*
5816 * The port does not support resource extents. The XRI, VPI,
5817 * VFI, RPI resource ids were determined from READ_CONFIG.
5818 * Just allocate the bitmasks and provision the resource id
5819 * arrays. If a port reset is active, the resources don't
5820 * need any action - just exit.
5821 */
5822 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005823 LPFC_IDX_RSRC_RDY) {
5824 lpfc_sli4_dealloc_resource_identifiers(phba);
5825 lpfc_sli4_remove_rpis(phba);
5826 }
James Smart6d368e52011-05-24 11:44:12 -04005827 /* RPIs. */
5828 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005829 if (count <= 0) {
5830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5831 "3279 Invalid provisioning of "
5832 "rpi:%d\n", count);
5833 rc = -EINVAL;
5834 goto err_exit;
5835 }
James Smart6d368e52011-05-24 11:44:12 -04005836 base = phba->sli4_hba.max_cfg_param.rpi_base;
5837 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5838 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5839 sizeof(unsigned long),
5840 GFP_KERNEL);
5841 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5842 rc = -ENOMEM;
5843 goto err_exit;
5844 }
5845 phba->sli4_hba.rpi_ids = kzalloc(count *
5846 sizeof(uint16_t),
5847 GFP_KERNEL);
5848 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5849 rc = -ENOMEM;
5850 goto free_rpi_bmask;
5851 }
5852
5853 for (i = 0; i < count; i++)
5854 phba->sli4_hba.rpi_ids[i] = base + i;
5855
5856 /* VPIs. */
5857 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005858 if (count <= 0) {
5859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5860 "3280 Invalid provisioning of "
5861 "vpi:%d\n", count);
5862 rc = -EINVAL;
5863 goto free_rpi_ids;
5864 }
James Smart6d368e52011-05-24 11:44:12 -04005865 base = phba->sli4_hba.max_cfg_param.vpi_base;
5866 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5867 phba->vpi_bmask = kzalloc(longs *
5868 sizeof(unsigned long),
5869 GFP_KERNEL);
5870 if (unlikely(!phba->vpi_bmask)) {
5871 rc = -ENOMEM;
5872 goto free_rpi_ids;
5873 }
5874 phba->vpi_ids = kzalloc(count *
5875 sizeof(uint16_t),
5876 GFP_KERNEL);
5877 if (unlikely(!phba->vpi_ids)) {
5878 rc = -ENOMEM;
5879 goto free_vpi_bmask;
5880 }
5881
5882 for (i = 0; i < count; i++)
5883 phba->vpi_ids[i] = base + i;
5884
5885 /* XRIs. */
5886 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005887 if (count <= 0) {
5888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5889 "3281 Invalid provisioning of "
5890 "xri:%d\n", count);
5891 rc = -EINVAL;
5892 goto free_vpi_ids;
5893 }
James Smart6d368e52011-05-24 11:44:12 -04005894 base = phba->sli4_hba.max_cfg_param.xri_base;
5895 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5896 phba->sli4_hba.xri_bmask = kzalloc(longs *
5897 sizeof(unsigned long),
5898 GFP_KERNEL);
5899 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5900 rc = -ENOMEM;
5901 goto free_vpi_ids;
5902 }
James Smart41899be2012-03-01 22:34:19 -05005903 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005904 phba->sli4_hba.xri_ids = kzalloc(count *
5905 sizeof(uint16_t),
5906 GFP_KERNEL);
5907 if (unlikely(!phba->sli4_hba.xri_ids)) {
5908 rc = -ENOMEM;
5909 goto free_xri_bmask;
5910 }
5911
5912 for (i = 0; i < count; i++)
5913 phba->sli4_hba.xri_ids[i] = base + i;
5914
5915 /* VFIs. */
5916 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005917 if (count <= 0) {
5918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5919 "3282 Invalid provisioning of "
5920 "vfi:%d\n", count);
5921 rc = -EINVAL;
5922 goto free_xri_ids;
5923 }
James Smart6d368e52011-05-24 11:44:12 -04005924 base = phba->sli4_hba.max_cfg_param.vfi_base;
5925 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5926 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5927 sizeof(unsigned long),
5928 GFP_KERNEL);
5929 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5930 rc = -ENOMEM;
5931 goto free_xri_ids;
5932 }
5933 phba->sli4_hba.vfi_ids = kzalloc(count *
5934 sizeof(uint16_t),
5935 GFP_KERNEL);
5936 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5937 rc = -ENOMEM;
5938 goto free_vfi_bmask;
5939 }
5940
5941 for (i = 0; i < count; i++)
5942 phba->sli4_hba.vfi_ids[i] = base + i;
5943
5944 /*
5945 * Mark all resources ready. An HBA reset doesn't need
5946 * to reset the initialization.
5947 */
5948 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5949 LPFC_IDX_RSRC_RDY);
5950 return 0;
5951 }
5952
5953 free_vfi_bmask:
5954 kfree(phba->sli4_hba.vfi_bmask);
5955 free_xri_ids:
5956 kfree(phba->sli4_hba.xri_ids);
5957 free_xri_bmask:
5958 kfree(phba->sli4_hba.xri_bmask);
5959 free_vpi_ids:
5960 kfree(phba->vpi_ids);
5961 free_vpi_bmask:
5962 kfree(phba->vpi_bmask);
5963 free_rpi_ids:
5964 kfree(phba->sli4_hba.rpi_ids);
5965 free_rpi_bmask:
5966 kfree(phba->sli4_hba.rpi_bmask);
5967 err_exit:
5968 return rc;
5969}
5970
5971/**
5972 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5973 * @phba: Pointer to HBA context object.
5974 *
5975 * This function allocates the number of elements for the specified
5976 * resource type.
5977 **/
5978int
5979lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5980{
5981 if (phba->sli4_hba.extents_in_use) {
5982 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5983 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5984 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5985 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5986 } else {
5987 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005988 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005989 kfree(phba->vpi_ids);
5990 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5991 kfree(phba->sli4_hba.xri_bmask);
5992 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005993 kfree(phba->sli4_hba.vfi_bmask);
5994 kfree(phba->sli4_hba.vfi_ids);
5995 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5996 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5997 }
5998
5999 return 0;
6000}
6001
6002/**
James Smartb76f2dc2011-07-22 18:37:42 -04006003 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6004 * @phba: Pointer to HBA context object.
6005 * @type: The resource extent type.
6006 * @extnt_count: buffer to hold port extent count response
6007 * @extnt_size: buffer to hold port extent size response.
6008 *
6009 * This function calls the port to read the host allocated extents
6010 * for a particular type.
6011 **/
6012int
6013lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6014 uint16_t *extnt_cnt, uint16_t *extnt_size)
6015{
6016 bool emb;
6017 int rc = 0;
6018 uint16_t curr_blks = 0;
6019 uint32_t req_len, emb_len;
6020 uint32_t alloc_len, mbox_tmo;
6021 struct list_head *blk_list_head;
6022 struct lpfc_rsrc_blks *rsrc_blk;
6023 LPFC_MBOXQ_t *mbox;
6024 void *virtaddr = NULL;
6025 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6026 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6027 union lpfc_sli4_cfg_shdr *shdr;
6028
6029 switch (type) {
6030 case LPFC_RSC_TYPE_FCOE_VPI:
6031 blk_list_head = &phba->lpfc_vpi_blk_list;
6032 break;
6033 case LPFC_RSC_TYPE_FCOE_XRI:
6034 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6035 break;
6036 case LPFC_RSC_TYPE_FCOE_VFI:
6037 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6038 break;
6039 case LPFC_RSC_TYPE_FCOE_RPI:
6040 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6041 break;
6042 default:
6043 return -EIO;
6044 }
6045
6046 /* Count the number of extents currently allocatd for this type. */
6047 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6048 if (curr_blks == 0) {
6049 /*
6050 * The GET_ALLOCATED mailbox does not return the size,
6051 * just the count. The size should be just the size
6052 * stored in the current allocated block and all sizes
6053 * for an extent type are the same so set the return
6054 * value now.
6055 */
6056 *extnt_size = rsrc_blk->rsrc_size;
6057 }
6058 curr_blks++;
6059 }
6060
James Smartb76f2dc2011-07-22 18:37:42 -04006061 /*
6062 * Calculate the size of an embedded mailbox. The uint32_t
6063 * accounts for extents-specific word.
6064 */
6065 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6066 sizeof(uint32_t);
6067
6068 /*
6069 * Presume the allocation and response will fit into an embedded
6070 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6071 */
6072 emb = LPFC_SLI4_MBX_EMBED;
6073 req_len = emb_len;
6074 if (req_len > emb_len) {
6075 req_len = curr_blks * sizeof(uint16_t) +
6076 sizeof(union lpfc_sli4_cfg_shdr) +
6077 sizeof(uint32_t);
6078 emb = LPFC_SLI4_MBX_NEMBED;
6079 }
6080
6081 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6082 if (!mbox)
6083 return -ENOMEM;
6084 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6085
6086 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6087 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6088 req_len, emb);
6089 if (alloc_len < req_len) {
6090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6091 "2983 Allocated DMA memory size (x%x) is "
6092 "less than the requested DMA memory "
6093 "size (x%x)\n", alloc_len, req_len);
6094 rc = -ENOMEM;
6095 goto err_exit;
6096 }
6097 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6098 if (unlikely(rc)) {
6099 rc = -EIO;
6100 goto err_exit;
6101 }
6102
6103 if (!phba->sli4_hba.intr_enable)
6104 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6105 else {
James Smarta183a152011-10-10 21:32:43 -04006106 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006107 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6108 }
6109
6110 if (unlikely(rc)) {
6111 rc = -EIO;
6112 goto err_exit;
6113 }
6114
6115 /*
6116 * Figure out where the response is located. Then get local pointers
6117 * to the response data. The port does not guarantee to respond to
6118 * all extents counts request so update the local variable with the
6119 * allocated count from the port.
6120 */
6121 if (emb == LPFC_SLI4_MBX_EMBED) {
6122 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6123 shdr = &rsrc_ext->header.cfg_shdr;
6124 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6125 } else {
6126 virtaddr = mbox->sge_array->addr[0];
6127 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6128 shdr = &n_rsrc->cfg_shdr;
6129 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6130 }
6131
6132 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6133 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6134 "2984 Failed to read allocated resources "
6135 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6136 type,
6137 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6138 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6139 rc = -EIO;
6140 goto err_exit;
6141 }
6142 err_exit:
6143 lpfc_sli4_mbox_cmd_free(phba, mbox);
6144 return rc;
6145}
6146
6147/**
James Smart8a9d2e82012-05-09 21:16:12 -04006148 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6149 * @phba: pointer to lpfc hba data structure.
6150 *
6151 * This routine walks the list of els buffers that have been allocated and
6152 * repost them to the port by using SGL block post. This is needed after a
6153 * pci_function_reset/warm_start or start. It attempts to construct blocks
6154 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6155 * SGL block post mailbox commands to post them to the port. For single els
6156 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6157 * mailbox command for posting.
6158 *
6159 * Returns: 0 = success, non-zero failure.
6160 **/
6161static int
6162lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6163{
6164 struct lpfc_sglq *sglq_entry = NULL;
6165 struct lpfc_sglq *sglq_entry_next = NULL;
6166 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04006167 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006168 int last_xritag = NO_XRI;
James Smartdafe8ce2014-09-03 12:56:40 -04006169 struct lpfc_sli_ring *pring;
James Smart8a9d2e82012-05-09 21:16:12 -04006170 LIST_HEAD(prep_sgl_list);
6171 LIST_HEAD(blck_sgl_list);
6172 LIST_HEAD(allc_sgl_list);
6173 LIST_HEAD(post_sgl_list);
6174 LIST_HEAD(free_sgl_list);
6175
James Smartdafe8ce2014-09-03 12:56:40 -04006176 pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart38c20672013-03-01 16:37:44 -05006177 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006178 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006179 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006180 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006181 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006182
James Smart711ea882013-04-17 20:18:29 -04006183 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006184 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6185 &allc_sgl_list, list) {
6186 list_del_init(&sglq_entry->list);
6187 block_cnt++;
6188 if ((last_xritag != NO_XRI) &&
6189 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6190 /* a hole in xri block, form a sgl posting block */
6191 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6192 post_cnt = block_cnt - 1;
6193 /* prepare list for next posting block */
6194 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6195 block_cnt = 1;
6196 } else {
6197 /* prepare list for next posting block */
6198 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6199 /* enough sgls for non-embed sgl mbox command */
6200 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6201 list_splice_init(&prep_sgl_list,
6202 &blck_sgl_list);
6203 post_cnt = block_cnt;
6204 block_cnt = 0;
6205 }
6206 }
6207 num_posted++;
6208
6209 /* keep track of last sgl's xritag */
6210 last_xritag = sglq_entry->sli4_xritag;
6211
6212 /* end of repost sgl list condition for els buffers */
6213 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6214 if (post_cnt == 0) {
6215 list_splice_init(&prep_sgl_list,
6216 &blck_sgl_list);
6217 post_cnt = block_cnt;
6218 } else if (block_cnt == 1) {
6219 status = lpfc_sli4_post_sgl(phba,
6220 sglq_entry->phys, 0,
6221 sglq_entry->sli4_xritag);
6222 if (!status) {
6223 /* successful, put sgl to posted list */
6224 list_add_tail(&sglq_entry->list,
6225 &post_sgl_list);
6226 } else {
6227 /* Failure, put sgl to free list */
6228 lpfc_printf_log(phba, KERN_WARNING,
6229 LOG_SLI,
6230 "3159 Failed to post els "
6231 "sgl, xritag:x%x\n",
6232 sglq_entry->sli4_xritag);
6233 list_add_tail(&sglq_entry->list,
6234 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006235 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006236 }
6237 }
6238 }
6239
6240 /* continue until a nembed page worth of sgls */
6241 if (post_cnt == 0)
6242 continue;
6243
6244 /* post the els buffer list sgls as a block */
6245 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6246 post_cnt);
6247
6248 if (!status) {
6249 /* success, put sgl list to posted sgl list */
6250 list_splice_init(&blck_sgl_list, &post_sgl_list);
6251 } else {
6252 /* Failure, put sgl list to free sgl list */
6253 sglq_entry_first = list_first_entry(&blck_sgl_list,
6254 struct lpfc_sglq,
6255 list);
6256 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6257 "3160 Failed to post els sgl-list, "
6258 "xritag:x%x-x%x\n",
6259 sglq_entry_first->sli4_xritag,
6260 (sglq_entry_first->sli4_xritag +
6261 post_cnt - 1));
6262 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006263 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006264 }
6265
6266 /* don't reset xirtag due to hole in xri block */
6267 if (block_cnt == 0)
6268 last_xritag = NO_XRI;
6269
6270 /* reset els sgl post count for next round of posting */
6271 post_cnt = 0;
6272 }
James Smart711ea882013-04-17 20:18:29 -04006273 /* update the number of XRIs posted for ELS */
6274 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006275
6276 /* free the els sgls failed to post */
6277 lpfc_free_sgl_list(phba, &free_sgl_list);
6278
6279 /* push els sgls posted to the availble list */
6280 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006281 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006282 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006283 list_splice_init(&post_sgl_list,
6284 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006285 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006286 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006287 } else {
6288 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6289 "3161 Failure to post els sgl to port.\n");
6290 return -EIO;
6291 }
6292 return 0;
6293}
6294
6295/**
James Smartda0436e2009-05-22 14:51:39 -04006296 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6297 * @phba: Pointer to HBA context object.
6298 *
6299 * This function is the main SLI4 device intialization PCI function. This
6300 * function is called by the HBA intialization code, HBA reset code and
6301 * HBA error attention handler code. Caller is not required to hold any
6302 * locks.
6303 **/
6304int
6305lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6306{
6307 int rc;
6308 LPFC_MBOXQ_t *mboxq;
6309 struct lpfc_mqe *mqe;
6310 uint8_t *vpd;
6311 uint32_t vpd_size;
6312 uint32_t ftr_rsp = 0;
6313 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6314 struct lpfc_vport *vport = phba->pport;
6315 struct lpfc_dmabuf *mp;
6316
6317 /* Perform a PCI function reset to start from clean */
6318 rc = lpfc_pci_function_reset(phba);
6319 if (unlikely(rc))
6320 return -ENODEV;
6321
6322 /* Check the HBA Host Status Register for readyness */
6323 rc = lpfc_sli4_post_status_check(phba);
6324 if (unlikely(rc))
6325 return -ENODEV;
6326 else {
6327 spin_lock_irq(&phba->hbalock);
6328 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6329 spin_unlock_irq(&phba->hbalock);
6330 }
6331
6332 /*
6333 * Allocate a single mailbox container for initializing the
6334 * port.
6335 */
6336 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6337 if (!mboxq)
6338 return -ENOMEM;
6339
James Smartda0436e2009-05-22 14:51:39 -04006340 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006341 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006342 vpd = kzalloc(vpd_size, GFP_KERNEL);
6343 if (!vpd) {
6344 rc = -ENOMEM;
6345 goto out_free_mbox;
6346 }
6347
6348 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006349 if (unlikely(rc)) {
6350 kfree(vpd);
6351 goto out_free_mbox;
6352 }
James Smart572709e2013-07-15 18:32:43 -04006353
James Smartda0436e2009-05-22 14:51:39 -04006354 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006355 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006356 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006357 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006358 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6359 } else {
James Smart76a95d72010-11-20 23:11:48 -05006360 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006361 }
James Smart45ed1192009-10-02 15:17:02 -04006362
6363 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6364 LPFC_DCBX_CEE_MODE)
6365 phba->hba_flag |= HBA_FIP_SUPPORT;
6366 else
6367 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6368
James Smart4f2e66c2012-05-09 21:17:07 -04006369 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6370
James Smartc31098c2011-04-16 11:03:33 -04006371 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006372 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6373 "0376 READ_REV Error. SLI Level %d "
6374 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006375 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006376 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006377 kfree(vpd);
6378 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006379 }
James Smartcd1c8302011-10-10 21:33:25 -04006380
6381 /*
James Smartff78d8f2011-12-13 13:21:35 -05006382 * Continue initialization with default values even if driver failed
6383 * to read FCoE param config regions, only read parameters if the
6384 * board is FCoE
6385 */
6386 if (phba->hba_flag & HBA_FCOE_MODE &&
6387 lpfc_sli4_read_fcoe_params(phba))
6388 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6389 "2570 Failed to read FCoE parameters\n");
6390
6391 /*
James Smartcd1c8302011-10-10 21:33:25 -04006392 * Retrieve sli4 device physical port name, failure of doing it
6393 * is considered as non-fatal.
6394 */
6395 rc = lpfc_sli4_retrieve_pport_name(phba);
6396 if (!rc)
6397 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6398 "3080 Successful retrieving SLI4 device "
6399 "physical port name: %s.\n", phba->Port);
6400
James Smartda0436e2009-05-22 14:51:39 -04006401 /*
6402 * Evaluate the read rev and vpd data. Populate the driver
6403 * state with the results. If this routine fails, the failure
6404 * is not fatal as the driver will use generic values.
6405 */
6406 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6407 if (unlikely(!rc)) {
6408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6409 "0377 Error %d parsing vpd. "
6410 "Using defaults.\n", rc);
6411 rc = 0;
6412 }
James Smart76a95d72010-11-20 23:11:48 -05006413 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006414
James Smartf1126682009-06-10 17:22:44 -04006415 /* Save information as VPD data */
6416 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6417 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6418 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6419 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6420 &mqe->un.read_rev);
6421 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6422 &mqe->un.read_rev);
6423 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6424 &mqe->un.read_rev);
6425 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6426 &mqe->un.read_rev);
6427 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6428 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6429 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6430 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6431 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6432 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6433 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6434 "(%d):0380 READ_REV Status x%x "
6435 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6436 mboxq->vport ? mboxq->vport->vpi : 0,
6437 bf_get(lpfc_mqe_status, mqe),
6438 phba->vpd.rev.opFwName,
6439 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6440 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006441
James Smart572709e2013-07-15 18:32:43 -04006442 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6443 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6444 if (phba->pport->cfg_lun_queue_depth > rc) {
6445 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6446 "3362 LUN queue depth changed from %d to %d\n",
6447 phba->pport->cfg_lun_queue_depth, rc);
6448 phba->pport->cfg_lun_queue_depth = rc;
6449 }
6450
James Smart65791f12016-07-06 12:35:56 -07006451 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6452 LPFC_SLI_INTF_IF_TYPE_0)
6453 lpfc_set_features(phba);
James Smart572709e2013-07-15 18:32:43 -04006454
James Smartda0436e2009-05-22 14:51:39 -04006455 /*
6456 * Discover the port's supported feature set and match it against the
6457 * hosts requests.
6458 */
6459 lpfc_request_features(phba, mboxq);
6460 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6461 if (unlikely(rc)) {
6462 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006463 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006464 }
6465
6466 /*
6467 * The port must support FCP initiator mode as this is the
6468 * only mode running in the host.
6469 */
6470 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6471 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6472 "0378 No support for fcpi mode.\n");
6473 ftr_rsp++;
6474 }
James Smartfedd3b72011-02-16 12:39:24 -05006475 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6476 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6477 else
6478 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006479 /*
6480 * If the port cannot support the host's requested features
6481 * then turn off the global config parameters to disable the
6482 * feature in the driver. This is not a fatal error.
6483 */
James Smartbf086112011-08-21 21:48:13 -04006484 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6485 if (phba->cfg_enable_bg) {
6486 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6487 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6488 else
6489 ftr_rsp++;
6490 }
James Smartda0436e2009-05-22 14:51:39 -04006491
6492 if (phba->max_vpi && phba->cfg_enable_npiv &&
6493 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6494 ftr_rsp++;
6495
6496 if (ftr_rsp) {
6497 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6498 "0379 Feature Mismatch Data: x%08x %08x "
6499 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6500 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6501 phba->cfg_enable_npiv, phba->max_vpi);
6502 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6503 phba->cfg_enable_bg = 0;
6504 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6505 phba->cfg_enable_npiv = 0;
6506 }
6507
6508 /* These SLI3 features are assumed in SLI4 */
6509 spin_lock_irq(&phba->hbalock);
6510 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6511 spin_unlock_irq(&phba->hbalock);
6512
James Smart6d368e52011-05-24 11:44:12 -04006513 /*
6514 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6515 * calls depends on these resources to complete port setup.
6516 */
6517 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6518 if (rc) {
6519 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6520 "2920 Failed to alloc Resource IDs "
6521 "rc = x%x\n", rc);
6522 goto out_free_mbox;
6523 }
6524
James Smartda0436e2009-05-22 14:51:39 -04006525 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006526 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6527 if (rc) {
6528 phba->link_state = LPFC_HBA_ERROR;
6529 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006530 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006531 }
6532
James Smartda0436e2009-05-22 14:51:39 -04006533 mboxq->vport = vport;
6534 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6535 mp = (struct lpfc_dmabuf *) mboxq->context1;
6536 if (rc == MBX_SUCCESS) {
6537 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6538 rc = 0;
6539 }
6540
6541 /*
6542 * This memory was allocated by the lpfc_read_sparam routine. Release
6543 * it to the mbuf pool.
6544 */
6545 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6546 kfree(mp);
6547 mboxq->context1 = NULL;
6548 if (unlikely(rc)) {
6549 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6550 "0382 READ_SPARAM command failed "
6551 "status %d, mbxStatus x%x\n",
6552 rc, bf_get(lpfc_mqe_status, mqe));
6553 phba->link_state = LPFC_HBA_ERROR;
6554 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006555 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006556 }
6557
James Smart05580562011-05-24 11:40:48 -04006558 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006559
6560 /* Update the fc_host data structures with new wwn. */
6561 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6562 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6563
James Smart8a9d2e82012-05-09 21:16:12 -04006564 /* update host els and scsi xri-sgl sizes and mappings */
6565 rc = lpfc_sli4_xri_sgl_update(phba);
6566 if (unlikely(rc)) {
6567 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6568 "1400 Failed to update xri-sgl size and "
6569 "mapping: %d\n", rc);
6570 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006571 }
6572
James Smart8a9d2e82012-05-09 21:16:12 -04006573 /* register the els sgl pool to the port */
6574 rc = lpfc_sli4_repost_els_sgl_list(phba);
6575 if (unlikely(rc)) {
6576 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6577 "0582 Error %d during els sgl post "
6578 "operation\n", rc);
6579 rc = -ENODEV;
6580 goto out_free_mbox;
6581 }
6582
6583 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006584 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6585 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006586 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006587 "0383 Error %d during scsi sgl post "
6588 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006589 /* Some Scsi buffers were moved to the abort scsi list */
6590 /* A pci function reset will repost them */
6591 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006592 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006593 }
6594
6595 /* Post the rpi header region to the device. */
6596 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6597 if (unlikely(rc)) {
6598 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6599 "0393 Error %d during rpi post operation\n",
6600 rc);
6601 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006602 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006603 }
James Smart97f2ecf2012-03-01 22:35:23 -05006604 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006605
James Smart5350d872011-10-10 21:33:49 -04006606 /* Create all the SLI4 queues */
6607 rc = lpfc_sli4_queue_create(phba);
6608 if (rc) {
6609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6610 "3089 Failed to allocate queues\n");
6611 rc = -ENODEV;
6612 goto out_stop_timers;
6613 }
James Smartda0436e2009-05-22 14:51:39 -04006614 /* Set up all the queues to the device */
6615 rc = lpfc_sli4_queue_setup(phba);
6616 if (unlikely(rc)) {
6617 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6618 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006619 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006620 }
6621
6622 /* Arm the CQs and then EQs on device */
6623 lpfc_sli4_arm_cqeq_intr(phba);
6624
6625 /* Indicate device interrupt mode */
6626 phba->sli4_hba.intr_enable = 1;
6627
6628 /* Allow asynchronous mailbox command to go through */
6629 spin_lock_irq(&phba->hbalock);
6630 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6631 spin_unlock_irq(&phba->hbalock);
6632
6633 /* Post receive buffers to the device */
6634 lpfc_sli4_rb_setup(phba);
6635
James Smartfc2b9892010-02-26 14:15:29 -05006636 /* Reset HBA FCF states after HBA reset */
6637 phba->fcf.fcf_flag = 0;
6638 phba->fcf.current_rec.flag = 0;
6639
James Smartda0436e2009-05-22 14:51:39 -04006640 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006641 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006642 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006643
6644 /* Start heart beat timer */
6645 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006646 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006647 phba->hb_outstanding = 0;
6648 phba->last_completion_time = jiffies;
6649
6650 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006651 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006652 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006653
James Smart75baf692010-06-08 18:31:21 -04006654 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6655 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6656 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6657 if (!rc) {
6658 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6659 "2829 This device supports "
6660 "Advanced Error Reporting (AER)\n");
6661 spin_lock_irq(&phba->hbalock);
6662 phba->hba_flag |= HBA_AER_ENABLED;
6663 spin_unlock_irq(&phba->hbalock);
6664 } else {
6665 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6666 "2830 This device does not support "
6667 "Advanced Error Reporting (AER)\n");
6668 phba->cfg_aer_support = 0;
6669 }
James Smart0a96e972011-07-22 18:37:28 -04006670 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006671 }
6672
James Smart76a95d72010-11-20 23:11:48 -05006673 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6674 /*
6675 * The FC Port needs to register FCFI (index 0)
6676 */
6677 lpfc_reg_fcfi(phba, mboxq);
6678 mboxq->vport = phba->pport;
6679 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006680 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006681 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006682 rc = 0;
6683 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6684 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006685
6686 /* Check if the port is configured to be disabled */
6687 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006688 }
James Smart026abb82011-12-13 13:20:45 -05006689
James Smartda0436e2009-05-22 14:51:39 -04006690 /*
6691 * The port is ready, set the host's link state to LINK_DOWN
6692 * in preparation for link interrupts.
6693 */
James Smartda0436e2009-05-22 14:51:39 -04006694 spin_lock_irq(&phba->hbalock);
6695 phba->link_state = LPFC_LINK_DOWN;
6696 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006697 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6698 (phba->hba_flag & LINK_DISABLED)) {
6699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6700 "3103 Adapter Link is disabled.\n");
6701 lpfc_down_link(phba, mboxq);
6702 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6703 if (rc != MBX_SUCCESS) {
6704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6705 "3104 Adapter failed to issue "
6706 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6707 goto out_unset_queue;
6708 }
6709 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006710 /* don't perform init_link on SLI4 FC port loopback test */
6711 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6712 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6713 if (rc)
6714 goto out_unset_queue;
6715 }
James Smart5350d872011-10-10 21:33:49 -04006716 }
6717 mempool_free(mboxq, phba->mbox_mem_pool);
6718 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006719out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006720 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006721 lpfc_sli4_queue_unset(phba);
6722out_destroy_queue:
6723 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006724out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006725 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006726out_free_mbox:
6727 mempool_free(mboxq, phba->mbox_mem_pool);
6728 return rc;
6729}
James Smarte59058c2008-08-24 21:49:00 -04006730
6731/**
James Smart3621a712009-04-06 18:47:14 -04006732 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006733 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006734 *
James Smarte59058c2008-08-24 21:49:00 -04006735 * This is the callback function for mailbox timer. The mailbox
6736 * timer is armed when a new mailbox command is issued and the timer
6737 * is deleted when the mailbox complete. The function is called by
6738 * the kernel timer code when a mailbox does not complete within
6739 * expected time. This function wakes up the worker thread to
6740 * process the mailbox timeout and returns. All the processing is
6741 * done by the worker thread function lpfc_mbox_timeout_handler.
6742 **/
dea31012005-04-17 16:05:31 -05006743void
6744lpfc_mbox_timeout(unsigned long ptr)
6745{
James Smart92d7f7b2007-06-17 19:56:38 -05006746 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006747 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006748 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006749
James Smart2e0fef82007-06-17 19:56:36 -05006750 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006751 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006752 if (!tmo_posted)
6753 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6754 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6755
James Smart5e9d9b82008-06-14 22:52:53 -04006756 if (!tmo_posted)
6757 lpfc_worker_wake_up(phba);
6758 return;
dea31012005-04-17 16:05:31 -05006759}
6760
James Smarte8d3c3b2013-10-10 12:21:30 -04006761/**
6762 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6763 * are pending
6764 * @phba: Pointer to HBA context object.
6765 *
6766 * This function checks if any mailbox completions are present on the mailbox
6767 * completion queue.
6768 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006769static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006770lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6771{
6772
6773 uint32_t idx;
6774 struct lpfc_queue *mcq;
6775 struct lpfc_mcqe *mcqe;
6776 bool pending_completions = false;
6777
6778 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6779 return false;
6780
6781 /* Check for completions on mailbox completion queue */
6782
6783 mcq = phba->sli4_hba.mbx_cq;
6784 idx = mcq->hba_index;
6785 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6786 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6787 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6788 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6789 pending_completions = true;
6790 break;
6791 }
6792 idx = (idx + 1) % mcq->entry_count;
6793 if (mcq->hba_index == idx)
6794 break;
6795 }
6796 return pending_completions;
6797
6798}
6799
6800/**
6801 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6802 * that were missed.
6803 * @phba: Pointer to HBA context object.
6804 *
6805 * For sli4, it is possible to miss an interrupt. As such mbox completions
6806 * maybe missed causing erroneous mailbox timeouts to occur. This function
6807 * checks to see if mbox completions are on the mailbox completion queue
6808 * and will process all the completions associated with the eq for the
6809 * mailbox completion queue.
6810 **/
6811bool
6812lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6813{
6814
6815 uint32_t eqidx;
6816 struct lpfc_queue *fpeq = NULL;
6817 struct lpfc_eqe *eqe;
6818 bool mbox_pending;
6819
6820 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6821 return false;
6822
6823 /* Find the eq associated with the mcq */
6824
6825 if (phba->sli4_hba.hba_eq)
6826 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6827 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6828 phba->sli4_hba.mbx_cq->assoc_qid) {
6829 fpeq = phba->sli4_hba.hba_eq[eqidx];
6830 break;
6831 }
6832 if (!fpeq)
6833 return false;
6834
6835 /* Turn off interrupts from this EQ */
6836
6837 lpfc_sli4_eq_clr_intr(fpeq);
6838
6839 /* Check to see if a mbox completion is pending */
6840
6841 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6842
6843 /*
6844 * If a mbox completion is pending, process all the events on EQ
6845 * associated with the mbox completion queue (this could include
6846 * mailbox commands, async events, els commands, receive queue data
6847 * and fcp commands)
6848 */
6849
6850 if (mbox_pending)
6851 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6852 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6853 fpeq->EQ_processed++;
6854 }
6855
6856 /* Always clear and re-arm the EQ */
6857
6858 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6859
6860 return mbox_pending;
6861
6862}
James Smarte59058c2008-08-24 21:49:00 -04006863
6864/**
James Smart3621a712009-04-06 18:47:14 -04006865 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006866 * @phba: Pointer to HBA context object.
6867 *
6868 * This function is called from worker thread when a mailbox command times out.
6869 * The caller is not required to hold any locks. This function will reset the
6870 * HBA and recover all the pending commands.
6871 **/
dea31012005-04-17 16:05:31 -05006872void
6873lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6874{
James Smart2e0fef82007-06-17 19:56:36 -05006875 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04006876 MAILBOX_t *mb = NULL;
6877
James Smart1dcb58e2007-04-25 09:51:30 -04006878 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006879
James Smarte8d3c3b2013-10-10 12:21:30 -04006880 /* If the mailbox completed, process the completion and return */
6881 if (lpfc_sli4_process_missed_mbox_completions(phba))
6882 return;
6883
James Smarteb016562014-09-03 12:58:06 -04006884 if (pmbox != NULL)
6885 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04006886 /* Check the pmbox pointer first. There is a race condition
6887 * between the mbox timeout handler getting executed in the
6888 * worklist and the mailbox actually completing. When this
6889 * race condition occurs, the mbox_active will be NULL.
6890 */
6891 spin_lock_irq(&phba->hbalock);
6892 if (pmbox == NULL) {
6893 lpfc_printf_log(phba, KERN_WARNING,
6894 LOG_MBOX | LOG_SLI,
6895 "0353 Active Mailbox cleared - mailbox timeout "
6896 "exiting\n");
6897 spin_unlock_irq(&phba->hbalock);
6898 return;
6899 }
6900
dea31012005-04-17 16:05:31 -05006901 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006902 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006903 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006904 mb->mbxCommand,
6905 phba->pport->port_state,
6906 phba->sli.sli_flag,
6907 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006908 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006909
James Smart1dcb58e2007-04-25 09:51:30 -04006910 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6911 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006912 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006913 */
James Smart2e0fef82007-06-17 19:56:36 -05006914 spin_lock_irq(&phba->pport->work_port_lock);
6915 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6916 spin_unlock_irq(&phba->pport->work_port_lock);
6917 spin_lock_irq(&phba->hbalock);
6918 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006919 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006920 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006921
James Smartdb55fba2014-04-04 13:52:02 -04006922 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04006923
6924 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006925 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006926
6927 /* Reset the HBA device */
6928 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006929}
6930
James Smarte59058c2008-08-24 21:49:00 -04006931/**
James Smart3772a992009-05-22 14:50:54 -04006932 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006933 * @phba: Pointer to HBA context object.
6934 * @pmbox: Pointer to mailbox object.
6935 * @flag: Flag indicating how the mailbox need to be processed.
6936 *
6937 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006938 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6939 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006940 * The mailbox command can be submitted in polling mode, in which case
6941 * this function will wait in a polling loop for the completion of the
6942 * mailbox.
6943 * If the mailbox is submitted in no_wait mode (not polling) the
6944 * function will submit the command and returns immediately without waiting
6945 * for the mailbox completion. The no_wait is supported only when HBA
6946 * is in SLI2/SLI3 mode - interrupts are enabled.
6947 * The SLI interface allows only one mailbox pending at a time. If the
6948 * mailbox is issued in polling mode and there is already a mailbox
6949 * pending, then the function will return an error. If the mailbox is issued
6950 * in NO_WAIT mode and there is a mailbox pending already, the function
6951 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6952 * The sli layer owns the mailbox object until the completion of mailbox
6953 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6954 * return codes the caller owns the mailbox command after the return of
6955 * the function.
6956 **/
James Smart3772a992009-05-22 14:50:54 -04006957static int
6958lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6959 uint32_t flag)
dea31012005-04-17 16:05:31 -05006960{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006961 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006962 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006963 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006964 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006965 int i;
James Smart09372822008-01-11 01:52:54 -05006966 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006967 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006968 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006969 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006970 int processing_queue = 0;
6971
6972 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6973 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006974 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006975 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006976 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6977 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6978 return MBX_SUCCESS;
6979 }
James Smart58da1ff2008-04-07 10:15:56 -04006980 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006981 pmbox = lpfc_mbox_get(phba);
6982 if (!pmbox) {
6983 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6984 return MBX_SUCCESS;
6985 }
6986 }
dea31012005-04-17 16:05:31 -05006987
James Smarted957682007-06-17 19:56:37 -05006988 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006989 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006990 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006991 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006992 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006993 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006994 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006995 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006996 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006997 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006998 }
6999 }
7000
Linas Vepstas8d63f372007-02-14 14:28:36 -06007001 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007002 if (unlikely(pci_channel_offline(phba->pcidev))) {
7003 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7004 goto out_not_finished;
7005 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007006
James Smarta257bf92009-04-06 18:48:10 -04007007 /* If HBA has a deferred error attention, fail the iocb. */
7008 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7009 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7010 goto out_not_finished;
7011 }
7012
dea31012005-04-17 16:05:31 -05007013 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007014
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007015 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007016 status = MBX_SUCCESS;
7017
James Smart2e0fef82007-06-17 19:56:36 -05007018 if (phba->link_state == LPFC_HBA_ERROR) {
7019 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007020
7021 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007022 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7023 "(%d):0311 Mailbox command x%x cannot "
7024 "issue Data: x%x x%x\n",
7025 pmbox->vport ? pmbox->vport->vpi : 0,
7026 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007027 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007028 }
7029
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007030 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007031 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7032 !(hc_copy & HC_MBINT_ENA)) {
7033 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7034 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007035 "(%d):2528 Mailbox command x%x cannot "
7036 "issue Data: x%x x%x\n",
7037 pmbox->vport ? pmbox->vport->vpi : 0,
7038 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007039 goto out_not_finished;
7040 }
James Smart92908312006-03-07 15:04:13 -05007041 }
7042
dea31012005-04-17 16:05:31 -05007043 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7044 /* Polling for a mbox command when another one is already active
7045 * is not allowed in SLI. Also, the driver must have established
7046 * SLI2 mode to queue and process multiple mbox commands.
7047 */
7048
7049 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007050 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007051
7052 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007053 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7054 "(%d):2529 Mailbox command x%x "
7055 "cannot issue Data: x%x x%x\n",
7056 pmbox->vport ? pmbox->vport->vpi : 0,
7057 pmbox->u.mb.mbxCommand,
7058 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007059 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007060 }
7061
James Smart3772a992009-05-22 14:50:54 -04007062 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007064 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007065 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7066 "(%d):2530 Mailbox command x%x "
7067 "cannot issue Data: x%x x%x\n",
7068 pmbox->vport ? pmbox->vport->vpi : 0,
7069 pmbox->u.mb.mbxCommand,
7070 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007071 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007072 }
7073
dea31012005-04-17 16:05:31 -05007074 /* Another mailbox command is still being processed, queue this
7075 * command to be processed later.
7076 */
7077 lpfc_mbox_put(phba, pmbox);
7078
7079 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007080 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007081 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007082 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007083 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007084 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007085 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007086
7087 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007088 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007089
James Smart858c9f62007-06-17 19:56:39 -05007090 if (pmbox->vport) {
7091 lpfc_debugfs_disc_trc(pmbox->vport,
7092 LPFC_DISC_TRC_MBOX_VPORT,
7093 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007094 (uint32_t)mbx->mbxCommand,
7095 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007096 }
7097 else {
7098 lpfc_debugfs_disc_trc(phba->pport,
7099 LPFC_DISC_TRC_MBOX,
7100 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007101 (uint32_t)mbx->mbxCommand,
7102 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007103 }
7104
James Smart2e0fef82007-06-17 19:56:36 -05007105 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007106 }
7107
dea31012005-04-17 16:05:31 -05007108 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7109
7110 /* If we are not polling, we MUST be in SLI2 mode */
7111 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007112 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007113 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007114 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007115 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007116 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007117 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7118 "(%d):2531 Mailbox command x%x "
7119 "cannot issue Data: x%x x%x\n",
7120 pmbox->vport ? pmbox->vport->vpi : 0,
7121 pmbox->u.mb.mbxCommand,
7122 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007123 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007124 }
7125 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007126 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7127 1000);
7128 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007129 }
7130
7131 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007132 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007133 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007134 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007135 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007136 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007137 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007138
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007139 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007140 if (pmbox->vport) {
7141 lpfc_debugfs_disc_trc(pmbox->vport,
7142 LPFC_DISC_TRC_MBOX_VPORT,
7143 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007144 (uint32_t)mbx->mbxCommand,
7145 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007146 }
7147 else {
7148 lpfc_debugfs_disc_trc(phba->pport,
7149 LPFC_DISC_TRC_MBOX,
7150 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007151 (uint32_t)mbx->mbxCommand,
7152 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007153 }
7154 }
7155
dea31012005-04-17 16:05:31 -05007156 psli->slistat.mbox_cmd++;
7157 evtctr = psli->slistat.mbox_event;
7158
7159 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007160 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007161
James Smart3772a992009-05-22 14:50:54 -04007162 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007163 /* Populate mbox extension offset word. */
7164 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007165 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007166 = (uint8_t *)phba->mbox_ext
7167 - (uint8_t *)phba->mbox;
7168 }
7169
7170 /* Copy the mailbox extension data */
7171 if (pmbox->in_ext_byte_len && pmbox->context2) {
7172 lpfc_sli_pcimem_bcopy(pmbox->context2,
7173 (uint8_t *)phba->mbox_ext,
7174 pmbox->in_ext_byte_len);
7175 }
7176 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007177 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007178 } else {
James Smart7a470272010-03-15 11:25:20 -04007179 /* Populate mbox extension offset word. */
7180 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007181 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007182 = MAILBOX_HBA_EXT_OFFSET;
7183
7184 /* Copy the mailbox extension data */
7185 if (pmbox->in_ext_byte_len && pmbox->context2) {
7186 lpfc_memcpy_to_slim(phba->MBslimaddr +
7187 MAILBOX_HBA_EXT_OFFSET,
7188 pmbox->context2, pmbox->in_ext_byte_len);
7189
7190 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007191 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007192 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007193 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007194 }
7195
7196 /* First copy mbox command data to HBA SLIM, skip past first
7197 word */
7198 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007199 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007200 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7201
7202 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007203 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007204 to_slim = phba->MBslimaddr;
7205 writel(ldata, to_slim);
7206 readl(to_slim); /* flush */
7207
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007208 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007209 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007210 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007211 }
7212 }
7213
7214 wmb();
dea31012005-04-17 16:05:31 -05007215
7216 switch (flag) {
7217 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007218 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007219 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007220 /* Interrupt board to do it */
7221 writel(CA_MBATT, phba->CAregaddr);
7222 readl(phba->CAregaddr); /* flush */
7223 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007224 break;
7225
7226 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007227 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007228 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007229 /* Interrupt board to do it */
7230 writel(CA_MBATT, phba->CAregaddr);
7231 readl(phba->CAregaddr); /* flush */
7232
James Smart3772a992009-05-22 14:50:54 -04007233 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007234 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007235 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007236 word0 = le32_to_cpu(word0);
7237 } else {
7238 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007239 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7240 spin_unlock_irqrestore(&phba->hbalock,
7241 drvr_flag);
7242 goto out_not_finished;
7243 }
dea31012005-04-17 16:05:31 -05007244 }
7245
7246 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007247 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7248 spin_unlock_irqrestore(&phba->hbalock,
7249 drvr_flag);
7250 goto out_not_finished;
7251 }
James Smarta183a152011-10-10 21:32:43 -04007252 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7253 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007254 i = 0;
dea31012005-04-17 16:05:31 -05007255 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007256 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7257 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007258 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007259 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007260 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007261 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007262 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007263 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007264 }
7265
7266 /* Check if we took a mbox interrupt while we were
7267 polling */
7268 if (((word0 & OWN_CHIP) != OWN_CHIP)
7269 && (evtctr != psli->slistat.mbox_event))
7270 break;
7271
James Smart09372822008-01-11 01:52:54 -05007272 if (i++ > 10) {
7273 spin_unlock_irqrestore(&phba->hbalock,
7274 drvr_flag);
7275 msleep(1);
7276 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7277 }
dea31012005-04-17 16:05:31 -05007278
James Smart3772a992009-05-22 14:50:54 -04007279 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007280 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007281 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007282 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007283 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007284 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007285 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007286 /* Check real SLIM for any errors */
7287 slimword0 = readl(phba->MBslimaddr);
7288 slimmb = (MAILBOX_t *) & slimword0;
7289 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7290 && slimmb->mbxStatus) {
7291 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007292 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007293 word0 = slimword0;
7294 }
7295 }
7296 } else {
7297 /* First copy command data */
7298 word0 = readl(phba->MBslimaddr);
7299 }
7300 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007301 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7302 spin_unlock_irqrestore(&phba->hbalock,
7303 drvr_flag);
7304 goto out_not_finished;
7305 }
dea31012005-04-17 16:05:31 -05007306 }
7307
James Smart3772a992009-05-22 14:50:54 -04007308 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007309 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007310 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007311 /* Copy the mailbox extension data */
7312 if (pmbox->out_ext_byte_len && pmbox->context2) {
7313 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7314 pmbox->context2,
7315 pmbox->out_ext_byte_len);
7316 }
dea31012005-04-17 16:05:31 -05007317 } else {
7318 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007319 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007320 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007321 /* Copy the mailbox extension data */
7322 if (pmbox->out_ext_byte_len && pmbox->context2) {
7323 lpfc_memcpy_from_slim(pmbox->context2,
7324 phba->MBslimaddr +
7325 MAILBOX_HBA_EXT_OFFSET,
7326 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007327 }
7328 }
7329
7330 writel(HA_MBATT, phba->HAregaddr);
7331 readl(phba->HAregaddr); /* flush */
7332
7333 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007334 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007335 }
7336
James Smart2e0fef82007-06-17 19:56:36 -05007337 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7338 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007339
7340out_not_finished:
7341 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007342 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007343 lpfc_mbox_cmpl_put(phba, pmbox);
7344 }
7345 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007346}
7347
James Smarte59058c2008-08-24 21:49:00 -04007348/**
James Smartf1126682009-06-10 17:22:44 -04007349 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7350 * @phba: Pointer to HBA context object.
7351 *
7352 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7353 * the driver internal pending mailbox queue. It will then try to wait out the
7354 * possible outstanding mailbox command before return.
7355 *
7356 * Returns:
7357 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7358 * the outstanding mailbox command timed out.
7359 **/
7360static int
7361lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7362{
7363 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007364 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007365 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007366
7367 /* Mark the asynchronous mailbox command posting as blocked */
7368 spin_lock_irq(&phba->hbalock);
7369 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007370 /* Determine how long we might wait for the active mailbox
7371 * command to be gracefully completed by firmware.
7372 */
James Smarta183a152011-10-10 21:32:43 -04007373 if (phba->sli.mbox_active)
7374 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7375 phba->sli.mbox_active) *
7376 1000) + jiffies;
7377 spin_unlock_irq(&phba->hbalock);
7378
James Smarte8d3c3b2013-10-10 12:21:30 -04007379 /* Make sure the mailbox is really active */
7380 if (timeout)
7381 lpfc_sli4_process_missed_mbox_completions(phba);
7382
James Smartf1126682009-06-10 17:22:44 -04007383 /* Wait for the outstnading mailbox command to complete */
7384 while (phba->sli.mbox_active) {
7385 /* Check active mailbox complete status every 2ms */
7386 msleep(2);
7387 if (time_after(jiffies, timeout)) {
7388 /* Timeout, marked the outstanding cmd not complete */
7389 rc = 1;
7390 break;
7391 }
7392 }
7393
7394 /* Can not cleanly block async mailbox command, fails it */
7395 if (rc) {
7396 spin_lock_irq(&phba->hbalock);
7397 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7398 spin_unlock_irq(&phba->hbalock);
7399 }
7400 return rc;
7401}
7402
7403/**
7404 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7405 * @phba: Pointer to HBA context object.
7406 *
7407 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7408 * commands from the driver internal pending mailbox queue. It makes sure
7409 * that there is no outstanding mailbox command before resuming posting
7410 * asynchronous mailbox commands. If, for any reason, there is outstanding
7411 * mailbox command, it will try to wait it out before resuming asynchronous
7412 * mailbox command posting.
7413 **/
7414static void
7415lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7416{
7417 struct lpfc_sli *psli = &phba->sli;
7418
7419 spin_lock_irq(&phba->hbalock);
7420 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7421 /* Asynchronous mailbox posting is not blocked, do nothing */
7422 spin_unlock_irq(&phba->hbalock);
7423 return;
7424 }
7425
7426 /* Outstanding synchronous mailbox command is guaranteed to be done,
7427 * successful or timeout, after timing-out the outstanding mailbox
7428 * command shall always be removed, so just unblock posting async
7429 * mailbox command and resume
7430 */
7431 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7432 spin_unlock_irq(&phba->hbalock);
7433
7434 /* wake up worker thread to post asynchronlous mailbox command */
7435 lpfc_worker_wake_up(phba);
7436}
7437
7438/**
James Smart2d843ed2012-09-29 11:29:06 -04007439 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7440 * @phba: Pointer to HBA context object.
7441 * @mboxq: Pointer to mailbox object.
7442 *
7443 * The function waits for the bootstrap mailbox register ready bit from
7444 * port for twice the regular mailbox command timeout value.
7445 *
7446 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7447 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7448 **/
7449static int
7450lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7451{
7452 uint32_t db_ready;
7453 unsigned long timeout;
7454 struct lpfc_register bmbx_reg;
7455
7456 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7457 * 1000) + jiffies;
7458
7459 do {
7460 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7461 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7462 if (!db_ready)
7463 msleep(2);
7464
7465 if (time_after(jiffies, timeout))
7466 return MBXERR_ERROR;
7467 } while (!db_ready);
7468
7469 return 0;
7470}
7471
7472/**
James Smartda0436e2009-05-22 14:51:39 -04007473 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7474 * @phba: Pointer to HBA context object.
7475 * @mboxq: Pointer to mailbox object.
7476 *
7477 * The function posts a mailbox to the port. The mailbox is expected
7478 * to be comletely filled in and ready for the port to operate on it.
7479 * This routine executes a synchronous completion operation on the
7480 * mailbox by polling for its completion.
7481 *
7482 * The caller must not be holding any locks when calling this routine.
7483 *
7484 * Returns:
7485 * MBX_SUCCESS - mailbox posted successfully
7486 * Any of the MBX error values.
7487 **/
7488static int
7489lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7490{
7491 int rc = MBX_SUCCESS;
7492 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007493 uint32_t mcqe_status;
7494 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007495 struct lpfc_sli *psli = &phba->sli;
7496 struct lpfc_mqe *mb = &mboxq->u.mqe;
7497 struct lpfc_bmbx_create *mbox_rgn;
7498 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007499
7500 /*
7501 * Only one mailbox can be active to the bootstrap mailbox region
7502 * at a time and there is no queueing provided.
7503 */
7504 spin_lock_irqsave(&phba->hbalock, iflag);
7505 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7506 spin_unlock_irqrestore(&phba->hbalock, iflag);
7507 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007508 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007509 "cannot issue Data: x%x x%x\n",
7510 mboxq->vport ? mboxq->vport->vpi : 0,
7511 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007512 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7513 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007514 psli->sli_flag, MBX_POLL);
7515 return MBXERR_ERROR;
7516 }
7517 /* The server grabs the token and owns it until release */
7518 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7519 phba->sli.mbox_active = mboxq;
7520 spin_unlock_irqrestore(&phba->hbalock, iflag);
7521
James Smart2d843ed2012-09-29 11:29:06 -04007522 /* wait for bootstrap mbox register for readyness */
7523 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7524 if (rc)
7525 goto exit;
7526
James Smartda0436e2009-05-22 14:51:39 -04007527 /*
7528 * Initialize the bootstrap memory region to avoid stale data areas
7529 * in the mailbox post. Then copy the caller's mailbox contents to
7530 * the bmbx mailbox region.
7531 */
7532 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7533 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7534 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7535 sizeof(struct lpfc_mqe));
7536
7537 /* Post the high mailbox dma address to the port and wait for ready. */
7538 dma_address = &phba->sli4_hba.bmbx.dma_address;
7539 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7540
James Smart2d843ed2012-09-29 11:29:06 -04007541 /* wait for bootstrap mbox register for hi-address write done */
7542 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7543 if (rc)
7544 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007545
7546 /* Post the low mailbox dma address to the port. */
7547 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007548
James Smart2d843ed2012-09-29 11:29:06 -04007549 /* wait for bootstrap mbox register for low address write done */
7550 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7551 if (rc)
7552 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007553
7554 /*
7555 * Read the CQ to ensure the mailbox has completed.
7556 * If so, update the mailbox status so that the upper layers
7557 * can complete the request normally.
7558 */
7559 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7560 sizeof(struct lpfc_mqe));
7561 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7562 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7563 sizeof(struct lpfc_mcqe));
7564 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007565 /*
7566 * When the CQE status indicates a failure and the mailbox status
7567 * indicates success then copy the CQE status into the mailbox status
7568 * (and prefix it with x4000).
7569 */
James Smartda0436e2009-05-22 14:51:39 -04007570 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007571 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7572 bf_set(lpfc_mqe_status, mb,
7573 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007574 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007575 } else
7576 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007577
7578 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007579 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007580 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7581 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007582 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7583 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7584 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007585 bf_get(lpfc_mqe_status, mb),
7586 mb->un.mb_words[0], mb->un.mb_words[1],
7587 mb->un.mb_words[2], mb->un.mb_words[3],
7588 mb->un.mb_words[4], mb->un.mb_words[5],
7589 mb->un.mb_words[6], mb->un.mb_words[7],
7590 mb->un.mb_words[8], mb->un.mb_words[9],
7591 mb->un.mb_words[10], mb->un.mb_words[11],
7592 mb->un.mb_words[12], mboxq->mcqe.word0,
7593 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7594 mboxq->mcqe.trailer);
7595exit:
7596 /* We are holding the token, no needed for lock when release */
7597 spin_lock_irqsave(&phba->hbalock, iflag);
7598 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7599 phba->sli.mbox_active = NULL;
7600 spin_unlock_irqrestore(&phba->hbalock, iflag);
7601 return rc;
7602}
7603
7604/**
7605 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7606 * @phba: Pointer to HBA context object.
7607 * @pmbox: Pointer to mailbox object.
7608 * @flag: Flag indicating how the mailbox need to be processed.
7609 *
7610 * This function is called by discovery code and HBA management code to submit
7611 * a mailbox command to firmware with SLI-4 interface spec.
7612 *
7613 * Return codes the caller owns the mailbox command after the return of the
7614 * function.
7615 **/
7616static int
7617lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7618 uint32_t flag)
7619{
7620 struct lpfc_sli *psli = &phba->sli;
7621 unsigned long iflags;
7622 int rc;
7623
James Smartb76f2dc2011-07-22 18:37:42 -04007624 /* dump from issue mailbox command if setup */
7625 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7626
James Smart8fa38512009-07-19 10:01:03 -04007627 rc = lpfc_mbox_dev_check(phba);
7628 if (unlikely(rc)) {
7629 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007630 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007631 "cannot issue Data: x%x x%x\n",
7632 mboxq->vport ? mboxq->vport->vpi : 0,
7633 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007634 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7635 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007636 psli->sli_flag, flag);
7637 goto out_not_finished;
7638 }
7639
James Smartda0436e2009-05-22 14:51:39 -04007640 /* Detect polling mode and jump to a handler */
7641 if (!phba->sli4_hba.intr_enable) {
7642 if (flag == MBX_POLL)
7643 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7644 else
7645 rc = -EIO;
7646 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007647 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007648 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007649 "(x%x/x%x) failure: "
7650 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7651 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007652 mboxq->vport ? mboxq->vport->vpi : 0,
7653 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007654 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 Smartda0436e2009-05-22 14:51:39 -04007662 psli->sli_flag, flag);
7663 return rc;
7664 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007665 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7666 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007667 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007668 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007669 mboxq->vport ? mboxq->vport->vpi : 0,
7670 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007671 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7672 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007673 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007674 /* Try to block the asynchronous mailbox posting */
7675 rc = lpfc_sli4_async_mbox_block(phba);
7676 if (!rc) {
7677 /* Successfully blocked, now issue sync mbox cmd */
7678 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7679 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007680 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007681 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007682 "(%d):2597 Sync Mailbox command "
7683 "x%x (x%x/x%x) failure: "
7684 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7685 "Data: x%x x%x\n,",
7686 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007687 mboxq->u.mb.mbxCommand,
7688 lpfc_sli_config_mbox_subsys_get(phba,
7689 mboxq),
7690 lpfc_sli_config_mbox_opcode_get(phba,
7691 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007692 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7693 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7694 bf_get(lpfc_mcqe_ext_status,
7695 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007696 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007697 /* Unblock the async mailbox posting afterward */
7698 lpfc_sli4_async_mbox_unblock(phba);
7699 }
7700 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007701 }
7702
7703 /* Now, interrupt mode asynchrous mailbox command */
7704 rc = lpfc_mbox_cmd_check(phba, mboxq);
7705 if (rc) {
7706 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007707 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007708 "cannot issue Data: x%x x%x\n",
7709 mboxq->vport ? mboxq->vport->vpi : 0,
7710 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007711 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7712 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007713 psli->sli_flag, flag);
7714 goto out_not_finished;
7715 }
James Smartda0436e2009-05-22 14:51:39 -04007716
7717 /* Put the mailbox command to the driver internal FIFO */
7718 psli->slistat.mbox_busy++;
7719 spin_lock_irqsave(&phba->hbalock, iflags);
7720 lpfc_mbox_put(phba, mboxq);
7721 spin_unlock_irqrestore(&phba->hbalock, iflags);
7722 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7723 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007724 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007725 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7726 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007727 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7728 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007729 phba->pport->port_state,
7730 psli->sli_flag, MBX_NOWAIT);
7731 /* Wake up worker thread to transport mailbox command from head */
7732 lpfc_worker_wake_up(phba);
7733
7734 return MBX_BUSY;
7735
7736out_not_finished:
7737 return MBX_NOT_FINISHED;
7738}
7739
7740/**
7741 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7742 * @phba: Pointer to HBA context object.
7743 *
7744 * This function is called by worker thread to send a mailbox command to
7745 * SLI4 HBA firmware.
7746 *
7747 **/
7748int
7749lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7750{
7751 struct lpfc_sli *psli = &phba->sli;
7752 LPFC_MBOXQ_t *mboxq;
7753 int rc = MBX_SUCCESS;
7754 unsigned long iflags;
7755 struct lpfc_mqe *mqe;
7756 uint32_t mbx_cmnd;
7757
7758 /* Check interrupt mode before post async mailbox command */
7759 if (unlikely(!phba->sli4_hba.intr_enable))
7760 return MBX_NOT_FINISHED;
7761
7762 /* Check for mailbox command service token */
7763 spin_lock_irqsave(&phba->hbalock, iflags);
7764 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7765 spin_unlock_irqrestore(&phba->hbalock, iflags);
7766 return MBX_NOT_FINISHED;
7767 }
7768 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7769 spin_unlock_irqrestore(&phba->hbalock, iflags);
7770 return MBX_NOT_FINISHED;
7771 }
7772 if (unlikely(phba->sli.mbox_active)) {
7773 spin_unlock_irqrestore(&phba->hbalock, iflags);
7774 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7775 "0384 There is pending active mailbox cmd\n");
7776 return MBX_NOT_FINISHED;
7777 }
7778 /* Take the mailbox command service token */
7779 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7780
7781 /* Get the next mailbox command from head of queue */
7782 mboxq = lpfc_mbox_get(phba);
7783
7784 /* If no more mailbox command waiting for post, we're done */
7785 if (!mboxq) {
7786 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7787 spin_unlock_irqrestore(&phba->hbalock, iflags);
7788 return MBX_SUCCESS;
7789 }
7790 phba->sli.mbox_active = mboxq;
7791 spin_unlock_irqrestore(&phba->hbalock, iflags);
7792
7793 /* Check device readiness for posting mailbox command */
7794 rc = lpfc_mbox_dev_check(phba);
7795 if (unlikely(rc))
7796 /* Driver clean routine will clean up pending mailbox */
7797 goto out_not_finished;
7798
7799 /* Prepare the mbox command to be posted */
7800 mqe = &mboxq->u.mqe;
7801 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7802
7803 /* Start timer for the mbox_tmo and log some mailbox post messages */
7804 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007805 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007806
7807 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007808 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007809 "x%x x%x\n",
7810 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007811 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7812 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007813 phba->pport->port_state, psli->sli_flag);
7814
7815 if (mbx_cmnd != MBX_HEARTBEAT) {
7816 if (mboxq->vport) {
7817 lpfc_debugfs_disc_trc(mboxq->vport,
7818 LPFC_DISC_TRC_MBOX_VPORT,
7819 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7820 mbx_cmnd, mqe->un.mb_words[0],
7821 mqe->un.mb_words[1]);
7822 } else {
7823 lpfc_debugfs_disc_trc(phba->pport,
7824 LPFC_DISC_TRC_MBOX,
7825 "MBOX Send: cmd:x%x mb:x%x x%x",
7826 mbx_cmnd, mqe->un.mb_words[0],
7827 mqe->un.mb_words[1]);
7828 }
7829 }
7830 psli->slistat.mbox_cmd++;
7831
7832 /* Post the mailbox command to the port */
7833 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7834 if (rc != MBX_SUCCESS) {
7835 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007836 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007837 "cannot issue Data: x%x x%x\n",
7838 mboxq->vport ? mboxq->vport->vpi : 0,
7839 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007840 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7841 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007842 psli->sli_flag, MBX_NOWAIT);
7843 goto out_not_finished;
7844 }
7845
7846 return rc;
7847
7848out_not_finished:
7849 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007850 if (phba->sli.mbox_active) {
7851 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7852 __lpfc_mbox_cmpl_put(phba, mboxq);
7853 /* Release the token */
7854 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7855 phba->sli.mbox_active = NULL;
7856 }
James Smartda0436e2009-05-22 14:51:39 -04007857 spin_unlock_irqrestore(&phba->hbalock, iflags);
7858
7859 return MBX_NOT_FINISHED;
7860}
7861
7862/**
7863 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7864 * @phba: Pointer to HBA context object.
7865 * @pmbox: Pointer to mailbox object.
7866 * @flag: Flag indicating how the mailbox need to be processed.
7867 *
7868 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7869 * the API jump table function pointer from the lpfc_hba struct.
7870 *
7871 * Return codes the caller owns the mailbox command after the return of the
7872 * function.
7873 **/
7874int
7875lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7876{
7877 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7878}
7879
7880/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007881 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007882 * @phba: The hba struct for which this call is being executed.
7883 * @dev_grp: The HBA PCI-Device group number.
7884 *
7885 * This routine sets up the mbox interface API function jump table in @phba
7886 * struct.
7887 * Returns: 0 - success, -ENODEV - failure.
7888 **/
7889int
7890lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7891{
7892
7893 switch (dev_grp) {
7894 case LPFC_PCI_DEV_LP:
7895 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7896 phba->lpfc_sli_handle_slow_ring_event =
7897 lpfc_sli_handle_slow_ring_event_s3;
7898 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7899 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7900 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7901 break;
7902 case LPFC_PCI_DEV_OC:
7903 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7904 phba->lpfc_sli_handle_slow_ring_event =
7905 lpfc_sli_handle_slow_ring_event_s4;
7906 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7907 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7908 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7909 break;
7910 default:
7911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7912 "1420 Invalid HBA PCI-device group: 0x%x\n",
7913 dev_grp);
7914 return -ENODEV;
7915 break;
7916 }
7917 return 0;
7918}
7919
7920/**
James Smart3621a712009-04-06 18:47:14 -04007921 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007922 * @phba: Pointer to HBA context object.
7923 * @pring: Pointer to driver SLI ring object.
7924 * @piocb: Pointer to address of newly added command iocb.
7925 *
7926 * This function is called with hbalock held to add a command
7927 * iocb to the txq when SLI layer cannot submit the command iocb
7928 * to the ring.
7929 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007930void
James Smart92d7f7b2007-06-17 19:56:38 -05007931__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007932 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007933{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007934 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007935 /* Insert the caller's iocb in the txq tail for later processing. */
7936 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007937}
7938
James Smarte59058c2008-08-24 21:49:00 -04007939/**
James Smart3621a712009-04-06 18:47:14 -04007940 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007941 * @phba: Pointer to HBA context object.
7942 * @pring: Pointer to driver SLI ring object.
7943 * @piocb: Pointer to address of newly added command iocb.
7944 *
7945 * This function is called with hbalock held before a new
7946 * iocb is submitted to the firmware. This function checks
7947 * txq to flush the iocbs in txq to Firmware before
7948 * submitting new iocbs to the Firmware.
7949 * If there are iocbs in the txq which need to be submitted
7950 * to firmware, lpfc_sli_next_iocb returns the first element
7951 * of the txq after dequeuing it from txq.
7952 * If there is no iocb in the txq then the function will return
7953 * *piocb and *piocb is set to NULL. Caller needs to check
7954 * *piocb to find if there are more commands in the txq.
7955 **/
dea31012005-04-17 16:05:31 -05007956static struct lpfc_iocbq *
7957lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007958 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007959{
7960 struct lpfc_iocbq * nextiocb;
7961
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007962 lockdep_assert_held(&phba->hbalock);
7963
dea31012005-04-17 16:05:31 -05007964 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7965 if (!nextiocb) {
7966 nextiocb = *piocb;
7967 *piocb = NULL;
7968 }
7969
7970 return nextiocb;
7971}
7972
James Smarte59058c2008-08-24 21:49:00 -04007973/**
James Smart3772a992009-05-22 14:50:54 -04007974 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007975 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007976 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007977 * @piocb: Pointer to command iocb.
7978 * @flag: Flag indicating if this command can be put into txq.
7979 *
James Smart3772a992009-05-22 14:50:54 -04007980 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7981 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7982 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7983 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7984 * this function allows only iocbs for posting buffers. This function finds
7985 * next available slot in the command ring and posts the command to the
7986 * available slot and writes the port attention register to request HBA start
7987 * processing new iocb. If there is no slot available in the ring and
7988 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7989 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007990 *
James Smart3772a992009-05-22 14:50:54 -04007991 * This function is called with hbalock held. The function will return success
7992 * after it successfully submit the iocb to firmware or after adding to the
7993 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007994 **/
James Smart98c9ea52007-10-27 13:37:33 -04007995static int
James Smart3772a992009-05-22 14:50:54 -04007996__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007997 struct lpfc_iocbq *piocb, uint32_t flag)
7998{
7999 struct lpfc_iocbq *nextiocb;
8000 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04008001 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05008002
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008003 lockdep_assert_held(&phba->hbalock);
8004
James Smart92d7f7b2007-06-17 19:56:38 -05008005 if (piocb->iocb_cmpl && (!piocb->vport) &&
8006 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8007 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8008 lpfc_printf_log(phba, KERN_ERR,
8009 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008010 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008011 piocb->iocb.ulpCommand);
8012 dump_stack();
8013 return IOCB_ERROR;
8014 }
8015
8016
Linas Vepstas8d63f372007-02-14 14:28:36 -06008017 /* If the PCI channel is in offline state, do not post iocbs. */
8018 if (unlikely(pci_channel_offline(phba->pcidev)))
8019 return IOCB_ERROR;
8020
James Smarta257bf92009-04-06 18:48:10 -04008021 /* If HBA has a deferred error attention, fail the iocb. */
8022 if (unlikely(phba->hba_flag & DEFER_ERATT))
8023 return IOCB_ERROR;
8024
dea31012005-04-17 16:05:31 -05008025 /*
8026 * We should never get an IOCB if we are in a < LINK_DOWN state
8027 */
James Smart2e0fef82007-06-17 19:56:36 -05008028 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008029 return IOCB_ERROR;
8030
8031 /*
8032 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008033 * outstanding event.
dea31012005-04-17 16:05:31 -05008034 */
James Smart0b727fe2007-10-27 13:37:25 -04008035 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008036 goto iocb_busy;
8037
James Smart2e0fef82007-06-17 19:56:36 -05008038 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008039 /*
James Smart2680eea2007-04-25 09:52:55 -04008040 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008041 * can be issued if the link is not up.
8042 */
8043 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008044 case CMD_GEN_REQUEST64_CR:
8045 case CMD_GEN_REQUEST64_CX:
8046 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8047 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008048 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008049 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8050 MENLO_TRANSPORT_TYPE))
8051
8052 goto iocb_busy;
8053 break;
dea31012005-04-17 16:05:31 -05008054 case CMD_QUE_RING_BUF_CN:
8055 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008056 /*
8057 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8058 * completion, iocb_cmpl MUST be 0.
8059 */
8060 if (piocb->iocb_cmpl)
8061 piocb->iocb_cmpl = NULL;
8062 /*FALLTHROUGH*/
8063 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008064 case CMD_CLOSE_XRI_CN:
8065 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008066 break;
8067 default:
8068 goto iocb_busy;
8069 }
8070
8071 /*
8072 * For FCP commands, we must be in a state where we can process link
8073 * attention events.
8074 */
8075 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05008076 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008077 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008078 }
dea31012005-04-17 16:05:31 -05008079
dea31012005-04-17 16:05:31 -05008080 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8081 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8082 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8083
8084 if (iocb)
8085 lpfc_sli_update_ring(phba, pring);
8086 else
8087 lpfc_sli_update_full_ring(phba, pring);
8088
8089 if (!piocb)
8090 return IOCB_SUCCESS;
8091
8092 goto out_busy;
8093
8094 iocb_busy:
8095 pring->stats.iocb_cmd_delay++;
8096
8097 out_busy:
8098
8099 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008100 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008101 return IOCB_SUCCESS;
8102 }
8103
8104 return IOCB_BUSY;
8105}
8106
James Smart3772a992009-05-22 14:50:54 -04008107/**
James Smart4f774512009-05-22 14:52:35 -04008108 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8109 * @phba: Pointer to HBA context object.
8110 * @piocb: Pointer to command iocb.
8111 * @sglq: Pointer to the scatter gather queue object.
8112 *
8113 * This routine converts the bpl or bde that is in the IOCB
8114 * to a sgl list for the sli4 hardware. The physical address
8115 * of the bpl/bde is converted back to a virtual address.
8116 * If the IOCB contains a BPL then the list of BDE's is
8117 * converted to sli4_sge's. If the IOCB contains a single
8118 * BDE then it is converted to a single sli_sge.
8119 * The IOCB is still in cpu endianess so the contents of
8120 * the bpl can be used without byte swapping.
8121 *
8122 * Returns valid XRI = Success, NO_XRI = Failure.
8123**/
8124static uint16_t
8125lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8126 struct lpfc_sglq *sglq)
8127{
8128 uint16_t xritag = NO_XRI;
8129 struct ulp_bde64 *bpl = NULL;
8130 struct ulp_bde64 bde;
8131 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008132 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008133 IOCB_t *icmd;
8134 int numBdes = 0;
8135 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008136 uint32_t offset = 0; /* accumulated offset in the sg request list */
8137 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008138
8139 if (!piocbq || !sglq)
8140 return xritag;
8141
8142 sgl = (struct sli4_sge *)sglq->sgl;
8143 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008144 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8145 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008146 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8147 numBdes = icmd->un.genreq64.bdl.bdeSize /
8148 sizeof(struct ulp_bde64);
8149 /* The addrHigh and addrLow fields within the IOCB
8150 * have not been byteswapped yet so there is no
8151 * need to swap them back.
8152 */
James Smart1b511972011-12-13 13:23:09 -05008153 if (piocbq->context3)
8154 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8155 else
8156 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008157
James Smart1b511972011-12-13 13:23:09 -05008158 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008159 if (!bpl)
8160 return xritag;
8161
8162 for (i = 0; i < numBdes; i++) {
8163 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008164 sgl->addr_hi = bpl->addrHigh;
8165 sgl->addr_lo = bpl->addrLow;
8166
James Smart05580562011-05-24 11:40:48 -04008167 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008168 if ((i+1) == numBdes)
8169 bf_set(lpfc_sli4_sge_last, sgl, 1);
8170 else
8171 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008172 /* swap the size field back to the cpu so we
8173 * can assign it to the sgl.
8174 */
8175 bde.tus.w = le32_to_cpu(bpl->tus.w);
8176 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008177 /* The offsets in the sgl need to be accumulated
8178 * separately for the request and reply lists.
8179 * The request is always first, the reply follows.
8180 */
8181 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8182 /* add up the reply sg entries */
8183 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8184 inbound++;
8185 /* first inbound? reset the offset */
8186 if (inbound == 1)
8187 offset = 0;
8188 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008189 bf_set(lpfc_sli4_sge_type, sgl,
8190 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008191 offset += bde.tus.f.bdeSize;
8192 }
James Smart546fc852011-03-11 16:06:29 -05008193 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008194 bpl++;
8195 sgl++;
8196 }
8197 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8198 /* The addrHigh and addrLow fields of the BDE have not
8199 * been byteswapped yet so they need to be swapped
8200 * before putting them in the sgl.
8201 */
8202 sgl->addr_hi =
8203 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8204 sgl->addr_lo =
8205 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008206 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008207 bf_set(lpfc_sli4_sge_last, sgl, 1);
8208 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008209 sgl->sge_len =
8210 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008211 }
8212 return sglq->sli4_xritag;
8213}
8214
8215/**
James Smart4f774512009-05-22 14:52:35 -04008216 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8217 * @phba: Pointer to HBA context object.
8218 * @piocb: Pointer to command iocb.
8219 * @wqe: Pointer to the work queue entry.
8220 *
8221 * This routine converts the iocb command to its Work Queue Entry
8222 * equivalent. The wqe pointer should not have any fields set when
8223 * this routine is called because it will memcpy over them.
8224 * This routine does not set the CQ_ID or the WQEC bits in the
8225 * wqe.
8226 *
8227 * Returns: 0 = Success, IOCB_ERROR = Failure.
8228 **/
8229static int
8230lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8231 union lpfc_wqe *wqe)
8232{
James Smart5ffc2662009-11-18 15:39:44 -05008233 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008234 uint8_t ct = 0;
8235 uint32_t fip;
8236 uint32_t abort_tag;
8237 uint8_t command_type = ELS_COMMAND_NON_FIP;
8238 uint8_t cmnd;
8239 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008240 uint16_t abrt_iotag;
8241 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008242 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008243 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008244 int numBdes, i;
8245 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008246 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008247 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008248 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008249
James Smart45ed1192009-10-02 15:17:02 -04008250 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008251 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008252 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008253 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008254 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008255 command_type = ELS_COMMAND_FIP;
8256 else
8257 command_type = ELS_COMMAND_NON_FIP;
8258
James Smartb5c53952016-03-31 14:12:30 -07008259 if (phba->fcp_embed_io)
8260 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008261 /* Some of the fields are in the right position already */
8262 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008263 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008264 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008265
8266 abort_tag = (uint32_t) iocbq->iotag;
8267 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008268 /* words0-2 bpl convert bde */
8269 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008270 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8271 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008272 bpl = (struct ulp_bde64 *)
8273 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8274 if (!bpl)
8275 return IOCB_ERROR;
8276
8277 /* Should already be byte swapped. */
8278 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8279 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8280 /* swap the size field back to the cpu so we
8281 * can assign it to the sgl.
8282 */
8283 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008284 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8285 total_len = 0;
8286 for (i = 0; i < numBdes; i++) {
8287 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8288 total_len += bde.tus.f.bdeSize;
8289 }
James Smart4f774512009-05-22 14:52:35 -04008290 } else
James Smart5ffc2662009-11-18 15:39:44 -05008291 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008292
8293 iocbq->iocb.ulpIoTag = iocbq->iotag;
8294 cmnd = iocbq->iocb.ulpCommand;
8295
8296 switch (iocbq->iocb.ulpCommand) {
8297 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008298 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8299 ndlp = iocbq->context_un.ndlp;
8300 else
8301 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008302 if (!iocbq->iocb.ulpLe) {
8303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8304 "2007 Only Limited Edition cmd Format"
8305 " supported 0x%x\n",
8306 iocbq->iocb.ulpCommand);
8307 return IOCB_ERROR;
8308 }
James Smartff78d8f2011-12-13 13:21:35 -05008309
James Smart5ffc2662009-11-18 15:39:44 -05008310 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008311 /* Els_reguest64 has a TMO */
8312 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8313 iocbq->iocb.ulpTimeout);
8314 /* Need a VF for word 4 set the vf bit*/
8315 bf_set(els_req64_vf, &wqe->els_req, 0);
8316 /* And a VFID for word 12 */
8317 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008318 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008319 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8320 iocbq->iocb.ulpContext);
8321 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8322 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008323 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008324 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008325 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8326 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008327 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8328 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008329 if_type = bf_get(lpfc_sli_intf_if_type,
8330 &phba->sli4_hba.sli_intf);
8331 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008332 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008333 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008334 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008335 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008336 *pcmd == ELS_CMD_PLOGI)) {
8337 bf_set(els_req64_sp, &wqe->els_req, 1);
8338 bf_set(els_req64_sid, &wqe->els_req,
8339 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008340 if ((*pcmd == ELS_CMD_FLOGI) &&
8341 !(phba->fc_topology ==
8342 LPFC_TOPOLOGY_LOOP))
8343 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008344 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8345 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008346 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008347 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008348 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8349 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8350 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8351 }
James Smartc8685952009-11-18 15:39:16 -05008352 }
James Smart6d368e52011-05-24 11:44:12 -04008353 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8354 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008355 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8356 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8357 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8358 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8359 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8360 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008361 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008362 break;
James Smart5ffc2662009-11-18 15:39:44 -05008363 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008364 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8365 iocbq->iocb.un.ulpWord[3]);
8366 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008367 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008368 /* The entire sequence is transmitted for this IOCB */
8369 xmit_len = total_len;
8370 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008371 if (phba->link_flag & LS_LOOPBACK_MODE)
8372 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008373 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008374 /* word3 iocb=io_tag32 wqe=reserved */
8375 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008376 /* word4 relative_offset memcpy */
8377 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008378 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8379 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8380 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8381 LPFC_WQE_IOD_WRITE);
8382 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8383 LPFC_WQE_LENLOC_WORD12);
8384 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008385 wqe->xmit_sequence.xmit_len = xmit_len;
8386 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008387 break;
James Smart4f774512009-05-22 14:52:35 -04008388 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008389 /* word3 iocb=iotag32 wqe=seq_payload_len */
8390 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008391 /* word4 iocb=rsvd wqe=rsvd */
8392 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8393 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008394 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008395 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008396 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8397 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8398 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8399 LPFC_WQE_LENLOC_WORD3);
8400 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008401 break;
James Smart4f774512009-05-22 14:52:35 -04008402 case CMD_FCP_IWRITE64_CR:
8403 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008404 /* word3 iocb=iotag wqe=payload_offset_len */
8405 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008406 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8407 xmit_len + sizeof(struct fcp_rsp));
8408 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8409 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008410 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8411 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8412 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8413 iocbq->iocb.ulpFCP2Rcvy);
8414 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8415 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008416 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8417 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8418 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008419 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008420 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008421 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8422 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8423 if (phba->cfg_XLanePriority) {
8424 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8425 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8426 (phba->cfg_XLanePriority << 1));
8427 }
8428 }
James Smartb5c53952016-03-31 14:12:30 -07008429 /* Note, word 10 is already initialized to 0 */
8430
8431 if (phba->fcp_embed_io) {
8432 struct lpfc_scsi_buf *lpfc_cmd;
8433 struct sli4_sge *sgl;
8434 union lpfc_wqe128 *wqe128;
8435 struct fcp_cmnd *fcp_cmnd;
8436 uint32_t *ptr;
8437
8438 /* 128 byte wqe support here */
8439 wqe128 = (union lpfc_wqe128 *)wqe;
8440
8441 lpfc_cmd = iocbq->context1;
8442 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8443 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8444
8445 /* Word 0-2 - FCP_CMND */
8446 wqe128->generic.bde.tus.f.bdeFlags =
8447 BUFF_TYPE_BDE_IMMED;
8448 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8449 wqe128->generic.bde.addrHigh = 0;
8450 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8451
8452 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8453
8454 /* Word 22-29 FCP CMND Payload */
8455 ptr = &wqe128->words[22];
8456 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8457 }
James Smart7851fe22011-07-22 18:36:52 -04008458 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008459 case CMD_FCP_IREAD64_CR:
8460 /* word3 iocb=iotag wqe=payload_offset_len */
8461 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008462 bf_set(payload_offset_len, &wqe->fcp_iread,
8463 xmit_len + sizeof(struct fcp_rsp));
8464 bf_set(cmd_buff_len, &wqe->fcp_iread,
8465 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008466 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8467 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8468 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8469 iocbq->iocb.ulpFCP2Rcvy);
8470 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008471 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008472 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8473 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8474 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008475 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008476 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008477 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8478 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8479 if (phba->cfg_XLanePriority) {
8480 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8481 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8482 (phba->cfg_XLanePriority << 1));
8483 }
8484 }
James Smartb5c53952016-03-31 14:12:30 -07008485 /* Note, word 10 is already initialized to 0 */
8486
8487 if (phba->fcp_embed_io) {
8488 struct lpfc_scsi_buf *lpfc_cmd;
8489 struct sli4_sge *sgl;
8490 union lpfc_wqe128 *wqe128;
8491 struct fcp_cmnd *fcp_cmnd;
8492 uint32_t *ptr;
8493
8494 /* 128 byte wqe support here */
8495 wqe128 = (union lpfc_wqe128 *)wqe;
8496
8497 lpfc_cmd = iocbq->context1;
8498 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8499 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8500
8501 /* Word 0-2 - FCP_CMND */
8502 wqe128->generic.bde.tus.f.bdeFlags =
8503 BUFF_TYPE_BDE_IMMED;
8504 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8505 wqe128->generic.bde.addrHigh = 0;
8506 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8507
8508 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8509
8510 /* Word 22-29 FCP CMND Payload */
8511 ptr = &wqe128->words[22];
8512 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8513 }
James Smart7851fe22011-07-22 18:36:52 -04008514 break;
James Smartf1126682009-06-10 17:22:44 -04008515 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008516 /* word3 iocb=iotag wqe=payload_offset_len */
8517 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8518 bf_set(payload_offset_len, &wqe->fcp_icmd,
8519 xmit_len + sizeof(struct fcp_rsp));
8520 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8521 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008522 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008523 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008524 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008525 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8526 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8527 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8528 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8529 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008530 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8531 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008532 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8533 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8534 if (phba->cfg_XLanePriority) {
8535 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8536 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8537 (phba->cfg_XLanePriority << 1));
8538 }
8539 }
James Smartb5c53952016-03-31 14:12:30 -07008540 /* Note, word 10 is already initialized to 0 */
8541
8542 if (phba->fcp_embed_io) {
8543 struct lpfc_scsi_buf *lpfc_cmd;
8544 struct sli4_sge *sgl;
8545 union lpfc_wqe128 *wqe128;
8546 struct fcp_cmnd *fcp_cmnd;
8547 uint32_t *ptr;
8548
8549 /* 128 byte wqe support here */
8550 wqe128 = (union lpfc_wqe128 *)wqe;
8551
8552 lpfc_cmd = iocbq->context1;
8553 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8554 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8555
8556 /* Word 0-2 - FCP_CMND */
8557 wqe128->generic.bde.tus.f.bdeFlags =
8558 BUFF_TYPE_BDE_IMMED;
8559 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8560 wqe128->generic.bde.addrHigh = 0;
8561 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8562
8563 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8564
8565 /* Word 22-29 FCP CMND Payload */
8566 ptr = &wqe128->words[22];
8567 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8568 }
James Smart7851fe22011-07-22 18:36:52 -04008569 break;
James Smart4f774512009-05-22 14:52:35 -04008570 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008571 /* For this command calculate the xmit length of the
8572 * request bde.
8573 */
8574 xmit_len = 0;
8575 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8576 sizeof(struct ulp_bde64);
8577 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008578 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008579 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8580 break;
James Smart63e801c2010-11-20 23:14:19 -05008581 xmit_len += bde.tus.f.bdeSize;
8582 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008583 /* word3 iocb=IO_TAG wqe=request_payload_len */
8584 wqe->gen_req.request_payload_len = xmit_len;
8585 /* word4 iocb=parameter wqe=relative_offset memcpy */
8586 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008587 /* word6 context tag copied in memcpy */
8588 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8589 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8590 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8591 "2015 Invalid CT %x command 0x%x\n",
8592 ct, iocbq->iocb.ulpCommand);
8593 return IOCB_ERROR;
8594 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008595 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8596 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8597 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8598 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8599 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8600 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8601 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8602 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008603 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008604 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008605 break;
James Smart4f774512009-05-22 14:52:35 -04008606 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008607 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008608 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008609 /* word3 iocb=iotag32 wqe=response_payload_len */
8610 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008611 /* word4 */
8612 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008613 /* word5 iocb=rsvd wge=did */
8614 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008615 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8616
8617 if_type = bf_get(lpfc_sli_intf_if_type,
8618 &phba->sli4_hba.sli_intf);
8619 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8620 if (iocbq->vport->fc_flag & FC_PT2PT) {
8621 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8622 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8623 iocbq->vport->fc_myDID);
8624 if (iocbq->vport->fc_myDID == Fabric_DID) {
8625 bf_set(wqe_els_did,
8626 &wqe->xmit_els_rsp.wqe_dest, 0);
8627 }
8628 }
8629 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008630 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8631 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8632 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8633 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008634 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008635 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008636 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008637 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008638 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8639 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8640 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8641 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8642 LPFC_WQE_LENLOC_WORD3);
8643 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008644 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8645 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008646 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8647 iocbq->context2)->virt);
8648 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008649 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8650 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008651 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008652 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8653 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008654 phba->vpi_ids[phba->pport->vpi]);
8655 }
James Smart4f774512009-05-22 14:52:35 -04008656 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008657 break;
James Smart4f774512009-05-22 14:52:35 -04008658 case CMD_CLOSE_XRI_CN:
8659 case CMD_ABORT_XRI_CN:
8660 case CMD_ABORT_XRI_CX:
8661 /* words 0-2 memcpy should be 0 rserved */
8662 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008663 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8664 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8665 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8666 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8667 } else
8668 fip = 0;
8669
8670 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008671 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008672 * The link is down, or the command was ELS_FIP
8673 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008674 * on the wire.
8675 */
8676 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8677 else
8678 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8679 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008680 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8681 wqe->abort_cmd.rsrvd5 = 0;
8682 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008683 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8684 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008685 /*
8686 * The abort handler will send us CMD_ABORT_XRI_CN or
8687 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8688 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008689 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8690 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8691 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8692 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008693 cmnd = CMD_ABORT_XRI_CX;
8694 command_type = OTHER_COMMAND;
8695 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008696 break;
James Smart6669f9b2009-10-02 15:16:45 -04008697 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008698 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008699 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008700 * we re-construct this WQE here based on information in
8701 * iocbq from scratch.
8702 */
8703 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008704 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008705 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008706 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8707 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008708 LPFC_ABTS_UNSOL_INT) {
8709 /* ABTS sent by initiator to CT exchange, the
8710 * RX_ID field will be filled with the newly
8711 * allocated responder XRI.
8712 */
8713 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8714 iocbq->sli4_xritag);
8715 } else {
8716 /* ABTS sent by responder to CT exchange, the
8717 * RX_ID field will be filled with the responder
8718 * RX_ID from ABTS.
8719 */
8720 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008721 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008722 }
James Smart6669f9b2009-10-02 15:16:45 -04008723 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8724 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008725
8726 /* Use CT=VPI */
8727 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8728 ndlp->nlp_DID);
8729 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8730 iocbq->iocb.ulpContext);
8731 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008732 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008733 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008734 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8735 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8736 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008737 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8738 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008739 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8740 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8741 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8742 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8743 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8744 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8745 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8746 }
8747
James Smart7851fe22011-07-22 18:36:52 -04008748 break;
James Smart4f774512009-05-22 14:52:35 -04008749 case CMD_XRI_ABORTED_CX:
8750 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008751 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8752 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8753 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8754 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8755 default:
8756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8757 "2014 Invalid command 0x%x\n",
8758 iocbq->iocb.ulpCommand);
8759 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008760 break;
James Smart4f774512009-05-22 14:52:35 -04008761 }
James Smart6d368e52011-05-24 11:44:12 -04008762
James Smart8012cc32012-10-31 14:44:49 -04008763 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8764 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8765 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8766 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8767 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8768 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8769 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8770 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008771 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8772 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8773 wqe->generic.wqe_com.abort_tag = abort_tag;
8774 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8775 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8776 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8777 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008778 return 0;
8779}
8780
8781/**
8782 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8783 * @phba: Pointer to HBA context object.
8784 * @ring_number: SLI ring number to issue iocb on.
8785 * @piocb: Pointer to command iocb.
8786 * @flag: Flag indicating if this command can be put into txq.
8787 *
8788 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8789 * an iocb command to an HBA with SLI-4 interface spec.
8790 *
8791 * This function is called with hbalock held. The function will return success
8792 * after it successfully submit the iocb to firmware or after adding to the
8793 * txq.
8794 **/
8795static int
8796__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8797 struct lpfc_iocbq *piocb, uint32_t flag)
8798{
8799 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008800 union lpfc_wqe *wqe;
8801 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008802 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008803 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008804
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008805 lockdep_assert_held(&phba->hbalock);
8806
James Smartb5c53952016-03-31 14:12:30 -07008807 /*
8808 * The WQE can be either 64 or 128 bytes,
8809 * so allocate space on the stack assuming the largest.
8810 */
8811 wqe = (union lpfc_wqe *)&wqe128;
8812
James Smart4f774512009-05-22 14:52:35 -04008813 if (piocb->sli4_xritag == NO_XRI) {
8814 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008815 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008816 sglq = NULL;
8817 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008818 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008819 if (!(flag & SLI_IOCB_RET_IOCB)) {
8820 __lpfc_sli_ringtx_put(phba,
8821 pring, piocb);
8822 return IOCB_SUCCESS;
8823 } else {
8824 return IOCB_BUSY;
8825 }
8826 } else {
James Smart6d368e52011-05-24 11:44:12 -04008827 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008828 if (!sglq) {
8829 if (!(flag & SLI_IOCB_RET_IOCB)) {
8830 __lpfc_sli_ringtx_put(phba,
8831 pring,
8832 piocb);
8833 return IOCB_SUCCESS;
8834 } else
8835 return IOCB_BUSY;
8836 }
8837 }
James Smart4f774512009-05-22 14:52:35 -04008838 }
8839 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008840 /* These IO's already have an XRI and a mapped sgl. */
8841 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008842 } else {
James Smart6d368e52011-05-24 11:44:12 -04008843 /*
8844 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008845 * sglq is on the active list
8846 */
James Smartedccdc12013-01-03 15:43:45 -05008847 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008848 if (!sglq)
8849 return IOCB_ERROR;
8850 }
8851
8852 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008853 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008854 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008855 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008856 return IOCB_ERROR;
8857 }
8858
James Smartb5c53952016-03-31 14:12:30 -07008859 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008860 return IOCB_ERROR;
8861
James Smart341af102010-01-26 23:07:37 -05008862 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008863 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008864 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008865 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8866 } else {
8867 wq = phba->sli4_hba.oas_wq;
8868 }
James Smartb5c53952016-03-31 14:12:30 -07008869 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008870 return IOCB_ERROR;
8871 } else {
James Smartea714f32013-04-17 20:18:47 -04008872 if (unlikely(!phba->sli4_hba.els_wq))
8873 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008874 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008875 return IOCB_ERROR;
8876 }
8877 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8878
8879 return 0;
8880}
8881
8882/**
James Smart3772a992009-05-22 14:50:54 -04008883 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8884 *
8885 * This routine wraps the actual lockless version for issusing IOCB function
8886 * pointer from the lpfc_hba struct.
8887 *
8888 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008889 * IOCB_ERROR - Error
8890 * IOCB_SUCCESS - Success
8891 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008892 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008893int
James Smart3772a992009-05-22 14:50:54 -04008894__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8895 struct lpfc_iocbq *piocb, uint32_t flag)
8896{
8897 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8898}
8899
8900/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008901 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008902 * @phba: The hba struct for which this call is being executed.
8903 * @dev_grp: The HBA PCI-Device group number.
8904 *
8905 * This routine sets up the SLI interface API function jump table in @phba
8906 * struct.
8907 * Returns: 0 - success, -ENODEV - failure.
8908 **/
8909int
8910lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8911{
8912
8913 switch (dev_grp) {
8914 case LPFC_PCI_DEV_LP:
8915 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8916 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8917 break;
James Smart4f774512009-05-22 14:52:35 -04008918 case LPFC_PCI_DEV_OC:
8919 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8920 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8921 break;
James Smart3772a992009-05-22 14:50:54 -04008922 default:
8923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8924 "1419 Invalid HBA PCI-device group: 0x%x\n",
8925 dev_grp);
8926 return -ENODEV;
8927 break;
8928 }
8929 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8930 return 0;
8931}
James Smart92d7f7b2007-06-17 19:56:38 -05008932
James Smarta1efe162015-05-21 13:55:20 -04008933/**
8934 * lpfc_sli_calc_ring - Calculates which ring to use
8935 * @phba: Pointer to HBA context object.
8936 * @ring_number: Initial ring
8937 * @piocb: Pointer to command iocb.
8938 *
8939 * For SLI4, FCP IO can deferred to one fo many WQs, based on
8940 * fcp_wqidx, thus we need to calculate the corresponding ring.
8941 * Since ABORTS must go on the same WQ of the command they are
8942 * aborting, we use command's fcp_wqidx.
8943 */
James Smart9bd2bff52014-09-03 12:57:30 -04008944int
8945lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
8946 struct lpfc_iocbq *piocb)
8947{
James Smart8b0dff12015-05-22 10:42:38 -04008948 if (phba->sli_rev < LPFC_SLI_REV4)
8949 return ring_number;
8950
8951 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8952 if (!(phba->cfg_fof) ||
8953 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8954 if (unlikely(!phba->sli4_hba.fcp_wq))
8955 return LPFC_HBA_ERROR;
8956 /*
8957 * for abort iocb fcp_wqidx should already
8958 * be setup based on what work queue we used.
8959 */
8960 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
8961 piocb->fcp_wqidx =
8962 lpfc_sli4_scmd_to_wqidx_distr(phba,
8963 piocb->context1);
8964 ring_number = MAX_SLI3_CONFIGURED_RINGS +
8965 piocb->fcp_wqidx;
8966 } else {
8967 if (unlikely(!phba->sli4_hba.oas_wq))
8968 return LPFC_HBA_ERROR;
8969 piocb->fcp_wqidx = 0;
8970 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04008971 }
8972 }
8973 return ring_number;
8974}
8975
James Smarte59058c2008-08-24 21:49:00 -04008976/**
James Smart3621a712009-04-06 18:47:14 -04008977 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008978 * @phba: Pointer to HBA context object.
8979 * @pring: Pointer to driver SLI ring object.
8980 * @piocb: Pointer to command iocb.
8981 * @flag: Flag indicating if this command can be put into txq.
8982 *
8983 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8984 * function. This function gets the hbalock and calls
8985 * __lpfc_sli_issue_iocb function and will return the error returned
8986 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8987 * functions which do not hold hbalock.
8988 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008989int
James Smart3772a992009-05-22 14:50:54 -04008990lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008991 struct lpfc_iocbq *piocb, uint32_t flag)
8992{
James Smartba20c852012-08-03 12:36:52 -04008993 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008994 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008995 struct lpfc_queue *fpeq;
8996 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008997 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008998 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008999
James Smart7e56aa22012-08-03 12:35:34 -04009000 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009001 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9002 if (unlikely(ring_number == LPFC_HBA_ERROR))
9003 return IOCB_ERROR;
9004 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04009005
James Smart9bd2bff52014-09-03 12:57:30 -04009006 pring = &phba->sli.ring[ring_number];
9007 spin_lock_irqsave(&pring->ring_lock, iflags);
9008 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9009 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009010
James Smart9bd2bff52014-09-03 12:57:30 -04009011 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9012 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009013
James Smart9bd2bff52014-09-03 12:57:30 -04009014 if (atomic_dec_and_test(&fcp_eq_hdl->
9015 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009016
James Smart9bd2bff52014-09-03 12:57:30 -04009017 /* Get associated EQ with this index */
9018 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009019
James Smart9bd2bff52014-09-03 12:57:30 -04009020 /* Turn off interrupts from this EQ */
9021 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009022
James Smart9bd2bff52014-09-03 12:57:30 -04009023 /*
9024 * Process all the events on FCP EQ
9025 */
9026 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9027 lpfc_sli4_hba_handle_eqe(phba,
9028 eqe, idx);
9029 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009030 }
James Smartba20c852012-08-03 12:36:52 -04009031
James Smart9bd2bff52014-09-03 12:57:30 -04009032 /* Always clear and re-arm the EQ */
9033 lpfc_sli4_eq_release(fpeq,
9034 LPFC_QUEUE_REARM);
9035 }
9036 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009037 }
James Smart7e56aa22012-08-03 12:35:34 -04009038 } else {
9039 /* For now, SLI2/3 will still use hbalock */
9040 spin_lock_irqsave(&phba->hbalock, iflags);
9041 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9042 spin_unlock_irqrestore(&phba->hbalock, iflags);
9043 }
James Smart92d7f7b2007-06-17 19:56:38 -05009044 return rc;
9045}
9046
James Smarte59058c2008-08-24 21:49:00 -04009047/**
James Smart3621a712009-04-06 18:47:14 -04009048 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009049 * @phba: Pointer to HBA context object.
9050 *
9051 * This function is called while driver attaches with the
9052 * HBA to setup the extra ring. The extra ring is used
9053 * only when driver needs to support target mode functionality
9054 * or IP over FC functionalities.
9055 *
9056 * This function is called with no lock held.
9057 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009058static int
9059lpfc_extra_ring_setup( struct lpfc_hba *phba)
9060{
9061 struct lpfc_sli *psli;
9062 struct lpfc_sli_ring *pring;
9063
9064 psli = &phba->sli;
9065
9066 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009067
9068 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009069 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009070 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9071 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9072 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9073 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009074
James Smarta4bc3372006-12-02 13:34:16 -05009075 /* and give them to the extra ring */
9076 pring = &psli->ring[psli->extra_ring];
9077
James Smart7e56aa22012-08-03 12:35:34 -04009078 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9079 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9080 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9081 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009082
9083 /* Setup default profile for this ring */
9084 pring->iotag_max = 4096;
9085 pring->num_mask = 1;
9086 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009087 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9088 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009089 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9090 return 0;
9091}
9092
James Smartcb69f7d2011-12-13 13:21:57 -05009093/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9094 * @phba: Pointer to HBA context object.
9095 * @iocbq: Pointer to iocb object.
9096 *
9097 * The async_event handler calls this routine when it receives
9098 * an ASYNC_STATUS_CN event from the port. The port generates
9099 * this event when an Abort Sequence request to an rport fails
9100 * twice in succession. The abort could be originated by the
9101 * driver or by the port. The ABTS could have been for an ELS
9102 * or FCP IO. The port only generates this event when an ABTS
9103 * fails to complete after one retry.
9104 */
9105static void
9106lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9107 struct lpfc_iocbq *iocbq)
9108{
9109 struct lpfc_nodelist *ndlp = NULL;
9110 uint16_t rpi = 0, vpi = 0;
9111 struct lpfc_vport *vport = NULL;
9112
9113 /* The rpi in the ulpContext is vport-sensitive. */
9114 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9115 rpi = iocbq->iocb.ulpContext;
9116
9117 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9118 "3092 Port generated ABTS async event "
9119 "on vpi %d rpi %d status 0x%x\n",
9120 vpi, rpi, iocbq->iocb.ulpStatus);
9121
9122 vport = lpfc_find_vport_by_vpid(phba, vpi);
9123 if (!vport)
9124 goto err_exit;
9125 ndlp = lpfc_findnode_rpi(vport, rpi);
9126 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9127 goto err_exit;
9128
9129 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9130 lpfc_sli_abts_recover_port(vport, ndlp);
9131 return;
9132
9133 err_exit:
9134 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9135 "3095 Event Context not found, no "
9136 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9137 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9138 vpi, rpi);
9139}
9140
9141/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9142 * @phba: pointer to HBA context object.
9143 * @ndlp: nodelist pointer for the impacted rport.
9144 * @axri: pointer to the wcqe containing the failed exchange.
9145 *
9146 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9147 * port. The port generates this event when an abort exchange request to an
9148 * rport fails twice in succession with no reply. The abort could be originated
9149 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9150 */
9151void
9152lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9153 struct lpfc_nodelist *ndlp,
9154 struct sli4_wcqe_xri_aborted *axri)
9155{
9156 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009157 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009158
James Smart6b5151f2012-01-18 16:24:06 -05009159 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009160 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9161 "3115 Node Context not found, driver "
9162 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009163 return;
9164 }
9165
James Smartcb69f7d2011-12-13 13:21:57 -05009166 vport = ndlp->vport;
9167 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9168 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009169 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009170 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009171 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009172 bf_get(lpfc_wcqe_xa_status, axri),
9173 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009174
James Smart5c1db2a2012-03-01 22:34:36 -05009175 /*
9176 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9177 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9178 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9179 */
James Smarte3d2b802012-08-14 14:25:43 -04009180 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009181 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9182 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009183 lpfc_sli_abts_recover_port(vport, ndlp);
9184}
9185
James Smarte59058c2008-08-24 21:49:00 -04009186/**
James Smart3621a712009-04-06 18:47:14 -04009187 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009188 * @phba: Pointer to HBA context object.
9189 * @pring: Pointer to driver SLI ring object.
9190 * @iocbq: Pointer to iocb object.
9191 *
9192 * This function is called by the slow ring event handler
9193 * function when there is an ASYNC event iocb in the ring.
9194 * This function is called with no lock held.
9195 * Currently this function handles only temperature related
9196 * ASYNC events. The function decodes the temperature sensor
9197 * event message and posts events for the management applications.
9198 **/
James Smart98c9ea52007-10-27 13:37:33 -04009199static void
James Smart57127f12007-10-27 13:37:05 -04009200lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9201 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9202{
9203 IOCB_t *icmd;
9204 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009205 struct temp_event temp_event_data;
9206 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009207 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009208
9209 icmd = &iocbq->iocb;
9210 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009211
James Smartcb69f7d2011-12-13 13:21:57 -05009212 switch (evt_code) {
9213 case ASYNC_TEMP_WARN:
9214 case ASYNC_TEMP_SAFE:
9215 temp_event_data.data = (uint32_t) icmd->ulpContext;
9216 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9217 if (evt_code == ASYNC_TEMP_WARN) {
9218 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9219 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9220 "0347 Adapter is very hot, please take "
9221 "corrective action. temperature : %d Celsius\n",
9222 (uint32_t) icmd->ulpContext);
9223 } else {
9224 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9225 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9226 "0340 Adapter temperature is OK now. "
9227 "temperature : %d Celsius\n",
9228 (uint32_t) icmd->ulpContext);
9229 }
9230
9231 /* Send temperature change event to applications */
9232 shost = lpfc_shost_from_vport(phba->pport);
9233 fc_host_post_vendor_event(shost, fc_get_event_number(),
9234 sizeof(temp_event_data), (char *) &temp_event_data,
9235 LPFC_NL_VENDOR_ID);
9236 break;
9237 case ASYNC_STATUS_CN:
9238 lpfc_sli_abts_err_handler(phba, iocbq);
9239 break;
9240 default:
James Smarta257bf92009-04-06 18:48:10 -04009241 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009242 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009243 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009244 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009245 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9246 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9247 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9248 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009249 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009250 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9251 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9252 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9253 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9254
James Smartcb69f7d2011-12-13 13:21:57 -05009255 break;
James Smart57127f12007-10-27 13:37:05 -04009256 }
James Smart57127f12007-10-27 13:37:05 -04009257}
9258
9259
James Smarte59058c2008-08-24 21:49:00 -04009260/**
James Smart3621a712009-04-06 18:47:14 -04009261 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009262 * @phba: Pointer to HBA context object.
9263 *
9264 * lpfc_sli_setup sets up rings of the SLI interface with
9265 * number of iocbs per ring and iotags. This function is
9266 * called while driver attach to the HBA and before the
9267 * interrupts are enabled. So there is no need for locking.
9268 *
9269 * This function always returns 0.
9270 **/
dea31012005-04-17 16:05:31 -05009271int
9272lpfc_sli_setup(struct lpfc_hba *phba)
9273{
James Smarted957682007-06-17 19:56:37 -05009274 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009275 struct lpfc_sli *psli = &phba->sli;
9276 struct lpfc_sli_ring *pring;
9277
James Smart2a76a282012-08-03 12:35:54 -04009278 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9279 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009280 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009281 psli->sli_flag = 0;
9282 psli->fcp_ring = LPFC_FCP_RING;
9283 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009284 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009285
James Bottomley604a3e32005-10-29 10:28:33 -05009286 psli->iocbq_lookup = NULL;
9287 psli->iocbq_lookup_len = 0;
9288 psli->last_iotag = 0;
9289
dea31012005-04-17 16:05:31 -05009290 for (i = 0; i < psli->num_rings; i++) {
9291 pring = &psli->ring[i];
9292 switch (i) {
9293 case LPFC_FCP_RING: /* ring 0 - FCP */
9294 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009295 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9296 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9297 pring->sli.sli3.numCiocb +=
9298 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9299 pring->sli.sli3.numRiocb +=
9300 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9301 pring->sli.sli3.numCiocb +=
9302 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9303 pring->sli.sli3.numRiocb +=
9304 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9305 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009306 SLI3_IOCB_CMD_SIZE :
9307 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009308 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009309 SLI3_IOCB_RSP_SIZE :
9310 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009311 pring->iotag_ctr = 0;
9312 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009313 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009314 pring->fast_iotag = pring->iotag_max;
9315 pring->num_mask = 0;
9316 break;
James Smarta4bc3372006-12-02 13:34:16 -05009317 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009318 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009319 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9320 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9321 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009322 SLI3_IOCB_CMD_SIZE :
9323 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009324 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009325 SLI3_IOCB_RSP_SIZE :
9326 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009327 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009328 pring->num_mask = 0;
9329 break;
9330 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9331 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009332 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9333 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9334 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009335 SLI3_IOCB_CMD_SIZE :
9336 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009337 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009338 SLI3_IOCB_RSP_SIZE :
9339 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009340 pring->fast_iotag = 0;
9341 pring->iotag_ctr = 0;
9342 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009343 pring->lpfc_sli_rcv_async_status =
9344 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009345 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009346 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009347 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9348 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009349 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009350 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009351 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009352 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9353 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009354 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009355 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009356 pring->prt[2].profile = 0; /* Mask 2 */
9357 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009358 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009359 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009360 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009361 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009362 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009363 pring->prt[3].profile = 0; /* Mask 3 */
9364 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009365 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009366 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009367 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009368 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009369 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009370 break;
9371 }
James Smart7e56aa22012-08-03 12:35:34 -04009372 totiocbsize += (pring->sli.sli3.numCiocb *
9373 pring->sli.sli3.sizeCiocb) +
9374 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009375 }
James Smarted957682007-06-17 19:56:37 -05009376 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009377 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009378 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9379 "SLI2 SLIM Data: x%x x%lx\n",
9380 phba->brd_no, totiocbsize,
9381 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009382 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009383 if (phba->cfg_multi_ring_support == 2)
9384 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009385
9386 return 0;
9387}
9388
James Smarte59058c2008-08-24 21:49:00 -04009389/**
James Smart3621a712009-04-06 18:47:14 -04009390 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009391 * @phba: Pointer to HBA context object.
9392 *
9393 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9394 * ring. This function also initializes ring indices of each ring.
9395 * This function is called during the initialization of the SLI
9396 * interface of an HBA.
9397 * This function is called with no lock held and always returns
9398 * 1.
9399 **/
dea31012005-04-17 16:05:31 -05009400int
James Smart2e0fef82007-06-17 19:56:36 -05009401lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009402{
9403 struct lpfc_sli *psli;
9404 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009405 int i;
dea31012005-04-17 16:05:31 -05009406
9407 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009408 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009409 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009410 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009411 /* Initialize list headers for txq and txcmplq as double linked lists */
9412 for (i = 0; i < psli->num_rings; i++) {
9413 pring = &psli->ring[i];
9414 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009415 pring->sli.sli3.next_cmdidx = 0;
9416 pring->sli.sli3.local_getidx = 0;
9417 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009418 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009419 INIT_LIST_HEAD(&pring->txq);
9420 INIT_LIST_HEAD(&pring->txcmplq);
9421 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009422 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009423 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009424 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009425 }
James Smart2e0fef82007-06-17 19:56:36 -05009426 spin_unlock_irq(&phba->hbalock);
9427 return 1;
dea31012005-04-17 16:05:31 -05009428}
9429
James Smarte59058c2008-08-24 21:49:00 -04009430/**
James Smart04c68492009-05-22 14:52:52 -04009431 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9432 * @phba: Pointer to HBA context object.
9433 *
9434 * This routine flushes the mailbox command subsystem. It will unconditionally
9435 * flush all the mailbox commands in the three possible stages in the mailbox
9436 * command sub-system: pending mailbox command queue; the outstanding mailbox
9437 * command; and completed mailbox command queue. It is caller's responsibility
9438 * to make sure that the driver is in the proper state to flush the mailbox
9439 * command sub-system. Namely, the posting of mailbox commands into the
9440 * pending mailbox command queue from the various clients must be stopped;
9441 * either the HBA is in a state that it will never works on the outstanding
9442 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9443 * mailbox command has been completed.
9444 **/
9445static void
9446lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9447{
9448 LIST_HEAD(completions);
9449 struct lpfc_sli *psli = &phba->sli;
9450 LPFC_MBOXQ_t *pmb;
9451 unsigned long iflag;
9452
9453 /* Flush all the mailbox commands in the mbox system */
9454 spin_lock_irqsave(&phba->hbalock, iflag);
9455 /* The pending mailbox command queue */
9456 list_splice_init(&phba->sli.mboxq, &completions);
9457 /* The outstanding active mailbox command */
9458 if (psli->mbox_active) {
9459 list_add_tail(&psli->mbox_active->list, &completions);
9460 psli->mbox_active = NULL;
9461 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9462 }
9463 /* The completed mailbox command queue */
9464 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9465 spin_unlock_irqrestore(&phba->hbalock, iflag);
9466
9467 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9468 while (!list_empty(&completions)) {
9469 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9470 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9471 if (pmb->mbox_cmpl)
9472 pmb->mbox_cmpl(phba, pmb);
9473 }
9474}
9475
9476/**
James Smart3621a712009-04-06 18:47:14 -04009477 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009478 * @vport: Pointer to virtual port object.
9479 *
9480 * lpfc_sli_host_down is called to clean up the resources
9481 * associated with a vport before destroying virtual
9482 * port data structures.
9483 * This function does following operations:
9484 * - Free discovery resources associated with this virtual
9485 * port.
9486 * - Free iocbs associated with this virtual port in
9487 * the txq.
9488 * - Send abort for all iocb commands associated with this
9489 * vport in txcmplq.
9490 *
9491 * This function is called with no lock held and always returns 1.
9492 **/
dea31012005-04-17 16:05:31 -05009493int
James Smart92d7f7b2007-06-17 19:56:38 -05009494lpfc_sli_host_down(struct lpfc_vport *vport)
9495{
James Smart858c9f62007-06-17 19:56:39 -05009496 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009497 struct lpfc_hba *phba = vport->phba;
9498 struct lpfc_sli *psli = &phba->sli;
9499 struct lpfc_sli_ring *pring;
9500 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009501 int i;
9502 unsigned long flags = 0;
9503 uint16_t prev_pring_flag;
9504
9505 lpfc_cleanup_discovery_resources(vport);
9506
9507 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009508 for (i = 0; i < psli->num_rings; i++) {
9509 pring = &psli->ring[i];
9510 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009511 /* Only slow rings */
9512 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009513 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009514 /* Set the lpfc data pending flag */
9515 set_bit(LPFC_DATA_READY, &phba->data_flags);
9516 }
James Smart92d7f7b2007-06-17 19:56:38 -05009517 /*
9518 * Error everything on the txq since these iocbs have not been
9519 * given to the FW yet.
9520 */
James Smart92d7f7b2007-06-17 19:56:38 -05009521 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9522 if (iocb->vport != vport)
9523 continue;
James Smart858c9f62007-06-17 19:56:39 -05009524 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009525 }
9526
9527 /* Next issue ABTS for everything on the txcmplq */
9528 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9529 list) {
9530 if (iocb->vport != vport)
9531 continue;
9532 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9533 }
9534
9535 pring->flag = prev_pring_flag;
9536 }
9537
9538 spin_unlock_irqrestore(&phba->hbalock, flags);
9539
James Smarta257bf92009-04-06 18:48:10 -04009540 /* Cancel all the IOCBs from the completions list */
9541 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9542 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009543 return 1;
9544}
9545
James Smarte59058c2008-08-24 21:49:00 -04009546/**
James Smart3621a712009-04-06 18:47:14 -04009547 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009548 * @phba: Pointer to HBA context object.
9549 *
9550 * This function cleans up all iocb, buffers, mailbox commands
9551 * while shutting down the HBA. This function is called with no
9552 * lock held and always returns 1.
9553 * This function does the following to cleanup driver resources:
9554 * - Free discovery resources for each virtual port
9555 * - Cleanup any pending fabric iocbs
9556 * - Iterate through the iocb txq and free each entry
9557 * in the list.
9558 * - Free up any buffer posted to the HBA
9559 * - Free mailbox commands in the mailbox queue.
9560 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009561int
James Smart2e0fef82007-06-17 19:56:36 -05009562lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009563{
James Smart2534ba72007-04-25 09:52:20 -04009564 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009565 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009566 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009567 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009568 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009569 int i;
9570
9571 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009572 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009573
dea31012005-04-17 16:05:31 -05009574 lpfc_hba_down_prep(phba);
9575
James Smart92d7f7b2007-06-17 19:56:38 -05009576 lpfc_fabric_abort_hba(phba);
9577
James Smart2e0fef82007-06-17 19:56:36 -05009578 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009579 for (i = 0; i < psli->num_rings; i++) {
9580 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009581 /* Only slow rings */
9582 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009583 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009584 /* Set the lpfc data pending flag */
9585 set_bit(LPFC_DATA_READY, &phba->data_flags);
9586 }
dea31012005-04-17 16:05:31 -05009587
9588 /*
9589 * Error everything on the txq since these iocbs have not been
9590 * given to the FW yet.
9591 */
James Smart2534ba72007-04-25 09:52:20 -04009592 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009593 }
James Smart2e0fef82007-06-17 19:56:36 -05009594 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009595
James Smarta257bf92009-04-06 18:48:10 -04009596 /* Cancel all the IOCBs from the completions list */
9597 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9598 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009599
James Smart0ff10d42008-01-11 01:52:36 -05009600 spin_lock_irqsave(&phba->hbalock, flags);
9601 list_splice_init(&phba->elsbuf, &completions);
9602 phba->elsbuf_cnt = 0;
9603 phba->elsbuf_prev_cnt = 0;
9604 spin_unlock_irqrestore(&phba->hbalock, flags);
9605
9606 while (!list_empty(&completions)) {
9607 list_remove_head(&completions, buf_ptr,
9608 struct lpfc_dmabuf, list);
9609 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9610 kfree(buf_ptr);
9611 }
9612
dea31012005-04-17 16:05:31 -05009613 /* Return any active mbox cmds */
9614 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009615
James Smartda0436e2009-05-22 14:51:39 -04009616 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009617 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009618 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009619
James Smartda0436e2009-05-22 14:51:39 -04009620 return 1;
9621}
James Smart92d7f7b2007-06-17 19:56:38 -05009622
James Smartda0436e2009-05-22 14:51:39 -04009623/**
James Smart3621a712009-04-06 18:47:14 -04009624 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009625 * @srcp: Source memory pointer.
9626 * @destp: Destination memory pointer.
9627 * @cnt: Number of words required to be copied.
9628 *
9629 * This function is used for copying data between driver memory
9630 * and the SLI memory. This function also changes the endianness
9631 * of each word if native endianness is different from SLI
9632 * endianness. This function can be called with or without
9633 * lock.
9634 **/
dea31012005-04-17 16:05:31 -05009635void
9636lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9637{
9638 uint32_t *src = srcp;
9639 uint32_t *dest = destp;
9640 uint32_t ldata;
9641 int i;
9642
9643 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9644 ldata = *src;
9645 ldata = le32_to_cpu(ldata);
9646 *dest = ldata;
9647 src++;
9648 dest++;
9649 }
9650}
9651
James Smarte59058c2008-08-24 21:49:00 -04009652
9653/**
James Smarta0c87cb2009-07-19 10:01:10 -04009654 * lpfc_sli_bemem_bcopy - SLI memory copy function
9655 * @srcp: Source memory pointer.
9656 * @destp: Destination memory pointer.
9657 * @cnt: Number of words required to be copied.
9658 *
9659 * This function is used for copying data between a data structure
9660 * with big endian representation to local endianness.
9661 * This function can be called with or without lock.
9662 **/
9663void
9664lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9665{
9666 uint32_t *src = srcp;
9667 uint32_t *dest = destp;
9668 uint32_t ldata;
9669 int i;
9670
9671 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9672 ldata = *src;
9673 ldata = be32_to_cpu(ldata);
9674 *dest = ldata;
9675 src++;
9676 dest++;
9677 }
9678}
9679
9680/**
James Smart3621a712009-04-06 18:47:14 -04009681 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009682 * @phba: Pointer to HBA context object.
9683 * @pring: Pointer to driver SLI ring object.
9684 * @mp: Pointer to driver buffer object.
9685 *
9686 * This function is called with no lock held.
9687 * It always return zero after adding the buffer to the postbufq
9688 * buffer list.
9689 **/
dea31012005-04-17 16:05:31 -05009690int
James Smart2e0fef82007-06-17 19:56:36 -05009691lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9692 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009693{
9694 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9695 later */
James Smart2e0fef82007-06-17 19:56:36 -05009696 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009697 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009698 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009699 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009700 return 0;
9701}
9702
James Smarte59058c2008-08-24 21:49:00 -04009703/**
James Smart3621a712009-04-06 18:47:14 -04009704 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009705 * @phba: Pointer to HBA context object.
9706 *
9707 * When HBQ is enabled, buffers are searched based on tags. This function
9708 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9709 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9710 * does not conflict with tags of buffer posted for unsolicited events.
9711 * The function returns the allocated tag. The function is called with
9712 * no locks held.
9713 **/
James Smart76bb24e2007-10-27 13:38:00 -04009714uint32_t
9715lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9716{
9717 spin_lock_irq(&phba->hbalock);
9718 phba->buffer_tag_count++;
9719 /*
9720 * Always set the QUE_BUFTAG_BIT to distiguish between
9721 * a tag assigned by HBQ.
9722 */
9723 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9724 spin_unlock_irq(&phba->hbalock);
9725 return phba->buffer_tag_count;
9726}
9727
James Smarte59058c2008-08-24 21:49:00 -04009728/**
James Smart3621a712009-04-06 18:47:14 -04009729 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009730 * @phba: Pointer to HBA context object.
9731 * @pring: Pointer to driver SLI ring object.
9732 * @tag: Buffer tag.
9733 *
9734 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9735 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9736 * iocb is posted to the response ring with the tag of the buffer.
9737 * This function searches the pring->postbufq list using the tag
9738 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9739 * iocb. If the buffer is found then lpfc_dmabuf object of the
9740 * buffer is returned to the caller else NULL is returned.
9741 * This function is called with no lock held.
9742 **/
James Smart76bb24e2007-10-27 13:38:00 -04009743struct lpfc_dmabuf *
9744lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9745 uint32_t tag)
9746{
9747 struct lpfc_dmabuf *mp, *next_mp;
9748 struct list_head *slp = &pring->postbufq;
9749
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009750 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009751 spin_lock_irq(&phba->hbalock);
9752 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9753 if (mp->buffer_tag == tag) {
9754 list_del_init(&mp->list);
9755 pring->postbufq_cnt--;
9756 spin_unlock_irq(&phba->hbalock);
9757 return mp;
9758 }
9759 }
9760
9761 spin_unlock_irq(&phba->hbalock);
9762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009763 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009764 "ring %d Data x%lx x%p x%p x%x\n",
9765 pring->ringno, (unsigned long) tag,
9766 slp->next, slp->prev, pring->postbufq_cnt);
9767
9768 return NULL;
9769}
dea31012005-04-17 16:05:31 -05009770
James Smarte59058c2008-08-24 21:49:00 -04009771/**
James Smart3621a712009-04-06 18:47:14 -04009772 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009773 * @phba: Pointer to HBA context object.
9774 * @pring: Pointer to driver SLI ring object.
9775 * @phys: DMA address of the buffer.
9776 *
9777 * This function searches the buffer list using the dma_address
9778 * of unsolicited event to find the driver's lpfc_dmabuf object
9779 * corresponding to the dma_address. The function returns the
9780 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9781 * This function is called by the ct and els unsolicited event
9782 * handlers to get the buffer associated with the unsolicited
9783 * event.
9784 *
9785 * This function is called with no lock held.
9786 **/
dea31012005-04-17 16:05:31 -05009787struct lpfc_dmabuf *
9788lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9789 dma_addr_t phys)
9790{
9791 struct lpfc_dmabuf *mp, *next_mp;
9792 struct list_head *slp = &pring->postbufq;
9793
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009794 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009795 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009796 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9797 if (mp->phys == phys) {
9798 list_del_init(&mp->list);
9799 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009800 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009801 return mp;
9802 }
9803 }
9804
James Smart2e0fef82007-06-17 19:56:36 -05009805 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009807 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009808 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009809 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009810 slp->next, slp->prev, pring->postbufq_cnt);
9811 return NULL;
9812}
9813
James Smarte59058c2008-08-24 21:49:00 -04009814/**
James Smart3621a712009-04-06 18:47:14 -04009815 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009816 * @phba: Pointer to HBA context object.
9817 * @cmdiocb: Pointer to driver command iocb object.
9818 * @rspiocb: Pointer to driver response iocb object.
9819 *
9820 * This function is the completion handler for the abort iocbs for
9821 * ELS commands. This function is called from the ELS ring event
9822 * handler with no lock held. This function frees memory resources
9823 * associated with the abort iocb.
9824 **/
dea31012005-04-17 16:05:31 -05009825static void
James Smart2e0fef82007-06-17 19:56:36 -05009826lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9827 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009828{
James Smart2e0fef82007-06-17 19:56:36 -05009829 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009830 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009831 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009832
9833 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009834
9835 /*
9836 * Assume that the port already completed and returned, or
9837 * will return the iocb. Just Log the message.
9838 */
James Smart2680eea2007-04-25 09:52:55 -04009839 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9840 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9841
James Smart2e0fef82007-06-17 19:56:36 -05009842 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009843 if (phba->sli_rev < LPFC_SLI_REV4) {
9844 if (abort_iotag != 0 &&
9845 abort_iotag <= phba->sli.last_iotag)
9846 abort_iocb =
9847 phba->sli.iocbq_lookup[abort_iotag];
9848 } else
9849 /* For sli4 the abort_tag is the XRI,
9850 * so the abort routine puts the iotag of the iocb
9851 * being aborted in the context field of the abort
9852 * IOCB.
9853 */
9854 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009855
James Smart2a9bf3d2010-06-07 15:24:45 -04009856 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9857 "0327 Cannot abort els iocb %p "
9858 "with tag %x context %x, abort status %x, "
9859 "abort code %x\n",
9860 abort_iocb, abort_iotag, abort_context,
9861 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009862
James Smartff78d8f2011-12-13 13:21:35 -05009863 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009864 }
James Bottomley604a3e32005-10-29 10:28:33 -05009865 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009866 return;
9867}
9868
James Smarte59058c2008-08-24 21:49:00 -04009869/**
James Smart3621a712009-04-06 18:47:14 -04009870 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009871 * @phba: Pointer to HBA context object.
9872 * @cmdiocb: Pointer to driver command iocb object.
9873 * @rspiocb: Pointer to driver response iocb object.
9874 *
9875 * The function is called from SLI ring event handler with no
9876 * lock held. This function is the completion handler for ELS commands
9877 * which are aborted. The function frees memory resources used for
9878 * the aborted ELS commands.
9879 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009880static void
9881lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9882 struct lpfc_iocbq *rspiocb)
9883{
9884 IOCB_t *irsp = &rspiocb->iocb;
9885
9886 /* ELS cmd tag <ulpIoTag> completes */
9887 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009888 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009889 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009890 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009891 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009892 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9893 lpfc_ct_free_iocb(phba, cmdiocb);
9894 else
9895 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009896 return;
9897}
9898
James Smarte59058c2008-08-24 21:49:00 -04009899/**
James Smart5af5eee2010-10-22 11:06:38 -04009900 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009901 * @phba: Pointer to HBA context object.
9902 * @pring: Pointer to driver SLI ring object.
9903 * @cmdiocb: Pointer to driver command iocb object.
9904 *
James Smart5af5eee2010-10-22 11:06:38 -04009905 * This function issues an abort iocb for the provided command iocb down to
9906 * the port. Other than the case the outstanding command iocb is an abort
9907 * request, this function issues abort out unconditionally. This function is
9908 * called with hbalock held. The function returns 0 when it fails due to
9909 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009910 **/
James Smart5af5eee2010-10-22 11:06:38 -04009911static int
9912lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009913 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009914{
James Smart2e0fef82007-06-17 19:56:36 -05009915 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009916 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009917 IOCB_t *icmd = NULL;
9918 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009919 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009920 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009921 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009922
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009923 lockdep_assert_held(&phba->hbalock);
9924
James Smart92d7f7b2007-06-17 19:56:38 -05009925 /*
9926 * There are certain command types we don't want to abort. And we
9927 * don't want to abort commands that are already in the process of
9928 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009929 */
9930 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009931 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009932 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9933 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009934 return 0;
9935
dea31012005-04-17 16:05:31 -05009936 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009937 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009938 if (abtsiocbp == NULL)
9939 return 0;
dea31012005-04-17 16:05:31 -05009940
James Smart07951072007-04-25 09:51:38 -04009941 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009942 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009943 */
9944 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9945
dea31012005-04-17 16:05:31 -05009946 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009947 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9948 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009949 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009950 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009951 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9952 }
James Smartda0436e2009-05-22 14:51:39 -04009953 else
9954 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009955 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009956 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009957
James Smart5ffc2662009-11-18 15:39:44 -05009958 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9959 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009960 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9961 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04009962 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
9963 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05009964
James Smart2e0fef82007-06-17 19:56:36 -05009965 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009966 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9967 else
9968 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9969
9970 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009971
James Smarte8b62012007-08-02 11:10:09 -04009972 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9973 "0339 Abort xri x%x, original iotag x%x, "
9974 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009975 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009976 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009977 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009978
9979 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009980 ring_number =
9981 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
9982 if (unlikely(ring_number == LPFC_HBA_ERROR))
9983 return 0;
9984 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -04009985 /* Note: both hbalock and ring_lock need to be set here */
9986 spin_lock_irqsave(&pring->ring_lock, iflags);
9987 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9988 abtsiocbp, 0);
9989 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9990 } else {
9991 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9992 abtsiocbp, 0);
9993 }
James Smart07951072007-04-25 09:51:38 -04009994
James Smartd7c255b2008-08-24 21:50:00 -04009995 if (retval)
9996 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009997
9998 /*
9999 * Caller to this routine should check for IOCB_ERROR
10000 * and handle it properly. This routine no longer removes
10001 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10002 */
10003 return retval;
10004}
10005
10006/**
10007 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10008 * @phba: Pointer to HBA context object.
10009 * @pring: Pointer to driver SLI ring object.
10010 * @cmdiocb: Pointer to driver command iocb object.
10011 *
10012 * This function issues an abort iocb for the provided command iocb. In case
10013 * of unloading, the abort iocb will not be issued to commands on the ELS
10014 * ring. Instead, the callback function shall be changed to those commands
10015 * so that nothing happens when them finishes. This function is called with
10016 * hbalock held. The function returns 0 when the command iocb is an abort
10017 * request.
10018 **/
10019int
10020lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10021 struct lpfc_iocbq *cmdiocb)
10022{
10023 struct lpfc_vport *vport = cmdiocb->vport;
10024 int retval = IOCB_ERROR;
10025 IOCB_t *icmd = NULL;
10026
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010027 lockdep_assert_held(&phba->hbalock);
10028
James Smart5af5eee2010-10-22 11:06:38 -040010029 /*
10030 * There are certain command types we don't want to abort. And we
10031 * don't want to abort commands that are already in the process of
10032 * being aborted.
10033 */
10034 icmd = &cmdiocb->iocb;
10035 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10036 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10037 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10038 return 0;
10039
10040 /*
10041 * If we're unloading, don't abort iocb on the ELS ring, but change
10042 * the callback so that nothing happens when it finishes.
10043 */
10044 if ((vport->load_flag & FC_UNLOADING) &&
10045 (pring->ringno == LPFC_ELS_RING)) {
10046 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10047 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10048 else
10049 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10050 goto abort_iotag_exit;
10051 }
10052
10053 /* Now, we try to issue the abort to the cmdiocb out */
10054 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10055
James Smart07951072007-04-25 09:51:38 -040010056abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010057 /*
10058 * Caller to this routine should check for IOCB_ERROR
10059 * and handle it properly. This routine no longer removes
10060 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010061 */
James Smart2e0fef82007-06-17 19:56:36 -050010062 return retval;
dea31012005-04-17 16:05:31 -050010063}
10064
James Smarte59058c2008-08-24 21:49:00 -040010065/**
James Smart5af5eee2010-10-22 11:06:38 -040010066 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10067 * @phba: pointer to lpfc HBA data structure.
10068 *
10069 * This routine will abort all pending and outstanding iocbs to an HBA.
10070 **/
10071void
10072lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10073{
10074 struct lpfc_sli *psli = &phba->sli;
10075 struct lpfc_sli_ring *pring;
10076 int i;
10077
10078 for (i = 0; i < psli->num_rings; i++) {
10079 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010080 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010081 }
10082}
10083
10084/**
James Smart3621a712009-04-06 18:47:14 -040010085 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010086 * @iocbq: Pointer to driver iocb object.
10087 * @vport: Pointer to driver virtual port object.
10088 * @tgt_id: SCSI ID of the target.
10089 * @lun_id: LUN ID of the scsi device.
10090 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10091 *
James Smart3621a712009-04-06 18:47:14 -040010092 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010093 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10094 * 0 if the filtering criteria is met for the given iocb and will return
10095 * 1 if the filtering criteria is not met.
10096 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10097 * given iocb is for the SCSI device specified by vport, tgt_id and
10098 * lun_id parameter.
10099 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10100 * given iocb is for the SCSI target specified by vport and tgt_id
10101 * parameters.
10102 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10103 * given iocb is for the SCSI host associated with the given vport.
10104 * This function is called with no locks held.
10105 **/
dea31012005-04-17 16:05:31 -050010106static int
James Smart51ef4c22007-08-02 11:10:31 -040010107lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10108 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010109 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010110{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010111 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010112 int rc = 1;
10113
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010114 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10115 return rc;
10116
James Smart51ef4c22007-08-02 11:10:31 -040010117 if (iocbq->vport != vport)
10118 return rc;
10119
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010120 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010121
James Smart495a7142008-06-14 22:52:59 -040010122 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010123 return rc;
10124
10125 switch (ctx_cmd) {
10126 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010127 if ((lpfc_cmd->rdata->pnode) &&
10128 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10129 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010130 rc = 0;
10131 break;
10132 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010133 if ((lpfc_cmd->rdata->pnode) &&
10134 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010135 rc = 0;
10136 break;
dea31012005-04-17 16:05:31 -050010137 case LPFC_CTX_HOST:
10138 rc = 0;
10139 break;
10140 default:
10141 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010142 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010143 break;
10144 }
10145
10146 return rc;
10147}
10148
James Smarte59058c2008-08-24 21:49:00 -040010149/**
James Smart3621a712009-04-06 18:47:14 -040010150 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010151 * @vport: Pointer to virtual port.
10152 * @tgt_id: SCSI ID of the target.
10153 * @lun_id: LUN ID of the scsi device.
10154 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10155 *
10156 * This function returns number of FCP commands pending for the vport.
10157 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10158 * commands pending on the vport associated with SCSI device specified
10159 * by tgt_id and lun_id parameters.
10160 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10161 * commands pending on the vport associated with SCSI target specified
10162 * by tgt_id parameter.
10163 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10164 * commands pending on the vport.
10165 * This function returns the number of iocbs which satisfy the filter.
10166 * This function is called without any lock held.
10167 **/
dea31012005-04-17 16:05:31 -050010168int
James Smart51ef4c22007-08-02 11:10:31 -040010169lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10170 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010171{
James Smart51ef4c22007-08-02 11:10:31 -040010172 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010173 struct lpfc_iocbq *iocbq;
10174 int sum, i;
dea31012005-04-17 16:05:31 -050010175
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010176 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10177 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010178
James Smart51ef4c22007-08-02 11:10:31 -040010179 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10180 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010181 sum++;
dea31012005-04-17 16:05:31 -050010182 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010183
dea31012005-04-17 16:05:31 -050010184 return sum;
10185}
10186
James Smarte59058c2008-08-24 21:49:00 -040010187/**
James Smart3621a712009-04-06 18:47:14 -040010188 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010189 * @phba: Pointer to HBA context object
10190 * @cmdiocb: Pointer to command iocb object.
10191 * @rspiocb: Pointer to response iocb object.
10192 *
10193 * This function is called when an aborted FCP iocb completes. This
10194 * function is called by the ring event handler with no lock held.
10195 * This function frees the iocb.
10196 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010197void
James Smart2e0fef82007-06-17 19:56:36 -050010198lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10199 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010200{
James Smartcb69f7d2011-12-13 13:21:57 -050010201 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010202 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010203 "original iotag x%x, abort cmd iotag x%x "
10204 "status 0x%x, reason 0x%x\n",
10205 cmdiocb->iocb.un.acxri.abortContextTag,
10206 cmdiocb->iocb.un.acxri.abortIoTag,
10207 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10208 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010209 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010210 return;
10211}
10212
James Smarte59058c2008-08-24 21:49:00 -040010213/**
James Smart3621a712009-04-06 18:47:14 -040010214 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010215 * @vport: Pointer to virtual port.
10216 * @pring: Pointer to driver SLI ring object.
10217 * @tgt_id: SCSI ID of the target.
10218 * @lun_id: LUN ID of the scsi device.
10219 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10220 *
10221 * This function sends an abort command for every SCSI command
10222 * associated with the given virtual port pending on the ring
10223 * filtered by lpfc_sli_validate_fcp_iocb function.
10224 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10225 * FCP iocbs associated with lun specified by tgt_id and lun_id
10226 * parameters
10227 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10228 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10229 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10230 * FCP iocbs associated with virtual port.
10231 * This function returns number of iocbs it failed to abort.
10232 * This function is called with no locks held.
10233 **/
dea31012005-04-17 16:05:31 -050010234int
James Smart51ef4c22007-08-02 11:10:31 -040010235lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10236 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010237{
James Smart51ef4c22007-08-02 11:10:31 -040010238 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010239 struct lpfc_iocbq *iocbq;
10240 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010241 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010242 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010243 int i;
dea31012005-04-17 16:05:31 -050010244
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010245 for (i = 1; i <= phba->sli.last_iotag; i++) {
10246 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010247
James Smart51ef4c22007-08-02 11:10:31 -040010248 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010249 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010250 continue;
10251
James Smartafbd8d82013-09-06 12:22:13 -040010252 /*
10253 * If the iocbq is already being aborted, don't take a second
10254 * action, but do count it.
10255 */
10256 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10257 continue;
10258
dea31012005-04-17 16:05:31 -050010259 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010260 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010261 if (abtsiocb == NULL) {
10262 errcnt++;
10263 continue;
10264 }
dea31012005-04-17 16:05:31 -050010265
James Smartafbd8d82013-09-06 12:22:13 -040010266 /* indicate the IO is being aborted by the driver. */
10267 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10268
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010269 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010270 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10271 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010272 if (phba->sli_rev == LPFC_SLI_REV4)
10273 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10274 else
10275 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010276 abtsiocb->iocb.ulpLe = 1;
10277 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010278 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010279
James Smart5ffc2662009-11-18 15:39:44 -050010280 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10281 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010282 if (iocbq->iocb_flag & LPFC_IO_FCP)
10283 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010284 if (iocbq->iocb_flag & LPFC_IO_FOF)
10285 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010286
James Smart2e0fef82007-06-17 19:56:36 -050010287 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010288 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10289 else
10290 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10291
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010292 /* Setup callback routine and issue the command. */
10293 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010294 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10295 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010296 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010297 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010298 errcnt++;
10299 continue;
10300 }
10301 }
10302
10303 return errcnt;
10304}
10305
James Smarte59058c2008-08-24 21:49:00 -040010306/**
James Smart98912dda2014-04-04 13:52:31 -040010307 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10308 * @vport: Pointer to virtual port.
10309 * @pring: Pointer to driver SLI ring object.
10310 * @tgt_id: SCSI ID of the target.
10311 * @lun_id: LUN ID of the scsi device.
10312 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10313 *
10314 * This function sends an abort command for every SCSI command
10315 * associated with the given virtual port pending on the ring
10316 * filtered by lpfc_sli_validate_fcp_iocb function.
10317 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10318 * FCP iocbs associated with lun specified by tgt_id and lun_id
10319 * parameters
10320 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10321 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10322 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10323 * FCP iocbs associated with virtual port.
10324 * This function returns number of iocbs it aborted .
10325 * This function is called with no locks held right after a taskmgmt
10326 * command is sent.
10327 **/
10328int
10329lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10330 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10331{
10332 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010333 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010334 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010335 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010336 struct lpfc_iocbq *iocbq;
10337 IOCB_t *icmd;
10338 int sum, i, ret_val;
10339 unsigned long iflags;
10340 struct lpfc_sli_ring *pring_s4;
10341 uint32_t ring_number;
10342
10343 spin_lock_irq(&phba->hbalock);
10344
10345 /* all I/Os are in process of being flushed */
10346 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10347 spin_unlock_irq(&phba->hbalock);
10348 return 0;
10349 }
10350 sum = 0;
10351
10352 for (i = 1; i <= phba->sli.last_iotag; i++) {
10353 iocbq = phba->sli.iocbq_lookup[i];
10354
10355 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10356 cmd) != 0)
10357 continue;
10358
10359 /*
10360 * If the iocbq is already being aborted, don't take a second
10361 * action, but do count it.
10362 */
10363 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10364 continue;
10365
10366 /* issue ABTS for this IOCB based on iotag */
10367 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10368 if (abtsiocbq == NULL)
10369 continue;
10370
10371 icmd = &iocbq->iocb;
10372 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10373 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10374 if (phba->sli_rev == LPFC_SLI_REV4)
10375 abtsiocbq->iocb.un.acxri.abortIoTag =
10376 iocbq->sli4_xritag;
10377 else
10378 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10379 abtsiocbq->iocb.ulpLe = 1;
10380 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10381 abtsiocbq->vport = vport;
10382
10383 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10384 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10385 if (iocbq->iocb_flag & LPFC_IO_FCP)
10386 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010387 if (iocbq->iocb_flag & LPFC_IO_FOF)
10388 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010389
James Smart8c50d252014-09-03 12:58:16 -040010390 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10391 ndlp = lpfc_cmd->rdata->pnode;
10392
10393 if (lpfc_is_link_up(phba) &&
10394 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010395 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10396 else
10397 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10398
10399 /* Setup callback routine and issue the command. */
10400 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10401
10402 /*
10403 * Indicate the IO is being aborted by the driver and set
10404 * the caller's flag into the aborted IO.
10405 */
10406 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10407
10408 if (phba->sli_rev == LPFC_SLI_REV4) {
10409 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10410 iocbq->fcp_wqidx;
10411 pring_s4 = &phba->sli.ring[ring_number];
10412 /* Note: both hbalock and ring_lock must be set here */
10413 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10414 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10415 abtsiocbq, 0);
10416 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10417 } else {
10418 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10419 abtsiocbq, 0);
10420 }
10421
10422
10423 if (ret_val == IOCB_ERROR)
10424 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10425 else
10426 sum++;
10427 }
10428 spin_unlock_irq(&phba->hbalock);
10429 return sum;
10430}
10431
10432/**
James Smart3621a712009-04-06 18:47:14 -040010433 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010434 * @phba: Pointer to HBA context object.
10435 * @cmdiocbq: Pointer to command iocb.
10436 * @rspiocbq: Pointer to response iocb.
10437 *
10438 * This function is the completion handler for iocbs issued using
10439 * lpfc_sli_issue_iocb_wait function. This function is called by the
10440 * ring event handler function without any lock held. This function
10441 * can be called from both worker thread context and interrupt
10442 * context. This function also can be called from other thread which
10443 * cleans up the SLI layer objects.
10444 * This function copy the contents of the response iocb to the
10445 * response iocb memory object provided by the caller of
10446 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10447 * sleeps for the iocb completion.
10448 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010449static void
10450lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10451 struct lpfc_iocbq *cmdiocbq,
10452 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010453{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010454 wait_queue_head_t *pdone_q;
10455 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010456 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010457
James Smart2e0fef82007-06-17 19:56:36 -050010458 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010459 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10460
10461 /*
10462 * A time out has occurred for the iocb. If a time out
10463 * completion handler has been supplied, call it. Otherwise,
10464 * just free the iocbq.
10465 */
10466
10467 spin_unlock_irqrestore(&phba->hbalock, iflags);
10468 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10469 cmdiocbq->wait_iocb_cmpl = NULL;
10470 if (cmdiocbq->iocb_cmpl)
10471 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10472 else
10473 lpfc_sli_release_iocbq(phba, cmdiocbq);
10474 return;
10475 }
10476
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010477 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10478 if (cmdiocbq->context2 && rspiocbq)
10479 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10480 &rspiocbq->iocb, sizeof(IOCB_t));
10481
James Smart0f65ff62010-02-26 14:14:23 -050010482 /* Set the exchange busy flag for task management commands */
10483 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10484 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10485 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10486 cur_iocbq);
10487 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10488 }
10489
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010490 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010491 if (pdone_q)
10492 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010493 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010494 return;
10495}
10496
James Smarte59058c2008-08-24 21:49:00 -040010497/**
James Smartd11e31d2009-06-10 17:23:06 -040010498 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10499 * @phba: Pointer to HBA context object..
10500 * @piocbq: Pointer to command iocb.
10501 * @flag: Flag to test.
10502 *
10503 * This routine grabs the hbalock and then test the iocb_flag to
10504 * see if the passed in flag is set.
10505 * Returns:
10506 * 1 if flag is set.
10507 * 0 if flag is not set.
10508 **/
10509static int
10510lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10511 struct lpfc_iocbq *piocbq, uint32_t flag)
10512{
10513 unsigned long iflags;
10514 int ret;
10515
10516 spin_lock_irqsave(&phba->hbalock, iflags);
10517 ret = piocbq->iocb_flag & flag;
10518 spin_unlock_irqrestore(&phba->hbalock, iflags);
10519 return ret;
10520
10521}
10522
10523/**
James Smart3621a712009-04-06 18:47:14 -040010524 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010525 * @phba: Pointer to HBA context object..
10526 * @pring: Pointer to sli ring.
10527 * @piocb: Pointer to command iocb.
10528 * @prspiocbq: Pointer to response iocb.
10529 * @timeout: Timeout in number of seconds.
10530 *
10531 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010532 * iocb to complete. The iocb_cmpl field of the shall be used
10533 * to handle iocbs which time out. If the field is NULL, the
10534 * function shall free the iocbq structure. If more clean up is
10535 * needed, the caller is expected to provide a completion function
10536 * that will provide the needed clean up. If the iocb command is
10537 * not completed within timeout seconds, the function will either
10538 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10539 * completion function set in the iocb_cmpl field and then return
10540 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10541 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010542 * The function waits for the iocb completion using an
10543 * non-interruptible wait.
10544 * This function will sleep while waiting for iocb completion.
10545 * So, this function should not be called from any context which
10546 * does not allow sleeping. Due to the same reason, this function
10547 * cannot be called with interrupt disabled.
10548 * This function assumes that the iocb completions occur while
10549 * this function sleep. So, this function cannot be called from
10550 * the thread which process iocb completion for this ring.
10551 * This function clears the iocb_flag of the iocb object before
10552 * issuing the iocb and the iocb completion handler sets this
10553 * flag and wakes this thread when the iocb completes.
10554 * The contents of the response iocb will be copied to prspiocbq
10555 * by the completion handler when the command completes.
10556 * This function returns IOCB_SUCCESS when success.
10557 * This function is called with no lock held.
10558 **/
dea31012005-04-17 16:05:31 -050010559int
James Smart2e0fef82007-06-17 19:56:36 -050010560lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010561 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010562 struct lpfc_iocbq *piocb,
10563 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010564 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010565{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010566 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010567 long timeleft, timeout_req = 0;
10568 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010569 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010570 struct lpfc_iocbq *iocb;
10571 int txq_cnt = 0;
10572 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010573 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010574 unsigned long iflags;
10575 bool iocb_completed = true;
10576
dea31012005-04-17 16:05:31 -050010577 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010578 * If the caller has provided a response iocbq buffer, then context2
10579 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010580 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010581 if (prspiocbq) {
10582 if (piocb->context2)
10583 return IOCB_ERROR;
10584 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010585 }
10586
James Smart5a0916b2013-07-15 18:31:42 -040010587 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010588 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10589 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010590 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010591
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010592 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010593 if (lpfc_readl(phba->HCregaddr, &creg_val))
10594 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010595 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10596 writel(creg_val, phba->HCregaddr);
10597 readl(phba->HCregaddr); /* flush */
10598 }
10599
James Smart2a9bf3d2010-06-07 15:24:45 -040010600 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10601 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010602 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010603 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010604 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010605 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010606 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010607 spin_lock_irqsave(&phba->hbalock, iflags);
10608 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010609
James Smart5a0916b2013-07-15 18:31:42 -040010610 /*
10611 * IOCB timed out. Inform the wake iocb wait
10612 * completion function and set local status
10613 */
10614
10615 iocb_completed = false;
10616 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10617 }
10618 spin_unlock_irqrestore(&phba->hbalock, iflags);
10619 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010620 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010621 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010622 /* Note: we are not indicating if the IOCB has a success
10623 * status or not - that's for the caller to check.
10624 * IOCB_SUCCESS means just that the command was sent and
10625 * completed. Not that it completed successfully.
10626 * */
James Smart7054a602007-04-25 09:52:34 -040010627 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010628 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010629 "0338 IOCB wait timeout error - no "
10630 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010631 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010632 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010634 "0330 IOCB wake NOT set, "
10635 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010636 timeout, (timeleft / jiffies));
10637 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010638 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010639 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010640 if (phba->cfg_log_verbose & LOG_SLI) {
10641 list_for_each_entry(iocb, &pring->txq, list) {
10642 txq_cnt++;
10643 }
10644 list_for_each_entry(iocb, &pring->txcmplq, list) {
10645 txcmplq_cnt++;
10646 }
10647 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10648 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10649 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10650 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010651 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010652 } else {
10653 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010654 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010655 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010656 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010657 }
10658
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010659 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010660 if (lpfc_readl(phba->HCregaddr, &creg_val))
10661 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010662 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10663 writel(creg_val, phba->HCregaddr);
10664 readl(phba->HCregaddr); /* flush */
10665 }
10666
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010667 if (prspiocbq)
10668 piocb->context2 = NULL;
10669
10670 piocb->context_un.wait_queue = NULL;
10671 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010672 return retval;
10673}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010674
James Smarte59058c2008-08-24 21:49:00 -040010675/**
James Smart3621a712009-04-06 18:47:14 -040010676 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010677 * @phba: Pointer to HBA context object.
10678 * @pmboxq: Pointer to driver mailbox object.
10679 * @timeout: Timeout in number of seconds.
10680 *
10681 * This function issues the mailbox to firmware and waits for the
10682 * mailbox command to complete. If the mailbox command is not
10683 * completed within timeout seconds, it returns MBX_TIMEOUT.
10684 * The function waits for the mailbox completion using an
10685 * interruptible wait. If the thread is woken up due to a
10686 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10687 * should not free the mailbox resources, if this function returns
10688 * MBX_TIMEOUT.
10689 * This function will sleep while waiting for mailbox completion.
10690 * So, this function should not be called from any context which
10691 * does not allow sleeping. Due to the same reason, this function
10692 * cannot be called with interrupt disabled.
10693 * This function assumes that the mailbox completion occurs while
10694 * this function sleep. So, this function cannot be called from
10695 * the worker thread which processes mailbox completion.
10696 * This function is called in the context of HBA management
10697 * applications.
10698 * This function returns MBX_SUCCESS when successful.
10699 * This function is called with no lock held.
10700 **/
dea31012005-04-17 16:05:31 -050010701int
James Smart2e0fef82007-06-17 19:56:36 -050010702lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010703 uint32_t timeout)
10704{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010705 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010706 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010707 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010708 unsigned long flag;
dea31012005-04-17 16:05:31 -050010709
James Smartb230b8a2013-05-31 17:05:27 -040010710 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010711 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010712 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010713
James Smart495a7142008-06-14 22:52:59 -040010714 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010715 /* setup wake call as IOCB callback */
10716 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10717 /* setup context field to pass wait_queue pointer to wake function */
10718 pmboxq->context1 = &done_q;
10719
dea31012005-04-17 16:05:31 -050010720 /* now issue the command */
10721 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010722 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010723 wait_event_interruptible_timeout(done_q,
10724 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010725 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010726
James Smart858c9f62007-06-17 19:56:39 -050010727 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010728 /* restore the possible extended buffer for free resource */
10729 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010730 /*
10731 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10732 * else do not free the resources.
10733 */
James Smartd7c47992010-06-08 18:31:54 -040010734 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010735 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010736 } else {
James Smart7054a602007-04-25 09:52:34 -040010737 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010738 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10739 }
10740 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010741 } else {
10742 /* restore the possible extended buffer for free resource */
10743 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010744 }
10745
dea31012005-04-17 16:05:31 -050010746 return retval;
10747}
10748
James Smarte59058c2008-08-24 21:49:00 -040010749/**
James Smart3772a992009-05-22 14:50:54 -040010750 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010751 * @phba: Pointer to HBA context.
10752 *
James Smart3772a992009-05-22 14:50:54 -040010753 * This function is called to shutdown the driver's mailbox sub-system.
10754 * It first marks the mailbox sub-system is in a block state to prevent
10755 * the asynchronous mailbox command from issued off the pending mailbox
10756 * command queue. If the mailbox command sub-system shutdown is due to
10757 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10758 * the mailbox sub-system flush routine to forcefully bring down the
10759 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10760 * as with offline or HBA function reset), this routine will wait for the
10761 * outstanding mailbox command to complete before invoking the mailbox
10762 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010763 **/
James Smart3772a992009-05-22 14:50:54 -040010764void
James Smart618a5232012-06-12 13:54:36 -040010765lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010766{
James Smart3772a992009-05-22 14:50:54 -040010767 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010768 unsigned long timeout;
10769
James Smart618a5232012-06-12 13:54:36 -040010770 if (mbx_action == LPFC_MBX_NO_WAIT) {
10771 /* delay 100ms for port state */
10772 msleep(100);
10773 lpfc_sli_mbox_sys_flush(phba);
10774 return;
10775 }
James Smarta183a152011-10-10 21:32:43 -040010776 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010777
James Smart3772a992009-05-22 14:50:54 -040010778 spin_lock_irq(&phba->hbalock);
10779 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010780
10781 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010782 /* Determine how long we might wait for the active mailbox
10783 * command to be gracefully completed by firmware.
10784 */
James Smarta183a152011-10-10 21:32:43 -040010785 if (phba->sli.mbox_active)
10786 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10787 phba->sli.mbox_active) *
10788 1000) + jiffies;
10789 spin_unlock_irq(&phba->hbalock);
10790
James Smart3772a992009-05-22 14:50:54 -040010791 while (phba->sli.mbox_active) {
10792 /* Check active mailbox complete status every 2ms */
10793 msleep(2);
10794 if (time_after(jiffies, timeout))
10795 /* Timeout, let the mailbox flush routine to
10796 * forcefully release active mailbox command
10797 */
10798 break;
10799 }
James Smartd7069f02012-03-01 22:36:29 -050010800 } else
10801 spin_unlock_irq(&phba->hbalock);
10802
James Smart3772a992009-05-22 14:50:54 -040010803 lpfc_sli_mbox_sys_flush(phba);
10804}
10805
10806/**
10807 * lpfc_sli_eratt_read - read sli-3 error attention events
10808 * @phba: Pointer to HBA context.
10809 *
10810 * This function is called to read the SLI3 device error attention registers
10811 * for possible error attention events. The caller must hold the hostlock
10812 * with spin_lock_irq().
10813 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010814 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010815 * Register and returns 0 otherwise.
10816 **/
10817static int
10818lpfc_sli_eratt_read(struct lpfc_hba *phba)
10819{
James Smarted957682007-06-17 19:56:37 -050010820 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010821
James Smart3772a992009-05-22 14:50:54 -040010822 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010823 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10824 goto unplug_err;
10825
James Smart3772a992009-05-22 14:50:54 -040010826 if (ha_copy & HA_ERATT) {
10827 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010828 if (lpfc_sli_read_hs(phba))
10829 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010830
James Smart3772a992009-05-22 14:50:54 -040010831 /* Check if there is a deferred error condition is active */
10832 if ((HS_FFER1 & phba->work_hs) &&
10833 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010834 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010835 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010836 /* Clear all interrupt enable conditions */
10837 writel(0, phba->HCregaddr);
10838 readl(phba->HCregaddr);
10839 }
10840
10841 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010842 phba->work_ha |= HA_ERATT;
10843 /* Indicate polling handles this ERATT */
10844 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010845 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010846 }
James Smart3772a992009-05-22 14:50:54 -040010847 return 0;
James Smart9940b972011-03-11 16:06:12 -050010848
10849unplug_err:
10850 /* Set the driver HS work bitmap */
10851 phba->work_hs |= UNPLUG_ERR;
10852 /* Set the driver HA work bitmap */
10853 phba->work_ha |= HA_ERATT;
10854 /* Indicate polling handles this ERATT */
10855 phba->hba_flag |= HBA_ERATT_HANDLED;
10856 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010857}
10858
James Smarte59058c2008-08-24 21:49:00 -040010859/**
James Smartda0436e2009-05-22 14:51:39 -040010860 * lpfc_sli4_eratt_read - read sli-4 error attention events
10861 * @phba: Pointer to HBA context.
10862 *
10863 * This function is called to read the SLI4 device error attention registers
10864 * for possible error attention events. The caller must hold the hostlock
10865 * with spin_lock_irq().
10866 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010867 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010868 * Register and returns 0 otherwise.
10869 **/
10870static int
10871lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10872{
10873 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010874 uint32_t if_type, portsmphr;
10875 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010876
James Smart2fcee4b2010-12-15 17:57:46 -050010877 /*
10878 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010879 * registers for error attention. This can be changed later.
10880 */
James Smart2fcee4b2010-12-15 17:57:46 -050010881 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10882 switch (if_type) {
10883 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010884 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10885 &uerr_sta_lo) ||
10886 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10887 &uerr_sta_hi)) {
10888 phba->work_hs |= UNPLUG_ERR;
10889 phba->work_ha |= HA_ERATT;
10890 phba->hba_flag |= HBA_ERATT_HANDLED;
10891 return 1;
10892 }
James Smart2fcee4b2010-12-15 17:57:46 -050010893 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10894 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10896 "1423 HBA Unrecoverable error: "
10897 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10898 "ue_mask_lo_reg=0x%x, "
10899 "ue_mask_hi_reg=0x%x\n",
10900 uerr_sta_lo, uerr_sta_hi,
10901 phba->sli4_hba.ue_mask_lo,
10902 phba->sli4_hba.ue_mask_hi);
10903 phba->work_status[0] = uerr_sta_lo;
10904 phba->work_status[1] = uerr_sta_hi;
10905 phba->work_ha |= HA_ERATT;
10906 phba->hba_flag |= HBA_ERATT_HANDLED;
10907 return 1;
10908 }
10909 break;
10910 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010911 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10912 &portstat_reg.word0) ||
10913 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10914 &portsmphr)){
10915 phba->work_hs |= UNPLUG_ERR;
10916 phba->work_ha |= HA_ERATT;
10917 phba->hba_flag |= HBA_ERATT_HANDLED;
10918 return 1;
10919 }
James Smart2fcee4b2010-12-15 17:57:46 -050010920 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10921 phba->work_status[0] =
10922 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10923 phba->work_status[1] =
10924 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010926 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010927 "port status reg 0x%x, "
10928 "port smphr reg 0x%x, "
10929 "error 1=0x%x, error 2=0x%x\n",
10930 portstat_reg.word0,
10931 portsmphr,
10932 phba->work_status[0],
10933 phba->work_status[1]);
10934 phba->work_ha |= HA_ERATT;
10935 phba->hba_flag |= HBA_ERATT_HANDLED;
10936 return 1;
10937 }
10938 break;
10939 case LPFC_SLI_INTF_IF_TYPE_1:
10940 default:
James Smarta747c9c2009-11-18 15:41:10 -050010941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010942 "2886 HBA Error Attention on unsupported "
10943 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010944 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010945 }
James Smart2fcee4b2010-12-15 17:57:46 -050010946
James Smartda0436e2009-05-22 14:51:39 -040010947 return 0;
10948}
10949
10950/**
James Smart3621a712009-04-06 18:47:14 -040010951 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010952 * @phba: Pointer to HBA context.
10953 *
James Smart3772a992009-05-22 14:50:54 -040010954 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010955 * error attention register bit for error attention events.
10956 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010957 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010958 * Register and returns 0 otherwise.
10959 **/
10960int
10961lpfc_sli_check_eratt(struct lpfc_hba *phba)
10962{
10963 uint32_t ha_copy;
10964
10965 /* If somebody is waiting to handle an eratt, don't process it
10966 * here. The brdkill function will do this.
10967 */
10968 if (phba->link_flag & LS_IGNORE_ERATT)
10969 return 0;
10970
10971 /* Check if interrupt handler handles this ERATT */
10972 spin_lock_irq(&phba->hbalock);
10973 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10974 /* Interrupt handler has handled ERATT */
10975 spin_unlock_irq(&phba->hbalock);
10976 return 0;
10977 }
10978
James Smarta257bf92009-04-06 18:48:10 -040010979 /*
10980 * If there is deferred error attention, do not check for error
10981 * attention
10982 */
10983 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10984 spin_unlock_irq(&phba->hbalock);
10985 return 0;
10986 }
10987
James Smart3772a992009-05-22 14:50:54 -040010988 /* If PCI channel is offline, don't process it */
10989 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010990 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010991 return 0;
10992 }
10993
10994 switch (phba->sli_rev) {
10995 case LPFC_SLI_REV2:
10996 case LPFC_SLI_REV3:
10997 /* Read chip Host Attention (HA) register */
10998 ha_copy = lpfc_sli_eratt_read(phba);
10999 break;
James Smartda0436e2009-05-22 14:51:39 -040011000 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011001 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011002 ha_copy = lpfc_sli4_eratt_read(phba);
11003 break;
James Smart3772a992009-05-22 14:50:54 -040011004 default:
11005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11006 "0299 Invalid SLI revision (%d)\n",
11007 phba->sli_rev);
11008 ha_copy = 0;
11009 break;
James Smart93996272008-08-24 21:50:30 -040011010 }
11011 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011012
11013 return ha_copy;
11014}
11015
11016/**
11017 * lpfc_intr_state_check - Check device state for interrupt handling
11018 * @phba: Pointer to HBA context.
11019 *
11020 * This inline routine checks whether a device or its PCI slot is in a state
11021 * that the interrupt should be handled.
11022 *
11023 * This function returns 0 if the device or the PCI slot is in a state that
11024 * interrupt should be handled, otherwise -EIO.
11025 */
11026static inline int
11027lpfc_intr_state_check(struct lpfc_hba *phba)
11028{
11029 /* If the pci channel is offline, ignore all the interrupts */
11030 if (unlikely(pci_channel_offline(phba->pcidev)))
11031 return -EIO;
11032
11033 /* Update device level interrupt statistics */
11034 phba->sli.slistat.sli_intr++;
11035
11036 /* Ignore all interrupts during initialization. */
11037 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11038 return -EIO;
11039
James Smart93996272008-08-24 21:50:30 -040011040 return 0;
11041}
11042
11043/**
James Smart3772a992009-05-22 14:50:54 -040011044 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011045 * @irq: Interrupt number.
11046 * @dev_id: The device context pointer.
11047 *
James Smart93996272008-08-24 21:50:30 -040011048 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011049 * service routine when device with SLI-3 interface spec is enabled with
11050 * MSI-X multi-message interrupt mode and there are slow-path events in
11051 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11052 * interrupt mode, this function is called as part of the device-level
11053 * interrupt handler. When the PCI slot is in error recovery or the HBA
11054 * is undergoing initialization, the interrupt handler will not process
11055 * the interrupt. The link attention and ELS ring attention events are
11056 * handled by the worker thread. The interrupt handler signals the worker
11057 * thread and returns for these events. This function is called without
11058 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011059 * structures.
11060 *
11061 * This function returns IRQ_HANDLED when interrupt is handled else it
11062 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011063 **/
dea31012005-04-17 16:05:31 -050011064irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011065lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011066{
James Smart2e0fef82007-06-17 19:56:36 -050011067 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011068 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011069 uint32_t work_ha_copy;
11070 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011071 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011072 uint32_t control;
11073
James Smart92d7f7b2007-06-17 19:56:38 -050011074 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011075 struct lpfc_vport *vport;
11076 struct lpfc_nodelist *ndlp;
11077 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011078 LPFC_MBOXQ_t *pmb;
11079 int rc;
11080
dea31012005-04-17 16:05:31 -050011081 /*
11082 * Get the driver's phba structure from the dev_id and
11083 * assume the HBA is not interrupting.
11084 */
James Smart93996272008-08-24 21:50:30 -040011085 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011086
11087 if (unlikely(!phba))
11088 return IRQ_NONE;
11089
dea31012005-04-17 16:05:31 -050011090 /*
James Smart93996272008-08-24 21:50:30 -040011091 * Stuff needs to be attented to when this function is invoked as an
11092 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011093 */
James Smart93996272008-08-24 21:50:30 -040011094 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011095 /* Check device state for handling interrupt */
11096 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011097 return IRQ_NONE;
11098 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011099 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011100 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11101 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011102 /* If somebody is waiting to handle an eratt don't process it
11103 * here. The brdkill function will do this.
11104 */
11105 if (phba->link_flag & LS_IGNORE_ERATT)
11106 ha_copy &= ~HA_ERATT;
11107 /* Check the need for handling ERATT in interrupt handler */
11108 if (ha_copy & HA_ERATT) {
11109 if (phba->hba_flag & HBA_ERATT_HANDLED)
11110 /* ERATT polling has handled ERATT */
11111 ha_copy &= ~HA_ERATT;
11112 else
11113 /* Indicate interrupt handler handles ERATT */
11114 phba->hba_flag |= HBA_ERATT_HANDLED;
11115 }
James Smarta257bf92009-04-06 18:48:10 -040011116
11117 /*
11118 * If there is deferred error attention, do not check for any
11119 * interrupt.
11120 */
11121 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011122 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011123 return IRQ_NONE;
11124 }
11125
James Smart93996272008-08-24 21:50:30 -040011126 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011127 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11128 goto unplug_error;
11129
James Smarta747c9c2009-11-18 15:41:10 -050011130 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11131 HC_LAINT_ENA | HC_ERINT_ENA),
11132 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011133 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11134 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011135 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011136 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011137 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011138 } else
11139 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011140
dea31012005-04-17 16:05:31 -050011141 work_ha_copy = ha_copy & phba->work_ha_mask;
11142
James Smart93996272008-08-24 21:50:30 -040011143 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011144 if (work_ha_copy & HA_LATT) {
11145 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11146 /*
11147 * Turn off Link Attention interrupts
11148 * until CLEAR_LA done
11149 */
James Smart5b75da22008-12-04 22:39:35 -050011150 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011151 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011152 if (lpfc_readl(phba->HCregaddr, &control))
11153 goto unplug_error;
dea31012005-04-17 16:05:31 -050011154 control &= ~HC_LAINT_ENA;
11155 writel(control, phba->HCregaddr);
11156 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011157 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011158 }
11159 else
11160 work_ha_copy &= ~HA_LATT;
11161 }
11162
James Smart93996272008-08-24 21:50:30 -040011163 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011164 /*
11165 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11166 * the only slow ring.
11167 */
11168 status = (work_ha_copy &
11169 (HA_RXMASK << (4*LPFC_ELS_RING)));
11170 status >>= (4*LPFC_ELS_RING);
11171 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011172 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011173 if (lpfc_readl(phba->HCregaddr, &control))
11174 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011175
11176 lpfc_debugfs_slow_ring_trc(phba,
11177 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11178 control, status,
11179 (uint32_t)phba->sli.slistat.sli_intr);
11180
James Smart858c9f62007-06-17 19:56:39 -050011181 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011182 lpfc_debugfs_slow_ring_trc(phba,
11183 "ISR Disable ring:"
11184 "pwork:x%x hawork:x%x wait:x%x",
11185 phba->work_ha, work_ha_copy,
11186 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011187 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011188
James Smart858c9f62007-06-17 19:56:39 -050011189 control &=
11190 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011191 writel(control, phba->HCregaddr);
11192 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011193 }
James Smarta58cbd52007-08-02 11:09:43 -040011194 else {
11195 lpfc_debugfs_slow_ring_trc(phba,
11196 "ISR slow ring: pwork:"
11197 "x%x hawork:x%x wait:x%x",
11198 phba->work_ha, work_ha_copy,
11199 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011200 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011201 }
James Smart5b75da22008-12-04 22:39:35 -050011202 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011203 }
11204 }
James Smart5b75da22008-12-04 22:39:35 -050011205 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011206 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011207 if (lpfc_sli_read_hs(phba))
11208 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011209 /*
11210 * Check if there is a deferred error condition
11211 * is active
11212 */
11213 if ((HS_FFER1 & phba->work_hs) &&
11214 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011215 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11216 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011217 phba->hba_flag |= DEFER_ERATT;
11218 /* Clear all interrupt enable conditions */
11219 writel(0, phba->HCregaddr);
11220 readl(phba->HCregaddr);
11221 }
11222 }
11223
James Smart93996272008-08-24 21:50:30 -040011224 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011225 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011226 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011227 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011228 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011229
11230 /* First check out the status word */
11231 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11232 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011233 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011234 /*
11235 * Stray Mailbox Interrupt, mbxCommand <cmd>
11236 * mbxStatus <status>
11237 */
James Smart09372822008-01-11 01:52:54 -050011238 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011239 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011240 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011241 "Interrupt mbxCommand x%x "
11242 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011243 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011244 pmbox->mbxCommand,
11245 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011246 /* clear mailbox attention bit */
11247 work_ha_copy &= ~HA_MBATT;
11248 } else {
James Smart97eab632008-04-07 10:16:05 -040011249 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011250 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011251 phba->last_completion_time = jiffies;
11252 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011253 if (pmb->mbox_cmpl) {
11254 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11255 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011256 if (pmb->out_ext_byte_len &&
11257 pmb->context2)
11258 lpfc_sli_pcimem_bcopy(
11259 phba->mbox_ext,
11260 pmb->context2,
11261 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011262 }
James Smart09372822008-01-11 01:52:54 -050011263 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11264 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11265
11266 lpfc_debugfs_disc_trc(vport,
11267 LPFC_DISC_TRC_MBOX_VPORT,
11268 "MBOX dflt rpi: : "
11269 "status:x%x rpi:x%x",
11270 (uint32_t)pmbox->mbxStatus,
11271 pmbox->un.varWords[0], 0);
11272
11273 if (!pmbox->mbxStatus) {
11274 mp = (struct lpfc_dmabuf *)
11275 (pmb->context1);
11276 ndlp = (struct lpfc_nodelist *)
11277 pmb->context2;
11278
11279 /* Reg_LOGIN of dflt RPI was
11280 * successful. new lets get
11281 * rid of the RPI using the
11282 * same mbox buffer.
11283 */
11284 lpfc_unreg_login(phba,
11285 vport->vpi,
11286 pmbox->un.varWords[0],
11287 pmb);
11288 pmb->mbox_cmpl =
11289 lpfc_mbx_cmpl_dflt_rpi;
11290 pmb->context1 = mp;
11291 pmb->context2 = ndlp;
11292 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011293 rc = lpfc_sli_issue_mbox(phba,
11294 pmb,
11295 MBX_NOWAIT);
11296 if (rc != MBX_BUSY)
11297 lpfc_printf_log(phba,
11298 KERN_ERR,
11299 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011300 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011301 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011302 if (rc != MBX_NOT_FINISHED)
11303 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011304 }
11305 }
James Smart5b75da22008-12-04 22:39:35 -050011306 spin_lock_irqsave(
11307 &phba->pport->work_port_lock,
11308 iflag);
James Smart09372822008-01-11 01:52:54 -050011309 phba->pport->work_port_events &=
11310 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011311 spin_unlock_irqrestore(
11312 &phba->pport->work_port_lock,
11313 iflag);
James Smart09372822008-01-11 01:52:54 -050011314 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011315 }
James Smart97eab632008-04-07 10:16:05 -040011316 } else
James Smart5b75da22008-12-04 22:39:35 -050011317 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011318
James Smart92d7f7b2007-06-17 19:56:38 -050011319 if ((work_ha_copy & HA_MBATT) &&
11320 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011321send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011322 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011323 do {
11324 rc = lpfc_sli_issue_mbox(phba, NULL,
11325 MBX_NOWAIT);
11326 } while (rc == MBX_NOT_FINISHED);
11327 if (rc != MBX_SUCCESS)
11328 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11329 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011330 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011331 }
11332
James Smart5b75da22008-12-04 22:39:35 -050011333 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011334 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011335 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011336 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011337 }
James Smart93996272008-08-24 21:50:30 -040011338 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011339unplug_error:
11340 spin_unlock_irqrestore(&phba->hbalock, iflag);
11341 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011342
James Smart3772a992009-05-22 14:50:54 -040011343} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011344
11345/**
James Smart3772a992009-05-22 14:50:54 -040011346 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011347 * @irq: Interrupt number.
11348 * @dev_id: The device context pointer.
11349 *
11350 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011351 * service routine when device with SLI-3 interface spec is enabled with
11352 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11353 * ring event in the HBA. However, when the device is enabled with either
11354 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11355 * device-level interrupt handler. When the PCI slot is in error recovery
11356 * or the HBA is undergoing initialization, the interrupt handler will not
11357 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11358 * the intrrupt context. This function is called without any lock held.
11359 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011360 *
11361 * This function returns IRQ_HANDLED when interrupt is handled else it
11362 * returns IRQ_NONE.
11363 **/
11364irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011365lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011366{
11367 struct lpfc_hba *phba;
11368 uint32_t ha_copy;
11369 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011370 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011371
11372 /* Get the driver's phba structure from the dev_id and
11373 * assume the HBA is not interrupting.
11374 */
11375 phba = (struct lpfc_hba *) dev_id;
11376
11377 if (unlikely(!phba))
11378 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011379
11380 /*
James Smart93996272008-08-24 21:50:30 -040011381 * Stuff needs to be attented to when this function is invoked as an
11382 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011383 */
James Smart93996272008-08-24 21:50:30 -040011384 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011385 /* Check device state for handling interrupt */
11386 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011387 return IRQ_NONE;
11388 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011389 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11390 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011391 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011392 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011393 /*
11394 * If there is deferred error attention, do not check for
11395 * any interrupt.
11396 */
11397 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011398 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011399 return IRQ_NONE;
11400 }
James Smart93996272008-08-24 21:50:30 -040011401 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11402 phba->HAregaddr);
11403 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011404 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011405 } else
11406 ha_copy = phba->ha_copy;
11407
11408 /*
11409 * Process all events on FCP ring. Take the optimized path for FCP IO.
11410 */
11411 ha_copy &= ~(phba->work_ha_mask);
11412
11413 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011414 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011415 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011416 lpfc_sli_handle_fast_ring_event(phba,
11417 &phba->sli.ring[LPFC_FCP_RING],
11418 status);
James Smarta4bc3372006-12-02 13:34:16 -050011419
11420 if (phba->cfg_multi_ring_support == 2) {
11421 /*
James Smart93996272008-08-24 21:50:30 -040011422 * Process all events on extra ring. Take the optimized path
11423 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011424 */
James Smart93996272008-08-24 21:50:30 -040011425 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011426 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011427 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011428 lpfc_sli_handle_fast_ring_event(phba,
11429 &phba->sli.ring[LPFC_EXTRA_RING],
11430 status);
11431 }
11432 }
dea31012005-04-17 16:05:31 -050011433 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011434} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011435
James Smart93996272008-08-24 21:50:30 -040011436/**
James Smart3772a992009-05-22 14:50:54 -040011437 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011438 * @irq: Interrupt number.
11439 * @dev_id: The device context pointer.
11440 *
James Smart3772a992009-05-22 14:50:54 -040011441 * This function is the HBA device-level interrupt handler to device with
11442 * SLI-3 interface spec, called from the PCI layer when either MSI or
11443 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11444 * requires driver attention. This function invokes the slow-path interrupt
11445 * attention handling function and fast-path interrupt attention handling
11446 * function in turn to process the relevant HBA attention events. This
11447 * function is called without any lock held. It gets the hbalock to access
11448 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011449 *
11450 * This function returns IRQ_HANDLED when interrupt is handled, else it
11451 * returns IRQ_NONE.
11452 **/
11453irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011454lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011455{
11456 struct lpfc_hba *phba;
11457 irqreturn_t sp_irq_rc, fp_irq_rc;
11458 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011459 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011460
11461 /*
11462 * Get the driver's phba structure from the dev_id and
11463 * assume the HBA is not interrupting.
11464 */
11465 phba = (struct lpfc_hba *) dev_id;
11466
11467 if (unlikely(!phba))
11468 return IRQ_NONE;
11469
James Smart3772a992009-05-22 14:50:54 -040011470 /* Check device state for handling interrupt */
11471 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011472 return IRQ_NONE;
11473
11474 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011475 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11476 spin_unlock(&phba->hbalock);
11477 return IRQ_HANDLED;
11478 }
11479
James Smart93996272008-08-24 21:50:30 -040011480 if (unlikely(!phba->ha_copy)) {
11481 spin_unlock(&phba->hbalock);
11482 return IRQ_NONE;
11483 } else if (phba->ha_copy & HA_ERATT) {
11484 if (phba->hba_flag & HBA_ERATT_HANDLED)
11485 /* ERATT polling has handled ERATT */
11486 phba->ha_copy &= ~HA_ERATT;
11487 else
11488 /* Indicate interrupt handler handles ERATT */
11489 phba->hba_flag |= HBA_ERATT_HANDLED;
11490 }
11491
James Smarta257bf92009-04-06 18:48:10 -040011492 /*
11493 * If there is deferred error attention, do not check for any interrupt.
11494 */
11495 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011496 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011497 return IRQ_NONE;
11498 }
11499
James Smart93996272008-08-24 21:50:30 -040011500 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011501 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11502 spin_unlock(&phba->hbalock);
11503 return IRQ_HANDLED;
11504 }
James Smarta747c9c2009-11-18 15:41:10 -050011505 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11506 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11507 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011508 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011509 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011510 readl(phba->HAregaddr); /* flush */
11511 spin_unlock(&phba->hbalock);
11512
11513 /*
11514 * Invokes slow-path host attention interrupt handling as appropriate.
11515 */
11516
11517 /* status of events with mailbox and link attention */
11518 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11519
11520 /* status of events with ELS ring */
11521 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11522 status2 >>= (4*LPFC_ELS_RING);
11523
11524 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011525 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011526 else
11527 sp_irq_rc = IRQ_NONE;
11528
11529 /*
11530 * Invoke fast-path host attention interrupt handling as appropriate.
11531 */
11532
11533 /* status of events with FCP ring */
11534 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11535 status1 >>= (4*LPFC_FCP_RING);
11536
11537 /* status of events with extra ring */
11538 if (phba->cfg_multi_ring_support == 2) {
11539 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11540 status2 >>= (4*LPFC_EXTRA_RING);
11541 } else
11542 status2 = 0;
11543
11544 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011545 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011546 else
11547 fp_irq_rc = IRQ_NONE;
11548
11549 /* Return device-level interrupt handling status */
11550 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011551} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011552
11553/**
11554 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11555 * @phba: pointer to lpfc hba data structure.
11556 *
11557 * This routine is invoked by the worker thread to process all the pending
11558 * SLI4 FCP abort XRI events.
11559 **/
11560void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11561{
11562 struct lpfc_cq_event *cq_event;
11563
11564 /* First, declare the fcp xri abort event has been handled */
11565 spin_lock_irq(&phba->hbalock);
11566 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11567 spin_unlock_irq(&phba->hbalock);
11568 /* Now, handle all the fcp xri abort events */
11569 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11570 /* Get the first event from the head of the event queue */
11571 spin_lock_irq(&phba->hbalock);
11572 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11573 cq_event, struct lpfc_cq_event, list);
11574 spin_unlock_irq(&phba->hbalock);
11575 /* Notify aborted XRI for FCP work queue */
11576 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11577 /* Free the event processed back to the free pool */
11578 lpfc_sli4_cq_event_release(phba, cq_event);
11579 }
11580}
11581
11582/**
11583 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11584 * @phba: pointer to lpfc hba data structure.
11585 *
11586 * This routine is invoked by the worker thread to process all the pending
11587 * SLI4 els abort xri events.
11588 **/
11589void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11590{
11591 struct lpfc_cq_event *cq_event;
11592
11593 /* First, declare the els xri abort event has been handled */
11594 spin_lock_irq(&phba->hbalock);
11595 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11596 spin_unlock_irq(&phba->hbalock);
11597 /* Now, handle all the els xri abort events */
11598 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11599 /* Get the first event from the head of the event queue */
11600 spin_lock_irq(&phba->hbalock);
11601 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11602 cq_event, struct lpfc_cq_event, list);
11603 spin_unlock_irq(&phba->hbalock);
11604 /* Notify aborted XRI for ELS work queue */
11605 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11606 /* Free the event processed back to the free pool */
11607 lpfc_sli4_cq_event_release(phba, cq_event);
11608 }
11609}
11610
James Smart341af102010-01-26 23:07:37 -050011611/**
11612 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11613 * @phba: pointer to lpfc hba data structure
11614 * @pIocbIn: pointer to the rspiocbq
11615 * @pIocbOut: pointer to the cmdiocbq
11616 * @wcqe: pointer to the complete wcqe
11617 *
11618 * This routine transfers the fields of a command iocbq to a response iocbq
11619 * by copying all the IOCB fields from command iocbq and transferring the
11620 * completion status information from the complete wcqe.
11621 **/
James Smart4f774512009-05-22 14:52:35 -040011622static void
James Smart341af102010-01-26 23:07:37 -050011623lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11624 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011625 struct lpfc_iocbq *pIocbOut,
11626 struct lpfc_wcqe_complete *wcqe)
11627{
James Smartaf227412013-10-10 12:23:10 -040011628 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011629 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011630 uint32_t status, max_response;
11631 struct lpfc_dmabuf *dmabuf;
11632 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011633 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11634
11635 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11636 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011637 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011638 status = bf_get(lpfc_wcqe_c_status, wcqe);
11639 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011640 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11641 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11642 pIocbIn->iocb.un.fcpi.fcpi_parm =
11643 pIocbOut->iocb.un.fcpi.fcpi_parm -
11644 wcqe->total_data_placed;
11645 else
11646 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011647 else {
James Smart4f774512009-05-22 14:52:35 -040011648 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011649 switch (pIocbOut->iocb.ulpCommand) {
11650 case CMD_ELS_REQUEST64_CR:
11651 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11652 bpl = (struct ulp_bde64 *)dmabuf->virt;
11653 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11654 max_response = bde.tus.f.bdeSize;
11655 break;
11656 case CMD_GEN_REQUEST64_CR:
11657 max_response = 0;
11658 if (!pIocbOut->context3)
11659 break;
11660 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11661 sizeof(struct ulp_bde64);
11662 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11663 bpl = (struct ulp_bde64 *)dmabuf->virt;
11664 for (i = 0; i < numBdes; i++) {
11665 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11666 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11667 max_response += bde.tus.f.bdeSize;
11668 }
11669 break;
11670 default:
11671 max_response = wcqe->total_data_placed;
11672 break;
11673 }
11674 if (max_response < wcqe->total_data_placed)
11675 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11676 else
11677 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11678 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011679 }
James Smart341af102010-01-26 23:07:37 -050011680
James Smartacd68592012-01-18 16:25:09 -050011681 /* Convert BG errors for completion status */
11682 if (status == CQE_STATUS_DI_ERROR) {
11683 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11684
11685 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11686 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11687 else
11688 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11689
11690 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11691 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11692 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11693 BGS_GUARD_ERR_MASK;
11694 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11695 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11696 BGS_APPTAG_ERR_MASK;
11697 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11698 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11699 BGS_REFTAG_ERR_MASK;
11700
11701 /* Check to see if there was any good data before the error */
11702 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11703 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11704 BGS_HI_WATER_MARK_PRESENT_MASK;
11705 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11706 wcqe->total_data_placed;
11707 }
11708
11709 /*
11710 * Set ALL the error bits to indicate we don't know what
11711 * type of error it is.
11712 */
11713 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11714 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11715 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11716 BGS_GUARD_ERR_MASK);
11717 }
11718
James Smart341af102010-01-26 23:07:37 -050011719 /* Pick up HBA exchange busy condition */
11720 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11721 spin_lock_irqsave(&phba->hbalock, iflags);
11722 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11723 spin_unlock_irqrestore(&phba->hbalock, iflags);
11724 }
James Smart4f774512009-05-22 14:52:35 -040011725}
11726
11727/**
James Smart45ed1192009-10-02 15:17:02 -040011728 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11729 * @phba: Pointer to HBA context object.
11730 * @wcqe: Pointer to work-queue completion queue entry.
11731 *
11732 * This routine handles an ELS work-queue completion event and construct
11733 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11734 * discovery engine to handle.
11735 *
11736 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11737 **/
11738static struct lpfc_iocbq *
11739lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11740 struct lpfc_iocbq *irspiocbq)
11741{
11742 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11743 struct lpfc_iocbq *cmdiocbq;
11744 struct lpfc_wcqe_complete *wcqe;
11745 unsigned long iflags;
11746
11747 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011748 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011749 pring->stats.iocb_event++;
11750 /* Look up the ELS command IOCB and create pseudo response IOCB */
11751 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11752 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011753 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011754
11755 if (unlikely(!cmdiocbq)) {
11756 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11757 "0386 ELS complete with no corresponding "
11758 "cmdiocb: iotag (%d)\n",
11759 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11760 lpfc_sli_release_iocbq(phba, irspiocbq);
11761 return NULL;
11762 }
11763
11764 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011765 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011766
11767 return irspiocbq;
11768}
11769
11770/**
James Smart04c68492009-05-22 14:52:52 -040011771 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11772 * @phba: Pointer to HBA context object.
11773 * @cqe: Pointer to mailbox completion queue entry.
11774 *
11775 * This routine process a mailbox completion queue entry with asynchrous
11776 * event.
11777 *
11778 * Return: true if work posted to worker thread, otherwise false.
11779 **/
11780static bool
11781lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11782{
11783 struct lpfc_cq_event *cq_event;
11784 unsigned long iflags;
11785
11786 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11787 "0392 Async Event: word0:x%x, word1:x%x, "
11788 "word2:x%x, word3:x%x\n", mcqe->word0,
11789 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11790
11791 /* Allocate a new internal CQ_EVENT entry */
11792 cq_event = lpfc_sli4_cq_event_alloc(phba);
11793 if (!cq_event) {
11794 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11795 "0394 Failed to allocate CQ_EVENT entry\n");
11796 return false;
11797 }
11798
11799 /* Move the CQE into an asynchronous event entry */
11800 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11801 spin_lock_irqsave(&phba->hbalock, iflags);
11802 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11803 /* Set the async event flag */
11804 phba->hba_flag |= ASYNC_EVENT;
11805 spin_unlock_irqrestore(&phba->hbalock, iflags);
11806
11807 return true;
11808}
11809
11810/**
11811 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11812 * @phba: Pointer to HBA context object.
11813 * @cqe: Pointer to mailbox completion queue entry.
11814 *
11815 * This routine process a mailbox completion queue entry with mailbox
11816 * completion event.
11817 *
11818 * Return: true if work posted to worker thread, otherwise false.
11819 **/
11820static bool
11821lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11822{
11823 uint32_t mcqe_status;
11824 MAILBOX_t *mbox, *pmbox;
11825 struct lpfc_mqe *mqe;
11826 struct lpfc_vport *vport;
11827 struct lpfc_nodelist *ndlp;
11828 struct lpfc_dmabuf *mp;
11829 unsigned long iflags;
11830 LPFC_MBOXQ_t *pmb;
11831 bool workposted = false;
11832 int rc;
11833
11834 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11835 if (!bf_get(lpfc_trailer_completed, mcqe))
11836 goto out_no_mqe_complete;
11837
11838 /* Get the reference to the active mbox command */
11839 spin_lock_irqsave(&phba->hbalock, iflags);
11840 pmb = phba->sli.mbox_active;
11841 if (unlikely(!pmb)) {
11842 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11843 "1832 No pending MBOX command to handle\n");
11844 spin_unlock_irqrestore(&phba->hbalock, iflags);
11845 goto out_no_mqe_complete;
11846 }
11847 spin_unlock_irqrestore(&phba->hbalock, iflags);
11848 mqe = &pmb->u.mqe;
11849 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11850 mbox = phba->mbox;
11851 vport = pmb->vport;
11852
11853 /* Reset heartbeat timer */
11854 phba->last_completion_time = jiffies;
11855 del_timer(&phba->sli.mbox_tmo);
11856
11857 /* Move mbox data to caller's mailbox region, do endian swapping */
11858 if (pmb->mbox_cmpl && mbox)
11859 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011860
James Smart73d91e52011-10-10 21:32:10 -040011861 /*
11862 * For mcqe errors, conditionally move a modified error code to
11863 * the mbox so that the error will not be missed.
11864 */
11865 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11866 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11867 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11868 bf_set(lpfc_mqe_status, mqe,
11869 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11870 }
James Smart04c68492009-05-22 14:52:52 -040011871 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11872 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11873 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11874 "MBOX dflt rpi: status:x%x rpi:x%x",
11875 mcqe_status,
11876 pmbox->un.varWords[0], 0);
11877 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11878 mp = (struct lpfc_dmabuf *)(pmb->context1);
11879 ndlp = (struct lpfc_nodelist *)pmb->context2;
11880 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11881 * RID of the PPI using the same mbox buffer.
11882 */
11883 lpfc_unreg_login(phba, vport->vpi,
11884 pmbox->un.varWords[0], pmb);
11885 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11886 pmb->context1 = mp;
11887 pmb->context2 = ndlp;
11888 pmb->vport = vport;
11889 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11890 if (rc != MBX_BUSY)
11891 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11892 LOG_SLI, "0385 rc should "
11893 "have been MBX_BUSY\n");
11894 if (rc != MBX_NOT_FINISHED)
11895 goto send_current_mbox;
11896 }
11897 }
11898 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11899 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11900 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11901
11902 /* There is mailbox completion work to do */
11903 spin_lock_irqsave(&phba->hbalock, iflags);
11904 __lpfc_mbox_cmpl_put(phba, pmb);
11905 phba->work_ha |= HA_MBATT;
11906 spin_unlock_irqrestore(&phba->hbalock, iflags);
11907 workposted = true;
11908
11909send_current_mbox:
11910 spin_lock_irqsave(&phba->hbalock, iflags);
11911 /* Release the mailbox command posting token */
11912 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11913 /* Setting active mailbox pointer need to be in sync to flag clear */
11914 phba->sli.mbox_active = NULL;
11915 spin_unlock_irqrestore(&phba->hbalock, iflags);
11916 /* Wake up worker thread to post the next pending mailbox command */
11917 lpfc_worker_wake_up(phba);
11918out_no_mqe_complete:
11919 if (bf_get(lpfc_trailer_consumed, mcqe))
11920 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11921 return workposted;
11922}
11923
11924/**
11925 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11926 * @phba: Pointer to HBA context object.
11927 * @cqe: Pointer to mailbox completion queue entry.
11928 *
11929 * This routine process a mailbox completion queue entry, it invokes the
11930 * proper mailbox complete handling or asynchrous event handling routine
11931 * according to the MCQE's async bit.
11932 *
11933 * Return: true if work posted to worker thread, otherwise false.
11934 **/
11935static bool
11936lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11937{
11938 struct lpfc_mcqe mcqe;
11939 bool workposted;
11940
11941 /* Copy the mailbox MCQE and convert endian order as needed */
11942 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11943
11944 /* Invoke the proper event handling routine */
11945 if (!bf_get(lpfc_trailer_async, &mcqe))
11946 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11947 else
11948 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11949 return workposted;
11950}
11951
11952/**
James Smart4f774512009-05-22 14:52:35 -040011953 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11954 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011955 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011956 * @wcqe: Pointer to work-queue completion queue entry.
11957 *
11958 * This routine handles an ELS work-queue completion event.
11959 *
11960 * Return: true if work posted to worker thread, otherwise false.
11961 **/
11962static bool
James Smart2a76a282012-08-03 12:35:54 -040011963lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011964 struct lpfc_wcqe_complete *wcqe)
11965{
James Smart4f774512009-05-22 14:52:35 -040011966 struct lpfc_iocbq *irspiocbq;
11967 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011968 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011969 int txq_cnt = 0;
11970 int txcmplq_cnt = 0;
11971 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040011972
James Smart45ed1192009-10-02 15:17:02 -040011973 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011974 irspiocbq = lpfc_sli_get_iocbq(phba);
11975 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011976 if (!list_empty(&pring->txq))
11977 txq_cnt++;
11978 if (!list_empty(&pring->txcmplq))
11979 txcmplq_cnt++;
11980 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11981 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040011982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011983 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11984 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050011985 txq_cnt, phba->iocb_cnt,
11986 fcp_txcmplq_cnt,
11987 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011988 return false;
James Smart4f774512009-05-22 14:52:35 -040011989 }
James Smart4f774512009-05-22 14:52:35 -040011990
James Smart45ed1192009-10-02 15:17:02 -040011991 /* Save off the slow-path queue event for work thread to process */
11992 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011993 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011994 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011995 &phba->sli4_hba.sp_queue_event);
11996 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011997 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011998
James Smart45ed1192009-10-02 15:17:02 -040011999 return true;
James Smart4f774512009-05-22 14:52:35 -040012000}
12001
12002/**
12003 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12004 * @phba: Pointer to HBA context object.
12005 * @wcqe: Pointer to work-queue completion queue entry.
12006 *
12007 * This routine handles slow-path WQ entry comsumed event by invoking the
12008 * proper WQ release routine to the slow-path WQ.
12009 **/
12010static void
12011lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12012 struct lpfc_wcqe_release *wcqe)
12013{
James Smart2e90f4b2011-12-13 13:22:37 -050012014 /* sanity check on queue memory */
12015 if (unlikely(!phba->sli4_hba.els_wq))
12016 return;
James Smart4f774512009-05-22 14:52:35 -040012017 /* Check for the slow-path ELS work queue */
12018 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12019 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12020 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12021 else
12022 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12023 "2579 Slow-path wqe consume event carries "
12024 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12025 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12026 phba->sli4_hba.els_wq->queue_id);
12027}
12028
12029/**
12030 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12031 * @phba: Pointer to HBA context object.
12032 * @cq: Pointer to a WQ completion queue.
12033 * @wcqe: Pointer to work-queue completion queue entry.
12034 *
12035 * This routine handles an XRI abort event.
12036 *
12037 * Return: true if work posted to worker thread, otherwise false.
12038 **/
12039static bool
12040lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12041 struct lpfc_queue *cq,
12042 struct sli4_wcqe_xri_aborted *wcqe)
12043{
12044 bool workposted = false;
12045 struct lpfc_cq_event *cq_event;
12046 unsigned long iflags;
12047
12048 /* Allocate a new internal CQ_EVENT entry */
12049 cq_event = lpfc_sli4_cq_event_alloc(phba);
12050 if (!cq_event) {
12051 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12052 "0602 Failed to allocate CQ_EVENT entry\n");
12053 return false;
12054 }
12055
12056 /* Move the CQE into the proper xri abort event list */
12057 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12058 switch (cq->subtype) {
12059 case LPFC_FCP:
12060 spin_lock_irqsave(&phba->hbalock, iflags);
12061 list_add_tail(&cq_event->list,
12062 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12063 /* Set the fcp xri abort event flag */
12064 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12065 spin_unlock_irqrestore(&phba->hbalock, iflags);
12066 workposted = true;
12067 break;
12068 case LPFC_ELS:
12069 spin_lock_irqsave(&phba->hbalock, iflags);
12070 list_add_tail(&cq_event->list,
12071 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12072 /* Set the els xri abort event flag */
12073 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12074 spin_unlock_irqrestore(&phba->hbalock, iflags);
12075 workposted = true;
12076 break;
12077 default:
12078 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12079 "0603 Invalid work queue CQE subtype (x%x)\n",
12080 cq->subtype);
12081 workposted = false;
12082 break;
12083 }
12084 return workposted;
12085}
12086
12087/**
James Smart4d9ab992009-10-02 15:16:39 -040012088 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012089 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012090 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012091 *
James Smart4d9ab992009-10-02 15:16:39 -040012092 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012093 *
12094 * Return: true if work posted to worker thread, otherwise false.
12095 **/
12096static bool
James Smart4d9ab992009-10-02 15:16:39 -040012097lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12098{
12099 bool workposted = false;
12100 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12101 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12102 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012103 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012104 unsigned long iflags;
12105
James Smart2e90f4b2011-12-13 13:22:37 -050012106 /* sanity check on queue memory */
12107 if (unlikely(!hrq) || unlikely(!drq))
12108 return workposted;
12109
James Smart7851fe22011-07-22 18:36:52 -040012110 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12111 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12112 else
12113 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12114 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012115 goto out;
12116
12117 status = bf_get(lpfc_rcqe_status, rcqe);
12118 switch (status) {
12119 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12120 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12121 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012122 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012123 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012124 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012125 spin_lock_irqsave(&phba->hbalock, iflags);
12126 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12127 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012128 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012129 spin_unlock_irqrestore(&phba->hbalock, iflags);
12130 goto out;
12131 }
James Smartb84daac2012-08-03 12:35:13 -040012132 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012133 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12134 /* save off the frame for the word thread to process */
12135 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012136 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012137 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012138 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012139 spin_unlock_irqrestore(&phba->hbalock, iflags);
12140 workposted = true;
12141 break;
12142 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12143 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012144 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012145 /* Post more buffers if possible */
12146 spin_lock_irqsave(&phba->hbalock, iflags);
12147 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12148 spin_unlock_irqrestore(&phba->hbalock, iflags);
12149 workposted = true;
12150 break;
12151 }
12152out:
12153 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012154}
12155
12156/**
12157 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12158 * @phba: Pointer to HBA context object.
12159 * @cq: Pointer to the completion queue.
12160 * @wcqe: Pointer to a completion queue entry.
12161 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012162 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012163 * entry.
12164 *
12165 * Return: true if work posted to worker thread, otherwise false.
12166 **/
12167static bool
12168lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012169 struct lpfc_cqe *cqe)
12170{
James Smart45ed1192009-10-02 15:17:02 -040012171 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012172 bool workposted = false;
12173
12174 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012175 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012176
12177 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012178 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012179 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012180 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012181 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012182 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012183 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012184 break;
12185 case CQE_CODE_RELEASE_WQE:
12186 /* Process the WQ release event */
12187 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012188 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012189 break;
12190 case CQE_CODE_XRI_ABORTED:
12191 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012192 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012193 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012194 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012195 break;
James Smart4d9ab992009-10-02 15:16:39 -040012196 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012197 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012198 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012199 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012200 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012201 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012202 break;
James Smart4f774512009-05-22 14:52:35 -040012203 default:
12204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12205 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012206 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012207 break;
12208 }
12209 return workposted;
12210}
12211
12212/**
James Smart4f774512009-05-22 14:52:35 -040012213 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12214 * @phba: Pointer to HBA context object.
12215 * @eqe: Pointer to fast-path event queue entry.
12216 *
12217 * This routine process a event queue entry from the slow-path event queue.
12218 * It will check the MajorCode and MinorCode to determine this is for a
12219 * completion event on a completion queue, if not, an error shall be logged
12220 * and just return. Otherwise, it will get to the corresponding completion
12221 * queue and process all the entries on that completion queue, rearm the
12222 * completion queue, and then return.
12223 *
12224 **/
12225static void
James Smart67d12732012-08-03 12:36:13 -040012226lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12227 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012228{
James Smart67d12732012-08-03 12:36:13 -040012229 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012230 struct lpfc_cqe *cqe;
12231 bool workposted = false;
12232 int ecount = 0;
12233 uint16_t cqid;
12234
James Smart4f774512009-05-22 14:52:35 -040012235 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012236 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012237
James Smart4f774512009-05-22 14:52:35 -040012238 list_for_each_entry(childq, &speq->child_list, list) {
12239 if (childq->queue_id == cqid) {
12240 cq = childq;
12241 break;
12242 }
12243 }
12244 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012245 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12247 "0365 Slow-path CQ identifier "
12248 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012249 return;
12250 }
12251
12252 /* Process all the entries to the CQ */
12253 switch (cq->type) {
12254 case LPFC_MCQ:
12255 while ((cqe = lpfc_sli4_cq_get(cq))) {
12256 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012257 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012258 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012259 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012260 }
12261 break;
12262 case LPFC_WCQ:
12263 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012264 if (cq->subtype == LPFC_FCP)
12265 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12266 cqe);
12267 else
12268 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12269 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012270 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012271 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12272 }
James Smartb84daac2012-08-03 12:35:13 -040012273
12274 /* Track the max number of CQEs processed in 1 EQ */
12275 if (ecount > cq->CQ_max_cqe)
12276 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012277 break;
12278 default:
12279 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12280 "0370 Invalid completion queue type (%d)\n",
12281 cq->type);
12282 return;
12283 }
12284
12285 /* Catch the no cq entry condition, log an error */
12286 if (unlikely(ecount == 0))
12287 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12288 "0371 No entry from the CQ: identifier "
12289 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12290
12291 /* In any case, flash and re-arm the RCQ */
12292 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12293
12294 /* wake up worker thread if there are works to be done */
12295 if (workposted)
12296 lpfc_worker_wake_up(phba);
12297}
12298
12299/**
12300 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012301 * @phba: Pointer to HBA context object.
12302 * @cq: Pointer to associated CQ
12303 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012304 *
12305 * This routine process a fast-path work queue completion entry from fast-path
12306 * event queue for FCP command response completion.
12307 **/
12308static void
James Smart2a76a282012-08-03 12:35:54 -040012309lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012310 struct lpfc_wcqe_complete *wcqe)
12311{
James Smart2a76a282012-08-03 12:35:54 -040012312 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012313 struct lpfc_iocbq *cmdiocbq;
12314 struct lpfc_iocbq irspiocbq;
12315 unsigned long iflags;
12316
James Smart4f774512009-05-22 14:52:35 -040012317 /* Check for response status */
12318 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12319 /* If resource errors reported from HBA, reduce queue
12320 * depth of the SCSI device.
12321 */
James Smarte3d2b802012-08-14 14:25:43 -040012322 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12323 IOSTAT_LOCAL_REJECT)) &&
12324 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12325 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012326 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012327
James Smart4f774512009-05-22 14:52:35 -040012328 /* Log the error status */
12329 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12330 "0373 FCP complete error: status=x%x, "
12331 "hw_status=x%x, total_data_specified=%d, "
12332 "parameter=x%x, word3=x%x\n",
12333 bf_get(lpfc_wcqe_c_status, wcqe),
12334 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12335 wcqe->total_data_placed, wcqe->parameter,
12336 wcqe->word3);
12337 }
12338
12339 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012340 spin_lock_irqsave(&pring->ring_lock, iflags);
12341 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012342 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12343 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012344 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012345 if (unlikely(!cmdiocbq)) {
12346 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12347 "0374 FCP complete with no corresponding "
12348 "cmdiocb: iotag (%d)\n",
12349 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12350 return;
12351 }
12352 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12353 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12354 "0375 FCP cmdiocb not callback function "
12355 "iotag: (%d)\n",
12356 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12357 return;
12358 }
12359
12360 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012361 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012362
James Smart0f65ff62010-02-26 14:14:23 -050012363 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12364 spin_lock_irqsave(&phba->hbalock, iflags);
12365 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12366 spin_unlock_irqrestore(&phba->hbalock, iflags);
12367 }
12368
James Smart4f774512009-05-22 14:52:35 -040012369 /* Pass the cmd_iocb and the rsp state to the upper layer */
12370 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12371}
12372
12373/**
12374 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12375 * @phba: Pointer to HBA context object.
12376 * @cq: Pointer to completion queue.
12377 * @wcqe: Pointer to work-queue completion queue entry.
12378 *
12379 * This routine handles an fast-path WQ entry comsumed event by invoking the
12380 * proper WQ release routine to the slow-path WQ.
12381 **/
12382static void
12383lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12384 struct lpfc_wcqe_release *wcqe)
12385{
12386 struct lpfc_queue *childwq;
12387 bool wqid_matched = false;
12388 uint16_t fcp_wqid;
12389
12390 /* Check for fast-path FCP work queue release */
12391 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12392 list_for_each_entry(childwq, &cq->child_list, list) {
12393 if (childwq->queue_id == fcp_wqid) {
12394 lpfc_sli4_wq_release(childwq,
12395 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12396 wqid_matched = true;
12397 break;
12398 }
12399 }
12400 /* Report warning log message if no match found */
12401 if (wqid_matched != true)
12402 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12403 "2580 Fast-path wqe consume event carries "
12404 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12405}
12406
12407/**
12408 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12409 * @cq: Pointer to the completion queue.
12410 * @eqe: Pointer to fast-path completion queue entry.
12411 *
12412 * This routine process a fast-path work queue completion entry from fast-path
12413 * event queue for FCP command response completion.
12414 **/
12415static int
12416lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12417 struct lpfc_cqe *cqe)
12418{
12419 struct lpfc_wcqe_release wcqe;
12420 bool workposted = false;
12421
12422 /* Copy the work queue CQE and convert endian order if needed */
12423 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12424
12425 /* Check and process for different type of WCQE and dispatch */
12426 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12427 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012428 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012429 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012430 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012431 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012432 (struct lpfc_wcqe_complete *)&wcqe);
12433 break;
12434 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012435 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012436 /* Process the WQ release event */
12437 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12438 (struct lpfc_wcqe_release *)&wcqe);
12439 break;
12440 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012441 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012442 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012443 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012444 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12445 (struct sli4_wcqe_xri_aborted *)&wcqe);
12446 break;
12447 default:
12448 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12449 "0144 Not a valid WCQE code: x%x\n",
12450 bf_get(lpfc_wcqe_c_code, &wcqe));
12451 break;
12452 }
12453 return workposted;
12454}
12455
12456/**
James Smart67d12732012-08-03 12:36:13 -040012457 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012458 * @phba: Pointer to HBA context object.
12459 * @eqe: Pointer to fast-path event queue entry.
12460 *
12461 * This routine process a event queue entry from the fast-path event queue.
12462 * It will check the MajorCode and MinorCode to determine this is for a
12463 * completion event on a completion queue, if not, an error shall be logged
12464 * and just return. Otherwise, it will get to the corresponding completion
12465 * queue and process all the entries on the completion queue, rearm the
12466 * completion queue, and then return.
12467 **/
12468static void
James Smart67d12732012-08-03 12:36:13 -040012469lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12470 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012471{
12472 struct lpfc_queue *cq;
12473 struct lpfc_cqe *cqe;
12474 bool workposted = false;
12475 uint16_t cqid;
12476 int ecount = 0;
12477
James Smartcb5172e2010-03-15 11:25:07 -040012478 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012480 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012481 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012482 bf_get_le32(lpfc_eqe_major_code, eqe),
12483 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012484 return;
12485 }
12486
James Smart67d12732012-08-03 12:36:13 -040012487 /* Get the reference to the corresponding CQ */
12488 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12489
12490 /* Check if this is a Slow path event */
12491 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12492 lpfc_sli4_sp_handle_eqe(phba, eqe,
12493 phba->sli4_hba.hba_eq[qidx]);
12494 return;
12495 }
12496
James Smart2e90f4b2011-12-13 13:22:37 -050012497 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12498 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12499 "3146 Fast-path completion queues "
12500 "does not exist\n");
12501 return;
12502 }
James Smart67d12732012-08-03 12:36:13 -040012503 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012504 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012505 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12506 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12507 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012508 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012509 return;
12510 }
12511
James Smart4f774512009-05-22 14:52:35 -040012512 if (unlikely(cqid != cq->queue_id)) {
12513 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12514 "0368 Miss-matched fast-path completion "
12515 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12516 cqid, cq->queue_id);
12517 return;
12518 }
12519
12520 /* Process all the entries to the CQ */
12521 while ((cqe = lpfc_sli4_cq_get(cq))) {
12522 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012523 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012524 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12525 }
12526
James Smartb84daac2012-08-03 12:35:13 -040012527 /* Track the max number of CQEs processed in 1 EQ */
12528 if (ecount > cq->CQ_max_cqe)
12529 cq->CQ_max_cqe = ecount;
12530
James Smart4f774512009-05-22 14:52:35 -040012531 /* Catch the no cq entry condition */
12532 if (unlikely(ecount == 0))
12533 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12534 "0369 No entry from fast-path completion "
12535 "queue fcpcqid=%d\n", cq->queue_id);
12536
12537 /* In any case, flash and re-arm the CQ */
12538 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12539
12540 /* wake up worker thread if there are works to be done */
12541 if (workposted)
12542 lpfc_worker_wake_up(phba);
12543}
12544
12545static void
12546lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12547{
12548 struct lpfc_eqe *eqe;
12549
12550 /* walk all the EQ entries and drop on the floor */
12551 while ((eqe = lpfc_sli4_eq_get(eq)))
12552 ;
12553
12554 /* Clear and re-arm the EQ */
12555 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12556}
12557
James Smart1ba981f2014-02-20 09:56:45 -050012558
12559/**
12560 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12561 * entry
12562 * @phba: Pointer to HBA context object.
12563 * @eqe: Pointer to fast-path event queue entry.
12564 *
12565 * This routine process a event queue entry from the Flash Optimized Fabric
12566 * event queue. It will check the MajorCode and MinorCode to determine this
12567 * is for a completion event on a completion queue, if not, an error shall be
12568 * logged and just return. Otherwise, it will get to the corresponding
12569 * completion queue and process all the entries on the completion queue, rearm
12570 * the completion queue, and then return.
12571 **/
12572static void
12573lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12574{
12575 struct lpfc_queue *cq;
12576 struct lpfc_cqe *cqe;
12577 bool workposted = false;
12578 uint16_t cqid;
12579 int ecount = 0;
12580
12581 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12583 "9147 Not a valid completion "
12584 "event: majorcode=x%x, minorcode=x%x\n",
12585 bf_get_le32(lpfc_eqe_major_code, eqe),
12586 bf_get_le32(lpfc_eqe_minor_code, eqe));
12587 return;
12588 }
12589
12590 /* Get the reference to the corresponding CQ */
12591 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12592
12593 /* Next check for OAS */
12594 cq = phba->sli4_hba.oas_cq;
12595 if (unlikely(!cq)) {
12596 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12597 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12598 "9148 OAS completion queue "
12599 "does not exist\n");
12600 return;
12601 }
12602
12603 if (unlikely(cqid != cq->queue_id)) {
12604 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12605 "9149 Miss-matched fast-path compl "
12606 "queue id: eqcqid=%d, fcpcqid=%d\n",
12607 cqid, cq->queue_id);
12608 return;
12609 }
12610
12611 /* Process all the entries to the OAS CQ */
12612 while ((cqe = lpfc_sli4_cq_get(cq))) {
12613 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12614 if (!(++ecount % cq->entry_repost))
12615 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12616 }
12617
12618 /* Track the max number of CQEs processed in 1 EQ */
12619 if (ecount > cq->CQ_max_cqe)
12620 cq->CQ_max_cqe = ecount;
12621
12622 /* Catch the no cq entry condition */
12623 if (unlikely(ecount == 0))
12624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12625 "9153 No entry from fast-path completion "
12626 "queue fcpcqid=%d\n", cq->queue_id);
12627
12628 /* In any case, flash and re-arm the CQ */
12629 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12630
12631 /* wake up worker thread if there are works to be done */
12632 if (workposted)
12633 lpfc_worker_wake_up(phba);
12634}
12635
12636/**
12637 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12638 * @irq: Interrupt number.
12639 * @dev_id: The device context pointer.
12640 *
12641 * This function is directly called from the PCI layer as an interrupt
12642 * service routine when device with SLI-4 interface spec is enabled with
12643 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12644 * IOCB ring event in the HBA. However, when the device is enabled with either
12645 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12646 * device-level interrupt handler. When the PCI slot is in error recovery
12647 * or the HBA is undergoing initialization, the interrupt handler will not
12648 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12649 * the intrrupt context. This function is called without any lock held.
12650 * It gets the hbalock to access and update SLI data structures. Note that,
12651 * the EQ to CQ are one-to-one map such that the EQ index is
12652 * equal to that of CQ index.
12653 *
12654 * This function returns IRQ_HANDLED when interrupt is handled else it
12655 * returns IRQ_NONE.
12656 **/
12657irqreturn_t
12658lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12659{
12660 struct lpfc_hba *phba;
12661 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12662 struct lpfc_queue *eq;
12663 struct lpfc_eqe *eqe;
12664 unsigned long iflag;
12665 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012666
12667 /* Get the driver's phba structure from the dev_id */
12668 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12669 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012670
12671 if (unlikely(!phba))
12672 return IRQ_NONE;
12673
12674 /* Get to the EQ struct associated with this vector */
12675 eq = phba->sli4_hba.fof_eq;
12676 if (unlikely(!eq))
12677 return IRQ_NONE;
12678
12679 /* Check device state for handling interrupt */
12680 if (unlikely(lpfc_intr_state_check(phba))) {
12681 eq->EQ_badstate++;
12682 /* Check again for link_state with lock held */
12683 spin_lock_irqsave(&phba->hbalock, iflag);
12684 if (phba->link_state < LPFC_LINK_DOWN)
12685 /* Flush, clear interrupt, and rearm the EQ */
12686 lpfc_sli4_eq_flush(phba, eq);
12687 spin_unlock_irqrestore(&phba->hbalock, iflag);
12688 return IRQ_NONE;
12689 }
12690
12691 /*
12692 * Process all the event on FCP fast-path EQ
12693 */
12694 while ((eqe = lpfc_sli4_eq_get(eq))) {
12695 lpfc_sli4_fof_handle_eqe(phba, eqe);
12696 if (!(++ecount % eq->entry_repost))
12697 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12698 eq->EQ_processed++;
12699 }
12700
12701 /* Track the max number of EQEs processed in 1 intr */
12702 if (ecount > eq->EQ_max_eqe)
12703 eq->EQ_max_eqe = ecount;
12704
12705
12706 if (unlikely(ecount == 0)) {
12707 eq->EQ_no_entry++;
12708
12709 if (phba->intr_type == MSIX)
12710 /* MSI-X treated interrupt served as no EQ share INT */
12711 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12712 "9145 MSI-X interrupt with no EQE\n");
12713 else {
12714 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12715 "9146 ISR interrupt with no EQE\n");
12716 /* Non MSI-X treated on interrupt as EQ share INT */
12717 return IRQ_NONE;
12718 }
12719 }
12720 /* Always clear and re-arm the fast-path EQ */
12721 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12722 return IRQ_HANDLED;
12723}
12724
James Smart4f774512009-05-22 14:52:35 -040012725/**
James Smart67d12732012-08-03 12:36:13 -040012726 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012727 * @irq: Interrupt number.
12728 * @dev_id: The device context pointer.
12729 *
12730 * This function is directly called from the PCI layer as an interrupt
12731 * service routine when device with SLI-4 interface spec is enabled with
12732 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12733 * ring event in the HBA. However, when the device is enabled with either
12734 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12735 * device-level interrupt handler. When the PCI slot is in error recovery
12736 * or the HBA is undergoing initialization, the interrupt handler will not
12737 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12738 * the intrrupt context. This function is called without any lock held.
12739 * It gets the hbalock to access and update SLI data structures. Note that,
12740 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12741 * equal to that of FCP CQ index.
12742 *
James Smart67d12732012-08-03 12:36:13 -040012743 * The link attention and ELS ring attention events are handled
12744 * by the worker thread. The interrupt handler signals the worker thread
12745 * and returns for these events. This function is called without any lock
12746 * held. It gets the hbalock to access and update SLI data structures.
12747 *
James Smart4f774512009-05-22 14:52:35 -040012748 * This function returns IRQ_HANDLED when interrupt is handled else it
12749 * returns IRQ_NONE.
12750 **/
12751irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012752lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012753{
12754 struct lpfc_hba *phba;
12755 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12756 struct lpfc_queue *fpeq;
12757 struct lpfc_eqe *eqe;
12758 unsigned long iflag;
12759 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012760 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012761
12762 /* Get the driver's phba structure from the dev_id */
12763 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12764 phba = fcp_eq_hdl->phba;
12765 fcp_eqidx = fcp_eq_hdl->idx;
12766
12767 if (unlikely(!phba))
12768 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012769 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012770 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012771
12772 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012773 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012774 if (unlikely(!fpeq))
12775 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012776
James Smartba20c852012-08-03 12:36:52 -040012777 if (lpfc_fcp_look_ahead) {
12778 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12779 lpfc_sli4_eq_clr_intr(fpeq);
12780 else {
12781 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12782 return IRQ_NONE;
12783 }
12784 }
12785
James Smart4f774512009-05-22 14:52:35 -040012786 /* Check device state for handling interrupt */
12787 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012788 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012789 /* Check again for link_state with lock held */
12790 spin_lock_irqsave(&phba->hbalock, iflag);
12791 if (phba->link_state < LPFC_LINK_DOWN)
12792 /* Flush, clear interrupt, and rearm the EQ */
12793 lpfc_sli4_eq_flush(phba, fpeq);
12794 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012795 if (lpfc_fcp_look_ahead)
12796 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012797 return IRQ_NONE;
12798 }
12799
12800 /*
12801 * Process all the event on FCP fast-path EQ
12802 */
12803 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012804 if (eqe == NULL)
12805 break;
12806
James Smart67d12732012-08-03 12:36:13 -040012807 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012808 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012809 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012810 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012811 }
12812
James Smartb84daac2012-08-03 12:35:13 -040012813 /* Track the max number of EQEs processed in 1 intr */
12814 if (ecount > fpeq->EQ_max_eqe)
12815 fpeq->EQ_max_eqe = ecount;
12816
James Smart4f774512009-05-22 14:52:35 -040012817 /* Always clear and re-arm the fast-path EQ */
12818 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12819
12820 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012821 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012822
12823 if (lpfc_fcp_look_ahead) {
12824 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12825 return IRQ_NONE;
12826 }
12827
James Smart4f774512009-05-22 14:52:35 -040012828 if (phba->intr_type == MSIX)
12829 /* MSI-X treated interrupt served as no EQ share INT */
12830 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12831 "0358 MSI-X interrupt with no EQE\n");
12832 else
12833 /* Non MSI-X treated on interrupt as EQ share INT */
12834 return IRQ_NONE;
12835 }
12836
James Smartba20c852012-08-03 12:36:52 -040012837 if (lpfc_fcp_look_ahead)
12838 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012839 return IRQ_HANDLED;
12840} /* lpfc_sli4_fp_intr_handler */
12841
12842/**
12843 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12844 * @irq: Interrupt number.
12845 * @dev_id: The device context pointer.
12846 *
12847 * This function is the device-level interrupt handler to device with SLI-4
12848 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12849 * interrupt mode is enabled and there is an event in the HBA which requires
12850 * driver attention. This function invokes the slow-path interrupt attention
12851 * handling function and fast-path interrupt attention handling function in
12852 * turn to process the relevant HBA attention events. This function is called
12853 * without any lock held. It gets the hbalock to access and update SLI data
12854 * structures.
12855 *
12856 * This function returns IRQ_HANDLED when interrupt is handled, else it
12857 * returns IRQ_NONE.
12858 **/
12859irqreturn_t
12860lpfc_sli4_intr_handler(int irq, void *dev_id)
12861{
12862 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012863 irqreturn_t hba_irq_rc;
12864 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012865 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012866
12867 /* Get the driver's phba structure from the dev_id */
12868 phba = (struct lpfc_hba *)dev_id;
12869
12870 if (unlikely(!phba))
12871 return IRQ_NONE;
12872
12873 /*
James Smart4f774512009-05-22 14:52:35 -040012874 * Invoke fast-path host attention interrupt handling as appropriate.
12875 */
James Smart67d12732012-08-03 12:36:13 -040012876 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12877 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012878 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012879 if (hba_irq_rc == IRQ_HANDLED)
12880 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012881 }
12882
James Smart1ba981f2014-02-20 09:56:45 -050012883 if (phba->cfg_fof) {
12884 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12885 &phba->sli4_hba.fcp_eq_hdl[0]);
12886 if (hba_irq_rc == IRQ_HANDLED)
12887 hba_handled |= true;
12888 }
12889
James Smart67d12732012-08-03 12:36:13 -040012890 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012891} /* lpfc_sli4_intr_handler */
12892
12893/**
12894 * lpfc_sli4_queue_free - free a queue structure and associated memory
12895 * @queue: The queue structure to free.
12896 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012897 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012898 * the host resident queue. This function must be called after destroying the
12899 * queue on the HBA.
12900 **/
12901void
12902lpfc_sli4_queue_free(struct lpfc_queue *queue)
12903{
12904 struct lpfc_dmabuf *dmabuf;
12905
12906 if (!queue)
12907 return;
12908
12909 while (!list_empty(&queue->page_list)) {
12910 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12911 list);
James Smart49198b32010-04-06 15:04:33 -040012912 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012913 dmabuf->virt, dmabuf->phys);
12914 kfree(dmabuf);
12915 }
12916 kfree(queue);
12917 return;
12918}
12919
12920/**
12921 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12922 * @phba: The HBA that this queue is being created on.
12923 * @entry_size: The size of each queue entry for this queue.
12924 * @entry count: The number of entries that this queue will handle.
12925 *
12926 * This function allocates a queue structure and the DMAable memory used for
12927 * the host resident queue. This function must be called before creating the
12928 * queue on the HBA.
12929 **/
12930struct lpfc_queue *
12931lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12932 uint32_t entry_count)
12933{
12934 struct lpfc_queue *queue;
12935 struct lpfc_dmabuf *dmabuf;
12936 int x, total_qe_count;
12937 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012938 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012939
James Smartcb5172e2010-03-15 11:25:07 -040012940 if (!phba->sli4_hba.pc_sli4_params.supported)
12941 hw_page_size = SLI4_PAGE_SIZE;
12942
James Smart4f774512009-05-22 14:52:35 -040012943 queue = kzalloc(sizeof(struct lpfc_queue) +
12944 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12945 if (!queue)
12946 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012947 queue->page_count = (ALIGN(entry_size * entry_count,
12948 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012949 INIT_LIST_HEAD(&queue->list);
12950 INIT_LIST_HEAD(&queue->page_list);
12951 INIT_LIST_HEAD(&queue->child_list);
12952 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12953 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12954 if (!dmabuf)
12955 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040012956 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
12957 hw_page_size, &dmabuf->phys,
12958 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040012959 if (!dmabuf->virt) {
12960 kfree(dmabuf);
12961 goto out_fail;
12962 }
12963 dmabuf->buffer_tag = x;
12964 list_add_tail(&dmabuf->list, &queue->page_list);
12965 /* initialize queue's entry array */
12966 dma_pointer = dmabuf->virt;
12967 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012968 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012969 total_qe_count++, dma_pointer += entry_size) {
12970 queue->qe[total_qe_count].address = dma_pointer;
12971 }
12972 }
12973 queue->entry_size = entry_size;
12974 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012975
12976 /*
12977 * entry_repost is calculated based on the number of entries in the
12978 * queue. This works out except for RQs. If buffers are NOT initially
12979 * posted for every RQE, entry_repost should be adjusted accordingly.
12980 */
12981 queue->entry_repost = (entry_count >> 3);
12982 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12983 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012984 queue->phba = phba;
12985
12986 return queue;
12987out_fail:
12988 lpfc_sli4_queue_free(queue);
12989 return NULL;
12990}
12991
12992/**
James Smart962bc512013-01-03 15:44:00 -050012993 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12994 * @phba: HBA structure that indicates port to create a queue on.
12995 * @pci_barset: PCI BAR set flag.
12996 *
12997 * This function shall perform iomap of the specified PCI BAR address to host
12998 * memory address if not already done so and return it. The returned host
12999 * memory address can be NULL.
13000 */
13001static void __iomem *
13002lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13003{
James Smart962bc512013-01-03 15:44:00 -050013004 if (!phba->pcidev)
13005 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013006
13007 switch (pci_barset) {
13008 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013009 return phba->pci_bar0_memmap_p;
13010 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013011 return phba->pci_bar2_memmap_p;
13012 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013013 return phba->pci_bar4_memmap_p;
13014 default:
13015 break;
13016 }
13017 return NULL;
13018}
13019
13020/**
James Smart173edbb2012-06-12 13:54:50 -040013021 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13022 * @phba: HBA structure that indicates port to create a queue on.
13023 * @startq: The starting FCP EQ to modify
13024 *
13025 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13026 *
13027 * The @phba struct is used to send mailbox command to HBA. The @startq
13028 * is used to get the starting FCP EQ to change.
13029 * This function is asynchronous and will wait for the mailbox
13030 * command to finish before continuing.
13031 *
13032 * On success this function will return a zero. If unable to allocate enough
13033 * memory this function will return -ENOMEM. If the queue create mailbox command
13034 * fails this function will return -ENXIO.
13035 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013036int
James Smart2c9c5a02015-04-07 15:07:15 -040013037lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013038{
13039 struct lpfc_mbx_modify_eq_delay *eq_delay;
13040 LPFC_MBOXQ_t *mbox;
13041 struct lpfc_queue *eq;
13042 int cnt, rc, length, status = 0;
13043 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013044 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013045 int fcp_eqidx;
13046 union lpfc_sli4_cfg_shdr *shdr;
13047 uint16_t dmult;
13048
James Smart67d12732012-08-03 12:36:13 -040013049 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013050 return 0;
13051
13052 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13053 if (!mbox)
13054 return -ENOMEM;
13055 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13056 sizeof(struct lpfc_sli4_cfg_mhdr));
13057 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13058 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13059 length, LPFC_SLI4_MBX_EMBED);
13060 eq_delay = &mbox->u.mqe.un.eq_delay;
13061
13062 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013063 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13064 if (result > LPFC_DMULT_CONST)
13065 dmult = 0;
13066 else
13067 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013068
13069 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013070 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013071 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013072 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013073 if (!eq)
13074 continue;
13075 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13076 eq_delay->u.request.eq[cnt].phase = 0;
13077 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13078 cnt++;
13079 if (cnt >= LPFC_MAX_EQ_DELAY)
13080 break;
13081 }
13082 eq_delay->u.request.num_eq = cnt;
13083
13084 mbox->vport = phba->pport;
13085 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13086 mbox->context1 = NULL;
13087 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13088 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13089 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13090 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13091 if (shdr_status || shdr_add_status || rc) {
13092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13093 "2512 MODIFY_EQ_DELAY mailbox failed with "
13094 "status x%x add_status x%x, mbx status x%x\n",
13095 shdr_status, shdr_add_status, rc);
13096 status = -ENXIO;
13097 }
13098 mempool_free(mbox, phba->mbox_mem_pool);
13099 return status;
13100}
13101
13102/**
James Smart4f774512009-05-22 14:52:35 -040013103 * lpfc_eq_create - Create an Event Queue on the HBA
13104 * @phba: HBA structure that indicates port to create a queue on.
13105 * @eq: The queue structure to use to create the event queue.
13106 * @imax: The maximum interrupt per second limit.
13107 *
13108 * This function creates an event queue, as detailed in @eq, on a port,
13109 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13110 *
13111 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13112 * is used to get the entry count and entry size that are necessary to
13113 * determine the number of pages to allocate and use for this queue. This
13114 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13115 * event queue. This function is asynchronous and will wait for the mailbox
13116 * command to finish before continuing.
13117 *
13118 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013119 * memory this function will return -ENOMEM. If the queue create mailbox command
13120 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013121 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013122int
James Smartee020062012-09-29 11:28:52 -040013123lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013124{
13125 struct lpfc_mbx_eq_create *eq_create;
13126 LPFC_MBOXQ_t *mbox;
13127 int rc, length, status = 0;
13128 struct lpfc_dmabuf *dmabuf;
13129 uint32_t shdr_status, shdr_add_status;
13130 union lpfc_sli4_cfg_shdr *shdr;
13131 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013132 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13133
James Smart2e90f4b2011-12-13 13:22:37 -050013134 /* sanity check on queue memory */
13135 if (!eq)
13136 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013137 if (!phba->sli4_hba.pc_sli4_params.supported)
13138 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013139
13140 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13141 if (!mbox)
13142 return -ENOMEM;
13143 length = (sizeof(struct lpfc_mbx_eq_create) -
13144 sizeof(struct lpfc_sli4_cfg_mhdr));
13145 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13146 LPFC_MBOX_OPCODE_EQ_CREATE,
13147 length, LPFC_SLI4_MBX_EMBED);
13148 eq_create = &mbox->u.mqe.un.eq_create;
13149 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13150 eq->page_count);
13151 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13152 LPFC_EQE_SIZE);
13153 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013154 /* don't setup delay multiplier using EQ_CREATE */
13155 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013156 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13157 dmult);
13158 switch (eq->entry_count) {
13159 default:
13160 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13161 "0360 Unsupported EQ count. (%d)\n",
13162 eq->entry_count);
13163 if (eq->entry_count < 256)
13164 return -EINVAL;
13165 /* otherwise default to smallest count (drop through) */
13166 case 256:
13167 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13168 LPFC_EQ_CNT_256);
13169 break;
13170 case 512:
13171 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13172 LPFC_EQ_CNT_512);
13173 break;
13174 case 1024:
13175 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13176 LPFC_EQ_CNT_1024);
13177 break;
13178 case 2048:
13179 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13180 LPFC_EQ_CNT_2048);
13181 break;
13182 case 4096:
13183 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13184 LPFC_EQ_CNT_4096);
13185 break;
13186 }
13187 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013188 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013189 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13190 putPaddrLow(dmabuf->phys);
13191 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13192 putPaddrHigh(dmabuf->phys);
13193 }
13194 mbox->vport = phba->pport;
13195 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13196 mbox->context1 = NULL;
13197 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13198 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13199 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13200 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13201 if (shdr_status || shdr_add_status || rc) {
13202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13203 "2500 EQ_CREATE mailbox failed with "
13204 "status x%x add_status x%x, mbx status x%x\n",
13205 shdr_status, shdr_add_status, rc);
13206 status = -ENXIO;
13207 }
13208 eq->type = LPFC_EQ;
13209 eq->subtype = LPFC_NONE;
13210 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13211 if (eq->queue_id == 0xFFFF)
13212 status = -ENXIO;
13213 eq->host_index = 0;
13214 eq->hba_index = 0;
13215
James Smart8fa38512009-07-19 10:01:03 -040013216 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013217 return status;
13218}
13219
13220/**
13221 * lpfc_cq_create - Create a Completion Queue on the HBA
13222 * @phba: HBA structure that indicates port to create a queue on.
13223 * @cq: The queue structure to use to create the completion queue.
13224 * @eq: The event queue to bind this completion queue to.
13225 *
13226 * This function creates a completion queue, as detailed in @wq, on a port,
13227 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13228 *
13229 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13230 * is used to get the entry count and entry size that are necessary to
13231 * determine the number of pages to allocate and use for this queue. The @eq
13232 * is used to indicate which event queue to bind this completion queue to. This
13233 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13234 * completion queue. This function is asynchronous and will wait for the mailbox
13235 * command to finish before continuing.
13236 *
13237 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013238 * memory this function will return -ENOMEM. If the queue create mailbox command
13239 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013240 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013241int
James Smart4f774512009-05-22 14:52:35 -040013242lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13243 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13244{
13245 struct lpfc_mbx_cq_create *cq_create;
13246 struct lpfc_dmabuf *dmabuf;
13247 LPFC_MBOXQ_t *mbox;
13248 int rc, length, status = 0;
13249 uint32_t shdr_status, shdr_add_status;
13250 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013251 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13252
James Smart2e90f4b2011-12-13 13:22:37 -050013253 /* sanity check on queue memory */
13254 if (!cq || !eq)
13255 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013256 if (!phba->sli4_hba.pc_sli4_params.supported)
13257 hw_page_size = SLI4_PAGE_SIZE;
13258
James Smart4f774512009-05-22 14:52:35 -040013259 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13260 if (!mbox)
13261 return -ENOMEM;
13262 length = (sizeof(struct lpfc_mbx_cq_create) -
13263 sizeof(struct lpfc_sli4_cfg_mhdr));
13264 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13265 LPFC_MBOX_OPCODE_CQ_CREATE,
13266 length, LPFC_SLI4_MBX_EMBED);
13267 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013268 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013269 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13270 cq->page_count);
13271 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13272 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013273 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13274 phba->sli4_hba.pc_sli4_params.cqv);
13275 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013276 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13277 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013278 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13279 eq->queue_id);
13280 } else {
13281 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13282 eq->queue_id);
13283 }
James Smart4f774512009-05-22 14:52:35 -040013284 switch (cq->entry_count) {
13285 default:
13286 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13287 "0361 Unsupported CQ count. (%d)\n",
13288 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013289 if (cq->entry_count < 256) {
13290 status = -EINVAL;
13291 goto out;
13292 }
James Smart4f774512009-05-22 14:52:35 -040013293 /* otherwise default to smallest count (drop through) */
13294 case 256:
13295 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13296 LPFC_CQ_CNT_256);
13297 break;
13298 case 512:
13299 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13300 LPFC_CQ_CNT_512);
13301 break;
13302 case 1024:
13303 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13304 LPFC_CQ_CNT_1024);
13305 break;
13306 }
13307 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013308 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013309 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13310 putPaddrLow(dmabuf->phys);
13311 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13312 putPaddrHigh(dmabuf->phys);
13313 }
13314 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13315
13316 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013317 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13318 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13319 if (shdr_status || shdr_add_status || rc) {
13320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13321 "2501 CQ_CREATE mailbox failed with "
13322 "status x%x add_status x%x, mbx status x%x\n",
13323 shdr_status, shdr_add_status, rc);
13324 status = -ENXIO;
13325 goto out;
13326 }
13327 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13328 if (cq->queue_id == 0xFFFF) {
13329 status = -ENXIO;
13330 goto out;
13331 }
13332 /* link the cq onto the parent eq child list */
13333 list_add_tail(&cq->list, &eq->child_list);
13334 /* Set up completion queue's type and subtype */
13335 cq->type = type;
13336 cq->subtype = subtype;
13337 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013338 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013339 cq->host_index = 0;
13340 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013341
James Smart8fa38512009-07-19 10:01:03 -040013342out:
13343 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013344 return status;
13345}
13346
13347/**
James Smartb19a0612010-04-06 14:48:51 -040013348 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013349 * @phba: HBA structure that indicates port to create a queue on.
13350 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013351 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13352 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013353 *
James Smartb19a0612010-04-06 14:48:51 -040013354 * This function provides failback (fb) functionality when the
13355 * mq_create_ext fails on older FW generations. It's purpose is identical
13356 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013357 *
James Smartb19a0612010-04-06 14:48:51 -040013358 * This routine cannot fail as all attributes were previously accessed and
13359 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013360 **/
James Smartb19a0612010-04-06 14:48:51 -040013361static void
13362lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13363 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013364{
13365 struct lpfc_mbx_mq_create *mq_create;
13366 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013367 int length;
James Smart04c68492009-05-22 14:52:52 -040013368
James Smart04c68492009-05-22 14:52:52 -040013369 length = (sizeof(struct lpfc_mbx_mq_create) -
13370 sizeof(struct lpfc_sli4_cfg_mhdr));
13371 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13372 LPFC_MBOX_OPCODE_MQ_CREATE,
13373 length, LPFC_SLI4_MBX_EMBED);
13374 mq_create = &mbox->u.mqe.un.mq_create;
13375 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013376 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013377 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013378 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013379 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13380 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013381 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013382 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13383 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013384 break;
13385 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013386 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13387 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013388 break;
13389 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013390 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13391 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013392 break;
13393 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013394 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13395 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013396 break;
13397 }
13398 list_for_each_entry(dmabuf, &mq->page_list, list) {
13399 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013400 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013401 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013402 putPaddrHigh(dmabuf->phys);
13403 }
13404}
13405
13406/**
13407 * lpfc_mq_create - Create a mailbox Queue on the HBA
13408 * @phba: HBA structure that indicates port to create a queue on.
13409 * @mq: The queue structure to use to create the mailbox queue.
13410 * @cq: The completion queue to associate with this cq.
13411 * @subtype: The queue's subtype.
13412 *
13413 * This function creates a mailbox queue, as detailed in @mq, on a port,
13414 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13415 *
13416 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13417 * is used to get the entry count and entry size that are necessary to
13418 * determine the number of pages to allocate and use for this queue. This
13419 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13420 * mailbox queue. This function is asynchronous and will wait for the mailbox
13421 * command to finish before continuing.
13422 *
13423 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013424 * memory this function will return -ENOMEM. If the queue create mailbox command
13425 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013426 **/
13427int32_t
13428lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13429 struct lpfc_queue *cq, uint32_t subtype)
13430{
13431 struct lpfc_mbx_mq_create *mq_create;
13432 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13433 struct lpfc_dmabuf *dmabuf;
13434 LPFC_MBOXQ_t *mbox;
13435 int rc, length, status = 0;
13436 uint32_t shdr_status, shdr_add_status;
13437 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013438 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013439
James Smart2e90f4b2011-12-13 13:22:37 -050013440 /* sanity check on queue memory */
13441 if (!mq || !cq)
13442 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013443 if (!phba->sli4_hba.pc_sli4_params.supported)
13444 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013445
13446 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13447 if (!mbox)
13448 return -ENOMEM;
13449 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13450 sizeof(struct lpfc_sli4_cfg_mhdr));
13451 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13452 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13453 length, LPFC_SLI4_MBX_EMBED);
13454
13455 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013456 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013457 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13458 &mq_create_ext->u.request, mq->page_count);
13459 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13460 &mq_create_ext->u.request, 1);
13461 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013462 &mq_create_ext->u.request, 1);
13463 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13464 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013465 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13466 &mq_create_ext->u.request, 1);
13467 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13468 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013469 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013470 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13471 phba->sli4_hba.pc_sli4_params.mqv);
13472 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13473 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13474 cq->queue_id);
13475 else
13476 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13477 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013478 switch (mq->entry_count) {
13479 default:
13480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13481 "0362 Unsupported MQ count. (%d)\n",
13482 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013483 if (mq->entry_count < 16) {
13484 status = -EINVAL;
13485 goto out;
13486 }
James Smartb19a0612010-04-06 14:48:51 -040013487 /* otherwise default to smallest count (drop through) */
13488 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013489 bf_set(lpfc_mq_context_ring_size,
13490 &mq_create_ext->u.request.context,
13491 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013492 break;
13493 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013494 bf_set(lpfc_mq_context_ring_size,
13495 &mq_create_ext->u.request.context,
13496 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013497 break;
13498 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013499 bf_set(lpfc_mq_context_ring_size,
13500 &mq_create_ext->u.request.context,
13501 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013502 break;
13503 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013504 bf_set(lpfc_mq_context_ring_size,
13505 &mq_create_ext->u.request.context,
13506 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013507 break;
13508 }
13509 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013510 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013511 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13512 putPaddrLow(dmabuf->phys);
13513 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013514 putPaddrHigh(dmabuf->phys);
13515 }
13516 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013517 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13518 &mq_create_ext->u.response);
13519 if (rc != MBX_SUCCESS) {
13520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13521 "2795 MQ_CREATE_EXT failed with "
13522 "status x%x. Failback to MQ_CREATE.\n",
13523 rc);
13524 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13525 mq_create = &mbox->u.mqe.un.mq_create;
13526 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13527 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13528 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13529 &mq_create->u.response);
13530 }
13531
James Smart04c68492009-05-22 14:52:52 -040013532 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013533 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13534 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13535 if (shdr_status || shdr_add_status || rc) {
13536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13537 "2502 MQ_CREATE mailbox failed with "
13538 "status x%x add_status x%x, mbx status x%x\n",
13539 shdr_status, shdr_add_status, rc);
13540 status = -ENXIO;
13541 goto out;
13542 }
James Smart04c68492009-05-22 14:52:52 -040013543 if (mq->queue_id == 0xFFFF) {
13544 status = -ENXIO;
13545 goto out;
13546 }
13547 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013548 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013549 mq->subtype = subtype;
13550 mq->host_index = 0;
13551 mq->hba_index = 0;
13552
13553 /* link the mq onto the parent cq child list */
13554 list_add_tail(&mq->list, &cq->child_list);
13555out:
James Smart8fa38512009-07-19 10:01:03 -040013556 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013557 return status;
13558}
13559
13560/**
James Smart4f774512009-05-22 14:52:35 -040013561 * lpfc_wq_create - Create a Work Queue on the HBA
13562 * @phba: HBA structure that indicates port to create a queue on.
13563 * @wq: The queue structure to use to create the work queue.
13564 * @cq: The completion queue to bind this work queue to.
13565 * @subtype: The subtype of the work queue indicating its functionality.
13566 *
13567 * This function creates a work queue, as detailed in @wq, on a port, described
13568 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13569 *
13570 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13571 * is used to get the entry count and entry size that are necessary to
13572 * determine the number of pages to allocate and use for this queue. The @cq
13573 * is used to indicate which completion queue to bind this work queue to. This
13574 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13575 * work queue. This function is asynchronous and will wait for the mailbox
13576 * command to finish before continuing.
13577 *
13578 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013579 * memory this function will return -ENOMEM. If the queue create mailbox command
13580 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013581 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013582int
James Smart4f774512009-05-22 14:52:35 -040013583lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13584 struct lpfc_queue *cq, uint32_t subtype)
13585{
13586 struct lpfc_mbx_wq_create *wq_create;
13587 struct lpfc_dmabuf *dmabuf;
13588 LPFC_MBOXQ_t *mbox;
13589 int rc, length, status = 0;
13590 uint32_t shdr_status, shdr_add_status;
13591 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013592 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013593 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013594 void __iomem *bar_memmap_p;
13595 uint32_t db_offset;
13596 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013597
James Smart2e90f4b2011-12-13 13:22:37 -050013598 /* sanity check on queue memory */
13599 if (!wq || !cq)
13600 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013601 if (!phba->sli4_hba.pc_sli4_params.supported)
13602 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013603
13604 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13605 if (!mbox)
13606 return -ENOMEM;
13607 length = (sizeof(struct lpfc_mbx_wq_create) -
13608 sizeof(struct lpfc_sli4_cfg_mhdr));
13609 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13610 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13611 length, LPFC_SLI4_MBX_EMBED);
13612 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013613 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013614 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13615 wq->page_count);
13616 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13617 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013618
13619 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013620 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13621 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013622
James Smart0c651872013-07-15 18:33:23 -040013623 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13624 case LPFC_Q_CREATE_VERSION_0:
13625 switch (wq->entry_size) {
13626 default:
13627 case 64:
13628 /* Nothing to do, version 0 ONLY supports 64 byte */
13629 page = wq_create->u.request.page;
13630 break;
13631 case 128:
13632 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13633 LPFC_WQ_SZ128_SUPPORT)) {
13634 status = -ERANGE;
13635 goto out;
13636 }
13637 /* If we get here the HBA MUST also support V1 and
13638 * we MUST use it
13639 */
13640 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13641 LPFC_Q_CREATE_VERSION_1);
13642
13643 bf_set(lpfc_mbx_wq_create_wqe_count,
13644 &wq_create->u.request_1, wq->entry_count);
13645 bf_set(lpfc_mbx_wq_create_wqe_size,
13646 &wq_create->u.request_1,
13647 LPFC_WQ_WQE_SIZE_128);
13648 bf_set(lpfc_mbx_wq_create_page_size,
13649 &wq_create->u.request_1,
13650 (PAGE_SIZE/SLI4_PAGE_SIZE));
13651 page = wq_create->u.request_1.page;
13652 break;
13653 }
13654 break;
13655 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013656 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13657 wq->entry_count);
13658 switch (wq->entry_size) {
13659 default:
13660 case 64:
13661 bf_set(lpfc_mbx_wq_create_wqe_size,
13662 &wq_create->u.request_1,
13663 LPFC_WQ_WQE_SIZE_64);
13664 break;
13665 case 128:
James Smart0c651872013-07-15 18:33:23 -040013666 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13667 LPFC_WQ_SZ128_SUPPORT)) {
13668 status = -ERANGE;
13669 goto out;
13670 }
James Smart5a6f1332011-03-11 16:05:35 -050013671 bf_set(lpfc_mbx_wq_create_wqe_size,
13672 &wq_create->u.request_1,
13673 LPFC_WQ_WQE_SIZE_128);
13674 break;
13675 }
13676 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13677 (PAGE_SIZE/SLI4_PAGE_SIZE));
13678 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013679 break;
13680 default:
13681 status = -ERANGE;
13682 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013683 }
James Smart0c651872013-07-15 18:33:23 -040013684
James Smart4f774512009-05-22 14:52:35 -040013685 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013686 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013687 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13688 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013689 }
James Smart962bc512013-01-03 15:44:00 -050013690
13691 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13692 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13693
James Smart4f774512009-05-22 14:52:35 -040013694 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13695 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013696 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13697 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13698 if (shdr_status || shdr_add_status || rc) {
13699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13700 "2503 WQ_CREATE mailbox failed with "
13701 "status x%x add_status x%x, mbx status x%x\n",
13702 shdr_status, shdr_add_status, rc);
13703 status = -ENXIO;
13704 goto out;
13705 }
13706 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13707 if (wq->queue_id == 0xFFFF) {
13708 status = -ENXIO;
13709 goto out;
13710 }
James Smart962bc512013-01-03 15:44:00 -050013711 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13712 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13713 &wq_create->u.response);
13714 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13715 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13717 "3265 WQ[%d] doorbell format not "
13718 "supported: x%x\n", wq->queue_id,
13719 wq->db_format);
13720 status = -EINVAL;
13721 goto out;
13722 }
13723 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13724 &wq_create->u.response);
13725 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13726 if (!bar_memmap_p) {
13727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13728 "3263 WQ[%d] failed to memmap pci "
13729 "barset:x%x\n", wq->queue_id,
13730 pci_barset);
13731 status = -ENOMEM;
13732 goto out;
13733 }
13734 db_offset = wq_create->u.response.doorbell_offset;
13735 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13736 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13738 "3252 WQ[%d] doorbell offset not "
13739 "supported: x%x\n", wq->queue_id,
13740 db_offset);
13741 status = -EINVAL;
13742 goto out;
13743 }
13744 wq->db_regaddr = bar_memmap_p + db_offset;
13745 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013746 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13747 "format:x%x\n", wq->queue_id, pci_barset,
13748 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013749 } else {
13750 wq->db_format = LPFC_DB_LIST_FORMAT;
13751 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13752 }
James Smart4f774512009-05-22 14:52:35 -040013753 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013754 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013755 wq->subtype = subtype;
13756 wq->host_index = 0;
13757 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013758 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013759
13760 /* link the wq onto the parent cq child list */
13761 list_add_tail(&wq->list, &cq->child_list);
13762out:
James Smart8fa38512009-07-19 10:01:03 -040013763 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013764 return status;
13765}
13766
13767/**
James Smart73d91e52011-10-10 21:32:10 -040013768 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13769 * @phba: HBA structure that indicates port to create a queue on.
13770 * @rq: The queue structure to use for the receive queue.
13771 * @qno: The associated HBQ number
13772 *
13773 *
13774 * For SLI4 we need to adjust the RQ repost value based on
13775 * the number of buffers that are initially posted to the RQ.
13776 */
13777void
13778lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13779{
13780 uint32_t cnt;
13781
James Smart2e90f4b2011-12-13 13:22:37 -050013782 /* sanity check on queue memory */
13783 if (!rq)
13784 return;
James Smart73d91e52011-10-10 21:32:10 -040013785 cnt = lpfc_hbq_defs[qno]->entry_count;
13786
13787 /* Recalc repost for RQs based on buffers initially posted */
13788 cnt = (cnt >> 3);
13789 if (cnt < LPFC_QUEUE_MIN_REPOST)
13790 cnt = LPFC_QUEUE_MIN_REPOST;
13791
13792 rq->entry_repost = cnt;
13793}
13794
13795/**
James Smart4f774512009-05-22 14:52:35 -040013796 * lpfc_rq_create - Create a Receive Queue on the HBA
13797 * @phba: HBA structure that indicates port to create a queue on.
13798 * @hrq: The queue structure to use to create the header receive queue.
13799 * @drq: The queue structure to use to create the data receive queue.
13800 * @cq: The completion queue to bind this work queue to.
13801 *
13802 * This function creates a receive buffer queue pair , as detailed in @hrq and
13803 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13804 * to the HBA.
13805 *
13806 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13807 * struct is used to get the entry count that is necessary to determine the
13808 * number of pages to use for this queue. The @cq is used to indicate which
13809 * completion queue to bind received buffers that are posted to these queues to.
13810 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13811 * receive queue pair. This function is asynchronous and will wait for the
13812 * mailbox command to finish before continuing.
13813 *
13814 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013815 * memory this function will return -ENOMEM. If the queue create mailbox command
13816 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013817 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013818int
James Smart4f774512009-05-22 14:52:35 -040013819lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13820 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13821{
13822 struct lpfc_mbx_rq_create *rq_create;
13823 struct lpfc_dmabuf *dmabuf;
13824 LPFC_MBOXQ_t *mbox;
13825 int rc, length, status = 0;
13826 uint32_t shdr_status, shdr_add_status;
13827 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013828 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013829 void __iomem *bar_memmap_p;
13830 uint32_t db_offset;
13831 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013832
James Smart2e90f4b2011-12-13 13:22:37 -050013833 /* sanity check on queue memory */
13834 if (!hrq || !drq || !cq)
13835 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013836 if (!phba->sli4_hba.pc_sli4_params.supported)
13837 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013838
13839 if (hrq->entry_count != drq->entry_count)
13840 return -EINVAL;
13841 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13842 if (!mbox)
13843 return -ENOMEM;
13844 length = (sizeof(struct lpfc_mbx_rq_create) -
13845 sizeof(struct lpfc_sli4_cfg_mhdr));
13846 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13847 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13848 length, LPFC_SLI4_MBX_EMBED);
13849 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013850 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13851 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13852 phba->sli4_hba.pc_sli4_params.rqv);
13853 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13854 bf_set(lpfc_rq_context_rqe_count_1,
13855 &rq_create->u.request.context,
13856 hrq->entry_count);
13857 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013858 bf_set(lpfc_rq_context_rqe_size,
13859 &rq_create->u.request.context,
13860 LPFC_RQE_SIZE_8);
13861 bf_set(lpfc_rq_context_page_size,
13862 &rq_create->u.request.context,
13863 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013864 } else {
13865 switch (hrq->entry_count) {
13866 default:
13867 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13868 "2535 Unsupported RQ count. (%d)\n",
13869 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013870 if (hrq->entry_count < 512) {
13871 status = -EINVAL;
13872 goto out;
13873 }
James Smart5a6f1332011-03-11 16:05:35 -050013874 /* otherwise default to smallest count (drop through) */
13875 case 512:
13876 bf_set(lpfc_rq_context_rqe_count,
13877 &rq_create->u.request.context,
13878 LPFC_RQ_RING_SIZE_512);
13879 break;
13880 case 1024:
13881 bf_set(lpfc_rq_context_rqe_count,
13882 &rq_create->u.request.context,
13883 LPFC_RQ_RING_SIZE_1024);
13884 break;
13885 case 2048:
13886 bf_set(lpfc_rq_context_rqe_count,
13887 &rq_create->u.request.context,
13888 LPFC_RQ_RING_SIZE_2048);
13889 break;
13890 case 4096:
13891 bf_set(lpfc_rq_context_rqe_count,
13892 &rq_create->u.request.context,
13893 LPFC_RQ_RING_SIZE_4096);
13894 break;
13895 }
13896 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13897 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013898 }
13899 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13900 cq->queue_id);
13901 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13902 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013903 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013904 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013905 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13906 putPaddrLow(dmabuf->phys);
13907 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13908 putPaddrHigh(dmabuf->phys);
13909 }
James Smart962bc512013-01-03 15:44:00 -050013910 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13911 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13912
James Smart4f774512009-05-22 14:52:35 -040013913 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13914 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013915 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13916 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13917 if (shdr_status || shdr_add_status || rc) {
13918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13919 "2504 RQ_CREATE mailbox failed with "
13920 "status x%x add_status x%x, mbx status x%x\n",
13921 shdr_status, shdr_add_status, rc);
13922 status = -ENXIO;
13923 goto out;
13924 }
13925 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13926 if (hrq->queue_id == 0xFFFF) {
13927 status = -ENXIO;
13928 goto out;
13929 }
James Smart962bc512013-01-03 15:44:00 -050013930
13931 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13932 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13933 &rq_create->u.response);
13934 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13935 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13937 "3262 RQ [%d] doorbell format not "
13938 "supported: x%x\n", hrq->queue_id,
13939 hrq->db_format);
13940 status = -EINVAL;
13941 goto out;
13942 }
13943
13944 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13945 &rq_create->u.response);
13946 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13947 if (!bar_memmap_p) {
13948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13949 "3269 RQ[%d] failed to memmap pci "
13950 "barset:x%x\n", hrq->queue_id,
13951 pci_barset);
13952 status = -ENOMEM;
13953 goto out;
13954 }
13955
13956 db_offset = rq_create->u.response.doorbell_offset;
13957 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13958 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13960 "3270 RQ[%d] doorbell offset not "
13961 "supported: x%x\n", hrq->queue_id,
13962 db_offset);
13963 status = -EINVAL;
13964 goto out;
13965 }
13966 hrq->db_regaddr = bar_memmap_p + db_offset;
13967 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013968 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13969 "format:x%x\n", hrq->queue_id, pci_barset,
13970 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013971 } else {
13972 hrq->db_format = LPFC_DB_RING_FORMAT;
13973 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13974 }
James Smart4f774512009-05-22 14:52:35 -040013975 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013976 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013977 hrq->subtype = subtype;
13978 hrq->host_index = 0;
13979 hrq->hba_index = 0;
13980
13981 /* now create the data queue */
13982 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13983 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13984 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050013985 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13986 phba->sli4_hba.pc_sli4_params.rqv);
13987 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13988 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040013989 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050013990 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013991 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13992 LPFC_RQE_SIZE_8);
13993 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13994 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013995 } else {
13996 switch (drq->entry_count) {
13997 default:
13998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13999 "2536 Unsupported RQ count. (%d)\n",
14000 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014001 if (drq->entry_count < 512) {
14002 status = -EINVAL;
14003 goto out;
14004 }
James Smart5a6f1332011-03-11 16:05:35 -050014005 /* otherwise default to smallest count (drop through) */
14006 case 512:
14007 bf_set(lpfc_rq_context_rqe_count,
14008 &rq_create->u.request.context,
14009 LPFC_RQ_RING_SIZE_512);
14010 break;
14011 case 1024:
14012 bf_set(lpfc_rq_context_rqe_count,
14013 &rq_create->u.request.context,
14014 LPFC_RQ_RING_SIZE_1024);
14015 break;
14016 case 2048:
14017 bf_set(lpfc_rq_context_rqe_count,
14018 &rq_create->u.request.context,
14019 LPFC_RQ_RING_SIZE_2048);
14020 break;
14021 case 4096:
14022 bf_set(lpfc_rq_context_rqe_count,
14023 &rq_create->u.request.context,
14024 LPFC_RQ_RING_SIZE_4096);
14025 break;
14026 }
14027 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14028 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014029 }
14030 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14031 cq->queue_id);
14032 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14033 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014034 list_for_each_entry(dmabuf, &drq->page_list, list) {
14035 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14036 putPaddrLow(dmabuf->phys);
14037 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14038 putPaddrHigh(dmabuf->phys);
14039 }
James Smart962bc512013-01-03 15:44:00 -050014040 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14041 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014042 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14043 /* The IOCTL status is embedded in the mailbox subheader. */
14044 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14045 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14046 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14047 if (shdr_status || shdr_add_status || rc) {
14048 status = -ENXIO;
14049 goto out;
14050 }
14051 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14052 if (drq->queue_id == 0xFFFF) {
14053 status = -ENXIO;
14054 goto out;
14055 }
14056 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014057 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014058 drq->subtype = subtype;
14059 drq->host_index = 0;
14060 drq->hba_index = 0;
14061
14062 /* link the header and data RQs onto the parent cq child list */
14063 list_add_tail(&hrq->list, &cq->child_list);
14064 list_add_tail(&drq->list, &cq->child_list);
14065
14066out:
James Smart8fa38512009-07-19 10:01:03 -040014067 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014068 return status;
14069}
14070
14071/**
14072 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14073 * @eq: The queue structure associated with the queue to destroy.
14074 *
14075 * This function destroys a queue, as detailed in @eq by sending an mailbox
14076 * command, specific to the type of queue, to the HBA.
14077 *
14078 * The @eq struct is used to get the queue ID of the queue to destroy.
14079 *
14080 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014081 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014082 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014083int
James Smart4f774512009-05-22 14:52:35 -040014084lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14085{
14086 LPFC_MBOXQ_t *mbox;
14087 int rc, length, status = 0;
14088 uint32_t shdr_status, shdr_add_status;
14089 union lpfc_sli4_cfg_shdr *shdr;
14090
James Smart2e90f4b2011-12-13 13:22:37 -050014091 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014092 if (!eq)
14093 return -ENODEV;
14094 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14095 if (!mbox)
14096 return -ENOMEM;
14097 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14098 sizeof(struct lpfc_sli4_cfg_mhdr));
14099 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14100 LPFC_MBOX_OPCODE_EQ_DESTROY,
14101 length, LPFC_SLI4_MBX_EMBED);
14102 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14103 eq->queue_id);
14104 mbox->vport = eq->phba->pport;
14105 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14106
14107 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14108 /* The IOCTL status is embedded in the mailbox subheader. */
14109 shdr = (union lpfc_sli4_cfg_shdr *)
14110 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14111 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14112 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14113 if (shdr_status || shdr_add_status || rc) {
14114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14115 "2505 EQ_DESTROY mailbox failed with "
14116 "status x%x add_status x%x, mbx status x%x\n",
14117 shdr_status, shdr_add_status, rc);
14118 status = -ENXIO;
14119 }
14120
14121 /* Remove eq from any list */
14122 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014123 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014124 return status;
14125}
14126
14127/**
14128 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14129 * @cq: The queue structure associated with the queue to destroy.
14130 *
14131 * This function destroys a queue, as detailed in @cq by sending an mailbox
14132 * command, specific to the type of queue, to the HBA.
14133 *
14134 * The @cq struct is used to get the queue ID of the queue to destroy.
14135 *
14136 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014137 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014138 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014139int
James Smart4f774512009-05-22 14:52:35 -040014140lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14141{
14142 LPFC_MBOXQ_t *mbox;
14143 int rc, length, status = 0;
14144 uint32_t shdr_status, shdr_add_status;
14145 union lpfc_sli4_cfg_shdr *shdr;
14146
James Smart2e90f4b2011-12-13 13:22:37 -050014147 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014148 if (!cq)
14149 return -ENODEV;
14150 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14151 if (!mbox)
14152 return -ENOMEM;
14153 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14154 sizeof(struct lpfc_sli4_cfg_mhdr));
14155 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14156 LPFC_MBOX_OPCODE_CQ_DESTROY,
14157 length, LPFC_SLI4_MBX_EMBED);
14158 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14159 cq->queue_id);
14160 mbox->vport = cq->phba->pport;
14161 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14162 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14163 /* The IOCTL status is embedded in the mailbox subheader. */
14164 shdr = (union lpfc_sli4_cfg_shdr *)
14165 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14166 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14167 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14168 if (shdr_status || shdr_add_status || rc) {
14169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14170 "2506 CQ_DESTROY mailbox failed with "
14171 "status x%x add_status x%x, mbx status x%x\n",
14172 shdr_status, shdr_add_status, rc);
14173 status = -ENXIO;
14174 }
14175 /* Remove cq from any list */
14176 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014177 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014178 return status;
14179}
14180
14181/**
James Smart04c68492009-05-22 14:52:52 -040014182 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14183 * @qm: The queue structure associated with the queue to destroy.
14184 *
14185 * This function destroys a queue, as detailed in @mq by sending an mailbox
14186 * command, specific to the type of queue, to the HBA.
14187 *
14188 * The @mq struct is used to get the queue ID of the queue to destroy.
14189 *
14190 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014191 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014192 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014193int
James Smart04c68492009-05-22 14:52:52 -040014194lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14195{
14196 LPFC_MBOXQ_t *mbox;
14197 int rc, length, status = 0;
14198 uint32_t shdr_status, shdr_add_status;
14199 union lpfc_sli4_cfg_shdr *shdr;
14200
James Smart2e90f4b2011-12-13 13:22:37 -050014201 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014202 if (!mq)
14203 return -ENODEV;
14204 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14205 if (!mbox)
14206 return -ENOMEM;
14207 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14208 sizeof(struct lpfc_sli4_cfg_mhdr));
14209 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14210 LPFC_MBOX_OPCODE_MQ_DESTROY,
14211 length, LPFC_SLI4_MBX_EMBED);
14212 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14213 mq->queue_id);
14214 mbox->vport = mq->phba->pport;
14215 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14216 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14217 /* The IOCTL status is embedded in the mailbox subheader. */
14218 shdr = (union lpfc_sli4_cfg_shdr *)
14219 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14220 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14221 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14222 if (shdr_status || shdr_add_status || rc) {
14223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14224 "2507 MQ_DESTROY mailbox failed with "
14225 "status x%x add_status x%x, mbx status x%x\n",
14226 shdr_status, shdr_add_status, rc);
14227 status = -ENXIO;
14228 }
14229 /* Remove mq from any list */
14230 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014231 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014232 return status;
14233}
14234
14235/**
James Smart4f774512009-05-22 14:52:35 -040014236 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14237 * @wq: The queue structure associated with the queue to destroy.
14238 *
14239 * This function destroys a queue, as detailed in @wq by sending an mailbox
14240 * command, specific to the type of queue, to the HBA.
14241 *
14242 * The @wq struct is used to get the queue ID of the queue to destroy.
14243 *
14244 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014245 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014246 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014247int
James Smart4f774512009-05-22 14:52:35 -040014248lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14249{
14250 LPFC_MBOXQ_t *mbox;
14251 int rc, length, status = 0;
14252 uint32_t shdr_status, shdr_add_status;
14253 union lpfc_sli4_cfg_shdr *shdr;
14254
James Smart2e90f4b2011-12-13 13:22:37 -050014255 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014256 if (!wq)
14257 return -ENODEV;
14258 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14259 if (!mbox)
14260 return -ENOMEM;
14261 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14262 sizeof(struct lpfc_sli4_cfg_mhdr));
14263 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14264 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14265 length, LPFC_SLI4_MBX_EMBED);
14266 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14267 wq->queue_id);
14268 mbox->vport = wq->phba->pport;
14269 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14270 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14271 shdr = (union lpfc_sli4_cfg_shdr *)
14272 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14273 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14274 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14275 if (shdr_status || shdr_add_status || rc) {
14276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14277 "2508 WQ_DESTROY mailbox failed with "
14278 "status x%x add_status x%x, mbx status x%x\n",
14279 shdr_status, shdr_add_status, rc);
14280 status = -ENXIO;
14281 }
14282 /* Remove wq from any list */
14283 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014284 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014285 return status;
14286}
14287
14288/**
14289 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14290 * @rq: The queue structure associated with the queue to destroy.
14291 *
14292 * This function destroys a queue, as detailed in @rq by sending an mailbox
14293 * command, specific to the type of queue, to the HBA.
14294 *
14295 * The @rq struct is used to get the queue ID of the queue to destroy.
14296 *
14297 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014298 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014299 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014300int
James Smart4f774512009-05-22 14:52:35 -040014301lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14302 struct lpfc_queue *drq)
14303{
14304 LPFC_MBOXQ_t *mbox;
14305 int rc, length, status = 0;
14306 uint32_t shdr_status, shdr_add_status;
14307 union lpfc_sli4_cfg_shdr *shdr;
14308
James Smart2e90f4b2011-12-13 13:22:37 -050014309 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014310 if (!hrq || !drq)
14311 return -ENODEV;
14312 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14313 if (!mbox)
14314 return -ENOMEM;
14315 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014316 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014317 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14318 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14319 length, LPFC_SLI4_MBX_EMBED);
14320 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14321 hrq->queue_id);
14322 mbox->vport = hrq->phba->pport;
14323 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14324 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14325 /* The IOCTL status is embedded in the mailbox subheader. */
14326 shdr = (union lpfc_sli4_cfg_shdr *)
14327 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14328 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14329 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14330 if (shdr_status || shdr_add_status || rc) {
14331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14332 "2509 RQ_DESTROY mailbox failed with "
14333 "status x%x add_status x%x, mbx status x%x\n",
14334 shdr_status, shdr_add_status, rc);
14335 if (rc != MBX_TIMEOUT)
14336 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14337 return -ENXIO;
14338 }
14339 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14340 drq->queue_id);
14341 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14342 shdr = (union lpfc_sli4_cfg_shdr *)
14343 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14344 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14345 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14346 if (shdr_status || shdr_add_status || rc) {
14347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14348 "2510 RQ_DESTROY mailbox failed with "
14349 "status x%x add_status x%x, mbx status x%x\n",
14350 shdr_status, shdr_add_status, rc);
14351 status = -ENXIO;
14352 }
14353 list_del_init(&hrq->list);
14354 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014355 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014356 return status;
14357}
14358
14359/**
14360 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14361 * @phba: The virtual port for which this call being executed.
14362 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14363 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14364 * @xritag: the xritag that ties this io to the SGL pages.
14365 *
14366 * This routine will post the sgl pages for the IO that has the xritag
14367 * that is in the iocbq structure. The xritag is assigned during iocbq
14368 * creation and persists for as long as the driver is loaded.
14369 * if the caller has fewer than 256 scatter gather segments to map then
14370 * pdma_phys_addr1 should be 0.
14371 * If the caller needs to map more than 256 scatter gather segment then
14372 * pdma_phys_addr1 should be a valid physical address.
14373 * physical address for SGLs must be 64 byte aligned.
14374 * If you are going to map 2 SGL's then the first one must have 256 entries
14375 * the second sgl can have between 1 and 256 entries.
14376 *
14377 * Return codes:
14378 * 0 - Success
14379 * -ENXIO, -ENOMEM - Failure
14380 **/
14381int
14382lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14383 dma_addr_t pdma_phys_addr0,
14384 dma_addr_t pdma_phys_addr1,
14385 uint16_t xritag)
14386{
14387 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14388 LPFC_MBOXQ_t *mbox;
14389 int rc;
14390 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014391 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014392 union lpfc_sli4_cfg_shdr *shdr;
14393
14394 if (xritag == NO_XRI) {
14395 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14396 "0364 Invalid param:\n");
14397 return -EINVAL;
14398 }
14399
14400 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14401 if (!mbox)
14402 return -ENOMEM;
14403
14404 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14405 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14406 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014407 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014408
14409 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14410 &mbox->u.mqe.un.post_sgl_pages;
14411 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14412 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14413
14414 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14415 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14416 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14417 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14418
14419 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14420 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14421 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14422 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14423 if (!phba->sli4_hba.intr_enable)
14424 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014425 else {
James Smarta183a152011-10-10 21:32:43 -040014426 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014427 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14428 }
James Smart4f774512009-05-22 14:52:35 -040014429 /* The IOCTL status is embedded in the mailbox subheader. */
14430 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14431 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14432 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14433 if (rc != MBX_TIMEOUT)
14434 mempool_free(mbox, phba->mbox_mem_pool);
14435 if (shdr_status || shdr_add_status || rc) {
14436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14437 "2511 POST_SGL mailbox failed with "
14438 "status x%x add_status x%x, mbx status x%x\n",
14439 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014440 }
14441 return 0;
14442}
James Smart4f774512009-05-22 14:52:35 -040014443
14444/**
James Smart88a2cfb2011-07-22 18:36:33 -040014445 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014446 * @phba: pointer to lpfc hba data structure.
14447 *
14448 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014449 * HBA consistent with the SLI-4 interface spec. This routine
14450 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14451 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014452 *
James Smart88a2cfb2011-07-22 18:36:33 -040014453 * Returns
14454 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14455 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14456 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014457static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014458lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14459{
14460 unsigned long xri;
14461
14462 /*
14463 * Fetch the next logical xri. Because this index is logical,
14464 * the driver starts at 0 each time.
14465 */
14466 spin_lock_irq(&phba->hbalock);
14467 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14468 phba->sli4_hba.max_cfg_param.max_xri, 0);
14469 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14470 spin_unlock_irq(&phba->hbalock);
14471 return NO_XRI;
14472 } else {
14473 set_bit(xri, phba->sli4_hba.xri_bmask);
14474 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014475 }
James Smart6d368e52011-05-24 11:44:12 -040014476 spin_unlock_irq(&phba->hbalock);
14477 return xri;
14478}
14479
14480/**
14481 * lpfc_sli4_free_xri - Release an xri for reuse.
14482 * @phba: pointer to lpfc hba data structure.
14483 *
14484 * This routine is invoked to release an xri to the pool of
14485 * available rpis maintained by the driver.
14486 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014487static void
James Smart6d368e52011-05-24 11:44:12 -040014488__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14489{
14490 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014491 phba->sli4_hba.max_cfg_param.xri_used--;
14492 }
14493}
14494
14495/**
14496 * lpfc_sli4_free_xri - Release an xri for reuse.
14497 * @phba: pointer to lpfc hba data structure.
14498 *
14499 * This routine is invoked to release an xri to the pool of
14500 * available rpis maintained by the driver.
14501 **/
14502void
14503lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14504{
14505 spin_lock_irq(&phba->hbalock);
14506 __lpfc_sli4_free_xri(phba, xri);
14507 spin_unlock_irq(&phba->hbalock);
14508}
14509
14510/**
James Smart4f774512009-05-22 14:52:35 -040014511 * lpfc_sli4_next_xritag - Get an xritag for the io
14512 * @phba: Pointer to HBA context object.
14513 *
14514 * This function gets an xritag for the iocb. If there is no unused xritag
14515 * it will return 0xffff.
14516 * The function returns the allocated xritag if successful, else returns zero.
14517 * Zero is not a valid xritag.
14518 * The caller is not required to hold any lock.
14519 **/
14520uint16_t
14521lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14522{
James Smart6d368e52011-05-24 11:44:12 -040014523 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014524
James Smart6d368e52011-05-24 11:44:12 -040014525 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014526 if (xri_index == NO_XRI)
14527 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14528 "2004 Failed to allocate XRI.last XRITAG is %d"
14529 " Max XRI is %d, Used XRI is %d\n",
14530 xri_index,
14531 phba->sli4_hba.max_cfg_param.max_xri,
14532 phba->sli4_hba.max_cfg_param.xri_used);
14533 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014534}
14535
14536/**
James Smart6d368e52011-05-24 11:44:12 -040014537 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014538 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014539 * @post_sgl_list: pointer to els sgl entry list.
14540 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014541 *
14542 * This routine is invoked to post a block of driver's sgl pages to the
14543 * HBA using non-embedded mailbox command. No Lock is held. This routine
14544 * is only called when the driver is loading and after all IO has been
14545 * stopped.
14546 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014547static int
14548lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14549 struct list_head *post_sgl_list,
14550 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014551{
James Smart8a9d2e82012-05-09 21:16:12 -040014552 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014553 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14554 struct sgl_page_pairs *sgl_pg_pairs;
14555 void *viraddr;
14556 LPFC_MBOXQ_t *mbox;
14557 uint32_t reqlen, alloclen, pg_pairs;
14558 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014559 uint16_t xritag_start = 0;
14560 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014561 uint32_t shdr_status, shdr_add_status;
14562 union lpfc_sli4_cfg_shdr *shdr;
14563
James Smart8a9d2e82012-05-09 21:16:12 -040014564 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014565 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014566 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014567 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14568 "2559 Block sgl registration required DMA "
14569 "size (%d) great than a page\n", reqlen);
14570 return -ENOMEM;
14571 }
14572 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014573 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014574 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014575
14576 /* Allocate DMA memory and set up the non-embedded mailbox command */
14577 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14578 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14579 LPFC_SLI4_MBX_NEMBED);
14580
14581 if (alloclen < reqlen) {
14582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14583 "0285 Allocated DMA memory size (%d) is "
14584 "less than the requested DMA memory "
14585 "size (%d)\n", alloclen, reqlen);
14586 lpfc_sli4_mbox_cmd_free(phba, mbox);
14587 return -ENOMEM;
14588 }
James Smart4f774512009-05-22 14:52:35 -040014589 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014590 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014591 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14592 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14593
James Smart8a9d2e82012-05-09 21:16:12 -040014594 pg_pairs = 0;
14595 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014596 /* Set up the sge entry */
14597 sgl_pg_pairs->sgl_pg0_addr_lo =
14598 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14599 sgl_pg_pairs->sgl_pg0_addr_hi =
14600 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14601 sgl_pg_pairs->sgl_pg1_addr_lo =
14602 cpu_to_le32(putPaddrLow(0));
14603 sgl_pg_pairs->sgl_pg1_addr_hi =
14604 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014605
James Smart4f774512009-05-22 14:52:35 -040014606 /* Keep the first xritag on the list */
14607 if (pg_pairs == 0)
14608 xritag_start = sglq_entry->sli4_xritag;
14609 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014610 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014611 }
James Smart6d368e52011-05-24 11:44:12 -040014612
14613 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014614 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014615 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014616 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014617 if (!phba->sli4_hba.intr_enable)
14618 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14619 else {
James Smarta183a152011-10-10 21:32:43 -040014620 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014621 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14622 }
14623 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14626 if (rc != MBX_TIMEOUT)
14627 lpfc_sli4_mbox_cmd_free(phba, mbox);
14628 if (shdr_status || shdr_add_status || rc) {
14629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14630 "2513 POST_SGL_BLOCK mailbox command failed "
14631 "status x%x add_status x%x mbx status x%x\n",
14632 shdr_status, shdr_add_status, rc);
14633 rc = -ENXIO;
14634 }
14635 return rc;
14636}
14637
14638/**
14639 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14640 * @phba: pointer to lpfc hba data structure.
14641 * @sblist: pointer to scsi buffer list.
14642 * @count: number of scsi buffers on the list.
14643 *
14644 * This routine is invoked to post a block of @count scsi sgl pages from a
14645 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14646 * No Lock is held.
14647 *
14648 **/
14649int
James Smart8a9d2e82012-05-09 21:16:12 -040014650lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14651 struct list_head *sblist,
14652 int count)
James Smart4f774512009-05-22 14:52:35 -040014653{
14654 struct lpfc_scsi_buf *psb;
14655 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14656 struct sgl_page_pairs *sgl_pg_pairs;
14657 void *viraddr;
14658 LPFC_MBOXQ_t *mbox;
14659 uint32_t reqlen, alloclen, pg_pairs;
14660 uint32_t mbox_tmo;
14661 uint16_t xritag_start = 0;
14662 int rc = 0;
14663 uint32_t shdr_status, shdr_add_status;
14664 dma_addr_t pdma_phys_bpl1;
14665 union lpfc_sli4_cfg_shdr *shdr;
14666
14667 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014668 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014669 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014670 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014671 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14672 "0217 Block sgl registration required DMA "
14673 "size (%d) great than a page\n", reqlen);
14674 return -ENOMEM;
14675 }
14676 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14677 if (!mbox) {
14678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14679 "0283 Failed to allocate mbox cmd memory\n");
14680 return -ENOMEM;
14681 }
14682
14683 /* Allocate DMA memory and set up the non-embedded mailbox command */
14684 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14685 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14686 LPFC_SLI4_MBX_NEMBED);
14687
14688 if (alloclen < reqlen) {
14689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14690 "2561 Allocated DMA memory size (%d) is "
14691 "less than the requested DMA memory "
14692 "size (%d)\n", alloclen, reqlen);
14693 lpfc_sli4_mbox_cmd_free(phba, mbox);
14694 return -ENOMEM;
14695 }
James Smart6d368e52011-05-24 11:44:12 -040014696
James Smart4f774512009-05-22 14:52:35 -040014697 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014698 viraddr = mbox->sge_array->addr[0];
14699
14700 /* Set up the SGL pages in the non-embedded DMA pages */
14701 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14702 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14703
14704 pg_pairs = 0;
14705 list_for_each_entry(psb, sblist, list) {
14706 /* Set up the sge entry */
14707 sgl_pg_pairs->sgl_pg0_addr_lo =
14708 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14709 sgl_pg_pairs->sgl_pg0_addr_hi =
14710 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14711 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14712 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14713 else
14714 pdma_phys_bpl1 = 0;
14715 sgl_pg_pairs->sgl_pg1_addr_lo =
14716 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14717 sgl_pg_pairs->sgl_pg1_addr_hi =
14718 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14719 /* Keep the first xritag on the list */
14720 if (pg_pairs == 0)
14721 xritag_start = psb->cur_iocbq.sli4_xritag;
14722 sgl_pg_pairs++;
14723 pg_pairs++;
14724 }
14725 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14726 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14727 /* Perform endian conversion if necessary */
14728 sgl->word0 = cpu_to_le32(sgl->word0);
14729
14730 if (!phba->sli4_hba.intr_enable)
14731 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14732 else {
James Smarta183a152011-10-10 21:32:43 -040014733 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014734 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14735 }
14736 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14737 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14738 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14739 if (rc != MBX_TIMEOUT)
14740 lpfc_sli4_mbox_cmd_free(phba, mbox);
14741 if (shdr_status || shdr_add_status || rc) {
14742 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14743 "2564 POST_SGL_BLOCK mailbox command failed "
14744 "status x%x add_status x%x mbx status x%x\n",
14745 shdr_status, shdr_add_status, rc);
14746 rc = -ENXIO;
14747 }
14748 return rc;
14749}
14750
14751/**
14752 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14753 * @phba: pointer to lpfc_hba struct that the frame was received on
14754 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14755 *
14756 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14757 * valid type of frame that the LPFC driver will handle. This function will
14758 * return a zero if the frame is a valid frame or a non zero value when the
14759 * frame does not pass the check.
14760 **/
14761static int
14762lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14763{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014764 /* make rctl_names static to save stack space */
14765 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014766 char *type_names[] = FC_TYPE_NAMES_INIT;
14767 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014768 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014769
14770 switch (fc_hdr->fh_r_ctl) {
14771 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14772 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14773 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14774 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14775 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14776 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14777 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14778 case FC_RCTL_DD_CMD_STATUS: /* command status */
14779 case FC_RCTL_ELS_REQ: /* extended link services request */
14780 case FC_RCTL_ELS_REP: /* extended link services reply */
14781 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14782 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14783 case FC_RCTL_BA_NOP: /* basic link service NOP */
14784 case FC_RCTL_BA_ABTS: /* basic link service abort */
14785 case FC_RCTL_BA_RMC: /* remove connection */
14786 case FC_RCTL_BA_ACC: /* basic accept */
14787 case FC_RCTL_BA_RJT: /* basic reject */
14788 case FC_RCTL_BA_PRMT:
14789 case FC_RCTL_ACK_1: /* acknowledge_1 */
14790 case FC_RCTL_ACK_0: /* acknowledge_0 */
14791 case FC_RCTL_P_RJT: /* port reject */
14792 case FC_RCTL_F_RJT: /* fabric reject */
14793 case FC_RCTL_P_BSY: /* port busy */
14794 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14795 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14796 case FC_RCTL_LCR: /* link credit reset */
14797 case FC_RCTL_END: /* end */
14798 break;
14799 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14800 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14801 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14802 return lpfc_fc_frame_check(phba, fc_hdr);
14803 default:
14804 goto drop;
14805 }
14806 switch (fc_hdr->fh_type) {
14807 case FC_TYPE_BLS:
14808 case FC_TYPE_ELS:
14809 case FC_TYPE_FCP:
14810 case FC_TYPE_CT:
14811 break;
14812 case FC_TYPE_IP:
14813 case FC_TYPE_ILS:
14814 default:
14815 goto drop;
14816 }
James Smart546fc852011-03-11 16:06:29 -050014817
James Smart4f774512009-05-22 14:52:35 -040014818 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014819 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14820 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14821 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14822 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014823 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14824 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014825 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14826 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014827 return 0;
14828drop:
14829 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14830 "2539 Dropped frame rctl:%s type:%s\n",
14831 rctl_names[fc_hdr->fh_r_ctl],
14832 type_names[fc_hdr->fh_type]);
14833 return 1;
14834}
14835
14836/**
14837 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14838 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14839 *
14840 * This function processes the FC header to retrieve the VFI from the VF
14841 * header, if one exists. This function will return the VFI if one exists
14842 * or 0 if no VSAN Header exists.
14843 **/
14844static uint32_t
14845lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14846{
14847 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14848
14849 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14850 return 0;
14851 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14852}
14853
14854/**
14855 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14856 * @phba: Pointer to the HBA structure to search for the vport on
14857 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14858 * @fcfi: The FC Fabric ID that the frame came from
14859 *
14860 * This function searches the @phba for a vport that matches the content of the
14861 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14862 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14863 * returns the matching vport pointer or NULL if unable to match frame to a
14864 * vport.
14865 **/
14866static struct lpfc_vport *
14867lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14868 uint16_t fcfi)
14869{
14870 struct lpfc_vport **vports;
14871 struct lpfc_vport *vport = NULL;
14872 int i;
14873 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14874 fc_hdr->fh_d_id[1] << 8 |
14875 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014876
James Smartbf086112011-08-21 21:48:13 -040014877 if (did == Fabric_DID)
14878 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014879 if ((phba->pport->fc_flag & FC_PT2PT) &&
14880 !(phba->link_state == LPFC_HBA_READY))
14881 return phba->pport;
14882
James Smart4f774512009-05-22 14:52:35 -040014883 vports = lpfc_create_vport_work_array(phba);
14884 if (vports != NULL)
14885 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14886 if (phba->fcf.fcfi == fcfi &&
14887 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14888 vports[i]->fc_myDID == did) {
14889 vport = vports[i];
14890 break;
14891 }
14892 }
14893 lpfc_destroy_vport_work_array(phba, vports);
14894 return vport;
14895}
14896
14897/**
James Smart45ed1192009-10-02 15:17:02 -040014898 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14899 * @vport: The vport to work on.
14900 *
14901 * This function updates the receive sequence time stamp for this vport. The
14902 * receive sequence time stamp indicates the time that the last frame of the
14903 * the sequence that has been idle for the longest amount of time was received.
14904 * the driver uses this time stamp to indicate if any received sequences have
14905 * timed out.
14906 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014907static void
James Smart45ed1192009-10-02 15:17:02 -040014908lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14909{
14910 struct lpfc_dmabuf *h_buf;
14911 struct hbq_dmabuf *dmabuf = NULL;
14912
14913 /* get the oldest sequence on the rcv list */
14914 h_buf = list_get_first(&vport->rcv_buffer_list,
14915 struct lpfc_dmabuf, list);
14916 if (!h_buf)
14917 return;
14918 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14919 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14920}
14921
14922/**
14923 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14924 * @vport: The vport that the received sequences were sent to.
14925 *
14926 * This function cleans up all outstanding received sequences. This is called
14927 * by the driver when a link event or user action invalidates all the received
14928 * sequences.
14929 **/
14930void
14931lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14932{
14933 struct lpfc_dmabuf *h_buf, *hnext;
14934 struct lpfc_dmabuf *d_buf, *dnext;
14935 struct hbq_dmabuf *dmabuf = NULL;
14936
14937 /* start with the oldest sequence on the rcv list */
14938 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14939 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14940 list_del_init(&dmabuf->hbuf.list);
14941 list_for_each_entry_safe(d_buf, dnext,
14942 &dmabuf->dbuf.list, list) {
14943 list_del_init(&d_buf->list);
14944 lpfc_in_buf_free(vport->phba, d_buf);
14945 }
14946 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14947 }
14948}
14949
14950/**
14951 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14952 * @vport: The vport that the received sequences were sent to.
14953 *
14954 * This function determines whether any received sequences have timed out by
14955 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14956 * indicates that there is at least one timed out sequence this routine will
14957 * go through the received sequences one at a time from most inactive to most
14958 * active to determine which ones need to be cleaned up. Once it has determined
14959 * that a sequence needs to be cleaned up it will simply free up the resources
14960 * without sending an abort.
14961 **/
14962void
14963lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14964{
14965 struct lpfc_dmabuf *h_buf, *hnext;
14966 struct lpfc_dmabuf *d_buf, *dnext;
14967 struct hbq_dmabuf *dmabuf = NULL;
14968 unsigned long timeout;
14969 int abort_count = 0;
14970
14971 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14972 vport->rcv_buffer_time_stamp);
14973 if (list_empty(&vport->rcv_buffer_list) ||
14974 time_before(jiffies, timeout))
14975 return;
14976 /* start with the oldest sequence on the rcv list */
14977 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14978 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14979 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14980 dmabuf->time_stamp);
14981 if (time_before(jiffies, timeout))
14982 break;
14983 abort_count++;
14984 list_del_init(&dmabuf->hbuf.list);
14985 list_for_each_entry_safe(d_buf, dnext,
14986 &dmabuf->dbuf.list, list) {
14987 list_del_init(&d_buf->list);
14988 lpfc_in_buf_free(vport->phba, d_buf);
14989 }
14990 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14991 }
14992 if (abort_count)
14993 lpfc_update_rcv_time_stamp(vport);
14994}
14995
14996/**
James Smart4f774512009-05-22 14:52:35 -040014997 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14998 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14999 *
15000 * This function searches through the existing incomplete sequences that have
15001 * been sent to this @vport. If the frame matches one of the incomplete
15002 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15003 * make up that sequence. If no sequence is found that matches this frame then
15004 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15005 * This function returns a pointer to the first dmabuf in the sequence list that
15006 * the frame was linked to.
15007 **/
15008static struct hbq_dmabuf *
15009lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15010{
15011 struct fc_frame_header *new_hdr;
15012 struct fc_frame_header *temp_hdr;
15013 struct lpfc_dmabuf *d_buf;
15014 struct lpfc_dmabuf *h_buf;
15015 struct hbq_dmabuf *seq_dmabuf = NULL;
15016 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015017 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015018
James Smart4d9ab992009-10-02 15:16:39 -040015019 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015020 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015021 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015022
James Smart4f774512009-05-22 14:52:35 -040015023 /* Use the hdr_buf to find the sequence that this frame belongs to */
15024 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15025 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15026 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15027 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15028 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15029 continue;
15030 /* found a pending sequence that matches this frame */
15031 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15032 break;
15033 }
15034 if (!seq_dmabuf) {
15035 /*
15036 * This indicates first frame received for this sequence.
15037 * Queue the buffer on the vport's rcv_buffer_list.
15038 */
15039 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015040 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015041 return dmabuf;
15042 }
15043 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015044 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15045 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015046 list_del_init(&seq_dmabuf->hbuf.list);
15047 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15048 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015049 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015050 return dmabuf;
15051 }
James Smart45ed1192009-10-02 15:17:02 -040015052 /* move this sequence to the tail to indicate a young sequence */
15053 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15054 seq_dmabuf->time_stamp = jiffies;
15055 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015056 if (list_empty(&seq_dmabuf->dbuf.list)) {
15057 temp_hdr = dmabuf->hbuf.virt;
15058 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15059 return seq_dmabuf;
15060 }
James Smart4f774512009-05-22 14:52:35 -040015061 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015062 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15063 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015064 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15065 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15066 /*
15067 * If the frame's sequence count is greater than the frame on
15068 * the list then insert the frame right after this frame
15069 */
James Smarteeead812009-12-21 17:01:23 -050015070 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15071 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015072 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015073 found = 1;
15074 break;
James Smart4f774512009-05-22 14:52:35 -040015075 }
James Smart4360ca92015-12-16 18:12:04 -050015076
15077 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15078 break;
15079 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015080 }
James Smart4360ca92015-12-16 18:12:04 -050015081
15082 if (found)
15083 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015084 return NULL;
15085}
15086
15087/**
James Smart6669f9b2009-10-02 15:16:45 -040015088 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15089 * @vport: pointer to a vitural port
15090 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15091 *
15092 * This function tries to abort from the partially assembed sequence, described
15093 * by the information from basic abbort @dmabuf. It checks to see whether such
15094 * partially assembled sequence held by the driver. If so, it shall free up all
15095 * the frames from the partially assembled sequence.
15096 *
15097 * Return
15098 * true -- if there is matching partially assembled sequence present and all
15099 * the frames freed with the sequence;
15100 * false -- if there is no matching partially assembled sequence present so
15101 * nothing got aborted in the lower layer driver
15102 **/
15103static bool
15104lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15105 struct hbq_dmabuf *dmabuf)
15106{
15107 struct fc_frame_header *new_hdr;
15108 struct fc_frame_header *temp_hdr;
15109 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15110 struct hbq_dmabuf *seq_dmabuf = NULL;
15111
15112 /* Use the hdr_buf to find the sequence that matches this frame */
15113 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15114 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15115 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15116 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15117 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15118 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15119 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15120 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15121 continue;
15122 /* found a pending sequence that matches this frame */
15123 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15124 break;
15125 }
15126
15127 /* Free up all the frames from the partially assembled sequence */
15128 if (seq_dmabuf) {
15129 list_for_each_entry_safe(d_buf, n_buf,
15130 &seq_dmabuf->dbuf.list, list) {
15131 list_del_init(&d_buf->list);
15132 lpfc_in_buf_free(vport->phba, d_buf);
15133 }
15134 return true;
15135 }
15136 return false;
15137}
15138
15139/**
James Smart6dd9e312013-01-03 15:43:37 -050015140 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15141 * @vport: pointer to a vitural port
15142 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15143 *
15144 * This function tries to abort from the assembed sequence from upper level
15145 * protocol, described by the information from basic abbort @dmabuf. It
15146 * checks to see whether such pending context exists at upper level protocol.
15147 * If so, it shall clean up the pending context.
15148 *
15149 * Return
15150 * true -- if there is matching pending context of the sequence cleaned
15151 * at ulp;
15152 * false -- if there is no matching pending context of the sequence present
15153 * at ulp.
15154 **/
15155static bool
15156lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15157{
15158 struct lpfc_hba *phba = vport->phba;
15159 int handled;
15160
15161 /* Accepting abort at ulp with SLI4 only */
15162 if (phba->sli_rev < LPFC_SLI_REV4)
15163 return false;
15164
15165 /* Register all caring upper level protocols to attend abort */
15166 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15167 if (handled)
15168 return true;
15169
15170 return false;
15171}
15172
15173/**
James Smart546fc852011-03-11 16:06:29 -050015174 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015175 * @phba: Pointer to HBA context object.
15176 * @cmd_iocbq: pointer to the command iocbq structure.
15177 * @rsp_iocbq: pointer to the response iocbq structure.
15178 *
James Smart546fc852011-03-11 16:06:29 -050015179 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015180 * event. It properly releases the memory allocated to the sequence abort
15181 * accept iocb.
15182 **/
15183static void
James Smart546fc852011-03-11 16:06:29 -050015184lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015185 struct lpfc_iocbq *cmd_iocbq,
15186 struct lpfc_iocbq *rsp_iocbq)
15187{
James Smart6dd9e312013-01-03 15:43:37 -050015188 struct lpfc_nodelist *ndlp;
15189
15190 if (cmd_iocbq) {
15191 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15192 lpfc_nlp_put(ndlp);
15193 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015194 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015195 }
James Smart6b5151f2012-01-18 16:24:06 -050015196
15197 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15198 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15199 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15200 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15201 rsp_iocbq->iocb.ulpStatus,
15202 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015203}
15204
15205/**
James Smart6d368e52011-05-24 11:44:12 -040015206 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15207 * @phba: Pointer to HBA context object.
15208 * @xri: xri id in transaction.
15209 *
15210 * This function validates the xri maps to the known range of XRIs allocated an
15211 * used by the driver.
15212 **/
James Smart7851fe22011-07-22 18:36:52 -040015213uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015214lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15215 uint16_t xri)
15216{
James Smarta2fc4aef2014-09-03 12:57:55 -040015217 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015218
15219 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15220 if (xri == phba->sli4_hba.xri_ids[i])
15221 return i;
15222 }
15223 return NO_XRI;
15224}
15225
James Smart6d368e52011-05-24 11:44:12 -040015226/**
James Smart546fc852011-03-11 16:06:29 -050015227 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015228 * @phba: Pointer to HBA context object.
15229 * @fc_hdr: pointer to a FC frame header.
15230 *
James Smart546fc852011-03-11 16:06:29 -050015231 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015232 * event after aborting the sequence handling.
15233 **/
15234static void
James Smart6dd9e312013-01-03 15:43:37 -050015235lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15236 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015237{
James Smart6dd9e312013-01-03 15:43:37 -050015238 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015239 struct lpfc_iocbq *ctiocb = NULL;
15240 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015241 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015242 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015243 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015244 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015245
15246 if (!lpfc_is_link_up(phba))
15247 return;
15248
15249 sid = sli4_sid_from_fc_hdr(fc_hdr);
15250 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015251 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015252
James Smart6dd9e312013-01-03 15:43:37 -050015253 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015254 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015255 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15256 if (!ndlp) {
15257 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15258 "1268 Failed to allocate ndlp for "
15259 "oxid:x%x SID:x%x\n", oxid, sid);
15260 return;
15261 }
15262 lpfc_nlp_init(vport, ndlp, sid);
15263 /* Put ndlp onto pport node list */
15264 lpfc_enqueue_node(vport, ndlp);
15265 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15266 /* re-setup ndlp without removing from node list */
15267 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15268 if (!ndlp) {
15269 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15270 "3275 Failed to active ndlp found "
15271 "for oxid:x%x SID:x%x\n", oxid, sid);
15272 return;
15273 }
James Smart6669f9b2009-10-02 15:16:45 -040015274 }
15275
James Smart546fc852011-03-11 16:06:29 -050015276 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015277 ctiocb = lpfc_sli_get_iocbq(phba);
15278 if (!ctiocb)
15279 return;
15280
James Smart5ffc2662009-11-18 15:39:44 -050015281 /* Extract the F_CTL field from FC_HDR */
15282 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15283
James Smart6669f9b2009-10-02 15:16:45 -040015284 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015285 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015286 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015287 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15288 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15289 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15290
15291 /* Fill in the rest of iocb fields */
15292 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15293 icmd->ulpBdeCount = 0;
15294 icmd->ulpLe = 1;
15295 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015296 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015297 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015298
James Smart6669f9b2009-10-02 15:16:45 -040015299 ctiocb->iocb_cmpl = NULL;
15300 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015301 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015302 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015303 ctiocb->sli4_xritag = NO_XRI;
15304
James Smartee0f4fe2012-05-09 21:19:14 -040015305 if (fctl & FC_FC_EX_CTX)
15306 /* Exchange responder sent the abort so we
15307 * own the oxid.
15308 */
15309 xri = oxid;
15310 else
15311 xri = rxid;
15312 lxri = lpfc_sli4_xri_inrange(phba, xri);
15313 if (lxri != NO_XRI)
15314 lpfc_set_rrq_active(phba, ndlp, lxri,
15315 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015316 /* For BA_ABTS from exchange responder, if the logical xri with
15317 * the oxid maps to the FCP XRI range, the port no longer has
15318 * that exchange context, send a BLS_RJT. Override the IOCB for
15319 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015320 */
James Smart6dd9e312013-01-03 15:43:37 -050015321 if ((fctl & FC_FC_EX_CTX) &&
15322 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15323 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15324 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15325 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15326 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15327 }
15328
15329 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15330 * the driver no longer has that exchange, send a BLS_RJT. Override
15331 * the IOCB for a BA_RJT.
15332 */
15333 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015334 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15335 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15336 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15337 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15338 }
James Smart6669f9b2009-10-02 15:16:45 -040015339
James Smart5ffc2662009-11-18 15:39:44 -050015340 if (fctl & FC_FC_EX_CTX) {
15341 /* ABTS sent by responder to CT exchange, construction
15342 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15343 * field and RX_ID from ABTS for RX_ID field.
15344 */
James Smart546fc852011-03-11 16:06:29 -050015345 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015346 } else {
15347 /* ABTS sent by initiator to CT exchange, construction
15348 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015349 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015350 */
James Smart546fc852011-03-11 16:06:29 -050015351 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015352 }
James Smartf09c3ac2012-03-01 22:33:29 -050015353 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015354 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015355
James Smart546fc852011-03-11 16:06:29 -050015356 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015357 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15358 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15359 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015360
15361 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15362 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015363 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15364 "2925 Failed to issue CT ABTS RSP x%x on "
15365 "xri x%x, Data x%x\n",
15366 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15367 phba->link_state);
15368 lpfc_nlp_put(ndlp);
15369 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015370 lpfc_sli_release_iocbq(phba, ctiocb);
15371 }
James Smart6669f9b2009-10-02 15:16:45 -040015372}
15373
15374/**
15375 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15376 * @vport: Pointer to the vport on which this sequence was received
15377 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15378 *
15379 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15380 * receive sequence is only partially assembed by the driver, it shall abort
15381 * the partially assembled frames for the sequence. Otherwise, if the
15382 * unsolicited receive sequence has been completely assembled and passed to
15383 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15384 * unsolicited sequence has been aborted. After that, it will issue a basic
15385 * accept to accept the abort.
15386 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015387static void
James Smart6669f9b2009-10-02 15:16:45 -040015388lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15389 struct hbq_dmabuf *dmabuf)
15390{
15391 struct lpfc_hba *phba = vport->phba;
15392 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015393 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015394 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015395
James Smart6669f9b2009-10-02 15:16:45 -040015396 /* Make a copy of fc_hdr before the dmabuf being released */
15397 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015398 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015399
James Smart5ffc2662009-11-18 15:39:44 -050015400 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015401 /* ABTS by responder to exchange, no cleanup needed */
15402 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015403 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015404 /* ABTS by initiator to exchange, need to do cleanup */
15405 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15406 if (aborted == false)
15407 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015408 }
James Smart6dd9e312013-01-03 15:43:37 -050015409 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15410
15411 /* Respond with BA_ACC or BA_RJT accordingly */
15412 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015413}
15414
15415/**
James Smart4f774512009-05-22 14:52:35 -040015416 * lpfc_seq_complete - Indicates if a sequence is complete
15417 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15418 *
15419 * This function checks the sequence, starting with the frame described by
15420 * @dmabuf, to see if all the frames associated with this sequence are present.
15421 * the frames associated with this sequence are linked to the @dmabuf using the
15422 * dbuf list. This function looks for two major things. 1) That the first frame
15423 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15424 * set. 3) That there are no holes in the sequence count. The function will
15425 * return 1 when the sequence is complete, otherwise it will return 0.
15426 **/
15427static int
15428lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15429{
15430 struct fc_frame_header *hdr;
15431 struct lpfc_dmabuf *d_buf;
15432 struct hbq_dmabuf *seq_dmabuf;
15433 uint32_t fctl;
15434 int seq_count = 0;
15435
15436 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15437 /* make sure first fame of sequence has a sequence count of zero */
15438 if (hdr->fh_seq_cnt != seq_count)
15439 return 0;
15440 fctl = (hdr->fh_f_ctl[0] << 16 |
15441 hdr->fh_f_ctl[1] << 8 |
15442 hdr->fh_f_ctl[2]);
15443 /* If last frame of sequence we can return success. */
15444 if (fctl & FC_FC_END_SEQ)
15445 return 1;
15446 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15447 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15448 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15449 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015450 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015451 return 0;
15452 fctl = (hdr->fh_f_ctl[0] << 16 |
15453 hdr->fh_f_ctl[1] << 8 |
15454 hdr->fh_f_ctl[2]);
15455 /* If last frame of sequence we can return success. */
15456 if (fctl & FC_FC_END_SEQ)
15457 return 1;
15458 }
15459 return 0;
15460}
15461
15462/**
15463 * lpfc_prep_seq - Prep sequence for ULP processing
15464 * @vport: Pointer to the vport on which this sequence was received
15465 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15466 *
15467 * This function takes a sequence, described by a list of frames, and creates
15468 * a list of iocbq structures to describe the sequence. This iocbq list will be
15469 * used to issue to the generic unsolicited sequence handler. This routine
15470 * returns a pointer to the first iocbq in the list. If the function is unable
15471 * to allocate an iocbq then it throw out the received frames that were not
15472 * able to be described and return a pointer to the first iocbq. If unable to
15473 * allocate any iocbqs (including the first) this function will return NULL.
15474 **/
15475static struct lpfc_iocbq *
15476lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15477{
James Smart7851fe22011-07-22 18:36:52 -040015478 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015479 struct lpfc_dmabuf *d_buf, *n_buf;
15480 struct lpfc_iocbq *first_iocbq, *iocbq;
15481 struct fc_frame_header *fc_hdr;
15482 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015483 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015484 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015485
15486 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15487 /* remove from receive buffer list */
15488 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015489 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015490 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015491 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015492 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015493 /* Get an iocbq struct to fill in. */
15494 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15495 if (first_iocbq) {
15496 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015497 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015498 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015499
15500 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15501 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15502 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15503 first_iocbq->iocb.un.rcvels.parmRo =
15504 sli4_did_from_fc_hdr(fc_hdr);
15505 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15506 } else
15507 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015508 first_iocbq->iocb.ulpContext = NO_XRI;
15509 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15510 be16_to_cpu(fc_hdr->fh_ox_id);
15511 /* iocbq is prepped for internal consumption. Physical vpi. */
15512 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15513 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015514 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015515 tot_len = bf_get(lpfc_rcqe_length,
15516 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15517
James Smart4f774512009-05-22 14:52:35 -040015518 first_iocbq->context2 = &seq_dmabuf->dbuf;
15519 first_iocbq->context3 = NULL;
15520 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015521 if (tot_len > LPFC_DATA_BUF_SIZE)
15522 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015523 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015524 else
15525 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15526
James Smart4f774512009-05-22 14:52:35 -040015527 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015528
James Smart7851fe22011-07-22 18:36:52 -040015529 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015530 }
15531 iocbq = first_iocbq;
15532 /*
15533 * Each IOCBq can have two Buffers assigned, so go through the list
15534 * of buffers for this sequence and save two buffers in each IOCBq
15535 */
15536 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15537 if (!iocbq) {
15538 lpfc_in_buf_free(vport->phba, d_buf);
15539 continue;
15540 }
15541 if (!iocbq->context3) {
15542 iocbq->context3 = d_buf;
15543 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015544 /* We need to get the size out of the right CQE */
15545 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15546 len = bf_get(lpfc_rcqe_length,
15547 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015548 pbde = (struct ulp_bde64 *)
15549 &iocbq->iocb.unsli3.sli3Words[4];
15550 if (len > LPFC_DATA_BUF_SIZE)
15551 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15552 else
15553 pbde->tus.f.bdeSize = len;
15554
James Smart7851fe22011-07-22 18:36:52 -040015555 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15556 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015557 } else {
15558 iocbq = lpfc_sli_get_iocbq(vport->phba);
15559 if (!iocbq) {
15560 if (first_iocbq) {
15561 first_iocbq->iocb.ulpStatus =
15562 IOSTAT_FCP_RSP_ERROR;
15563 first_iocbq->iocb.un.ulpWord[4] =
15564 IOERR_NO_RESOURCES;
15565 }
15566 lpfc_in_buf_free(vport->phba, d_buf);
15567 continue;
15568 }
James Smart7851fe22011-07-22 18:36:52 -040015569 /* We need to get the size out of the right CQE */
15570 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15571 len = bf_get(lpfc_rcqe_length,
15572 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015573 iocbq->context2 = d_buf;
15574 iocbq->context3 = NULL;
15575 iocbq->iocb.ulpBdeCount = 1;
15576 if (len > LPFC_DATA_BUF_SIZE)
15577 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15578 LPFC_DATA_BUF_SIZE;
15579 else
15580 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15581
James Smart7851fe22011-07-22 18:36:52 -040015582 tot_len += len;
15583 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15584
James Smart4f774512009-05-22 14:52:35 -040015585 iocbq->iocb.un.rcvels.remoteID = sid;
15586 list_add_tail(&iocbq->list, &first_iocbq->list);
15587 }
15588 }
15589 return first_iocbq;
15590}
15591
James Smart6669f9b2009-10-02 15:16:45 -040015592static void
15593lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15594 struct hbq_dmabuf *seq_dmabuf)
15595{
15596 struct fc_frame_header *fc_hdr;
15597 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15598 struct lpfc_hba *phba = vport->phba;
15599
15600 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15601 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15602 if (!iocbq) {
15603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15604 "2707 Ring %d handler: Failed to allocate "
15605 "iocb Rctl x%x Type x%x received\n",
15606 LPFC_ELS_RING,
15607 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15608 return;
15609 }
15610 if (!lpfc_complete_unsol_iocb(phba,
15611 &phba->sli.ring[LPFC_ELS_RING],
15612 iocbq, fc_hdr->fh_r_ctl,
15613 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015614 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015615 "2540 Ring %d handler: unexpected Rctl "
15616 "x%x Type x%x received\n",
15617 LPFC_ELS_RING,
15618 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15619
15620 /* Free iocb created in lpfc_prep_seq */
15621 list_for_each_entry_safe(curr_iocb, next_iocb,
15622 &iocbq->list, list) {
15623 list_del_init(&curr_iocb->list);
15624 lpfc_sli_release_iocbq(phba, curr_iocb);
15625 }
15626 lpfc_sli_release_iocbq(phba, iocbq);
15627}
15628
James Smart4f774512009-05-22 14:52:35 -040015629/**
15630 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15631 * @phba: Pointer to HBA context object.
15632 *
15633 * This function is called with no lock held. This function processes all
15634 * the received buffers and gives it to upper layers when a received buffer
15635 * indicates that it is the final frame in the sequence. The interrupt
15636 * service routine processes received buffers at interrupt contexts and adds
15637 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15638 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15639 * appropriate receive function when the final frame in a sequence is received.
15640 **/
James Smart4d9ab992009-10-02 15:16:39 -040015641void
15642lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15643 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015644{
James Smart4d9ab992009-10-02 15:16:39 -040015645 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015646 struct fc_frame_header *fc_hdr;
15647 struct lpfc_vport *vport;
15648 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015649 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015650
James Smart4f774512009-05-22 14:52:35 -040015651 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015652 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15653 /* check to see if this a valid type of frame */
15654 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15655 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15656 return;
15657 }
James Smart7851fe22011-07-22 18:36:52 -040015658 if ((bf_get(lpfc_cqe_code,
15659 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15660 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15661 &dmabuf->cq_event.cqe.rcqe_cmpl);
15662 else
15663 fcfi = bf_get(lpfc_rcqe_fcf_id,
15664 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015665
James Smart4d9ab992009-10-02 15:16:39 -040015666 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015667 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015668 /* throw out the frame */
15669 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15670 return;
15671 }
James Smart939723a2012-05-09 21:19:03 -040015672
15673 /* d_id this frame is directed to */
15674 did = sli4_did_from_fc_hdr(fc_hdr);
15675
15676 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15677 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15678 (did != Fabric_DID)) {
15679 /*
15680 * Throw out the frame if we are not pt2pt.
15681 * The pt2pt protocol allows for discovery frames
15682 * to be received without a registered VPI.
15683 */
15684 if (!(vport->fc_flag & FC_PT2PT) ||
15685 (phba->link_state == LPFC_HBA_READY)) {
15686 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15687 return;
15688 }
15689 }
15690
James Smart6669f9b2009-10-02 15:16:45 -040015691 /* Handle the basic abort sequence (BA_ABTS) event */
15692 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15693 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15694 return;
15695 }
15696
James Smart4d9ab992009-10-02 15:16:39 -040015697 /* Link this frame */
15698 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15699 if (!seq_dmabuf) {
15700 /* unable to add frame to vport - throw it out */
15701 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15702 return;
15703 }
15704 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015705 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015706 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015707
James Smart6669f9b2009-10-02 15:16:45 -040015708 /* Send the complete sequence to the upper layer protocol */
15709 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015710}
James Smart6fb120a2009-05-22 14:52:59 -040015711
15712/**
15713 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15714 * @phba: pointer to lpfc hba data structure.
15715 *
15716 * This routine is invoked to post rpi header templates to the
15717 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015718 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15719 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015720 *
15721 * This routine does not require any locks. It's usage is expected
15722 * to be driver load or reset recovery when the driver is
15723 * sequential.
15724 *
15725 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015726 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015727 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015728 * When this error occurs, the driver is not guaranteed
15729 * to have any rpi regions posted to the device and
15730 * must either attempt to repost the regions or take a
15731 * fatal error.
15732 **/
15733int
15734lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15735{
15736 struct lpfc_rpi_hdr *rpi_page;
15737 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015738 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015739
James Smart6d368e52011-05-24 11:44:12 -040015740 /* SLI4 ports that support extents do not require RPI headers. */
15741 if (!phba->sli4_hba.rpi_hdrs_in_use)
15742 goto exit;
15743 if (phba->sli4_hba.extents_in_use)
15744 return -EIO;
15745
James Smart6fb120a2009-05-22 14:52:59 -040015746 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015747 /*
15748 * Assign the rpi headers a physical rpi only if the driver
15749 * has not initialized those resources. A port reset only
15750 * needs the headers posted.
15751 */
15752 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15753 LPFC_RPI_RSRC_RDY)
15754 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15755
James Smart6fb120a2009-05-22 14:52:59 -040015756 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15757 if (rc != MBX_SUCCESS) {
15758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15759 "2008 Error %d posting all rpi "
15760 "headers\n", rc);
15761 rc = -EIO;
15762 break;
15763 }
15764 }
15765
James Smart6d368e52011-05-24 11:44:12 -040015766 exit:
15767 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15768 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015769 return rc;
15770}
15771
15772/**
15773 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15774 * @phba: pointer to lpfc hba data structure.
15775 * @rpi_page: pointer to the rpi memory region.
15776 *
15777 * This routine is invoked to post a single rpi header to the
15778 * HBA consistent with the SLI-4 interface spec. This memory region
15779 * maps up to 64 rpi context regions.
15780 *
15781 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015782 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015783 * -ENOMEM - No available memory
15784 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015785 **/
15786int
15787lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15788{
15789 LPFC_MBOXQ_t *mboxq;
15790 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15791 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015792 uint32_t shdr_status, shdr_add_status;
15793 union lpfc_sli4_cfg_shdr *shdr;
15794
James Smart6d368e52011-05-24 11:44:12 -040015795 /* SLI4 ports that support extents do not require RPI headers. */
15796 if (!phba->sli4_hba.rpi_hdrs_in_use)
15797 return rc;
15798 if (phba->sli4_hba.extents_in_use)
15799 return -EIO;
15800
James Smart6fb120a2009-05-22 14:52:59 -040015801 /* The port is notified of the header region via a mailbox command. */
15802 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15803 if (!mboxq) {
15804 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15805 "2001 Unable to allocate memory for issuing "
15806 "SLI_CONFIG_SPECIAL mailbox command\n");
15807 return -ENOMEM;
15808 }
15809
15810 /* Post all rpi memory regions to the port. */
15811 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015812 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15813 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15814 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015815 sizeof(struct lpfc_sli4_cfg_mhdr),
15816 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015817
15818
15819 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015820 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15821 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015822 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15823 hdr_tmpl, rpi_page->page_count);
15824
James Smart6fb120a2009-05-22 14:52:59 -040015825 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15826 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015827 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015828 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15829 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15830 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15831 if (rc != MBX_TIMEOUT)
15832 mempool_free(mboxq, phba->mbox_mem_pool);
15833 if (shdr_status || shdr_add_status || rc) {
15834 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15835 "2514 POST_RPI_HDR mailbox failed with "
15836 "status x%x add_status x%x, mbx status x%x\n",
15837 shdr_status, shdr_add_status, rc);
15838 rc = -ENXIO;
15839 }
15840 return rc;
15841}
15842
15843/**
15844 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15845 * @phba: pointer to lpfc hba data structure.
15846 *
15847 * This routine is invoked to post rpi header templates to the
15848 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015849 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15850 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015851 *
15852 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015853 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015854 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15855 **/
15856int
15857lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15858{
James Smart6d368e52011-05-24 11:44:12 -040015859 unsigned long rpi;
15860 uint16_t max_rpi, rpi_limit;
15861 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015862 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015863 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015864
James Smart6fb120a2009-05-22 14:52:59 -040015865 /*
James Smart6d368e52011-05-24 11:44:12 -040015866 * Fetch the next logical rpi. Because this index is logical,
15867 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015868 */
James Smart4902b382013-10-10 12:20:35 -040015869 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015870 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15871 rpi_limit = phba->sli4_hba.next_rpi;
15872
James Smart6d368e52011-05-24 11:44:12 -040015873 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15874 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015875 rpi = LPFC_RPI_ALLOC_ERROR;
15876 else {
15877 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15878 phba->sli4_hba.max_cfg_param.rpi_used++;
15879 phba->sli4_hba.rpi_count++;
15880 }
James Smartbe6bb942015-04-07 15:07:22 -040015881 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15882 "0001 rpi:%x max:%x lim:%x\n",
15883 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015884
15885 /*
15886 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015887 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015888 */
15889 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15890 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015891 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015892 return rpi;
15893 }
15894
15895 /*
James Smart6d368e52011-05-24 11:44:12 -040015896 * RPI header postings are not required for SLI4 ports capable of
15897 * extents.
15898 */
15899 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015900 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015901 return rpi;
15902 }
15903
15904 /*
James Smart6fb120a2009-05-22 14:52:59 -040015905 * If the driver is running low on rpi resources, allocate another
15906 * page now. Note that the next_rpi value is used because
15907 * it represents how many are actually in use whereas max_rpi notes
15908 * how many are supported max by the device.
15909 */
James Smart6d368e52011-05-24 11:44:12 -040015910 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015911 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015912 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15913 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15914 if (!rpi_hdr) {
15915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15916 "2002 Error Could not grow rpi "
15917 "count\n");
15918 } else {
James Smart6d368e52011-05-24 11:44:12 -040015919 lrpi = rpi_hdr->start_rpi;
15920 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015921 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15922 }
15923 }
15924
15925 return rpi;
15926}
15927
15928/**
15929 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15930 * @phba: pointer to lpfc hba data structure.
15931 *
15932 * This routine is invoked to release an rpi to the pool of
15933 * available rpis maintained by the driver.
15934 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015935static void
James Smartd7c47992010-06-08 18:31:54 -040015936__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15937{
15938 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15939 phba->sli4_hba.rpi_count--;
15940 phba->sli4_hba.max_cfg_param.rpi_used--;
15941 }
15942}
15943
15944/**
15945 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15946 * @phba: pointer to lpfc hba data structure.
15947 *
15948 * This routine is invoked to release an rpi to the pool of
15949 * available rpis maintained by the driver.
15950 **/
15951void
James Smart6fb120a2009-05-22 14:52:59 -040015952lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15953{
15954 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015955 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015956 spin_unlock_irq(&phba->hbalock);
15957}
15958
15959/**
15960 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15961 * @phba: pointer to lpfc hba data structure.
15962 *
15963 * This routine is invoked to remove the memory region that
15964 * provided rpi via a bitmask.
15965 **/
15966void
15967lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15968{
15969 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040015970 kfree(phba->sli4_hba.rpi_ids);
15971 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040015972}
15973
15974/**
15975 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15976 * @phba: pointer to lpfc hba data structure.
15977 *
15978 * This routine is invoked to remove the memory region that
15979 * provided rpi via a bitmask.
15980 **/
15981int
James Smart6b5151f2012-01-18 16:24:06 -050015982lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15983 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040015984{
15985 LPFC_MBOXQ_t *mboxq;
15986 struct lpfc_hba *phba = ndlp->phba;
15987 int rc;
15988
15989 /* The port is notified of the header region via a mailbox command. */
15990 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15991 if (!mboxq)
15992 return -ENOMEM;
15993
15994 /* Post all rpi memory regions to the port. */
15995 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050015996 if (cmpl) {
15997 mboxq->mbox_cmpl = cmpl;
15998 mboxq->context1 = arg;
15999 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016000 } else
16001 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016002 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016003 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16004 if (rc == MBX_NOT_FINISHED) {
16005 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16006 "2010 Resume RPI Mailbox failed "
16007 "status %d, mbxStatus x%x\n", rc,
16008 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16009 mempool_free(mboxq, phba->mbox_mem_pool);
16010 return -EIO;
16011 }
16012 return 0;
16013}
16014
16015/**
16016 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016017 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016018 *
James Smart76a95d72010-11-20 23:11:48 -050016019 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016020 *
16021 * Returns:
16022 * 0 success
16023 * -Evalue otherwise
16024 **/
16025int
James Smart76a95d72010-11-20 23:11:48 -050016026lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016027{
16028 LPFC_MBOXQ_t *mboxq;
16029 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016030 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016031 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016032 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016033 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16034 if (!mboxq)
16035 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016036 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016037 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016038 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016039 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016040 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016041 "2022 INIT VPI Mailbox failed "
16042 "status %d, mbxStatus x%x\n", rc,
16043 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016044 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016045 }
James Smart6a9c52c2009-10-02 15:16:51 -040016046 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016047 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016048
16049 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016050}
16051
16052/**
16053 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16054 * @phba: pointer to lpfc hba data structure.
16055 * @mboxq: Pointer to mailbox object.
16056 *
16057 * This routine is invoked to manually add a single FCF record. The caller
16058 * must pass a completely initialized FCF_Record. This routine takes
16059 * care of the nonembedded mailbox operations.
16060 **/
16061static void
16062lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16063{
16064 void *virt_addr;
16065 union lpfc_sli4_cfg_shdr *shdr;
16066 uint32_t shdr_status, shdr_add_status;
16067
16068 virt_addr = mboxq->sge_array->addr[0];
16069 /* The IOCTL status is embedded in the mailbox subheader. */
16070 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16071 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16072 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16073
16074 if ((shdr_status || shdr_add_status) &&
16075 (shdr_status != STATUS_FCF_IN_USE))
16076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16077 "2558 ADD_FCF_RECORD mailbox failed with "
16078 "status x%x add_status x%x\n",
16079 shdr_status, shdr_add_status);
16080
16081 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16082}
16083
16084/**
16085 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16086 * @phba: pointer to lpfc hba data structure.
16087 * @fcf_record: pointer to the initialized fcf record to add.
16088 *
16089 * This routine is invoked to manually add a single FCF record. The caller
16090 * must pass a completely initialized FCF_Record. This routine takes
16091 * care of the nonembedded mailbox operations.
16092 **/
16093int
16094lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16095{
16096 int rc = 0;
16097 LPFC_MBOXQ_t *mboxq;
16098 uint8_t *bytep;
16099 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016100 struct lpfc_mbx_sge sge;
16101 uint32_t alloc_len, req_len;
16102 uint32_t fcfindex;
16103
16104 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16105 if (!mboxq) {
16106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16107 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16108 return -ENOMEM;
16109 }
16110
16111 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16112 sizeof(uint32_t);
16113
16114 /* Allocate DMA memory and set up the non-embedded mailbox command */
16115 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16116 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16117 req_len, LPFC_SLI4_MBX_NEMBED);
16118 if (alloc_len < req_len) {
16119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16120 "2523 Allocated DMA memory size (x%x) is "
16121 "less than the requested DMA memory "
16122 "size (x%x)\n", alloc_len, req_len);
16123 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16124 return -ENOMEM;
16125 }
16126
16127 /*
16128 * Get the first SGE entry from the non-embedded DMA memory. This
16129 * routine only uses a single SGE.
16130 */
16131 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016132 virt_addr = mboxq->sge_array->addr[0];
16133 /*
16134 * Configure the FCF record for FCFI 0. This is the driver's
16135 * hardcoded default and gets used in nonFIP mode.
16136 */
16137 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16138 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16139 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16140
16141 /*
16142 * Copy the fcf_index and the FCF Record Data. The data starts after
16143 * the FCoE header plus word10. The data copy needs to be endian
16144 * correct.
16145 */
16146 bytep += sizeof(uint32_t);
16147 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16148 mboxq->vport = phba->pport;
16149 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16150 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16151 if (rc == MBX_NOT_FINISHED) {
16152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16153 "2515 ADD_FCF_RECORD mailbox failed with "
16154 "status 0x%x\n", rc);
16155 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16156 rc = -EIO;
16157 } else
16158 rc = 0;
16159
16160 return rc;
16161}
16162
16163/**
16164 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16165 * @phba: pointer to lpfc hba data structure.
16166 * @fcf_record: pointer to the fcf record to write the default data.
16167 * @fcf_index: FCF table entry index.
16168 *
16169 * This routine is invoked to build the driver's default FCF record. The
16170 * values used are hardcoded. This routine handles memory initialization.
16171 *
16172 **/
16173void
16174lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16175 struct fcf_record *fcf_record,
16176 uint16_t fcf_index)
16177{
16178 memset(fcf_record, 0, sizeof(struct fcf_record));
16179 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16180 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16181 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16182 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16183 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16184 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16185 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16186 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16187 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16188 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16189 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16190 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16191 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016192 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016193 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16194 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16195 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16196 /* Set the VLAN bit map */
16197 if (phba->valid_vlan) {
16198 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16199 = 1 << (phba->vlan_id % 8);
16200 }
16201}
16202
16203/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016204 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016205 * @phba: pointer to lpfc hba data structure.
16206 * @fcf_index: FCF table entry offset.
16207 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016208 * This routine is invoked to scan the entire FCF table by reading FCF
16209 * record and processing it one at a time starting from the @fcf_index
16210 * for initial FCF discovery or fast FCF failover rediscovery.
16211 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016212 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016213 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016214 **/
16215int
James Smart0c9ab6f2010-02-26 14:15:57 -050016216lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016217{
16218 int rc = 0, error;
16219 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016220
James Smart32b97932009-07-19 10:01:21 -040016221 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016222 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016223 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16224 if (!mboxq) {
16225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16226 "2000 Failed to allocate mbox for "
16227 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016228 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016229 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016230 }
James Smartecfd03c2010-02-12 14:41:27 -050016231 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016232 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016233 if (rc) {
16234 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016235 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016236 }
James Smartecfd03c2010-02-12 14:41:27 -050016237 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016238 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016239 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016240
16241 spin_lock_irq(&phba->hbalock);
16242 phba->hba_flag |= FCF_TS_INPROG;
16243 spin_unlock_irq(&phba->hbalock);
16244
James Smart6fb120a2009-05-22 14:52:59 -040016245 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016246 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016247 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016248 else {
James Smart38b92ef2010-08-04 16:11:39 -040016249 /* Reset eligible FCF count for new scan */
16250 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016251 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016252 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016253 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016254fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016255 if (error) {
16256 if (mboxq)
16257 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016258 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016259 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016260 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016261 spin_unlock_irq(&phba->hbalock);
16262 }
James Smart6fb120a2009-05-22 14:52:59 -040016263 return error;
16264}
James Smarta0c87cb2009-07-19 10:01:10 -040016265
16266/**
James Smarta93ff372010-10-22 11:06:08 -040016267 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016268 * @phba: pointer to lpfc hba data structure.
16269 * @fcf_index: FCF table entry offset.
16270 *
16271 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016272 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016273 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016274 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016275 * otherwise.
16276 **/
16277int
16278lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16279{
16280 int rc = 0, error;
16281 LPFC_MBOXQ_t *mboxq;
16282
16283 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16284 if (!mboxq) {
16285 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16286 "2763 Failed to allocate mbox for "
16287 "READ_FCF cmd\n");
16288 error = -ENOMEM;
16289 goto fail_fcf_read;
16290 }
16291 /* Construct the read FCF record mailbox command */
16292 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16293 if (rc) {
16294 error = -EINVAL;
16295 goto fail_fcf_read;
16296 }
16297 /* Issue the mailbox command asynchronously */
16298 mboxq->vport = phba->pport;
16299 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16300 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16301 if (rc == MBX_NOT_FINISHED)
16302 error = -EIO;
16303 else
16304 error = 0;
16305
16306fail_fcf_read:
16307 if (error && mboxq)
16308 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16309 return error;
16310}
16311
16312/**
16313 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16314 * @phba: pointer to lpfc hba data structure.
16315 * @fcf_index: FCF table entry offset.
16316 *
16317 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016318 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016319 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016320 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016321 * otherwise.
16322 **/
16323int
16324lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16325{
16326 int rc = 0, error;
16327 LPFC_MBOXQ_t *mboxq;
16328
16329 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16330 if (!mboxq) {
16331 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16332 "2758 Failed to allocate mbox for "
16333 "READ_FCF cmd\n");
16334 error = -ENOMEM;
16335 goto fail_fcf_read;
16336 }
16337 /* Construct the read FCF record mailbox command */
16338 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16339 if (rc) {
16340 error = -EINVAL;
16341 goto fail_fcf_read;
16342 }
16343 /* Issue the mailbox command asynchronously */
16344 mboxq->vport = phba->pport;
16345 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16346 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16347 if (rc == MBX_NOT_FINISHED)
16348 error = -EIO;
16349 else
16350 error = 0;
16351
16352fail_fcf_read:
16353 if (error && mboxq)
16354 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16355 return error;
16356}
16357
16358/**
James Smartf5cb5302015-12-16 18:11:52 -050016359 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016360 * phba pointer to the lpfc_hba struct for this port.
16361 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16362 * routine when the rr_bmask is empty. The FCF indecies are put into the
16363 * rr_bmask based on their priority level. Starting from the highest priority
16364 * to the lowest. The most likely FCF candidate will be in the highest
16365 * priority group. When this routine is called it searches the fcf_pri list for
16366 * next lowest priority group and repopulates the rr_bmask with only those
16367 * fcf_indexes.
16368 * returns:
16369 * 1=success 0=failure
16370 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016371static int
James Smart7d791df2011-07-22 18:37:52 -040016372lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16373{
16374 uint16_t next_fcf_pri;
16375 uint16_t last_index;
16376 struct lpfc_fcf_pri *fcf_pri;
16377 int rc;
16378 int ret = 0;
16379
16380 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16381 LPFC_SLI4_FCF_TBL_INDX_MAX);
16382 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16383 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016384
16385 /* Verify the priority list has 2 or more entries */
16386 spin_lock_irq(&phba->hbalock);
16387 if (list_empty(&phba->fcf.fcf_pri_list) ||
16388 list_is_singular(&phba->fcf.fcf_pri_list)) {
16389 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016390 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16391 "3061 Last IDX %d\n", last_index);
16392 return 0; /* Empty rr list */
16393 }
James Smart25626692013-03-01 16:36:54 -050016394 spin_unlock_irq(&phba->hbalock);
16395
James Smart7d791df2011-07-22 18:37:52 -040016396 next_fcf_pri = 0;
16397 /*
16398 * Clear the rr_bmask and set all of the bits that are at this
16399 * priority.
16400 */
16401 memset(phba->fcf.fcf_rr_bmask, 0,
16402 sizeof(*phba->fcf.fcf_rr_bmask));
16403 spin_lock_irq(&phba->hbalock);
16404 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16405 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16406 continue;
16407 /*
16408 * the 1st priority that has not FLOGI failed
16409 * will be the highest.
16410 */
16411 if (!next_fcf_pri)
16412 next_fcf_pri = fcf_pri->fcf_rec.priority;
16413 spin_unlock_irq(&phba->hbalock);
16414 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16415 rc = lpfc_sli4_fcf_rr_index_set(phba,
16416 fcf_pri->fcf_rec.fcf_index);
16417 if (rc)
16418 return 0;
16419 }
16420 spin_lock_irq(&phba->hbalock);
16421 }
16422 /*
16423 * if next_fcf_pri was not set above and the list is not empty then
16424 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016425 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016426 */
16427 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16428 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16429 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16430 /*
16431 * the 1st priority that has not FLOGI failed
16432 * will be the highest.
16433 */
16434 if (!next_fcf_pri)
16435 next_fcf_pri = fcf_pri->fcf_rec.priority;
16436 spin_unlock_irq(&phba->hbalock);
16437 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16438 rc = lpfc_sli4_fcf_rr_index_set(phba,
16439 fcf_pri->fcf_rec.fcf_index);
16440 if (rc)
16441 return 0;
16442 }
16443 spin_lock_irq(&phba->hbalock);
16444 }
16445 } else
16446 ret = 1;
16447 spin_unlock_irq(&phba->hbalock);
16448
16449 return ret;
16450}
16451/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016452 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16453 * @phba: pointer to lpfc hba data structure.
16454 *
16455 * This routine is to get the next eligible FCF record index in a round
16456 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016457 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016458 * shall be returned, otherwise, the next eligible FCF record's index
16459 * shall be returned.
16460 **/
16461uint16_t
16462lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16463{
16464 uint16_t next_fcf_index;
16465
James Smart421c6622013-01-03 15:44:16 -050016466initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016467 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016468 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16469
James Smart7d791df2011-07-22 18:37:52 -040016470next_priority:
James Smart421c6622013-01-03 15:44:16 -050016471 /* Determine the next fcf index to check */
16472 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016473 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16474 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016475 next_fcf_index);
16476
James Smart0c9ab6f2010-02-26 14:15:57 -050016477 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016478 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16479 /*
16480 * If we have wrapped then we need to clear the bits that
16481 * have been tested so that we can detect when we should
16482 * change the priority level.
16483 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016484 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16485 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016486 }
16487
James Smart0c9ab6f2010-02-26 14:15:57 -050016488
James Smart3804dc82010-07-14 15:31:37 -040016489 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016490 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16491 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16492 /*
16493 * If next fcf index is not found check if there are lower
16494 * Priority level fcf's in the fcf_priority list.
16495 * Set up the rr_bmask with all of the avaiable fcf bits
16496 * at that level and continue the selection process.
16497 */
16498 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016499 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016500 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16501 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016502 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16503 return LPFC_FCOE_FCF_NEXT_NONE;
16504 else {
16505 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16506 "3063 Only FCF available idx %d, flag %x\n",
16507 next_fcf_index,
16508 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16509 return next_fcf_index;
16510 }
James Smart3804dc82010-07-14 15:31:37 -040016511 }
16512
James Smart7d791df2011-07-22 18:37:52 -040016513 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16514 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016515 LPFC_FCF_FLOGI_FAILED) {
16516 if (list_is_singular(&phba->fcf.fcf_pri_list))
16517 return LPFC_FCOE_FCF_NEXT_NONE;
16518
James Smart7d791df2011-07-22 18:37:52 -040016519 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016520 }
James Smart7d791df2011-07-22 18:37:52 -040016521
James Smart3804dc82010-07-14 15:31:37 -040016522 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016523 "2845 Get next roundrobin failover FCF (x%x)\n",
16524 next_fcf_index);
16525
James Smart0c9ab6f2010-02-26 14:15:57 -050016526 return next_fcf_index;
16527}
16528
16529/**
16530 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16531 * @phba: pointer to lpfc hba data structure.
16532 *
16533 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016534 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016535 * does not go beyond the range of the driver allocated bmask dimension
16536 * before setting the bit.
16537 *
16538 * Returns 0 if the index bit successfully set, otherwise, it returns
16539 * -EINVAL.
16540 **/
16541int
16542lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16543{
16544 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16545 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016546 "2610 FCF (x%x) reached driver's book "
16547 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016548 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16549 return -EINVAL;
16550 }
16551 /* Set the eligible FCF record index bmask */
16552 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16553
James Smart3804dc82010-07-14 15:31:37 -040016554 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016555 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016556 "bmask\n", fcf_index);
16557
James Smart0c9ab6f2010-02-26 14:15:57 -050016558 return 0;
16559}
16560
16561/**
James Smart3804dc82010-07-14 15:31:37 -040016562 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016563 * @phba: pointer to lpfc hba data structure.
16564 *
16565 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016566 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016567 * does not go beyond the range of the driver allocated bmask dimension
16568 * before clearing the bit.
16569 **/
16570void
16571lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16572{
James Smart9a803a72013-09-06 12:17:56 -040016573 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016574 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16575 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016576 "2762 FCF (x%x) reached driver's book "
16577 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016578 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16579 return;
16580 }
16581 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016582 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016583 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16584 list) {
James Smart7d791df2011-07-22 18:37:52 -040016585 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16586 list_del_init(&fcf_pri->list);
16587 break;
16588 }
16589 }
16590 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016591 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016592
16593 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016594 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016595 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016596}
16597
16598/**
James Smartecfd03c2010-02-12 14:41:27 -050016599 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16600 * @phba: pointer to lpfc hba data structure.
16601 *
16602 * This routine is the completion routine for the rediscover FCF table mailbox
16603 * command. If the mailbox command returned failure, it will try to stop the
16604 * FCF rediscover wait timer.
16605 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016606static void
James Smartecfd03c2010-02-12 14:41:27 -050016607lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16608{
16609 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16610 uint32_t shdr_status, shdr_add_status;
16611
16612 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16613
16614 shdr_status = bf_get(lpfc_mbox_hdr_status,
16615 &redisc_fcf->header.cfg_shdr.response);
16616 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16617 &redisc_fcf->header.cfg_shdr.response);
16618 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016619 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016620 "2746 Requesting for FCF rediscovery failed "
16621 "status x%x add_status x%x\n",
16622 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016623 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016624 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016625 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016626 spin_unlock_irq(&phba->hbalock);
16627 /*
16628 * CVL event triggered FCF rediscover request failed,
16629 * last resort to re-try current registered FCF entry.
16630 */
16631 lpfc_retry_pport_discovery(phba);
16632 } else {
16633 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016634 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016635 spin_unlock_irq(&phba->hbalock);
16636 /*
16637 * DEAD FCF event triggered FCF rediscover request
16638 * failed, last resort to fail over as a link down
16639 * to FCF registration.
16640 */
16641 lpfc_sli4_fcf_dead_failthrough(phba);
16642 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016643 } else {
16644 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016645 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016646 /*
16647 * Start FCF rediscovery wait timer for pending FCF
16648 * before rescan FCF record table.
16649 */
16650 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016651 }
James Smartecfd03c2010-02-12 14:41:27 -050016652
16653 mempool_free(mbox, phba->mbox_mem_pool);
16654}
16655
16656/**
James Smart3804dc82010-07-14 15:31:37 -040016657 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016658 * @phba: pointer to lpfc hba data structure.
16659 *
16660 * This routine is invoked to request for rediscovery of the entire FCF table
16661 * by the port.
16662 **/
16663int
16664lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16665{
16666 LPFC_MBOXQ_t *mbox;
16667 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16668 int rc, length;
16669
James Smart0c9ab6f2010-02-26 14:15:57 -050016670 /* Cancel retry delay timers to all vports before FCF rediscover */
16671 lpfc_cancel_all_vport_retry_delay_timer(phba);
16672
James Smartecfd03c2010-02-12 14:41:27 -050016673 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16674 if (!mbox) {
16675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16676 "2745 Failed to allocate mbox for "
16677 "requesting FCF rediscover.\n");
16678 return -ENOMEM;
16679 }
16680
16681 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16682 sizeof(struct lpfc_sli4_cfg_mhdr));
16683 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16684 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16685 length, LPFC_SLI4_MBX_EMBED);
16686
16687 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16688 /* Set count to 0 for invalidating the entire FCF database */
16689 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16690
16691 /* Issue the mailbox command asynchronously */
16692 mbox->vport = phba->pport;
16693 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16694 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16695
16696 if (rc == MBX_NOT_FINISHED) {
16697 mempool_free(mbox, phba->mbox_mem_pool);
16698 return -EIO;
16699 }
16700 return 0;
16701}
16702
16703/**
James Smartfc2b9892010-02-26 14:15:29 -050016704 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16705 * @phba: pointer to lpfc hba data structure.
16706 *
16707 * This function is the failover routine as a last resort to the FCF DEAD
16708 * event when driver failed to perform fast FCF failover.
16709 **/
16710void
16711lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16712{
16713 uint32_t link_state;
16714
16715 /*
16716 * Last resort as FCF DEAD event failover will treat this as
16717 * a link down, but save the link state because we don't want
16718 * it to be changed to Link Down unless it is already down.
16719 */
16720 link_state = phba->link_state;
16721 lpfc_linkdown(phba);
16722 phba->link_state = link_state;
16723
16724 /* Unregister FCF if no devices connected to it */
16725 lpfc_unregister_unused_fcf(phba);
16726}
16727
16728/**
James Smart026abb82011-12-13 13:20:45 -050016729 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016730 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016731 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016732 *
James Smart026abb82011-12-13 13:20:45 -050016733 * This function gets SLI3 port configure region 23 data through memory dump
16734 * mailbox command. When it successfully retrieves data, the size of the data
16735 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016736 **/
James Smart026abb82011-12-13 13:20:45 -050016737static uint32_t
16738lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016739{
16740 LPFC_MBOXQ_t *pmb = NULL;
16741 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016742 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016743 int rc;
16744
James Smart026abb82011-12-13 13:20:45 -050016745 if (!rgn23_data)
16746 return 0;
16747
James Smarta0c87cb2009-07-19 10:01:10 -040016748 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16749 if (!pmb) {
16750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016751 "2600 failed to allocate mailbox memory\n");
16752 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016753 }
16754 mb = &pmb->u.mb;
16755
James Smarta0c87cb2009-07-19 10:01:10 -040016756 do {
16757 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16758 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16759
16760 if (rc != MBX_SUCCESS) {
16761 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016762 "2601 failed to read config "
16763 "region 23, rc 0x%x Status 0x%x\n",
16764 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016765 mb->un.varDmp.word_cnt = 0;
16766 }
16767 /*
16768 * dump mem may return a zero when finished or we got a
16769 * mailbox error, either way we are done.
16770 */
16771 if (mb->un.varDmp.word_cnt == 0)
16772 break;
16773 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16774 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16775
16776 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016777 rgn23_data + offset,
16778 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016779 offset += mb->un.varDmp.word_cnt;
16780 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16781
James Smart026abb82011-12-13 13:20:45 -050016782 mempool_free(pmb, phba->mbox_mem_pool);
16783 return offset;
16784}
16785
16786/**
16787 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16788 * @phba: pointer to lpfc hba data structure.
16789 * @rgn23_data: pointer to configure region 23 data.
16790 *
16791 * This function gets SLI4 port configure region 23 data through memory dump
16792 * mailbox command. When it successfully retrieves data, the size of the data
16793 * will be returned, otherwise, 0 will be returned.
16794 **/
16795static uint32_t
16796lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16797{
16798 LPFC_MBOXQ_t *mboxq = NULL;
16799 struct lpfc_dmabuf *mp = NULL;
16800 struct lpfc_mqe *mqe;
16801 uint32_t data_length = 0;
16802 int rc;
16803
16804 if (!rgn23_data)
16805 return 0;
16806
16807 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16808 if (!mboxq) {
16809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16810 "3105 failed to allocate mailbox memory\n");
16811 return 0;
16812 }
16813
16814 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16815 goto out;
16816 mqe = &mboxq->u.mqe;
16817 mp = (struct lpfc_dmabuf *) mboxq->context1;
16818 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16819 if (rc)
16820 goto out;
16821 data_length = mqe->un.mb_words[5];
16822 if (data_length == 0)
16823 goto out;
16824 if (data_length > DMP_RGN23_SIZE) {
16825 data_length = 0;
16826 goto out;
16827 }
16828 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16829out:
16830 mempool_free(mboxq, phba->mbox_mem_pool);
16831 if (mp) {
16832 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16833 kfree(mp);
16834 }
16835 return data_length;
16836}
16837
16838/**
16839 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16840 * @phba: pointer to lpfc hba data structure.
16841 *
16842 * This function read region 23 and parse TLV for port status to
16843 * decide if the user disaled the port. If the TLV indicates the
16844 * port is disabled, the hba_flag is set accordingly.
16845 **/
16846void
16847lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16848{
16849 uint8_t *rgn23_data = NULL;
16850 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16851 uint32_t offset = 0;
16852
16853 /* Get adapter Region 23 data */
16854 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16855 if (!rgn23_data)
16856 goto out;
16857
16858 if (phba->sli_rev < LPFC_SLI_REV4)
16859 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16860 else {
16861 if_type = bf_get(lpfc_sli_intf_if_type,
16862 &phba->sli4_hba.sli_intf);
16863 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16864 goto out;
16865 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16866 }
James Smarta0c87cb2009-07-19 10:01:10 -040016867
16868 if (!data_size)
16869 goto out;
16870
16871 /* Check the region signature first */
16872 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16874 "2619 Config region 23 has bad signature\n");
16875 goto out;
16876 }
16877 offset += 4;
16878
16879 /* Check the data structure version */
16880 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16882 "2620 Config region 23 has bad version\n");
16883 goto out;
16884 }
16885 offset += 4;
16886
16887 /* Parse TLV entries in the region */
16888 while (offset < data_size) {
16889 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16890 break;
16891 /*
16892 * If the TLV is not driver specific TLV or driver id is
16893 * not linux driver id, skip the record.
16894 */
16895 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16896 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16897 (rgn23_data[offset + 3] != 0)) {
16898 offset += rgn23_data[offset + 1] * 4 + 4;
16899 continue;
16900 }
16901
16902 /* Driver found a driver specific TLV in the config region */
16903 sub_tlv_len = rgn23_data[offset + 1] * 4;
16904 offset += 4;
16905 tlv_offset = 0;
16906
16907 /*
16908 * Search for configured port state sub-TLV.
16909 */
16910 while ((offset < data_size) &&
16911 (tlv_offset < sub_tlv_len)) {
16912 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16913 offset += 4;
16914 tlv_offset += 4;
16915 break;
16916 }
16917 if (rgn23_data[offset] != PORT_STE_TYPE) {
16918 offset += rgn23_data[offset + 1] * 4 + 4;
16919 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16920 continue;
16921 }
16922
16923 /* This HBA contains PORT_STE configured */
16924 if (!rgn23_data[offset + 2])
16925 phba->hba_flag |= LINK_DISABLED;
16926
16927 goto out;
16928 }
16929 }
James Smart026abb82011-12-13 13:20:45 -050016930
James Smarta0c87cb2009-07-19 10:01:10 -040016931out:
James Smarta0c87cb2009-07-19 10:01:10 -040016932 kfree(rgn23_data);
16933 return;
16934}
James Smart695a8142010-01-26 23:08:03 -050016935
16936/**
James Smart52d52442011-05-24 11:42:45 -040016937 * lpfc_wr_object - write an object to the firmware
16938 * @phba: HBA structure that indicates port to create a queue on.
16939 * @dmabuf_list: list of dmabufs to write to the port.
16940 * @size: the total byte value of the objects to write to the port.
16941 * @offset: the current offset to be used to start the transfer.
16942 *
16943 * This routine will create a wr_object mailbox command to send to the port.
16944 * the mailbox command will be constructed using the dma buffers described in
16945 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16946 * BDEs that the imbedded mailbox can support. The @offset variable will be
16947 * used to indicate the starting offset of the transfer and will also return
16948 * the offset after the write object mailbox has completed. @size is used to
16949 * determine the end of the object and whether the eof bit should be set.
16950 *
16951 * Return 0 is successful and offset will contain the the new offset to use
16952 * for the next write.
16953 * Return negative value for error cases.
16954 **/
16955int
16956lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16957 uint32_t size, uint32_t *offset)
16958{
16959 struct lpfc_mbx_wr_object *wr_object;
16960 LPFC_MBOXQ_t *mbox;
16961 int rc = 0, i = 0;
16962 uint32_t shdr_status, shdr_add_status;
16963 uint32_t mbox_tmo;
16964 union lpfc_sli4_cfg_shdr *shdr;
16965 struct lpfc_dmabuf *dmabuf;
16966 uint32_t written = 0;
16967
16968 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16969 if (!mbox)
16970 return -ENOMEM;
16971
16972 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16973 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16974 sizeof(struct lpfc_mbx_wr_object) -
16975 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16976
16977 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16978 wr_object->u.request.write_offset = *offset;
16979 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16980 wr_object->u.request.object_name[0] =
16981 cpu_to_le32(wr_object->u.request.object_name[0]);
16982 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16983 list_for_each_entry(dmabuf, dmabuf_list, list) {
16984 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16985 break;
16986 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16987 wr_object->u.request.bde[i].addrHigh =
16988 putPaddrHigh(dmabuf->phys);
16989 if (written + SLI4_PAGE_SIZE >= size) {
16990 wr_object->u.request.bde[i].tus.f.bdeSize =
16991 (size - written);
16992 written += (size - written);
16993 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16994 } else {
16995 wr_object->u.request.bde[i].tus.f.bdeSize =
16996 SLI4_PAGE_SIZE;
16997 written += SLI4_PAGE_SIZE;
16998 }
16999 i++;
17000 }
17001 wr_object->u.request.bde_count = i;
17002 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17003 if (!phba->sli4_hba.intr_enable)
17004 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17005 else {
James Smarta183a152011-10-10 21:32:43 -040017006 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017007 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17008 }
17009 /* The IOCTL status is embedded in the mailbox subheader. */
17010 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17011 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17012 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17013 if (rc != MBX_TIMEOUT)
17014 mempool_free(mbox, phba->mbox_mem_pool);
17015 if (shdr_status || shdr_add_status || rc) {
17016 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17017 "3025 Write Object mailbox failed with "
17018 "status x%x add_status x%x, mbx status x%x\n",
17019 shdr_status, shdr_add_status, rc);
17020 rc = -ENXIO;
17021 } else
17022 *offset += wr_object->u.response.actual_write_length;
17023 return rc;
17024}
17025
17026/**
James Smart695a8142010-01-26 23:08:03 -050017027 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17028 * @vport: pointer to vport data structure.
17029 *
17030 * This function iterate through the mailboxq and clean up all REG_LOGIN
17031 * and REG_VPI mailbox commands associated with the vport. This function
17032 * is called when driver want to restart discovery of the vport due to
17033 * a Clear Virtual Link event.
17034 **/
17035void
17036lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17037{
17038 struct lpfc_hba *phba = vport->phba;
17039 LPFC_MBOXQ_t *mb, *nextmb;
17040 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017041 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017042 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017043 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017044 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017045 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017046
James Smartd439d282010-09-29 11:18:45 -040017047 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017048 spin_lock_irq(&phba->hbalock);
17049 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17050 if (mb->vport != vport)
17051 continue;
17052
17053 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17054 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17055 continue;
17056
James Smartd439d282010-09-29 11:18:45 -040017057 list_del(&mb->list);
17058 list_add_tail(&mb->list, &mbox_cmd_list);
17059 }
17060 /* Clean up active mailbox command with the vport */
17061 mb = phba->sli.mbox_active;
17062 if (mb && (mb->vport == vport)) {
17063 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17064 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17065 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17066 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17067 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17068 /* Put reference count for delayed processing */
17069 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17070 /* Unregister the RPI when mailbox complete */
17071 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17072 }
17073 }
James Smart63e801c2010-11-20 23:14:19 -050017074 /* Cleanup any mailbox completions which are not yet processed */
17075 do {
17076 restart_loop = 0;
17077 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17078 /*
17079 * If this mailox is already processed or it is
17080 * for another vport ignore it.
17081 */
17082 if ((mb->vport != vport) ||
17083 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17084 continue;
17085
17086 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17087 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17088 continue;
17089
17090 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17091 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17092 ndlp = (struct lpfc_nodelist *)mb->context2;
17093 /* Unregister the RPI when mailbox complete */
17094 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17095 restart_loop = 1;
17096 spin_unlock_irq(&phba->hbalock);
17097 spin_lock(shost->host_lock);
17098 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17099 spin_unlock(shost->host_lock);
17100 spin_lock_irq(&phba->hbalock);
17101 break;
17102 }
17103 }
17104 } while (restart_loop);
17105
James Smartd439d282010-09-29 11:18:45 -040017106 spin_unlock_irq(&phba->hbalock);
17107
17108 /* Release the cleaned-up mailbox commands */
17109 while (!list_empty(&mbox_cmd_list)) {
17110 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017111 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17112 mp = (struct lpfc_dmabuf *) (mb->context1);
17113 if (mp) {
17114 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17115 kfree(mp);
17116 }
James Smart78730cf2010-04-06 15:06:30 -040017117 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017118 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017119 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017120 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017121 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017122 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017123 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017124 }
James Smart695a8142010-01-26 23:08:03 -050017125 }
James Smart695a8142010-01-26 23:08:03 -050017126 mempool_free(mb, phba->mbox_mem_pool);
17127 }
James Smartd439d282010-09-29 11:18:45 -040017128
17129 /* Release the ndlp with the cleaned-up active mailbox command */
17130 if (act_mbx_ndlp) {
17131 spin_lock(shost->host_lock);
17132 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17133 spin_unlock(shost->host_lock);
17134 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017135 }
James Smart695a8142010-01-26 23:08:03 -050017136}
17137
James Smart2a9bf3d2010-06-07 15:24:45 -040017138/**
17139 * lpfc_drain_txq - Drain the txq
17140 * @phba: Pointer to HBA context object.
17141 *
17142 * This function attempt to submit IOCBs on the txq
17143 * to the adapter. For SLI4 adapters, the txq contains
17144 * ELS IOCBs that have been deferred because the there
17145 * are no SGLs. This congestion can occur with large
17146 * vport counts during node discovery.
17147 **/
17148
17149uint32_t
17150lpfc_drain_txq(struct lpfc_hba *phba)
17151{
17152 LIST_HEAD(completions);
17153 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017154 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017155 unsigned long iflags = 0;
17156 char *fail_msg = NULL;
17157 struct lpfc_sglq *sglq;
17158 union lpfc_wqe wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040017159 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017160
James Smart398d81c2013-05-31 17:04:19 -040017161 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017162 list_for_each_entry(piocbq, &pring->txq, list) {
17163 txq_cnt++;
17164 }
17165
17166 if (txq_cnt > pring->txq_max)
17167 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017168
James Smart398d81c2013-05-31 17:04:19 -040017169 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017170
James Smart0e9bb8d2013-03-01 16:35:12 -050017171 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017172 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017173
James Smart19ca7602010-11-20 23:11:55 -050017174 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017175 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017176 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017177 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17178 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017179 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017180 break;
17181 }
James Smart19ca7602010-11-20 23:11:55 -050017182 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017183 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017184 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017185 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017186 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017187 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017188 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017189
17190 /* The xri and iocb resources secured,
17191 * attempt to issue request
17192 */
James Smart6d368e52011-05-24 11:44:12 -040017193 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017194 piocbq->sli4_xritag = sglq->sli4_xritag;
17195 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17196 fail_msg = "to convert bpl to sgl";
17197 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17198 fail_msg = "to convert iocb to wqe";
17199 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17200 fail_msg = " - Wq is full";
17201 else
17202 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17203
17204 if (fail_msg) {
17205 /* Failed means we can't issue and need to cancel */
17206 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17207 "2822 IOCB failed %s iotag 0x%x "
17208 "xri 0x%x\n",
17209 fail_msg,
17210 piocbq->iotag, piocbq->sli4_xritag);
17211 list_add_tail(&piocbq->list, &completions);
17212 }
James Smart398d81c2013-05-31 17:04:19 -040017213 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017214 }
17215
James Smart2a9bf3d2010-06-07 15:24:45 -040017216 /* Cancel all the IOCBs that cannot be issued */
17217 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17218 IOERR_SLI_ABORTED);
17219
James Smart0e9bb8d2013-03-01 16:35:12 -050017220 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017221}