blob: b1ddbaac1c791a6300b4dcf07c799d72e41bba64 [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 Smart4597663f2016-07-06 12:36:01 -07004699 else if (rc && mode == 2)
4700 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004701 if (rc)
dea31012005-04-17 16:05:31 -05004702 goto lpfc_sli_hba_setup_error;
4703
James Smart0d878412009-10-02 15:16:56 -04004704 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4705 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4706 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4707 if (!rc) {
4708 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4709 "2709 This device supports "
4710 "Advanced Error Reporting (AER)\n");
4711 spin_lock_irq(&phba->hbalock);
4712 phba->hba_flag |= HBA_AER_ENABLED;
4713 spin_unlock_irq(&phba->hbalock);
4714 } else {
4715 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4716 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004717 "Advanced Error Reporting (AER): %d\n",
4718 rc);
James Smart0d878412009-10-02 15:16:56 -04004719 phba->cfg_aer_support = 0;
4720 }
4721 }
4722
James Smarted957682007-06-17 19:56:37 -05004723 if (phba->sli_rev == 3) {
4724 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4725 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004726 } else {
4727 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4728 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004729 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004730 }
4731
4732 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004733 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4734 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004735 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004736
4737 if (rc)
4738 goto lpfc_sli_hba_setup_error;
4739
James Smart6d368e52011-05-24 11:44:12 -04004740 /* Initialize VPIs. */
4741 if (phba->sli_rev == LPFC_SLI_REV3) {
4742 /*
4743 * The VPI bitmask and physical ID array are allocated
4744 * and initialized once only - at driver load. A port
4745 * reset doesn't need to reinitialize this memory.
4746 */
4747 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4748 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4749 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4750 GFP_KERNEL);
4751 if (!phba->vpi_bmask) {
4752 rc = -ENOMEM;
4753 goto lpfc_sli_hba_setup_error;
4754 }
4755
4756 phba->vpi_ids = kzalloc(
4757 (phba->max_vpi+1) * sizeof(uint16_t),
4758 GFP_KERNEL);
4759 if (!phba->vpi_ids) {
4760 kfree(phba->vpi_bmask);
4761 rc = -ENOMEM;
4762 goto lpfc_sli_hba_setup_error;
4763 }
4764 for (i = 0; i < phba->max_vpi; i++)
4765 phba->vpi_ids[i] = i;
4766 }
4767 }
4768
James Smart93996272008-08-24 21:50:30 -04004769 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004770 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4771 rc = lpfc_sli_hbq_setup(phba);
4772 if (rc)
4773 goto lpfc_sli_hba_setup_error;
4774 }
James Smart04c68492009-05-22 14:52:52 -04004775 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004776 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004777 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004778
4779 rc = lpfc_config_port_post(phba);
4780 if (rc)
4781 goto lpfc_sli_hba_setup_error;
4782
James Smarted957682007-06-17 19:56:37 -05004783 return rc;
4784
James Smart92d7f7b2007-06-17 19:56:38 -05004785lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004786 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004788 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004789 return rc;
4790}
4791
James Smartda0436e2009-05-22 14:51:39 -04004792/**
4793 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4794 * @phba: Pointer to HBA context object.
4795 * @mboxq: mailbox pointer.
4796 * This function issue a dump mailbox command to read config region
4797 * 23 and parse the records in the region and populate driver
4798 * data structure.
4799 **/
4800static int
James Smartff78d8f2011-12-13 13:21:35 -05004801lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004802{
James Smartff78d8f2011-12-13 13:21:35 -05004803 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004804 struct lpfc_dmabuf *mp;
4805 struct lpfc_mqe *mqe;
4806 uint32_t data_length;
4807 int rc;
4808
4809 /* Program the default value of vlan_id and fc_map */
4810 phba->valid_vlan = 0;
4811 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4812 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4813 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4814
James Smartff78d8f2011-12-13 13:21:35 -05004815 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4816 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004817 return -ENOMEM;
4818
James Smartff78d8f2011-12-13 13:21:35 -05004819 mqe = &mboxq->u.mqe;
4820 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4821 rc = -ENOMEM;
4822 goto out_free_mboxq;
4823 }
4824
James Smartda0436e2009-05-22 14:51:39 -04004825 mp = (struct lpfc_dmabuf *) mboxq->context1;
4826 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4827
4828 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4829 "(%d):2571 Mailbox cmd x%x Status x%x "
4830 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4831 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4832 "CQ: x%x x%x x%x x%x\n",
4833 mboxq->vport ? mboxq->vport->vpi : 0,
4834 bf_get(lpfc_mqe_command, mqe),
4835 bf_get(lpfc_mqe_status, mqe),
4836 mqe->un.mb_words[0], mqe->un.mb_words[1],
4837 mqe->un.mb_words[2], mqe->un.mb_words[3],
4838 mqe->un.mb_words[4], mqe->un.mb_words[5],
4839 mqe->un.mb_words[6], mqe->un.mb_words[7],
4840 mqe->un.mb_words[8], mqe->un.mb_words[9],
4841 mqe->un.mb_words[10], mqe->un.mb_words[11],
4842 mqe->un.mb_words[12], mqe->un.mb_words[13],
4843 mqe->un.mb_words[14], mqe->un.mb_words[15],
4844 mqe->un.mb_words[16], mqe->un.mb_words[50],
4845 mboxq->mcqe.word0,
4846 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4847 mboxq->mcqe.trailer);
4848
4849 if (rc) {
4850 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4851 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004852 rc = -EIO;
4853 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004854 }
4855 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004856 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004857 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4858 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004859 rc = -EIO;
4860 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004861 }
James Smartda0436e2009-05-22 14:51:39 -04004862
4863 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4864 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4865 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004866 rc = 0;
4867
4868out_free_mboxq:
4869 mempool_free(mboxq, phba->mbox_mem_pool);
4870 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004871}
4872
4873/**
4874 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4875 * @phba: pointer to lpfc hba data structure.
4876 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4877 * @vpd: pointer to the memory to hold resulting port vpd data.
4878 * @vpd_size: On input, the number of bytes allocated to @vpd.
4879 * On output, the number of data bytes in @vpd.
4880 *
4881 * This routine executes a READ_REV SLI4 mailbox command. In
4882 * addition, this routine gets the port vpd data.
4883 *
4884 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004885 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004886 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004887 **/
4888static int
4889lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4890 uint8_t *vpd, uint32_t *vpd_size)
4891{
4892 int rc = 0;
4893 uint32_t dma_size;
4894 struct lpfc_dmabuf *dmabuf;
4895 struct lpfc_mqe *mqe;
4896
4897 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4898 if (!dmabuf)
4899 return -ENOMEM;
4900
4901 /*
4902 * Get a DMA buffer for the vpd data resulting from the READ_REV
4903 * mailbox command.
4904 */
4905 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004906 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4907 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004908 if (!dmabuf->virt) {
4909 kfree(dmabuf);
4910 return -ENOMEM;
4911 }
James Smartda0436e2009-05-22 14:51:39 -04004912
4913 /*
4914 * The SLI4 implementation of READ_REV conflicts at word1,
4915 * bits 31:16 and SLI4 adds vpd functionality not present
4916 * in SLI3. This code corrects the conflicts.
4917 */
4918 lpfc_read_rev(phba, mboxq);
4919 mqe = &mboxq->u.mqe;
4920 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4921 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4922 mqe->un.read_rev.word1 &= 0x0000FFFF;
4923 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4924 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4925
4926 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4927 if (rc) {
4928 dma_free_coherent(&phba->pcidev->dev, dma_size,
4929 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004930 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004931 return -EIO;
4932 }
4933
James Smartda0436e2009-05-22 14:51:39 -04004934 /*
4935 * The available vpd length cannot be bigger than the
4936 * DMA buffer passed to the port. Catch the less than
4937 * case and update the caller's size.
4938 */
4939 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4940 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4941
James Smartd7c47992010-06-08 18:31:54 -04004942 memcpy(vpd, dmabuf->virt, *vpd_size);
4943
James Smartda0436e2009-05-22 14:51:39 -04004944 dma_free_coherent(&phba->pcidev->dev, dma_size,
4945 dmabuf->virt, dmabuf->phys);
4946 kfree(dmabuf);
4947 return 0;
4948}
4949
4950/**
James Smartcd1c8302011-10-10 21:33:25 -04004951 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4952 * @phba: pointer to lpfc hba data structure.
4953 *
4954 * This routine retrieves SLI4 device physical port name this PCI function
4955 * is attached to.
4956 *
4957 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004958 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004959 * otherwise - failed to retrieve physical port name
4960 **/
4961static int
4962lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4963{
4964 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004965 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4966 struct lpfc_controller_attribute *cntl_attr;
4967 struct lpfc_mbx_get_port_name *get_port_name;
4968 void *virtaddr = NULL;
4969 uint32_t alloclen, reqlen;
4970 uint32_t shdr_status, shdr_add_status;
4971 union lpfc_sli4_cfg_shdr *shdr;
4972 char cport_name = 0;
4973 int rc;
4974
4975 /* We assume nothing at this point */
4976 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4977 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4978
4979 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4980 if (!mboxq)
4981 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004982 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004983 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4984 lpfc_sli4_read_config(phba);
4985 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4986 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004987
4988 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4989 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4990 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4991 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4992 LPFC_SLI4_MBX_NEMBED);
4993 if (alloclen < reqlen) {
4994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4995 "3084 Allocated DMA memory size (%d) is "
4996 "less than the requested DMA memory size "
4997 "(%d)\n", alloclen, reqlen);
4998 rc = -ENOMEM;
4999 goto out_free_mboxq;
5000 }
5001 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5002 virtaddr = mboxq->sge_array->addr[0];
5003 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5004 shdr = &mbx_cntl_attr->cfg_shdr;
5005 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5006 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5007 if (shdr_status || shdr_add_status || rc) {
5008 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5009 "3085 Mailbox x%x (x%x/x%x) failed, "
5010 "rc:x%x, status:x%x, add_status:x%x\n",
5011 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5012 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5013 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5014 rc, shdr_status, shdr_add_status);
5015 rc = -ENXIO;
5016 goto out_free_mboxq;
5017 }
5018 cntl_attr = &mbx_cntl_attr->cntl_attr;
5019 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5020 phba->sli4_hba.lnk_info.lnk_tp =
5021 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5022 phba->sli4_hba.lnk_info.lnk_no =
5023 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5024 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5025 "3086 lnk_type:%d, lnk_numb:%d\n",
5026 phba->sli4_hba.lnk_info.lnk_tp,
5027 phba->sli4_hba.lnk_info.lnk_no);
5028
5029retrieve_ppname:
5030 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5031 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5032 sizeof(struct lpfc_mbx_get_port_name) -
5033 sizeof(struct lpfc_sli4_cfg_mhdr),
5034 LPFC_SLI4_MBX_EMBED);
5035 get_port_name = &mboxq->u.mqe.un.get_port_name;
5036 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5037 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5038 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5039 phba->sli4_hba.lnk_info.lnk_tp);
5040 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5041 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5042 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5043 if (shdr_status || shdr_add_status || rc) {
5044 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5045 "3087 Mailbox x%x (x%x/x%x) failed: "
5046 "rc:x%x, status:x%x, add_status:x%x\n",
5047 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5048 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5049 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5050 rc, shdr_status, shdr_add_status);
5051 rc = -ENXIO;
5052 goto out_free_mboxq;
5053 }
5054 switch (phba->sli4_hba.lnk_info.lnk_no) {
5055 case LPFC_LINK_NUMBER_0:
5056 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5057 &get_port_name->u.response);
5058 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5059 break;
5060 case LPFC_LINK_NUMBER_1:
5061 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5062 &get_port_name->u.response);
5063 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5064 break;
5065 case LPFC_LINK_NUMBER_2:
5066 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5067 &get_port_name->u.response);
5068 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5069 break;
5070 case LPFC_LINK_NUMBER_3:
5071 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5072 &get_port_name->u.response);
5073 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5074 break;
5075 default:
5076 break;
5077 }
5078
5079 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5080 phba->Port[0] = cport_name;
5081 phba->Port[1] = '\0';
5082 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5083 "3091 SLI get port name: %s\n", phba->Port);
5084 }
5085
5086out_free_mboxq:
5087 if (rc != MBX_TIMEOUT) {
5088 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5089 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5090 else
5091 mempool_free(mboxq, phba->mbox_mem_pool);
5092 }
5093 return rc;
5094}
5095
5096/**
James Smartda0436e2009-05-22 14:51:39 -04005097 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5098 * @phba: pointer to lpfc hba data structure.
5099 *
5100 * This routine is called to explicitly arm the SLI4 device's completion and
5101 * event queues
5102 **/
5103static void
5104lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5105{
James Smart962bc512013-01-03 15:44:00 -05005106 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04005107
5108 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5109 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04005110 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05005111 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04005112 do {
James Smart2e90f4b2011-12-13 13:22:37 -05005113 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5114 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04005115 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05005116 }
James Smart1ba981f2014-02-20 09:56:45 -05005117
James Smartf38fa0b2014-04-04 13:52:21 -04005118 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005119 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5120
James Smart67d12732012-08-03 12:36:13 -04005121 if (phba->sli4_hba.hba_eq) {
5122 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05005123 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04005124 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05005125 LPFC_QUEUE_REARM);
5126 }
James Smart1ba981f2014-02-20 09:56:45 -05005127
5128 if (phba->cfg_fof)
5129 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005130}
5131
5132/**
James Smart6d368e52011-05-24 11:44:12 -04005133 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5134 * @phba: Pointer to HBA context object.
5135 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005136 * @extnt_count: buffer to hold port available extent count.
5137 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005138 *
James Smartb76f2dc2011-07-22 18:37:42 -04005139 * This function calls the port and retrievs the number of available
5140 * extents and their size for a particular extent type.
5141 *
5142 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005143 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005144int
James Smart6d368e52011-05-24 11:44:12 -04005145lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5146 uint16_t *extnt_count, uint16_t *extnt_size)
5147{
5148 int rc = 0;
5149 uint32_t length;
5150 uint32_t mbox_tmo;
5151 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5152 LPFC_MBOXQ_t *mbox;
5153
5154 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5155 if (!mbox)
5156 return -ENOMEM;
5157
5158 /* Find out how many extents are available for this resource type */
5159 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5160 sizeof(struct lpfc_sli4_cfg_mhdr));
5161 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5162 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5163 length, LPFC_SLI4_MBX_EMBED);
5164
5165 /* Send an extents count of 0 - the GET doesn't use it. */
5166 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5167 LPFC_SLI4_MBX_EMBED);
5168 if (unlikely(rc)) {
5169 rc = -EIO;
5170 goto err_exit;
5171 }
5172
5173 if (!phba->sli4_hba.intr_enable)
5174 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5175 else {
James Smarta183a152011-10-10 21:32:43 -04005176 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005177 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5178 }
5179 if (unlikely(rc)) {
5180 rc = -EIO;
5181 goto err_exit;
5182 }
5183
5184 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5185 if (bf_get(lpfc_mbox_hdr_status,
5186 &rsrc_info->header.cfg_shdr.response)) {
5187 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5188 "2930 Failed to get resource extents "
5189 "Status 0x%x Add'l Status 0x%x\n",
5190 bf_get(lpfc_mbox_hdr_status,
5191 &rsrc_info->header.cfg_shdr.response),
5192 bf_get(lpfc_mbox_hdr_add_status,
5193 &rsrc_info->header.cfg_shdr.response));
5194 rc = -EIO;
5195 goto err_exit;
5196 }
5197
5198 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5199 &rsrc_info->u.rsp);
5200 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5201 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005202
5203 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5204 "3162 Retrieved extents type-%d from port: count:%d, "
5205 "size:%d\n", type, *extnt_count, *extnt_size);
5206
5207err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005208 mempool_free(mbox, phba->mbox_mem_pool);
5209 return rc;
5210}
5211
5212/**
5213 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5214 * @phba: Pointer to HBA context object.
5215 * @type: The extent type to check.
5216 *
5217 * This function reads the current available extents from the port and checks
5218 * if the extent count or extent size has changed since the last access.
5219 * Callers use this routine post port reset to understand if there is a
5220 * extent reprovisioning requirement.
5221 *
5222 * Returns:
5223 * -Error: error indicates problem.
5224 * 1: Extent count or size has changed.
5225 * 0: No changes.
5226 **/
5227static int
5228lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5229{
5230 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5231 uint16_t size_diff, rsrc_ext_size;
5232 int rc = 0;
5233 struct lpfc_rsrc_blks *rsrc_entry;
5234 struct list_head *rsrc_blk_list = NULL;
5235
5236 size_diff = 0;
5237 curr_ext_cnt = 0;
5238 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5239 &rsrc_ext_cnt,
5240 &rsrc_ext_size);
5241 if (unlikely(rc))
5242 return -EIO;
5243
5244 switch (type) {
5245 case LPFC_RSC_TYPE_FCOE_RPI:
5246 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5247 break;
5248 case LPFC_RSC_TYPE_FCOE_VPI:
5249 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5250 break;
5251 case LPFC_RSC_TYPE_FCOE_XRI:
5252 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5253 break;
5254 case LPFC_RSC_TYPE_FCOE_VFI:
5255 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5256 break;
5257 default:
5258 break;
5259 }
5260
5261 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5262 curr_ext_cnt++;
5263 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5264 size_diff++;
5265 }
5266
5267 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5268 rc = 1;
5269
5270 return rc;
5271}
5272
5273/**
5274 * lpfc_sli4_cfg_post_extnts -
5275 * @phba: Pointer to HBA context object.
5276 * @extnt_cnt - number of available extents.
5277 * @type - the extent type (rpi, xri, vfi, vpi).
5278 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5279 * @mbox - pointer to the caller's allocated mailbox structure.
5280 *
5281 * This function executes the extents allocation request. It also
5282 * takes care of the amount of memory needed to allocate or get the
5283 * allocated extents. It is the caller's responsibility to evaluate
5284 * the response.
5285 *
5286 * Returns:
5287 * -Error: Error value describes the condition found.
5288 * 0: if successful
5289 **/
5290static int
James Smart8a9d2e82012-05-09 21:16:12 -04005291lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005292 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5293{
5294 int rc = 0;
5295 uint32_t req_len;
5296 uint32_t emb_len;
5297 uint32_t alloc_len, mbox_tmo;
5298
5299 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005300 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005301
5302 /*
5303 * Calculate the size of an embedded mailbox. The uint32_t
5304 * accounts for extents-specific word.
5305 */
5306 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5307 sizeof(uint32_t);
5308
5309 /*
5310 * Presume the allocation and response will fit into an embedded
5311 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5312 */
5313 *emb = LPFC_SLI4_MBX_EMBED;
5314 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005315 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005316 sizeof(union lpfc_sli4_cfg_shdr) +
5317 sizeof(uint32_t);
5318 *emb = LPFC_SLI4_MBX_NEMBED;
5319 }
5320
5321 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5322 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5323 req_len, *emb);
5324 if (alloc_len < req_len) {
5325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005326 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005327 "less than the requested DMA memory "
5328 "size (x%x)\n", alloc_len, req_len);
5329 return -ENOMEM;
5330 }
James Smart8a9d2e82012-05-09 21:16:12 -04005331 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005332 if (unlikely(rc))
5333 return -EIO;
5334
5335 if (!phba->sli4_hba.intr_enable)
5336 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5337 else {
James Smarta183a152011-10-10 21:32:43 -04005338 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005339 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5340 }
5341
5342 if (unlikely(rc))
5343 rc = -EIO;
5344 return rc;
5345}
5346
5347/**
5348 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5349 * @phba: Pointer to HBA context object.
5350 * @type: The resource extent type to allocate.
5351 *
5352 * This function allocates the number of elements for the specified
5353 * resource type.
5354 **/
5355static int
5356lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5357{
5358 bool emb = false;
5359 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5360 uint16_t rsrc_id, rsrc_start, j, k;
5361 uint16_t *ids;
5362 int i, rc;
5363 unsigned long longs;
5364 unsigned long *bmask;
5365 struct lpfc_rsrc_blks *rsrc_blks;
5366 LPFC_MBOXQ_t *mbox;
5367 uint32_t length;
5368 struct lpfc_id_range *id_array = NULL;
5369 void *virtaddr = NULL;
5370 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5371 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5372 struct list_head *ext_blk_list;
5373
5374 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5375 &rsrc_cnt,
5376 &rsrc_size);
5377 if (unlikely(rc))
5378 return -EIO;
5379
5380 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5381 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5382 "3009 No available Resource Extents "
5383 "for resource type 0x%x: Count: 0x%x, "
5384 "Size 0x%x\n", type, rsrc_cnt,
5385 rsrc_size);
5386 return -ENOMEM;
5387 }
5388
James Smart8a9d2e82012-05-09 21:16:12 -04005389 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5390 "2903 Post resource extents type-0x%x: "
5391 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005392
5393 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5394 if (!mbox)
5395 return -ENOMEM;
5396
James Smart8a9d2e82012-05-09 21:16:12 -04005397 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005398 if (unlikely(rc)) {
5399 rc = -EIO;
5400 goto err_exit;
5401 }
5402
5403 /*
5404 * Figure out where the response is located. Then get local pointers
5405 * to the response data. The port does not guarantee to respond to
5406 * all extents counts request so update the local variable with the
5407 * allocated count from the port.
5408 */
5409 if (emb == LPFC_SLI4_MBX_EMBED) {
5410 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5411 id_array = &rsrc_ext->u.rsp.id[0];
5412 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5413 } else {
5414 virtaddr = mbox->sge_array->addr[0];
5415 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5416 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5417 id_array = &n_rsrc->id;
5418 }
5419
5420 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5421 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5422
5423 /*
5424 * Based on the resource size and count, correct the base and max
5425 * resource values.
5426 */
5427 length = sizeof(struct lpfc_rsrc_blks);
5428 switch (type) {
5429 case LPFC_RSC_TYPE_FCOE_RPI:
5430 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5431 sizeof(unsigned long),
5432 GFP_KERNEL);
5433 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5434 rc = -ENOMEM;
5435 goto err_exit;
5436 }
5437 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5438 sizeof(uint16_t),
5439 GFP_KERNEL);
5440 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5441 kfree(phba->sli4_hba.rpi_bmask);
5442 rc = -ENOMEM;
5443 goto err_exit;
5444 }
5445
5446 /*
5447 * The next_rpi was initialized with the maximum available
5448 * count but the port may allocate a smaller number. Catch
5449 * that case and update the next_rpi.
5450 */
5451 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5452
5453 /* Initialize local ptrs for common extent processing later. */
5454 bmask = phba->sli4_hba.rpi_bmask;
5455 ids = phba->sli4_hba.rpi_ids;
5456 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5457 break;
5458 case LPFC_RSC_TYPE_FCOE_VPI:
5459 phba->vpi_bmask = kzalloc(longs *
5460 sizeof(unsigned long),
5461 GFP_KERNEL);
5462 if (unlikely(!phba->vpi_bmask)) {
5463 rc = -ENOMEM;
5464 goto err_exit;
5465 }
5466 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5467 sizeof(uint16_t),
5468 GFP_KERNEL);
5469 if (unlikely(!phba->vpi_ids)) {
5470 kfree(phba->vpi_bmask);
5471 rc = -ENOMEM;
5472 goto err_exit;
5473 }
5474
5475 /* Initialize local ptrs for common extent processing later. */
5476 bmask = phba->vpi_bmask;
5477 ids = phba->vpi_ids;
5478 ext_blk_list = &phba->lpfc_vpi_blk_list;
5479 break;
5480 case LPFC_RSC_TYPE_FCOE_XRI:
5481 phba->sli4_hba.xri_bmask = kzalloc(longs *
5482 sizeof(unsigned long),
5483 GFP_KERNEL);
5484 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5485 rc = -ENOMEM;
5486 goto err_exit;
5487 }
James Smart8a9d2e82012-05-09 21:16:12 -04005488 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005489 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5490 sizeof(uint16_t),
5491 GFP_KERNEL);
5492 if (unlikely(!phba->sli4_hba.xri_ids)) {
5493 kfree(phba->sli4_hba.xri_bmask);
5494 rc = -ENOMEM;
5495 goto err_exit;
5496 }
5497
5498 /* Initialize local ptrs for common extent processing later. */
5499 bmask = phba->sli4_hba.xri_bmask;
5500 ids = phba->sli4_hba.xri_ids;
5501 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5502 break;
5503 case LPFC_RSC_TYPE_FCOE_VFI:
5504 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5505 sizeof(unsigned long),
5506 GFP_KERNEL);
5507 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5508 rc = -ENOMEM;
5509 goto err_exit;
5510 }
5511 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5512 sizeof(uint16_t),
5513 GFP_KERNEL);
5514 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5515 kfree(phba->sli4_hba.vfi_bmask);
5516 rc = -ENOMEM;
5517 goto err_exit;
5518 }
5519
5520 /* Initialize local ptrs for common extent processing later. */
5521 bmask = phba->sli4_hba.vfi_bmask;
5522 ids = phba->sli4_hba.vfi_ids;
5523 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5524 break;
5525 default:
5526 /* Unsupported Opcode. Fail call. */
5527 id_array = NULL;
5528 bmask = NULL;
5529 ids = NULL;
5530 ext_blk_list = NULL;
5531 goto err_exit;
5532 }
5533
5534 /*
5535 * Complete initializing the extent configuration with the
5536 * allocated ids assigned to this function. The bitmask serves
5537 * as an index into the array and manages the available ids. The
5538 * array just stores the ids communicated to the port via the wqes.
5539 */
5540 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5541 if ((i % 2) == 0)
5542 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5543 &id_array[k]);
5544 else
5545 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5546 &id_array[k]);
5547
5548 rsrc_blks = kzalloc(length, GFP_KERNEL);
5549 if (unlikely(!rsrc_blks)) {
5550 rc = -ENOMEM;
5551 kfree(bmask);
5552 kfree(ids);
5553 goto err_exit;
5554 }
5555 rsrc_blks->rsrc_start = rsrc_id;
5556 rsrc_blks->rsrc_size = rsrc_size;
5557 list_add_tail(&rsrc_blks->list, ext_blk_list);
5558 rsrc_start = rsrc_id;
5559 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5560 phba->sli4_hba.scsi_xri_start = rsrc_start +
5561 lpfc_sli4_get_els_iocb_cnt(phba);
5562
5563 while (rsrc_id < (rsrc_start + rsrc_size)) {
5564 ids[j] = rsrc_id;
5565 rsrc_id++;
5566 j++;
5567 }
5568 /* Entire word processed. Get next word.*/
5569 if ((i % 2) == 1)
5570 k++;
5571 }
5572 err_exit:
5573 lpfc_sli4_mbox_cmd_free(phba, mbox);
5574 return rc;
5575}
5576
5577/**
5578 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5579 * @phba: Pointer to HBA context object.
5580 * @type: the extent's type.
5581 *
5582 * This function deallocates all extents of a particular resource type.
5583 * SLI4 does not allow for deallocating a particular extent range. It
5584 * is the caller's responsibility to release all kernel memory resources.
5585 **/
5586static int
5587lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5588{
5589 int rc;
5590 uint32_t length, mbox_tmo = 0;
5591 LPFC_MBOXQ_t *mbox;
5592 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5593 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5594
5595 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5596 if (!mbox)
5597 return -ENOMEM;
5598
5599 /*
5600 * This function sends an embedded mailbox because it only sends the
5601 * the resource type. All extents of this type are released by the
5602 * port.
5603 */
5604 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5605 sizeof(struct lpfc_sli4_cfg_mhdr));
5606 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5607 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5608 length, LPFC_SLI4_MBX_EMBED);
5609
5610 /* Send an extents count of 0 - the dealloc doesn't use it. */
5611 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5612 LPFC_SLI4_MBX_EMBED);
5613 if (unlikely(rc)) {
5614 rc = -EIO;
5615 goto out_free_mbox;
5616 }
5617 if (!phba->sli4_hba.intr_enable)
5618 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5619 else {
James Smarta183a152011-10-10 21:32:43 -04005620 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005621 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5622 }
5623 if (unlikely(rc)) {
5624 rc = -EIO;
5625 goto out_free_mbox;
5626 }
5627
5628 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5629 if (bf_get(lpfc_mbox_hdr_status,
5630 &dealloc_rsrc->header.cfg_shdr.response)) {
5631 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5632 "2919 Failed to release resource extents "
5633 "for type %d - Status 0x%x Add'l Status 0x%x. "
5634 "Resource memory not released.\n",
5635 type,
5636 bf_get(lpfc_mbox_hdr_status,
5637 &dealloc_rsrc->header.cfg_shdr.response),
5638 bf_get(lpfc_mbox_hdr_add_status,
5639 &dealloc_rsrc->header.cfg_shdr.response));
5640 rc = -EIO;
5641 goto out_free_mbox;
5642 }
5643
5644 /* Release kernel memory resources for the specific type. */
5645 switch (type) {
5646 case LPFC_RSC_TYPE_FCOE_VPI:
5647 kfree(phba->vpi_bmask);
5648 kfree(phba->vpi_ids);
5649 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5650 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5651 &phba->lpfc_vpi_blk_list, list) {
5652 list_del_init(&rsrc_blk->list);
5653 kfree(rsrc_blk);
5654 }
James Smart16a3a202013-04-17 20:14:38 -04005655 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005656 break;
5657 case LPFC_RSC_TYPE_FCOE_XRI:
5658 kfree(phba->sli4_hba.xri_bmask);
5659 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005660 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5661 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5662 list_del_init(&rsrc_blk->list);
5663 kfree(rsrc_blk);
5664 }
5665 break;
5666 case LPFC_RSC_TYPE_FCOE_VFI:
5667 kfree(phba->sli4_hba.vfi_bmask);
5668 kfree(phba->sli4_hba.vfi_ids);
5669 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5670 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5671 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5672 list_del_init(&rsrc_blk->list);
5673 kfree(rsrc_blk);
5674 }
5675 break;
5676 case LPFC_RSC_TYPE_FCOE_RPI:
5677 /* RPI bitmask and physical id array are cleaned up earlier. */
5678 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5679 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5680 list_del_init(&rsrc_blk->list);
5681 kfree(rsrc_blk);
5682 }
5683 break;
5684 default:
5685 break;
5686 }
5687
5688 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5689
5690 out_free_mbox:
5691 mempool_free(mbox, phba->mbox_mem_pool);
5692 return rc;
5693}
5694
James Smart65791f12016-07-06 12:35:56 -07005695void
James Smart7bdedb32016-07-06 12:36:00 -07005696lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5697 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005698{
James Smart65791f12016-07-06 12:35:56 -07005699 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005700
James Smart65791f12016-07-06 12:35:56 -07005701 len = sizeof(struct lpfc_mbx_set_feature) -
5702 sizeof(struct lpfc_sli4_cfg_mhdr);
5703 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5704 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5705 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005706
James Smart7bdedb32016-07-06 12:36:00 -07005707 switch (feature) {
5708 case LPFC_SET_UE_RECOVERY:
5709 bf_set(lpfc_mbx_set_feature_UER,
5710 &mbox->u.mqe.un.set_feature, 1);
5711 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5712 mbox->u.mqe.un.set_feature.param_len = 8;
5713 break;
5714 case LPFC_SET_MDS_DIAGS:
5715 bf_set(lpfc_mbx_set_feature_mds,
5716 &mbox->u.mqe.un.set_feature, 1);
5717 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5718 &mbox->u.mqe.un.set_feature, 0);
5719 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5720 mbox->u.mqe.un.set_feature.param_len = 8;
5721 break;
James Smart65791f12016-07-06 12:35:56 -07005722 }
James Smart7bdedb32016-07-06 12:36:00 -07005723
5724 return;
James Smart65791f12016-07-06 12:35:56 -07005725}
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) ==
James Smart7bdedb32016-07-06 12:36:00 -07006452 LPFC_SLI_INTF_IF_TYPE_0) {
6453 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6454 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6455 if (rc == MBX_SUCCESS) {
6456 phba->hba_flag |= HBA_RECOVERABLE_UE;
6457 /* Set 1Sec interval to detect UE */
6458 phba->eratt_poll_interval = 1;
6459 phba->sli4_hba.ue_to_sr = bf_get(
6460 lpfc_mbx_set_feature_UESR,
6461 &mboxq->u.mqe.un.set_feature);
6462 phba->sli4_hba.ue_to_rp = bf_get(
6463 lpfc_mbx_set_feature_UERP,
6464 &mboxq->u.mqe.un.set_feature);
6465 }
6466 }
6467
6468 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6469 /* Enable MDS Diagnostics only if the SLI Port supports it */
6470 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6471 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6472 if (rc != MBX_SUCCESS)
6473 phba->mds_diags_support = 0;
6474 }
James Smart572709e2013-07-15 18:32:43 -04006475
James Smartda0436e2009-05-22 14:51:39 -04006476 /*
6477 * Discover the port's supported feature set and match it against the
6478 * hosts requests.
6479 */
6480 lpfc_request_features(phba, mboxq);
6481 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6482 if (unlikely(rc)) {
6483 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006484 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006485 }
6486
6487 /*
6488 * The port must support FCP initiator mode as this is the
6489 * only mode running in the host.
6490 */
6491 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6492 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6493 "0378 No support for fcpi mode.\n");
6494 ftr_rsp++;
6495 }
James Smartfedd3b72011-02-16 12:39:24 -05006496 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6497 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6498 else
6499 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006500 /*
6501 * If the port cannot support the host's requested features
6502 * then turn off the global config parameters to disable the
6503 * feature in the driver. This is not a fatal error.
6504 */
James Smartbf086112011-08-21 21:48:13 -04006505 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6506 if (phba->cfg_enable_bg) {
6507 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6508 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6509 else
6510 ftr_rsp++;
6511 }
James Smartda0436e2009-05-22 14:51:39 -04006512
6513 if (phba->max_vpi && phba->cfg_enable_npiv &&
6514 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6515 ftr_rsp++;
6516
6517 if (ftr_rsp) {
6518 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6519 "0379 Feature Mismatch Data: x%08x %08x "
6520 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6521 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6522 phba->cfg_enable_npiv, phba->max_vpi);
6523 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6524 phba->cfg_enable_bg = 0;
6525 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6526 phba->cfg_enable_npiv = 0;
6527 }
6528
6529 /* These SLI3 features are assumed in SLI4 */
6530 spin_lock_irq(&phba->hbalock);
6531 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6532 spin_unlock_irq(&phba->hbalock);
6533
James Smart6d368e52011-05-24 11:44:12 -04006534 /*
6535 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6536 * calls depends on these resources to complete port setup.
6537 */
6538 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6539 if (rc) {
6540 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6541 "2920 Failed to alloc Resource IDs "
6542 "rc = x%x\n", rc);
6543 goto out_free_mbox;
6544 }
6545
James Smartda0436e2009-05-22 14:51:39 -04006546 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006547 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6548 if (rc) {
6549 phba->link_state = LPFC_HBA_ERROR;
6550 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006551 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006552 }
6553
James Smartda0436e2009-05-22 14:51:39 -04006554 mboxq->vport = vport;
6555 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6556 mp = (struct lpfc_dmabuf *) mboxq->context1;
6557 if (rc == MBX_SUCCESS) {
6558 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6559 rc = 0;
6560 }
6561
6562 /*
6563 * This memory was allocated by the lpfc_read_sparam routine. Release
6564 * it to the mbuf pool.
6565 */
6566 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6567 kfree(mp);
6568 mboxq->context1 = NULL;
6569 if (unlikely(rc)) {
6570 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6571 "0382 READ_SPARAM command failed "
6572 "status %d, mbxStatus x%x\n",
6573 rc, bf_get(lpfc_mqe_status, mqe));
6574 phba->link_state = LPFC_HBA_ERROR;
6575 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006576 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006577 }
6578
James Smart05580562011-05-24 11:40:48 -04006579 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006580
6581 /* Update the fc_host data structures with new wwn. */
6582 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6583 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6584
James Smart8a9d2e82012-05-09 21:16:12 -04006585 /* update host els and scsi xri-sgl sizes and mappings */
6586 rc = lpfc_sli4_xri_sgl_update(phba);
6587 if (unlikely(rc)) {
6588 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6589 "1400 Failed to update xri-sgl size and "
6590 "mapping: %d\n", rc);
6591 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006592 }
6593
James Smart8a9d2e82012-05-09 21:16:12 -04006594 /* register the els sgl pool to the port */
6595 rc = lpfc_sli4_repost_els_sgl_list(phba);
6596 if (unlikely(rc)) {
6597 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6598 "0582 Error %d during els sgl post "
6599 "operation\n", rc);
6600 rc = -ENODEV;
6601 goto out_free_mbox;
6602 }
6603
6604 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006605 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6606 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006607 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006608 "0383 Error %d during scsi sgl post "
6609 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006610 /* Some Scsi buffers were moved to the abort scsi list */
6611 /* A pci function reset will repost them */
6612 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006613 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006614 }
6615
6616 /* Post the rpi header region to the device. */
6617 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6618 if (unlikely(rc)) {
6619 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6620 "0393 Error %d during rpi post operation\n",
6621 rc);
6622 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006623 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006624 }
James Smart97f2ecf2012-03-01 22:35:23 -05006625 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006626
James Smart5350d872011-10-10 21:33:49 -04006627 /* Create all the SLI4 queues */
6628 rc = lpfc_sli4_queue_create(phba);
6629 if (rc) {
6630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6631 "3089 Failed to allocate queues\n");
6632 rc = -ENODEV;
6633 goto out_stop_timers;
6634 }
James Smartda0436e2009-05-22 14:51:39 -04006635 /* Set up all the queues to the device */
6636 rc = lpfc_sli4_queue_setup(phba);
6637 if (unlikely(rc)) {
6638 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6639 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006640 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006641 }
6642
6643 /* Arm the CQs and then EQs on device */
6644 lpfc_sli4_arm_cqeq_intr(phba);
6645
6646 /* Indicate device interrupt mode */
6647 phba->sli4_hba.intr_enable = 1;
6648
6649 /* Allow asynchronous mailbox command to go through */
6650 spin_lock_irq(&phba->hbalock);
6651 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6652 spin_unlock_irq(&phba->hbalock);
6653
6654 /* Post receive buffers to the device */
6655 lpfc_sli4_rb_setup(phba);
6656
James Smartfc2b9892010-02-26 14:15:29 -05006657 /* Reset HBA FCF states after HBA reset */
6658 phba->fcf.fcf_flag = 0;
6659 phba->fcf.current_rec.flag = 0;
6660
James Smartda0436e2009-05-22 14:51:39 -04006661 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006662 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006663 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006664
6665 /* Start heart beat timer */
6666 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006667 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006668 phba->hb_outstanding = 0;
6669 phba->last_completion_time = jiffies;
6670
6671 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006672 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006673 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006674
James Smart75baf692010-06-08 18:31:21 -04006675 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6676 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6677 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6678 if (!rc) {
6679 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6680 "2829 This device supports "
6681 "Advanced Error Reporting (AER)\n");
6682 spin_lock_irq(&phba->hbalock);
6683 phba->hba_flag |= HBA_AER_ENABLED;
6684 spin_unlock_irq(&phba->hbalock);
6685 } else {
6686 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6687 "2830 This device does not support "
6688 "Advanced Error Reporting (AER)\n");
6689 phba->cfg_aer_support = 0;
6690 }
James Smart0a96e972011-07-22 18:37:28 -04006691 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006692 }
6693
James Smart76a95d72010-11-20 23:11:48 -05006694 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6695 /*
6696 * The FC Port needs to register FCFI (index 0)
6697 */
6698 lpfc_reg_fcfi(phba, mboxq);
6699 mboxq->vport = phba->pport;
6700 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006701 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006702 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006703 rc = 0;
6704 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6705 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006706
6707 /* Check if the port is configured to be disabled */
6708 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006709 }
James Smart026abb82011-12-13 13:20:45 -05006710
James Smartda0436e2009-05-22 14:51:39 -04006711 /*
6712 * The port is ready, set the host's link state to LINK_DOWN
6713 * in preparation for link interrupts.
6714 */
James Smartda0436e2009-05-22 14:51:39 -04006715 spin_lock_irq(&phba->hbalock);
6716 phba->link_state = LPFC_LINK_DOWN;
6717 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006718 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6719 (phba->hba_flag & LINK_DISABLED)) {
6720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6721 "3103 Adapter Link is disabled.\n");
6722 lpfc_down_link(phba, mboxq);
6723 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6724 if (rc != MBX_SUCCESS) {
6725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6726 "3104 Adapter failed to issue "
6727 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6728 goto out_unset_queue;
6729 }
6730 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006731 /* don't perform init_link on SLI4 FC port loopback test */
6732 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6733 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6734 if (rc)
6735 goto out_unset_queue;
6736 }
James Smart5350d872011-10-10 21:33:49 -04006737 }
6738 mempool_free(mboxq, phba->mbox_mem_pool);
6739 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006740out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006741 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006742 lpfc_sli4_queue_unset(phba);
6743out_destroy_queue:
6744 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006745out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006746 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006747out_free_mbox:
6748 mempool_free(mboxq, phba->mbox_mem_pool);
6749 return rc;
6750}
James Smarte59058c2008-08-24 21:49:00 -04006751
6752/**
James Smart3621a712009-04-06 18:47:14 -04006753 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006754 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006755 *
James Smarte59058c2008-08-24 21:49:00 -04006756 * This is the callback function for mailbox timer. The mailbox
6757 * timer is armed when a new mailbox command is issued and the timer
6758 * is deleted when the mailbox complete. The function is called by
6759 * the kernel timer code when a mailbox does not complete within
6760 * expected time. This function wakes up the worker thread to
6761 * process the mailbox timeout and returns. All the processing is
6762 * done by the worker thread function lpfc_mbox_timeout_handler.
6763 **/
dea31012005-04-17 16:05:31 -05006764void
6765lpfc_mbox_timeout(unsigned long ptr)
6766{
James Smart92d7f7b2007-06-17 19:56:38 -05006767 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006768 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006769 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006770
James Smart2e0fef82007-06-17 19:56:36 -05006771 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006772 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006773 if (!tmo_posted)
6774 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6775 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6776
James Smart5e9d9b82008-06-14 22:52:53 -04006777 if (!tmo_posted)
6778 lpfc_worker_wake_up(phba);
6779 return;
dea31012005-04-17 16:05:31 -05006780}
6781
James Smarte8d3c3b2013-10-10 12:21:30 -04006782/**
6783 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6784 * are pending
6785 * @phba: Pointer to HBA context object.
6786 *
6787 * This function checks if any mailbox completions are present on the mailbox
6788 * completion queue.
6789 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006790static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006791lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6792{
6793
6794 uint32_t idx;
6795 struct lpfc_queue *mcq;
6796 struct lpfc_mcqe *mcqe;
6797 bool pending_completions = false;
6798
6799 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6800 return false;
6801
6802 /* Check for completions on mailbox completion queue */
6803
6804 mcq = phba->sli4_hba.mbx_cq;
6805 idx = mcq->hba_index;
6806 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6807 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6808 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6809 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6810 pending_completions = true;
6811 break;
6812 }
6813 idx = (idx + 1) % mcq->entry_count;
6814 if (mcq->hba_index == idx)
6815 break;
6816 }
6817 return pending_completions;
6818
6819}
6820
6821/**
6822 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6823 * that were missed.
6824 * @phba: Pointer to HBA context object.
6825 *
6826 * For sli4, it is possible to miss an interrupt. As such mbox completions
6827 * maybe missed causing erroneous mailbox timeouts to occur. This function
6828 * checks to see if mbox completions are on the mailbox completion queue
6829 * and will process all the completions associated with the eq for the
6830 * mailbox completion queue.
6831 **/
6832bool
6833lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6834{
6835
6836 uint32_t eqidx;
6837 struct lpfc_queue *fpeq = NULL;
6838 struct lpfc_eqe *eqe;
6839 bool mbox_pending;
6840
6841 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6842 return false;
6843
6844 /* Find the eq associated with the mcq */
6845
6846 if (phba->sli4_hba.hba_eq)
6847 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6848 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6849 phba->sli4_hba.mbx_cq->assoc_qid) {
6850 fpeq = phba->sli4_hba.hba_eq[eqidx];
6851 break;
6852 }
6853 if (!fpeq)
6854 return false;
6855
6856 /* Turn off interrupts from this EQ */
6857
6858 lpfc_sli4_eq_clr_intr(fpeq);
6859
6860 /* Check to see if a mbox completion is pending */
6861
6862 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6863
6864 /*
6865 * If a mbox completion is pending, process all the events on EQ
6866 * associated with the mbox completion queue (this could include
6867 * mailbox commands, async events, els commands, receive queue data
6868 * and fcp commands)
6869 */
6870
6871 if (mbox_pending)
6872 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6873 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6874 fpeq->EQ_processed++;
6875 }
6876
6877 /* Always clear and re-arm the EQ */
6878
6879 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6880
6881 return mbox_pending;
6882
6883}
James Smarte59058c2008-08-24 21:49:00 -04006884
6885/**
James Smart3621a712009-04-06 18:47:14 -04006886 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006887 * @phba: Pointer to HBA context object.
6888 *
6889 * This function is called from worker thread when a mailbox command times out.
6890 * The caller is not required to hold any locks. This function will reset the
6891 * HBA and recover all the pending commands.
6892 **/
dea31012005-04-17 16:05:31 -05006893void
6894lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6895{
James Smart2e0fef82007-06-17 19:56:36 -05006896 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04006897 MAILBOX_t *mb = NULL;
6898
James Smart1dcb58e2007-04-25 09:51:30 -04006899 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006900
James Smarte8d3c3b2013-10-10 12:21:30 -04006901 /* If the mailbox completed, process the completion and return */
6902 if (lpfc_sli4_process_missed_mbox_completions(phba))
6903 return;
6904
James Smarteb016562014-09-03 12:58:06 -04006905 if (pmbox != NULL)
6906 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04006907 /* Check the pmbox pointer first. There is a race condition
6908 * between the mbox timeout handler getting executed in the
6909 * worklist and the mailbox actually completing. When this
6910 * race condition occurs, the mbox_active will be NULL.
6911 */
6912 spin_lock_irq(&phba->hbalock);
6913 if (pmbox == NULL) {
6914 lpfc_printf_log(phba, KERN_WARNING,
6915 LOG_MBOX | LOG_SLI,
6916 "0353 Active Mailbox cleared - mailbox timeout "
6917 "exiting\n");
6918 spin_unlock_irq(&phba->hbalock);
6919 return;
6920 }
6921
dea31012005-04-17 16:05:31 -05006922 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006923 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006924 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006925 mb->mbxCommand,
6926 phba->pport->port_state,
6927 phba->sli.sli_flag,
6928 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006929 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006930
James Smart1dcb58e2007-04-25 09:51:30 -04006931 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6932 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006933 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006934 */
James Smart2e0fef82007-06-17 19:56:36 -05006935 spin_lock_irq(&phba->pport->work_port_lock);
6936 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6937 spin_unlock_irq(&phba->pport->work_port_lock);
6938 spin_lock_irq(&phba->hbalock);
6939 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006940 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006941 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006942
James Smartdb55fba2014-04-04 13:52:02 -04006943 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04006944
6945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006946 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006947
6948 /* Reset the HBA device */
6949 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006950}
6951
James Smarte59058c2008-08-24 21:49:00 -04006952/**
James Smart3772a992009-05-22 14:50:54 -04006953 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006954 * @phba: Pointer to HBA context object.
6955 * @pmbox: Pointer to mailbox object.
6956 * @flag: Flag indicating how the mailbox need to be processed.
6957 *
6958 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006959 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6960 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006961 * The mailbox command can be submitted in polling mode, in which case
6962 * this function will wait in a polling loop for the completion of the
6963 * mailbox.
6964 * If the mailbox is submitted in no_wait mode (not polling) the
6965 * function will submit the command and returns immediately without waiting
6966 * for the mailbox completion. The no_wait is supported only when HBA
6967 * is in SLI2/SLI3 mode - interrupts are enabled.
6968 * The SLI interface allows only one mailbox pending at a time. If the
6969 * mailbox is issued in polling mode and there is already a mailbox
6970 * pending, then the function will return an error. If the mailbox is issued
6971 * in NO_WAIT mode and there is a mailbox pending already, the function
6972 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6973 * The sli layer owns the mailbox object until the completion of mailbox
6974 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6975 * return codes the caller owns the mailbox command after the return of
6976 * the function.
6977 **/
James Smart3772a992009-05-22 14:50:54 -04006978static int
6979lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6980 uint32_t flag)
dea31012005-04-17 16:05:31 -05006981{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006982 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006983 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006984 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006985 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006986 int i;
James Smart09372822008-01-11 01:52:54 -05006987 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006988 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006989 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006990 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006991 int processing_queue = 0;
6992
6993 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6994 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006995 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006996 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006997 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6998 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6999 return MBX_SUCCESS;
7000 }
James Smart58da1ff2008-04-07 10:15:56 -04007001 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007002 pmbox = lpfc_mbox_get(phba);
7003 if (!pmbox) {
7004 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7005 return MBX_SUCCESS;
7006 }
7007 }
dea31012005-04-17 16:05:31 -05007008
James Smarted957682007-06-17 19:56:37 -05007009 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007010 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007011 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007012 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007013 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007014 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007015 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007016 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007017 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007018 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007019 }
7020 }
7021
Linas Vepstas8d63f372007-02-14 14:28:36 -06007022 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007023 if (unlikely(pci_channel_offline(phba->pcidev))) {
7024 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7025 goto out_not_finished;
7026 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007027
James Smarta257bf92009-04-06 18:48:10 -04007028 /* If HBA has a deferred error attention, fail the iocb. */
7029 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7030 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7031 goto out_not_finished;
7032 }
7033
dea31012005-04-17 16:05:31 -05007034 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007035
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007036 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007037 status = MBX_SUCCESS;
7038
James Smart2e0fef82007-06-17 19:56:36 -05007039 if (phba->link_state == LPFC_HBA_ERROR) {
7040 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007041
7042 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007043 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7044 "(%d):0311 Mailbox command x%x cannot "
7045 "issue Data: x%x x%x\n",
7046 pmbox->vport ? pmbox->vport->vpi : 0,
7047 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007048 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007049 }
7050
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007051 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007052 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7053 !(hc_copy & HC_MBINT_ENA)) {
7054 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7055 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007056 "(%d):2528 Mailbox command x%x cannot "
7057 "issue Data: x%x x%x\n",
7058 pmbox->vport ? pmbox->vport->vpi : 0,
7059 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007060 goto out_not_finished;
7061 }
James Smart92908312006-03-07 15:04:13 -05007062 }
7063
dea31012005-04-17 16:05:31 -05007064 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7065 /* Polling for a mbox command when another one is already active
7066 * is not allowed in SLI. Also, the driver must have established
7067 * SLI2 mode to queue and process multiple mbox commands.
7068 */
7069
7070 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007071 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007072
7073 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007074 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7075 "(%d):2529 Mailbox command x%x "
7076 "cannot issue Data: x%x x%x\n",
7077 pmbox->vport ? pmbox->vport->vpi : 0,
7078 pmbox->u.mb.mbxCommand,
7079 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007080 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007081 }
7082
James Smart3772a992009-05-22 14:50:54 -04007083 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007084 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007085 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007086 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7087 "(%d):2530 Mailbox command x%x "
7088 "cannot issue Data: x%x x%x\n",
7089 pmbox->vport ? pmbox->vport->vpi : 0,
7090 pmbox->u.mb.mbxCommand,
7091 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007092 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007093 }
7094
dea31012005-04-17 16:05:31 -05007095 /* Another mailbox command is still being processed, queue this
7096 * command to be processed later.
7097 */
7098 lpfc_mbox_put(phba, pmbox);
7099
7100 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007101 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007102 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007103 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007104 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007105 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007106 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007107
7108 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007109 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007110
James Smart858c9f62007-06-17 19:56:39 -05007111 if (pmbox->vport) {
7112 lpfc_debugfs_disc_trc(pmbox->vport,
7113 LPFC_DISC_TRC_MBOX_VPORT,
7114 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007115 (uint32_t)mbx->mbxCommand,
7116 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007117 }
7118 else {
7119 lpfc_debugfs_disc_trc(phba->pport,
7120 LPFC_DISC_TRC_MBOX,
7121 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007122 (uint32_t)mbx->mbxCommand,
7123 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007124 }
7125
James Smart2e0fef82007-06-17 19:56:36 -05007126 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007127 }
7128
dea31012005-04-17 16:05:31 -05007129 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7130
7131 /* If we are not polling, we MUST be in SLI2 mode */
7132 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007133 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007134 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007135 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007136 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007137 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007138 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7139 "(%d):2531 Mailbox command x%x "
7140 "cannot issue Data: x%x x%x\n",
7141 pmbox->vport ? pmbox->vport->vpi : 0,
7142 pmbox->u.mb.mbxCommand,
7143 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007144 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007145 }
7146 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007147 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7148 1000);
7149 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007150 }
7151
7152 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007153 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007154 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007155 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007156 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007157 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007158 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007159
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007160 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007161 if (pmbox->vport) {
7162 lpfc_debugfs_disc_trc(pmbox->vport,
7163 LPFC_DISC_TRC_MBOX_VPORT,
7164 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007165 (uint32_t)mbx->mbxCommand,
7166 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007167 }
7168 else {
7169 lpfc_debugfs_disc_trc(phba->pport,
7170 LPFC_DISC_TRC_MBOX,
7171 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007172 (uint32_t)mbx->mbxCommand,
7173 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007174 }
7175 }
7176
dea31012005-04-17 16:05:31 -05007177 psli->slistat.mbox_cmd++;
7178 evtctr = psli->slistat.mbox_event;
7179
7180 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007181 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007182
James Smart3772a992009-05-22 14:50:54 -04007183 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007184 /* Populate mbox extension offset word. */
7185 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007186 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007187 = (uint8_t *)phba->mbox_ext
7188 - (uint8_t *)phba->mbox;
7189 }
7190
7191 /* Copy the mailbox extension data */
7192 if (pmbox->in_ext_byte_len && pmbox->context2) {
7193 lpfc_sli_pcimem_bcopy(pmbox->context2,
7194 (uint8_t *)phba->mbox_ext,
7195 pmbox->in_ext_byte_len);
7196 }
7197 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007198 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007199 } else {
James Smart7a470272010-03-15 11:25:20 -04007200 /* Populate mbox extension offset word. */
7201 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007202 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007203 = MAILBOX_HBA_EXT_OFFSET;
7204
7205 /* Copy the mailbox extension data */
7206 if (pmbox->in_ext_byte_len && pmbox->context2) {
7207 lpfc_memcpy_to_slim(phba->MBslimaddr +
7208 MAILBOX_HBA_EXT_OFFSET,
7209 pmbox->context2, pmbox->in_ext_byte_len);
7210
7211 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007212 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007213 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007214 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007215 }
7216
7217 /* First copy mbox command data to HBA SLIM, skip past first
7218 word */
7219 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007220 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007221 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7222
7223 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007224 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007225 to_slim = phba->MBslimaddr;
7226 writel(ldata, to_slim);
7227 readl(to_slim); /* flush */
7228
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007229 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007230 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007231 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007232 }
7233 }
7234
7235 wmb();
dea31012005-04-17 16:05:31 -05007236
7237 switch (flag) {
7238 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007239 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007240 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007241 /* Interrupt board to do it */
7242 writel(CA_MBATT, phba->CAregaddr);
7243 readl(phba->CAregaddr); /* flush */
7244 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007245 break;
7246
7247 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007248 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007249 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007250 /* Interrupt board to do it */
7251 writel(CA_MBATT, phba->CAregaddr);
7252 readl(phba->CAregaddr); /* flush */
7253
James Smart3772a992009-05-22 14:50:54 -04007254 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007255 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007256 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007257 word0 = le32_to_cpu(word0);
7258 } else {
7259 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007260 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7261 spin_unlock_irqrestore(&phba->hbalock,
7262 drvr_flag);
7263 goto out_not_finished;
7264 }
dea31012005-04-17 16:05:31 -05007265 }
7266
7267 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007268 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7269 spin_unlock_irqrestore(&phba->hbalock,
7270 drvr_flag);
7271 goto out_not_finished;
7272 }
James Smarta183a152011-10-10 21:32:43 -04007273 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7274 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007275 i = 0;
dea31012005-04-17 16:05:31 -05007276 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007277 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7278 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007279 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007280 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007281 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007282 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007283 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007284 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007285 }
7286
7287 /* Check if we took a mbox interrupt while we were
7288 polling */
7289 if (((word0 & OWN_CHIP) != OWN_CHIP)
7290 && (evtctr != psli->slistat.mbox_event))
7291 break;
7292
James Smart09372822008-01-11 01:52:54 -05007293 if (i++ > 10) {
7294 spin_unlock_irqrestore(&phba->hbalock,
7295 drvr_flag);
7296 msleep(1);
7297 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7298 }
dea31012005-04-17 16:05:31 -05007299
James Smart3772a992009-05-22 14:50:54 -04007300 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007301 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007302 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007303 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007304 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007305 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007306 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007307 /* Check real SLIM for any errors */
7308 slimword0 = readl(phba->MBslimaddr);
7309 slimmb = (MAILBOX_t *) & slimword0;
7310 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7311 && slimmb->mbxStatus) {
7312 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007313 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007314 word0 = slimword0;
7315 }
7316 }
7317 } else {
7318 /* First copy command data */
7319 word0 = readl(phba->MBslimaddr);
7320 }
7321 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007322 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7323 spin_unlock_irqrestore(&phba->hbalock,
7324 drvr_flag);
7325 goto out_not_finished;
7326 }
dea31012005-04-17 16:05:31 -05007327 }
7328
James Smart3772a992009-05-22 14:50:54 -04007329 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007330 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007331 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007332 /* Copy the mailbox extension data */
7333 if (pmbox->out_ext_byte_len && pmbox->context2) {
7334 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7335 pmbox->context2,
7336 pmbox->out_ext_byte_len);
7337 }
dea31012005-04-17 16:05:31 -05007338 } else {
7339 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007340 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007341 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007342 /* Copy the mailbox extension data */
7343 if (pmbox->out_ext_byte_len && pmbox->context2) {
7344 lpfc_memcpy_from_slim(pmbox->context2,
7345 phba->MBslimaddr +
7346 MAILBOX_HBA_EXT_OFFSET,
7347 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007348 }
7349 }
7350
7351 writel(HA_MBATT, phba->HAregaddr);
7352 readl(phba->HAregaddr); /* flush */
7353
7354 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007355 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007356 }
7357
James Smart2e0fef82007-06-17 19:56:36 -05007358 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7359 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007360
7361out_not_finished:
7362 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007363 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007364 lpfc_mbox_cmpl_put(phba, pmbox);
7365 }
7366 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007367}
7368
James Smarte59058c2008-08-24 21:49:00 -04007369/**
James Smartf1126682009-06-10 17:22:44 -04007370 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7371 * @phba: Pointer to HBA context object.
7372 *
7373 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7374 * the driver internal pending mailbox queue. It will then try to wait out the
7375 * possible outstanding mailbox command before return.
7376 *
7377 * Returns:
7378 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7379 * the outstanding mailbox command timed out.
7380 **/
7381static int
7382lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7383{
7384 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007385 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007386 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007387
7388 /* Mark the asynchronous mailbox command posting as blocked */
7389 spin_lock_irq(&phba->hbalock);
7390 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007391 /* Determine how long we might wait for the active mailbox
7392 * command to be gracefully completed by firmware.
7393 */
James Smarta183a152011-10-10 21:32:43 -04007394 if (phba->sli.mbox_active)
7395 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7396 phba->sli.mbox_active) *
7397 1000) + jiffies;
7398 spin_unlock_irq(&phba->hbalock);
7399
James Smarte8d3c3b2013-10-10 12:21:30 -04007400 /* Make sure the mailbox is really active */
7401 if (timeout)
7402 lpfc_sli4_process_missed_mbox_completions(phba);
7403
James Smartf1126682009-06-10 17:22:44 -04007404 /* Wait for the outstnading mailbox command to complete */
7405 while (phba->sli.mbox_active) {
7406 /* Check active mailbox complete status every 2ms */
7407 msleep(2);
7408 if (time_after(jiffies, timeout)) {
7409 /* Timeout, marked the outstanding cmd not complete */
7410 rc = 1;
7411 break;
7412 }
7413 }
7414
7415 /* Can not cleanly block async mailbox command, fails it */
7416 if (rc) {
7417 spin_lock_irq(&phba->hbalock);
7418 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7419 spin_unlock_irq(&phba->hbalock);
7420 }
7421 return rc;
7422}
7423
7424/**
7425 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7426 * @phba: Pointer to HBA context object.
7427 *
7428 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7429 * commands from the driver internal pending mailbox queue. It makes sure
7430 * that there is no outstanding mailbox command before resuming posting
7431 * asynchronous mailbox commands. If, for any reason, there is outstanding
7432 * mailbox command, it will try to wait it out before resuming asynchronous
7433 * mailbox command posting.
7434 **/
7435static void
7436lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7437{
7438 struct lpfc_sli *psli = &phba->sli;
7439
7440 spin_lock_irq(&phba->hbalock);
7441 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7442 /* Asynchronous mailbox posting is not blocked, do nothing */
7443 spin_unlock_irq(&phba->hbalock);
7444 return;
7445 }
7446
7447 /* Outstanding synchronous mailbox command is guaranteed to be done,
7448 * successful or timeout, after timing-out the outstanding mailbox
7449 * command shall always be removed, so just unblock posting async
7450 * mailbox command and resume
7451 */
7452 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7453 spin_unlock_irq(&phba->hbalock);
7454
7455 /* wake up worker thread to post asynchronlous mailbox command */
7456 lpfc_worker_wake_up(phba);
7457}
7458
7459/**
James Smart2d843ed2012-09-29 11:29:06 -04007460 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7461 * @phba: Pointer to HBA context object.
7462 * @mboxq: Pointer to mailbox object.
7463 *
7464 * The function waits for the bootstrap mailbox register ready bit from
7465 * port for twice the regular mailbox command timeout value.
7466 *
7467 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7468 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7469 **/
7470static int
7471lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7472{
7473 uint32_t db_ready;
7474 unsigned long timeout;
7475 struct lpfc_register bmbx_reg;
7476
7477 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7478 * 1000) + jiffies;
7479
7480 do {
7481 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7482 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7483 if (!db_ready)
7484 msleep(2);
7485
7486 if (time_after(jiffies, timeout))
7487 return MBXERR_ERROR;
7488 } while (!db_ready);
7489
7490 return 0;
7491}
7492
7493/**
James Smartda0436e2009-05-22 14:51:39 -04007494 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7495 * @phba: Pointer to HBA context object.
7496 * @mboxq: Pointer to mailbox object.
7497 *
7498 * The function posts a mailbox to the port. The mailbox is expected
7499 * to be comletely filled in and ready for the port to operate on it.
7500 * This routine executes a synchronous completion operation on the
7501 * mailbox by polling for its completion.
7502 *
7503 * The caller must not be holding any locks when calling this routine.
7504 *
7505 * Returns:
7506 * MBX_SUCCESS - mailbox posted successfully
7507 * Any of the MBX error values.
7508 **/
7509static int
7510lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7511{
7512 int rc = MBX_SUCCESS;
7513 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007514 uint32_t mcqe_status;
7515 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007516 struct lpfc_sli *psli = &phba->sli;
7517 struct lpfc_mqe *mb = &mboxq->u.mqe;
7518 struct lpfc_bmbx_create *mbox_rgn;
7519 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007520
7521 /*
7522 * Only one mailbox can be active to the bootstrap mailbox region
7523 * at a time and there is no queueing provided.
7524 */
7525 spin_lock_irqsave(&phba->hbalock, iflag);
7526 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7527 spin_unlock_irqrestore(&phba->hbalock, iflag);
7528 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007529 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007530 "cannot issue Data: x%x x%x\n",
7531 mboxq->vport ? mboxq->vport->vpi : 0,
7532 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007533 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7534 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007535 psli->sli_flag, MBX_POLL);
7536 return MBXERR_ERROR;
7537 }
7538 /* The server grabs the token and owns it until release */
7539 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7540 phba->sli.mbox_active = mboxq;
7541 spin_unlock_irqrestore(&phba->hbalock, iflag);
7542
James Smart2d843ed2012-09-29 11:29:06 -04007543 /* wait for bootstrap mbox register for readyness */
7544 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7545 if (rc)
7546 goto exit;
7547
James Smartda0436e2009-05-22 14:51:39 -04007548 /*
7549 * Initialize the bootstrap memory region to avoid stale data areas
7550 * in the mailbox post. Then copy the caller's mailbox contents to
7551 * the bmbx mailbox region.
7552 */
7553 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7554 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7555 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7556 sizeof(struct lpfc_mqe));
7557
7558 /* Post the high mailbox dma address to the port and wait for ready. */
7559 dma_address = &phba->sli4_hba.bmbx.dma_address;
7560 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7561
James Smart2d843ed2012-09-29 11:29:06 -04007562 /* wait for bootstrap mbox register for hi-address write done */
7563 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7564 if (rc)
7565 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007566
7567 /* Post the low mailbox dma address to the port. */
7568 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007569
James Smart2d843ed2012-09-29 11:29:06 -04007570 /* wait for bootstrap mbox register for low address write done */
7571 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7572 if (rc)
7573 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007574
7575 /*
7576 * Read the CQ to ensure the mailbox has completed.
7577 * If so, update the mailbox status so that the upper layers
7578 * can complete the request normally.
7579 */
7580 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7581 sizeof(struct lpfc_mqe));
7582 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7583 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7584 sizeof(struct lpfc_mcqe));
7585 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007586 /*
7587 * When the CQE status indicates a failure and the mailbox status
7588 * indicates success then copy the CQE status into the mailbox status
7589 * (and prefix it with x4000).
7590 */
James Smartda0436e2009-05-22 14:51:39 -04007591 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007592 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7593 bf_set(lpfc_mqe_status, mb,
7594 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007595 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007596 } else
7597 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007598
7599 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007600 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007601 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7602 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007603 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7604 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7605 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007606 bf_get(lpfc_mqe_status, mb),
7607 mb->un.mb_words[0], mb->un.mb_words[1],
7608 mb->un.mb_words[2], mb->un.mb_words[3],
7609 mb->un.mb_words[4], mb->un.mb_words[5],
7610 mb->un.mb_words[6], mb->un.mb_words[7],
7611 mb->un.mb_words[8], mb->un.mb_words[9],
7612 mb->un.mb_words[10], mb->un.mb_words[11],
7613 mb->un.mb_words[12], mboxq->mcqe.word0,
7614 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7615 mboxq->mcqe.trailer);
7616exit:
7617 /* We are holding the token, no needed for lock when release */
7618 spin_lock_irqsave(&phba->hbalock, iflag);
7619 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7620 phba->sli.mbox_active = NULL;
7621 spin_unlock_irqrestore(&phba->hbalock, iflag);
7622 return rc;
7623}
7624
7625/**
7626 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7627 * @phba: Pointer to HBA context object.
7628 * @pmbox: Pointer to mailbox object.
7629 * @flag: Flag indicating how the mailbox need to be processed.
7630 *
7631 * This function is called by discovery code and HBA management code to submit
7632 * a mailbox command to firmware with SLI-4 interface spec.
7633 *
7634 * Return codes the caller owns the mailbox command after the return of the
7635 * function.
7636 **/
7637static int
7638lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7639 uint32_t flag)
7640{
7641 struct lpfc_sli *psli = &phba->sli;
7642 unsigned long iflags;
7643 int rc;
7644
James Smartb76f2dc2011-07-22 18:37:42 -04007645 /* dump from issue mailbox command if setup */
7646 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7647
James Smart8fa38512009-07-19 10:01:03 -04007648 rc = lpfc_mbox_dev_check(phba);
7649 if (unlikely(rc)) {
7650 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007651 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007652 "cannot issue Data: x%x x%x\n",
7653 mboxq->vport ? mboxq->vport->vpi : 0,
7654 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007655 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7656 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007657 psli->sli_flag, flag);
7658 goto out_not_finished;
7659 }
7660
James Smartda0436e2009-05-22 14:51:39 -04007661 /* Detect polling mode and jump to a handler */
7662 if (!phba->sli4_hba.intr_enable) {
7663 if (flag == MBX_POLL)
7664 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7665 else
7666 rc = -EIO;
7667 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007668 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007669 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007670 "(x%x/x%x) failure: "
7671 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7672 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007673 mboxq->vport ? mboxq->vport->vpi : 0,
7674 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007675 lpfc_sli_config_mbox_subsys_get(phba,
7676 mboxq),
7677 lpfc_sli_config_mbox_opcode_get(phba,
7678 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007679 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7680 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7681 bf_get(lpfc_mcqe_ext_status,
7682 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007683 psli->sli_flag, flag);
7684 return rc;
7685 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007686 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7687 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007688 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007689 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007690 mboxq->vport ? mboxq->vport->vpi : 0,
7691 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007692 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7693 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007694 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007695 /* Try to block the asynchronous mailbox posting */
7696 rc = lpfc_sli4_async_mbox_block(phba);
7697 if (!rc) {
7698 /* Successfully blocked, now issue sync mbox cmd */
7699 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7700 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007701 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007702 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007703 "(%d):2597 Sync Mailbox command "
7704 "x%x (x%x/x%x) failure: "
7705 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7706 "Data: x%x x%x\n,",
7707 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007708 mboxq->u.mb.mbxCommand,
7709 lpfc_sli_config_mbox_subsys_get(phba,
7710 mboxq),
7711 lpfc_sli_config_mbox_opcode_get(phba,
7712 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007713 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7714 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7715 bf_get(lpfc_mcqe_ext_status,
7716 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007717 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007718 /* Unblock the async mailbox posting afterward */
7719 lpfc_sli4_async_mbox_unblock(phba);
7720 }
7721 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007722 }
7723
7724 /* Now, interrupt mode asynchrous mailbox command */
7725 rc = lpfc_mbox_cmd_check(phba, mboxq);
7726 if (rc) {
7727 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007728 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007729 "cannot issue Data: x%x x%x\n",
7730 mboxq->vport ? mboxq->vport->vpi : 0,
7731 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007732 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7733 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007734 psli->sli_flag, flag);
7735 goto out_not_finished;
7736 }
James Smartda0436e2009-05-22 14:51:39 -04007737
7738 /* Put the mailbox command to the driver internal FIFO */
7739 psli->slistat.mbox_busy++;
7740 spin_lock_irqsave(&phba->hbalock, iflags);
7741 lpfc_mbox_put(phba, mboxq);
7742 spin_unlock_irqrestore(&phba->hbalock, iflags);
7743 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7744 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007745 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007746 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7747 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007748 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7749 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007750 phba->pport->port_state,
7751 psli->sli_flag, MBX_NOWAIT);
7752 /* Wake up worker thread to transport mailbox command from head */
7753 lpfc_worker_wake_up(phba);
7754
7755 return MBX_BUSY;
7756
7757out_not_finished:
7758 return MBX_NOT_FINISHED;
7759}
7760
7761/**
7762 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7763 * @phba: Pointer to HBA context object.
7764 *
7765 * This function is called by worker thread to send a mailbox command to
7766 * SLI4 HBA firmware.
7767 *
7768 **/
7769int
7770lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7771{
7772 struct lpfc_sli *psli = &phba->sli;
7773 LPFC_MBOXQ_t *mboxq;
7774 int rc = MBX_SUCCESS;
7775 unsigned long iflags;
7776 struct lpfc_mqe *mqe;
7777 uint32_t mbx_cmnd;
7778
7779 /* Check interrupt mode before post async mailbox command */
7780 if (unlikely(!phba->sli4_hba.intr_enable))
7781 return MBX_NOT_FINISHED;
7782
7783 /* Check for mailbox command service token */
7784 spin_lock_irqsave(&phba->hbalock, iflags);
7785 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7786 spin_unlock_irqrestore(&phba->hbalock, iflags);
7787 return MBX_NOT_FINISHED;
7788 }
7789 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7790 spin_unlock_irqrestore(&phba->hbalock, iflags);
7791 return MBX_NOT_FINISHED;
7792 }
7793 if (unlikely(phba->sli.mbox_active)) {
7794 spin_unlock_irqrestore(&phba->hbalock, iflags);
7795 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7796 "0384 There is pending active mailbox cmd\n");
7797 return MBX_NOT_FINISHED;
7798 }
7799 /* Take the mailbox command service token */
7800 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7801
7802 /* Get the next mailbox command from head of queue */
7803 mboxq = lpfc_mbox_get(phba);
7804
7805 /* If no more mailbox command waiting for post, we're done */
7806 if (!mboxq) {
7807 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7808 spin_unlock_irqrestore(&phba->hbalock, iflags);
7809 return MBX_SUCCESS;
7810 }
7811 phba->sli.mbox_active = mboxq;
7812 spin_unlock_irqrestore(&phba->hbalock, iflags);
7813
7814 /* Check device readiness for posting mailbox command */
7815 rc = lpfc_mbox_dev_check(phba);
7816 if (unlikely(rc))
7817 /* Driver clean routine will clean up pending mailbox */
7818 goto out_not_finished;
7819
7820 /* Prepare the mbox command to be posted */
7821 mqe = &mboxq->u.mqe;
7822 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7823
7824 /* Start timer for the mbox_tmo and log some mailbox post messages */
7825 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007826 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007827
7828 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007829 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007830 "x%x x%x\n",
7831 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007832 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7833 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007834 phba->pport->port_state, psli->sli_flag);
7835
7836 if (mbx_cmnd != MBX_HEARTBEAT) {
7837 if (mboxq->vport) {
7838 lpfc_debugfs_disc_trc(mboxq->vport,
7839 LPFC_DISC_TRC_MBOX_VPORT,
7840 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7841 mbx_cmnd, mqe->un.mb_words[0],
7842 mqe->un.mb_words[1]);
7843 } else {
7844 lpfc_debugfs_disc_trc(phba->pport,
7845 LPFC_DISC_TRC_MBOX,
7846 "MBOX Send: cmd:x%x mb:x%x x%x",
7847 mbx_cmnd, mqe->un.mb_words[0],
7848 mqe->un.mb_words[1]);
7849 }
7850 }
7851 psli->slistat.mbox_cmd++;
7852
7853 /* Post the mailbox command to the port */
7854 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7855 if (rc != MBX_SUCCESS) {
7856 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007857 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007858 "cannot issue Data: x%x x%x\n",
7859 mboxq->vport ? mboxq->vport->vpi : 0,
7860 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007861 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7862 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007863 psli->sli_flag, MBX_NOWAIT);
7864 goto out_not_finished;
7865 }
7866
7867 return rc;
7868
7869out_not_finished:
7870 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007871 if (phba->sli.mbox_active) {
7872 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7873 __lpfc_mbox_cmpl_put(phba, mboxq);
7874 /* Release the token */
7875 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7876 phba->sli.mbox_active = NULL;
7877 }
James Smartda0436e2009-05-22 14:51:39 -04007878 spin_unlock_irqrestore(&phba->hbalock, iflags);
7879
7880 return MBX_NOT_FINISHED;
7881}
7882
7883/**
7884 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7885 * @phba: Pointer to HBA context object.
7886 * @pmbox: Pointer to mailbox object.
7887 * @flag: Flag indicating how the mailbox need to be processed.
7888 *
7889 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7890 * the API jump table function pointer from the lpfc_hba struct.
7891 *
7892 * Return codes the caller owns the mailbox command after the return of the
7893 * function.
7894 **/
7895int
7896lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7897{
7898 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7899}
7900
7901/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007902 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007903 * @phba: The hba struct for which this call is being executed.
7904 * @dev_grp: The HBA PCI-Device group number.
7905 *
7906 * This routine sets up the mbox interface API function jump table in @phba
7907 * struct.
7908 * Returns: 0 - success, -ENODEV - failure.
7909 **/
7910int
7911lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7912{
7913
7914 switch (dev_grp) {
7915 case LPFC_PCI_DEV_LP:
7916 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7917 phba->lpfc_sli_handle_slow_ring_event =
7918 lpfc_sli_handle_slow_ring_event_s3;
7919 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7920 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7921 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7922 break;
7923 case LPFC_PCI_DEV_OC:
7924 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7925 phba->lpfc_sli_handle_slow_ring_event =
7926 lpfc_sli_handle_slow_ring_event_s4;
7927 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7928 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7929 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7930 break;
7931 default:
7932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7933 "1420 Invalid HBA PCI-device group: 0x%x\n",
7934 dev_grp);
7935 return -ENODEV;
7936 break;
7937 }
7938 return 0;
7939}
7940
7941/**
James Smart3621a712009-04-06 18:47:14 -04007942 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007943 * @phba: Pointer to HBA context object.
7944 * @pring: Pointer to driver SLI ring object.
7945 * @piocb: Pointer to address of newly added command iocb.
7946 *
7947 * This function is called with hbalock held to add a command
7948 * iocb to the txq when SLI layer cannot submit the command iocb
7949 * to the ring.
7950 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007951void
James Smart92d7f7b2007-06-17 19:56:38 -05007952__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007953 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007954{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007955 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007956 /* Insert the caller's iocb in the txq tail for later processing. */
7957 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007958}
7959
James Smarte59058c2008-08-24 21:49:00 -04007960/**
James Smart3621a712009-04-06 18:47:14 -04007961 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007962 * @phba: Pointer to HBA context object.
7963 * @pring: Pointer to driver SLI ring object.
7964 * @piocb: Pointer to address of newly added command iocb.
7965 *
7966 * This function is called with hbalock held before a new
7967 * iocb is submitted to the firmware. This function checks
7968 * txq to flush the iocbs in txq to Firmware before
7969 * submitting new iocbs to the Firmware.
7970 * If there are iocbs in the txq which need to be submitted
7971 * to firmware, lpfc_sli_next_iocb returns the first element
7972 * of the txq after dequeuing it from txq.
7973 * If there is no iocb in the txq then the function will return
7974 * *piocb and *piocb is set to NULL. Caller needs to check
7975 * *piocb to find if there are more commands in the txq.
7976 **/
dea31012005-04-17 16:05:31 -05007977static struct lpfc_iocbq *
7978lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007979 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007980{
7981 struct lpfc_iocbq * nextiocb;
7982
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007983 lockdep_assert_held(&phba->hbalock);
7984
dea31012005-04-17 16:05:31 -05007985 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7986 if (!nextiocb) {
7987 nextiocb = *piocb;
7988 *piocb = NULL;
7989 }
7990
7991 return nextiocb;
7992}
7993
James Smarte59058c2008-08-24 21:49:00 -04007994/**
James Smart3772a992009-05-22 14:50:54 -04007995 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007996 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007997 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007998 * @piocb: Pointer to command iocb.
7999 * @flag: Flag indicating if this command can be put into txq.
8000 *
James Smart3772a992009-05-22 14:50:54 -04008001 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8002 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8003 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8004 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8005 * this function allows only iocbs for posting buffers. This function finds
8006 * next available slot in the command ring and posts the command to the
8007 * available slot and writes the port attention register to request HBA start
8008 * processing new iocb. If there is no slot available in the ring and
8009 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8010 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008011 *
James Smart3772a992009-05-22 14:50:54 -04008012 * This function is called with hbalock held. The function will return success
8013 * after it successfully submit the iocb to firmware or after adding to the
8014 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008015 **/
James Smart98c9ea52007-10-27 13:37:33 -04008016static int
James Smart3772a992009-05-22 14:50:54 -04008017__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008018 struct lpfc_iocbq *piocb, uint32_t flag)
8019{
8020 struct lpfc_iocbq *nextiocb;
8021 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04008022 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05008023
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008024 lockdep_assert_held(&phba->hbalock);
8025
James Smart92d7f7b2007-06-17 19:56:38 -05008026 if (piocb->iocb_cmpl && (!piocb->vport) &&
8027 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8028 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8029 lpfc_printf_log(phba, KERN_ERR,
8030 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008031 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008032 piocb->iocb.ulpCommand);
8033 dump_stack();
8034 return IOCB_ERROR;
8035 }
8036
8037
Linas Vepstas8d63f372007-02-14 14:28:36 -06008038 /* If the PCI channel is in offline state, do not post iocbs. */
8039 if (unlikely(pci_channel_offline(phba->pcidev)))
8040 return IOCB_ERROR;
8041
James Smarta257bf92009-04-06 18:48:10 -04008042 /* If HBA has a deferred error attention, fail the iocb. */
8043 if (unlikely(phba->hba_flag & DEFER_ERATT))
8044 return IOCB_ERROR;
8045
dea31012005-04-17 16:05:31 -05008046 /*
8047 * We should never get an IOCB if we are in a < LINK_DOWN state
8048 */
James Smart2e0fef82007-06-17 19:56:36 -05008049 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008050 return IOCB_ERROR;
8051
8052 /*
8053 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008054 * outstanding event.
dea31012005-04-17 16:05:31 -05008055 */
James Smart0b727fe2007-10-27 13:37:25 -04008056 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008057 goto iocb_busy;
8058
James Smart2e0fef82007-06-17 19:56:36 -05008059 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008060 /*
James Smart2680eea2007-04-25 09:52:55 -04008061 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008062 * can be issued if the link is not up.
8063 */
8064 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008065 case CMD_GEN_REQUEST64_CR:
8066 case CMD_GEN_REQUEST64_CX:
8067 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8068 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008069 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008070 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8071 MENLO_TRANSPORT_TYPE))
8072
8073 goto iocb_busy;
8074 break;
dea31012005-04-17 16:05:31 -05008075 case CMD_QUE_RING_BUF_CN:
8076 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008077 /*
8078 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8079 * completion, iocb_cmpl MUST be 0.
8080 */
8081 if (piocb->iocb_cmpl)
8082 piocb->iocb_cmpl = NULL;
8083 /*FALLTHROUGH*/
8084 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008085 case CMD_CLOSE_XRI_CN:
8086 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008087 break;
8088 default:
8089 goto iocb_busy;
8090 }
8091
8092 /*
8093 * For FCP commands, we must be in a state where we can process link
8094 * attention events.
8095 */
8096 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05008097 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008098 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008099 }
dea31012005-04-17 16:05:31 -05008100
dea31012005-04-17 16:05:31 -05008101 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8102 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8103 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8104
8105 if (iocb)
8106 lpfc_sli_update_ring(phba, pring);
8107 else
8108 lpfc_sli_update_full_ring(phba, pring);
8109
8110 if (!piocb)
8111 return IOCB_SUCCESS;
8112
8113 goto out_busy;
8114
8115 iocb_busy:
8116 pring->stats.iocb_cmd_delay++;
8117
8118 out_busy:
8119
8120 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008121 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008122 return IOCB_SUCCESS;
8123 }
8124
8125 return IOCB_BUSY;
8126}
8127
James Smart3772a992009-05-22 14:50:54 -04008128/**
James Smart4f774512009-05-22 14:52:35 -04008129 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8130 * @phba: Pointer to HBA context object.
8131 * @piocb: Pointer to command iocb.
8132 * @sglq: Pointer to the scatter gather queue object.
8133 *
8134 * This routine converts the bpl or bde that is in the IOCB
8135 * to a sgl list for the sli4 hardware. The physical address
8136 * of the bpl/bde is converted back to a virtual address.
8137 * If the IOCB contains a BPL then the list of BDE's is
8138 * converted to sli4_sge's. If the IOCB contains a single
8139 * BDE then it is converted to a single sli_sge.
8140 * The IOCB is still in cpu endianess so the contents of
8141 * the bpl can be used without byte swapping.
8142 *
8143 * Returns valid XRI = Success, NO_XRI = Failure.
8144**/
8145static uint16_t
8146lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8147 struct lpfc_sglq *sglq)
8148{
8149 uint16_t xritag = NO_XRI;
8150 struct ulp_bde64 *bpl = NULL;
8151 struct ulp_bde64 bde;
8152 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008153 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008154 IOCB_t *icmd;
8155 int numBdes = 0;
8156 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008157 uint32_t offset = 0; /* accumulated offset in the sg request list */
8158 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008159
8160 if (!piocbq || !sglq)
8161 return xritag;
8162
8163 sgl = (struct sli4_sge *)sglq->sgl;
8164 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008165 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8166 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008167 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8168 numBdes = icmd->un.genreq64.bdl.bdeSize /
8169 sizeof(struct ulp_bde64);
8170 /* The addrHigh and addrLow fields within the IOCB
8171 * have not been byteswapped yet so there is no
8172 * need to swap them back.
8173 */
James Smart1b511972011-12-13 13:23:09 -05008174 if (piocbq->context3)
8175 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8176 else
8177 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008178
James Smart1b511972011-12-13 13:23:09 -05008179 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008180 if (!bpl)
8181 return xritag;
8182
8183 for (i = 0; i < numBdes; i++) {
8184 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008185 sgl->addr_hi = bpl->addrHigh;
8186 sgl->addr_lo = bpl->addrLow;
8187
James Smart05580562011-05-24 11:40:48 -04008188 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008189 if ((i+1) == numBdes)
8190 bf_set(lpfc_sli4_sge_last, sgl, 1);
8191 else
8192 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008193 /* swap the size field back to the cpu so we
8194 * can assign it to the sgl.
8195 */
8196 bde.tus.w = le32_to_cpu(bpl->tus.w);
8197 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008198 /* The offsets in the sgl need to be accumulated
8199 * separately for the request and reply lists.
8200 * The request is always first, the reply follows.
8201 */
8202 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8203 /* add up the reply sg entries */
8204 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8205 inbound++;
8206 /* first inbound? reset the offset */
8207 if (inbound == 1)
8208 offset = 0;
8209 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008210 bf_set(lpfc_sli4_sge_type, sgl,
8211 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008212 offset += bde.tus.f.bdeSize;
8213 }
James Smart546fc852011-03-11 16:06:29 -05008214 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008215 bpl++;
8216 sgl++;
8217 }
8218 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8219 /* The addrHigh and addrLow fields of the BDE have not
8220 * been byteswapped yet so they need to be swapped
8221 * before putting them in the sgl.
8222 */
8223 sgl->addr_hi =
8224 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8225 sgl->addr_lo =
8226 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008227 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008228 bf_set(lpfc_sli4_sge_last, sgl, 1);
8229 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008230 sgl->sge_len =
8231 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008232 }
8233 return sglq->sli4_xritag;
8234}
8235
8236/**
James Smart4f774512009-05-22 14:52:35 -04008237 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8238 * @phba: Pointer to HBA context object.
8239 * @piocb: Pointer to command iocb.
8240 * @wqe: Pointer to the work queue entry.
8241 *
8242 * This routine converts the iocb command to its Work Queue Entry
8243 * equivalent. The wqe pointer should not have any fields set when
8244 * this routine is called because it will memcpy over them.
8245 * This routine does not set the CQ_ID or the WQEC bits in the
8246 * wqe.
8247 *
8248 * Returns: 0 = Success, IOCB_ERROR = Failure.
8249 **/
8250static int
8251lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8252 union lpfc_wqe *wqe)
8253{
James Smart5ffc2662009-11-18 15:39:44 -05008254 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008255 uint8_t ct = 0;
8256 uint32_t fip;
8257 uint32_t abort_tag;
8258 uint8_t command_type = ELS_COMMAND_NON_FIP;
8259 uint8_t cmnd;
8260 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008261 uint16_t abrt_iotag;
8262 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008263 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008264 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008265 int numBdes, i;
8266 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008267 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008268 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008269 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008270
James Smart45ed1192009-10-02 15:17:02 -04008271 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008272 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008273 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008274 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008275 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008276 command_type = ELS_COMMAND_FIP;
8277 else
8278 command_type = ELS_COMMAND_NON_FIP;
8279
James Smartb5c53952016-03-31 14:12:30 -07008280 if (phba->fcp_embed_io)
8281 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008282 /* Some of the fields are in the right position already */
8283 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008284 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008285 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008286
8287 abort_tag = (uint32_t) iocbq->iotag;
8288 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008289 /* words0-2 bpl convert bde */
8290 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008291 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8292 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008293 bpl = (struct ulp_bde64 *)
8294 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8295 if (!bpl)
8296 return IOCB_ERROR;
8297
8298 /* Should already be byte swapped. */
8299 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8300 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8301 /* swap the size field back to the cpu so we
8302 * can assign it to the sgl.
8303 */
8304 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008305 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8306 total_len = 0;
8307 for (i = 0; i < numBdes; i++) {
8308 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8309 total_len += bde.tus.f.bdeSize;
8310 }
James Smart4f774512009-05-22 14:52:35 -04008311 } else
James Smart5ffc2662009-11-18 15:39:44 -05008312 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008313
8314 iocbq->iocb.ulpIoTag = iocbq->iotag;
8315 cmnd = iocbq->iocb.ulpCommand;
8316
8317 switch (iocbq->iocb.ulpCommand) {
8318 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008319 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8320 ndlp = iocbq->context_un.ndlp;
8321 else
8322 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008323 if (!iocbq->iocb.ulpLe) {
8324 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8325 "2007 Only Limited Edition cmd Format"
8326 " supported 0x%x\n",
8327 iocbq->iocb.ulpCommand);
8328 return IOCB_ERROR;
8329 }
James Smartff78d8f2011-12-13 13:21:35 -05008330
James Smart5ffc2662009-11-18 15:39:44 -05008331 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008332 /* Els_reguest64 has a TMO */
8333 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8334 iocbq->iocb.ulpTimeout);
8335 /* Need a VF for word 4 set the vf bit*/
8336 bf_set(els_req64_vf, &wqe->els_req, 0);
8337 /* And a VFID for word 12 */
8338 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008339 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008340 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8341 iocbq->iocb.ulpContext);
8342 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8343 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008344 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008345 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008346 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8347 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008348 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8349 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008350 if_type = bf_get(lpfc_sli_intf_if_type,
8351 &phba->sli4_hba.sli_intf);
8352 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008353 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008354 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008355 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008356 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008357 *pcmd == ELS_CMD_PLOGI)) {
8358 bf_set(els_req64_sp, &wqe->els_req, 1);
8359 bf_set(els_req64_sid, &wqe->els_req,
8360 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008361 if ((*pcmd == ELS_CMD_FLOGI) &&
8362 !(phba->fc_topology ==
8363 LPFC_TOPOLOGY_LOOP))
8364 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008365 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8366 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008367 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008368 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008369 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8370 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8371 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8372 }
James Smartc8685952009-11-18 15:39:16 -05008373 }
James Smart6d368e52011-05-24 11:44:12 -04008374 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8375 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008376 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8377 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8378 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8379 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8380 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8381 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008382 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008383 break;
James Smart5ffc2662009-11-18 15:39:44 -05008384 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008385 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8386 iocbq->iocb.un.ulpWord[3]);
8387 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008388 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008389 /* The entire sequence is transmitted for this IOCB */
8390 xmit_len = total_len;
8391 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008392 if (phba->link_flag & LS_LOOPBACK_MODE)
8393 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008394 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008395 /* word3 iocb=io_tag32 wqe=reserved */
8396 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008397 /* word4 relative_offset memcpy */
8398 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008399 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8400 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8401 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8402 LPFC_WQE_IOD_WRITE);
8403 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8404 LPFC_WQE_LENLOC_WORD12);
8405 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008406 wqe->xmit_sequence.xmit_len = xmit_len;
8407 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008408 break;
James Smart4f774512009-05-22 14:52:35 -04008409 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008410 /* word3 iocb=iotag32 wqe=seq_payload_len */
8411 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008412 /* word4 iocb=rsvd wqe=rsvd */
8413 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8414 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008415 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008416 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008417 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8418 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8419 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8420 LPFC_WQE_LENLOC_WORD3);
8421 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008422 break;
James Smart4f774512009-05-22 14:52:35 -04008423 case CMD_FCP_IWRITE64_CR:
8424 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008425 /* word3 iocb=iotag wqe=payload_offset_len */
8426 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008427 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8428 xmit_len + sizeof(struct fcp_rsp));
8429 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8430 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008431 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8432 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8433 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8434 iocbq->iocb.ulpFCP2Rcvy);
8435 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8436 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008437 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8438 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8439 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008440 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008441 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008442 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8443 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8444 if (phba->cfg_XLanePriority) {
8445 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8446 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8447 (phba->cfg_XLanePriority << 1));
8448 }
8449 }
James Smartb5c53952016-03-31 14:12:30 -07008450 /* Note, word 10 is already initialized to 0 */
8451
8452 if (phba->fcp_embed_io) {
8453 struct lpfc_scsi_buf *lpfc_cmd;
8454 struct sli4_sge *sgl;
8455 union lpfc_wqe128 *wqe128;
8456 struct fcp_cmnd *fcp_cmnd;
8457 uint32_t *ptr;
8458
8459 /* 128 byte wqe support here */
8460 wqe128 = (union lpfc_wqe128 *)wqe;
8461
8462 lpfc_cmd = iocbq->context1;
8463 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8464 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8465
8466 /* Word 0-2 - FCP_CMND */
8467 wqe128->generic.bde.tus.f.bdeFlags =
8468 BUFF_TYPE_BDE_IMMED;
8469 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8470 wqe128->generic.bde.addrHigh = 0;
8471 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8472
8473 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8474
8475 /* Word 22-29 FCP CMND Payload */
8476 ptr = &wqe128->words[22];
8477 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8478 }
James Smart7851fe22011-07-22 18:36:52 -04008479 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008480 case CMD_FCP_IREAD64_CR:
8481 /* word3 iocb=iotag wqe=payload_offset_len */
8482 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008483 bf_set(payload_offset_len, &wqe->fcp_iread,
8484 xmit_len + sizeof(struct fcp_rsp));
8485 bf_set(cmd_buff_len, &wqe->fcp_iread,
8486 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008487 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8488 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8489 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8490 iocbq->iocb.ulpFCP2Rcvy);
8491 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008492 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008493 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8494 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8495 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008496 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008497 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008498 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8499 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8500 if (phba->cfg_XLanePriority) {
8501 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8502 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8503 (phba->cfg_XLanePriority << 1));
8504 }
8505 }
James Smartb5c53952016-03-31 14:12:30 -07008506 /* Note, word 10 is already initialized to 0 */
8507
8508 if (phba->fcp_embed_io) {
8509 struct lpfc_scsi_buf *lpfc_cmd;
8510 struct sli4_sge *sgl;
8511 union lpfc_wqe128 *wqe128;
8512 struct fcp_cmnd *fcp_cmnd;
8513 uint32_t *ptr;
8514
8515 /* 128 byte wqe support here */
8516 wqe128 = (union lpfc_wqe128 *)wqe;
8517
8518 lpfc_cmd = iocbq->context1;
8519 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8520 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8521
8522 /* Word 0-2 - FCP_CMND */
8523 wqe128->generic.bde.tus.f.bdeFlags =
8524 BUFF_TYPE_BDE_IMMED;
8525 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8526 wqe128->generic.bde.addrHigh = 0;
8527 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8528
8529 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8530
8531 /* Word 22-29 FCP CMND Payload */
8532 ptr = &wqe128->words[22];
8533 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8534 }
James Smart7851fe22011-07-22 18:36:52 -04008535 break;
James Smartf1126682009-06-10 17:22:44 -04008536 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008537 /* word3 iocb=iotag wqe=payload_offset_len */
8538 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8539 bf_set(payload_offset_len, &wqe->fcp_icmd,
8540 xmit_len + sizeof(struct fcp_rsp));
8541 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8542 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008543 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008544 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008545 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008546 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8547 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8548 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8549 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8550 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008551 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8552 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008553 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8554 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8555 if (phba->cfg_XLanePriority) {
8556 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8557 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8558 (phba->cfg_XLanePriority << 1));
8559 }
8560 }
James Smartb5c53952016-03-31 14:12:30 -07008561 /* Note, word 10 is already initialized to 0 */
8562
8563 if (phba->fcp_embed_io) {
8564 struct lpfc_scsi_buf *lpfc_cmd;
8565 struct sli4_sge *sgl;
8566 union lpfc_wqe128 *wqe128;
8567 struct fcp_cmnd *fcp_cmnd;
8568 uint32_t *ptr;
8569
8570 /* 128 byte wqe support here */
8571 wqe128 = (union lpfc_wqe128 *)wqe;
8572
8573 lpfc_cmd = iocbq->context1;
8574 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8575 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8576
8577 /* Word 0-2 - FCP_CMND */
8578 wqe128->generic.bde.tus.f.bdeFlags =
8579 BUFF_TYPE_BDE_IMMED;
8580 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8581 wqe128->generic.bde.addrHigh = 0;
8582 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8583
8584 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8585
8586 /* Word 22-29 FCP CMND Payload */
8587 ptr = &wqe128->words[22];
8588 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8589 }
James Smart7851fe22011-07-22 18:36:52 -04008590 break;
James Smart4f774512009-05-22 14:52:35 -04008591 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008592 /* For this command calculate the xmit length of the
8593 * request bde.
8594 */
8595 xmit_len = 0;
8596 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8597 sizeof(struct ulp_bde64);
8598 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008599 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008600 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8601 break;
James Smart63e801c2010-11-20 23:14:19 -05008602 xmit_len += bde.tus.f.bdeSize;
8603 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008604 /* word3 iocb=IO_TAG wqe=request_payload_len */
8605 wqe->gen_req.request_payload_len = xmit_len;
8606 /* word4 iocb=parameter wqe=relative_offset memcpy */
8607 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008608 /* word6 context tag copied in memcpy */
8609 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8610 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8611 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8612 "2015 Invalid CT %x command 0x%x\n",
8613 ct, iocbq->iocb.ulpCommand);
8614 return IOCB_ERROR;
8615 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008616 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8617 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8618 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8619 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8620 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8621 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8622 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8623 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008624 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008625 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008626 break;
James Smart4f774512009-05-22 14:52:35 -04008627 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008628 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008629 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008630 /* word3 iocb=iotag32 wqe=response_payload_len */
8631 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008632 /* word4 */
8633 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008634 /* word5 iocb=rsvd wge=did */
8635 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008636 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8637
8638 if_type = bf_get(lpfc_sli_intf_if_type,
8639 &phba->sli4_hba.sli_intf);
8640 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8641 if (iocbq->vport->fc_flag & FC_PT2PT) {
8642 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8643 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8644 iocbq->vport->fc_myDID);
8645 if (iocbq->vport->fc_myDID == Fabric_DID) {
8646 bf_set(wqe_els_did,
8647 &wqe->xmit_els_rsp.wqe_dest, 0);
8648 }
8649 }
8650 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008651 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8652 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8653 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8654 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008655 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008656 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008657 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008658 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008659 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8660 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8661 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8662 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8663 LPFC_WQE_LENLOC_WORD3);
8664 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008665 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8666 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008667 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8668 iocbq->context2)->virt);
8669 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008670 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8671 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008672 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008673 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8674 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008675 phba->vpi_ids[phba->pport->vpi]);
8676 }
James Smart4f774512009-05-22 14:52:35 -04008677 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008678 break;
James Smart4f774512009-05-22 14:52:35 -04008679 case CMD_CLOSE_XRI_CN:
8680 case CMD_ABORT_XRI_CN:
8681 case CMD_ABORT_XRI_CX:
8682 /* words 0-2 memcpy should be 0 rserved */
8683 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008684 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8685 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8686 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8687 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8688 } else
8689 fip = 0;
8690
8691 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008692 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008693 * The link is down, or the command was ELS_FIP
8694 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008695 * on the wire.
8696 */
8697 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8698 else
8699 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8700 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008701 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8702 wqe->abort_cmd.rsrvd5 = 0;
8703 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008704 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8705 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008706 /*
8707 * The abort handler will send us CMD_ABORT_XRI_CN or
8708 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8709 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008710 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8711 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8712 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8713 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008714 cmnd = CMD_ABORT_XRI_CX;
8715 command_type = OTHER_COMMAND;
8716 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008717 break;
James Smart6669f9b2009-10-02 15:16:45 -04008718 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008719 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008720 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008721 * we re-construct this WQE here based on information in
8722 * iocbq from scratch.
8723 */
8724 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008725 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008726 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008727 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8728 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008729 LPFC_ABTS_UNSOL_INT) {
8730 /* ABTS sent by initiator to CT exchange, the
8731 * RX_ID field will be filled with the newly
8732 * allocated responder XRI.
8733 */
8734 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8735 iocbq->sli4_xritag);
8736 } else {
8737 /* ABTS sent by responder to CT exchange, the
8738 * RX_ID field will be filled with the responder
8739 * RX_ID from ABTS.
8740 */
8741 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008742 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008743 }
James Smart6669f9b2009-10-02 15:16:45 -04008744 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8745 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008746
8747 /* Use CT=VPI */
8748 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8749 ndlp->nlp_DID);
8750 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8751 iocbq->iocb.ulpContext);
8752 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008753 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008754 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008755 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8756 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8757 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008758 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8759 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008760 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8761 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8762 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8763 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8764 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8765 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8766 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8767 }
8768
James Smart7851fe22011-07-22 18:36:52 -04008769 break;
James Smart4f774512009-05-22 14:52:35 -04008770 case CMD_XRI_ABORTED_CX:
8771 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008772 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8773 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8774 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8775 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8776 default:
8777 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8778 "2014 Invalid command 0x%x\n",
8779 iocbq->iocb.ulpCommand);
8780 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008781 break;
James Smart4f774512009-05-22 14:52:35 -04008782 }
James Smart6d368e52011-05-24 11:44:12 -04008783
James Smart8012cc32012-10-31 14:44:49 -04008784 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8785 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8786 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8787 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8788 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8789 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8790 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8791 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008792 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8793 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8794 wqe->generic.wqe_com.abort_tag = abort_tag;
8795 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8796 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8797 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8798 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008799 return 0;
8800}
8801
8802/**
8803 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8804 * @phba: Pointer to HBA context object.
8805 * @ring_number: SLI ring number to issue iocb on.
8806 * @piocb: Pointer to command iocb.
8807 * @flag: Flag indicating if this command can be put into txq.
8808 *
8809 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8810 * an iocb command to an HBA with SLI-4 interface spec.
8811 *
8812 * This function is called with hbalock held. The function will return success
8813 * after it successfully submit the iocb to firmware or after adding to the
8814 * txq.
8815 **/
8816static int
8817__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8818 struct lpfc_iocbq *piocb, uint32_t flag)
8819{
8820 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008821 union lpfc_wqe *wqe;
8822 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008823 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008824 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008825
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008826 lockdep_assert_held(&phba->hbalock);
8827
James Smartb5c53952016-03-31 14:12:30 -07008828 /*
8829 * The WQE can be either 64 or 128 bytes,
8830 * so allocate space on the stack assuming the largest.
8831 */
8832 wqe = (union lpfc_wqe *)&wqe128;
8833
James Smart4f774512009-05-22 14:52:35 -04008834 if (piocb->sli4_xritag == NO_XRI) {
8835 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008836 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008837 sglq = NULL;
8838 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008839 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008840 if (!(flag & SLI_IOCB_RET_IOCB)) {
8841 __lpfc_sli_ringtx_put(phba,
8842 pring, piocb);
8843 return IOCB_SUCCESS;
8844 } else {
8845 return IOCB_BUSY;
8846 }
8847 } else {
James Smart6d368e52011-05-24 11:44:12 -04008848 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008849 if (!sglq) {
8850 if (!(flag & SLI_IOCB_RET_IOCB)) {
8851 __lpfc_sli_ringtx_put(phba,
8852 pring,
8853 piocb);
8854 return IOCB_SUCCESS;
8855 } else
8856 return IOCB_BUSY;
8857 }
8858 }
James Smart4f774512009-05-22 14:52:35 -04008859 }
8860 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008861 /* These IO's already have an XRI and a mapped sgl. */
8862 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008863 } else {
James Smart6d368e52011-05-24 11:44:12 -04008864 /*
8865 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008866 * sglq is on the active list
8867 */
James Smartedccdc12013-01-03 15:43:45 -05008868 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008869 if (!sglq)
8870 return IOCB_ERROR;
8871 }
8872
8873 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008874 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008875 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008876 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008877 return IOCB_ERROR;
8878 }
8879
James Smartb5c53952016-03-31 14:12:30 -07008880 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008881 return IOCB_ERROR;
8882
James Smart341af102010-01-26 23:07:37 -05008883 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008884 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008885 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008886 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8887 } else {
8888 wq = phba->sli4_hba.oas_wq;
8889 }
James Smartb5c53952016-03-31 14:12:30 -07008890 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008891 return IOCB_ERROR;
8892 } else {
James Smartea714f32013-04-17 20:18:47 -04008893 if (unlikely(!phba->sli4_hba.els_wq))
8894 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008895 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008896 return IOCB_ERROR;
8897 }
8898 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8899
8900 return 0;
8901}
8902
8903/**
James Smart3772a992009-05-22 14:50:54 -04008904 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8905 *
8906 * This routine wraps the actual lockless version for issusing IOCB function
8907 * pointer from the lpfc_hba struct.
8908 *
8909 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008910 * IOCB_ERROR - Error
8911 * IOCB_SUCCESS - Success
8912 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008913 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008914int
James Smart3772a992009-05-22 14:50:54 -04008915__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8916 struct lpfc_iocbq *piocb, uint32_t flag)
8917{
8918 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8919}
8920
8921/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008922 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008923 * @phba: The hba struct for which this call is being executed.
8924 * @dev_grp: The HBA PCI-Device group number.
8925 *
8926 * This routine sets up the SLI interface API function jump table in @phba
8927 * struct.
8928 * Returns: 0 - success, -ENODEV - failure.
8929 **/
8930int
8931lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8932{
8933
8934 switch (dev_grp) {
8935 case LPFC_PCI_DEV_LP:
8936 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8937 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8938 break;
James Smart4f774512009-05-22 14:52:35 -04008939 case LPFC_PCI_DEV_OC:
8940 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8941 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8942 break;
James Smart3772a992009-05-22 14:50:54 -04008943 default:
8944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8945 "1419 Invalid HBA PCI-device group: 0x%x\n",
8946 dev_grp);
8947 return -ENODEV;
8948 break;
8949 }
8950 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8951 return 0;
8952}
James Smart92d7f7b2007-06-17 19:56:38 -05008953
James Smarta1efe162015-05-21 13:55:20 -04008954/**
8955 * lpfc_sli_calc_ring - Calculates which ring to use
8956 * @phba: Pointer to HBA context object.
8957 * @ring_number: Initial ring
8958 * @piocb: Pointer to command iocb.
8959 *
8960 * For SLI4, FCP IO can deferred to one fo many WQs, based on
8961 * fcp_wqidx, thus we need to calculate the corresponding ring.
8962 * Since ABORTS must go on the same WQ of the command they are
8963 * aborting, we use command's fcp_wqidx.
8964 */
James Smart9bd2bff52014-09-03 12:57:30 -04008965int
8966lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
8967 struct lpfc_iocbq *piocb)
8968{
James Smart8b0dff12015-05-22 10:42:38 -04008969 if (phba->sli_rev < LPFC_SLI_REV4)
8970 return ring_number;
8971
8972 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8973 if (!(phba->cfg_fof) ||
8974 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8975 if (unlikely(!phba->sli4_hba.fcp_wq))
8976 return LPFC_HBA_ERROR;
8977 /*
8978 * for abort iocb fcp_wqidx should already
8979 * be setup based on what work queue we used.
8980 */
8981 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
8982 piocb->fcp_wqidx =
8983 lpfc_sli4_scmd_to_wqidx_distr(phba,
8984 piocb->context1);
8985 ring_number = MAX_SLI3_CONFIGURED_RINGS +
8986 piocb->fcp_wqidx;
8987 } else {
8988 if (unlikely(!phba->sli4_hba.oas_wq))
8989 return LPFC_HBA_ERROR;
8990 piocb->fcp_wqidx = 0;
8991 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04008992 }
8993 }
8994 return ring_number;
8995}
8996
James Smarte59058c2008-08-24 21:49:00 -04008997/**
James Smart3621a712009-04-06 18:47:14 -04008998 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008999 * @phba: Pointer to HBA context object.
9000 * @pring: Pointer to driver SLI ring object.
9001 * @piocb: Pointer to command iocb.
9002 * @flag: Flag indicating if this command can be put into txq.
9003 *
9004 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9005 * function. This function gets the hbalock and calls
9006 * __lpfc_sli_issue_iocb function and will return the error returned
9007 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9008 * functions which do not hold hbalock.
9009 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009010int
James Smart3772a992009-05-22 14:50:54 -04009011lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009012 struct lpfc_iocbq *piocb, uint32_t flag)
9013{
James Smartba20c852012-08-03 12:36:52 -04009014 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009015 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009016 struct lpfc_queue *fpeq;
9017 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009018 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009019 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009020
James Smart7e56aa22012-08-03 12:35:34 -04009021 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009022 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9023 if (unlikely(ring_number == LPFC_HBA_ERROR))
9024 return IOCB_ERROR;
9025 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04009026
James Smart9bd2bff52014-09-03 12:57:30 -04009027 pring = &phba->sli.ring[ring_number];
9028 spin_lock_irqsave(&pring->ring_lock, iflags);
9029 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9030 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009031
James Smart9bd2bff52014-09-03 12:57:30 -04009032 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9033 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009034
James Smart9bd2bff52014-09-03 12:57:30 -04009035 if (atomic_dec_and_test(&fcp_eq_hdl->
9036 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009037
James Smart9bd2bff52014-09-03 12:57:30 -04009038 /* Get associated EQ with this index */
9039 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009040
James Smart9bd2bff52014-09-03 12:57:30 -04009041 /* Turn off interrupts from this EQ */
9042 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009043
James Smart9bd2bff52014-09-03 12:57:30 -04009044 /*
9045 * Process all the events on FCP EQ
9046 */
9047 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9048 lpfc_sli4_hba_handle_eqe(phba,
9049 eqe, idx);
9050 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009051 }
James Smartba20c852012-08-03 12:36:52 -04009052
James Smart9bd2bff52014-09-03 12:57:30 -04009053 /* Always clear and re-arm the EQ */
9054 lpfc_sli4_eq_release(fpeq,
9055 LPFC_QUEUE_REARM);
9056 }
9057 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009058 }
James Smart7e56aa22012-08-03 12:35:34 -04009059 } else {
9060 /* For now, SLI2/3 will still use hbalock */
9061 spin_lock_irqsave(&phba->hbalock, iflags);
9062 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9063 spin_unlock_irqrestore(&phba->hbalock, iflags);
9064 }
James Smart92d7f7b2007-06-17 19:56:38 -05009065 return rc;
9066}
9067
James Smarte59058c2008-08-24 21:49:00 -04009068/**
James Smart3621a712009-04-06 18:47:14 -04009069 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009070 * @phba: Pointer to HBA context object.
9071 *
9072 * This function is called while driver attaches with the
9073 * HBA to setup the extra ring. The extra ring is used
9074 * only when driver needs to support target mode functionality
9075 * or IP over FC functionalities.
9076 *
9077 * This function is called with no lock held.
9078 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009079static int
9080lpfc_extra_ring_setup( struct lpfc_hba *phba)
9081{
9082 struct lpfc_sli *psli;
9083 struct lpfc_sli_ring *pring;
9084
9085 psli = &phba->sli;
9086
9087 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009088
9089 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009090 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009091 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9092 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9093 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9094 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009095
James Smarta4bc3372006-12-02 13:34:16 -05009096 /* and give them to the extra ring */
9097 pring = &psli->ring[psli->extra_ring];
9098
James Smart7e56aa22012-08-03 12:35:34 -04009099 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9100 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9101 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9102 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009103
9104 /* Setup default profile for this ring */
9105 pring->iotag_max = 4096;
9106 pring->num_mask = 1;
9107 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009108 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9109 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009110 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9111 return 0;
9112}
9113
James Smartcb69f7d2011-12-13 13:21:57 -05009114/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9115 * @phba: Pointer to HBA context object.
9116 * @iocbq: Pointer to iocb object.
9117 *
9118 * The async_event handler calls this routine when it receives
9119 * an ASYNC_STATUS_CN event from the port. The port generates
9120 * this event when an Abort Sequence request to an rport fails
9121 * twice in succession. The abort could be originated by the
9122 * driver or by the port. The ABTS could have been for an ELS
9123 * or FCP IO. The port only generates this event when an ABTS
9124 * fails to complete after one retry.
9125 */
9126static void
9127lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9128 struct lpfc_iocbq *iocbq)
9129{
9130 struct lpfc_nodelist *ndlp = NULL;
9131 uint16_t rpi = 0, vpi = 0;
9132 struct lpfc_vport *vport = NULL;
9133
9134 /* The rpi in the ulpContext is vport-sensitive. */
9135 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9136 rpi = iocbq->iocb.ulpContext;
9137
9138 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9139 "3092 Port generated ABTS async event "
9140 "on vpi %d rpi %d status 0x%x\n",
9141 vpi, rpi, iocbq->iocb.ulpStatus);
9142
9143 vport = lpfc_find_vport_by_vpid(phba, vpi);
9144 if (!vport)
9145 goto err_exit;
9146 ndlp = lpfc_findnode_rpi(vport, rpi);
9147 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9148 goto err_exit;
9149
9150 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9151 lpfc_sli_abts_recover_port(vport, ndlp);
9152 return;
9153
9154 err_exit:
9155 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9156 "3095 Event Context not found, no "
9157 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9158 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9159 vpi, rpi);
9160}
9161
9162/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9163 * @phba: pointer to HBA context object.
9164 * @ndlp: nodelist pointer for the impacted rport.
9165 * @axri: pointer to the wcqe containing the failed exchange.
9166 *
9167 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9168 * port. The port generates this event when an abort exchange request to an
9169 * rport fails twice in succession with no reply. The abort could be originated
9170 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9171 */
9172void
9173lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9174 struct lpfc_nodelist *ndlp,
9175 struct sli4_wcqe_xri_aborted *axri)
9176{
9177 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009178 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009179
James Smart6b5151f2012-01-18 16:24:06 -05009180 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009181 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9182 "3115 Node Context not found, driver "
9183 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009184 return;
9185 }
9186
James Smartcb69f7d2011-12-13 13:21:57 -05009187 vport = ndlp->vport;
9188 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9189 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009190 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009191 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009192 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009193 bf_get(lpfc_wcqe_xa_status, axri),
9194 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009195
James Smart5c1db2a2012-03-01 22:34:36 -05009196 /*
9197 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9198 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9199 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9200 */
James Smarte3d2b802012-08-14 14:25:43 -04009201 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009202 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9203 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009204 lpfc_sli_abts_recover_port(vport, ndlp);
9205}
9206
James Smarte59058c2008-08-24 21:49:00 -04009207/**
James Smart3621a712009-04-06 18:47:14 -04009208 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009209 * @phba: Pointer to HBA context object.
9210 * @pring: Pointer to driver SLI ring object.
9211 * @iocbq: Pointer to iocb object.
9212 *
9213 * This function is called by the slow ring event handler
9214 * function when there is an ASYNC event iocb in the ring.
9215 * This function is called with no lock held.
9216 * Currently this function handles only temperature related
9217 * ASYNC events. The function decodes the temperature sensor
9218 * event message and posts events for the management applications.
9219 **/
James Smart98c9ea52007-10-27 13:37:33 -04009220static void
James Smart57127f12007-10-27 13:37:05 -04009221lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9222 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9223{
9224 IOCB_t *icmd;
9225 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009226 struct temp_event temp_event_data;
9227 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009228 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009229
9230 icmd = &iocbq->iocb;
9231 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009232
James Smartcb69f7d2011-12-13 13:21:57 -05009233 switch (evt_code) {
9234 case ASYNC_TEMP_WARN:
9235 case ASYNC_TEMP_SAFE:
9236 temp_event_data.data = (uint32_t) icmd->ulpContext;
9237 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9238 if (evt_code == ASYNC_TEMP_WARN) {
9239 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9240 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9241 "0347 Adapter is very hot, please take "
9242 "corrective action. temperature : %d Celsius\n",
9243 (uint32_t) icmd->ulpContext);
9244 } else {
9245 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9246 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9247 "0340 Adapter temperature is OK now. "
9248 "temperature : %d Celsius\n",
9249 (uint32_t) icmd->ulpContext);
9250 }
9251
9252 /* Send temperature change event to applications */
9253 shost = lpfc_shost_from_vport(phba->pport);
9254 fc_host_post_vendor_event(shost, fc_get_event_number(),
9255 sizeof(temp_event_data), (char *) &temp_event_data,
9256 LPFC_NL_VENDOR_ID);
9257 break;
9258 case ASYNC_STATUS_CN:
9259 lpfc_sli_abts_err_handler(phba, iocbq);
9260 break;
9261 default:
James Smarta257bf92009-04-06 18:48:10 -04009262 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009264 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009265 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009266 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9267 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9268 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9269 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009270 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009271 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9272 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9273 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9274 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9275
James Smartcb69f7d2011-12-13 13:21:57 -05009276 break;
James Smart57127f12007-10-27 13:37:05 -04009277 }
James Smart57127f12007-10-27 13:37:05 -04009278}
9279
9280
James Smarte59058c2008-08-24 21:49:00 -04009281/**
James Smart3621a712009-04-06 18:47:14 -04009282 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009283 * @phba: Pointer to HBA context object.
9284 *
9285 * lpfc_sli_setup sets up rings of the SLI interface with
9286 * number of iocbs per ring and iotags. This function is
9287 * called while driver attach to the HBA and before the
9288 * interrupts are enabled. So there is no need for locking.
9289 *
9290 * This function always returns 0.
9291 **/
dea31012005-04-17 16:05:31 -05009292int
9293lpfc_sli_setup(struct lpfc_hba *phba)
9294{
James Smarted957682007-06-17 19:56:37 -05009295 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009296 struct lpfc_sli *psli = &phba->sli;
9297 struct lpfc_sli_ring *pring;
9298
James Smart2a76a282012-08-03 12:35:54 -04009299 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9300 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009301 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009302 psli->sli_flag = 0;
9303 psli->fcp_ring = LPFC_FCP_RING;
9304 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009305 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009306
James Bottomley604a3e32005-10-29 10:28:33 -05009307 psli->iocbq_lookup = NULL;
9308 psli->iocbq_lookup_len = 0;
9309 psli->last_iotag = 0;
9310
dea31012005-04-17 16:05:31 -05009311 for (i = 0; i < psli->num_rings; i++) {
9312 pring = &psli->ring[i];
9313 switch (i) {
9314 case LPFC_FCP_RING: /* ring 0 - FCP */
9315 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009316 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9317 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9318 pring->sli.sli3.numCiocb +=
9319 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9320 pring->sli.sli3.numRiocb +=
9321 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9322 pring->sli.sli3.numCiocb +=
9323 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9324 pring->sli.sli3.numRiocb +=
9325 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9326 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009327 SLI3_IOCB_CMD_SIZE :
9328 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009329 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009330 SLI3_IOCB_RSP_SIZE :
9331 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009332 pring->iotag_ctr = 0;
9333 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009334 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009335 pring->fast_iotag = pring->iotag_max;
9336 pring->num_mask = 0;
9337 break;
James Smarta4bc3372006-12-02 13:34:16 -05009338 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009339 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009340 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9341 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9342 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009343 SLI3_IOCB_CMD_SIZE :
9344 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009345 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009346 SLI3_IOCB_RSP_SIZE :
9347 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009348 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009349 pring->num_mask = 0;
9350 break;
9351 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9352 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009353 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9354 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9355 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009356 SLI3_IOCB_CMD_SIZE :
9357 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009358 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009359 SLI3_IOCB_RSP_SIZE :
9360 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009361 pring->fast_iotag = 0;
9362 pring->iotag_ctr = 0;
9363 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009364 pring->lpfc_sli_rcv_async_status =
9365 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009366 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009367 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009368 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9369 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009370 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009371 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009372 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009373 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9374 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009375 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009376 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009377 pring->prt[2].profile = 0; /* Mask 2 */
9378 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009379 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009380 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009381 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009382 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009383 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009384 pring->prt[3].profile = 0; /* Mask 3 */
9385 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009386 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009387 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009388 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009389 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009390 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009391 break;
9392 }
James Smart7e56aa22012-08-03 12:35:34 -04009393 totiocbsize += (pring->sli.sli3.numCiocb *
9394 pring->sli.sli3.sizeCiocb) +
9395 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009396 }
James Smarted957682007-06-17 19:56:37 -05009397 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009398 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009399 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9400 "SLI2 SLIM Data: x%x x%lx\n",
9401 phba->brd_no, totiocbsize,
9402 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009403 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009404 if (phba->cfg_multi_ring_support == 2)
9405 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009406
9407 return 0;
9408}
9409
James Smarte59058c2008-08-24 21:49:00 -04009410/**
James Smart3621a712009-04-06 18:47:14 -04009411 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009412 * @phba: Pointer to HBA context object.
9413 *
9414 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9415 * ring. This function also initializes ring indices of each ring.
9416 * This function is called during the initialization of the SLI
9417 * interface of an HBA.
9418 * This function is called with no lock held and always returns
9419 * 1.
9420 **/
dea31012005-04-17 16:05:31 -05009421int
James Smart2e0fef82007-06-17 19:56:36 -05009422lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009423{
9424 struct lpfc_sli *psli;
9425 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009426 int i;
dea31012005-04-17 16:05:31 -05009427
9428 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009429 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009430 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009431 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009432 /* Initialize list headers for txq and txcmplq as double linked lists */
9433 for (i = 0; i < psli->num_rings; i++) {
9434 pring = &psli->ring[i];
9435 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009436 pring->sli.sli3.next_cmdidx = 0;
9437 pring->sli.sli3.local_getidx = 0;
9438 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009439 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009440 INIT_LIST_HEAD(&pring->txq);
9441 INIT_LIST_HEAD(&pring->txcmplq);
9442 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009443 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009444 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009445 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009446 }
James Smart2e0fef82007-06-17 19:56:36 -05009447 spin_unlock_irq(&phba->hbalock);
9448 return 1;
dea31012005-04-17 16:05:31 -05009449}
9450
James Smarte59058c2008-08-24 21:49:00 -04009451/**
James Smart04c68492009-05-22 14:52:52 -04009452 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9453 * @phba: Pointer to HBA context object.
9454 *
9455 * This routine flushes the mailbox command subsystem. It will unconditionally
9456 * flush all the mailbox commands in the three possible stages in the mailbox
9457 * command sub-system: pending mailbox command queue; the outstanding mailbox
9458 * command; and completed mailbox command queue. It is caller's responsibility
9459 * to make sure that the driver is in the proper state to flush the mailbox
9460 * command sub-system. Namely, the posting of mailbox commands into the
9461 * pending mailbox command queue from the various clients must be stopped;
9462 * either the HBA is in a state that it will never works on the outstanding
9463 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9464 * mailbox command has been completed.
9465 **/
9466static void
9467lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9468{
9469 LIST_HEAD(completions);
9470 struct lpfc_sli *psli = &phba->sli;
9471 LPFC_MBOXQ_t *pmb;
9472 unsigned long iflag;
9473
9474 /* Flush all the mailbox commands in the mbox system */
9475 spin_lock_irqsave(&phba->hbalock, iflag);
9476 /* The pending mailbox command queue */
9477 list_splice_init(&phba->sli.mboxq, &completions);
9478 /* The outstanding active mailbox command */
9479 if (psli->mbox_active) {
9480 list_add_tail(&psli->mbox_active->list, &completions);
9481 psli->mbox_active = NULL;
9482 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9483 }
9484 /* The completed mailbox command queue */
9485 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9486 spin_unlock_irqrestore(&phba->hbalock, iflag);
9487
9488 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9489 while (!list_empty(&completions)) {
9490 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9491 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9492 if (pmb->mbox_cmpl)
9493 pmb->mbox_cmpl(phba, pmb);
9494 }
9495}
9496
9497/**
James Smart3621a712009-04-06 18:47:14 -04009498 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009499 * @vport: Pointer to virtual port object.
9500 *
9501 * lpfc_sli_host_down is called to clean up the resources
9502 * associated with a vport before destroying virtual
9503 * port data structures.
9504 * This function does following operations:
9505 * - Free discovery resources associated with this virtual
9506 * port.
9507 * - Free iocbs associated with this virtual port in
9508 * the txq.
9509 * - Send abort for all iocb commands associated with this
9510 * vport in txcmplq.
9511 *
9512 * This function is called with no lock held and always returns 1.
9513 **/
dea31012005-04-17 16:05:31 -05009514int
James Smart92d7f7b2007-06-17 19:56:38 -05009515lpfc_sli_host_down(struct lpfc_vport *vport)
9516{
James Smart858c9f62007-06-17 19:56:39 -05009517 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009518 struct lpfc_hba *phba = vport->phba;
9519 struct lpfc_sli *psli = &phba->sli;
9520 struct lpfc_sli_ring *pring;
9521 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009522 int i;
9523 unsigned long flags = 0;
9524 uint16_t prev_pring_flag;
9525
9526 lpfc_cleanup_discovery_resources(vport);
9527
9528 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009529 for (i = 0; i < psli->num_rings; i++) {
9530 pring = &psli->ring[i];
9531 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009532 /* Only slow rings */
9533 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009534 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009535 /* Set the lpfc data pending flag */
9536 set_bit(LPFC_DATA_READY, &phba->data_flags);
9537 }
James Smart92d7f7b2007-06-17 19:56:38 -05009538 /*
9539 * Error everything on the txq since these iocbs have not been
9540 * given to the FW yet.
9541 */
James Smart92d7f7b2007-06-17 19:56:38 -05009542 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9543 if (iocb->vport != vport)
9544 continue;
James Smart858c9f62007-06-17 19:56:39 -05009545 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009546 }
9547
9548 /* Next issue ABTS for everything on the txcmplq */
9549 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9550 list) {
9551 if (iocb->vport != vport)
9552 continue;
9553 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9554 }
9555
9556 pring->flag = prev_pring_flag;
9557 }
9558
9559 spin_unlock_irqrestore(&phba->hbalock, flags);
9560
James Smarta257bf92009-04-06 18:48:10 -04009561 /* Cancel all the IOCBs from the completions list */
9562 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9563 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009564 return 1;
9565}
9566
James Smarte59058c2008-08-24 21:49:00 -04009567/**
James Smart3621a712009-04-06 18:47:14 -04009568 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009569 * @phba: Pointer to HBA context object.
9570 *
9571 * This function cleans up all iocb, buffers, mailbox commands
9572 * while shutting down the HBA. This function is called with no
9573 * lock held and always returns 1.
9574 * This function does the following to cleanup driver resources:
9575 * - Free discovery resources for each virtual port
9576 * - Cleanup any pending fabric iocbs
9577 * - Iterate through the iocb txq and free each entry
9578 * in the list.
9579 * - Free up any buffer posted to the HBA
9580 * - Free mailbox commands in the mailbox queue.
9581 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009582int
James Smart2e0fef82007-06-17 19:56:36 -05009583lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009584{
James Smart2534ba72007-04-25 09:52:20 -04009585 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009586 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009587 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009588 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009589 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009590 int i;
9591
9592 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009593 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009594
dea31012005-04-17 16:05:31 -05009595 lpfc_hba_down_prep(phba);
9596
James Smart92d7f7b2007-06-17 19:56:38 -05009597 lpfc_fabric_abort_hba(phba);
9598
James Smart2e0fef82007-06-17 19:56:36 -05009599 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009600 for (i = 0; i < psli->num_rings; i++) {
9601 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009602 /* Only slow rings */
9603 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009604 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009605 /* Set the lpfc data pending flag */
9606 set_bit(LPFC_DATA_READY, &phba->data_flags);
9607 }
dea31012005-04-17 16:05:31 -05009608
9609 /*
9610 * Error everything on the txq since these iocbs have not been
9611 * given to the FW yet.
9612 */
James Smart2534ba72007-04-25 09:52:20 -04009613 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009614 }
James Smart2e0fef82007-06-17 19:56:36 -05009615 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009616
James Smarta257bf92009-04-06 18:48:10 -04009617 /* Cancel all the IOCBs from the completions list */
9618 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9619 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009620
James Smart0ff10d42008-01-11 01:52:36 -05009621 spin_lock_irqsave(&phba->hbalock, flags);
9622 list_splice_init(&phba->elsbuf, &completions);
9623 phba->elsbuf_cnt = 0;
9624 phba->elsbuf_prev_cnt = 0;
9625 spin_unlock_irqrestore(&phba->hbalock, flags);
9626
9627 while (!list_empty(&completions)) {
9628 list_remove_head(&completions, buf_ptr,
9629 struct lpfc_dmabuf, list);
9630 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9631 kfree(buf_ptr);
9632 }
9633
dea31012005-04-17 16:05:31 -05009634 /* Return any active mbox cmds */
9635 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009636
James Smartda0436e2009-05-22 14:51:39 -04009637 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009638 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009639 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009640
James Smartda0436e2009-05-22 14:51:39 -04009641 return 1;
9642}
James Smart92d7f7b2007-06-17 19:56:38 -05009643
James Smartda0436e2009-05-22 14:51:39 -04009644/**
James Smart3621a712009-04-06 18:47:14 -04009645 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009646 * @srcp: Source memory pointer.
9647 * @destp: Destination memory pointer.
9648 * @cnt: Number of words required to be copied.
9649 *
9650 * This function is used for copying data between driver memory
9651 * and the SLI memory. This function also changes the endianness
9652 * of each word if native endianness is different from SLI
9653 * endianness. This function can be called with or without
9654 * lock.
9655 **/
dea31012005-04-17 16:05:31 -05009656void
9657lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9658{
9659 uint32_t *src = srcp;
9660 uint32_t *dest = destp;
9661 uint32_t ldata;
9662 int i;
9663
9664 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9665 ldata = *src;
9666 ldata = le32_to_cpu(ldata);
9667 *dest = ldata;
9668 src++;
9669 dest++;
9670 }
9671}
9672
James Smarte59058c2008-08-24 21:49:00 -04009673
9674/**
James Smarta0c87cb2009-07-19 10:01:10 -04009675 * lpfc_sli_bemem_bcopy - SLI memory copy function
9676 * @srcp: Source memory pointer.
9677 * @destp: Destination memory pointer.
9678 * @cnt: Number of words required to be copied.
9679 *
9680 * This function is used for copying data between a data structure
9681 * with big endian representation to local endianness.
9682 * This function can be called with or without lock.
9683 **/
9684void
9685lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9686{
9687 uint32_t *src = srcp;
9688 uint32_t *dest = destp;
9689 uint32_t ldata;
9690 int i;
9691
9692 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9693 ldata = *src;
9694 ldata = be32_to_cpu(ldata);
9695 *dest = ldata;
9696 src++;
9697 dest++;
9698 }
9699}
9700
9701/**
James Smart3621a712009-04-06 18:47:14 -04009702 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009703 * @phba: Pointer to HBA context object.
9704 * @pring: Pointer to driver SLI ring object.
9705 * @mp: Pointer to driver buffer object.
9706 *
9707 * This function is called with no lock held.
9708 * It always return zero after adding the buffer to the postbufq
9709 * buffer list.
9710 **/
dea31012005-04-17 16:05:31 -05009711int
James Smart2e0fef82007-06-17 19:56:36 -05009712lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9713 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009714{
9715 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9716 later */
James Smart2e0fef82007-06-17 19:56:36 -05009717 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009718 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009719 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009720 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009721 return 0;
9722}
9723
James Smarte59058c2008-08-24 21:49:00 -04009724/**
James Smart3621a712009-04-06 18:47:14 -04009725 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009726 * @phba: Pointer to HBA context object.
9727 *
9728 * When HBQ is enabled, buffers are searched based on tags. This function
9729 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9730 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9731 * does not conflict with tags of buffer posted for unsolicited events.
9732 * The function returns the allocated tag. The function is called with
9733 * no locks held.
9734 **/
James Smart76bb24e2007-10-27 13:38:00 -04009735uint32_t
9736lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9737{
9738 spin_lock_irq(&phba->hbalock);
9739 phba->buffer_tag_count++;
9740 /*
9741 * Always set the QUE_BUFTAG_BIT to distiguish between
9742 * a tag assigned by HBQ.
9743 */
9744 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9745 spin_unlock_irq(&phba->hbalock);
9746 return phba->buffer_tag_count;
9747}
9748
James Smarte59058c2008-08-24 21:49:00 -04009749/**
James Smart3621a712009-04-06 18:47:14 -04009750 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009751 * @phba: Pointer to HBA context object.
9752 * @pring: Pointer to driver SLI ring object.
9753 * @tag: Buffer tag.
9754 *
9755 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9756 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9757 * iocb is posted to the response ring with the tag of the buffer.
9758 * This function searches the pring->postbufq list using the tag
9759 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9760 * iocb. If the buffer is found then lpfc_dmabuf object of the
9761 * buffer is returned to the caller else NULL is returned.
9762 * This function is called with no lock held.
9763 **/
James Smart76bb24e2007-10-27 13:38:00 -04009764struct lpfc_dmabuf *
9765lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9766 uint32_t tag)
9767{
9768 struct lpfc_dmabuf *mp, *next_mp;
9769 struct list_head *slp = &pring->postbufq;
9770
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009771 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009772 spin_lock_irq(&phba->hbalock);
9773 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9774 if (mp->buffer_tag == tag) {
9775 list_del_init(&mp->list);
9776 pring->postbufq_cnt--;
9777 spin_unlock_irq(&phba->hbalock);
9778 return mp;
9779 }
9780 }
9781
9782 spin_unlock_irq(&phba->hbalock);
9783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009784 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009785 "ring %d Data x%lx x%p x%p x%x\n",
9786 pring->ringno, (unsigned long) tag,
9787 slp->next, slp->prev, pring->postbufq_cnt);
9788
9789 return NULL;
9790}
dea31012005-04-17 16:05:31 -05009791
James Smarte59058c2008-08-24 21:49:00 -04009792/**
James Smart3621a712009-04-06 18:47:14 -04009793 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009794 * @phba: Pointer to HBA context object.
9795 * @pring: Pointer to driver SLI ring object.
9796 * @phys: DMA address of the buffer.
9797 *
9798 * This function searches the buffer list using the dma_address
9799 * of unsolicited event to find the driver's lpfc_dmabuf object
9800 * corresponding to the dma_address. The function returns the
9801 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9802 * This function is called by the ct and els unsolicited event
9803 * handlers to get the buffer associated with the unsolicited
9804 * event.
9805 *
9806 * This function is called with no lock held.
9807 **/
dea31012005-04-17 16:05:31 -05009808struct lpfc_dmabuf *
9809lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9810 dma_addr_t phys)
9811{
9812 struct lpfc_dmabuf *mp, *next_mp;
9813 struct list_head *slp = &pring->postbufq;
9814
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009815 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009816 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009817 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9818 if (mp->phys == phys) {
9819 list_del_init(&mp->list);
9820 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009821 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009822 return mp;
9823 }
9824 }
9825
James Smart2e0fef82007-06-17 19:56:36 -05009826 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009828 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009829 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009830 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009831 slp->next, slp->prev, pring->postbufq_cnt);
9832 return NULL;
9833}
9834
James Smarte59058c2008-08-24 21:49:00 -04009835/**
James Smart3621a712009-04-06 18:47:14 -04009836 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009837 * @phba: Pointer to HBA context object.
9838 * @cmdiocb: Pointer to driver command iocb object.
9839 * @rspiocb: Pointer to driver response iocb object.
9840 *
9841 * This function is the completion handler for the abort iocbs for
9842 * ELS commands. This function is called from the ELS ring event
9843 * handler with no lock held. This function frees memory resources
9844 * associated with the abort iocb.
9845 **/
dea31012005-04-17 16:05:31 -05009846static void
James Smart2e0fef82007-06-17 19:56:36 -05009847lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9848 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009849{
James Smart2e0fef82007-06-17 19:56:36 -05009850 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009851 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009852 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009853
9854 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009855
9856 /*
9857 * Assume that the port already completed and returned, or
9858 * will return the iocb. Just Log the message.
9859 */
James Smart2680eea2007-04-25 09:52:55 -04009860 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9861 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9862
James Smart2e0fef82007-06-17 19:56:36 -05009863 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009864 if (phba->sli_rev < LPFC_SLI_REV4) {
9865 if (abort_iotag != 0 &&
9866 abort_iotag <= phba->sli.last_iotag)
9867 abort_iocb =
9868 phba->sli.iocbq_lookup[abort_iotag];
9869 } else
9870 /* For sli4 the abort_tag is the XRI,
9871 * so the abort routine puts the iotag of the iocb
9872 * being aborted in the context field of the abort
9873 * IOCB.
9874 */
9875 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009876
James Smart2a9bf3d2010-06-07 15:24:45 -04009877 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9878 "0327 Cannot abort els iocb %p "
9879 "with tag %x context %x, abort status %x, "
9880 "abort code %x\n",
9881 abort_iocb, abort_iotag, abort_context,
9882 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009883
James Smartff78d8f2011-12-13 13:21:35 -05009884 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009885 }
James Bottomley604a3e32005-10-29 10:28:33 -05009886 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009887 return;
9888}
9889
James Smarte59058c2008-08-24 21:49:00 -04009890/**
James Smart3621a712009-04-06 18:47:14 -04009891 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009892 * @phba: Pointer to HBA context object.
9893 * @cmdiocb: Pointer to driver command iocb object.
9894 * @rspiocb: Pointer to driver response iocb object.
9895 *
9896 * The function is called from SLI ring event handler with no
9897 * lock held. This function is the completion handler for ELS commands
9898 * which are aborted. The function frees memory resources used for
9899 * the aborted ELS commands.
9900 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009901static void
9902lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9903 struct lpfc_iocbq *rspiocb)
9904{
9905 IOCB_t *irsp = &rspiocb->iocb;
9906
9907 /* ELS cmd tag <ulpIoTag> completes */
9908 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009909 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009910 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009911 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009912 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009913 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9914 lpfc_ct_free_iocb(phba, cmdiocb);
9915 else
9916 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009917 return;
9918}
9919
James Smarte59058c2008-08-24 21:49:00 -04009920/**
James Smart5af5eee2010-10-22 11:06:38 -04009921 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009922 * @phba: Pointer to HBA context object.
9923 * @pring: Pointer to driver SLI ring object.
9924 * @cmdiocb: Pointer to driver command iocb object.
9925 *
James Smart5af5eee2010-10-22 11:06:38 -04009926 * This function issues an abort iocb for the provided command iocb down to
9927 * the port. Other than the case the outstanding command iocb is an abort
9928 * request, this function issues abort out unconditionally. This function is
9929 * called with hbalock held. The function returns 0 when it fails due to
9930 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009931 **/
James Smart5af5eee2010-10-22 11:06:38 -04009932static int
9933lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009934 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009935{
James Smart2e0fef82007-06-17 19:56:36 -05009936 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009937 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009938 IOCB_t *icmd = NULL;
9939 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009940 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009941 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009942 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009943
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009944 lockdep_assert_held(&phba->hbalock);
9945
James Smart92d7f7b2007-06-17 19:56:38 -05009946 /*
9947 * There are certain command types we don't want to abort. And we
9948 * don't want to abort commands that are already in the process of
9949 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009950 */
9951 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009952 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009953 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9954 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009955 return 0;
9956
dea31012005-04-17 16:05:31 -05009957 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009958 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009959 if (abtsiocbp == NULL)
9960 return 0;
dea31012005-04-17 16:05:31 -05009961
James Smart07951072007-04-25 09:51:38 -04009962 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009963 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009964 */
9965 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9966
dea31012005-04-17 16:05:31 -05009967 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009968 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9969 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009970 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009971 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009972 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9973 }
James Smartda0436e2009-05-22 14:51:39 -04009974 else
9975 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009976 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009977 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009978
James Smart5ffc2662009-11-18 15:39:44 -05009979 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9980 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009981 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9982 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04009983 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
9984 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05009985
James Smart2e0fef82007-06-17 19:56:36 -05009986 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009987 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9988 else
9989 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9990
9991 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009992
James Smarte8b62012007-08-02 11:10:09 -04009993 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9994 "0339 Abort xri x%x, original iotag x%x, "
9995 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009996 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009997 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009998 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009999
10000 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -040010001 ring_number =
10002 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10003 if (unlikely(ring_number == LPFC_HBA_ERROR))
10004 return 0;
10005 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -040010006 /* Note: both hbalock and ring_lock need to be set here */
10007 spin_lock_irqsave(&pring->ring_lock, iflags);
10008 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10009 abtsiocbp, 0);
10010 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10011 } else {
10012 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10013 abtsiocbp, 0);
10014 }
James Smart07951072007-04-25 09:51:38 -040010015
James Smartd7c255b2008-08-24 21:50:00 -040010016 if (retval)
10017 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010018
10019 /*
10020 * Caller to this routine should check for IOCB_ERROR
10021 * and handle it properly. This routine no longer removes
10022 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10023 */
10024 return retval;
10025}
10026
10027/**
10028 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10029 * @phba: Pointer to HBA context object.
10030 * @pring: Pointer to driver SLI ring object.
10031 * @cmdiocb: Pointer to driver command iocb object.
10032 *
10033 * This function issues an abort iocb for the provided command iocb. In case
10034 * of unloading, the abort iocb will not be issued to commands on the ELS
10035 * ring. Instead, the callback function shall be changed to those commands
10036 * so that nothing happens when them finishes. This function is called with
10037 * hbalock held. The function returns 0 when the command iocb is an abort
10038 * request.
10039 **/
10040int
10041lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10042 struct lpfc_iocbq *cmdiocb)
10043{
10044 struct lpfc_vport *vport = cmdiocb->vport;
10045 int retval = IOCB_ERROR;
10046 IOCB_t *icmd = NULL;
10047
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010048 lockdep_assert_held(&phba->hbalock);
10049
James Smart5af5eee2010-10-22 11:06:38 -040010050 /*
10051 * There are certain command types we don't want to abort. And we
10052 * don't want to abort commands that are already in the process of
10053 * being aborted.
10054 */
10055 icmd = &cmdiocb->iocb;
10056 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10057 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10058 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10059 return 0;
10060
10061 /*
10062 * If we're unloading, don't abort iocb on the ELS ring, but change
10063 * the callback so that nothing happens when it finishes.
10064 */
10065 if ((vport->load_flag & FC_UNLOADING) &&
10066 (pring->ringno == LPFC_ELS_RING)) {
10067 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10068 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10069 else
10070 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10071 goto abort_iotag_exit;
10072 }
10073
10074 /* Now, we try to issue the abort to the cmdiocb out */
10075 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10076
James Smart07951072007-04-25 09:51:38 -040010077abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010078 /*
10079 * Caller to this routine should check for IOCB_ERROR
10080 * and handle it properly. This routine no longer removes
10081 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010082 */
James Smart2e0fef82007-06-17 19:56:36 -050010083 return retval;
dea31012005-04-17 16:05:31 -050010084}
10085
James Smarte59058c2008-08-24 21:49:00 -040010086/**
James Smart5af5eee2010-10-22 11:06:38 -040010087 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10088 * @phba: pointer to lpfc HBA data structure.
10089 *
10090 * This routine will abort all pending and outstanding iocbs to an HBA.
10091 **/
10092void
10093lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10094{
10095 struct lpfc_sli *psli = &phba->sli;
10096 struct lpfc_sli_ring *pring;
10097 int i;
10098
10099 for (i = 0; i < psli->num_rings; i++) {
10100 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010101 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010102 }
10103}
10104
10105/**
James Smart3621a712009-04-06 18:47:14 -040010106 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010107 * @iocbq: Pointer to driver iocb object.
10108 * @vport: Pointer to driver virtual port object.
10109 * @tgt_id: SCSI ID of the target.
10110 * @lun_id: LUN ID of the scsi device.
10111 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10112 *
James Smart3621a712009-04-06 18:47:14 -040010113 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010114 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10115 * 0 if the filtering criteria is met for the given iocb and will return
10116 * 1 if the filtering criteria is not met.
10117 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10118 * given iocb is for the SCSI device specified by vport, tgt_id and
10119 * lun_id parameter.
10120 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10121 * given iocb is for the SCSI target specified by vport and tgt_id
10122 * parameters.
10123 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10124 * given iocb is for the SCSI host associated with the given vport.
10125 * This function is called with no locks held.
10126 **/
dea31012005-04-17 16:05:31 -050010127static int
James Smart51ef4c22007-08-02 11:10:31 -040010128lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10129 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010130 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010131{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010132 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010133 int rc = 1;
10134
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010135 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10136 return rc;
10137
James Smart51ef4c22007-08-02 11:10:31 -040010138 if (iocbq->vport != vport)
10139 return rc;
10140
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010141 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010142
James Smart495a7142008-06-14 22:52:59 -040010143 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010144 return rc;
10145
10146 switch (ctx_cmd) {
10147 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010148 if ((lpfc_cmd->rdata->pnode) &&
10149 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10150 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010151 rc = 0;
10152 break;
10153 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010154 if ((lpfc_cmd->rdata->pnode) &&
10155 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010156 rc = 0;
10157 break;
dea31012005-04-17 16:05:31 -050010158 case LPFC_CTX_HOST:
10159 rc = 0;
10160 break;
10161 default:
10162 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010163 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010164 break;
10165 }
10166
10167 return rc;
10168}
10169
James Smarte59058c2008-08-24 21:49:00 -040010170/**
James Smart3621a712009-04-06 18:47:14 -040010171 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010172 * @vport: Pointer to virtual port.
10173 * @tgt_id: SCSI ID of the target.
10174 * @lun_id: LUN ID of the scsi device.
10175 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10176 *
10177 * This function returns number of FCP commands pending for the vport.
10178 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10179 * commands pending on the vport associated with SCSI device specified
10180 * by tgt_id and lun_id parameters.
10181 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10182 * commands pending on the vport associated with SCSI target specified
10183 * by tgt_id parameter.
10184 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10185 * commands pending on the vport.
10186 * This function returns the number of iocbs which satisfy the filter.
10187 * This function is called without any lock held.
10188 **/
dea31012005-04-17 16:05:31 -050010189int
James Smart51ef4c22007-08-02 11:10:31 -040010190lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10191 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010192{
James Smart51ef4c22007-08-02 11:10:31 -040010193 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010194 struct lpfc_iocbq *iocbq;
10195 int sum, i;
dea31012005-04-17 16:05:31 -050010196
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010197 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10198 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010199
James Smart51ef4c22007-08-02 11:10:31 -040010200 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10201 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010202 sum++;
dea31012005-04-17 16:05:31 -050010203 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010204
dea31012005-04-17 16:05:31 -050010205 return sum;
10206}
10207
James Smarte59058c2008-08-24 21:49:00 -040010208/**
James Smart3621a712009-04-06 18:47:14 -040010209 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010210 * @phba: Pointer to HBA context object
10211 * @cmdiocb: Pointer to command iocb object.
10212 * @rspiocb: Pointer to response iocb object.
10213 *
10214 * This function is called when an aborted FCP iocb completes. This
10215 * function is called by the ring event handler with no lock held.
10216 * This function frees the iocb.
10217 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010218void
James Smart2e0fef82007-06-17 19:56:36 -050010219lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10220 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010221{
James Smartcb69f7d2011-12-13 13:21:57 -050010222 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010223 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010224 "original iotag x%x, abort cmd iotag x%x "
10225 "status 0x%x, reason 0x%x\n",
10226 cmdiocb->iocb.un.acxri.abortContextTag,
10227 cmdiocb->iocb.un.acxri.abortIoTag,
10228 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10229 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010230 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010231 return;
10232}
10233
James Smarte59058c2008-08-24 21:49:00 -040010234/**
James Smart3621a712009-04-06 18:47:14 -040010235 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010236 * @vport: Pointer to virtual port.
10237 * @pring: Pointer to driver SLI ring object.
10238 * @tgt_id: SCSI ID of the target.
10239 * @lun_id: LUN ID of the scsi device.
10240 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10241 *
10242 * This function sends an abort command for every SCSI command
10243 * associated with the given virtual port pending on the ring
10244 * filtered by lpfc_sli_validate_fcp_iocb function.
10245 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10246 * FCP iocbs associated with lun specified by tgt_id and lun_id
10247 * parameters
10248 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10249 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10250 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10251 * FCP iocbs associated with virtual port.
10252 * This function returns number of iocbs it failed to abort.
10253 * This function is called with no locks held.
10254 **/
dea31012005-04-17 16:05:31 -050010255int
James Smart51ef4c22007-08-02 11:10:31 -040010256lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10257 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010258{
James Smart51ef4c22007-08-02 11:10:31 -040010259 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010260 struct lpfc_iocbq *iocbq;
10261 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010262 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010263 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010264 int i;
dea31012005-04-17 16:05:31 -050010265
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010266 for (i = 1; i <= phba->sli.last_iotag; i++) {
10267 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010268
James Smart51ef4c22007-08-02 11:10:31 -040010269 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010270 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010271 continue;
10272
James Smartafbd8d82013-09-06 12:22:13 -040010273 /*
10274 * If the iocbq is already being aborted, don't take a second
10275 * action, but do count it.
10276 */
10277 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10278 continue;
10279
dea31012005-04-17 16:05:31 -050010280 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010281 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010282 if (abtsiocb == NULL) {
10283 errcnt++;
10284 continue;
10285 }
dea31012005-04-17 16:05:31 -050010286
James Smartafbd8d82013-09-06 12:22:13 -040010287 /* indicate the IO is being aborted by the driver. */
10288 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10289
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010290 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010291 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10292 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010293 if (phba->sli_rev == LPFC_SLI_REV4)
10294 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10295 else
10296 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010297 abtsiocb->iocb.ulpLe = 1;
10298 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010299 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010300
James Smart5ffc2662009-11-18 15:39:44 -050010301 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10302 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010303 if (iocbq->iocb_flag & LPFC_IO_FCP)
10304 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010305 if (iocbq->iocb_flag & LPFC_IO_FOF)
10306 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010307
James Smart2e0fef82007-06-17 19:56:36 -050010308 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010309 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10310 else
10311 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10312
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010313 /* Setup callback routine and issue the command. */
10314 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010315 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10316 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010317 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010318 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010319 errcnt++;
10320 continue;
10321 }
10322 }
10323
10324 return errcnt;
10325}
10326
James Smarte59058c2008-08-24 21:49:00 -040010327/**
James Smart98912dda2014-04-04 13:52:31 -040010328 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10329 * @vport: Pointer to virtual port.
10330 * @pring: Pointer to driver SLI ring object.
10331 * @tgt_id: SCSI ID of the target.
10332 * @lun_id: LUN ID of the scsi device.
10333 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10334 *
10335 * This function sends an abort command for every SCSI command
10336 * associated with the given virtual port pending on the ring
10337 * filtered by lpfc_sli_validate_fcp_iocb function.
10338 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10339 * FCP iocbs associated with lun specified by tgt_id and lun_id
10340 * parameters
10341 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10342 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10343 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10344 * FCP iocbs associated with virtual port.
10345 * This function returns number of iocbs it aborted .
10346 * This function is called with no locks held right after a taskmgmt
10347 * command is sent.
10348 **/
10349int
10350lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10351 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10352{
10353 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010354 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010355 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010356 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010357 struct lpfc_iocbq *iocbq;
10358 IOCB_t *icmd;
10359 int sum, i, ret_val;
10360 unsigned long iflags;
10361 struct lpfc_sli_ring *pring_s4;
10362 uint32_t ring_number;
10363
10364 spin_lock_irq(&phba->hbalock);
10365
10366 /* all I/Os are in process of being flushed */
10367 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10368 spin_unlock_irq(&phba->hbalock);
10369 return 0;
10370 }
10371 sum = 0;
10372
10373 for (i = 1; i <= phba->sli.last_iotag; i++) {
10374 iocbq = phba->sli.iocbq_lookup[i];
10375
10376 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10377 cmd) != 0)
10378 continue;
10379
10380 /*
10381 * If the iocbq is already being aborted, don't take a second
10382 * action, but do count it.
10383 */
10384 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10385 continue;
10386
10387 /* issue ABTS for this IOCB based on iotag */
10388 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10389 if (abtsiocbq == NULL)
10390 continue;
10391
10392 icmd = &iocbq->iocb;
10393 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10394 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10395 if (phba->sli_rev == LPFC_SLI_REV4)
10396 abtsiocbq->iocb.un.acxri.abortIoTag =
10397 iocbq->sli4_xritag;
10398 else
10399 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10400 abtsiocbq->iocb.ulpLe = 1;
10401 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10402 abtsiocbq->vport = vport;
10403
10404 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10405 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10406 if (iocbq->iocb_flag & LPFC_IO_FCP)
10407 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010408 if (iocbq->iocb_flag & LPFC_IO_FOF)
10409 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010410
James Smart8c50d252014-09-03 12:58:16 -040010411 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10412 ndlp = lpfc_cmd->rdata->pnode;
10413
10414 if (lpfc_is_link_up(phba) &&
10415 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010416 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10417 else
10418 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10419
10420 /* Setup callback routine and issue the command. */
10421 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10422
10423 /*
10424 * Indicate the IO is being aborted by the driver and set
10425 * the caller's flag into the aborted IO.
10426 */
10427 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10428
10429 if (phba->sli_rev == LPFC_SLI_REV4) {
10430 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10431 iocbq->fcp_wqidx;
10432 pring_s4 = &phba->sli.ring[ring_number];
10433 /* Note: both hbalock and ring_lock must be set here */
10434 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10435 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10436 abtsiocbq, 0);
10437 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10438 } else {
10439 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10440 abtsiocbq, 0);
10441 }
10442
10443
10444 if (ret_val == IOCB_ERROR)
10445 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10446 else
10447 sum++;
10448 }
10449 spin_unlock_irq(&phba->hbalock);
10450 return sum;
10451}
10452
10453/**
James Smart3621a712009-04-06 18:47:14 -040010454 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010455 * @phba: Pointer to HBA context object.
10456 * @cmdiocbq: Pointer to command iocb.
10457 * @rspiocbq: Pointer to response iocb.
10458 *
10459 * This function is the completion handler for iocbs issued using
10460 * lpfc_sli_issue_iocb_wait function. This function is called by the
10461 * ring event handler function without any lock held. This function
10462 * can be called from both worker thread context and interrupt
10463 * context. This function also can be called from other thread which
10464 * cleans up the SLI layer objects.
10465 * This function copy the contents of the response iocb to the
10466 * response iocb memory object provided by the caller of
10467 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10468 * sleeps for the iocb completion.
10469 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010470static void
10471lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10472 struct lpfc_iocbq *cmdiocbq,
10473 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010474{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010475 wait_queue_head_t *pdone_q;
10476 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010477 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010478
James Smart2e0fef82007-06-17 19:56:36 -050010479 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010480 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10481
10482 /*
10483 * A time out has occurred for the iocb. If a time out
10484 * completion handler has been supplied, call it. Otherwise,
10485 * just free the iocbq.
10486 */
10487
10488 spin_unlock_irqrestore(&phba->hbalock, iflags);
10489 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10490 cmdiocbq->wait_iocb_cmpl = NULL;
10491 if (cmdiocbq->iocb_cmpl)
10492 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10493 else
10494 lpfc_sli_release_iocbq(phba, cmdiocbq);
10495 return;
10496 }
10497
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010498 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10499 if (cmdiocbq->context2 && rspiocbq)
10500 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10501 &rspiocbq->iocb, sizeof(IOCB_t));
10502
James Smart0f65ff62010-02-26 14:14:23 -050010503 /* Set the exchange busy flag for task management commands */
10504 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10505 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10506 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10507 cur_iocbq);
10508 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10509 }
10510
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010511 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010512 if (pdone_q)
10513 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010514 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010515 return;
10516}
10517
James Smarte59058c2008-08-24 21:49:00 -040010518/**
James Smartd11e31d2009-06-10 17:23:06 -040010519 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10520 * @phba: Pointer to HBA context object..
10521 * @piocbq: Pointer to command iocb.
10522 * @flag: Flag to test.
10523 *
10524 * This routine grabs the hbalock and then test the iocb_flag to
10525 * see if the passed in flag is set.
10526 * Returns:
10527 * 1 if flag is set.
10528 * 0 if flag is not set.
10529 **/
10530static int
10531lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10532 struct lpfc_iocbq *piocbq, uint32_t flag)
10533{
10534 unsigned long iflags;
10535 int ret;
10536
10537 spin_lock_irqsave(&phba->hbalock, iflags);
10538 ret = piocbq->iocb_flag & flag;
10539 spin_unlock_irqrestore(&phba->hbalock, iflags);
10540 return ret;
10541
10542}
10543
10544/**
James Smart3621a712009-04-06 18:47:14 -040010545 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010546 * @phba: Pointer to HBA context object..
10547 * @pring: Pointer to sli ring.
10548 * @piocb: Pointer to command iocb.
10549 * @prspiocbq: Pointer to response iocb.
10550 * @timeout: Timeout in number of seconds.
10551 *
10552 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010553 * iocb to complete. The iocb_cmpl field of the shall be used
10554 * to handle iocbs which time out. If the field is NULL, the
10555 * function shall free the iocbq structure. If more clean up is
10556 * needed, the caller is expected to provide a completion function
10557 * that will provide the needed clean up. If the iocb command is
10558 * not completed within timeout seconds, the function will either
10559 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10560 * completion function set in the iocb_cmpl field and then return
10561 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10562 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010563 * The function waits for the iocb completion using an
10564 * non-interruptible wait.
10565 * This function will sleep while waiting for iocb completion.
10566 * So, this function should not be called from any context which
10567 * does not allow sleeping. Due to the same reason, this function
10568 * cannot be called with interrupt disabled.
10569 * This function assumes that the iocb completions occur while
10570 * this function sleep. So, this function cannot be called from
10571 * the thread which process iocb completion for this ring.
10572 * This function clears the iocb_flag of the iocb object before
10573 * issuing the iocb and the iocb completion handler sets this
10574 * flag and wakes this thread when the iocb completes.
10575 * The contents of the response iocb will be copied to prspiocbq
10576 * by the completion handler when the command completes.
10577 * This function returns IOCB_SUCCESS when success.
10578 * This function is called with no lock held.
10579 **/
dea31012005-04-17 16:05:31 -050010580int
James Smart2e0fef82007-06-17 19:56:36 -050010581lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010582 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010583 struct lpfc_iocbq *piocb,
10584 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010585 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010586{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010587 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010588 long timeleft, timeout_req = 0;
10589 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010590 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010591 struct lpfc_iocbq *iocb;
10592 int txq_cnt = 0;
10593 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010594 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010595 unsigned long iflags;
10596 bool iocb_completed = true;
10597
dea31012005-04-17 16:05:31 -050010598 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010599 * If the caller has provided a response iocbq buffer, then context2
10600 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010601 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010602 if (prspiocbq) {
10603 if (piocb->context2)
10604 return IOCB_ERROR;
10605 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010606 }
10607
James Smart5a0916b2013-07-15 18:31:42 -040010608 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010609 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10610 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010611 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010612
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010613 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010614 if (lpfc_readl(phba->HCregaddr, &creg_val))
10615 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010616 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10617 writel(creg_val, phba->HCregaddr);
10618 readl(phba->HCregaddr); /* flush */
10619 }
10620
James Smart2a9bf3d2010-06-07 15:24:45 -040010621 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10622 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010623 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010624 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010625 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010626 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010627 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010628 spin_lock_irqsave(&phba->hbalock, iflags);
10629 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010630
James Smart5a0916b2013-07-15 18:31:42 -040010631 /*
10632 * IOCB timed out. Inform the wake iocb wait
10633 * completion function and set local status
10634 */
10635
10636 iocb_completed = false;
10637 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10638 }
10639 spin_unlock_irqrestore(&phba->hbalock, iflags);
10640 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010641 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010642 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010643 /* Note: we are not indicating if the IOCB has a success
10644 * status or not - that's for the caller to check.
10645 * IOCB_SUCCESS means just that the command was sent and
10646 * completed. Not that it completed successfully.
10647 * */
James Smart7054a602007-04-25 09:52:34 -040010648 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010649 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010650 "0338 IOCB wait timeout error - no "
10651 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010652 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010653 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010654 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010655 "0330 IOCB wake NOT set, "
10656 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010657 timeout, (timeleft / jiffies));
10658 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010659 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010660 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010661 if (phba->cfg_log_verbose & LOG_SLI) {
10662 list_for_each_entry(iocb, &pring->txq, list) {
10663 txq_cnt++;
10664 }
10665 list_for_each_entry(iocb, &pring->txcmplq, list) {
10666 txcmplq_cnt++;
10667 }
10668 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10669 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10670 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10671 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010672 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010673 } else {
10674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010675 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010676 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010677 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010678 }
10679
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010680 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010681 if (lpfc_readl(phba->HCregaddr, &creg_val))
10682 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010683 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10684 writel(creg_val, phba->HCregaddr);
10685 readl(phba->HCregaddr); /* flush */
10686 }
10687
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010688 if (prspiocbq)
10689 piocb->context2 = NULL;
10690
10691 piocb->context_un.wait_queue = NULL;
10692 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010693 return retval;
10694}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010695
James Smarte59058c2008-08-24 21:49:00 -040010696/**
James Smart3621a712009-04-06 18:47:14 -040010697 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010698 * @phba: Pointer to HBA context object.
10699 * @pmboxq: Pointer to driver mailbox object.
10700 * @timeout: Timeout in number of seconds.
10701 *
10702 * This function issues the mailbox to firmware and waits for the
10703 * mailbox command to complete. If the mailbox command is not
10704 * completed within timeout seconds, it returns MBX_TIMEOUT.
10705 * The function waits for the mailbox completion using an
10706 * interruptible wait. If the thread is woken up due to a
10707 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10708 * should not free the mailbox resources, if this function returns
10709 * MBX_TIMEOUT.
10710 * This function will sleep while waiting for mailbox completion.
10711 * So, this function should not be called from any context which
10712 * does not allow sleeping. Due to the same reason, this function
10713 * cannot be called with interrupt disabled.
10714 * This function assumes that the mailbox completion occurs while
10715 * this function sleep. So, this function cannot be called from
10716 * the worker thread which processes mailbox completion.
10717 * This function is called in the context of HBA management
10718 * applications.
10719 * This function returns MBX_SUCCESS when successful.
10720 * This function is called with no lock held.
10721 **/
dea31012005-04-17 16:05:31 -050010722int
James Smart2e0fef82007-06-17 19:56:36 -050010723lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010724 uint32_t timeout)
10725{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010726 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010727 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010728 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010729 unsigned long flag;
dea31012005-04-17 16:05:31 -050010730
James Smartb230b8a2013-05-31 17:05:27 -040010731 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010732 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010733 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010734
James Smart495a7142008-06-14 22:52:59 -040010735 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010736 /* setup wake call as IOCB callback */
10737 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10738 /* setup context field to pass wait_queue pointer to wake function */
10739 pmboxq->context1 = &done_q;
10740
dea31012005-04-17 16:05:31 -050010741 /* now issue the command */
10742 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010743 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010744 wait_event_interruptible_timeout(done_q,
10745 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010746 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010747
James Smart858c9f62007-06-17 19:56:39 -050010748 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010749 /* restore the possible extended buffer for free resource */
10750 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010751 /*
10752 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10753 * else do not free the resources.
10754 */
James Smartd7c47992010-06-08 18:31:54 -040010755 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010756 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010757 } else {
James Smart7054a602007-04-25 09:52:34 -040010758 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010759 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10760 }
10761 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010762 } else {
10763 /* restore the possible extended buffer for free resource */
10764 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010765 }
10766
dea31012005-04-17 16:05:31 -050010767 return retval;
10768}
10769
James Smarte59058c2008-08-24 21:49:00 -040010770/**
James Smart3772a992009-05-22 14:50:54 -040010771 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010772 * @phba: Pointer to HBA context.
10773 *
James Smart3772a992009-05-22 14:50:54 -040010774 * This function is called to shutdown the driver's mailbox sub-system.
10775 * It first marks the mailbox sub-system is in a block state to prevent
10776 * the asynchronous mailbox command from issued off the pending mailbox
10777 * command queue. If the mailbox command sub-system shutdown is due to
10778 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10779 * the mailbox sub-system flush routine to forcefully bring down the
10780 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10781 * as with offline or HBA function reset), this routine will wait for the
10782 * outstanding mailbox command to complete before invoking the mailbox
10783 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010784 **/
James Smart3772a992009-05-22 14:50:54 -040010785void
James Smart618a5232012-06-12 13:54:36 -040010786lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010787{
James Smart3772a992009-05-22 14:50:54 -040010788 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010789 unsigned long timeout;
10790
James Smart618a5232012-06-12 13:54:36 -040010791 if (mbx_action == LPFC_MBX_NO_WAIT) {
10792 /* delay 100ms for port state */
10793 msleep(100);
10794 lpfc_sli_mbox_sys_flush(phba);
10795 return;
10796 }
James Smarta183a152011-10-10 21:32:43 -040010797 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010798
James Smart3772a992009-05-22 14:50:54 -040010799 spin_lock_irq(&phba->hbalock);
10800 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010801
10802 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010803 /* Determine how long we might wait for the active mailbox
10804 * command to be gracefully completed by firmware.
10805 */
James Smarta183a152011-10-10 21:32:43 -040010806 if (phba->sli.mbox_active)
10807 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10808 phba->sli.mbox_active) *
10809 1000) + jiffies;
10810 spin_unlock_irq(&phba->hbalock);
10811
James Smart3772a992009-05-22 14:50:54 -040010812 while (phba->sli.mbox_active) {
10813 /* Check active mailbox complete status every 2ms */
10814 msleep(2);
10815 if (time_after(jiffies, timeout))
10816 /* Timeout, let the mailbox flush routine to
10817 * forcefully release active mailbox command
10818 */
10819 break;
10820 }
James Smartd7069f02012-03-01 22:36:29 -050010821 } else
10822 spin_unlock_irq(&phba->hbalock);
10823
James Smart3772a992009-05-22 14:50:54 -040010824 lpfc_sli_mbox_sys_flush(phba);
10825}
10826
10827/**
10828 * lpfc_sli_eratt_read - read sli-3 error attention events
10829 * @phba: Pointer to HBA context.
10830 *
10831 * This function is called to read the SLI3 device error attention registers
10832 * for possible error attention events. The caller must hold the hostlock
10833 * with spin_lock_irq().
10834 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010835 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010836 * Register and returns 0 otherwise.
10837 **/
10838static int
10839lpfc_sli_eratt_read(struct lpfc_hba *phba)
10840{
James Smarted957682007-06-17 19:56:37 -050010841 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010842
James Smart3772a992009-05-22 14:50:54 -040010843 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010844 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10845 goto unplug_err;
10846
James Smart3772a992009-05-22 14:50:54 -040010847 if (ha_copy & HA_ERATT) {
10848 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010849 if (lpfc_sli_read_hs(phba))
10850 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010851
James Smart3772a992009-05-22 14:50:54 -040010852 /* Check if there is a deferred error condition is active */
10853 if ((HS_FFER1 & phba->work_hs) &&
10854 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010855 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010856 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010857 /* Clear all interrupt enable conditions */
10858 writel(0, phba->HCregaddr);
10859 readl(phba->HCregaddr);
10860 }
10861
10862 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010863 phba->work_ha |= HA_ERATT;
10864 /* Indicate polling handles this ERATT */
10865 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010866 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010867 }
James Smart3772a992009-05-22 14:50:54 -040010868 return 0;
James Smart9940b972011-03-11 16:06:12 -050010869
10870unplug_err:
10871 /* Set the driver HS work bitmap */
10872 phba->work_hs |= UNPLUG_ERR;
10873 /* Set the driver HA work bitmap */
10874 phba->work_ha |= HA_ERATT;
10875 /* Indicate polling handles this ERATT */
10876 phba->hba_flag |= HBA_ERATT_HANDLED;
10877 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010878}
10879
James Smarte59058c2008-08-24 21:49:00 -040010880/**
James Smartda0436e2009-05-22 14:51:39 -040010881 * lpfc_sli4_eratt_read - read sli-4 error attention events
10882 * @phba: Pointer to HBA context.
10883 *
10884 * This function is called to read the SLI4 device error attention registers
10885 * for possible error attention events. The caller must hold the hostlock
10886 * with spin_lock_irq().
10887 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010888 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010889 * Register and returns 0 otherwise.
10890 **/
10891static int
10892lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10893{
10894 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010895 uint32_t if_type, portsmphr;
10896 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010897
James Smart2fcee4b2010-12-15 17:57:46 -050010898 /*
10899 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010900 * registers for error attention. This can be changed later.
10901 */
James Smart2fcee4b2010-12-15 17:57:46 -050010902 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10903 switch (if_type) {
10904 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010905 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10906 &uerr_sta_lo) ||
10907 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10908 &uerr_sta_hi)) {
10909 phba->work_hs |= UNPLUG_ERR;
10910 phba->work_ha |= HA_ERATT;
10911 phba->hba_flag |= HBA_ERATT_HANDLED;
10912 return 1;
10913 }
James Smart2fcee4b2010-12-15 17:57:46 -050010914 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10915 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10917 "1423 HBA Unrecoverable error: "
10918 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10919 "ue_mask_lo_reg=0x%x, "
10920 "ue_mask_hi_reg=0x%x\n",
10921 uerr_sta_lo, uerr_sta_hi,
10922 phba->sli4_hba.ue_mask_lo,
10923 phba->sli4_hba.ue_mask_hi);
10924 phba->work_status[0] = uerr_sta_lo;
10925 phba->work_status[1] = uerr_sta_hi;
10926 phba->work_ha |= HA_ERATT;
10927 phba->hba_flag |= HBA_ERATT_HANDLED;
10928 return 1;
10929 }
10930 break;
10931 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010932 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10933 &portstat_reg.word0) ||
10934 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10935 &portsmphr)){
10936 phba->work_hs |= UNPLUG_ERR;
10937 phba->work_ha |= HA_ERATT;
10938 phba->hba_flag |= HBA_ERATT_HANDLED;
10939 return 1;
10940 }
James Smart2fcee4b2010-12-15 17:57:46 -050010941 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10942 phba->work_status[0] =
10943 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10944 phba->work_status[1] =
10945 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010947 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010948 "port status reg 0x%x, "
10949 "port smphr reg 0x%x, "
10950 "error 1=0x%x, error 2=0x%x\n",
10951 portstat_reg.word0,
10952 portsmphr,
10953 phba->work_status[0],
10954 phba->work_status[1]);
10955 phba->work_ha |= HA_ERATT;
10956 phba->hba_flag |= HBA_ERATT_HANDLED;
10957 return 1;
10958 }
10959 break;
10960 case LPFC_SLI_INTF_IF_TYPE_1:
10961 default:
James Smarta747c9c2009-11-18 15:41:10 -050010962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010963 "2886 HBA Error Attention on unsupported "
10964 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010965 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010966 }
James Smart2fcee4b2010-12-15 17:57:46 -050010967
James Smartda0436e2009-05-22 14:51:39 -040010968 return 0;
10969}
10970
10971/**
James Smart3621a712009-04-06 18:47:14 -040010972 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010973 * @phba: Pointer to HBA context.
10974 *
James Smart3772a992009-05-22 14:50:54 -040010975 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010976 * error attention register bit for error attention events.
10977 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010978 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010979 * Register and returns 0 otherwise.
10980 **/
10981int
10982lpfc_sli_check_eratt(struct lpfc_hba *phba)
10983{
10984 uint32_t ha_copy;
10985
10986 /* If somebody is waiting to handle an eratt, don't process it
10987 * here. The brdkill function will do this.
10988 */
10989 if (phba->link_flag & LS_IGNORE_ERATT)
10990 return 0;
10991
10992 /* Check if interrupt handler handles this ERATT */
10993 spin_lock_irq(&phba->hbalock);
10994 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10995 /* Interrupt handler has handled ERATT */
10996 spin_unlock_irq(&phba->hbalock);
10997 return 0;
10998 }
10999
James Smarta257bf92009-04-06 18:48:10 -040011000 /*
11001 * If there is deferred error attention, do not check for error
11002 * attention
11003 */
11004 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11005 spin_unlock_irq(&phba->hbalock);
11006 return 0;
11007 }
11008
James Smart3772a992009-05-22 14:50:54 -040011009 /* If PCI channel is offline, don't process it */
11010 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011011 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011012 return 0;
11013 }
11014
11015 switch (phba->sli_rev) {
11016 case LPFC_SLI_REV2:
11017 case LPFC_SLI_REV3:
11018 /* Read chip Host Attention (HA) register */
11019 ha_copy = lpfc_sli_eratt_read(phba);
11020 break;
James Smartda0436e2009-05-22 14:51:39 -040011021 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011022 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011023 ha_copy = lpfc_sli4_eratt_read(phba);
11024 break;
James Smart3772a992009-05-22 14:50:54 -040011025 default:
11026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11027 "0299 Invalid SLI revision (%d)\n",
11028 phba->sli_rev);
11029 ha_copy = 0;
11030 break;
James Smart93996272008-08-24 21:50:30 -040011031 }
11032 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011033
11034 return ha_copy;
11035}
11036
11037/**
11038 * lpfc_intr_state_check - Check device state for interrupt handling
11039 * @phba: Pointer to HBA context.
11040 *
11041 * This inline routine checks whether a device or its PCI slot is in a state
11042 * that the interrupt should be handled.
11043 *
11044 * This function returns 0 if the device or the PCI slot is in a state that
11045 * interrupt should be handled, otherwise -EIO.
11046 */
11047static inline int
11048lpfc_intr_state_check(struct lpfc_hba *phba)
11049{
11050 /* If the pci channel is offline, ignore all the interrupts */
11051 if (unlikely(pci_channel_offline(phba->pcidev)))
11052 return -EIO;
11053
11054 /* Update device level interrupt statistics */
11055 phba->sli.slistat.sli_intr++;
11056
11057 /* Ignore all interrupts during initialization. */
11058 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11059 return -EIO;
11060
James Smart93996272008-08-24 21:50:30 -040011061 return 0;
11062}
11063
11064/**
James Smart3772a992009-05-22 14:50:54 -040011065 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011066 * @irq: Interrupt number.
11067 * @dev_id: The device context pointer.
11068 *
James Smart93996272008-08-24 21:50:30 -040011069 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011070 * service routine when device with SLI-3 interface spec is enabled with
11071 * MSI-X multi-message interrupt mode and there are slow-path events in
11072 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11073 * interrupt mode, this function is called as part of the device-level
11074 * interrupt handler. When the PCI slot is in error recovery or the HBA
11075 * is undergoing initialization, the interrupt handler will not process
11076 * the interrupt. The link attention and ELS ring attention events are
11077 * handled by the worker thread. The interrupt handler signals the worker
11078 * thread and returns for these events. This function is called without
11079 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011080 * structures.
11081 *
11082 * This function returns IRQ_HANDLED when interrupt is handled else it
11083 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011084 **/
dea31012005-04-17 16:05:31 -050011085irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011086lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011087{
James Smart2e0fef82007-06-17 19:56:36 -050011088 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011089 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011090 uint32_t work_ha_copy;
11091 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011092 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011093 uint32_t control;
11094
James Smart92d7f7b2007-06-17 19:56:38 -050011095 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011096 struct lpfc_vport *vport;
11097 struct lpfc_nodelist *ndlp;
11098 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011099 LPFC_MBOXQ_t *pmb;
11100 int rc;
11101
dea31012005-04-17 16:05:31 -050011102 /*
11103 * Get the driver's phba structure from the dev_id and
11104 * assume the HBA is not interrupting.
11105 */
James Smart93996272008-08-24 21:50:30 -040011106 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011107
11108 if (unlikely(!phba))
11109 return IRQ_NONE;
11110
dea31012005-04-17 16:05:31 -050011111 /*
James Smart93996272008-08-24 21:50:30 -040011112 * Stuff needs to be attented to when this function is invoked as an
11113 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011114 */
James Smart93996272008-08-24 21:50:30 -040011115 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011116 /* Check device state for handling interrupt */
11117 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011118 return IRQ_NONE;
11119 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011120 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011121 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11122 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011123 /* If somebody is waiting to handle an eratt don't process it
11124 * here. The brdkill function will do this.
11125 */
11126 if (phba->link_flag & LS_IGNORE_ERATT)
11127 ha_copy &= ~HA_ERATT;
11128 /* Check the need for handling ERATT in interrupt handler */
11129 if (ha_copy & HA_ERATT) {
11130 if (phba->hba_flag & HBA_ERATT_HANDLED)
11131 /* ERATT polling has handled ERATT */
11132 ha_copy &= ~HA_ERATT;
11133 else
11134 /* Indicate interrupt handler handles ERATT */
11135 phba->hba_flag |= HBA_ERATT_HANDLED;
11136 }
James Smarta257bf92009-04-06 18:48:10 -040011137
11138 /*
11139 * If there is deferred error attention, do not check for any
11140 * interrupt.
11141 */
11142 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011143 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011144 return IRQ_NONE;
11145 }
11146
James Smart93996272008-08-24 21:50:30 -040011147 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011148 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11149 goto unplug_error;
11150
James Smarta747c9c2009-11-18 15:41:10 -050011151 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11152 HC_LAINT_ENA | HC_ERINT_ENA),
11153 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011154 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11155 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011156 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011157 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011158 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011159 } else
11160 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011161
dea31012005-04-17 16:05:31 -050011162 work_ha_copy = ha_copy & phba->work_ha_mask;
11163
James Smart93996272008-08-24 21:50:30 -040011164 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011165 if (work_ha_copy & HA_LATT) {
11166 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11167 /*
11168 * Turn off Link Attention interrupts
11169 * until CLEAR_LA done
11170 */
James Smart5b75da22008-12-04 22:39:35 -050011171 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011172 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011173 if (lpfc_readl(phba->HCregaddr, &control))
11174 goto unplug_error;
dea31012005-04-17 16:05:31 -050011175 control &= ~HC_LAINT_ENA;
11176 writel(control, phba->HCregaddr);
11177 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011178 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011179 }
11180 else
11181 work_ha_copy &= ~HA_LATT;
11182 }
11183
James Smart93996272008-08-24 21:50:30 -040011184 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011185 /*
11186 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11187 * the only slow ring.
11188 */
11189 status = (work_ha_copy &
11190 (HA_RXMASK << (4*LPFC_ELS_RING)));
11191 status >>= (4*LPFC_ELS_RING);
11192 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011193 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011194 if (lpfc_readl(phba->HCregaddr, &control))
11195 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011196
11197 lpfc_debugfs_slow_ring_trc(phba,
11198 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11199 control, status,
11200 (uint32_t)phba->sli.slistat.sli_intr);
11201
James Smart858c9f62007-06-17 19:56:39 -050011202 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011203 lpfc_debugfs_slow_ring_trc(phba,
11204 "ISR Disable ring:"
11205 "pwork:x%x hawork:x%x wait:x%x",
11206 phba->work_ha, work_ha_copy,
11207 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011208 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011209
James Smart858c9f62007-06-17 19:56:39 -050011210 control &=
11211 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011212 writel(control, phba->HCregaddr);
11213 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011214 }
James Smarta58cbd52007-08-02 11:09:43 -040011215 else {
11216 lpfc_debugfs_slow_ring_trc(phba,
11217 "ISR slow ring: pwork:"
11218 "x%x hawork:x%x wait:x%x",
11219 phba->work_ha, work_ha_copy,
11220 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011221 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011222 }
James Smart5b75da22008-12-04 22:39:35 -050011223 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011224 }
11225 }
James Smart5b75da22008-12-04 22:39:35 -050011226 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011227 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011228 if (lpfc_sli_read_hs(phba))
11229 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011230 /*
11231 * Check if there is a deferred error condition
11232 * is active
11233 */
11234 if ((HS_FFER1 & phba->work_hs) &&
11235 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011236 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11237 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011238 phba->hba_flag |= DEFER_ERATT;
11239 /* Clear all interrupt enable conditions */
11240 writel(0, phba->HCregaddr);
11241 readl(phba->HCregaddr);
11242 }
11243 }
11244
James Smart93996272008-08-24 21:50:30 -040011245 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011246 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011247 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011248 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011249 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011250
11251 /* First check out the status word */
11252 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11253 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011254 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011255 /*
11256 * Stray Mailbox Interrupt, mbxCommand <cmd>
11257 * mbxStatus <status>
11258 */
James Smart09372822008-01-11 01:52:54 -050011259 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011260 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011261 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011262 "Interrupt mbxCommand x%x "
11263 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011264 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011265 pmbox->mbxCommand,
11266 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011267 /* clear mailbox attention bit */
11268 work_ha_copy &= ~HA_MBATT;
11269 } else {
James Smart97eab632008-04-07 10:16:05 -040011270 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011271 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011272 phba->last_completion_time = jiffies;
11273 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011274 if (pmb->mbox_cmpl) {
11275 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11276 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011277 if (pmb->out_ext_byte_len &&
11278 pmb->context2)
11279 lpfc_sli_pcimem_bcopy(
11280 phba->mbox_ext,
11281 pmb->context2,
11282 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011283 }
James Smart09372822008-01-11 01:52:54 -050011284 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11285 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11286
11287 lpfc_debugfs_disc_trc(vport,
11288 LPFC_DISC_TRC_MBOX_VPORT,
11289 "MBOX dflt rpi: : "
11290 "status:x%x rpi:x%x",
11291 (uint32_t)pmbox->mbxStatus,
11292 pmbox->un.varWords[0], 0);
11293
11294 if (!pmbox->mbxStatus) {
11295 mp = (struct lpfc_dmabuf *)
11296 (pmb->context1);
11297 ndlp = (struct lpfc_nodelist *)
11298 pmb->context2;
11299
11300 /* Reg_LOGIN of dflt RPI was
11301 * successful. new lets get
11302 * rid of the RPI using the
11303 * same mbox buffer.
11304 */
11305 lpfc_unreg_login(phba,
11306 vport->vpi,
11307 pmbox->un.varWords[0],
11308 pmb);
11309 pmb->mbox_cmpl =
11310 lpfc_mbx_cmpl_dflt_rpi;
11311 pmb->context1 = mp;
11312 pmb->context2 = ndlp;
11313 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011314 rc = lpfc_sli_issue_mbox(phba,
11315 pmb,
11316 MBX_NOWAIT);
11317 if (rc != MBX_BUSY)
11318 lpfc_printf_log(phba,
11319 KERN_ERR,
11320 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011321 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011322 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011323 if (rc != MBX_NOT_FINISHED)
11324 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011325 }
11326 }
James Smart5b75da22008-12-04 22:39:35 -050011327 spin_lock_irqsave(
11328 &phba->pport->work_port_lock,
11329 iflag);
James Smart09372822008-01-11 01:52:54 -050011330 phba->pport->work_port_events &=
11331 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011332 spin_unlock_irqrestore(
11333 &phba->pport->work_port_lock,
11334 iflag);
James Smart09372822008-01-11 01:52:54 -050011335 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011336 }
James Smart97eab632008-04-07 10:16:05 -040011337 } else
James Smart5b75da22008-12-04 22:39:35 -050011338 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011339
James Smart92d7f7b2007-06-17 19:56:38 -050011340 if ((work_ha_copy & HA_MBATT) &&
11341 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011342send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011343 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011344 do {
11345 rc = lpfc_sli_issue_mbox(phba, NULL,
11346 MBX_NOWAIT);
11347 } while (rc == MBX_NOT_FINISHED);
11348 if (rc != MBX_SUCCESS)
11349 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11350 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011351 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011352 }
11353
James Smart5b75da22008-12-04 22:39:35 -050011354 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011355 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011356 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011357 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011358 }
James Smart93996272008-08-24 21:50:30 -040011359 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011360unplug_error:
11361 spin_unlock_irqrestore(&phba->hbalock, iflag);
11362 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011363
James Smart3772a992009-05-22 14:50:54 -040011364} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011365
11366/**
James Smart3772a992009-05-22 14:50:54 -040011367 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011368 * @irq: Interrupt number.
11369 * @dev_id: The device context pointer.
11370 *
11371 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011372 * service routine when device with SLI-3 interface spec is enabled with
11373 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11374 * ring event in the HBA. However, when the device is enabled with either
11375 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11376 * device-level interrupt handler. When the PCI slot is in error recovery
11377 * or the HBA is undergoing initialization, the interrupt handler will not
11378 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11379 * the intrrupt context. This function is called without any lock held.
11380 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011381 *
11382 * This function returns IRQ_HANDLED when interrupt is handled else it
11383 * returns IRQ_NONE.
11384 **/
11385irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011386lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011387{
11388 struct lpfc_hba *phba;
11389 uint32_t ha_copy;
11390 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011391 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011392
11393 /* Get the driver's phba structure from the dev_id and
11394 * assume the HBA is not interrupting.
11395 */
11396 phba = (struct lpfc_hba *) dev_id;
11397
11398 if (unlikely(!phba))
11399 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011400
11401 /*
James Smart93996272008-08-24 21:50:30 -040011402 * Stuff needs to be attented to when this function is invoked as an
11403 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011404 */
James Smart93996272008-08-24 21:50:30 -040011405 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011406 /* Check device state for handling interrupt */
11407 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011408 return IRQ_NONE;
11409 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011410 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11411 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011412 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011413 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011414 /*
11415 * If there is deferred error attention, do not check for
11416 * any interrupt.
11417 */
11418 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011419 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011420 return IRQ_NONE;
11421 }
James Smart93996272008-08-24 21:50:30 -040011422 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11423 phba->HAregaddr);
11424 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011425 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011426 } else
11427 ha_copy = phba->ha_copy;
11428
11429 /*
11430 * Process all events on FCP ring. Take the optimized path for FCP IO.
11431 */
11432 ha_copy &= ~(phba->work_ha_mask);
11433
11434 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011435 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011436 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011437 lpfc_sli_handle_fast_ring_event(phba,
11438 &phba->sli.ring[LPFC_FCP_RING],
11439 status);
James Smarta4bc3372006-12-02 13:34:16 -050011440
11441 if (phba->cfg_multi_ring_support == 2) {
11442 /*
James Smart93996272008-08-24 21:50:30 -040011443 * Process all events on extra ring. Take the optimized path
11444 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011445 */
James Smart93996272008-08-24 21:50:30 -040011446 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011447 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011448 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011449 lpfc_sli_handle_fast_ring_event(phba,
11450 &phba->sli.ring[LPFC_EXTRA_RING],
11451 status);
11452 }
11453 }
dea31012005-04-17 16:05:31 -050011454 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011455} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011456
James Smart93996272008-08-24 21:50:30 -040011457/**
James Smart3772a992009-05-22 14:50:54 -040011458 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011459 * @irq: Interrupt number.
11460 * @dev_id: The device context pointer.
11461 *
James Smart3772a992009-05-22 14:50:54 -040011462 * This function is the HBA device-level interrupt handler to device with
11463 * SLI-3 interface spec, called from the PCI layer when either MSI or
11464 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11465 * requires driver attention. This function invokes the slow-path interrupt
11466 * attention handling function and fast-path interrupt attention handling
11467 * function in turn to process the relevant HBA attention events. This
11468 * function is called without any lock held. It gets the hbalock to access
11469 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011470 *
11471 * This function returns IRQ_HANDLED when interrupt is handled, else it
11472 * returns IRQ_NONE.
11473 **/
11474irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011475lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011476{
11477 struct lpfc_hba *phba;
11478 irqreturn_t sp_irq_rc, fp_irq_rc;
11479 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011480 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011481
11482 /*
11483 * Get the driver's phba structure from the dev_id and
11484 * assume the HBA is not interrupting.
11485 */
11486 phba = (struct lpfc_hba *) dev_id;
11487
11488 if (unlikely(!phba))
11489 return IRQ_NONE;
11490
James Smart3772a992009-05-22 14:50:54 -040011491 /* Check device state for handling interrupt */
11492 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011493 return IRQ_NONE;
11494
11495 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011496 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11497 spin_unlock(&phba->hbalock);
11498 return IRQ_HANDLED;
11499 }
11500
James Smart93996272008-08-24 21:50:30 -040011501 if (unlikely(!phba->ha_copy)) {
11502 spin_unlock(&phba->hbalock);
11503 return IRQ_NONE;
11504 } else if (phba->ha_copy & HA_ERATT) {
11505 if (phba->hba_flag & HBA_ERATT_HANDLED)
11506 /* ERATT polling has handled ERATT */
11507 phba->ha_copy &= ~HA_ERATT;
11508 else
11509 /* Indicate interrupt handler handles ERATT */
11510 phba->hba_flag |= HBA_ERATT_HANDLED;
11511 }
11512
James Smarta257bf92009-04-06 18:48:10 -040011513 /*
11514 * If there is deferred error attention, do not check for any interrupt.
11515 */
11516 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011517 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011518 return IRQ_NONE;
11519 }
11520
James Smart93996272008-08-24 21:50:30 -040011521 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011522 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11523 spin_unlock(&phba->hbalock);
11524 return IRQ_HANDLED;
11525 }
James Smarta747c9c2009-11-18 15:41:10 -050011526 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11527 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11528 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011529 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011530 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011531 readl(phba->HAregaddr); /* flush */
11532 spin_unlock(&phba->hbalock);
11533
11534 /*
11535 * Invokes slow-path host attention interrupt handling as appropriate.
11536 */
11537
11538 /* status of events with mailbox and link attention */
11539 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11540
11541 /* status of events with ELS ring */
11542 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11543 status2 >>= (4*LPFC_ELS_RING);
11544
11545 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011546 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011547 else
11548 sp_irq_rc = IRQ_NONE;
11549
11550 /*
11551 * Invoke fast-path host attention interrupt handling as appropriate.
11552 */
11553
11554 /* status of events with FCP ring */
11555 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11556 status1 >>= (4*LPFC_FCP_RING);
11557
11558 /* status of events with extra ring */
11559 if (phba->cfg_multi_ring_support == 2) {
11560 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11561 status2 >>= (4*LPFC_EXTRA_RING);
11562 } else
11563 status2 = 0;
11564
11565 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011566 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011567 else
11568 fp_irq_rc = IRQ_NONE;
11569
11570 /* Return device-level interrupt handling status */
11571 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011572} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011573
11574/**
11575 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11576 * @phba: pointer to lpfc hba data structure.
11577 *
11578 * This routine is invoked by the worker thread to process all the pending
11579 * SLI4 FCP abort XRI events.
11580 **/
11581void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11582{
11583 struct lpfc_cq_event *cq_event;
11584
11585 /* First, declare the fcp xri abort event has been handled */
11586 spin_lock_irq(&phba->hbalock);
11587 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11588 spin_unlock_irq(&phba->hbalock);
11589 /* Now, handle all the fcp xri abort events */
11590 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11591 /* Get the first event from the head of the event queue */
11592 spin_lock_irq(&phba->hbalock);
11593 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11594 cq_event, struct lpfc_cq_event, list);
11595 spin_unlock_irq(&phba->hbalock);
11596 /* Notify aborted XRI for FCP work queue */
11597 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11598 /* Free the event processed back to the free pool */
11599 lpfc_sli4_cq_event_release(phba, cq_event);
11600 }
11601}
11602
11603/**
11604 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11605 * @phba: pointer to lpfc hba data structure.
11606 *
11607 * This routine is invoked by the worker thread to process all the pending
11608 * SLI4 els abort xri events.
11609 **/
11610void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11611{
11612 struct lpfc_cq_event *cq_event;
11613
11614 /* First, declare the els xri abort event has been handled */
11615 spin_lock_irq(&phba->hbalock);
11616 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11617 spin_unlock_irq(&phba->hbalock);
11618 /* Now, handle all the els xri abort events */
11619 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11620 /* Get the first event from the head of the event queue */
11621 spin_lock_irq(&phba->hbalock);
11622 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11623 cq_event, struct lpfc_cq_event, list);
11624 spin_unlock_irq(&phba->hbalock);
11625 /* Notify aborted XRI for ELS work queue */
11626 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11627 /* Free the event processed back to the free pool */
11628 lpfc_sli4_cq_event_release(phba, cq_event);
11629 }
11630}
11631
James Smart341af102010-01-26 23:07:37 -050011632/**
11633 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11634 * @phba: pointer to lpfc hba data structure
11635 * @pIocbIn: pointer to the rspiocbq
11636 * @pIocbOut: pointer to the cmdiocbq
11637 * @wcqe: pointer to the complete wcqe
11638 *
11639 * This routine transfers the fields of a command iocbq to a response iocbq
11640 * by copying all the IOCB fields from command iocbq and transferring the
11641 * completion status information from the complete wcqe.
11642 **/
James Smart4f774512009-05-22 14:52:35 -040011643static void
James Smart341af102010-01-26 23:07:37 -050011644lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11645 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011646 struct lpfc_iocbq *pIocbOut,
11647 struct lpfc_wcqe_complete *wcqe)
11648{
James Smartaf227412013-10-10 12:23:10 -040011649 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011650 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011651 uint32_t status, max_response;
11652 struct lpfc_dmabuf *dmabuf;
11653 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011654 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11655
11656 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11657 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011658 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011659 status = bf_get(lpfc_wcqe_c_status, wcqe);
11660 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011661 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11662 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11663 pIocbIn->iocb.un.fcpi.fcpi_parm =
11664 pIocbOut->iocb.un.fcpi.fcpi_parm -
11665 wcqe->total_data_placed;
11666 else
11667 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011668 else {
James Smart4f774512009-05-22 14:52:35 -040011669 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011670 switch (pIocbOut->iocb.ulpCommand) {
11671 case CMD_ELS_REQUEST64_CR:
11672 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11673 bpl = (struct ulp_bde64 *)dmabuf->virt;
11674 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11675 max_response = bde.tus.f.bdeSize;
11676 break;
11677 case CMD_GEN_REQUEST64_CR:
11678 max_response = 0;
11679 if (!pIocbOut->context3)
11680 break;
11681 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11682 sizeof(struct ulp_bde64);
11683 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11684 bpl = (struct ulp_bde64 *)dmabuf->virt;
11685 for (i = 0; i < numBdes; i++) {
11686 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11687 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11688 max_response += bde.tus.f.bdeSize;
11689 }
11690 break;
11691 default:
11692 max_response = wcqe->total_data_placed;
11693 break;
11694 }
11695 if (max_response < wcqe->total_data_placed)
11696 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11697 else
11698 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11699 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011700 }
James Smart341af102010-01-26 23:07:37 -050011701
James Smartacd68592012-01-18 16:25:09 -050011702 /* Convert BG errors for completion status */
11703 if (status == CQE_STATUS_DI_ERROR) {
11704 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11705
11706 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11707 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11708 else
11709 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11710
11711 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11712 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11713 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11714 BGS_GUARD_ERR_MASK;
11715 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11716 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11717 BGS_APPTAG_ERR_MASK;
11718 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11719 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11720 BGS_REFTAG_ERR_MASK;
11721
11722 /* Check to see if there was any good data before the error */
11723 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11724 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11725 BGS_HI_WATER_MARK_PRESENT_MASK;
11726 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11727 wcqe->total_data_placed;
11728 }
11729
11730 /*
11731 * Set ALL the error bits to indicate we don't know what
11732 * type of error it is.
11733 */
11734 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11735 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11736 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11737 BGS_GUARD_ERR_MASK);
11738 }
11739
James Smart341af102010-01-26 23:07:37 -050011740 /* Pick up HBA exchange busy condition */
11741 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11742 spin_lock_irqsave(&phba->hbalock, iflags);
11743 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11744 spin_unlock_irqrestore(&phba->hbalock, iflags);
11745 }
James Smart4f774512009-05-22 14:52:35 -040011746}
11747
11748/**
James Smart45ed1192009-10-02 15:17:02 -040011749 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11750 * @phba: Pointer to HBA context object.
11751 * @wcqe: Pointer to work-queue completion queue entry.
11752 *
11753 * This routine handles an ELS work-queue completion event and construct
11754 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11755 * discovery engine to handle.
11756 *
11757 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11758 **/
11759static struct lpfc_iocbq *
11760lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11761 struct lpfc_iocbq *irspiocbq)
11762{
11763 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11764 struct lpfc_iocbq *cmdiocbq;
11765 struct lpfc_wcqe_complete *wcqe;
11766 unsigned long iflags;
11767
11768 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011769 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011770 pring->stats.iocb_event++;
11771 /* Look up the ELS command IOCB and create pseudo response IOCB */
11772 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11773 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011774 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011775
11776 if (unlikely(!cmdiocbq)) {
11777 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11778 "0386 ELS complete with no corresponding "
11779 "cmdiocb: iotag (%d)\n",
11780 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11781 lpfc_sli_release_iocbq(phba, irspiocbq);
11782 return NULL;
11783 }
11784
11785 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011786 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011787
11788 return irspiocbq;
11789}
11790
11791/**
James Smart04c68492009-05-22 14:52:52 -040011792 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11793 * @phba: Pointer to HBA context object.
11794 * @cqe: Pointer to mailbox completion queue entry.
11795 *
11796 * This routine process a mailbox completion queue entry with asynchrous
11797 * event.
11798 *
11799 * Return: true if work posted to worker thread, otherwise false.
11800 **/
11801static bool
11802lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11803{
11804 struct lpfc_cq_event *cq_event;
11805 unsigned long iflags;
11806
11807 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11808 "0392 Async Event: word0:x%x, word1:x%x, "
11809 "word2:x%x, word3:x%x\n", mcqe->word0,
11810 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11811
11812 /* Allocate a new internal CQ_EVENT entry */
11813 cq_event = lpfc_sli4_cq_event_alloc(phba);
11814 if (!cq_event) {
11815 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11816 "0394 Failed to allocate CQ_EVENT entry\n");
11817 return false;
11818 }
11819
11820 /* Move the CQE into an asynchronous event entry */
11821 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11822 spin_lock_irqsave(&phba->hbalock, iflags);
11823 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11824 /* Set the async event flag */
11825 phba->hba_flag |= ASYNC_EVENT;
11826 spin_unlock_irqrestore(&phba->hbalock, iflags);
11827
11828 return true;
11829}
11830
11831/**
11832 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11833 * @phba: Pointer to HBA context object.
11834 * @cqe: Pointer to mailbox completion queue entry.
11835 *
11836 * This routine process a mailbox completion queue entry with mailbox
11837 * completion event.
11838 *
11839 * Return: true if work posted to worker thread, otherwise false.
11840 **/
11841static bool
11842lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11843{
11844 uint32_t mcqe_status;
11845 MAILBOX_t *mbox, *pmbox;
11846 struct lpfc_mqe *mqe;
11847 struct lpfc_vport *vport;
11848 struct lpfc_nodelist *ndlp;
11849 struct lpfc_dmabuf *mp;
11850 unsigned long iflags;
11851 LPFC_MBOXQ_t *pmb;
11852 bool workposted = false;
11853 int rc;
11854
11855 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11856 if (!bf_get(lpfc_trailer_completed, mcqe))
11857 goto out_no_mqe_complete;
11858
11859 /* Get the reference to the active mbox command */
11860 spin_lock_irqsave(&phba->hbalock, iflags);
11861 pmb = phba->sli.mbox_active;
11862 if (unlikely(!pmb)) {
11863 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11864 "1832 No pending MBOX command to handle\n");
11865 spin_unlock_irqrestore(&phba->hbalock, iflags);
11866 goto out_no_mqe_complete;
11867 }
11868 spin_unlock_irqrestore(&phba->hbalock, iflags);
11869 mqe = &pmb->u.mqe;
11870 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11871 mbox = phba->mbox;
11872 vport = pmb->vport;
11873
11874 /* Reset heartbeat timer */
11875 phba->last_completion_time = jiffies;
11876 del_timer(&phba->sli.mbox_tmo);
11877
11878 /* Move mbox data to caller's mailbox region, do endian swapping */
11879 if (pmb->mbox_cmpl && mbox)
11880 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011881
James Smart73d91e52011-10-10 21:32:10 -040011882 /*
11883 * For mcqe errors, conditionally move a modified error code to
11884 * the mbox so that the error will not be missed.
11885 */
11886 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11887 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11888 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11889 bf_set(lpfc_mqe_status, mqe,
11890 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11891 }
James Smart04c68492009-05-22 14:52:52 -040011892 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11893 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11894 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11895 "MBOX dflt rpi: status:x%x rpi:x%x",
11896 mcqe_status,
11897 pmbox->un.varWords[0], 0);
11898 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11899 mp = (struct lpfc_dmabuf *)(pmb->context1);
11900 ndlp = (struct lpfc_nodelist *)pmb->context2;
11901 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11902 * RID of the PPI using the same mbox buffer.
11903 */
11904 lpfc_unreg_login(phba, vport->vpi,
11905 pmbox->un.varWords[0], pmb);
11906 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11907 pmb->context1 = mp;
11908 pmb->context2 = ndlp;
11909 pmb->vport = vport;
11910 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11911 if (rc != MBX_BUSY)
11912 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11913 LOG_SLI, "0385 rc should "
11914 "have been MBX_BUSY\n");
11915 if (rc != MBX_NOT_FINISHED)
11916 goto send_current_mbox;
11917 }
11918 }
11919 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11920 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11921 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11922
11923 /* There is mailbox completion work to do */
11924 spin_lock_irqsave(&phba->hbalock, iflags);
11925 __lpfc_mbox_cmpl_put(phba, pmb);
11926 phba->work_ha |= HA_MBATT;
11927 spin_unlock_irqrestore(&phba->hbalock, iflags);
11928 workposted = true;
11929
11930send_current_mbox:
11931 spin_lock_irqsave(&phba->hbalock, iflags);
11932 /* Release the mailbox command posting token */
11933 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11934 /* Setting active mailbox pointer need to be in sync to flag clear */
11935 phba->sli.mbox_active = NULL;
11936 spin_unlock_irqrestore(&phba->hbalock, iflags);
11937 /* Wake up worker thread to post the next pending mailbox command */
11938 lpfc_worker_wake_up(phba);
11939out_no_mqe_complete:
11940 if (bf_get(lpfc_trailer_consumed, mcqe))
11941 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11942 return workposted;
11943}
11944
11945/**
11946 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11947 * @phba: Pointer to HBA context object.
11948 * @cqe: Pointer to mailbox completion queue entry.
11949 *
11950 * This routine process a mailbox completion queue entry, it invokes the
11951 * proper mailbox complete handling or asynchrous event handling routine
11952 * according to the MCQE's async bit.
11953 *
11954 * Return: true if work posted to worker thread, otherwise false.
11955 **/
11956static bool
11957lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11958{
11959 struct lpfc_mcqe mcqe;
11960 bool workposted;
11961
11962 /* Copy the mailbox MCQE and convert endian order as needed */
11963 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11964
11965 /* Invoke the proper event handling routine */
11966 if (!bf_get(lpfc_trailer_async, &mcqe))
11967 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11968 else
11969 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11970 return workposted;
11971}
11972
11973/**
James Smart4f774512009-05-22 14:52:35 -040011974 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11975 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011976 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011977 * @wcqe: Pointer to work-queue completion queue entry.
11978 *
11979 * This routine handles an ELS work-queue completion event.
11980 *
11981 * Return: true if work posted to worker thread, otherwise false.
11982 **/
11983static bool
James Smart2a76a282012-08-03 12:35:54 -040011984lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011985 struct lpfc_wcqe_complete *wcqe)
11986{
James Smart4f774512009-05-22 14:52:35 -040011987 struct lpfc_iocbq *irspiocbq;
11988 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011989 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011990 int txq_cnt = 0;
11991 int txcmplq_cnt = 0;
11992 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040011993
James Smart45ed1192009-10-02 15:17:02 -040011994 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011995 irspiocbq = lpfc_sli_get_iocbq(phba);
11996 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011997 if (!list_empty(&pring->txq))
11998 txq_cnt++;
11999 if (!list_empty(&pring->txcmplq))
12000 txcmplq_cnt++;
12001 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12002 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012003 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012004 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12005 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012006 txq_cnt, phba->iocb_cnt,
12007 fcp_txcmplq_cnt,
12008 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012009 return false;
James Smart4f774512009-05-22 14:52:35 -040012010 }
James Smart4f774512009-05-22 14:52:35 -040012011
James Smart45ed1192009-10-02 15:17:02 -040012012 /* Save off the slow-path queue event for work thread to process */
12013 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012014 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012015 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012016 &phba->sli4_hba.sp_queue_event);
12017 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012018 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012019
James Smart45ed1192009-10-02 15:17:02 -040012020 return true;
James Smart4f774512009-05-22 14:52:35 -040012021}
12022
12023/**
12024 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12025 * @phba: Pointer to HBA context object.
12026 * @wcqe: Pointer to work-queue completion queue entry.
12027 *
12028 * This routine handles slow-path WQ entry comsumed event by invoking the
12029 * proper WQ release routine to the slow-path WQ.
12030 **/
12031static void
12032lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12033 struct lpfc_wcqe_release *wcqe)
12034{
James Smart2e90f4b2011-12-13 13:22:37 -050012035 /* sanity check on queue memory */
12036 if (unlikely(!phba->sli4_hba.els_wq))
12037 return;
James Smart4f774512009-05-22 14:52:35 -040012038 /* Check for the slow-path ELS work queue */
12039 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12040 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12041 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12042 else
12043 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12044 "2579 Slow-path wqe consume event carries "
12045 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12046 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12047 phba->sli4_hba.els_wq->queue_id);
12048}
12049
12050/**
12051 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12052 * @phba: Pointer to HBA context object.
12053 * @cq: Pointer to a WQ completion queue.
12054 * @wcqe: Pointer to work-queue completion queue entry.
12055 *
12056 * This routine handles an XRI abort event.
12057 *
12058 * Return: true if work posted to worker thread, otherwise false.
12059 **/
12060static bool
12061lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12062 struct lpfc_queue *cq,
12063 struct sli4_wcqe_xri_aborted *wcqe)
12064{
12065 bool workposted = false;
12066 struct lpfc_cq_event *cq_event;
12067 unsigned long iflags;
12068
12069 /* Allocate a new internal CQ_EVENT entry */
12070 cq_event = lpfc_sli4_cq_event_alloc(phba);
12071 if (!cq_event) {
12072 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12073 "0602 Failed to allocate CQ_EVENT entry\n");
12074 return false;
12075 }
12076
12077 /* Move the CQE into the proper xri abort event list */
12078 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12079 switch (cq->subtype) {
12080 case LPFC_FCP:
12081 spin_lock_irqsave(&phba->hbalock, iflags);
12082 list_add_tail(&cq_event->list,
12083 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12084 /* Set the fcp xri abort event flag */
12085 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12086 spin_unlock_irqrestore(&phba->hbalock, iflags);
12087 workposted = true;
12088 break;
12089 case LPFC_ELS:
12090 spin_lock_irqsave(&phba->hbalock, iflags);
12091 list_add_tail(&cq_event->list,
12092 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12093 /* Set the els xri abort event flag */
12094 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12095 spin_unlock_irqrestore(&phba->hbalock, iflags);
12096 workposted = true;
12097 break;
12098 default:
12099 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12100 "0603 Invalid work queue CQE subtype (x%x)\n",
12101 cq->subtype);
12102 workposted = false;
12103 break;
12104 }
12105 return workposted;
12106}
12107
12108/**
James Smart4d9ab992009-10-02 15:16:39 -040012109 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012110 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012111 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012112 *
James Smart4d9ab992009-10-02 15:16:39 -040012113 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012114 *
12115 * Return: true if work posted to worker thread, otherwise false.
12116 **/
12117static bool
James Smart4d9ab992009-10-02 15:16:39 -040012118lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12119{
12120 bool workposted = false;
12121 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12122 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12123 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012124 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012125 unsigned long iflags;
12126
James Smart2e90f4b2011-12-13 13:22:37 -050012127 /* sanity check on queue memory */
12128 if (unlikely(!hrq) || unlikely(!drq))
12129 return workposted;
12130
James Smart7851fe22011-07-22 18:36:52 -040012131 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12132 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12133 else
12134 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12135 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012136 goto out;
12137
12138 status = bf_get(lpfc_rcqe_status, rcqe);
12139 switch (status) {
12140 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12142 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012143 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012144 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012145 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012146 spin_lock_irqsave(&phba->hbalock, iflags);
12147 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12148 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012149 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012150 spin_unlock_irqrestore(&phba->hbalock, iflags);
12151 goto out;
12152 }
James Smartb84daac2012-08-03 12:35:13 -040012153 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012154 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12155 /* save off the frame for the word thread to process */
12156 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012157 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012158 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012159 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012160 spin_unlock_irqrestore(&phba->hbalock, iflags);
12161 workposted = true;
12162 break;
12163 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12164 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012165 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012166 /* Post more buffers if possible */
12167 spin_lock_irqsave(&phba->hbalock, iflags);
12168 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12169 spin_unlock_irqrestore(&phba->hbalock, iflags);
12170 workposted = true;
12171 break;
12172 }
12173out:
12174 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012175}
12176
12177/**
12178 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12179 * @phba: Pointer to HBA context object.
12180 * @cq: Pointer to the completion queue.
12181 * @wcqe: Pointer to a completion queue entry.
12182 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012183 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012184 * entry.
12185 *
12186 * Return: true if work posted to worker thread, otherwise false.
12187 **/
12188static bool
12189lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012190 struct lpfc_cqe *cqe)
12191{
James Smart45ed1192009-10-02 15:17:02 -040012192 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012193 bool workposted = false;
12194
12195 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012196 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012197
12198 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012199 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012200 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012201 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012202 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012203 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012204 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012205 break;
12206 case CQE_CODE_RELEASE_WQE:
12207 /* Process the WQ release event */
12208 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012209 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012210 break;
12211 case CQE_CODE_XRI_ABORTED:
12212 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012213 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012214 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012215 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012216 break;
James Smart4d9ab992009-10-02 15:16:39 -040012217 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012218 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012219 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012220 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012221 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012222 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012223 break;
James Smart4f774512009-05-22 14:52:35 -040012224 default:
12225 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12226 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012227 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012228 break;
12229 }
12230 return workposted;
12231}
12232
12233/**
James Smart4f774512009-05-22 14:52:35 -040012234 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12235 * @phba: Pointer to HBA context object.
12236 * @eqe: Pointer to fast-path event queue entry.
12237 *
12238 * This routine process a event queue entry from the slow-path event queue.
12239 * It will check the MajorCode and MinorCode to determine this is for a
12240 * completion event on a completion queue, if not, an error shall be logged
12241 * and just return. Otherwise, it will get to the corresponding completion
12242 * queue and process all the entries on that completion queue, rearm the
12243 * completion queue, and then return.
12244 *
12245 **/
12246static void
James Smart67d12732012-08-03 12:36:13 -040012247lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12248 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012249{
James Smart67d12732012-08-03 12:36:13 -040012250 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012251 struct lpfc_cqe *cqe;
12252 bool workposted = false;
12253 int ecount = 0;
12254 uint16_t cqid;
12255
James Smart4f774512009-05-22 14:52:35 -040012256 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012257 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012258
James Smart4f774512009-05-22 14:52:35 -040012259 list_for_each_entry(childq, &speq->child_list, list) {
12260 if (childq->queue_id == cqid) {
12261 cq = childq;
12262 break;
12263 }
12264 }
12265 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012266 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12267 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12268 "0365 Slow-path CQ identifier "
12269 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012270 return;
12271 }
12272
12273 /* Process all the entries to the CQ */
12274 switch (cq->type) {
12275 case LPFC_MCQ:
12276 while ((cqe = lpfc_sli4_cq_get(cq))) {
12277 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012278 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012279 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012280 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012281 }
12282 break;
12283 case LPFC_WCQ:
12284 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012285 if (cq->subtype == LPFC_FCP)
12286 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12287 cqe);
12288 else
12289 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12290 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012291 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012292 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12293 }
James Smartb84daac2012-08-03 12:35:13 -040012294
12295 /* Track the max number of CQEs processed in 1 EQ */
12296 if (ecount > cq->CQ_max_cqe)
12297 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012298 break;
12299 default:
12300 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12301 "0370 Invalid completion queue type (%d)\n",
12302 cq->type);
12303 return;
12304 }
12305
12306 /* Catch the no cq entry condition, log an error */
12307 if (unlikely(ecount == 0))
12308 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12309 "0371 No entry from the CQ: identifier "
12310 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12311
12312 /* In any case, flash and re-arm the RCQ */
12313 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12314
12315 /* wake up worker thread if there are works to be done */
12316 if (workposted)
12317 lpfc_worker_wake_up(phba);
12318}
12319
12320/**
12321 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012322 * @phba: Pointer to HBA context object.
12323 * @cq: Pointer to associated CQ
12324 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012325 *
12326 * This routine process a fast-path work queue completion entry from fast-path
12327 * event queue for FCP command response completion.
12328 **/
12329static void
James Smart2a76a282012-08-03 12:35:54 -040012330lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012331 struct lpfc_wcqe_complete *wcqe)
12332{
James Smart2a76a282012-08-03 12:35:54 -040012333 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012334 struct lpfc_iocbq *cmdiocbq;
12335 struct lpfc_iocbq irspiocbq;
12336 unsigned long iflags;
12337
James Smart4f774512009-05-22 14:52:35 -040012338 /* Check for response status */
12339 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12340 /* If resource errors reported from HBA, reduce queue
12341 * depth of the SCSI device.
12342 */
James Smarte3d2b802012-08-14 14:25:43 -040012343 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12344 IOSTAT_LOCAL_REJECT)) &&
12345 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12346 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012347 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012348
James Smart4f774512009-05-22 14:52:35 -040012349 /* Log the error status */
12350 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12351 "0373 FCP complete error: status=x%x, "
12352 "hw_status=x%x, total_data_specified=%d, "
12353 "parameter=x%x, word3=x%x\n",
12354 bf_get(lpfc_wcqe_c_status, wcqe),
12355 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12356 wcqe->total_data_placed, wcqe->parameter,
12357 wcqe->word3);
12358 }
12359
12360 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012361 spin_lock_irqsave(&pring->ring_lock, iflags);
12362 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012363 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12364 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012365 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012366 if (unlikely(!cmdiocbq)) {
12367 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12368 "0374 FCP complete with no corresponding "
12369 "cmdiocb: iotag (%d)\n",
12370 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12371 return;
12372 }
12373 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12374 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12375 "0375 FCP cmdiocb not callback function "
12376 "iotag: (%d)\n",
12377 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12378 return;
12379 }
12380
12381 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012382 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012383
James Smart0f65ff62010-02-26 14:14:23 -050012384 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12385 spin_lock_irqsave(&phba->hbalock, iflags);
12386 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12387 spin_unlock_irqrestore(&phba->hbalock, iflags);
12388 }
12389
James Smart4f774512009-05-22 14:52:35 -040012390 /* Pass the cmd_iocb and the rsp state to the upper layer */
12391 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12392}
12393
12394/**
12395 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12396 * @phba: Pointer to HBA context object.
12397 * @cq: Pointer to completion queue.
12398 * @wcqe: Pointer to work-queue completion queue entry.
12399 *
12400 * This routine handles an fast-path WQ entry comsumed event by invoking the
12401 * proper WQ release routine to the slow-path WQ.
12402 **/
12403static void
12404lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12405 struct lpfc_wcqe_release *wcqe)
12406{
12407 struct lpfc_queue *childwq;
12408 bool wqid_matched = false;
12409 uint16_t fcp_wqid;
12410
12411 /* Check for fast-path FCP work queue release */
12412 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12413 list_for_each_entry(childwq, &cq->child_list, list) {
12414 if (childwq->queue_id == fcp_wqid) {
12415 lpfc_sli4_wq_release(childwq,
12416 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12417 wqid_matched = true;
12418 break;
12419 }
12420 }
12421 /* Report warning log message if no match found */
12422 if (wqid_matched != true)
12423 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12424 "2580 Fast-path wqe consume event carries "
12425 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12426}
12427
12428/**
12429 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12430 * @cq: Pointer to the completion queue.
12431 * @eqe: Pointer to fast-path completion queue entry.
12432 *
12433 * This routine process a fast-path work queue completion entry from fast-path
12434 * event queue for FCP command response completion.
12435 **/
12436static int
12437lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12438 struct lpfc_cqe *cqe)
12439{
12440 struct lpfc_wcqe_release wcqe;
12441 bool workposted = false;
12442
12443 /* Copy the work queue CQE and convert endian order if needed */
12444 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12445
12446 /* Check and process for different type of WCQE and dispatch */
12447 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12448 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012449 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012450 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012451 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012452 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012453 (struct lpfc_wcqe_complete *)&wcqe);
12454 break;
12455 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012456 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012457 /* Process the WQ release event */
12458 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12459 (struct lpfc_wcqe_release *)&wcqe);
12460 break;
12461 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012462 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012463 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012464 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012465 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12466 (struct sli4_wcqe_xri_aborted *)&wcqe);
12467 break;
12468 default:
12469 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12470 "0144 Not a valid WCQE code: x%x\n",
12471 bf_get(lpfc_wcqe_c_code, &wcqe));
12472 break;
12473 }
12474 return workposted;
12475}
12476
12477/**
James Smart67d12732012-08-03 12:36:13 -040012478 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012479 * @phba: Pointer to HBA context object.
12480 * @eqe: Pointer to fast-path event queue entry.
12481 *
12482 * This routine process a event queue entry from the fast-path event queue.
12483 * It will check the MajorCode and MinorCode to determine this is for a
12484 * completion event on a completion queue, if not, an error shall be logged
12485 * and just return. Otherwise, it will get to the corresponding completion
12486 * queue and process all the entries on the completion queue, rearm the
12487 * completion queue, and then return.
12488 **/
12489static void
James Smart67d12732012-08-03 12:36:13 -040012490lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12491 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012492{
12493 struct lpfc_queue *cq;
12494 struct lpfc_cqe *cqe;
12495 bool workposted = false;
12496 uint16_t cqid;
12497 int ecount = 0;
12498
James Smartcb5172e2010-03-15 11:25:07 -040012499 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012500 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012501 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012502 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012503 bf_get_le32(lpfc_eqe_major_code, eqe),
12504 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012505 return;
12506 }
12507
James Smart67d12732012-08-03 12:36:13 -040012508 /* Get the reference to the corresponding CQ */
12509 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12510
12511 /* Check if this is a Slow path event */
12512 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12513 lpfc_sli4_sp_handle_eqe(phba, eqe,
12514 phba->sli4_hba.hba_eq[qidx]);
12515 return;
12516 }
12517
James Smart2e90f4b2011-12-13 13:22:37 -050012518 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12519 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12520 "3146 Fast-path completion queues "
12521 "does not exist\n");
12522 return;
12523 }
James Smart67d12732012-08-03 12:36:13 -040012524 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012525 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012526 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12527 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12528 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012529 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012530 return;
12531 }
12532
James Smart4f774512009-05-22 14:52:35 -040012533 if (unlikely(cqid != cq->queue_id)) {
12534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12535 "0368 Miss-matched fast-path completion "
12536 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12537 cqid, cq->queue_id);
12538 return;
12539 }
12540
12541 /* Process all the entries to the CQ */
12542 while ((cqe = lpfc_sli4_cq_get(cq))) {
12543 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012544 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012545 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12546 }
12547
James Smartb84daac2012-08-03 12:35:13 -040012548 /* Track the max number of CQEs processed in 1 EQ */
12549 if (ecount > cq->CQ_max_cqe)
12550 cq->CQ_max_cqe = ecount;
12551
James Smart4f774512009-05-22 14:52:35 -040012552 /* Catch the no cq entry condition */
12553 if (unlikely(ecount == 0))
12554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12555 "0369 No entry from fast-path completion "
12556 "queue fcpcqid=%d\n", cq->queue_id);
12557
12558 /* In any case, flash and re-arm the CQ */
12559 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12560
12561 /* wake up worker thread if there are works to be done */
12562 if (workposted)
12563 lpfc_worker_wake_up(phba);
12564}
12565
12566static void
12567lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12568{
12569 struct lpfc_eqe *eqe;
12570
12571 /* walk all the EQ entries and drop on the floor */
12572 while ((eqe = lpfc_sli4_eq_get(eq)))
12573 ;
12574
12575 /* Clear and re-arm the EQ */
12576 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12577}
12578
James Smart1ba981f2014-02-20 09:56:45 -050012579
12580/**
12581 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12582 * entry
12583 * @phba: Pointer to HBA context object.
12584 * @eqe: Pointer to fast-path event queue entry.
12585 *
12586 * This routine process a event queue entry from the Flash Optimized Fabric
12587 * event queue. It will check the MajorCode and MinorCode to determine this
12588 * is for a completion event on a completion queue, if not, an error shall be
12589 * logged and just return. Otherwise, it will get to the corresponding
12590 * completion queue and process all the entries on the completion queue, rearm
12591 * the completion queue, and then return.
12592 **/
12593static void
12594lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12595{
12596 struct lpfc_queue *cq;
12597 struct lpfc_cqe *cqe;
12598 bool workposted = false;
12599 uint16_t cqid;
12600 int ecount = 0;
12601
12602 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12604 "9147 Not a valid completion "
12605 "event: majorcode=x%x, minorcode=x%x\n",
12606 bf_get_le32(lpfc_eqe_major_code, eqe),
12607 bf_get_le32(lpfc_eqe_minor_code, eqe));
12608 return;
12609 }
12610
12611 /* Get the reference to the corresponding CQ */
12612 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12613
12614 /* Next check for OAS */
12615 cq = phba->sli4_hba.oas_cq;
12616 if (unlikely(!cq)) {
12617 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12618 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12619 "9148 OAS completion queue "
12620 "does not exist\n");
12621 return;
12622 }
12623
12624 if (unlikely(cqid != cq->queue_id)) {
12625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12626 "9149 Miss-matched fast-path compl "
12627 "queue id: eqcqid=%d, fcpcqid=%d\n",
12628 cqid, cq->queue_id);
12629 return;
12630 }
12631
12632 /* Process all the entries to the OAS CQ */
12633 while ((cqe = lpfc_sli4_cq_get(cq))) {
12634 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12635 if (!(++ecount % cq->entry_repost))
12636 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12637 }
12638
12639 /* Track the max number of CQEs processed in 1 EQ */
12640 if (ecount > cq->CQ_max_cqe)
12641 cq->CQ_max_cqe = ecount;
12642
12643 /* Catch the no cq entry condition */
12644 if (unlikely(ecount == 0))
12645 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12646 "9153 No entry from fast-path completion "
12647 "queue fcpcqid=%d\n", cq->queue_id);
12648
12649 /* In any case, flash and re-arm the CQ */
12650 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12651
12652 /* wake up worker thread if there are works to be done */
12653 if (workposted)
12654 lpfc_worker_wake_up(phba);
12655}
12656
12657/**
12658 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12659 * @irq: Interrupt number.
12660 * @dev_id: The device context pointer.
12661 *
12662 * This function is directly called from the PCI layer as an interrupt
12663 * service routine when device with SLI-4 interface spec is enabled with
12664 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12665 * IOCB ring event in the HBA. However, when the device is enabled with either
12666 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12667 * device-level interrupt handler. When the PCI slot is in error recovery
12668 * or the HBA is undergoing initialization, the interrupt handler will not
12669 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12670 * the intrrupt context. This function is called without any lock held.
12671 * It gets the hbalock to access and update SLI data structures. Note that,
12672 * the EQ to CQ are one-to-one map such that the EQ index is
12673 * equal to that of CQ index.
12674 *
12675 * This function returns IRQ_HANDLED when interrupt is handled else it
12676 * returns IRQ_NONE.
12677 **/
12678irqreturn_t
12679lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12680{
12681 struct lpfc_hba *phba;
12682 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12683 struct lpfc_queue *eq;
12684 struct lpfc_eqe *eqe;
12685 unsigned long iflag;
12686 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012687
12688 /* Get the driver's phba structure from the dev_id */
12689 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12690 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012691
12692 if (unlikely(!phba))
12693 return IRQ_NONE;
12694
12695 /* Get to the EQ struct associated with this vector */
12696 eq = phba->sli4_hba.fof_eq;
12697 if (unlikely(!eq))
12698 return IRQ_NONE;
12699
12700 /* Check device state for handling interrupt */
12701 if (unlikely(lpfc_intr_state_check(phba))) {
12702 eq->EQ_badstate++;
12703 /* Check again for link_state with lock held */
12704 spin_lock_irqsave(&phba->hbalock, iflag);
12705 if (phba->link_state < LPFC_LINK_DOWN)
12706 /* Flush, clear interrupt, and rearm the EQ */
12707 lpfc_sli4_eq_flush(phba, eq);
12708 spin_unlock_irqrestore(&phba->hbalock, iflag);
12709 return IRQ_NONE;
12710 }
12711
12712 /*
12713 * Process all the event on FCP fast-path EQ
12714 */
12715 while ((eqe = lpfc_sli4_eq_get(eq))) {
12716 lpfc_sli4_fof_handle_eqe(phba, eqe);
12717 if (!(++ecount % eq->entry_repost))
12718 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12719 eq->EQ_processed++;
12720 }
12721
12722 /* Track the max number of EQEs processed in 1 intr */
12723 if (ecount > eq->EQ_max_eqe)
12724 eq->EQ_max_eqe = ecount;
12725
12726
12727 if (unlikely(ecount == 0)) {
12728 eq->EQ_no_entry++;
12729
12730 if (phba->intr_type == MSIX)
12731 /* MSI-X treated interrupt served as no EQ share INT */
12732 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12733 "9145 MSI-X interrupt with no EQE\n");
12734 else {
12735 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12736 "9146 ISR interrupt with no EQE\n");
12737 /* Non MSI-X treated on interrupt as EQ share INT */
12738 return IRQ_NONE;
12739 }
12740 }
12741 /* Always clear and re-arm the fast-path EQ */
12742 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12743 return IRQ_HANDLED;
12744}
12745
James Smart4f774512009-05-22 14:52:35 -040012746/**
James Smart67d12732012-08-03 12:36:13 -040012747 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012748 * @irq: Interrupt number.
12749 * @dev_id: The device context pointer.
12750 *
12751 * This function is directly called from the PCI layer as an interrupt
12752 * service routine when device with SLI-4 interface spec is enabled with
12753 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12754 * ring event in the HBA. However, when the device is enabled with either
12755 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12756 * device-level interrupt handler. When the PCI slot is in error recovery
12757 * or the HBA is undergoing initialization, the interrupt handler will not
12758 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12759 * the intrrupt context. This function is called without any lock held.
12760 * It gets the hbalock to access and update SLI data structures. Note that,
12761 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12762 * equal to that of FCP CQ index.
12763 *
James Smart67d12732012-08-03 12:36:13 -040012764 * The link attention and ELS ring attention events are handled
12765 * by the worker thread. The interrupt handler signals the worker thread
12766 * and returns for these events. This function is called without any lock
12767 * held. It gets the hbalock to access and update SLI data structures.
12768 *
James Smart4f774512009-05-22 14:52:35 -040012769 * This function returns IRQ_HANDLED when interrupt is handled else it
12770 * returns IRQ_NONE.
12771 **/
12772irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012773lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012774{
12775 struct lpfc_hba *phba;
12776 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12777 struct lpfc_queue *fpeq;
12778 struct lpfc_eqe *eqe;
12779 unsigned long iflag;
12780 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012781 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012782
12783 /* Get the driver's phba structure from the dev_id */
12784 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12785 phba = fcp_eq_hdl->phba;
12786 fcp_eqidx = fcp_eq_hdl->idx;
12787
12788 if (unlikely(!phba))
12789 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012790 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012791 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012792
12793 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012794 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012795 if (unlikely(!fpeq))
12796 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012797
James Smartba20c852012-08-03 12:36:52 -040012798 if (lpfc_fcp_look_ahead) {
12799 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12800 lpfc_sli4_eq_clr_intr(fpeq);
12801 else {
12802 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12803 return IRQ_NONE;
12804 }
12805 }
12806
James Smart4f774512009-05-22 14:52:35 -040012807 /* Check device state for handling interrupt */
12808 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012809 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012810 /* Check again for link_state with lock held */
12811 spin_lock_irqsave(&phba->hbalock, iflag);
12812 if (phba->link_state < LPFC_LINK_DOWN)
12813 /* Flush, clear interrupt, and rearm the EQ */
12814 lpfc_sli4_eq_flush(phba, fpeq);
12815 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012816 if (lpfc_fcp_look_ahead)
12817 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012818 return IRQ_NONE;
12819 }
12820
12821 /*
12822 * Process all the event on FCP fast-path EQ
12823 */
12824 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012825 if (eqe == NULL)
12826 break;
12827
James Smart67d12732012-08-03 12:36:13 -040012828 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012829 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012830 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012831 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012832 }
12833
James Smartb84daac2012-08-03 12:35:13 -040012834 /* Track the max number of EQEs processed in 1 intr */
12835 if (ecount > fpeq->EQ_max_eqe)
12836 fpeq->EQ_max_eqe = ecount;
12837
James Smart4f774512009-05-22 14:52:35 -040012838 /* Always clear and re-arm the fast-path EQ */
12839 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12840
12841 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012842 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012843
12844 if (lpfc_fcp_look_ahead) {
12845 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12846 return IRQ_NONE;
12847 }
12848
James Smart4f774512009-05-22 14:52:35 -040012849 if (phba->intr_type == MSIX)
12850 /* MSI-X treated interrupt served as no EQ share INT */
12851 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12852 "0358 MSI-X interrupt with no EQE\n");
12853 else
12854 /* Non MSI-X treated on interrupt as EQ share INT */
12855 return IRQ_NONE;
12856 }
12857
James Smartba20c852012-08-03 12:36:52 -040012858 if (lpfc_fcp_look_ahead)
12859 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012860 return IRQ_HANDLED;
12861} /* lpfc_sli4_fp_intr_handler */
12862
12863/**
12864 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12865 * @irq: Interrupt number.
12866 * @dev_id: The device context pointer.
12867 *
12868 * This function is the device-level interrupt handler to device with SLI-4
12869 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12870 * interrupt mode is enabled and there is an event in the HBA which requires
12871 * driver attention. This function invokes the slow-path interrupt attention
12872 * handling function and fast-path interrupt attention handling function in
12873 * turn to process the relevant HBA attention events. This function is called
12874 * without any lock held. It gets the hbalock to access and update SLI data
12875 * structures.
12876 *
12877 * This function returns IRQ_HANDLED when interrupt is handled, else it
12878 * returns IRQ_NONE.
12879 **/
12880irqreturn_t
12881lpfc_sli4_intr_handler(int irq, void *dev_id)
12882{
12883 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012884 irqreturn_t hba_irq_rc;
12885 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012886 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012887
12888 /* Get the driver's phba structure from the dev_id */
12889 phba = (struct lpfc_hba *)dev_id;
12890
12891 if (unlikely(!phba))
12892 return IRQ_NONE;
12893
12894 /*
James Smart4f774512009-05-22 14:52:35 -040012895 * Invoke fast-path host attention interrupt handling as appropriate.
12896 */
James Smart67d12732012-08-03 12:36:13 -040012897 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12898 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012899 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012900 if (hba_irq_rc == IRQ_HANDLED)
12901 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012902 }
12903
James Smart1ba981f2014-02-20 09:56:45 -050012904 if (phba->cfg_fof) {
12905 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12906 &phba->sli4_hba.fcp_eq_hdl[0]);
12907 if (hba_irq_rc == IRQ_HANDLED)
12908 hba_handled |= true;
12909 }
12910
James Smart67d12732012-08-03 12:36:13 -040012911 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012912} /* lpfc_sli4_intr_handler */
12913
12914/**
12915 * lpfc_sli4_queue_free - free a queue structure and associated memory
12916 * @queue: The queue structure to free.
12917 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012918 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012919 * the host resident queue. This function must be called after destroying the
12920 * queue on the HBA.
12921 **/
12922void
12923lpfc_sli4_queue_free(struct lpfc_queue *queue)
12924{
12925 struct lpfc_dmabuf *dmabuf;
12926
12927 if (!queue)
12928 return;
12929
12930 while (!list_empty(&queue->page_list)) {
12931 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12932 list);
James Smart49198b32010-04-06 15:04:33 -040012933 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012934 dmabuf->virt, dmabuf->phys);
12935 kfree(dmabuf);
12936 }
12937 kfree(queue);
12938 return;
12939}
12940
12941/**
12942 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12943 * @phba: The HBA that this queue is being created on.
12944 * @entry_size: The size of each queue entry for this queue.
12945 * @entry count: The number of entries that this queue will handle.
12946 *
12947 * This function allocates a queue structure and the DMAable memory used for
12948 * the host resident queue. This function must be called before creating the
12949 * queue on the HBA.
12950 **/
12951struct lpfc_queue *
12952lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12953 uint32_t entry_count)
12954{
12955 struct lpfc_queue *queue;
12956 struct lpfc_dmabuf *dmabuf;
12957 int x, total_qe_count;
12958 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012959 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012960
James Smartcb5172e2010-03-15 11:25:07 -040012961 if (!phba->sli4_hba.pc_sli4_params.supported)
12962 hw_page_size = SLI4_PAGE_SIZE;
12963
James Smart4f774512009-05-22 14:52:35 -040012964 queue = kzalloc(sizeof(struct lpfc_queue) +
12965 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12966 if (!queue)
12967 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012968 queue->page_count = (ALIGN(entry_size * entry_count,
12969 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012970 INIT_LIST_HEAD(&queue->list);
12971 INIT_LIST_HEAD(&queue->page_list);
12972 INIT_LIST_HEAD(&queue->child_list);
12973 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12974 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12975 if (!dmabuf)
12976 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040012977 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
12978 hw_page_size, &dmabuf->phys,
12979 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040012980 if (!dmabuf->virt) {
12981 kfree(dmabuf);
12982 goto out_fail;
12983 }
12984 dmabuf->buffer_tag = x;
12985 list_add_tail(&dmabuf->list, &queue->page_list);
12986 /* initialize queue's entry array */
12987 dma_pointer = dmabuf->virt;
12988 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012989 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012990 total_qe_count++, dma_pointer += entry_size) {
12991 queue->qe[total_qe_count].address = dma_pointer;
12992 }
12993 }
12994 queue->entry_size = entry_size;
12995 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012996
12997 /*
12998 * entry_repost is calculated based on the number of entries in the
12999 * queue. This works out except for RQs. If buffers are NOT initially
13000 * posted for every RQE, entry_repost should be adjusted accordingly.
13001 */
13002 queue->entry_repost = (entry_count >> 3);
13003 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13004 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013005 queue->phba = phba;
13006
13007 return queue;
13008out_fail:
13009 lpfc_sli4_queue_free(queue);
13010 return NULL;
13011}
13012
13013/**
James Smart962bc512013-01-03 15:44:00 -050013014 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13015 * @phba: HBA structure that indicates port to create a queue on.
13016 * @pci_barset: PCI BAR set flag.
13017 *
13018 * This function shall perform iomap of the specified PCI BAR address to host
13019 * memory address if not already done so and return it. The returned host
13020 * memory address can be NULL.
13021 */
13022static void __iomem *
13023lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13024{
James Smart962bc512013-01-03 15:44:00 -050013025 if (!phba->pcidev)
13026 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013027
13028 switch (pci_barset) {
13029 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013030 return phba->pci_bar0_memmap_p;
13031 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013032 return phba->pci_bar2_memmap_p;
13033 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013034 return phba->pci_bar4_memmap_p;
13035 default:
13036 break;
13037 }
13038 return NULL;
13039}
13040
13041/**
James Smart173edbb2012-06-12 13:54:50 -040013042 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13043 * @phba: HBA structure that indicates port to create a queue on.
13044 * @startq: The starting FCP EQ to modify
13045 *
13046 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13047 *
13048 * The @phba struct is used to send mailbox command to HBA. The @startq
13049 * is used to get the starting FCP EQ to change.
13050 * This function is asynchronous and will wait for the mailbox
13051 * command to finish before continuing.
13052 *
13053 * On success this function will return a zero. If unable to allocate enough
13054 * memory this function will return -ENOMEM. If the queue create mailbox command
13055 * fails this function will return -ENXIO.
13056 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013057int
James Smart2c9c5a02015-04-07 15:07:15 -040013058lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013059{
13060 struct lpfc_mbx_modify_eq_delay *eq_delay;
13061 LPFC_MBOXQ_t *mbox;
13062 struct lpfc_queue *eq;
13063 int cnt, rc, length, status = 0;
13064 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013065 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013066 int fcp_eqidx;
13067 union lpfc_sli4_cfg_shdr *shdr;
13068 uint16_t dmult;
13069
James Smart67d12732012-08-03 12:36:13 -040013070 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013071 return 0;
13072
13073 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13074 if (!mbox)
13075 return -ENOMEM;
13076 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13077 sizeof(struct lpfc_sli4_cfg_mhdr));
13078 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13079 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13080 length, LPFC_SLI4_MBX_EMBED);
13081 eq_delay = &mbox->u.mqe.un.eq_delay;
13082
13083 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013084 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13085 if (result > LPFC_DMULT_CONST)
13086 dmult = 0;
13087 else
13088 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013089
13090 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013091 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013092 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013093 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013094 if (!eq)
13095 continue;
13096 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13097 eq_delay->u.request.eq[cnt].phase = 0;
13098 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13099 cnt++;
13100 if (cnt >= LPFC_MAX_EQ_DELAY)
13101 break;
13102 }
13103 eq_delay->u.request.num_eq = cnt;
13104
13105 mbox->vport = phba->pport;
13106 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13107 mbox->context1 = NULL;
13108 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13109 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13110 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13111 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13112 if (shdr_status || shdr_add_status || rc) {
13113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13114 "2512 MODIFY_EQ_DELAY mailbox failed with "
13115 "status x%x add_status x%x, mbx status x%x\n",
13116 shdr_status, shdr_add_status, rc);
13117 status = -ENXIO;
13118 }
13119 mempool_free(mbox, phba->mbox_mem_pool);
13120 return status;
13121}
13122
13123/**
James Smart4f774512009-05-22 14:52:35 -040013124 * lpfc_eq_create - Create an Event Queue on the HBA
13125 * @phba: HBA structure that indicates port to create a queue on.
13126 * @eq: The queue structure to use to create the event queue.
13127 * @imax: The maximum interrupt per second limit.
13128 *
13129 * This function creates an event queue, as detailed in @eq, on a port,
13130 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13131 *
13132 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13133 * is used to get the entry count and entry size that are necessary to
13134 * determine the number of pages to allocate and use for this queue. This
13135 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13136 * event queue. This function is asynchronous and will wait for the mailbox
13137 * command to finish before continuing.
13138 *
13139 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013140 * memory this function will return -ENOMEM. If the queue create mailbox command
13141 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013142 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013143int
James Smartee020062012-09-29 11:28:52 -040013144lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013145{
13146 struct lpfc_mbx_eq_create *eq_create;
13147 LPFC_MBOXQ_t *mbox;
13148 int rc, length, status = 0;
13149 struct lpfc_dmabuf *dmabuf;
13150 uint32_t shdr_status, shdr_add_status;
13151 union lpfc_sli4_cfg_shdr *shdr;
13152 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013153 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13154
James Smart2e90f4b2011-12-13 13:22:37 -050013155 /* sanity check on queue memory */
13156 if (!eq)
13157 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013158 if (!phba->sli4_hba.pc_sli4_params.supported)
13159 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013160
13161 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13162 if (!mbox)
13163 return -ENOMEM;
13164 length = (sizeof(struct lpfc_mbx_eq_create) -
13165 sizeof(struct lpfc_sli4_cfg_mhdr));
13166 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13167 LPFC_MBOX_OPCODE_EQ_CREATE,
13168 length, LPFC_SLI4_MBX_EMBED);
13169 eq_create = &mbox->u.mqe.un.eq_create;
13170 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13171 eq->page_count);
13172 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13173 LPFC_EQE_SIZE);
13174 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013175 /* don't setup delay multiplier using EQ_CREATE */
13176 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013177 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13178 dmult);
13179 switch (eq->entry_count) {
13180 default:
13181 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13182 "0360 Unsupported EQ count. (%d)\n",
13183 eq->entry_count);
13184 if (eq->entry_count < 256)
13185 return -EINVAL;
13186 /* otherwise default to smallest count (drop through) */
13187 case 256:
13188 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13189 LPFC_EQ_CNT_256);
13190 break;
13191 case 512:
13192 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13193 LPFC_EQ_CNT_512);
13194 break;
13195 case 1024:
13196 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13197 LPFC_EQ_CNT_1024);
13198 break;
13199 case 2048:
13200 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13201 LPFC_EQ_CNT_2048);
13202 break;
13203 case 4096:
13204 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13205 LPFC_EQ_CNT_4096);
13206 break;
13207 }
13208 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013209 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013210 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13211 putPaddrLow(dmabuf->phys);
13212 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13213 putPaddrHigh(dmabuf->phys);
13214 }
13215 mbox->vport = phba->pport;
13216 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13217 mbox->context1 = NULL;
13218 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13219 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13220 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13221 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13222 if (shdr_status || shdr_add_status || rc) {
13223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13224 "2500 EQ_CREATE mailbox failed with "
13225 "status x%x add_status x%x, mbx status x%x\n",
13226 shdr_status, shdr_add_status, rc);
13227 status = -ENXIO;
13228 }
13229 eq->type = LPFC_EQ;
13230 eq->subtype = LPFC_NONE;
13231 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13232 if (eq->queue_id == 0xFFFF)
13233 status = -ENXIO;
13234 eq->host_index = 0;
13235 eq->hba_index = 0;
13236
James Smart8fa38512009-07-19 10:01:03 -040013237 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013238 return status;
13239}
13240
13241/**
13242 * lpfc_cq_create - Create a Completion Queue on the HBA
13243 * @phba: HBA structure that indicates port to create a queue on.
13244 * @cq: The queue structure to use to create the completion queue.
13245 * @eq: The event queue to bind this completion queue to.
13246 *
13247 * This function creates a completion queue, as detailed in @wq, on a port,
13248 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13249 *
13250 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13251 * is used to get the entry count and entry size that are necessary to
13252 * determine the number of pages to allocate and use for this queue. The @eq
13253 * is used to indicate which event queue to bind this completion queue to. This
13254 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13255 * completion queue. This function is asynchronous and will wait for the mailbox
13256 * command to finish before continuing.
13257 *
13258 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013259 * memory this function will return -ENOMEM. If the queue create mailbox command
13260 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013261 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013262int
James Smart4f774512009-05-22 14:52:35 -040013263lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13264 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13265{
13266 struct lpfc_mbx_cq_create *cq_create;
13267 struct lpfc_dmabuf *dmabuf;
13268 LPFC_MBOXQ_t *mbox;
13269 int rc, length, status = 0;
13270 uint32_t shdr_status, shdr_add_status;
13271 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013272 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13273
James Smart2e90f4b2011-12-13 13:22:37 -050013274 /* sanity check on queue memory */
13275 if (!cq || !eq)
13276 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013277 if (!phba->sli4_hba.pc_sli4_params.supported)
13278 hw_page_size = SLI4_PAGE_SIZE;
13279
James Smart4f774512009-05-22 14:52:35 -040013280 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13281 if (!mbox)
13282 return -ENOMEM;
13283 length = (sizeof(struct lpfc_mbx_cq_create) -
13284 sizeof(struct lpfc_sli4_cfg_mhdr));
13285 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13286 LPFC_MBOX_OPCODE_CQ_CREATE,
13287 length, LPFC_SLI4_MBX_EMBED);
13288 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013289 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013290 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13291 cq->page_count);
13292 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13293 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013294 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13295 phba->sli4_hba.pc_sli4_params.cqv);
13296 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013297 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13298 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013299 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13300 eq->queue_id);
13301 } else {
13302 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13303 eq->queue_id);
13304 }
James Smart4f774512009-05-22 14:52:35 -040013305 switch (cq->entry_count) {
13306 default:
13307 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13308 "0361 Unsupported CQ count. (%d)\n",
13309 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013310 if (cq->entry_count < 256) {
13311 status = -EINVAL;
13312 goto out;
13313 }
James Smart4f774512009-05-22 14:52:35 -040013314 /* otherwise default to smallest count (drop through) */
13315 case 256:
13316 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13317 LPFC_CQ_CNT_256);
13318 break;
13319 case 512:
13320 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13321 LPFC_CQ_CNT_512);
13322 break;
13323 case 1024:
13324 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13325 LPFC_CQ_CNT_1024);
13326 break;
13327 }
13328 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013329 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013330 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13331 putPaddrLow(dmabuf->phys);
13332 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13333 putPaddrHigh(dmabuf->phys);
13334 }
13335 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13336
13337 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013338 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13339 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13340 if (shdr_status || shdr_add_status || rc) {
13341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13342 "2501 CQ_CREATE mailbox failed with "
13343 "status x%x add_status x%x, mbx status x%x\n",
13344 shdr_status, shdr_add_status, rc);
13345 status = -ENXIO;
13346 goto out;
13347 }
13348 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13349 if (cq->queue_id == 0xFFFF) {
13350 status = -ENXIO;
13351 goto out;
13352 }
13353 /* link the cq onto the parent eq child list */
13354 list_add_tail(&cq->list, &eq->child_list);
13355 /* Set up completion queue's type and subtype */
13356 cq->type = type;
13357 cq->subtype = subtype;
13358 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013359 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013360 cq->host_index = 0;
13361 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013362
James Smart8fa38512009-07-19 10:01:03 -040013363out:
13364 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013365 return status;
13366}
13367
13368/**
James Smartb19a0612010-04-06 14:48:51 -040013369 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013370 * @phba: HBA structure that indicates port to create a queue on.
13371 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013372 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13373 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013374 *
James Smartb19a0612010-04-06 14:48:51 -040013375 * This function provides failback (fb) functionality when the
13376 * mq_create_ext fails on older FW generations. It's purpose is identical
13377 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013378 *
James Smartb19a0612010-04-06 14:48:51 -040013379 * This routine cannot fail as all attributes were previously accessed and
13380 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013381 **/
James Smartb19a0612010-04-06 14:48:51 -040013382static void
13383lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13384 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013385{
13386 struct lpfc_mbx_mq_create *mq_create;
13387 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013388 int length;
James Smart04c68492009-05-22 14:52:52 -040013389
James Smart04c68492009-05-22 14:52:52 -040013390 length = (sizeof(struct lpfc_mbx_mq_create) -
13391 sizeof(struct lpfc_sli4_cfg_mhdr));
13392 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13393 LPFC_MBOX_OPCODE_MQ_CREATE,
13394 length, LPFC_SLI4_MBX_EMBED);
13395 mq_create = &mbox->u.mqe.un.mq_create;
13396 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013397 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013398 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013399 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013400 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13401 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013402 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013403 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13404 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013405 break;
13406 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013407 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13408 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013409 break;
13410 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013411 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13412 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013413 break;
13414 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013415 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13416 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013417 break;
13418 }
13419 list_for_each_entry(dmabuf, &mq->page_list, list) {
13420 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013421 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013422 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013423 putPaddrHigh(dmabuf->phys);
13424 }
13425}
13426
13427/**
13428 * lpfc_mq_create - Create a mailbox Queue on the HBA
13429 * @phba: HBA structure that indicates port to create a queue on.
13430 * @mq: The queue structure to use to create the mailbox queue.
13431 * @cq: The completion queue to associate with this cq.
13432 * @subtype: The queue's subtype.
13433 *
13434 * This function creates a mailbox queue, as detailed in @mq, on a port,
13435 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13436 *
13437 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13438 * is used to get the entry count and entry size that are necessary to
13439 * determine the number of pages to allocate and use for this queue. This
13440 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13441 * mailbox queue. This function is asynchronous and will wait for the mailbox
13442 * command to finish before continuing.
13443 *
13444 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013445 * memory this function will return -ENOMEM. If the queue create mailbox command
13446 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013447 **/
13448int32_t
13449lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13450 struct lpfc_queue *cq, uint32_t subtype)
13451{
13452 struct lpfc_mbx_mq_create *mq_create;
13453 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13454 struct lpfc_dmabuf *dmabuf;
13455 LPFC_MBOXQ_t *mbox;
13456 int rc, length, status = 0;
13457 uint32_t shdr_status, shdr_add_status;
13458 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013459 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013460
James Smart2e90f4b2011-12-13 13:22:37 -050013461 /* sanity check on queue memory */
13462 if (!mq || !cq)
13463 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013464 if (!phba->sli4_hba.pc_sli4_params.supported)
13465 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013466
13467 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13468 if (!mbox)
13469 return -ENOMEM;
13470 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13471 sizeof(struct lpfc_sli4_cfg_mhdr));
13472 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13473 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13474 length, LPFC_SLI4_MBX_EMBED);
13475
13476 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013477 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013478 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13479 &mq_create_ext->u.request, mq->page_count);
13480 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13481 &mq_create_ext->u.request, 1);
13482 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013483 &mq_create_ext->u.request, 1);
13484 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13485 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013486 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13487 &mq_create_ext->u.request, 1);
13488 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13489 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013490 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013491 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13492 phba->sli4_hba.pc_sli4_params.mqv);
13493 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13494 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13495 cq->queue_id);
13496 else
13497 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13498 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013499 switch (mq->entry_count) {
13500 default:
13501 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13502 "0362 Unsupported MQ count. (%d)\n",
13503 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013504 if (mq->entry_count < 16) {
13505 status = -EINVAL;
13506 goto out;
13507 }
James Smartb19a0612010-04-06 14:48:51 -040013508 /* otherwise default to smallest count (drop through) */
13509 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013510 bf_set(lpfc_mq_context_ring_size,
13511 &mq_create_ext->u.request.context,
13512 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013513 break;
13514 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013515 bf_set(lpfc_mq_context_ring_size,
13516 &mq_create_ext->u.request.context,
13517 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013518 break;
13519 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013520 bf_set(lpfc_mq_context_ring_size,
13521 &mq_create_ext->u.request.context,
13522 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013523 break;
13524 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013525 bf_set(lpfc_mq_context_ring_size,
13526 &mq_create_ext->u.request.context,
13527 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013528 break;
13529 }
13530 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013531 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013532 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13533 putPaddrLow(dmabuf->phys);
13534 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013535 putPaddrHigh(dmabuf->phys);
13536 }
13537 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013538 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13539 &mq_create_ext->u.response);
13540 if (rc != MBX_SUCCESS) {
13541 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13542 "2795 MQ_CREATE_EXT failed with "
13543 "status x%x. Failback to MQ_CREATE.\n",
13544 rc);
13545 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13546 mq_create = &mbox->u.mqe.un.mq_create;
13547 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13548 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13549 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13550 &mq_create->u.response);
13551 }
13552
James Smart04c68492009-05-22 14:52:52 -040013553 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013554 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13555 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13556 if (shdr_status || shdr_add_status || rc) {
13557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13558 "2502 MQ_CREATE mailbox failed with "
13559 "status x%x add_status x%x, mbx status x%x\n",
13560 shdr_status, shdr_add_status, rc);
13561 status = -ENXIO;
13562 goto out;
13563 }
James Smart04c68492009-05-22 14:52:52 -040013564 if (mq->queue_id == 0xFFFF) {
13565 status = -ENXIO;
13566 goto out;
13567 }
13568 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013569 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013570 mq->subtype = subtype;
13571 mq->host_index = 0;
13572 mq->hba_index = 0;
13573
13574 /* link the mq onto the parent cq child list */
13575 list_add_tail(&mq->list, &cq->child_list);
13576out:
James Smart8fa38512009-07-19 10:01:03 -040013577 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013578 return status;
13579}
13580
13581/**
James Smart4f774512009-05-22 14:52:35 -040013582 * lpfc_wq_create - Create a Work Queue on the HBA
13583 * @phba: HBA structure that indicates port to create a queue on.
13584 * @wq: The queue structure to use to create the work queue.
13585 * @cq: The completion queue to bind this work queue to.
13586 * @subtype: The subtype of the work queue indicating its functionality.
13587 *
13588 * This function creates a work queue, as detailed in @wq, on a port, described
13589 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13590 *
13591 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13592 * is used to get the entry count and entry size that are necessary to
13593 * determine the number of pages to allocate and use for this queue. The @cq
13594 * is used to indicate which completion queue to bind this work queue to. This
13595 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13596 * work queue. This function is asynchronous and will wait for the mailbox
13597 * command to finish before continuing.
13598 *
13599 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013600 * memory this function will return -ENOMEM. If the queue create mailbox command
13601 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013602 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013603int
James Smart4f774512009-05-22 14:52:35 -040013604lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13605 struct lpfc_queue *cq, uint32_t subtype)
13606{
13607 struct lpfc_mbx_wq_create *wq_create;
13608 struct lpfc_dmabuf *dmabuf;
13609 LPFC_MBOXQ_t *mbox;
13610 int rc, length, status = 0;
13611 uint32_t shdr_status, shdr_add_status;
13612 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013613 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013614 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013615 void __iomem *bar_memmap_p;
13616 uint32_t db_offset;
13617 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013618
James Smart2e90f4b2011-12-13 13:22:37 -050013619 /* sanity check on queue memory */
13620 if (!wq || !cq)
13621 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013622 if (!phba->sli4_hba.pc_sli4_params.supported)
13623 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013624
13625 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13626 if (!mbox)
13627 return -ENOMEM;
13628 length = (sizeof(struct lpfc_mbx_wq_create) -
13629 sizeof(struct lpfc_sli4_cfg_mhdr));
13630 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13631 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13632 length, LPFC_SLI4_MBX_EMBED);
13633 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013634 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013635 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13636 wq->page_count);
13637 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13638 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013639
13640 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013641 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13642 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013643
James Smart0c651872013-07-15 18:33:23 -040013644 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13645 case LPFC_Q_CREATE_VERSION_0:
13646 switch (wq->entry_size) {
13647 default:
13648 case 64:
13649 /* Nothing to do, version 0 ONLY supports 64 byte */
13650 page = wq_create->u.request.page;
13651 break;
13652 case 128:
13653 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13654 LPFC_WQ_SZ128_SUPPORT)) {
13655 status = -ERANGE;
13656 goto out;
13657 }
13658 /* If we get here the HBA MUST also support V1 and
13659 * we MUST use it
13660 */
13661 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13662 LPFC_Q_CREATE_VERSION_1);
13663
13664 bf_set(lpfc_mbx_wq_create_wqe_count,
13665 &wq_create->u.request_1, wq->entry_count);
13666 bf_set(lpfc_mbx_wq_create_wqe_size,
13667 &wq_create->u.request_1,
13668 LPFC_WQ_WQE_SIZE_128);
13669 bf_set(lpfc_mbx_wq_create_page_size,
13670 &wq_create->u.request_1,
13671 (PAGE_SIZE/SLI4_PAGE_SIZE));
13672 page = wq_create->u.request_1.page;
13673 break;
13674 }
13675 break;
13676 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013677 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13678 wq->entry_count);
13679 switch (wq->entry_size) {
13680 default:
13681 case 64:
13682 bf_set(lpfc_mbx_wq_create_wqe_size,
13683 &wq_create->u.request_1,
13684 LPFC_WQ_WQE_SIZE_64);
13685 break;
13686 case 128:
James Smart0c651872013-07-15 18:33:23 -040013687 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13688 LPFC_WQ_SZ128_SUPPORT)) {
13689 status = -ERANGE;
13690 goto out;
13691 }
James Smart5a6f1332011-03-11 16:05:35 -050013692 bf_set(lpfc_mbx_wq_create_wqe_size,
13693 &wq_create->u.request_1,
13694 LPFC_WQ_WQE_SIZE_128);
13695 break;
13696 }
13697 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13698 (PAGE_SIZE/SLI4_PAGE_SIZE));
13699 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013700 break;
13701 default:
13702 status = -ERANGE;
13703 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013704 }
James Smart0c651872013-07-15 18:33:23 -040013705
James Smart4f774512009-05-22 14:52:35 -040013706 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013707 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013708 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13709 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013710 }
James Smart962bc512013-01-03 15:44:00 -050013711
13712 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13713 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13714
James Smart4f774512009-05-22 14:52:35 -040013715 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13716 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013717 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13718 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13719 if (shdr_status || shdr_add_status || rc) {
13720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13721 "2503 WQ_CREATE mailbox failed with "
13722 "status x%x add_status x%x, mbx status x%x\n",
13723 shdr_status, shdr_add_status, rc);
13724 status = -ENXIO;
13725 goto out;
13726 }
13727 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13728 if (wq->queue_id == 0xFFFF) {
13729 status = -ENXIO;
13730 goto out;
13731 }
James Smart962bc512013-01-03 15:44:00 -050013732 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13733 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13734 &wq_create->u.response);
13735 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13736 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13738 "3265 WQ[%d] doorbell format not "
13739 "supported: x%x\n", wq->queue_id,
13740 wq->db_format);
13741 status = -EINVAL;
13742 goto out;
13743 }
13744 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13745 &wq_create->u.response);
13746 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13747 if (!bar_memmap_p) {
13748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13749 "3263 WQ[%d] failed to memmap pci "
13750 "barset:x%x\n", wq->queue_id,
13751 pci_barset);
13752 status = -ENOMEM;
13753 goto out;
13754 }
13755 db_offset = wq_create->u.response.doorbell_offset;
13756 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13757 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13759 "3252 WQ[%d] doorbell offset not "
13760 "supported: x%x\n", wq->queue_id,
13761 db_offset);
13762 status = -EINVAL;
13763 goto out;
13764 }
13765 wq->db_regaddr = bar_memmap_p + db_offset;
13766 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013767 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13768 "format:x%x\n", wq->queue_id, pci_barset,
13769 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013770 } else {
13771 wq->db_format = LPFC_DB_LIST_FORMAT;
13772 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13773 }
James Smart4f774512009-05-22 14:52:35 -040013774 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013775 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013776 wq->subtype = subtype;
13777 wq->host_index = 0;
13778 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013779 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013780
13781 /* link the wq onto the parent cq child list */
13782 list_add_tail(&wq->list, &cq->child_list);
13783out:
James Smart8fa38512009-07-19 10:01:03 -040013784 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013785 return status;
13786}
13787
13788/**
James Smart73d91e52011-10-10 21:32:10 -040013789 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13790 * @phba: HBA structure that indicates port to create a queue on.
13791 * @rq: The queue structure to use for the receive queue.
13792 * @qno: The associated HBQ number
13793 *
13794 *
13795 * For SLI4 we need to adjust the RQ repost value based on
13796 * the number of buffers that are initially posted to the RQ.
13797 */
13798void
13799lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13800{
13801 uint32_t cnt;
13802
James Smart2e90f4b2011-12-13 13:22:37 -050013803 /* sanity check on queue memory */
13804 if (!rq)
13805 return;
James Smart73d91e52011-10-10 21:32:10 -040013806 cnt = lpfc_hbq_defs[qno]->entry_count;
13807
13808 /* Recalc repost for RQs based on buffers initially posted */
13809 cnt = (cnt >> 3);
13810 if (cnt < LPFC_QUEUE_MIN_REPOST)
13811 cnt = LPFC_QUEUE_MIN_REPOST;
13812
13813 rq->entry_repost = cnt;
13814}
13815
13816/**
James Smart4f774512009-05-22 14:52:35 -040013817 * lpfc_rq_create - Create a Receive Queue on the HBA
13818 * @phba: HBA structure that indicates port to create a queue on.
13819 * @hrq: The queue structure to use to create the header receive queue.
13820 * @drq: The queue structure to use to create the data receive queue.
13821 * @cq: The completion queue to bind this work queue to.
13822 *
13823 * This function creates a receive buffer queue pair , as detailed in @hrq and
13824 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13825 * to the HBA.
13826 *
13827 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13828 * struct is used to get the entry count that is necessary to determine the
13829 * number of pages to use for this queue. The @cq is used to indicate which
13830 * completion queue to bind received buffers that are posted to these queues to.
13831 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13832 * receive queue pair. This function is asynchronous and will wait for the
13833 * mailbox command to finish before continuing.
13834 *
13835 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013836 * memory this function will return -ENOMEM. If the queue create mailbox command
13837 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013838 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013839int
James Smart4f774512009-05-22 14:52:35 -040013840lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13841 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13842{
13843 struct lpfc_mbx_rq_create *rq_create;
13844 struct lpfc_dmabuf *dmabuf;
13845 LPFC_MBOXQ_t *mbox;
13846 int rc, length, status = 0;
13847 uint32_t shdr_status, shdr_add_status;
13848 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013849 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013850 void __iomem *bar_memmap_p;
13851 uint32_t db_offset;
13852 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013853
James Smart2e90f4b2011-12-13 13:22:37 -050013854 /* sanity check on queue memory */
13855 if (!hrq || !drq || !cq)
13856 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013857 if (!phba->sli4_hba.pc_sli4_params.supported)
13858 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013859
13860 if (hrq->entry_count != drq->entry_count)
13861 return -EINVAL;
13862 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13863 if (!mbox)
13864 return -ENOMEM;
13865 length = (sizeof(struct lpfc_mbx_rq_create) -
13866 sizeof(struct lpfc_sli4_cfg_mhdr));
13867 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13868 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13869 length, LPFC_SLI4_MBX_EMBED);
13870 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013871 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13872 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13873 phba->sli4_hba.pc_sli4_params.rqv);
13874 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13875 bf_set(lpfc_rq_context_rqe_count_1,
13876 &rq_create->u.request.context,
13877 hrq->entry_count);
13878 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013879 bf_set(lpfc_rq_context_rqe_size,
13880 &rq_create->u.request.context,
13881 LPFC_RQE_SIZE_8);
13882 bf_set(lpfc_rq_context_page_size,
13883 &rq_create->u.request.context,
13884 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013885 } else {
13886 switch (hrq->entry_count) {
13887 default:
13888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13889 "2535 Unsupported RQ count. (%d)\n",
13890 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013891 if (hrq->entry_count < 512) {
13892 status = -EINVAL;
13893 goto out;
13894 }
James Smart5a6f1332011-03-11 16:05:35 -050013895 /* otherwise default to smallest count (drop through) */
13896 case 512:
13897 bf_set(lpfc_rq_context_rqe_count,
13898 &rq_create->u.request.context,
13899 LPFC_RQ_RING_SIZE_512);
13900 break;
13901 case 1024:
13902 bf_set(lpfc_rq_context_rqe_count,
13903 &rq_create->u.request.context,
13904 LPFC_RQ_RING_SIZE_1024);
13905 break;
13906 case 2048:
13907 bf_set(lpfc_rq_context_rqe_count,
13908 &rq_create->u.request.context,
13909 LPFC_RQ_RING_SIZE_2048);
13910 break;
13911 case 4096:
13912 bf_set(lpfc_rq_context_rqe_count,
13913 &rq_create->u.request.context,
13914 LPFC_RQ_RING_SIZE_4096);
13915 break;
13916 }
13917 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13918 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013919 }
13920 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13921 cq->queue_id);
13922 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13923 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013924 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013925 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013926 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13927 putPaddrLow(dmabuf->phys);
13928 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13929 putPaddrHigh(dmabuf->phys);
13930 }
James Smart962bc512013-01-03 15:44:00 -050013931 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13932 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13933
James Smart4f774512009-05-22 14:52:35 -040013934 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13935 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013936 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13937 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13938 if (shdr_status || shdr_add_status || rc) {
13939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13940 "2504 RQ_CREATE mailbox failed with "
13941 "status x%x add_status x%x, mbx status x%x\n",
13942 shdr_status, shdr_add_status, rc);
13943 status = -ENXIO;
13944 goto out;
13945 }
13946 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13947 if (hrq->queue_id == 0xFFFF) {
13948 status = -ENXIO;
13949 goto out;
13950 }
James Smart962bc512013-01-03 15:44:00 -050013951
13952 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13953 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13954 &rq_create->u.response);
13955 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13956 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13958 "3262 RQ [%d] doorbell format not "
13959 "supported: x%x\n", hrq->queue_id,
13960 hrq->db_format);
13961 status = -EINVAL;
13962 goto out;
13963 }
13964
13965 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13966 &rq_create->u.response);
13967 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13968 if (!bar_memmap_p) {
13969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13970 "3269 RQ[%d] failed to memmap pci "
13971 "barset:x%x\n", hrq->queue_id,
13972 pci_barset);
13973 status = -ENOMEM;
13974 goto out;
13975 }
13976
13977 db_offset = rq_create->u.response.doorbell_offset;
13978 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13979 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13981 "3270 RQ[%d] doorbell offset not "
13982 "supported: x%x\n", hrq->queue_id,
13983 db_offset);
13984 status = -EINVAL;
13985 goto out;
13986 }
13987 hrq->db_regaddr = bar_memmap_p + db_offset;
13988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013989 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13990 "format:x%x\n", hrq->queue_id, pci_barset,
13991 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013992 } else {
13993 hrq->db_format = LPFC_DB_RING_FORMAT;
13994 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13995 }
James Smart4f774512009-05-22 14:52:35 -040013996 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013997 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013998 hrq->subtype = subtype;
13999 hrq->host_index = 0;
14000 hrq->hba_index = 0;
14001
14002 /* now create the data queue */
14003 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14004 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14005 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014006 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14007 phba->sli4_hba.pc_sli4_params.rqv);
14008 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14009 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014010 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014011 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014012 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14013 LPFC_RQE_SIZE_8);
14014 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14015 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014016 } else {
14017 switch (drq->entry_count) {
14018 default:
14019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14020 "2536 Unsupported RQ count. (%d)\n",
14021 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014022 if (drq->entry_count < 512) {
14023 status = -EINVAL;
14024 goto out;
14025 }
James Smart5a6f1332011-03-11 16:05:35 -050014026 /* otherwise default to smallest count (drop through) */
14027 case 512:
14028 bf_set(lpfc_rq_context_rqe_count,
14029 &rq_create->u.request.context,
14030 LPFC_RQ_RING_SIZE_512);
14031 break;
14032 case 1024:
14033 bf_set(lpfc_rq_context_rqe_count,
14034 &rq_create->u.request.context,
14035 LPFC_RQ_RING_SIZE_1024);
14036 break;
14037 case 2048:
14038 bf_set(lpfc_rq_context_rqe_count,
14039 &rq_create->u.request.context,
14040 LPFC_RQ_RING_SIZE_2048);
14041 break;
14042 case 4096:
14043 bf_set(lpfc_rq_context_rqe_count,
14044 &rq_create->u.request.context,
14045 LPFC_RQ_RING_SIZE_4096);
14046 break;
14047 }
14048 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14049 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014050 }
14051 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14052 cq->queue_id);
14053 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14054 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014055 list_for_each_entry(dmabuf, &drq->page_list, list) {
14056 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14057 putPaddrLow(dmabuf->phys);
14058 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14059 putPaddrHigh(dmabuf->phys);
14060 }
James Smart962bc512013-01-03 15:44:00 -050014061 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14062 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014063 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14064 /* The IOCTL status is embedded in the mailbox subheader. */
14065 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14066 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14067 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14068 if (shdr_status || shdr_add_status || rc) {
14069 status = -ENXIO;
14070 goto out;
14071 }
14072 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14073 if (drq->queue_id == 0xFFFF) {
14074 status = -ENXIO;
14075 goto out;
14076 }
14077 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014078 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014079 drq->subtype = subtype;
14080 drq->host_index = 0;
14081 drq->hba_index = 0;
14082
14083 /* link the header and data RQs onto the parent cq child list */
14084 list_add_tail(&hrq->list, &cq->child_list);
14085 list_add_tail(&drq->list, &cq->child_list);
14086
14087out:
James Smart8fa38512009-07-19 10:01:03 -040014088 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014089 return status;
14090}
14091
14092/**
14093 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14094 * @eq: The queue structure associated with the queue to destroy.
14095 *
14096 * This function destroys a queue, as detailed in @eq by sending an mailbox
14097 * command, specific to the type of queue, to the HBA.
14098 *
14099 * The @eq struct is used to get the queue ID of the queue to destroy.
14100 *
14101 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014102 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014103 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014104int
James Smart4f774512009-05-22 14:52:35 -040014105lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14106{
14107 LPFC_MBOXQ_t *mbox;
14108 int rc, length, status = 0;
14109 uint32_t shdr_status, shdr_add_status;
14110 union lpfc_sli4_cfg_shdr *shdr;
14111
James Smart2e90f4b2011-12-13 13:22:37 -050014112 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014113 if (!eq)
14114 return -ENODEV;
14115 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14116 if (!mbox)
14117 return -ENOMEM;
14118 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14119 sizeof(struct lpfc_sli4_cfg_mhdr));
14120 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14121 LPFC_MBOX_OPCODE_EQ_DESTROY,
14122 length, LPFC_SLI4_MBX_EMBED);
14123 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14124 eq->queue_id);
14125 mbox->vport = eq->phba->pport;
14126 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14127
14128 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14129 /* The IOCTL status is embedded in the mailbox subheader. */
14130 shdr = (union lpfc_sli4_cfg_shdr *)
14131 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14132 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14133 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14134 if (shdr_status || shdr_add_status || rc) {
14135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14136 "2505 EQ_DESTROY mailbox failed with "
14137 "status x%x add_status x%x, mbx status x%x\n",
14138 shdr_status, shdr_add_status, rc);
14139 status = -ENXIO;
14140 }
14141
14142 /* Remove eq from any list */
14143 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014144 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014145 return status;
14146}
14147
14148/**
14149 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14150 * @cq: The queue structure associated with the queue to destroy.
14151 *
14152 * This function destroys a queue, as detailed in @cq by sending an mailbox
14153 * command, specific to the type of queue, to the HBA.
14154 *
14155 * The @cq struct is used to get the queue ID of the queue to destroy.
14156 *
14157 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014158 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014159 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014160int
James Smart4f774512009-05-22 14:52:35 -040014161lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14162{
14163 LPFC_MBOXQ_t *mbox;
14164 int rc, length, status = 0;
14165 uint32_t shdr_status, shdr_add_status;
14166 union lpfc_sli4_cfg_shdr *shdr;
14167
James Smart2e90f4b2011-12-13 13:22:37 -050014168 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014169 if (!cq)
14170 return -ENODEV;
14171 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14172 if (!mbox)
14173 return -ENOMEM;
14174 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14175 sizeof(struct lpfc_sli4_cfg_mhdr));
14176 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14177 LPFC_MBOX_OPCODE_CQ_DESTROY,
14178 length, LPFC_SLI4_MBX_EMBED);
14179 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14180 cq->queue_id);
14181 mbox->vport = cq->phba->pport;
14182 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14183 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14184 /* The IOCTL status is embedded in the mailbox subheader. */
14185 shdr = (union lpfc_sli4_cfg_shdr *)
14186 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14187 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14188 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14189 if (shdr_status || shdr_add_status || rc) {
14190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14191 "2506 CQ_DESTROY mailbox failed with "
14192 "status x%x add_status x%x, mbx status x%x\n",
14193 shdr_status, shdr_add_status, rc);
14194 status = -ENXIO;
14195 }
14196 /* Remove cq from any list */
14197 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014198 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014199 return status;
14200}
14201
14202/**
James Smart04c68492009-05-22 14:52:52 -040014203 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14204 * @qm: The queue structure associated with the queue to destroy.
14205 *
14206 * This function destroys a queue, as detailed in @mq by sending an mailbox
14207 * command, specific to the type of queue, to the HBA.
14208 *
14209 * The @mq struct is used to get the queue ID of the queue to destroy.
14210 *
14211 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014212 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014213 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014214int
James Smart04c68492009-05-22 14:52:52 -040014215lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14216{
14217 LPFC_MBOXQ_t *mbox;
14218 int rc, length, status = 0;
14219 uint32_t shdr_status, shdr_add_status;
14220 union lpfc_sli4_cfg_shdr *shdr;
14221
James Smart2e90f4b2011-12-13 13:22:37 -050014222 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014223 if (!mq)
14224 return -ENODEV;
14225 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14226 if (!mbox)
14227 return -ENOMEM;
14228 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14229 sizeof(struct lpfc_sli4_cfg_mhdr));
14230 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14231 LPFC_MBOX_OPCODE_MQ_DESTROY,
14232 length, LPFC_SLI4_MBX_EMBED);
14233 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14234 mq->queue_id);
14235 mbox->vport = mq->phba->pport;
14236 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14237 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14238 /* The IOCTL status is embedded in the mailbox subheader. */
14239 shdr = (union lpfc_sli4_cfg_shdr *)
14240 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14241 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14242 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14243 if (shdr_status || shdr_add_status || rc) {
14244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14245 "2507 MQ_DESTROY mailbox failed with "
14246 "status x%x add_status x%x, mbx status x%x\n",
14247 shdr_status, shdr_add_status, rc);
14248 status = -ENXIO;
14249 }
14250 /* Remove mq from any list */
14251 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014252 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014253 return status;
14254}
14255
14256/**
James Smart4f774512009-05-22 14:52:35 -040014257 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14258 * @wq: The queue structure associated with the queue to destroy.
14259 *
14260 * This function destroys a queue, as detailed in @wq by sending an mailbox
14261 * command, specific to the type of queue, to the HBA.
14262 *
14263 * The @wq struct is used to get the queue ID of the queue to destroy.
14264 *
14265 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014266 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014267 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014268int
James Smart4f774512009-05-22 14:52:35 -040014269lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14270{
14271 LPFC_MBOXQ_t *mbox;
14272 int rc, length, status = 0;
14273 uint32_t shdr_status, shdr_add_status;
14274 union lpfc_sli4_cfg_shdr *shdr;
14275
James Smart2e90f4b2011-12-13 13:22:37 -050014276 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014277 if (!wq)
14278 return -ENODEV;
14279 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14280 if (!mbox)
14281 return -ENOMEM;
14282 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14283 sizeof(struct lpfc_sli4_cfg_mhdr));
14284 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14285 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14286 length, LPFC_SLI4_MBX_EMBED);
14287 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14288 wq->queue_id);
14289 mbox->vport = wq->phba->pport;
14290 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14291 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14292 shdr = (union lpfc_sli4_cfg_shdr *)
14293 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14296 if (shdr_status || shdr_add_status || rc) {
14297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14298 "2508 WQ_DESTROY mailbox failed with "
14299 "status x%x add_status x%x, mbx status x%x\n",
14300 shdr_status, shdr_add_status, rc);
14301 status = -ENXIO;
14302 }
14303 /* Remove wq from any list */
14304 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014305 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014306 return status;
14307}
14308
14309/**
14310 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14311 * @rq: The queue structure associated with the queue to destroy.
14312 *
14313 * This function destroys a queue, as detailed in @rq by sending an mailbox
14314 * command, specific to the type of queue, to the HBA.
14315 *
14316 * The @rq struct is used to get the queue ID of the queue to destroy.
14317 *
14318 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014319 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014320 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014321int
James Smart4f774512009-05-22 14:52:35 -040014322lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14323 struct lpfc_queue *drq)
14324{
14325 LPFC_MBOXQ_t *mbox;
14326 int rc, length, status = 0;
14327 uint32_t shdr_status, shdr_add_status;
14328 union lpfc_sli4_cfg_shdr *shdr;
14329
James Smart2e90f4b2011-12-13 13:22:37 -050014330 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014331 if (!hrq || !drq)
14332 return -ENODEV;
14333 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14334 if (!mbox)
14335 return -ENOMEM;
14336 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014337 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014338 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14339 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14340 length, LPFC_SLI4_MBX_EMBED);
14341 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14342 hrq->queue_id);
14343 mbox->vport = hrq->phba->pport;
14344 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14345 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14346 /* The IOCTL status is embedded in the mailbox subheader. */
14347 shdr = (union lpfc_sli4_cfg_shdr *)
14348 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14349 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14350 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14351 if (shdr_status || shdr_add_status || rc) {
14352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14353 "2509 RQ_DESTROY mailbox failed with "
14354 "status x%x add_status x%x, mbx status x%x\n",
14355 shdr_status, shdr_add_status, rc);
14356 if (rc != MBX_TIMEOUT)
14357 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14358 return -ENXIO;
14359 }
14360 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14361 drq->queue_id);
14362 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14363 shdr = (union lpfc_sli4_cfg_shdr *)
14364 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14365 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14366 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14367 if (shdr_status || shdr_add_status || rc) {
14368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14369 "2510 RQ_DESTROY mailbox failed with "
14370 "status x%x add_status x%x, mbx status x%x\n",
14371 shdr_status, shdr_add_status, rc);
14372 status = -ENXIO;
14373 }
14374 list_del_init(&hrq->list);
14375 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014376 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014377 return status;
14378}
14379
14380/**
14381 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14382 * @phba: The virtual port for which this call being executed.
14383 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14384 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14385 * @xritag: the xritag that ties this io to the SGL pages.
14386 *
14387 * This routine will post the sgl pages for the IO that has the xritag
14388 * that is in the iocbq structure. The xritag is assigned during iocbq
14389 * creation and persists for as long as the driver is loaded.
14390 * if the caller has fewer than 256 scatter gather segments to map then
14391 * pdma_phys_addr1 should be 0.
14392 * If the caller needs to map more than 256 scatter gather segment then
14393 * pdma_phys_addr1 should be a valid physical address.
14394 * physical address for SGLs must be 64 byte aligned.
14395 * If you are going to map 2 SGL's then the first one must have 256 entries
14396 * the second sgl can have between 1 and 256 entries.
14397 *
14398 * Return codes:
14399 * 0 - Success
14400 * -ENXIO, -ENOMEM - Failure
14401 **/
14402int
14403lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14404 dma_addr_t pdma_phys_addr0,
14405 dma_addr_t pdma_phys_addr1,
14406 uint16_t xritag)
14407{
14408 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14409 LPFC_MBOXQ_t *mbox;
14410 int rc;
14411 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014412 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014413 union lpfc_sli4_cfg_shdr *shdr;
14414
14415 if (xritag == NO_XRI) {
14416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14417 "0364 Invalid param:\n");
14418 return -EINVAL;
14419 }
14420
14421 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14422 if (!mbox)
14423 return -ENOMEM;
14424
14425 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14426 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14427 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014428 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014429
14430 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14431 &mbox->u.mqe.un.post_sgl_pages;
14432 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14433 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14434
14435 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14436 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14437 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14438 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14439
14440 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14441 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14442 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14443 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14444 if (!phba->sli4_hba.intr_enable)
14445 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014446 else {
James Smarta183a152011-10-10 21:32:43 -040014447 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014448 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14449 }
James Smart4f774512009-05-22 14:52:35 -040014450 /* The IOCTL status is embedded in the mailbox subheader. */
14451 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14452 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14453 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14454 if (rc != MBX_TIMEOUT)
14455 mempool_free(mbox, phba->mbox_mem_pool);
14456 if (shdr_status || shdr_add_status || rc) {
14457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14458 "2511 POST_SGL mailbox failed with "
14459 "status x%x add_status x%x, mbx status x%x\n",
14460 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014461 }
14462 return 0;
14463}
James Smart4f774512009-05-22 14:52:35 -040014464
14465/**
James Smart88a2cfb2011-07-22 18:36:33 -040014466 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014467 * @phba: pointer to lpfc hba data structure.
14468 *
14469 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014470 * HBA consistent with the SLI-4 interface spec. This routine
14471 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14472 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014473 *
James Smart88a2cfb2011-07-22 18:36:33 -040014474 * Returns
14475 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14476 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14477 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014478static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014479lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14480{
14481 unsigned long xri;
14482
14483 /*
14484 * Fetch the next logical xri. Because this index is logical,
14485 * the driver starts at 0 each time.
14486 */
14487 spin_lock_irq(&phba->hbalock);
14488 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14489 phba->sli4_hba.max_cfg_param.max_xri, 0);
14490 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14491 spin_unlock_irq(&phba->hbalock);
14492 return NO_XRI;
14493 } else {
14494 set_bit(xri, phba->sli4_hba.xri_bmask);
14495 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014496 }
James Smart6d368e52011-05-24 11:44:12 -040014497 spin_unlock_irq(&phba->hbalock);
14498 return xri;
14499}
14500
14501/**
14502 * lpfc_sli4_free_xri - Release an xri for reuse.
14503 * @phba: pointer to lpfc hba data structure.
14504 *
14505 * This routine is invoked to release an xri to the pool of
14506 * available rpis maintained by the driver.
14507 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014508static void
James Smart6d368e52011-05-24 11:44:12 -040014509__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14510{
14511 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014512 phba->sli4_hba.max_cfg_param.xri_used--;
14513 }
14514}
14515
14516/**
14517 * lpfc_sli4_free_xri - Release an xri for reuse.
14518 * @phba: pointer to lpfc hba data structure.
14519 *
14520 * This routine is invoked to release an xri to the pool of
14521 * available rpis maintained by the driver.
14522 **/
14523void
14524lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14525{
14526 spin_lock_irq(&phba->hbalock);
14527 __lpfc_sli4_free_xri(phba, xri);
14528 spin_unlock_irq(&phba->hbalock);
14529}
14530
14531/**
James Smart4f774512009-05-22 14:52:35 -040014532 * lpfc_sli4_next_xritag - Get an xritag for the io
14533 * @phba: Pointer to HBA context object.
14534 *
14535 * This function gets an xritag for the iocb. If there is no unused xritag
14536 * it will return 0xffff.
14537 * The function returns the allocated xritag if successful, else returns zero.
14538 * Zero is not a valid xritag.
14539 * The caller is not required to hold any lock.
14540 **/
14541uint16_t
14542lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14543{
James Smart6d368e52011-05-24 11:44:12 -040014544 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014545
James Smart6d368e52011-05-24 11:44:12 -040014546 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014547 if (xri_index == NO_XRI)
14548 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14549 "2004 Failed to allocate XRI.last XRITAG is %d"
14550 " Max XRI is %d, Used XRI is %d\n",
14551 xri_index,
14552 phba->sli4_hba.max_cfg_param.max_xri,
14553 phba->sli4_hba.max_cfg_param.xri_used);
14554 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014555}
14556
14557/**
James Smart6d368e52011-05-24 11:44:12 -040014558 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014559 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014560 * @post_sgl_list: pointer to els sgl entry list.
14561 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014562 *
14563 * This routine is invoked to post a block of driver's sgl pages to the
14564 * HBA using non-embedded mailbox command. No Lock is held. This routine
14565 * is only called when the driver is loading and after all IO has been
14566 * stopped.
14567 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014568static int
14569lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14570 struct list_head *post_sgl_list,
14571 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014572{
James Smart8a9d2e82012-05-09 21:16:12 -040014573 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014574 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14575 struct sgl_page_pairs *sgl_pg_pairs;
14576 void *viraddr;
14577 LPFC_MBOXQ_t *mbox;
14578 uint32_t reqlen, alloclen, pg_pairs;
14579 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014580 uint16_t xritag_start = 0;
14581 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014582 uint32_t shdr_status, shdr_add_status;
14583 union lpfc_sli4_cfg_shdr *shdr;
14584
James Smart8a9d2e82012-05-09 21:16:12 -040014585 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014586 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014587 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014588 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14589 "2559 Block sgl registration required DMA "
14590 "size (%d) great than a page\n", reqlen);
14591 return -ENOMEM;
14592 }
14593 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014594 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014595 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014596
14597 /* Allocate DMA memory and set up the non-embedded mailbox command */
14598 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14599 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14600 LPFC_SLI4_MBX_NEMBED);
14601
14602 if (alloclen < reqlen) {
14603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14604 "0285 Allocated DMA memory size (%d) is "
14605 "less than the requested DMA memory "
14606 "size (%d)\n", alloclen, reqlen);
14607 lpfc_sli4_mbox_cmd_free(phba, mbox);
14608 return -ENOMEM;
14609 }
James Smart4f774512009-05-22 14:52:35 -040014610 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014611 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014612 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14613 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14614
James Smart8a9d2e82012-05-09 21:16:12 -040014615 pg_pairs = 0;
14616 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014617 /* Set up the sge entry */
14618 sgl_pg_pairs->sgl_pg0_addr_lo =
14619 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14620 sgl_pg_pairs->sgl_pg0_addr_hi =
14621 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14622 sgl_pg_pairs->sgl_pg1_addr_lo =
14623 cpu_to_le32(putPaddrLow(0));
14624 sgl_pg_pairs->sgl_pg1_addr_hi =
14625 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014626
James Smart4f774512009-05-22 14:52:35 -040014627 /* Keep the first xritag on the list */
14628 if (pg_pairs == 0)
14629 xritag_start = sglq_entry->sli4_xritag;
14630 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014631 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014632 }
James Smart6d368e52011-05-24 11:44:12 -040014633
14634 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014635 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014636 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014637 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014638 if (!phba->sli4_hba.intr_enable)
14639 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14640 else {
James Smarta183a152011-10-10 21:32:43 -040014641 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014642 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14643 }
14644 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14645 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14646 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14647 if (rc != MBX_TIMEOUT)
14648 lpfc_sli4_mbox_cmd_free(phba, mbox);
14649 if (shdr_status || shdr_add_status || rc) {
14650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14651 "2513 POST_SGL_BLOCK mailbox command failed "
14652 "status x%x add_status x%x mbx status x%x\n",
14653 shdr_status, shdr_add_status, rc);
14654 rc = -ENXIO;
14655 }
14656 return rc;
14657}
14658
14659/**
14660 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14661 * @phba: pointer to lpfc hba data structure.
14662 * @sblist: pointer to scsi buffer list.
14663 * @count: number of scsi buffers on the list.
14664 *
14665 * This routine is invoked to post a block of @count scsi sgl pages from a
14666 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14667 * No Lock is held.
14668 *
14669 **/
14670int
James Smart8a9d2e82012-05-09 21:16:12 -040014671lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14672 struct list_head *sblist,
14673 int count)
James Smart4f774512009-05-22 14:52:35 -040014674{
14675 struct lpfc_scsi_buf *psb;
14676 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14677 struct sgl_page_pairs *sgl_pg_pairs;
14678 void *viraddr;
14679 LPFC_MBOXQ_t *mbox;
14680 uint32_t reqlen, alloclen, pg_pairs;
14681 uint32_t mbox_tmo;
14682 uint16_t xritag_start = 0;
14683 int rc = 0;
14684 uint32_t shdr_status, shdr_add_status;
14685 dma_addr_t pdma_phys_bpl1;
14686 union lpfc_sli4_cfg_shdr *shdr;
14687
14688 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014689 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014690 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014691 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014692 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14693 "0217 Block sgl registration required DMA "
14694 "size (%d) great than a page\n", reqlen);
14695 return -ENOMEM;
14696 }
14697 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14698 if (!mbox) {
14699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14700 "0283 Failed to allocate mbox cmd memory\n");
14701 return -ENOMEM;
14702 }
14703
14704 /* Allocate DMA memory and set up the non-embedded mailbox command */
14705 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14706 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14707 LPFC_SLI4_MBX_NEMBED);
14708
14709 if (alloclen < reqlen) {
14710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14711 "2561 Allocated DMA memory size (%d) is "
14712 "less than the requested DMA memory "
14713 "size (%d)\n", alloclen, reqlen);
14714 lpfc_sli4_mbox_cmd_free(phba, mbox);
14715 return -ENOMEM;
14716 }
James Smart6d368e52011-05-24 11:44:12 -040014717
James Smart4f774512009-05-22 14:52:35 -040014718 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014719 viraddr = mbox->sge_array->addr[0];
14720
14721 /* Set up the SGL pages in the non-embedded DMA pages */
14722 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14723 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14724
14725 pg_pairs = 0;
14726 list_for_each_entry(psb, sblist, list) {
14727 /* Set up the sge entry */
14728 sgl_pg_pairs->sgl_pg0_addr_lo =
14729 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14730 sgl_pg_pairs->sgl_pg0_addr_hi =
14731 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14732 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14733 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14734 else
14735 pdma_phys_bpl1 = 0;
14736 sgl_pg_pairs->sgl_pg1_addr_lo =
14737 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14738 sgl_pg_pairs->sgl_pg1_addr_hi =
14739 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14740 /* Keep the first xritag on the list */
14741 if (pg_pairs == 0)
14742 xritag_start = psb->cur_iocbq.sli4_xritag;
14743 sgl_pg_pairs++;
14744 pg_pairs++;
14745 }
14746 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14747 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14748 /* Perform endian conversion if necessary */
14749 sgl->word0 = cpu_to_le32(sgl->word0);
14750
14751 if (!phba->sli4_hba.intr_enable)
14752 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14753 else {
James Smarta183a152011-10-10 21:32:43 -040014754 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014755 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14756 }
14757 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14758 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14759 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14760 if (rc != MBX_TIMEOUT)
14761 lpfc_sli4_mbox_cmd_free(phba, mbox);
14762 if (shdr_status || shdr_add_status || rc) {
14763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14764 "2564 POST_SGL_BLOCK mailbox command failed "
14765 "status x%x add_status x%x mbx status x%x\n",
14766 shdr_status, shdr_add_status, rc);
14767 rc = -ENXIO;
14768 }
14769 return rc;
14770}
14771
14772/**
14773 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14774 * @phba: pointer to lpfc_hba struct that the frame was received on
14775 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14776 *
14777 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14778 * valid type of frame that the LPFC driver will handle. This function will
14779 * return a zero if the frame is a valid frame or a non zero value when the
14780 * frame does not pass the check.
14781 **/
14782static int
14783lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14784{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014785 /* make rctl_names static to save stack space */
14786 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014787 char *type_names[] = FC_TYPE_NAMES_INIT;
14788 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014789 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014790
14791 switch (fc_hdr->fh_r_ctl) {
14792 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14793 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14794 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14795 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14796 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14797 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14798 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14799 case FC_RCTL_DD_CMD_STATUS: /* command status */
14800 case FC_RCTL_ELS_REQ: /* extended link services request */
14801 case FC_RCTL_ELS_REP: /* extended link services reply */
14802 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14803 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14804 case FC_RCTL_BA_NOP: /* basic link service NOP */
14805 case FC_RCTL_BA_ABTS: /* basic link service abort */
14806 case FC_RCTL_BA_RMC: /* remove connection */
14807 case FC_RCTL_BA_ACC: /* basic accept */
14808 case FC_RCTL_BA_RJT: /* basic reject */
14809 case FC_RCTL_BA_PRMT:
14810 case FC_RCTL_ACK_1: /* acknowledge_1 */
14811 case FC_RCTL_ACK_0: /* acknowledge_0 */
14812 case FC_RCTL_P_RJT: /* port reject */
14813 case FC_RCTL_F_RJT: /* fabric reject */
14814 case FC_RCTL_P_BSY: /* port busy */
14815 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14816 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14817 case FC_RCTL_LCR: /* link credit reset */
14818 case FC_RCTL_END: /* end */
14819 break;
14820 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14821 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14822 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14823 return lpfc_fc_frame_check(phba, fc_hdr);
14824 default:
14825 goto drop;
14826 }
14827 switch (fc_hdr->fh_type) {
14828 case FC_TYPE_BLS:
14829 case FC_TYPE_ELS:
14830 case FC_TYPE_FCP:
14831 case FC_TYPE_CT:
14832 break;
14833 case FC_TYPE_IP:
14834 case FC_TYPE_ILS:
14835 default:
14836 goto drop;
14837 }
James Smart546fc852011-03-11 16:06:29 -050014838
James Smart4f774512009-05-22 14:52:35 -040014839 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014840 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14841 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14842 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14843 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014844 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14845 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014846 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14847 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014848 return 0;
14849drop:
14850 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14851 "2539 Dropped frame rctl:%s type:%s\n",
14852 rctl_names[fc_hdr->fh_r_ctl],
14853 type_names[fc_hdr->fh_type]);
14854 return 1;
14855}
14856
14857/**
14858 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14859 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14860 *
14861 * This function processes the FC header to retrieve the VFI from the VF
14862 * header, if one exists. This function will return the VFI if one exists
14863 * or 0 if no VSAN Header exists.
14864 **/
14865static uint32_t
14866lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14867{
14868 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14869
14870 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14871 return 0;
14872 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14873}
14874
14875/**
14876 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14877 * @phba: Pointer to the HBA structure to search for the vport on
14878 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14879 * @fcfi: The FC Fabric ID that the frame came from
14880 *
14881 * This function searches the @phba for a vport that matches the content of the
14882 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14883 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14884 * returns the matching vport pointer or NULL if unable to match frame to a
14885 * vport.
14886 **/
14887static struct lpfc_vport *
14888lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14889 uint16_t fcfi)
14890{
14891 struct lpfc_vport **vports;
14892 struct lpfc_vport *vport = NULL;
14893 int i;
14894 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14895 fc_hdr->fh_d_id[1] << 8 |
14896 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014897
James Smartbf086112011-08-21 21:48:13 -040014898 if (did == Fabric_DID)
14899 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014900 if ((phba->pport->fc_flag & FC_PT2PT) &&
14901 !(phba->link_state == LPFC_HBA_READY))
14902 return phba->pport;
14903
James Smart4f774512009-05-22 14:52:35 -040014904 vports = lpfc_create_vport_work_array(phba);
14905 if (vports != NULL)
14906 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14907 if (phba->fcf.fcfi == fcfi &&
14908 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14909 vports[i]->fc_myDID == did) {
14910 vport = vports[i];
14911 break;
14912 }
14913 }
14914 lpfc_destroy_vport_work_array(phba, vports);
14915 return vport;
14916}
14917
14918/**
James Smart45ed1192009-10-02 15:17:02 -040014919 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14920 * @vport: The vport to work on.
14921 *
14922 * This function updates the receive sequence time stamp for this vport. The
14923 * receive sequence time stamp indicates the time that the last frame of the
14924 * the sequence that has been idle for the longest amount of time was received.
14925 * the driver uses this time stamp to indicate if any received sequences have
14926 * timed out.
14927 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014928static void
James Smart45ed1192009-10-02 15:17:02 -040014929lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14930{
14931 struct lpfc_dmabuf *h_buf;
14932 struct hbq_dmabuf *dmabuf = NULL;
14933
14934 /* get the oldest sequence on the rcv list */
14935 h_buf = list_get_first(&vport->rcv_buffer_list,
14936 struct lpfc_dmabuf, list);
14937 if (!h_buf)
14938 return;
14939 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14940 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14941}
14942
14943/**
14944 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14945 * @vport: The vport that the received sequences were sent to.
14946 *
14947 * This function cleans up all outstanding received sequences. This is called
14948 * by the driver when a link event or user action invalidates all the received
14949 * sequences.
14950 **/
14951void
14952lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14953{
14954 struct lpfc_dmabuf *h_buf, *hnext;
14955 struct lpfc_dmabuf *d_buf, *dnext;
14956 struct hbq_dmabuf *dmabuf = NULL;
14957
14958 /* start with the oldest sequence on the rcv list */
14959 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14960 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14961 list_del_init(&dmabuf->hbuf.list);
14962 list_for_each_entry_safe(d_buf, dnext,
14963 &dmabuf->dbuf.list, list) {
14964 list_del_init(&d_buf->list);
14965 lpfc_in_buf_free(vport->phba, d_buf);
14966 }
14967 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14968 }
14969}
14970
14971/**
14972 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14973 * @vport: The vport that the received sequences were sent to.
14974 *
14975 * This function determines whether any received sequences have timed out by
14976 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14977 * indicates that there is at least one timed out sequence this routine will
14978 * go through the received sequences one at a time from most inactive to most
14979 * active to determine which ones need to be cleaned up. Once it has determined
14980 * that a sequence needs to be cleaned up it will simply free up the resources
14981 * without sending an abort.
14982 **/
14983void
14984lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14985{
14986 struct lpfc_dmabuf *h_buf, *hnext;
14987 struct lpfc_dmabuf *d_buf, *dnext;
14988 struct hbq_dmabuf *dmabuf = NULL;
14989 unsigned long timeout;
14990 int abort_count = 0;
14991
14992 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14993 vport->rcv_buffer_time_stamp);
14994 if (list_empty(&vport->rcv_buffer_list) ||
14995 time_before(jiffies, timeout))
14996 return;
14997 /* start with the oldest sequence on the rcv list */
14998 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14999 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15000 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15001 dmabuf->time_stamp);
15002 if (time_before(jiffies, timeout))
15003 break;
15004 abort_count++;
15005 list_del_init(&dmabuf->hbuf.list);
15006 list_for_each_entry_safe(d_buf, dnext,
15007 &dmabuf->dbuf.list, list) {
15008 list_del_init(&d_buf->list);
15009 lpfc_in_buf_free(vport->phba, d_buf);
15010 }
15011 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15012 }
15013 if (abort_count)
15014 lpfc_update_rcv_time_stamp(vport);
15015}
15016
15017/**
James Smart4f774512009-05-22 14:52:35 -040015018 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15019 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15020 *
15021 * This function searches through the existing incomplete sequences that have
15022 * been sent to this @vport. If the frame matches one of the incomplete
15023 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15024 * make up that sequence. If no sequence is found that matches this frame then
15025 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15026 * This function returns a pointer to the first dmabuf in the sequence list that
15027 * the frame was linked to.
15028 **/
15029static struct hbq_dmabuf *
15030lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15031{
15032 struct fc_frame_header *new_hdr;
15033 struct fc_frame_header *temp_hdr;
15034 struct lpfc_dmabuf *d_buf;
15035 struct lpfc_dmabuf *h_buf;
15036 struct hbq_dmabuf *seq_dmabuf = NULL;
15037 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015038 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015039
James Smart4d9ab992009-10-02 15:16:39 -040015040 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015041 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015042 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015043
James Smart4f774512009-05-22 14:52:35 -040015044 /* Use the hdr_buf to find the sequence that this frame belongs to */
15045 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15046 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15047 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15048 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15049 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15050 continue;
15051 /* found a pending sequence that matches this frame */
15052 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15053 break;
15054 }
15055 if (!seq_dmabuf) {
15056 /*
15057 * This indicates first frame received for this sequence.
15058 * Queue the buffer on the vport's rcv_buffer_list.
15059 */
15060 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015061 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015062 return dmabuf;
15063 }
15064 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015065 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15066 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015067 list_del_init(&seq_dmabuf->hbuf.list);
15068 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15069 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015070 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015071 return dmabuf;
15072 }
James Smart45ed1192009-10-02 15:17:02 -040015073 /* move this sequence to the tail to indicate a young sequence */
15074 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15075 seq_dmabuf->time_stamp = jiffies;
15076 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015077 if (list_empty(&seq_dmabuf->dbuf.list)) {
15078 temp_hdr = dmabuf->hbuf.virt;
15079 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15080 return seq_dmabuf;
15081 }
James Smart4f774512009-05-22 14:52:35 -040015082 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015083 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15084 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015085 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15086 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15087 /*
15088 * If the frame's sequence count is greater than the frame on
15089 * the list then insert the frame right after this frame
15090 */
James Smarteeead812009-12-21 17:01:23 -050015091 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15092 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015093 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015094 found = 1;
15095 break;
James Smart4f774512009-05-22 14:52:35 -040015096 }
James Smart4360ca92015-12-16 18:12:04 -050015097
15098 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15099 break;
15100 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015101 }
James Smart4360ca92015-12-16 18:12:04 -050015102
15103 if (found)
15104 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015105 return NULL;
15106}
15107
15108/**
James Smart6669f9b2009-10-02 15:16:45 -040015109 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15110 * @vport: pointer to a vitural port
15111 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15112 *
15113 * This function tries to abort from the partially assembed sequence, described
15114 * by the information from basic abbort @dmabuf. It checks to see whether such
15115 * partially assembled sequence held by the driver. If so, it shall free up all
15116 * the frames from the partially assembled sequence.
15117 *
15118 * Return
15119 * true -- if there is matching partially assembled sequence present and all
15120 * the frames freed with the sequence;
15121 * false -- if there is no matching partially assembled sequence present so
15122 * nothing got aborted in the lower layer driver
15123 **/
15124static bool
15125lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15126 struct hbq_dmabuf *dmabuf)
15127{
15128 struct fc_frame_header *new_hdr;
15129 struct fc_frame_header *temp_hdr;
15130 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15131 struct hbq_dmabuf *seq_dmabuf = NULL;
15132
15133 /* Use the hdr_buf to find the sequence that matches this frame */
15134 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15135 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15136 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15137 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15138 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15139 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15140 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15141 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15142 continue;
15143 /* found a pending sequence that matches this frame */
15144 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15145 break;
15146 }
15147
15148 /* Free up all the frames from the partially assembled sequence */
15149 if (seq_dmabuf) {
15150 list_for_each_entry_safe(d_buf, n_buf,
15151 &seq_dmabuf->dbuf.list, list) {
15152 list_del_init(&d_buf->list);
15153 lpfc_in_buf_free(vport->phba, d_buf);
15154 }
15155 return true;
15156 }
15157 return false;
15158}
15159
15160/**
James Smart6dd9e312013-01-03 15:43:37 -050015161 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15162 * @vport: pointer to a vitural port
15163 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15164 *
15165 * This function tries to abort from the assembed sequence from upper level
15166 * protocol, described by the information from basic abbort @dmabuf. It
15167 * checks to see whether such pending context exists at upper level protocol.
15168 * If so, it shall clean up the pending context.
15169 *
15170 * Return
15171 * true -- if there is matching pending context of the sequence cleaned
15172 * at ulp;
15173 * false -- if there is no matching pending context of the sequence present
15174 * at ulp.
15175 **/
15176static bool
15177lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15178{
15179 struct lpfc_hba *phba = vport->phba;
15180 int handled;
15181
15182 /* Accepting abort at ulp with SLI4 only */
15183 if (phba->sli_rev < LPFC_SLI_REV4)
15184 return false;
15185
15186 /* Register all caring upper level protocols to attend abort */
15187 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15188 if (handled)
15189 return true;
15190
15191 return false;
15192}
15193
15194/**
James Smart546fc852011-03-11 16:06:29 -050015195 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015196 * @phba: Pointer to HBA context object.
15197 * @cmd_iocbq: pointer to the command iocbq structure.
15198 * @rsp_iocbq: pointer to the response iocbq structure.
15199 *
James Smart546fc852011-03-11 16:06:29 -050015200 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015201 * event. It properly releases the memory allocated to the sequence abort
15202 * accept iocb.
15203 **/
15204static void
James Smart546fc852011-03-11 16:06:29 -050015205lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015206 struct lpfc_iocbq *cmd_iocbq,
15207 struct lpfc_iocbq *rsp_iocbq)
15208{
James Smart6dd9e312013-01-03 15:43:37 -050015209 struct lpfc_nodelist *ndlp;
15210
15211 if (cmd_iocbq) {
15212 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15213 lpfc_nlp_put(ndlp);
15214 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015215 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015216 }
James Smart6b5151f2012-01-18 16:24:06 -050015217
15218 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15219 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15221 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15222 rsp_iocbq->iocb.ulpStatus,
15223 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015224}
15225
15226/**
James Smart6d368e52011-05-24 11:44:12 -040015227 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15228 * @phba: Pointer to HBA context object.
15229 * @xri: xri id in transaction.
15230 *
15231 * This function validates the xri maps to the known range of XRIs allocated an
15232 * used by the driver.
15233 **/
James Smart7851fe22011-07-22 18:36:52 -040015234uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015235lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15236 uint16_t xri)
15237{
James Smarta2fc4aef2014-09-03 12:57:55 -040015238 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015239
15240 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15241 if (xri == phba->sli4_hba.xri_ids[i])
15242 return i;
15243 }
15244 return NO_XRI;
15245}
15246
James Smart6d368e52011-05-24 11:44:12 -040015247/**
James Smart546fc852011-03-11 16:06:29 -050015248 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015249 * @phba: Pointer to HBA context object.
15250 * @fc_hdr: pointer to a FC frame header.
15251 *
James Smart546fc852011-03-11 16:06:29 -050015252 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015253 * event after aborting the sequence handling.
15254 **/
15255static void
James Smart6dd9e312013-01-03 15:43:37 -050015256lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15257 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015258{
James Smart6dd9e312013-01-03 15:43:37 -050015259 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015260 struct lpfc_iocbq *ctiocb = NULL;
15261 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015262 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015263 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015264 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015265 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015266
15267 if (!lpfc_is_link_up(phba))
15268 return;
15269
15270 sid = sli4_sid_from_fc_hdr(fc_hdr);
15271 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015272 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015273
James Smart6dd9e312013-01-03 15:43:37 -050015274 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015275 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015276 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15277 if (!ndlp) {
15278 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15279 "1268 Failed to allocate ndlp for "
15280 "oxid:x%x SID:x%x\n", oxid, sid);
15281 return;
15282 }
15283 lpfc_nlp_init(vport, ndlp, sid);
15284 /* Put ndlp onto pport node list */
15285 lpfc_enqueue_node(vport, ndlp);
15286 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15287 /* re-setup ndlp without removing from node list */
15288 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15289 if (!ndlp) {
15290 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15291 "3275 Failed to active ndlp found "
15292 "for oxid:x%x SID:x%x\n", oxid, sid);
15293 return;
15294 }
James Smart6669f9b2009-10-02 15:16:45 -040015295 }
15296
James Smart546fc852011-03-11 16:06:29 -050015297 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015298 ctiocb = lpfc_sli_get_iocbq(phba);
15299 if (!ctiocb)
15300 return;
15301
James Smart5ffc2662009-11-18 15:39:44 -050015302 /* Extract the F_CTL field from FC_HDR */
15303 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15304
James Smart6669f9b2009-10-02 15:16:45 -040015305 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015306 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015307 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015308 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15309 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15310 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15311
15312 /* Fill in the rest of iocb fields */
15313 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15314 icmd->ulpBdeCount = 0;
15315 icmd->ulpLe = 1;
15316 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015317 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015318 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015319
James Smart6669f9b2009-10-02 15:16:45 -040015320 ctiocb->iocb_cmpl = NULL;
15321 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015322 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015323 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015324 ctiocb->sli4_xritag = NO_XRI;
15325
James Smartee0f4fe2012-05-09 21:19:14 -040015326 if (fctl & FC_FC_EX_CTX)
15327 /* Exchange responder sent the abort so we
15328 * own the oxid.
15329 */
15330 xri = oxid;
15331 else
15332 xri = rxid;
15333 lxri = lpfc_sli4_xri_inrange(phba, xri);
15334 if (lxri != NO_XRI)
15335 lpfc_set_rrq_active(phba, ndlp, lxri,
15336 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015337 /* For BA_ABTS from exchange responder, if the logical xri with
15338 * the oxid maps to the FCP XRI range, the port no longer has
15339 * that exchange context, send a BLS_RJT. Override the IOCB for
15340 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015341 */
James Smart6dd9e312013-01-03 15:43:37 -050015342 if ((fctl & FC_FC_EX_CTX) &&
15343 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15344 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15345 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15346 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15347 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15348 }
15349
15350 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15351 * the driver no longer has that exchange, send a BLS_RJT. Override
15352 * the IOCB for a BA_RJT.
15353 */
15354 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015355 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15356 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15357 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15358 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15359 }
James Smart6669f9b2009-10-02 15:16:45 -040015360
James Smart5ffc2662009-11-18 15:39:44 -050015361 if (fctl & FC_FC_EX_CTX) {
15362 /* ABTS sent by responder to CT exchange, construction
15363 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15364 * field and RX_ID from ABTS for RX_ID field.
15365 */
James Smart546fc852011-03-11 16:06:29 -050015366 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015367 } else {
15368 /* ABTS sent by initiator to CT exchange, construction
15369 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015370 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015371 */
James Smart546fc852011-03-11 16:06:29 -050015372 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015373 }
James Smartf09c3ac2012-03-01 22:33:29 -050015374 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015375 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015376
James Smart546fc852011-03-11 16:06:29 -050015377 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015378 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15379 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15380 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015381
15382 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15383 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015384 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15385 "2925 Failed to issue CT ABTS RSP x%x on "
15386 "xri x%x, Data x%x\n",
15387 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15388 phba->link_state);
15389 lpfc_nlp_put(ndlp);
15390 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015391 lpfc_sli_release_iocbq(phba, ctiocb);
15392 }
James Smart6669f9b2009-10-02 15:16:45 -040015393}
15394
15395/**
15396 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15397 * @vport: Pointer to the vport on which this sequence was received
15398 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15399 *
15400 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15401 * receive sequence is only partially assembed by the driver, it shall abort
15402 * the partially assembled frames for the sequence. Otherwise, if the
15403 * unsolicited receive sequence has been completely assembled and passed to
15404 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15405 * unsolicited sequence has been aborted. After that, it will issue a basic
15406 * accept to accept the abort.
15407 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015408static void
James Smart6669f9b2009-10-02 15:16:45 -040015409lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15410 struct hbq_dmabuf *dmabuf)
15411{
15412 struct lpfc_hba *phba = vport->phba;
15413 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015414 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015415 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015416
James Smart6669f9b2009-10-02 15:16:45 -040015417 /* Make a copy of fc_hdr before the dmabuf being released */
15418 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015419 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015420
James Smart5ffc2662009-11-18 15:39:44 -050015421 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015422 /* ABTS by responder to exchange, no cleanup needed */
15423 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015424 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015425 /* ABTS by initiator to exchange, need to do cleanup */
15426 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15427 if (aborted == false)
15428 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015429 }
James Smart6dd9e312013-01-03 15:43:37 -050015430 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15431
15432 /* Respond with BA_ACC or BA_RJT accordingly */
15433 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015434}
15435
15436/**
James Smart4f774512009-05-22 14:52:35 -040015437 * lpfc_seq_complete - Indicates if a sequence is complete
15438 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15439 *
15440 * This function checks the sequence, starting with the frame described by
15441 * @dmabuf, to see if all the frames associated with this sequence are present.
15442 * the frames associated with this sequence are linked to the @dmabuf using the
15443 * dbuf list. This function looks for two major things. 1) That the first frame
15444 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15445 * set. 3) That there are no holes in the sequence count. The function will
15446 * return 1 when the sequence is complete, otherwise it will return 0.
15447 **/
15448static int
15449lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15450{
15451 struct fc_frame_header *hdr;
15452 struct lpfc_dmabuf *d_buf;
15453 struct hbq_dmabuf *seq_dmabuf;
15454 uint32_t fctl;
15455 int seq_count = 0;
15456
15457 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15458 /* make sure first fame of sequence has a sequence count of zero */
15459 if (hdr->fh_seq_cnt != seq_count)
15460 return 0;
15461 fctl = (hdr->fh_f_ctl[0] << 16 |
15462 hdr->fh_f_ctl[1] << 8 |
15463 hdr->fh_f_ctl[2]);
15464 /* If last frame of sequence we can return success. */
15465 if (fctl & FC_FC_END_SEQ)
15466 return 1;
15467 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15468 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15469 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15470 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015471 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015472 return 0;
15473 fctl = (hdr->fh_f_ctl[0] << 16 |
15474 hdr->fh_f_ctl[1] << 8 |
15475 hdr->fh_f_ctl[2]);
15476 /* If last frame of sequence we can return success. */
15477 if (fctl & FC_FC_END_SEQ)
15478 return 1;
15479 }
15480 return 0;
15481}
15482
15483/**
15484 * lpfc_prep_seq - Prep sequence for ULP processing
15485 * @vport: Pointer to the vport on which this sequence was received
15486 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15487 *
15488 * This function takes a sequence, described by a list of frames, and creates
15489 * a list of iocbq structures to describe the sequence. This iocbq list will be
15490 * used to issue to the generic unsolicited sequence handler. This routine
15491 * returns a pointer to the first iocbq in the list. If the function is unable
15492 * to allocate an iocbq then it throw out the received frames that were not
15493 * able to be described and return a pointer to the first iocbq. If unable to
15494 * allocate any iocbqs (including the first) this function will return NULL.
15495 **/
15496static struct lpfc_iocbq *
15497lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15498{
James Smart7851fe22011-07-22 18:36:52 -040015499 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015500 struct lpfc_dmabuf *d_buf, *n_buf;
15501 struct lpfc_iocbq *first_iocbq, *iocbq;
15502 struct fc_frame_header *fc_hdr;
15503 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015504 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015505 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015506
15507 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15508 /* remove from receive buffer list */
15509 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015510 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015511 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015512 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015513 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015514 /* Get an iocbq struct to fill in. */
15515 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15516 if (first_iocbq) {
15517 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015518 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015519 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015520
15521 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15522 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15523 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15524 first_iocbq->iocb.un.rcvels.parmRo =
15525 sli4_did_from_fc_hdr(fc_hdr);
15526 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15527 } else
15528 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015529 first_iocbq->iocb.ulpContext = NO_XRI;
15530 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15531 be16_to_cpu(fc_hdr->fh_ox_id);
15532 /* iocbq is prepped for internal consumption. Physical vpi. */
15533 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15534 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015535 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015536 tot_len = bf_get(lpfc_rcqe_length,
15537 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15538
James Smart4f774512009-05-22 14:52:35 -040015539 first_iocbq->context2 = &seq_dmabuf->dbuf;
15540 first_iocbq->context3 = NULL;
15541 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015542 if (tot_len > LPFC_DATA_BUF_SIZE)
15543 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015544 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015545 else
15546 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15547
James Smart4f774512009-05-22 14:52:35 -040015548 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015549
James Smart7851fe22011-07-22 18:36:52 -040015550 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015551 }
15552 iocbq = first_iocbq;
15553 /*
15554 * Each IOCBq can have two Buffers assigned, so go through the list
15555 * of buffers for this sequence and save two buffers in each IOCBq
15556 */
15557 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15558 if (!iocbq) {
15559 lpfc_in_buf_free(vport->phba, d_buf);
15560 continue;
15561 }
15562 if (!iocbq->context3) {
15563 iocbq->context3 = d_buf;
15564 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015565 /* We need to get the size out of the right CQE */
15566 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15567 len = bf_get(lpfc_rcqe_length,
15568 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015569 pbde = (struct ulp_bde64 *)
15570 &iocbq->iocb.unsli3.sli3Words[4];
15571 if (len > LPFC_DATA_BUF_SIZE)
15572 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15573 else
15574 pbde->tus.f.bdeSize = len;
15575
James Smart7851fe22011-07-22 18:36:52 -040015576 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15577 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015578 } else {
15579 iocbq = lpfc_sli_get_iocbq(vport->phba);
15580 if (!iocbq) {
15581 if (first_iocbq) {
15582 first_iocbq->iocb.ulpStatus =
15583 IOSTAT_FCP_RSP_ERROR;
15584 first_iocbq->iocb.un.ulpWord[4] =
15585 IOERR_NO_RESOURCES;
15586 }
15587 lpfc_in_buf_free(vport->phba, d_buf);
15588 continue;
15589 }
James Smart7851fe22011-07-22 18:36:52 -040015590 /* We need to get the size out of the right CQE */
15591 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15592 len = bf_get(lpfc_rcqe_length,
15593 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015594 iocbq->context2 = d_buf;
15595 iocbq->context3 = NULL;
15596 iocbq->iocb.ulpBdeCount = 1;
15597 if (len > LPFC_DATA_BUF_SIZE)
15598 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15599 LPFC_DATA_BUF_SIZE;
15600 else
15601 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15602
James Smart7851fe22011-07-22 18:36:52 -040015603 tot_len += len;
15604 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15605
James Smart4f774512009-05-22 14:52:35 -040015606 iocbq->iocb.un.rcvels.remoteID = sid;
15607 list_add_tail(&iocbq->list, &first_iocbq->list);
15608 }
15609 }
15610 return first_iocbq;
15611}
15612
James Smart6669f9b2009-10-02 15:16:45 -040015613static void
15614lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15615 struct hbq_dmabuf *seq_dmabuf)
15616{
15617 struct fc_frame_header *fc_hdr;
15618 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15619 struct lpfc_hba *phba = vport->phba;
15620
15621 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15622 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15623 if (!iocbq) {
15624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15625 "2707 Ring %d handler: Failed to allocate "
15626 "iocb Rctl x%x Type x%x received\n",
15627 LPFC_ELS_RING,
15628 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15629 return;
15630 }
15631 if (!lpfc_complete_unsol_iocb(phba,
15632 &phba->sli.ring[LPFC_ELS_RING],
15633 iocbq, fc_hdr->fh_r_ctl,
15634 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015636 "2540 Ring %d handler: unexpected Rctl "
15637 "x%x Type x%x received\n",
15638 LPFC_ELS_RING,
15639 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15640
15641 /* Free iocb created in lpfc_prep_seq */
15642 list_for_each_entry_safe(curr_iocb, next_iocb,
15643 &iocbq->list, list) {
15644 list_del_init(&curr_iocb->list);
15645 lpfc_sli_release_iocbq(phba, curr_iocb);
15646 }
15647 lpfc_sli_release_iocbq(phba, iocbq);
15648}
15649
James Smart4f774512009-05-22 14:52:35 -040015650/**
15651 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15652 * @phba: Pointer to HBA context object.
15653 *
15654 * This function is called with no lock held. This function processes all
15655 * the received buffers and gives it to upper layers when a received buffer
15656 * indicates that it is the final frame in the sequence. The interrupt
15657 * service routine processes received buffers at interrupt contexts and adds
15658 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15659 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15660 * appropriate receive function when the final frame in a sequence is received.
15661 **/
James Smart4d9ab992009-10-02 15:16:39 -040015662void
15663lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15664 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015665{
James Smart4d9ab992009-10-02 15:16:39 -040015666 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015667 struct fc_frame_header *fc_hdr;
15668 struct lpfc_vport *vport;
15669 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015670 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015671
James Smart4f774512009-05-22 14:52:35 -040015672 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015673 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15674 /* check to see if this a valid type of frame */
15675 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15676 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15677 return;
15678 }
James Smart7851fe22011-07-22 18:36:52 -040015679 if ((bf_get(lpfc_cqe_code,
15680 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15681 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15682 &dmabuf->cq_event.cqe.rcqe_cmpl);
15683 else
15684 fcfi = bf_get(lpfc_rcqe_fcf_id,
15685 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015686
James Smart4d9ab992009-10-02 15:16:39 -040015687 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015688 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015689 /* throw out the frame */
15690 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15691 return;
15692 }
James Smart939723a2012-05-09 21:19:03 -040015693
15694 /* d_id this frame is directed to */
15695 did = sli4_did_from_fc_hdr(fc_hdr);
15696
15697 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15698 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15699 (did != Fabric_DID)) {
15700 /*
15701 * Throw out the frame if we are not pt2pt.
15702 * The pt2pt protocol allows for discovery frames
15703 * to be received without a registered VPI.
15704 */
15705 if (!(vport->fc_flag & FC_PT2PT) ||
15706 (phba->link_state == LPFC_HBA_READY)) {
15707 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15708 return;
15709 }
15710 }
15711
James Smart6669f9b2009-10-02 15:16:45 -040015712 /* Handle the basic abort sequence (BA_ABTS) event */
15713 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15714 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15715 return;
15716 }
15717
James Smart4d9ab992009-10-02 15:16:39 -040015718 /* Link this frame */
15719 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15720 if (!seq_dmabuf) {
15721 /* unable to add frame to vport - throw it out */
15722 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15723 return;
15724 }
15725 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015726 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015727 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015728
James Smart6669f9b2009-10-02 15:16:45 -040015729 /* Send the complete sequence to the upper layer protocol */
15730 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015731}
James Smart6fb120a2009-05-22 14:52:59 -040015732
15733/**
15734 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15735 * @phba: pointer to lpfc hba data structure.
15736 *
15737 * This routine is invoked to post rpi header templates to the
15738 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015739 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15740 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015741 *
15742 * This routine does not require any locks. It's usage is expected
15743 * to be driver load or reset recovery when the driver is
15744 * sequential.
15745 *
15746 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015747 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015748 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015749 * When this error occurs, the driver is not guaranteed
15750 * to have any rpi regions posted to the device and
15751 * must either attempt to repost the regions or take a
15752 * fatal error.
15753 **/
15754int
15755lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15756{
15757 struct lpfc_rpi_hdr *rpi_page;
15758 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015759 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015760
James Smart6d368e52011-05-24 11:44:12 -040015761 /* SLI4 ports that support extents do not require RPI headers. */
15762 if (!phba->sli4_hba.rpi_hdrs_in_use)
15763 goto exit;
15764 if (phba->sli4_hba.extents_in_use)
15765 return -EIO;
15766
James Smart6fb120a2009-05-22 14:52:59 -040015767 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015768 /*
15769 * Assign the rpi headers a physical rpi only if the driver
15770 * has not initialized those resources. A port reset only
15771 * needs the headers posted.
15772 */
15773 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15774 LPFC_RPI_RSRC_RDY)
15775 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15776
James Smart6fb120a2009-05-22 14:52:59 -040015777 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15778 if (rc != MBX_SUCCESS) {
15779 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15780 "2008 Error %d posting all rpi "
15781 "headers\n", rc);
15782 rc = -EIO;
15783 break;
15784 }
15785 }
15786
James Smart6d368e52011-05-24 11:44:12 -040015787 exit:
15788 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15789 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015790 return rc;
15791}
15792
15793/**
15794 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15795 * @phba: pointer to lpfc hba data structure.
15796 * @rpi_page: pointer to the rpi memory region.
15797 *
15798 * This routine is invoked to post a single rpi header to the
15799 * HBA consistent with the SLI-4 interface spec. This memory region
15800 * maps up to 64 rpi context regions.
15801 *
15802 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015803 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015804 * -ENOMEM - No available memory
15805 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015806 **/
15807int
15808lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15809{
15810 LPFC_MBOXQ_t *mboxq;
15811 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15812 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015813 uint32_t shdr_status, shdr_add_status;
15814 union lpfc_sli4_cfg_shdr *shdr;
15815
James Smart6d368e52011-05-24 11:44:12 -040015816 /* SLI4 ports that support extents do not require RPI headers. */
15817 if (!phba->sli4_hba.rpi_hdrs_in_use)
15818 return rc;
15819 if (phba->sli4_hba.extents_in_use)
15820 return -EIO;
15821
James Smart6fb120a2009-05-22 14:52:59 -040015822 /* The port is notified of the header region via a mailbox command. */
15823 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15824 if (!mboxq) {
15825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15826 "2001 Unable to allocate memory for issuing "
15827 "SLI_CONFIG_SPECIAL mailbox command\n");
15828 return -ENOMEM;
15829 }
15830
15831 /* Post all rpi memory regions to the port. */
15832 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015833 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15834 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15835 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015836 sizeof(struct lpfc_sli4_cfg_mhdr),
15837 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015838
15839
15840 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015841 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15842 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015843 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15844 hdr_tmpl, rpi_page->page_count);
15845
James Smart6fb120a2009-05-22 14:52:59 -040015846 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15847 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015848 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015849 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15850 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15851 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15852 if (rc != MBX_TIMEOUT)
15853 mempool_free(mboxq, phba->mbox_mem_pool);
15854 if (shdr_status || shdr_add_status || rc) {
15855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15856 "2514 POST_RPI_HDR mailbox failed with "
15857 "status x%x add_status x%x, mbx status x%x\n",
15858 shdr_status, shdr_add_status, rc);
15859 rc = -ENXIO;
15860 }
15861 return rc;
15862}
15863
15864/**
15865 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15866 * @phba: pointer to lpfc hba data structure.
15867 *
15868 * This routine is invoked to post rpi header templates to the
15869 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015870 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15871 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015872 *
15873 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015874 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015875 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15876 **/
15877int
15878lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15879{
James Smart6d368e52011-05-24 11:44:12 -040015880 unsigned long rpi;
15881 uint16_t max_rpi, rpi_limit;
15882 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015883 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015884 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015885
James Smart6fb120a2009-05-22 14:52:59 -040015886 /*
James Smart6d368e52011-05-24 11:44:12 -040015887 * Fetch the next logical rpi. Because this index is logical,
15888 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015889 */
James Smart4902b382013-10-10 12:20:35 -040015890 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015891 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15892 rpi_limit = phba->sli4_hba.next_rpi;
15893
James Smart6d368e52011-05-24 11:44:12 -040015894 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15895 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015896 rpi = LPFC_RPI_ALLOC_ERROR;
15897 else {
15898 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15899 phba->sli4_hba.max_cfg_param.rpi_used++;
15900 phba->sli4_hba.rpi_count++;
15901 }
James Smartbe6bb942015-04-07 15:07:22 -040015902 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15903 "0001 rpi:%x max:%x lim:%x\n",
15904 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015905
15906 /*
15907 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015908 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015909 */
15910 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15911 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015912 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015913 return rpi;
15914 }
15915
15916 /*
James Smart6d368e52011-05-24 11:44:12 -040015917 * RPI header postings are not required for SLI4 ports capable of
15918 * extents.
15919 */
15920 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015921 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015922 return rpi;
15923 }
15924
15925 /*
James Smart6fb120a2009-05-22 14:52:59 -040015926 * If the driver is running low on rpi resources, allocate another
15927 * page now. Note that the next_rpi value is used because
15928 * it represents how many are actually in use whereas max_rpi notes
15929 * how many are supported max by the device.
15930 */
James Smart6d368e52011-05-24 11:44:12 -040015931 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015932 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015933 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15934 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15935 if (!rpi_hdr) {
15936 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15937 "2002 Error Could not grow rpi "
15938 "count\n");
15939 } else {
James Smart6d368e52011-05-24 11:44:12 -040015940 lrpi = rpi_hdr->start_rpi;
15941 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015942 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15943 }
15944 }
15945
15946 return rpi;
15947}
15948
15949/**
15950 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15951 * @phba: pointer to lpfc hba data structure.
15952 *
15953 * This routine is invoked to release an rpi to the pool of
15954 * available rpis maintained by the driver.
15955 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015956static void
James Smartd7c47992010-06-08 18:31:54 -040015957__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15958{
15959 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15960 phba->sli4_hba.rpi_count--;
15961 phba->sli4_hba.max_cfg_param.rpi_used--;
15962 }
15963}
15964
15965/**
15966 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15967 * @phba: pointer to lpfc hba data structure.
15968 *
15969 * This routine is invoked to release an rpi to the pool of
15970 * available rpis maintained by the driver.
15971 **/
15972void
James Smart6fb120a2009-05-22 14:52:59 -040015973lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15974{
15975 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015976 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015977 spin_unlock_irq(&phba->hbalock);
15978}
15979
15980/**
15981 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15982 * @phba: pointer to lpfc hba data structure.
15983 *
15984 * This routine is invoked to remove the memory region that
15985 * provided rpi via a bitmask.
15986 **/
15987void
15988lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15989{
15990 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040015991 kfree(phba->sli4_hba.rpi_ids);
15992 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040015993}
15994
15995/**
15996 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15997 * @phba: pointer to lpfc hba data structure.
15998 *
15999 * This routine is invoked to remove the memory region that
16000 * provided rpi via a bitmask.
16001 **/
16002int
James Smart6b5151f2012-01-18 16:24:06 -050016003lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16004 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016005{
16006 LPFC_MBOXQ_t *mboxq;
16007 struct lpfc_hba *phba = ndlp->phba;
16008 int rc;
16009
16010 /* The port is notified of the header region via a mailbox command. */
16011 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16012 if (!mboxq)
16013 return -ENOMEM;
16014
16015 /* Post all rpi memory regions to the port. */
16016 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016017 if (cmpl) {
16018 mboxq->mbox_cmpl = cmpl;
16019 mboxq->context1 = arg;
16020 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016021 } else
16022 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016023 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016024 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16025 if (rc == MBX_NOT_FINISHED) {
16026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16027 "2010 Resume RPI Mailbox failed "
16028 "status %d, mbxStatus x%x\n", rc,
16029 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16030 mempool_free(mboxq, phba->mbox_mem_pool);
16031 return -EIO;
16032 }
16033 return 0;
16034}
16035
16036/**
16037 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016038 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016039 *
James Smart76a95d72010-11-20 23:11:48 -050016040 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016041 *
16042 * Returns:
16043 * 0 success
16044 * -Evalue otherwise
16045 **/
16046int
James Smart76a95d72010-11-20 23:11:48 -050016047lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016048{
16049 LPFC_MBOXQ_t *mboxq;
16050 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016051 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016052 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016053 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016054 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16055 if (!mboxq)
16056 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016057 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016058 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016059 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016060 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016061 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016062 "2022 INIT VPI Mailbox failed "
16063 "status %d, mbxStatus x%x\n", rc,
16064 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016065 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016066 }
James Smart6a9c52c2009-10-02 15:16:51 -040016067 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016068 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016069
16070 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016071}
16072
16073/**
16074 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16075 * @phba: pointer to lpfc hba data structure.
16076 * @mboxq: Pointer to mailbox object.
16077 *
16078 * This routine is invoked to manually add a single FCF record. The caller
16079 * must pass a completely initialized FCF_Record. This routine takes
16080 * care of the nonembedded mailbox operations.
16081 **/
16082static void
16083lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16084{
16085 void *virt_addr;
16086 union lpfc_sli4_cfg_shdr *shdr;
16087 uint32_t shdr_status, shdr_add_status;
16088
16089 virt_addr = mboxq->sge_array->addr[0];
16090 /* The IOCTL status is embedded in the mailbox subheader. */
16091 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16092 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16093 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16094
16095 if ((shdr_status || shdr_add_status) &&
16096 (shdr_status != STATUS_FCF_IN_USE))
16097 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16098 "2558 ADD_FCF_RECORD mailbox failed with "
16099 "status x%x add_status x%x\n",
16100 shdr_status, shdr_add_status);
16101
16102 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16103}
16104
16105/**
16106 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16107 * @phba: pointer to lpfc hba data structure.
16108 * @fcf_record: pointer to the initialized fcf record to add.
16109 *
16110 * This routine is invoked to manually add a single FCF record. The caller
16111 * must pass a completely initialized FCF_Record. This routine takes
16112 * care of the nonembedded mailbox operations.
16113 **/
16114int
16115lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16116{
16117 int rc = 0;
16118 LPFC_MBOXQ_t *mboxq;
16119 uint8_t *bytep;
16120 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016121 struct lpfc_mbx_sge sge;
16122 uint32_t alloc_len, req_len;
16123 uint32_t fcfindex;
16124
16125 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16126 if (!mboxq) {
16127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16128 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16129 return -ENOMEM;
16130 }
16131
16132 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16133 sizeof(uint32_t);
16134
16135 /* Allocate DMA memory and set up the non-embedded mailbox command */
16136 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16137 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16138 req_len, LPFC_SLI4_MBX_NEMBED);
16139 if (alloc_len < req_len) {
16140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16141 "2523 Allocated DMA memory size (x%x) is "
16142 "less than the requested DMA memory "
16143 "size (x%x)\n", alloc_len, req_len);
16144 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16145 return -ENOMEM;
16146 }
16147
16148 /*
16149 * Get the first SGE entry from the non-embedded DMA memory. This
16150 * routine only uses a single SGE.
16151 */
16152 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016153 virt_addr = mboxq->sge_array->addr[0];
16154 /*
16155 * Configure the FCF record for FCFI 0. This is the driver's
16156 * hardcoded default and gets used in nonFIP mode.
16157 */
16158 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16159 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16160 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16161
16162 /*
16163 * Copy the fcf_index and the FCF Record Data. The data starts after
16164 * the FCoE header plus word10. The data copy needs to be endian
16165 * correct.
16166 */
16167 bytep += sizeof(uint32_t);
16168 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16169 mboxq->vport = phba->pport;
16170 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16171 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16172 if (rc == MBX_NOT_FINISHED) {
16173 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16174 "2515 ADD_FCF_RECORD mailbox failed with "
16175 "status 0x%x\n", rc);
16176 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16177 rc = -EIO;
16178 } else
16179 rc = 0;
16180
16181 return rc;
16182}
16183
16184/**
16185 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16186 * @phba: pointer to lpfc hba data structure.
16187 * @fcf_record: pointer to the fcf record to write the default data.
16188 * @fcf_index: FCF table entry index.
16189 *
16190 * This routine is invoked to build the driver's default FCF record. The
16191 * values used are hardcoded. This routine handles memory initialization.
16192 *
16193 **/
16194void
16195lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16196 struct fcf_record *fcf_record,
16197 uint16_t fcf_index)
16198{
16199 memset(fcf_record, 0, sizeof(struct fcf_record));
16200 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16201 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16202 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16203 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16204 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16205 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16206 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16207 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16208 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16209 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16210 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16211 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16212 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016213 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016214 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16215 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16216 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16217 /* Set the VLAN bit map */
16218 if (phba->valid_vlan) {
16219 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16220 = 1 << (phba->vlan_id % 8);
16221 }
16222}
16223
16224/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016225 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016226 * @phba: pointer to lpfc hba data structure.
16227 * @fcf_index: FCF table entry offset.
16228 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016229 * This routine is invoked to scan the entire FCF table by reading FCF
16230 * record and processing it one at a time starting from the @fcf_index
16231 * for initial FCF discovery or fast FCF failover rediscovery.
16232 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016233 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016234 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016235 **/
16236int
James Smart0c9ab6f2010-02-26 14:15:57 -050016237lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016238{
16239 int rc = 0, error;
16240 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016241
James Smart32b97932009-07-19 10:01:21 -040016242 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016243 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016244 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16245 if (!mboxq) {
16246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16247 "2000 Failed to allocate mbox for "
16248 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016249 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016250 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016251 }
James Smartecfd03c2010-02-12 14:41:27 -050016252 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016253 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016254 if (rc) {
16255 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016256 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016257 }
James Smartecfd03c2010-02-12 14:41:27 -050016258 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016259 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016260 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016261
16262 spin_lock_irq(&phba->hbalock);
16263 phba->hba_flag |= FCF_TS_INPROG;
16264 spin_unlock_irq(&phba->hbalock);
16265
James Smart6fb120a2009-05-22 14:52:59 -040016266 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016267 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016268 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016269 else {
James Smart38b92ef2010-08-04 16:11:39 -040016270 /* Reset eligible FCF count for new scan */
16271 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016272 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016273 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016274 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016275fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016276 if (error) {
16277 if (mboxq)
16278 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016279 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016280 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016281 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016282 spin_unlock_irq(&phba->hbalock);
16283 }
James Smart6fb120a2009-05-22 14:52:59 -040016284 return error;
16285}
James Smarta0c87cb2009-07-19 10:01:10 -040016286
16287/**
James Smarta93ff372010-10-22 11:06:08 -040016288 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016289 * @phba: pointer to lpfc hba data structure.
16290 * @fcf_index: FCF table entry offset.
16291 *
16292 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016293 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016294 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016295 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016296 * otherwise.
16297 **/
16298int
16299lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16300{
16301 int rc = 0, error;
16302 LPFC_MBOXQ_t *mboxq;
16303
16304 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16305 if (!mboxq) {
16306 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16307 "2763 Failed to allocate mbox for "
16308 "READ_FCF cmd\n");
16309 error = -ENOMEM;
16310 goto fail_fcf_read;
16311 }
16312 /* Construct the read FCF record mailbox command */
16313 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16314 if (rc) {
16315 error = -EINVAL;
16316 goto fail_fcf_read;
16317 }
16318 /* Issue the mailbox command asynchronously */
16319 mboxq->vport = phba->pport;
16320 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16321 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16322 if (rc == MBX_NOT_FINISHED)
16323 error = -EIO;
16324 else
16325 error = 0;
16326
16327fail_fcf_read:
16328 if (error && mboxq)
16329 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16330 return error;
16331}
16332
16333/**
16334 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16335 * @phba: pointer to lpfc hba data structure.
16336 * @fcf_index: FCF table entry offset.
16337 *
16338 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016339 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016340 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016341 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016342 * otherwise.
16343 **/
16344int
16345lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16346{
16347 int rc = 0, error;
16348 LPFC_MBOXQ_t *mboxq;
16349
16350 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16351 if (!mboxq) {
16352 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16353 "2758 Failed to allocate mbox for "
16354 "READ_FCF cmd\n");
16355 error = -ENOMEM;
16356 goto fail_fcf_read;
16357 }
16358 /* Construct the read FCF record mailbox command */
16359 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16360 if (rc) {
16361 error = -EINVAL;
16362 goto fail_fcf_read;
16363 }
16364 /* Issue the mailbox command asynchronously */
16365 mboxq->vport = phba->pport;
16366 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16367 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16368 if (rc == MBX_NOT_FINISHED)
16369 error = -EIO;
16370 else
16371 error = 0;
16372
16373fail_fcf_read:
16374 if (error && mboxq)
16375 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16376 return error;
16377}
16378
16379/**
James Smartf5cb5302015-12-16 18:11:52 -050016380 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016381 * phba pointer to the lpfc_hba struct for this port.
16382 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16383 * routine when the rr_bmask is empty. The FCF indecies are put into the
16384 * rr_bmask based on their priority level. Starting from the highest priority
16385 * to the lowest. The most likely FCF candidate will be in the highest
16386 * priority group. When this routine is called it searches the fcf_pri list for
16387 * next lowest priority group and repopulates the rr_bmask with only those
16388 * fcf_indexes.
16389 * returns:
16390 * 1=success 0=failure
16391 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016392static int
James Smart7d791df2011-07-22 18:37:52 -040016393lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16394{
16395 uint16_t next_fcf_pri;
16396 uint16_t last_index;
16397 struct lpfc_fcf_pri *fcf_pri;
16398 int rc;
16399 int ret = 0;
16400
16401 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16402 LPFC_SLI4_FCF_TBL_INDX_MAX);
16403 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16404 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016405
16406 /* Verify the priority list has 2 or more entries */
16407 spin_lock_irq(&phba->hbalock);
16408 if (list_empty(&phba->fcf.fcf_pri_list) ||
16409 list_is_singular(&phba->fcf.fcf_pri_list)) {
16410 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016411 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16412 "3061 Last IDX %d\n", last_index);
16413 return 0; /* Empty rr list */
16414 }
James Smart25626692013-03-01 16:36:54 -050016415 spin_unlock_irq(&phba->hbalock);
16416
James Smart7d791df2011-07-22 18:37:52 -040016417 next_fcf_pri = 0;
16418 /*
16419 * Clear the rr_bmask and set all of the bits that are at this
16420 * priority.
16421 */
16422 memset(phba->fcf.fcf_rr_bmask, 0,
16423 sizeof(*phba->fcf.fcf_rr_bmask));
16424 spin_lock_irq(&phba->hbalock);
16425 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16426 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16427 continue;
16428 /*
16429 * the 1st priority that has not FLOGI failed
16430 * will be the highest.
16431 */
16432 if (!next_fcf_pri)
16433 next_fcf_pri = fcf_pri->fcf_rec.priority;
16434 spin_unlock_irq(&phba->hbalock);
16435 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16436 rc = lpfc_sli4_fcf_rr_index_set(phba,
16437 fcf_pri->fcf_rec.fcf_index);
16438 if (rc)
16439 return 0;
16440 }
16441 spin_lock_irq(&phba->hbalock);
16442 }
16443 /*
16444 * if next_fcf_pri was not set above and the list is not empty then
16445 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016446 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016447 */
16448 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16449 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16450 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16451 /*
16452 * the 1st priority that has not FLOGI failed
16453 * will be the highest.
16454 */
16455 if (!next_fcf_pri)
16456 next_fcf_pri = fcf_pri->fcf_rec.priority;
16457 spin_unlock_irq(&phba->hbalock);
16458 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16459 rc = lpfc_sli4_fcf_rr_index_set(phba,
16460 fcf_pri->fcf_rec.fcf_index);
16461 if (rc)
16462 return 0;
16463 }
16464 spin_lock_irq(&phba->hbalock);
16465 }
16466 } else
16467 ret = 1;
16468 spin_unlock_irq(&phba->hbalock);
16469
16470 return ret;
16471}
16472/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016473 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16474 * @phba: pointer to lpfc hba data structure.
16475 *
16476 * This routine is to get the next eligible FCF record index in a round
16477 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016478 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016479 * shall be returned, otherwise, the next eligible FCF record's index
16480 * shall be returned.
16481 **/
16482uint16_t
16483lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16484{
16485 uint16_t next_fcf_index;
16486
James Smart421c6622013-01-03 15:44:16 -050016487initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016488 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016489 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16490
James Smart7d791df2011-07-22 18:37:52 -040016491next_priority:
James Smart421c6622013-01-03 15:44:16 -050016492 /* Determine the next fcf index to check */
16493 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016494 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16495 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016496 next_fcf_index);
16497
James Smart0c9ab6f2010-02-26 14:15:57 -050016498 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016499 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16500 /*
16501 * If we have wrapped then we need to clear the bits that
16502 * have been tested so that we can detect when we should
16503 * change the priority level.
16504 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016505 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16506 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016507 }
16508
James Smart0c9ab6f2010-02-26 14:15:57 -050016509
James Smart3804dc82010-07-14 15:31:37 -040016510 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016511 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16512 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16513 /*
16514 * If next fcf index is not found check if there are lower
16515 * Priority level fcf's in the fcf_priority list.
16516 * Set up the rr_bmask with all of the avaiable fcf bits
16517 * at that level and continue the selection process.
16518 */
16519 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016520 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016521 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16522 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016523 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16524 return LPFC_FCOE_FCF_NEXT_NONE;
16525 else {
16526 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16527 "3063 Only FCF available idx %d, flag %x\n",
16528 next_fcf_index,
16529 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16530 return next_fcf_index;
16531 }
James Smart3804dc82010-07-14 15:31:37 -040016532 }
16533
James Smart7d791df2011-07-22 18:37:52 -040016534 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16535 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016536 LPFC_FCF_FLOGI_FAILED) {
16537 if (list_is_singular(&phba->fcf.fcf_pri_list))
16538 return LPFC_FCOE_FCF_NEXT_NONE;
16539
James Smart7d791df2011-07-22 18:37:52 -040016540 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016541 }
James Smart7d791df2011-07-22 18:37:52 -040016542
James Smart3804dc82010-07-14 15:31:37 -040016543 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016544 "2845 Get next roundrobin failover FCF (x%x)\n",
16545 next_fcf_index);
16546
James Smart0c9ab6f2010-02-26 14:15:57 -050016547 return next_fcf_index;
16548}
16549
16550/**
16551 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16552 * @phba: pointer to lpfc hba data structure.
16553 *
16554 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016555 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016556 * does not go beyond the range of the driver allocated bmask dimension
16557 * before setting the bit.
16558 *
16559 * Returns 0 if the index bit successfully set, otherwise, it returns
16560 * -EINVAL.
16561 **/
16562int
16563lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16564{
16565 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16566 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016567 "2610 FCF (x%x) reached driver's book "
16568 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016569 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16570 return -EINVAL;
16571 }
16572 /* Set the eligible FCF record index bmask */
16573 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16574
James Smart3804dc82010-07-14 15:31:37 -040016575 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016576 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016577 "bmask\n", fcf_index);
16578
James Smart0c9ab6f2010-02-26 14:15:57 -050016579 return 0;
16580}
16581
16582/**
James Smart3804dc82010-07-14 15:31:37 -040016583 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016584 * @phba: pointer to lpfc hba data structure.
16585 *
16586 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016587 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016588 * does not go beyond the range of the driver allocated bmask dimension
16589 * before clearing the bit.
16590 **/
16591void
16592lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16593{
James Smart9a803a72013-09-06 12:17:56 -040016594 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016595 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16596 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016597 "2762 FCF (x%x) reached driver's book "
16598 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016599 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16600 return;
16601 }
16602 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016603 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016604 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16605 list) {
James Smart7d791df2011-07-22 18:37:52 -040016606 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16607 list_del_init(&fcf_pri->list);
16608 break;
16609 }
16610 }
16611 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016612 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016613
16614 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016615 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016616 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016617}
16618
16619/**
James Smartecfd03c2010-02-12 14:41:27 -050016620 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16621 * @phba: pointer to lpfc hba data structure.
16622 *
16623 * This routine is the completion routine for the rediscover FCF table mailbox
16624 * command. If the mailbox command returned failure, it will try to stop the
16625 * FCF rediscover wait timer.
16626 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016627static void
James Smartecfd03c2010-02-12 14:41:27 -050016628lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16629{
16630 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16631 uint32_t shdr_status, shdr_add_status;
16632
16633 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16634
16635 shdr_status = bf_get(lpfc_mbox_hdr_status,
16636 &redisc_fcf->header.cfg_shdr.response);
16637 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16638 &redisc_fcf->header.cfg_shdr.response);
16639 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016640 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016641 "2746 Requesting for FCF rediscovery failed "
16642 "status x%x add_status x%x\n",
16643 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016644 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016645 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016646 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016647 spin_unlock_irq(&phba->hbalock);
16648 /*
16649 * CVL event triggered FCF rediscover request failed,
16650 * last resort to re-try current registered FCF entry.
16651 */
16652 lpfc_retry_pport_discovery(phba);
16653 } else {
16654 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016655 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016656 spin_unlock_irq(&phba->hbalock);
16657 /*
16658 * DEAD FCF event triggered FCF rediscover request
16659 * failed, last resort to fail over as a link down
16660 * to FCF registration.
16661 */
16662 lpfc_sli4_fcf_dead_failthrough(phba);
16663 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016664 } else {
16665 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016666 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016667 /*
16668 * Start FCF rediscovery wait timer for pending FCF
16669 * before rescan FCF record table.
16670 */
16671 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016672 }
James Smartecfd03c2010-02-12 14:41:27 -050016673
16674 mempool_free(mbox, phba->mbox_mem_pool);
16675}
16676
16677/**
James Smart3804dc82010-07-14 15:31:37 -040016678 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016679 * @phba: pointer to lpfc hba data structure.
16680 *
16681 * This routine is invoked to request for rediscovery of the entire FCF table
16682 * by the port.
16683 **/
16684int
16685lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16686{
16687 LPFC_MBOXQ_t *mbox;
16688 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16689 int rc, length;
16690
James Smart0c9ab6f2010-02-26 14:15:57 -050016691 /* Cancel retry delay timers to all vports before FCF rediscover */
16692 lpfc_cancel_all_vport_retry_delay_timer(phba);
16693
James Smartecfd03c2010-02-12 14:41:27 -050016694 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16695 if (!mbox) {
16696 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16697 "2745 Failed to allocate mbox for "
16698 "requesting FCF rediscover.\n");
16699 return -ENOMEM;
16700 }
16701
16702 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16703 sizeof(struct lpfc_sli4_cfg_mhdr));
16704 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16705 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16706 length, LPFC_SLI4_MBX_EMBED);
16707
16708 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16709 /* Set count to 0 for invalidating the entire FCF database */
16710 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16711
16712 /* Issue the mailbox command asynchronously */
16713 mbox->vport = phba->pport;
16714 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16715 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16716
16717 if (rc == MBX_NOT_FINISHED) {
16718 mempool_free(mbox, phba->mbox_mem_pool);
16719 return -EIO;
16720 }
16721 return 0;
16722}
16723
16724/**
James Smartfc2b9892010-02-26 14:15:29 -050016725 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16726 * @phba: pointer to lpfc hba data structure.
16727 *
16728 * This function is the failover routine as a last resort to the FCF DEAD
16729 * event when driver failed to perform fast FCF failover.
16730 **/
16731void
16732lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16733{
16734 uint32_t link_state;
16735
16736 /*
16737 * Last resort as FCF DEAD event failover will treat this as
16738 * a link down, but save the link state because we don't want
16739 * it to be changed to Link Down unless it is already down.
16740 */
16741 link_state = phba->link_state;
16742 lpfc_linkdown(phba);
16743 phba->link_state = link_state;
16744
16745 /* Unregister FCF if no devices connected to it */
16746 lpfc_unregister_unused_fcf(phba);
16747}
16748
16749/**
James Smart026abb82011-12-13 13:20:45 -050016750 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016751 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016752 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016753 *
James Smart026abb82011-12-13 13:20:45 -050016754 * This function gets SLI3 port configure region 23 data through memory dump
16755 * mailbox command. When it successfully retrieves data, the size of the data
16756 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016757 **/
James Smart026abb82011-12-13 13:20:45 -050016758static uint32_t
16759lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016760{
16761 LPFC_MBOXQ_t *pmb = NULL;
16762 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016763 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016764 int rc;
16765
James Smart026abb82011-12-13 13:20:45 -050016766 if (!rgn23_data)
16767 return 0;
16768
James Smarta0c87cb2009-07-19 10:01:10 -040016769 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16770 if (!pmb) {
16771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016772 "2600 failed to allocate mailbox memory\n");
16773 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016774 }
16775 mb = &pmb->u.mb;
16776
James Smarta0c87cb2009-07-19 10:01:10 -040016777 do {
16778 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16779 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16780
16781 if (rc != MBX_SUCCESS) {
16782 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016783 "2601 failed to read config "
16784 "region 23, rc 0x%x Status 0x%x\n",
16785 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016786 mb->un.varDmp.word_cnt = 0;
16787 }
16788 /*
16789 * dump mem may return a zero when finished or we got a
16790 * mailbox error, either way we are done.
16791 */
16792 if (mb->un.varDmp.word_cnt == 0)
16793 break;
16794 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16795 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16796
16797 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016798 rgn23_data + offset,
16799 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016800 offset += mb->un.varDmp.word_cnt;
16801 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16802
James Smart026abb82011-12-13 13:20:45 -050016803 mempool_free(pmb, phba->mbox_mem_pool);
16804 return offset;
16805}
16806
16807/**
16808 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16809 * @phba: pointer to lpfc hba data structure.
16810 * @rgn23_data: pointer to configure region 23 data.
16811 *
16812 * This function gets SLI4 port configure region 23 data through memory dump
16813 * mailbox command. When it successfully retrieves data, the size of the data
16814 * will be returned, otherwise, 0 will be returned.
16815 **/
16816static uint32_t
16817lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16818{
16819 LPFC_MBOXQ_t *mboxq = NULL;
16820 struct lpfc_dmabuf *mp = NULL;
16821 struct lpfc_mqe *mqe;
16822 uint32_t data_length = 0;
16823 int rc;
16824
16825 if (!rgn23_data)
16826 return 0;
16827
16828 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16829 if (!mboxq) {
16830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16831 "3105 failed to allocate mailbox memory\n");
16832 return 0;
16833 }
16834
16835 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16836 goto out;
16837 mqe = &mboxq->u.mqe;
16838 mp = (struct lpfc_dmabuf *) mboxq->context1;
16839 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16840 if (rc)
16841 goto out;
16842 data_length = mqe->un.mb_words[5];
16843 if (data_length == 0)
16844 goto out;
16845 if (data_length > DMP_RGN23_SIZE) {
16846 data_length = 0;
16847 goto out;
16848 }
16849 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16850out:
16851 mempool_free(mboxq, phba->mbox_mem_pool);
16852 if (mp) {
16853 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16854 kfree(mp);
16855 }
16856 return data_length;
16857}
16858
16859/**
16860 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16861 * @phba: pointer to lpfc hba data structure.
16862 *
16863 * This function read region 23 and parse TLV for port status to
16864 * decide if the user disaled the port. If the TLV indicates the
16865 * port is disabled, the hba_flag is set accordingly.
16866 **/
16867void
16868lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16869{
16870 uint8_t *rgn23_data = NULL;
16871 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16872 uint32_t offset = 0;
16873
16874 /* Get adapter Region 23 data */
16875 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16876 if (!rgn23_data)
16877 goto out;
16878
16879 if (phba->sli_rev < LPFC_SLI_REV4)
16880 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16881 else {
16882 if_type = bf_get(lpfc_sli_intf_if_type,
16883 &phba->sli4_hba.sli_intf);
16884 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16885 goto out;
16886 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16887 }
James Smarta0c87cb2009-07-19 10:01:10 -040016888
16889 if (!data_size)
16890 goto out;
16891
16892 /* Check the region signature first */
16893 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16895 "2619 Config region 23 has bad signature\n");
16896 goto out;
16897 }
16898 offset += 4;
16899
16900 /* Check the data structure version */
16901 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16903 "2620 Config region 23 has bad version\n");
16904 goto out;
16905 }
16906 offset += 4;
16907
16908 /* Parse TLV entries in the region */
16909 while (offset < data_size) {
16910 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16911 break;
16912 /*
16913 * If the TLV is not driver specific TLV or driver id is
16914 * not linux driver id, skip the record.
16915 */
16916 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16917 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16918 (rgn23_data[offset + 3] != 0)) {
16919 offset += rgn23_data[offset + 1] * 4 + 4;
16920 continue;
16921 }
16922
16923 /* Driver found a driver specific TLV in the config region */
16924 sub_tlv_len = rgn23_data[offset + 1] * 4;
16925 offset += 4;
16926 tlv_offset = 0;
16927
16928 /*
16929 * Search for configured port state sub-TLV.
16930 */
16931 while ((offset < data_size) &&
16932 (tlv_offset < sub_tlv_len)) {
16933 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16934 offset += 4;
16935 tlv_offset += 4;
16936 break;
16937 }
16938 if (rgn23_data[offset] != PORT_STE_TYPE) {
16939 offset += rgn23_data[offset + 1] * 4 + 4;
16940 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16941 continue;
16942 }
16943
16944 /* This HBA contains PORT_STE configured */
16945 if (!rgn23_data[offset + 2])
16946 phba->hba_flag |= LINK_DISABLED;
16947
16948 goto out;
16949 }
16950 }
James Smart026abb82011-12-13 13:20:45 -050016951
James Smarta0c87cb2009-07-19 10:01:10 -040016952out:
James Smarta0c87cb2009-07-19 10:01:10 -040016953 kfree(rgn23_data);
16954 return;
16955}
James Smart695a8142010-01-26 23:08:03 -050016956
16957/**
James Smart52d52442011-05-24 11:42:45 -040016958 * lpfc_wr_object - write an object to the firmware
16959 * @phba: HBA structure that indicates port to create a queue on.
16960 * @dmabuf_list: list of dmabufs to write to the port.
16961 * @size: the total byte value of the objects to write to the port.
16962 * @offset: the current offset to be used to start the transfer.
16963 *
16964 * This routine will create a wr_object mailbox command to send to the port.
16965 * the mailbox command will be constructed using the dma buffers described in
16966 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16967 * BDEs that the imbedded mailbox can support. The @offset variable will be
16968 * used to indicate the starting offset of the transfer and will also return
16969 * the offset after the write object mailbox has completed. @size is used to
16970 * determine the end of the object and whether the eof bit should be set.
16971 *
16972 * Return 0 is successful and offset will contain the the new offset to use
16973 * for the next write.
16974 * Return negative value for error cases.
16975 **/
16976int
16977lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16978 uint32_t size, uint32_t *offset)
16979{
16980 struct lpfc_mbx_wr_object *wr_object;
16981 LPFC_MBOXQ_t *mbox;
16982 int rc = 0, i = 0;
16983 uint32_t shdr_status, shdr_add_status;
16984 uint32_t mbox_tmo;
16985 union lpfc_sli4_cfg_shdr *shdr;
16986 struct lpfc_dmabuf *dmabuf;
16987 uint32_t written = 0;
16988
16989 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16990 if (!mbox)
16991 return -ENOMEM;
16992
16993 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16994 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16995 sizeof(struct lpfc_mbx_wr_object) -
16996 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16997
16998 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16999 wr_object->u.request.write_offset = *offset;
17000 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17001 wr_object->u.request.object_name[0] =
17002 cpu_to_le32(wr_object->u.request.object_name[0]);
17003 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17004 list_for_each_entry(dmabuf, dmabuf_list, list) {
17005 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17006 break;
17007 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17008 wr_object->u.request.bde[i].addrHigh =
17009 putPaddrHigh(dmabuf->phys);
17010 if (written + SLI4_PAGE_SIZE >= size) {
17011 wr_object->u.request.bde[i].tus.f.bdeSize =
17012 (size - written);
17013 written += (size - written);
17014 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17015 } else {
17016 wr_object->u.request.bde[i].tus.f.bdeSize =
17017 SLI4_PAGE_SIZE;
17018 written += SLI4_PAGE_SIZE;
17019 }
17020 i++;
17021 }
17022 wr_object->u.request.bde_count = i;
17023 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17024 if (!phba->sli4_hba.intr_enable)
17025 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17026 else {
James Smarta183a152011-10-10 21:32:43 -040017027 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017028 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17029 }
17030 /* The IOCTL status is embedded in the mailbox subheader. */
17031 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17032 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17033 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17034 if (rc != MBX_TIMEOUT)
17035 mempool_free(mbox, phba->mbox_mem_pool);
17036 if (shdr_status || shdr_add_status || rc) {
17037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17038 "3025 Write Object mailbox failed with "
17039 "status x%x add_status x%x, mbx status x%x\n",
17040 shdr_status, shdr_add_status, rc);
17041 rc = -ENXIO;
17042 } else
17043 *offset += wr_object->u.response.actual_write_length;
17044 return rc;
17045}
17046
17047/**
James Smart695a8142010-01-26 23:08:03 -050017048 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17049 * @vport: pointer to vport data structure.
17050 *
17051 * This function iterate through the mailboxq and clean up all REG_LOGIN
17052 * and REG_VPI mailbox commands associated with the vport. This function
17053 * is called when driver want to restart discovery of the vport due to
17054 * a Clear Virtual Link event.
17055 **/
17056void
17057lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17058{
17059 struct lpfc_hba *phba = vport->phba;
17060 LPFC_MBOXQ_t *mb, *nextmb;
17061 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017062 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017063 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017064 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017065 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017066 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017067
James Smartd439d282010-09-29 11:18:45 -040017068 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017069 spin_lock_irq(&phba->hbalock);
17070 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17071 if (mb->vport != vport)
17072 continue;
17073
17074 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17075 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17076 continue;
17077
James Smartd439d282010-09-29 11:18:45 -040017078 list_del(&mb->list);
17079 list_add_tail(&mb->list, &mbox_cmd_list);
17080 }
17081 /* Clean up active mailbox command with the vport */
17082 mb = phba->sli.mbox_active;
17083 if (mb && (mb->vport == vport)) {
17084 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17085 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17086 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17087 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17088 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17089 /* Put reference count for delayed processing */
17090 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17091 /* Unregister the RPI when mailbox complete */
17092 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17093 }
17094 }
James Smart63e801c2010-11-20 23:14:19 -050017095 /* Cleanup any mailbox completions which are not yet processed */
17096 do {
17097 restart_loop = 0;
17098 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17099 /*
17100 * If this mailox is already processed or it is
17101 * for another vport ignore it.
17102 */
17103 if ((mb->vport != vport) ||
17104 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17105 continue;
17106
17107 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17108 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17109 continue;
17110
17111 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17112 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17113 ndlp = (struct lpfc_nodelist *)mb->context2;
17114 /* Unregister the RPI when mailbox complete */
17115 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17116 restart_loop = 1;
17117 spin_unlock_irq(&phba->hbalock);
17118 spin_lock(shost->host_lock);
17119 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17120 spin_unlock(shost->host_lock);
17121 spin_lock_irq(&phba->hbalock);
17122 break;
17123 }
17124 }
17125 } while (restart_loop);
17126
James Smartd439d282010-09-29 11:18:45 -040017127 spin_unlock_irq(&phba->hbalock);
17128
17129 /* Release the cleaned-up mailbox commands */
17130 while (!list_empty(&mbox_cmd_list)) {
17131 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017132 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17133 mp = (struct lpfc_dmabuf *) (mb->context1);
17134 if (mp) {
17135 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17136 kfree(mp);
17137 }
James Smart78730cf2010-04-06 15:06:30 -040017138 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017139 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017140 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017141 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017142 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017143 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017144 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017145 }
James Smart695a8142010-01-26 23:08:03 -050017146 }
James Smart695a8142010-01-26 23:08:03 -050017147 mempool_free(mb, phba->mbox_mem_pool);
17148 }
James Smartd439d282010-09-29 11:18:45 -040017149
17150 /* Release the ndlp with the cleaned-up active mailbox command */
17151 if (act_mbx_ndlp) {
17152 spin_lock(shost->host_lock);
17153 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17154 spin_unlock(shost->host_lock);
17155 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017156 }
James Smart695a8142010-01-26 23:08:03 -050017157}
17158
James Smart2a9bf3d2010-06-07 15:24:45 -040017159/**
17160 * lpfc_drain_txq - Drain the txq
17161 * @phba: Pointer to HBA context object.
17162 *
17163 * This function attempt to submit IOCBs on the txq
17164 * to the adapter. For SLI4 adapters, the txq contains
17165 * ELS IOCBs that have been deferred because the there
17166 * are no SGLs. This congestion can occur with large
17167 * vport counts during node discovery.
17168 **/
17169
17170uint32_t
17171lpfc_drain_txq(struct lpfc_hba *phba)
17172{
17173 LIST_HEAD(completions);
17174 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017175 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017176 unsigned long iflags = 0;
17177 char *fail_msg = NULL;
17178 struct lpfc_sglq *sglq;
17179 union lpfc_wqe wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040017180 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017181
James Smart398d81c2013-05-31 17:04:19 -040017182 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017183 list_for_each_entry(piocbq, &pring->txq, list) {
17184 txq_cnt++;
17185 }
17186
17187 if (txq_cnt > pring->txq_max)
17188 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017189
James Smart398d81c2013-05-31 17:04:19 -040017190 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017191
James Smart0e9bb8d2013-03-01 16:35:12 -050017192 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017193 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017194
James Smart19ca7602010-11-20 23:11:55 -050017195 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017196 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017197 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017198 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17199 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017200 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017201 break;
17202 }
James Smart19ca7602010-11-20 23:11:55 -050017203 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017204 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017205 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017206 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017207 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017208 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017209 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017210
17211 /* The xri and iocb resources secured,
17212 * attempt to issue request
17213 */
James Smart6d368e52011-05-24 11:44:12 -040017214 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017215 piocbq->sli4_xritag = sglq->sli4_xritag;
17216 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17217 fail_msg = "to convert bpl to sgl";
17218 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17219 fail_msg = "to convert iocb to wqe";
17220 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17221 fail_msg = " - Wq is full";
17222 else
17223 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17224
17225 if (fail_msg) {
17226 /* Failed means we can't issue and need to cancel */
17227 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17228 "2822 IOCB failed %s iotag 0x%x "
17229 "xri 0x%x\n",
17230 fail_msg,
17231 piocbq->iotag, piocbq->sli4_xritag);
17232 list_add_tail(&piocbq->list, &completions);
17233 }
James Smart398d81c2013-05-31 17:04:19 -040017234 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017235 }
17236
James Smart2a9bf3d2010-06-07 15:24:45 -040017237 /* Cancel all the IOCBs that cannot be issued */
17238 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17239 IOERR_SLI_ABORTED);
17240
James Smart0e9bb8d2013-03-01 16:35:12 -050017241 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017242}