blob: 53ed6c534f82cd969cb28be03b4294db98f8c1ea [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);
James Smartc92c8412016-07-06 12:36:05 -07008444 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8445 if (iocbq->priority) {
8446 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8447 (iocbq->priority << 1));
8448 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008449 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8450 (phba->cfg_XLanePriority << 1));
8451 }
8452 }
James Smartb5c53952016-03-31 14:12:30 -07008453 /* Note, word 10 is already initialized to 0 */
8454
8455 if (phba->fcp_embed_io) {
8456 struct lpfc_scsi_buf *lpfc_cmd;
8457 struct sli4_sge *sgl;
8458 union lpfc_wqe128 *wqe128;
8459 struct fcp_cmnd *fcp_cmnd;
8460 uint32_t *ptr;
8461
8462 /* 128 byte wqe support here */
8463 wqe128 = (union lpfc_wqe128 *)wqe;
8464
8465 lpfc_cmd = iocbq->context1;
8466 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8467 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8468
8469 /* Word 0-2 - FCP_CMND */
8470 wqe128->generic.bde.tus.f.bdeFlags =
8471 BUFF_TYPE_BDE_IMMED;
8472 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8473 wqe128->generic.bde.addrHigh = 0;
8474 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8475
8476 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8477
8478 /* Word 22-29 FCP CMND Payload */
8479 ptr = &wqe128->words[22];
8480 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8481 }
James Smart7851fe22011-07-22 18:36:52 -04008482 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008483 case CMD_FCP_IREAD64_CR:
8484 /* word3 iocb=iotag wqe=payload_offset_len */
8485 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008486 bf_set(payload_offset_len, &wqe->fcp_iread,
8487 xmit_len + sizeof(struct fcp_rsp));
8488 bf_set(cmd_buff_len, &wqe->fcp_iread,
8489 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008490 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8491 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8492 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8493 iocbq->iocb.ulpFCP2Rcvy);
8494 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008495 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008496 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8497 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8498 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008499 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008500 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008501 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8502 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008503 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8504 if (iocbq->priority) {
8505 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8506 (iocbq->priority << 1));
8507 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008508 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8509 (phba->cfg_XLanePriority << 1));
8510 }
8511 }
James Smartb5c53952016-03-31 14:12:30 -07008512 /* Note, word 10 is already initialized to 0 */
8513
8514 if (phba->fcp_embed_io) {
8515 struct lpfc_scsi_buf *lpfc_cmd;
8516 struct sli4_sge *sgl;
8517 union lpfc_wqe128 *wqe128;
8518 struct fcp_cmnd *fcp_cmnd;
8519 uint32_t *ptr;
8520
8521 /* 128 byte wqe support here */
8522 wqe128 = (union lpfc_wqe128 *)wqe;
8523
8524 lpfc_cmd = iocbq->context1;
8525 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8526 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8527
8528 /* Word 0-2 - FCP_CMND */
8529 wqe128->generic.bde.tus.f.bdeFlags =
8530 BUFF_TYPE_BDE_IMMED;
8531 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8532 wqe128->generic.bde.addrHigh = 0;
8533 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8534
8535 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8536
8537 /* Word 22-29 FCP CMND Payload */
8538 ptr = &wqe128->words[22];
8539 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8540 }
James Smart7851fe22011-07-22 18:36:52 -04008541 break;
James Smartf1126682009-06-10 17:22:44 -04008542 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008543 /* word3 iocb=iotag wqe=payload_offset_len */
8544 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8545 bf_set(payload_offset_len, &wqe->fcp_icmd,
8546 xmit_len + sizeof(struct fcp_rsp));
8547 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8548 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008549 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008550 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008551 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008552 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8553 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8554 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8555 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8556 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008557 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8558 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008559 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8560 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008561 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8562 if (iocbq->priority) {
8563 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8564 (iocbq->priority << 1));
8565 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008566 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8567 (phba->cfg_XLanePriority << 1));
8568 }
8569 }
James Smartb5c53952016-03-31 14:12:30 -07008570 /* Note, word 10 is already initialized to 0 */
8571
8572 if (phba->fcp_embed_io) {
8573 struct lpfc_scsi_buf *lpfc_cmd;
8574 struct sli4_sge *sgl;
8575 union lpfc_wqe128 *wqe128;
8576 struct fcp_cmnd *fcp_cmnd;
8577 uint32_t *ptr;
8578
8579 /* 128 byte wqe support here */
8580 wqe128 = (union lpfc_wqe128 *)wqe;
8581
8582 lpfc_cmd = iocbq->context1;
8583 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8584 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8585
8586 /* Word 0-2 - FCP_CMND */
8587 wqe128->generic.bde.tus.f.bdeFlags =
8588 BUFF_TYPE_BDE_IMMED;
8589 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8590 wqe128->generic.bde.addrHigh = 0;
8591 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8592
8593 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8594
8595 /* Word 22-29 FCP CMND Payload */
8596 ptr = &wqe128->words[22];
8597 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8598 }
James Smart7851fe22011-07-22 18:36:52 -04008599 break;
James Smart4f774512009-05-22 14:52:35 -04008600 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008601 /* For this command calculate the xmit length of the
8602 * request bde.
8603 */
8604 xmit_len = 0;
8605 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8606 sizeof(struct ulp_bde64);
8607 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008608 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008609 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8610 break;
James Smart63e801c2010-11-20 23:14:19 -05008611 xmit_len += bde.tus.f.bdeSize;
8612 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008613 /* word3 iocb=IO_TAG wqe=request_payload_len */
8614 wqe->gen_req.request_payload_len = xmit_len;
8615 /* word4 iocb=parameter wqe=relative_offset memcpy */
8616 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008617 /* word6 context tag copied in memcpy */
8618 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8619 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8620 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8621 "2015 Invalid CT %x command 0x%x\n",
8622 ct, iocbq->iocb.ulpCommand);
8623 return IOCB_ERROR;
8624 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008625 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8626 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8627 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8628 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8629 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8630 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8631 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8632 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008633 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008634 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008635 break;
James Smart4f774512009-05-22 14:52:35 -04008636 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008637 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008638 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008639 /* word3 iocb=iotag32 wqe=response_payload_len */
8640 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008641 /* word4 */
8642 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008643 /* word5 iocb=rsvd wge=did */
8644 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008645 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8646
8647 if_type = bf_get(lpfc_sli_intf_if_type,
8648 &phba->sli4_hba.sli_intf);
8649 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8650 if (iocbq->vport->fc_flag & FC_PT2PT) {
8651 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8652 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8653 iocbq->vport->fc_myDID);
8654 if (iocbq->vport->fc_myDID == Fabric_DID) {
8655 bf_set(wqe_els_did,
8656 &wqe->xmit_els_rsp.wqe_dest, 0);
8657 }
8658 }
8659 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008660 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8661 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8662 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8663 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008664 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008665 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008666 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008667 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008668 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8669 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8670 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8671 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8672 LPFC_WQE_LENLOC_WORD3);
8673 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008674 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8675 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008676 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8677 iocbq->context2)->virt);
8678 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008679 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8680 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008681 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008682 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8683 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008684 phba->vpi_ids[phba->pport->vpi]);
8685 }
James Smart4f774512009-05-22 14:52:35 -04008686 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008687 break;
James Smart4f774512009-05-22 14:52:35 -04008688 case CMD_CLOSE_XRI_CN:
8689 case CMD_ABORT_XRI_CN:
8690 case CMD_ABORT_XRI_CX:
8691 /* words 0-2 memcpy should be 0 rserved */
8692 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008693 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8694 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8695 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8696 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8697 } else
8698 fip = 0;
8699
8700 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008701 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008702 * The link is down, or the command was ELS_FIP
8703 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008704 * on the wire.
8705 */
8706 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8707 else
8708 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8709 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008710 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8711 wqe->abort_cmd.rsrvd5 = 0;
8712 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008713 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8714 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008715 /*
8716 * The abort handler will send us CMD_ABORT_XRI_CN or
8717 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8718 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008719 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8720 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8721 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8722 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008723 cmnd = CMD_ABORT_XRI_CX;
8724 command_type = OTHER_COMMAND;
8725 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008726 break;
James Smart6669f9b2009-10-02 15:16:45 -04008727 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008728 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008729 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008730 * we re-construct this WQE here based on information in
8731 * iocbq from scratch.
8732 */
8733 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008734 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008735 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008736 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8737 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008738 LPFC_ABTS_UNSOL_INT) {
8739 /* ABTS sent by initiator to CT exchange, the
8740 * RX_ID field will be filled with the newly
8741 * allocated responder XRI.
8742 */
8743 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8744 iocbq->sli4_xritag);
8745 } else {
8746 /* ABTS sent by responder to CT exchange, the
8747 * RX_ID field will be filled with the responder
8748 * RX_ID from ABTS.
8749 */
8750 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008751 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008752 }
James Smart6669f9b2009-10-02 15:16:45 -04008753 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8754 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008755
8756 /* Use CT=VPI */
8757 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8758 ndlp->nlp_DID);
8759 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8760 iocbq->iocb.ulpContext);
8761 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008762 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008763 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008764 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8765 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8766 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008767 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8768 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008769 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8770 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8771 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8772 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8773 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8774 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8775 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8776 }
8777
James Smart7851fe22011-07-22 18:36:52 -04008778 break;
James Smart4f774512009-05-22 14:52:35 -04008779 case CMD_XRI_ABORTED_CX:
8780 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008781 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8782 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8783 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8784 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8785 default:
8786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8787 "2014 Invalid command 0x%x\n",
8788 iocbq->iocb.ulpCommand);
8789 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008790 break;
James Smart4f774512009-05-22 14:52:35 -04008791 }
James Smart6d368e52011-05-24 11:44:12 -04008792
James Smart8012cc32012-10-31 14:44:49 -04008793 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8794 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8795 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8796 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8797 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8798 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8799 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8800 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008801 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8802 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8803 wqe->generic.wqe_com.abort_tag = abort_tag;
8804 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8805 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8806 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8807 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008808 return 0;
8809}
8810
8811/**
8812 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8813 * @phba: Pointer to HBA context object.
8814 * @ring_number: SLI ring number to issue iocb on.
8815 * @piocb: Pointer to command iocb.
8816 * @flag: Flag indicating if this command can be put into txq.
8817 *
8818 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8819 * an iocb command to an HBA with SLI-4 interface spec.
8820 *
8821 * This function is called with hbalock held. The function will return success
8822 * after it successfully submit the iocb to firmware or after adding to the
8823 * txq.
8824 **/
8825static int
8826__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8827 struct lpfc_iocbq *piocb, uint32_t flag)
8828{
8829 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008830 union lpfc_wqe *wqe;
8831 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008832 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008833 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008834
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008835 lockdep_assert_held(&phba->hbalock);
8836
James Smartb5c53952016-03-31 14:12:30 -07008837 /*
8838 * The WQE can be either 64 or 128 bytes,
8839 * so allocate space on the stack assuming the largest.
8840 */
8841 wqe = (union lpfc_wqe *)&wqe128;
8842
James Smart4f774512009-05-22 14:52:35 -04008843 if (piocb->sli4_xritag == NO_XRI) {
8844 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008845 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008846 sglq = NULL;
8847 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008848 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008849 if (!(flag & SLI_IOCB_RET_IOCB)) {
8850 __lpfc_sli_ringtx_put(phba,
8851 pring, piocb);
8852 return IOCB_SUCCESS;
8853 } else {
8854 return IOCB_BUSY;
8855 }
8856 } else {
James Smart6d368e52011-05-24 11:44:12 -04008857 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008858 if (!sglq) {
8859 if (!(flag & SLI_IOCB_RET_IOCB)) {
8860 __lpfc_sli_ringtx_put(phba,
8861 pring,
8862 piocb);
8863 return IOCB_SUCCESS;
8864 } else
8865 return IOCB_BUSY;
8866 }
8867 }
James Smart4f774512009-05-22 14:52:35 -04008868 }
8869 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008870 /* These IO's already have an XRI and a mapped sgl. */
8871 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008872 } else {
James Smart6d368e52011-05-24 11:44:12 -04008873 /*
8874 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008875 * sglq is on the active list
8876 */
James Smartedccdc12013-01-03 15:43:45 -05008877 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008878 if (!sglq)
8879 return IOCB_ERROR;
8880 }
8881
8882 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008883 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008884 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008885 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008886 return IOCB_ERROR;
8887 }
8888
James Smartb5c53952016-03-31 14:12:30 -07008889 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008890 return IOCB_ERROR;
8891
James Smart341af102010-01-26 23:07:37 -05008892 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008893 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008894 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008895 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8896 } else {
8897 wq = phba->sli4_hba.oas_wq;
8898 }
James Smartb5c53952016-03-31 14:12:30 -07008899 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008900 return IOCB_ERROR;
8901 } else {
James Smartea714f32013-04-17 20:18:47 -04008902 if (unlikely(!phba->sli4_hba.els_wq))
8903 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008904 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008905 return IOCB_ERROR;
8906 }
8907 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8908
8909 return 0;
8910}
8911
8912/**
James Smart3772a992009-05-22 14:50:54 -04008913 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8914 *
8915 * This routine wraps the actual lockless version for issusing IOCB function
8916 * pointer from the lpfc_hba struct.
8917 *
8918 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008919 * IOCB_ERROR - Error
8920 * IOCB_SUCCESS - Success
8921 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008922 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008923int
James Smart3772a992009-05-22 14:50:54 -04008924__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8925 struct lpfc_iocbq *piocb, uint32_t flag)
8926{
8927 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8928}
8929
8930/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008931 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008932 * @phba: The hba struct for which this call is being executed.
8933 * @dev_grp: The HBA PCI-Device group number.
8934 *
8935 * This routine sets up the SLI interface API function jump table in @phba
8936 * struct.
8937 * Returns: 0 - success, -ENODEV - failure.
8938 **/
8939int
8940lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8941{
8942
8943 switch (dev_grp) {
8944 case LPFC_PCI_DEV_LP:
8945 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8946 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8947 break;
James Smart4f774512009-05-22 14:52:35 -04008948 case LPFC_PCI_DEV_OC:
8949 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8950 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8951 break;
James Smart3772a992009-05-22 14:50:54 -04008952 default:
8953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8954 "1419 Invalid HBA PCI-device group: 0x%x\n",
8955 dev_grp);
8956 return -ENODEV;
8957 break;
8958 }
8959 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8960 return 0;
8961}
James Smart92d7f7b2007-06-17 19:56:38 -05008962
James Smarta1efe162015-05-21 13:55:20 -04008963/**
8964 * lpfc_sli_calc_ring - Calculates which ring to use
8965 * @phba: Pointer to HBA context object.
8966 * @ring_number: Initial ring
8967 * @piocb: Pointer to command iocb.
8968 *
8969 * For SLI4, FCP IO can deferred to one fo many WQs, based on
8970 * fcp_wqidx, thus we need to calculate the corresponding ring.
8971 * Since ABORTS must go on the same WQ of the command they are
8972 * aborting, we use command's fcp_wqidx.
8973 */
James Smart9bd2bff52014-09-03 12:57:30 -04008974int
8975lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
8976 struct lpfc_iocbq *piocb)
8977{
James Smart8b0dff12015-05-22 10:42:38 -04008978 if (phba->sli_rev < LPFC_SLI_REV4)
8979 return ring_number;
8980
8981 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8982 if (!(phba->cfg_fof) ||
8983 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8984 if (unlikely(!phba->sli4_hba.fcp_wq))
8985 return LPFC_HBA_ERROR;
8986 /*
8987 * for abort iocb fcp_wqidx should already
8988 * be setup based on what work queue we used.
8989 */
8990 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
8991 piocb->fcp_wqidx =
8992 lpfc_sli4_scmd_to_wqidx_distr(phba,
8993 piocb->context1);
8994 ring_number = MAX_SLI3_CONFIGURED_RINGS +
8995 piocb->fcp_wqidx;
8996 } else {
8997 if (unlikely(!phba->sli4_hba.oas_wq))
8998 return LPFC_HBA_ERROR;
8999 piocb->fcp_wqidx = 0;
9000 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04009001 }
9002 }
9003 return ring_number;
9004}
9005
James Smarte59058c2008-08-24 21:49:00 -04009006/**
James Smart3621a712009-04-06 18:47:14 -04009007 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009008 * @phba: Pointer to HBA context object.
9009 * @pring: Pointer to driver SLI ring object.
9010 * @piocb: Pointer to command iocb.
9011 * @flag: Flag indicating if this command can be put into txq.
9012 *
9013 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9014 * function. This function gets the hbalock and calls
9015 * __lpfc_sli_issue_iocb function and will return the error returned
9016 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9017 * functions which do not hold hbalock.
9018 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009019int
James Smart3772a992009-05-22 14:50:54 -04009020lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009021 struct lpfc_iocbq *piocb, uint32_t flag)
9022{
James Smartba20c852012-08-03 12:36:52 -04009023 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009024 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009025 struct lpfc_queue *fpeq;
9026 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009027 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009028 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009029
James Smart7e56aa22012-08-03 12:35:34 -04009030 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009031 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9032 if (unlikely(ring_number == LPFC_HBA_ERROR))
9033 return IOCB_ERROR;
9034 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04009035
James Smart9bd2bff52014-09-03 12:57:30 -04009036 pring = &phba->sli.ring[ring_number];
9037 spin_lock_irqsave(&pring->ring_lock, iflags);
9038 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9039 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009040
James Smart9bd2bff52014-09-03 12:57:30 -04009041 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9042 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009043
James Smart9bd2bff52014-09-03 12:57:30 -04009044 if (atomic_dec_and_test(&fcp_eq_hdl->
9045 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009046
James Smart9bd2bff52014-09-03 12:57:30 -04009047 /* Get associated EQ with this index */
9048 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009049
James Smart9bd2bff52014-09-03 12:57:30 -04009050 /* Turn off interrupts from this EQ */
9051 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009052
James Smart9bd2bff52014-09-03 12:57:30 -04009053 /*
9054 * Process all the events on FCP EQ
9055 */
9056 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9057 lpfc_sli4_hba_handle_eqe(phba,
9058 eqe, idx);
9059 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009060 }
James Smartba20c852012-08-03 12:36:52 -04009061
James Smart9bd2bff52014-09-03 12:57:30 -04009062 /* Always clear and re-arm the EQ */
9063 lpfc_sli4_eq_release(fpeq,
9064 LPFC_QUEUE_REARM);
9065 }
9066 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009067 }
James Smart7e56aa22012-08-03 12:35:34 -04009068 } else {
9069 /* For now, SLI2/3 will still use hbalock */
9070 spin_lock_irqsave(&phba->hbalock, iflags);
9071 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9072 spin_unlock_irqrestore(&phba->hbalock, iflags);
9073 }
James Smart92d7f7b2007-06-17 19:56:38 -05009074 return rc;
9075}
9076
James Smarte59058c2008-08-24 21:49:00 -04009077/**
James Smart3621a712009-04-06 18:47:14 -04009078 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009079 * @phba: Pointer to HBA context object.
9080 *
9081 * This function is called while driver attaches with the
9082 * HBA to setup the extra ring. The extra ring is used
9083 * only when driver needs to support target mode functionality
9084 * or IP over FC functionalities.
9085 *
9086 * This function is called with no lock held.
9087 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009088static int
9089lpfc_extra_ring_setup( struct lpfc_hba *phba)
9090{
9091 struct lpfc_sli *psli;
9092 struct lpfc_sli_ring *pring;
9093
9094 psli = &phba->sli;
9095
9096 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009097
9098 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009099 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009100 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9101 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9102 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9103 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009104
James Smarta4bc3372006-12-02 13:34:16 -05009105 /* and give them to the extra ring */
9106 pring = &psli->ring[psli->extra_ring];
9107
James Smart7e56aa22012-08-03 12:35:34 -04009108 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9109 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9110 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9111 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009112
9113 /* Setup default profile for this ring */
9114 pring->iotag_max = 4096;
9115 pring->num_mask = 1;
9116 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009117 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9118 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009119 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9120 return 0;
9121}
9122
James Smartcb69f7d2011-12-13 13:21:57 -05009123/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9124 * @phba: Pointer to HBA context object.
9125 * @iocbq: Pointer to iocb object.
9126 *
9127 * The async_event handler calls this routine when it receives
9128 * an ASYNC_STATUS_CN event from the port. The port generates
9129 * this event when an Abort Sequence request to an rport fails
9130 * twice in succession. The abort could be originated by the
9131 * driver or by the port. The ABTS could have been for an ELS
9132 * or FCP IO. The port only generates this event when an ABTS
9133 * fails to complete after one retry.
9134 */
9135static void
9136lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9137 struct lpfc_iocbq *iocbq)
9138{
9139 struct lpfc_nodelist *ndlp = NULL;
9140 uint16_t rpi = 0, vpi = 0;
9141 struct lpfc_vport *vport = NULL;
9142
9143 /* The rpi in the ulpContext is vport-sensitive. */
9144 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9145 rpi = iocbq->iocb.ulpContext;
9146
9147 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9148 "3092 Port generated ABTS async event "
9149 "on vpi %d rpi %d status 0x%x\n",
9150 vpi, rpi, iocbq->iocb.ulpStatus);
9151
9152 vport = lpfc_find_vport_by_vpid(phba, vpi);
9153 if (!vport)
9154 goto err_exit;
9155 ndlp = lpfc_findnode_rpi(vport, rpi);
9156 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9157 goto err_exit;
9158
9159 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9160 lpfc_sli_abts_recover_port(vport, ndlp);
9161 return;
9162
9163 err_exit:
9164 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9165 "3095 Event Context not found, no "
9166 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9167 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9168 vpi, rpi);
9169}
9170
9171/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9172 * @phba: pointer to HBA context object.
9173 * @ndlp: nodelist pointer for the impacted rport.
9174 * @axri: pointer to the wcqe containing the failed exchange.
9175 *
9176 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9177 * port. The port generates this event when an abort exchange request to an
9178 * rport fails twice in succession with no reply. The abort could be originated
9179 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9180 */
9181void
9182lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9183 struct lpfc_nodelist *ndlp,
9184 struct sli4_wcqe_xri_aborted *axri)
9185{
9186 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009187 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009188
James Smart6b5151f2012-01-18 16:24:06 -05009189 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009190 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9191 "3115 Node Context not found, driver "
9192 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009193 return;
9194 }
9195
James Smartcb69f7d2011-12-13 13:21:57 -05009196 vport = ndlp->vport;
9197 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9198 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009199 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009200 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009201 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009202 bf_get(lpfc_wcqe_xa_status, axri),
9203 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009204
James Smart5c1db2a2012-03-01 22:34:36 -05009205 /*
9206 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9207 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9208 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9209 */
James Smarte3d2b802012-08-14 14:25:43 -04009210 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009211 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9212 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009213 lpfc_sli_abts_recover_port(vport, ndlp);
9214}
9215
James Smarte59058c2008-08-24 21:49:00 -04009216/**
James Smart3621a712009-04-06 18:47:14 -04009217 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009218 * @phba: Pointer to HBA context object.
9219 * @pring: Pointer to driver SLI ring object.
9220 * @iocbq: Pointer to iocb object.
9221 *
9222 * This function is called by the slow ring event handler
9223 * function when there is an ASYNC event iocb in the ring.
9224 * This function is called with no lock held.
9225 * Currently this function handles only temperature related
9226 * ASYNC events. The function decodes the temperature sensor
9227 * event message and posts events for the management applications.
9228 **/
James Smart98c9ea52007-10-27 13:37:33 -04009229static void
James Smart57127f12007-10-27 13:37:05 -04009230lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9231 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9232{
9233 IOCB_t *icmd;
9234 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009235 struct temp_event temp_event_data;
9236 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009237 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009238
9239 icmd = &iocbq->iocb;
9240 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009241
James Smartcb69f7d2011-12-13 13:21:57 -05009242 switch (evt_code) {
9243 case ASYNC_TEMP_WARN:
9244 case ASYNC_TEMP_SAFE:
9245 temp_event_data.data = (uint32_t) icmd->ulpContext;
9246 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9247 if (evt_code == ASYNC_TEMP_WARN) {
9248 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9249 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9250 "0347 Adapter is very hot, please take "
9251 "corrective action. temperature : %d Celsius\n",
9252 (uint32_t) icmd->ulpContext);
9253 } else {
9254 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9255 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9256 "0340 Adapter temperature is OK now. "
9257 "temperature : %d Celsius\n",
9258 (uint32_t) icmd->ulpContext);
9259 }
9260
9261 /* Send temperature change event to applications */
9262 shost = lpfc_shost_from_vport(phba->pport);
9263 fc_host_post_vendor_event(shost, fc_get_event_number(),
9264 sizeof(temp_event_data), (char *) &temp_event_data,
9265 LPFC_NL_VENDOR_ID);
9266 break;
9267 case ASYNC_STATUS_CN:
9268 lpfc_sli_abts_err_handler(phba, iocbq);
9269 break;
9270 default:
James Smarta257bf92009-04-06 18:48:10 -04009271 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009273 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009274 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009275 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9276 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9277 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9278 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009279 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009280 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9281 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9282 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9283 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9284
James Smartcb69f7d2011-12-13 13:21:57 -05009285 break;
James Smart57127f12007-10-27 13:37:05 -04009286 }
James Smart57127f12007-10-27 13:37:05 -04009287}
9288
9289
James Smarte59058c2008-08-24 21:49:00 -04009290/**
James Smart3621a712009-04-06 18:47:14 -04009291 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009292 * @phba: Pointer to HBA context object.
9293 *
9294 * lpfc_sli_setup sets up rings of the SLI interface with
9295 * number of iocbs per ring and iotags. This function is
9296 * called while driver attach to the HBA and before the
9297 * interrupts are enabled. So there is no need for locking.
9298 *
9299 * This function always returns 0.
9300 **/
dea31012005-04-17 16:05:31 -05009301int
9302lpfc_sli_setup(struct lpfc_hba *phba)
9303{
James Smarted957682007-06-17 19:56:37 -05009304 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009305 struct lpfc_sli *psli = &phba->sli;
9306 struct lpfc_sli_ring *pring;
9307
James Smart2a76a282012-08-03 12:35:54 -04009308 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9309 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009310 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009311 psli->sli_flag = 0;
9312 psli->fcp_ring = LPFC_FCP_RING;
9313 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009314 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009315
James Bottomley604a3e32005-10-29 10:28:33 -05009316 psli->iocbq_lookup = NULL;
9317 psli->iocbq_lookup_len = 0;
9318 psli->last_iotag = 0;
9319
dea31012005-04-17 16:05:31 -05009320 for (i = 0; i < psli->num_rings; i++) {
9321 pring = &psli->ring[i];
9322 switch (i) {
9323 case LPFC_FCP_RING: /* ring 0 - FCP */
9324 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009325 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9326 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9327 pring->sli.sli3.numCiocb +=
9328 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9329 pring->sli.sli3.numRiocb +=
9330 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9331 pring->sli.sli3.numCiocb +=
9332 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9333 pring->sli.sli3.numRiocb +=
9334 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9335 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009336 SLI3_IOCB_CMD_SIZE :
9337 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009338 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009339 SLI3_IOCB_RSP_SIZE :
9340 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009341 pring->iotag_ctr = 0;
9342 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009343 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009344 pring->fast_iotag = pring->iotag_max;
9345 pring->num_mask = 0;
9346 break;
James Smarta4bc3372006-12-02 13:34:16 -05009347 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009348 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009349 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9350 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9351 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009352 SLI3_IOCB_CMD_SIZE :
9353 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009354 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009355 SLI3_IOCB_RSP_SIZE :
9356 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009357 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009358 pring->num_mask = 0;
9359 break;
9360 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9361 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009362 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9363 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9364 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009365 SLI3_IOCB_CMD_SIZE :
9366 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009367 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009368 SLI3_IOCB_RSP_SIZE :
9369 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009370 pring->fast_iotag = 0;
9371 pring->iotag_ctr = 0;
9372 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009373 pring->lpfc_sli_rcv_async_status =
9374 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009375 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009376 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009377 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9378 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009379 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009380 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009381 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009382 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9383 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009384 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009385 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009386 pring->prt[2].profile = 0; /* Mask 2 */
9387 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009388 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009389 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009390 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009391 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009392 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009393 pring->prt[3].profile = 0; /* Mask 3 */
9394 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009395 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009396 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009397 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009398 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009399 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009400 break;
9401 }
James Smart7e56aa22012-08-03 12:35:34 -04009402 totiocbsize += (pring->sli.sli3.numCiocb *
9403 pring->sli.sli3.sizeCiocb) +
9404 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009405 }
James Smarted957682007-06-17 19:56:37 -05009406 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009407 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009408 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9409 "SLI2 SLIM Data: x%x x%lx\n",
9410 phba->brd_no, totiocbsize,
9411 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009412 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009413 if (phba->cfg_multi_ring_support == 2)
9414 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009415
9416 return 0;
9417}
9418
James Smarte59058c2008-08-24 21:49:00 -04009419/**
James Smart3621a712009-04-06 18:47:14 -04009420 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009421 * @phba: Pointer to HBA context object.
9422 *
9423 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9424 * ring. This function also initializes ring indices of each ring.
9425 * This function is called during the initialization of the SLI
9426 * interface of an HBA.
9427 * This function is called with no lock held and always returns
9428 * 1.
9429 **/
dea31012005-04-17 16:05:31 -05009430int
James Smart2e0fef82007-06-17 19:56:36 -05009431lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009432{
9433 struct lpfc_sli *psli;
9434 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009435 int i;
dea31012005-04-17 16:05:31 -05009436
9437 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009438 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009439 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009440 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009441 /* Initialize list headers for txq and txcmplq as double linked lists */
9442 for (i = 0; i < psli->num_rings; i++) {
9443 pring = &psli->ring[i];
9444 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009445 pring->sli.sli3.next_cmdidx = 0;
9446 pring->sli.sli3.local_getidx = 0;
9447 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009448 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009449 INIT_LIST_HEAD(&pring->txq);
9450 INIT_LIST_HEAD(&pring->txcmplq);
9451 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009452 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009453 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009454 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009455 }
James Smart2e0fef82007-06-17 19:56:36 -05009456 spin_unlock_irq(&phba->hbalock);
9457 return 1;
dea31012005-04-17 16:05:31 -05009458}
9459
James Smarte59058c2008-08-24 21:49:00 -04009460/**
James Smart04c68492009-05-22 14:52:52 -04009461 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9462 * @phba: Pointer to HBA context object.
9463 *
9464 * This routine flushes the mailbox command subsystem. It will unconditionally
9465 * flush all the mailbox commands in the three possible stages in the mailbox
9466 * command sub-system: pending mailbox command queue; the outstanding mailbox
9467 * command; and completed mailbox command queue. It is caller's responsibility
9468 * to make sure that the driver is in the proper state to flush the mailbox
9469 * command sub-system. Namely, the posting of mailbox commands into the
9470 * pending mailbox command queue from the various clients must be stopped;
9471 * either the HBA is in a state that it will never works on the outstanding
9472 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9473 * mailbox command has been completed.
9474 **/
9475static void
9476lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9477{
9478 LIST_HEAD(completions);
9479 struct lpfc_sli *psli = &phba->sli;
9480 LPFC_MBOXQ_t *pmb;
9481 unsigned long iflag;
9482
9483 /* Flush all the mailbox commands in the mbox system */
9484 spin_lock_irqsave(&phba->hbalock, iflag);
9485 /* The pending mailbox command queue */
9486 list_splice_init(&phba->sli.mboxq, &completions);
9487 /* The outstanding active mailbox command */
9488 if (psli->mbox_active) {
9489 list_add_tail(&psli->mbox_active->list, &completions);
9490 psli->mbox_active = NULL;
9491 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9492 }
9493 /* The completed mailbox command queue */
9494 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9495 spin_unlock_irqrestore(&phba->hbalock, iflag);
9496
9497 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9498 while (!list_empty(&completions)) {
9499 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9500 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9501 if (pmb->mbox_cmpl)
9502 pmb->mbox_cmpl(phba, pmb);
9503 }
9504}
9505
9506/**
James Smart3621a712009-04-06 18:47:14 -04009507 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009508 * @vport: Pointer to virtual port object.
9509 *
9510 * lpfc_sli_host_down is called to clean up the resources
9511 * associated with a vport before destroying virtual
9512 * port data structures.
9513 * This function does following operations:
9514 * - Free discovery resources associated with this virtual
9515 * port.
9516 * - Free iocbs associated with this virtual port in
9517 * the txq.
9518 * - Send abort for all iocb commands associated with this
9519 * vport in txcmplq.
9520 *
9521 * This function is called with no lock held and always returns 1.
9522 **/
dea31012005-04-17 16:05:31 -05009523int
James Smart92d7f7b2007-06-17 19:56:38 -05009524lpfc_sli_host_down(struct lpfc_vport *vport)
9525{
James Smart858c9f62007-06-17 19:56:39 -05009526 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009527 struct lpfc_hba *phba = vport->phba;
9528 struct lpfc_sli *psli = &phba->sli;
9529 struct lpfc_sli_ring *pring;
9530 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009531 int i;
9532 unsigned long flags = 0;
9533 uint16_t prev_pring_flag;
9534
9535 lpfc_cleanup_discovery_resources(vport);
9536
9537 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009538 for (i = 0; i < psli->num_rings; i++) {
9539 pring = &psli->ring[i];
9540 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009541 /* Only slow rings */
9542 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009543 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009544 /* Set the lpfc data pending flag */
9545 set_bit(LPFC_DATA_READY, &phba->data_flags);
9546 }
James Smart92d7f7b2007-06-17 19:56:38 -05009547 /*
9548 * Error everything on the txq since these iocbs have not been
9549 * given to the FW yet.
9550 */
James Smart92d7f7b2007-06-17 19:56:38 -05009551 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9552 if (iocb->vport != vport)
9553 continue;
James Smart858c9f62007-06-17 19:56:39 -05009554 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009555 }
9556
9557 /* Next issue ABTS for everything on the txcmplq */
9558 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9559 list) {
9560 if (iocb->vport != vport)
9561 continue;
9562 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9563 }
9564
9565 pring->flag = prev_pring_flag;
9566 }
9567
9568 spin_unlock_irqrestore(&phba->hbalock, flags);
9569
James Smarta257bf92009-04-06 18:48:10 -04009570 /* Cancel all the IOCBs from the completions list */
9571 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9572 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009573 return 1;
9574}
9575
James Smarte59058c2008-08-24 21:49:00 -04009576/**
James Smart3621a712009-04-06 18:47:14 -04009577 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009578 * @phba: Pointer to HBA context object.
9579 *
9580 * This function cleans up all iocb, buffers, mailbox commands
9581 * while shutting down the HBA. This function is called with no
9582 * lock held and always returns 1.
9583 * This function does the following to cleanup driver resources:
9584 * - Free discovery resources for each virtual port
9585 * - Cleanup any pending fabric iocbs
9586 * - Iterate through the iocb txq and free each entry
9587 * in the list.
9588 * - Free up any buffer posted to the HBA
9589 * - Free mailbox commands in the mailbox queue.
9590 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009591int
James Smart2e0fef82007-06-17 19:56:36 -05009592lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009593{
James Smart2534ba72007-04-25 09:52:20 -04009594 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009595 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009596 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009597 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009598 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009599 int i;
9600
9601 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009602 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009603
dea31012005-04-17 16:05:31 -05009604 lpfc_hba_down_prep(phba);
9605
James Smart92d7f7b2007-06-17 19:56:38 -05009606 lpfc_fabric_abort_hba(phba);
9607
James Smart2e0fef82007-06-17 19:56:36 -05009608 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009609 for (i = 0; i < psli->num_rings; i++) {
9610 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009611 /* Only slow rings */
9612 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009613 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009614 /* Set the lpfc data pending flag */
9615 set_bit(LPFC_DATA_READY, &phba->data_flags);
9616 }
dea31012005-04-17 16:05:31 -05009617
9618 /*
9619 * Error everything on the txq since these iocbs have not been
9620 * given to the FW yet.
9621 */
James Smart2534ba72007-04-25 09:52:20 -04009622 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009623 }
James Smart2e0fef82007-06-17 19:56:36 -05009624 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009625
James Smarta257bf92009-04-06 18:48:10 -04009626 /* Cancel all the IOCBs from the completions list */
9627 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9628 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009629
James Smart0ff10d42008-01-11 01:52:36 -05009630 spin_lock_irqsave(&phba->hbalock, flags);
9631 list_splice_init(&phba->elsbuf, &completions);
9632 phba->elsbuf_cnt = 0;
9633 phba->elsbuf_prev_cnt = 0;
9634 spin_unlock_irqrestore(&phba->hbalock, flags);
9635
9636 while (!list_empty(&completions)) {
9637 list_remove_head(&completions, buf_ptr,
9638 struct lpfc_dmabuf, list);
9639 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9640 kfree(buf_ptr);
9641 }
9642
dea31012005-04-17 16:05:31 -05009643 /* Return any active mbox cmds */
9644 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009645
James Smartda0436e2009-05-22 14:51:39 -04009646 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009647 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009648 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009649
James Smartda0436e2009-05-22 14:51:39 -04009650 return 1;
9651}
James Smart92d7f7b2007-06-17 19:56:38 -05009652
James Smartda0436e2009-05-22 14:51:39 -04009653/**
James Smart3621a712009-04-06 18:47:14 -04009654 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009655 * @srcp: Source memory pointer.
9656 * @destp: Destination memory pointer.
9657 * @cnt: Number of words required to be copied.
9658 *
9659 * This function is used for copying data between driver memory
9660 * and the SLI memory. This function also changes the endianness
9661 * of each word if native endianness is different from SLI
9662 * endianness. This function can be called with or without
9663 * lock.
9664 **/
dea31012005-04-17 16:05:31 -05009665void
9666lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9667{
9668 uint32_t *src = srcp;
9669 uint32_t *dest = destp;
9670 uint32_t ldata;
9671 int i;
9672
9673 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9674 ldata = *src;
9675 ldata = le32_to_cpu(ldata);
9676 *dest = ldata;
9677 src++;
9678 dest++;
9679 }
9680}
9681
James Smarte59058c2008-08-24 21:49:00 -04009682
9683/**
James Smarta0c87cb2009-07-19 10:01:10 -04009684 * lpfc_sli_bemem_bcopy - SLI memory copy function
9685 * @srcp: Source memory pointer.
9686 * @destp: Destination memory pointer.
9687 * @cnt: Number of words required to be copied.
9688 *
9689 * This function is used for copying data between a data structure
9690 * with big endian representation to local endianness.
9691 * This function can be called with or without lock.
9692 **/
9693void
9694lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9695{
9696 uint32_t *src = srcp;
9697 uint32_t *dest = destp;
9698 uint32_t ldata;
9699 int i;
9700
9701 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9702 ldata = *src;
9703 ldata = be32_to_cpu(ldata);
9704 *dest = ldata;
9705 src++;
9706 dest++;
9707 }
9708}
9709
9710/**
James Smart3621a712009-04-06 18:47:14 -04009711 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009712 * @phba: Pointer to HBA context object.
9713 * @pring: Pointer to driver SLI ring object.
9714 * @mp: Pointer to driver buffer object.
9715 *
9716 * This function is called with no lock held.
9717 * It always return zero after adding the buffer to the postbufq
9718 * buffer list.
9719 **/
dea31012005-04-17 16:05:31 -05009720int
James Smart2e0fef82007-06-17 19:56:36 -05009721lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9722 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009723{
9724 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9725 later */
James Smart2e0fef82007-06-17 19:56:36 -05009726 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009727 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009728 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009729 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009730 return 0;
9731}
9732
James Smarte59058c2008-08-24 21:49:00 -04009733/**
James Smart3621a712009-04-06 18:47:14 -04009734 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009735 * @phba: Pointer to HBA context object.
9736 *
9737 * When HBQ is enabled, buffers are searched based on tags. This function
9738 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9739 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9740 * does not conflict with tags of buffer posted for unsolicited events.
9741 * The function returns the allocated tag. The function is called with
9742 * no locks held.
9743 **/
James Smart76bb24e2007-10-27 13:38:00 -04009744uint32_t
9745lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9746{
9747 spin_lock_irq(&phba->hbalock);
9748 phba->buffer_tag_count++;
9749 /*
9750 * Always set the QUE_BUFTAG_BIT to distiguish between
9751 * a tag assigned by HBQ.
9752 */
9753 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9754 spin_unlock_irq(&phba->hbalock);
9755 return phba->buffer_tag_count;
9756}
9757
James Smarte59058c2008-08-24 21:49:00 -04009758/**
James Smart3621a712009-04-06 18:47:14 -04009759 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009760 * @phba: Pointer to HBA context object.
9761 * @pring: Pointer to driver SLI ring object.
9762 * @tag: Buffer tag.
9763 *
9764 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9765 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9766 * iocb is posted to the response ring with the tag of the buffer.
9767 * This function searches the pring->postbufq list using the tag
9768 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9769 * iocb. If the buffer is found then lpfc_dmabuf object of the
9770 * buffer is returned to the caller else NULL is returned.
9771 * This function is called with no lock held.
9772 **/
James Smart76bb24e2007-10-27 13:38:00 -04009773struct lpfc_dmabuf *
9774lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9775 uint32_t tag)
9776{
9777 struct lpfc_dmabuf *mp, *next_mp;
9778 struct list_head *slp = &pring->postbufq;
9779
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009780 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009781 spin_lock_irq(&phba->hbalock);
9782 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9783 if (mp->buffer_tag == tag) {
9784 list_del_init(&mp->list);
9785 pring->postbufq_cnt--;
9786 spin_unlock_irq(&phba->hbalock);
9787 return mp;
9788 }
9789 }
9790
9791 spin_unlock_irq(&phba->hbalock);
9792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009793 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009794 "ring %d Data x%lx x%p x%p x%x\n",
9795 pring->ringno, (unsigned long) tag,
9796 slp->next, slp->prev, pring->postbufq_cnt);
9797
9798 return NULL;
9799}
dea31012005-04-17 16:05:31 -05009800
James Smarte59058c2008-08-24 21:49:00 -04009801/**
James Smart3621a712009-04-06 18:47:14 -04009802 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009803 * @phba: Pointer to HBA context object.
9804 * @pring: Pointer to driver SLI ring object.
9805 * @phys: DMA address of the buffer.
9806 *
9807 * This function searches the buffer list using the dma_address
9808 * of unsolicited event to find the driver's lpfc_dmabuf object
9809 * corresponding to the dma_address. The function returns the
9810 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9811 * This function is called by the ct and els unsolicited event
9812 * handlers to get the buffer associated with the unsolicited
9813 * event.
9814 *
9815 * This function is called with no lock held.
9816 **/
dea31012005-04-17 16:05:31 -05009817struct lpfc_dmabuf *
9818lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9819 dma_addr_t phys)
9820{
9821 struct lpfc_dmabuf *mp, *next_mp;
9822 struct list_head *slp = &pring->postbufq;
9823
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009824 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009825 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009826 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9827 if (mp->phys == phys) {
9828 list_del_init(&mp->list);
9829 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009830 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009831 return mp;
9832 }
9833 }
9834
James Smart2e0fef82007-06-17 19:56:36 -05009835 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009837 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009838 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009839 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009840 slp->next, slp->prev, pring->postbufq_cnt);
9841 return NULL;
9842}
9843
James Smarte59058c2008-08-24 21:49:00 -04009844/**
James Smart3621a712009-04-06 18:47:14 -04009845 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009846 * @phba: Pointer to HBA context object.
9847 * @cmdiocb: Pointer to driver command iocb object.
9848 * @rspiocb: Pointer to driver response iocb object.
9849 *
9850 * This function is the completion handler for the abort iocbs for
9851 * ELS commands. This function is called from the ELS ring event
9852 * handler with no lock held. This function frees memory resources
9853 * associated with the abort iocb.
9854 **/
dea31012005-04-17 16:05:31 -05009855static void
James Smart2e0fef82007-06-17 19:56:36 -05009856lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9857 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009858{
James Smart2e0fef82007-06-17 19:56:36 -05009859 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009860 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009861 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009862
9863 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009864
9865 /*
9866 * Assume that the port already completed and returned, or
9867 * will return the iocb. Just Log the message.
9868 */
James Smart2680eea2007-04-25 09:52:55 -04009869 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9870 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9871
James Smart2e0fef82007-06-17 19:56:36 -05009872 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009873 if (phba->sli_rev < LPFC_SLI_REV4) {
9874 if (abort_iotag != 0 &&
9875 abort_iotag <= phba->sli.last_iotag)
9876 abort_iocb =
9877 phba->sli.iocbq_lookup[abort_iotag];
9878 } else
9879 /* For sli4 the abort_tag is the XRI,
9880 * so the abort routine puts the iotag of the iocb
9881 * being aborted in the context field of the abort
9882 * IOCB.
9883 */
9884 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009885
James Smart2a9bf3d2010-06-07 15:24:45 -04009886 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9887 "0327 Cannot abort els iocb %p "
9888 "with tag %x context %x, abort status %x, "
9889 "abort code %x\n",
9890 abort_iocb, abort_iotag, abort_context,
9891 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009892
James Smartff78d8f2011-12-13 13:21:35 -05009893 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009894 }
James Bottomley604a3e32005-10-29 10:28:33 -05009895 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009896 return;
9897}
9898
James Smarte59058c2008-08-24 21:49:00 -04009899/**
James Smart3621a712009-04-06 18:47:14 -04009900 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009901 * @phba: Pointer to HBA context object.
9902 * @cmdiocb: Pointer to driver command iocb object.
9903 * @rspiocb: Pointer to driver response iocb object.
9904 *
9905 * The function is called from SLI ring event handler with no
9906 * lock held. This function is the completion handler for ELS commands
9907 * which are aborted. The function frees memory resources used for
9908 * the aborted ELS commands.
9909 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009910static void
9911lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9912 struct lpfc_iocbq *rspiocb)
9913{
9914 IOCB_t *irsp = &rspiocb->iocb;
9915
9916 /* ELS cmd tag <ulpIoTag> completes */
9917 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009918 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009919 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009920 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009921 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009922 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9923 lpfc_ct_free_iocb(phba, cmdiocb);
9924 else
9925 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009926 return;
9927}
9928
James Smarte59058c2008-08-24 21:49:00 -04009929/**
James Smart5af5eee2010-10-22 11:06:38 -04009930 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009931 * @phba: Pointer to HBA context object.
9932 * @pring: Pointer to driver SLI ring object.
9933 * @cmdiocb: Pointer to driver command iocb object.
9934 *
James Smart5af5eee2010-10-22 11:06:38 -04009935 * This function issues an abort iocb for the provided command iocb down to
9936 * the port. Other than the case the outstanding command iocb is an abort
9937 * request, this function issues abort out unconditionally. This function is
9938 * called with hbalock held. The function returns 0 when it fails due to
9939 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009940 **/
James Smart5af5eee2010-10-22 11:06:38 -04009941static int
9942lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009943 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009944{
James Smart2e0fef82007-06-17 19:56:36 -05009945 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009946 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009947 IOCB_t *icmd = NULL;
9948 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009949 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009950 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009951 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009952
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009953 lockdep_assert_held(&phba->hbalock);
9954
James Smart92d7f7b2007-06-17 19:56:38 -05009955 /*
9956 * There are certain command types we don't want to abort. And we
9957 * don't want to abort commands that are already in the process of
9958 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009959 */
9960 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009961 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009962 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9963 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009964 return 0;
9965
dea31012005-04-17 16:05:31 -05009966 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009967 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009968 if (abtsiocbp == NULL)
9969 return 0;
dea31012005-04-17 16:05:31 -05009970
James Smart07951072007-04-25 09:51:38 -04009971 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009972 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009973 */
9974 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9975
dea31012005-04-17 16:05:31 -05009976 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009977 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9978 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009979 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009980 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009981 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9982 }
James Smartda0436e2009-05-22 14:51:39 -04009983 else
9984 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009985 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009986 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009987
James Smart5ffc2662009-11-18 15:39:44 -05009988 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9989 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009990 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9991 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04009992 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
9993 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05009994
James Smart2e0fef82007-06-17 19:56:36 -05009995 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009996 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9997 else
9998 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9999
10000 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -040010001
James Smarte8b62012007-08-02 11:10:09 -040010002 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10003 "0339 Abort xri x%x, original iotag x%x, "
10004 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010005 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010006 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010007 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010008
10009 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -040010010 ring_number =
10011 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10012 if (unlikely(ring_number == LPFC_HBA_ERROR))
10013 return 0;
10014 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -040010015 /* Note: both hbalock and ring_lock need to be set here */
10016 spin_lock_irqsave(&pring->ring_lock, iflags);
10017 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10018 abtsiocbp, 0);
10019 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10020 } else {
10021 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10022 abtsiocbp, 0);
10023 }
James Smart07951072007-04-25 09:51:38 -040010024
James Smartd7c255b2008-08-24 21:50:00 -040010025 if (retval)
10026 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010027
10028 /*
10029 * Caller to this routine should check for IOCB_ERROR
10030 * and handle it properly. This routine no longer removes
10031 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10032 */
10033 return retval;
10034}
10035
10036/**
10037 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10038 * @phba: Pointer to HBA context object.
10039 * @pring: Pointer to driver SLI ring object.
10040 * @cmdiocb: Pointer to driver command iocb object.
10041 *
10042 * This function issues an abort iocb for the provided command iocb. In case
10043 * of unloading, the abort iocb will not be issued to commands on the ELS
10044 * ring. Instead, the callback function shall be changed to those commands
10045 * so that nothing happens when them finishes. This function is called with
10046 * hbalock held. The function returns 0 when the command iocb is an abort
10047 * request.
10048 **/
10049int
10050lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10051 struct lpfc_iocbq *cmdiocb)
10052{
10053 struct lpfc_vport *vport = cmdiocb->vport;
10054 int retval = IOCB_ERROR;
10055 IOCB_t *icmd = NULL;
10056
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010057 lockdep_assert_held(&phba->hbalock);
10058
James Smart5af5eee2010-10-22 11:06:38 -040010059 /*
10060 * There are certain command types we don't want to abort. And we
10061 * don't want to abort commands that are already in the process of
10062 * being aborted.
10063 */
10064 icmd = &cmdiocb->iocb;
10065 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10066 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10067 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10068 return 0;
10069
10070 /*
10071 * If we're unloading, don't abort iocb on the ELS ring, but change
10072 * the callback so that nothing happens when it finishes.
10073 */
10074 if ((vport->load_flag & FC_UNLOADING) &&
10075 (pring->ringno == LPFC_ELS_RING)) {
10076 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10077 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10078 else
10079 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10080 goto abort_iotag_exit;
10081 }
10082
10083 /* Now, we try to issue the abort to the cmdiocb out */
10084 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10085
James Smart07951072007-04-25 09:51:38 -040010086abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010087 /*
10088 * Caller to this routine should check for IOCB_ERROR
10089 * and handle it properly. This routine no longer removes
10090 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010091 */
James Smart2e0fef82007-06-17 19:56:36 -050010092 return retval;
dea31012005-04-17 16:05:31 -050010093}
10094
James Smarte59058c2008-08-24 21:49:00 -040010095/**
James Smart5af5eee2010-10-22 11:06:38 -040010096 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10097 * @phba: pointer to lpfc HBA data structure.
10098 *
10099 * This routine will abort all pending and outstanding iocbs to an HBA.
10100 **/
10101void
10102lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10103{
10104 struct lpfc_sli *psli = &phba->sli;
10105 struct lpfc_sli_ring *pring;
10106 int i;
10107
10108 for (i = 0; i < psli->num_rings; i++) {
10109 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010110 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010111 }
10112}
10113
10114/**
James Smart3621a712009-04-06 18:47:14 -040010115 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010116 * @iocbq: Pointer to driver iocb object.
10117 * @vport: Pointer to driver virtual port object.
10118 * @tgt_id: SCSI ID of the target.
10119 * @lun_id: LUN ID of the scsi device.
10120 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10121 *
James Smart3621a712009-04-06 18:47:14 -040010122 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010123 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10124 * 0 if the filtering criteria is met for the given iocb and will return
10125 * 1 if the filtering criteria is not met.
10126 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10127 * given iocb is for the SCSI device specified by vport, tgt_id and
10128 * lun_id parameter.
10129 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10130 * given iocb is for the SCSI target specified by vport and tgt_id
10131 * parameters.
10132 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10133 * given iocb is for the SCSI host associated with the given vport.
10134 * This function is called with no locks held.
10135 **/
dea31012005-04-17 16:05:31 -050010136static int
James Smart51ef4c22007-08-02 11:10:31 -040010137lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10138 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010139 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010140{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010141 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010142 int rc = 1;
10143
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010144 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10145 return rc;
10146
James Smart51ef4c22007-08-02 11:10:31 -040010147 if (iocbq->vport != vport)
10148 return rc;
10149
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010150 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010151
James Smart495a7142008-06-14 22:52:59 -040010152 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010153 return rc;
10154
10155 switch (ctx_cmd) {
10156 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010157 if ((lpfc_cmd->rdata->pnode) &&
10158 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10159 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010160 rc = 0;
10161 break;
10162 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010163 if ((lpfc_cmd->rdata->pnode) &&
10164 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010165 rc = 0;
10166 break;
dea31012005-04-17 16:05:31 -050010167 case LPFC_CTX_HOST:
10168 rc = 0;
10169 break;
10170 default:
10171 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010172 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010173 break;
10174 }
10175
10176 return rc;
10177}
10178
James Smarte59058c2008-08-24 21:49:00 -040010179/**
James Smart3621a712009-04-06 18:47:14 -040010180 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010181 * @vport: Pointer to virtual port.
10182 * @tgt_id: SCSI ID of the target.
10183 * @lun_id: LUN ID of the scsi device.
10184 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10185 *
10186 * This function returns number of FCP commands pending for the vport.
10187 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10188 * commands pending on the vport associated with SCSI device specified
10189 * by tgt_id and lun_id parameters.
10190 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10191 * commands pending on the vport associated with SCSI target specified
10192 * by tgt_id parameter.
10193 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10194 * commands pending on the vport.
10195 * This function returns the number of iocbs which satisfy the filter.
10196 * This function is called without any lock held.
10197 **/
dea31012005-04-17 16:05:31 -050010198int
James Smart51ef4c22007-08-02 11:10:31 -040010199lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10200 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010201{
James Smart51ef4c22007-08-02 11:10:31 -040010202 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010203 struct lpfc_iocbq *iocbq;
10204 int sum, i;
dea31012005-04-17 16:05:31 -050010205
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010206 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10207 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010208
James Smart51ef4c22007-08-02 11:10:31 -040010209 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10210 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010211 sum++;
dea31012005-04-17 16:05:31 -050010212 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010213
dea31012005-04-17 16:05:31 -050010214 return sum;
10215}
10216
James Smarte59058c2008-08-24 21:49:00 -040010217/**
James Smart3621a712009-04-06 18:47:14 -040010218 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010219 * @phba: Pointer to HBA context object
10220 * @cmdiocb: Pointer to command iocb object.
10221 * @rspiocb: Pointer to response iocb object.
10222 *
10223 * This function is called when an aborted FCP iocb completes. This
10224 * function is called by the ring event handler with no lock held.
10225 * This function frees the iocb.
10226 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010227void
James Smart2e0fef82007-06-17 19:56:36 -050010228lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10229 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010230{
James Smartcb69f7d2011-12-13 13:21:57 -050010231 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010232 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010233 "original iotag x%x, abort cmd iotag x%x "
10234 "status 0x%x, reason 0x%x\n",
10235 cmdiocb->iocb.un.acxri.abortContextTag,
10236 cmdiocb->iocb.un.acxri.abortIoTag,
10237 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10238 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010239 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010240 return;
10241}
10242
James Smarte59058c2008-08-24 21:49:00 -040010243/**
James Smart3621a712009-04-06 18:47:14 -040010244 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010245 * @vport: Pointer to virtual port.
10246 * @pring: Pointer to driver SLI ring object.
10247 * @tgt_id: SCSI ID of the target.
10248 * @lun_id: LUN ID of the scsi device.
10249 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10250 *
10251 * This function sends an abort command for every SCSI command
10252 * associated with the given virtual port pending on the ring
10253 * filtered by lpfc_sli_validate_fcp_iocb function.
10254 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10255 * FCP iocbs associated with lun specified by tgt_id and lun_id
10256 * parameters
10257 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10258 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10259 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10260 * FCP iocbs associated with virtual port.
10261 * This function returns number of iocbs it failed to abort.
10262 * This function is called with no locks held.
10263 **/
dea31012005-04-17 16:05:31 -050010264int
James Smart51ef4c22007-08-02 11:10:31 -040010265lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10266 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010267{
James Smart51ef4c22007-08-02 11:10:31 -040010268 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010269 struct lpfc_iocbq *iocbq;
10270 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010271 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010272 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010273 int i;
dea31012005-04-17 16:05:31 -050010274
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010275 for (i = 1; i <= phba->sli.last_iotag; i++) {
10276 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010277
James Smart51ef4c22007-08-02 11:10:31 -040010278 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010279 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010280 continue;
10281
James Smartafbd8d82013-09-06 12:22:13 -040010282 /*
10283 * If the iocbq is already being aborted, don't take a second
10284 * action, but do count it.
10285 */
10286 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10287 continue;
10288
dea31012005-04-17 16:05:31 -050010289 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010290 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010291 if (abtsiocb == NULL) {
10292 errcnt++;
10293 continue;
10294 }
dea31012005-04-17 16:05:31 -050010295
James Smartafbd8d82013-09-06 12:22:13 -040010296 /* indicate the IO is being aborted by the driver. */
10297 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10298
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010299 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010300 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10301 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010302 if (phba->sli_rev == LPFC_SLI_REV4)
10303 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10304 else
10305 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010306 abtsiocb->iocb.ulpLe = 1;
10307 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010308 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010309
James Smart5ffc2662009-11-18 15:39:44 -050010310 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10311 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010312 if (iocbq->iocb_flag & LPFC_IO_FCP)
10313 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010314 if (iocbq->iocb_flag & LPFC_IO_FOF)
10315 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010316
James Smart2e0fef82007-06-17 19:56:36 -050010317 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010318 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10319 else
10320 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10321
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010322 /* Setup callback routine and issue the command. */
10323 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010324 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10325 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010326 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010327 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010328 errcnt++;
10329 continue;
10330 }
10331 }
10332
10333 return errcnt;
10334}
10335
James Smarte59058c2008-08-24 21:49:00 -040010336/**
James Smart98912dda2014-04-04 13:52:31 -040010337 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10338 * @vport: Pointer to virtual port.
10339 * @pring: Pointer to driver SLI ring object.
10340 * @tgt_id: SCSI ID of the target.
10341 * @lun_id: LUN ID of the scsi device.
10342 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10343 *
10344 * This function sends an abort command for every SCSI command
10345 * associated with the given virtual port pending on the ring
10346 * filtered by lpfc_sli_validate_fcp_iocb function.
10347 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10348 * FCP iocbs associated with lun specified by tgt_id and lun_id
10349 * parameters
10350 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10351 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10352 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10353 * FCP iocbs associated with virtual port.
10354 * This function returns number of iocbs it aborted .
10355 * This function is called with no locks held right after a taskmgmt
10356 * command is sent.
10357 **/
10358int
10359lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10360 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10361{
10362 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010363 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010364 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010365 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010366 struct lpfc_iocbq *iocbq;
10367 IOCB_t *icmd;
10368 int sum, i, ret_val;
10369 unsigned long iflags;
10370 struct lpfc_sli_ring *pring_s4;
10371 uint32_t ring_number;
10372
10373 spin_lock_irq(&phba->hbalock);
10374
10375 /* all I/Os are in process of being flushed */
10376 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10377 spin_unlock_irq(&phba->hbalock);
10378 return 0;
10379 }
10380 sum = 0;
10381
10382 for (i = 1; i <= phba->sli.last_iotag; i++) {
10383 iocbq = phba->sli.iocbq_lookup[i];
10384
10385 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10386 cmd) != 0)
10387 continue;
10388
10389 /*
10390 * If the iocbq is already being aborted, don't take a second
10391 * action, but do count it.
10392 */
10393 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10394 continue;
10395
10396 /* issue ABTS for this IOCB based on iotag */
10397 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10398 if (abtsiocbq == NULL)
10399 continue;
10400
10401 icmd = &iocbq->iocb;
10402 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10403 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10404 if (phba->sli_rev == LPFC_SLI_REV4)
10405 abtsiocbq->iocb.un.acxri.abortIoTag =
10406 iocbq->sli4_xritag;
10407 else
10408 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10409 abtsiocbq->iocb.ulpLe = 1;
10410 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10411 abtsiocbq->vport = vport;
10412
10413 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10414 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10415 if (iocbq->iocb_flag & LPFC_IO_FCP)
10416 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010417 if (iocbq->iocb_flag & LPFC_IO_FOF)
10418 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010419
James Smart8c50d252014-09-03 12:58:16 -040010420 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10421 ndlp = lpfc_cmd->rdata->pnode;
10422
10423 if (lpfc_is_link_up(phba) &&
10424 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010425 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10426 else
10427 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10428
10429 /* Setup callback routine and issue the command. */
10430 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10431
10432 /*
10433 * Indicate the IO is being aborted by the driver and set
10434 * the caller's flag into the aborted IO.
10435 */
10436 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10437
10438 if (phba->sli_rev == LPFC_SLI_REV4) {
10439 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10440 iocbq->fcp_wqidx;
10441 pring_s4 = &phba->sli.ring[ring_number];
10442 /* Note: both hbalock and ring_lock must be set here */
10443 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10444 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10445 abtsiocbq, 0);
10446 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10447 } else {
10448 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10449 abtsiocbq, 0);
10450 }
10451
10452
10453 if (ret_val == IOCB_ERROR)
10454 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10455 else
10456 sum++;
10457 }
10458 spin_unlock_irq(&phba->hbalock);
10459 return sum;
10460}
10461
10462/**
James Smart3621a712009-04-06 18:47:14 -040010463 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010464 * @phba: Pointer to HBA context object.
10465 * @cmdiocbq: Pointer to command iocb.
10466 * @rspiocbq: Pointer to response iocb.
10467 *
10468 * This function is the completion handler for iocbs issued using
10469 * lpfc_sli_issue_iocb_wait function. This function is called by the
10470 * ring event handler function without any lock held. This function
10471 * can be called from both worker thread context and interrupt
10472 * context. This function also can be called from other thread which
10473 * cleans up the SLI layer objects.
10474 * This function copy the contents of the response iocb to the
10475 * response iocb memory object provided by the caller of
10476 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10477 * sleeps for the iocb completion.
10478 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010479static void
10480lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10481 struct lpfc_iocbq *cmdiocbq,
10482 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010483{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010484 wait_queue_head_t *pdone_q;
10485 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010486 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010487
James Smart2e0fef82007-06-17 19:56:36 -050010488 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010489 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10490
10491 /*
10492 * A time out has occurred for the iocb. If a time out
10493 * completion handler has been supplied, call it. Otherwise,
10494 * just free the iocbq.
10495 */
10496
10497 spin_unlock_irqrestore(&phba->hbalock, iflags);
10498 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10499 cmdiocbq->wait_iocb_cmpl = NULL;
10500 if (cmdiocbq->iocb_cmpl)
10501 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10502 else
10503 lpfc_sli_release_iocbq(phba, cmdiocbq);
10504 return;
10505 }
10506
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010507 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10508 if (cmdiocbq->context2 && rspiocbq)
10509 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10510 &rspiocbq->iocb, sizeof(IOCB_t));
10511
James Smart0f65ff62010-02-26 14:14:23 -050010512 /* Set the exchange busy flag for task management commands */
10513 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10514 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10515 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10516 cur_iocbq);
10517 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10518 }
10519
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010520 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010521 if (pdone_q)
10522 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010523 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010524 return;
10525}
10526
James Smarte59058c2008-08-24 21:49:00 -040010527/**
James Smartd11e31d2009-06-10 17:23:06 -040010528 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10529 * @phba: Pointer to HBA context object..
10530 * @piocbq: Pointer to command iocb.
10531 * @flag: Flag to test.
10532 *
10533 * This routine grabs the hbalock and then test the iocb_flag to
10534 * see if the passed in flag is set.
10535 * Returns:
10536 * 1 if flag is set.
10537 * 0 if flag is not set.
10538 **/
10539static int
10540lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10541 struct lpfc_iocbq *piocbq, uint32_t flag)
10542{
10543 unsigned long iflags;
10544 int ret;
10545
10546 spin_lock_irqsave(&phba->hbalock, iflags);
10547 ret = piocbq->iocb_flag & flag;
10548 spin_unlock_irqrestore(&phba->hbalock, iflags);
10549 return ret;
10550
10551}
10552
10553/**
James Smart3621a712009-04-06 18:47:14 -040010554 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010555 * @phba: Pointer to HBA context object..
10556 * @pring: Pointer to sli ring.
10557 * @piocb: Pointer to command iocb.
10558 * @prspiocbq: Pointer to response iocb.
10559 * @timeout: Timeout in number of seconds.
10560 *
10561 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010562 * iocb to complete. The iocb_cmpl field of the shall be used
10563 * to handle iocbs which time out. If the field is NULL, the
10564 * function shall free the iocbq structure. If more clean up is
10565 * needed, the caller is expected to provide a completion function
10566 * that will provide the needed clean up. If the iocb command is
10567 * not completed within timeout seconds, the function will either
10568 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10569 * completion function set in the iocb_cmpl field and then return
10570 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10571 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010572 * The function waits for the iocb completion using an
10573 * non-interruptible wait.
10574 * This function will sleep while waiting for iocb completion.
10575 * So, this function should not be called from any context which
10576 * does not allow sleeping. Due to the same reason, this function
10577 * cannot be called with interrupt disabled.
10578 * This function assumes that the iocb completions occur while
10579 * this function sleep. So, this function cannot be called from
10580 * the thread which process iocb completion for this ring.
10581 * This function clears the iocb_flag of the iocb object before
10582 * issuing the iocb and the iocb completion handler sets this
10583 * flag and wakes this thread when the iocb completes.
10584 * The contents of the response iocb will be copied to prspiocbq
10585 * by the completion handler when the command completes.
10586 * This function returns IOCB_SUCCESS when success.
10587 * This function is called with no lock held.
10588 **/
dea31012005-04-17 16:05:31 -050010589int
James Smart2e0fef82007-06-17 19:56:36 -050010590lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010591 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010592 struct lpfc_iocbq *piocb,
10593 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010594 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010595{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010596 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010597 long timeleft, timeout_req = 0;
10598 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010599 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010600 struct lpfc_iocbq *iocb;
10601 int txq_cnt = 0;
10602 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010603 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010604 unsigned long iflags;
10605 bool iocb_completed = true;
10606
dea31012005-04-17 16:05:31 -050010607 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010608 * If the caller has provided a response iocbq buffer, then context2
10609 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010610 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010611 if (prspiocbq) {
10612 if (piocb->context2)
10613 return IOCB_ERROR;
10614 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010615 }
10616
James Smart5a0916b2013-07-15 18:31:42 -040010617 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010618 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10619 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010620 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010621
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010622 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010623 if (lpfc_readl(phba->HCregaddr, &creg_val))
10624 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010625 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10626 writel(creg_val, phba->HCregaddr);
10627 readl(phba->HCregaddr); /* flush */
10628 }
10629
James Smart2a9bf3d2010-06-07 15:24:45 -040010630 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10631 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010632 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010633 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010634 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010635 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010636 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010637 spin_lock_irqsave(&phba->hbalock, iflags);
10638 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010639
James Smart5a0916b2013-07-15 18:31:42 -040010640 /*
10641 * IOCB timed out. Inform the wake iocb wait
10642 * completion function and set local status
10643 */
10644
10645 iocb_completed = false;
10646 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10647 }
10648 spin_unlock_irqrestore(&phba->hbalock, iflags);
10649 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010650 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010651 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010652 /* Note: we are not indicating if the IOCB has a success
10653 * status or not - that's for the caller to check.
10654 * IOCB_SUCCESS means just that the command was sent and
10655 * completed. Not that it completed successfully.
10656 * */
James Smart7054a602007-04-25 09:52:34 -040010657 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010659 "0338 IOCB wait timeout error - no "
10660 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010661 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010662 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010663 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010664 "0330 IOCB wake NOT set, "
10665 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010666 timeout, (timeleft / jiffies));
10667 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010668 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010669 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010670 if (phba->cfg_log_verbose & LOG_SLI) {
10671 list_for_each_entry(iocb, &pring->txq, list) {
10672 txq_cnt++;
10673 }
10674 list_for_each_entry(iocb, &pring->txcmplq, list) {
10675 txcmplq_cnt++;
10676 }
10677 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10678 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10679 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10680 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010681 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010682 } else {
10683 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010684 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010685 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010686 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010687 }
10688
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010689 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010690 if (lpfc_readl(phba->HCregaddr, &creg_val))
10691 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010692 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10693 writel(creg_val, phba->HCregaddr);
10694 readl(phba->HCregaddr); /* flush */
10695 }
10696
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010697 if (prspiocbq)
10698 piocb->context2 = NULL;
10699
10700 piocb->context_un.wait_queue = NULL;
10701 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010702 return retval;
10703}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010704
James Smarte59058c2008-08-24 21:49:00 -040010705/**
James Smart3621a712009-04-06 18:47:14 -040010706 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010707 * @phba: Pointer to HBA context object.
10708 * @pmboxq: Pointer to driver mailbox object.
10709 * @timeout: Timeout in number of seconds.
10710 *
10711 * This function issues the mailbox to firmware and waits for the
10712 * mailbox command to complete. If the mailbox command is not
10713 * completed within timeout seconds, it returns MBX_TIMEOUT.
10714 * The function waits for the mailbox completion using an
10715 * interruptible wait. If the thread is woken up due to a
10716 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10717 * should not free the mailbox resources, if this function returns
10718 * MBX_TIMEOUT.
10719 * This function will sleep while waiting for mailbox completion.
10720 * So, this function should not be called from any context which
10721 * does not allow sleeping. Due to the same reason, this function
10722 * cannot be called with interrupt disabled.
10723 * This function assumes that the mailbox completion occurs while
10724 * this function sleep. So, this function cannot be called from
10725 * the worker thread which processes mailbox completion.
10726 * This function is called in the context of HBA management
10727 * applications.
10728 * This function returns MBX_SUCCESS when successful.
10729 * This function is called with no lock held.
10730 **/
dea31012005-04-17 16:05:31 -050010731int
James Smart2e0fef82007-06-17 19:56:36 -050010732lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010733 uint32_t timeout)
10734{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010735 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010736 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010737 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010738 unsigned long flag;
dea31012005-04-17 16:05:31 -050010739
James Smartb230b8a2013-05-31 17:05:27 -040010740 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010741 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010742 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010743
James Smart495a7142008-06-14 22:52:59 -040010744 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010745 /* setup wake call as IOCB callback */
10746 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10747 /* setup context field to pass wait_queue pointer to wake function */
10748 pmboxq->context1 = &done_q;
10749
dea31012005-04-17 16:05:31 -050010750 /* now issue the command */
10751 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010752 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010753 wait_event_interruptible_timeout(done_q,
10754 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010755 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010756
James Smart858c9f62007-06-17 19:56:39 -050010757 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010758 /* restore the possible extended buffer for free resource */
10759 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010760 /*
10761 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10762 * else do not free the resources.
10763 */
James Smartd7c47992010-06-08 18:31:54 -040010764 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010765 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010766 } else {
James Smart7054a602007-04-25 09:52:34 -040010767 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010768 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10769 }
10770 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010771 } else {
10772 /* restore the possible extended buffer for free resource */
10773 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010774 }
10775
dea31012005-04-17 16:05:31 -050010776 return retval;
10777}
10778
James Smarte59058c2008-08-24 21:49:00 -040010779/**
James Smart3772a992009-05-22 14:50:54 -040010780 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010781 * @phba: Pointer to HBA context.
10782 *
James Smart3772a992009-05-22 14:50:54 -040010783 * This function is called to shutdown the driver's mailbox sub-system.
10784 * It first marks the mailbox sub-system is in a block state to prevent
10785 * the asynchronous mailbox command from issued off the pending mailbox
10786 * command queue. If the mailbox command sub-system shutdown is due to
10787 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10788 * the mailbox sub-system flush routine to forcefully bring down the
10789 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10790 * as with offline or HBA function reset), this routine will wait for the
10791 * outstanding mailbox command to complete before invoking the mailbox
10792 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010793 **/
James Smart3772a992009-05-22 14:50:54 -040010794void
James Smart618a5232012-06-12 13:54:36 -040010795lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010796{
James Smart3772a992009-05-22 14:50:54 -040010797 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010798 unsigned long timeout;
10799
James Smart618a5232012-06-12 13:54:36 -040010800 if (mbx_action == LPFC_MBX_NO_WAIT) {
10801 /* delay 100ms for port state */
10802 msleep(100);
10803 lpfc_sli_mbox_sys_flush(phba);
10804 return;
10805 }
James Smarta183a152011-10-10 21:32:43 -040010806 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010807
James Smart3772a992009-05-22 14:50:54 -040010808 spin_lock_irq(&phba->hbalock);
10809 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010810
10811 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010812 /* Determine how long we might wait for the active mailbox
10813 * command to be gracefully completed by firmware.
10814 */
James Smarta183a152011-10-10 21:32:43 -040010815 if (phba->sli.mbox_active)
10816 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10817 phba->sli.mbox_active) *
10818 1000) + jiffies;
10819 spin_unlock_irq(&phba->hbalock);
10820
James Smart3772a992009-05-22 14:50:54 -040010821 while (phba->sli.mbox_active) {
10822 /* Check active mailbox complete status every 2ms */
10823 msleep(2);
10824 if (time_after(jiffies, timeout))
10825 /* Timeout, let the mailbox flush routine to
10826 * forcefully release active mailbox command
10827 */
10828 break;
10829 }
James Smartd7069f02012-03-01 22:36:29 -050010830 } else
10831 spin_unlock_irq(&phba->hbalock);
10832
James Smart3772a992009-05-22 14:50:54 -040010833 lpfc_sli_mbox_sys_flush(phba);
10834}
10835
10836/**
10837 * lpfc_sli_eratt_read - read sli-3 error attention events
10838 * @phba: Pointer to HBA context.
10839 *
10840 * This function is called to read the SLI3 device error attention registers
10841 * for possible error attention events. The caller must hold the hostlock
10842 * with spin_lock_irq().
10843 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010844 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010845 * Register and returns 0 otherwise.
10846 **/
10847static int
10848lpfc_sli_eratt_read(struct lpfc_hba *phba)
10849{
James Smarted957682007-06-17 19:56:37 -050010850 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010851
James Smart3772a992009-05-22 14:50:54 -040010852 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010853 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10854 goto unplug_err;
10855
James Smart3772a992009-05-22 14:50:54 -040010856 if (ha_copy & HA_ERATT) {
10857 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010858 if (lpfc_sli_read_hs(phba))
10859 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010860
James Smart3772a992009-05-22 14:50:54 -040010861 /* Check if there is a deferred error condition is active */
10862 if ((HS_FFER1 & phba->work_hs) &&
10863 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010864 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010865 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010866 /* Clear all interrupt enable conditions */
10867 writel(0, phba->HCregaddr);
10868 readl(phba->HCregaddr);
10869 }
10870
10871 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010872 phba->work_ha |= HA_ERATT;
10873 /* Indicate polling handles this ERATT */
10874 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010875 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010876 }
James Smart3772a992009-05-22 14:50:54 -040010877 return 0;
James Smart9940b972011-03-11 16:06:12 -050010878
10879unplug_err:
10880 /* Set the driver HS work bitmap */
10881 phba->work_hs |= UNPLUG_ERR;
10882 /* Set the driver HA work bitmap */
10883 phba->work_ha |= HA_ERATT;
10884 /* Indicate polling handles this ERATT */
10885 phba->hba_flag |= HBA_ERATT_HANDLED;
10886 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010887}
10888
James Smarte59058c2008-08-24 21:49:00 -040010889/**
James Smartda0436e2009-05-22 14:51:39 -040010890 * lpfc_sli4_eratt_read - read sli-4 error attention events
10891 * @phba: Pointer to HBA context.
10892 *
10893 * This function is called to read the SLI4 device error attention registers
10894 * for possible error attention events. The caller must hold the hostlock
10895 * with spin_lock_irq().
10896 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010897 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010898 * Register and returns 0 otherwise.
10899 **/
10900static int
10901lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10902{
10903 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010904 uint32_t if_type, portsmphr;
10905 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010906
James Smart2fcee4b2010-12-15 17:57:46 -050010907 /*
10908 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010909 * registers for error attention. This can be changed later.
10910 */
James Smart2fcee4b2010-12-15 17:57:46 -050010911 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10912 switch (if_type) {
10913 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010914 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10915 &uerr_sta_lo) ||
10916 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10917 &uerr_sta_hi)) {
10918 phba->work_hs |= UNPLUG_ERR;
10919 phba->work_ha |= HA_ERATT;
10920 phba->hba_flag |= HBA_ERATT_HANDLED;
10921 return 1;
10922 }
James Smart2fcee4b2010-12-15 17:57:46 -050010923 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10924 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10926 "1423 HBA Unrecoverable error: "
10927 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10928 "ue_mask_lo_reg=0x%x, "
10929 "ue_mask_hi_reg=0x%x\n",
10930 uerr_sta_lo, uerr_sta_hi,
10931 phba->sli4_hba.ue_mask_lo,
10932 phba->sli4_hba.ue_mask_hi);
10933 phba->work_status[0] = uerr_sta_lo;
10934 phba->work_status[1] = uerr_sta_hi;
10935 phba->work_ha |= HA_ERATT;
10936 phba->hba_flag |= HBA_ERATT_HANDLED;
10937 return 1;
10938 }
10939 break;
10940 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010941 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10942 &portstat_reg.word0) ||
10943 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10944 &portsmphr)){
10945 phba->work_hs |= UNPLUG_ERR;
10946 phba->work_ha |= HA_ERATT;
10947 phba->hba_flag |= HBA_ERATT_HANDLED;
10948 return 1;
10949 }
James Smart2fcee4b2010-12-15 17:57:46 -050010950 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10951 phba->work_status[0] =
10952 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10953 phba->work_status[1] =
10954 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010956 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010957 "port status reg 0x%x, "
10958 "port smphr reg 0x%x, "
10959 "error 1=0x%x, error 2=0x%x\n",
10960 portstat_reg.word0,
10961 portsmphr,
10962 phba->work_status[0],
10963 phba->work_status[1]);
10964 phba->work_ha |= HA_ERATT;
10965 phba->hba_flag |= HBA_ERATT_HANDLED;
10966 return 1;
10967 }
10968 break;
10969 case LPFC_SLI_INTF_IF_TYPE_1:
10970 default:
James Smarta747c9c2009-11-18 15:41:10 -050010971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010972 "2886 HBA Error Attention on unsupported "
10973 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010974 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010975 }
James Smart2fcee4b2010-12-15 17:57:46 -050010976
James Smartda0436e2009-05-22 14:51:39 -040010977 return 0;
10978}
10979
10980/**
James Smart3621a712009-04-06 18:47:14 -040010981 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010982 * @phba: Pointer to HBA context.
10983 *
James Smart3772a992009-05-22 14:50:54 -040010984 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010985 * error attention register bit for error attention events.
10986 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010987 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010988 * Register and returns 0 otherwise.
10989 **/
10990int
10991lpfc_sli_check_eratt(struct lpfc_hba *phba)
10992{
10993 uint32_t ha_copy;
10994
10995 /* If somebody is waiting to handle an eratt, don't process it
10996 * here. The brdkill function will do this.
10997 */
10998 if (phba->link_flag & LS_IGNORE_ERATT)
10999 return 0;
11000
11001 /* Check if interrupt handler handles this ERATT */
11002 spin_lock_irq(&phba->hbalock);
11003 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11004 /* Interrupt handler has handled ERATT */
11005 spin_unlock_irq(&phba->hbalock);
11006 return 0;
11007 }
11008
James Smarta257bf92009-04-06 18:48:10 -040011009 /*
11010 * If there is deferred error attention, do not check for error
11011 * attention
11012 */
11013 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11014 spin_unlock_irq(&phba->hbalock);
11015 return 0;
11016 }
11017
James Smart3772a992009-05-22 14:50:54 -040011018 /* If PCI channel is offline, don't process it */
11019 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011020 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011021 return 0;
11022 }
11023
11024 switch (phba->sli_rev) {
11025 case LPFC_SLI_REV2:
11026 case LPFC_SLI_REV3:
11027 /* Read chip Host Attention (HA) register */
11028 ha_copy = lpfc_sli_eratt_read(phba);
11029 break;
James Smartda0436e2009-05-22 14:51:39 -040011030 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011031 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011032 ha_copy = lpfc_sli4_eratt_read(phba);
11033 break;
James Smart3772a992009-05-22 14:50:54 -040011034 default:
11035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11036 "0299 Invalid SLI revision (%d)\n",
11037 phba->sli_rev);
11038 ha_copy = 0;
11039 break;
James Smart93996272008-08-24 21:50:30 -040011040 }
11041 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011042
11043 return ha_copy;
11044}
11045
11046/**
11047 * lpfc_intr_state_check - Check device state for interrupt handling
11048 * @phba: Pointer to HBA context.
11049 *
11050 * This inline routine checks whether a device or its PCI slot is in a state
11051 * that the interrupt should be handled.
11052 *
11053 * This function returns 0 if the device or the PCI slot is in a state that
11054 * interrupt should be handled, otherwise -EIO.
11055 */
11056static inline int
11057lpfc_intr_state_check(struct lpfc_hba *phba)
11058{
11059 /* If the pci channel is offline, ignore all the interrupts */
11060 if (unlikely(pci_channel_offline(phba->pcidev)))
11061 return -EIO;
11062
11063 /* Update device level interrupt statistics */
11064 phba->sli.slistat.sli_intr++;
11065
11066 /* Ignore all interrupts during initialization. */
11067 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11068 return -EIO;
11069
James Smart93996272008-08-24 21:50:30 -040011070 return 0;
11071}
11072
11073/**
James Smart3772a992009-05-22 14:50:54 -040011074 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011075 * @irq: Interrupt number.
11076 * @dev_id: The device context pointer.
11077 *
James Smart93996272008-08-24 21:50:30 -040011078 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011079 * service routine when device with SLI-3 interface spec is enabled with
11080 * MSI-X multi-message interrupt mode and there are slow-path events in
11081 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11082 * interrupt mode, this function is called as part of the device-level
11083 * interrupt handler. When the PCI slot is in error recovery or the HBA
11084 * is undergoing initialization, the interrupt handler will not process
11085 * the interrupt. The link attention and ELS ring attention events are
11086 * handled by the worker thread. The interrupt handler signals the worker
11087 * thread and returns for these events. This function is called without
11088 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011089 * structures.
11090 *
11091 * This function returns IRQ_HANDLED when interrupt is handled else it
11092 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011093 **/
dea31012005-04-17 16:05:31 -050011094irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011095lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011096{
James Smart2e0fef82007-06-17 19:56:36 -050011097 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011098 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011099 uint32_t work_ha_copy;
11100 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011101 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011102 uint32_t control;
11103
James Smart92d7f7b2007-06-17 19:56:38 -050011104 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011105 struct lpfc_vport *vport;
11106 struct lpfc_nodelist *ndlp;
11107 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011108 LPFC_MBOXQ_t *pmb;
11109 int rc;
11110
dea31012005-04-17 16:05:31 -050011111 /*
11112 * Get the driver's phba structure from the dev_id and
11113 * assume the HBA is not interrupting.
11114 */
James Smart93996272008-08-24 21:50:30 -040011115 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011116
11117 if (unlikely(!phba))
11118 return IRQ_NONE;
11119
dea31012005-04-17 16:05:31 -050011120 /*
James Smart93996272008-08-24 21:50:30 -040011121 * Stuff needs to be attented to when this function is invoked as an
11122 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011123 */
James Smart93996272008-08-24 21:50:30 -040011124 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011125 /* Check device state for handling interrupt */
11126 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011127 return IRQ_NONE;
11128 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011129 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011130 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11131 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011132 /* If somebody is waiting to handle an eratt don't process it
11133 * here. The brdkill function will do this.
11134 */
11135 if (phba->link_flag & LS_IGNORE_ERATT)
11136 ha_copy &= ~HA_ERATT;
11137 /* Check the need for handling ERATT in interrupt handler */
11138 if (ha_copy & HA_ERATT) {
11139 if (phba->hba_flag & HBA_ERATT_HANDLED)
11140 /* ERATT polling has handled ERATT */
11141 ha_copy &= ~HA_ERATT;
11142 else
11143 /* Indicate interrupt handler handles ERATT */
11144 phba->hba_flag |= HBA_ERATT_HANDLED;
11145 }
James Smarta257bf92009-04-06 18:48:10 -040011146
11147 /*
11148 * If there is deferred error attention, do not check for any
11149 * interrupt.
11150 */
11151 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011152 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011153 return IRQ_NONE;
11154 }
11155
James Smart93996272008-08-24 21:50:30 -040011156 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011157 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11158 goto unplug_error;
11159
James Smarta747c9c2009-11-18 15:41:10 -050011160 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11161 HC_LAINT_ENA | HC_ERINT_ENA),
11162 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011163 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11164 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011165 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011166 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011167 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011168 } else
11169 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011170
dea31012005-04-17 16:05:31 -050011171 work_ha_copy = ha_copy & phba->work_ha_mask;
11172
James Smart93996272008-08-24 21:50:30 -040011173 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011174 if (work_ha_copy & HA_LATT) {
11175 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11176 /*
11177 * Turn off Link Attention interrupts
11178 * until CLEAR_LA done
11179 */
James Smart5b75da22008-12-04 22:39:35 -050011180 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011181 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011182 if (lpfc_readl(phba->HCregaddr, &control))
11183 goto unplug_error;
dea31012005-04-17 16:05:31 -050011184 control &= ~HC_LAINT_ENA;
11185 writel(control, phba->HCregaddr);
11186 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011187 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011188 }
11189 else
11190 work_ha_copy &= ~HA_LATT;
11191 }
11192
James Smart93996272008-08-24 21:50:30 -040011193 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011194 /*
11195 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11196 * the only slow ring.
11197 */
11198 status = (work_ha_copy &
11199 (HA_RXMASK << (4*LPFC_ELS_RING)));
11200 status >>= (4*LPFC_ELS_RING);
11201 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011202 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011203 if (lpfc_readl(phba->HCregaddr, &control))
11204 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011205
11206 lpfc_debugfs_slow_ring_trc(phba,
11207 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11208 control, status,
11209 (uint32_t)phba->sli.slistat.sli_intr);
11210
James Smart858c9f62007-06-17 19:56:39 -050011211 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011212 lpfc_debugfs_slow_ring_trc(phba,
11213 "ISR Disable ring:"
11214 "pwork:x%x hawork:x%x wait:x%x",
11215 phba->work_ha, work_ha_copy,
11216 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011217 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011218
James Smart858c9f62007-06-17 19:56:39 -050011219 control &=
11220 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011221 writel(control, phba->HCregaddr);
11222 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011223 }
James Smarta58cbd52007-08-02 11:09:43 -040011224 else {
11225 lpfc_debugfs_slow_ring_trc(phba,
11226 "ISR slow ring: pwork:"
11227 "x%x hawork:x%x wait:x%x",
11228 phba->work_ha, work_ha_copy,
11229 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011230 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011231 }
James Smart5b75da22008-12-04 22:39:35 -050011232 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011233 }
11234 }
James Smart5b75da22008-12-04 22:39:35 -050011235 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011236 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011237 if (lpfc_sli_read_hs(phba))
11238 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011239 /*
11240 * Check if there is a deferred error condition
11241 * is active
11242 */
11243 if ((HS_FFER1 & phba->work_hs) &&
11244 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011245 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11246 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011247 phba->hba_flag |= DEFER_ERATT;
11248 /* Clear all interrupt enable conditions */
11249 writel(0, phba->HCregaddr);
11250 readl(phba->HCregaddr);
11251 }
11252 }
11253
James Smart93996272008-08-24 21:50:30 -040011254 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011255 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011256 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011257 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011258 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011259
11260 /* First check out the status word */
11261 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11262 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011263 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011264 /*
11265 * Stray Mailbox Interrupt, mbxCommand <cmd>
11266 * mbxStatus <status>
11267 */
James Smart09372822008-01-11 01:52:54 -050011268 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011269 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011270 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011271 "Interrupt mbxCommand x%x "
11272 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011273 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011274 pmbox->mbxCommand,
11275 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011276 /* clear mailbox attention bit */
11277 work_ha_copy &= ~HA_MBATT;
11278 } else {
James Smart97eab632008-04-07 10:16:05 -040011279 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011280 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011281 phba->last_completion_time = jiffies;
11282 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011283 if (pmb->mbox_cmpl) {
11284 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11285 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011286 if (pmb->out_ext_byte_len &&
11287 pmb->context2)
11288 lpfc_sli_pcimem_bcopy(
11289 phba->mbox_ext,
11290 pmb->context2,
11291 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011292 }
James Smart09372822008-01-11 01:52:54 -050011293 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11294 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11295
11296 lpfc_debugfs_disc_trc(vport,
11297 LPFC_DISC_TRC_MBOX_VPORT,
11298 "MBOX dflt rpi: : "
11299 "status:x%x rpi:x%x",
11300 (uint32_t)pmbox->mbxStatus,
11301 pmbox->un.varWords[0], 0);
11302
11303 if (!pmbox->mbxStatus) {
11304 mp = (struct lpfc_dmabuf *)
11305 (pmb->context1);
11306 ndlp = (struct lpfc_nodelist *)
11307 pmb->context2;
11308
11309 /* Reg_LOGIN of dflt RPI was
11310 * successful. new lets get
11311 * rid of the RPI using the
11312 * same mbox buffer.
11313 */
11314 lpfc_unreg_login(phba,
11315 vport->vpi,
11316 pmbox->un.varWords[0],
11317 pmb);
11318 pmb->mbox_cmpl =
11319 lpfc_mbx_cmpl_dflt_rpi;
11320 pmb->context1 = mp;
11321 pmb->context2 = ndlp;
11322 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011323 rc = lpfc_sli_issue_mbox(phba,
11324 pmb,
11325 MBX_NOWAIT);
11326 if (rc != MBX_BUSY)
11327 lpfc_printf_log(phba,
11328 KERN_ERR,
11329 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011330 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011331 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011332 if (rc != MBX_NOT_FINISHED)
11333 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011334 }
11335 }
James Smart5b75da22008-12-04 22:39:35 -050011336 spin_lock_irqsave(
11337 &phba->pport->work_port_lock,
11338 iflag);
James Smart09372822008-01-11 01:52:54 -050011339 phba->pport->work_port_events &=
11340 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011341 spin_unlock_irqrestore(
11342 &phba->pport->work_port_lock,
11343 iflag);
James Smart09372822008-01-11 01:52:54 -050011344 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011345 }
James Smart97eab632008-04-07 10:16:05 -040011346 } else
James Smart5b75da22008-12-04 22:39:35 -050011347 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011348
James Smart92d7f7b2007-06-17 19:56:38 -050011349 if ((work_ha_copy & HA_MBATT) &&
11350 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011351send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011352 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011353 do {
11354 rc = lpfc_sli_issue_mbox(phba, NULL,
11355 MBX_NOWAIT);
11356 } while (rc == MBX_NOT_FINISHED);
11357 if (rc != MBX_SUCCESS)
11358 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11359 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011360 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011361 }
11362
James Smart5b75da22008-12-04 22:39:35 -050011363 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011364 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011365 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011366 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011367 }
James Smart93996272008-08-24 21:50:30 -040011368 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011369unplug_error:
11370 spin_unlock_irqrestore(&phba->hbalock, iflag);
11371 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011372
James Smart3772a992009-05-22 14:50:54 -040011373} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011374
11375/**
James Smart3772a992009-05-22 14:50:54 -040011376 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011377 * @irq: Interrupt number.
11378 * @dev_id: The device context pointer.
11379 *
11380 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011381 * service routine when device with SLI-3 interface spec is enabled with
11382 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11383 * ring event in the HBA. However, when the device is enabled with either
11384 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11385 * device-level interrupt handler. When the PCI slot is in error recovery
11386 * or the HBA is undergoing initialization, the interrupt handler will not
11387 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11388 * the intrrupt context. This function is called without any lock held.
11389 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011390 *
11391 * This function returns IRQ_HANDLED when interrupt is handled else it
11392 * returns IRQ_NONE.
11393 **/
11394irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011395lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011396{
11397 struct lpfc_hba *phba;
11398 uint32_t ha_copy;
11399 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011400 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011401
11402 /* Get the driver's phba structure from the dev_id and
11403 * assume the HBA is not interrupting.
11404 */
11405 phba = (struct lpfc_hba *) dev_id;
11406
11407 if (unlikely(!phba))
11408 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011409
11410 /*
James Smart93996272008-08-24 21:50:30 -040011411 * Stuff needs to be attented to when this function is invoked as an
11412 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011413 */
James Smart93996272008-08-24 21:50:30 -040011414 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011415 /* Check device state for handling interrupt */
11416 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011417 return IRQ_NONE;
11418 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011419 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11420 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011421 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011422 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011423 /*
11424 * If there is deferred error attention, do not check for
11425 * any interrupt.
11426 */
11427 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011428 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011429 return IRQ_NONE;
11430 }
James Smart93996272008-08-24 21:50:30 -040011431 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11432 phba->HAregaddr);
11433 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011434 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011435 } else
11436 ha_copy = phba->ha_copy;
11437
11438 /*
11439 * Process all events on FCP ring. Take the optimized path for FCP IO.
11440 */
11441 ha_copy &= ~(phba->work_ha_mask);
11442
11443 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011444 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011445 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011446 lpfc_sli_handle_fast_ring_event(phba,
11447 &phba->sli.ring[LPFC_FCP_RING],
11448 status);
James Smarta4bc3372006-12-02 13:34:16 -050011449
11450 if (phba->cfg_multi_ring_support == 2) {
11451 /*
James Smart93996272008-08-24 21:50:30 -040011452 * Process all events on extra ring. Take the optimized path
11453 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011454 */
James Smart93996272008-08-24 21:50:30 -040011455 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011456 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011457 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011458 lpfc_sli_handle_fast_ring_event(phba,
11459 &phba->sli.ring[LPFC_EXTRA_RING],
11460 status);
11461 }
11462 }
dea31012005-04-17 16:05:31 -050011463 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011464} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011465
James Smart93996272008-08-24 21:50:30 -040011466/**
James Smart3772a992009-05-22 14:50:54 -040011467 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011468 * @irq: Interrupt number.
11469 * @dev_id: The device context pointer.
11470 *
James Smart3772a992009-05-22 14:50:54 -040011471 * This function is the HBA device-level interrupt handler to device with
11472 * SLI-3 interface spec, called from the PCI layer when either MSI or
11473 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11474 * requires driver attention. This function invokes the slow-path interrupt
11475 * attention handling function and fast-path interrupt attention handling
11476 * function in turn to process the relevant HBA attention events. This
11477 * function is called without any lock held. It gets the hbalock to access
11478 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011479 *
11480 * This function returns IRQ_HANDLED when interrupt is handled, else it
11481 * returns IRQ_NONE.
11482 **/
11483irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011484lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011485{
11486 struct lpfc_hba *phba;
11487 irqreturn_t sp_irq_rc, fp_irq_rc;
11488 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011489 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011490
11491 /*
11492 * Get the driver's phba structure from the dev_id and
11493 * assume the HBA is not interrupting.
11494 */
11495 phba = (struct lpfc_hba *) dev_id;
11496
11497 if (unlikely(!phba))
11498 return IRQ_NONE;
11499
James Smart3772a992009-05-22 14:50:54 -040011500 /* Check device state for handling interrupt */
11501 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011502 return IRQ_NONE;
11503
11504 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011505 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11506 spin_unlock(&phba->hbalock);
11507 return IRQ_HANDLED;
11508 }
11509
James Smart93996272008-08-24 21:50:30 -040011510 if (unlikely(!phba->ha_copy)) {
11511 spin_unlock(&phba->hbalock);
11512 return IRQ_NONE;
11513 } else if (phba->ha_copy & HA_ERATT) {
11514 if (phba->hba_flag & HBA_ERATT_HANDLED)
11515 /* ERATT polling has handled ERATT */
11516 phba->ha_copy &= ~HA_ERATT;
11517 else
11518 /* Indicate interrupt handler handles ERATT */
11519 phba->hba_flag |= HBA_ERATT_HANDLED;
11520 }
11521
James Smarta257bf92009-04-06 18:48:10 -040011522 /*
11523 * If there is deferred error attention, do not check for any interrupt.
11524 */
11525 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011526 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011527 return IRQ_NONE;
11528 }
11529
James Smart93996272008-08-24 21:50:30 -040011530 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011531 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11532 spin_unlock(&phba->hbalock);
11533 return IRQ_HANDLED;
11534 }
James Smarta747c9c2009-11-18 15:41:10 -050011535 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11536 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11537 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011538 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011539 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011540 readl(phba->HAregaddr); /* flush */
11541 spin_unlock(&phba->hbalock);
11542
11543 /*
11544 * Invokes slow-path host attention interrupt handling as appropriate.
11545 */
11546
11547 /* status of events with mailbox and link attention */
11548 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11549
11550 /* status of events with ELS ring */
11551 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11552 status2 >>= (4*LPFC_ELS_RING);
11553
11554 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011555 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011556 else
11557 sp_irq_rc = IRQ_NONE;
11558
11559 /*
11560 * Invoke fast-path host attention interrupt handling as appropriate.
11561 */
11562
11563 /* status of events with FCP ring */
11564 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11565 status1 >>= (4*LPFC_FCP_RING);
11566
11567 /* status of events with extra ring */
11568 if (phba->cfg_multi_ring_support == 2) {
11569 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11570 status2 >>= (4*LPFC_EXTRA_RING);
11571 } else
11572 status2 = 0;
11573
11574 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011575 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011576 else
11577 fp_irq_rc = IRQ_NONE;
11578
11579 /* Return device-level interrupt handling status */
11580 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011581} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011582
11583/**
11584 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11585 * @phba: pointer to lpfc hba data structure.
11586 *
11587 * This routine is invoked by the worker thread to process all the pending
11588 * SLI4 FCP abort XRI events.
11589 **/
11590void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11591{
11592 struct lpfc_cq_event *cq_event;
11593
11594 /* First, declare the fcp xri abort event has been handled */
11595 spin_lock_irq(&phba->hbalock);
11596 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11597 spin_unlock_irq(&phba->hbalock);
11598 /* Now, handle all the fcp xri abort events */
11599 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11600 /* Get the first event from the head of the event queue */
11601 spin_lock_irq(&phba->hbalock);
11602 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11603 cq_event, struct lpfc_cq_event, list);
11604 spin_unlock_irq(&phba->hbalock);
11605 /* Notify aborted XRI for FCP work queue */
11606 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11607 /* Free the event processed back to the free pool */
11608 lpfc_sli4_cq_event_release(phba, cq_event);
11609 }
11610}
11611
11612/**
11613 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11614 * @phba: pointer to lpfc hba data structure.
11615 *
11616 * This routine is invoked by the worker thread to process all the pending
11617 * SLI4 els abort xri events.
11618 **/
11619void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11620{
11621 struct lpfc_cq_event *cq_event;
11622
11623 /* First, declare the els xri abort event has been handled */
11624 spin_lock_irq(&phba->hbalock);
11625 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11626 spin_unlock_irq(&phba->hbalock);
11627 /* Now, handle all the els xri abort events */
11628 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11629 /* Get the first event from the head of the event queue */
11630 spin_lock_irq(&phba->hbalock);
11631 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11632 cq_event, struct lpfc_cq_event, list);
11633 spin_unlock_irq(&phba->hbalock);
11634 /* Notify aborted XRI for ELS work queue */
11635 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11636 /* Free the event processed back to the free pool */
11637 lpfc_sli4_cq_event_release(phba, cq_event);
11638 }
11639}
11640
James Smart341af102010-01-26 23:07:37 -050011641/**
11642 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11643 * @phba: pointer to lpfc hba data structure
11644 * @pIocbIn: pointer to the rspiocbq
11645 * @pIocbOut: pointer to the cmdiocbq
11646 * @wcqe: pointer to the complete wcqe
11647 *
11648 * This routine transfers the fields of a command iocbq to a response iocbq
11649 * by copying all the IOCB fields from command iocbq and transferring the
11650 * completion status information from the complete wcqe.
11651 **/
James Smart4f774512009-05-22 14:52:35 -040011652static void
James Smart341af102010-01-26 23:07:37 -050011653lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11654 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011655 struct lpfc_iocbq *pIocbOut,
11656 struct lpfc_wcqe_complete *wcqe)
11657{
James Smartaf227412013-10-10 12:23:10 -040011658 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011659 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011660 uint32_t status, max_response;
11661 struct lpfc_dmabuf *dmabuf;
11662 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011663 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11664
11665 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11666 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011667 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011668 status = bf_get(lpfc_wcqe_c_status, wcqe);
11669 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011670 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11671 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11672 pIocbIn->iocb.un.fcpi.fcpi_parm =
11673 pIocbOut->iocb.un.fcpi.fcpi_parm -
11674 wcqe->total_data_placed;
11675 else
11676 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011677 else {
James Smart4f774512009-05-22 14:52:35 -040011678 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011679 switch (pIocbOut->iocb.ulpCommand) {
11680 case CMD_ELS_REQUEST64_CR:
11681 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11682 bpl = (struct ulp_bde64 *)dmabuf->virt;
11683 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11684 max_response = bde.tus.f.bdeSize;
11685 break;
11686 case CMD_GEN_REQUEST64_CR:
11687 max_response = 0;
11688 if (!pIocbOut->context3)
11689 break;
11690 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11691 sizeof(struct ulp_bde64);
11692 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11693 bpl = (struct ulp_bde64 *)dmabuf->virt;
11694 for (i = 0; i < numBdes; i++) {
11695 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11696 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11697 max_response += bde.tus.f.bdeSize;
11698 }
11699 break;
11700 default:
11701 max_response = wcqe->total_data_placed;
11702 break;
11703 }
11704 if (max_response < wcqe->total_data_placed)
11705 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11706 else
11707 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11708 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011709 }
James Smart341af102010-01-26 23:07:37 -050011710
James Smartacd68592012-01-18 16:25:09 -050011711 /* Convert BG errors for completion status */
11712 if (status == CQE_STATUS_DI_ERROR) {
11713 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11714
11715 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11716 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11717 else
11718 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11719
11720 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11721 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11722 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11723 BGS_GUARD_ERR_MASK;
11724 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11725 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11726 BGS_APPTAG_ERR_MASK;
11727 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11728 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11729 BGS_REFTAG_ERR_MASK;
11730
11731 /* Check to see if there was any good data before the error */
11732 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11733 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11734 BGS_HI_WATER_MARK_PRESENT_MASK;
11735 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11736 wcqe->total_data_placed;
11737 }
11738
11739 /*
11740 * Set ALL the error bits to indicate we don't know what
11741 * type of error it is.
11742 */
11743 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11744 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11745 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11746 BGS_GUARD_ERR_MASK);
11747 }
11748
James Smart341af102010-01-26 23:07:37 -050011749 /* Pick up HBA exchange busy condition */
11750 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11751 spin_lock_irqsave(&phba->hbalock, iflags);
11752 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11753 spin_unlock_irqrestore(&phba->hbalock, iflags);
11754 }
James Smart4f774512009-05-22 14:52:35 -040011755}
11756
11757/**
James Smart45ed1192009-10-02 15:17:02 -040011758 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11759 * @phba: Pointer to HBA context object.
11760 * @wcqe: Pointer to work-queue completion queue entry.
11761 *
11762 * This routine handles an ELS work-queue completion event and construct
11763 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11764 * discovery engine to handle.
11765 *
11766 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11767 **/
11768static struct lpfc_iocbq *
11769lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11770 struct lpfc_iocbq *irspiocbq)
11771{
11772 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11773 struct lpfc_iocbq *cmdiocbq;
11774 struct lpfc_wcqe_complete *wcqe;
11775 unsigned long iflags;
11776
11777 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011778 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011779 pring->stats.iocb_event++;
11780 /* Look up the ELS command IOCB and create pseudo response IOCB */
11781 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11782 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011783 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011784
11785 if (unlikely(!cmdiocbq)) {
11786 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11787 "0386 ELS complete with no corresponding "
11788 "cmdiocb: iotag (%d)\n",
11789 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11790 lpfc_sli_release_iocbq(phba, irspiocbq);
11791 return NULL;
11792 }
11793
11794 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011795 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011796
11797 return irspiocbq;
11798}
11799
11800/**
James Smart04c68492009-05-22 14:52:52 -040011801 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11802 * @phba: Pointer to HBA context object.
11803 * @cqe: Pointer to mailbox completion queue entry.
11804 *
11805 * This routine process a mailbox completion queue entry with asynchrous
11806 * event.
11807 *
11808 * Return: true if work posted to worker thread, otherwise false.
11809 **/
11810static bool
11811lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11812{
11813 struct lpfc_cq_event *cq_event;
11814 unsigned long iflags;
11815
11816 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11817 "0392 Async Event: word0:x%x, word1:x%x, "
11818 "word2:x%x, word3:x%x\n", mcqe->word0,
11819 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11820
11821 /* Allocate a new internal CQ_EVENT entry */
11822 cq_event = lpfc_sli4_cq_event_alloc(phba);
11823 if (!cq_event) {
11824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11825 "0394 Failed to allocate CQ_EVENT entry\n");
11826 return false;
11827 }
11828
11829 /* Move the CQE into an asynchronous event entry */
11830 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11831 spin_lock_irqsave(&phba->hbalock, iflags);
11832 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11833 /* Set the async event flag */
11834 phba->hba_flag |= ASYNC_EVENT;
11835 spin_unlock_irqrestore(&phba->hbalock, iflags);
11836
11837 return true;
11838}
11839
11840/**
11841 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11842 * @phba: Pointer to HBA context object.
11843 * @cqe: Pointer to mailbox completion queue entry.
11844 *
11845 * This routine process a mailbox completion queue entry with mailbox
11846 * completion event.
11847 *
11848 * Return: true if work posted to worker thread, otherwise false.
11849 **/
11850static bool
11851lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11852{
11853 uint32_t mcqe_status;
11854 MAILBOX_t *mbox, *pmbox;
11855 struct lpfc_mqe *mqe;
11856 struct lpfc_vport *vport;
11857 struct lpfc_nodelist *ndlp;
11858 struct lpfc_dmabuf *mp;
11859 unsigned long iflags;
11860 LPFC_MBOXQ_t *pmb;
11861 bool workposted = false;
11862 int rc;
11863
11864 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11865 if (!bf_get(lpfc_trailer_completed, mcqe))
11866 goto out_no_mqe_complete;
11867
11868 /* Get the reference to the active mbox command */
11869 spin_lock_irqsave(&phba->hbalock, iflags);
11870 pmb = phba->sli.mbox_active;
11871 if (unlikely(!pmb)) {
11872 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11873 "1832 No pending MBOX command to handle\n");
11874 spin_unlock_irqrestore(&phba->hbalock, iflags);
11875 goto out_no_mqe_complete;
11876 }
11877 spin_unlock_irqrestore(&phba->hbalock, iflags);
11878 mqe = &pmb->u.mqe;
11879 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11880 mbox = phba->mbox;
11881 vport = pmb->vport;
11882
11883 /* Reset heartbeat timer */
11884 phba->last_completion_time = jiffies;
11885 del_timer(&phba->sli.mbox_tmo);
11886
11887 /* Move mbox data to caller's mailbox region, do endian swapping */
11888 if (pmb->mbox_cmpl && mbox)
11889 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011890
James Smart73d91e52011-10-10 21:32:10 -040011891 /*
11892 * For mcqe errors, conditionally move a modified error code to
11893 * the mbox so that the error will not be missed.
11894 */
11895 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11896 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11897 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11898 bf_set(lpfc_mqe_status, mqe,
11899 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11900 }
James Smart04c68492009-05-22 14:52:52 -040011901 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11902 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11903 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11904 "MBOX dflt rpi: status:x%x rpi:x%x",
11905 mcqe_status,
11906 pmbox->un.varWords[0], 0);
11907 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11908 mp = (struct lpfc_dmabuf *)(pmb->context1);
11909 ndlp = (struct lpfc_nodelist *)pmb->context2;
11910 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11911 * RID of the PPI using the same mbox buffer.
11912 */
11913 lpfc_unreg_login(phba, vport->vpi,
11914 pmbox->un.varWords[0], pmb);
11915 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11916 pmb->context1 = mp;
11917 pmb->context2 = ndlp;
11918 pmb->vport = vport;
11919 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11920 if (rc != MBX_BUSY)
11921 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11922 LOG_SLI, "0385 rc should "
11923 "have been MBX_BUSY\n");
11924 if (rc != MBX_NOT_FINISHED)
11925 goto send_current_mbox;
11926 }
11927 }
11928 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11929 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11930 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11931
11932 /* There is mailbox completion work to do */
11933 spin_lock_irqsave(&phba->hbalock, iflags);
11934 __lpfc_mbox_cmpl_put(phba, pmb);
11935 phba->work_ha |= HA_MBATT;
11936 spin_unlock_irqrestore(&phba->hbalock, iflags);
11937 workposted = true;
11938
11939send_current_mbox:
11940 spin_lock_irqsave(&phba->hbalock, iflags);
11941 /* Release the mailbox command posting token */
11942 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11943 /* Setting active mailbox pointer need to be in sync to flag clear */
11944 phba->sli.mbox_active = NULL;
11945 spin_unlock_irqrestore(&phba->hbalock, iflags);
11946 /* Wake up worker thread to post the next pending mailbox command */
11947 lpfc_worker_wake_up(phba);
11948out_no_mqe_complete:
11949 if (bf_get(lpfc_trailer_consumed, mcqe))
11950 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11951 return workposted;
11952}
11953
11954/**
11955 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11956 * @phba: Pointer to HBA context object.
11957 * @cqe: Pointer to mailbox completion queue entry.
11958 *
11959 * This routine process a mailbox completion queue entry, it invokes the
11960 * proper mailbox complete handling or asynchrous event handling routine
11961 * according to the MCQE's async bit.
11962 *
11963 * Return: true if work posted to worker thread, otherwise false.
11964 **/
11965static bool
11966lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11967{
11968 struct lpfc_mcqe mcqe;
11969 bool workposted;
11970
11971 /* Copy the mailbox MCQE and convert endian order as needed */
11972 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11973
11974 /* Invoke the proper event handling routine */
11975 if (!bf_get(lpfc_trailer_async, &mcqe))
11976 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11977 else
11978 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11979 return workposted;
11980}
11981
11982/**
James Smart4f774512009-05-22 14:52:35 -040011983 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11984 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011985 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011986 * @wcqe: Pointer to work-queue completion queue entry.
11987 *
11988 * This routine handles an ELS work-queue completion event.
11989 *
11990 * Return: true if work posted to worker thread, otherwise false.
11991 **/
11992static bool
James Smart2a76a282012-08-03 12:35:54 -040011993lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011994 struct lpfc_wcqe_complete *wcqe)
11995{
James Smart4f774512009-05-22 14:52:35 -040011996 struct lpfc_iocbq *irspiocbq;
11997 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011998 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011999 int txq_cnt = 0;
12000 int txcmplq_cnt = 0;
12001 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012002
James Smart45ed1192009-10-02 15:17:02 -040012003 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012004 irspiocbq = lpfc_sli_get_iocbq(phba);
12005 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012006 if (!list_empty(&pring->txq))
12007 txq_cnt++;
12008 if (!list_empty(&pring->txcmplq))
12009 txcmplq_cnt++;
12010 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12011 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012013 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12014 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012015 txq_cnt, phba->iocb_cnt,
12016 fcp_txcmplq_cnt,
12017 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012018 return false;
James Smart4f774512009-05-22 14:52:35 -040012019 }
James Smart4f774512009-05-22 14:52:35 -040012020
James Smart45ed1192009-10-02 15:17:02 -040012021 /* Save off the slow-path queue event for work thread to process */
12022 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012023 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012024 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012025 &phba->sli4_hba.sp_queue_event);
12026 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012027 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012028
James Smart45ed1192009-10-02 15:17:02 -040012029 return true;
James Smart4f774512009-05-22 14:52:35 -040012030}
12031
12032/**
12033 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12034 * @phba: Pointer to HBA context object.
12035 * @wcqe: Pointer to work-queue completion queue entry.
12036 *
12037 * This routine handles slow-path WQ entry comsumed event by invoking the
12038 * proper WQ release routine to the slow-path WQ.
12039 **/
12040static void
12041lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12042 struct lpfc_wcqe_release *wcqe)
12043{
James Smart2e90f4b2011-12-13 13:22:37 -050012044 /* sanity check on queue memory */
12045 if (unlikely(!phba->sli4_hba.els_wq))
12046 return;
James Smart4f774512009-05-22 14:52:35 -040012047 /* Check for the slow-path ELS work queue */
12048 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12049 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12050 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12051 else
12052 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12053 "2579 Slow-path wqe consume event carries "
12054 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12055 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12056 phba->sli4_hba.els_wq->queue_id);
12057}
12058
12059/**
12060 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12061 * @phba: Pointer to HBA context object.
12062 * @cq: Pointer to a WQ completion queue.
12063 * @wcqe: Pointer to work-queue completion queue entry.
12064 *
12065 * This routine handles an XRI abort event.
12066 *
12067 * Return: true if work posted to worker thread, otherwise false.
12068 **/
12069static bool
12070lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12071 struct lpfc_queue *cq,
12072 struct sli4_wcqe_xri_aborted *wcqe)
12073{
12074 bool workposted = false;
12075 struct lpfc_cq_event *cq_event;
12076 unsigned long iflags;
12077
12078 /* Allocate a new internal CQ_EVENT entry */
12079 cq_event = lpfc_sli4_cq_event_alloc(phba);
12080 if (!cq_event) {
12081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12082 "0602 Failed to allocate CQ_EVENT entry\n");
12083 return false;
12084 }
12085
12086 /* Move the CQE into the proper xri abort event list */
12087 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12088 switch (cq->subtype) {
12089 case LPFC_FCP:
12090 spin_lock_irqsave(&phba->hbalock, iflags);
12091 list_add_tail(&cq_event->list,
12092 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12093 /* Set the fcp xri abort event flag */
12094 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12095 spin_unlock_irqrestore(&phba->hbalock, iflags);
12096 workposted = true;
12097 break;
12098 case LPFC_ELS:
12099 spin_lock_irqsave(&phba->hbalock, iflags);
12100 list_add_tail(&cq_event->list,
12101 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12102 /* Set the els xri abort event flag */
12103 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12104 spin_unlock_irqrestore(&phba->hbalock, iflags);
12105 workposted = true;
12106 break;
12107 default:
12108 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12109 "0603 Invalid work queue CQE subtype (x%x)\n",
12110 cq->subtype);
12111 workposted = false;
12112 break;
12113 }
12114 return workposted;
12115}
12116
12117/**
James Smart4d9ab992009-10-02 15:16:39 -040012118 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012119 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012120 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012121 *
James Smart4d9ab992009-10-02 15:16:39 -040012122 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012123 *
12124 * Return: true if work posted to worker thread, otherwise false.
12125 **/
12126static bool
James Smart4d9ab992009-10-02 15:16:39 -040012127lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12128{
12129 bool workposted = false;
12130 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12131 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12132 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012133 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012134 unsigned long iflags;
12135
James Smart2e90f4b2011-12-13 13:22:37 -050012136 /* sanity check on queue memory */
12137 if (unlikely(!hrq) || unlikely(!drq))
12138 return workposted;
12139
James Smart7851fe22011-07-22 18:36:52 -040012140 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12141 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12142 else
12143 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12144 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012145 goto out;
12146
12147 status = bf_get(lpfc_rcqe_status, rcqe);
12148 switch (status) {
12149 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12150 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12151 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012152 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012153 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012154 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012155 spin_lock_irqsave(&phba->hbalock, iflags);
12156 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12157 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012158 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012159 spin_unlock_irqrestore(&phba->hbalock, iflags);
12160 goto out;
12161 }
James Smartb84daac2012-08-03 12:35:13 -040012162 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012163 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12164 /* save off the frame for the word thread to process */
12165 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012166 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012167 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012168 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012169 spin_unlock_irqrestore(&phba->hbalock, iflags);
12170 workposted = true;
12171 break;
12172 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12173 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012174 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012175 /* Post more buffers if possible */
12176 spin_lock_irqsave(&phba->hbalock, iflags);
12177 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12178 spin_unlock_irqrestore(&phba->hbalock, iflags);
12179 workposted = true;
12180 break;
12181 }
12182out:
12183 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012184}
12185
12186/**
12187 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12188 * @phba: Pointer to HBA context object.
12189 * @cq: Pointer to the completion queue.
12190 * @wcqe: Pointer to a completion queue entry.
12191 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012192 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012193 * entry.
12194 *
12195 * Return: true if work posted to worker thread, otherwise false.
12196 **/
12197static bool
12198lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012199 struct lpfc_cqe *cqe)
12200{
James Smart45ed1192009-10-02 15:17:02 -040012201 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012202 bool workposted = false;
12203
12204 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012205 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012206
12207 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012208 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012209 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012210 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012211 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012212 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012213 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012214 break;
12215 case CQE_CODE_RELEASE_WQE:
12216 /* Process the WQ release event */
12217 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012218 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012219 break;
12220 case CQE_CODE_XRI_ABORTED:
12221 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012222 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012223 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012224 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012225 break;
James Smart4d9ab992009-10-02 15:16:39 -040012226 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012227 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012228 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012229 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012230 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012231 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012232 break;
James Smart4f774512009-05-22 14:52:35 -040012233 default:
12234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12235 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012236 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012237 break;
12238 }
12239 return workposted;
12240}
12241
12242/**
James Smart4f774512009-05-22 14:52:35 -040012243 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12244 * @phba: Pointer to HBA context object.
12245 * @eqe: Pointer to fast-path event queue entry.
12246 *
12247 * This routine process a event queue entry from the slow-path event queue.
12248 * It will check the MajorCode and MinorCode to determine this is for a
12249 * completion event on a completion queue, if not, an error shall be logged
12250 * and just return. Otherwise, it will get to the corresponding completion
12251 * queue and process all the entries on that completion queue, rearm the
12252 * completion queue, and then return.
12253 *
12254 **/
12255static void
James Smart67d12732012-08-03 12:36:13 -040012256lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12257 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012258{
James Smart67d12732012-08-03 12:36:13 -040012259 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012260 struct lpfc_cqe *cqe;
12261 bool workposted = false;
12262 int ecount = 0;
12263 uint16_t cqid;
12264
James Smart4f774512009-05-22 14:52:35 -040012265 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012266 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012267
James Smart4f774512009-05-22 14:52:35 -040012268 list_for_each_entry(childq, &speq->child_list, list) {
12269 if (childq->queue_id == cqid) {
12270 cq = childq;
12271 break;
12272 }
12273 }
12274 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012275 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12277 "0365 Slow-path CQ identifier "
12278 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012279 return;
12280 }
12281
12282 /* Process all the entries to the CQ */
12283 switch (cq->type) {
12284 case LPFC_MCQ:
12285 while ((cqe = lpfc_sli4_cq_get(cq))) {
12286 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012287 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012288 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012289 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012290 }
12291 break;
12292 case LPFC_WCQ:
12293 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012294 if (cq->subtype == LPFC_FCP)
12295 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12296 cqe);
12297 else
12298 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12299 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012300 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012301 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12302 }
James Smartb84daac2012-08-03 12:35:13 -040012303
12304 /* Track the max number of CQEs processed in 1 EQ */
12305 if (ecount > cq->CQ_max_cqe)
12306 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012307 break;
12308 default:
12309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12310 "0370 Invalid completion queue type (%d)\n",
12311 cq->type);
12312 return;
12313 }
12314
12315 /* Catch the no cq entry condition, log an error */
12316 if (unlikely(ecount == 0))
12317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12318 "0371 No entry from the CQ: identifier "
12319 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12320
12321 /* In any case, flash and re-arm the RCQ */
12322 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12323
12324 /* wake up worker thread if there are works to be done */
12325 if (workposted)
12326 lpfc_worker_wake_up(phba);
12327}
12328
12329/**
12330 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012331 * @phba: Pointer to HBA context object.
12332 * @cq: Pointer to associated CQ
12333 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012334 *
12335 * This routine process a fast-path work queue completion entry from fast-path
12336 * event queue for FCP command response completion.
12337 **/
12338static void
James Smart2a76a282012-08-03 12:35:54 -040012339lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012340 struct lpfc_wcqe_complete *wcqe)
12341{
James Smart2a76a282012-08-03 12:35:54 -040012342 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012343 struct lpfc_iocbq *cmdiocbq;
12344 struct lpfc_iocbq irspiocbq;
12345 unsigned long iflags;
12346
James Smart4f774512009-05-22 14:52:35 -040012347 /* Check for response status */
12348 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12349 /* If resource errors reported from HBA, reduce queue
12350 * depth of the SCSI device.
12351 */
James Smarte3d2b802012-08-14 14:25:43 -040012352 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12353 IOSTAT_LOCAL_REJECT)) &&
12354 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12355 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012356 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012357
James Smart4f774512009-05-22 14:52:35 -040012358 /* Log the error status */
12359 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12360 "0373 FCP complete error: status=x%x, "
12361 "hw_status=x%x, total_data_specified=%d, "
12362 "parameter=x%x, word3=x%x\n",
12363 bf_get(lpfc_wcqe_c_status, wcqe),
12364 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12365 wcqe->total_data_placed, wcqe->parameter,
12366 wcqe->word3);
12367 }
12368
12369 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012370 spin_lock_irqsave(&pring->ring_lock, iflags);
12371 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012372 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12373 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012374 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012375 if (unlikely(!cmdiocbq)) {
12376 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12377 "0374 FCP complete with no corresponding "
12378 "cmdiocb: iotag (%d)\n",
12379 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12380 return;
12381 }
12382 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12383 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12384 "0375 FCP cmdiocb not callback function "
12385 "iotag: (%d)\n",
12386 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12387 return;
12388 }
12389
12390 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012391 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012392
James Smart0f65ff62010-02-26 14:14:23 -050012393 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12394 spin_lock_irqsave(&phba->hbalock, iflags);
12395 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12396 spin_unlock_irqrestore(&phba->hbalock, iflags);
12397 }
12398
James Smart4f774512009-05-22 14:52:35 -040012399 /* Pass the cmd_iocb and the rsp state to the upper layer */
12400 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12401}
12402
12403/**
12404 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12405 * @phba: Pointer to HBA context object.
12406 * @cq: Pointer to completion queue.
12407 * @wcqe: Pointer to work-queue completion queue entry.
12408 *
12409 * This routine handles an fast-path WQ entry comsumed event by invoking the
12410 * proper WQ release routine to the slow-path WQ.
12411 **/
12412static void
12413lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12414 struct lpfc_wcqe_release *wcqe)
12415{
12416 struct lpfc_queue *childwq;
12417 bool wqid_matched = false;
12418 uint16_t fcp_wqid;
12419
12420 /* Check for fast-path FCP work queue release */
12421 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12422 list_for_each_entry(childwq, &cq->child_list, list) {
12423 if (childwq->queue_id == fcp_wqid) {
12424 lpfc_sli4_wq_release(childwq,
12425 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12426 wqid_matched = true;
12427 break;
12428 }
12429 }
12430 /* Report warning log message if no match found */
12431 if (wqid_matched != true)
12432 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12433 "2580 Fast-path wqe consume event carries "
12434 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12435}
12436
12437/**
12438 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12439 * @cq: Pointer to the completion queue.
12440 * @eqe: Pointer to fast-path completion queue entry.
12441 *
12442 * This routine process a fast-path work queue completion entry from fast-path
12443 * event queue for FCP command response completion.
12444 **/
12445static int
12446lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12447 struct lpfc_cqe *cqe)
12448{
12449 struct lpfc_wcqe_release wcqe;
12450 bool workposted = false;
12451
12452 /* Copy the work queue CQE and convert endian order if needed */
12453 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12454
12455 /* Check and process for different type of WCQE and dispatch */
12456 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12457 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012458 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012459 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012460 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012461 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012462 (struct lpfc_wcqe_complete *)&wcqe);
12463 break;
12464 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012465 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012466 /* Process the WQ release event */
12467 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12468 (struct lpfc_wcqe_release *)&wcqe);
12469 break;
12470 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012471 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012472 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012473 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012474 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12475 (struct sli4_wcqe_xri_aborted *)&wcqe);
12476 break;
12477 default:
12478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12479 "0144 Not a valid WCQE code: x%x\n",
12480 bf_get(lpfc_wcqe_c_code, &wcqe));
12481 break;
12482 }
12483 return workposted;
12484}
12485
12486/**
James Smart67d12732012-08-03 12:36:13 -040012487 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012488 * @phba: Pointer to HBA context object.
12489 * @eqe: Pointer to fast-path event queue entry.
12490 *
12491 * This routine process a event queue entry from the fast-path event queue.
12492 * It will check the MajorCode and MinorCode to determine this is for a
12493 * completion event on a completion queue, if not, an error shall be logged
12494 * and just return. Otherwise, it will get to the corresponding completion
12495 * queue and process all the entries on the completion queue, rearm the
12496 * completion queue, and then return.
12497 **/
12498static void
James Smart67d12732012-08-03 12:36:13 -040012499lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12500 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012501{
12502 struct lpfc_queue *cq;
12503 struct lpfc_cqe *cqe;
12504 bool workposted = false;
12505 uint16_t cqid;
12506 int ecount = 0;
12507
James Smartcb5172e2010-03-15 11:25:07 -040012508 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012509 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012510 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012511 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012512 bf_get_le32(lpfc_eqe_major_code, eqe),
12513 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012514 return;
12515 }
12516
James Smart67d12732012-08-03 12:36:13 -040012517 /* Get the reference to the corresponding CQ */
12518 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12519
12520 /* Check if this is a Slow path event */
12521 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12522 lpfc_sli4_sp_handle_eqe(phba, eqe,
12523 phba->sli4_hba.hba_eq[qidx]);
12524 return;
12525 }
12526
James Smart2e90f4b2011-12-13 13:22:37 -050012527 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12528 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12529 "3146 Fast-path completion queues "
12530 "does not exist\n");
12531 return;
12532 }
James Smart67d12732012-08-03 12:36:13 -040012533 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012534 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012535 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12536 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12537 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012538 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012539 return;
12540 }
12541
James Smart4f774512009-05-22 14:52:35 -040012542 if (unlikely(cqid != cq->queue_id)) {
12543 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12544 "0368 Miss-matched fast-path completion "
12545 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12546 cqid, cq->queue_id);
12547 return;
12548 }
12549
12550 /* Process all the entries to the CQ */
12551 while ((cqe = lpfc_sli4_cq_get(cq))) {
12552 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012553 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012554 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12555 }
12556
James Smartb84daac2012-08-03 12:35:13 -040012557 /* Track the max number of CQEs processed in 1 EQ */
12558 if (ecount > cq->CQ_max_cqe)
12559 cq->CQ_max_cqe = ecount;
12560
James Smart4f774512009-05-22 14:52:35 -040012561 /* Catch the no cq entry condition */
12562 if (unlikely(ecount == 0))
12563 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12564 "0369 No entry from fast-path completion "
12565 "queue fcpcqid=%d\n", cq->queue_id);
12566
12567 /* In any case, flash and re-arm the CQ */
12568 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12569
12570 /* wake up worker thread if there are works to be done */
12571 if (workposted)
12572 lpfc_worker_wake_up(phba);
12573}
12574
12575static void
12576lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12577{
12578 struct lpfc_eqe *eqe;
12579
12580 /* walk all the EQ entries and drop on the floor */
12581 while ((eqe = lpfc_sli4_eq_get(eq)))
12582 ;
12583
12584 /* Clear and re-arm the EQ */
12585 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12586}
12587
James Smart1ba981f2014-02-20 09:56:45 -050012588
12589/**
12590 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12591 * entry
12592 * @phba: Pointer to HBA context object.
12593 * @eqe: Pointer to fast-path event queue entry.
12594 *
12595 * This routine process a event queue entry from the Flash Optimized Fabric
12596 * event queue. It will check the MajorCode and MinorCode to determine this
12597 * is for a completion event on a completion queue, if not, an error shall be
12598 * logged and just return. Otherwise, it will get to the corresponding
12599 * completion queue and process all the entries on the completion queue, rearm
12600 * the completion queue, and then return.
12601 **/
12602static void
12603lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12604{
12605 struct lpfc_queue *cq;
12606 struct lpfc_cqe *cqe;
12607 bool workposted = false;
12608 uint16_t cqid;
12609 int ecount = 0;
12610
12611 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12612 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12613 "9147 Not a valid completion "
12614 "event: majorcode=x%x, minorcode=x%x\n",
12615 bf_get_le32(lpfc_eqe_major_code, eqe),
12616 bf_get_le32(lpfc_eqe_minor_code, eqe));
12617 return;
12618 }
12619
12620 /* Get the reference to the corresponding CQ */
12621 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12622
12623 /* Next check for OAS */
12624 cq = phba->sli4_hba.oas_cq;
12625 if (unlikely(!cq)) {
12626 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12628 "9148 OAS completion queue "
12629 "does not exist\n");
12630 return;
12631 }
12632
12633 if (unlikely(cqid != cq->queue_id)) {
12634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12635 "9149 Miss-matched fast-path compl "
12636 "queue id: eqcqid=%d, fcpcqid=%d\n",
12637 cqid, cq->queue_id);
12638 return;
12639 }
12640
12641 /* Process all the entries to the OAS CQ */
12642 while ((cqe = lpfc_sli4_cq_get(cq))) {
12643 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12644 if (!(++ecount % cq->entry_repost))
12645 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12646 }
12647
12648 /* Track the max number of CQEs processed in 1 EQ */
12649 if (ecount > cq->CQ_max_cqe)
12650 cq->CQ_max_cqe = ecount;
12651
12652 /* Catch the no cq entry condition */
12653 if (unlikely(ecount == 0))
12654 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12655 "9153 No entry from fast-path completion "
12656 "queue fcpcqid=%d\n", cq->queue_id);
12657
12658 /* In any case, flash and re-arm the CQ */
12659 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12660
12661 /* wake up worker thread if there are works to be done */
12662 if (workposted)
12663 lpfc_worker_wake_up(phba);
12664}
12665
12666/**
12667 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12668 * @irq: Interrupt number.
12669 * @dev_id: The device context pointer.
12670 *
12671 * This function is directly called from the PCI layer as an interrupt
12672 * service routine when device with SLI-4 interface spec is enabled with
12673 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12674 * IOCB ring event in the HBA. However, when the device is enabled with either
12675 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12676 * device-level interrupt handler. When the PCI slot is in error recovery
12677 * or the HBA is undergoing initialization, the interrupt handler will not
12678 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12679 * the intrrupt context. This function is called without any lock held.
12680 * It gets the hbalock to access and update SLI data structures. Note that,
12681 * the EQ to CQ are one-to-one map such that the EQ index is
12682 * equal to that of CQ index.
12683 *
12684 * This function returns IRQ_HANDLED when interrupt is handled else it
12685 * returns IRQ_NONE.
12686 **/
12687irqreturn_t
12688lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12689{
12690 struct lpfc_hba *phba;
12691 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12692 struct lpfc_queue *eq;
12693 struct lpfc_eqe *eqe;
12694 unsigned long iflag;
12695 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012696
12697 /* Get the driver's phba structure from the dev_id */
12698 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12699 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012700
12701 if (unlikely(!phba))
12702 return IRQ_NONE;
12703
12704 /* Get to the EQ struct associated with this vector */
12705 eq = phba->sli4_hba.fof_eq;
12706 if (unlikely(!eq))
12707 return IRQ_NONE;
12708
12709 /* Check device state for handling interrupt */
12710 if (unlikely(lpfc_intr_state_check(phba))) {
12711 eq->EQ_badstate++;
12712 /* Check again for link_state with lock held */
12713 spin_lock_irqsave(&phba->hbalock, iflag);
12714 if (phba->link_state < LPFC_LINK_DOWN)
12715 /* Flush, clear interrupt, and rearm the EQ */
12716 lpfc_sli4_eq_flush(phba, eq);
12717 spin_unlock_irqrestore(&phba->hbalock, iflag);
12718 return IRQ_NONE;
12719 }
12720
12721 /*
12722 * Process all the event on FCP fast-path EQ
12723 */
12724 while ((eqe = lpfc_sli4_eq_get(eq))) {
12725 lpfc_sli4_fof_handle_eqe(phba, eqe);
12726 if (!(++ecount % eq->entry_repost))
12727 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12728 eq->EQ_processed++;
12729 }
12730
12731 /* Track the max number of EQEs processed in 1 intr */
12732 if (ecount > eq->EQ_max_eqe)
12733 eq->EQ_max_eqe = ecount;
12734
12735
12736 if (unlikely(ecount == 0)) {
12737 eq->EQ_no_entry++;
12738
12739 if (phba->intr_type == MSIX)
12740 /* MSI-X treated interrupt served as no EQ share INT */
12741 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12742 "9145 MSI-X interrupt with no EQE\n");
12743 else {
12744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12745 "9146 ISR interrupt with no EQE\n");
12746 /* Non MSI-X treated on interrupt as EQ share INT */
12747 return IRQ_NONE;
12748 }
12749 }
12750 /* Always clear and re-arm the fast-path EQ */
12751 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12752 return IRQ_HANDLED;
12753}
12754
James Smart4f774512009-05-22 14:52:35 -040012755/**
James Smart67d12732012-08-03 12:36:13 -040012756 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012757 * @irq: Interrupt number.
12758 * @dev_id: The device context pointer.
12759 *
12760 * This function is directly called from the PCI layer as an interrupt
12761 * service routine when device with SLI-4 interface spec is enabled with
12762 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12763 * ring event in the HBA. However, when the device is enabled with either
12764 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12765 * device-level interrupt handler. When the PCI slot is in error recovery
12766 * or the HBA is undergoing initialization, the interrupt handler will not
12767 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12768 * the intrrupt context. This function is called without any lock held.
12769 * It gets the hbalock to access and update SLI data structures. Note that,
12770 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12771 * equal to that of FCP CQ index.
12772 *
James Smart67d12732012-08-03 12:36:13 -040012773 * The link attention and ELS ring attention events are handled
12774 * by the worker thread. The interrupt handler signals the worker thread
12775 * and returns for these events. This function is called without any lock
12776 * held. It gets the hbalock to access and update SLI data structures.
12777 *
James Smart4f774512009-05-22 14:52:35 -040012778 * This function returns IRQ_HANDLED when interrupt is handled else it
12779 * returns IRQ_NONE.
12780 **/
12781irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012782lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012783{
12784 struct lpfc_hba *phba;
12785 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12786 struct lpfc_queue *fpeq;
12787 struct lpfc_eqe *eqe;
12788 unsigned long iflag;
12789 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012790 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012791
12792 /* Get the driver's phba structure from the dev_id */
12793 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12794 phba = fcp_eq_hdl->phba;
12795 fcp_eqidx = fcp_eq_hdl->idx;
12796
12797 if (unlikely(!phba))
12798 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012799 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012800 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012801
12802 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012803 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012804 if (unlikely(!fpeq))
12805 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012806
James Smartba20c852012-08-03 12:36:52 -040012807 if (lpfc_fcp_look_ahead) {
12808 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12809 lpfc_sli4_eq_clr_intr(fpeq);
12810 else {
12811 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12812 return IRQ_NONE;
12813 }
12814 }
12815
James Smart4f774512009-05-22 14:52:35 -040012816 /* Check device state for handling interrupt */
12817 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012818 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012819 /* Check again for link_state with lock held */
12820 spin_lock_irqsave(&phba->hbalock, iflag);
12821 if (phba->link_state < LPFC_LINK_DOWN)
12822 /* Flush, clear interrupt, and rearm the EQ */
12823 lpfc_sli4_eq_flush(phba, fpeq);
12824 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012825 if (lpfc_fcp_look_ahead)
12826 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012827 return IRQ_NONE;
12828 }
12829
12830 /*
12831 * Process all the event on FCP fast-path EQ
12832 */
12833 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012834 if (eqe == NULL)
12835 break;
12836
James Smart67d12732012-08-03 12:36:13 -040012837 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012838 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012839 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012840 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012841 }
12842
James Smartb84daac2012-08-03 12:35:13 -040012843 /* Track the max number of EQEs processed in 1 intr */
12844 if (ecount > fpeq->EQ_max_eqe)
12845 fpeq->EQ_max_eqe = ecount;
12846
James Smart4f774512009-05-22 14:52:35 -040012847 /* Always clear and re-arm the fast-path EQ */
12848 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12849
12850 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012851 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012852
12853 if (lpfc_fcp_look_ahead) {
12854 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12855 return IRQ_NONE;
12856 }
12857
James Smart4f774512009-05-22 14:52:35 -040012858 if (phba->intr_type == MSIX)
12859 /* MSI-X treated interrupt served as no EQ share INT */
12860 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12861 "0358 MSI-X interrupt with no EQE\n");
12862 else
12863 /* Non MSI-X treated on interrupt as EQ share INT */
12864 return IRQ_NONE;
12865 }
12866
James Smartba20c852012-08-03 12:36:52 -040012867 if (lpfc_fcp_look_ahead)
12868 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012869 return IRQ_HANDLED;
12870} /* lpfc_sli4_fp_intr_handler */
12871
12872/**
12873 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12874 * @irq: Interrupt number.
12875 * @dev_id: The device context pointer.
12876 *
12877 * This function is the device-level interrupt handler to device with SLI-4
12878 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12879 * interrupt mode is enabled and there is an event in the HBA which requires
12880 * driver attention. This function invokes the slow-path interrupt attention
12881 * handling function and fast-path interrupt attention handling function in
12882 * turn to process the relevant HBA attention events. This function is called
12883 * without any lock held. It gets the hbalock to access and update SLI data
12884 * structures.
12885 *
12886 * This function returns IRQ_HANDLED when interrupt is handled, else it
12887 * returns IRQ_NONE.
12888 **/
12889irqreturn_t
12890lpfc_sli4_intr_handler(int irq, void *dev_id)
12891{
12892 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012893 irqreturn_t hba_irq_rc;
12894 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012895 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012896
12897 /* Get the driver's phba structure from the dev_id */
12898 phba = (struct lpfc_hba *)dev_id;
12899
12900 if (unlikely(!phba))
12901 return IRQ_NONE;
12902
12903 /*
James Smart4f774512009-05-22 14:52:35 -040012904 * Invoke fast-path host attention interrupt handling as appropriate.
12905 */
James Smart67d12732012-08-03 12:36:13 -040012906 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12907 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012908 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012909 if (hba_irq_rc == IRQ_HANDLED)
12910 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012911 }
12912
James Smart1ba981f2014-02-20 09:56:45 -050012913 if (phba->cfg_fof) {
12914 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12915 &phba->sli4_hba.fcp_eq_hdl[0]);
12916 if (hba_irq_rc == IRQ_HANDLED)
12917 hba_handled |= true;
12918 }
12919
James Smart67d12732012-08-03 12:36:13 -040012920 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012921} /* lpfc_sli4_intr_handler */
12922
12923/**
12924 * lpfc_sli4_queue_free - free a queue structure and associated memory
12925 * @queue: The queue structure to free.
12926 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012927 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012928 * the host resident queue. This function must be called after destroying the
12929 * queue on the HBA.
12930 **/
12931void
12932lpfc_sli4_queue_free(struct lpfc_queue *queue)
12933{
12934 struct lpfc_dmabuf *dmabuf;
12935
12936 if (!queue)
12937 return;
12938
12939 while (!list_empty(&queue->page_list)) {
12940 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12941 list);
James Smart49198b32010-04-06 15:04:33 -040012942 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012943 dmabuf->virt, dmabuf->phys);
12944 kfree(dmabuf);
12945 }
12946 kfree(queue);
12947 return;
12948}
12949
12950/**
12951 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12952 * @phba: The HBA that this queue is being created on.
12953 * @entry_size: The size of each queue entry for this queue.
12954 * @entry count: The number of entries that this queue will handle.
12955 *
12956 * This function allocates a queue structure and the DMAable memory used for
12957 * the host resident queue. This function must be called before creating the
12958 * queue on the HBA.
12959 **/
12960struct lpfc_queue *
12961lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12962 uint32_t entry_count)
12963{
12964 struct lpfc_queue *queue;
12965 struct lpfc_dmabuf *dmabuf;
12966 int x, total_qe_count;
12967 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012968 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012969
James Smartcb5172e2010-03-15 11:25:07 -040012970 if (!phba->sli4_hba.pc_sli4_params.supported)
12971 hw_page_size = SLI4_PAGE_SIZE;
12972
James Smart4f774512009-05-22 14:52:35 -040012973 queue = kzalloc(sizeof(struct lpfc_queue) +
12974 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12975 if (!queue)
12976 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012977 queue->page_count = (ALIGN(entry_size * entry_count,
12978 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012979 INIT_LIST_HEAD(&queue->list);
12980 INIT_LIST_HEAD(&queue->page_list);
12981 INIT_LIST_HEAD(&queue->child_list);
12982 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12983 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12984 if (!dmabuf)
12985 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040012986 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
12987 hw_page_size, &dmabuf->phys,
12988 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040012989 if (!dmabuf->virt) {
12990 kfree(dmabuf);
12991 goto out_fail;
12992 }
12993 dmabuf->buffer_tag = x;
12994 list_add_tail(&dmabuf->list, &queue->page_list);
12995 /* initialize queue's entry array */
12996 dma_pointer = dmabuf->virt;
12997 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012998 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012999 total_qe_count++, dma_pointer += entry_size) {
13000 queue->qe[total_qe_count].address = dma_pointer;
13001 }
13002 }
13003 queue->entry_size = entry_size;
13004 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013005
13006 /*
13007 * entry_repost is calculated based on the number of entries in the
13008 * queue. This works out except for RQs. If buffers are NOT initially
13009 * posted for every RQE, entry_repost should be adjusted accordingly.
13010 */
13011 queue->entry_repost = (entry_count >> 3);
13012 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13013 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013014 queue->phba = phba;
13015
13016 return queue;
13017out_fail:
13018 lpfc_sli4_queue_free(queue);
13019 return NULL;
13020}
13021
13022/**
James Smart962bc512013-01-03 15:44:00 -050013023 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13024 * @phba: HBA structure that indicates port to create a queue on.
13025 * @pci_barset: PCI BAR set flag.
13026 *
13027 * This function shall perform iomap of the specified PCI BAR address to host
13028 * memory address if not already done so and return it. The returned host
13029 * memory address can be NULL.
13030 */
13031static void __iomem *
13032lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13033{
James Smart962bc512013-01-03 15:44:00 -050013034 if (!phba->pcidev)
13035 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013036
13037 switch (pci_barset) {
13038 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013039 return phba->pci_bar0_memmap_p;
13040 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013041 return phba->pci_bar2_memmap_p;
13042 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013043 return phba->pci_bar4_memmap_p;
13044 default:
13045 break;
13046 }
13047 return NULL;
13048}
13049
13050/**
James Smart173edbb2012-06-12 13:54:50 -040013051 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13052 * @phba: HBA structure that indicates port to create a queue on.
13053 * @startq: The starting FCP EQ to modify
13054 *
13055 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13056 *
13057 * The @phba struct is used to send mailbox command to HBA. The @startq
13058 * is used to get the starting FCP EQ to change.
13059 * This function is asynchronous and will wait for the mailbox
13060 * command to finish before continuing.
13061 *
13062 * On success this function will return a zero. If unable to allocate enough
13063 * memory this function will return -ENOMEM. If the queue create mailbox command
13064 * fails this function will return -ENXIO.
13065 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013066int
James Smart2c9c5a02015-04-07 15:07:15 -040013067lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013068{
13069 struct lpfc_mbx_modify_eq_delay *eq_delay;
13070 LPFC_MBOXQ_t *mbox;
13071 struct lpfc_queue *eq;
13072 int cnt, rc, length, status = 0;
13073 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013074 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013075 int fcp_eqidx;
13076 union lpfc_sli4_cfg_shdr *shdr;
13077 uint16_t dmult;
13078
James Smart67d12732012-08-03 12:36:13 -040013079 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013080 return 0;
13081
13082 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13083 if (!mbox)
13084 return -ENOMEM;
13085 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13086 sizeof(struct lpfc_sli4_cfg_mhdr));
13087 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13088 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13089 length, LPFC_SLI4_MBX_EMBED);
13090 eq_delay = &mbox->u.mqe.un.eq_delay;
13091
13092 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013093 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13094 if (result > LPFC_DMULT_CONST)
13095 dmult = 0;
13096 else
13097 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013098
13099 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013100 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013101 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013102 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013103 if (!eq)
13104 continue;
13105 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13106 eq_delay->u.request.eq[cnt].phase = 0;
13107 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13108 cnt++;
13109 if (cnt >= LPFC_MAX_EQ_DELAY)
13110 break;
13111 }
13112 eq_delay->u.request.num_eq = cnt;
13113
13114 mbox->vport = phba->pport;
13115 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13116 mbox->context1 = NULL;
13117 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13118 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13119 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13120 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13121 if (shdr_status || shdr_add_status || rc) {
13122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13123 "2512 MODIFY_EQ_DELAY mailbox failed with "
13124 "status x%x add_status x%x, mbx status x%x\n",
13125 shdr_status, shdr_add_status, rc);
13126 status = -ENXIO;
13127 }
13128 mempool_free(mbox, phba->mbox_mem_pool);
13129 return status;
13130}
13131
13132/**
James Smart4f774512009-05-22 14:52:35 -040013133 * lpfc_eq_create - Create an Event Queue on the HBA
13134 * @phba: HBA structure that indicates port to create a queue on.
13135 * @eq: The queue structure to use to create the event queue.
13136 * @imax: The maximum interrupt per second limit.
13137 *
13138 * This function creates an event queue, as detailed in @eq, on a port,
13139 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13140 *
13141 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13142 * is used to get the entry count and entry size that are necessary to
13143 * determine the number of pages to allocate and use for this queue. This
13144 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13145 * event queue. This function is asynchronous and will wait for the mailbox
13146 * command to finish before continuing.
13147 *
13148 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013149 * memory this function will return -ENOMEM. If the queue create mailbox command
13150 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013151 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013152int
James Smartee020062012-09-29 11:28:52 -040013153lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013154{
13155 struct lpfc_mbx_eq_create *eq_create;
13156 LPFC_MBOXQ_t *mbox;
13157 int rc, length, status = 0;
13158 struct lpfc_dmabuf *dmabuf;
13159 uint32_t shdr_status, shdr_add_status;
13160 union lpfc_sli4_cfg_shdr *shdr;
13161 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013162 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13163
James Smart2e90f4b2011-12-13 13:22:37 -050013164 /* sanity check on queue memory */
13165 if (!eq)
13166 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013167 if (!phba->sli4_hba.pc_sli4_params.supported)
13168 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013169
13170 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13171 if (!mbox)
13172 return -ENOMEM;
13173 length = (sizeof(struct lpfc_mbx_eq_create) -
13174 sizeof(struct lpfc_sli4_cfg_mhdr));
13175 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13176 LPFC_MBOX_OPCODE_EQ_CREATE,
13177 length, LPFC_SLI4_MBX_EMBED);
13178 eq_create = &mbox->u.mqe.un.eq_create;
13179 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13180 eq->page_count);
13181 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13182 LPFC_EQE_SIZE);
13183 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013184 /* don't setup delay multiplier using EQ_CREATE */
13185 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013186 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13187 dmult);
13188 switch (eq->entry_count) {
13189 default:
13190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13191 "0360 Unsupported EQ count. (%d)\n",
13192 eq->entry_count);
13193 if (eq->entry_count < 256)
13194 return -EINVAL;
13195 /* otherwise default to smallest count (drop through) */
13196 case 256:
13197 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13198 LPFC_EQ_CNT_256);
13199 break;
13200 case 512:
13201 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13202 LPFC_EQ_CNT_512);
13203 break;
13204 case 1024:
13205 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13206 LPFC_EQ_CNT_1024);
13207 break;
13208 case 2048:
13209 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13210 LPFC_EQ_CNT_2048);
13211 break;
13212 case 4096:
13213 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13214 LPFC_EQ_CNT_4096);
13215 break;
13216 }
13217 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013218 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013219 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13220 putPaddrLow(dmabuf->phys);
13221 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13222 putPaddrHigh(dmabuf->phys);
13223 }
13224 mbox->vport = phba->pport;
13225 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13226 mbox->context1 = NULL;
13227 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13228 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13229 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13230 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13231 if (shdr_status || shdr_add_status || rc) {
13232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13233 "2500 EQ_CREATE mailbox failed with "
13234 "status x%x add_status x%x, mbx status x%x\n",
13235 shdr_status, shdr_add_status, rc);
13236 status = -ENXIO;
13237 }
13238 eq->type = LPFC_EQ;
13239 eq->subtype = LPFC_NONE;
13240 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13241 if (eq->queue_id == 0xFFFF)
13242 status = -ENXIO;
13243 eq->host_index = 0;
13244 eq->hba_index = 0;
13245
James Smart8fa38512009-07-19 10:01:03 -040013246 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013247 return status;
13248}
13249
13250/**
13251 * lpfc_cq_create - Create a Completion Queue on the HBA
13252 * @phba: HBA structure that indicates port to create a queue on.
13253 * @cq: The queue structure to use to create the completion queue.
13254 * @eq: The event queue to bind this completion queue to.
13255 *
13256 * This function creates a completion queue, as detailed in @wq, on a port,
13257 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13258 *
13259 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13260 * is used to get the entry count and entry size that are necessary to
13261 * determine the number of pages to allocate and use for this queue. The @eq
13262 * is used to indicate which event queue to bind this completion queue to. This
13263 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13264 * completion queue. This function is asynchronous and will wait for the mailbox
13265 * command to finish before continuing.
13266 *
13267 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013268 * memory this function will return -ENOMEM. If the queue create mailbox command
13269 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013270 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013271int
James Smart4f774512009-05-22 14:52:35 -040013272lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13273 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13274{
13275 struct lpfc_mbx_cq_create *cq_create;
13276 struct lpfc_dmabuf *dmabuf;
13277 LPFC_MBOXQ_t *mbox;
13278 int rc, length, status = 0;
13279 uint32_t shdr_status, shdr_add_status;
13280 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013281 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13282
James Smart2e90f4b2011-12-13 13:22:37 -050013283 /* sanity check on queue memory */
13284 if (!cq || !eq)
13285 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013286 if (!phba->sli4_hba.pc_sli4_params.supported)
13287 hw_page_size = SLI4_PAGE_SIZE;
13288
James Smart4f774512009-05-22 14:52:35 -040013289 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13290 if (!mbox)
13291 return -ENOMEM;
13292 length = (sizeof(struct lpfc_mbx_cq_create) -
13293 sizeof(struct lpfc_sli4_cfg_mhdr));
13294 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13295 LPFC_MBOX_OPCODE_CQ_CREATE,
13296 length, LPFC_SLI4_MBX_EMBED);
13297 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013298 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013299 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13300 cq->page_count);
13301 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13302 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013303 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13304 phba->sli4_hba.pc_sli4_params.cqv);
13305 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013306 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13307 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013308 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13309 eq->queue_id);
13310 } else {
13311 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13312 eq->queue_id);
13313 }
James Smart4f774512009-05-22 14:52:35 -040013314 switch (cq->entry_count) {
13315 default:
13316 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13317 "0361 Unsupported CQ count. (%d)\n",
13318 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013319 if (cq->entry_count < 256) {
13320 status = -EINVAL;
13321 goto out;
13322 }
James Smart4f774512009-05-22 14:52:35 -040013323 /* otherwise default to smallest count (drop through) */
13324 case 256:
13325 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13326 LPFC_CQ_CNT_256);
13327 break;
13328 case 512:
13329 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13330 LPFC_CQ_CNT_512);
13331 break;
13332 case 1024:
13333 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13334 LPFC_CQ_CNT_1024);
13335 break;
13336 }
13337 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013338 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013339 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13340 putPaddrLow(dmabuf->phys);
13341 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13342 putPaddrHigh(dmabuf->phys);
13343 }
13344 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13345
13346 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013347 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13348 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13349 if (shdr_status || shdr_add_status || rc) {
13350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13351 "2501 CQ_CREATE mailbox failed with "
13352 "status x%x add_status x%x, mbx status x%x\n",
13353 shdr_status, shdr_add_status, rc);
13354 status = -ENXIO;
13355 goto out;
13356 }
13357 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13358 if (cq->queue_id == 0xFFFF) {
13359 status = -ENXIO;
13360 goto out;
13361 }
13362 /* link the cq onto the parent eq child list */
13363 list_add_tail(&cq->list, &eq->child_list);
13364 /* Set up completion queue's type and subtype */
13365 cq->type = type;
13366 cq->subtype = subtype;
13367 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013368 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013369 cq->host_index = 0;
13370 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013371
James Smart8fa38512009-07-19 10:01:03 -040013372out:
13373 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013374 return status;
13375}
13376
13377/**
James Smartb19a0612010-04-06 14:48:51 -040013378 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013379 * @phba: HBA structure that indicates port to create a queue on.
13380 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013381 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13382 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013383 *
James Smartb19a0612010-04-06 14:48:51 -040013384 * This function provides failback (fb) functionality when the
13385 * mq_create_ext fails on older FW generations. It's purpose is identical
13386 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013387 *
James Smartb19a0612010-04-06 14:48:51 -040013388 * This routine cannot fail as all attributes were previously accessed and
13389 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013390 **/
James Smartb19a0612010-04-06 14:48:51 -040013391static void
13392lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13393 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013394{
13395 struct lpfc_mbx_mq_create *mq_create;
13396 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013397 int length;
James Smart04c68492009-05-22 14:52:52 -040013398
James Smart04c68492009-05-22 14:52:52 -040013399 length = (sizeof(struct lpfc_mbx_mq_create) -
13400 sizeof(struct lpfc_sli4_cfg_mhdr));
13401 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13402 LPFC_MBOX_OPCODE_MQ_CREATE,
13403 length, LPFC_SLI4_MBX_EMBED);
13404 mq_create = &mbox->u.mqe.un.mq_create;
13405 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013406 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013407 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013408 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013409 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13410 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013411 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013412 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13413 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013414 break;
13415 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013416 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13417 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013418 break;
13419 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013420 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13421 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013422 break;
13423 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013424 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13425 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013426 break;
13427 }
13428 list_for_each_entry(dmabuf, &mq->page_list, list) {
13429 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013430 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013431 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013432 putPaddrHigh(dmabuf->phys);
13433 }
13434}
13435
13436/**
13437 * lpfc_mq_create - Create a mailbox Queue on the HBA
13438 * @phba: HBA structure that indicates port to create a queue on.
13439 * @mq: The queue structure to use to create the mailbox queue.
13440 * @cq: The completion queue to associate with this cq.
13441 * @subtype: The queue's subtype.
13442 *
13443 * This function creates a mailbox queue, as detailed in @mq, on a port,
13444 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13445 *
13446 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13447 * is used to get the entry count and entry size that are necessary to
13448 * determine the number of pages to allocate and use for this queue. This
13449 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13450 * mailbox queue. This function is asynchronous and will wait for the mailbox
13451 * command to finish before continuing.
13452 *
13453 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013454 * memory this function will return -ENOMEM. If the queue create mailbox command
13455 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013456 **/
13457int32_t
13458lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13459 struct lpfc_queue *cq, uint32_t subtype)
13460{
13461 struct lpfc_mbx_mq_create *mq_create;
13462 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13463 struct lpfc_dmabuf *dmabuf;
13464 LPFC_MBOXQ_t *mbox;
13465 int rc, length, status = 0;
13466 uint32_t shdr_status, shdr_add_status;
13467 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013468 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013469
James Smart2e90f4b2011-12-13 13:22:37 -050013470 /* sanity check on queue memory */
13471 if (!mq || !cq)
13472 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013473 if (!phba->sli4_hba.pc_sli4_params.supported)
13474 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013475
13476 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13477 if (!mbox)
13478 return -ENOMEM;
13479 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13480 sizeof(struct lpfc_sli4_cfg_mhdr));
13481 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13482 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13483 length, LPFC_SLI4_MBX_EMBED);
13484
13485 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013486 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013487 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13488 &mq_create_ext->u.request, mq->page_count);
13489 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13490 &mq_create_ext->u.request, 1);
13491 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013492 &mq_create_ext->u.request, 1);
13493 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13494 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013495 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13496 &mq_create_ext->u.request, 1);
13497 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13498 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013499 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013500 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13501 phba->sli4_hba.pc_sli4_params.mqv);
13502 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13503 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13504 cq->queue_id);
13505 else
13506 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13507 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013508 switch (mq->entry_count) {
13509 default:
13510 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13511 "0362 Unsupported MQ count. (%d)\n",
13512 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013513 if (mq->entry_count < 16) {
13514 status = -EINVAL;
13515 goto out;
13516 }
James Smartb19a0612010-04-06 14:48:51 -040013517 /* otherwise default to smallest count (drop through) */
13518 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013519 bf_set(lpfc_mq_context_ring_size,
13520 &mq_create_ext->u.request.context,
13521 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013522 break;
13523 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013524 bf_set(lpfc_mq_context_ring_size,
13525 &mq_create_ext->u.request.context,
13526 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013527 break;
13528 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013529 bf_set(lpfc_mq_context_ring_size,
13530 &mq_create_ext->u.request.context,
13531 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013532 break;
13533 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013534 bf_set(lpfc_mq_context_ring_size,
13535 &mq_create_ext->u.request.context,
13536 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013537 break;
13538 }
13539 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013540 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013541 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13542 putPaddrLow(dmabuf->phys);
13543 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013544 putPaddrHigh(dmabuf->phys);
13545 }
13546 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013547 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13548 &mq_create_ext->u.response);
13549 if (rc != MBX_SUCCESS) {
13550 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13551 "2795 MQ_CREATE_EXT failed with "
13552 "status x%x. Failback to MQ_CREATE.\n",
13553 rc);
13554 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13555 mq_create = &mbox->u.mqe.un.mq_create;
13556 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13557 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13558 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13559 &mq_create->u.response);
13560 }
13561
James Smart04c68492009-05-22 14:52:52 -040013562 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013563 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13564 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13565 if (shdr_status || shdr_add_status || rc) {
13566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13567 "2502 MQ_CREATE mailbox failed with "
13568 "status x%x add_status x%x, mbx status x%x\n",
13569 shdr_status, shdr_add_status, rc);
13570 status = -ENXIO;
13571 goto out;
13572 }
James Smart04c68492009-05-22 14:52:52 -040013573 if (mq->queue_id == 0xFFFF) {
13574 status = -ENXIO;
13575 goto out;
13576 }
13577 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013578 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013579 mq->subtype = subtype;
13580 mq->host_index = 0;
13581 mq->hba_index = 0;
13582
13583 /* link the mq onto the parent cq child list */
13584 list_add_tail(&mq->list, &cq->child_list);
13585out:
James Smart8fa38512009-07-19 10:01:03 -040013586 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013587 return status;
13588}
13589
13590/**
James Smart4f774512009-05-22 14:52:35 -040013591 * lpfc_wq_create - Create a Work Queue on the HBA
13592 * @phba: HBA structure that indicates port to create a queue on.
13593 * @wq: The queue structure to use to create the work queue.
13594 * @cq: The completion queue to bind this work queue to.
13595 * @subtype: The subtype of the work queue indicating its functionality.
13596 *
13597 * This function creates a work queue, as detailed in @wq, on a port, described
13598 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13599 *
13600 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13601 * is used to get the entry count and entry size that are necessary to
13602 * determine the number of pages to allocate and use for this queue. The @cq
13603 * is used to indicate which completion queue to bind this work queue to. This
13604 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13605 * work queue. This function is asynchronous and will wait for the mailbox
13606 * command to finish before continuing.
13607 *
13608 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013609 * memory this function will return -ENOMEM. If the queue create mailbox command
13610 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013611 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013612int
James Smart4f774512009-05-22 14:52:35 -040013613lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13614 struct lpfc_queue *cq, uint32_t subtype)
13615{
13616 struct lpfc_mbx_wq_create *wq_create;
13617 struct lpfc_dmabuf *dmabuf;
13618 LPFC_MBOXQ_t *mbox;
13619 int rc, length, status = 0;
13620 uint32_t shdr_status, shdr_add_status;
13621 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013622 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013623 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013624 void __iomem *bar_memmap_p;
13625 uint32_t db_offset;
13626 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013627
James Smart2e90f4b2011-12-13 13:22:37 -050013628 /* sanity check on queue memory */
13629 if (!wq || !cq)
13630 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013631 if (!phba->sli4_hba.pc_sli4_params.supported)
13632 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013633
13634 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13635 if (!mbox)
13636 return -ENOMEM;
13637 length = (sizeof(struct lpfc_mbx_wq_create) -
13638 sizeof(struct lpfc_sli4_cfg_mhdr));
13639 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13640 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13641 length, LPFC_SLI4_MBX_EMBED);
13642 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013643 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013644 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13645 wq->page_count);
13646 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13647 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013648
13649 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013650 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13651 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013652
James Smart0c651872013-07-15 18:33:23 -040013653 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13654 case LPFC_Q_CREATE_VERSION_0:
13655 switch (wq->entry_size) {
13656 default:
13657 case 64:
13658 /* Nothing to do, version 0 ONLY supports 64 byte */
13659 page = wq_create->u.request.page;
13660 break;
13661 case 128:
13662 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13663 LPFC_WQ_SZ128_SUPPORT)) {
13664 status = -ERANGE;
13665 goto out;
13666 }
13667 /* If we get here the HBA MUST also support V1 and
13668 * we MUST use it
13669 */
13670 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13671 LPFC_Q_CREATE_VERSION_1);
13672
13673 bf_set(lpfc_mbx_wq_create_wqe_count,
13674 &wq_create->u.request_1, wq->entry_count);
13675 bf_set(lpfc_mbx_wq_create_wqe_size,
13676 &wq_create->u.request_1,
13677 LPFC_WQ_WQE_SIZE_128);
13678 bf_set(lpfc_mbx_wq_create_page_size,
13679 &wq_create->u.request_1,
13680 (PAGE_SIZE/SLI4_PAGE_SIZE));
13681 page = wq_create->u.request_1.page;
13682 break;
13683 }
13684 break;
13685 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013686 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13687 wq->entry_count);
13688 switch (wq->entry_size) {
13689 default:
13690 case 64:
13691 bf_set(lpfc_mbx_wq_create_wqe_size,
13692 &wq_create->u.request_1,
13693 LPFC_WQ_WQE_SIZE_64);
13694 break;
13695 case 128:
James Smart0c651872013-07-15 18:33:23 -040013696 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13697 LPFC_WQ_SZ128_SUPPORT)) {
13698 status = -ERANGE;
13699 goto out;
13700 }
James Smart5a6f1332011-03-11 16:05:35 -050013701 bf_set(lpfc_mbx_wq_create_wqe_size,
13702 &wq_create->u.request_1,
13703 LPFC_WQ_WQE_SIZE_128);
13704 break;
13705 }
13706 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13707 (PAGE_SIZE/SLI4_PAGE_SIZE));
13708 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013709 break;
13710 default:
13711 status = -ERANGE;
13712 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013713 }
James Smart0c651872013-07-15 18:33:23 -040013714
James Smart4f774512009-05-22 14:52:35 -040013715 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013716 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013717 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13718 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013719 }
James Smart962bc512013-01-03 15:44:00 -050013720
13721 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13722 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13723
James Smart4f774512009-05-22 14:52:35 -040013724 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13725 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013726 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13727 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13728 if (shdr_status || shdr_add_status || rc) {
13729 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13730 "2503 WQ_CREATE mailbox failed with "
13731 "status x%x add_status x%x, mbx status x%x\n",
13732 shdr_status, shdr_add_status, rc);
13733 status = -ENXIO;
13734 goto out;
13735 }
13736 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13737 if (wq->queue_id == 0xFFFF) {
13738 status = -ENXIO;
13739 goto out;
13740 }
James Smart962bc512013-01-03 15:44:00 -050013741 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13742 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13743 &wq_create->u.response);
13744 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13745 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13747 "3265 WQ[%d] doorbell format not "
13748 "supported: x%x\n", wq->queue_id,
13749 wq->db_format);
13750 status = -EINVAL;
13751 goto out;
13752 }
13753 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13754 &wq_create->u.response);
13755 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13756 if (!bar_memmap_p) {
13757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13758 "3263 WQ[%d] failed to memmap pci "
13759 "barset:x%x\n", wq->queue_id,
13760 pci_barset);
13761 status = -ENOMEM;
13762 goto out;
13763 }
13764 db_offset = wq_create->u.response.doorbell_offset;
13765 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13766 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13767 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13768 "3252 WQ[%d] doorbell offset not "
13769 "supported: x%x\n", wq->queue_id,
13770 db_offset);
13771 status = -EINVAL;
13772 goto out;
13773 }
13774 wq->db_regaddr = bar_memmap_p + db_offset;
13775 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013776 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13777 "format:x%x\n", wq->queue_id, pci_barset,
13778 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013779 } else {
13780 wq->db_format = LPFC_DB_LIST_FORMAT;
13781 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13782 }
James Smart4f774512009-05-22 14:52:35 -040013783 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013784 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013785 wq->subtype = subtype;
13786 wq->host_index = 0;
13787 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013788 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013789
13790 /* link the wq onto the parent cq child list */
13791 list_add_tail(&wq->list, &cq->child_list);
13792out:
James Smart8fa38512009-07-19 10:01:03 -040013793 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013794 return status;
13795}
13796
13797/**
James Smart73d91e52011-10-10 21:32:10 -040013798 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13799 * @phba: HBA structure that indicates port to create a queue on.
13800 * @rq: The queue structure to use for the receive queue.
13801 * @qno: The associated HBQ number
13802 *
13803 *
13804 * For SLI4 we need to adjust the RQ repost value based on
13805 * the number of buffers that are initially posted to the RQ.
13806 */
13807void
13808lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13809{
13810 uint32_t cnt;
13811
James Smart2e90f4b2011-12-13 13:22:37 -050013812 /* sanity check on queue memory */
13813 if (!rq)
13814 return;
James Smart73d91e52011-10-10 21:32:10 -040013815 cnt = lpfc_hbq_defs[qno]->entry_count;
13816
13817 /* Recalc repost for RQs based on buffers initially posted */
13818 cnt = (cnt >> 3);
13819 if (cnt < LPFC_QUEUE_MIN_REPOST)
13820 cnt = LPFC_QUEUE_MIN_REPOST;
13821
13822 rq->entry_repost = cnt;
13823}
13824
13825/**
James Smart4f774512009-05-22 14:52:35 -040013826 * lpfc_rq_create - Create a Receive Queue on the HBA
13827 * @phba: HBA structure that indicates port to create a queue on.
13828 * @hrq: The queue structure to use to create the header receive queue.
13829 * @drq: The queue structure to use to create the data receive queue.
13830 * @cq: The completion queue to bind this work queue to.
13831 *
13832 * This function creates a receive buffer queue pair , as detailed in @hrq and
13833 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13834 * to the HBA.
13835 *
13836 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13837 * struct is used to get the entry count that is necessary to determine the
13838 * number of pages to use for this queue. The @cq is used to indicate which
13839 * completion queue to bind received buffers that are posted to these queues to.
13840 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13841 * receive queue pair. This function is asynchronous and will wait for the
13842 * mailbox command to finish before continuing.
13843 *
13844 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013845 * memory this function will return -ENOMEM. If the queue create mailbox command
13846 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013847 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013848int
James Smart4f774512009-05-22 14:52:35 -040013849lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13850 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13851{
13852 struct lpfc_mbx_rq_create *rq_create;
13853 struct lpfc_dmabuf *dmabuf;
13854 LPFC_MBOXQ_t *mbox;
13855 int rc, length, status = 0;
13856 uint32_t shdr_status, shdr_add_status;
13857 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013858 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013859 void __iomem *bar_memmap_p;
13860 uint32_t db_offset;
13861 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013862
James Smart2e90f4b2011-12-13 13:22:37 -050013863 /* sanity check on queue memory */
13864 if (!hrq || !drq || !cq)
13865 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013866 if (!phba->sli4_hba.pc_sli4_params.supported)
13867 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013868
13869 if (hrq->entry_count != drq->entry_count)
13870 return -EINVAL;
13871 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13872 if (!mbox)
13873 return -ENOMEM;
13874 length = (sizeof(struct lpfc_mbx_rq_create) -
13875 sizeof(struct lpfc_sli4_cfg_mhdr));
13876 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13877 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13878 length, LPFC_SLI4_MBX_EMBED);
13879 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013880 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13881 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13882 phba->sli4_hba.pc_sli4_params.rqv);
13883 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13884 bf_set(lpfc_rq_context_rqe_count_1,
13885 &rq_create->u.request.context,
13886 hrq->entry_count);
13887 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013888 bf_set(lpfc_rq_context_rqe_size,
13889 &rq_create->u.request.context,
13890 LPFC_RQE_SIZE_8);
13891 bf_set(lpfc_rq_context_page_size,
13892 &rq_create->u.request.context,
13893 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013894 } else {
13895 switch (hrq->entry_count) {
13896 default:
13897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13898 "2535 Unsupported RQ count. (%d)\n",
13899 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013900 if (hrq->entry_count < 512) {
13901 status = -EINVAL;
13902 goto out;
13903 }
James Smart5a6f1332011-03-11 16:05:35 -050013904 /* otherwise default to smallest count (drop through) */
13905 case 512:
13906 bf_set(lpfc_rq_context_rqe_count,
13907 &rq_create->u.request.context,
13908 LPFC_RQ_RING_SIZE_512);
13909 break;
13910 case 1024:
13911 bf_set(lpfc_rq_context_rqe_count,
13912 &rq_create->u.request.context,
13913 LPFC_RQ_RING_SIZE_1024);
13914 break;
13915 case 2048:
13916 bf_set(lpfc_rq_context_rqe_count,
13917 &rq_create->u.request.context,
13918 LPFC_RQ_RING_SIZE_2048);
13919 break;
13920 case 4096:
13921 bf_set(lpfc_rq_context_rqe_count,
13922 &rq_create->u.request.context,
13923 LPFC_RQ_RING_SIZE_4096);
13924 break;
13925 }
13926 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13927 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013928 }
13929 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13930 cq->queue_id);
13931 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13932 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013933 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013934 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013935 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13936 putPaddrLow(dmabuf->phys);
13937 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13938 putPaddrHigh(dmabuf->phys);
13939 }
James Smart962bc512013-01-03 15:44:00 -050013940 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13941 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13942
James Smart4f774512009-05-22 14:52:35 -040013943 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13944 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013945 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13946 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13947 if (shdr_status || shdr_add_status || rc) {
13948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13949 "2504 RQ_CREATE mailbox failed with "
13950 "status x%x add_status x%x, mbx status x%x\n",
13951 shdr_status, shdr_add_status, rc);
13952 status = -ENXIO;
13953 goto out;
13954 }
13955 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13956 if (hrq->queue_id == 0xFFFF) {
13957 status = -ENXIO;
13958 goto out;
13959 }
James Smart962bc512013-01-03 15:44:00 -050013960
13961 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13962 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13963 &rq_create->u.response);
13964 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13965 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13967 "3262 RQ [%d] doorbell format not "
13968 "supported: x%x\n", hrq->queue_id,
13969 hrq->db_format);
13970 status = -EINVAL;
13971 goto out;
13972 }
13973
13974 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13975 &rq_create->u.response);
13976 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13977 if (!bar_memmap_p) {
13978 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13979 "3269 RQ[%d] failed to memmap pci "
13980 "barset:x%x\n", hrq->queue_id,
13981 pci_barset);
13982 status = -ENOMEM;
13983 goto out;
13984 }
13985
13986 db_offset = rq_create->u.response.doorbell_offset;
13987 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13988 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13990 "3270 RQ[%d] doorbell offset not "
13991 "supported: x%x\n", hrq->queue_id,
13992 db_offset);
13993 status = -EINVAL;
13994 goto out;
13995 }
13996 hrq->db_regaddr = bar_memmap_p + db_offset;
13997 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013998 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13999 "format:x%x\n", hrq->queue_id, pci_barset,
14000 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014001 } else {
14002 hrq->db_format = LPFC_DB_RING_FORMAT;
14003 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14004 }
James Smart4f774512009-05-22 14:52:35 -040014005 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014006 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014007 hrq->subtype = subtype;
14008 hrq->host_index = 0;
14009 hrq->hba_index = 0;
14010
14011 /* now create the data queue */
14012 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14013 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14014 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014015 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14016 phba->sli4_hba.pc_sli4_params.rqv);
14017 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14018 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014019 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014020 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014021 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14022 LPFC_RQE_SIZE_8);
14023 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14024 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014025 } else {
14026 switch (drq->entry_count) {
14027 default:
14028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14029 "2536 Unsupported RQ count. (%d)\n",
14030 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014031 if (drq->entry_count < 512) {
14032 status = -EINVAL;
14033 goto out;
14034 }
James Smart5a6f1332011-03-11 16:05:35 -050014035 /* otherwise default to smallest count (drop through) */
14036 case 512:
14037 bf_set(lpfc_rq_context_rqe_count,
14038 &rq_create->u.request.context,
14039 LPFC_RQ_RING_SIZE_512);
14040 break;
14041 case 1024:
14042 bf_set(lpfc_rq_context_rqe_count,
14043 &rq_create->u.request.context,
14044 LPFC_RQ_RING_SIZE_1024);
14045 break;
14046 case 2048:
14047 bf_set(lpfc_rq_context_rqe_count,
14048 &rq_create->u.request.context,
14049 LPFC_RQ_RING_SIZE_2048);
14050 break;
14051 case 4096:
14052 bf_set(lpfc_rq_context_rqe_count,
14053 &rq_create->u.request.context,
14054 LPFC_RQ_RING_SIZE_4096);
14055 break;
14056 }
14057 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14058 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014059 }
14060 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14061 cq->queue_id);
14062 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14063 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014064 list_for_each_entry(dmabuf, &drq->page_list, list) {
14065 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14066 putPaddrLow(dmabuf->phys);
14067 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14068 putPaddrHigh(dmabuf->phys);
14069 }
James Smart962bc512013-01-03 15:44:00 -050014070 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14071 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014072 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14073 /* The IOCTL status is embedded in the mailbox subheader. */
14074 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14075 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14076 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14077 if (shdr_status || shdr_add_status || rc) {
14078 status = -ENXIO;
14079 goto out;
14080 }
14081 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14082 if (drq->queue_id == 0xFFFF) {
14083 status = -ENXIO;
14084 goto out;
14085 }
14086 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014087 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014088 drq->subtype = subtype;
14089 drq->host_index = 0;
14090 drq->hba_index = 0;
14091
14092 /* link the header and data RQs onto the parent cq child list */
14093 list_add_tail(&hrq->list, &cq->child_list);
14094 list_add_tail(&drq->list, &cq->child_list);
14095
14096out:
James Smart8fa38512009-07-19 10:01:03 -040014097 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014098 return status;
14099}
14100
14101/**
14102 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14103 * @eq: The queue structure associated with the queue to destroy.
14104 *
14105 * This function destroys a queue, as detailed in @eq by sending an mailbox
14106 * command, specific to the type of queue, to the HBA.
14107 *
14108 * The @eq struct is used to get the queue ID of the queue to destroy.
14109 *
14110 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014111 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014112 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014113int
James Smart4f774512009-05-22 14:52:35 -040014114lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14115{
14116 LPFC_MBOXQ_t *mbox;
14117 int rc, length, status = 0;
14118 uint32_t shdr_status, shdr_add_status;
14119 union lpfc_sli4_cfg_shdr *shdr;
14120
James Smart2e90f4b2011-12-13 13:22:37 -050014121 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014122 if (!eq)
14123 return -ENODEV;
14124 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14125 if (!mbox)
14126 return -ENOMEM;
14127 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14128 sizeof(struct lpfc_sli4_cfg_mhdr));
14129 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14130 LPFC_MBOX_OPCODE_EQ_DESTROY,
14131 length, LPFC_SLI4_MBX_EMBED);
14132 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14133 eq->queue_id);
14134 mbox->vport = eq->phba->pport;
14135 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14136
14137 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14138 /* The IOCTL status is embedded in the mailbox subheader. */
14139 shdr = (union lpfc_sli4_cfg_shdr *)
14140 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14141 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14142 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14143 if (shdr_status || shdr_add_status || rc) {
14144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14145 "2505 EQ_DESTROY mailbox failed with "
14146 "status x%x add_status x%x, mbx status x%x\n",
14147 shdr_status, shdr_add_status, rc);
14148 status = -ENXIO;
14149 }
14150
14151 /* Remove eq from any list */
14152 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014153 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014154 return status;
14155}
14156
14157/**
14158 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14159 * @cq: The queue structure associated with the queue to destroy.
14160 *
14161 * This function destroys a queue, as detailed in @cq by sending an mailbox
14162 * command, specific to the type of queue, to the HBA.
14163 *
14164 * The @cq struct is used to get the queue ID of the queue to destroy.
14165 *
14166 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014167 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014168 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014169int
James Smart4f774512009-05-22 14:52:35 -040014170lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14171{
14172 LPFC_MBOXQ_t *mbox;
14173 int rc, length, status = 0;
14174 uint32_t shdr_status, shdr_add_status;
14175 union lpfc_sli4_cfg_shdr *shdr;
14176
James Smart2e90f4b2011-12-13 13:22:37 -050014177 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014178 if (!cq)
14179 return -ENODEV;
14180 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14181 if (!mbox)
14182 return -ENOMEM;
14183 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14184 sizeof(struct lpfc_sli4_cfg_mhdr));
14185 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14186 LPFC_MBOX_OPCODE_CQ_DESTROY,
14187 length, LPFC_SLI4_MBX_EMBED);
14188 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14189 cq->queue_id);
14190 mbox->vport = cq->phba->pport;
14191 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14192 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14193 /* The IOCTL status is embedded in the mailbox subheader. */
14194 shdr = (union lpfc_sli4_cfg_shdr *)
14195 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14196 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14197 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14198 if (shdr_status || shdr_add_status || rc) {
14199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14200 "2506 CQ_DESTROY mailbox failed with "
14201 "status x%x add_status x%x, mbx status x%x\n",
14202 shdr_status, shdr_add_status, rc);
14203 status = -ENXIO;
14204 }
14205 /* Remove cq from any list */
14206 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014207 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014208 return status;
14209}
14210
14211/**
James Smart04c68492009-05-22 14:52:52 -040014212 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14213 * @qm: The queue structure associated with the queue to destroy.
14214 *
14215 * This function destroys a queue, as detailed in @mq by sending an mailbox
14216 * command, specific to the type of queue, to the HBA.
14217 *
14218 * The @mq struct is used to get the queue ID of the queue to destroy.
14219 *
14220 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014221 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014222 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014223int
James Smart04c68492009-05-22 14:52:52 -040014224lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14225{
14226 LPFC_MBOXQ_t *mbox;
14227 int rc, length, status = 0;
14228 uint32_t shdr_status, shdr_add_status;
14229 union lpfc_sli4_cfg_shdr *shdr;
14230
James Smart2e90f4b2011-12-13 13:22:37 -050014231 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014232 if (!mq)
14233 return -ENODEV;
14234 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14235 if (!mbox)
14236 return -ENOMEM;
14237 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14238 sizeof(struct lpfc_sli4_cfg_mhdr));
14239 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14240 LPFC_MBOX_OPCODE_MQ_DESTROY,
14241 length, LPFC_SLI4_MBX_EMBED);
14242 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14243 mq->queue_id);
14244 mbox->vport = mq->phba->pport;
14245 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14246 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14247 /* The IOCTL status is embedded in the mailbox subheader. */
14248 shdr = (union lpfc_sli4_cfg_shdr *)
14249 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14250 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14251 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14252 if (shdr_status || shdr_add_status || rc) {
14253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14254 "2507 MQ_DESTROY mailbox failed with "
14255 "status x%x add_status x%x, mbx status x%x\n",
14256 shdr_status, shdr_add_status, rc);
14257 status = -ENXIO;
14258 }
14259 /* Remove mq from any list */
14260 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014261 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014262 return status;
14263}
14264
14265/**
James Smart4f774512009-05-22 14:52:35 -040014266 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14267 * @wq: The queue structure associated with the queue to destroy.
14268 *
14269 * This function destroys a queue, as detailed in @wq by sending an mailbox
14270 * command, specific to the type of queue, to the HBA.
14271 *
14272 * The @wq struct is used to get the queue ID of the queue to destroy.
14273 *
14274 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014275 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014276 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014277int
James Smart4f774512009-05-22 14:52:35 -040014278lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14279{
14280 LPFC_MBOXQ_t *mbox;
14281 int rc, length, status = 0;
14282 uint32_t shdr_status, shdr_add_status;
14283 union lpfc_sli4_cfg_shdr *shdr;
14284
James Smart2e90f4b2011-12-13 13:22:37 -050014285 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014286 if (!wq)
14287 return -ENODEV;
14288 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14289 if (!mbox)
14290 return -ENOMEM;
14291 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14292 sizeof(struct lpfc_sli4_cfg_mhdr));
14293 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14294 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14295 length, LPFC_SLI4_MBX_EMBED);
14296 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14297 wq->queue_id);
14298 mbox->vport = wq->phba->pport;
14299 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14300 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14301 shdr = (union lpfc_sli4_cfg_shdr *)
14302 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14303 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14304 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14305 if (shdr_status || shdr_add_status || rc) {
14306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14307 "2508 WQ_DESTROY mailbox failed with "
14308 "status x%x add_status x%x, mbx status x%x\n",
14309 shdr_status, shdr_add_status, rc);
14310 status = -ENXIO;
14311 }
14312 /* Remove wq from any list */
14313 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014314 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014315 return status;
14316}
14317
14318/**
14319 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14320 * @rq: The queue structure associated with the queue to destroy.
14321 *
14322 * This function destroys a queue, as detailed in @rq by sending an mailbox
14323 * command, specific to the type of queue, to the HBA.
14324 *
14325 * The @rq struct is used to get the queue ID of the queue to destroy.
14326 *
14327 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014328 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014329 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014330int
James Smart4f774512009-05-22 14:52:35 -040014331lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14332 struct lpfc_queue *drq)
14333{
14334 LPFC_MBOXQ_t *mbox;
14335 int rc, length, status = 0;
14336 uint32_t shdr_status, shdr_add_status;
14337 union lpfc_sli4_cfg_shdr *shdr;
14338
James Smart2e90f4b2011-12-13 13:22:37 -050014339 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014340 if (!hrq || !drq)
14341 return -ENODEV;
14342 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14343 if (!mbox)
14344 return -ENOMEM;
14345 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014346 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014347 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14348 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14349 length, LPFC_SLI4_MBX_EMBED);
14350 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14351 hrq->queue_id);
14352 mbox->vport = hrq->phba->pport;
14353 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14354 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14355 /* The IOCTL status is embedded in the mailbox subheader. */
14356 shdr = (union lpfc_sli4_cfg_shdr *)
14357 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14358 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14359 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14360 if (shdr_status || shdr_add_status || rc) {
14361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14362 "2509 RQ_DESTROY mailbox failed with "
14363 "status x%x add_status x%x, mbx status x%x\n",
14364 shdr_status, shdr_add_status, rc);
14365 if (rc != MBX_TIMEOUT)
14366 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14367 return -ENXIO;
14368 }
14369 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14370 drq->queue_id);
14371 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14372 shdr = (union lpfc_sli4_cfg_shdr *)
14373 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14374 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14375 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14376 if (shdr_status || shdr_add_status || rc) {
14377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14378 "2510 RQ_DESTROY mailbox failed with "
14379 "status x%x add_status x%x, mbx status x%x\n",
14380 shdr_status, shdr_add_status, rc);
14381 status = -ENXIO;
14382 }
14383 list_del_init(&hrq->list);
14384 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014385 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014386 return status;
14387}
14388
14389/**
14390 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14391 * @phba: The virtual port for which this call being executed.
14392 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14393 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14394 * @xritag: the xritag that ties this io to the SGL pages.
14395 *
14396 * This routine will post the sgl pages for the IO that has the xritag
14397 * that is in the iocbq structure. The xritag is assigned during iocbq
14398 * creation and persists for as long as the driver is loaded.
14399 * if the caller has fewer than 256 scatter gather segments to map then
14400 * pdma_phys_addr1 should be 0.
14401 * If the caller needs to map more than 256 scatter gather segment then
14402 * pdma_phys_addr1 should be a valid physical address.
14403 * physical address for SGLs must be 64 byte aligned.
14404 * If you are going to map 2 SGL's then the first one must have 256 entries
14405 * the second sgl can have between 1 and 256 entries.
14406 *
14407 * Return codes:
14408 * 0 - Success
14409 * -ENXIO, -ENOMEM - Failure
14410 **/
14411int
14412lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14413 dma_addr_t pdma_phys_addr0,
14414 dma_addr_t pdma_phys_addr1,
14415 uint16_t xritag)
14416{
14417 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14418 LPFC_MBOXQ_t *mbox;
14419 int rc;
14420 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014421 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014422 union lpfc_sli4_cfg_shdr *shdr;
14423
14424 if (xritag == NO_XRI) {
14425 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14426 "0364 Invalid param:\n");
14427 return -EINVAL;
14428 }
14429
14430 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14431 if (!mbox)
14432 return -ENOMEM;
14433
14434 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14435 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14436 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014437 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014438
14439 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14440 &mbox->u.mqe.un.post_sgl_pages;
14441 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14442 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14443
14444 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14445 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14446 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14447 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14448
14449 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14450 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14451 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14452 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14453 if (!phba->sli4_hba.intr_enable)
14454 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014455 else {
James Smarta183a152011-10-10 21:32:43 -040014456 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014457 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14458 }
James Smart4f774512009-05-22 14:52:35 -040014459 /* The IOCTL status is embedded in the mailbox subheader. */
14460 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14461 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14462 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14463 if (rc != MBX_TIMEOUT)
14464 mempool_free(mbox, phba->mbox_mem_pool);
14465 if (shdr_status || shdr_add_status || rc) {
14466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14467 "2511 POST_SGL mailbox failed with "
14468 "status x%x add_status x%x, mbx status x%x\n",
14469 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014470 }
14471 return 0;
14472}
James Smart4f774512009-05-22 14:52:35 -040014473
14474/**
James Smart88a2cfb2011-07-22 18:36:33 -040014475 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014476 * @phba: pointer to lpfc hba data structure.
14477 *
14478 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014479 * HBA consistent with the SLI-4 interface spec. This routine
14480 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14481 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014482 *
James Smart88a2cfb2011-07-22 18:36:33 -040014483 * Returns
14484 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14485 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14486 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014487static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014488lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14489{
14490 unsigned long xri;
14491
14492 /*
14493 * Fetch the next logical xri. Because this index is logical,
14494 * the driver starts at 0 each time.
14495 */
14496 spin_lock_irq(&phba->hbalock);
14497 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14498 phba->sli4_hba.max_cfg_param.max_xri, 0);
14499 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14500 spin_unlock_irq(&phba->hbalock);
14501 return NO_XRI;
14502 } else {
14503 set_bit(xri, phba->sli4_hba.xri_bmask);
14504 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014505 }
James Smart6d368e52011-05-24 11:44:12 -040014506 spin_unlock_irq(&phba->hbalock);
14507 return xri;
14508}
14509
14510/**
14511 * lpfc_sli4_free_xri - Release an xri for reuse.
14512 * @phba: pointer to lpfc hba data structure.
14513 *
14514 * This routine is invoked to release an xri to the pool of
14515 * available rpis maintained by the driver.
14516 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014517static void
James Smart6d368e52011-05-24 11:44:12 -040014518__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14519{
14520 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014521 phba->sli4_hba.max_cfg_param.xri_used--;
14522 }
14523}
14524
14525/**
14526 * lpfc_sli4_free_xri - Release an xri for reuse.
14527 * @phba: pointer to lpfc hba data structure.
14528 *
14529 * This routine is invoked to release an xri to the pool of
14530 * available rpis maintained by the driver.
14531 **/
14532void
14533lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14534{
14535 spin_lock_irq(&phba->hbalock);
14536 __lpfc_sli4_free_xri(phba, xri);
14537 spin_unlock_irq(&phba->hbalock);
14538}
14539
14540/**
James Smart4f774512009-05-22 14:52:35 -040014541 * lpfc_sli4_next_xritag - Get an xritag for the io
14542 * @phba: Pointer to HBA context object.
14543 *
14544 * This function gets an xritag for the iocb. If there is no unused xritag
14545 * it will return 0xffff.
14546 * The function returns the allocated xritag if successful, else returns zero.
14547 * Zero is not a valid xritag.
14548 * The caller is not required to hold any lock.
14549 **/
14550uint16_t
14551lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14552{
James Smart6d368e52011-05-24 11:44:12 -040014553 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014554
James Smart6d368e52011-05-24 11:44:12 -040014555 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014556 if (xri_index == NO_XRI)
14557 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14558 "2004 Failed to allocate XRI.last XRITAG is %d"
14559 " Max XRI is %d, Used XRI is %d\n",
14560 xri_index,
14561 phba->sli4_hba.max_cfg_param.max_xri,
14562 phba->sli4_hba.max_cfg_param.xri_used);
14563 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014564}
14565
14566/**
James Smart6d368e52011-05-24 11:44:12 -040014567 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014568 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014569 * @post_sgl_list: pointer to els sgl entry list.
14570 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014571 *
14572 * This routine is invoked to post a block of driver's sgl pages to the
14573 * HBA using non-embedded mailbox command. No Lock is held. This routine
14574 * is only called when the driver is loading and after all IO has been
14575 * stopped.
14576 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014577static int
14578lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14579 struct list_head *post_sgl_list,
14580 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014581{
James Smart8a9d2e82012-05-09 21:16:12 -040014582 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014583 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14584 struct sgl_page_pairs *sgl_pg_pairs;
14585 void *viraddr;
14586 LPFC_MBOXQ_t *mbox;
14587 uint32_t reqlen, alloclen, pg_pairs;
14588 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014589 uint16_t xritag_start = 0;
14590 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014591 uint32_t shdr_status, shdr_add_status;
14592 union lpfc_sli4_cfg_shdr *shdr;
14593
James Smart8a9d2e82012-05-09 21:16:12 -040014594 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014595 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014596 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014597 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14598 "2559 Block sgl registration required DMA "
14599 "size (%d) great than a page\n", reqlen);
14600 return -ENOMEM;
14601 }
14602 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014603 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014604 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014605
14606 /* Allocate DMA memory and set up the non-embedded mailbox command */
14607 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14608 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14609 LPFC_SLI4_MBX_NEMBED);
14610
14611 if (alloclen < reqlen) {
14612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14613 "0285 Allocated DMA memory size (%d) is "
14614 "less than the requested DMA memory "
14615 "size (%d)\n", alloclen, reqlen);
14616 lpfc_sli4_mbox_cmd_free(phba, mbox);
14617 return -ENOMEM;
14618 }
James Smart4f774512009-05-22 14:52:35 -040014619 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014620 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014621 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14622 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14623
James Smart8a9d2e82012-05-09 21:16:12 -040014624 pg_pairs = 0;
14625 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014626 /* Set up the sge entry */
14627 sgl_pg_pairs->sgl_pg0_addr_lo =
14628 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14629 sgl_pg_pairs->sgl_pg0_addr_hi =
14630 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14631 sgl_pg_pairs->sgl_pg1_addr_lo =
14632 cpu_to_le32(putPaddrLow(0));
14633 sgl_pg_pairs->sgl_pg1_addr_hi =
14634 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014635
James Smart4f774512009-05-22 14:52:35 -040014636 /* Keep the first xritag on the list */
14637 if (pg_pairs == 0)
14638 xritag_start = sglq_entry->sli4_xritag;
14639 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014640 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014641 }
James Smart6d368e52011-05-24 11:44:12 -040014642
14643 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014644 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014645 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014646 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014647 if (!phba->sli4_hba.intr_enable)
14648 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14649 else {
James Smarta183a152011-10-10 21:32:43 -040014650 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014651 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14652 }
14653 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14654 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14655 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14656 if (rc != MBX_TIMEOUT)
14657 lpfc_sli4_mbox_cmd_free(phba, mbox);
14658 if (shdr_status || shdr_add_status || rc) {
14659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14660 "2513 POST_SGL_BLOCK mailbox command failed "
14661 "status x%x add_status x%x mbx status x%x\n",
14662 shdr_status, shdr_add_status, rc);
14663 rc = -ENXIO;
14664 }
14665 return rc;
14666}
14667
14668/**
14669 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14670 * @phba: pointer to lpfc hba data structure.
14671 * @sblist: pointer to scsi buffer list.
14672 * @count: number of scsi buffers on the list.
14673 *
14674 * This routine is invoked to post a block of @count scsi sgl pages from a
14675 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14676 * No Lock is held.
14677 *
14678 **/
14679int
James Smart8a9d2e82012-05-09 21:16:12 -040014680lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14681 struct list_head *sblist,
14682 int count)
James Smart4f774512009-05-22 14:52:35 -040014683{
14684 struct lpfc_scsi_buf *psb;
14685 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14686 struct sgl_page_pairs *sgl_pg_pairs;
14687 void *viraddr;
14688 LPFC_MBOXQ_t *mbox;
14689 uint32_t reqlen, alloclen, pg_pairs;
14690 uint32_t mbox_tmo;
14691 uint16_t xritag_start = 0;
14692 int rc = 0;
14693 uint32_t shdr_status, shdr_add_status;
14694 dma_addr_t pdma_phys_bpl1;
14695 union lpfc_sli4_cfg_shdr *shdr;
14696
14697 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014698 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014699 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014700 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014701 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14702 "0217 Block sgl registration required DMA "
14703 "size (%d) great than a page\n", reqlen);
14704 return -ENOMEM;
14705 }
14706 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14707 if (!mbox) {
14708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14709 "0283 Failed to allocate mbox cmd memory\n");
14710 return -ENOMEM;
14711 }
14712
14713 /* Allocate DMA memory and set up the non-embedded mailbox command */
14714 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14715 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14716 LPFC_SLI4_MBX_NEMBED);
14717
14718 if (alloclen < reqlen) {
14719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14720 "2561 Allocated DMA memory size (%d) is "
14721 "less than the requested DMA memory "
14722 "size (%d)\n", alloclen, reqlen);
14723 lpfc_sli4_mbox_cmd_free(phba, mbox);
14724 return -ENOMEM;
14725 }
James Smart6d368e52011-05-24 11:44:12 -040014726
James Smart4f774512009-05-22 14:52:35 -040014727 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014728 viraddr = mbox->sge_array->addr[0];
14729
14730 /* Set up the SGL pages in the non-embedded DMA pages */
14731 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14732 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14733
14734 pg_pairs = 0;
14735 list_for_each_entry(psb, sblist, list) {
14736 /* Set up the sge entry */
14737 sgl_pg_pairs->sgl_pg0_addr_lo =
14738 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14739 sgl_pg_pairs->sgl_pg0_addr_hi =
14740 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14741 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14742 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14743 else
14744 pdma_phys_bpl1 = 0;
14745 sgl_pg_pairs->sgl_pg1_addr_lo =
14746 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14747 sgl_pg_pairs->sgl_pg1_addr_hi =
14748 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14749 /* Keep the first xritag on the list */
14750 if (pg_pairs == 0)
14751 xritag_start = psb->cur_iocbq.sli4_xritag;
14752 sgl_pg_pairs++;
14753 pg_pairs++;
14754 }
14755 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14756 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14757 /* Perform endian conversion if necessary */
14758 sgl->word0 = cpu_to_le32(sgl->word0);
14759
14760 if (!phba->sli4_hba.intr_enable)
14761 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14762 else {
James Smarta183a152011-10-10 21:32:43 -040014763 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014764 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14765 }
14766 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14767 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14768 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14769 if (rc != MBX_TIMEOUT)
14770 lpfc_sli4_mbox_cmd_free(phba, mbox);
14771 if (shdr_status || shdr_add_status || rc) {
14772 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14773 "2564 POST_SGL_BLOCK mailbox command failed "
14774 "status x%x add_status x%x mbx status x%x\n",
14775 shdr_status, shdr_add_status, rc);
14776 rc = -ENXIO;
14777 }
14778 return rc;
14779}
14780
14781/**
14782 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14783 * @phba: pointer to lpfc_hba struct that the frame was received on
14784 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14785 *
14786 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14787 * valid type of frame that the LPFC driver will handle. This function will
14788 * return a zero if the frame is a valid frame or a non zero value when the
14789 * frame does not pass the check.
14790 **/
14791static int
14792lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14793{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014794 /* make rctl_names static to save stack space */
14795 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014796 char *type_names[] = FC_TYPE_NAMES_INIT;
14797 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014798 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014799
14800 switch (fc_hdr->fh_r_ctl) {
14801 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14802 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14803 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14804 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14805 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14806 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14807 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14808 case FC_RCTL_DD_CMD_STATUS: /* command status */
14809 case FC_RCTL_ELS_REQ: /* extended link services request */
14810 case FC_RCTL_ELS_REP: /* extended link services reply */
14811 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14812 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14813 case FC_RCTL_BA_NOP: /* basic link service NOP */
14814 case FC_RCTL_BA_ABTS: /* basic link service abort */
14815 case FC_RCTL_BA_RMC: /* remove connection */
14816 case FC_RCTL_BA_ACC: /* basic accept */
14817 case FC_RCTL_BA_RJT: /* basic reject */
14818 case FC_RCTL_BA_PRMT:
14819 case FC_RCTL_ACK_1: /* acknowledge_1 */
14820 case FC_RCTL_ACK_0: /* acknowledge_0 */
14821 case FC_RCTL_P_RJT: /* port reject */
14822 case FC_RCTL_F_RJT: /* fabric reject */
14823 case FC_RCTL_P_BSY: /* port busy */
14824 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14825 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14826 case FC_RCTL_LCR: /* link credit reset */
14827 case FC_RCTL_END: /* end */
14828 break;
14829 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14830 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14831 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14832 return lpfc_fc_frame_check(phba, fc_hdr);
14833 default:
14834 goto drop;
14835 }
14836 switch (fc_hdr->fh_type) {
14837 case FC_TYPE_BLS:
14838 case FC_TYPE_ELS:
14839 case FC_TYPE_FCP:
14840 case FC_TYPE_CT:
14841 break;
14842 case FC_TYPE_IP:
14843 case FC_TYPE_ILS:
14844 default:
14845 goto drop;
14846 }
James Smart546fc852011-03-11 16:06:29 -050014847
James Smart4f774512009-05-22 14:52:35 -040014848 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014849 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14850 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14851 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14852 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014853 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14854 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014855 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14856 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014857 return 0;
14858drop:
14859 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14860 "2539 Dropped frame rctl:%s type:%s\n",
14861 rctl_names[fc_hdr->fh_r_ctl],
14862 type_names[fc_hdr->fh_type]);
14863 return 1;
14864}
14865
14866/**
14867 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14868 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14869 *
14870 * This function processes the FC header to retrieve the VFI from the VF
14871 * header, if one exists. This function will return the VFI if one exists
14872 * or 0 if no VSAN Header exists.
14873 **/
14874static uint32_t
14875lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14876{
14877 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14878
14879 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14880 return 0;
14881 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14882}
14883
14884/**
14885 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14886 * @phba: Pointer to the HBA structure to search for the vport on
14887 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14888 * @fcfi: The FC Fabric ID that the frame came from
14889 *
14890 * This function searches the @phba for a vport that matches the content of the
14891 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14892 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14893 * returns the matching vport pointer or NULL if unable to match frame to a
14894 * vport.
14895 **/
14896static struct lpfc_vport *
14897lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14898 uint16_t fcfi)
14899{
14900 struct lpfc_vport **vports;
14901 struct lpfc_vport *vport = NULL;
14902 int i;
14903 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14904 fc_hdr->fh_d_id[1] << 8 |
14905 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014906
James Smartbf086112011-08-21 21:48:13 -040014907 if (did == Fabric_DID)
14908 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014909 if ((phba->pport->fc_flag & FC_PT2PT) &&
14910 !(phba->link_state == LPFC_HBA_READY))
14911 return phba->pport;
14912
James Smart4f774512009-05-22 14:52:35 -040014913 vports = lpfc_create_vport_work_array(phba);
14914 if (vports != NULL)
14915 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14916 if (phba->fcf.fcfi == fcfi &&
14917 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14918 vports[i]->fc_myDID == did) {
14919 vport = vports[i];
14920 break;
14921 }
14922 }
14923 lpfc_destroy_vport_work_array(phba, vports);
14924 return vport;
14925}
14926
14927/**
James Smart45ed1192009-10-02 15:17:02 -040014928 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14929 * @vport: The vport to work on.
14930 *
14931 * This function updates the receive sequence time stamp for this vport. The
14932 * receive sequence time stamp indicates the time that the last frame of the
14933 * the sequence that has been idle for the longest amount of time was received.
14934 * the driver uses this time stamp to indicate if any received sequences have
14935 * timed out.
14936 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014937static void
James Smart45ed1192009-10-02 15:17:02 -040014938lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14939{
14940 struct lpfc_dmabuf *h_buf;
14941 struct hbq_dmabuf *dmabuf = NULL;
14942
14943 /* get the oldest sequence on the rcv list */
14944 h_buf = list_get_first(&vport->rcv_buffer_list,
14945 struct lpfc_dmabuf, list);
14946 if (!h_buf)
14947 return;
14948 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14949 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14950}
14951
14952/**
14953 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14954 * @vport: The vport that the received sequences were sent to.
14955 *
14956 * This function cleans up all outstanding received sequences. This is called
14957 * by the driver when a link event or user action invalidates all the received
14958 * sequences.
14959 **/
14960void
14961lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14962{
14963 struct lpfc_dmabuf *h_buf, *hnext;
14964 struct lpfc_dmabuf *d_buf, *dnext;
14965 struct hbq_dmabuf *dmabuf = NULL;
14966
14967 /* start with the oldest sequence on the rcv list */
14968 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14969 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14970 list_del_init(&dmabuf->hbuf.list);
14971 list_for_each_entry_safe(d_buf, dnext,
14972 &dmabuf->dbuf.list, list) {
14973 list_del_init(&d_buf->list);
14974 lpfc_in_buf_free(vport->phba, d_buf);
14975 }
14976 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14977 }
14978}
14979
14980/**
14981 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14982 * @vport: The vport that the received sequences were sent to.
14983 *
14984 * This function determines whether any received sequences have timed out by
14985 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14986 * indicates that there is at least one timed out sequence this routine will
14987 * go through the received sequences one at a time from most inactive to most
14988 * active to determine which ones need to be cleaned up. Once it has determined
14989 * that a sequence needs to be cleaned up it will simply free up the resources
14990 * without sending an abort.
14991 **/
14992void
14993lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14994{
14995 struct lpfc_dmabuf *h_buf, *hnext;
14996 struct lpfc_dmabuf *d_buf, *dnext;
14997 struct hbq_dmabuf *dmabuf = NULL;
14998 unsigned long timeout;
14999 int abort_count = 0;
15000
15001 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15002 vport->rcv_buffer_time_stamp);
15003 if (list_empty(&vport->rcv_buffer_list) ||
15004 time_before(jiffies, timeout))
15005 return;
15006 /* start with the oldest sequence on the rcv list */
15007 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15008 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15009 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15010 dmabuf->time_stamp);
15011 if (time_before(jiffies, timeout))
15012 break;
15013 abort_count++;
15014 list_del_init(&dmabuf->hbuf.list);
15015 list_for_each_entry_safe(d_buf, dnext,
15016 &dmabuf->dbuf.list, list) {
15017 list_del_init(&d_buf->list);
15018 lpfc_in_buf_free(vport->phba, d_buf);
15019 }
15020 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15021 }
15022 if (abort_count)
15023 lpfc_update_rcv_time_stamp(vport);
15024}
15025
15026/**
James Smart4f774512009-05-22 14:52:35 -040015027 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15028 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15029 *
15030 * This function searches through the existing incomplete sequences that have
15031 * been sent to this @vport. If the frame matches one of the incomplete
15032 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15033 * make up that sequence. If no sequence is found that matches this frame then
15034 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15035 * This function returns a pointer to the first dmabuf in the sequence list that
15036 * the frame was linked to.
15037 **/
15038static struct hbq_dmabuf *
15039lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15040{
15041 struct fc_frame_header *new_hdr;
15042 struct fc_frame_header *temp_hdr;
15043 struct lpfc_dmabuf *d_buf;
15044 struct lpfc_dmabuf *h_buf;
15045 struct hbq_dmabuf *seq_dmabuf = NULL;
15046 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015047 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015048
James Smart4d9ab992009-10-02 15:16:39 -040015049 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015050 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015051 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015052
James Smart4f774512009-05-22 14:52:35 -040015053 /* Use the hdr_buf to find the sequence that this frame belongs to */
15054 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15055 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15056 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15057 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15058 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15059 continue;
15060 /* found a pending sequence that matches this frame */
15061 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15062 break;
15063 }
15064 if (!seq_dmabuf) {
15065 /*
15066 * This indicates first frame received for this sequence.
15067 * Queue the buffer on the vport's rcv_buffer_list.
15068 */
15069 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_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 }
15073 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015074 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15075 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015076 list_del_init(&seq_dmabuf->hbuf.list);
15077 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15078 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015079 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015080 return dmabuf;
15081 }
James Smart45ed1192009-10-02 15:17:02 -040015082 /* move this sequence to the tail to indicate a young sequence */
15083 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15084 seq_dmabuf->time_stamp = jiffies;
15085 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015086 if (list_empty(&seq_dmabuf->dbuf.list)) {
15087 temp_hdr = dmabuf->hbuf.virt;
15088 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15089 return seq_dmabuf;
15090 }
James Smart4f774512009-05-22 14:52:35 -040015091 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015092 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15093 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015094 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15095 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15096 /*
15097 * If the frame's sequence count is greater than the frame on
15098 * the list then insert the frame right after this frame
15099 */
James Smarteeead812009-12-21 17:01:23 -050015100 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15101 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015102 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015103 found = 1;
15104 break;
James Smart4f774512009-05-22 14:52:35 -040015105 }
James Smart4360ca92015-12-16 18:12:04 -050015106
15107 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15108 break;
15109 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015110 }
James Smart4360ca92015-12-16 18:12:04 -050015111
15112 if (found)
15113 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015114 return NULL;
15115}
15116
15117/**
James Smart6669f9b2009-10-02 15:16:45 -040015118 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15119 * @vport: pointer to a vitural port
15120 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15121 *
15122 * This function tries to abort from the partially assembed sequence, described
15123 * by the information from basic abbort @dmabuf. It checks to see whether such
15124 * partially assembled sequence held by the driver. If so, it shall free up all
15125 * the frames from the partially assembled sequence.
15126 *
15127 * Return
15128 * true -- if there is matching partially assembled sequence present and all
15129 * the frames freed with the sequence;
15130 * false -- if there is no matching partially assembled sequence present so
15131 * nothing got aborted in the lower layer driver
15132 **/
15133static bool
15134lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15135 struct hbq_dmabuf *dmabuf)
15136{
15137 struct fc_frame_header *new_hdr;
15138 struct fc_frame_header *temp_hdr;
15139 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15140 struct hbq_dmabuf *seq_dmabuf = NULL;
15141
15142 /* Use the hdr_buf to find the sequence that matches this frame */
15143 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15144 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15145 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15146 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15147 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15148 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15149 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15150 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15151 continue;
15152 /* found a pending sequence that matches this frame */
15153 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15154 break;
15155 }
15156
15157 /* Free up all the frames from the partially assembled sequence */
15158 if (seq_dmabuf) {
15159 list_for_each_entry_safe(d_buf, n_buf,
15160 &seq_dmabuf->dbuf.list, list) {
15161 list_del_init(&d_buf->list);
15162 lpfc_in_buf_free(vport->phba, d_buf);
15163 }
15164 return true;
15165 }
15166 return false;
15167}
15168
15169/**
James Smart6dd9e312013-01-03 15:43:37 -050015170 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15171 * @vport: pointer to a vitural port
15172 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15173 *
15174 * This function tries to abort from the assembed sequence from upper level
15175 * protocol, described by the information from basic abbort @dmabuf. It
15176 * checks to see whether such pending context exists at upper level protocol.
15177 * If so, it shall clean up the pending context.
15178 *
15179 * Return
15180 * true -- if there is matching pending context of the sequence cleaned
15181 * at ulp;
15182 * false -- if there is no matching pending context of the sequence present
15183 * at ulp.
15184 **/
15185static bool
15186lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15187{
15188 struct lpfc_hba *phba = vport->phba;
15189 int handled;
15190
15191 /* Accepting abort at ulp with SLI4 only */
15192 if (phba->sli_rev < LPFC_SLI_REV4)
15193 return false;
15194
15195 /* Register all caring upper level protocols to attend abort */
15196 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15197 if (handled)
15198 return true;
15199
15200 return false;
15201}
15202
15203/**
James Smart546fc852011-03-11 16:06:29 -050015204 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015205 * @phba: Pointer to HBA context object.
15206 * @cmd_iocbq: pointer to the command iocbq structure.
15207 * @rsp_iocbq: pointer to the response iocbq structure.
15208 *
James Smart546fc852011-03-11 16:06:29 -050015209 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015210 * event. It properly releases the memory allocated to the sequence abort
15211 * accept iocb.
15212 **/
15213static void
James Smart546fc852011-03-11 16:06:29 -050015214lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015215 struct lpfc_iocbq *cmd_iocbq,
15216 struct lpfc_iocbq *rsp_iocbq)
15217{
James Smart6dd9e312013-01-03 15:43:37 -050015218 struct lpfc_nodelist *ndlp;
15219
15220 if (cmd_iocbq) {
15221 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15222 lpfc_nlp_put(ndlp);
15223 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015224 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015225 }
James Smart6b5151f2012-01-18 16:24:06 -050015226
15227 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15228 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15229 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15230 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15231 rsp_iocbq->iocb.ulpStatus,
15232 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015233}
15234
15235/**
James Smart6d368e52011-05-24 11:44:12 -040015236 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15237 * @phba: Pointer to HBA context object.
15238 * @xri: xri id in transaction.
15239 *
15240 * This function validates the xri maps to the known range of XRIs allocated an
15241 * used by the driver.
15242 **/
James Smart7851fe22011-07-22 18:36:52 -040015243uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015244lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15245 uint16_t xri)
15246{
James Smarta2fc4aef2014-09-03 12:57:55 -040015247 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015248
15249 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15250 if (xri == phba->sli4_hba.xri_ids[i])
15251 return i;
15252 }
15253 return NO_XRI;
15254}
15255
James Smart6d368e52011-05-24 11:44:12 -040015256/**
James Smart546fc852011-03-11 16:06:29 -050015257 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015258 * @phba: Pointer to HBA context object.
15259 * @fc_hdr: pointer to a FC frame header.
15260 *
James Smart546fc852011-03-11 16:06:29 -050015261 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015262 * event after aborting the sequence handling.
15263 **/
15264static void
James Smart6dd9e312013-01-03 15:43:37 -050015265lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15266 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015267{
James Smart6dd9e312013-01-03 15:43:37 -050015268 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015269 struct lpfc_iocbq *ctiocb = NULL;
15270 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015271 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015272 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015273 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015274 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015275
15276 if (!lpfc_is_link_up(phba))
15277 return;
15278
15279 sid = sli4_sid_from_fc_hdr(fc_hdr);
15280 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015281 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015282
James Smart6dd9e312013-01-03 15:43:37 -050015283 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015284 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015285 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15286 if (!ndlp) {
15287 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15288 "1268 Failed to allocate ndlp for "
15289 "oxid:x%x SID:x%x\n", oxid, sid);
15290 return;
15291 }
15292 lpfc_nlp_init(vport, ndlp, sid);
15293 /* Put ndlp onto pport node list */
15294 lpfc_enqueue_node(vport, ndlp);
15295 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15296 /* re-setup ndlp without removing from node list */
15297 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15298 if (!ndlp) {
15299 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15300 "3275 Failed to active ndlp found "
15301 "for oxid:x%x SID:x%x\n", oxid, sid);
15302 return;
15303 }
James Smart6669f9b2009-10-02 15:16:45 -040015304 }
15305
James Smart546fc852011-03-11 16:06:29 -050015306 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015307 ctiocb = lpfc_sli_get_iocbq(phba);
15308 if (!ctiocb)
15309 return;
15310
James Smart5ffc2662009-11-18 15:39:44 -050015311 /* Extract the F_CTL field from FC_HDR */
15312 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15313
James Smart6669f9b2009-10-02 15:16:45 -040015314 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015315 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015316 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015317 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15318 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15319 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15320
15321 /* Fill in the rest of iocb fields */
15322 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15323 icmd->ulpBdeCount = 0;
15324 icmd->ulpLe = 1;
15325 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015326 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015327 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015328
James Smart6669f9b2009-10-02 15:16:45 -040015329 ctiocb->iocb_cmpl = NULL;
15330 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015331 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015332 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015333 ctiocb->sli4_xritag = NO_XRI;
15334
James Smartee0f4fe2012-05-09 21:19:14 -040015335 if (fctl & FC_FC_EX_CTX)
15336 /* Exchange responder sent the abort so we
15337 * own the oxid.
15338 */
15339 xri = oxid;
15340 else
15341 xri = rxid;
15342 lxri = lpfc_sli4_xri_inrange(phba, xri);
15343 if (lxri != NO_XRI)
15344 lpfc_set_rrq_active(phba, ndlp, lxri,
15345 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015346 /* For BA_ABTS from exchange responder, if the logical xri with
15347 * the oxid maps to the FCP XRI range, the port no longer has
15348 * that exchange context, send a BLS_RJT. Override the IOCB for
15349 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015350 */
James Smart6dd9e312013-01-03 15:43:37 -050015351 if ((fctl & FC_FC_EX_CTX) &&
15352 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15353 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15354 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15355 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15356 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15357 }
15358
15359 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15360 * the driver no longer has that exchange, send a BLS_RJT. Override
15361 * the IOCB for a BA_RJT.
15362 */
15363 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015364 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15365 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15366 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15367 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15368 }
James Smart6669f9b2009-10-02 15:16:45 -040015369
James Smart5ffc2662009-11-18 15:39:44 -050015370 if (fctl & FC_FC_EX_CTX) {
15371 /* ABTS sent by responder to CT exchange, construction
15372 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15373 * field and RX_ID from ABTS for RX_ID field.
15374 */
James Smart546fc852011-03-11 16:06:29 -050015375 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015376 } else {
15377 /* ABTS sent by initiator to CT exchange, construction
15378 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015379 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015380 */
James Smart546fc852011-03-11 16:06:29 -050015381 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015382 }
James Smartf09c3ac2012-03-01 22:33:29 -050015383 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015384 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015385
James Smart546fc852011-03-11 16:06:29 -050015386 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015387 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15388 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15389 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015390
15391 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15392 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015393 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15394 "2925 Failed to issue CT ABTS RSP x%x on "
15395 "xri x%x, Data x%x\n",
15396 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15397 phba->link_state);
15398 lpfc_nlp_put(ndlp);
15399 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015400 lpfc_sli_release_iocbq(phba, ctiocb);
15401 }
James Smart6669f9b2009-10-02 15:16:45 -040015402}
15403
15404/**
15405 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15406 * @vport: Pointer to the vport on which this sequence was received
15407 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15408 *
15409 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15410 * receive sequence is only partially assembed by the driver, it shall abort
15411 * the partially assembled frames for the sequence. Otherwise, if the
15412 * unsolicited receive sequence has been completely assembled and passed to
15413 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15414 * unsolicited sequence has been aborted. After that, it will issue a basic
15415 * accept to accept the abort.
15416 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015417static void
James Smart6669f9b2009-10-02 15:16:45 -040015418lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15419 struct hbq_dmabuf *dmabuf)
15420{
15421 struct lpfc_hba *phba = vport->phba;
15422 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015423 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015424 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015425
James Smart6669f9b2009-10-02 15:16:45 -040015426 /* Make a copy of fc_hdr before the dmabuf being released */
15427 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015428 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015429
James Smart5ffc2662009-11-18 15:39:44 -050015430 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015431 /* ABTS by responder to exchange, no cleanup needed */
15432 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015433 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015434 /* ABTS by initiator to exchange, need to do cleanup */
15435 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15436 if (aborted == false)
15437 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015438 }
James Smart6dd9e312013-01-03 15:43:37 -050015439 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15440
15441 /* Respond with BA_ACC or BA_RJT accordingly */
15442 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015443}
15444
15445/**
James Smart4f774512009-05-22 14:52:35 -040015446 * lpfc_seq_complete - Indicates if a sequence is complete
15447 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15448 *
15449 * This function checks the sequence, starting with the frame described by
15450 * @dmabuf, to see if all the frames associated with this sequence are present.
15451 * the frames associated with this sequence are linked to the @dmabuf using the
15452 * dbuf list. This function looks for two major things. 1) That the first frame
15453 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15454 * set. 3) That there are no holes in the sequence count. The function will
15455 * return 1 when the sequence is complete, otherwise it will return 0.
15456 **/
15457static int
15458lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15459{
15460 struct fc_frame_header *hdr;
15461 struct lpfc_dmabuf *d_buf;
15462 struct hbq_dmabuf *seq_dmabuf;
15463 uint32_t fctl;
15464 int seq_count = 0;
15465
15466 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15467 /* make sure first fame of sequence has a sequence count of zero */
15468 if (hdr->fh_seq_cnt != seq_count)
15469 return 0;
15470 fctl = (hdr->fh_f_ctl[0] << 16 |
15471 hdr->fh_f_ctl[1] << 8 |
15472 hdr->fh_f_ctl[2]);
15473 /* If last frame of sequence we can return success. */
15474 if (fctl & FC_FC_END_SEQ)
15475 return 1;
15476 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15477 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15478 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15479 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015480 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015481 return 0;
15482 fctl = (hdr->fh_f_ctl[0] << 16 |
15483 hdr->fh_f_ctl[1] << 8 |
15484 hdr->fh_f_ctl[2]);
15485 /* If last frame of sequence we can return success. */
15486 if (fctl & FC_FC_END_SEQ)
15487 return 1;
15488 }
15489 return 0;
15490}
15491
15492/**
15493 * lpfc_prep_seq - Prep sequence for ULP processing
15494 * @vport: Pointer to the vport on which this sequence was received
15495 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15496 *
15497 * This function takes a sequence, described by a list of frames, and creates
15498 * a list of iocbq structures to describe the sequence. This iocbq list will be
15499 * used to issue to the generic unsolicited sequence handler. This routine
15500 * returns a pointer to the first iocbq in the list. If the function is unable
15501 * to allocate an iocbq then it throw out the received frames that were not
15502 * able to be described and return a pointer to the first iocbq. If unable to
15503 * allocate any iocbqs (including the first) this function will return NULL.
15504 **/
15505static struct lpfc_iocbq *
15506lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15507{
James Smart7851fe22011-07-22 18:36:52 -040015508 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015509 struct lpfc_dmabuf *d_buf, *n_buf;
15510 struct lpfc_iocbq *first_iocbq, *iocbq;
15511 struct fc_frame_header *fc_hdr;
15512 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015513 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015514 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015515
15516 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15517 /* remove from receive buffer list */
15518 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015519 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015520 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015521 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015522 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015523 /* Get an iocbq struct to fill in. */
15524 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15525 if (first_iocbq) {
15526 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015527 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015528 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015529
15530 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15531 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15532 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15533 first_iocbq->iocb.un.rcvels.parmRo =
15534 sli4_did_from_fc_hdr(fc_hdr);
15535 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15536 } else
15537 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015538 first_iocbq->iocb.ulpContext = NO_XRI;
15539 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15540 be16_to_cpu(fc_hdr->fh_ox_id);
15541 /* iocbq is prepped for internal consumption. Physical vpi. */
15542 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15543 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015544 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015545 tot_len = bf_get(lpfc_rcqe_length,
15546 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15547
James Smart4f774512009-05-22 14:52:35 -040015548 first_iocbq->context2 = &seq_dmabuf->dbuf;
15549 first_iocbq->context3 = NULL;
15550 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015551 if (tot_len > LPFC_DATA_BUF_SIZE)
15552 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015553 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015554 else
15555 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15556
James Smart4f774512009-05-22 14:52:35 -040015557 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015558
James Smart7851fe22011-07-22 18:36:52 -040015559 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015560 }
15561 iocbq = first_iocbq;
15562 /*
15563 * Each IOCBq can have two Buffers assigned, so go through the list
15564 * of buffers for this sequence and save two buffers in each IOCBq
15565 */
15566 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15567 if (!iocbq) {
15568 lpfc_in_buf_free(vport->phba, d_buf);
15569 continue;
15570 }
15571 if (!iocbq->context3) {
15572 iocbq->context3 = d_buf;
15573 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015574 /* We need to get the size out of the right CQE */
15575 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15576 len = bf_get(lpfc_rcqe_length,
15577 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015578 pbde = (struct ulp_bde64 *)
15579 &iocbq->iocb.unsli3.sli3Words[4];
15580 if (len > LPFC_DATA_BUF_SIZE)
15581 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15582 else
15583 pbde->tus.f.bdeSize = len;
15584
James Smart7851fe22011-07-22 18:36:52 -040015585 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15586 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015587 } else {
15588 iocbq = lpfc_sli_get_iocbq(vport->phba);
15589 if (!iocbq) {
15590 if (first_iocbq) {
15591 first_iocbq->iocb.ulpStatus =
15592 IOSTAT_FCP_RSP_ERROR;
15593 first_iocbq->iocb.un.ulpWord[4] =
15594 IOERR_NO_RESOURCES;
15595 }
15596 lpfc_in_buf_free(vport->phba, d_buf);
15597 continue;
15598 }
James Smart7851fe22011-07-22 18:36:52 -040015599 /* We need to get the size out of the right CQE */
15600 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15601 len = bf_get(lpfc_rcqe_length,
15602 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015603 iocbq->context2 = d_buf;
15604 iocbq->context3 = NULL;
15605 iocbq->iocb.ulpBdeCount = 1;
15606 if (len > LPFC_DATA_BUF_SIZE)
15607 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15608 LPFC_DATA_BUF_SIZE;
15609 else
15610 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15611
James Smart7851fe22011-07-22 18:36:52 -040015612 tot_len += len;
15613 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15614
James Smart4f774512009-05-22 14:52:35 -040015615 iocbq->iocb.un.rcvels.remoteID = sid;
15616 list_add_tail(&iocbq->list, &first_iocbq->list);
15617 }
15618 }
15619 return first_iocbq;
15620}
15621
James Smart6669f9b2009-10-02 15:16:45 -040015622static void
15623lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15624 struct hbq_dmabuf *seq_dmabuf)
15625{
15626 struct fc_frame_header *fc_hdr;
15627 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15628 struct lpfc_hba *phba = vport->phba;
15629
15630 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15631 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15632 if (!iocbq) {
15633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15634 "2707 Ring %d handler: Failed to allocate "
15635 "iocb Rctl x%x Type x%x received\n",
15636 LPFC_ELS_RING,
15637 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15638 return;
15639 }
15640 if (!lpfc_complete_unsol_iocb(phba,
15641 &phba->sli.ring[LPFC_ELS_RING],
15642 iocbq, fc_hdr->fh_r_ctl,
15643 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015644 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015645 "2540 Ring %d handler: unexpected Rctl "
15646 "x%x Type x%x received\n",
15647 LPFC_ELS_RING,
15648 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15649
15650 /* Free iocb created in lpfc_prep_seq */
15651 list_for_each_entry_safe(curr_iocb, next_iocb,
15652 &iocbq->list, list) {
15653 list_del_init(&curr_iocb->list);
15654 lpfc_sli_release_iocbq(phba, curr_iocb);
15655 }
15656 lpfc_sli_release_iocbq(phba, iocbq);
15657}
15658
James Smart4f774512009-05-22 14:52:35 -040015659/**
15660 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15661 * @phba: Pointer to HBA context object.
15662 *
15663 * This function is called with no lock held. This function processes all
15664 * the received buffers and gives it to upper layers when a received buffer
15665 * indicates that it is the final frame in the sequence. The interrupt
15666 * service routine processes received buffers at interrupt contexts and adds
15667 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15668 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15669 * appropriate receive function when the final frame in a sequence is received.
15670 **/
James Smart4d9ab992009-10-02 15:16:39 -040015671void
15672lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15673 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015674{
James Smart4d9ab992009-10-02 15:16:39 -040015675 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015676 struct fc_frame_header *fc_hdr;
15677 struct lpfc_vport *vport;
15678 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015679 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015680
James Smart4f774512009-05-22 14:52:35 -040015681 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015682 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15683 /* check to see if this a valid type of frame */
15684 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15685 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15686 return;
15687 }
James Smart7851fe22011-07-22 18:36:52 -040015688 if ((bf_get(lpfc_cqe_code,
15689 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15690 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15691 &dmabuf->cq_event.cqe.rcqe_cmpl);
15692 else
15693 fcfi = bf_get(lpfc_rcqe_fcf_id,
15694 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015695
James Smart4d9ab992009-10-02 15:16:39 -040015696 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015697 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015698 /* throw out the frame */
15699 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15700 return;
15701 }
James Smart939723a2012-05-09 21:19:03 -040015702
15703 /* d_id this frame is directed to */
15704 did = sli4_did_from_fc_hdr(fc_hdr);
15705
15706 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15707 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15708 (did != Fabric_DID)) {
15709 /*
15710 * Throw out the frame if we are not pt2pt.
15711 * The pt2pt protocol allows for discovery frames
15712 * to be received without a registered VPI.
15713 */
15714 if (!(vport->fc_flag & FC_PT2PT) ||
15715 (phba->link_state == LPFC_HBA_READY)) {
15716 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15717 return;
15718 }
15719 }
15720
James Smart6669f9b2009-10-02 15:16:45 -040015721 /* Handle the basic abort sequence (BA_ABTS) event */
15722 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15723 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15724 return;
15725 }
15726
James Smart4d9ab992009-10-02 15:16:39 -040015727 /* Link this frame */
15728 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15729 if (!seq_dmabuf) {
15730 /* unable to add frame to vport - throw it out */
15731 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15732 return;
15733 }
15734 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015735 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015736 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015737
James Smart6669f9b2009-10-02 15:16:45 -040015738 /* Send the complete sequence to the upper layer protocol */
15739 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015740}
James Smart6fb120a2009-05-22 14:52:59 -040015741
15742/**
15743 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15744 * @phba: pointer to lpfc hba data structure.
15745 *
15746 * This routine is invoked to post rpi header templates to the
15747 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015748 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15749 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015750 *
15751 * This routine does not require any locks. It's usage is expected
15752 * to be driver load or reset recovery when the driver is
15753 * sequential.
15754 *
15755 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015756 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015757 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015758 * When this error occurs, the driver is not guaranteed
15759 * to have any rpi regions posted to the device and
15760 * must either attempt to repost the regions or take a
15761 * fatal error.
15762 **/
15763int
15764lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15765{
15766 struct lpfc_rpi_hdr *rpi_page;
15767 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015768 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015769
James Smart6d368e52011-05-24 11:44:12 -040015770 /* SLI4 ports that support extents do not require RPI headers. */
15771 if (!phba->sli4_hba.rpi_hdrs_in_use)
15772 goto exit;
15773 if (phba->sli4_hba.extents_in_use)
15774 return -EIO;
15775
James Smart6fb120a2009-05-22 14:52:59 -040015776 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015777 /*
15778 * Assign the rpi headers a physical rpi only if the driver
15779 * has not initialized those resources. A port reset only
15780 * needs the headers posted.
15781 */
15782 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15783 LPFC_RPI_RSRC_RDY)
15784 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15785
James Smart6fb120a2009-05-22 14:52:59 -040015786 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15787 if (rc != MBX_SUCCESS) {
15788 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15789 "2008 Error %d posting all rpi "
15790 "headers\n", rc);
15791 rc = -EIO;
15792 break;
15793 }
15794 }
15795
James Smart6d368e52011-05-24 11:44:12 -040015796 exit:
15797 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15798 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015799 return rc;
15800}
15801
15802/**
15803 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15804 * @phba: pointer to lpfc hba data structure.
15805 * @rpi_page: pointer to the rpi memory region.
15806 *
15807 * This routine is invoked to post a single rpi header to the
15808 * HBA consistent with the SLI-4 interface spec. This memory region
15809 * maps up to 64 rpi context regions.
15810 *
15811 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015812 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015813 * -ENOMEM - No available memory
15814 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015815 **/
15816int
15817lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15818{
15819 LPFC_MBOXQ_t *mboxq;
15820 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15821 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015822 uint32_t shdr_status, shdr_add_status;
15823 union lpfc_sli4_cfg_shdr *shdr;
15824
James Smart6d368e52011-05-24 11:44:12 -040015825 /* SLI4 ports that support extents do not require RPI headers. */
15826 if (!phba->sli4_hba.rpi_hdrs_in_use)
15827 return rc;
15828 if (phba->sli4_hba.extents_in_use)
15829 return -EIO;
15830
James Smart6fb120a2009-05-22 14:52:59 -040015831 /* The port is notified of the header region via a mailbox command. */
15832 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15833 if (!mboxq) {
15834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15835 "2001 Unable to allocate memory for issuing "
15836 "SLI_CONFIG_SPECIAL mailbox command\n");
15837 return -ENOMEM;
15838 }
15839
15840 /* Post all rpi memory regions to the port. */
15841 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015842 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15843 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15844 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015845 sizeof(struct lpfc_sli4_cfg_mhdr),
15846 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015847
15848
15849 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015850 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15851 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015852 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15853 hdr_tmpl, rpi_page->page_count);
15854
James Smart6fb120a2009-05-22 14:52:59 -040015855 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15856 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015857 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015858 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15859 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15860 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15861 if (rc != MBX_TIMEOUT)
15862 mempool_free(mboxq, phba->mbox_mem_pool);
15863 if (shdr_status || shdr_add_status || rc) {
15864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15865 "2514 POST_RPI_HDR mailbox failed with "
15866 "status x%x add_status x%x, mbx status x%x\n",
15867 shdr_status, shdr_add_status, rc);
15868 rc = -ENXIO;
15869 }
15870 return rc;
15871}
15872
15873/**
15874 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15875 * @phba: pointer to lpfc hba data structure.
15876 *
15877 * This routine is invoked to post rpi header templates to the
15878 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015879 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15880 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015881 *
15882 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015883 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015884 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15885 **/
15886int
15887lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15888{
James Smart6d368e52011-05-24 11:44:12 -040015889 unsigned long rpi;
15890 uint16_t max_rpi, rpi_limit;
15891 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015892 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015893 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015894
James Smart6fb120a2009-05-22 14:52:59 -040015895 /*
James Smart6d368e52011-05-24 11:44:12 -040015896 * Fetch the next logical rpi. Because this index is logical,
15897 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015898 */
James Smart4902b382013-10-10 12:20:35 -040015899 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015900 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15901 rpi_limit = phba->sli4_hba.next_rpi;
15902
James Smart6d368e52011-05-24 11:44:12 -040015903 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15904 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015905 rpi = LPFC_RPI_ALLOC_ERROR;
15906 else {
15907 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15908 phba->sli4_hba.max_cfg_param.rpi_used++;
15909 phba->sli4_hba.rpi_count++;
15910 }
James Smartbe6bb942015-04-07 15:07:22 -040015911 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15912 "0001 rpi:%x max:%x lim:%x\n",
15913 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015914
15915 /*
15916 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015917 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015918 */
15919 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15920 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015921 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015922 return rpi;
15923 }
15924
15925 /*
James Smart6d368e52011-05-24 11:44:12 -040015926 * RPI header postings are not required for SLI4 ports capable of
15927 * extents.
15928 */
15929 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015930 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015931 return rpi;
15932 }
15933
15934 /*
James Smart6fb120a2009-05-22 14:52:59 -040015935 * If the driver is running low on rpi resources, allocate another
15936 * page now. Note that the next_rpi value is used because
15937 * it represents how many are actually in use whereas max_rpi notes
15938 * how many are supported max by the device.
15939 */
James Smart6d368e52011-05-24 11:44:12 -040015940 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015941 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015942 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15943 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15944 if (!rpi_hdr) {
15945 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15946 "2002 Error Could not grow rpi "
15947 "count\n");
15948 } else {
James Smart6d368e52011-05-24 11:44:12 -040015949 lrpi = rpi_hdr->start_rpi;
15950 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015951 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15952 }
15953 }
15954
15955 return rpi;
15956}
15957
15958/**
15959 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15960 * @phba: pointer to lpfc hba data structure.
15961 *
15962 * This routine is invoked to release an rpi to the pool of
15963 * available rpis maintained by the driver.
15964 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015965static void
James Smartd7c47992010-06-08 18:31:54 -040015966__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15967{
15968 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15969 phba->sli4_hba.rpi_count--;
15970 phba->sli4_hba.max_cfg_param.rpi_used--;
15971 }
15972}
15973
15974/**
15975 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15976 * @phba: pointer to lpfc hba data structure.
15977 *
15978 * This routine is invoked to release an rpi to the pool of
15979 * available rpis maintained by the driver.
15980 **/
15981void
James Smart6fb120a2009-05-22 14:52:59 -040015982lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15983{
15984 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015985 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015986 spin_unlock_irq(&phba->hbalock);
15987}
15988
15989/**
15990 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15991 * @phba: pointer to lpfc hba data structure.
15992 *
15993 * This routine is invoked to remove the memory region that
15994 * provided rpi via a bitmask.
15995 **/
15996void
15997lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15998{
15999 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040016000 kfree(phba->sli4_hba.rpi_ids);
16001 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040016002}
16003
16004/**
16005 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16006 * @phba: pointer to lpfc hba data structure.
16007 *
16008 * This routine is invoked to remove the memory region that
16009 * provided rpi via a bitmask.
16010 **/
16011int
James Smart6b5151f2012-01-18 16:24:06 -050016012lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16013 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016014{
16015 LPFC_MBOXQ_t *mboxq;
16016 struct lpfc_hba *phba = ndlp->phba;
16017 int rc;
16018
16019 /* The port is notified of the header region via a mailbox command. */
16020 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16021 if (!mboxq)
16022 return -ENOMEM;
16023
16024 /* Post all rpi memory regions to the port. */
16025 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016026 if (cmpl) {
16027 mboxq->mbox_cmpl = cmpl;
16028 mboxq->context1 = arg;
16029 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016030 } else
16031 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016032 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016033 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16034 if (rc == MBX_NOT_FINISHED) {
16035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16036 "2010 Resume RPI Mailbox failed "
16037 "status %d, mbxStatus x%x\n", rc,
16038 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16039 mempool_free(mboxq, phba->mbox_mem_pool);
16040 return -EIO;
16041 }
16042 return 0;
16043}
16044
16045/**
16046 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016047 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016048 *
James Smart76a95d72010-11-20 23:11:48 -050016049 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016050 *
16051 * Returns:
16052 * 0 success
16053 * -Evalue otherwise
16054 **/
16055int
James Smart76a95d72010-11-20 23:11:48 -050016056lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016057{
16058 LPFC_MBOXQ_t *mboxq;
16059 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016060 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016061 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016062 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016063 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16064 if (!mboxq)
16065 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016066 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016067 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016068 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016069 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016070 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016071 "2022 INIT VPI Mailbox failed "
16072 "status %d, mbxStatus x%x\n", rc,
16073 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016074 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016075 }
James Smart6a9c52c2009-10-02 15:16:51 -040016076 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016077 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016078
16079 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016080}
16081
16082/**
16083 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16084 * @phba: pointer to lpfc hba data structure.
16085 * @mboxq: Pointer to mailbox object.
16086 *
16087 * This routine is invoked to manually add a single FCF record. The caller
16088 * must pass a completely initialized FCF_Record. This routine takes
16089 * care of the nonembedded mailbox operations.
16090 **/
16091static void
16092lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16093{
16094 void *virt_addr;
16095 union lpfc_sli4_cfg_shdr *shdr;
16096 uint32_t shdr_status, shdr_add_status;
16097
16098 virt_addr = mboxq->sge_array->addr[0];
16099 /* The IOCTL status is embedded in the mailbox subheader. */
16100 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16101 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16102 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16103
16104 if ((shdr_status || shdr_add_status) &&
16105 (shdr_status != STATUS_FCF_IN_USE))
16106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16107 "2558 ADD_FCF_RECORD mailbox failed with "
16108 "status x%x add_status x%x\n",
16109 shdr_status, shdr_add_status);
16110
16111 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16112}
16113
16114/**
16115 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16116 * @phba: pointer to lpfc hba data structure.
16117 * @fcf_record: pointer to the initialized fcf record to add.
16118 *
16119 * This routine is invoked to manually add a single FCF record. The caller
16120 * must pass a completely initialized FCF_Record. This routine takes
16121 * care of the nonembedded mailbox operations.
16122 **/
16123int
16124lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16125{
16126 int rc = 0;
16127 LPFC_MBOXQ_t *mboxq;
16128 uint8_t *bytep;
16129 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016130 struct lpfc_mbx_sge sge;
16131 uint32_t alloc_len, req_len;
16132 uint32_t fcfindex;
16133
16134 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16135 if (!mboxq) {
16136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16137 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16138 return -ENOMEM;
16139 }
16140
16141 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16142 sizeof(uint32_t);
16143
16144 /* Allocate DMA memory and set up the non-embedded mailbox command */
16145 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16146 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16147 req_len, LPFC_SLI4_MBX_NEMBED);
16148 if (alloc_len < req_len) {
16149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16150 "2523 Allocated DMA memory size (x%x) is "
16151 "less than the requested DMA memory "
16152 "size (x%x)\n", alloc_len, req_len);
16153 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16154 return -ENOMEM;
16155 }
16156
16157 /*
16158 * Get the first SGE entry from the non-embedded DMA memory. This
16159 * routine only uses a single SGE.
16160 */
16161 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016162 virt_addr = mboxq->sge_array->addr[0];
16163 /*
16164 * Configure the FCF record for FCFI 0. This is the driver's
16165 * hardcoded default and gets used in nonFIP mode.
16166 */
16167 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16168 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16169 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16170
16171 /*
16172 * Copy the fcf_index and the FCF Record Data. The data starts after
16173 * the FCoE header plus word10. The data copy needs to be endian
16174 * correct.
16175 */
16176 bytep += sizeof(uint32_t);
16177 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16178 mboxq->vport = phba->pport;
16179 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16180 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16181 if (rc == MBX_NOT_FINISHED) {
16182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16183 "2515 ADD_FCF_RECORD mailbox failed with "
16184 "status 0x%x\n", rc);
16185 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16186 rc = -EIO;
16187 } else
16188 rc = 0;
16189
16190 return rc;
16191}
16192
16193/**
16194 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16195 * @phba: pointer to lpfc hba data structure.
16196 * @fcf_record: pointer to the fcf record to write the default data.
16197 * @fcf_index: FCF table entry index.
16198 *
16199 * This routine is invoked to build the driver's default FCF record. The
16200 * values used are hardcoded. This routine handles memory initialization.
16201 *
16202 **/
16203void
16204lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16205 struct fcf_record *fcf_record,
16206 uint16_t fcf_index)
16207{
16208 memset(fcf_record, 0, sizeof(struct fcf_record));
16209 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16210 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16211 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16212 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16213 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16214 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16215 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16216 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16217 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16218 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16219 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16220 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16221 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016222 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016223 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16224 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16225 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16226 /* Set the VLAN bit map */
16227 if (phba->valid_vlan) {
16228 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16229 = 1 << (phba->vlan_id % 8);
16230 }
16231}
16232
16233/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016234 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016235 * @phba: pointer to lpfc hba data structure.
16236 * @fcf_index: FCF table entry offset.
16237 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016238 * This routine is invoked to scan the entire FCF table by reading FCF
16239 * record and processing it one at a time starting from the @fcf_index
16240 * for initial FCF discovery or fast FCF failover rediscovery.
16241 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016242 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016243 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016244 **/
16245int
James Smart0c9ab6f2010-02-26 14:15:57 -050016246lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016247{
16248 int rc = 0, error;
16249 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016250
James Smart32b97932009-07-19 10:01:21 -040016251 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016252 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016253 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16254 if (!mboxq) {
16255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16256 "2000 Failed to allocate mbox for "
16257 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016258 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016259 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016260 }
James Smartecfd03c2010-02-12 14:41:27 -050016261 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016262 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016263 if (rc) {
16264 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016265 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016266 }
James Smartecfd03c2010-02-12 14:41:27 -050016267 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016268 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016269 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016270
16271 spin_lock_irq(&phba->hbalock);
16272 phba->hba_flag |= FCF_TS_INPROG;
16273 spin_unlock_irq(&phba->hbalock);
16274
James Smart6fb120a2009-05-22 14:52:59 -040016275 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016276 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016277 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016278 else {
James Smart38b92ef2010-08-04 16:11:39 -040016279 /* Reset eligible FCF count for new scan */
16280 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016281 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016282 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016283 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016284fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016285 if (error) {
16286 if (mboxq)
16287 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016288 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016289 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016290 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016291 spin_unlock_irq(&phba->hbalock);
16292 }
James Smart6fb120a2009-05-22 14:52:59 -040016293 return error;
16294}
James Smarta0c87cb2009-07-19 10:01:10 -040016295
16296/**
James Smarta93ff372010-10-22 11:06:08 -040016297 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016298 * @phba: pointer to lpfc hba data structure.
16299 * @fcf_index: FCF table entry offset.
16300 *
16301 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016302 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016303 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016304 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016305 * otherwise.
16306 **/
16307int
16308lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16309{
16310 int rc = 0, error;
16311 LPFC_MBOXQ_t *mboxq;
16312
16313 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16314 if (!mboxq) {
16315 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16316 "2763 Failed to allocate mbox for "
16317 "READ_FCF cmd\n");
16318 error = -ENOMEM;
16319 goto fail_fcf_read;
16320 }
16321 /* Construct the read FCF record mailbox command */
16322 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16323 if (rc) {
16324 error = -EINVAL;
16325 goto fail_fcf_read;
16326 }
16327 /* Issue the mailbox command asynchronously */
16328 mboxq->vport = phba->pport;
16329 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16330 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16331 if (rc == MBX_NOT_FINISHED)
16332 error = -EIO;
16333 else
16334 error = 0;
16335
16336fail_fcf_read:
16337 if (error && mboxq)
16338 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16339 return error;
16340}
16341
16342/**
16343 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16344 * @phba: pointer to lpfc hba data structure.
16345 * @fcf_index: FCF table entry offset.
16346 *
16347 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016348 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016349 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016350 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016351 * otherwise.
16352 **/
16353int
16354lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16355{
16356 int rc = 0, error;
16357 LPFC_MBOXQ_t *mboxq;
16358
16359 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16360 if (!mboxq) {
16361 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16362 "2758 Failed to allocate mbox for "
16363 "READ_FCF cmd\n");
16364 error = -ENOMEM;
16365 goto fail_fcf_read;
16366 }
16367 /* Construct the read FCF record mailbox command */
16368 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16369 if (rc) {
16370 error = -EINVAL;
16371 goto fail_fcf_read;
16372 }
16373 /* Issue the mailbox command asynchronously */
16374 mboxq->vport = phba->pport;
16375 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16376 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16377 if (rc == MBX_NOT_FINISHED)
16378 error = -EIO;
16379 else
16380 error = 0;
16381
16382fail_fcf_read:
16383 if (error && mboxq)
16384 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16385 return error;
16386}
16387
16388/**
James Smartf5cb5302015-12-16 18:11:52 -050016389 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016390 * phba pointer to the lpfc_hba struct for this port.
16391 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16392 * routine when the rr_bmask is empty. The FCF indecies are put into the
16393 * rr_bmask based on their priority level. Starting from the highest priority
16394 * to the lowest. The most likely FCF candidate will be in the highest
16395 * priority group. When this routine is called it searches the fcf_pri list for
16396 * next lowest priority group and repopulates the rr_bmask with only those
16397 * fcf_indexes.
16398 * returns:
16399 * 1=success 0=failure
16400 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016401static int
James Smart7d791df2011-07-22 18:37:52 -040016402lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16403{
16404 uint16_t next_fcf_pri;
16405 uint16_t last_index;
16406 struct lpfc_fcf_pri *fcf_pri;
16407 int rc;
16408 int ret = 0;
16409
16410 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16411 LPFC_SLI4_FCF_TBL_INDX_MAX);
16412 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16413 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016414
16415 /* Verify the priority list has 2 or more entries */
16416 spin_lock_irq(&phba->hbalock);
16417 if (list_empty(&phba->fcf.fcf_pri_list) ||
16418 list_is_singular(&phba->fcf.fcf_pri_list)) {
16419 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016420 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16421 "3061 Last IDX %d\n", last_index);
16422 return 0; /* Empty rr list */
16423 }
James Smart25626692013-03-01 16:36:54 -050016424 spin_unlock_irq(&phba->hbalock);
16425
James Smart7d791df2011-07-22 18:37:52 -040016426 next_fcf_pri = 0;
16427 /*
16428 * Clear the rr_bmask and set all of the bits that are at this
16429 * priority.
16430 */
16431 memset(phba->fcf.fcf_rr_bmask, 0,
16432 sizeof(*phba->fcf.fcf_rr_bmask));
16433 spin_lock_irq(&phba->hbalock);
16434 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16435 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16436 continue;
16437 /*
16438 * the 1st priority that has not FLOGI failed
16439 * will be the highest.
16440 */
16441 if (!next_fcf_pri)
16442 next_fcf_pri = fcf_pri->fcf_rec.priority;
16443 spin_unlock_irq(&phba->hbalock);
16444 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16445 rc = lpfc_sli4_fcf_rr_index_set(phba,
16446 fcf_pri->fcf_rec.fcf_index);
16447 if (rc)
16448 return 0;
16449 }
16450 spin_lock_irq(&phba->hbalock);
16451 }
16452 /*
16453 * if next_fcf_pri was not set above and the list is not empty then
16454 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016455 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016456 */
16457 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16458 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16459 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16460 /*
16461 * the 1st priority that has not FLOGI failed
16462 * will be the highest.
16463 */
16464 if (!next_fcf_pri)
16465 next_fcf_pri = fcf_pri->fcf_rec.priority;
16466 spin_unlock_irq(&phba->hbalock);
16467 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16468 rc = lpfc_sli4_fcf_rr_index_set(phba,
16469 fcf_pri->fcf_rec.fcf_index);
16470 if (rc)
16471 return 0;
16472 }
16473 spin_lock_irq(&phba->hbalock);
16474 }
16475 } else
16476 ret = 1;
16477 spin_unlock_irq(&phba->hbalock);
16478
16479 return ret;
16480}
16481/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016482 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16483 * @phba: pointer to lpfc hba data structure.
16484 *
16485 * This routine is to get the next eligible FCF record index in a round
16486 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016487 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016488 * shall be returned, otherwise, the next eligible FCF record's index
16489 * shall be returned.
16490 **/
16491uint16_t
16492lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16493{
16494 uint16_t next_fcf_index;
16495
James Smart421c6622013-01-03 15:44:16 -050016496initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016497 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016498 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16499
James Smart7d791df2011-07-22 18:37:52 -040016500next_priority:
James Smart421c6622013-01-03 15:44:16 -050016501 /* Determine the next fcf index to check */
16502 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016503 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16504 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016505 next_fcf_index);
16506
James Smart0c9ab6f2010-02-26 14:15:57 -050016507 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016508 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16509 /*
16510 * If we have wrapped then we need to clear the bits that
16511 * have been tested so that we can detect when we should
16512 * change the priority level.
16513 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016514 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16515 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016516 }
16517
James Smart0c9ab6f2010-02-26 14:15:57 -050016518
James Smart3804dc82010-07-14 15:31:37 -040016519 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016520 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16521 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16522 /*
16523 * If next fcf index is not found check if there are lower
16524 * Priority level fcf's in the fcf_priority list.
16525 * Set up the rr_bmask with all of the avaiable fcf bits
16526 * at that level and continue the selection process.
16527 */
16528 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016529 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016530 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16531 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016532 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16533 return LPFC_FCOE_FCF_NEXT_NONE;
16534 else {
16535 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16536 "3063 Only FCF available idx %d, flag %x\n",
16537 next_fcf_index,
16538 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16539 return next_fcf_index;
16540 }
James Smart3804dc82010-07-14 15:31:37 -040016541 }
16542
James Smart7d791df2011-07-22 18:37:52 -040016543 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16544 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016545 LPFC_FCF_FLOGI_FAILED) {
16546 if (list_is_singular(&phba->fcf.fcf_pri_list))
16547 return LPFC_FCOE_FCF_NEXT_NONE;
16548
James Smart7d791df2011-07-22 18:37:52 -040016549 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016550 }
James Smart7d791df2011-07-22 18:37:52 -040016551
James Smart3804dc82010-07-14 15:31:37 -040016552 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016553 "2845 Get next roundrobin failover FCF (x%x)\n",
16554 next_fcf_index);
16555
James Smart0c9ab6f2010-02-26 14:15:57 -050016556 return next_fcf_index;
16557}
16558
16559/**
16560 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16561 * @phba: pointer to lpfc hba data structure.
16562 *
16563 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016564 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016565 * does not go beyond the range of the driver allocated bmask dimension
16566 * before setting the bit.
16567 *
16568 * Returns 0 if the index bit successfully set, otherwise, it returns
16569 * -EINVAL.
16570 **/
16571int
16572lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16573{
16574 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16575 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016576 "2610 FCF (x%x) reached driver's book "
16577 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016578 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16579 return -EINVAL;
16580 }
16581 /* Set the eligible FCF record index bmask */
16582 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16583
James Smart3804dc82010-07-14 15:31:37 -040016584 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016585 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016586 "bmask\n", fcf_index);
16587
James Smart0c9ab6f2010-02-26 14:15:57 -050016588 return 0;
16589}
16590
16591/**
James Smart3804dc82010-07-14 15:31:37 -040016592 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016593 * @phba: pointer to lpfc hba data structure.
16594 *
16595 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016596 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016597 * does not go beyond the range of the driver allocated bmask dimension
16598 * before clearing the bit.
16599 **/
16600void
16601lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16602{
James Smart9a803a72013-09-06 12:17:56 -040016603 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016604 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16605 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016606 "2762 FCF (x%x) reached driver's book "
16607 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016608 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16609 return;
16610 }
16611 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016612 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016613 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16614 list) {
James Smart7d791df2011-07-22 18:37:52 -040016615 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16616 list_del_init(&fcf_pri->list);
16617 break;
16618 }
16619 }
16620 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016621 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016622
16623 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016624 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016625 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016626}
16627
16628/**
James Smartecfd03c2010-02-12 14:41:27 -050016629 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16630 * @phba: pointer to lpfc hba data structure.
16631 *
16632 * This routine is the completion routine for the rediscover FCF table mailbox
16633 * command. If the mailbox command returned failure, it will try to stop the
16634 * FCF rediscover wait timer.
16635 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016636static void
James Smartecfd03c2010-02-12 14:41:27 -050016637lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16638{
16639 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16640 uint32_t shdr_status, shdr_add_status;
16641
16642 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16643
16644 shdr_status = bf_get(lpfc_mbox_hdr_status,
16645 &redisc_fcf->header.cfg_shdr.response);
16646 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16647 &redisc_fcf->header.cfg_shdr.response);
16648 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016649 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016650 "2746 Requesting for FCF rediscovery failed "
16651 "status x%x add_status x%x\n",
16652 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016653 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016654 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016655 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016656 spin_unlock_irq(&phba->hbalock);
16657 /*
16658 * CVL event triggered FCF rediscover request failed,
16659 * last resort to re-try current registered FCF entry.
16660 */
16661 lpfc_retry_pport_discovery(phba);
16662 } else {
16663 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016664 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016665 spin_unlock_irq(&phba->hbalock);
16666 /*
16667 * DEAD FCF event triggered FCF rediscover request
16668 * failed, last resort to fail over as a link down
16669 * to FCF registration.
16670 */
16671 lpfc_sli4_fcf_dead_failthrough(phba);
16672 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016673 } else {
16674 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016675 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016676 /*
16677 * Start FCF rediscovery wait timer for pending FCF
16678 * before rescan FCF record table.
16679 */
16680 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016681 }
James Smartecfd03c2010-02-12 14:41:27 -050016682
16683 mempool_free(mbox, phba->mbox_mem_pool);
16684}
16685
16686/**
James Smart3804dc82010-07-14 15:31:37 -040016687 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016688 * @phba: pointer to lpfc hba data structure.
16689 *
16690 * This routine is invoked to request for rediscovery of the entire FCF table
16691 * by the port.
16692 **/
16693int
16694lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16695{
16696 LPFC_MBOXQ_t *mbox;
16697 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16698 int rc, length;
16699
James Smart0c9ab6f2010-02-26 14:15:57 -050016700 /* Cancel retry delay timers to all vports before FCF rediscover */
16701 lpfc_cancel_all_vport_retry_delay_timer(phba);
16702
James Smartecfd03c2010-02-12 14:41:27 -050016703 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16704 if (!mbox) {
16705 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16706 "2745 Failed to allocate mbox for "
16707 "requesting FCF rediscover.\n");
16708 return -ENOMEM;
16709 }
16710
16711 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16712 sizeof(struct lpfc_sli4_cfg_mhdr));
16713 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16714 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16715 length, LPFC_SLI4_MBX_EMBED);
16716
16717 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16718 /* Set count to 0 for invalidating the entire FCF database */
16719 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16720
16721 /* Issue the mailbox command asynchronously */
16722 mbox->vport = phba->pport;
16723 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16724 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16725
16726 if (rc == MBX_NOT_FINISHED) {
16727 mempool_free(mbox, phba->mbox_mem_pool);
16728 return -EIO;
16729 }
16730 return 0;
16731}
16732
16733/**
James Smartfc2b9892010-02-26 14:15:29 -050016734 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16735 * @phba: pointer to lpfc hba data structure.
16736 *
16737 * This function is the failover routine as a last resort to the FCF DEAD
16738 * event when driver failed to perform fast FCF failover.
16739 **/
16740void
16741lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16742{
16743 uint32_t link_state;
16744
16745 /*
16746 * Last resort as FCF DEAD event failover will treat this as
16747 * a link down, but save the link state because we don't want
16748 * it to be changed to Link Down unless it is already down.
16749 */
16750 link_state = phba->link_state;
16751 lpfc_linkdown(phba);
16752 phba->link_state = link_state;
16753
16754 /* Unregister FCF if no devices connected to it */
16755 lpfc_unregister_unused_fcf(phba);
16756}
16757
16758/**
James Smart026abb82011-12-13 13:20:45 -050016759 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016760 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016761 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016762 *
James Smart026abb82011-12-13 13:20:45 -050016763 * This function gets SLI3 port configure region 23 data through memory dump
16764 * mailbox command. When it successfully retrieves data, the size of the data
16765 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016766 **/
James Smart026abb82011-12-13 13:20:45 -050016767static uint32_t
16768lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016769{
16770 LPFC_MBOXQ_t *pmb = NULL;
16771 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016772 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016773 int rc;
16774
James Smart026abb82011-12-13 13:20:45 -050016775 if (!rgn23_data)
16776 return 0;
16777
James Smarta0c87cb2009-07-19 10:01:10 -040016778 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16779 if (!pmb) {
16780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016781 "2600 failed to allocate mailbox memory\n");
16782 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016783 }
16784 mb = &pmb->u.mb;
16785
James Smarta0c87cb2009-07-19 10:01:10 -040016786 do {
16787 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16788 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16789
16790 if (rc != MBX_SUCCESS) {
16791 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016792 "2601 failed to read config "
16793 "region 23, rc 0x%x Status 0x%x\n",
16794 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016795 mb->un.varDmp.word_cnt = 0;
16796 }
16797 /*
16798 * dump mem may return a zero when finished or we got a
16799 * mailbox error, either way we are done.
16800 */
16801 if (mb->un.varDmp.word_cnt == 0)
16802 break;
16803 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16804 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16805
16806 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016807 rgn23_data + offset,
16808 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016809 offset += mb->un.varDmp.word_cnt;
16810 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16811
James Smart026abb82011-12-13 13:20:45 -050016812 mempool_free(pmb, phba->mbox_mem_pool);
16813 return offset;
16814}
16815
16816/**
16817 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16818 * @phba: pointer to lpfc hba data structure.
16819 * @rgn23_data: pointer to configure region 23 data.
16820 *
16821 * This function gets SLI4 port configure region 23 data through memory dump
16822 * mailbox command. When it successfully retrieves data, the size of the data
16823 * will be returned, otherwise, 0 will be returned.
16824 **/
16825static uint32_t
16826lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16827{
16828 LPFC_MBOXQ_t *mboxq = NULL;
16829 struct lpfc_dmabuf *mp = NULL;
16830 struct lpfc_mqe *mqe;
16831 uint32_t data_length = 0;
16832 int rc;
16833
16834 if (!rgn23_data)
16835 return 0;
16836
16837 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16838 if (!mboxq) {
16839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16840 "3105 failed to allocate mailbox memory\n");
16841 return 0;
16842 }
16843
16844 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16845 goto out;
16846 mqe = &mboxq->u.mqe;
16847 mp = (struct lpfc_dmabuf *) mboxq->context1;
16848 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16849 if (rc)
16850 goto out;
16851 data_length = mqe->un.mb_words[5];
16852 if (data_length == 0)
16853 goto out;
16854 if (data_length > DMP_RGN23_SIZE) {
16855 data_length = 0;
16856 goto out;
16857 }
16858 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16859out:
16860 mempool_free(mboxq, phba->mbox_mem_pool);
16861 if (mp) {
16862 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16863 kfree(mp);
16864 }
16865 return data_length;
16866}
16867
16868/**
16869 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16870 * @phba: pointer to lpfc hba data structure.
16871 *
16872 * This function read region 23 and parse TLV for port status to
16873 * decide if the user disaled the port. If the TLV indicates the
16874 * port is disabled, the hba_flag is set accordingly.
16875 **/
16876void
16877lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16878{
16879 uint8_t *rgn23_data = NULL;
16880 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16881 uint32_t offset = 0;
16882
16883 /* Get adapter Region 23 data */
16884 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16885 if (!rgn23_data)
16886 goto out;
16887
16888 if (phba->sli_rev < LPFC_SLI_REV4)
16889 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16890 else {
16891 if_type = bf_get(lpfc_sli_intf_if_type,
16892 &phba->sli4_hba.sli_intf);
16893 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16894 goto out;
16895 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16896 }
James Smarta0c87cb2009-07-19 10:01:10 -040016897
16898 if (!data_size)
16899 goto out;
16900
16901 /* Check the region signature first */
16902 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16904 "2619 Config region 23 has bad signature\n");
16905 goto out;
16906 }
16907 offset += 4;
16908
16909 /* Check the data structure version */
16910 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16912 "2620 Config region 23 has bad version\n");
16913 goto out;
16914 }
16915 offset += 4;
16916
16917 /* Parse TLV entries in the region */
16918 while (offset < data_size) {
16919 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16920 break;
16921 /*
16922 * If the TLV is not driver specific TLV or driver id is
16923 * not linux driver id, skip the record.
16924 */
16925 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16926 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16927 (rgn23_data[offset + 3] != 0)) {
16928 offset += rgn23_data[offset + 1] * 4 + 4;
16929 continue;
16930 }
16931
16932 /* Driver found a driver specific TLV in the config region */
16933 sub_tlv_len = rgn23_data[offset + 1] * 4;
16934 offset += 4;
16935 tlv_offset = 0;
16936
16937 /*
16938 * Search for configured port state sub-TLV.
16939 */
16940 while ((offset < data_size) &&
16941 (tlv_offset < sub_tlv_len)) {
16942 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16943 offset += 4;
16944 tlv_offset += 4;
16945 break;
16946 }
16947 if (rgn23_data[offset] != PORT_STE_TYPE) {
16948 offset += rgn23_data[offset + 1] * 4 + 4;
16949 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16950 continue;
16951 }
16952
16953 /* This HBA contains PORT_STE configured */
16954 if (!rgn23_data[offset + 2])
16955 phba->hba_flag |= LINK_DISABLED;
16956
16957 goto out;
16958 }
16959 }
James Smart026abb82011-12-13 13:20:45 -050016960
James Smarta0c87cb2009-07-19 10:01:10 -040016961out:
James Smarta0c87cb2009-07-19 10:01:10 -040016962 kfree(rgn23_data);
16963 return;
16964}
James Smart695a8142010-01-26 23:08:03 -050016965
16966/**
James Smart52d52442011-05-24 11:42:45 -040016967 * lpfc_wr_object - write an object to the firmware
16968 * @phba: HBA structure that indicates port to create a queue on.
16969 * @dmabuf_list: list of dmabufs to write to the port.
16970 * @size: the total byte value of the objects to write to the port.
16971 * @offset: the current offset to be used to start the transfer.
16972 *
16973 * This routine will create a wr_object mailbox command to send to the port.
16974 * the mailbox command will be constructed using the dma buffers described in
16975 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16976 * BDEs that the imbedded mailbox can support. The @offset variable will be
16977 * used to indicate the starting offset of the transfer and will also return
16978 * the offset after the write object mailbox has completed. @size is used to
16979 * determine the end of the object and whether the eof bit should be set.
16980 *
16981 * Return 0 is successful and offset will contain the the new offset to use
16982 * for the next write.
16983 * Return negative value for error cases.
16984 **/
16985int
16986lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16987 uint32_t size, uint32_t *offset)
16988{
16989 struct lpfc_mbx_wr_object *wr_object;
16990 LPFC_MBOXQ_t *mbox;
16991 int rc = 0, i = 0;
16992 uint32_t shdr_status, shdr_add_status;
16993 uint32_t mbox_tmo;
16994 union lpfc_sli4_cfg_shdr *shdr;
16995 struct lpfc_dmabuf *dmabuf;
16996 uint32_t written = 0;
16997
16998 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16999 if (!mbox)
17000 return -ENOMEM;
17001
17002 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17003 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17004 sizeof(struct lpfc_mbx_wr_object) -
17005 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17006
17007 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17008 wr_object->u.request.write_offset = *offset;
17009 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17010 wr_object->u.request.object_name[0] =
17011 cpu_to_le32(wr_object->u.request.object_name[0]);
17012 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17013 list_for_each_entry(dmabuf, dmabuf_list, list) {
17014 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17015 break;
17016 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17017 wr_object->u.request.bde[i].addrHigh =
17018 putPaddrHigh(dmabuf->phys);
17019 if (written + SLI4_PAGE_SIZE >= size) {
17020 wr_object->u.request.bde[i].tus.f.bdeSize =
17021 (size - written);
17022 written += (size - written);
17023 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17024 } else {
17025 wr_object->u.request.bde[i].tus.f.bdeSize =
17026 SLI4_PAGE_SIZE;
17027 written += SLI4_PAGE_SIZE;
17028 }
17029 i++;
17030 }
17031 wr_object->u.request.bde_count = i;
17032 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17033 if (!phba->sli4_hba.intr_enable)
17034 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17035 else {
James Smarta183a152011-10-10 21:32:43 -040017036 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017037 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17038 }
17039 /* The IOCTL status is embedded in the mailbox subheader. */
17040 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17041 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17042 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17043 if (rc != MBX_TIMEOUT)
17044 mempool_free(mbox, phba->mbox_mem_pool);
17045 if (shdr_status || shdr_add_status || rc) {
17046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17047 "3025 Write Object mailbox failed with "
17048 "status x%x add_status x%x, mbx status x%x\n",
17049 shdr_status, shdr_add_status, rc);
17050 rc = -ENXIO;
17051 } else
17052 *offset += wr_object->u.response.actual_write_length;
17053 return rc;
17054}
17055
17056/**
James Smart695a8142010-01-26 23:08:03 -050017057 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17058 * @vport: pointer to vport data structure.
17059 *
17060 * This function iterate through the mailboxq and clean up all REG_LOGIN
17061 * and REG_VPI mailbox commands associated with the vport. This function
17062 * is called when driver want to restart discovery of the vport due to
17063 * a Clear Virtual Link event.
17064 **/
17065void
17066lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17067{
17068 struct lpfc_hba *phba = vport->phba;
17069 LPFC_MBOXQ_t *mb, *nextmb;
17070 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017071 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017072 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017073 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017074 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017075 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017076
James Smartd439d282010-09-29 11:18:45 -040017077 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017078 spin_lock_irq(&phba->hbalock);
17079 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17080 if (mb->vport != vport)
17081 continue;
17082
17083 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17084 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17085 continue;
17086
James Smartd439d282010-09-29 11:18:45 -040017087 list_del(&mb->list);
17088 list_add_tail(&mb->list, &mbox_cmd_list);
17089 }
17090 /* Clean up active mailbox command with the vport */
17091 mb = phba->sli.mbox_active;
17092 if (mb && (mb->vport == vport)) {
17093 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17094 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17095 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17096 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17097 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17098 /* Put reference count for delayed processing */
17099 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17100 /* Unregister the RPI when mailbox complete */
17101 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17102 }
17103 }
James Smart63e801c2010-11-20 23:14:19 -050017104 /* Cleanup any mailbox completions which are not yet processed */
17105 do {
17106 restart_loop = 0;
17107 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17108 /*
17109 * If this mailox is already processed or it is
17110 * for another vport ignore it.
17111 */
17112 if ((mb->vport != vport) ||
17113 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17114 continue;
17115
17116 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17117 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17118 continue;
17119
17120 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17121 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17122 ndlp = (struct lpfc_nodelist *)mb->context2;
17123 /* Unregister the RPI when mailbox complete */
17124 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17125 restart_loop = 1;
17126 spin_unlock_irq(&phba->hbalock);
17127 spin_lock(shost->host_lock);
17128 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17129 spin_unlock(shost->host_lock);
17130 spin_lock_irq(&phba->hbalock);
17131 break;
17132 }
17133 }
17134 } while (restart_loop);
17135
James Smartd439d282010-09-29 11:18:45 -040017136 spin_unlock_irq(&phba->hbalock);
17137
17138 /* Release the cleaned-up mailbox commands */
17139 while (!list_empty(&mbox_cmd_list)) {
17140 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017141 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17142 mp = (struct lpfc_dmabuf *) (mb->context1);
17143 if (mp) {
17144 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17145 kfree(mp);
17146 }
James Smart78730cf2010-04-06 15:06:30 -040017147 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017148 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017149 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017150 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017151 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017152 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017153 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017154 }
James Smart695a8142010-01-26 23:08:03 -050017155 }
James Smart695a8142010-01-26 23:08:03 -050017156 mempool_free(mb, phba->mbox_mem_pool);
17157 }
James Smartd439d282010-09-29 11:18:45 -040017158
17159 /* Release the ndlp with the cleaned-up active mailbox command */
17160 if (act_mbx_ndlp) {
17161 spin_lock(shost->host_lock);
17162 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17163 spin_unlock(shost->host_lock);
17164 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017165 }
James Smart695a8142010-01-26 23:08:03 -050017166}
17167
James Smart2a9bf3d2010-06-07 15:24:45 -040017168/**
17169 * lpfc_drain_txq - Drain the txq
17170 * @phba: Pointer to HBA context object.
17171 *
17172 * This function attempt to submit IOCBs on the txq
17173 * to the adapter. For SLI4 adapters, the txq contains
17174 * ELS IOCBs that have been deferred because the there
17175 * are no SGLs. This congestion can occur with large
17176 * vport counts during node discovery.
17177 **/
17178
17179uint32_t
17180lpfc_drain_txq(struct lpfc_hba *phba)
17181{
17182 LIST_HEAD(completions);
17183 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017184 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017185 unsigned long iflags = 0;
17186 char *fail_msg = NULL;
17187 struct lpfc_sglq *sglq;
17188 union lpfc_wqe wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040017189 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017190
James Smart398d81c2013-05-31 17:04:19 -040017191 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017192 list_for_each_entry(piocbq, &pring->txq, list) {
17193 txq_cnt++;
17194 }
17195
17196 if (txq_cnt > pring->txq_max)
17197 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017198
James Smart398d81c2013-05-31 17:04:19 -040017199 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017200
James Smart0e9bb8d2013-03-01 16:35:12 -050017201 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017202 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017203
James Smart19ca7602010-11-20 23:11:55 -050017204 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017205 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017206 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017207 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17208 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017209 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017210 break;
17211 }
James Smart19ca7602010-11-20 23:11:55 -050017212 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017213 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017214 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017215 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017216 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017217 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017218 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017219
17220 /* The xri and iocb resources secured,
17221 * attempt to issue request
17222 */
James Smart6d368e52011-05-24 11:44:12 -040017223 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017224 piocbq->sli4_xritag = sglq->sli4_xritag;
17225 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17226 fail_msg = "to convert bpl to sgl";
17227 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17228 fail_msg = "to convert iocb to wqe";
17229 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17230 fail_msg = " - Wq is full";
17231 else
17232 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17233
17234 if (fail_msg) {
17235 /* Failed means we can't issue and need to cancel */
17236 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17237 "2822 IOCB failed %s iotag 0x%x "
17238 "xri 0x%x\n",
17239 fail_msg,
17240 piocbq->iotag, piocbq->sli4_xritag);
17241 list_add_tail(&piocbq->list, &completions);
17242 }
James Smart398d81c2013-05-31 17:04:19 -040017243 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017244 }
17245
James Smart2a9bf3d2010-06-07 15:24:45 -040017246 /* Cancel all the IOCBs that cannot be issued */
17247 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17248 IOERR_SLI_ABORTED);
17249
James Smart0e9bb8d2013-03-01 16:35:12 -050017250 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017251}