blob: c7181d85b9935cbe4b039196a2671d5ce4b78b52 [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 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>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040068static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040070static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
James Smartba20c852012-08-03 12:36:52 -040072static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040074static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
75static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart05580562011-05-24 11:40:48 -040076
James Smart4f774512009-05-22 14:52:35 -040077static IOCB_t *
78lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
79{
80 return &iocbq->iocb;
81}
82
83/**
84 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
85 * @q: The Work Queue to operate on.
86 * @wqe: The work Queue Entry to put on the Work queue.
87 *
88 * This routine will copy the contents of @wqe to the next available entry on
89 * the @q. This function will then ring the Work Queue Doorbell to signal the
90 * HBA to start processing the Work Queue Entry. This function returns 0 if
91 * successful. If no entries are available on @q then this function will return
92 * -ENOMEM.
93 * The caller is expected to hold the hbalock when calling this routine.
94 **/
95static uint32_t
96lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
97{
James Smart2e90f4b2011-12-13 13:22:37 -050098 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040099 struct lpfc_register doorbell;
100 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400101 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400102
James Smart2e90f4b2011-12-13 13:22:37 -0500103 /* sanity check on queue memory */
104 if (unlikely(!q))
105 return -ENOMEM;
106 temp_wqe = q->qe[q->host_index].wqe;
107
James Smart4f774512009-05-22 14:52:35 -0400108 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400109 idx = ((q->host_index + 1) % q->entry_count);
110 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400111 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400112 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400113 }
114 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400115 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500116 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400117 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500118 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
119 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400120 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
121
122 /* Update the host index before invoking device */
123 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400124
125 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400126
127 /* Ring Doorbell */
128 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500129 if (q->db_format == LPFC_DB_LIST_FORMAT) {
130 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
131 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
132 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
133 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
134 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
135 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
136 } else {
137 return -EINVAL;
138 }
139 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400140
141 return 0;
142}
143
144/**
145 * lpfc_sli4_wq_release - Updates internal hba index for WQ
146 * @q: The Work Queue to operate on.
147 * @index: The index to advance the hba index to.
148 *
149 * This routine will update the HBA index of a queue to reflect consumption of
150 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
151 * an entry the host calls this function to update the queue's internal
152 * pointers. This routine returns the number of entries that were consumed by
153 * the HBA.
154 **/
155static uint32_t
156lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
157{
158 uint32_t released = 0;
159
James Smart2e90f4b2011-12-13 13:22:37 -0500160 /* sanity check on queue memory */
161 if (unlikely(!q))
162 return 0;
163
James Smart4f774512009-05-22 14:52:35 -0400164 if (q->hba_index == index)
165 return 0;
166 do {
167 q->hba_index = ((q->hba_index + 1) % q->entry_count);
168 released++;
169 } while (q->hba_index != index);
170 return released;
171}
172
173/**
174 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
175 * @q: The Mailbox Queue to operate on.
176 * @wqe: The Mailbox Queue Entry to put on the Work queue.
177 *
178 * This routine will copy the contents of @mqe to the next available entry on
179 * the @q. This function will then ring the Work Queue Doorbell to signal the
180 * HBA to start processing the Work Queue Entry. This function returns 0 if
181 * successful. If no entries are available on @q then this function will return
182 * -ENOMEM.
183 * The caller is expected to hold the hbalock when calling this routine.
184 **/
185static uint32_t
186lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
187{
James Smart2e90f4b2011-12-13 13:22:37 -0500188 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400189 struct lpfc_register doorbell;
190 uint32_t host_index;
191
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 */
205 host_index = q->host_index;
206 q->host_index = ((q->host_index + 1) % q->entry_count);
207
208 /* Ring Doorbell */
209 doorbell.word0 = 0;
210 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
211 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
212 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400213 return 0;
214}
215
216/**
217 * lpfc_sli4_mq_release - Updates internal hba index for MQ
218 * @q: The Mailbox Queue to operate on.
219 *
220 * This routine will update the HBA index of a queue to reflect consumption of
221 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
222 * an entry the host calls this function to update the queue's internal
223 * pointers. This routine returns the number of entries that were consumed by
224 * the HBA.
225 **/
226static uint32_t
227lpfc_sli4_mq_release(struct lpfc_queue *q)
228{
James Smart2e90f4b2011-12-13 13:22:37 -0500229 /* sanity check on queue memory */
230 if (unlikely(!q))
231 return 0;
232
James Smart4f774512009-05-22 14:52:35 -0400233 /* Clear the mailbox pointer for completion */
234 q->phba->mbox = NULL;
235 q->hba_index = ((q->hba_index + 1) % q->entry_count);
236 return 1;
237}
238
239/**
240 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
241 * @q: The Event Queue to get the first valid EQE from
242 *
243 * This routine will get the first valid Event Queue Entry from @q, update
244 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
245 * the Queue (no more work to do), or the Queue is full of EQEs that have been
246 * processed, but not popped back to the HBA then this routine will return NULL.
247 **/
248static struct lpfc_eqe *
249lpfc_sli4_eq_get(struct lpfc_queue *q)
250{
James Smart2e90f4b2011-12-13 13:22:37 -0500251 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400252 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500253
254 /* sanity check on queue memory */
255 if (unlikely(!q))
256 return NULL;
257 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400258
259 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400260 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400261 return NULL;
262 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400263 idx = ((q->hba_index + 1) % q->entry_count);
264 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400265 return NULL;
266
James Smart027140e2012-08-03 12:35:44 -0400267 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400268 return eqe;
269}
270
271/**
James Smartba20c852012-08-03 12:36:52 -0400272 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
273 * @q: The Event Queue to disable interrupts
274 *
275 **/
276static inline void
277lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
278{
279 struct lpfc_register doorbell;
280
281 doorbell.word0 = 0;
282 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
283 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
284 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
285 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
286 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
287 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
288}
289
290/**
James Smart4f774512009-05-22 14:52:35 -0400291 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
292 * @q: The Event Queue that the host has completed processing for.
293 * @arm: Indicates whether the host wants to arms this CQ.
294 *
295 * This routine will mark all Event Queue Entries on @q, from the last
296 * known completed entry to the last entry that was processed, as completed
297 * by clearing the valid bit for each completion queue entry. Then it will
298 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
299 * The internal host index in the @q will be updated by this routine to indicate
300 * that the host has finished processing the entries. The @arm parameter
301 * indicates that the queue should be rearmed when ringing the doorbell.
302 *
303 * This function will return the number of EQEs that were popped.
304 **/
305uint32_t
306lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
307{
308 uint32_t released = 0;
309 struct lpfc_eqe *temp_eqe;
310 struct lpfc_register doorbell;
311
James Smart2e90f4b2011-12-13 13:22:37 -0500312 /* sanity check on queue memory */
313 if (unlikely(!q))
314 return 0;
315
James Smart4f774512009-05-22 14:52:35 -0400316 /* while there are valid entries */
317 while (q->hba_index != q->host_index) {
318 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400319 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400320 released++;
321 q->host_index = ((q->host_index + 1) % q->entry_count);
322 }
323 if (unlikely(released == 0 && !arm))
324 return 0;
325
326 /* ring doorbell for number popped */
327 doorbell.word0 = 0;
328 if (arm) {
329 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
330 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
331 }
332 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
333 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500334 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
335 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
336 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400337 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500338 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
339 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
340 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400341 return released;
342}
343
344/**
345 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
346 * @q: The Completion Queue to get the first valid CQE from
347 *
348 * This routine will get the first valid Completion Queue Entry from @q, update
349 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
350 * the Queue (no more work to do), or the Queue is full of CQEs that have been
351 * processed, but not popped back to the HBA then this routine will return NULL.
352 **/
353static struct lpfc_cqe *
354lpfc_sli4_cq_get(struct lpfc_queue *q)
355{
356 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400357 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400358
James Smart2e90f4b2011-12-13 13:22:37 -0500359 /* sanity check on queue memory */
360 if (unlikely(!q))
361 return NULL;
362
James Smart4f774512009-05-22 14:52:35 -0400363 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400364 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400365 return NULL;
366 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400367 idx = ((q->hba_index + 1) % q->entry_count);
368 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400369 return NULL;
370
371 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400372 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400373 return cqe;
374}
375
376/**
377 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
378 * @q: The Completion Queue that the host has completed processing for.
379 * @arm: Indicates whether the host wants to arms this CQ.
380 *
381 * This routine will mark all Completion queue entries on @q, from the last
382 * known completed entry to the last entry that was processed, as completed
383 * by clearing the valid bit for each completion queue entry. Then it will
384 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
385 * The internal host index in the @q will be updated by this routine to indicate
386 * that the host has finished processing the entries. The @arm parameter
387 * indicates that the queue should be rearmed when ringing the doorbell.
388 *
389 * This function will return the number of CQEs that were released.
390 **/
391uint32_t
392lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
393{
394 uint32_t released = 0;
395 struct lpfc_cqe *temp_qe;
396 struct lpfc_register doorbell;
397
James Smart2e90f4b2011-12-13 13:22:37 -0500398 /* sanity check on queue memory */
399 if (unlikely(!q))
400 return 0;
James Smart4f774512009-05-22 14:52:35 -0400401 /* while there are valid entries */
402 while (q->hba_index != q->host_index) {
403 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400404 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400405 released++;
406 q->host_index = ((q->host_index + 1) % q->entry_count);
407 }
408 if (unlikely(released == 0 && !arm))
409 return 0;
410
411 /* ring doorbell for number popped */
412 doorbell.word0 = 0;
413 if (arm)
414 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
415 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
416 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500417 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
418 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
419 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400420 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
421 return released;
422}
423
424/**
425 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
426 * @q: The Header Receive Queue to operate on.
427 * @wqe: The Receive Queue Entry to put on the Receive queue.
428 *
429 * This routine will copy the contents of @wqe to the next available entry on
430 * the @q. This function will then ring the Receive Queue Doorbell to signal the
431 * HBA to start processing the Receive Queue Entry. This function returns the
432 * index that the rqe was copied to if successful. If no entries are available
433 * on @q then this function will return -ENOMEM.
434 * The caller is expected to hold the hbalock when calling this routine.
435 **/
436static int
437lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
438 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
439{
James Smart2e90f4b2011-12-13 13:22:37 -0500440 struct lpfc_rqe *temp_hrqe;
441 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400442 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500443 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400444
James Smart2e90f4b2011-12-13 13:22:37 -0500445 /* sanity check on queue memory */
446 if (unlikely(!hq) || unlikely(!dq))
447 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500448 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500449 temp_hrqe = hq->qe[hq->host_index].rqe;
450 temp_drqe = dq->qe[dq->host_index].rqe;
451
James Smart4f774512009-05-22 14:52:35 -0400452 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
453 return -EINVAL;
454 if (hq->host_index != dq->host_index)
455 return -EINVAL;
456 /* If the host has not yet processed the next entry then we are done */
457 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
458 return -EBUSY;
459 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
460 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
461
462 /* Update the host index to point to the next slot */
463 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
464 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
465
466 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400467 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400468 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500469 if (hq->db_format == LPFC_DB_RING_FORMAT) {
470 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
471 hq->entry_repost);
472 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
473 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
474 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
475 hq->entry_repost);
476 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
477 hq->host_index);
478 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
479 } else {
480 return -EINVAL;
481 }
482 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400483 }
484 return put_index;
485}
486
487/**
488 * lpfc_sli4_rq_release - Updates internal hba index for RQ
489 * @q: The Header Receive Queue to operate on.
490 *
491 * This routine will update the HBA index of a queue to reflect consumption of
492 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
493 * consumed an entry the host calls this function to update the queue's
494 * internal pointers. This routine returns the number of entries that were
495 * consumed by the HBA.
496 **/
497static uint32_t
498lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
499{
James Smart2e90f4b2011-12-13 13:22:37 -0500500 /* sanity check on queue memory */
501 if (unlikely(!hq) || unlikely(!dq))
502 return 0;
503
James Smart4f774512009-05-22 14:52:35 -0400504 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
505 return 0;
506 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
507 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
508 return 1;
509}
510
James Smarte59058c2008-08-24 21:49:00 -0400511/**
James Smart3621a712009-04-06 18:47:14 -0400512 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400513 * @phba: Pointer to HBA context object.
514 * @pring: Pointer to driver SLI ring object.
515 *
516 * This function returns pointer to next command iocb entry
517 * in the command ring. The caller must hold hbalock to prevent
518 * other threads consume the next command iocb.
519 * SLI-2/SLI-3 provide different sized iocbs.
520 **/
James Smarted957682007-06-17 19:56:37 -0500521static inline IOCB_t *
522lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
523{
James Smart7e56aa22012-08-03 12:35:34 -0400524 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
525 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500526}
527
James Smarte59058c2008-08-24 21:49:00 -0400528/**
James Smart3621a712009-04-06 18:47:14 -0400529 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400530 * @phba: Pointer to HBA context object.
531 * @pring: Pointer to driver SLI ring object.
532 *
533 * This function returns pointer to next response iocb entry
534 * in the response ring. The caller must hold hbalock to make sure
535 * that no other thread consume the next response iocb.
536 * SLI-2/SLI-3 provide different sized iocbs.
537 **/
James Smarted957682007-06-17 19:56:37 -0500538static inline IOCB_t *
539lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
540{
James Smart7e56aa22012-08-03 12:35:34 -0400541 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
542 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500543}
544
James Smarte59058c2008-08-24 21:49:00 -0400545/**
James Smart3621a712009-04-06 18:47:14 -0400546 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400547 * @phba: Pointer to HBA context object.
548 *
549 * This function is called with hbalock held. This function
550 * allocates a new driver iocb object from the iocb pool. If the
551 * allocation is successful, it returns pointer to the newly
552 * allocated iocb object else it returns NULL.
553 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400554struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500555__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400556{
557 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
558 struct lpfc_iocbq * iocbq = NULL;
559
560 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400561 if (iocbq)
562 phba->iocb_cnt++;
563 if (phba->iocb_cnt > phba->iocb_max)
564 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400565 return iocbq;
566}
567
James Smarte59058c2008-08-24 21:49:00 -0400568/**
James Smartda0436e2009-05-22 14:51:39 -0400569 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
570 * @phba: Pointer to HBA context object.
571 * @xritag: XRI value.
572 *
573 * This function clears the sglq pointer from the array of acive
574 * sglq's. The xritag that is passed in is used to index into the
575 * array. Before the xritag can be used it needs to be adjusted
576 * by subtracting the xribase.
577 *
578 * Returns sglq ponter = success, NULL = Failure.
579 **/
580static struct lpfc_sglq *
581__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
582{
James Smartda0436e2009-05-22 14:51:39 -0400583 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400584
585 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
586 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400587 return sglq;
588}
589
590/**
591 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
592 * @phba: Pointer to HBA context object.
593 * @xritag: XRI value.
594 *
595 * This function returns 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 **/
James Smart0f65ff62010-02-26 14:14:23 -0500602struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400603__lpfc_get_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];
James Smartda0436e2009-05-22 14:51:39 -0400608 return sglq;
609}
610
611/**
James Smart1151e3e2011-02-16 12:39:35 -0500612 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500613 * @phba: Pointer to HBA context object.
614 * @xritag: xri used in this exchange.
615 * @rrq: The RRQ to be cleared.
616 *
James Smart19ca7602010-11-20 23:11:55 -0500617 **/
James Smart1151e3e2011-02-16 12:39:35 -0500618void
619lpfc_clr_rrq_active(struct lpfc_hba *phba,
620 uint16_t xritag,
621 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500622{
James Smart1151e3e2011-02-16 12:39:35 -0500623 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500624
James Smart1151e3e2011-02-16 12:39:35 -0500625 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
626 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500627
628 /* The target DID could have been swapped (cable swap)
629 * we should use the ndlp from the findnode if it is
630 * available.
631 */
James Smart1151e3e2011-02-16 12:39:35 -0500632 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500633 ndlp = rrq->ndlp;
634
James Smart1151e3e2011-02-16 12:39:35 -0500635 if (!ndlp)
636 goto out;
637
James Smartcff261f2013-12-17 20:29:47 -0500638 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500639 rrq->send_rrq = 0;
640 rrq->xritag = 0;
641 rrq->rrq_stop_time = 0;
642 }
James Smart1151e3e2011-02-16 12:39:35 -0500643out:
James Smart19ca7602010-11-20 23:11:55 -0500644 mempool_free(rrq, phba->rrq_pool);
645}
646
647/**
648 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
649 * @phba: Pointer to HBA context object.
650 *
651 * This function is called with hbalock held. This function
652 * Checks if stop_time (ratov from setting rrq active) has
653 * been reached, if it has and the send_rrq flag is set then
654 * it will call lpfc_send_rrq. If the send_rrq flag is not set
655 * then it will just call the routine to clear the rrq and
656 * free the rrq resource.
657 * The timer is set to the next rrq that is going to expire before
658 * leaving the routine.
659 *
660 **/
661void
662lpfc_handle_rrq_active(struct lpfc_hba *phba)
663{
664 struct lpfc_node_rrq *rrq;
665 struct lpfc_node_rrq *nextrrq;
666 unsigned long next_time;
667 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500668 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500669
670 spin_lock_irqsave(&phba->hbalock, iflags);
671 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400672 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500673 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500674 &phba->active_rrq_list, list) {
675 if (time_after(jiffies, rrq->rrq_stop_time))
676 list_move(&rrq->list, &send_rrq);
677 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500678 next_time = rrq->rrq_stop_time;
679 }
680 spin_unlock_irqrestore(&phba->hbalock, iflags);
681 if (!list_empty(&phba->active_rrq_list))
682 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500683 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
684 list_del(&rrq->list);
685 if (!rrq->send_rrq)
686 /* this call will free the rrq */
687 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
688 else if (lpfc_send_rrq(phba, rrq)) {
689 /* if we send the rrq then the completion handler
690 * will clear the bit in the xribitmap.
691 */
692 lpfc_clr_rrq_active(phba, rrq->xritag,
693 rrq);
694 }
695 }
James Smart19ca7602010-11-20 23:11:55 -0500696}
697
698/**
699 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
700 * @vport: Pointer to vport context object.
701 * @xri: The xri used in the exchange.
702 * @did: The targets DID for this exchange.
703 *
704 * returns NULL = rrq not found in the phba->active_rrq_list.
705 * rrq = rrq for this xri and target.
706 **/
707struct lpfc_node_rrq *
708lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
709{
710 struct lpfc_hba *phba = vport->phba;
711 struct lpfc_node_rrq *rrq;
712 struct lpfc_node_rrq *nextrrq;
713 unsigned long iflags;
714
715 if (phba->sli_rev != LPFC_SLI_REV4)
716 return NULL;
717 spin_lock_irqsave(&phba->hbalock, iflags);
718 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
719 if (rrq->vport == vport && rrq->xritag == xri &&
720 rrq->nlp_DID == did){
721 list_del(&rrq->list);
722 spin_unlock_irqrestore(&phba->hbalock, iflags);
723 return rrq;
724 }
725 }
726 spin_unlock_irqrestore(&phba->hbalock, iflags);
727 return NULL;
728}
729
730/**
731 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
732 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500733 * @ndlp: Pointer to the lpfc_node_list structure.
734 * If ndlp is NULL Remove all active RRQs for this vport from the
735 * phba->active_rrq_list and clear the rrq.
736 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500737 **/
738void
James Smart1151e3e2011-02-16 12:39:35 -0500739lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500740
741{
742 struct lpfc_hba *phba = vport->phba;
743 struct lpfc_node_rrq *rrq;
744 struct lpfc_node_rrq *nextrrq;
745 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500746 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500747
748 if (phba->sli_rev != LPFC_SLI_REV4)
749 return;
James Smart1151e3e2011-02-16 12:39:35 -0500750 if (!ndlp) {
751 lpfc_sli4_vport_delete_els_xri_aborted(vport);
752 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500753 }
James Smart1151e3e2011-02-16 12:39:35 -0500754 spin_lock_irqsave(&phba->hbalock, iflags);
755 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
756 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
757 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500758 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500759
760 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
761 list_del(&rrq->list);
762 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
763 }
James Smart19ca7602010-11-20 23:11:55 -0500764}
765
766/**
767 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
768 * @phba: Pointer to HBA context object.
769 *
770 * Remove all rrqs from the phba->active_rrq_list and free them by
771 * calling __lpfc_clr_active_rrq
772 *
773 **/
774void
775lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
776{
777 struct lpfc_node_rrq *rrq;
778 struct lpfc_node_rrq *nextrrq;
779 unsigned long next_time;
780 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500781 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500782
783 if (phba->sli_rev != LPFC_SLI_REV4)
784 return;
785 spin_lock_irqsave(&phba->hbalock, iflags);
786 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400787 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2));
James Smart1151e3e2011-02-16 12:39:35 -0500788 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500789 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500790
791 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
792 list_del(&rrq->list);
793 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
794 }
James Smart19ca7602010-11-20 23:11:55 -0500795 if (!list_empty(&phba->active_rrq_list))
796 mod_timer(&phba->rrq_tmr, next_time);
797}
798
799
800/**
James Smart1151e3e2011-02-16 12:39:35 -0500801 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500802 * @phba: Pointer to HBA context object.
803 * @ndlp: Targets nodelist pointer for this exchange.
804 * @xritag the xri in the bitmap to test.
805 *
806 * This function is called with hbalock held. This function
807 * returns 0 = rrq not active for this xri
808 * 1 = rrq is valid for this xri.
809 **/
James Smart1151e3e2011-02-16 12:39:35 -0500810int
811lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500812 uint16_t xritag)
813{
James Smart19ca7602010-11-20 23:11:55 -0500814 if (!ndlp)
815 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500816 if (!ndlp->active_rrqs_xri_bitmap)
817 return 0;
818 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500819 return 1;
820 else
821 return 0;
822}
823
824/**
825 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
826 * @phba: Pointer to HBA context object.
827 * @ndlp: nodelist pointer for this target.
828 * @xritag: xri used in this exchange.
829 * @rxid: Remote Exchange ID.
830 * @send_rrq: Flag used to determine if we should send rrq els cmd.
831 *
832 * This function takes the hbalock.
833 * The active bit is always set in the active rrq xri_bitmap even
834 * if there is no slot avaiable for the other rrq information.
835 *
836 * returns 0 rrq actived for this xri
837 * < 0 No memory or invalid ndlp.
838 **/
839int
840lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500841 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500842{
James Smart19ca7602010-11-20 23:11:55 -0500843 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500844 struct lpfc_node_rrq *rrq;
845 int empty;
846
847 if (!ndlp)
848 return -EINVAL;
849
850 if (!phba->cfg_enable_rrq)
851 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500852
853 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500854 if (phba->pport->load_flag & FC_UNLOADING) {
855 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
856 goto out;
857 }
858
859 /*
860 * set the active bit even if there is no mem available.
861 */
862 if (NLP_CHK_FREE_REQ(ndlp))
863 goto out;
864
865 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
866 goto out;
867
James Smartcff261f2013-12-17 20:29:47 -0500868 if (!ndlp->active_rrqs_xri_bitmap)
869 goto out;
870
871 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500872 goto out;
873
James Smart19ca7602010-11-20 23:11:55 -0500874 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500875 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
876 if (!rrq) {
877 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
878 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
879 " DID:0x%x Send:%d\n",
880 xritag, rxid, ndlp->nlp_DID, send_rrq);
881 return -EINVAL;
882 }
James Smarte5771b42013-03-01 16:37:14 -0500883 if (phba->cfg_enable_rrq == 1)
884 rrq->send_rrq = send_rrq;
885 else
886 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500887 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400888 rrq->rrq_stop_time = jiffies +
889 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500890 rrq->ndlp = ndlp;
891 rrq->nlp_DID = ndlp->nlp_DID;
892 rrq->vport = ndlp->vport;
893 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500894 spin_lock_irqsave(&phba->hbalock, iflags);
895 empty = list_empty(&phba->active_rrq_list);
896 list_add_tail(&rrq->list, &phba->active_rrq_list);
897 phba->hba_flag |= HBA_RRQ_ACTIVE;
898 if (empty)
899 lpfc_worker_wake_up(phba);
900 spin_unlock_irqrestore(&phba->hbalock, iflags);
901 return 0;
902out:
903 spin_unlock_irqrestore(&phba->hbalock, iflags);
904 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
905 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
906 " DID:0x%x Send:%d\n",
907 xritag, rxid, ndlp->nlp_DID, send_rrq);
908 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500909}
910
911/**
James Smartda0436e2009-05-22 14:51:39 -0400912 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
913 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500914 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400915 *
916 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400917 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400918 * list is not empty then it is successful, it returns pointer to the newly
919 * allocated sglq object else it returns NULL.
920 **/
921static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500922__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400923{
924 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
925 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500926 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500927 struct lpfc_scsi_buf *lpfc_cmd;
928 struct lpfc_nodelist *ndlp;
929 int found = 0;
930
931 if (piocbq->iocb_flag & LPFC_IO_FCP) {
932 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
933 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500934 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
935 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500936 ndlp = piocbq->context_un.ndlp;
James Smartd5ce53b2013-04-17 20:17:26 -0400937 else if (piocbq->iocb_flag & LPFC_IO_LIBDFC)
James Smart93d1379e2012-05-09 21:19:34 -0400938 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500939 else
940 ndlp = piocbq->context1;
941
James Smartda0436e2009-05-22 14:51:39 -0400942 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500943 start_sglq = sglq;
944 while (!found) {
945 if (!sglq)
946 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400947 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500948 /* This xri has an rrq outstanding for this DID.
949 * put it back in the list and get another xri.
950 */
951 list_add_tail(&sglq->list, lpfc_sgl_list);
952 sglq = NULL;
953 list_remove_head(lpfc_sgl_list, sglq,
954 struct lpfc_sglq, list);
955 if (sglq == start_sglq) {
956 sglq = NULL;
957 break;
958 } else
959 continue;
960 }
961 sglq->ndlp = ndlp;
962 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400963 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500964 sglq->state = SGL_ALLOCATED;
965 }
James Smartda0436e2009-05-22 14:51:39 -0400966 return sglq;
967}
968
969/**
James Smart3621a712009-04-06 18:47:14 -0400970 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400971 * @phba: Pointer to HBA context object.
972 *
973 * This function is called with no lock held. This function
974 * allocates a new driver iocb object from the iocb pool. If the
975 * allocation is successful, it returns pointer to the newly
976 * allocated iocb object else it returns NULL.
977 **/
James Smart2e0fef82007-06-17 19:56:36 -0500978struct lpfc_iocbq *
979lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500980{
James Smart2e0fef82007-06-17 19:56:36 -0500981 struct lpfc_iocbq * iocbq = NULL;
982 unsigned long iflags;
983
984 spin_lock_irqsave(&phba->hbalock, iflags);
985 iocbq = __lpfc_sli_get_iocbq(phba);
986 spin_unlock_irqrestore(&phba->hbalock, iflags);
987 return iocbq;
988}
989
James Smarte59058c2008-08-24 21:49:00 -0400990/**
James Smart4f774512009-05-22 14:52:35 -0400991 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
992 * @phba: Pointer to HBA context object.
993 * @iocbq: Pointer to driver iocb object.
994 *
995 * This function is called with hbalock held to release driver
996 * iocb object to the iocb pool. The iotag in the iocb object
997 * does not change for each use of the iocb object. This function
998 * clears all other fields of the iocb object when it is freed.
999 * The sqlq structure that holds the xritag and phys and virtual
1000 * mappings for the scatter gather list is retrieved from the
1001 * active array of sglq. The get of the sglq pointer also clears
1002 * the entry in the array. If the status of the IO indiactes that
1003 * this IO was aborted then the sglq entry it put on the
1004 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1005 * IO has good status or fails for any other reason then the sglq
1006 * entry is added to the free list (lpfc_sgl_list).
1007 **/
1008static void
1009__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1010{
1011 struct lpfc_sglq *sglq;
1012 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001013 unsigned long iflag = 0;
1014 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001015
1016 if (iocbq->sli4_xritag == NO_XRI)
1017 sglq = NULL;
1018 else
James Smart6d368e52011-05-24 11:44:12 -04001019 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1020
James Smart0e9bb8d2013-03-01 16:35:12 -05001021
James Smart4f774512009-05-22 14:52:35 -04001022 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001023 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1024 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001025 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1026 iflag);
1027 list_add(&sglq->list,
1028 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1029 spin_unlock_irqrestore(
1030 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001031 } else {
1032 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001033 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001034 list_add_tail(&sglq->list,
1035 &phba->sli4_hba.lpfc_sgl_list);
James 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
James Smart0e9bb8d2013-03-01 16:35:12 -05001069
1070 /*
James Smart3772a992009-05-22 14:50:54 -04001071 * Clean all volatile data fields, preserve iotag and node struct.
1072 */
1073 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1074 iocbq->sli4_xritag = NO_XRI;
1075 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1076}
1077
1078/**
James Smart3621a712009-04-06 18:47:14 -04001079 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001080 * @phba: Pointer to HBA context object.
1081 * @iocbq: Pointer to driver iocb object.
1082 *
1083 * This function is called with hbalock held to release driver
1084 * iocb object to the iocb pool. The iotag in the iocb object
1085 * does not change for each use of the iocb object. This function
1086 * clears all other fields of the iocb object when it is freed.
1087 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001088static void
James Smart2e0fef82007-06-17 19:56:36 -05001089__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1090{
James Smart3772a992009-05-22 14:50:54 -04001091 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001092 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001093}
1094
James Smarte59058c2008-08-24 21:49:00 -04001095/**
James Smart3621a712009-04-06 18:47:14 -04001096 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001097 * @phba: Pointer to HBA context object.
1098 * @iocbq: Pointer to driver iocb object.
1099 *
1100 * This function is called with no lock held to release the iocb to
1101 * iocb pool.
1102 **/
James Smart2e0fef82007-06-17 19:56:36 -05001103void
1104lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1105{
1106 unsigned long iflags;
1107
1108 /*
1109 * Clean all volatile data fields, preserve iotag and node struct.
1110 */
1111 spin_lock_irqsave(&phba->hbalock, iflags);
1112 __lpfc_sli_release_iocbq(phba, iocbq);
1113 spin_unlock_irqrestore(&phba->hbalock, iflags);
1114}
1115
James Smarte59058c2008-08-24 21:49:00 -04001116/**
James Smarta257bf92009-04-06 18:48:10 -04001117 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1118 * @phba: Pointer to HBA context object.
1119 * @iocblist: List of IOCBs.
1120 * @ulpstatus: ULP status in IOCB command field.
1121 * @ulpWord4: ULP word-4 in IOCB command field.
1122 *
1123 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1124 * on the list by invoking the complete callback function associated with the
1125 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1126 * fields.
1127 **/
1128void
1129lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1130 uint32_t ulpstatus, uint32_t ulpWord4)
1131{
1132 struct lpfc_iocbq *piocb;
1133
1134 while (!list_empty(iocblist)) {
1135 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001136 if (!piocb->iocb_cmpl)
1137 lpfc_sli_release_iocbq(phba, piocb);
1138 else {
1139 piocb->iocb.ulpStatus = ulpstatus;
1140 piocb->iocb.un.ulpWord[4] = ulpWord4;
1141 (piocb->iocb_cmpl) (phba, piocb, piocb);
1142 }
1143 }
1144 return;
1145}
1146
1147/**
James Smart3621a712009-04-06 18:47:14 -04001148 * lpfc_sli_iocb_cmd_type - Get the iocb type
1149 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001150 *
1151 * This function is called by ring event handler function to get the iocb type.
1152 * This function translates the iocb command to an iocb command type used to
1153 * decide the final disposition of each completed IOCB.
1154 * The function returns
1155 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1156 * LPFC_SOL_IOCB if it is a solicited iocb completion
1157 * LPFC_ABORT_IOCB if it is an abort iocb
1158 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1159 *
1160 * The caller is not required to hold any lock.
1161 **/
dea31012005-04-17 16:05:31 -05001162static lpfc_iocb_type
1163lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1164{
1165 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1166
1167 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1168 return 0;
1169
1170 switch (iocb_cmnd) {
1171 case CMD_XMIT_SEQUENCE_CR:
1172 case CMD_XMIT_SEQUENCE_CX:
1173 case CMD_XMIT_BCAST_CN:
1174 case CMD_XMIT_BCAST_CX:
1175 case CMD_ELS_REQUEST_CR:
1176 case CMD_ELS_REQUEST_CX:
1177 case CMD_CREATE_XRI_CR:
1178 case CMD_CREATE_XRI_CX:
1179 case CMD_GET_RPI_CN:
1180 case CMD_XMIT_ELS_RSP_CX:
1181 case CMD_GET_RPI_CR:
1182 case CMD_FCP_IWRITE_CR:
1183 case CMD_FCP_IWRITE_CX:
1184 case CMD_FCP_IREAD_CR:
1185 case CMD_FCP_IREAD_CX:
1186 case CMD_FCP_ICMND_CR:
1187 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001188 case CMD_FCP_TSEND_CX:
1189 case CMD_FCP_TRSP_CX:
1190 case CMD_FCP_TRECEIVE_CX:
1191 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001192 case CMD_ADAPTER_MSG:
1193 case CMD_ADAPTER_DUMP:
1194 case CMD_XMIT_SEQUENCE64_CR:
1195 case CMD_XMIT_SEQUENCE64_CX:
1196 case CMD_XMIT_BCAST64_CN:
1197 case CMD_XMIT_BCAST64_CX:
1198 case CMD_ELS_REQUEST64_CR:
1199 case CMD_ELS_REQUEST64_CX:
1200 case CMD_FCP_IWRITE64_CR:
1201 case CMD_FCP_IWRITE64_CX:
1202 case CMD_FCP_IREAD64_CR:
1203 case CMD_FCP_IREAD64_CX:
1204 case CMD_FCP_ICMND64_CR:
1205 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001206 case CMD_FCP_TSEND64_CX:
1207 case CMD_FCP_TRSP64_CX:
1208 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001209 case CMD_GEN_REQUEST64_CR:
1210 case CMD_GEN_REQUEST64_CX:
1211 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001212 case DSSCMD_IWRITE64_CR:
1213 case DSSCMD_IWRITE64_CX:
1214 case DSSCMD_IREAD64_CR:
1215 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001216 type = LPFC_SOL_IOCB;
1217 break;
1218 case CMD_ABORT_XRI_CN:
1219 case CMD_ABORT_XRI_CX:
1220 case CMD_CLOSE_XRI_CN:
1221 case CMD_CLOSE_XRI_CX:
1222 case CMD_XRI_ABORTED_CX:
1223 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001224 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001225 type = LPFC_ABORT_IOCB;
1226 break;
1227 case CMD_RCV_SEQUENCE_CX:
1228 case CMD_RCV_ELS_REQ_CX:
1229 case CMD_RCV_SEQUENCE64_CX:
1230 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001231 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001232 case CMD_IOCB_RCV_SEQ64_CX:
1233 case CMD_IOCB_RCV_ELS64_CX:
1234 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001235 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001236 type = LPFC_UNSOL_IOCB;
1237 break;
James Smart3163f722008-02-08 18:50:25 -05001238 case CMD_IOCB_XMIT_MSEQ64_CR:
1239 case CMD_IOCB_XMIT_MSEQ64_CX:
1240 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1241 case CMD_IOCB_RCV_ELS_LIST64_CX:
1242 case CMD_IOCB_CLOSE_EXTENDED_CN:
1243 case CMD_IOCB_ABORT_EXTENDED_CN:
1244 case CMD_IOCB_RET_HBQE64_CN:
1245 case CMD_IOCB_FCP_IBIDIR64_CR:
1246 case CMD_IOCB_FCP_IBIDIR64_CX:
1247 case CMD_IOCB_FCP_ITASKMGT64_CX:
1248 case CMD_IOCB_LOGENTRY_CN:
1249 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1250 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001251 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001252 type = LPFC_UNKNOWN_IOCB;
1253 break;
dea31012005-04-17 16:05:31 -05001254 default:
1255 type = LPFC_UNKNOWN_IOCB;
1256 break;
1257 }
1258
1259 return type;
1260}
1261
James Smarte59058c2008-08-24 21:49:00 -04001262/**
James Smart3621a712009-04-06 18:47:14 -04001263 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001264 * @phba: Pointer to HBA context object.
1265 *
1266 * This function is called from SLI initialization code
1267 * to configure every ring of the HBA's SLI interface. The
1268 * caller is not required to hold any lock. This function issues
1269 * a config_ring mailbox command for each ring.
1270 * This function returns zero if successful else returns a negative
1271 * error code.
1272 **/
dea31012005-04-17 16:05:31 -05001273static int
James Smarted957682007-06-17 19:56:37 -05001274lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001275{
1276 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001277 LPFC_MBOXQ_t *pmb;
1278 MAILBOX_t *pmbox;
1279 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001280
James Smarted957682007-06-17 19:56:37 -05001281 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1282 if (!pmb)
1283 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001284 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001285 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001286 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001287 lpfc_config_ring(phba, i, pmb);
1288 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1289 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001291 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001292 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1293 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001294 rc, pmbox->mbxCommand,
1295 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001296 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001297 ret = -ENXIO;
1298 break;
dea31012005-04-17 16:05:31 -05001299 }
1300 }
James Smarted957682007-06-17 19:56:37 -05001301 mempool_free(pmb, phba->mbox_mem_pool);
1302 return ret;
dea31012005-04-17 16:05:31 -05001303}
1304
James Smarte59058c2008-08-24 21:49:00 -04001305/**
James Smart3621a712009-04-06 18:47:14 -04001306 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001307 * @phba: Pointer to HBA context object.
1308 * @pring: Pointer to driver SLI ring object.
1309 * @piocb: Pointer to the driver iocb object.
1310 *
1311 * This function is called with hbalock held. The function adds the
1312 * new iocb to txcmplq of the given ring. This function always returns
1313 * 0. If this function is called for ELS ring, this function checks if
1314 * there is a vport associated with the ELS command. This function also
1315 * starts els_tmofunc timer if this is an ELS command.
1316 **/
dea31012005-04-17 16:05:31 -05001317static int
James Smart2e0fef82007-06-17 19:56:36 -05001318lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1319 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001320{
dea31012005-04-17 16:05:31 -05001321 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001322 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001323
James Smart92d7f7b2007-06-17 19:56:38 -05001324 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1325 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1326 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1327 if (!piocb->vport)
1328 BUG();
1329 else
1330 mod_timer(&piocb->vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001331 jiffies +
1332 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
James Smart92d7f7b2007-06-17 19:56:38 -05001333 }
1334
dea31012005-04-17 16:05:31 -05001335
James Smart2e0fef82007-06-17 19:56:36 -05001336 return 0;
dea31012005-04-17 16:05:31 -05001337}
1338
James Smarte59058c2008-08-24 21:49:00 -04001339/**
James Smart3621a712009-04-06 18:47:14 -04001340 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001341 * @phba: Pointer to HBA context object.
1342 * @pring: Pointer to driver SLI ring object.
1343 *
1344 * This function is called with hbalock held to get next
1345 * iocb in txq of the given ring. If there is any iocb in
1346 * the txq, the function returns first iocb in the list after
1347 * removing the iocb from the list, else it returns NULL.
1348 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001349struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001350lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001351{
dea31012005-04-17 16:05:31 -05001352 struct lpfc_iocbq *cmd_iocb;
1353
James Smart858c9f62007-06-17 19:56:39 -05001354 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001355 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001356}
1357
James Smarte59058c2008-08-24 21:49:00 -04001358/**
James Smart3621a712009-04-06 18:47:14 -04001359 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001360 * @phba: Pointer to HBA context object.
1361 * @pring: Pointer to driver SLI ring object.
1362 *
1363 * This function is called with hbalock held and the caller must post the
1364 * iocb without releasing the lock. If the caller releases the lock,
1365 * iocb slot returned by the function is not guaranteed to be available.
1366 * The function returns pointer to the next available iocb slot if there
1367 * is available slot in the ring, else it returns NULL.
1368 * If the get index of the ring is ahead of the put index, the function
1369 * will post an error attention event to the worker thread to take the
1370 * HBA to offline state.
1371 **/
dea31012005-04-17 16:05:31 -05001372static IOCB_t *
1373lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1374{
James Smart34b02dc2008-08-24 21:49:55 -04001375 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001376 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1377 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1378 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1379 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001380
James Smart7e56aa22012-08-03 12:35:34 -04001381 if (unlikely(pring->sli.sli3.local_getidx ==
1382 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001383
James Smart7e56aa22012-08-03 12:35:34 -04001384 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001385
James Smart7e56aa22012-08-03 12:35:34 -04001386 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001387 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001388 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001389 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001390 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001391 pring->sli.sli3.local_getidx,
1392 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001393
James Smart2e0fef82007-06-17 19:56:36 -05001394 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001395 /*
1396 * All error attention handlers are posted to
1397 * worker thread
1398 */
1399 phba->work_ha |= HA_ERATT;
1400 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001401
James Smart5e9d9b82008-06-14 22:52:53 -04001402 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001403
1404 return NULL;
1405 }
1406
James Smart7e56aa22012-08-03 12:35:34 -04001407 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001408 return NULL;
1409 }
1410
James Smarted957682007-06-17 19:56:37 -05001411 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001412}
1413
James Smarte59058c2008-08-24 21:49:00 -04001414/**
James Smart3621a712009-04-06 18:47:14 -04001415 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001416 * @phba: Pointer to HBA context object.
1417 * @iocbq: Pointer to driver iocb object.
1418 *
1419 * This function gets an iotag for the iocb. If there is no unused iotag and
1420 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1421 * array and assigns a new iotag.
1422 * The function returns the allocated iotag if successful, else returns zero.
1423 * Zero is not a valid iotag.
1424 * The caller is not required to hold any lock.
1425 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001426uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001427lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001428{
James Smart2e0fef82007-06-17 19:56:36 -05001429 struct lpfc_iocbq **new_arr;
1430 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001431 size_t new_len;
1432 struct lpfc_sli *psli = &phba->sli;
1433 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001434
James Smart2e0fef82007-06-17 19:56:36 -05001435 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001436 iotag = psli->last_iotag;
1437 if(++iotag < psli->iocbq_lookup_len) {
1438 psli->last_iotag = iotag;
1439 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001440 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001441 iocbq->iotag = iotag;
1442 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001443 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001444 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1445 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001446 spin_unlock_irq(&phba->hbalock);
1447 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001448 GFP_KERNEL);
1449 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001450 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001451 old_arr = psli->iocbq_lookup;
1452 if (new_len <= psli->iocbq_lookup_len) {
1453 /* highly unprobable case */
1454 kfree(new_arr);
1455 iotag = psli->last_iotag;
1456 if(++iotag < psli->iocbq_lookup_len) {
1457 psli->last_iotag = iotag;
1458 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001459 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001460 iocbq->iotag = iotag;
1461 return iotag;
1462 }
James Smart2e0fef82007-06-17 19:56:36 -05001463 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001464 return 0;
1465 }
1466 if (psli->iocbq_lookup)
1467 memcpy(new_arr, old_arr,
1468 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001469 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001470 psli->iocbq_lookup = new_arr;
1471 psli->iocbq_lookup_len = new_len;
1472 psli->last_iotag = iotag;
1473 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001474 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001475 iocbq->iotag = iotag;
1476 kfree(old_arr);
1477 return iotag;
1478 }
James Smart8f6d98d2006-08-01 07:34:00 -04001479 } else
James Smart2e0fef82007-06-17 19:56:36 -05001480 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001481
James Smartbc739052010-08-04 16:11:18 -04001482 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001483 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1484 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001485
James Bottomley604a3e32005-10-29 10:28:33 -05001486 return 0;
dea31012005-04-17 16:05:31 -05001487}
1488
James Smarte59058c2008-08-24 21:49:00 -04001489/**
James Smart3621a712009-04-06 18:47:14 -04001490 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001491 * @phba: Pointer to HBA context object.
1492 * @pring: Pointer to driver SLI ring object.
1493 * @iocb: Pointer to iocb slot in the ring.
1494 * @nextiocb: Pointer to driver iocb object which need to be
1495 * posted to firmware.
1496 *
1497 * This function is called with hbalock held to post a new iocb to
1498 * the firmware. This function copies the new iocb to ring iocb slot and
1499 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1500 * a completion call back for this iocb else the function will free the
1501 * iocb object.
1502 **/
dea31012005-04-17 16:05:31 -05001503static void
1504lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1505 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1506{
1507 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001508 * Set up an iotag
dea31012005-04-17 16:05:31 -05001509 */
James Bottomley604a3e32005-10-29 10:28:33 -05001510 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001511
James Smarte2a0a9d2008-12-04 22:40:02 -05001512
James Smarta58cbd52007-08-02 11:09:43 -04001513 if (pring->ringno == LPFC_ELS_RING) {
1514 lpfc_debugfs_slow_ring_trc(phba,
1515 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1516 *(((uint32_t *) &nextiocb->iocb) + 4),
1517 *(((uint32_t *) &nextiocb->iocb) + 6),
1518 *(((uint32_t *) &nextiocb->iocb) + 7));
1519 }
1520
dea31012005-04-17 16:05:31 -05001521 /*
1522 * Issue iocb command to adapter
1523 */
James Smart92d7f7b2007-06-17 19:56:38 -05001524 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001525 wmb();
1526 pring->stats.iocb_cmd++;
1527
1528 /*
1529 * If there is no completion routine to call, we can release the
1530 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1531 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1532 */
1533 if (nextiocb->iocb_cmpl)
1534 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001535 else
James Smart2e0fef82007-06-17 19:56:36 -05001536 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001537
1538 /*
1539 * Let the HBA know what IOCB slot will be the next one the
1540 * driver will put a command into.
1541 */
James Smart7e56aa22012-08-03 12:35:34 -04001542 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1543 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001544}
1545
James Smarte59058c2008-08-24 21:49:00 -04001546/**
James Smart3621a712009-04-06 18:47:14 -04001547 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001548 * @phba: Pointer to HBA context object.
1549 * @pring: Pointer to driver SLI ring object.
1550 *
1551 * The caller is not required to hold any lock for calling this function.
1552 * This function updates the chip attention bits for the ring to inform firmware
1553 * that there are pending work to be done for this ring and requests an
1554 * interrupt when there is space available in the ring. This function is
1555 * called when the driver is unable to post more iocbs to the ring due
1556 * to unavailability of space in the ring.
1557 **/
dea31012005-04-17 16:05:31 -05001558static void
James Smart2e0fef82007-06-17 19:56:36 -05001559lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001560{
1561 int ringno = pring->ringno;
1562
1563 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1564
1565 wmb();
1566
1567 /*
1568 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1569 * The HBA will tell us when an IOCB entry is available.
1570 */
1571 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1572 readl(phba->CAregaddr); /* flush */
1573
1574 pring->stats.iocb_cmd_full++;
1575}
1576
James Smarte59058c2008-08-24 21:49:00 -04001577/**
James Smart3621a712009-04-06 18:47:14 -04001578 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001579 * @phba: Pointer to HBA context object.
1580 * @pring: Pointer to driver SLI ring object.
1581 *
1582 * This function updates the chip attention register bit for the
1583 * given ring to inform HBA that there is more work to be done
1584 * in this ring. The caller is not required to hold any lock.
1585 **/
dea31012005-04-17 16:05:31 -05001586static void
James Smart2e0fef82007-06-17 19:56:36 -05001587lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001588{
1589 int ringno = pring->ringno;
1590
1591 /*
1592 * Tell the HBA that there is work to do in this ring.
1593 */
James Smart34b02dc2008-08-24 21:49:55 -04001594 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1595 wmb();
1596 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1597 readl(phba->CAregaddr); /* flush */
1598 }
dea31012005-04-17 16:05:31 -05001599}
1600
James Smarte59058c2008-08-24 21:49:00 -04001601/**
James Smart3621a712009-04-06 18:47:14 -04001602 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001603 * @phba: Pointer to HBA context object.
1604 * @pring: Pointer to driver SLI ring object.
1605 *
1606 * This function is called with hbalock held to post pending iocbs
1607 * in the txq to the firmware. This function is called when driver
1608 * detects space available in the ring.
1609 **/
dea31012005-04-17 16:05:31 -05001610static void
James Smart2e0fef82007-06-17 19:56:36 -05001611lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001612{
1613 IOCB_t *iocb;
1614 struct lpfc_iocbq *nextiocb;
1615
1616 /*
1617 * Check to see if:
1618 * (a) there is anything on the txq to send
1619 * (b) link is up
1620 * (c) link attention events can be processed (fcp ring only)
1621 * (d) IOCB processing is not blocked by the outstanding mbox command.
1622 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001623
1624 if (lpfc_is_link_up(phba) &&
1625 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001626 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001627 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001628
1629 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1630 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1631 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1632
1633 if (iocb)
1634 lpfc_sli_update_ring(phba, pring);
1635 else
1636 lpfc_sli_update_full_ring(phba, pring);
1637 }
1638
1639 return;
1640}
1641
James Smarte59058c2008-08-24 21:49:00 -04001642/**
James Smart3621a712009-04-06 18:47:14 -04001643 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001644 * @phba: Pointer to HBA context object.
1645 * @hbqno: HBQ number.
1646 *
1647 * This function is called with hbalock held to get the next
1648 * available slot for the given HBQ. If there is free slot
1649 * available for the HBQ it will return pointer to the next available
1650 * HBQ entry else it will return NULL.
1651 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001652static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001653lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1654{
1655 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1656
1657 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1658 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1659 hbqp->next_hbqPutIdx = 0;
1660
1661 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001662 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001663 uint32_t getidx = le32_to_cpu(raw_index);
1664
1665 hbqp->local_hbqGetIdx = getidx;
1666
1667 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1668 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001669 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001670 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001671 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001672 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001673 hbqp->entry_count);
1674
1675 phba->link_state = LPFC_HBA_ERROR;
1676 return NULL;
1677 }
1678
1679 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1680 return NULL;
1681 }
1682
James Smart51ef4c22007-08-02 11:10:31 -04001683 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1684 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001685}
1686
James Smarte59058c2008-08-24 21:49:00 -04001687/**
James Smart3621a712009-04-06 18:47:14 -04001688 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001689 * @phba: Pointer to HBA context object.
1690 *
1691 * This function is called with no lock held to free all the
1692 * hbq buffers while uninitializing the SLI interface. It also
1693 * frees the HBQ buffers returned by the firmware but not yet
1694 * processed by the upper layers.
1695 **/
James Smarted957682007-06-17 19:56:37 -05001696void
1697lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1698{
James Smart92d7f7b2007-06-17 19:56:38 -05001699 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1700 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001701 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001702 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001703 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001704
James Smart51ef4c22007-08-02 11:10:31 -04001705 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001706 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001707 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001708 for (i = 0; i < hbq_count; ++i) {
1709 list_for_each_entry_safe(dmabuf, next_dmabuf,
1710 &phba->hbqs[i].hbq_buffer_list, list) {
1711 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1712 list_del(&hbq_buf->dbuf.list);
1713 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1714 }
James Smarta8adb832007-10-27 13:37:53 -04001715 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001716 }
James Smart3163f722008-02-08 18:50:25 -05001717 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001718 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1719 list) {
James Smart3163f722008-02-08 18:50:25 -05001720 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1721 list_del(&hbq_buf->dbuf.list);
1722 if (hbq_buf->tag == -1) {
1723 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1724 (phba, hbq_buf);
1725 } else {
1726 hbqno = hbq_buf->tag >> 16;
1727 if (hbqno >= LPFC_MAX_HBQS)
1728 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1729 (phba, hbq_buf);
1730 else
1731 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1732 hbq_buf);
1733 }
1734 }
1735
1736 /* Mark the HBQs not in use */
1737 phba->hbq_in_use = 0;
1738 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001739}
1740
James Smarte59058c2008-08-24 21:49:00 -04001741/**
James Smart3621a712009-04-06 18:47:14 -04001742 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001743 * @phba: Pointer to HBA context object.
1744 * @hbqno: HBQ number.
1745 * @hbq_buf: Pointer to HBQ buffer.
1746 *
1747 * This function is called with the hbalock held to post a
1748 * hbq buffer to the firmware. If the function finds an empty
1749 * slot in the HBQ, it will post the buffer. The function will return
1750 * pointer to the hbq entry if it successfully post the buffer
1751 * else it will return NULL.
1752 **/
James Smart3772a992009-05-22 14:50:54 -04001753static int
James Smarted957682007-06-17 19:56:37 -05001754lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001755 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001756{
James Smart3772a992009-05-22 14:50:54 -04001757 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1758}
1759
1760/**
1761 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1762 * @phba: Pointer to HBA context object.
1763 * @hbqno: HBQ number.
1764 * @hbq_buf: Pointer to HBQ buffer.
1765 *
1766 * This function is called with the hbalock held to post a hbq buffer to the
1767 * firmware. If the function finds an empty slot in the HBQ, it will post the
1768 * buffer and place it on the hbq_buffer_list. The function will return zero if
1769 * it successfully post the buffer else it will return an error.
1770 **/
1771static int
1772lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1773 struct hbq_dmabuf *hbq_buf)
1774{
James Smarted957682007-06-17 19:56:37 -05001775 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001776 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001777
1778 /* Get next HBQ entry slot to use */
1779 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1780 if (hbqe) {
1781 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1782
James Smart92d7f7b2007-06-17 19:56:38 -05001783 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1784 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001785 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001786 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001787 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1788 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1789 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001790 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1791 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001792 /* flush */
James Smarted957682007-06-17 19:56:37 -05001793 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001794 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001795 return 0;
1796 } else
1797 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001798}
1799
James Smart4f774512009-05-22 14:52:35 -04001800/**
1801 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1802 * @phba: Pointer to HBA context object.
1803 * @hbqno: HBQ number.
1804 * @hbq_buf: Pointer to HBQ buffer.
1805 *
1806 * This function is called with the hbalock held to post an RQE to the SLI4
1807 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1808 * the hbq_buffer_list and return zero, otherwise it will return an error.
1809 **/
1810static int
1811lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1812 struct hbq_dmabuf *hbq_buf)
1813{
1814 int rc;
1815 struct lpfc_rqe hrqe;
1816 struct lpfc_rqe drqe;
1817
1818 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1819 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1820 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1821 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1822 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1823 &hrqe, &drqe);
1824 if (rc < 0)
1825 return rc;
1826 hbq_buf->tag = rc;
1827 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1828 return 0;
1829}
1830
James Smarte59058c2008-08-24 21:49:00 -04001831/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001832static struct lpfc_hbq_init lpfc_els_hbq = {
1833 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001834 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001835 .mask_count = 0,
1836 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001837 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001838 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001839 .init_count = 40,
1840 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001841};
James Smarted957682007-06-17 19:56:37 -05001842
James Smarte59058c2008-08-24 21:49:00 -04001843/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001844static struct lpfc_hbq_init lpfc_extra_hbq = {
1845 .rn = 1,
1846 .entry_count = 200,
1847 .mask_count = 0,
1848 .profile = 0,
1849 .ring_mask = (1 << LPFC_EXTRA_RING),
1850 .buffer_count = 0,
1851 .init_count = 0,
1852 .add_count = 5,
1853};
1854
James Smarte59058c2008-08-24 21:49:00 -04001855/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001856struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001857 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001858 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001859};
1860
James Smarte59058c2008-08-24 21:49:00 -04001861/**
James Smart3621a712009-04-06 18:47:14 -04001862 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001863 * @phba: Pointer to HBA context object.
1864 * @hbqno: HBQ number.
1865 * @count: Number of HBQ buffers to be posted.
1866 *
James Smartd7c255b2008-08-24 21:50:00 -04001867 * This function is called with no lock held to post more hbq buffers to the
1868 * given HBQ. The function returns the number of HBQ buffers successfully
1869 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001870 **/
James Smart311464e2007-08-02 11:10:37 -04001871static int
James Smart92d7f7b2007-06-17 19:56:38 -05001872lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1873{
James Smartd7c255b2008-08-24 21:50:00 -04001874 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001875 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001876 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001877 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001878 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001879 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001880
James Smartd7c255b2008-08-24 21:50:00 -04001881 if ((phba->hbqs[hbqno].buffer_count + count) >
1882 lpfc_hbq_defs[hbqno]->entry_count)
1883 count = lpfc_hbq_defs[hbqno]->entry_count -
1884 phba->hbqs[hbqno].buffer_count;
1885 if (!count)
1886 return 0;
1887 /* Allocate HBQ entries */
1888 for (i = 0; i < count; i++) {
1889 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1890 if (!hbq_buffer)
1891 break;
1892 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1893 }
James Smart3163f722008-02-08 18:50:25 -05001894 /* Check whether HBQ is still in use */
1895 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001896 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001897 goto err;
1898 while (!list_empty(&hbq_buf_list)) {
1899 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1900 dbuf.list);
1901 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1902 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001903 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001904 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001905 posted++;
1906 } else
James Smart51ef4c22007-08-02 11:10:31 -04001907 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001908 }
James Smart3163f722008-02-08 18:50:25 -05001909 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001910 return posted;
1911err:
1912 spin_unlock_irqrestore(&phba->hbalock, flags);
1913 while (!list_empty(&hbq_buf_list)) {
1914 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1915 dbuf.list);
1916 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1917 }
James Smart92d7f7b2007-06-17 19:56:38 -05001918 return 0;
James Smarted957682007-06-17 19:56:37 -05001919}
1920
James Smarte59058c2008-08-24 21:49:00 -04001921/**
James Smart3621a712009-04-06 18:47:14 -04001922 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001923 * @phba: Pointer to HBA context object.
1924 * @qno: HBQ number.
1925 *
1926 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001927 * is called with no lock held. The function returns the number of HBQ entries
1928 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001929 **/
James Smarted957682007-06-17 19:56:37 -05001930int
James Smart92d7f7b2007-06-17 19:56:38 -05001931lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001932{
James Smartdef9c7a2009-12-21 17:02:28 -05001933 if (phba->sli_rev == LPFC_SLI_REV4)
1934 return 0;
1935 else
1936 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1937 lpfc_hbq_defs[qno]->add_count);
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_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001942 * @phba: Pointer to HBA context object.
1943 * @qno: HBQ queue number.
1944 *
1945 * This function is called from SLI initialization code path with
1946 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001947 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001948 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001949static int
James Smart92d7f7b2007-06-17 19:56:38 -05001950lpfc_sli_hbqbuf_init_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 lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001954 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001955 else
1956 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1957 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001958}
1959
James Smarte59058c2008-08-24 21:49:00 -04001960/**
James Smart3772a992009-05-22 14:50:54 -04001961 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1962 * @phba: Pointer to HBA context object.
1963 * @hbqno: HBQ number.
1964 *
1965 * This function removes the first hbq buffer on an hbq list and returns a
1966 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1967 **/
1968static struct hbq_dmabuf *
1969lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1970{
1971 struct lpfc_dmabuf *d_buf;
1972
1973 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1974 if (!d_buf)
1975 return NULL;
1976 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1977}
1978
1979/**
James Smart3621a712009-04-06 18:47:14 -04001980 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001981 * @phba: Pointer to HBA context object.
1982 * @tag: Tag of the hbq buffer.
1983 *
1984 * This function is called with hbalock held. This function searches
1985 * for the hbq buffer associated with the given tag in the hbq buffer
1986 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1987 * it returns NULL.
1988 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001989static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001990lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1991{
James Smart92d7f7b2007-06-17 19:56:38 -05001992 struct lpfc_dmabuf *d_buf;
1993 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001994 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001995
James Smart51ef4c22007-08-02 11:10:31 -04001996 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001997 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001998 return NULL;
1999
James Smart3772a992009-05-22 14:50:54 -04002000 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002001 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002002 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002003 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002004 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002005 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002006 }
2007 }
James Smart3772a992009-05-22 14:50:54 -04002008 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002010 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002011 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002012 return NULL;
James Smarted957682007-06-17 19:56:37 -05002013}
2014
James Smarte59058c2008-08-24 21:49:00 -04002015/**
James Smart3621a712009-04-06 18:47:14 -04002016 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002017 * @phba: Pointer to HBA context object.
2018 * @hbq_buffer: Pointer to HBQ buffer.
2019 *
2020 * This function is called with hbalock. This function gives back
2021 * the hbq buffer to firmware. If the HBQ does not have space to
2022 * post the buffer, it will free the buffer.
2023 **/
James Smarted957682007-06-17 19:56:37 -05002024void
James Smart51ef4c22007-08-02 11:10:31 -04002025lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002026{
2027 uint32_t hbqno;
2028
James Smart51ef4c22007-08-02 11:10:31 -04002029 if (hbq_buffer) {
2030 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002031 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002032 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002033 }
2034}
2035
James Smarte59058c2008-08-24 21:49:00 -04002036/**
James Smart3621a712009-04-06 18:47:14 -04002037 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002038 * @mbxCommand: mailbox command code.
2039 *
2040 * This function is called by the mailbox event handler function to verify
2041 * that the completed mailbox command is a legitimate mailbox command. If the
2042 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2043 * and the mailbox event handler will take the HBA offline.
2044 **/
dea31012005-04-17 16:05:31 -05002045static int
2046lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2047{
2048 uint8_t ret;
2049
2050 switch (mbxCommand) {
2051 case MBX_LOAD_SM:
2052 case MBX_READ_NV:
2053 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002054 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002055 case MBX_RUN_BIU_DIAG:
2056 case MBX_INIT_LINK:
2057 case MBX_DOWN_LINK:
2058 case MBX_CONFIG_LINK:
2059 case MBX_CONFIG_RING:
2060 case MBX_RESET_RING:
2061 case MBX_READ_CONFIG:
2062 case MBX_READ_RCONFIG:
2063 case MBX_READ_SPARM:
2064 case MBX_READ_STATUS:
2065 case MBX_READ_RPI:
2066 case MBX_READ_XRI:
2067 case MBX_READ_REV:
2068 case MBX_READ_LNK_STAT:
2069 case MBX_REG_LOGIN:
2070 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002071 case MBX_CLEAR_LA:
2072 case MBX_DUMP_MEMORY:
2073 case MBX_DUMP_CONTEXT:
2074 case MBX_RUN_DIAGS:
2075 case MBX_RESTART:
2076 case MBX_UPDATE_CFG:
2077 case MBX_DOWN_LOAD:
2078 case MBX_DEL_LD_ENTRY:
2079 case MBX_RUN_PROGRAM:
2080 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002081 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002082 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002083 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002084 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002085 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002086 case MBX_LOAD_AREA:
2087 case MBX_RUN_BIU_DIAG64:
2088 case MBX_CONFIG_PORT:
2089 case MBX_READ_SPARM64:
2090 case MBX_READ_RPI64:
2091 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002092 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002093 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002094 case MBX_SET_DEBUG:
2095 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002096 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002097 case MBX_REG_VPI:
2098 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002099 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002100 case MBX_PORT_CAPABILITIES:
2101 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002102 case MBX_SLI4_CONFIG:
2103 case MBX_SLI4_REQ_FTRS:
2104 case MBX_REG_FCFI:
2105 case MBX_UNREG_FCFI:
2106 case MBX_REG_VFI:
2107 case MBX_UNREG_VFI:
2108 case MBX_INIT_VPI:
2109 case MBX_INIT_VFI:
2110 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002111 case MBX_READ_EVENT_LOG_STATUS:
2112 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002113 case MBX_SECURITY_MGMT:
2114 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002115 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002116 ret = mbxCommand;
2117 break;
2118 default:
2119 ret = MBX_SHUTDOWN;
2120 break;
2121 }
James Smart2e0fef82007-06-17 19:56:36 -05002122 return ret;
dea31012005-04-17 16:05:31 -05002123}
James Smarte59058c2008-08-24 21:49:00 -04002124
2125/**
James Smart3621a712009-04-06 18:47:14 -04002126 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002127 * @phba: Pointer to HBA context object.
2128 * @pmboxq: Pointer to mailbox command.
2129 *
2130 * This is completion handler function for mailbox commands issued from
2131 * lpfc_sli_issue_mbox_wait function. This function is called by the
2132 * mailbox event handler function with no lock held. This function
2133 * will wake up thread waiting on the wait queue pointed by context1
2134 * of the mailbox.
2135 **/
James Smart04c68492009-05-22 14:52:52 -04002136void
James Smart2e0fef82007-06-17 19:56:36 -05002137lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002138{
2139 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002140 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002141
2142 /*
2143 * If pdone_q is empty, the driver thread gave up waiting and
2144 * continued running.
2145 */
James Smart7054a602007-04-25 09:52:34 -04002146 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002147 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002148 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2149 if (pdone_q)
2150 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002151 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002152 return;
2153}
2154
James Smarte59058c2008-08-24 21:49:00 -04002155
2156/**
James Smart3621a712009-04-06 18:47:14 -04002157 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002158 * @phba: Pointer to HBA context object.
2159 * @pmb: Pointer to mailbox object.
2160 *
2161 * This function is the default mailbox completion handler. It
2162 * frees the memory resources associated with the completed mailbox
2163 * command. If the completed command is a REG_LOGIN mailbox command,
2164 * this function will issue a UREG_LOGIN to re-claim the RPI.
2165 **/
dea31012005-04-17 16:05:31 -05002166void
James Smart2e0fef82007-06-17 19:56:36 -05002167lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002168{
James Smartd439d282010-09-29 11:18:45 -04002169 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002170 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002171 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002172 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002173 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002174 int rc;
2175
dea31012005-04-17 16:05:31 -05002176 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002177
dea31012005-04-17 16:05:31 -05002178 if (mp) {
2179 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2180 kfree(mp);
2181 }
James Smart7054a602007-04-25 09:52:34 -04002182
2183 /*
2184 * If a REG_LOGIN succeeded after node is destroyed or node
2185 * is in re-discovery driver need to cleanup the RPI.
2186 */
James Smart2e0fef82007-06-17 19:56:36 -05002187 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002188 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2189 !pmb->u.mb.mbxStatus) {
2190 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002191 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002192 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002193 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002194 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2195 if (rc != MBX_NOT_FINISHED)
2196 return;
2197 }
2198
James Smart695a8142010-01-26 23:08:03 -05002199 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2200 !(phba->pport->load_flag & FC_UNLOADING) &&
2201 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002202 shost = lpfc_shost_from_vport(vport);
2203 spin_lock_irq(shost->host_lock);
2204 vport->vpi_state |= LPFC_VPI_REGISTERED;
2205 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2206 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002207 }
2208
James Smartd439d282010-09-29 11:18:45 -04002209 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2210 ndlp = (struct lpfc_nodelist *)pmb->context2;
2211 lpfc_nlp_put(ndlp);
2212 pmb->context2 = NULL;
2213 }
2214
James Smartdcf2a4e2010-09-29 11:18:53 -04002215 /* Check security permission status on INIT_LINK mailbox command */
2216 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2217 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2218 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2219 "2860 SLI authentication is required "
2220 "for INIT_LINK but has not done yet\n");
2221
James Smart04c68492009-05-22 14:52:52 -04002222 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2223 lpfc_sli4_mbox_cmd_free(phba, pmb);
2224 else
2225 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002226}
2227
James Smarte59058c2008-08-24 21:49:00 -04002228/**
James Smart3621a712009-04-06 18:47:14 -04002229 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002230 * @phba: Pointer to HBA context object.
2231 *
2232 * This function is called with no lock held. This function processes all
2233 * the completed mailbox commands and gives it to upper layers. The interrupt
2234 * service routine processes mailbox completion interrupt and adds completed
2235 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2236 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2237 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2238 * function returns the mailbox commands to the upper layer by calling the
2239 * completion handler function of each mailbox.
2240 **/
dea31012005-04-17 16:05:31 -05002241int
James Smart2e0fef82007-06-17 19:56:36 -05002242lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002243{
James Smart92d7f7b2007-06-17 19:56:38 -05002244 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002245 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002246 int rc;
2247 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002248
2249 phba->sli.slistat.mbox_event++;
2250
James Smart92d7f7b2007-06-17 19:56:38 -05002251 /* Get all completed mailboxe buffers into the cmplq */
2252 spin_lock_irq(&phba->hbalock);
2253 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2254 spin_unlock_irq(&phba->hbalock);
2255
dea31012005-04-17 16:05:31 -05002256 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002257 do {
2258 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2259 if (pmb == NULL)
2260 break;
2261
James Smart04c68492009-05-22 14:52:52 -04002262 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002263
James Smart858c9f62007-06-17 19:56:39 -05002264 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2265 if (pmb->vport) {
2266 lpfc_debugfs_disc_trc(pmb->vport,
2267 LPFC_DISC_TRC_MBOX_VPORT,
2268 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2269 (uint32_t)pmbox->mbxCommand,
2270 pmbox->un.varWords[0],
2271 pmbox->un.varWords[1]);
2272 }
2273 else {
2274 lpfc_debugfs_disc_trc(phba->pport,
2275 LPFC_DISC_TRC_MBOX,
2276 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2277 (uint32_t)pmbox->mbxCommand,
2278 pmbox->un.varWords[0],
2279 pmbox->un.varWords[1]);
2280 }
2281 }
2282
dea31012005-04-17 16:05:31 -05002283 /*
2284 * It is a fatal error if unknown mbox command completion.
2285 */
2286 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2287 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002288 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002289 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002290 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002291 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002292 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002293 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002294 lpfc_sli_config_mbox_subsys_get(phba,
2295 pmb),
2296 lpfc_sli_config_mbox_opcode_get(phba,
2297 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002298 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002299 phba->work_hs = HS_FFER3;
2300 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002301 continue;
dea31012005-04-17 16:05:31 -05002302 }
2303
dea31012005-04-17 16:05:31 -05002304 if (pmbox->mbxStatus) {
2305 phba->sli.slistat.mbox_stat_err++;
2306 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2307 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002308 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002309 LOG_MBOX | LOG_SLI,
2310 "(%d):0305 Mbox cmd cmpl "
2311 "error - RETRYing Data: x%x "
2312 "(x%x/x%x) x%x x%x x%x\n",
2313 pmb->vport ? pmb->vport->vpi : 0,
2314 pmbox->mbxCommand,
2315 lpfc_sli_config_mbox_subsys_get(phba,
2316 pmb),
2317 lpfc_sli_config_mbox_opcode_get(phba,
2318 pmb),
2319 pmbox->mbxStatus,
2320 pmbox->un.varWords[0],
2321 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002322 pmbox->mbxStatus = 0;
2323 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002324 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002325 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002326 continue;
dea31012005-04-17 16:05:31 -05002327 }
2328 }
2329
2330 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002331 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002332 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002333 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2334 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002335 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002336 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002337 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2338 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002339 pmb->mbox_cmpl,
2340 *((uint32_t *) pmbox),
2341 pmbox->un.varWords[0],
2342 pmbox->un.varWords[1],
2343 pmbox->un.varWords[2],
2344 pmbox->un.varWords[3],
2345 pmbox->un.varWords[4],
2346 pmbox->un.varWords[5],
2347 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002348 pmbox->un.varWords[7],
2349 pmbox->un.varWords[8],
2350 pmbox->un.varWords[9],
2351 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002352
James Smart92d7f7b2007-06-17 19:56:38 -05002353 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002354 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002355 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002356 return 0;
dea31012005-04-17 16:05:31 -05002357}
James Smart92d7f7b2007-06-17 19:56:38 -05002358
James Smarte59058c2008-08-24 21:49:00 -04002359/**
James Smart3621a712009-04-06 18:47:14 -04002360 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002361 * @phba: Pointer to HBA context object.
2362 * @pring: Pointer to driver SLI ring object.
2363 * @tag: buffer tag.
2364 *
2365 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2366 * is set in the tag the buffer is posted for a particular exchange,
2367 * the function will return the buffer without replacing the buffer.
2368 * If the buffer is for unsolicited ELS or CT traffic, this function
2369 * returns the buffer and also posts another buffer to the firmware.
2370 **/
James Smart76bb24e2007-10-27 13:38:00 -04002371static struct lpfc_dmabuf *
2372lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002373 struct lpfc_sli_ring *pring,
2374 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002375{
James Smart9f1e1b52008-12-04 22:39:40 -05002376 struct hbq_dmabuf *hbq_entry;
2377
James Smart76bb24e2007-10-27 13:38:00 -04002378 if (tag & QUE_BUFTAG_BIT)
2379 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002380 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2381 if (!hbq_entry)
2382 return NULL;
2383 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002384}
James Smart57127f12007-10-27 13:37:05 -04002385
James Smart3772a992009-05-22 14:50:54 -04002386/**
2387 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2388 * @phba: Pointer to HBA context object.
2389 * @pring: Pointer to driver SLI ring object.
2390 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2391 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2392 * @fch_type: the type for the first frame of the sequence.
2393 *
2394 * This function is called with no lock held. This function uses the r_ctl and
2395 * type of the received sequence to find the correct callback function to call
2396 * to process the sequence.
2397 **/
2398static int
2399lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2400 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2401 uint32_t fch_type)
2402{
2403 int i;
2404
2405 /* unSolicited Responses */
2406 if (pring->prt[0].profile) {
2407 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2408 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2409 saveq);
2410 return 1;
2411 }
2412 /* We must search, based on rctl / type
2413 for the right routine */
2414 for (i = 0; i < pring->num_mask; i++) {
2415 if ((pring->prt[i].rctl == fch_r_ctl) &&
2416 (pring->prt[i].type == fch_type)) {
2417 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2418 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2419 (phba, pring, saveq);
2420 return 1;
2421 }
2422 }
2423 return 0;
2424}
James Smarte59058c2008-08-24 21:49:00 -04002425
2426/**
James Smart3621a712009-04-06 18:47:14 -04002427 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002428 * @phba: Pointer to HBA context object.
2429 * @pring: Pointer to driver SLI ring object.
2430 * @saveq: Pointer to the unsolicited iocb.
2431 *
2432 * This function is called with no lock held by the ring event handler
2433 * when there is an unsolicited iocb posted to the response ring by the
2434 * firmware. This function gets the buffer associated with the iocbs
2435 * and calls the event handler for the ring. This function handles both
2436 * qring buffers and hbq buffers.
2437 * When the function returns 1 the caller can free the iocb object otherwise
2438 * upper layer functions will free the iocb objects.
2439 **/
dea31012005-04-17 16:05:31 -05002440static int
2441lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2442 struct lpfc_iocbq *saveq)
2443{
2444 IOCB_t * irsp;
2445 WORD5 * w5p;
2446 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002447 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002448 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002449 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002450
2451 match = 0;
2452 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002453
2454 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2455 if (pring->lpfc_sli_rcv_async_status)
2456 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2457 else
2458 lpfc_printf_log(phba,
2459 KERN_WARNING,
2460 LOG_SLI,
2461 "0316 Ring %d handler: unexpected "
2462 "ASYNC_STATUS iocb received evt_code "
2463 "0x%x\n",
2464 pring->ringno,
2465 irsp->un.asyncstat.evt_code);
2466 return 1;
2467 }
2468
James Smart3163f722008-02-08 18:50:25 -05002469 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2470 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2471 if (irsp->ulpBdeCount > 0) {
2472 dmzbuf = lpfc_sli_get_buff(phba, pring,
2473 irsp->un.ulpWord[3]);
2474 lpfc_in_buf_free(phba, dmzbuf);
2475 }
2476
2477 if (irsp->ulpBdeCount > 1) {
2478 dmzbuf = lpfc_sli_get_buff(phba, pring,
2479 irsp->unsli3.sli3Words[3]);
2480 lpfc_in_buf_free(phba, dmzbuf);
2481 }
2482
2483 if (irsp->ulpBdeCount > 2) {
2484 dmzbuf = lpfc_sli_get_buff(phba, pring,
2485 irsp->unsli3.sli3Words[7]);
2486 lpfc_in_buf_free(phba, dmzbuf);
2487 }
2488
2489 return 1;
2490 }
2491
James Smart92d7f7b2007-06-17 19:56:38 -05002492 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002493 if (irsp->ulpBdeCount != 0) {
2494 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002495 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002496 if (!saveq->context2)
2497 lpfc_printf_log(phba,
2498 KERN_ERR,
2499 LOG_SLI,
2500 "0341 Ring %d Cannot find buffer for "
2501 "an unsolicited iocb. tag 0x%x\n",
2502 pring->ringno,
2503 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002504 }
2505 if (irsp->ulpBdeCount == 2) {
2506 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002507 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002508 if (!saveq->context3)
2509 lpfc_printf_log(phba,
2510 KERN_ERR,
2511 LOG_SLI,
2512 "0342 Ring %d Cannot find buffer for an"
2513 " unsolicited iocb. tag 0x%x\n",
2514 pring->ringno,
2515 irsp->unsli3.sli3Words[7]);
2516 }
2517 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002518 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002519 if (irsp->ulpBdeCount != 0) {
2520 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2521 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002522 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002523 lpfc_printf_log(phba,
2524 KERN_ERR,
2525 LOG_SLI,
2526 "0343 Ring %d Cannot find "
2527 "buffer for an unsolicited iocb"
2528 ". tag 0x%x\n", pring->ringno,
2529 irsp->un.ulpWord[3]);
2530 }
2531 if (irsp->ulpBdeCount == 2) {
2532 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2533 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002534 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002535 lpfc_printf_log(phba,
2536 KERN_ERR,
2537 LOG_SLI,
2538 "0344 Ring %d Cannot find "
2539 "buffer for an unsolicited "
2540 "iocb. tag 0x%x\n",
2541 pring->ringno,
2542 irsp->unsli3.sli3Words[7]);
2543 }
2544 }
James Smart92d7f7b2007-06-17 19:56:38 -05002545 }
James Smart9c2face2008-01-11 01:53:18 -05002546 if (irsp->ulpBdeCount != 0 &&
2547 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2548 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2549 int found = 0;
2550
2551 /* search continue save q for same XRI */
2552 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002553 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2554 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002555 list_add_tail(&saveq->list, &iocbq->list);
2556 found = 1;
2557 break;
2558 }
2559 }
2560 if (!found)
2561 list_add_tail(&saveq->clist,
2562 &pring->iocb_continue_saveq);
2563 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2564 list_del_init(&iocbq->clist);
2565 saveq = iocbq;
2566 irsp = &(saveq->iocb);
2567 } else
2568 return 0;
2569 }
2570 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2571 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2572 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002573 Rctl = FC_RCTL_ELS_REQ;
2574 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002575 } else {
2576 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2577 Rctl = w5p->hcsw.Rctl;
2578 Type = w5p->hcsw.Type;
2579
2580 /* Firmware Workaround */
2581 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2582 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2583 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002584 Rctl = FC_RCTL_ELS_REQ;
2585 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002586 w5p->hcsw.Rctl = Rctl;
2587 w5p->hcsw.Type = Type;
2588 }
2589 }
James Smart92d7f7b2007-06-17 19:56:38 -05002590
James Smart3772a992009-05-22 14:50:54 -04002591 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002592 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002593 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002594 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002595 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002596
James Smart92d7f7b2007-06-17 19:56:38 -05002597 return 1;
dea31012005-04-17 16:05:31 -05002598}
2599
James Smarte59058c2008-08-24 21:49:00 -04002600/**
James Smart3621a712009-04-06 18:47:14 -04002601 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002602 * @phba: Pointer to HBA context object.
2603 * @pring: Pointer to driver SLI ring object.
2604 * @prspiocb: Pointer to response iocb object.
2605 *
2606 * This function looks up the iocb_lookup table to get the command iocb
2607 * corresponding to the given response iocb using the iotag of the
2608 * response iocb. This function is called with the hbalock held.
2609 * This function returns the command iocb object if it finds the command
2610 * iocb else returns NULL.
2611 **/
dea31012005-04-17 16:05:31 -05002612static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002613lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2614 struct lpfc_sli_ring *pring,
2615 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002616{
dea31012005-04-17 16:05:31 -05002617 struct lpfc_iocbq *cmd_iocb = NULL;
2618 uint16_t iotag;
2619
James Bottomley604a3e32005-10-29 10:28:33 -05002620 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002621
James Bottomley604a3e32005-10-29 10:28:33 -05002622 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2623 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002624 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002625 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002626 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002627 }
James Bottomley604a3e32005-10-29 10:28:33 -05002628 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002629 }
2630
dea31012005-04-17 16:05:31 -05002631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002632 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002633 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002634 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002635 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002636 return NULL;
2637}
2638
James Smarte59058c2008-08-24 21:49:00 -04002639/**
James Smart3772a992009-05-22 14:50:54 -04002640 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2641 * @phba: Pointer to HBA context object.
2642 * @pring: Pointer to driver SLI ring object.
2643 * @iotag: IOCB tag.
2644 *
2645 * This function looks up the iocb_lookup table to get the command iocb
2646 * corresponding to the given iotag. This function is called with the
2647 * hbalock held.
2648 * This function returns the command iocb object if it finds the command
2649 * iocb else returns NULL.
2650 **/
2651static struct lpfc_iocbq *
2652lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2653 struct lpfc_sli_ring *pring, uint16_t iotag)
2654{
2655 struct lpfc_iocbq *cmd_iocb;
2656
2657 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2658 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002659 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2660 /* remove from txcmpl queue list */
2661 list_del_init(&cmd_iocb->list);
2662 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002663 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002664 }
James Smart3772a992009-05-22 14:50:54 -04002665 }
James Smart3772a992009-05-22 14:50:54 -04002666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2667 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2668 iotag, phba->sli.last_iotag);
2669 return NULL;
2670}
2671
2672/**
James Smart3621a712009-04-06 18:47:14 -04002673 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002674 * @phba: Pointer to HBA context object.
2675 * @pring: Pointer to driver SLI ring object.
2676 * @saveq: Pointer to the response iocb to be processed.
2677 *
2678 * This function is called by the ring event handler for non-fcp
2679 * rings when there is a new response iocb in the response ring.
2680 * The caller is not required to hold any locks. This function
2681 * gets the command iocb associated with the response iocb and
2682 * calls the completion handler for the command iocb. If there
2683 * is no completion handler, the function will free the resources
2684 * associated with command iocb. If the response iocb is for
2685 * an already aborted command iocb, the status of the completion
2686 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2687 * This function always returns 1.
2688 **/
dea31012005-04-17 16:05:31 -05002689static int
James Smart2e0fef82007-06-17 19:56:36 -05002690lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002691 struct lpfc_iocbq *saveq)
2692{
James Smart2e0fef82007-06-17 19:56:36 -05002693 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002694 int rc = 1;
2695 unsigned long iflag;
2696
2697 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002698 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002699 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002700 spin_unlock_irqrestore(&phba->hbalock, iflag);
2701
dea31012005-04-17 16:05:31 -05002702 if (cmdiocbp) {
2703 if (cmdiocbp->iocb_cmpl) {
2704 /*
James Smartea2151b2008-09-07 11:52:10 -04002705 * If an ELS command failed send an event to mgmt
2706 * application.
2707 */
2708 if (saveq->iocb.ulpStatus &&
2709 (pring->ringno == LPFC_ELS_RING) &&
2710 (cmdiocbp->iocb.ulpCommand ==
2711 CMD_ELS_REQUEST64_CR))
2712 lpfc_send_els_failure_event(phba,
2713 cmdiocbp, saveq);
2714
2715 /*
dea31012005-04-17 16:05:31 -05002716 * Post all ELS completions to the worker thread.
2717 * All other are passed to the completion callback.
2718 */
2719 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002720 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2721 (cmdiocbp->iocb_flag &
2722 LPFC_DRIVER_ABORTED)) {
2723 spin_lock_irqsave(&phba->hbalock,
2724 iflag);
James Smart07951072007-04-25 09:51:38 -04002725 cmdiocbp->iocb_flag &=
2726 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002727 spin_unlock_irqrestore(&phba->hbalock,
2728 iflag);
James Smart07951072007-04-25 09:51:38 -04002729 saveq->iocb.ulpStatus =
2730 IOSTAT_LOCAL_REJECT;
2731 saveq->iocb.un.ulpWord[4] =
2732 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002733
2734 /* Firmware could still be in progress
2735 * of DMAing payload, so don't free data
2736 * buffer till after a hbeat.
2737 */
James Smart341af102010-01-26 23:07:37 -05002738 spin_lock_irqsave(&phba->hbalock,
2739 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002740 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002741 spin_unlock_irqrestore(&phba->hbalock,
2742 iflag);
2743 }
James Smart0f65ff62010-02-26 14:14:23 -05002744 if (phba->sli_rev == LPFC_SLI_REV4) {
2745 if (saveq->iocb_flag &
2746 LPFC_EXCHANGE_BUSY) {
2747 /* Set cmdiocb flag for the
2748 * exchange busy so sgl (xri)
2749 * will not be released until
2750 * the abort xri is received
2751 * from hba.
2752 */
2753 spin_lock_irqsave(
2754 &phba->hbalock, iflag);
2755 cmdiocbp->iocb_flag |=
2756 LPFC_EXCHANGE_BUSY;
2757 spin_unlock_irqrestore(
2758 &phba->hbalock, iflag);
2759 }
2760 if (cmdiocbp->iocb_flag &
2761 LPFC_DRIVER_ABORTED) {
2762 /*
2763 * Clear LPFC_DRIVER_ABORTED
2764 * bit in case it was driver
2765 * initiated abort.
2766 */
2767 spin_lock_irqsave(
2768 &phba->hbalock, iflag);
2769 cmdiocbp->iocb_flag &=
2770 ~LPFC_DRIVER_ABORTED;
2771 spin_unlock_irqrestore(
2772 &phba->hbalock, iflag);
2773 cmdiocbp->iocb.ulpStatus =
2774 IOSTAT_LOCAL_REJECT;
2775 cmdiocbp->iocb.un.ulpWord[4] =
2776 IOERR_ABORT_REQUESTED;
2777 /*
2778 * For SLI4, irsiocb contains
2779 * NO_XRI in sli_xritag, it
2780 * shall not affect releasing
2781 * sgl (xri) process.
2782 */
2783 saveq->iocb.ulpStatus =
2784 IOSTAT_LOCAL_REJECT;
2785 saveq->iocb.un.ulpWord[4] =
2786 IOERR_SLI_ABORTED;
2787 spin_lock_irqsave(
2788 &phba->hbalock, iflag);
2789 saveq->iocb_flag |=
2790 LPFC_DELAY_MEM_FREE;
2791 spin_unlock_irqrestore(
2792 &phba->hbalock, iflag);
2793 }
James Smart07951072007-04-25 09:51:38 -04002794 }
dea31012005-04-17 16:05:31 -05002795 }
James Smart2e0fef82007-06-17 19:56:36 -05002796 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002797 } else
2798 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002799 } else {
2800 /*
2801 * Unknown initiating command based on the response iotag.
2802 * This could be the case on the ELS ring because of
2803 * lpfc_els_abort().
2804 */
2805 if (pring->ringno != LPFC_ELS_RING) {
2806 /*
2807 * Ring <ringno> handler: unexpected completion IoTag
2808 * <IoTag>
2809 */
James Smarta257bf92009-04-06 18:48:10 -04002810 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002811 "0322 Ring %d handler: "
2812 "unexpected completion IoTag x%x "
2813 "Data: x%x x%x x%x x%x\n",
2814 pring->ringno,
2815 saveq->iocb.ulpIoTag,
2816 saveq->iocb.ulpStatus,
2817 saveq->iocb.un.ulpWord[4],
2818 saveq->iocb.ulpCommand,
2819 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002820 }
2821 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002822
dea31012005-04-17 16:05:31 -05002823 return rc;
2824}
2825
James Smarte59058c2008-08-24 21:49:00 -04002826/**
James Smart3621a712009-04-06 18:47:14 -04002827 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002828 * @phba: Pointer to HBA context object.
2829 * @pring: Pointer to driver SLI ring object.
2830 *
2831 * This function is called from the iocb ring event handlers when
2832 * put pointer is ahead of the get pointer for a ring. This function signal
2833 * an error attention condition to the worker thread and the worker
2834 * thread will transition the HBA to offline state.
2835 **/
James Smart2e0fef82007-06-17 19:56:36 -05002836static void
2837lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002838{
James Smart34b02dc2008-08-24 21:49:55 -04002839 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002840 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002841 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002842 * rsp ring <portRspMax>
2843 */
2844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002845 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002846 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002847 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002848 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002849
James Smart2e0fef82007-06-17 19:56:36 -05002850 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002851
2852 /*
2853 * All error attention handlers are posted to
2854 * worker thread
2855 */
2856 phba->work_ha |= HA_ERATT;
2857 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002858
James Smart5e9d9b82008-06-14 22:52:53 -04002859 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002860
2861 return;
2862}
2863
James Smarte59058c2008-08-24 21:49:00 -04002864/**
James Smart3621a712009-04-06 18:47:14 -04002865 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002866 * @ptr: Pointer to address of HBA context object.
2867 *
2868 * This function is invoked by the Error Attention polling timer when the
2869 * timer times out. It will check the SLI Error Attention register for
2870 * possible attention events. If so, it will post an Error Attention event
2871 * and wake up worker thread to process it. Otherwise, it will set up the
2872 * Error Attention polling timer for the next poll.
2873 **/
2874void lpfc_poll_eratt(unsigned long ptr)
2875{
2876 struct lpfc_hba *phba;
James Smartaa6fbb72012-08-03 12:36:03 -04002877 uint32_t eratt = 0, rem;
2878 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002879
2880 phba = (struct lpfc_hba *)ptr;
2881
James Smartaa6fbb72012-08-03 12:36:03 -04002882 /* Here we will also keep track of interrupts per sec of the hba */
2883 sli_intr = phba->sli.slistat.sli_intr;
2884
2885 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2886 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2887 sli_intr);
2888 else
2889 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2890
2891 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2892 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2893 phba->sli.slistat.sli_ips = cnt;
2894
2895 phba->sli.slistat.sli_prev_intr = sli_intr;
2896
James Smart93996272008-08-24 21:50:30 -04002897 /* Check chip HA register for error event */
2898 eratt = lpfc_sli_check_eratt(phba);
2899
2900 if (eratt)
2901 /* Tell the worker thread there is work to do */
2902 lpfc_worker_wake_up(phba);
2903 else
2904 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002905 mod_timer(&phba->eratt_poll,
2906 jiffies +
2907 msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smart93996272008-08-24 21:50:30 -04002908 return;
2909}
2910
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002911
James Smarte59058c2008-08-24 21:49:00 -04002912/**
James Smart3621a712009-04-06 18:47:14 -04002913 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002914 * @phba: Pointer to HBA context object.
2915 * @pring: Pointer to driver SLI ring object.
2916 * @mask: Host attention register mask for this ring.
2917 *
2918 * This function is called from the interrupt context when there is a ring
2919 * event for the fcp ring. The caller does not hold any lock.
2920 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002921 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002922 * LE bit set. The function will call the completion handler of the command iocb
2923 * if the response iocb indicates a completion for a command iocb or it is
2924 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2925 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002926 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002927 * to check it explicitly.
2928 */
2929int
James Smart2e0fef82007-06-17 19:56:36 -05002930lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2931 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002932{
James Smart34b02dc2008-08-24 21:49:55 -04002933 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002934 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002935 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002936 struct lpfc_iocbq *cmdiocbq = NULL;
2937 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002938 uint32_t status;
2939 uint32_t portRspPut, portRspMax;
2940 int rc = 1;
2941 lpfc_iocb_type type;
2942 unsigned long iflag;
2943 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002944
James Smart2e0fef82007-06-17 19:56:36 -05002945 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002946 pring->stats.iocb_event++;
2947
dea31012005-04-17 16:05:31 -05002948 /*
2949 * The next available response entry should never exceed the maximum
2950 * entries. If it does, treat it as an adapter hardware error.
2951 */
James Smart7e56aa22012-08-03 12:35:34 -04002952 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002953 portRspPut = le32_to_cpu(pgp->rspPutInx);
2954 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002955 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002956 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002957 return 1;
2958 }
James Smart45ed1192009-10-02 15:17:02 -04002959 if (phba->fcp_ring_in_use) {
2960 spin_unlock_irqrestore(&phba->hbalock, iflag);
2961 return 1;
2962 } else
2963 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002964
2965 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002966 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002967 /*
2968 * Fetch an entry off the ring and copy it into a local data
2969 * structure. The copy involves a byte-swap since the
2970 * network byte order and pci byte orders are different.
2971 */
James Smarted957682007-06-17 19:56:37 -05002972 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002973 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002974
James Smart7e56aa22012-08-03 12:35:34 -04002975 if (++pring->sli.sli3.rspidx >= portRspMax)
2976 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002977
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002978 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2979 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002980 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002981 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002982 irsp = &rspiocbq.iocb;
2983
dea31012005-04-17 16:05:31 -05002984 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2985 pring->stats.iocb_rsp++;
2986 rsp_cmpl++;
2987
2988 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002989 /*
2990 * If resource errors reported from HBA, reduce
2991 * queuedepths of the SCSI device.
2992 */
2993 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04002994 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
2995 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002996 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002997 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002998 spin_lock_irqsave(&phba->hbalock, iflag);
2999 }
3000
dea31012005-04-17 16:05:31 -05003001 /* Rsp ring <ringno> error: IOCB */
3002 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003003 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003004 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003005 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003006 irsp->un.ulpWord[0],
3007 irsp->un.ulpWord[1],
3008 irsp->un.ulpWord[2],
3009 irsp->un.ulpWord[3],
3010 irsp->un.ulpWord[4],
3011 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003012 *(uint32_t *)&irsp->un1,
3013 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003014 }
3015
3016 switch (type) {
3017 case LPFC_ABORT_IOCB:
3018 case LPFC_SOL_IOCB:
3019 /*
3020 * Idle exchange closed via ABTS from port. No iocb
3021 * resources need to be recovered.
3022 */
3023 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003024 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003025 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003026 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003027 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003028 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003029 break;
3030 }
3031
James Bottomley604a3e32005-10-29 10:28:33 -05003032 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3033 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003034 if (unlikely(!cmdiocbq))
3035 break;
3036 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3037 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3038 if (cmdiocbq->iocb_cmpl) {
3039 spin_unlock_irqrestore(&phba->hbalock, iflag);
3040 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3041 &rspiocbq);
3042 spin_lock_irqsave(&phba->hbalock, iflag);
3043 }
dea31012005-04-17 16:05:31 -05003044 break;
James Smarta4bc3372006-12-02 13:34:16 -05003045 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003046 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003047 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003048 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003049 break;
dea31012005-04-17 16:05:31 -05003050 default:
3051 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3052 char adaptermsg[LPFC_MAX_ADPTMSG];
3053 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3054 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3055 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003056 dev_warn(&((phba->pcidev)->dev),
3057 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003058 phba->brd_no, adaptermsg);
3059 } else {
3060 /* Unknown IOCB command */
3061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003062 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003063 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003064 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003065 irsp->ulpStatus,
3066 irsp->ulpIoTag,
3067 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003068 }
3069 break;
3070 }
3071
3072 /*
3073 * The response IOCB has been processed. Update the ring
3074 * pointer in SLIM. If the port response put pointer has not
3075 * been updated, sync the pgp->rspPutInx and fetch the new port
3076 * response put pointer.
3077 */
James Smart7e56aa22012-08-03 12:35:34 -04003078 writel(pring->sli.sli3.rspidx,
3079 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003080
James Smart7e56aa22012-08-03 12:35:34 -04003081 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003082 portRspPut = le32_to_cpu(pgp->rspPutInx);
3083 }
3084
3085 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3086 pring->stats.iocb_rsp_full++;
3087 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3088 writel(status, phba->CAregaddr);
3089 readl(phba->CAregaddr);
3090 }
3091 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3092 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3093 pring->stats.iocb_cmd_empty++;
3094
3095 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003096 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003097 lpfc_sli_resume_iocb(phba, pring);
3098
3099 if ((pring->lpfc_sli_cmd_available))
3100 (pring->lpfc_sli_cmd_available) (phba, pring);
3101
3102 }
3103
James Smart45ed1192009-10-02 15:17:02 -04003104 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003105 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003106 return rc;
3107}
3108
James Smarte59058c2008-08-24 21:49:00 -04003109/**
James Smart3772a992009-05-22 14:50:54 -04003110 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3111 * @phba: Pointer to HBA context object.
3112 * @pring: Pointer to driver SLI ring object.
3113 * @rspiocbp: Pointer to driver response IOCB object.
3114 *
3115 * This function is called from the worker thread when there is a slow-path
3116 * response IOCB to process. This function chains all the response iocbs until
3117 * seeing the iocb with the LE bit set. The function will call
3118 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3119 * completion of a command iocb. The function will call the
3120 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3121 * The function frees the resources or calls the completion handler if this
3122 * iocb is an abort completion. The function returns NULL when the response
3123 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3124 * this function shall chain the iocb on to the iocb_continueq and return the
3125 * response iocb passed in.
3126 **/
3127static struct lpfc_iocbq *
3128lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3129 struct lpfc_iocbq *rspiocbp)
3130{
3131 struct lpfc_iocbq *saveq;
3132 struct lpfc_iocbq *cmdiocbp;
3133 struct lpfc_iocbq *next_iocb;
3134 IOCB_t *irsp = NULL;
3135 uint32_t free_saveq;
3136 uint8_t iocb_cmd_type;
3137 lpfc_iocb_type type;
3138 unsigned long iflag;
3139 int rc;
3140
3141 spin_lock_irqsave(&phba->hbalock, iflag);
3142 /* First add the response iocb to the countinueq list */
3143 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3144 pring->iocb_continueq_cnt++;
3145
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003146 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003147 irsp = &rspiocbp->iocb;
3148 if (irsp->ulpLe) {
3149 /*
3150 * By default, the driver expects to free all resources
3151 * associated with this iocb completion.
3152 */
3153 free_saveq = 1;
3154 saveq = list_get_first(&pring->iocb_continueq,
3155 struct lpfc_iocbq, list);
3156 irsp = &(saveq->iocb);
3157 list_del_init(&pring->iocb_continueq);
3158 pring->iocb_continueq_cnt = 0;
3159
3160 pring->stats.iocb_rsp++;
3161
3162 /*
3163 * If resource errors reported from HBA, reduce
3164 * queuedepths of the SCSI device.
3165 */
3166 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003167 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3168 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003169 spin_unlock_irqrestore(&phba->hbalock, iflag);
3170 phba->lpfc_rampdown_queue_depth(phba);
3171 spin_lock_irqsave(&phba->hbalock, iflag);
3172 }
3173
3174 if (irsp->ulpStatus) {
3175 /* Rsp ring <ringno> error: IOCB */
3176 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3177 "0328 Rsp Ring %d error: "
3178 "IOCB Data: "
3179 "x%x x%x x%x x%x "
3180 "x%x x%x x%x x%x "
3181 "x%x x%x x%x x%x "
3182 "x%x x%x x%x x%x\n",
3183 pring->ringno,
3184 irsp->un.ulpWord[0],
3185 irsp->un.ulpWord[1],
3186 irsp->un.ulpWord[2],
3187 irsp->un.ulpWord[3],
3188 irsp->un.ulpWord[4],
3189 irsp->un.ulpWord[5],
3190 *(((uint32_t *) irsp) + 6),
3191 *(((uint32_t *) irsp) + 7),
3192 *(((uint32_t *) irsp) + 8),
3193 *(((uint32_t *) irsp) + 9),
3194 *(((uint32_t *) irsp) + 10),
3195 *(((uint32_t *) irsp) + 11),
3196 *(((uint32_t *) irsp) + 12),
3197 *(((uint32_t *) irsp) + 13),
3198 *(((uint32_t *) irsp) + 14),
3199 *(((uint32_t *) irsp) + 15));
3200 }
3201
3202 /*
3203 * Fetch the IOCB command type and call the correct completion
3204 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3205 * get freed back to the lpfc_iocb_list by the discovery
3206 * kernel thread.
3207 */
3208 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3209 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3210 switch (type) {
3211 case LPFC_SOL_IOCB:
3212 spin_unlock_irqrestore(&phba->hbalock, iflag);
3213 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3214 spin_lock_irqsave(&phba->hbalock, iflag);
3215 break;
3216
3217 case LPFC_UNSOL_IOCB:
3218 spin_unlock_irqrestore(&phba->hbalock, iflag);
3219 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3220 spin_lock_irqsave(&phba->hbalock, iflag);
3221 if (!rc)
3222 free_saveq = 0;
3223 break;
3224
3225 case LPFC_ABORT_IOCB:
3226 cmdiocbp = NULL;
3227 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3228 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3229 saveq);
3230 if (cmdiocbp) {
3231 /* Call the specified completion routine */
3232 if (cmdiocbp->iocb_cmpl) {
3233 spin_unlock_irqrestore(&phba->hbalock,
3234 iflag);
3235 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3236 saveq);
3237 spin_lock_irqsave(&phba->hbalock,
3238 iflag);
3239 } else
3240 __lpfc_sli_release_iocbq(phba,
3241 cmdiocbp);
3242 }
3243 break;
3244
3245 case LPFC_UNKNOWN_IOCB:
3246 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3247 char adaptermsg[LPFC_MAX_ADPTMSG];
3248 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3249 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3250 MAX_MSG_DATA);
3251 dev_warn(&((phba->pcidev)->dev),
3252 "lpfc%d: %s\n",
3253 phba->brd_no, adaptermsg);
3254 } else {
3255 /* Unknown IOCB command */
3256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3257 "0335 Unknown IOCB "
3258 "command Data: x%x "
3259 "x%x x%x x%x\n",
3260 irsp->ulpCommand,
3261 irsp->ulpStatus,
3262 irsp->ulpIoTag,
3263 irsp->ulpContext);
3264 }
3265 break;
3266 }
3267
3268 if (free_saveq) {
3269 list_for_each_entry_safe(rspiocbp, next_iocb,
3270 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003271 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003272 __lpfc_sli_release_iocbq(phba, rspiocbp);
3273 }
3274 __lpfc_sli_release_iocbq(phba, saveq);
3275 }
3276 rspiocbp = NULL;
3277 }
3278 spin_unlock_irqrestore(&phba->hbalock, iflag);
3279 return rspiocbp;
3280}
3281
3282/**
3283 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003284 * @phba: Pointer to HBA context object.
3285 * @pring: Pointer to driver SLI ring object.
3286 * @mask: Host attention register mask for this ring.
3287 *
James Smart3772a992009-05-22 14:50:54 -04003288 * This routine wraps the actual slow_ring event process routine from the
3289 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003290 **/
James Smart3772a992009-05-22 14:50:54 -04003291void
James Smart2e0fef82007-06-17 19:56:36 -05003292lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3293 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003294{
James Smart3772a992009-05-22 14:50:54 -04003295 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3296}
3297
3298/**
3299 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3300 * @phba: Pointer to HBA context object.
3301 * @pring: Pointer to driver SLI ring object.
3302 * @mask: Host attention register mask for this ring.
3303 *
3304 * This function is called from the worker thread when there is a ring event
3305 * for non-fcp rings. The caller does not hold any lock. The function will
3306 * remove each response iocb in the response ring and calls the handle
3307 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3308 **/
3309static void
3310lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3311 struct lpfc_sli_ring *pring, uint32_t mask)
3312{
James Smart34b02dc2008-08-24 21:49:55 -04003313 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003314 IOCB_t *entry;
3315 IOCB_t *irsp = NULL;
3316 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003317 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003318 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003319 uint32_t status;
dea31012005-04-17 16:05:31 -05003320
James Smart34b02dc2008-08-24 21:49:55 -04003321 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003322 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003323 pring->stats.iocb_event++;
3324
dea31012005-04-17 16:05:31 -05003325 /*
3326 * The next available response entry should never exceed the maximum
3327 * entries. If it does, treat it as an adapter hardware error.
3328 */
James Smart7e56aa22012-08-03 12:35:34 -04003329 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003330 portRspPut = le32_to_cpu(pgp->rspPutInx);
3331 if (portRspPut >= portRspMax) {
3332 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003333 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003334 * rsp ring <portRspMax>
3335 */
James Smarted957682007-06-17 19:56:37 -05003336 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003337 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003338 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003339 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003340
James Smart2e0fef82007-06-17 19:56:36 -05003341 phba->link_state = LPFC_HBA_ERROR;
3342 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003343
3344 phba->work_hs = HS_FFER3;
3345 lpfc_handle_eratt(phba);
3346
James Smart3772a992009-05-22 14:50:54 -04003347 return;
dea31012005-04-17 16:05:31 -05003348 }
3349
3350 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003351 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003352 /*
3353 * Build a completion list and call the appropriate handler.
3354 * The process is to get the next available response iocb, get
3355 * a free iocb from the list, copy the response data into the
3356 * free iocb, insert to the continuation list, and update the
3357 * next response index to slim. This process makes response
3358 * iocb's in the ring available to DMA as fast as possible but
3359 * pays a penalty for a copy operation. Since the iocb is
3360 * only 32 bytes, this penalty is considered small relative to
3361 * the PCI reads for register values and a slim write. When
3362 * the ulpLe field is set, the entire Command has been
3363 * received.
3364 */
James Smarted957682007-06-17 19:56:37 -05003365 entry = lpfc_resp_iocb(phba, pring);
3366
James Smart858c9f62007-06-17 19:56:39 -05003367 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003368 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003369 if (rspiocbp == NULL) {
3370 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003371 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003372 break;
3373 }
3374
James Smarted957682007-06-17 19:56:37 -05003375 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3376 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003377 irsp = &rspiocbp->iocb;
3378
James Smart7e56aa22012-08-03 12:35:34 -04003379 if (++pring->sli.sli3.rspidx >= portRspMax)
3380 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003381
James Smarta58cbd52007-08-02 11:09:43 -04003382 if (pring->ringno == LPFC_ELS_RING) {
3383 lpfc_debugfs_slow_ring_trc(phba,
3384 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3385 *(((uint32_t *) irsp) + 4),
3386 *(((uint32_t *) irsp) + 6),
3387 *(((uint32_t *) irsp) + 7));
3388 }
3389
James Smart7e56aa22012-08-03 12:35:34 -04003390 writel(pring->sli.sli3.rspidx,
3391 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003392
James Smart3772a992009-05-22 14:50:54 -04003393 spin_unlock_irqrestore(&phba->hbalock, iflag);
3394 /* Handle the response IOCB */
3395 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3396 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003397
3398 /*
3399 * If the port response put pointer has not been updated, sync
3400 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3401 * response put pointer.
3402 */
James Smart7e56aa22012-08-03 12:35:34 -04003403 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003404 portRspPut = le32_to_cpu(pgp->rspPutInx);
3405 }
James Smart7e56aa22012-08-03 12:35:34 -04003406 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003407
James Smart92d7f7b2007-06-17 19:56:38 -05003408 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003409 /* At least one response entry has been freed */
3410 pring->stats.iocb_rsp_full++;
3411 /* SET RxRE_RSP in Chip Att register */
3412 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3413 writel(status, phba->CAregaddr);
3414 readl(phba->CAregaddr); /* flush */
3415 }
3416 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3417 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3418 pring->stats.iocb_cmd_empty++;
3419
3420 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003421 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003422 lpfc_sli_resume_iocb(phba, pring);
3423
3424 if ((pring->lpfc_sli_cmd_available))
3425 (pring->lpfc_sli_cmd_available) (phba, pring);
3426
3427 }
3428
James Smart2e0fef82007-06-17 19:56:36 -05003429 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003430 return;
dea31012005-04-17 16:05:31 -05003431}
3432
James Smarte59058c2008-08-24 21:49:00 -04003433/**
James Smart4f774512009-05-22 14:52:35 -04003434 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3435 * @phba: Pointer to HBA context object.
3436 * @pring: Pointer to driver SLI ring object.
3437 * @mask: Host attention register mask for this ring.
3438 *
3439 * This function is called from the worker thread when there is a pending
3440 * ELS response iocb on the driver internal slow-path response iocb worker
3441 * queue. The caller does not hold any lock. The function will remove each
3442 * response iocb from the response worker queue and calls the handle
3443 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3444 **/
3445static void
3446lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3447 struct lpfc_sli_ring *pring, uint32_t mask)
3448{
3449 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003450 struct hbq_dmabuf *dmabuf;
3451 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003452 unsigned long iflag;
3453
James Smart45ed1192009-10-02 15:17:02 -04003454 spin_lock_irqsave(&phba->hbalock, iflag);
3455 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3456 spin_unlock_irqrestore(&phba->hbalock, iflag);
3457 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003458 /* Get the response iocb from the head of work queue */
3459 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003460 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003461 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003462 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003463
3464 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3465 case CQE_CODE_COMPL_WQE:
3466 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3467 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003468 /* Translate ELS WCQE to response IOCBQ */
3469 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3470 irspiocbq);
3471 if (irspiocbq)
3472 lpfc_sli_sp_handle_rspiocb(phba, pring,
3473 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003474 break;
3475 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003476 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003477 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3478 cq_event);
3479 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3480 break;
3481 default:
3482 break;
3483 }
James Smart4f774512009-05-22 14:52:35 -04003484 }
3485}
3486
3487/**
James Smart3621a712009-04-06 18:47:14 -04003488 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003489 * @phba: Pointer to HBA context object.
3490 * @pring: Pointer to driver SLI ring object.
3491 *
3492 * This function aborts all iocbs in the given ring and frees all the iocb
3493 * objects in txq. This function issues an abort iocb for all the iocb commands
3494 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3495 * the return of this function. The caller is not required to hold any locks.
3496 **/
James Smart2e0fef82007-06-17 19:56:36 -05003497void
dea31012005-04-17 16:05:31 -05003498lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3499{
James Smart2534ba72007-04-25 09:52:20 -04003500 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003501 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003502
James Smart92d7f7b2007-06-17 19:56:38 -05003503 if (pring->ringno == LPFC_ELS_RING) {
3504 lpfc_fabric_abort_hba(phba);
3505 }
3506
dea31012005-04-17 16:05:31 -05003507 /* Error everything on txq and txcmplq
3508 * First do the txq.
3509 */
James Smart2e0fef82007-06-17 19:56:36 -05003510 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003511 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003512
3513 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003514 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3515 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3516
James Smart2e0fef82007-06-17 19:56:36 -05003517 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003518
James Smarta257bf92009-04-06 18:48:10 -04003519 /* Cancel all the IOCBs from the completions list */
3520 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3521 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003522}
3523
James Smarte59058c2008-08-24 21:49:00 -04003524/**
James Smart3621a712009-04-06 18:47:14 -04003525 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003526 * @phba: Pointer to HBA context object.
3527 *
3528 * This function flushes all iocbs in the fcp ring and frees all the iocb
3529 * objects in txq and txcmplq. This function will not issue abort iocbs
3530 * for all the iocb commands in txcmplq, they will just be returned with
3531 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3532 * slot has been permanently disabled.
3533 **/
3534void
3535lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3536{
3537 LIST_HEAD(txq);
3538 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003539 struct lpfc_sli *psli = &phba->sli;
3540 struct lpfc_sli_ring *pring;
3541
3542 /* Currently, only one fcp ring */
3543 pring = &psli->ring[psli->fcp_ring];
3544
3545 spin_lock_irq(&phba->hbalock);
3546 /* Retrieve everything on txq */
3547 list_splice_init(&pring->txq, &txq);
James Smarta8e497d2008-08-24 21:50:11 -04003548
3549 /* Retrieve everything on the txcmplq */
3550 list_splice_init(&pring->txcmplq, &txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04003551
3552 /* Indicate the I/O queues are flushed */
3553 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003554 spin_unlock_irq(&phba->hbalock);
3555
3556 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003557 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3558 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003559
3560 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003561 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3562 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003563}
3564
3565/**
James Smart3772a992009-05-22 14:50:54 -04003566 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003567 * @phba: Pointer to HBA context object.
3568 * @mask: Bit mask to be checked.
3569 *
3570 * This function reads the host status register and compares
3571 * with the provided bit mask to check if HBA completed
3572 * the restart. This function will wait in a loop for the
3573 * HBA to complete restart. If the HBA does not restart within
3574 * 15 iterations, the function will reset the HBA again. The
3575 * function returns 1 when HBA fail to restart otherwise returns
3576 * zero.
3577 **/
James Smart3772a992009-05-22 14:50:54 -04003578static int
3579lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003580{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003581 uint32_t status;
3582 int i = 0;
3583 int retval = 0;
dea31012005-04-17 16:05:31 -05003584
Jamie Wellnitz41415862006-02-28 19:25:27 -05003585 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003586 if (lpfc_readl(phba->HSregaddr, &status))
3587 return 1;
dea31012005-04-17 16:05:31 -05003588
Jamie Wellnitz41415862006-02-28 19:25:27 -05003589 /*
3590 * Check status register every 100ms for 5 retries, then every
3591 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3592 * every 2.5 sec for 4.
3593 * Break our of the loop if errors occurred during init.
3594 */
3595 while (((status & mask) != mask) &&
3596 !(status & HS_FFERM) &&
3597 i++ < 20) {
dea31012005-04-17 16:05:31 -05003598
Jamie Wellnitz41415862006-02-28 19:25:27 -05003599 if (i <= 5)
3600 msleep(10);
3601 else if (i <= 10)
3602 msleep(500);
3603 else
3604 msleep(2500);
dea31012005-04-17 16:05:31 -05003605
Jamie Wellnitz41415862006-02-28 19:25:27 -05003606 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003607 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003608 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003609 lpfc_sli_brdrestart(phba);
3610 }
3611 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003612 if (lpfc_readl(phba->HSregaddr, &status)) {
3613 retval = 1;
3614 break;
3615 }
dea31012005-04-17 16:05:31 -05003616 }
dea31012005-04-17 16:05:31 -05003617
Jamie Wellnitz41415862006-02-28 19:25:27 -05003618 /* Check to see if any errors occurred during init */
3619 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3621 "2751 Adapter failed to restart, "
3622 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3623 status,
3624 readl(phba->MBslimaddr + 0xa8),
3625 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003626 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003627 retval = 1;
3628 }
dea31012005-04-17 16:05:31 -05003629
Jamie Wellnitz41415862006-02-28 19:25:27 -05003630 return retval;
dea31012005-04-17 16:05:31 -05003631}
3632
James Smartda0436e2009-05-22 14:51:39 -04003633/**
3634 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3635 * @phba: Pointer to HBA context object.
3636 * @mask: Bit mask to be checked.
3637 *
3638 * This function checks the host status register to check if HBA is
3639 * ready. This function will wait in a loop for the HBA to be ready
3640 * If the HBA is not ready , the function will will reset the HBA PCI
3641 * function again. The function returns 1 when HBA fail to be ready
3642 * otherwise returns zero.
3643 **/
3644static int
3645lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3646{
3647 uint32_t status;
3648 int retval = 0;
3649
3650 /* Read the HBA Host Status Register */
3651 status = lpfc_sli4_post_status_check(phba);
3652
3653 if (status) {
3654 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3655 lpfc_sli_brdrestart(phba);
3656 status = lpfc_sli4_post_status_check(phba);
3657 }
3658
3659 /* Check to see if any errors occurred during init */
3660 if (status) {
3661 phba->link_state = LPFC_HBA_ERROR;
3662 retval = 1;
3663 } else
3664 phba->sli4_hba.intr_enable = 0;
3665
3666 return retval;
3667}
3668
3669/**
3670 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3671 * @phba: Pointer to HBA context object.
3672 * @mask: Bit mask to be checked.
3673 *
3674 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3675 * from the API jump table function pointer from the lpfc_hba struct.
3676 **/
3677int
3678lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3679{
3680 return phba->lpfc_sli_brdready(phba, mask);
3681}
3682
James Smart92908312006-03-07 15:04:13 -05003683#define BARRIER_TEST_PATTERN (0xdeadbeef)
3684
James Smarte59058c2008-08-24 21:49:00 -04003685/**
James Smart3621a712009-04-06 18:47:14 -04003686 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003687 * @phba: Pointer to HBA context object.
3688 *
James Smart1b511972011-12-13 13:23:09 -05003689 * This function is called before resetting an HBA. This function is called
3690 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003691 **/
James Smart2e0fef82007-06-17 19:56:36 -05003692void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003693{
James Smart65a29c12006-07-06 15:50:50 -04003694 uint32_t __iomem *resp_buf;
3695 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003696 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003697 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003698 int i;
3699 uint8_t hdrtype;
3700
3701 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3702 if (hdrtype != 0x80 ||
3703 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3704 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3705 return;
3706
3707 /*
3708 * Tell the other part of the chip to suspend temporarily all
3709 * its DMA activity.
3710 */
James Smart65a29c12006-07-06 15:50:50 -04003711 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003712
3713 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003714 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3715 return;
James Smart92908312006-03-07 15:04:13 -05003716 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3717 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003718 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003719
James Smart9940b972011-03-11 16:06:12 -05003720 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3721 return;
3722 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003723 /* Clear Chip error bit */
3724 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003725 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003726 }
3727
3728 mbox = 0;
3729 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3730 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3731
3732 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003733 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003734 writel(mbox, mbox_buf);
3735
James Smart9940b972011-03-11 16:06:12 -05003736 for (i = 0; i < 50; i++) {
3737 if (lpfc_readl((resp_buf + 1), &resp_data))
3738 return;
3739 if (resp_data != ~(BARRIER_TEST_PATTERN))
3740 mdelay(1);
3741 else
3742 break;
3743 }
3744 resp_data = 0;
3745 if (lpfc_readl((resp_buf + 1), &resp_data))
3746 return;
3747 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003748 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003749 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003750 goto restore_hc;
3751 else
3752 goto clear_errat;
3753 }
3754
3755 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003756 resp_data = 0;
3757 for (i = 0; i < 500; i++) {
3758 if (lpfc_readl(resp_buf, &resp_data))
3759 return;
3760 if (resp_data != mbox)
3761 mdelay(1);
3762 else
3763 break;
3764 }
James Smart92908312006-03-07 15:04:13 -05003765
3766clear_errat:
3767
James Smart9940b972011-03-11 16:06:12 -05003768 while (++i < 500) {
3769 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3770 return;
3771 if (!(ha_copy & HA_ERATT))
3772 mdelay(1);
3773 else
3774 break;
3775 }
James Smart92908312006-03-07 15:04:13 -05003776
3777 if (readl(phba->HAregaddr) & HA_ERATT) {
3778 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003779 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003780 }
3781
3782restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003783 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003784 writel(hc_copy, phba->HCregaddr);
3785 readl(phba->HCregaddr); /* flush */
3786}
3787
James Smarte59058c2008-08-24 21:49:00 -04003788/**
James Smart3621a712009-04-06 18:47:14 -04003789 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003790 * @phba: Pointer to HBA context object.
3791 *
3792 * This function issues a kill_board mailbox command and waits for
3793 * the error attention interrupt. This function is called for stopping
3794 * the firmware processing. The caller is not required to hold any
3795 * locks. This function calls lpfc_hba_down_post function to free
3796 * any pending commands after the kill. The function will return 1 when it
3797 * fails to kill the board else will return 0.
3798 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003799int
James Smart2e0fef82007-06-17 19:56:36 -05003800lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003801{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003802 struct lpfc_sli *psli;
3803 LPFC_MBOXQ_t *pmb;
3804 uint32_t status;
3805 uint32_t ha_copy;
3806 int retval;
3807 int i = 0;
3808
3809 psli = &phba->sli;
3810
3811 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003812 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003813 "0329 Kill HBA Data: x%x x%x\n",
3814 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003815
James Smart98c9ea52007-10-27 13:37:33 -04003816 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3817 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003818 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003819
3820 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003821 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003822 if (lpfc_readl(phba->HCregaddr, &status)) {
3823 spin_unlock_irq(&phba->hbalock);
3824 mempool_free(pmb, phba->mbox_mem_pool);
3825 return 1;
3826 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003827 status &= ~HC_ERINT_ENA;
3828 writel(status, phba->HCregaddr);
3829 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003830 phba->link_flag |= LS_IGNORE_ERATT;
3831 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003832
3833 lpfc_kill_board(phba, pmb);
3834 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3835 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3836
3837 if (retval != MBX_SUCCESS) {
3838 if (retval != MBX_BUSY)
3839 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003840 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3841 "2752 KILL_BOARD command failed retval %d\n",
3842 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003843 spin_lock_irq(&phba->hbalock);
3844 phba->link_flag &= ~LS_IGNORE_ERATT;
3845 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003846 return 1;
3847 }
3848
James Smartf4b4c682009-05-22 14:53:12 -04003849 spin_lock_irq(&phba->hbalock);
3850 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3851 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003852
Jamie Wellnitz41415862006-02-28 19:25:27 -05003853 mempool_free(pmb, phba->mbox_mem_pool);
3854
3855 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3856 * attention every 100ms for 3 seconds. If we don't get ERATT after
3857 * 3 seconds we still set HBA_ERROR state because the status of the
3858 * board is now undefined.
3859 */
James Smart9940b972011-03-11 16:06:12 -05003860 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3861 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003862 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3863 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003864 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3865 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003866 }
3867
3868 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003869 if (ha_copy & HA_ERATT) {
3870 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003871 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003872 }
James Smart2e0fef82007-06-17 19:56:36 -05003873 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003874 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003875 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003876 phba->link_flag &= ~LS_IGNORE_ERATT;
3877 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003878
Jamie Wellnitz41415862006-02-28 19:25:27 -05003879 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003880 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003881
James Smart2e0fef82007-06-17 19:56:36 -05003882 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003883}
3884
James Smarte59058c2008-08-24 21:49:00 -04003885/**
James Smart3772a992009-05-22 14:50:54 -04003886 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003887 * @phba: Pointer to HBA context object.
3888 *
3889 * This function resets the HBA by writing HC_INITFF to the control
3890 * register. After the HBA resets, this function resets all the iocb ring
3891 * indices. This function disables PCI layer parity checking during
3892 * the reset.
3893 * This function returns 0 always.
3894 * The caller is not required to hold any locks.
3895 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003896int
James Smart2e0fef82007-06-17 19:56:36 -05003897lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003898{
3899 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003900 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003901 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003902 int i;
dea31012005-04-17 16:05:31 -05003903
Jamie Wellnitz41415862006-02-28 19:25:27 -05003904 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003905
Jamie Wellnitz41415862006-02-28 19:25:27 -05003906 /* Reset HBA */
3907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003908 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003909 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003910
3911 /* perform board reset */
3912 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003913 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003914 phba->pport->fc_myDID = 0;
3915 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003916
Jamie Wellnitz41415862006-02-28 19:25:27 -05003917 /* Turn off parity checking and serr during the physical reset */
3918 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3919 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3920 (cfg_value &
3921 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3922
James Smart3772a992009-05-22 14:50:54 -04003923 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3924
Jamie Wellnitz41415862006-02-28 19:25:27 -05003925 /* Now toggle INITFF bit in the Host Control Register */
3926 writel(HC_INITFF, phba->HCregaddr);
3927 mdelay(1);
3928 readl(phba->HCregaddr); /* flush */
3929 writel(0, phba->HCregaddr);
3930 readl(phba->HCregaddr); /* flush */
3931
3932 /* Restore PCI cmd register */
3933 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003934
3935 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936 for (i = 0; i < psli->num_rings; i++) {
3937 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003938 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003939 pring->sli.sli3.rspidx = 0;
3940 pring->sli.sli3.next_cmdidx = 0;
3941 pring->sli.sli3.local_getidx = 0;
3942 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003943 pring->missbufcnt = 0;
3944 }
dea31012005-04-17 16:05:31 -05003945
James Smart2e0fef82007-06-17 19:56:36 -05003946 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003947 return 0;
3948}
3949
James Smarte59058c2008-08-24 21:49:00 -04003950/**
James Smartda0436e2009-05-22 14:51:39 -04003951 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3952 * @phba: Pointer to HBA context object.
3953 *
3954 * This function resets a SLI4 HBA. This function disables PCI layer parity
3955 * checking during resets the device. The caller is not required to hold
3956 * any locks.
3957 *
3958 * This function returns 0 always.
3959 **/
3960int
3961lpfc_sli4_brdreset(struct lpfc_hba *phba)
3962{
3963 struct lpfc_sli *psli = &phba->sli;
3964 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003965 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003966
3967 /* Reset HBA */
3968 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3969 "0295 Reset HBA Data: x%x x%x\n",
3970 phba->pport->port_state, psli->sli_flag);
3971
3972 /* perform board reset */
3973 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003974 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003975 phba->pport->fc_myDID = 0;
3976 phba->pport->fc_prevDID = 0;
3977
James Smartda0436e2009-05-22 14:51:39 -04003978 spin_lock_irq(&phba->hbalock);
3979 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3980 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003981 spin_unlock_irq(&phba->hbalock);
3982
3983 /* Now physically reset the device */
3984 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3985 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003986
3987 /* Turn off parity checking and serr during the physical reset */
3988 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3989 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3990 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3991
James Smart88318812012-09-29 11:29:29 -04003992 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04003993 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04003994 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04003995
James Smartbe858b62010-12-15 17:57:20 -05003996 /* Restore PCI cmd register */
3997 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3998
James Smart27b01b82012-05-09 21:19:44 -04003999 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004000}
4001
4002/**
4003 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004004 * @phba: Pointer to HBA context object.
4005 *
4006 * This function is called in the SLI initialization code path to
4007 * restart the HBA. The caller is not required to hold any lock.
4008 * This function writes MBX_RESTART mailbox command to the SLIM and
4009 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4010 * function to free any pending commands. The function enables
4011 * POST only during the first initialization. The function returns zero.
4012 * The function does not guarantee completion of MBX_RESTART mailbox
4013 * command before the return of this function.
4014 **/
James Smartda0436e2009-05-22 14:51:39 -04004015static int
4016lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004017{
4018 MAILBOX_t *mb;
4019 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004020 volatile uint32_t word0;
4021 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004022 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004023
James Smart2e0fef82007-06-17 19:56:36 -05004024 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004025
James Smart0d878412009-10-02 15:16:56 -04004026 /* Take PCIe device Advanced Error Reporting (AER) state */
4027 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4028
Jamie Wellnitz41415862006-02-28 19:25:27 -05004029 psli = &phba->sli;
4030
4031 /* Restart HBA */
4032 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004033 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004034 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004035
4036 word0 = 0;
4037 mb = (MAILBOX_t *) &word0;
4038 mb->mbxCommand = MBX_RESTART;
4039 mb->mbxHc = 1;
4040
James Smart92908312006-03-07 15:04:13 -05004041 lpfc_reset_barrier(phba);
4042
Jamie Wellnitz41415862006-02-28 19:25:27 -05004043 to_slim = phba->MBslimaddr;
4044 writel(*(uint32_t *) mb, to_slim);
4045 readl(to_slim); /* flush */
4046
4047 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004048 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004049 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004050 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004051 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004052 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004053 writel(*(uint32_t *) mb, to_slim);
4054 readl(to_slim); /* flush */
4055
4056 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004057 phba->pport->stopped = 0;
4058 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004059 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004060 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004061
James Smart64ba8812006-08-02 15:24:34 -04004062 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4063 psli->stats_start = get_seconds();
4064
James Smarteaf15d52008-12-04 22:39:29 -05004065 /* Give the INITFF and Post time to settle. */
4066 mdelay(100);
dea31012005-04-17 16:05:31 -05004067
James Smart0d878412009-10-02 15:16:56 -04004068 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4069 if (hba_aer_enabled)
4070 pci_disable_pcie_error_reporting(phba->pcidev);
4071
Jamie Wellnitz41415862006-02-28 19:25:27 -05004072 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004073
4074 return 0;
4075}
4076
James Smarte59058c2008-08-24 21:49:00 -04004077/**
James Smartda0436e2009-05-22 14:51:39 -04004078 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4079 * @phba: Pointer to HBA context object.
4080 *
4081 * This function is called in the SLI initialization code path to restart
4082 * a SLI4 HBA. The caller is not required to hold any lock.
4083 * At the end of the function, it calls lpfc_hba_down_post function to
4084 * free any pending commands.
4085 **/
4086static int
4087lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4088{
4089 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004090 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004091 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004092
4093 /* Restart HBA */
4094 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4095 "0296 Restart HBA Data: x%x x%x\n",
4096 phba->pport->port_state, psli->sli_flag);
4097
James Smart75baf692010-06-08 18:31:21 -04004098 /* Take PCIe device Advanced Error Reporting (AER) state */
4099 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4100
James Smart27b01b82012-05-09 21:19:44 -04004101 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004102
4103 spin_lock_irq(&phba->hbalock);
4104 phba->pport->stopped = 0;
4105 phba->link_state = LPFC_INIT_START;
4106 phba->hba_flag = 0;
4107 spin_unlock_irq(&phba->hbalock);
4108
4109 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4110 psli->stats_start = get_seconds();
4111
James Smart75baf692010-06-08 18:31:21 -04004112 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4113 if (hba_aer_enabled)
4114 pci_disable_pcie_error_reporting(phba->pcidev);
4115
James Smartda0436e2009-05-22 14:51:39 -04004116 lpfc_hba_down_post(phba);
4117
James Smart27b01b82012-05-09 21:19:44 -04004118 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004119}
4120
4121/**
4122 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4123 * @phba: Pointer to HBA context object.
4124 *
4125 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4126 * API jump table function pointer from the lpfc_hba struct.
4127**/
4128int
4129lpfc_sli_brdrestart(struct lpfc_hba *phba)
4130{
4131 return phba->lpfc_sli_brdrestart(phba);
4132}
4133
4134/**
James Smart3621a712009-04-06 18:47:14 -04004135 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004136 * @phba: Pointer to HBA context object.
4137 *
4138 * This function is called after a HBA restart to wait for successful
4139 * restart of the HBA. Successful restart of the HBA is indicated by
4140 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4141 * iteration, the function will restart the HBA again. The function returns
4142 * zero if HBA successfully restarted else returns negative error code.
4143 **/
dea31012005-04-17 16:05:31 -05004144static int
4145lpfc_sli_chipset_init(struct lpfc_hba *phba)
4146{
4147 uint32_t status, i = 0;
4148
4149 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004150 if (lpfc_readl(phba->HSregaddr, &status))
4151 return -EIO;
dea31012005-04-17 16:05:31 -05004152
4153 /* Check status register to see what current state is */
4154 i = 0;
4155 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4156
James Smartdcf2a4e2010-09-29 11:18:53 -04004157 /* Check every 10ms for 10 retries, then every 100ms for 90
4158 * retries, then every 1 sec for 50 retires for a total of
4159 * ~60 seconds before reset the board again and check every
4160 * 1 sec for 50 retries. The up to 60 seconds before the
4161 * board ready is required by the Falcon FIPS zeroization
4162 * complete, and any reset the board in between shall cause
4163 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004164 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004165 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004166 /* Adapter failed to init, timeout, status reg
4167 <status> */
James Smarted957682007-06-17 19:56:37 -05004168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004169 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004170 "timeout, status reg x%x, "
4171 "FW Data: A8 x%x AC x%x\n", status,
4172 readl(phba->MBslimaddr + 0xa8),
4173 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004174 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004175 return -ETIMEDOUT;
4176 }
4177
4178 /* Check to see if any errors occurred during init */
4179 if (status & HS_FFERM) {
4180 /* ERROR: During chipset initialization */
4181 /* Adapter failed to init, chipset, status reg
4182 <status> */
James Smarted957682007-06-17 19:56:37 -05004183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004184 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004185 "chipset, status reg x%x, "
4186 "FW Data: A8 x%x AC x%x\n", status,
4187 readl(phba->MBslimaddr + 0xa8),
4188 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004189 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004190 return -EIO;
4191 }
4192
James Smartdcf2a4e2010-09-29 11:18:53 -04004193 if (i <= 10)
dea31012005-04-17 16:05:31 -05004194 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004195 else if (i <= 100)
4196 msleep(100);
4197 else
4198 msleep(1000);
dea31012005-04-17 16:05:31 -05004199
James Smartdcf2a4e2010-09-29 11:18:53 -04004200 if (i == 150) {
4201 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004202 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004203 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004204 }
4205 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004206 if (lpfc_readl(phba->HSregaddr, &status))
4207 return -EIO;
dea31012005-04-17 16:05:31 -05004208 }
4209
4210 /* Check to see if any errors occurred during init */
4211 if (status & HS_FFERM) {
4212 /* ERROR: During chipset initialization */
4213 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004215 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004216 "status reg x%x, "
4217 "FW Data: A8 x%x AC x%x\n", status,
4218 readl(phba->MBslimaddr + 0xa8),
4219 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004220 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004221 return -EIO;
4222 }
4223
4224 /* Clear all interrupt enable conditions */
4225 writel(0, phba->HCregaddr);
4226 readl(phba->HCregaddr); /* flush */
4227
4228 /* setup host attn register */
4229 writel(0xffffffff, phba->HAregaddr);
4230 readl(phba->HAregaddr); /* flush */
4231 return 0;
4232}
4233
James Smarte59058c2008-08-24 21:49:00 -04004234/**
James Smart3621a712009-04-06 18:47:14 -04004235 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004236 *
4237 * This function calculates and returns the number of HBQs required to be
4238 * configured.
4239 **/
James Smart78b2d852007-08-02 11:10:21 -04004240int
James Smarted957682007-06-17 19:56:37 -05004241lpfc_sli_hbq_count(void)
4242{
James Smart92d7f7b2007-06-17 19:56:38 -05004243 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004244}
4245
James Smarte59058c2008-08-24 21:49:00 -04004246/**
James Smart3621a712009-04-06 18:47:14 -04004247 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004248 *
4249 * This function adds the number of hbq entries in every HBQ to get
4250 * the total number of hbq entries required for the HBA and returns
4251 * the total count.
4252 **/
James Smarted957682007-06-17 19:56:37 -05004253static int
4254lpfc_sli_hbq_entry_count(void)
4255{
4256 int hbq_count = lpfc_sli_hbq_count();
4257 int count = 0;
4258 int i;
4259
4260 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004261 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004262 return count;
4263}
4264
James Smarte59058c2008-08-24 21:49:00 -04004265/**
James Smart3621a712009-04-06 18:47:14 -04004266 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004267 *
4268 * This function calculates amount of memory required for all hbq entries
4269 * to be configured and returns the total memory required.
4270 **/
dea31012005-04-17 16:05:31 -05004271int
James Smarted957682007-06-17 19:56:37 -05004272lpfc_sli_hbq_size(void)
4273{
4274 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4275}
4276
James Smarte59058c2008-08-24 21:49:00 -04004277/**
James Smart3621a712009-04-06 18:47:14 -04004278 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004279 * @phba: Pointer to HBA context object.
4280 *
4281 * This function is called during the SLI initialization to configure
4282 * all the HBQs and post buffers to the HBQ. The caller is not
4283 * required to hold any locks. This function will return zero if successful
4284 * else it will return negative error code.
4285 **/
James Smarted957682007-06-17 19:56:37 -05004286static int
4287lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4288{
4289 int hbq_count = lpfc_sli_hbq_count();
4290 LPFC_MBOXQ_t *pmb;
4291 MAILBOX_t *pmbox;
4292 uint32_t hbqno;
4293 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004294
James Smart92d7f7b2007-06-17 19:56:38 -05004295 /* Get a Mailbox buffer to setup mailbox
4296 * commands for HBA initialization
4297 */
James Smarted957682007-06-17 19:56:37 -05004298 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4299
4300 if (!pmb)
4301 return -ENOMEM;
4302
James Smart04c68492009-05-22 14:52:52 -04004303 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004304
4305 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4306 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004307 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004308
4309 hbq_entry_index = 0;
4310 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4311 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4312 phba->hbqs[hbqno].hbqPutIdx = 0;
4313 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4314 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004315 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004316 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4317 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004318 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4319
4320 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4321 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4322 mbxStatus <status>, ring <num> */
4323
4324 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004325 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004326 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004327 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004328 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004329 pmbox->mbxStatus, hbqno);
4330
4331 phba->link_state = LPFC_HBA_ERROR;
4332 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004333 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004334 }
4335 }
4336 phba->hbq_count = hbq_count;
4337
James Smarted957682007-06-17 19:56:37 -05004338 mempool_free(pmb, phba->mbox_mem_pool);
4339
James Smart92d7f7b2007-06-17 19:56:38 -05004340 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004341 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4342 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004343 return 0;
4344}
4345
James Smarte59058c2008-08-24 21:49:00 -04004346/**
James Smart4f774512009-05-22 14:52:35 -04004347 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4348 * @phba: Pointer to HBA context object.
4349 *
4350 * This function is called during the SLI initialization to configure
4351 * all the HBQs and post buffers to the HBQ. The caller is not
4352 * required to hold any locks. This function will return zero if successful
4353 * else it will return negative error code.
4354 **/
4355static int
4356lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4357{
4358 phba->hbq_in_use = 1;
4359 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4360 phba->hbq_count = 1;
4361 /* Initially populate or replenish the HBQs */
4362 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4363 return 0;
4364}
4365
4366/**
James Smart3621a712009-04-06 18:47:14 -04004367 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004368 * @phba: Pointer to HBA context object.
4369 * @sli_mode: sli mode - 2/3
4370 *
4371 * This function is called by the sli intialization code path
4372 * to issue config_port mailbox command. This function restarts the
4373 * HBA firmware and issues a config_port mailbox command to configure
4374 * the SLI interface in the sli mode specified by sli_mode
4375 * variable. The caller is not required to hold any locks.
4376 * The function returns 0 if successful, else returns negative error
4377 * code.
4378 **/
James Smart93996272008-08-24 21:50:30 -04004379int
4380lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004381{
4382 LPFC_MBOXQ_t *pmb;
4383 uint32_t resetcount = 0, rc = 0, done = 0;
4384
4385 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4386 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004387 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004388 return -ENOMEM;
4389 }
4390
James Smarted957682007-06-17 19:56:37 -05004391 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004392 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004393 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004394 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004395 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004396 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004397 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004398 rc = lpfc_sli_chipset_init(phba);
4399 if (rc)
4400 break;
4401
James Smart2e0fef82007-06-17 19:56:36 -05004402 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004403 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004404 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004405 resetcount++;
4406
James Smarted957682007-06-17 19:56:37 -05004407 /* Call pre CONFIG_PORT mailbox command initialization. A
4408 * value of 0 means the call was successful. Any other
4409 * nonzero value is a failure, but if ERESTART is returned,
4410 * the driver may reset the HBA and try again.
4411 */
dea31012005-04-17 16:05:31 -05004412 rc = lpfc_config_port_prep(phba);
4413 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004414 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004415 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004416 } else if (rc)
dea31012005-04-17 16:05:31 -05004417 break;
James Smart6d368e52011-05-24 11:44:12 -04004418
James Smart2e0fef82007-06-17 19:56:36 -05004419 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004420 lpfc_config_port(phba, pmb);
4421 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004422 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4423 LPFC_SLI3_HBQ_ENABLED |
4424 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004425 LPFC_SLI3_BG_ENABLED |
4426 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004427 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004429 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004430 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004431 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004432 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004433 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004434 spin_unlock_irq(&phba->hbalock);
4435 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004436 } else {
4437 /* Allow asynchronous mailbox command to go through */
4438 spin_lock_irq(&phba->hbalock);
4439 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4440 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004441 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004442
4443 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4444 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4445 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4446 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004447 }
dea31012005-04-17 16:05:31 -05004448 }
James Smarted957682007-06-17 19:56:37 -05004449 if (!done) {
4450 rc = -EINVAL;
4451 goto do_prep_failed;
4452 }
James Smart04c68492009-05-22 14:52:52 -04004453 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4454 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004455 rc = -ENXIO;
4456 goto do_prep_failed;
4457 }
James Smart04c68492009-05-22 14:52:52 -04004458 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004459 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004460 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4461 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4462 phba->max_vpi : phba->max_vports;
4463
James Smart34b02dc2008-08-24 21:49:55 -04004464 } else
4465 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004466 phba->fips_level = 0;
4467 phba->fips_spec_rev = 0;
4468 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004469 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004470 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4471 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4472 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4473 "2850 Security Crypto Active. FIPS x%d "
4474 "(Spec Rev: x%d)",
4475 phba->fips_level, phba->fips_spec_rev);
4476 }
4477 if (pmb->u.mb.un.varCfgPort.sec_err) {
4478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4479 "2856 Config Port Security Crypto "
4480 "Error: x%x ",
4481 pmb->u.mb.un.varCfgPort.sec_err);
4482 }
James Smart04c68492009-05-22 14:52:52 -04004483 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004484 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004485 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004486 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004487
4488 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4489 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004490
4491 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004492 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004493 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4494 else
4495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4496 "0443 Adapter did not grant "
4497 "BlockGuard\n");
4498 }
James Smart34b02dc2008-08-24 21:49:55 -04004499 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004500 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004501 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004502 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004503 }
James Smart92d7f7b2007-06-17 19:56:38 -05004504do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004505 mempool_free(pmb, phba->mbox_mem_pool);
4506 return rc;
4507}
4508
James Smarte59058c2008-08-24 21:49:00 -04004509
4510/**
James Smart3621a712009-04-06 18:47:14 -04004511 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004512 * @phba: Pointer to HBA context object.
4513 *
4514 * This function is the main SLI intialization function. This function
4515 * is called by the HBA intialization code, HBA reset code and HBA
4516 * error attention handler code. Caller is not required to hold any
4517 * locks. This function issues config_port mailbox command to configure
4518 * the SLI, setup iocb rings and HBQ rings. In the end the function
4519 * calls the config_port_post function to issue init_link mailbox
4520 * command and to start the discovery. The function will return zero
4521 * if successful, else it will return negative error code.
4522 **/
James Smarted957682007-06-17 19:56:37 -05004523int
4524lpfc_sli_hba_setup(struct lpfc_hba *phba)
4525{
4526 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004527 int mode = 3, i;
4528 int longs;
James Smarted957682007-06-17 19:56:37 -05004529
4530 switch (lpfc_sli_mode) {
4531 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004532 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004534 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004535 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004536 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004537 break;
4538 }
James Smarted957682007-06-17 19:56:37 -05004539 mode = 2;
4540 break;
4541 case 0:
4542 case 3:
4543 break;
4544 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004546 "1819 Unrecognized lpfc_sli_mode "
4547 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004548
4549 break;
4550 }
4551
James Smart93996272008-08-24 21:50:30 -04004552 rc = lpfc_sli_config_port(phba, mode);
4553
James Smarted957682007-06-17 19:56:37 -05004554 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004556 "1820 Unable to select SLI-3. "
4557 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004558 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004559 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004560 if (rc)
dea31012005-04-17 16:05:31 -05004561 goto lpfc_sli_hba_setup_error;
4562
James Smart0d878412009-10-02 15:16:56 -04004563 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4564 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4565 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4566 if (!rc) {
4567 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4568 "2709 This device supports "
4569 "Advanced Error Reporting (AER)\n");
4570 spin_lock_irq(&phba->hbalock);
4571 phba->hba_flag |= HBA_AER_ENABLED;
4572 spin_unlock_irq(&phba->hbalock);
4573 } else {
4574 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4575 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004576 "Advanced Error Reporting (AER): %d\n",
4577 rc);
James Smart0d878412009-10-02 15:16:56 -04004578 phba->cfg_aer_support = 0;
4579 }
4580 }
4581
James Smarted957682007-06-17 19:56:37 -05004582 if (phba->sli_rev == 3) {
4583 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4584 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004585 } else {
4586 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4587 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004588 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004589 }
4590
4591 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004592 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4593 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004594 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004595
4596 if (rc)
4597 goto lpfc_sli_hba_setup_error;
4598
James Smart6d368e52011-05-24 11:44:12 -04004599 /* Initialize VPIs. */
4600 if (phba->sli_rev == LPFC_SLI_REV3) {
4601 /*
4602 * The VPI bitmask and physical ID array are allocated
4603 * and initialized once only - at driver load. A port
4604 * reset doesn't need to reinitialize this memory.
4605 */
4606 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4607 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4608 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4609 GFP_KERNEL);
4610 if (!phba->vpi_bmask) {
4611 rc = -ENOMEM;
4612 goto lpfc_sli_hba_setup_error;
4613 }
4614
4615 phba->vpi_ids = kzalloc(
4616 (phba->max_vpi+1) * sizeof(uint16_t),
4617 GFP_KERNEL);
4618 if (!phba->vpi_ids) {
4619 kfree(phba->vpi_bmask);
4620 rc = -ENOMEM;
4621 goto lpfc_sli_hba_setup_error;
4622 }
4623 for (i = 0; i < phba->max_vpi; i++)
4624 phba->vpi_ids[i] = i;
4625 }
4626 }
4627
James Smart93996272008-08-24 21:50:30 -04004628 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004629 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4630 rc = lpfc_sli_hbq_setup(phba);
4631 if (rc)
4632 goto lpfc_sli_hba_setup_error;
4633 }
James Smart04c68492009-05-22 14:52:52 -04004634 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004635 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004636 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004637
4638 rc = lpfc_config_port_post(phba);
4639 if (rc)
4640 goto lpfc_sli_hba_setup_error;
4641
James Smarted957682007-06-17 19:56:37 -05004642 return rc;
4643
James Smart92d7f7b2007-06-17 19:56:38 -05004644lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004645 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004647 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004648 return rc;
4649}
4650
James Smartda0436e2009-05-22 14:51:39 -04004651/**
4652 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4653 * @phba: Pointer to HBA context object.
4654 * @mboxq: mailbox pointer.
4655 * This function issue a dump mailbox command to read config region
4656 * 23 and parse the records in the region and populate driver
4657 * data structure.
4658 **/
4659static int
James Smartff78d8f2011-12-13 13:21:35 -05004660lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004661{
James Smartff78d8f2011-12-13 13:21:35 -05004662 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004663 struct lpfc_dmabuf *mp;
4664 struct lpfc_mqe *mqe;
4665 uint32_t data_length;
4666 int rc;
4667
4668 /* Program the default value of vlan_id and fc_map */
4669 phba->valid_vlan = 0;
4670 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4671 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4672 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4673
James Smartff78d8f2011-12-13 13:21:35 -05004674 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4675 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004676 return -ENOMEM;
4677
James Smartff78d8f2011-12-13 13:21:35 -05004678 mqe = &mboxq->u.mqe;
4679 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4680 rc = -ENOMEM;
4681 goto out_free_mboxq;
4682 }
4683
James Smartda0436e2009-05-22 14:51:39 -04004684 mp = (struct lpfc_dmabuf *) mboxq->context1;
4685 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4686
4687 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4688 "(%d):2571 Mailbox cmd x%x Status x%x "
4689 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4690 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4691 "CQ: x%x x%x x%x x%x\n",
4692 mboxq->vport ? mboxq->vport->vpi : 0,
4693 bf_get(lpfc_mqe_command, mqe),
4694 bf_get(lpfc_mqe_status, mqe),
4695 mqe->un.mb_words[0], mqe->un.mb_words[1],
4696 mqe->un.mb_words[2], mqe->un.mb_words[3],
4697 mqe->un.mb_words[4], mqe->un.mb_words[5],
4698 mqe->un.mb_words[6], mqe->un.mb_words[7],
4699 mqe->un.mb_words[8], mqe->un.mb_words[9],
4700 mqe->un.mb_words[10], mqe->un.mb_words[11],
4701 mqe->un.mb_words[12], mqe->un.mb_words[13],
4702 mqe->un.mb_words[14], mqe->un.mb_words[15],
4703 mqe->un.mb_words[16], mqe->un.mb_words[50],
4704 mboxq->mcqe.word0,
4705 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4706 mboxq->mcqe.trailer);
4707
4708 if (rc) {
4709 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4710 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004711 rc = -EIO;
4712 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004713 }
4714 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004715 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004716 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4717 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004718 rc = -EIO;
4719 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004720 }
James Smartda0436e2009-05-22 14:51:39 -04004721
4722 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4724 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004725 rc = 0;
4726
4727out_free_mboxq:
4728 mempool_free(mboxq, phba->mbox_mem_pool);
4729 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004730}
4731
4732/**
4733 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4734 * @phba: pointer to lpfc hba data structure.
4735 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4736 * @vpd: pointer to the memory to hold resulting port vpd data.
4737 * @vpd_size: On input, the number of bytes allocated to @vpd.
4738 * On output, the number of data bytes in @vpd.
4739 *
4740 * This routine executes a READ_REV SLI4 mailbox command. In
4741 * addition, this routine gets the port vpd data.
4742 *
4743 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004744 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004745 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004746 **/
4747static int
4748lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4749 uint8_t *vpd, uint32_t *vpd_size)
4750{
4751 int rc = 0;
4752 uint32_t dma_size;
4753 struct lpfc_dmabuf *dmabuf;
4754 struct lpfc_mqe *mqe;
4755
4756 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4757 if (!dmabuf)
4758 return -ENOMEM;
4759
4760 /*
4761 * Get a DMA buffer for the vpd data resulting from the READ_REV
4762 * mailbox command.
4763 */
4764 dma_size = *vpd_size;
4765 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4766 dma_size,
4767 &dmabuf->phys,
4768 GFP_KERNEL);
4769 if (!dmabuf->virt) {
4770 kfree(dmabuf);
4771 return -ENOMEM;
4772 }
4773 memset(dmabuf->virt, 0, dma_size);
4774
4775 /*
4776 * The SLI4 implementation of READ_REV conflicts at word1,
4777 * bits 31:16 and SLI4 adds vpd functionality not present
4778 * in SLI3. This code corrects the conflicts.
4779 */
4780 lpfc_read_rev(phba, mboxq);
4781 mqe = &mboxq->u.mqe;
4782 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4783 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4784 mqe->un.read_rev.word1 &= 0x0000FFFF;
4785 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4786 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4787
4788 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4789 if (rc) {
4790 dma_free_coherent(&phba->pcidev->dev, dma_size,
4791 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004792 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004793 return -EIO;
4794 }
4795
James Smartda0436e2009-05-22 14:51:39 -04004796 /*
4797 * The available vpd length cannot be bigger than the
4798 * DMA buffer passed to the port. Catch the less than
4799 * case and update the caller's size.
4800 */
4801 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4802 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4803
James Smartd7c47992010-06-08 18:31:54 -04004804 memcpy(vpd, dmabuf->virt, *vpd_size);
4805
James Smartda0436e2009-05-22 14:51:39 -04004806 dma_free_coherent(&phba->pcidev->dev, dma_size,
4807 dmabuf->virt, dmabuf->phys);
4808 kfree(dmabuf);
4809 return 0;
4810}
4811
4812/**
James Smartcd1c8302011-10-10 21:33:25 -04004813 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4814 * @phba: pointer to lpfc hba data structure.
4815 *
4816 * This routine retrieves SLI4 device physical port name this PCI function
4817 * is attached to.
4818 *
4819 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004820 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004821 * otherwise - failed to retrieve physical port name
4822 **/
4823static int
4824lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4825{
4826 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004827 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4828 struct lpfc_controller_attribute *cntl_attr;
4829 struct lpfc_mbx_get_port_name *get_port_name;
4830 void *virtaddr = NULL;
4831 uint32_t alloclen, reqlen;
4832 uint32_t shdr_status, shdr_add_status;
4833 union lpfc_sli4_cfg_shdr *shdr;
4834 char cport_name = 0;
4835 int rc;
4836
4837 /* We assume nothing at this point */
4838 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4839 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4840
4841 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4842 if (!mboxq)
4843 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004844 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004845 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4846 lpfc_sli4_read_config(phba);
4847 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4848 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004849
4850 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4851 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4852 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4853 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4854 LPFC_SLI4_MBX_NEMBED);
4855 if (alloclen < reqlen) {
4856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4857 "3084 Allocated DMA memory size (%d) is "
4858 "less than the requested DMA memory size "
4859 "(%d)\n", alloclen, reqlen);
4860 rc = -ENOMEM;
4861 goto out_free_mboxq;
4862 }
4863 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4864 virtaddr = mboxq->sge_array->addr[0];
4865 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4866 shdr = &mbx_cntl_attr->cfg_shdr;
4867 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4868 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4869 if (shdr_status || shdr_add_status || rc) {
4870 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4871 "3085 Mailbox x%x (x%x/x%x) failed, "
4872 "rc:x%x, status:x%x, add_status:x%x\n",
4873 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4874 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4875 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4876 rc, shdr_status, shdr_add_status);
4877 rc = -ENXIO;
4878 goto out_free_mboxq;
4879 }
4880 cntl_attr = &mbx_cntl_attr->cntl_attr;
4881 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4882 phba->sli4_hba.lnk_info.lnk_tp =
4883 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4884 phba->sli4_hba.lnk_info.lnk_no =
4885 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4886 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4887 "3086 lnk_type:%d, lnk_numb:%d\n",
4888 phba->sli4_hba.lnk_info.lnk_tp,
4889 phba->sli4_hba.lnk_info.lnk_no);
4890
4891retrieve_ppname:
4892 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4893 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4894 sizeof(struct lpfc_mbx_get_port_name) -
4895 sizeof(struct lpfc_sli4_cfg_mhdr),
4896 LPFC_SLI4_MBX_EMBED);
4897 get_port_name = &mboxq->u.mqe.un.get_port_name;
4898 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4899 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4900 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4901 phba->sli4_hba.lnk_info.lnk_tp);
4902 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4903 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4904 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4905 if (shdr_status || shdr_add_status || rc) {
4906 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4907 "3087 Mailbox x%x (x%x/x%x) failed: "
4908 "rc:x%x, status:x%x, add_status:x%x\n",
4909 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4910 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4911 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4912 rc, shdr_status, shdr_add_status);
4913 rc = -ENXIO;
4914 goto out_free_mboxq;
4915 }
4916 switch (phba->sli4_hba.lnk_info.lnk_no) {
4917 case LPFC_LINK_NUMBER_0:
4918 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4919 &get_port_name->u.response);
4920 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4921 break;
4922 case LPFC_LINK_NUMBER_1:
4923 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4924 &get_port_name->u.response);
4925 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4926 break;
4927 case LPFC_LINK_NUMBER_2:
4928 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4929 &get_port_name->u.response);
4930 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4931 break;
4932 case LPFC_LINK_NUMBER_3:
4933 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4934 &get_port_name->u.response);
4935 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4936 break;
4937 default:
4938 break;
4939 }
4940
4941 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4942 phba->Port[0] = cport_name;
4943 phba->Port[1] = '\0';
4944 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4945 "3091 SLI get port name: %s\n", phba->Port);
4946 }
4947
4948out_free_mboxq:
4949 if (rc != MBX_TIMEOUT) {
4950 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4951 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4952 else
4953 mempool_free(mboxq, phba->mbox_mem_pool);
4954 }
4955 return rc;
4956}
4957
4958/**
James Smartda0436e2009-05-22 14:51:39 -04004959 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4960 * @phba: pointer to lpfc hba data structure.
4961 *
4962 * This routine is called to explicitly arm the SLI4 device's completion and
4963 * event queues
4964 **/
4965static void
4966lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4967{
James Smart962bc512013-01-03 15:44:00 -05004968 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04004969
4970 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4971 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004972 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004973 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04004974 do {
James Smart2e90f4b2011-12-13 13:22:37 -05004975 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4976 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04004977 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05004978 }
James Smart67d12732012-08-03 12:36:13 -04004979 if (phba->sli4_hba.hba_eq) {
4980 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05004981 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04004982 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05004983 LPFC_QUEUE_REARM);
4984 }
James Smartda0436e2009-05-22 14:51:39 -04004985}
4986
4987/**
James Smart6d368e52011-05-24 11:44:12 -04004988 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4989 * @phba: Pointer to HBA context object.
4990 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004991 * @extnt_count: buffer to hold port available extent count.
4992 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004993 *
James Smartb76f2dc2011-07-22 18:37:42 -04004994 * This function calls the port and retrievs the number of available
4995 * extents and their size for a particular extent type.
4996 *
4997 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004998 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004999int
James Smart6d368e52011-05-24 11:44:12 -04005000lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5001 uint16_t *extnt_count, uint16_t *extnt_size)
5002{
5003 int rc = 0;
5004 uint32_t length;
5005 uint32_t mbox_tmo;
5006 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5007 LPFC_MBOXQ_t *mbox;
5008
5009 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5010 if (!mbox)
5011 return -ENOMEM;
5012
5013 /* Find out how many extents are available for this resource type */
5014 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5015 sizeof(struct lpfc_sli4_cfg_mhdr));
5016 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5017 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5018 length, LPFC_SLI4_MBX_EMBED);
5019
5020 /* Send an extents count of 0 - the GET doesn't use it. */
5021 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5022 LPFC_SLI4_MBX_EMBED);
5023 if (unlikely(rc)) {
5024 rc = -EIO;
5025 goto err_exit;
5026 }
5027
5028 if (!phba->sli4_hba.intr_enable)
5029 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5030 else {
James Smarta183a152011-10-10 21:32:43 -04005031 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005032 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5033 }
5034 if (unlikely(rc)) {
5035 rc = -EIO;
5036 goto err_exit;
5037 }
5038
5039 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5040 if (bf_get(lpfc_mbox_hdr_status,
5041 &rsrc_info->header.cfg_shdr.response)) {
5042 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5043 "2930 Failed to get resource extents "
5044 "Status 0x%x Add'l Status 0x%x\n",
5045 bf_get(lpfc_mbox_hdr_status,
5046 &rsrc_info->header.cfg_shdr.response),
5047 bf_get(lpfc_mbox_hdr_add_status,
5048 &rsrc_info->header.cfg_shdr.response));
5049 rc = -EIO;
5050 goto err_exit;
5051 }
5052
5053 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5054 &rsrc_info->u.rsp);
5055 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5056 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005057
5058 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5059 "3162 Retrieved extents type-%d from port: count:%d, "
5060 "size:%d\n", type, *extnt_count, *extnt_size);
5061
5062err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005063 mempool_free(mbox, phba->mbox_mem_pool);
5064 return rc;
5065}
5066
5067/**
5068 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5069 * @phba: Pointer to HBA context object.
5070 * @type: The extent type to check.
5071 *
5072 * This function reads the current available extents from the port and checks
5073 * if the extent count or extent size has changed since the last access.
5074 * Callers use this routine post port reset to understand if there is a
5075 * extent reprovisioning requirement.
5076 *
5077 * Returns:
5078 * -Error: error indicates problem.
5079 * 1: Extent count or size has changed.
5080 * 0: No changes.
5081 **/
5082static int
5083lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5084{
5085 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5086 uint16_t size_diff, rsrc_ext_size;
5087 int rc = 0;
5088 struct lpfc_rsrc_blks *rsrc_entry;
5089 struct list_head *rsrc_blk_list = NULL;
5090
5091 size_diff = 0;
5092 curr_ext_cnt = 0;
5093 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5094 &rsrc_ext_cnt,
5095 &rsrc_ext_size);
5096 if (unlikely(rc))
5097 return -EIO;
5098
5099 switch (type) {
5100 case LPFC_RSC_TYPE_FCOE_RPI:
5101 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5102 break;
5103 case LPFC_RSC_TYPE_FCOE_VPI:
5104 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5105 break;
5106 case LPFC_RSC_TYPE_FCOE_XRI:
5107 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5108 break;
5109 case LPFC_RSC_TYPE_FCOE_VFI:
5110 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5111 break;
5112 default:
5113 break;
5114 }
5115
5116 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5117 curr_ext_cnt++;
5118 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5119 size_diff++;
5120 }
5121
5122 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5123 rc = 1;
5124
5125 return rc;
5126}
5127
5128/**
5129 * lpfc_sli4_cfg_post_extnts -
5130 * @phba: Pointer to HBA context object.
5131 * @extnt_cnt - number of available extents.
5132 * @type - the extent type (rpi, xri, vfi, vpi).
5133 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5134 * @mbox - pointer to the caller's allocated mailbox structure.
5135 *
5136 * This function executes the extents allocation request. It also
5137 * takes care of the amount of memory needed to allocate or get the
5138 * allocated extents. It is the caller's responsibility to evaluate
5139 * the response.
5140 *
5141 * Returns:
5142 * -Error: Error value describes the condition found.
5143 * 0: if successful
5144 **/
5145static int
James Smart8a9d2e82012-05-09 21:16:12 -04005146lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005147 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5148{
5149 int rc = 0;
5150 uint32_t req_len;
5151 uint32_t emb_len;
5152 uint32_t alloc_len, mbox_tmo;
5153
5154 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005155 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005156
5157 /*
5158 * Calculate the size of an embedded mailbox. The uint32_t
5159 * accounts for extents-specific word.
5160 */
5161 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5162 sizeof(uint32_t);
5163
5164 /*
5165 * Presume the allocation and response will fit into an embedded
5166 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5167 */
5168 *emb = LPFC_SLI4_MBX_EMBED;
5169 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005170 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005171 sizeof(union lpfc_sli4_cfg_shdr) +
5172 sizeof(uint32_t);
5173 *emb = LPFC_SLI4_MBX_NEMBED;
5174 }
5175
5176 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5177 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5178 req_len, *emb);
5179 if (alloc_len < req_len) {
5180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005181 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005182 "less than the requested DMA memory "
5183 "size (x%x)\n", alloc_len, req_len);
5184 return -ENOMEM;
5185 }
James Smart8a9d2e82012-05-09 21:16:12 -04005186 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005187 if (unlikely(rc))
5188 return -EIO;
5189
5190 if (!phba->sli4_hba.intr_enable)
5191 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5192 else {
James Smarta183a152011-10-10 21:32:43 -04005193 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005194 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5195 }
5196
5197 if (unlikely(rc))
5198 rc = -EIO;
5199 return rc;
5200}
5201
5202/**
5203 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5204 * @phba: Pointer to HBA context object.
5205 * @type: The resource extent type to allocate.
5206 *
5207 * This function allocates the number of elements for the specified
5208 * resource type.
5209 **/
5210static int
5211lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5212{
5213 bool emb = false;
5214 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5215 uint16_t rsrc_id, rsrc_start, j, k;
5216 uint16_t *ids;
5217 int i, rc;
5218 unsigned long longs;
5219 unsigned long *bmask;
5220 struct lpfc_rsrc_blks *rsrc_blks;
5221 LPFC_MBOXQ_t *mbox;
5222 uint32_t length;
5223 struct lpfc_id_range *id_array = NULL;
5224 void *virtaddr = NULL;
5225 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5226 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5227 struct list_head *ext_blk_list;
5228
5229 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5230 &rsrc_cnt,
5231 &rsrc_size);
5232 if (unlikely(rc))
5233 return -EIO;
5234
5235 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5236 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5237 "3009 No available Resource Extents "
5238 "for resource type 0x%x: Count: 0x%x, "
5239 "Size 0x%x\n", type, rsrc_cnt,
5240 rsrc_size);
5241 return -ENOMEM;
5242 }
5243
James Smart8a9d2e82012-05-09 21:16:12 -04005244 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5245 "2903 Post resource extents type-0x%x: "
5246 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005247
5248 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5249 if (!mbox)
5250 return -ENOMEM;
5251
James Smart8a9d2e82012-05-09 21:16:12 -04005252 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005253 if (unlikely(rc)) {
5254 rc = -EIO;
5255 goto err_exit;
5256 }
5257
5258 /*
5259 * Figure out where the response is located. Then get local pointers
5260 * to the response data. The port does not guarantee to respond to
5261 * all extents counts request so update the local variable with the
5262 * allocated count from the port.
5263 */
5264 if (emb == LPFC_SLI4_MBX_EMBED) {
5265 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5266 id_array = &rsrc_ext->u.rsp.id[0];
5267 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5268 } else {
5269 virtaddr = mbox->sge_array->addr[0];
5270 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5271 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5272 id_array = &n_rsrc->id;
5273 }
5274
5275 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5276 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5277
5278 /*
5279 * Based on the resource size and count, correct the base and max
5280 * resource values.
5281 */
5282 length = sizeof(struct lpfc_rsrc_blks);
5283 switch (type) {
5284 case LPFC_RSC_TYPE_FCOE_RPI:
5285 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5286 sizeof(unsigned long),
5287 GFP_KERNEL);
5288 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5289 rc = -ENOMEM;
5290 goto err_exit;
5291 }
5292 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5293 sizeof(uint16_t),
5294 GFP_KERNEL);
5295 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5296 kfree(phba->sli4_hba.rpi_bmask);
5297 rc = -ENOMEM;
5298 goto err_exit;
5299 }
5300
5301 /*
5302 * The next_rpi was initialized with the maximum available
5303 * count but the port may allocate a smaller number. Catch
5304 * that case and update the next_rpi.
5305 */
5306 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5307
5308 /* Initialize local ptrs for common extent processing later. */
5309 bmask = phba->sli4_hba.rpi_bmask;
5310 ids = phba->sli4_hba.rpi_ids;
5311 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5312 break;
5313 case LPFC_RSC_TYPE_FCOE_VPI:
5314 phba->vpi_bmask = kzalloc(longs *
5315 sizeof(unsigned long),
5316 GFP_KERNEL);
5317 if (unlikely(!phba->vpi_bmask)) {
5318 rc = -ENOMEM;
5319 goto err_exit;
5320 }
5321 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5322 sizeof(uint16_t),
5323 GFP_KERNEL);
5324 if (unlikely(!phba->vpi_ids)) {
5325 kfree(phba->vpi_bmask);
5326 rc = -ENOMEM;
5327 goto err_exit;
5328 }
5329
5330 /* Initialize local ptrs for common extent processing later. */
5331 bmask = phba->vpi_bmask;
5332 ids = phba->vpi_ids;
5333 ext_blk_list = &phba->lpfc_vpi_blk_list;
5334 break;
5335 case LPFC_RSC_TYPE_FCOE_XRI:
5336 phba->sli4_hba.xri_bmask = kzalloc(longs *
5337 sizeof(unsigned long),
5338 GFP_KERNEL);
5339 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5340 rc = -ENOMEM;
5341 goto err_exit;
5342 }
James Smart8a9d2e82012-05-09 21:16:12 -04005343 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005344 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5345 sizeof(uint16_t),
5346 GFP_KERNEL);
5347 if (unlikely(!phba->sli4_hba.xri_ids)) {
5348 kfree(phba->sli4_hba.xri_bmask);
5349 rc = -ENOMEM;
5350 goto err_exit;
5351 }
5352
5353 /* Initialize local ptrs for common extent processing later. */
5354 bmask = phba->sli4_hba.xri_bmask;
5355 ids = phba->sli4_hba.xri_ids;
5356 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5357 break;
5358 case LPFC_RSC_TYPE_FCOE_VFI:
5359 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5360 sizeof(unsigned long),
5361 GFP_KERNEL);
5362 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5363 rc = -ENOMEM;
5364 goto err_exit;
5365 }
5366 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5367 sizeof(uint16_t),
5368 GFP_KERNEL);
5369 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5370 kfree(phba->sli4_hba.vfi_bmask);
5371 rc = -ENOMEM;
5372 goto err_exit;
5373 }
5374
5375 /* Initialize local ptrs for common extent processing later. */
5376 bmask = phba->sli4_hba.vfi_bmask;
5377 ids = phba->sli4_hba.vfi_ids;
5378 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5379 break;
5380 default:
5381 /* Unsupported Opcode. Fail call. */
5382 id_array = NULL;
5383 bmask = NULL;
5384 ids = NULL;
5385 ext_blk_list = NULL;
5386 goto err_exit;
5387 }
5388
5389 /*
5390 * Complete initializing the extent configuration with the
5391 * allocated ids assigned to this function. The bitmask serves
5392 * as an index into the array and manages the available ids. The
5393 * array just stores the ids communicated to the port via the wqes.
5394 */
5395 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5396 if ((i % 2) == 0)
5397 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5398 &id_array[k]);
5399 else
5400 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5401 &id_array[k]);
5402
5403 rsrc_blks = kzalloc(length, GFP_KERNEL);
5404 if (unlikely(!rsrc_blks)) {
5405 rc = -ENOMEM;
5406 kfree(bmask);
5407 kfree(ids);
5408 goto err_exit;
5409 }
5410 rsrc_blks->rsrc_start = rsrc_id;
5411 rsrc_blks->rsrc_size = rsrc_size;
5412 list_add_tail(&rsrc_blks->list, ext_blk_list);
5413 rsrc_start = rsrc_id;
5414 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5415 phba->sli4_hba.scsi_xri_start = rsrc_start +
5416 lpfc_sli4_get_els_iocb_cnt(phba);
5417
5418 while (rsrc_id < (rsrc_start + rsrc_size)) {
5419 ids[j] = rsrc_id;
5420 rsrc_id++;
5421 j++;
5422 }
5423 /* Entire word processed. Get next word.*/
5424 if ((i % 2) == 1)
5425 k++;
5426 }
5427 err_exit:
5428 lpfc_sli4_mbox_cmd_free(phba, mbox);
5429 return rc;
5430}
5431
5432/**
5433 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5434 * @phba: Pointer to HBA context object.
5435 * @type: the extent's type.
5436 *
5437 * This function deallocates all extents of a particular resource type.
5438 * SLI4 does not allow for deallocating a particular extent range. It
5439 * is the caller's responsibility to release all kernel memory resources.
5440 **/
5441static int
5442lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5443{
5444 int rc;
5445 uint32_t length, mbox_tmo = 0;
5446 LPFC_MBOXQ_t *mbox;
5447 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5448 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5449
5450 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5451 if (!mbox)
5452 return -ENOMEM;
5453
5454 /*
5455 * This function sends an embedded mailbox because it only sends the
5456 * the resource type. All extents of this type are released by the
5457 * port.
5458 */
5459 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5460 sizeof(struct lpfc_sli4_cfg_mhdr));
5461 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5462 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5463 length, LPFC_SLI4_MBX_EMBED);
5464
5465 /* Send an extents count of 0 - the dealloc doesn't use it. */
5466 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5467 LPFC_SLI4_MBX_EMBED);
5468 if (unlikely(rc)) {
5469 rc = -EIO;
5470 goto out_free_mbox;
5471 }
5472 if (!phba->sli4_hba.intr_enable)
5473 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5474 else {
James Smarta183a152011-10-10 21:32:43 -04005475 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005476 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5477 }
5478 if (unlikely(rc)) {
5479 rc = -EIO;
5480 goto out_free_mbox;
5481 }
5482
5483 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5484 if (bf_get(lpfc_mbox_hdr_status,
5485 &dealloc_rsrc->header.cfg_shdr.response)) {
5486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5487 "2919 Failed to release resource extents "
5488 "for type %d - Status 0x%x Add'l Status 0x%x. "
5489 "Resource memory not released.\n",
5490 type,
5491 bf_get(lpfc_mbox_hdr_status,
5492 &dealloc_rsrc->header.cfg_shdr.response),
5493 bf_get(lpfc_mbox_hdr_add_status,
5494 &dealloc_rsrc->header.cfg_shdr.response));
5495 rc = -EIO;
5496 goto out_free_mbox;
5497 }
5498
5499 /* Release kernel memory resources for the specific type. */
5500 switch (type) {
5501 case LPFC_RSC_TYPE_FCOE_VPI:
5502 kfree(phba->vpi_bmask);
5503 kfree(phba->vpi_ids);
5504 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5505 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5506 &phba->lpfc_vpi_blk_list, list) {
5507 list_del_init(&rsrc_blk->list);
5508 kfree(rsrc_blk);
5509 }
James Smart16a3a202013-04-17 20:14:38 -04005510 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005511 break;
5512 case LPFC_RSC_TYPE_FCOE_XRI:
5513 kfree(phba->sli4_hba.xri_bmask);
5514 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005515 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5516 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5517 list_del_init(&rsrc_blk->list);
5518 kfree(rsrc_blk);
5519 }
5520 break;
5521 case LPFC_RSC_TYPE_FCOE_VFI:
5522 kfree(phba->sli4_hba.vfi_bmask);
5523 kfree(phba->sli4_hba.vfi_ids);
5524 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5525 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5526 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5527 list_del_init(&rsrc_blk->list);
5528 kfree(rsrc_blk);
5529 }
5530 break;
5531 case LPFC_RSC_TYPE_FCOE_RPI:
5532 /* RPI bitmask and physical id array are cleaned up earlier. */
5533 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5534 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5535 list_del_init(&rsrc_blk->list);
5536 kfree(rsrc_blk);
5537 }
5538 break;
5539 default:
5540 break;
5541 }
5542
5543 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5544
5545 out_free_mbox:
5546 mempool_free(mbox, phba->mbox_mem_pool);
5547 return rc;
5548}
5549
5550/**
5551 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5552 * @phba: Pointer to HBA context object.
5553 *
5554 * This function allocates all SLI4 resource identifiers.
5555 **/
5556int
5557lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5558{
5559 int i, rc, error = 0;
5560 uint16_t count, base;
5561 unsigned long longs;
5562
James Smartff78d8f2011-12-13 13:21:35 -05005563 if (!phba->sli4_hba.rpi_hdrs_in_use)
5564 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005565 if (phba->sli4_hba.extents_in_use) {
5566 /*
5567 * The port supports resource extents. The XRI, VPI, VFI, RPI
5568 * resource extent count must be read and allocated before
5569 * provisioning the resource id arrays.
5570 */
5571 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5572 LPFC_IDX_RSRC_RDY) {
5573 /*
5574 * Extent-based resources are set - the driver could
5575 * be in a port reset. Figure out if any corrective
5576 * actions need to be taken.
5577 */
5578 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5579 LPFC_RSC_TYPE_FCOE_VFI);
5580 if (rc != 0)
5581 error++;
5582 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5583 LPFC_RSC_TYPE_FCOE_VPI);
5584 if (rc != 0)
5585 error++;
5586 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5587 LPFC_RSC_TYPE_FCOE_XRI);
5588 if (rc != 0)
5589 error++;
5590 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5591 LPFC_RSC_TYPE_FCOE_RPI);
5592 if (rc != 0)
5593 error++;
5594
5595 /*
5596 * It's possible that the number of resources
5597 * provided to this port instance changed between
5598 * resets. Detect this condition and reallocate
5599 * resources. Otherwise, there is no action.
5600 */
5601 if (error) {
5602 lpfc_printf_log(phba, KERN_INFO,
5603 LOG_MBOX | LOG_INIT,
5604 "2931 Detected extent resource "
5605 "change. Reallocating all "
5606 "extents.\n");
5607 rc = lpfc_sli4_dealloc_extent(phba,
5608 LPFC_RSC_TYPE_FCOE_VFI);
5609 rc = lpfc_sli4_dealloc_extent(phba,
5610 LPFC_RSC_TYPE_FCOE_VPI);
5611 rc = lpfc_sli4_dealloc_extent(phba,
5612 LPFC_RSC_TYPE_FCOE_XRI);
5613 rc = lpfc_sli4_dealloc_extent(phba,
5614 LPFC_RSC_TYPE_FCOE_RPI);
5615 } else
5616 return 0;
5617 }
5618
5619 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5620 if (unlikely(rc))
5621 goto err_exit;
5622
5623 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5624 if (unlikely(rc))
5625 goto err_exit;
5626
5627 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5628 if (unlikely(rc))
5629 goto err_exit;
5630
5631 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5632 if (unlikely(rc))
5633 goto err_exit;
5634 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5635 LPFC_IDX_RSRC_RDY);
5636 return rc;
5637 } else {
5638 /*
5639 * The port does not support resource extents. The XRI, VPI,
5640 * VFI, RPI resource ids were determined from READ_CONFIG.
5641 * Just allocate the bitmasks and provision the resource id
5642 * arrays. If a port reset is active, the resources don't
5643 * need any action - just exit.
5644 */
5645 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005646 LPFC_IDX_RSRC_RDY) {
5647 lpfc_sli4_dealloc_resource_identifiers(phba);
5648 lpfc_sli4_remove_rpis(phba);
5649 }
James Smart6d368e52011-05-24 11:44:12 -04005650 /* RPIs. */
5651 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005652 if (count <= 0) {
5653 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5654 "3279 Invalid provisioning of "
5655 "rpi:%d\n", count);
5656 rc = -EINVAL;
5657 goto err_exit;
5658 }
James Smart6d368e52011-05-24 11:44:12 -04005659 base = phba->sli4_hba.max_cfg_param.rpi_base;
5660 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5661 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5662 sizeof(unsigned long),
5663 GFP_KERNEL);
5664 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5665 rc = -ENOMEM;
5666 goto err_exit;
5667 }
5668 phba->sli4_hba.rpi_ids = kzalloc(count *
5669 sizeof(uint16_t),
5670 GFP_KERNEL);
5671 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5672 rc = -ENOMEM;
5673 goto free_rpi_bmask;
5674 }
5675
5676 for (i = 0; i < count; i++)
5677 phba->sli4_hba.rpi_ids[i] = base + i;
5678
5679 /* VPIs. */
5680 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005681 if (count <= 0) {
5682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5683 "3280 Invalid provisioning of "
5684 "vpi:%d\n", count);
5685 rc = -EINVAL;
5686 goto free_rpi_ids;
5687 }
James Smart6d368e52011-05-24 11:44:12 -04005688 base = phba->sli4_hba.max_cfg_param.vpi_base;
5689 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5690 phba->vpi_bmask = kzalloc(longs *
5691 sizeof(unsigned long),
5692 GFP_KERNEL);
5693 if (unlikely(!phba->vpi_bmask)) {
5694 rc = -ENOMEM;
5695 goto free_rpi_ids;
5696 }
5697 phba->vpi_ids = kzalloc(count *
5698 sizeof(uint16_t),
5699 GFP_KERNEL);
5700 if (unlikely(!phba->vpi_ids)) {
5701 rc = -ENOMEM;
5702 goto free_vpi_bmask;
5703 }
5704
5705 for (i = 0; i < count; i++)
5706 phba->vpi_ids[i] = base + i;
5707
5708 /* XRIs. */
5709 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005710 if (count <= 0) {
5711 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5712 "3281 Invalid provisioning of "
5713 "xri:%d\n", count);
5714 rc = -EINVAL;
5715 goto free_vpi_ids;
5716 }
James Smart6d368e52011-05-24 11:44:12 -04005717 base = phba->sli4_hba.max_cfg_param.xri_base;
5718 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5719 phba->sli4_hba.xri_bmask = kzalloc(longs *
5720 sizeof(unsigned long),
5721 GFP_KERNEL);
5722 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5723 rc = -ENOMEM;
5724 goto free_vpi_ids;
5725 }
James Smart41899be2012-03-01 22:34:19 -05005726 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005727 phba->sli4_hba.xri_ids = kzalloc(count *
5728 sizeof(uint16_t),
5729 GFP_KERNEL);
5730 if (unlikely(!phba->sli4_hba.xri_ids)) {
5731 rc = -ENOMEM;
5732 goto free_xri_bmask;
5733 }
5734
5735 for (i = 0; i < count; i++)
5736 phba->sli4_hba.xri_ids[i] = base + i;
5737
5738 /* VFIs. */
5739 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005740 if (count <= 0) {
5741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5742 "3282 Invalid provisioning of "
5743 "vfi:%d\n", count);
5744 rc = -EINVAL;
5745 goto free_xri_ids;
5746 }
James Smart6d368e52011-05-24 11:44:12 -04005747 base = phba->sli4_hba.max_cfg_param.vfi_base;
5748 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5749 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5750 sizeof(unsigned long),
5751 GFP_KERNEL);
5752 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5753 rc = -ENOMEM;
5754 goto free_xri_ids;
5755 }
5756 phba->sli4_hba.vfi_ids = kzalloc(count *
5757 sizeof(uint16_t),
5758 GFP_KERNEL);
5759 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5760 rc = -ENOMEM;
5761 goto free_vfi_bmask;
5762 }
5763
5764 for (i = 0; i < count; i++)
5765 phba->sli4_hba.vfi_ids[i] = base + i;
5766
5767 /*
5768 * Mark all resources ready. An HBA reset doesn't need
5769 * to reset the initialization.
5770 */
5771 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5772 LPFC_IDX_RSRC_RDY);
5773 return 0;
5774 }
5775
5776 free_vfi_bmask:
5777 kfree(phba->sli4_hba.vfi_bmask);
5778 free_xri_ids:
5779 kfree(phba->sli4_hba.xri_ids);
5780 free_xri_bmask:
5781 kfree(phba->sli4_hba.xri_bmask);
5782 free_vpi_ids:
5783 kfree(phba->vpi_ids);
5784 free_vpi_bmask:
5785 kfree(phba->vpi_bmask);
5786 free_rpi_ids:
5787 kfree(phba->sli4_hba.rpi_ids);
5788 free_rpi_bmask:
5789 kfree(phba->sli4_hba.rpi_bmask);
5790 err_exit:
5791 return rc;
5792}
5793
5794/**
5795 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5796 * @phba: Pointer to HBA context object.
5797 *
5798 * This function allocates the number of elements for the specified
5799 * resource type.
5800 **/
5801int
5802lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5803{
5804 if (phba->sli4_hba.extents_in_use) {
5805 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5806 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5807 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5808 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5809 } else {
5810 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005811 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005812 kfree(phba->vpi_ids);
5813 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5814 kfree(phba->sli4_hba.xri_bmask);
5815 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005816 kfree(phba->sli4_hba.vfi_bmask);
5817 kfree(phba->sli4_hba.vfi_ids);
5818 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5819 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5820 }
5821
5822 return 0;
5823}
5824
5825/**
James Smartb76f2dc2011-07-22 18:37:42 -04005826 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5827 * @phba: Pointer to HBA context object.
5828 * @type: The resource extent type.
5829 * @extnt_count: buffer to hold port extent count response
5830 * @extnt_size: buffer to hold port extent size response.
5831 *
5832 * This function calls the port to read the host allocated extents
5833 * for a particular type.
5834 **/
5835int
5836lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5837 uint16_t *extnt_cnt, uint16_t *extnt_size)
5838{
5839 bool emb;
5840 int rc = 0;
5841 uint16_t curr_blks = 0;
5842 uint32_t req_len, emb_len;
5843 uint32_t alloc_len, mbox_tmo;
5844 struct list_head *blk_list_head;
5845 struct lpfc_rsrc_blks *rsrc_blk;
5846 LPFC_MBOXQ_t *mbox;
5847 void *virtaddr = NULL;
5848 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5849 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5850 union lpfc_sli4_cfg_shdr *shdr;
5851
5852 switch (type) {
5853 case LPFC_RSC_TYPE_FCOE_VPI:
5854 blk_list_head = &phba->lpfc_vpi_blk_list;
5855 break;
5856 case LPFC_RSC_TYPE_FCOE_XRI:
5857 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5858 break;
5859 case LPFC_RSC_TYPE_FCOE_VFI:
5860 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5861 break;
5862 case LPFC_RSC_TYPE_FCOE_RPI:
5863 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5864 break;
5865 default:
5866 return -EIO;
5867 }
5868
5869 /* Count the number of extents currently allocatd for this type. */
5870 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5871 if (curr_blks == 0) {
5872 /*
5873 * The GET_ALLOCATED mailbox does not return the size,
5874 * just the count. The size should be just the size
5875 * stored in the current allocated block and all sizes
5876 * for an extent type are the same so set the return
5877 * value now.
5878 */
5879 *extnt_size = rsrc_blk->rsrc_size;
5880 }
5881 curr_blks++;
5882 }
5883
5884 /* Calculate the total requested length of the dma memory. */
5885 req_len = curr_blks * sizeof(uint16_t);
5886
5887 /*
5888 * Calculate the size of an embedded mailbox. The uint32_t
5889 * accounts for extents-specific word.
5890 */
5891 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5892 sizeof(uint32_t);
5893
5894 /*
5895 * Presume the allocation and response will fit into an embedded
5896 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5897 */
5898 emb = LPFC_SLI4_MBX_EMBED;
5899 req_len = emb_len;
5900 if (req_len > emb_len) {
5901 req_len = curr_blks * sizeof(uint16_t) +
5902 sizeof(union lpfc_sli4_cfg_shdr) +
5903 sizeof(uint32_t);
5904 emb = LPFC_SLI4_MBX_NEMBED;
5905 }
5906
5907 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5908 if (!mbox)
5909 return -ENOMEM;
5910 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5911
5912 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5913 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5914 req_len, emb);
5915 if (alloc_len < req_len) {
5916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5917 "2983 Allocated DMA memory size (x%x) is "
5918 "less than the requested DMA memory "
5919 "size (x%x)\n", alloc_len, req_len);
5920 rc = -ENOMEM;
5921 goto err_exit;
5922 }
5923 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5924 if (unlikely(rc)) {
5925 rc = -EIO;
5926 goto err_exit;
5927 }
5928
5929 if (!phba->sli4_hba.intr_enable)
5930 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5931 else {
James Smarta183a152011-10-10 21:32:43 -04005932 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005933 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5934 }
5935
5936 if (unlikely(rc)) {
5937 rc = -EIO;
5938 goto err_exit;
5939 }
5940
5941 /*
5942 * Figure out where the response is located. Then get local pointers
5943 * to the response data. The port does not guarantee to respond to
5944 * all extents counts request so update the local variable with the
5945 * allocated count from the port.
5946 */
5947 if (emb == LPFC_SLI4_MBX_EMBED) {
5948 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5949 shdr = &rsrc_ext->header.cfg_shdr;
5950 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5951 } else {
5952 virtaddr = mbox->sge_array->addr[0];
5953 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5954 shdr = &n_rsrc->cfg_shdr;
5955 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5956 }
5957
5958 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5959 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5960 "2984 Failed to read allocated resources "
5961 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5962 type,
5963 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5964 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5965 rc = -EIO;
5966 goto err_exit;
5967 }
5968 err_exit:
5969 lpfc_sli4_mbox_cmd_free(phba, mbox);
5970 return rc;
5971}
5972
5973/**
James Smart8a9d2e82012-05-09 21:16:12 -04005974 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5975 * @phba: pointer to lpfc hba data structure.
5976 *
5977 * This routine walks the list of els buffers that have been allocated and
5978 * repost them to the port by using SGL block post. This is needed after a
5979 * pci_function_reset/warm_start or start. It attempts to construct blocks
5980 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5981 * SGL block post mailbox commands to post them to the port. For single els
5982 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5983 * mailbox command for posting.
5984 *
5985 * Returns: 0 = success, non-zero failure.
5986 **/
5987static int
5988lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5989{
5990 struct lpfc_sglq *sglq_entry = NULL;
5991 struct lpfc_sglq *sglq_entry_next = NULL;
5992 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04005993 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04005994 int last_xritag = NO_XRI;
5995 LIST_HEAD(prep_sgl_list);
5996 LIST_HEAD(blck_sgl_list);
5997 LIST_HEAD(allc_sgl_list);
5998 LIST_HEAD(post_sgl_list);
5999 LIST_HEAD(free_sgl_list);
6000
James Smart38c20672013-03-01 16:37:44 -05006001 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006002 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05006003 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006004
James Smart711ea882013-04-17 20:18:29 -04006005 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006006 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6007 &allc_sgl_list, list) {
6008 list_del_init(&sglq_entry->list);
6009 block_cnt++;
6010 if ((last_xritag != NO_XRI) &&
6011 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6012 /* a hole in xri block, form a sgl posting block */
6013 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6014 post_cnt = block_cnt - 1;
6015 /* prepare list for next posting block */
6016 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6017 block_cnt = 1;
6018 } else {
6019 /* prepare list for next posting block */
6020 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6021 /* enough sgls for non-embed sgl mbox command */
6022 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6023 list_splice_init(&prep_sgl_list,
6024 &blck_sgl_list);
6025 post_cnt = block_cnt;
6026 block_cnt = 0;
6027 }
6028 }
6029 num_posted++;
6030
6031 /* keep track of last sgl's xritag */
6032 last_xritag = sglq_entry->sli4_xritag;
6033
6034 /* end of repost sgl list condition for els buffers */
6035 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6036 if (post_cnt == 0) {
6037 list_splice_init(&prep_sgl_list,
6038 &blck_sgl_list);
6039 post_cnt = block_cnt;
6040 } else if (block_cnt == 1) {
6041 status = lpfc_sli4_post_sgl(phba,
6042 sglq_entry->phys, 0,
6043 sglq_entry->sli4_xritag);
6044 if (!status) {
6045 /* successful, put sgl to posted list */
6046 list_add_tail(&sglq_entry->list,
6047 &post_sgl_list);
6048 } else {
6049 /* Failure, put sgl to free list */
6050 lpfc_printf_log(phba, KERN_WARNING,
6051 LOG_SLI,
6052 "3159 Failed to post els "
6053 "sgl, xritag:x%x\n",
6054 sglq_entry->sli4_xritag);
6055 list_add_tail(&sglq_entry->list,
6056 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006057 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006058 }
6059 }
6060 }
6061
6062 /* continue until a nembed page worth of sgls */
6063 if (post_cnt == 0)
6064 continue;
6065
6066 /* post the els buffer list sgls as a block */
6067 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6068 post_cnt);
6069
6070 if (!status) {
6071 /* success, put sgl list to posted sgl list */
6072 list_splice_init(&blck_sgl_list, &post_sgl_list);
6073 } else {
6074 /* Failure, put sgl list to free sgl list */
6075 sglq_entry_first = list_first_entry(&blck_sgl_list,
6076 struct lpfc_sglq,
6077 list);
6078 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6079 "3160 Failed to post els sgl-list, "
6080 "xritag:x%x-x%x\n",
6081 sglq_entry_first->sli4_xritag,
6082 (sglq_entry_first->sli4_xritag +
6083 post_cnt - 1));
6084 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006085 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006086 }
6087
6088 /* don't reset xirtag due to hole in xri block */
6089 if (block_cnt == 0)
6090 last_xritag = NO_XRI;
6091
6092 /* reset els sgl post count for next round of posting */
6093 post_cnt = 0;
6094 }
James Smart711ea882013-04-17 20:18:29 -04006095 /* update the number of XRIs posted for ELS */
6096 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006097
6098 /* free the els sgls failed to post */
6099 lpfc_free_sgl_list(phba, &free_sgl_list);
6100
6101 /* push els sgls posted to the availble list */
6102 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006103 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006104 list_splice_init(&post_sgl_list,
6105 &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05006106 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006107 } else {
6108 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6109 "3161 Failure to post els sgl to port.\n");
6110 return -EIO;
6111 }
6112 return 0;
6113}
6114
6115/**
James Smartda0436e2009-05-22 14:51:39 -04006116 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6117 * @phba: Pointer to HBA context object.
6118 *
6119 * This function is the main SLI4 device intialization PCI function. This
6120 * function is called by the HBA intialization code, HBA reset code and
6121 * HBA error attention handler code. Caller is not required to hold any
6122 * locks.
6123 **/
6124int
6125lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6126{
6127 int rc;
6128 LPFC_MBOXQ_t *mboxq;
6129 struct lpfc_mqe *mqe;
6130 uint8_t *vpd;
6131 uint32_t vpd_size;
6132 uint32_t ftr_rsp = 0;
6133 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6134 struct lpfc_vport *vport = phba->pport;
6135 struct lpfc_dmabuf *mp;
6136
6137 /* Perform a PCI function reset to start from clean */
6138 rc = lpfc_pci_function_reset(phba);
6139 if (unlikely(rc))
6140 return -ENODEV;
6141
6142 /* Check the HBA Host Status Register for readyness */
6143 rc = lpfc_sli4_post_status_check(phba);
6144 if (unlikely(rc))
6145 return -ENODEV;
6146 else {
6147 spin_lock_irq(&phba->hbalock);
6148 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6149 spin_unlock_irq(&phba->hbalock);
6150 }
6151
6152 /*
6153 * Allocate a single mailbox container for initializing the
6154 * port.
6155 */
6156 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6157 if (!mboxq)
6158 return -ENOMEM;
6159
James Smartda0436e2009-05-22 14:51:39 -04006160 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006161 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006162 vpd = kzalloc(vpd_size, GFP_KERNEL);
6163 if (!vpd) {
6164 rc = -ENOMEM;
6165 goto out_free_mbox;
6166 }
6167
6168 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006169 if (unlikely(rc)) {
6170 kfree(vpd);
6171 goto out_free_mbox;
6172 }
James Smart572709e2013-07-15 18:32:43 -04006173
James Smartda0436e2009-05-22 14:51:39 -04006174 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006175 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6176 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006177 phba->hba_flag |= HBA_FCOE_MODE;
6178 else
6179 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006180
6181 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6182 LPFC_DCBX_CEE_MODE)
6183 phba->hba_flag |= HBA_FIP_SUPPORT;
6184 else
6185 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6186
James Smart4f2e66c2012-05-09 21:17:07 -04006187 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6188
James Smartc31098c2011-04-16 11:03:33 -04006189 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006190 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6191 "0376 READ_REV Error. SLI Level %d "
6192 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006193 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006194 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006195 kfree(vpd);
6196 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006197 }
James Smartcd1c8302011-10-10 21:33:25 -04006198
6199 /*
James Smartff78d8f2011-12-13 13:21:35 -05006200 * Continue initialization with default values even if driver failed
6201 * to read FCoE param config regions, only read parameters if the
6202 * board is FCoE
6203 */
6204 if (phba->hba_flag & HBA_FCOE_MODE &&
6205 lpfc_sli4_read_fcoe_params(phba))
6206 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6207 "2570 Failed to read FCoE parameters\n");
6208
6209 /*
James Smartcd1c8302011-10-10 21:33:25 -04006210 * Retrieve sli4 device physical port name, failure of doing it
6211 * is considered as non-fatal.
6212 */
6213 rc = lpfc_sli4_retrieve_pport_name(phba);
6214 if (!rc)
6215 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6216 "3080 Successful retrieving SLI4 device "
6217 "physical port name: %s.\n", phba->Port);
6218
James Smartda0436e2009-05-22 14:51:39 -04006219 /*
6220 * Evaluate the read rev and vpd data. Populate the driver
6221 * state with the results. If this routine fails, the failure
6222 * is not fatal as the driver will use generic values.
6223 */
6224 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6225 if (unlikely(!rc)) {
6226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6227 "0377 Error %d parsing vpd. "
6228 "Using defaults.\n", rc);
6229 rc = 0;
6230 }
James Smart76a95d72010-11-20 23:11:48 -05006231 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006232
James Smartf1126682009-06-10 17:22:44 -04006233 /* Save information as VPD data */
6234 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6235 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6236 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6237 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6238 &mqe->un.read_rev);
6239 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6240 &mqe->un.read_rev);
6241 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6242 &mqe->un.read_rev);
6243 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6244 &mqe->un.read_rev);
6245 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6246 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6247 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6248 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6249 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6250 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6251 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6252 "(%d):0380 READ_REV Status x%x "
6253 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6254 mboxq->vport ? mboxq->vport->vpi : 0,
6255 bf_get(lpfc_mqe_status, mqe),
6256 phba->vpd.rev.opFwName,
6257 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6258 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006259
James Smart572709e2013-07-15 18:32:43 -04006260 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6261 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6262 if (phba->pport->cfg_lun_queue_depth > rc) {
6263 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6264 "3362 LUN queue depth changed from %d to %d\n",
6265 phba->pport->cfg_lun_queue_depth, rc);
6266 phba->pport->cfg_lun_queue_depth = rc;
6267 }
6268
6269
James Smartda0436e2009-05-22 14:51:39 -04006270 /*
6271 * Discover the port's supported feature set and match it against the
6272 * hosts requests.
6273 */
6274 lpfc_request_features(phba, mboxq);
6275 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6276 if (unlikely(rc)) {
6277 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006278 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006279 }
6280
6281 /*
6282 * The port must support FCP initiator mode as this is the
6283 * only mode running in the host.
6284 */
6285 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6286 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6287 "0378 No support for fcpi mode.\n");
6288 ftr_rsp++;
6289 }
James Smartfedd3b72011-02-16 12:39:24 -05006290 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6291 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6292 else
6293 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006294 /*
6295 * If the port cannot support the host's requested features
6296 * then turn off the global config parameters to disable the
6297 * feature in the driver. This is not a fatal error.
6298 */
James Smartbf086112011-08-21 21:48:13 -04006299 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6300 if (phba->cfg_enable_bg) {
6301 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6302 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6303 else
6304 ftr_rsp++;
6305 }
James Smartda0436e2009-05-22 14:51:39 -04006306
6307 if (phba->max_vpi && phba->cfg_enable_npiv &&
6308 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6309 ftr_rsp++;
6310
6311 if (ftr_rsp) {
6312 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6313 "0379 Feature Mismatch Data: x%08x %08x "
6314 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6315 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6316 phba->cfg_enable_npiv, phba->max_vpi);
6317 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6318 phba->cfg_enable_bg = 0;
6319 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6320 phba->cfg_enable_npiv = 0;
6321 }
6322
6323 /* These SLI3 features are assumed in SLI4 */
6324 spin_lock_irq(&phba->hbalock);
6325 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6326 spin_unlock_irq(&phba->hbalock);
6327
James Smart6d368e52011-05-24 11:44:12 -04006328 /*
6329 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6330 * calls depends on these resources to complete port setup.
6331 */
6332 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6333 if (rc) {
6334 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6335 "2920 Failed to alloc Resource IDs "
6336 "rc = x%x\n", rc);
6337 goto out_free_mbox;
6338 }
6339
James Smartda0436e2009-05-22 14:51:39 -04006340 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006341 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6342 if (rc) {
6343 phba->link_state = LPFC_HBA_ERROR;
6344 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006345 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006346 }
6347
James Smartda0436e2009-05-22 14:51:39 -04006348 mboxq->vport = vport;
6349 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6350 mp = (struct lpfc_dmabuf *) mboxq->context1;
6351 if (rc == MBX_SUCCESS) {
6352 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6353 rc = 0;
6354 }
6355
6356 /*
6357 * This memory was allocated by the lpfc_read_sparam routine. Release
6358 * it to the mbuf pool.
6359 */
6360 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6361 kfree(mp);
6362 mboxq->context1 = NULL;
6363 if (unlikely(rc)) {
6364 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6365 "0382 READ_SPARAM command failed "
6366 "status %d, mbxStatus x%x\n",
6367 rc, bf_get(lpfc_mqe_status, mqe));
6368 phba->link_state = LPFC_HBA_ERROR;
6369 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006370 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006371 }
6372
James Smart05580562011-05-24 11:40:48 -04006373 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006374
6375 /* Update the fc_host data structures with new wwn. */
6376 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6377 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6378
James Smart8a9d2e82012-05-09 21:16:12 -04006379 /* update host els and scsi xri-sgl sizes and mappings */
6380 rc = lpfc_sli4_xri_sgl_update(phba);
6381 if (unlikely(rc)) {
6382 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6383 "1400 Failed to update xri-sgl size and "
6384 "mapping: %d\n", rc);
6385 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006386 }
6387
James Smart8a9d2e82012-05-09 21:16:12 -04006388 /* register the els sgl pool to the port */
6389 rc = lpfc_sli4_repost_els_sgl_list(phba);
6390 if (unlikely(rc)) {
6391 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6392 "0582 Error %d during els sgl post "
6393 "operation\n", rc);
6394 rc = -ENODEV;
6395 goto out_free_mbox;
6396 }
6397
6398 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006399 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6400 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006401 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006402 "0383 Error %d during scsi sgl post "
6403 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006404 /* Some Scsi buffers were moved to the abort scsi list */
6405 /* A pci function reset will repost them */
6406 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006407 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006408 }
6409
6410 /* Post the rpi header region to the device. */
6411 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6412 if (unlikely(rc)) {
6413 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6414 "0393 Error %d during rpi post operation\n",
6415 rc);
6416 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006417 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006418 }
James Smart97f2ecf2012-03-01 22:35:23 -05006419 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006420
James Smart5350d872011-10-10 21:33:49 -04006421 /* Create all the SLI4 queues */
6422 rc = lpfc_sli4_queue_create(phba);
6423 if (rc) {
6424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6425 "3089 Failed to allocate queues\n");
6426 rc = -ENODEV;
6427 goto out_stop_timers;
6428 }
James Smartda0436e2009-05-22 14:51:39 -04006429 /* Set up all the queues to the device */
6430 rc = lpfc_sli4_queue_setup(phba);
6431 if (unlikely(rc)) {
6432 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6433 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006434 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006435 }
6436
6437 /* Arm the CQs and then EQs on device */
6438 lpfc_sli4_arm_cqeq_intr(phba);
6439
6440 /* Indicate device interrupt mode */
6441 phba->sli4_hba.intr_enable = 1;
6442
6443 /* Allow asynchronous mailbox command to go through */
6444 spin_lock_irq(&phba->hbalock);
6445 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6446 spin_unlock_irq(&phba->hbalock);
6447
6448 /* Post receive buffers to the device */
6449 lpfc_sli4_rb_setup(phba);
6450
James Smartfc2b9892010-02-26 14:15:29 -05006451 /* Reset HBA FCF states after HBA reset */
6452 phba->fcf.fcf_flag = 0;
6453 phba->fcf.current_rec.flag = 0;
6454
James Smartda0436e2009-05-22 14:51:39 -04006455 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006456 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006457 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006458
6459 /* Start heart beat timer */
6460 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006461 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006462 phba->hb_outstanding = 0;
6463 phba->last_completion_time = jiffies;
6464
6465 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006466 mod_timer(&phba->eratt_poll,
6467 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006468
James Smart75baf692010-06-08 18:31:21 -04006469 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6470 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6471 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6472 if (!rc) {
6473 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6474 "2829 This device supports "
6475 "Advanced Error Reporting (AER)\n");
6476 spin_lock_irq(&phba->hbalock);
6477 phba->hba_flag |= HBA_AER_ENABLED;
6478 spin_unlock_irq(&phba->hbalock);
6479 } else {
6480 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6481 "2830 This device does not support "
6482 "Advanced Error Reporting (AER)\n");
6483 phba->cfg_aer_support = 0;
6484 }
James Smart0a96e972011-07-22 18:37:28 -04006485 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006486 }
6487
James Smart76a95d72010-11-20 23:11:48 -05006488 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6489 /*
6490 * The FC Port needs to register FCFI (index 0)
6491 */
6492 lpfc_reg_fcfi(phba, mboxq);
6493 mboxq->vport = phba->pport;
6494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006495 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006496 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006497 rc = 0;
6498 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6499 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006500
6501 /* Check if the port is configured to be disabled */
6502 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006503 }
James Smart026abb82011-12-13 13:20:45 -05006504
James Smartda0436e2009-05-22 14:51:39 -04006505 /*
6506 * The port is ready, set the host's link state to LINK_DOWN
6507 * in preparation for link interrupts.
6508 */
James Smartda0436e2009-05-22 14:51:39 -04006509 spin_lock_irq(&phba->hbalock);
6510 phba->link_state = LPFC_LINK_DOWN;
6511 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006512 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6513 (phba->hba_flag & LINK_DISABLED)) {
6514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6515 "3103 Adapter Link is disabled.\n");
6516 lpfc_down_link(phba, mboxq);
6517 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6518 if (rc != MBX_SUCCESS) {
6519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6520 "3104 Adapter failed to issue "
6521 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6522 goto out_unset_queue;
6523 }
6524 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006525 /* don't perform init_link on SLI4 FC port loopback test */
6526 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6527 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6528 if (rc)
6529 goto out_unset_queue;
6530 }
James Smart5350d872011-10-10 21:33:49 -04006531 }
6532 mempool_free(mboxq, phba->mbox_mem_pool);
6533 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006534out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006535 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006536 lpfc_sli4_queue_unset(phba);
6537out_destroy_queue:
6538 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006539out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006540 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006541out_free_mbox:
6542 mempool_free(mboxq, phba->mbox_mem_pool);
6543 return rc;
6544}
James Smarte59058c2008-08-24 21:49:00 -04006545
6546/**
James Smart3621a712009-04-06 18:47:14 -04006547 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006548 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006549 *
James Smarte59058c2008-08-24 21:49:00 -04006550 * This is the callback function for mailbox timer. The mailbox
6551 * timer is armed when a new mailbox command is issued and the timer
6552 * is deleted when the mailbox complete. The function is called by
6553 * the kernel timer code when a mailbox does not complete within
6554 * expected time. This function wakes up the worker thread to
6555 * process the mailbox timeout and returns. All the processing is
6556 * done by the worker thread function lpfc_mbox_timeout_handler.
6557 **/
dea31012005-04-17 16:05:31 -05006558void
6559lpfc_mbox_timeout(unsigned long ptr)
6560{
James Smart92d7f7b2007-06-17 19:56:38 -05006561 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006562 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006563 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006564
James Smart2e0fef82007-06-17 19:56:36 -05006565 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006566 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006567 if (!tmo_posted)
6568 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6569 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6570
James Smart5e9d9b82008-06-14 22:52:53 -04006571 if (!tmo_posted)
6572 lpfc_worker_wake_up(phba);
6573 return;
dea31012005-04-17 16:05:31 -05006574}
6575
James Smarte8d3c3b2013-10-10 12:21:30 -04006576/**
6577 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6578 * are pending
6579 * @phba: Pointer to HBA context object.
6580 *
6581 * This function checks if any mailbox completions are present on the mailbox
6582 * completion queue.
6583 **/
6584bool
6585lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6586{
6587
6588 uint32_t idx;
6589 struct lpfc_queue *mcq;
6590 struct lpfc_mcqe *mcqe;
6591 bool pending_completions = false;
6592
6593 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6594 return false;
6595
6596 /* Check for completions on mailbox completion queue */
6597
6598 mcq = phba->sli4_hba.mbx_cq;
6599 idx = mcq->hba_index;
6600 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6601 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6602 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6603 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6604 pending_completions = true;
6605 break;
6606 }
6607 idx = (idx + 1) % mcq->entry_count;
6608 if (mcq->hba_index == idx)
6609 break;
6610 }
6611 return pending_completions;
6612
6613}
6614
6615/**
6616 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6617 * that were missed.
6618 * @phba: Pointer to HBA context object.
6619 *
6620 * For sli4, it is possible to miss an interrupt. As such mbox completions
6621 * maybe missed causing erroneous mailbox timeouts to occur. This function
6622 * checks to see if mbox completions are on the mailbox completion queue
6623 * and will process all the completions associated with the eq for the
6624 * mailbox completion queue.
6625 **/
6626bool
6627lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6628{
6629
6630 uint32_t eqidx;
6631 struct lpfc_queue *fpeq = NULL;
6632 struct lpfc_eqe *eqe;
6633 bool mbox_pending;
6634
6635 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6636 return false;
6637
6638 /* Find the eq associated with the mcq */
6639
6640 if (phba->sli4_hba.hba_eq)
6641 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6642 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6643 phba->sli4_hba.mbx_cq->assoc_qid) {
6644 fpeq = phba->sli4_hba.hba_eq[eqidx];
6645 break;
6646 }
6647 if (!fpeq)
6648 return false;
6649
6650 /* Turn off interrupts from this EQ */
6651
6652 lpfc_sli4_eq_clr_intr(fpeq);
6653
6654 /* Check to see if a mbox completion is pending */
6655
6656 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6657
6658 /*
6659 * If a mbox completion is pending, process all the events on EQ
6660 * associated with the mbox completion queue (this could include
6661 * mailbox commands, async events, els commands, receive queue data
6662 * and fcp commands)
6663 */
6664
6665 if (mbox_pending)
6666 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6667 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6668 fpeq->EQ_processed++;
6669 }
6670
6671 /* Always clear and re-arm the EQ */
6672
6673 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6674
6675 return mbox_pending;
6676
6677}
James Smarte59058c2008-08-24 21:49:00 -04006678
6679/**
James Smart3621a712009-04-06 18:47:14 -04006680 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006681 * @phba: Pointer to HBA context object.
6682 *
6683 * This function is called from worker thread when a mailbox command times out.
6684 * The caller is not required to hold any locks. This function will reset the
6685 * HBA and recover all the pending commands.
6686 **/
dea31012005-04-17 16:05:31 -05006687void
6688lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6689{
James Smart2e0fef82007-06-17 19:56:36 -05006690 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006691 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006692 struct lpfc_sli *psli = &phba->sli;
6693 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006694
James Smarte8d3c3b2013-10-10 12:21:30 -04006695 /* If the mailbox completed, process the completion and return */
6696 if (lpfc_sli4_process_missed_mbox_completions(phba))
6697 return;
6698
James Smarta257bf92009-04-06 18:48:10 -04006699 /* Check the pmbox pointer first. There is a race condition
6700 * between the mbox timeout handler getting executed in the
6701 * worklist and the mailbox actually completing. When this
6702 * race condition occurs, the mbox_active will be NULL.
6703 */
6704 spin_lock_irq(&phba->hbalock);
6705 if (pmbox == NULL) {
6706 lpfc_printf_log(phba, KERN_WARNING,
6707 LOG_MBOX | LOG_SLI,
6708 "0353 Active Mailbox cleared - mailbox timeout "
6709 "exiting\n");
6710 spin_unlock_irq(&phba->hbalock);
6711 return;
6712 }
6713
dea31012005-04-17 16:05:31 -05006714 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006715 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006716 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006717 mb->mbxCommand,
6718 phba->pport->port_state,
6719 phba->sli.sli_flag,
6720 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006721 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006722
James Smart1dcb58e2007-04-25 09:51:30 -04006723 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6724 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006725 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006726 */
James Smart2e0fef82007-06-17 19:56:36 -05006727 spin_lock_irq(&phba->pport->work_port_lock);
6728 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6729 spin_unlock_irq(&phba->pport->work_port_lock);
6730 spin_lock_irq(&phba->hbalock);
6731 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006732 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006733 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006734
6735 pring = &psli->ring[psli->fcp_ring];
6736 lpfc_sli_abort_iocb_ring(phba, pring);
6737
6738 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006739 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006740
6741 /* Reset the HBA device */
6742 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006743}
6744
James Smarte59058c2008-08-24 21:49:00 -04006745/**
James Smart3772a992009-05-22 14:50:54 -04006746 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006747 * @phba: Pointer to HBA context object.
6748 * @pmbox: Pointer to mailbox object.
6749 * @flag: Flag indicating how the mailbox need to be processed.
6750 *
6751 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006752 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6753 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006754 * The mailbox command can be submitted in polling mode, in which case
6755 * this function will wait in a polling loop for the completion of the
6756 * mailbox.
6757 * If the mailbox is submitted in no_wait mode (not polling) the
6758 * function will submit the command and returns immediately without waiting
6759 * for the mailbox completion. The no_wait is supported only when HBA
6760 * is in SLI2/SLI3 mode - interrupts are enabled.
6761 * The SLI interface allows only one mailbox pending at a time. If the
6762 * mailbox is issued in polling mode and there is already a mailbox
6763 * pending, then the function will return an error. If the mailbox is issued
6764 * in NO_WAIT mode and there is a mailbox pending already, the function
6765 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6766 * The sli layer owns the mailbox object until the completion of mailbox
6767 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6768 * return codes the caller owns the mailbox command after the return of
6769 * the function.
6770 **/
James Smart3772a992009-05-22 14:50:54 -04006771static int
6772lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6773 uint32_t flag)
dea31012005-04-17 16:05:31 -05006774{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006775 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006776 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006777 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006778 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006779 int i;
James Smart09372822008-01-11 01:52:54 -05006780 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006781 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006782 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006783 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006784 int processing_queue = 0;
6785
6786 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6787 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006788 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006789 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006790 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6791 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6792 return MBX_SUCCESS;
6793 }
James Smart58da1ff2008-04-07 10:15:56 -04006794 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006795 pmbox = lpfc_mbox_get(phba);
6796 if (!pmbox) {
6797 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6798 return MBX_SUCCESS;
6799 }
6800 }
dea31012005-04-17 16:05:31 -05006801
James Smarted957682007-06-17 19:56:37 -05006802 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006803 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006804 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006805 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006806 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006807 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006808 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006809 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006810 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006811 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006812 }
6813 }
6814
Linas Vepstas8d63f372007-02-14 14:28:36 -06006815 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006816 if (unlikely(pci_channel_offline(phba->pcidev))) {
6817 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6818 goto out_not_finished;
6819 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006820
James Smarta257bf92009-04-06 18:48:10 -04006821 /* If HBA has a deferred error attention, fail the iocb. */
6822 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6823 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6824 goto out_not_finished;
6825 }
6826
dea31012005-04-17 16:05:31 -05006827 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006828
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006829 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006830 status = MBX_SUCCESS;
6831
James Smart2e0fef82007-06-17 19:56:36 -05006832 if (phba->link_state == LPFC_HBA_ERROR) {
6833 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006834
6835 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006836 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6837 "(%d):0311 Mailbox command x%x cannot "
6838 "issue Data: x%x x%x\n",
6839 pmbox->vport ? pmbox->vport->vpi : 0,
6840 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006841 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006842 }
6843
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006844 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05006845 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6846 !(hc_copy & HC_MBINT_ENA)) {
6847 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6848 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006849 "(%d):2528 Mailbox command x%x cannot "
6850 "issue Data: x%x x%x\n",
6851 pmbox->vport ? pmbox->vport->vpi : 0,
6852 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006853 goto out_not_finished;
6854 }
James Smart92908312006-03-07 15:04:13 -05006855 }
6856
dea31012005-04-17 16:05:31 -05006857 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6858 /* Polling for a mbox command when another one is already active
6859 * is not allowed in SLI. Also, the driver must have established
6860 * SLI2 mode to queue and process multiple mbox commands.
6861 */
6862
6863 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006864 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006865
6866 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006867 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6868 "(%d):2529 Mailbox command x%x "
6869 "cannot issue Data: x%x x%x\n",
6870 pmbox->vport ? pmbox->vport->vpi : 0,
6871 pmbox->u.mb.mbxCommand,
6872 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006873 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006874 }
6875
James Smart3772a992009-05-22 14:50:54 -04006876 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006877 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006878 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006879 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6880 "(%d):2530 Mailbox command x%x "
6881 "cannot issue Data: x%x x%x\n",
6882 pmbox->vport ? pmbox->vport->vpi : 0,
6883 pmbox->u.mb.mbxCommand,
6884 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006885 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006886 }
6887
dea31012005-04-17 16:05:31 -05006888 /* Another mailbox command is still being processed, queue this
6889 * command to be processed later.
6890 */
6891 lpfc_mbox_put(phba, pmbox);
6892
6893 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006894 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006895 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006896 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006897 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006898 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006899 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006900
6901 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006902 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006903
James Smart858c9f62007-06-17 19:56:39 -05006904 if (pmbox->vport) {
6905 lpfc_debugfs_disc_trc(pmbox->vport,
6906 LPFC_DISC_TRC_MBOX_VPORT,
6907 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006908 (uint32_t)mbx->mbxCommand,
6909 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006910 }
6911 else {
6912 lpfc_debugfs_disc_trc(phba->pport,
6913 LPFC_DISC_TRC_MBOX,
6914 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006915 (uint32_t)mbx->mbxCommand,
6916 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006917 }
6918
James Smart2e0fef82007-06-17 19:56:36 -05006919 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006920 }
6921
dea31012005-04-17 16:05:31 -05006922 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6923
6924 /* If we are not polling, we MUST be in SLI2 mode */
6925 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006926 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006927 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006928 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006929 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006930 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006931 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6932 "(%d):2531 Mailbox command x%x "
6933 "cannot issue Data: x%x x%x\n",
6934 pmbox->vport ? pmbox->vport->vpi : 0,
6935 pmbox->u.mb.mbxCommand,
6936 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006937 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006938 }
6939 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04006940 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6941 1000);
6942 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05006943 }
6944
6945 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006946 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006947 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006948 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006949 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006950 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006951 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006952
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006953 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05006954 if (pmbox->vport) {
6955 lpfc_debugfs_disc_trc(pmbox->vport,
6956 LPFC_DISC_TRC_MBOX_VPORT,
6957 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006958 (uint32_t)mbx->mbxCommand,
6959 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006960 }
6961 else {
6962 lpfc_debugfs_disc_trc(phba->pport,
6963 LPFC_DISC_TRC_MBOX,
6964 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006965 (uint32_t)mbx->mbxCommand,
6966 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006967 }
6968 }
6969
dea31012005-04-17 16:05:31 -05006970 psli->slistat.mbox_cmd++;
6971 evtctr = psli->slistat.mbox_event;
6972
6973 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006974 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05006975
James Smart3772a992009-05-22 14:50:54 -04006976 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006977 /* Populate mbox extension offset word. */
6978 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006979 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04006980 = (uint8_t *)phba->mbox_ext
6981 - (uint8_t *)phba->mbox;
6982 }
6983
6984 /* Copy the mailbox extension data */
6985 if (pmbox->in_ext_byte_len && pmbox->context2) {
6986 lpfc_sli_pcimem_bcopy(pmbox->context2,
6987 (uint8_t *)phba->mbox_ext,
6988 pmbox->in_ext_byte_len);
6989 }
6990 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006991 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006992 } else {
James Smart7a470272010-03-15 11:25:20 -04006993 /* Populate mbox extension offset word. */
6994 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006995 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04006996 = MAILBOX_HBA_EXT_OFFSET;
6997
6998 /* Copy the mailbox extension data */
6999 if (pmbox->in_ext_byte_len && pmbox->context2) {
7000 lpfc_memcpy_to_slim(phba->MBslimaddr +
7001 MAILBOX_HBA_EXT_OFFSET,
7002 pmbox->context2, pmbox->in_ext_byte_len);
7003
7004 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007005 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007006 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007007 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007008 }
7009
7010 /* First copy mbox command data to HBA SLIM, skip past first
7011 word */
7012 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007013 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007014 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7015
7016 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007017 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007018 to_slim = phba->MBslimaddr;
7019 writel(ldata, to_slim);
7020 readl(to_slim); /* flush */
7021
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007022 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007023 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007024 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007025 }
7026 }
7027
7028 wmb();
dea31012005-04-17 16:05:31 -05007029
7030 switch (flag) {
7031 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007032 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007033 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007034 /* Interrupt board to do it */
7035 writel(CA_MBATT, phba->CAregaddr);
7036 readl(phba->CAregaddr); /* flush */
7037 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007038 break;
7039
7040 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007041 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007042 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007043 /* Interrupt board to do it */
7044 writel(CA_MBATT, phba->CAregaddr);
7045 readl(phba->CAregaddr); /* flush */
7046
James Smart3772a992009-05-22 14:50:54 -04007047 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007048 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007049 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007050 word0 = le32_to_cpu(word0);
7051 } else {
7052 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007053 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7054 spin_unlock_irqrestore(&phba->hbalock,
7055 drvr_flag);
7056 goto out_not_finished;
7057 }
dea31012005-04-17 16:05:31 -05007058 }
7059
7060 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007061 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7062 spin_unlock_irqrestore(&phba->hbalock,
7063 drvr_flag);
7064 goto out_not_finished;
7065 }
James Smarta183a152011-10-10 21:32:43 -04007066 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7067 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007068 i = 0;
dea31012005-04-17 16:05:31 -05007069 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007070 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7071 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007072 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007073 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007074 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007075 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007076 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007077 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007078 }
7079
7080 /* Check if we took a mbox interrupt while we were
7081 polling */
7082 if (((word0 & OWN_CHIP) != OWN_CHIP)
7083 && (evtctr != psli->slistat.mbox_event))
7084 break;
7085
James Smart09372822008-01-11 01:52:54 -05007086 if (i++ > 10) {
7087 spin_unlock_irqrestore(&phba->hbalock,
7088 drvr_flag);
7089 msleep(1);
7090 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7091 }
dea31012005-04-17 16:05:31 -05007092
James Smart3772a992009-05-22 14:50:54 -04007093 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007094 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007095 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007096 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007097 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007098 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007099 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007100 /* Check real SLIM for any errors */
7101 slimword0 = readl(phba->MBslimaddr);
7102 slimmb = (MAILBOX_t *) & slimword0;
7103 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7104 && slimmb->mbxStatus) {
7105 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007106 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007107 word0 = slimword0;
7108 }
7109 }
7110 } else {
7111 /* First copy command data */
7112 word0 = readl(phba->MBslimaddr);
7113 }
7114 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007115 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7116 spin_unlock_irqrestore(&phba->hbalock,
7117 drvr_flag);
7118 goto out_not_finished;
7119 }
dea31012005-04-17 16:05:31 -05007120 }
7121
James Smart3772a992009-05-22 14:50:54 -04007122 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007123 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007124 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007125 /* Copy the mailbox extension data */
7126 if (pmbox->out_ext_byte_len && pmbox->context2) {
7127 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7128 pmbox->context2,
7129 pmbox->out_ext_byte_len);
7130 }
dea31012005-04-17 16:05:31 -05007131 } else {
7132 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007133 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007134 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007135 /* Copy the mailbox extension data */
7136 if (pmbox->out_ext_byte_len && pmbox->context2) {
7137 lpfc_memcpy_from_slim(pmbox->context2,
7138 phba->MBslimaddr +
7139 MAILBOX_HBA_EXT_OFFSET,
7140 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007141 }
7142 }
7143
7144 writel(HA_MBATT, phba->HAregaddr);
7145 readl(phba->HAregaddr); /* flush */
7146
7147 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007148 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007149 }
7150
James Smart2e0fef82007-06-17 19:56:36 -05007151 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7152 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007153
7154out_not_finished:
7155 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007156 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007157 lpfc_mbox_cmpl_put(phba, pmbox);
7158 }
7159 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007160}
7161
James Smarte59058c2008-08-24 21:49:00 -04007162/**
James Smartf1126682009-06-10 17:22:44 -04007163 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7164 * @phba: Pointer to HBA context object.
7165 *
7166 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7167 * the driver internal pending mailbox queue. It will then try to wait out the
7168 * possible outstanding mailbox command before return.
7169 *
7170 * Returns:
7171 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7172 * the outstanding mailbox command timed out.
7173 **/
7174static int
7175lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7176{
7177 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007178 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007179 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007180
7181 /* Mark the asynchronous mailbox command posting as blocked */
7182 spin_lock_irq(&phba->hbalock);
7183 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007184 /* Determine how long we might wait for the active mailbox
7185 * command to be gracefully completed by firmware.
7186 */
James Smarta183a152011-10-10 21:32:43 -04007187 if (phba->sli.mbox_active)
7188 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7189 phba->sli.mbox_active) *
7190 1000) + jiffies;
7191 spin_unlock_irq(&phba->hbalock);
7192
James Smarte8d3c3b2013-10-10 12:21:30 -04007193 /* Make sure the mailbox is really active */
7194 if (timeout)
7195 lpfc_sli4_process_missed_mbox_completions(phba);
7196
James Smartf1126682009-06-10 17:22:44 -04007197 /* Wait for the outstnading mailbox command to complete */
7198 while (phba->sli.mbox_active) {
7199 /* Check active mailbox complete status every 2ms */
7200 msleep(2);
7201 if (time_after(jiffies, timeout)) {
7202 /* Timeout, marked the outstanding cmd not complete */
7203 rc = 1;
7204 break;
7205 }
7206 }
7207
7208 /* Can not cleanly block async mailbox command, fails it */
7209 if (rc) {
7210 spin_lock_irq(&phba->hbalock);
7211 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7212 spin_unlock_irq(&phba->hbalock);
7213 }
7214 return rc;
7215}
7216
7217/**
7218 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7219 * @phba: Pointer to HBA context object.
7220 *
7221 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7222 * commands from the driver internal pending mailbox queue. It makes sure
7223 * that there is no outstanding mailbox command before resuming posting
7224 * asynchronous mailbox commands. If, for any reason, there is outstanding
7225 * mailbox command, it will try to wait it out before resuming asynchronous
7226 * mailbox command posting.
7227 **/
7228static void
7229lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7230{
7231 struct lpfc_sli *psli = &phba->sli;
7232
7233 spin_lock_irq(&phba->hbalock);
7234 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7235 /* Asynchronous mailbox posting is not blocked, do nothing */
7236 spin_unlock_irq(&phba->hbalock);
7237 return;
7238 }
7239
7240 /* Outstanding synchronous mailbox command is guaranteed to be done,
7241 * successful or timeout, after timing-out the outstanding mailbox
7242 * command shall always be removed, so just unblock posting async
7243 * mailbox command and resume
7244 */
7245 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7246 spin_unlock_irq(&phba->hbalock);
7247
7248 /* wake up worker thread to post asynchronlous mailbox command */
7249 lpfc_worker_wake_up(phba);
7250}
7251
7252/**
James Smart2d843ed2012-09-29 11:29:06 -04007253 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7254 * @phba: Pointer to HBA context object.
7255 * @mboxq: Pointer to mailbox object.
7256 *
7257 * The function waits for the bootstrap mailbox register ready bit from
7258 * port for twice the regular mailbox command timeout value.
7259 *
7260 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7261 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7262 **/
7263static int
7264lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7265{
7266 uint32_t db_ready;
7267 unsigned long timeout;
7268 struct lpfc_register bmbx_reg;
7269
7270 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7271 * 1000) + jiffies;
7272
7273 do {
7274 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7275 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7276 if (!db_ready)
7277 msleep(2);
7278
7279 if (time_after(jiffies, timeout))
7280 return MBXERR_ERROR;
7281 } while (!db_ready);
7282
7283 return 0;
7284}
7285
7286/**
James Smartda0436e2009-05-22 14:51:39 -04007287 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7288 * @phba: Pointer to HBA context object.
7289 * @mboxq: Pointer to mailbox object.
7290 *
7291 * The function posts a mailbox to the port. The mailbox is expected
7292 * to be comletely filled in and ready for the port to operate on it.
7293 * This routine executes a synchronous completion operation on the
7294 * mailbox by polling for its completion.
7295 *
7296 * The caller must not be holding any locks when calling this routine.
7297 *
7298 * Returns:
7299 * MBX_SUCCESS - mailbox posted successfully
7300 * Any of the MBX error values.
7301 **/
7302static int
7303lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7304{
7305 int rc = MBX_SUCCESS;
7306 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007307 uint32_t mcqe_status;
7308 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007309 struct lpfc_sli *psli = &phba->sli;
7310 struct lpfc_mqe *mb = &mboxq->u.mqe;
7311 struct lpfc_bmbx_create *mbox_rgn;
7312 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007313
7314 /*
7315 * Only one mailbox can be active to the bootstrap mailbox region
7316 * at a time and there is no queueing provided.
7317 */
7318 spin_lock_irqsave(&phba->hbalock, iflag);
7319 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7320 spin_unlock_irqrestore(&phba->hbalock, iflag);
7321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007322 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007323 "cannot issue Data: x%x x%x\n",
7324 mboxq->vport ? mboxq->vport->vpi : 0,
7325 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007326 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7327 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007328 psli->sli_flag, MBX_POLL);
7329 return MBXERR_ERROR;
7330 }
7331 /* The server grabs the token and owns it until release */
7332 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7333 phba->sli.mbox_active = mboxq;
7334 spin_unlock_irqrestore(&phba->hbalock, iflag);
7335
James Smart2d843ed2012-09-29 11:29:06 -04007336 /* wait for bootstrap mbox register for readyness */
7337 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7338 if (rc)
7339 goto exit;
7340
James Smartda0436e2009-05-22 14:51:39 -04007341 /*
7342 * Initialize the bootstrap memory region to avoid stale data areas
7343 * in the mailbox post. Then copy the caller's mailbox contents to
7344 * the bmbx mailbox region.
7345 */
7346 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7347 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7348 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7349 sizeof(struct lpfc_mqe));
7350
7351 /* Post the high mailbox dma address to the port and wait for ready. */
7352 dma_address = &phba->sli4_hba.bmbx.dma_address;
7353 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7354
James Smart2d843ed2012-09-29 11:29:06 -04007355 /* wait for bootstrap mbox register for hi-address write done */
7356 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7357 if (rc)
7358 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007359
7360 /* Post the low mailbox dma address to the port. */
7361 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007362
James Smart2d843ed2012-09-29 11:29:06 -04007363 /* wait for bootstrap mbox register for low address write done */
7364 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7365 if (rc)
7366 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007367
7368 /*
7369 * Read the CQ to ensure the mailbox has completed.
7370 * If so, update the mailbox status so that the upper layers
7371 * can complete the request normally.
7372 */
7373 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7374 sizeof(struct lpfc_mqe));
7375 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7376 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7377 sizeof(struct lpfc_mcqe));
7378 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007379 /*
7380 * When the CQE status indicates a failure and the mailbox status
7381 * indicates success then copy the CQE status into the mailbox status
7382 * (and prefix it with x4000).
7383 */
James Smartda0436e2009-05-22 14:51:39 -04007384 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007385 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7386 bf_set(lpfc_mqe_status, mb,
7387 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007388 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007389 } else
7390 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007391
7392 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007393 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007394 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7395 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007396 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7397 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7398 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007399 bf_get(lpfc_mqe_status, mb),
7400 mb->un.mb_words[0], mb->un.mb_words[1],
7401 mb->un.mb_words[2], mb->un.mb_words[3],
7402 mb->un.mb_words[4], mb->un.mb_words[5],
7403 mb->un.mb_words[6], mb->un.mb_words[7],
7404 mb->un.mb_words[8], mb->un.mb_words[9],
7405 mb->un.mb_words[10], mb->un.mb_words[11],
7406 mb->un.mb_words[12], mboxq->mcqe.word0,
7407 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7408 mboxq->mcqe.trailer);
7409exit:
7410 /* We are holding the token, no needed for lock when release */
7411 spin_lock_irqsave(&phba->hbalock, iflag);
7412 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7413 phba->sli.mbox_active = NULL;
7414 spin_unlock_irqrestore(&phba->hbalock, iflag);
7415 return rc;
7416}
7417
7418/**
7419 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7420 * @phba: Pointer to HBA context object.
7421 * @pmbox: Pointer to mailbox object.
7422 * @flag: Flag indicating how the mailbox need to be processed.
7423 *
7424 * This function is called by discovery code and HBA management code to submit
7425 * a mailbox command to firmware with SLI-4 interface spec.
7426 *
7427 * Return codes the caller owns the mailbox command after the return of the
7428 * function.
7429 **/
7430static int
7431lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7432 uint32_t flag)
7433{
7434 struct lpfc_sli *psli = &phba->sli;
7435 unsigned long iflags;
7436 int rc;
7437
James Smartb76f2dc2011-07-22 18:37:42 -04007438 /* dump from issue mailbox command if setup */
7439 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7440
James Smart8fa38512009-07-19 10:01:03 -04007441 rc = lpfc_mbox_dev_check(phba);
7442 if (unlikely(rc)) {
7443 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007444 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007445 "cannot issue Data: x%x x%x\n",
7446 mboxq->vport ? mboxq->vport->vpi : 0,
7447 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007448 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7449 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007450 psli->sli_flag, flag);
7451 goto out_not_finished;
7452 }
7453
James Smartda0436e2009-05-22 14:51:39 -04007454 /* Detect polling mode and jump to a handler */
7455 if (!phba->sli4_hba.intr_enable) {
7456 if (flag == MBX_POLL)
7457 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7458 else
7459 rc = -EIO;
7460 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007461 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007462 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007463 "(x%x/x%x) failure: "
7464 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7465 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007466 mboxq->vport ? mboxq->vport->vpi : 0,
7467 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007468 lpfc_sli_config_mbox_subsys_get(phba,
7469 mboxq),
7470 lpfc_sli_config_mbox_opcode_get(phba,
7471 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007472 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7473 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7474 bf_get(lpfc_mcqe_ext_status,
7475 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007476 psli->sli_flag, flag);
7477 return rc;
7478 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007479 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7480 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007481 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007482 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007483 mboxq->vport ? mboxq->vport->vpi : 0,
7484 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007485 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7486 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007487 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007488 /* Try to block the asynchronous mailbox posting */
7489 rc = lpfc_sli4_async_mbox_block(phba);
7490 if (!rc) {
7491 /* Successfully blocked, now issue sync mbox cmd */
7492 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7493 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007494 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007495 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007496 "(%d):2597 Sync Mailbox command "
7497 "x%x (x%x/x%x) failure: "
7498 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7499 "Data: x%x x%x\n,",
7500 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007501 mboxq->u.mb.mbxCommand,
7502 lpfc_sli_config_mbox_subsys_get(phba,
7503 mboxq),
7504 lpfc_sli_config_mbox_opcode_get(phba,
7505 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007506 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7507 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7508 bf_get(lpfc_mcqe_ext_status,
7509 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007510 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007511 /* Unblock the async mailbox posting afterward */
7512 lpfc_sli4_async_mbox_unblock(phba);
7513 }
7514 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007515 }
7516
7517 /* Now, interrupt mode asynchrous mailbox command */
7518 rc = lpfc_mbox_cmd_check(phba, mboxq);
7519 if (rc) {
7520 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007521 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007522 "cannot issue Data: x%x x%x\n",
7523 mboxq->vport ? mboxq->vport->vpi : 0,
7524 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007525 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7526 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007527 psli->sli_flag, flag);
7528 goto out_not_finished;
7529 }
James Smartda0436e2009-05-22 14:51:39 -04007530
7531 /* Put the mailbox command to the driver internal FIFO */
7532 psli->slistat.mbox_busy++;
7533 spin_lock_irqsave(&phba->hbalock, iflags);
7534 lpfc_mbox_put(phba, mboxq);
7535 spin_unlock_irqrestore(&phba->hbalock, iflags);
7536 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7537 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007538 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007539 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7540 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007541 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7542 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007543 phba->pport->port_state,
7544 psli->sli_flag, MBX_NOWAIT);
7545 /* Wake up worker thread to transport mailbox command from head */
7546 lpfc_worker_wake_up(phba);
7547
7548 return MBX_BUSY;
7549
7550out_not_finished:
7551 return MBX_NOT_FINISHED;
7552}
7553
7554/**
7555 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7556 * @phba: Pointer to HBA context object.
7557 *
7558 * This function is called by worker thread to send a mailbox command to
7559 * SLI4 HBA firmware.
7560 *
7561 **/
7562int
7563lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7564{
7565 struct lpfc_sli *psli = &phba->sli;
7566 LPFC_MBOXQ_t *mboxq;
7567 int rc = MBX_SUCCESS;
7568 unsigned long iflags;
7569 struct lpfc_mqe *mqe;
7570 uint32_t mbx_cmnd;
7571
7572 /* Check interrupt mode before post async mailbox command */
7573 if (unlikely(!phba->sli4_hba.intr_enable))
7574 return MBX_NOT_FINISHED;
7575
7576 /* Check for mailbox command service token */
7577 spin_lock_irqsave(&phba->hbalock, iflags);
7578 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7579 spin_unlock_irqrestore(&phba->hbalock, iflags);
7580 return MBX_NOT_FINISHED;
7581 }
7582 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7583 spin_unlock_irqrestore(&phba->hbalock, iflags);
7584 return MBX_NOT_FINISHED;
7585 }
7586 if (unlikely(phba->sli.mbox_active)) {
7587 spin_unlock_irqrestore(&phba->hbalock, iflags);
7588 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7589 "0384 There is pending active mailbox cmd\n");
7590 return MBX_NOT_FINISHED;
7591 }
7592 /* Take the mailbox command service token */
7593 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7594
7595 /* Get the next mailbox command from head of queue */
7596 mboxq = lpfc_mbox_get(phba);
7597
7598 /* If no more mailbox command waiting for post, we're done */
7599 if (!mboxq) {
7600 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7601 spin_unlock_irqrestore(&phba->hbalock, iflags);
7602 return MBX_SUCCESS;
7603 }
7604 phba->sli.mbox_active = mboxq;
7605 spin_unlock_irqrestore(&phba->hbalock, iflags);
7606
7607 /* Check device readiness for posting mailbox command */
7608 rc = lpfc_mbox_dev_check(phba);
7609 if (unlikely(rc))
7610 /* Driver clean routine will clean up pending mailbox */
7611 goto out_not_finished;
7612
7613 /* Prepare the mbox command to be posted */
7614 mqe = &mboxq->u.mqe;
7615 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7616
7617 /* Start timer for the mbox_tmo and log some mailbox post messages */
7618 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007619 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007620
7621 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007622 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007623 "x%x x%x\n",
7624 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007625 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7626 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007627 phba->pport->port_state, psli->sli_flag);
7628
7629 if (mbx_cmnd != MBX_HEARTBEAT) {
7630 if (mboxq->vport) {
7631 lpfc_debugfs_disc_trc(mboxq->vport,
7632 LPFC_DISC_TRC_MBOX_VPORT,
7633 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7634 mbx_cmnd, mqe->un.mb_words[0],
7635 mqe->un.mb_words[1]);
7636 } else {
7637 lpfc_debugfs_disc_trc(phba->pport,
7638 LPFC_DISC_TRC_MBOX,
7639 "MBOX Send: cmd:x%x mb:x%x x%x",
7640 mbx_cmnd, mqe->un.mb_words[0],
7641 mqe->un.mb_words[1]);
7642 }
7643 }
7644 psli->slistat.mbox_cmd++;
7645
7646 /* Post the mailbox command to the port */
7647 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7648 if (rc != MBX_SUCCESS) {
7649 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007650 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007651 "cannot issue Data: x%x x%x\n",
7652 mboxq->vport ? mboxq->vport->vpi : 0,
7653 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007654 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7655 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007656 psli->sli_flag, MBX_NOWAIT);
7657 goto out_not_finished;
7658 }
7659
7660 return rc;
7661
7662out_not_finished:
7663 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007664 if (phba->sli.mbox_active) {
7665 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7666 __lpfc_mbox_cmpl_put(phba, mboxq);
7667 /* Release the token */
7668 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7669 phba->sli.mbox_active = NULL;
7670 }
James Smartda0436e2009-05-22 14:51:39 -04007671 spin_unlock_irqrestore(&phba->hbalock, iflags);
7672
7673 return MBX_NOT_FINISHED;
7674}
7675
7676/**
7677 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7678 * @phba: Pointer to HBA context object.
7679 * @pmbox: Pointer to mailbox object.
7680 * @flag: Flag indicating how the mailbox need to be processed.
7681 *
7682 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7683 * the API jump table function pointer from the lpfc_hba struct.
7684 *
7685 * Return codes the caller owns the mailbox command after the return of the
7686 * function.
7687 **/
7688int
7689lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7690{
7691 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7692}
7693
7694/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007695 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007696 * @phba: The hba struct for which this call is being executed.
7697 * @dev_grp: The HBA PCI-Device group number.
7698 *
7699 * This routine sets up the mbox interface API function jump table in @phba
7700 * struct.
7701 * Returns: 0 - success, -ENODEV - failure.
7702 **/
7703int
7704lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7705{
7706
7707 switch (dev_grp) {
7708 case LPFC_PCI_DEV_LP:
7709 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7710 phba->lpfc_sli_handle_slow_ring_event =
7711 lpfc_sli_handle_slow_ring_event_s3;
7712 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7713 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7714 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7715 break;
7716 case LPFC_PCI_DEV_OC:
7717 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7718 phba->lpfc_sli_handle_slow_ring_event =
7719 lpfc_sli_handle_slow_ring_event_s4;
7720 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7721 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7722 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7723 break;
7724 default:
7725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7726 "1420 Invalid HBA PCI-device group: 0x%x\n",
7727 dev_grp);
7728 return -ENODEV;
7729 break;
7730 }
7731 return 0;
7732}
7733
7734/**
James Smart3621a712009-04-06 18:47:14 -04007735 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007736 * @phba: Pointer to HBA context object.
7737 * @pring: Pointer to driver SLI ring object.
7738 * @piocb: Pointer to address of newly added command iocb.
7739 *
7740 * This function is called with hbalock held to add a command
7741 * iocb to the txq when SLI layer cannot submit the command iocb
7742 * to the ring.
7743 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007744void
James Smart92d7f7b2007-06-17 19:56:38 -05007745__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007746 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007747{
7748 /* Insert the caller's iocb in the txq tail for later processing. */
7749 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007750}
7751
James Smarte59058c2008-08-24 21:49:00 -04007752/**
James Smart3621a712009-04-06 18:47:14 -04007753 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007754 * @phba: Pointer to HBA context object.
7755 * @pring: Pointer to driver SLI ring object.
7756 * @piocb: Pointer to address of newly added command iocb.
7757 *
7758 * This function is called with hbalock held before a new
7759 * iocb is submitted to the firmware. This function checks
7760 * txq to flush the iocbs in txq to Firmware before
7761 * submitting new iocbs to the Firmware.
7762 * If there are iocbs in the txq which need to be submitted
7763 * to firmware, lpfc_sli_next_iocb returns the first element
7764 * of the txq after dequeuing it from txq.
7765 * If there is no iocb in the txq then the function will return
7766 * *piocb and *piocb is set to NULL. Caller needs to check
7767 * *piocb to find if there are more commands in the txq.
7768 **/
dea31012005-04-17 16:05:31 -05007769static struct lpfc_iocbq *
7770lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007771 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007772{
7773 struct lpfc_iocbq * nextiocb;
7774
7775 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7776 if (!nextiocb) {
7777 nextiocb = *piocb;
7778 *piocb = NULL;
7779 }
7780
7781 return nextiocb;
7782}
7783
James Smarte59058c2008-08-24 21:49:00 -04007784/**
James Smart3772a992009-05-22 14:50:54 -04007785 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007786 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007787 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007788 * @piocb: Pointer to command iocb.
7789 * @flag: Flag indicating if this command can be put into txq.
7790 *
James Smart3772a992009-05-22 14:50:54 -04007791 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7792 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7793 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7794 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7795 * this function allows only iocbs for posting buffers. This function finds
7796 * next available slot in the command ring and posts the command to the
7797 * available slot and writes the port attention register to request HBA start
7798 * processing new iocb. If there is no slot available in the ring and
7799 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7800 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007801 *
James Smart3772a992009-05-22 14:50:54 -04007802 * This function is called with hbalock held. The function will return success
7803 * after it successfully submit the iocb to firmware or after adding to the
7804 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007805 **/
James Smart98c9ea52007-10-27 13:37:33 -04007806static int
James Smart3772a992009-05-22 14:50:54 -04007807__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007808 struct lpfc_iocbq *piocb, uint32_t flag)
7809{
7810 struct lpfc_iocbq *nextiocb;
7811 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007812 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007813
James Smart92d7f7b2007-06-17 19:56:38 -05007814 if (piocb->iocb_cmpl && (!piocb->vport) &&
7815 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7816 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7817 lpfc_printf_log(phba, KERN_ERR,
7818 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007819 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007820 piocb->iocb.ulpCommand);
7821 dump_stack();
7822 return IOCB_ERROR;
7823 }
7824
7825
Linas Vepstas8d63f372007-02-14 14:28:36 -06007826 /* If the PCI channel is in offline state, do not post iocbs. */
7827 if (unlikely(pci_channel_offline(phba->pcidev)))
7828 return IOCB_ERROR;
7829
James Smarta257bf92009-04-06 18:48:10 -04007830 /* If HBA has a deferred error attention, fail the iocb. */
7831 if (unlikely(phba->hba_flag & DEFER_ERATT))
7832 return IOCB_ERROR;
7833
dea31012005-04-17 16:05:31 -05007834 /*
7835 * We should never get an IOCB if we are in a < LINK_DOWN state
7836 */
James Smart2e0fef82007-06-17 19:56:36 -05007837 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007838 return IOCB_ERROR;
7839
7840 /*
7841 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007842 * outstanding event.
dea31012005-04-17 16:05:31 -05007843 */
James Smart0b727fe2007-10-27 13:37:25 -04007844 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007845 goto iocb_busy;
7846
James Smart2e0fef82007-06-17 19:56:36 -05007847 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007848 /*
James Smart2680eea2007-04-25 09:52:55 -04007849 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007850 * can be issued if the link is not up.
7851 */
7852 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007853 case CMD_GEN_REQUEST64_CR:
7854 case CMD_GEN_REQUEST64_CX:
7855 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7856 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007857 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007858 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7859 MENLO_TRANSPORT_TYPE))
7860
7861 goto iocb_busy;
7862 break;
dea31012005-04-17 16:05:31 -05007863 case CMD_QUE_RING_BUF_CN:
7864 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007865 /*
7866 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7867 * completion, iocb_cmpl MUST be 0.
7868 */
7869 if (piocb->iocb_cmpl)
7870 piocb->iocb_cmpl = NULL;
7871 /*FALLTHROUGH*/
7872 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007873 case CMD_CLOSE_XRI_CN:
7874 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007875 break;
7876 default:
7877 goto iocb_busy;
7878 }
7879
7880 /*
7881 * For FCP commands, we must be in a state where we can process link
7882 * attention events.
7883 */
7884 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007885 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007886 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007887 }
dea31012005-04-17 16:05:31 -05007888
dea31012005-04-17 16:05:31 -05007889 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7890 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7891 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7892
7893 if (iocb)
7894 lpfc_sli_update_ring(phba, pring);
7895 else
7896 lpfc_sli_update_full_ring(phba, pring);
7897
7898 if (!piocb)
7899 return IOCB_SUCCESS;
7900
7901 goto out_busy;
7902
7903 iocb_busy:
7904 pring->stats.iocb_cmd_delay++;
7905
7906 out_busy:
7907
7908 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007909 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007910 return IOCB_SUCCESS;
7911 }
7912
7913 return IOCB_BUSY;
7914}
7915
James Smart3772a992009-05-22 14:50:54 -04007916/**
James Smart4f774512009-05-22 14:52:35 -04007917 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7918 * @phba: Pointer to HBA context object.
7919 * @piocb: Pointer to command iocb.
7920 * @sglq: Pointer to the scatter gather queue object.
7921 *
7922 * This routine converts the bpl or bde that is in the IOCB
7923 * to a sgl list for the sli4 hardware. The physical address
7924 * of the bpl/bde is converted back to a virtual address.
7925 * If the IOCB contains a BPL then the list of BDE's is
7926 * converted to sli4_sge's. If the IOCB contains a single
7927 * BDE then it is converted to a single sli_sge.
7928 * The IOCB is still in cpu endianess so the contents of
7929 * the bpl can be used without byte swapping.
7930 *
7931 * Returns valid XRI = Success, NO_XRI = Failure.
7932**/
7933static uint16_t
7934lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7935 struct lpfc_sglq *sglq)
7936{
7937 uint16_t xritag = NO_XRI;
7938 struct ulp_bde64 *bpl = NULL;
7939 struct ulp_bde64 bde;
7940 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007941 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007942 IOCB_t *icmd;
7943 int numBdes = 0;
7944 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007945 uint32_t offset = 0; /* accumulated offset in the sg request list */
7946 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007947
7948 if (!piocbq || !sglq)
7949 return xritag;
7950
7951 sgl = (struct sli4_sge *)sglq->sgl;
7952 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007953 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7954 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007955 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7956 numBdes = icmd->un.genreq64.bdl.bdeSize /
7957 sizeof(struct ulp_bde64);
7958 /* The addrHigh and addrLow fields within the IOCB
7959 * have not been byteswapped yet so there is no
7960 * need to swap them back.
7961 */
James Smart1b511972011-12-13 13:23:09 -05007962 if (piocbq->context3)
7963 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7964 else
7965 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007966
James Smart1b511972011-12-13 13:23:09 -05007967 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007968 if (!bpl)
7969 return xritag;
7970
7971 for (i = 0; i < numBdes; i++) {
7972 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007973 sgl->addr_hi = bpl->addrHigh;
7974 sgl->addr_lo = bpl->addrLow;
7975
James Smart05580562011-05-24 11:40:48 -04007976 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007977 if ((i+1) == numBdes)
7978 bf_set(lpfc_sli4_sge_last, sgl, 1);
7979 else
7980 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007981 /* swap the size field back to the cpu so we
7982 * can assign it to the sgl.
7983 */
7984 bde.tus.w = le32_to_cpu(bpl->tus.w);
7985 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007986 /* The offsets in the sgl need to be accumulated
7987 * separately for the request and reply lists.
7988 * The request is always first, the reply follows.
7989 */
7990 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7991 /* add up the reply sg entries */
7992 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7993 inbound++;
7994 /* first inbound? reset the offset */
7995 if (inbound == 1)
7996 offset = 0;
7997 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007998 bf_set(lpfc_sli4_sge_type, sgl,
7999 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008000 offset += bde.tus.f.bdeSize;
8001 }
James Smart546fc852011-03-11 16:06:29 -05008002 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008003 bpl++;
8004 sgl++;
8005 }
8006 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8007 /* The addrHigh and addrLow fields of the BDE have not
8008 * been byteswapped yet so they need to be swapped
8009 * before putting them in the sgl.
8010 */
8011 sgl->addr_hi =
8012 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8013 sgl->addr_lo =
8014 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008015 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008016 bf_set(lpfc_sli4_sge_last, sgl, 1);
8017 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008018 sgl->sge_len =
8019 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008020 }
8021 return sglq->sli4_xritag;
8022}
8023
8024/**
8025 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
8026 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04008027 *
James Smarta93ff372010-10-22 11:06:08 -04008028 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04008029 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
8030 * held.
James Smart4f774512009-05-22 14:52:35 -04008031 *
8032 * Return: index into SLI4 fast-path FCP queue index.
8033 **/
James Smart2a76a282012-08-03 12:35:54 -04008034static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04008035lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04008036{
James Smart7bb03bb2013-04-17 20:19:16 -04008037 struct lpfc_vector_map_info *cpup;
8038 int chann, cpu;
James Smart4f774512009-05-22 14:52:35 -04008039
James Smart76f96b62013-12-17 20:29:01 -05008040 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
8041 && phba->cfg_fcp_io_channel > 1) {
James Smart7bb03bb2013-04-17 20:19:16 -04008042 cpu = smp_processor_id();
8043 if (cpu < phba->sli4_hba.num_present_cpu) {
8044 cpup = phba->sli4_hba.cpu_map;
8045 cpup += cpu;
8046 return cpup->channel_id;
8047 }
8048 chann = cpu;
8049 }
8050 chann = atomic_add_return(1, &phba->fcp_qidx);
8051 chann = (chann % phba->cfg_fcp_io_channel);
8052 return chann;
James Smart4f774512009-05-22 14:52:35 -04008053}
8054
8055/**
8056 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8057 * @phba: Pointer to HBA context object.
8058 * @piocb: Pointer to command iocb.
8059 * @wqe: Pointer to the work queue entry.
8060 *
8061 * This routine converts the iocb command to its Work Queue Entry
8062 * equivalent. The wqe pointer should not have any fields set when
8063 * this routine is called because it will memcpy over them.
8064 * This routine does not set the CQ_ID or the WQEC bits in the
8065 * wqe.
8066 *
8067 * Returns: 0 = Success, IOCB_ERROR = Failure.
8068 **/
8069static int
8070lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8071 union lpfc_wqe *wqe)
8072{
James Smart5ffc2662009-11-18 15:39:44 -05008073 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008074 uint8_t ct = 0;
8075 uint32_t fip;
8076 uint32_t abort_tag;
8077 uint8_t command_type = ELS_COMMAND_NON_FIP;
8078 uint8_t cmnd;
8079 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008080 uint16_t abrt_iotag;
8081 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008082 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008083 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008084 int numBdes, i;
8085 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008086 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008087 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008088 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008089
James Smart45ed1192009-10-02 15:17:02 -04008090 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008091 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008092 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008093 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008094 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008095 command_type = ELS_COMMAND_FIP;
8096 else
8097 command_type = ELS_COMMAND_NON_FIP;
8098
James Smart4f774512009-05-22 14:52:35 -04008099 /* Some of the fields are in the right position already */
8100 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8101 abort_tag = (uint32_t) iocbq->iotag;
8102 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04008103 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04008104 /* words0-2 bpl convert bde */
8105 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008106 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8107 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008108 bpl = (struct ulp_bde64 *)
8109 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8110 if (!bpl)
8111 return IOCB_ERROR;
8112
8113 /* Should already be byte swapped. */
8114 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8115 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8116 /* swap the size field back to the cpu so we
8117 * can assign it to the sgl.
8118 */
8119 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008120 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8121 total_len = 0;
8122 for (i = 0; i < numBdes; i++) {
8123 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8124 total_len += bde.tus.f.bdeSize;
8125 }
James Smart4f774512009-05-22 14:52:35 -04008126 } else
James Smart5ffc2662009-11-18 15:39:44 -05008127 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008128
8129 iocbq->iocb.ulpIoTag = iocbq->iotag;
8130 cmnd = iocbq->iocb.ulpCommand;
8131
8132 switch (iocbq->iocb.ulpCommand) {
8133 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008134 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8135 ndlp = iocbq->context_un.ndlp;
8136 else
8137 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008138 if (!iocbq->iocb.ulpLe) {
8139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8140 "2007 Only Limited Edition cmd Format"
8141 " supported 0x%x\n",
8142 iocbq->iocb.ulpCommand);
8143 return IOCB_ERROR;
8144 }
James Smartff78d8f2011-12-13 13:21:35 -05008145
James Smart5ffc2662009-11-18 15:39:44 -05008146 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008147 /* Els_reguest64 has a TMO */
8148 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8149 iocbq->iocb.ulpTimeout);
8150 /* Need a VF for word 4 set the vf bit*/
8151 bf_set(els_req64_vf, &wqe->els_req, 0);
8152 /* And a VFID for word 12 */
8153 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008154 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008155 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8156 iocbq->iocb.ulpContext);
8157 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8158 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008159 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008160 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008161 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8162 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008163 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8164 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008165 if_type = bf_get(lpfc_sli_intf_if_type,
8166 &phba->sli4_hba.sli_intf);
8167 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008168 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008169 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008170 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008171 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008172 *pcmd == ELS_CMD_PLOGI)) {
8173 bf_set(els_req64_sp, &wqe->els_req, 1);
8174 bf_set(els_req64_sid, &wqe->els_req,
8175 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008176 if ((*pcmd == ELS_CMD_FLOGI) &&
8177 !(phba->fc_topology ==
8178 LPFC_TOPOLOGY_LOOP))
8179 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008180 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8181 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008182 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008183 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008184 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8185 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8186 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8187 }
James Smartc8685952009-11-18 15:39:16 -05008188 }
James Smart6d368e52011-05-24 11:44:12 -04008189 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8190 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008191 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8192 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8193 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8194 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8195 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8196 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008197 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008198 break;
James Smart5ffc2662009-11-18 15:39:44 -05008199 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008200 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8201 iocbq->iocb.un.ulpWord[3]);
8202 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008203 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008204 /* The entire sequence is transmitted for this IOCB */
8205 xmit_len = total_len;
8206 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008207 if (phba->link_flag & LS_LOOPBACK_MODE)
8208 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008209 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008210 /* word3 iocb=io_tag32 wqe=reserved */
8211 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008212 /* word4 relative_offset memcpy */
8213 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008214 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8215 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8216 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8217 LPFC_WQE_IOD_WRITE);
8218 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8219 LPFC_WQE_LENLOC_WORD12);
8220 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008221 wqe->xmit_sequence.xmit_len = xmit_len;
8222 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008223 break;
James Smart4f774512009-05-22 14:52:35 -04008224 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008225 /* word3 iocb=iotag32 wqe=seq_payload_len */
8226 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008227 /* word4 iocb=rsvd wqe=rsvd */
8228 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8229 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008230 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008231 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008232 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8233 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8234 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8235 LPFC_WQE_LENLOC_WORD3);
8236 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008237 break;
James Smart4f774512009-05-22 14:52:35 -04008238 case CMD_FCP_IWRITE64_CR:
8239 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008240 /* word3 iocb=iotag wqe=payload_offset_len */
8241 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008242 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8243 xmit_len + sizeof(struct fcp_rsp));
8244 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8245 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008246 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8247 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8248 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8249 iocbq->iocb.ulpFCP2Rcvy);
8250 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8251 /* Always open the exchange */
8252 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008253 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8254 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8255 LPFC_WQE_LENLOC_WORD4);
8256 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8257 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008258 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008259 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008260 case CMD_FCP_IREAD64_CR:
8261 /* word3 iocb=iotag wqe=payload_offset_len */
8262 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008263 bf_set(payload_offset_len, &wqe->fcp_iread,
8264 xmit_len + sizeof(struct fcp_rsp));
8265 bf_set(cmd_buff_len, &wqe->fcp_iread,
8266 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008267 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8268 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8269 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8270 iocbq->iocb.ulpFCP2Rcvy);
8271 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008272 /* Always open the exchange */
8273 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008274 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8275 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8276 LPFC_WQE_LENLOC_WORD4);
8277 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8278 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008279 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008280 break;
James Smartf1126682009-06-10 17:22:44 -04008281 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008282 /* word3 iocb=iotag wqe=payload_offset_len */
8283 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8284 bf_set(payload_offset_len, &wqe->fcp_icmd,
8285 xmit_len + sizeof(struct fcp_rsp));
8286 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8287 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008288 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008289 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008290 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008291 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8292 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8293 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8294 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8295 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8296 LPFC_WQE_LENLOC_NONE);
8297 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart2a94aea2012-09-29 11:30:31 -04008298 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8299 iocbq->iocb.ulpFCP2Rcvy);
James Smart7851fe22011-07-22 18:36:52 -04008300 break;
James Smart4f774512009-05-22 14:52:35 -04008301 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008302 /* For this command calculate the xmit length of the
8303 * request bde.
8304 */
8305 xmit_len = 0;
8306 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8307 sizeof(struct ulp_bde64);
8308 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008309 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008310 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8311 break;
James Smart63e801c2010-11-20 23:14:19 -05008312 xmit_len += bde.tus.f.bdeSize;
8313 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008314 /* word3 iocb=IO_TAG wqe=request_payload_len */
8315 wqe->gen_req.request_payload_len = xmit_len;
8316 /* word4 iocb=parameter wqe=relative_offset memcpy */
8317 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008318 /* word6 context tag copied in memcpy */
8319 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8320 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8321 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8322 "2015 Invalid CT %x command 0x%x\n",
8323 ct, iocbq->iocb.ulpCommand);
8324 return IOCB_ERROR;
8325 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008326 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8327 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8328 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8329 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8330 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8331 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8332 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8333 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008334 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008335 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008336 break;
James Smart4f774512009-05-22 14:52:35 -04008337 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008338 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008339 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008340 /* word3 iocb=iotag32 wqe=response_payload_len */
8341 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008342 /* word4 */
8343 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008344 /* word5 iocb=rsvd wge=did */
8345 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008346 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8347
8348 if_type = bf_get(lpfc_sli_intf_if_type,
8349 &phba->sli4_hba.sli_intf);
8350 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8351 if (iocbq->vport->fc_flag & FC_PT2PT) {
8352 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8353 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8354 iocbq->vport->fc_myDID);
8355 if (iocbq->vport->fc_myDID == Fabric_DID) {
8356 bf_set(wqe_els_did,
8357 &wqe->xmit_els_rsp.wqe_dest, 0);
8358 }
8359 }
8360 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008361 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8362 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8363 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8364 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008365 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008366 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008367 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008368 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008369 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8370 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8371 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8372 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8373 LPFC_WQE_LENLOC_WORD3);
8374 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008375 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8376 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008377 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8378 iocbq->context2)->virt);
8379 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008380 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8381 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008382 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008383 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8384 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008385 phba->vpi_ids[phba->pport->vpi]);
8386 }
James Smart4f774512009-05-22 14:52:35 -04008387 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008388 break;
James Smart4f774512009-05-22 14:52:35 -04008389 case CMD_CLOSE_XRI_CN:
8390 case CMD_ABORT_XRI_CN:
8391 case CMD_ABORT_XRI_CX:
8392 /* words 0-2 memcpy should be 0 rserved */
8393 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008394 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8395 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8396 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8397 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8398 } else
8399 fip = 0;
8400
8401 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008402 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008403 * The link is down, or the command was ELS_FIP
8404 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008405 * on the wire.
8406 */
8407 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8408 else
8409 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8410 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008411 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8412 wqe->abort_cmd.rsrvd5 = 0;
8413 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008414 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8415 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008416 /*
8417 * The abort handler will send us CMD_ABORT_XRI_CN or
8418 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8419 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008420 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8421 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8422 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8423 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008424 cmnd = CMD_ABORT_XRI_CX;
8425 command_type = OTHER_COMMAND;
8426 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008427 break;
James Smart6669f9b2009-10-02 15:16:45 -04008428 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008429 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008430 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008431 * we re-construct this WQE here based on information in
8432 * iocbq from scratch.
8433 */
8434 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008435 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008436 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008437 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8438 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008439 LPFC_ABTS_UNSOL_INT) {
8440 /* ABTS sent by initiator to CT exchange, the
8441 * RX_ID field will be filled with the newly
8442 * allocated responder XRI.
8443 */
8444 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8445 iocbq->sli4_xritag);
8446 } else {
8447 /* ABTS sent by responder to CT exchange, the
8448 * RX_ID field will be filled with the responder
8449 * RX_ID from ABTS.
8450 */
8451 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008452 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008453 }
James Smart6669f9b2009-10-02 15:16:45 -04008454 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8455 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008456
8457 /* Use CT=VPI */
8458 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8459 ndlp->nlp_DID);
8460 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8461 iocbq->iocb.ulpContext);
8462 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008463 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008464 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008465 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8466 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8467 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008468 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8469 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008470 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8471 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8472 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8473 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8474 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8475 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8476 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8477 }
8478
James Smart7851fe22011-07-22 18:36:52 -04008479 break;
James Smart4f774512009-05-22 14:52:35 -04008480 case CMD_XRI_ABORTED_CX:
8481 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008482 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8483 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8484 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8485 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8486 default:
8487 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8488 "2014 Invalid command 0x%x\n",
8489 iocbq->iocb.ulpCommand);
8490 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008491 break;
James Smart4f774512009-05-22 14:52:35 -04008492 }
James Smart6d368e52011-05-24 11:44:12 -04008493
James Smart8012cc32012-10-31 14:44:49 -04008494 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8495 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8496 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8497 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8498 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8499 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8500 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8501 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008502 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8503 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8504 wqe->generic.wqe_com.abort_tag = abort_tag;
8505 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8506 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8507 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8508 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008509 return 0;
8510}
8511
8512/**
8513 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8514 * @phba: Pointer to HBA context object.
8515 * @ring_number: SLI ring number to issue iocb on.
8516 * @piocb: Pointer to command iocb.
8517 * @flag: Flag indicating if this command can be put into txq.
8518 *
8519 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8520 * an iocb command to an HBA with SLI-4 interface spec.
8521 *
8522 * This function is called with hbalock held. The function will return success
8523 * after it successfully submit the iocb to firmware or after adding to the
8524 * txq.
8525 **/
8526static int
8527__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8528 struct lpfc_iocbq *piocb, uint32_t flag)
8529{
8530 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008531 union lpfc_wqe wqe;
8532 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008533
8534 if (piocb->sli4_xritag == NO_XRI) {
8535 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008536 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008537 sglq = NULL;
8538 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008539 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008540 if (!(flag & SLI_IOCB_RET_IOCB)) {
8541 __lpfc_sli_ringtx_put(phba,
8542 pring, piocb);
8543 return IOCB_SUCCESS;
8544 } else {
8545 return IOCB_BUSY;
8546 }
8547 } else {
James Smart6d368e52011-05-24 11:44:12 -04008548 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008549 if (!sglq) {
8550 if (!(flag & SLI_IOCB_RET_IOCB)) {
8551 __lpfc_sli_ringtx_put(phba,
8552 pring,
8553 piocb);
8554 return IOCB_SUCCESS;
8555 } else
8556 return IOCB_BUSY;
8557 }
8558 }
James Smart4f774512009-05-22 14:52:35 -04008559 }
8560 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008561 /* These IO's already have an XRI and a mapped sgl. */
8562 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008563 } else {
James Smart6d368e52011-05-24 11:44:12 -04008564 /*
8565 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008566 * sglq is on the active list
8567 */
James Smartedccdc12013-01-03 15:43:45 -05008568 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008569 if (!sglq)
8570 return IOCB_ERROR;
8571 }
8572
8573 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008574 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008575 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008576 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008577 return IOCB_ERROR;
8578 }
8579
8580 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8581 return IOCB_ERROR;
8582
James Smart341af102010-01-26 23:07:37 -05008583 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8584 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartea714f32013-04-17 20:18:47 -04008585 if (unlikely(!phba->sli4_hba.fcp_wq))
8586 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008587 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8588 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008589 return IOCB_ERROR;
8590 } else {
James Smartea714f32013-04-17 20:18:47 -04008591 if (unlikely(!phba->sli4_hba.els_wq))
8592 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04008593 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8594 return IOCB_ERROR;
8595 }
8596 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8597
8598 return 0;
8599}
8600
8601/**
James Smart3772a992009-05-22 14:50:54 -04008602 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8603 *
8604 * This routine wraps the actual lockless version for issusing IOCB function
8605 * pointer from the lpfc_hba struct.
8606 *
8607 * Return codes:
8608 * IOCB_ERROR - Error
8609 * IOCB_SUCCESS - Success
8610 * IOCB_BUSY - Busy
8611 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008612int
James Smart3772a992009-05-22 14:50:54 -04008613__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8614 struct lpfc_iocbq *piocb, uint32_t flag)
8615{
8616 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8617}
8618
8619/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008620 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008621 * @phba: The hba struct for which this call is being executed.
8622 * @dev_grp: The HBA PCI-Device group number.
8623 *
8624 * This routine sets up the SLI interface API function jump table in @phba
8625 * struct.
8626 * Returns: 0 - success, -ENODEV - failure.
8627 **/
8628int
8629lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8630{
8631
8632 switch (dev_grp) {
8633 case LPFC_PCI_DEV_LP:
8634 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8635 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8636 break;
James Smart4f774512009-05-22 14:52:35 -04008637 case LPFC_PCI_DEV_OC:
8638 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8639 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8640 break;
James Smart3772a992009-05-22 14:50:54 -04008641 default:
8642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8643 "1419 Invalid HBA PCI-device group: 0x%x\n",
8644 dev_grp);
8645 return -ENODEV;
8646 break;
8647 }
8648 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8649 return 0;
8650}
James Smart92d7f7b2007-06-17 19:56:38 -05008651
James Smarte59058c2008-08-24 21:49:00 -04008652/**
James Smart3621a712009-04-06 18:47:14 -04008653 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008654 * @phba: Pointer to HBA context object.
8655 * @pring: Pointer to driver SLI ring object.
8656 * @piocb: Pointer to command iocb.
8657 * @flag: Flag indicating if this command can be put into txq.
8658 *
8659 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8660 * function. This function gets the hbalock and calls
8661 * __lpfc_sli_issue_iocb function and will return the error returned
8662 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8663 * functions which do not hold hbalock.
8664 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008665int
James Smart3772a992009-05-22 14:50:54 -04008666lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008667 struct lpfc_iocbq *piocb, uint32_t flag)
8668{
James Smartba20c852012-08-03 12:36:52 -04008669 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008670 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008671 struct lpfc_queue *fpeq;
8672 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008673 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008674 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008675
James Smart7e56aa22012-08-03 12:35:34 -04008676 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008677 if (piocb->iocb_flag & LPFC_IO_FCP) {
8678 if (unlikely(!phba->sli4_hba.fcp_wq))
8679 return IOCB_ERROR;
8680 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8681 piocb->fcp_wqidx = idx;
8682 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008683
James Smartba20c852012-08-03 12:36:52 -04008684 pring = &phba->sli.ring[ring_number];
8685 spin_lock_irqsave(&pring->ring_lock, iflags);
8686 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8687 flag);
8688 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8689
8690 if (lpfc_fcp_look_ahead) {
8691 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8692
8693 if (atomic_dec_and_test(&fcp_eq_hdl->
8694 fcp_eq_in_use)) {
8695
8696 /* Get associated EQ with this index */
8697 fpeq = phba->sli4_hba.hba_eq[idx];
8698
8699 /* Turn off interrupts from this EQ */
8700 lpfc_sli4_eq_clr_intr(fpeq);
8701
8702 /*
8703 * Process all the events on FCP EQ
8704 */
8705 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8706 lpfc_sli4_hba_handle_eqe(phba,
8707 eqe, idx);
8708 fpeq->EQ_processed++;
8709 }
8710
8711 /* Always clear and re-arm the EQ */
8712 lpfc_sli4_eq_release(fpeq,
8713 LPFC_QUEUE_REARM);
8714 }
8715 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8716 }
8717 } else {
8718 pring = &phba->sli.ring[ring_number];
8719 spin_lock_irqsave(&pring->ring_lock, iflags);
8720 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8721 flag);
8722 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8723
James Smart2a76a282012-08-03 12:35:54 -04008724 }
James Smart7e56aa22012-08-03 12:35:34 -04008725 } else {
8726 /* For now, SLI2/3 will still use hbalock */
8727 spin_lock_irqsave(&phba->hbalock, iflags);
8728 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8729 spin_unlock_irqrestore(&phba->hbalock, iflags);
8730 }
James Smart92d7f7b2007-06-17 19:56:38 -05008731 return rc;
8732}
8733
James Smarte59058c2008-08-24 21:49:00 -04008734/**
James Smart3621a712009-04-06 18:47:14 -04008735 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008736 * @phba: Pointer to HBA context object.
8737 *
8738 * This function is called while driver attaches with the
8739 * HBA to setup the extra ring. The extra ring is used
8740 * only when driver needs to support target mode functionality
8741 * or IP over FC functionalities.
8742 *
8743 * This function is called with no lock held.
8744 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008745static int
8746lpfc_extra_ring_setup( struct lpfc_hba *phba)
8747{
8748 struct lpfc_sli *psli;
8749 struct lpfc_sli_ring *pring;
8750
8751 psli = &phba->sli;
8752
8753 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008754
8755 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008756 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008757 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8758 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8759 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8760 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008761
James Smarta4bc3372006-12-02 13:34:16 -05008762 /* and give them to the extra ring */
8763 pring = &psli->ring[psli->extra_ring];
8764
James Smart7e56aa22012-08-03 12:35:34 -04008765 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8766 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8767 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8768 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008769
8770 /* Setup default profile for this ring */
8771 pring->iotag_max = 4096;
8772 pring->num_mask = 1;
8773 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008774 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8775 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008776 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8777 return 0;
8778}
8779
James Smartcb69f7d2011-12-13 13:21:57 -05008780/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8781 * @phba: Pointer to HBA context object.
8782 * @iocbq: Pointer to iocb object.
8783 *
8784 * The async_event handler calls this routine when it receives
8785 * an ASYNC_STATUS_CN event from the port. The port generates
8786 * this event when an Abort Sequence request to an rport fails
8787 * twice in succession. The abort could be originated by the
8788 * driver or by the port. The ABTS could have been for an ELS
8789 * or FCP IO. The port only generates this event when an ABTS
8790 * fails to complete after one retry.
8791 */
8792static void
8793lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8794 struct lpfc_iocbq *iocbq)
8795{
8796 struct lpfc_nodelist *ndlp = NULL;
8797 uint16_t rpi = 0, vpi = 0;
8798 struct lpfc_vport *vport = NULL;
8799
8800 /* The rpi in the ulpContext is vport-sensitive. */
8801 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8802 rpi = iocbq->iocb.ulpContext;
8803
8804 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8805 "3092 Port generated ABTS async event "
8806 "on vpi %d rpi %d status 0x%x\n",
8807 vpi, rpi, iocbq->iocb.ulpStatus);
8808
8809 vport = lpfc_find_vport_by_vpid(phba, vpi);
8810 if (!vport)
8811 goto err_exit;
8812 ndlp = lpfc_findnode_rpi(vport, rpi);
8813 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8814 goto err_exit;
8815
8816 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8817 lpfc_sli_abts_recover_port(vport, ndlp);
8818 return;
8819
8820 err_exit:
8821 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8822 "3095 Event Context not found, no "
8823 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8824 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8825 vpi, rpi);
8826}
8827
8828/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8829 * @phba: pointer to HBA context object.
8830 * @ndlp: nodelist pointer for the impacted rport.
8831 * @axri: pointer to the wcqe containing the failed exchange.
8832 *
8833 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8834 * port. The port generates this event when an abort exchange request to an
8835 * rport fails twice in succession with no reply. The abort could be originated
8836 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8837 */
8838void
8839lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8840 struct lpfc_nodelist *ndlp,
8841 struct sli4_wcqe_xri_aborted *axri)
8842{
8843 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008844 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008845
James Smart6b5151f2012-01-18 16:24:06 -05008846 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008847 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8848 "3115 Node Context not found, driver "
8849 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008850 return;
8851 }
8852
James Smartcb69f7d2011-12-13 13:21:57 -05008853 vport = ndlp->vport;
8854 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8855 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008856 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04008857 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05008858 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008859 bf_get(lpfc_wcqe_xa_status, axri),
8860 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008861
James Smart5c1db2a2012-03-01 22:34:36 -05008862 /*
8863 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8864 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8865 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8866 */
James Smarte3d2b802012-08-14 14:25:43 -04008867 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05008868 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8869 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008870 lpfc_sli_abts_recover_port(vport, ndlp);
8871}
8872
James Smarte59058c2008-08-24 21:49:00 -04008873/**
James Smart3621a712009-04-06 18:47:14 -04008874 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008875 * @phba: Pointer to HBA context object.
8876 * @pring: Pointer to driver SLI ring object.
8877 * @iocbq: Pointer to iocb object.
8878 *
8879 * This function is called by the slow ring event handler
8880 * function when there is an ASYNC event iocb in the ring.
8881 * This function is called with no lock held.
8882 * Currently this function handles only temperature related
8883 * ASYNC events. The function decodes the temperature sensor
8884 * event message and posts events for the management applications.
8885 **/
James Smart98c9ea52007-10-27 13:37:33 -04008886static void
James Smart57127f12007-10-27 13:37:05 -04008887lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8888 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8889{
8890 IOCB_t *icmd;
8891 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008892 struct temp_event temp_event_data;
8893 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008894 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008895
8896 icmd = &iocbq->iocb;
8897 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008898
James Smartcb69f7d2011-12-13 13:21:57 -05008899 switch (evt_code) {
8900 case ASYNC_TEMP_WARN:
8901 case ASYNC_TEMP_SAFE:
8902 temp_event_data.data = (uint32_t) icmd->ulpContext;
8903 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8904 if (evt_code == ASYNC_TEMP_WARN) {
8905 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8906 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8907 "0347 Adapter is very hot, please take "
8908 "corrective action. temperature : %d Celsius\n",
8909 (uint32_t) icmd->ulpContext);
8910 } else {
8911 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8912 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8913 "0340 Adapter temperature is OK now. "
8914 "temperature : %d Celsius\n",
8915 (uint32_t) icmd->ulpContext);
8916 }
8917
8918 /* Send temperature change event to applications */
8919 shost = lpfc_shost_from_vport(phba->pport);
8920 fc_host_post_vendor_event(shost, fc_get_event_number(),
8921 sizeof(temp_event_data), (char *) &temp_event_data,
8922 LPFC_NL_VENDOR_ID);
8923 break;
8924 case ASYNC_STATUS_CN:
8925 lpfc_sli_abts_err_handler(phba, iocbq);
8926 break;
8927 default:
James Smarta257bf92009-04-06 18:48:10 -04008928 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008929 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008930 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008931 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008932 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8933 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8934 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8935 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008936 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008937 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8938 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8939 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8940 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8941
James Smartcb69f7d2011-12-13 13:21:57 -05008942 break;
James Smart57127f12007-10-27 13:37:05 -04008943 }
James Smart57127f12007-10-27 13:37:05 -04008944}
8945
8946
James Smarte59058c2008-08-24 21:49:00 -04008947/**
James Smart3621a712009-04-06 18:47:14 -04008948 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008949 * @phba: Pointer to HBA context object.
8950 *
8951 * lpfc_sli_setup sets up rings of the SLI interface with
8952 * number of iocbs per ring and iotags. This function is
8953 * called while driver attach to the HBA and before the
8954 * interrupts are enabled. So there is no need for locking.
8955 *
8956 * This function always returns 0.
8957 **/
dea31012005-04-17 16:05:31 -05008958int
8959lpfc_sli_setup(struct lpfc_hba *phba)
8960{
James Smarted957682007-06-17 19:56:37 -05008961 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008962 struct lpfc_sli *psli = &phba->sli;
8963 struct lpfc_sli_ring *pring;
8964
James Smart2a76a282012-08-03 12:35:54 -04008965 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8966 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04008967 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05008968 psli->sli_flag = 0;
8969 psli->fcp_ring = LPFC_FCP_RING;
8970 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008971 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008972
James Bottomley604a3e32005-10-29 10:28:33 -05008973 psli->iocbq_lookup = NULL;
8974 psli->iocbq_lookup_len = 0;
8975 psli->last_iotag = 0;
8976
dea31012005-04-17 16:05:31 -05008977 for (i = 0; i < psli->num_rings; i++) {
8978 pring = &psli->ring[i];
8979 switch (i) {
8980 case LPFC_FCP_RING: /* ring 0 - FCP */
8981 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008982 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8983 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8984 pring->sli.sli3.numCiocb +=
8985 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8986 pring->sli.sli3.numRiocb +=
8987 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8988 pring->sli.sli3.numCiocb +=
8989 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8990 pring->sli.sli3.numRiocb +=
8991 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8992 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008993 SLI3_IOCB_CMD_SIZE :
8994 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008995 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008996 SLI3_IOCB_RSP_SIZE :
8997 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008998 pring->iotag_ctr = 0;
8999 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009000 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009001 pring->fast_iotag = pring->iotag_max;
9002 pring->num_mask = 0;
9003 break;
James Smarta4bc3372006-12-02 13:34:16 -05009004 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009005 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009006 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9007 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9008 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009009 SLI3_IOCB_CMD_SIZE :
9010 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009011 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009012 SLI3_IOCB_RSP_SIZE :
9013 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009014 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009015 pring->num_mask = 0;
9016 break;
9017 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9018 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009019 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9020 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9021 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009022 SLI3_IOCB_CMD_SIZE :
9023 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009024 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009025 SLI3_IOCB_RSP_SIZE :
9026 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009027 pring->fast_iotag = 0;
9028 pring->iotag_ctr = 0;
9029 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009030 pring->lpfc_sli_rcv_async_status =
9031 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009032 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009033 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009034 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9035 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009036 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009037 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009038 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009039 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9040 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009041 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009042 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009043 pring->prt[2].profile = 0; /* Mask 2 */
9044 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009045 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009046 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009047 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009048 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009049 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009050 pring->prt[3].profile = 0; /* Mask 3 */
9051 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009052 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009053 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009054 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009055 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009056 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009057 break;
9058 }
James Smart7e56aa22012-08-03 12:35:34 -04009059 totiocbsize += (pring->sli.sli3.numCiocb *
9060 pring->sli.sli3.sizeCiocb) +
9061 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009062 }
James Smarted957682007-06-17 19:56:37 -05009063 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009064 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009065 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9066 "SLI2 SLIM Data: x%x x%lx\n",
9067 phba->brd_no, totiocbsize,
9068 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009069 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009070 if (phba->cfg_multi_ring_support == 2)
9071 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009072
9073 return 0;
9074}
9075
James Smarte59058c2008-08-24 21:49:00 -04009076/**
James Smart3621a712009-04-06 18:47:14 -04009077 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009078 * @phba: Pointer to HBA context object.
9079 *
9080 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9081 * ring. This function also initializes ring indices of each ring.
9082 * This function is called during the initialization of the SLI
9083 * interface of an HBA.
9084 * This function is called with no lock held and always returns
9085 * 1.
9086 **/
dea31012005-04-17 16:05:31 -05009087int
James Smart2e0fef82007-06-17 19:56:36 -05009088lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009089{
9090 struct lpfc_sli *psli;
9091 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009092 int i;
dea31012005-04-17 16:05:31 -05009093
9094 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009095 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009096 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009097 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009098 /* Initialize list headers for txq and txcmplq as double linked lists */
9099 for (i = 0; i < psli->num_rings; i++) {
9100 pring = &psli->ring[i];
9101 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009102 pring->sli.sli3.next_cmdidx = 0;
9103 pring->sli.sli3.local_getidx = 0;
9104 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009105 INIT_LIST_HEAD(&pring->txq);
9106 INIT_LIST_HEAD(&pring->txcmplq);
9107 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009108 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009109 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009110 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009111 }
James Smart2e0fef82007-06-17 19:56:36 -05009112 spin_unlock_irq(&phba->hbalock);
9113 return 1;
dea31012005-04-17 16:05:31 -05009114}
9115
James Smarte59058c2008-08-24 21:49:00 -04009116/**
James Smart04c68492009-05-22 14:52:52 -04009117 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9118 * @phba: Pointer to HBA context object.
9119 *
9120 * This routine flushes the mailbox command subsystem. It will unconditionally
9121 * flush all the mailbox commands in the three possible stages in the mailbox
9122 * command sub-system: pending mailbox command queue; the outstanding mailbox
9123 * command; and completed mailbox command queue. It is caller's responsibility
9124 * to make sure that the driver is in the proper state to flush the mailbox
9125 * command sub-system. Namely, the posting of mailbox commands into the
9126 * pending mailbox command queue from the various clients must be stopped;
9127 * either the HBA is in a state that it will never works on the outstanding
9128 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9129 * mailbox command has been completed.
9130 **/
9131static void
9132lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9133{
9134 LIST_HEAD(completions);
9135 struct lpfc_sli *psli = &phba->sli;
9136 LPFC_MBOXQ_t *pmb;
9137 unsigned long iflag;
9138
9139 /* Flush all the mailbox commands in the mbox system */
9140 spin_lock_irqsave(&phba->hbalock, iflag);
9141 /* The pending mailbox command queue */
9142 list_splice_init(&phba->sli.mboxq, &completions);
9143 /* The outstanding active mailbox command */
9144 if (psli->mbox_active) {
9145 list_add_tail(&psli->mbox_active->list, &completions);
9146 psli->mbox_active = NULL;
9147 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9148 }
9149 /* The completed mailbox command queue */
9150 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9151 spin_unlock_irqrestore(&phba->hbalock, iflag);
9152
9153 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9154 while (!list_empty(&completions)) {
9155 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9156 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9157 if (pmb->mbox_cmpl)
9158 pmb->mbox_cmpl(phba, pmb);
9159 }
9160}
9161
9162/**
James Smart3621a712009-04-06 18:47:14 -04009163 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009164 * @vport: Pointer to virtual port object.
9165 *
9166 * lpfc_sli_host_down is called to clean up the resources
9167 * associated with a vport before destroying virtual
9168 * port data structures.
9169 * This function does following operations:
9170 * - Free discovery resources associated with this virtual
9171 * port.
9172 * - Free iocbs associated with this virtual port in
9173 * the txq.
9174 * - Send abort for all iocb commands associated with this
9175 * vport in txcmplq.
9176 *
9177 * This function is called with no lock held and always returns 1.
9178 **/
dea31012005-04-17 16:05:31 -05009179int
James Smart92d7f7b2007-06-17 19:56:38 -05009180lpfc_sli_host_down(struct lpfc_vport *vport)
9181{
James Smart858c9f62007-06-17 19:56:39 -05009182 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009183 struct lpfc_hba *phba = vport->phba;
9184 struct lpfc_sli *psli = &phba->sli;
9185 struct lpfc_sli_ring *pring;
9186 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009187 int i;
9188 unsigned long flags = 0;
9189 uint16_t prev_pring_flag;
9190
9191 lpfc_cleanup_discovery_resources(vport);
9192
9193 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009194 for (i = 0; i < psli->num_rings; i++) {
9195 pring = &psli->ring[i];
9196 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009197 /* Only slow rings */
9198 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009199 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009200 /* Set the lpfc data pending flag */
9201 set_bit(LPFC_DATA_READY, &phba->data_flags);
9202 }
James Smart92d7f7b2007-06-17 19:56:38 -05009203 /*
9204 * Error everything on the txq since these iocbs have not been
9205 * given to the FW yet.
9206 */
James Smart92d7f7b2007-06-17 19:56:38 -05009207 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9208 if (iocb->vport != vport)
9209 continue;
James Smart858c9f62007-06-17 19:56:39 -05009210 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009211 }
9212
9213 /* Next issue ABTS for everything on the txcmplq */
9214 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9215 list) {
9216 if (iocb->vport != vport)
9217 continue;
9218 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9219 }
9220
9221 pring->flag = prev_pring_flag;
9222 }
9223
9224 spin_unlock_irqrestore(&phba->hbalock, flags);
9225
James Smarta257bf92009-04-06 18:48:10 -04009226 /* Cancel all the IOCBs from the completions list */
9227 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9228 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009229 return 1;
9230}
9231
James Smarte59058c2008-08-24 21:49:00 -04009232/**
James Smart3621a712009-04-06 18:47:14 -04009233 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009234 * @phba: Pointer to HBA context object.
9235 *
9236 * This function cleans up all iocb, buffers, mailbox commands
9237 * while shutting down the HBA. This function is called with no
9238 * lock held and always returns 1.
9239 * This function does the following to cleanup driver resources:
9240 * - Free discovery resources for each virtual port
9241 * - Cleanup any pending fabric iocbs
9242 * - Iterate through the iocb txq and free each entry
9243 * in the list.
9244 * - Free up any buffer posted to the HBA
9245 * - Free mailbox commands in the mailbox queue.
9246 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009247int
James Smart2e0fef82007-06-17 19:56:36 -05009248lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009249{
James Smart2534ba72007-04-25 09:52:20 -04009250 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009251 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009252 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009253 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009254 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009255 int i;
9256
9257 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009258 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009259
dea31012005-04-17 16:05:31 -05009260 lpfc_hba_down_prep(phba);
9261
James Smart92d7f7b2007-06-17 19:56:38 -05009262 lpfc_fabric_abort_hba(phba);
9263
James Smart2e0fef82007-06-17 19:56:36 -05009264 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009265 for (i = 0; i < psli->num_rings; i++) {
9266 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009267 /* Only slow rings */
9268 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009269 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009270 /* Set the lpfc data pending flag */
9271 set_bit(LPFC_DATA_READY, &phba->data_flags);
9272 }
dea31012005-04-17 16:05:31 -05009273
9274 /*
9275 * Error everything on the txq since these iocbs have not been
9276 * given to the FW yet.
9277 */
James Smart2534ba72007-04-25 09:52:20 -04009278 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009279 }
James Smart2e0fef82007-06-17 19:56:36 -05009280 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009281
James Smarta257bf92009-04-06 18:48:10 -04009282 /* Cancel all the IOCBs from the completions list */
9283 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9284 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009285
James Smart0ff10d42008-01-11 01:52:36 -05009286 spin_lock_irqsave(&phba->hbalock, flags);
9287 list_splice_init(&phba->elsbuf, &completions);
9288 phba->elsbuf_cnt = 0;
9289 phba->elsbuf_prev_cnt = 0;
9290 spin_unlock_irqrestore(&phba->hbalock, flags);
9291
9292 while (!list_empty(&completions)) {
9293 list_remove_head(&completions, buf_ptr,
9294 struct lpfc_dmabuf, list);
9295 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9296 kfree(buf_ptr);
9297 }
9298
dea31012005-04-17 16:05:31 -05009299 /* Return any active mbox cmds */
9300 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009301
James Smartda0436e2009-05-22 14:51:39 -04009302 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009303 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009304 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009305
James Smartda0436e2009-05-22 14:51:39 -04009306 return 1;
9307}
James Smart92d7f7b2007-06-17 19:56:38 -05009308
James Smartda0436e2009-05-22 14:51:39 -04009309/**
James Smart3621a712009-04-06 18:47:14 -04009310 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009311 * @srcp: Source memory pointer.
9312 * @destp: Destination memory pointer.
9313 * @cnt: Number of words required to be copied.
9314 *
9315 * This function is used for copying data between driver memory
9316 * and the SLI memory. This function also changes the endianness
9317 * of each word if native endianness is different from SLI
9318 * endianness. This function can be called with or without
9319 * lock.
9320 **/
dea31012005-04-17 16:05:31 -05009321void
9322lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9323{
9324 uint32_t *src = srcp;
9325 uint32_t *dest = destp;
9326 uint32_t ldata;
9327 int i;
9328
9329 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9330 ldata = *src;
9331 ldata = le32_to_cpu(ldata);
9332 *dest = ldata;
9333 src++;
9334 dest++;
9335 }
9336}
9337
James Smarte59058c2008-08-24 21:49:00 -04009338
9339/**
James Smarta0c87cb2009-07-19 10:01:10 -04009340 * lpfc_sli_bemem_bcopy - SLI memory copy function
9341 * @srcp: Source memory pointer.
9342 * @destp: Destination memory pointer.
9343 * @cnt: Number of words required to be copied.
9344 *
9345 * This function is used for copying data between a data structure
9346 * with big endian representation to local endianness.
9347 * This function can be called with or without lock.
9348 **/
9349void
9350lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9351{
9352 uint32_t *src = srcp;
9353 uint32_t *dest = destp;
9354 uint32_t ldata;
9355 int i;
9356
9357 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9358 ldata = *src;
9359 ldata = be32_to_cpu(ldata);
9360 *dest = ldata;
9361 src++;
9362 dest++;
9363 }
9364}
9365
9366/**
James Smart3621a712009-04-06 18:47:14 -04009367 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009368 * @phba: Pointer to HBA context object.
9369 * @pring: Pointer to driver SLI ring object.
9370 * @mp: Pointer to driver buffer object.
9371 *
9372 * This function is called with no lock held.
9373 * It always return zero after adding the buffer to the postbufq
9374 * buffer list.
9375 **/
dea31012005-04-17 16:05:31 -05009376int
James Smart2e0fef82007-06-17 19:56:36 -05009377lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9378 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009379{
9380 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9381 later */
James Smart2e0fef82007-06-17 19:56:36 -05009382 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009383 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009384 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009385 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009386 return 0;
9387}
9388
James Smarte59058c2008-08-24 21:49:00 -04009389/**
James Smart3621a712009-04-06 18:47:14 -04009390 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009391 * @phba: Pointer to HBA context object.
9392 *
9393 * When HBQ is enabled, buffers are searched based on tags. This function
9394 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9395 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9396 * does not conflict with tags of buffer posted for unsolicited events.
9397 * The function returns the allocated tag. The function is called with
9398 * no locks held.
9399 **/
James Smart76bb24e2007-10-27 13:38:00 -04009400uint32_t
9401lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9402{
9403 spin_lock_irq(&phba->hbalock);
9404 phba->buffer_tag_count++;
9405 /*
9406 * Always set the QUE_BUFTAG_BIT to distiguish between
9407 * a tag assigned by HBQ.
9408 */
9409 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9410 spin_unlock_irq(&phba->hbalock);
9411 return phba->buffer_tag_count;
9412}
9413
James Smarte59058c2008-08-24 21:49:00 -04009414/**
James Smart3621a712009-04-06 18:47:14 -04009415 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009416 * @phba: Pointer to HBA context object.
9417 * @pring: Pointer to driver SLI ring object.
9418 * @tag: Buffer tag.
9419 *
9420 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9421 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9422 * iocb is posted to the response ring with the tag of the buffer.
9423 * This function searches the pring->postbufq list using the tag
9424 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9425 * iocb. If the buffer is found then lpfc_dmabuf object of the
9426 * buffer is returned to the caller else NULL is returned.
9427 * This function is called with no lock held.
9428 **/
James Smart76bb24e2007-10-27 13:38:00 -04009429struct lpfc_dmabuf *
9430lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9431 uint32_t tag)
9432{
9433 struct lpfc_dmabuf *mp, *next_mp;
9434 struct list_head *slp = &pring->postbufq;
9435
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009436 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009437 spin_lock_irq(&phba->hbalock);
9438 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9439 if (mp->buffer_tag == tag) {
9440 list_del_init(&mp->list);
9441 pring->postbufq_cnt--;
9442 spin_unlock_irq(&phba->hbalock);
9443 return mp;
9444 }
9445 }
9446
9447 spin_unlock_irq(&phba->hbalock);
9448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009449 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009450 "ring %d Data x%lx x%p x%p x%x\n",
9451 pring->ringno, (unsigned long) tag,
9452 slp->next, slp->prev, pring->postbufq_cnt);
9453
9454 return NULL;
9455}
dea31012005-04-17 16:05:31 -05009456
James Smarte59058c2008-08-24 21:49:00 -04009457/**
James Smart3621a712009-04-06 18:47:14 -04009458 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009459 * @phba: Pointer to HBA context object.
9460 * @pring: Pointer to driver SLI ring object.
9461 * @phys: DMA address of the buffer.
9462 *
9463 * This function searches the buffer list using the dma_address
9464 * of unsolicited event to find the driver's lpfc_dmabuf object
9465 * corresponding to the dma_address. The function returns the
9466 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9467 * This function is called by the ct and els unsolicited event
9468 * handlers to get the buffer associated with the unsolicited
9469 * event.
9470 *
9471 * This function is called with no lock held.
9472 **/
dea31012005-04-17 16:05:31 -05009473struct lpfc_dmabuf *
9474lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9475 dma_addr_t phys)
9476{
9477 struct lpfc_dmabuf *mp, *next_mp;
9478 struct list_head *slp = &pring->postbufq;
9479
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009480 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009481 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009482 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9483 if (mp->phys == phys) {
9484 list_del_init(&mp->list);
9485 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009486 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009487 return mp;
9488 }
9489 }
9490
James Smart2e0fef82007-06-17 19:56:36 -05009491 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009493 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009494 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009495 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009496 slp->next, slp->prev, pring->postbufq_cnt);
9497 return NULL;
9498}
9499
James Smarte59058c2008-08-24 21:49:00 -04009500/**
James Smart3621a712009-04-06 18:47:14 -04009501 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009502 * @phba: Pointer to HBA context object.
9503 * @cmdiocb: Pointer to driver command iocb object.
9504 * @rspiocb: Pointer to driver response iocb object.
9505 *
9506 * This function is the completion handler for the abort iocbs for
9507 * ELS commands. This function is called from the ELS ring event
9508 * handler with no lock held. This function frees memory resources
9509 * associated with the abort iocb.
9510 **/
dea31012005-04-17 16:05:31 -05009511static void
James Smart2e0fef82007-06-17 19:56:36 -05009512lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9513 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009514{
James Smart2e0fef82007-06-17 19:56:36 -05009515 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009516 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009517 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009518
9519 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009520
9521 /*
9522 * Assume that the port already completed and returned, or
9523 * will return the iocb. Just Log the message.
9524 */
James Smart2680eea2007-04-25 09:52:55 -04009525 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9526 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9527
James Smart2e0fef82007-06-17 19:56:36 -05009528 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009529 if (phba->sli_rev < LPFC_SLI_REV4) {
9530 if (abort_iotag != 0 &&
9531 abort_iotag <= phba->sli.last_iotag)
9532 abort_iocb =
9533 phba->sli.iocbq_lookup[abort_iotag];
9534 } else
9535 /* For sli4 the abort_tag is the XRI,
9536 * so the abort routine puts the iotag of the iocb
9537 * being aborted in the context field of the abort
9538 * IOCB.
9539 */
9540 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009541
James Smart2a9bf3d2010-06-07 15:24:45 -04009542 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9543 "0327 Cannot abort els iocb %p "
9544 "with tag %x context %x, abort status %x, "
9545 "abort code %x\n",
9546 abort_iocb, abort_iotag, abort_context,
9547 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009548
James Smartff78d8f2011-12-13 13:21:35 -05009549 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009550 }
James Bottomley604a3e32005-10-29 10:28:33 -05009551 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009552 return;
9553}
9554
James Smarte59058c2008-08-24 21:49:00 -04009555/**
James Smart3621a712009-04-06 18:47:14 -04009556 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009557 * @phba: Pointer to HBA context object.
9558 * @cmdiocb: Pointer to driver command iocb object.
9559 * @rspiocb: Pointer to driver response iocb object.
9560 *
9561 * The function is called from SLI ring event handler with no
9562 * lock held. This function is the completion handler for ELS commands
9563 * which are aborted. The function frees memory resources used for
9564 * the aborted ELS commands.
9565 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009566static void
9567lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9568 struct lpfc_iocbq *rspiocb)
9569{
9570 IOCB_t *irsp = &rspiocb->iocb;
9571
9572 /* ELS cmd tag <ulpIoTag> completes */
9573 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009574 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009575 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009576 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009577 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009578 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9579 lpfc_ct_free_iocb(phba, cmdiocb);
9580 else
9581 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009582 return;
9583}
9584
James Smarte59058c2008-08-24 21:49:00 -04009585/**
James Smart5af5eee2010-10-22 11:06:38 -04009586 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009587 * @phba: Pointer to HBA context object.
9588 * @pring: Pointer to driver SLI ring object.
9589 * @cmdiocb: Pointer to driver command iocb object.
9590 *
James Smart5af5eee2010-10-22 11:06:38 -04009591 * This function issues an abort iocb for the provided command iocb down to
9592 * the port. Other than the case the outstanding command iocb is an abort
9593 * request, this function issues abort out unconditionally. This function is
9594 * called with hbalock held. The function returns 0 when it fails due to
9595 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009596 **/
James Smart5af5eee2010-10-22 11:06:38 -04009597static int
9598lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009599 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009600{
James Smart2e0fef82007-06-17 19:56:36 -05009601 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009602 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009603 IOCB_t *icmd = NULL;
9604 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009605 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009606 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009607
James Smart92d7f7b2007-06-17 19:56:38 -05009608 /*
9609 * There are certain command types we don't want to abort. And we
9610 * don't want to abort commands that are already in the process of
9611 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009612 */
9613 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009614 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009615 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9616 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009617 return 0;
9618
dea31012005-04-17 16:05:31 -05009619 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009620 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009621 if (abtsiocbp == NULL)
9622 return 0;
dea31012005-04-17 16:05:31 -05009623
James Smart07951072007-04-25 09:51:38 -04009624 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009625 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009626 */
9627 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9628
dea31012005-04-17 16:05:31 -05009629 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009630 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9631 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009632 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009633 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009634 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9635 }
James Smartda0436e2009-05-22 14:51:39 -04009636 else
9637 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009638 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009639 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009640
James Smart5ffc2662009-11-18 15:39:44 -05009641 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9642 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009643 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9644 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009645
James Smart2e0fef82007-06-17 19:56:36 -05009646 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009647 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9648 else
9649 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9650
9651 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009652
James Smarte8b62012007-08-02 11:10:09 -04009653 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9654 "0339 Abort xri x%x, original iotag x%x, "
9655 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009656 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009657 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009658 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009659
9660 if (phba->sli_rev == LPFC_SLI_REV4) {
9661 /* Note: both hbalock and ring_lock need to be set here */
9662 spin_lock_irqsave(&pring->ring_lock, iflags);
9663 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9664 abtsiocbp, 0);
9665 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9666 } else {
9667 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9668 abtsiocbp, 0);
9669 }
James Smart07951072007-04-25 09:51:38 -04009670
James Smartd7c255b2008-08-24 21:50:00 -04009671 if (retval)
9672 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009673
9674 /*
9675 * Caller to this routine should check for IOCB_ERROR
9676 * and handle it properly. This routine no longer removes
9677 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9678 */
9679 return retval;
9680}
9681
9682/**
9683 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9684 * @phba: Pointer to HBA context object.
9685 * @pring: Pointer to driver SLI ring object.
9686 * @cmdiocb: Pointer to driver command iocb object.
9687 *
9688 * This function issues an abort iocb for the provided command iocb. In case
9689 * of unloading, the abort iocb will not be issued to commands on the ELS
9690 * ring. Instead, the callback function shall be changed to those commands
9691 * so that nothing happens when them finishes. This function is called with
9692 * hbalock held. The function returns 0 when the command iocb is an abort
9693 * request.
9694 **/
9695int
9696lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9697 struct lpfc_iocbq *cmdiocb)
9698{
9699 struct lpfc_vport *vport = cmdiocb->vport;
9700 int retval = IOCB_ERROR;
9701 IOCB_t *icmd = NULL;
9702
9703 /*
9704 * There are certain command types we don't want to abort. And we
9705 * don't want to abort commands that are already in the process of
9706 * being aborted.
9707 */
9708 icmd = &cmdiocb->iocb;
9709 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9710 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9711 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9712 return 0;
9713
9714 /*
9715 * If we're unloading, don't abort iocb on the ELS ring, but change
9716 * the callback so that nothing happens when it finishes.
9717 */
9718 if ((vport->load_flag & FC_UNLOADING) &&
9719 (pring->ringno == LPFC_ELS_RING)) {
9720 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9721 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9722 else
9723 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9724 goto abort_iotag_exit;
9725 }
9726
9727 /* Now, we try to issue the abort to the cmdiocb out */
9728 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9729
James Smart07951072007-04-25 09:51:38 -04009730abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009731 /*
9732 * Caller to this routine should check for IOCB_ERROR
9733 * and handle it properly. This routine no longer removes
9734 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009735 */
James Smart2e0fef82007-06-17 19:56:36 -05009736 return retval;
dea31012005-04-17 16:05:31 -05009737}
9738
James Smarte59058c2008-08-24 21:49:00 -04009739/**
James Smart5af5eee2010-10-22 11:06:38 -04009740 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9741 * @phba: Pointer to HBA context object.
9742 * @pring: Pointer to driver SLI ring object.
9743 *
9744 * This function aborts all iocbs in the given ring and frees all the iocb
9745 * objects in txq. This function issues abort iocbs unconditionally for all
9746 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9747 * to complete before the return of this function. The caller is not required
9748 * to hold any locks.
9749 **/
9750static void
9751lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9752{
9753 LIST_HEAD(completions);
9754 struct lpfc_iocbq *iocb, *next_iocb;
9755
9756 if (pring->ringno == LPFC_ELS_RING)
9757 lpfc_fabric_abort_hba(phba);
9758
9759 spin_lock_irq(&phba->hbalock);
9760
9761 /* Take off all the iocbs on txq for cancelling */
9762 list_splice_init(&pring->txq, &completions);
9763 pring->txq_cnt = 0;
9764
9765 /* Next issue ABTS for everything on the txcmplq */
9766 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9767 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9768
9769 spin_unlock_irq(&phba->hbalock);
9770
9771 /* Cancel all the IOCBs from the completions list */
9772 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9773 IOERR_SLI_ABORTED);
9774}
9775
9776/**
9777 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9778 * @phba: pointer to lpfc HBA data structure.
9779 *
9780 * This routine will abort all pending and outstanding iocbs to an HBA.
9781 **/
9782void
9783lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9784{
9785 struct lpfc_sli *psli = &phba->sli;
9786 struct lpfc_sli_ring *pring;
9787 int i;
9788
9789 for (i = 0; i < psli->num_rings; i++) {
9790 pring = &psli->ring[i];
9791 lpfc_sli_iocb_ring_abort(phba, pring);
9792 }
9793}
9794
9795/**
James Smart3621a712009-04-06 18:47:14 -04009796 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009797 * @iocbq: Pointer to driver iocb object.
9798 * @vport: Pointer to driver virtual port object.
9799 * @tgt_id: SCSI ID of the target.
9800 * @lun_id: LUN ID of the scsi device.
9801 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9802 *
James Smart3621a712009-04-06 18:47:14 -04009803 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009804 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9805 * 0 if the filtering criteria is met for the given iocb and will return
9806 * 1 if the filtering criteria is not met.
9807 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9808 * given iocb is for the SCSI device specified by vport, tgt_id and
9809 * lun_id parameter.
9810 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9811 * given iocb is for the SCSI target specified by vport and tgt_id
9812 * parameters.
9813 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9814 * given iocb is for the SCSI host associated with the given vport.
9815 * This function is called with no locks held.
9816 **/
dea31012005-04-17 16:05:31 -05009817static int
James Smart51ef4c22007-08-02 11:10:31 -04009818lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9819 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009820 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009821{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009822 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009823 int rc = 1;
9824
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009825 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9826 return rc;
9827
James Smart51ef4c22007-08-02 11:10:31 -04009828 if (iocbq->vport != vport)
9829 return rc;
9830
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009831 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009832
James Smart495a7142008-06-14 22:52:59 -04009833 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009834 return rc;
9835
9836 switch (ctx_cmd) {
9837 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009838 if ((lpfc_cmd->rdata->pnode) &&
9839 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9840 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009841 rc = 0;
9842 break;
9843 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009844 if ((lpfc_cmd->rdata->pnode) &&
9845 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009846 rc = 0;
9847 break;
dea31012005-04-17 16:05:31 -05009848 case LPFC_CTX_HOST:
9849 rc = 0;
9850 break;
9851 default:
9852 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009853 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009854 break;
9855 }
9856
9857 return rc;
9858}
9859
James Smarte59058c2008-08-24 21:49:00 -04009860/**
James Smart3621a712009-04-06 18:47:14 -04009861 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009862 * @vport: Pointer to virtual port.
9863 * @tgt_id: SCSI ID of the target.
9864 * @lun_id: LUN ID of the scsi device.
9865 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9866 *
9867 * This function returns number of FCP commands pending for the vport.
9868 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9869 * commands pending on the vport associated with SCSI device specified
9870 * by tgt_id and lun_id parameters.
9871 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9872 * commands pending on the vport associated with SCSI target specified
9873 * by tgt_id parameter.
9874 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9875 * commands pending on the vport.
9876 * This function returns the number of iocbs which satisfy the filter.
9877 * This function is called without any lock held.
9878 **/
dea31012005-04-17 16:05:31 -05009879int
James Smart51ef4c22007-08-02 11:10:31 -04009880lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9881 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009882{
James Smart51ef4c22007-08-02 11:10:31 -04009883 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009884 struct lpfc_iocbq *iocbq;
9885 int sum, i;
dea31012005-04-17 16:05:31 -05009886
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009887 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9888 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009889
James Smart51ef4c22007-08-02 11:10:31 -04009890 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9891 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009892 sum++;
dea31012005-04-17 16:05:31 -05009893 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009894
dea31012005-04-17 16:05:31 -05009895 return sum;
9896}
9897
James Smarte59058c2008-08-24 21:49:00 -04009898/**
James Smart3621a712009-04-06 18:47:14 -04009899 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009900 * @phba: Pointer to HBA context object
9901 * @cmdiocb: Pointer to command iocb object.
9902 * @rspiocb: Pointer to response iocb object.
9903 *
9904 * This function is called when an aborted FCP iocb completes. This
9905 * function is called by the ring event handler with no lock held.
9906 * This function frees the iocb.
9907 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009908void
James Smart2e0fef82007-06-17 19:56:36 -05009909lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9910 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009911{
James Smartcb69f7d2011-12-13 13:21:57 -05009912 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -04009913 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -05009914 "original iotag x%x, abort cmd iotag x%x "
9915 "status 0x%x, reason 0x%x\n",
9916 cmdiocb->iocb.un.acxri.abortContextTag,
9917 cmdiocb->iocb.un.acxri.abortIoTag,
9918 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9919 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009920 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009921 return;
9922}
9923
James Smarte59058c2008-08-24 21:49:00 -04009924/**
James Smart3621a712009-04-06 18:47:14 -04009925 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009926 * @vport: Pointer to virtual port.
9927 * @pring: Pointer to driver SLI ring object.
9928 * @tgt_id: SCSI ID of the target.
9929 * @lun_id: LUN ID of the scsi device.
9930 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9931 *
9932 * This function sends an abort command for every SCSI command
9933 * associated with the given virtual port pending on the ring
9934 * filtered by lpfc_sli_validate_fcp_iocb function.
9935 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9936 * FCP iocbs associated with lun specified by tgt_id and lun_id
9937 * parameters
9938 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9939 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9940 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9941 * FCP iocbs associated with virtual port.
9942 * This function returns number of iocbs it failed to abort.
9943 * This function is called with no locks held.
9944 **/
dea31012005-04-17 16:05:31 -05009945int
James Smart51ef4c22007-08-02 11:10:31 -04009946lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9947 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009948{
James Smart51ef4c22007-08-02 11:10:31 -04009949 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009950 struct lpfc_iocbq *iocbq;
9951 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009952 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009953 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009954 int i;
dea31012005-04-17 16:05:31 -05009955
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009956 for (i = 1; i <= phba->sli.last_iotag; i++) {
9957 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009958
James Smart51ef4c22007-08-02 11:10:31 -04009959 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009960 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009961 continue;
9962
James Smartafbd8d82013-09-06 12:22:13 -04009963 /*
9964 * If the iocbq is already being aborted, don't take a second
9965 * action, but do count it.
9966 */
9967 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
9968 continue;
9969
dea31012005-04-17 16:05:31 -05009970 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009971 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009972 if (abtsiocb == NULL) {
9973 errcnt++;
9974 continue;
9975 }
dea31012005-04-17 16:05:31 -05009976
James Smartafbd8d82013-09-06 12:22:13 -04009977 /* indicate the IO is being aborted by the driver. */
9978 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
9979
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009980 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009981 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9982 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009983 if (phba->sli_rev == LPFC_SLI_REV4)
9984 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9985 else
9986 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009987 abtsiocb->iocb.ulpLe = 1;
9988 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -04009989 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -05009990
James Smart5ffc2662009-11-18 15:39:44 -05009991 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9992 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009993 if (iocbq->iocb_flag & LPFC_IO_FCP)
9994 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009995
James Smart2e0fef82007-06-17 19:56:36 -05009996 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009997 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9998 else
9999 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10000
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010001 /* Setup callback routine and issue the command. */
10002 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010003 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10004 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010005 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010006 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010007 errcnt++;
10008 continue;
10009 }
10010 }
10011
10012 return errcnt;
10013}
10014
James Smarte59058c2008-08-24 21:49:00 -040010015/**
James Smart3621a712009-04-06 18:47:14 -040010016 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010017 * @phba: Pointer to HBA context object.
10018 * @cmdiocbq: Pointer to command iocb.
10019 * @rspiocbq: Pointer to response iocb.
10020 *
10021 * This function is the completion handler for iocbs issued using
10022 * lpfc_sli_issue_iocb_wait function. This function is called by the
10023 * ring event handler function without any lock held. This function
10024 * can be called from both worker thread context and interrupt
10025 * context. This function also can be called from other thread which
10026 * cleans up the SLI layer objects.
10027 * This function copy the contents of the response iocb to the
10028 * response iocb memory object provided by the caller of
10029 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10030 * sleeps for the iocb completion.
10031 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010032static void
10033lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10034 struct lpfc_iocbq *cmdiocbq,
10035 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010036{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010037 wait_queue_head_t *pdone_q;
10038 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010039 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010040
James Smart2e0fef82007-06-17 19:56:36 -050010041 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010042 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10043
10044 /*
10045 * A time out has occurred for the iocb. If a time out
10046 * completion handler has been supplied, call it. Otherwise,
10047 * just free the iocbq.
10048 */
10049
10050 spin_unlock_irqrestore(&phba->hbalock, iflags);
10051 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10052 cmdiocbq->wait_iocb_cmpl = NULL;
10053 if (cmdiocbq->iocb_cmpl)
10054 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10055 else
10056 lpfc_sli_release_iocbq(phba, cmdiocbq);
10057 return;
10058 }
10059
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010060 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10061 if (cmdiocbq->context2 && rspiocbq)
10062 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10063 &rspiocbq->iocb, sizeof(IOCB_t));
10064
James Smart0f65ff62010-02-26 14:14:23 -050010065 /* Set the exchange busy flag for task management commands */
10066 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10067 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10068 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10069 cur_iocbq);
10070 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10071 }
10072
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010073 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010074 if (pdone_q)
10075 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010076 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010077 return;
10078}
10079
James Smarte59058c2008-08-24 21:49:00 -040010080/**
James Smartd11e31d2009-06-10 17:23:06 -040010081 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10082 * @phba: Pointer to HBA context object..
10083 * @piocbq: Pointer to command iocb.
10084 * @flag: Flag to test.
10085 *
10086 * This routine grabs the hbalock and then test the iocb_flag to
10087 * see if the passed in flag is set.
10088 * Returns:
10089 * 1 if flag is set.
10090 * 0 if flag is not set.
10091 **/
10092static int
10093lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10094 struct lpfc_iocbq *piocbq, uint32_t flag)
10095{
10096 unsigned long iflags;
10097 int ret;
10098
10099 spin_lock_irqsave(&phba->hbalock, iflags);
10100 ret = piocbq->iocb_flag & flag;
10101 spin_unlock_irqrestore(&phba->hbalock, iflags);
10102 return ret;
10103
10104}
10105
10106/**
James Smart3621a712009-04-06 18:47:14 -040010107 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010108 * @phba: Pointer to HBA context object..
10109 * @pring: Pointer to sli ring.
10110 * @piocb: Pointer to command iocb.
10111 * @prspiocbq: Pointer to response iocb.
10112 * @timeout: Timeout in number of seconds.
10113 *
10114 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010115 * iocb to complete. The iocb_cmpl field of the shall be used
10116 * to handle iocbs which time out. If the field is NULL, the
10117 * function shall free the iocbq structure. If more clean up is
10118 * needed, the caller is expected to provide a completion function
10119 * that will provide the needed clean up. If the iocb command is
10120 * not completed within timeout seconds, the function will either
10121 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10122 * completion function set in the iocb_cmpl field and then return
10123 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10124 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010125 * The function waits for the iocb completion using an
10126 * non-interruptible wait.
10127 * This function will sleep while waiting for iocb completion.
10128 * So, this function should not be called from any context which
10129 * does not allow sleeping. Due to the same reason, this function
10130 * cannot be called with interrupt disabled.
10131 * This function assumes that the iocb completions occur while
10132 * this function sleep. So, this function cannot be called from
10133 * the thread which process iocb completion for this ring.
10134 * This function clears the iocb_flag of the iocb object before
10135 * issuing the iocb and the iocb completion handler sets this
10136 * flag and wakes this thread when the iocb completes.
10137 * The contents of the response iocb will be copied to prspiocbq
10138 * by the completion handler when the command completes.
10139 * This function returns IOCB_SUCCESS when success.
10140 * This function is called with no lock held.
10141 **/
dea31012005-04-17 16:05:31 -050010142int
James Smart2e0fef82007-06-17 19:56:36 -050010143lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010144 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010145 struct lpfc_iocbq *piocb,
10146 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010147 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010148{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010149 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010150 long timeleft, timeout_req = 0;
10151 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010152 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010153 struct lpfc_iocbq *iocb;
10154 int txq_cnt = 0;
10155 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010156 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010157 unsigned long iflags;
10158 bool iocb_completed = true;
10159
dea31012005-04-17 16:05:31 -050010160 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010161 * If the caller has provided a response iocbq buffer, then context2
10162 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010163 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010164 if (prspiocbq) {
10165 if (piocb->context2)
10166 return IOCB_ERROR;
10167 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010168 }
10169
James Smart5a0916b2013-07-15 18:31:42 -040010170 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010171 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10172 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010173 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010174
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010175 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010176 if (lpfc_readl(phba->HCregaddr, &creg_val))
10177 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010178 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10179 writel(creg_val, phba->HCregaddr);
10180 readl(phba->HCregaddr); /* flush */
10181 }
10182
James Smart2a9bf3d2010-06-07 15:24:45 -040010183 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10184 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010185 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010186 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010187 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010188 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010189 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010190 spin_lock_irqsave(&phba->hbalock, iflags);
10191 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010192
James Smart5a0916b2013-07-15 18:31:42 -040010193 /*
10194 * IOCB timed out. Inform the wake iocb wait
10195 * completion function and set local status
10196 */
10197
10198 iocb_completed = false;
10199 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10200 }
10201 spin_unlock_irqrestore(&phba->hbalock, iflags);
10202 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010203 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010204 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010205 /* Note: we are not indicating if the IOCB has a success
10206 * status or not - that's for the caller to check.
10207 * IOCB_SUCCESS means just that the command was sent and
10208 * completed. Not that it completed successfully.
10209 * */
James Smart7054a602007-04-25 09:52:34 -040010210 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010211 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010212 "0338 IOCB wait timeout error - no "
10213 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010214 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010215 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010216 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010217 "0330 IOCB wake NOT set, "
10218 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010219 timeout, (timeleft / jiffies));
10220 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010221 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010222 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010223 if (phba->cfg_log_verbose & LOG_SLI) {
10224 list_for_each_entry(iocb, &pring->txq, list) {
10225 txq_cnt++;
10226 }
10227 list_for_each_entry(iocb, &pring->txcmplq, list) {
10228 txcmplq_cnt++;
10229 }
10230 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10231 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10232 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10233 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010234 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010235 } else {
10236 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010237 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010238 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010239 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010240 }
10241
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010242 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010243 if (lpfc_readl(phba->HCregaddr, &creg_val))
10244 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010245 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10246 writel(creg_val, phba->HCregaddr);
10247 readl(phba->HCregaddr); /* flush */
10248 }
10249
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010250 if (prspiocbq)
10251 piocb->context2 = NULL;
10252
10253 piocb->context_un.wait_queue = NULL;
10254 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010255 return retval;
10256}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010257
James Smarte59058c2008-08-24 21:49:00 -040010258/**
James Smart3621a712009-04-06 18:47:14 -040010259 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010260 * @phba: Pointer to HBA context object.
10261 * @pmboxq: Pointer to driver mailbox object.
10262 * @timeout: Timeout in number of seconds.
10263 *
10264 * This function issues the mailbox to firmware and waits for the
10265 * mailbox command to complete. If the mailbox command is not
10266 * completed within timeout seconds, it returns MBX_TIMEOUT.
10267 * The function waits for the mailbox completion using an
10268 * interruptible wait. If the thread is woken up due to a
10269 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10270 * should not free the mailbox resources, if this function returns
10271 * MBX_TIMEOUT.
10272 * This function will sleep while waiting for mailbox completion.
10273 * So, this function should not be called from any context which
10274 * does not allow sleeping. Due to the same reason, this function
10275 * cannot be called with interrupt disabled.
10276 * This function assumes that the mailbox completion occurs while
10277 * this function sleep. So, this function cannot be called from
10278 * the worker thread which processes mailbox completion.
10279 * This function is called in the context of HBA management
10280 * applications.
10281 * This function returns MBX_SUCCESS when successful.
10282 * This function is called with no lock held.
10283 **/
dea31012005-04-17 16:05:31 -050010284int
James Smart2e0fef82007-06-17 19:56:36 -050010285lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010286 uint32_t timeout)
10287{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010288 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010289 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010290 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010291 unsigned long flag;
dea31012005-04-17 16:05:31 -050010292
James Smartb230b8a2013-05-31 17:05:27 -040010293 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010294 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010295 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010296
James Smart495a7142008-06-14 22:52:59 -040010297 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010298 /* setup wake call as IOCB callback */
10299 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10300 /* setup context field to pass wait_queue pointer to wake function */
10301 pmboxq->context1 = &done_q;
10302
dea31012005-04-17 16:05:31 -050010303 /* now issue the command */
10304 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010305 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010306 wait_event_interruptible_timeout(done_q,
10307 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010308 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010309
James Smart858c9f62007-06-17 19:56:39 -050010310 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010311 /* restore the possible extended buffer for free resource */
10312 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010313 /*
10314 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10315 * else do not free the resources.
10316 */
James Smartd7c47992010-06-08 18:31:54 -040010317 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010318 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010319 } else {
James Smart7054a602007-04-25 09:52:34 -040010320 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010321 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10322 }
10323 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010324 } else {
10325 /* restore the possible extended buffer for free resource */
10326 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010327 }
10328
dea31012005-04-17 16:05:31 -050010329 return retval;
10330}
10331
James Smarte59058c2008-08-24 21:49:00 -040010332/**
James Smart3772a992009-05-22 14:50:54 -040010333 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010334 * @phba: Pointer to HBA context.
10335 *
James Smart3772a992009-05-22 14:50:54 -040010336 * This function is called to shutdown the driver's mailbox sub-system.
10337 * It first marks the mailbox sub-system is in a block state to prevent
10338 * the asynchronous mailbox command from issued off the pending mailbox
10339 * command queue. If the mailbox command sub-system shutdown is due to
10340 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10341 * the mailbox sub-system flush routine to forcefully bring down the
10342 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10343 * as with offline or HBA function reset), this routine will wait for the
10344 * outstanding mailbox command to complete before invoking the mailbox
10345 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010346 **/
James Smart3772a992009-05-22 14:50:54 -040010347void
James Smart618a5232012-06-12 13:54:36 -040010348lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010349{
James Smart3772a992009-05-22 14:50:54 -040010350 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010351 unsigned long timeout;
10352
James Smart618a5232012-06-12 13:54:36 -040010353 if (mbx_action == LPFC_MBX_NO_WAIT) {
10354 /* delay 100ms for port state */
10355 msleep(100);
10356 lpfc_sli_mbox_sys_flush(phba);
10357 return;
10358 }
James Smarta183a152011-10-10 21:32:43 -040010359 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010360
James Smart3772a992009-05-22 14:50:54 -040010361 spin_lock_irq(&phba->hbalock);
10362 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010363
10364 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010365 /* Determine how long we might wait for the active mailbox
10366 * command to be gracefully completed by firmware.
10367 */
James Smarta183a152011-10-10 21:32:43 -040010368 if (phba->sli.mbox_active)
10369 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10370 phba->sli.mbox_active) *
10371 1000) + jiffies;
10372 spin_unlock_irq(&phba->hbalock);
10373
James Smart3772a992009-05-22 14:50:54 -040010374 while (phba->sli.mbox_active) {
10375 /* Check active mailbox complete status every 2ms */
10376 msleep(2);
10377 if (time_after(jiffies, timeout))
10378 /* Timeout, let the mailbox flush routine to
10379 * forcefully release active mailbox command
10380 */
10381 break;
10382 }
James Smartd7069f02012-03-01 22:36:29 -050010383 } else
10384 spin_unlock_irq(&phba->hbalock);
10385
James Smart3772a992009-05-22 14:50:54 -040010386 lpfc_sli_mbox_sys_flush(phba);
10387}
10388
10389/**
10390 * lpfc_sli_eratt_read - read sli-3 error attention events
10391 * @phba: Pointer to HBA context.
10392 *
10393 * This function is called to read the SLI3 device error attention registers
10394 * for possible error attention events. The caller must hold the hostlock
10395 * with spin_lock_irq().
10396 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010397 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010398 * Register and returns 0 otherwise.
10399 **/
10400static int
10401lpfc_sli_eratt_read(struct lpfc_hba *phba)
10402{
James Smarted957682007-06-17 19:56:37 -050010403 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010404
James Smart3772a992009-05-22 14:50:54 -040010405 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010406 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10407 goto unplug_err;
10408
James Smart3772a992009-05-22 14:50:54 -040010409 if (ha_copy & HA_ERATT) {
10410 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010411 if (lpfc_sli_read_hs(phba))
10412 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010413
James Smart3772a992009-05-22 14:50:54 -040010414 /* Check if there is a deferred error condition is active */
10415 if ((HS_FFER1 & phba->work_hs) &&
10416 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010417 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010418 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010419 /* Clear all interrupt enable conditions */
10420 writel(0, phba->HCregaddr);
10421 readl(phba->HCregaddr);
10422 }
10423
10424 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010425 phba->work_ha |= HA_ERATT;
10426 /* Indicate polling handles this ERATT */
10427 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010428 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010429 }
James Smart3772a992009-05-22 14:50:54 -040010430 return 0;
James Smart9940b972011-03-11 16:06:12 -050010431
10432unplug_err:
10433 /* Set the driver HS work bitmap */
10434 phba->work_hs |= UNPLUG_ERR;
10435 /* Set the driver HA work bitmap */
10436 phba->work_ha |= HA_ERATT;
10437 /* Indicate polling handles this ERATT */
10438 phba->hba_flag |= HBA_ERATT_HANDLED;
10439 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010440}
10441
James Smarte59058c2008-08-24 21:49:00 -040010442/**
James Smartda0436e2009-05-22 14:51:39 -040010443 * lpfc_sli4_eratt_read - read sli-4 error attention events
10444 * @phba: Pointer to HBA context.
10445 *
10446 * This function is called to read the SLI4 device error attention registers
10447 * for possible error attention events. The caller must hold the hostlock
10448 * with spin_lock_irq().
10449 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010450 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010451 * Register and returns 0 otherwise.
10452 **/
10453static int
10454lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10455{
10456 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010457 uint32_t if_type, portsmphr;
10458 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010459
James Smart2fcee4b2010-12-15 17:57:46 -050010460 /*
10461 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010462 * registers for error attention. This can be changed later.
10463 */
James Smart2fcee4b2010-12-15 17:57:46 -050010464 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10465 switch (if_type) {
10466 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010467 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10468 &uerr_sta_lo) ||
10469 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10470 &uerr_sta_hi)) {
10471 phba->work_hs |= UNPLUG_ERR;
10472 phba->work_ha |= HA_ERATT;
10473 phba->hba_flag |= HBA_ERATT_HANDLED;
10474 return 1;
10475 }
James Smart2fcee4b2010-12-15 17:57:46 -050010476 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10477 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10479 "1423 HBA Unrecoverable error: "
10480 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10481 "ue_mask_lo_reg=0x%x, "
10482 "ue_mask_hi_reg=0x%x\n",
10483 uerr_sta_lo, uerr_sta_hi,
10484 phba->sli4_hba.ue_mask_lo,
10485 phba->sli4_hba.ue_mask_hi);
10486 phba->work_status[0] = uerr_sta_lo;
10487 phba->work_status[1] = uerr_sta_hi;
10488 phba->work_ha |= HA_ERATT;
10489 phba->hba_flag |= HBA_ERATT_HANDLED;
10490 return 1;
10491 }
10492 break;
10493 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010494 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10495 &portstat_reg.word0) ||
10496 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10497 &portsmphr)){
10498 phba->work_hs |= UNPLUG_ERR;
10499 phba->work_ha |= HA_ERATT;
10500 phba->hba_flag |= HBA_ERATT_HANDLED;
10501 return 1;
10502 }
James Smart2fcee4b2010-12-15 17:57:46 -050010503 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10504 phba->work_status[0] =
10505 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10506 phba->work_status[1] =
10507 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010509 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010510 "port status reg 0x%x, "
10511 "port smphr reg 0x%x, "
10512 "error 1=0x%x, error 2=0x%x\n",
10513 portstat_reg.word0,
10514 portsmphr,
10515 phba->work_status[0],
10516 phba->work_status[1]);
10517 phba->work_ha |= HA_ERATT;
10518 phba->hba_flag |= HBA_ERATT_HANDLED;
10519 return 1;
10520 }
10521 break;
10522 case LPFC_SLI_INTF_IF_TYPE_1:
10523 default:
James Smarta747c9c2009-11-18 15:41:10 -050010524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010525 "2886 HBA Error Attention on unsupported "
10526 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010527 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010528 }
James Smart2fcee4b2010-12-15 17:57:46 -050010529
James Smartda0436e2009-05-22 14:51:39 -040010530 return 0;
10531}
10532
10533/**
James Smart3621a712009-04-06 18:47:14 -040010534 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010535 * @phba: Pointer to HBA context.
10536 *
James Smart3772a992009-05-22 14:50:54 -040010537 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010538 * error attention register bit for error attention events.
10539 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010540 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010541 * Register and returns 0 otherwise.
10542 **/
10543int
10544lpfc_sli_check_eratt(struct lpfc_hba *phba)
10545{
10546 uint32_t ha_copy;
10547
10548 /* If somebody is waiting to handle an eratt, don't process it
10549 * here. The brdkill function will do this.
10550 */
10551 if (phba->link_flag & LS_IGNORE_ERATT)
10552 return 0;
10553
10554 /* Check if interrupt handler handles this ERATT */
10555 spin_lock_irq(&phba->hbalock);
10556 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10557 /* Interrupt handler has handled ERATT */
10558 spin_unlock_irq(&phba->hbalock);
10559 return 0;
10560 }
10561
James Smarta257bf92009-04-06 18:48:10 -040010562 /*
10563 * If there is deferred error attention, do not check for error
10564 * attention
10565 */
10566 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10567 spin_unlock_irq(&phba->hbalock);
10568 return 0;
10569 }
10570
James Smart3772a992009-05-22 14:50:54 -040010571 /* If PCI channel is offline, don't process it */
10572 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010573 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010574 return 0;
10575 }
10576
10577 switch (phba->sli_rev) {
10578 case LPFC_SLI_REV2:
10579 case LPFC_SLI_REV3:
10580 /* Read chip Host Attention (HA) register */
10581 ha_copy = lpfc_sli_eratt_read(phba);
10582 break;
James Smartda0436e2009-05-22 14:51:39 -040010583 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010584 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010585 ha_copy = lpfc_sli4_eratt_read(phba);
10586 break;
James Smart3772a992009-05-22 14:50:54 -040010587 default:
10588 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10589 "0299 Invalid SLI revision (%d)\n",
10590 phba->sli_rev);
10591 ha_copy = 0;
10592 break;
James Smart93996272008-08-24 21:50:30 -040010593 }
10594 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010595
10596 return ha_copy;
10597}
10598
10599/**
10600 * lpfc_intr_state_check - Check device state for interrupt handling
10601 * @phba: Pointer to HBA context.
10602 *
10603 * This inline routine checks whether a device or its PCI slot is in a state
10604 * that the interrupt should be handled.
10605 *
10606 * This function returns 0 if the device or the PCI slot is in a state that
10607 * interrupt should be handled, otherwise -EIO.
10608 */
10609static inline int
10610lpfc_intr_state_check(struct lpfc_hba *phba)
10611{
10612 /* If the pci channel is offline, ignore all the interrupts */
10613 if (unlikely(pci_channel_offline(phba->pcidev)))
10614 return -EIO;
10615
10616 /* Update device level interrupt statistics */
10617 phba->sli.slistat.sli_intr++;
10618
10619 /* Ignore all interrupts during initialization. */
10620 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10621 return -EIO;
10622
James Smart93996272008-08-24 21:50:30 -040010623 return 0;
10624}
10625
10626/**
James Smart3772a992009-05-22 14:50:54 -040010627 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010628 * @irq: Interrupt number.
10629 * @dev_id: The device context pointer.
10630 *
James Smart93996272008-08-24 21:50:30 -040010631 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010632 * service routine when device with SLI-3 interface spec is enabled with
10633 * MSI-X multi-message interrupt mode and there are slow-path events in
10634 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10635 * interrupt mode, this function is called as part of the device-level
10636 * interrupt handler. When the PCI slot is in error recovery or the HBA
10637 * is undergoing initialization, the interrupt handler will not process
10638 * the interrupt. The link attention and ELS ring attention events are
10639 * handled by the worker thread. The interrupt handler signals the worker
10640 * thread and returns for these events. This function is called without
10641 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010642 * structures.
10643 *
10644 * This function returns IRQ_HANDLED when interrupt is handled else it
10645 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010646 **/
dea31012005-04-17 16:05:31 -050010647irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010648lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010649{
James Smart2e0fef82007-06-17 19:56:36 -050010650 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010651 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010652 uint32_t work_ha_copy;
10653 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010654 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010655 uint32_t control;
10656
James Smart92d7f7b2007-06-17 19:56:38 -050010657 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010658 struct lpfc_vport *vport;
10659 struct lpfc_nodelist *ndlp;
10660 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010661 LPFC_MBOXQ_t *pmb;
10662 int rc;
10663
dea31012005-04-17 16:05:31 -050010664 /*
10665 * Get the driver's phba structure from the dev_id and
10666 * assume the HBA is not interrupting.
10667 */
James Smart93996272008-08-24 21:50:30 -040010668 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010669
10670 if (unlikely(!phba))
10671 return IRQ_NONE;
10672
dea31012005-04-17 16:05:31 -050010673 /*
James Smart93996272008-08-24 21:50:30 -040010674 * Stuff needs to be attented to when this function is invoked as an
10675 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010676 */
James Smart93996272008-08-24 21:50:30 -040010677 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010678 /* Check device state for handling interrupt */
10679 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010680 return IRQ_NONE;
10681 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010682 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010683 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10684 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010685 /* If somebody is waiting to handle an eratt don't process it
10686 * here. The brdkill function will do this.
10687 */
10688 if (phba->link_flag & LS_IGNORE_ERATT)
10689 ha_copy &= ~HA_ERATT;
10690 /* Check the need for handling ERATT in interrupt handler */
10691 if (ha_copy & HA_ERATT) {
10692 if (phba->hba_flag & HBA_ERATT_HANDLED)
10693 /* ERATT polling has handled ERATT */
10694 ha_copy &= ~HA_ERATT;
10695 else
10696 /* Indicate interrupt handler handles ERATT */
10697 phba->hba_flag |= HBA_ERATT_HANDLED;
10698 }
James Smarta257bf92009-04-06 18:48:10 -040010699
10700 /*
10701 * If there is deferred error attention, do not check for any
10702 * interrupt.
10703 */
10704 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010705 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010706 return IRQ_NONE;
10707 }
10708
James Smart93996272008-08-24 21:50:30 -040010709 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010710 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10711 goto unplug_error;
10712
James Smarta747c9c2009-11-18 15:41:10 -050010713 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10714 HC_LAINT_ENA | HC_ERINT_ENA),
10715 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010716 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10717 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010718 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010719 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010720 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010721 } else
10722 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010723
dea31012005-04-17 16:05:31 -050010724 work_ha_copy = ha_copy & phba->work_ha_mask;
10725
James Smart93996272008-08-24 21:50:30 -040010726 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010727 if (work_ha_copy & HA_LATT) {
10728 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10729 /*
10730 * Turn off Link Attention interrupts
10731 * until CLEAR_LA done
10732 */
James Smart5b75da22008-12-04 22:39:35 -050010733 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010734 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010735 if (lpfc_readl(phba->HCregaddr, &control))
10736 goto unplug_error;
dea31012005-04-17 16:05:31 -050010737 control &= ~HC_LAINT_ENA;
10738 writel(control, phba->HCregaddr);
10739 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010740 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010741 }
10742 else
10743 work_ha_copy &= ~HA_LATT;
10744 }
10745
James Smart93996272008-08-24 21:50:30 -040010746 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010747 /*
10748 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10749 * the only slow ring.
10750 */
10751 status = (work_ha_copy &
10752 (HA_RXMASK << (4*LPFC_ELS_RING)));
10753 status >>= (4*LPFC_ELS_RING);
10754 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010755 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010756 if (lpfc_readl(phba->HCregaddr, &control))
10757 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010758
10759 lpfc_debugfs_slow_ring_trc(phba,
10760 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10761 control, status,
10762 (uint32_t)phba->sli.slistat.sli_intr);
10763
James Smart858c9f62007-06-17 19:56:39 -050010764 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010765 lpfc_debugfs_slow_ring_trc(phba,
10766 "ISR Disable ring:"
10767 "pwork:x%x hawork:x%x wait:x%x",
10768 phba->work_ha, work_ha_copy,
10769 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010770 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010771
James Smart858c9f62007-06-17 19:56:39 -050010772 control &=
10773 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010774 writel(control, phba->HCregaddr);
10775 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010776 }
James Smarta58cbd52007-08-02 11:09:43 -040010777 else {
10778 lpfc_debugfs_slow_ring_trc(phba,
10779 "ISR slow ring: pwork:"
10780 "x%x hawork:x%x wait:x%x",
10781 phba->work_ha, work_ha_copy,
10782 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010783 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010784 }
James Smart5b75da22008-12-04 22:39:35 -050010785 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010786 }
10787 }
James Smart5b75da22008-12-04 22:39:35 -050010788 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010789 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010790 if (lpfc_sli_read_hs(phba))
10791 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010792 /*
10793 * Check if there is a deferred error condition
10794 * is active
10795 */
10796 if ((HS_FFER1 & phba->work_hs) &&
10797 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010798 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10799 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010800 phba->hba_flag |= DEFER_ERATT;
10801 /* Clear all interrupt enable conditions */
10802 writel(0, phba->HCregaddr);
10803 readl(phba->HCregaddr);
10804 }
10805 }
10806
James Smart93996272008-08-24 21:50:30 -040010807 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010808 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010809 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010810 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010811 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010812
10813 /* First check out the status word */
10814 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10815 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010816 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010817 /*
10818 * Stray Mailbox Interrupt, mbxCommand <cmd>
10819 * mbxStatus <status>
10820 */
James Smart09372822008-01-11 01:52:54 -050010821 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010822 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010823 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010824 "Interrupt mbxCommand x%x "
10825 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010826 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010827 pmbox->mbxCommand,
10828 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010829 /* clear mailbox attention bit */
10830 work_ha_copy &= ~HA_MBATT;
10831 } else {
James Smart97eab632008-04-07 10:16:05 -040010832 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010833 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010834 phba->last_completion_time = jiffies;
10835 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010836 if (pmb->mbox_cmpl) {
10837 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10838 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010839 if (pmb->out_ext_byte_len &&
10840 pmb->context2)
10841 lpfc_sli_pcimem_bcopy(
10842 phba->mbox_ext,
10843 pmb->context2,
10844 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010845 }
James Smart09372822008-01-11 01:52:54 -050010846 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10847 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10848
10849 lpfc_debugfs_disc_trc(vport,
10850 LPFC_DISC_TRC_MBOX_VPORT,
10851 "MBOX dflt rpi: : "
10852 "status:x%x rpi:x%x",
10853 (uint32_t)pmbox->mbxStatus,
10854 pmbox->un.varWords[0], 0);
10855
10856 if (!pmbox->mbxStatus) {
10857 mp = (struct lpfc_dmabuf *)
10858 (pmb->context1);
10859 ndlp = (struct lpfc_nodelist *)
10860 pmb->context2;
10861
10862 /* Reg_LOGIN of dflt RPI was
10863 * successful. new lets get
10864 * rid of the RPI using the
10865 * same mbox buffer.
10866 */
10867 lpfc_unreg_login(phba,
10868 vport->vpi,
10869 pmbox->un.varWords[0],
10870 pmb);
10871 pmb->mbox_cmpl =
10872 lpfc_mbx_cmpl_dflt_rpi;
10873 pmb->context1 = mp;
10874 pmb->context2 = ndlp;
10875 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010876 rc = lpfc_sli_issue_mbox(phba,
10877 pmb,
10878 MBX_NOWAIT);
10879 if (rc != MBX_BUSY)
10880 lpfc_printf_log(phba,
10881 KERN_ERR,
10882 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010883 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010884 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010885 if (rc != MBX_NOT_FINISHED)
10886 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010887 }
10888 }
James Smart5b75da22008-12-04 22:39:35 -050010889 spin_lock_irqsave(
10890 &phba->pport->work_port_lock,
10891 iflag);
James Smart09372822008-01-11 01:52:54 -050010892 phba->pport->work_port_events &=
10893 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010894 spin_unlock_irqrestore(
10895 &phba->pport->work_port_lock,
10896 iflag);
James Smart09372822008-01-11 01:52:54 -050010897 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010898 }
James Smart97eab632008-04-07 10:16:05 -040010899 } else
James Smart5b75da22008-12-04 22:39:35 -050010900 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010901
James Smart92d7f7b2007-06-17 19:56:38 -050010902 if ((work_ha_copy & HA_MBATT) &&
10903 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010904send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010905 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010906 do {
10907 rc = lpfc_sli_issue_mbox(phba, NULL,
10908 MBX_NOWAIT);
10909 } while (rc == MBX_NOT_FINISHED);
10910 if (rc != MBX_SUCCESS)
10911 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10912 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010913 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010914 }
10915
James Smart5b75da22008-12-04 22:39:35 -050010916 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010917 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010918 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010919 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010920 }
James Smart93996272008-08-24 21:50:30 -040010921 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010922unplug_error:
10923 spin_unlock_irqrestore(&phba->hbalock, iflag);
10924 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010925
James Smart3772a992009-05-22 14:50:54 -040010926} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010927
10928/**
James Smart3772a992009-05-22 14:50:54 -040010929 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010930 * @irq: Interrupt number.
10931 * @dev_id: The device context pointer.
10932 *
10933 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010934 * service routine when device with SLI-3 interface spec is enabled with
10935 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10936 * ring event in the HBA. However, when the device is enabled with either
10937 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10938 * device-level interrupt handler. When the PCI slot is in error recovery
10939 * or the HBA is undergoing initialization, the interrupt handler will not
10940 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10941 * the intrrupt context. This function is called without any lock held.
10942 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010943 *
10944 * This function returns IRQ_HANDLED when interrupt is handled else it
10945 * returns IRQ_NONE.
10946 **/
10947irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010948lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010949{
10950 struct lpfc_hba *phba;
10951 uint32_t ha_copy;
10952 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010953 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010954
10955 /* Get the driver's phba structure from the dev_id and
10956 * assume the HBA is not interrupting.
10957 */
10958 phba = (struct lpfc_hba *) dev_id;
10959
10960 if (unlikely(!phba))
10961 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010962
10963 /*
James Smart93996272008-08-24 21:50:30 -040010964 * Stuff needs to be attented to when this function is invoked as an
10965 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010966 */
James Smart93996272008-08-24 21:50:30 -040010967 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010968 /* Check device state for handling interrupt */
10969 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010970 return IRQ_NONE;
10971 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010972 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10973 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010974 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010975 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010976 /*
10977 * If there is deferred error attention, do not check for
10978 * any interrupt.
10979 */
10980 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010981 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010982 return IRQ_NONE;
10983 }
James Smart93996272008-08-24 21:50:30 -040010984 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10985 phba->HAregaddr);
10986 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010987 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010988 } else
10989 ha_copy = phba->ha_copy;
10990
10991 /*
10992 * Process all events on FCP ring. Take the optimized path for FCP IO.
10993 */
10994 ha_copy &= ~(phba->work_ha_mask);
10995
10996 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010997 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010998 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010999 lpfc_sli_handle_fast_ring_event(phba,
11000 &phba->sli.ring[LPFC_FCP_RING],
11001 status);
James Smarta4bc3372006-12-02 13:34:16 -050011002
11003 if (phba->cfg_multi_ring_support == 2) {
11004 /*
James Smart93996272008-08-24 21:50:30 -040011005 * Process all events on extra ring. Take the optimized path
11006 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011007 */
James Smart93996272008-08-24 21:50:30 -040011008 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011009 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011010 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011011 lpfc_sli_handle_fast_ring_event(phba,
11012 &phba->sli.ring[LPFC_EXTRA_RING],
11013 status);
11014 }
11015 }
dea31012005-04-17 16:05:31 -050011016 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011017} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011018
James Smart93996272008-08-24 21:50:30 -040011019/**
James Smart3772a992009-05-22 14:50:54 -040011020 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011021 * @irq: Interrupt number.
11022 * @dev_id: The device context pointer.
11023 *
James Smart3772a992009-05-22 14:50:54 -040011024 * This function is the HBA device-level interrupt handler to device with
11025 * SLI-3 interface spec, called from the PCI layer when either MSI or
11026 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11027 * requires driver attention. This function invokes the slow-path interrupt
11028 * attention handling function and fast-path interrupt attention handling
11029 * function in turn to process the relevant HBA attention events. This
11030 * function is called without any lock held. It gets the hbalock to access
11031 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011032 *
11033 * This function returns IRQ_HANDLED when interrupt is handled, else it
11034 * returns IRQ_NONE.
11035 **/
11036irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011037lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011038{
11039 struct lpfc_hba *phba;
11040 irqreturn_t sp_irq_rc, fp_irq_rc;
11041 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011042 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011043
11044 /*
11045 * Get the driver's phba structure from the dev_id and
11046 * assume the HBA is not interrupting.
11047 */
11048 phba = (struct lpfc_hba *) dev_id;
11049
11050 if (unlikely(!phba))
11051 return IRQ_NONE;
11052
James Smart3772a992009-05-22 14:50:54 -040011053 /* Check device state for handling interrupt */
11054 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011055 return IRQ_NONE;
11056
11057 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011058 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11059 spin_unlock(&phba->hbalock);
11060 return IRQ_HANDLED;
11061 }
11062
James Smart93996272008-08-24 21:50:30 -040011063 if (unlikely(!phba->ha_copy)) {
11064 spin_unlock(&phba->hbalock);
11065 return IRQ_NONE;
11066 } else if (phba->ha_copy & HA_ERATT) {
11067 if (phba->hba_flag & HBA_ERATT_HANDLED)
11068 /* ERATT polling has handled ERATT */
11069 phba->ha_copy &= ~HA_ERATT;
11070 else
11071 /* Indicate interrupt handler handles ERATT */
11072 phba->hba_flag |= HBA_ERATT_HANDLED;
11073 }
11074
James Smarta257bf92009-04-06 18:48:10 -040011075 /*
11076 * If there is deferred error attention, do not check for any interrupt.
11077 */
11078 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011079 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011080 return IRQ_NONE;
11081 }
11082
James Smart93996272008-08-24 21:50:30 -040011083 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011084 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11085 spin_unlock(&phba->hbalock);
11086 return IRQ_HANDLED;
11087 }
James Smarta747c9c2009-11-18 15:41:10 -050011088 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11089 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11090 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011091 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011092 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011093 readl(phba->HAregaddr); /* flush */
11094 spin_unlock(&phba->hbalock);
11095
11096 /*
11097 * Invokes slow-path host attention interrupt handling as appropriate.
11098 */
11099
11100 /* status of events with mailbox and link attention */
11101 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11102
11103 /* status of events with ELS ring */
11104 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11105 status2 >>= (4*LPFC_ELS_RING);
11106
11107 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011108 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011109 else
11110 sp_irq_rc = IRQ_NONE;
11111
11112 /*
11113 * Invoke fast-path host attention interrupt handling as appropriate.
11114 */
11115
11116 /* status of events with FCP ring */
11117 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11118 status1 >>= (4*LPFC_FCP_RING);
11119
11120 /* status of events with extra ring */
11121 if (phba->cfg_multi_ring_support == 2) {
11122 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11123 status2 >>= (4*LPFC_EXTRA_RING);
11124 } else
11125 status2 = 0;
11126
11127 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011128 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011129 else
11130 fp_irq_rc = IRQ_NONE;
11131
11132 /* Return device-level interrupt handling status */
11133 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011134} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011135
11136/**
11137 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11138 * @phba: pointer to lpfc hba data structure.
11139 *
11140 * This routine is invoked by the worker thread to process all the pending
11141 * SLI4 FCP abort XRI events.
11142 **/
11143void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11144{
11145 struct lpfc_cq_event *cq_event;
11146
11147 /* First, declare the fcp xri abort event has been handled */
11148 spin_lock_irq(&phba->hbalock);
11149 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11150 spin_unlock_irq(&phba->hbalock);
11151 /* Now, handle all the fcp xri abort events */
11152 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11153 /* Get the first event from the head of the event queue */
11154 spin_lock_irq(&phba->hbalock);
11155 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11156 cq_event, struct lpfc_cq_event, list);
11157 spin_unlock_irq(&phba->hbalock);
11158 /* Notify aborted XRI for FCP work queue */
11159 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11160 /* Free the event processed back to the free pool */
11161 lpfc_sli4_cq_event_release(phba, cq_event);
11162 }
11163}
11164
11165/**
11166 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11167 * @phba: pointer to lpfc hba data structure.
11168 *
11169 * This routine is invoked by the worker thread to process all the pending
11170 * SLI4 els abort xri events.
11171 **/
11172void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11173{
11174 struct lpfc_cq_event *cq_event;
11175
11176 /* First, declare the els xri abort event has been handled */
11177 spin_lock_irq(&phba->hbalock);
11178 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11179 spin_unlock_irq(&phba->hbalock);
11180 /* Now, handle all the els xri abort events */
11181 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11182 /* Get the first event from the head of the event queue */
11183 spin_lock_irq(&phba->hbalock);
11184 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11185 cq_event, struct lpfc_cq_event, list);
11186 spin_unlock_irq(&phba->hbalock);
11187 /* Notify aborted XRI for ELS work queue */
11188 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11189 /* Free the event processed back to the free pool */
11190 lpfc_sli4_cq_event_release(phba, cq_event);
11191 }
11192}
11193
James Smart341af102010-01-26 23:07:37 -050011194/**
11195 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11196 * @phba: pointer to lpfc hba data structure
11197 * @pIocbIn: pointer to the rspiocbq
11198 * @pIocbOut: pointer to the cmdiocbq
11199 * @wcqe: pointer to the complete wcqe
11200 *
11201 * This routine transfers the fields of a command iocbq to a response iocbq
11202 * by copying all the IOCB fields from command iocbq and transferring the
11203 * completion status information from the complete wcqe.
11204 **/
James Smart4f774512009-05-22 14:52:35 -040011205static void
James Smart341af102010-01-26 23:07:37 -050011206lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11207 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011208 struct lpfc_iocbq *pIocbOut,
11209 struct lpfc_wcqe_complete *wcqe)
11210{
James Smartaf227412013-10-10 12:23:10 -040011211 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011212 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011213 uint32_t status, max_response;
11214 struct lpfc_dmabuf *dmabuf;
11215 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011216 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11217
11218 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11219 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011220 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011221 status = bf_get(lpfc_wcqe_c_status, wcqe);
11222 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011223 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11224 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11225 pIocbIn->iocb.un.fcpi.fcpi_parm =
11226 pIocbOut->iocb.un.fcpi.fcpi_parm -
11227 wcqe->total_data_placed;
11228 else
11229 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011230 else {
James Smart4f774512009-05-22 14:52:35 -040011231 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011232 switch (pIocbOut->iocb.ulpCommand) {
11233 case CMD_ELS_REQUEST64_CR:
11234 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11235 bpl = (struct ulp_bde64 *)dmabuf->virt;
11236 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11237 max_response = bde.tus.f.bdeSize;
11238 break;
11239 case CMD_GEN_REQUEST64_CR:
11240 max_response = 0;
11241 if (!pIocbOut->context3)
11242 break;
11243 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11244 sizeof(struct ulp_bde64);
11245 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11246 bpl = (struct ulp_bde64 *)dmabuf->virt;
11247 for (i = 0; i < numBdes; i++) {
11248 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11249 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11250 max_response += bde.tus.f.bdeSize;
11251 }
11252 break;
11253 default:
11254 max_response = wcqe->total_data_placed;
11255 break;
11256 }
11257 if (max_response < wcqe->total_data_placed)
11258 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11259 else
11260 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11261 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011262 }
James Smart341af102010-01-26 23:07:37 -050011263
James Smartacd68592012-01-18 16:25:09 -050011264 /* Convert BG errors for completion status */
11265 if (status == CQE_STATUS_DI_ERROR) {
11266 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11267
11268 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11269 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11270 else
11271 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11272
11273 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11274 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11275 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11276 BGS_GUARD_ERR_MASK;
11277 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11278 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11279 BGS_APPTAG_ERR_MASK;
11280 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11281 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11282 BGS_REFTAG_ERR_MASK;
11283
11284 /* Check to see if there was any good data before the error */
11285 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11286 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11287 BGS_HI_WATER_MARK_PRESENT_MASK;
11288 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11289 wcqe->total_data_placed;
11290 }
11291
11292 /*
11293 * Set ALL the error bits to indicate we don't know what
11294 * type of error it is.
11295 */
11296 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11297 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11298 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11299 BGS_GUARD_ERR_MASK);
11300 }
11301
James Smart341af102010-01-26 23:07:37 -050011302 /* Pick up HBA exchange busy condition */
11303 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11304 spin_lock_irqsave(&phba->hbalock, iflags);
11305 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11306 spin_unlock_irqrestore(&phba->hbalock, iflags);
11307 }
James Smart4f774512009-05-22 14:52:35 -040011308}
11309
11310/**
James Smart45ed1192009-10-02 15:17:02 -040011311 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11312 * @phba: Pointer to HBA context object.
11313 * @wcqe: Pointer to work-queue completion queue entry.
11314 *
11315 * This routine handles an ELS work-queue completion event and construct
11316 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11317 * discovery engine to handle.
11318 *
11319 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11320 **/
11321static struct lpfc_iocbq *
11322lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11323 struct lpfc_iocbq *irspiocbq)
11324{
11325 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11326 struct lpfc_iocbq *cmdiocbq;
11327 struct lpfc_wcqe_complete *wcqe;
11328 unsigned long iflags;
11329
11330 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011331 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011332 pring->stats.iocb_event++;
11333 /* Look up the ELS command IOCB and create pseudo response IOCB */
11334 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11335 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011336 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011337
11338 if (unlikely(!cmdiocbq)) {
11339 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11340 "0386 ELS complete with no corresponding "
11341 "cmdiocb: iotag (%d)\n",
11342 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11343 lpfc_sli_release_iocbq(phba, irspiocbq);
11344 return NULL;
11345 }
11346
11347 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011348 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011349
11350 return irspiocbq;
11351}
11352
11353/**
James Smart04c68492009-05-22 14:52:52 -040011354 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11355 * @phba: Pointer to HBA context object.
11356 * @cqe: Pointer to mailbox completion queue entry.
11357 *
11358 * This routine process a mailbox completion queue entry with asynchrous
11359 * event.
11360 *
11361 * Return: true if work posted to worker thread, otherwise false.
11362 **/
11363static bool
11364lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11365{
11366 struct lpfc_cq_event *cq_event;
11367 unsigned long iflags;
11368
11369 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11370 "0392 Async Event: word0:x%x, word1:x%x, "
11371 "word2:x%x, word3:x%x\n", mcqe->word0,
11372 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11373
11374 /* Allocate a new internal CQ_EVENT entry */
11375 cq_event = lpfc_sli4_cq_event_alloc(phba);
11376 if (!cq_event) {
11377 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11378 "0394 Failed to allocate CQ_EVENT entry\n");
11379 return false;
11380 }
11381
11382 /* Move the CQE into an asynchronous event entry */
11383 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11384 spin_lock_irqsave(&phba->hbalock, iflags);
11385 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11386 /* Set the async event flag */
11387 phba->hba_flag |= ASYNC_EVENT;
11388 spin_unlock_irqrestore(&phba->hbalock, iflags);
11389
11390 return true;
11391}
11392
11393/**
11394 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11395 * @phba: Pointer to HBA context object.
11396 * @cqe: Pointer to mailbox completion queue entry.
11397 *
11398 * This routine process a mailbox completion queue entry with mailbox
11399 * completion event.
11400 *
11401 * Return: true if work posted to worker thread, otherwise false.
11402 **/
11403static bool
11404lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11405{
11406 uint32_t mcqe_status;
11407 MAILBOX_t *mbox, *pmbox;
11408 struct lpfc_mqe *mqe;
11409 struct lpfc_vport *vport;
11410 struct lpfc_nodelist *ndlp;
11411 struct lpfc_dmabuf *mp;
11412 unsigned long iflags;
11413 LPFC_MBOXQ_t *pmb;
11414 bool workposted = false;
11415 int rc;
11416
11417 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11418 if (!bf_get(lpfc_trailer_completed, mcqe))
11419 goto out_no_mqe_complete;
11420
11421 /* Get the reference to the active mbox command */
11422 spin_lock_irqsave(&phba->hbalock, iflags);
11423 pmb = phba->sli.mbox_active;
11424 if (unlikely(!pmb)) {
11425 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11426 "1832 No pending MBOX command to handle\n");
11427 spin_unlock_irqrestore(&phba->hbalock, iflags);
11428 goto out_no_mqe_complete;
11429 }
11430 spin_unlock_irqrestore(&phba->hbalock, iflags);
11431 mqe = &pmb->u.mqe;
11432 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11433 mbox = phba->mbox;
11434 vport = pmb->vport;
11435
11436 /* Reset heartbeat timer */
11437 phba->last_completion_time = jiffies;
11438 del_timer(&phba->sli.mbox_tmo);
11439
11440 /* Move mbox data to caller's mailbox region, do endian swapping */
11441 if (pmb->mbox_cmpl && mbox)
11442 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011443
James Smart73d91e52011-10-10 21:32:10 -040011444 /*
11445 * For mcqe errors, conditionally move a modified error code to
11446 * the mbox so that the error will not be missed.
11447 */
11448 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11449 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11450 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11451 bf_set(lpfc_mqe_status, mqe,
11452 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11453 }
James Smart04c68492009-05-22 14:52:52 -040011454 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11455 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11456 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11457 "MBOX dflt rpi: status:x%x rpi:x%x",
11458 mcqe_status,
11459 pmbox->un.varWords[0], 0);
11460 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11461 mp = (struct lpfc_dmabuf *)(pmb->context1);
11462 ndlp = (struct lpfc_nodelist *)pmb->context2;
11463 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11464 * RID of the PPI using the same mbox buffer.
11465 */
11466 lpfc_unreg_login(phba, vport->vpi,
11467 pmbox->un.varWords[0], pmb);
11468 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11469 pmb->context1 = mp;
11470 pmb->context2 = ndlp;
11471 pmb->vport = vport;
11472 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11473 if (rc != MBX_BUSY)
11474 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11475 LOG_SLI, "0385 rc should "
11476 "have been MBX_BUSY\n");
11477 if (rc != MBX_NOT_FINISHED)
11478 goto send_current_mbox;
11479 }
11480 }
11481 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11482 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11483 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11484
11485 /* There is mailbox completion work to do */
11486 spin_lock_irqsave(&phba->hbalock, iflags);
11487 __lpfc_mbox_cmpl_put(phba, pmb);
11488 phba->work_ha |= HA_MBATT;
11489 spin_unlock_irqrestore(&phba->hbalock, iflags);
11490 workposted = true;
11491
11492send_current_mbox:
11493 spin_lock_irqsave(&phba->hbalock, iflags);
11494 /* Release the mailbox command posting token */
11495 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11496 /* Setting active mailbox pointer need to be in sync to flag clear */
11497 phba->sli.mbox_active = NULL;
11498 spin_unlock_irqrestore(&phba->hbalock, iflags);
11499 /* Wake up worker thread to post the next pending mailbox command */
11500 lpfc_worker_wake_up(phba);
11501out_no_mqe_complete:
11502 if (bf_get(lpfc_trailer_consumed, mcqe))
11503 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11504 return workposted;
11505}
11506
11507/**
11508 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11509 * @phba: Pointer to HBA context object.
11510 * @cqe: Pointer to mailbox completion queue entry.
11511 *
11512 * This routine process a mailbox completion queue entry, it invokes the
11513 * proper mailbox complete handling or asynchrous event handling routine
11514 * according to the MCQE's async bit.
11515 *
11516 * Return: true if work posted to worker thread, otherwise false.
11517 **/
11518static bool
11519lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11520{
11521 struct lpfc_mcqe mcqe;
11522 bool workposted;
11523
11524 /* Copy the mailbox MCQE and convert endian order as needed */
11525 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11526
11527 /* Invoke the proper event handling routine */
11528 if (!bf_get(lpfc_trailer_async, &mcqe))
11529 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11530 else
11531 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11532 return workposted;
11533}
11534
11535/**
James Smart4f774512009-05-22 14:52:35 -040011536 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11537 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011538 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011539 * @wcqe: Pointer to work-queue completion queue entry.
11540 *
11541 * This routine handles an ELS work-queue completion event.
11542 *
11543 * Return: true if work posted to worker thread, otherwise false.
11544 **/
11545static bool
James Smart2a76a282012-08-03 12:35:54 -040011546lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011547 struct lpfc_wcqe_complete *wcqe)
11548{
James Smart4f774512009-05-22 14:52:35 -040011549 struct lpfc_iocbq *irspiocbq;
11550 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011551 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011552 int txq_cnt = 0;
11553 int txcmplq_cnt = 0;
11554 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040011555
James Smart45ed1192009-10-02 15:17:02 -040011556 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011557 irspiocbq = lpfc_sli_get_iocbq(phba);
11558 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011559 if (!list_empty(&pring->txq))
11560 txq_cnt++;
11561 if (!list_empty(&pring->txcmplq))
11562 txcmplq_cnt++;
11563 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11564 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040011565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011566 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11567 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050011568 txq_cnt, phba->iocb_cnt,
11569 fcp_txcmplq_cnt,
11570 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011571 return false;
James Smart4f774512009-05-22 14:52:35 -040011572 }
James Smart4f774512009-05-22 14:52:35 -040011573
James Smart45ed1192009-10-02 15:17:02 -040011574 /* Save off the slow-path queue event for work thread to process */
11575 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011576 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011577 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011578 &phba->sli4_hba.sp_queue_event);
11579 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011580 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011581
James Smart45ed1192009-10-02 15:17:02 -040011582 return true;
James Smart4f774512009-05-22 14:52:35 -040011583}
11584
11585/**
11586 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11587 * @phba: Pointer to HBA context object.
11588 * @wcqe: Pointer to work-queue completion queue entry.
11589 *
11590 * This routine handles slow-path WQ entry comsumed event by invoking the
11591 * proper WQ release routine to the slow-path WQ.
11592 **/
11593static void
11594lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11595 struct lpfc_wcqe_release *wcqe)
11596{
James Smart2e90f4b2011-12-13 13:22:37 -050011597 /* sanity check on queue memory */
11598 if (unlikely(!phba->sli4_hba.els_wq))
11599 return;
James Smart4f774512009-05-22 14:52:35 -040011600 /* Check for the slow-path ELS work queue */
11601 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11602 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11603 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11604 else
11605 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11606 "2579 Slow-path wqe consume event carries "
11607 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11608 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11609 phba->sli4_hba.els_wq->queue_id);
11610}
11611
11612/**
11613 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11614 * @phba: Pointer to HBA context object.
11615 * @cq: Pointer to a WQ completion queue.
11616 * @wcqe: Pointer to work-queue completion queue entry.
11617 *
11618 * This routine handles an XRI abort event.
11619 *
11620 * Return: true if work posted to worker thread, otherwise false.
11621 **/
11622static bool
11623lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11624 struct lpfc_queue *cq,
11625 struct sli4_wcqe_xri_aborted *wcqe)
11626{
11627 bool workposted = false;
11628 struct lpfc_cq_event *cq_event;
11629 unsigned long iflags;
11630
11631 /* Allocate a new internal CQ_EVENT entry */
11632 cq_event = lpfc_sli4_cq_event_alloc(phba);
11633 if (!cq_event) {
11634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11635 "0602 Failed to allocate CQ_EVENT entry\n");
11636 return false;
11637 }
11638
11639 /* Move the CQE into the proper xri abort event list */
11640 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11641 switch (cq->subtype) {
11642 case LPFC_FCP:
11643 spin_lock_irqsave(&phba->hbalock, iflags);
11644 list_add_tail(&cq_event->list,
11645 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11646 /* Set the fcp xri abort event flag */
11647 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11648 spin_unlock_irqrestore(&phba->hbalock, iflags);
11649 workposted = true;
11650 break;
11651 case LPFC_ELS:
11652 spin_lock_irqsave(&phba->hbalock, iflags);
11653 list_add_tail(&cq_event->list,
11654 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11655 /* Set the els xri abort event flag */
11656 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11657 spin_unlock_irqrestore(&phba->hbalock, iflags);
11658 workposted = true;
11659 break;
11660 default:
11661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11662 "0603 Invalid work queue CQE subtype (x%x)\n",
11663 cq->subtype);
11664 workposted = false;
11665 break;
11666 }
11667 return workposted;
11668}
11669
11670/**
James Smart4d9ab992009-10-02 15:16:39 -040011671 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011672 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011673 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011674 *
James Smart4d9ab992009-10-02 15:16:39 -040011675 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011676 *
11677 * Return: true if work posted to worker thread, otherwise false.
11678 **/
11679static bool
James Smart4d9ab992009-10-02 15:16:39 -040011680lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11681{
11682 bool workposted = false;
11683 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11684 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11685 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011686 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011687 unsigned long iflags;
11688
James Smart2e90f4b2011-12-13 13:22:37 -050011689 /* sanity check on queue memory */
11690 if (unlikely(!hrq) || unlikely(!drq))
11691 return workposted;
11692
James Smart7851fe22011-07-22 18:36:52 -040011693 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11694 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11695 else
11696 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11697 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011698 goto out;
11699
11700 status = bf_get(lpfc_rcqe_status, rcqe);
11701 switch (status) {
11702 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11703 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11704 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011705 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011706 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011707 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011708 spin_lock_irqsave(&phba->hbalock, iflags);
11709 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11710 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011711 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011712 spin_unlock_irqrestore(&phba->hbalock, iflags);
11713 goto out;
11714 }
James Smartb84daac2012-08-03 12:35:13 -040011715 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011716 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11717 /* save off the frame for the word thread to process */
11718 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011719 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011720 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011721 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011722 spin_unlock_irqrestore(&phba->hbalock, iflags);
11723 workposted = true;
11724 break;
11725 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11726 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011727 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011728 /* Post more buffers if possible */
11729 spin_lock_irqsave(&phba->hbalock, iflags);
11730 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11731 spin_unlock_irqrestore(&phba->hbalock, iflags);
11732 workposted = true;
11733 break;
11734 }
11735out:
11736 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011737}
11738
11739/**
11740 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11741 * @phba: Pointer to HBA context object.
11742 * @cq: Pointer to the completion queue.
11743 * @wcqe: Pointer to a completion queue entry.
11744 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011745 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011746 * entry.
11747 *
11748 * Return: true if work posted to worker thread, otherwise false.
11749 **/
11750static bool
11751lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011752 struct lpfc_cqe *cqe)
11753{
James Smart45ed1192009-10-02 15:17:02 -040011754 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011755 bool workposted = false;
11756
11757 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011758 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011759
11760 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011761 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011762 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011763 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011764 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011765 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011766 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011767 break;
11768 case CQE_CODE_RELEASE_WQE:
11769 /* Process the WQ release event */
11770 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011771 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011772 break;
11773 case CQE_CODE_XRI_ABORTED:
11774 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011775 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011776 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011777 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011778 break;
James Smart4d9ab992009-10-02 15:16:39 -040011779 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011780 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011781 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011782 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011783 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011784 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011785 break;
James Smart4f774512009-05-22 14:52:35 -040011786 default:
11787 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11788 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011789 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011790 break;
11791 }
11792 return workposted;
11793}
11794
11795/**
James Smart4f774512009-05-22 14:52:35 -040011796 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11797 * @phba: Pointer to HBA context object.
11798 * @eqe: Pointer to fast-path event queue entry.
11799 *
11800 * This routine process a event queue entry from the slow-path event queue.
11801 * It will check the MajorCode and MinorCode to determine this is for a
11802 * completion event on a completion queue, if not, an error shall be logged
11803 * and just return. Otherwise, it will get to the corresponding completion
11804 * queue and process all the entries on that completion queue, rearm the
11805 * completion queue, and then return.
11806 *
11807 **/
11808static void
James Smart67d12732012-08-03 12:36:13 -040011809lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11810 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040011811{
James Smart67d12732012-08-03 12:36:13 -040011812 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040011813 struct lpfc_cqe *cqe;
11814 bool workposted = false;
11815 int ecount = 0;
11816 uint16_t cqid;
11817
James Smart4f774512009-05-22 14:52:35 -040011818 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011819 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011820
James Smart4f774512009-05-22 14:52:35 -040011821 list_for_each_entry(childq, &speq->child_list, list) {
11822 if (childq->queue_id == cqid) {
11823 cq = childq;
11824 break;
11825 }
11826 }
11827 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011828 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11830 "0365 Slow-path CQ identifier "
11831 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011832 return;
11833 }
11834
11835 /* Process all the entries to the CQ */
11836 switch (cq->type) {
11837 case LPFC_MCQ:
11838 while ((cqe = lpfc_sli4_cq_get(cq))) {
11839 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011840 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011841 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011842 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011843 }
11844 break;
11845 case LPFC_WCQ:
11846 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011847 if (cq->subtype == LPFC_FCP)
11848 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11849 cqe);
11850 else
11851 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11852 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011853 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011854 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11855 }
James Smartb84daac2012-08-03 12:35:13 -040011856
11857 /* Track the max number of CQEs processed in 1 EQ */
11858 if (ecount > cq->CQ_max_cqe)
11859 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011860 break;
11861 default:
11862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11863 "0370 Invalid completion queue type (%d)\n",
11864 cq->type);
11865 return;
11866 }
11867
11868 /* Catch the no cq entry condition, log an error */
11869 if (unlikely(ecount == 0))
11870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11871 "0371 No entry from the CQ: identifier "
11872 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11873
11874 /* In any case, flash and re-arm the RCQ */
11875 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11876
11877 /* wake up worker thread if there are works to be done */
11878 if (workposted)
11879 lpfc_worker_wake_up(phba);
11880}
11881
11882/**
11883 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011884 * @phba: Pointer to HBA context object.
11885 * @cq: Pointer to associated CQ
11886 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011887 *
11888 * This routine process a fast-path work queue completion entry from fast-path
11889 * event queue for FCP command response completion.
11890 **/
11891static void
James Smart2a76a282012-08-03 12:35:54 -040011892lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011893 struct lpfc_wcqe_complete *wcqe)
11894{
James Smart2a76a282012-08-03 12:35:54 -040011895 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011896 struct lpfc_iocbq *cmdiocbq;
11897 struct lpfc_iocbq irspiocbq;
11898 unsigned long iflags;
11899
James Smart4f774512009-05-22 14:52:35 -040011900 /* Check for response status */
11901 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11902 /* If resource errors reported from HBA, reduce queue
11903 * depth of the SCSI device.
11904 */
James Smarte3d2b802012-08-14 14:25:43 -040011905 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11906 IOSTAT_LOCAL_REJECT)) &&
11907 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11908 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040011909 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040011910
James Smart4f774512009-05-22 14:52:35 -040011911 /* Log the error status */
11912 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11913 "0373 FCP complete error: status=x%x, "
11914 "hw_status=x%x, total_data_specified=%d, "
11915 "parameter=x%x, word3=x%x\n",
11916 bf_get(lpfc_wcqe_c_status, wcqe),
11917 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11918 wcqe->total_data_placed, wcqe->parameter,
11919 wcqe->word3);
11920 }
11921
11922 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011923 spin_lock_irqsave(&pring->ring_lock, iflags);
11924 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011925 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11926 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011927 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011928 if (unlikely(!cmdiocbq)) {
11929 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11930 "0374 FCP complete with no corresponding "
11931 "cmdiocb: iotag (%d)\n",
11932 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11933 return;
11934 }
11935 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11936 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11937 "0375 FCP cmdiocb not callback function "
11938 "iotag: (%d)\n",
11939 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11940 return;
11941 }
11942
11943 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011944 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011945
James Smart0f65ff62010-02-26 14:14:23 -050011946 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11947 spin_lock_irqsave(&phba->hbalock, iflags);
11948 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11949 spin_unlock_irqrestore(&phba->hbalock, iflags);
11950 }
11951
James Smart4f774512009-05-22 14:52:35 -040011952 /* Pass the cmd_iocb and the rsp state to the upper layer */
11953 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11954}
11955
11956/**
11957 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11958 * @phba: Pointer to HBA context object.
11959 * @cq: Pointer to completion queue.
11960 * @wcqe: Pointer to work-queue completion queue entry.
11961 *
11962 * This routine handles an fast-path WQ entry comsumed event by invoking the
11963 * proper WQ release routine to the slow-path WQ.
11964 **/
11965static void
11966lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11967 struct lpfc_wcqe_release *wcqe)
11968{
11969 struct lpfc_queue *childwq;
11970 bool wqid_matched = false;
11971 uint16_t fcp_wqid;
11972
11973 /* Check for fast-path FCP work queue release */
11974 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11975 list_for_each_entry(childwq, &cq->child_list, list) {
11976 if (childwq->queue_id == fcp_wqid) {
11977 lpfc_sli4_wq_release(childwq,
11978 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11979 wqid_matched = true;
11980 break;
11981 }
11982 }
11983 /* Report warning log message if no match found */
11984 if (wqid_matched != true)
11985 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11986 "2580 Fast-path wqe consume event carries "
11987 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11988}
11989
11990/**
11991 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11992 * @cq: Pointer to the completion queue.
11993 * @eqe: Pointer to fast-path completion queue entry.
11994 *
11995 * This routine process a fast-path work queue completion entry from fast-path
11996 * event queue for FCP command response completion.
11997 **/
11998static int
11999lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12000 struct lpfc_cqe *cqe)
12001{
12002 struct lpfc_wcqe_release wcqe;
12003 bool workposted = false;
12004
12005 /* Copy the work queue CQE and convert endian order if needed */
12006 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12007
12008 /* Check and process for different type of WCQE and dispatch */
12009 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12010 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012011 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012012 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012013 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012014 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012015 (struct lpfc_wcqe_complete *)&wcqe);
12016 break;
12017 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012018 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012019 /* Process the WQ release event */
12020 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12021 (struct lpfc_wcqe_release *)&wcqe);
12022 break;
12023 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012024 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012025 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012026 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012027 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12028 (struct sli4_wcqe_xri_aborted *)&wcqe);
12029 break;
12030 default:
12031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12032 "0144 Not a valid WCQE code: x%x\n",
12033 bf_get(lpfc_wcqe_c_code, &wcqe));
12034 break;
12035 }
12036 return workposted;
12037}
12038
12039/**
James Smart67d12732012-08-03 12:36:13 -040012040 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012041 * @phba: Pointer to HBA context object.
12042 * @eqe: Pointer to fast-path event queue entry.
12043 *
12044 * This routine process a event queue entry from the fast-path event queue.
12045 * It will check the MajorCode and MinorCode to determine this is for a
12046 * completion event on a completion queue, if not, an error shall be logged
12047 * and just return. Otherwise, it will get to the corresponding completion
12048 * queue and process all the entries on the completion queue, rearm the
12049 * completion queue, and then return.
12050 **/
12051static void
James Smart67d12732012-08-03 12:36:13 -040012052lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12053 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012054{
12055 struct lpfc_queue *cq;
12056 struct lpfc_cqe *cqe;
12057 bool workposted = false;
12058 uint16_t cqid;
12059 int ecount = 0;
12060
James Smartcb5172e2010-03-15 11:25:07 -040012061 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012062 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012063 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012064 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012065 bf_get_le32(lpfc_eqe_major_code, eqe),
12066 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012067 return;
12068 }
12069
James Smart67d12732012-08-03 12:36:13 -040012070 /* Get the reference to the corresponding CQ */
12071 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12072
12073 /* Check if this is a Slow path event */
12074 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12075 lpfc_sli4_sp_handle_eqe(phba, eqe,
12076 phba->sli4_hba.hba_eq[qidx]);
12077 return;
12078 }
12079
James Smart2e90f4b2011-12-13 13:22:37 -050012080 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12081 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12082 "3146 Fast-path completion queues "
12083 "does not exist\n");
12084 return;
12085 }
James Smart67d12732012-08-03 12:36:13 -040012086 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012087 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012088 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12089 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12090 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012091 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012092 return;
12093 }
12094
James Smart4f774512009-05-22 14:52:35 -040012095 if (unlikely(cqid != cq->queue_id)) {
12096 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12097 "0368 Miss-matched fast-path completion "
12098 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12099 cqid, cq->queue_id);
12100 return;
12101 }
12102
12103 /* Process all the entries to the CQ */
12104 while ((cqe = lpfc_sli4_cq_get(cq))) {
12105 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012106 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012107 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12108 }
12109
James Smartb84daac2012-08-03 12:35:13 -040012110 /* Track the max number of CQEs processed in 1 EQ */
12111 if (ecount > cq->CQ_max_cqe)
12112 cq->CQ_max_cqe = ecount;
12113
James Smart4f774512009-05-22 14:52:35 -040012114 /* Catch the no cq entry condition */
12115 if (unlikely(ecount == 0))
12116 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12117 "0369 No entry from fast-path completion "
12118 "queue fcpcqid=%d\n", cq->queue_id);
12119
12120 /* In any case, flash and re-arm the CQ */
12121 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12122
12123 /* wake up worker thread if there are works to be done */
12124 if (workposted)
12125 lpfc_worker_wake_up(phba);
12126}
12127
12128static void
12129lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12130{
12131 struct lpfc_eqe *eqe;
12132
12133 /* walk all the EQ entries and drop on the floor */
12134 while ((eqe = lpfc_sli4_eq_get(eq)))
12135 ;
12136
12137 /* Clear and re-arm the EQ */
12138 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12139}
12140
12141/**
James Smart67d12732012-08-03 12:36:13 -040012142 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012143 * @irq: Interrupt number.
12144 * @dev_id: The device context pointer.
12145 *
12146 * This function is directly called from the PCI layer as an interrupt
12147 * service routine when device with SLI-4 interface spec is enabled with
12148 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12149 * ring event in the HBA. However, when the device is enabled with either
12150 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12151 * device-level interrupt handler. When the PCI slot is in error recovery
12152 * or the HBA is undergoing initialization, the interrupt handler will not
12153 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12154 * the intrrupt context. This function is called without any lock held.
12155 * It gets the hbalock to access and update SLI data structures. Note that,
12156 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12157 * equal to that of FCP CQ index.
12158 *
James Smart67d12732012-08-03 12:36:13 -040012159 * The link attention and ELS ring attention events are handled
12160 * by the worker thread. The interrupt handler signals the worker thread
12161 * and returns for these events. This function is called without any lock
12162 * held. It gets the hbalock to access and update SLI data structures.
12163 *
James Smart4f774512009-05-22 14:52:35 -040012164 * This function returns IRQ_HANDLED when interrupt is handled else it
12165 * returns IRQ_NONE.
12166 **/
12167irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012168lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012169{
12170 struct lpfc_hba *phba;
12171 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12172 struct lpfc_queue *fpeq;
12173 struct lpfc_eqe *eqe;
12174 unsigned long iflag;
12175 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012176 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012177
12178 /* Get the driver's phba structure from the dev_id */
12179 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12180 phba = fcp_eq_hdl->phba;
12181 fcp_eqidx = fcp_eq_hdl->idx;
12182
12183 if (unlikely(!phba))
12184 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012185 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012186 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012187
12188 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012189 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012190 if (unlikely(!fpeq))
12191 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012192
James Smartba20c852012-08-03 12:36:52 -040012193 if (lpfc_fcp_look_ahead) {
12194 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12195 lpfc_sli4_eq_clr_intr(fpeq);
12196 else {
12197 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12198 return IRQ_NONE;
12199 }
12200 }
12201
James Smart4f774512009-05-22 14:52:35 -040012202 /* Check device state for handling interrupt */
12203 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012204 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012205 /* Check again for link_state with lock held */
12206 spin_lock_irqsave(&phba->hbalock, iflag);
12207 if (phba->link_state < LPFC_LINK_DOWN)
12208 /* Flush, clear interrupt, and rearm the EQ */
12209 lpfc_sli4_eq_flush(phba, fpeq);
12210 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012211 if (lpfc_fcp_look_ahead)
12212 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012213 return IRQ_NONE;
12214 }
12215
12216 /*
12217 * Process all the event on FCP fast-path EQ
12218 */
12219 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smart67d12732012-08-03 12:36:13 -040012220 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012221 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012222 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012223 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012224 }
12225
James Smartb84daac2012-08-03 12:35:13 -040012226 /* Track the max number of EQEs processed in 1 intr */
12227 if (ecount > fpeq->EQ_max_eqe)
12228 fpeq->EQ_max_eqe = ecount;
12229
James Smart4f774512009-05-22 14:52:35 -040012230 /* Always clear and re-arm the fast-path EQ */
12231 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12232
12233 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012234 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012235
12236 if (lpfc_fcp_look_ahead) {
12237 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12238 return IRQ_NONE;
12239 }
12240
James Smart4f774512009-05-22 14:52:35 -040012241 if (phba->intr_type == MSIX)
12242 /* MSI-X treated interrupt served as no EQ share INT */
12243 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12244 "0358 MSI-X interrupt with no EQE\n");
12245 else
12246 /* Non MSI-X treated on interrupt as EQ share INT */
12247 return IRQ_NONE;
12248 }
12249
James Smartba20c852012-08-03 12:36:52 -040012250 if (lpfc_fcp_look_ahead)
12251 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012252 return IRQ_HANDLED;
12253} /* lpfc_sli4_fp_intr_handler */
12254
12255/**
12256 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12257 * @irq: Interrupt number.
12258 * @dev_id: The device context pointer.
12259 *
12260 * This function is the device-level interrupt handler to device with SLI-4
12261 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12262 * interrupt mode is enabled and there is an event in the HBA which requires
12263 * driver attention. This function invokes the slow-path interrupt attention
12264 * handling function and fast-path interrupt attention handling function in
12265 * turn to process the relevant HBA attention events. This function is called
12266 * without any lock held. It gets the hbalock to access and update SLI data
12267 * structures.
12268 *
12269 * This function returns IRQ_HANDLED when interrupt is handled, else it
12270 * returns IRQ_NONE.
12271 **/
12272irqreturn_t
12273lpfc_sli4_intr_handler(int irq, void *dev_id)
12274{
12275 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012276 irqreturn_t hba_irq_rc;
12277 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012278 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012279
12280 /* Get the driver's phba structure from the dev_id */
12281 phba = (struct lpfc_hba *)dev_id;
12282
12283 if (unlikely(!phba))
12284 return IRQ_NONE;
12285
12286 /*
James Smart4f774512009-05-22 14:52:35 -040012287 * Invoke fast-path host attention interrupt handling as appropriate.
12288 */
James Smart67d12732012-08-03 12:36:13 -040012289 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12290 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012291 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012292 if (hba_irq_rc == IRQ_HANDLED)
12293 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012294 }
12295
James Smart67d12732012-08-03 12:36:13 -040012296 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012297} /* lpfc_sli4_intr_handler */
12298
12299/**
12300 * lpfc_sli4_queue_free - free a queue structure and associated memory
12301 * @queue: The queue structure to free.
12302 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012303 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012304 * the host resident queue. This function must be called after destroying the
12305 * queue on the HBA.
12306 **/
12307void
12308lpfc_sli4_queue_free(struct lpfc_queue *queue)
12309{
12310 struct lpfc_dmabuf *dmabuf;
12311
12312 if (!queue)
12313 return;
12314
12315 while (!list_empty(&queue->page_list)) {
12316 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12317 list);
James Smart49198b32010-04-06 15:04:33 -040012318 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012319 dmabuf->virt, dmabuf->phys);
12320 kfree(dmabuf);
12321 }
12322 kfree(queue);
12323 return;
12324}
12325
12326/**
12327 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12328 * @phba: The HBA that this queue is being created on.
12329 * @entry_size: The size of each queue entry for this queue.
12330 * @entry count: The number of entries that this queue will handle.
12331 *
12332 * This function allocates a queue structure and the DMAable memory used for
12333 * the host resident queue. This function must be called before creating the
12334 * queue on the HBA.
12335 **/
12336struct lpfc_queue *
12337lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12338 uint32_t entry_count)
12339{
12340 struct lpfc_queue *queue;
12341 struct lpfc_dmabuf *dmabuf;
12342 int x, total_qe_count;
12343 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012344 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012345
James Smartcb5172e2010-03-15 11:25:07 -040012346 if (!phba->sli4_hba.pc_sli4_params.supported)
12347 hw_page_size = SLI4_PAGE_SIZE;
12348
James Smart4f774512009-05-22 14:52:35 -040012349 queue = kzalloc(sizeof(struct lpfc_queue) +
12350 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12351 if (!queue)
12352 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012353 queue->page_count = (ALIGN(entry_size * entry_count,
12354 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012355 INIT_LIST_HEAD(&queue->list);
12356 INIT_LIST_HEAD(&queue->page_list);
12357 INIT_LIST_HEAD(&queue->child_list);
12358 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12359 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12360 if (!dmabuf)
12361 goto out_fail;
12362 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012363 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012364 GFP_KERNEL);
12365 if (!dmabuf->virt) {
12366 kfree(dmabuf);
12367 goto out_fail;
12368 }
James Smartcb5172e2010-03-15 11:25:07 -040012369 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012370 dmabuf->buffer_tag = x;
12371 list_add_tail(&dmabuf->list, &queue->page_list);
12372 /* initialize queue's entry array */
12373 dma_pointer = dmabuf->virt;
12374 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012375 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012376 total_qe_count++, dma_pointer += entry_size) {
12377 queue->qe[total_qe_count].address = dma_pointer;
12378 }
12379 }
12380 queue->entry_size = entry_size;
12381 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012382
12383 /*
12384 * entry_repost is calculated based on the number of entries in the
12385 * queue. This works out except for RQs. If buffers are NOT initially
12386 * posted for every RQE, entry_repost should be adjusted accordingly.
12387 */
12388 queue->entry_repost = (entry_count >> 3);
12389 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12390 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012391 queue->phba = phba;
12392
12393 return queue;
12394out_fail:
12395 lpfc_sli4_queue_free(queue);
12396 return NULL;
12397}
12398
12399/**
James Smart962bc512013-01-03 15:44:00 -050012400 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12401 * @phba: HBA structure that indicates port to create a queue on.
12402 * @pci_barset: PCI BAR set flag.
12403 *
12404 * This function shall perform iomap of the specified PCI BAR address to host
12405 * memory address if not already done so and return it. The returned host
12406 * memory address can be NULL.
12407 */
12408static void __iomem *
12409lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12410{
12411 struct pci_dev *pdev;
James Smart962bc512013-01-03 15:44:00 -050012412
12413 if (!phba->pcidev)
12414 return NULL;
12415 else
12416 pdev = phba->pcidev;
12417
12418 switch (pci_barset) {
12419 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050012420 return phba->pci_bar0_memmap_p;
12421 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050012422 return phba->pci_bar2_memmap_p;
12423 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050012424 return phba->pci_bar4_memmap_p;
12425 default:
12426 break;
12427 }
12428 return NULL;
12429}
12430
12431/**
James Smart173edbb2012-06-12 13:54:50 -040012432 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12433 * @phba: HBA structure that indicates port to create a queue on.
12434 * @startq: The starting FCP EQ to modify
12435 *
12436 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12437 *
12438 * The @phba struct is used to send mailbox command to HBA. The @startq
12439 * is used to get the starting FCP EQ to change.
12440 * This function is asynchronous and will wait for the mailbox
12441 * command to finish before continuing.
12442 *
12443 * On success this function will return a zero. If unable to allocate enough
12444 * memory this function will return -ENOMEM. If the queue create mailbox command
12445 * fails this function will return -ENXIO.
12446 **/
12447uint32_t
12448lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12449{
12450 struct lpfc_mbx_modify_eq_delay *eq_delay;
12451 LPFC_MBOXQ_t *mbox;
12452 struct lpfc_queue *eq;
12453 int cnt, rc, length, status = 0;
12454 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040012455 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040012456 int fcp_eqidx;
12457 union lpfc_sli4_cfg_shdr *shdr;
12458 uint16_t dmult;
12459
James Smart67d12732012-08-03 12:36:13 -040012460 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040012461 return 0;
12462
12463 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12464 if (!mbox)
12465 return -ENOMEM;
12466 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12467 sizeof(struct lpfc_sli4_cfg_mhdr));
12468 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12469 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12470 length, LPFC_SLI4_MBX_EMBED);
12471 eq_delay = &mbox->u.mqe.un.eq_delay;
12472
12473 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012474 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12475 if (result > LPFC_DMULT_CONST)
12476 dmult = 0;
12477 else
12478 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040012479
12480 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040012481 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040012482 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040012483 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040012484 if (!eq)
12485 continue;
12486 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12487 eq_delay->u.request.eq[cnt].phase = 0;
12488 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12489 cnt++;
12490 if (cnt >= LPFC_MAX_EQ_DELAY)
12491 break;
12492 }
12493 eq_delay->u.request.num_eq = cnt;
12494
12495 mbox->vport = phba->pport;
12496 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12497 mbox->context1 = NULL;
12498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12499 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12500 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12501 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12502 if (shdr_status || shdr_add_status || rc) {
12503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12504 "2512 MODIFY_EQ_DELAY mailbox failed with "
12505 "status x%x add_status x%x, mbx status x%x\n",
12506 shdr_status, shdr_add_status, rc);
12507 status = -ENXIO;
12508 }
12509 mempool_free(mbox, phba->mbox_mem_pool);
12510 return status;
12511}
12512
12513/**
James Smart4f774512009-05-22 14:52:35 -040012514 * lpfc_eq_create - Create an Event Queue on the HBA
12515 * @phba: HBA structure that indicates port to create a queue on.
12516 * @eq: The queue structure to use to create the event queue.
12517 * @imax: The maximum interrupt per second limit.
12518 *
12519 * This function creates an event queue, as detailed in @eq, on a port,
12520 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12521 *
12522 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12523 * is used to get the entry count and entry size that are necessary to
12524 * determine the number of pages to allocate and use for this queue. This
12525 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12526 * event queue. This function is asynchronous and will wait for the mailbox
12527 * command to finish before continuing.
12528 *
12529 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012530 * memory this function will return -ENOMEM. If the queue create mailbox command
12531 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012532 **/
12533uint32_t
James Smartee020062012-09-29 11:28:52 -040012534lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040012535{
12536 struct lpfc_mbx_eq_create *eq_create;
12537 LPFC_MBOXQ_t *mbox;
12538 int rc, length, status = 0;
12539 struct lpfc_dmabuf *dmabuf;
12540 uint32_t shdr_status, shdr_add_status;
12541 union lpfc_sli4_cfg_shdr *shdr;
12542 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012543 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12544
James Smart2e90f4b2011-12-13 13:22:37 -050012545 /* sanity check on queue memory */
12546 if (!eq)
12547 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012548 if (!phba->sli4_hba.pc_sli4_params.supported)
12549 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012550
12551 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12552 if (!mbox)
12553 return -ENOMEM;
12554 length = (sizeof(struct lpfc_mbx_eq_create) -
12555 sizeof(struct lpfc_sli4_cfg_mhdr));
12556 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12557 LPFC_MBOX_OPCODE_EQ_CREATE,
12558 length, LPFC_SLI4_MBX_EMBED);
12559 eq_create = &mbox->u.mqe.un.eq_create;
12560 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12561 eq->page_count);
12562 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12563 LPFC_EQE_SIZE);
12564 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12565 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012566 if (imax > LPFC_DMULT_CONST)
12567 dmult = 0;
12568 else
12569 dmult = LPFC_DMULT_CONST/imax - 1;
James Smart4f774512009-05-22 14:52:35 -040012570 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12571 dmult);
12572 switch (eq->entry_count) {
12573 default:
12574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12575 "0360 Unsupported EQ count. (%d)\n",
12576 eq->entry_count);
12577 if (eq->entry_count < 256)
12578 return -EINVAL;
12579 /* otherwise default to smallest count (drop through) */
12580 case 256:
12581 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12582 LPFC_EQ_CNT_256);
12583 break;
12584 case 512:
12585 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12586 LPFC_EQ_CNT_512);
12587 break;
12588 case 1024:
12589 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12590 LPFC_EQ_CNT_1024);
12591 break;
12592 case 2048:
12593 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12594 LPFC_EQ_CNT_2048);
12595 break;
12596 case 4096:
12597 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12598 LPFC_EQ_CNT_4096);
12599 break;
12600 }
12601 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012602 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012603 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12604 putPaddrLow(dmabuf->phys);
12605 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12606 putPaddrHigh(dmabuf->phys);
12607 }
12608 mbox->vport = phba->pport;
12609 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12610 mbox->context1 = NULL;
12611 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12612 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12613 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12614 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12615 if (shdr_status || shdr_add_status || rc) {
12616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12617 "2500 EQ_CREATE mailbox failed with "
12618 "status x%x add_status x%x, mbx status x%x\n",
12619 shdr_status, shdr_add_status, rc);
12620 status = -ENXIO;
12621 }
12622 eq->type = LPFC_EQ;
12623 eq->subtype = LPFC_NONE;
12624 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12625 if (eq->queue_id == 0xFFFF)
12626 status = -ENXIO;
12627 eq->host_index = 0;
12628 eq->hba_index = 0;
12629
James Smart8fa38512009-07-19 10:01:03 -040012630 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012631 return status;
12632}
12633
12634/**
12635 * lpfc_cq_create - Create a Completion Queue on the HBA
12636 * @phba: HBA structure that indicates port to create a queue on.
12637 * @cq: The queue structure to use to create the completion queue.
12638 * @eq: The event queue to bind this completion queue to.
12639 *
12640 * This function creates a completion queue, as detailed in @wq, on a port,
12641 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12642 *
12643 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12644 * is used to get the entry count and entry size that are necessary to
12645 * determine the number of pages to allocate and use for this queue. The @eq
12646 * is used to indicate which event queue to bind this completion queue to. This
12647 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12648 * completion queue. This function is asynchronous and will wait for the mailbox
12649 * command to finish before continuing.
12650 *
12651 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012652 * memory this function will return -ENOMEM. If the queue create mailbox command
12653 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012654 **/
12655uint32_t
12656lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12657 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12658{
12659 struct lpfc_mbx_cq_create *cq_create;
12660 struct lpfc_dmabuf *dmabuf;
12661 LPFC_MBOXQ_t *mbox;
12662 int rc, length, status = 0;
12663 uint32_t shdr_status, shdr_add_status;
12664 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012665 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12666
James Smart2e90f4b2011-12-13 13:22:37 -050012667 /* sanity check on queue memory */
12668 if (!cq || !eq)
12669 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012670 if (!phba->sli4_hba.pc_sli4_params.supported)
12671 hw_page_size = SLI4_PAGE_SIZE;
12672
James Smart4f774512009-05-22 14:52:35 -040012673 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12674 if (!mbox)
12675 return -ENOMEM;
12676 length = (sizeof(struct lpfc_mbx_cq_create) -
12677 sizeof(struct lpfc_sli4_cfg_mhdr));
12678 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12679 LPFC_MBOX_OPCODE_CQ_CREATE,
12680 length, LPFC_SLI4_MBX_EMBED);
12681 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012682 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012683 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12684 cq->page_count);
12685 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12686 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012687 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12688 phba->sli4_hba.pc_sli4_params.cqv);
12689 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012690 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12691 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012692 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12693 eq->queue_id);
12694 } else {
12695 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12696 eq->queue_id);
12697 }
James Smart4f774512009-05-22 14:52:35 -040012698 switch (cq->entry_count) {
12699 default:
12700 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12701 "0361 Unsupported CQ count. (%d)\n",
12702 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012703 if (cq->entry_count < 256) {
12704 status = -EINVAL;
12705 goto out;
12706 }
James Smart4f774512009-05-22 14:52:35 -040012707 /* otherwise default to smallest count (drop through) */
12708 case 256:
12709 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12710 LPFC_CQ_CNT_256);
12711 break;
12712 case 512:
12713 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12714 LPFC_CQ_CNT_512);
12715 break;
12716 case 1024:
12717 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12718 LPFC_CQ_CNT_1024);
12719 break;
12720 }
12721 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012722 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012723 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12724 putPaddrLow(dmabuf->phys);
12725 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12726 putPaddrHigh(dmabuf->phys);
12727 }
12728 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12729
12730 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012731 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12732 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12733 if (shdr_status || shdr_add_status || rc) {
12734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12735 "2501 CQ_CREATE mailbox failed with "
12736 "status x%x add_status x%x, mbx status x%x\n",
12737 shdr_status, shdr_add_status, rc);
12738 status = -ENXIO;
12739 goto out;
12740 }
12741 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12742 if (cq->queue_id == 0xFFFF) {
12743 status = -ENXIO;
12744 goto out;
12745 }
12746 /* link the cq onto the parent eq child list */
12747 list_add_tail(&cq->list, &eq->child_list);
12748 /* Set up completion queue's type and subtype */
12749 cq->type = type;
12750 cq->subtype = subtype;
12751 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012752 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012753 cq->host_index = 0;
12754 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012755
James Smart8fa38512009-07-19 10:01:03 -040012756out:
12757 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012758 return status;
12759}
12760
12761/**
James Smartb19a0612010-04-06 14:48:51 -040012762 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012763 * @phba: HBA structure that indicates port to create a queue on.
12764 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012765 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12766 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012767 *
James Smartb19a0612010-04-06 14:48:51 -040012768 * This function provides failback (fb) functionality when the
12769 * mq_create_ext fails on older FW generations. It's purpose is identical
12770 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012771 *
James Smartb19a0612010-04-06 14:48:51 -040012772 * This routine cannot fail as all attributes were previously accessed and
12773 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012774 **/
James Smartb19a0612010-04-06 14:48:51 -040012775static void
12776lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12777 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012778{
12779 struct lpfc_mbx_mq_create *mq_create;
12780 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012781 int length;
James Smart04c68492009-05-22 14:52:52 -040012782
James Smart04c68492009-05-22 14:52:52 -040012783 length = (sizeof(struct lpfc_mbx_mq_create) -
12784 sizeof(struct lpfc_sli4_cfg_mhdr));
12785 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12786 LPFC_MBOX_OPCODE_MQ_CREATE,
12787 length, LPFC_SLI4_MBX_EMBED);
12788 mq_create = &mbox->u.mqe.un.mq_create;
12789 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012790 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012791 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012792 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012793 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12794 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012795 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012796 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12797 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012798 break;
12799 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012800 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12801 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012802 break;
12803 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012804 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12805 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012806 break;
12807 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012808 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12809 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012810 break;
12811 }
12812 list_for_each_entry(dmabuf, &mq->page_list, list) {
12813 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012814 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012815 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012816 putPaddrHigh(dmabuf->phys);
12817 }
12818}
12819
12820/**
12821 * lpfc_mq_create - Create a mailbox Queue on the HBA
12822 * @phba: HBA structure that indicates port to create a queue on.
12823 * @mq: The queue structure to use to create the mailbox queue.
12824 * @cq: The completion queue to associate with this cq.
12825 * @subtype: The queue's subtype.
12826 *
12827 * This function creates a mailbox queue, as detailed in @mq, on a port,
12828 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12829 *
12830 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12831 * is used to get the entry count and entry size that are necessary to
12832 * determine the number of pages to allocate and use for this queue. This
12833 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12834 * mailbox queue. This function is asynchronous and will wait for the mailbox
12835 * command to finish before continuing.
12836 *
12837 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012838 * memory this function will return -ENOMEM. If the queue create mailbox command
12839 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012840 **/
12841int32_t
12842lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12843 struct lpfc_queue *cq, uint32_t subtype)
12844{
12845 struct lpfc_mbx_mq_create *mq_create;
12846 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12847 struct lpfc_dmabuf *dmabuf;
12848 LPFC_MBOXQ_t *mbox;
12849 int rc, length, status = 0;
12850 uint32_t shdr_status, shdr_add_status;
12851 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012852 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012853
James Smart2e90f4b2011-12-13 13:22:37 -050012854 /* sanity check on queue memory */
12855 if (!mq || !cq)
12856 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012857 if (!phba->sli4_hba.pc_sli4_params.supported)
12858 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012859
12860 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12861 if (!mbox)
12862 return -ENOMEM;
12863 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12864 sizeof(struct lpfc_sli4_cfg_mhdr));
12865 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12866 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12867 length, LPFC_SLI4_MBX_EMBED);
12868
12869 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012870 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012871 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12872 &mq_create_ext->u.request, mq->page_count);
12873 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12874 &mq_create_ext->u.request, 1);
12875 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012876 &mq_create_ext->u.request, 1);
12877 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12878 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012879 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12880 &mq_create_ext->u.request, 1);
12881 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12882 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012883 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012884 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12885 phba->sli4_hba.pc_sli4_params.mqv);
12886 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12887 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12888 cq->queue_id);
12889 else
12890 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12891 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012892 switch (mq->entry_count) {
12893 default:
12894 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12895 "0362 Unsupported MQ count. (%d)\n",
12896 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012897 if (mq->entry_count < 16) {
12898 status = -EINVAL;
12899 goto out;
12900 }
James Smartb19a0612010-04-06 14:48:51 -040012901 /* otherwise default to smallest count (drop through) */
12902 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012903 bf_set(lpfc_mq_context_ring_size,
12904 &mq_create_ext->u.request.context,
12905 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012906 break;
12907 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012908 bf_set(lpfc_mq_context_ring_size,
12909 &mq_create_ext->u.request.context,
12910 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012911 break;
12912 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012913 bf_set(lpfc_mq_context_ring_size,
12914 &mq_create_ext->u.request.context,
12915 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012916 break;
12917 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012918 bf_set(lpfc_mq_context_ring_size,
12919 &mq_create_ext->u.request.context,
12920 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012921 break;
12922 }
12923 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012924 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012925 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12926 putPaddrLow(dmabuf->phys);
12927 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012928 putPaddrHigh(dmabuf->phys);
12929 }
12930 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012931 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12932 &mq_create_ext->u.response);
12933 if (rc != MBX_SUCCESS) {
12934 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12935 "2795 MQ_CREATE_EXT failed with "
12936 "status x%x. Failback to MQ_CREATE.\n",
12937 rc);
12938 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12939 mq_create = &mbox->u.mqe.un.mq_create;
12940 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12941 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12942 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12943 &mq_create->u.response);
12944 }
12945
James Smart04c68492009-05-22 14:52:52 -040012946 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012947 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12948 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12949 if (shdr_status || shdr_add_status || rc) {
12950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12951 "2502 MQ_CREATE mailbox failed with "
12952 "status x%x add_status x%x, mbx status x%x\n",
12953 shdr_status, shdr_add_status, rc);
12954 status = -ENXIO;
12955 goto out;
12956 }
James Smart04c68492009-05-22 14:52:52 -040012957 if (mq->queue_id == 0xFFFF) {
12958 status = -ENXIO;
12959 goto out;
12960 }
12961 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012962 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012963 mq->subtype = subtype;
12964 mq->host_index = 0;
12965 mq->hba_index = 0;
12966
12967 /* link the mq onto the parent cq child list */
12968 list_add_tail(&mq->list, &cq->child_list);
12969out:
James Smart8fa38512009-07-19 10:01:03 -040012970 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012971 return status;
12972}
12973
12974/**
James Smart4f774512009-05-22 14:52:35 -040012975 * lpfc_wq_create - Create a Work Queue on the HBA
12976 * @phba: HBA structure that indicates port to create a queue on.
12977 * @wq: The queue structure to use to create the work queue.
12978 * @cq: The completion queue to bind this work queue to.
12979 * @subtype: The subtype of the work queue indicating its functionality.
12980 *
12981 * This function creates a work queue, as detailed in @wq, on a port, described
12982 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12983 *
12984 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12985 * is used to get the entry count and entry size that are necessary to
12986 * determine the number of pages to allocate and use for this queue. The @cq
12987 * is used to indicate which completion queue to bind this work queue to. This
12988 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12989 * work queue. This function is asynchronous and will wait for the mailbox
12990 * command to finish before continuing.
12991 *
12992 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012993 * memory this function will return -ENOMEM. If the queue create mailbox command
12994 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012995 **/
12996uint32_t
12997lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12998 struct lpfc_queue *cq, uint32_t subtype)
12999{
13000 struct lpfc_mbx_wq_create *wq_create;
13001 struct lpfc_dmabuf *dmabuf;
13002 LPFC_MBOXQ_t *mbox;
13003 int rc, length, status = 0;
13004 uint32_t shdr_status, shdr_add_status;
13005 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013006 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013007 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013008 void __iomem *bar_memmap_p;
13009 uint32_t db_offset;
13010 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013011
James Smart2e90f4b2011-12-13 13:22:37 -050013012 /* sanity check on queue memory */
13013 if (!wq || !cq)
13014 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013015 if (!phba->sli4_hba.pc_sli4_params.supported)
13016 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013017
13018 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13019 if (!mbox)
13020 return -ENOMEM;
13021 length = (sizeof(struct lpfc_mbx_wq_create) -
13022 sizeof(struct lpfc_sli4_cfg_mhdr));
13023 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13024 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13025 length, LPFC_SLI4_MBX_EMBED);
13026 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013027 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013028 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13029 wq->page_count);
13030 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13031 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013032
13033 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013034 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13035 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013036
James Smart0c651872013-07-15 18:33:23 -040013037 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13038 case LPFC_Q_CREATE_VERSION_0:
13039 switch (wq->entry_size) {
13040 default:
13041 case 64:
13042 /* Nothing to do, version 0 ONLY supports 64 byte */
13043 page = wq_create->u.request.page;
13044 break;
13045 case 128:
13046 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13047 LPFC_WQ_SZ128_SUPPORT)) {
13048 status = -ERANGE;
13049 goto out;
13050 }
13051 /* If we get here the HBA MUST also support V1 and
13052 * we MUST use it
13053 */
13054 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13055 LPFC_Q_CREATE_VERSION_1);
13056
13057 bf_set(lpfc_mbx_wq_create_wqe_count,
13058 &wq_create->u.request_1, wq->entry_count);
13059 bf_set(lpfc_mbx_wq_create_wqe_size,
13060 &wq_create->u.request_1,
13061 LPFC_WQ_WQE_SIZE_128);
13062 bf_set(lpfc_mbx_wq_create_page_size,
13063 &wq_create->u.request_1,
13064 (PAGE_SIZE/SLI4_PAGE_SIZE));
13065 page = wq_create->u.request_1.page;
13066 break;
13067 }
13068 break;
13069 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013070 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13071 wq->entry_count);
13072 switch (wq->entry_size) {
13073 default:
13074 case 64:
13075 bf_set(lpfc_mbx_wq_create_wqe_size,
13076 &wq_create->u.request_1,
13077 LPFC_WQ_WQE_SIZE_64);
13078 break;
13079 case 128:
James Smart0c651872013-07-15 18:33:23 -040013080 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13081 LPFC_WQ_SZ128_SUPPORT)) {
13082 status = -ERANGE;
13083 goto out;
13084 }
James Smart5a6f1332011-03-11 16:05:35 -050013085 bf_set(lpfc_mbx_wq_create_wqe_size,
13086 &wq_create->u.request_1,
13087 LPFC_WQ_WQE_SIZE_128);
13088 break;
13089 }
13090 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13091 (PAGE_SIZE/SLI4_PAGE_SIZE));
13092 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013093 break;
13094 default:
13095 status = -ERANGE;
13096 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013097 }
James Smart0c651872013-07-15 18:33:23 -040013098
James Smart4f774512009-05-22 14:52:35 -040013099 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013100 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013101 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13102 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013103 }
James Smart962bc512013-01-03 15:44:00 -050013104
13105 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13106 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13107
James Smart4f774512009-05-22 14:52:35 -040013108 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13109 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013110 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13111 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13112 if (shdr_status || shdr_add_status || rc) {
13113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13114 "2503 WQ_CREATE mailbox failed with "
13115 "status x%x add_status x%x, mbx status x%x\n",
13116 shdr_status, shdr_add_status, rc);
13117 status = -ENXIO;
13118 goto out;
13119 }
13120 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13121 if (wq->queue_id == 0xFFFF) {
13122 status = -ENXIO;
13123 goto out;
13124 }
James Smart962bc512013-01-03 15:44:00 -050013125 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13126 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13127 &wq_create->u.response);
13128 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13129 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13131 "3265 WQ[%d] doorbell format not "
13132 "supported: x%x\n", wq->queue_id,
13133 wq->db_format);
13134 status = -EINVAL;
13135 goto out;
13136 }
13137 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13138 &wq_create->u.response);
13139 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13140 if (!bar_memmap_p) {
13141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13142 "3263 WQ[%d] failed to memmap pci "
13143 "barset:x%x\n", wq->queue_id,
13144 pci_barset);
13145 status = -ENOMEM;
13146 goto out;
13147 }
13148 db_offset = wq_create->u.response.doorbell_offset;
13149 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13150 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13152 "3252 WQ[%d] doorbell offset not "
13153 "supported: x%x\n", wq->queue_id,
13154 db_offset);
13155 status = -EINVAL;
13156 goto out;
13157 }
13158 wq->db_regaddr = bar_memmap_p + db_offset;
13159 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013160 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13161 "format:x%x\n", wq->queue_id, pci_barset,
13162 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013163 } else {
13164 wq->db_format = LPFC_DB_LIST_FORMAT;
13165 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13166 }
James Smart4f774512009-05-22 14:52:35 -040013167 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013168 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013169 wq->subtype = subtype;
13170 wq->host_index = 0;
13171 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013172 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013173
13174 /* link the wq onto the parent cq child list */
13175 list_add_tail(&wq->list, &cq->child_list);
13176out:
James Smart8fa38512009-07-19 10:01:03 -040013177 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013178 return status;
13179}
13180
13181/**
James Smart73d91e52011-10-10 21:32:10 -040013182 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13183 * @phba: HBA structure that indicates port to create a queue on.
13184 * @rq: The queue structure to use for the receive queue.
13185 * @qno: The associated HBQ number
13186 *
13187 *
13188 * For SLI4 we need to adjust the RQ repost value based on
13189 * the number of buffers that are initially posted to the RQ.
13190 */
13191void
13192lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13193{
13194 uint32_t cnt;
13195
James Smart2e90f4b2011-12-13 13:22:37 -050013196 /* sanity check on queue memory */
13197 if (!rq)
13198 return;
James Smart73d91e52011-10-10 21:32:10 -040013199 cnt = lpfc_hbq_defs[qno]->entry_count;
13200
13201 /* Recalc repost for RQs based on buffers initially posted */
13202 cnt = (cnt >> 3);
13203 if (cnt < LPFC_QUEUE_MIN_REPOST)
13204 cnt = LPFC_QUEUE_MIN_REPOST;
13205
13206 rq->entry_repost = cnt;
13207}
13208
13209/**
James Smart4f774512009-05-22 14:52:35 -040013210 * lpfc_rq_create - Create a Receive Queue on the HBA
13211 * @phba: HBA structure that indicates port to create a queue on.
13212 * @hrq: The queue structure to use to create the header receive queue.
13213 * @drq: The queue structure to use to create the data receive queue.
13214 * @cq: The completion queue to bind this work queue to.
13215 *
13216 * This function creates a receive buffer queue pair , as detailed in @hrq and
13217 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13218 * to the HBA.
13219 *
13220 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13221 * struct is used to get the entry count that is necessary to determine the
13222 * number of pages to use for this queue. The @cq is used to indicate which
13223 * completion queue to bind received buffers that are posted to these queues to.
13224 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13225 * receive queue pair. This function is asynchronous and will wait for the
13226 * mailbox command to finish before continuing.
13227 *
13228 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013229 * memory this function will return -ENOMEM. If the queue create mailbox command
13230 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013231 **/
13232uint32_t
13233lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13234 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13235{
13236 struct lpfc_mbx_rq_create *rq_create;
13237 struct lpfc_dmabuf *dmabuf;
13238 LPFC_MBOXQ_t *mbox;
13239 int rc, length, status = 0;
13240 uint32_t shdr_status, shdr_add_status;
13241 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013242 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013243 void __iomem *bar_memmap_p;
13244 uint32_t db_offset;
13245 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013246
James Smart2e90f4b2011-12-13 13:22:37 -050013247 /* sanity check on queue memory */
13248 if (!hrq || !drq || !cq)
13249 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013250 if (!phba->sli4_hba.pc_sli4_params.supported)
13251 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013252
13253 if (hrq->entry_count != drq->entry_count)
13254 return -EINVAL;
13255 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13256 if (!mbox)
13257 return -ENOMEM;
13258 length = (sizeof(struct lpfc_mbx_rq_create) -
13259 sizeof(struct lpfc_sli4_cfg_mhdr));
13260 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13261 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13262 length, LPFC_SLI4_MBX_EMBED);
13263 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013264 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13265 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13266 phba->sli4_hba.pc_sli4_params.rqv);
13267 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13268 bf_set(lpfc_rq_context_rqe_count_1,
13269 &rq_create->u.request.context,
13270 hrq->entry_count);
13271 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013272 bf_set(lpfc_rq_context_rqe_size,
13273 &rq_create->u.request.context,
13274 LPFC_RQE_SIZE_8);
13275 bf_set(lpfc_rq_context_page_size,
13276 &rq_create->u.request.context,
13277 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013278 } else {
13279 switch (hrq->entry_count) {
13280 default:
13281 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13282 "2535 Unsupported RQ count. (%d)\n",
13283 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013284 if (hrq->entry_count < 512) {
13285 status = -EINVAL;
13286 goto out;
13287 }
James Smart5a6f1332011-03-11 16:05:35 -050013288 /* otherwise default to smallest count (drop through) */
13289 case 512:
13290 bf_set(lpfc_rq_context_rqe_count,
13291 &rq_create->u.request.context,
13292 LPFC_RQ_RING_SIZE_512);
13293 break;
13294 case 1024:
13295 bf_set(lpfc_rq_context_rqe_count,
13296 &rq_create->u.request.context,
13297 LPFC_RQ_RING_SIZE_1024);
13298 break;
13299 case 2048:
13300 bf_set(lpfc_rq_context_rqe_count,
13301 &rq_create->u.request.context,
13302 LPFC_RQ_RING_SIZE_2048);
13303 break;
13304 case 4096:
13305 bf_set(lpfc_rq_context_rqe_count,
13306 &rq_create->u.request.context,
13307 LPFC_RQ_RING_SIZE_4096);
13308 break;
13309 }
13310 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13311 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013312 }
13313 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13314 cq->queue_id);
13315 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13316 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013317 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013318 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013319 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13320 putPaddrLow(dmabuf->phys);
13321 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13322 putPaddrHigh(dmabuf->phys);
13323 }
James Smart962bc512013-01-03 15:44:00 -050013324 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13325 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13326
James Smart4f774512009-05-22 14:52:35 -040013327 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13328 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013329 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13330 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13331 if (shdr_status || shdr_add_status || rc) {
13332 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13333 "2504 RQ_CREATE mailbox failed with "
13334 "status x%x add_status x%x, mbx status x%x\n",
13335 shdr_status, shdr_add_status, rc);
13336 status = -ENXIO;
13337 goto out;
13338 }
13339 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13340 if (hrq->queue_id == 0xFFFF) {
13341 status = -ENXIO;
13342 goto out;
13343 }
James Smart962bc512013-01-03 15:44:00 -050013344
13345 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13346 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13347 &rq_create->u.response);
13348 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13349 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13351 "3262 RQ [%d] doorbell format not "
13352 "supported: x%x\n", hrq->queue_id,
13353 hrq->db_format);
13354 status = -EINVAL;
13355 goto out;
13356 }
13357
13358 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13359 &rq_create->u.response);
13360 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13361 if (!bar_memmap_p) {
13362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13363 "3269 RQ[%d] failed to memmap pci "
13364 "barset:x%x\n", hrq->queue_id,
13365 pci_barset);
13366 status = -ENOMEM;
13367 goto out;
13368 }
13369
13370 db_offset = rq_create->u.response.doorbell_offset;
13371 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13372 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13374 "3270 RQ[%d] doorbell offset not "
13375 "supported: x%x\n", hrq->queue_id,
13376 db_offset);
13377 status = -EINVAL;
13378 goto out;
13379 }
13380 hrq->db_regaddr = bar_memmap_p + db_offset;
13381 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013382 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13383 "format:x%x\n", hrq->queue_id, pci_barset,
13384 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013385 } else {
13386 hrq->db_format = LPFC_DB_RING_FORMAT;
13387 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13388 }
James Smart4f774512009-05-22 14:52:35 -040013389 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013390 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013391 hrq->subtype = subtype;
13392 hrq->host_index = 0;
13393 hrq->hba_index = 0;
13394
13395 /* now create the data queue */
13396 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13397 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13398 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050013399 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13400 phba->sli4_hba.pc_sli4_params.rqv);
13401 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13402 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040013403 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050013404 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013405 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13406 LPFC_RQE_SIZE_8);
13407 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13408 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013409 } else {
13410 switch (drq->entry_count) {
13411 default:
13412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13413 "2536 Unsupported RQ count. (%d)\n",
13414 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013415 if (drq->entry_count < 512) {
13416 status = -EINVAL;
13417 goto out;
13418 }
James Smart5a6f1332011-03-11 16:05:35 -050013419 /* otherwise default to smallest count (drop through) */
13420 case 512:
13421 bf_set(lpfc_rq_context_rqe_count,
13422 &rq_create->u.request.context,
13423 LPFC_RQ_RING_SIZE_512);
13424 break;
13425 case 1024:
13426 bf_set(lpfc_rq_context_rqe_count,
13427 &rq_create->u.request.context,
13428 LPFC_RQ_RING_SIZE_1024);
13429 break;
13430 case 2048:
13431 bf_set(lpfc_rq_context_rqe_count,
13432 &rq_create->u.request.context,
13433 LPFC_RQ_RING_SIZE_2048);
13434 break;
13435 case 4096:
13436 bf_set(lpfc_rq_context_rqe_count,
13437 &rq_create->u.request.context,
13438 LPFC_RQ_RING_SIZE_4096);
13439 break;
13440 }
13441 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13442 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013443 }
13444 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13445 cq->queue_id);
13446 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13447 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013448 list_for_each_entry(dmabuf, &drq->page_list, list) {
13449 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13450 putPaddrLow(dmabuf->phys);
13451 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13452 putPaddrHigh(dmabuf->phys);
13453 }
James Smart962bc512013-01-03 15:44:00 -050013454 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13455 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040013456 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13457 /* The IOCTL status is embedded in the mailbox subheader. */
13458 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13459 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13460 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13461 if (shdr_status || shdr_add_status || rc) {
13462 status = -ENXIO;
13463 goto out;
13464 }
13465 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13466 if (drq->queue_id == 0xFFFF) {
13467 status = -ENXIO;
13468 goto out;
13469 }
13470 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013471 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013472 drq->subtype = subtype;
13473 drq->host_index = 0;
13474 drq->hba_index = 0;
13475
13476 /* link the header and data RQs onto the parent cq child list */
13477 list_add_tail(&hrq->list, &cq->child_list);
13478 list_add_tail(&drq->list, &cq->child_list);
13479
13480out:
James Smart8fa38512009-07-19 10:01:03 -040013481 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013482 return status;
13483}
13484
13485/**
13486 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13487 * @eq: The queue structure associated with the queue to destroy.
13488 *
13489 * This function destroys a queue, as detailed in @eq by sending an mailbox
13490 * command, specific to the type of queue, to the HBA.
13491 *
13492 * The @eq struct is used to get the queue ID of the queue to destroy.
13493 *
13494 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013495 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013496 **/
13497uint32_t
13498lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13499{
13500 LPFC_MBOXQ_t *mbox;
13501 int rc, length, status = 0;
13502 uint32_t shdr_status, shdr_add_status;
13503 union lpfc_sli4_cfg_shdr *shdr;
13504
James Smart2e90f4b2011-12-13 13:22:37 -050013505 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013506 if (!eq)
13507 return -ENODEV;
13508 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13509 if (!mbox)
13510 return -ENOMEM;
13511 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13512 sizeof(struct lpfc_sli4_cfg_mhdr));
13513 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13514 LPFC_MBOX_OPCODE_EQ_DESTROY,
13515 length, LPFC_SLI4_MBX_EMBED);
13516 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13517 eq->queue_id);
13518 mbox->vport = eq->phba->pport;
13519 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13520
13521 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13522 /* The IOCTL status is embedded in the mailbox subheader. */
13523 shdr = (union lpfc_sli4_cfg_shdr *)
13524 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13525 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13526 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13527 if (shdr_status || shdr_add_status || rc) {
13528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13529 "2505 EQ_DESTROY mailbox failed with "
13530 "status x%x add_status x%x, mbx status x%x\n",
13531 shdr_status, shdr_add_status, rc);
13532 status = -ENXIO;
13533 }
13534
13535 /* Remove eq from any list */
13536 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013537 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013538 return status;
13539}
13540
13541/**
13542 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13543 * @cq: The queue structure associated with the queue to destroy.
13544 *
13545 * This function destroys a queue, as detailed in @cq by sending an mailbox
13546 * command, specific to the type of queue, to the HBA.
13547 *
13548 * The @cq struct is used to get the queue ID of the queue to destroy.
13549 *
13550 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013551 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013552 **/
13553uint32_t
13554lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13555{
13556 LPFC_MBOXQ_t *mbox;
13557 int rc, length, status = 0;
13558 uint32_t shdr_status, shdr_add_status;
13559 union lpfc_sli4_cfg_shdr *shdr;
13560
James Smart2e90f4b2011-12-13 13:22:37 -050013561 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013562 if (!cq)
13563 return -ENODEV;
13564 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13565 if (!mbox)
13566 return -ENOMEM;
13567 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13568 sizeof(struct lpfc_sli4_cfg_mhdr));
13569 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13570 LPFC_MBOX_OPCODE_CQ_DESTROY,
13571 length, LPFC_SLI4_MBX_EMBED);
13572 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13573 cq->queue_id);
13574 mbox->vport = cq->phba->pport;
13575 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13576 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13577 /* The IOCTL status is embedded in the mailbox subheader. */
13578 shdr = (union lpfc_sli4_cfg_shdr *)
13579 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13580 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13581 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13582 if (shdr_status || shdr_add_status || rc) {
13583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13584 "2506 CQ_DESTROY mailbox failed with "
13585 "status x%x add_status x%x, mbx status x%x\n",
13586 shdr_status, shdr_add_status, rc);
13587 status = -ENXIO;
13588 }
13589 /* Remove cq from any list */
13590 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013591 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013592 return status;
13593}
13594
13595/**
James Smart04c68492009-05-22 14:52:52 -040013596 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13597 * @qm: The queue structure associated with the queue to destroy.
13598 *
13599 * This function destroys a queue, as detailed in @mq by sending an mailbox
13600 * command, specific to the type of queue, to the HBA.
13601 *
13602 * The @mq struct is used to get the queue ID of the queue to destroy.
13603 *
13604 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013605 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013606 **/
13607uint32_t
13608lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13609{
13610 LPFC_MBOXQ_t *mbox;
13611 int rc, length, status = 0;
13612 uint32_t shdr_status, shdr_add_status;
13613 union lpfc_sli4_cfg_shdr *shdr;
13614
James Smart2e90f4b2011-12-13 13:22:37 -050013615 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013616 if (!mq)
13617 return -ENODEV;
13618 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13619 if (!mbox)
13620 return -ENOMEM;
13621 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13622 sizeof(struct lpfc_sli4_cfg_mhdr));
13623 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13624 LPFC_MBOX_OPCODE_MQ_DESTROY,
13625 length, LPFC_SLI4_MBX_EMBED);
13626 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13627 mq->queue_id);
13628 mbox->vport = mq->phba->pport;
13629 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13630 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13631 /* The IOCTL status is embedded in the mailbox subheader. */
13632 shdr = (union lpfc_sli4_cfg_shdr *)
13633 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13634 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13635 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13636 if (shdr_status || shdr_add_status || rc) {
13637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13638 "2507 MQ_DESTROY mailbox failed with "
13639 "status x%x add_status x%x, mbx status x%x\n",
13640 shdr_status, shdr_add_status, rc);
13641 status = -ENXIO;
13642 }
13643 /* Remove mq from any list */
13644 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013645 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013646 return status;
13647}
13648
13649/**
James Smart4f774512009-05-22 14:52:35 -040013650 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13651 * @wq: The queue structure associated with the queue to destroy.
13652 *
13653 * This function destroys a queue, as detailed in @wq by sending an mailbox
13654 * command, specific to the type of queue, to the HBA.
13655 *
13656 * The @wq struct is used to get the queue ID of the queue to destroy.
13657 *
13658 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013659 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013660 **/
13661uint32_t
13662lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13663{
13664 LPFC_MBOXQ_t *mbox;
13665 int rc, length, status = 0;
13666 uint32_t shdr_status, shdr_add_status;
13667 union lpfc_sli4_cfg_shdr *shdr;
13668
James Smart2e90f4b2011-12-13 13:22:37 -050013669 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013670 if (!wq)
13671 return -ENODEV;
13672 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13673 if (!mbox)
13674 return -ENOMEM;
13675 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13676 sizeof(struct lpfc_sli4_cfg_mhdr));
13677 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13678 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13679 length, LPFC_SLI4_MBX_EMBED);
13680 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13681 wq->queue_id);
13682 mbox->vport = wq->phba->pport;
13683 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13684 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13685 shdr = (union lpfc_sli4_cfg_shdr *)
13686 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13687 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13688 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13689 if (shdr_status || shdr_add_status || rc) {
13690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13691 "2508 WQ_DESTROY mailbox failed with "
13692 "status x%x add_status x%x, mbx status x%x\n",
13693 shdr_status, shdr_add_status, rc);
13694 status = -ENXIO;
13695 }
13696 /* Remove wq from any list */
13697 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013698 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013699 return status;
13700}
13701
13702/**
13703 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13704 * @rq: The queue structure associated with the queue to destroy.
13705 *
13706 * This function destroys a queue, as detailed in @rq by sending an mailbox
13707 * command, specific to the type of queue, to the HBA.
13708 *
13709 * The @rq struct is used to get the queue ID of the queue to destroy.
13710 *
13711 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013712 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013713 **/
13714uint32_t
13715lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13716 struct lpfc_queue *drq)
13717{
13718 LPFC_MBOXQ_t *mbox;
13719 int rc, length, status = 0;
13720 uint32_t shdr_status, shdr_add_status;
13721 union lpfc_sli4_cfg_shdr *shdr;
13722
James Smart2e90f4b2011-12-13 13:22:37 -050013723 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013724 if (!hrq || !drq)
13725 return -ENODEV;
13726 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13727 if (!mbox)
13728 return -ENOMEM;
13729 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013730 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013731 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13732 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13733 length, LPFC_SLI4_MBX_EMBED);
13734 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13735 hrq->queue_id);
13736 mbox->vport = hrq->phba->pport;
13737 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13738 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13739 /* The IOCTL status is embedded in the mailbox subheader. */
13740 shdr = (union lpfc_sli4_cfg_shdr *)
13741 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13742 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13743 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13744 if (shdr_status || shdr_add_status || rc) {
13745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13746 "2509 RQ_DESTROY mailbox failed with "
13747 "status x%x add_status x%x, mbx status x%x\n",
13748 shdr_status, shdr_add_status, rc);
13749 if (rc != MBX_TIMEOUT)
13750 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13751 return -ENXIO;
13752 }
13753 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13754 drq->queue_id);
13755 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13756 shdr = (union lpfc_sli4_cfg_shdr *)
13757 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13758 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13759 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13760 if (shdr_status || shdr_add_status || rc) {
13761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13762 "2510 RQ_DESTROY mailbox failed with "
13763 "status x%x add_status x%x, mbx status x%x\n",
13764 shdr_status, shdr_add_status, rc);
13765 status = -ENXIO;
13766 }
13767 list_del_init(&hrq->list);
13768 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013769 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013770 return status;
13771}
13772
13773/**
13774 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13775 * @phba: The virtual port for which this call being executed.
13776 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13777 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13778 * @xritag: the xritag that ties this io to the SGL pages.
13779 *
13780 * This routine will post the sgl pages for the IO that has the xritag
13781 * that is in the iocbq structure. The xritag is assigned during iocbq
13782 * creation and persists for as long as the driver is loaded.
13783 * if the caller has fewer than 256 scatter gather segments to map then
13784 * pdma_phys_addr1 should be 0.
13785 * If the caller needs to map more than 256 scatter gather segment then
13786 * pdma_phys_addr1 should be a valid physical address.
13787 * physical address for SGLs must be 64 byte aligned.
13788 * If you are going to map 2 SGL's then the first one must have 256 entries
13789 * the second sgl can have between 1 and 256 entries.
13790 *
13791 * Return codes:
13792 * 0 - Success
13793 * -ENXIO, -ENOMEM - Failure
13794 **/
13795int
13796lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13797 dma_addr_t pdma_phys_addr0,
13798 dma_addr_t pdma_phys_addr1,
13799 uint16_t xritag)
13800{
13801 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13802 LPFC_MBOXQ_t *mbox;
13803 int rc;
13804 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013805 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013806 union lpfc_sli4_cfg_shdr *shdr;
13807
13808 if (xritag == NO_XRI) {
13809 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13810 "0364 Invalid param:\n");
13811 return -EINVAL;
13812 }
13813
13814 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13815 if (!mbox)
13816 return -ENOMEM;
13817
13818 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13819 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13820 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013821 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013822
13823 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13824 &mbox->u.mqe.un.post_sgl_pages;
13825 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13826 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13827
13828 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13829 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13830 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13831 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13832
13833 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13834 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13835 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13836 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13837 if (!phba->sli4_hba.intr_enable)
13838 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013839 else {
James Smarta183a152011-10-10 21:32:43 -040013840 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013841 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13842 }
James Smart4f774512009-05-22 14:52:35 -040013843 /* The IOCTL status is embedded in the mailbox subheader. */
13844 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13845 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13846 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13847 if (rc != MBX_TIMEOUT)
13848 mempool_free(mbox, phba->mbox_mem_pool);
13849 if (shdr_status || shdr_add_status || rc) {
13850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13851 "2511 POST_SGL mailbox failed with "
13852 "status x%x add_status x%x, mbx status x%x\n",
13853 shdr_status, shdr_add_status, rc);
13854 rc = -ENXIO;
13855 }
13856 return 0;
13857}
James Smart4f774512009-05-22 14:52:35 -040013858
13859/**
James Smart88a2cfb2011-07-22 18:36:33 -040013860 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013861 * @phba: pointer to lpfc hba data structure.
13862 *
13863 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013864 * HBA consistent with the SLI-4 interface spec. This routine
13865 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13866 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013867 *
James Smart88a2cfb2011-07-22 18:36:33 -040013868 * Returns
13869 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13870 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13871 **/
James Smart6d368e52011-05-24 11:44:12 -040013872uint16_t
13873lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13874{
13875 unsigned long xri;
13876
13877 /*
13878 * Fetch the next logical xri. Because this index is logical,
13879 * the driver starts at 0 each time.
13880 */
13881 spin_lock_irq(&phba->hbalock);
13882 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13883 phba->sli4_hba.max_cfg_param.max_xri, 0);
13884 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13885 spin_unlock_irq(&phba->hbalock);
13886 return NO_XRI;
13887 } else {
13888 set_bit(xri, phba->sli4_hba.xri_bmask);
13889 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013890 }
James Smart6d368e52011-05-24 11:44:12 -040013891 spin_unlock_irq(&phba->hbalock);
13892 return xri;
13893}
13894
13895/**
13896 * lpfc_sli4_free_xri - Release an xri for reuse.
13897 * @phba: pointer to lpfc hba data structure.
13898 *
13899 * This routine is invoked to release an xri to the pool of
13900 * available rpis maintained by the driver.
13901 **/
13902void
13903__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13904{
13905 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013906 phba->sli4_hba.max_cfg_param.xri_used--;
13907 }
13908}
13909
13910/**
13911 * lpfc_sli4_free_xri - Release an xri for reuse.
13912 * @phba: pointer to lpfc hba data structure.
13913 *
13914 * This routine is invoked to release an xri to the pool of
13915 * available rpis maintained by the driver.
13916 **/
13917void
13918lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13919{
13920 spin_lock_irq(&phba->hbalock);
13921 __lpfc_sli4_free_xri(phba, xri);
13922 spin_unlock_irq(&phba->hbalock);
13923}
13924
13925/**
James Smart4f774512009-05-22 14:52:35 -040013926 * lpfc_sli4_next_xritag - Get an xritag for the io
13927 * @phba: Pointer to HBA context object.
13928 *
13929 * This function gets an xritag for the iocb. If there is no unused xritag
13930 * it will return 0xffff.
13931 * The function returns the allocated xritag if successful, else returns zero.
13932 * Zero is not a valid xritag.
13933 * The caller is not required to hold any lock.
13934 **/
13935uint16_t
13936lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13937{
James Smart6d368e52011-05-24 11:44:12 -040013938 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013939
James Smart6d368e52011-05-24 11:44:12 -040013940 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013941 if (xri_index == NO_XRI)
13942 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13943 "2004 Failed to allocate XRI.last XRITAG is %d"
13944 " Max XRI is %d, Used XRI is %d\n",
13945 xri_index,
13946 phba->sli4_hba.max_cfg_param.max_xri,
13947 phba->sli4_hba.max_cfg_param.xri_used);
13948 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013949}
13950
13951/**
James Smart6d368e52011-05-24 11:44:12 -040013952 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013953 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013954 * @post_sgl_list: pointer to els sgl entry list.
13955 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013956 *
13957 * This routine is invoked to post a block of driver's sgl pages to the
13958 * HBA using non-embedded mailbox command. No Lock is held. This routine
13959 * is only called when the driver is loading and after all IO has been
13960 * stopped.
13961 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013962static int
13963lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13964 struct list_head *post_sgl_list,
13965 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013966{
James Smart8a9d2e82012-05-09 21:16:12 -040013967 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013968 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13969 struct sgl_page_pairs *sgl_pg_pairs;
13970 void *viraddr;
13971 LPFC_MBOXQ_t *mbox;
13972 uint32_t reqlen, alloclen, pg_pairs;
13973 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013974 uint16_t xritag_start = 0;
13975 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013976 uint32_t shdr_status, shdr_add_status;
13977 union lpfc_sli4_cfg_shdr *shdr;
13978
James Smart8a9d2e82012-05-09 21:16:12 -040013979 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013980 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013981 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013982 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13983 "2559 Block sgl registration required DMA "
13984 "size (%d) great than a page\n", reqlen);
13985 return -ENOMEM;
13986 }
13987 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013988 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013989 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013990
13991 /* Allocate DMA memory and set up the non-embedded mailbox command */
13992 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13993 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13994 LPFC_SLI4_MBX_NEMBED);
13995
13996 if (alloclen < reqlen) {
13997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13998 "0285 Allocated DMA memory size (%d) is "
13999 "less than the requested DMA memory "
14000 "size (%d)\n", alloclen, reqlen);
14001 lpfc_sli4_mbox_cmd_free(phba, mbox);
14002 return -ENOMEM;
14003 }
James Smart4f774512009-05-22 14:52:35 -040014004 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014005 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014006 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14007 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14008
James Smart8a9d2e82012-05-09 21:16:12 -040014009 pg_pairs = 0;
14010 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014011 /* Set up the sge entry */
14012 sgl_pg_pairs->sgl_pg0_addr_lo =
14013 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14014 sgl_pg_pairs->sgl_pg0_addr_hi =
14015 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14016 sgl_pg_pairs->sgl_pg1_addr_lo =
14017 cpu_to_le32(putPaddrLow(0));
14018 sgl_pg_pairs->sgl_pg1_addr_hi =
14019 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014020
James Smart4f774512009-05-22 14:52:35 -040014021 /* Keep the first xritag on the list */
14022 if (pg_pairs == 0)
14023 xritag_start = sglq_entry->sli4_xritag;
14024 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014025 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014026 }
James Smart6d368e52011-05-24 11:44:12 -040014027
14028 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014029 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014030 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014031 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014032 if (!phba->sli4_hba.intr_enable)
14033 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14034 else {
James Smarta183a152011-10-10 21:32:43 -040014035 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014036 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14037 }
14038 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14039 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14040 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14041 if (rc != MBX_TIMEOUT)
14042 lpfc_sli4_mbox_cmd_free(phba, mbox);
14043 if (shdr_status || shdr_add_status || rc) {
14044 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14045 "2513 POST_SGL_BLOCK mailbox command failed "
14046 "status x%x add_status x%x mbx status x%x\n",
14047 shdr_status, shdr_add_status, rc);
14048 rc = -ENXIO;
14049 }
14050 return rc;
14051}
14052
14053/**
14054 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14055 * @phba: pointer to lpfc hba data structure.
14056 * @sblist: pointer to scsi buffer list.
14057 * @count: number of scsi buffers on the list.
14058 *
14059 * This routine is invoked to post a block of @count scsi sgl pages from a
14060 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14061 * No Lock is held.
14062 *
14063 **/
14064int
James Smart8a9d2e82012-05-09 21:16:12 -040014065lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14066 struct list_head *sblist,
14067 int count)
James Smart4f774512009-05-22 14:52:35 -040014068{
14069 struct lpfc_scsi_buf *psb;
14070 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14071 struct sgl_page_pairs *sgl_pg_pairs;
14072 void *viraddr;
14073 LPFC_MBOXQ_t *mbox;
14074 uint32_t reqlen, alloclen, pg_pairs;
14075 uint32_t mbox_tmo;
14076 uint16_t xritag_start = 0;
14077 int rc = 0;
14078 uint32_t shdr_status, shdr_add_status;
14079 dma_addr_t pdma_phys_bpl1;
14080 union lpfc_sli4_cfg_shdr *shdr;
14081
14082 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014083 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014084 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014085 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014086 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14087 "0217 Block sgl registration required DMA "
14088 "size (%d) great than a page\n", reqlen);
14089 return -ENOMEM;
14090 }
14091 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14092 if (!mbox) {
14093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14094 "0283 Failed to allocate mbox cmd memory\n");
14095 return -ENOMEM;
14096 }
14097
14098 /* Allocate DMA memory and set up the non-embedded mailbox command */
14099 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14100 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14101 LPFC_SLI4_MBX_NEMBED);
14102
14103 if (alloclen < reqlen) {
14104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14105 "2561 Allocated DMA memory size (%d) is "
14106 "less than the requested DMA memory "
14107 "size (%d)\n", alloclen, reqlen);
14108 lpfc_sli4_mbox_cmd_free(phba, mbox);
14109 return -ENOMEM;
14110 }
James Smart6d368e52011-05-24 11:44:12 -040014111
James Smart4f774512009-05-22 14:52:35 -040014112 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014113 viraddr = mbox->sge_array->addr[0];
14114
14115 /* Set up the SGL pages in the non-embedded DMA pages */
14116 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14117 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14118
14119 pg_pairs = 0;
14120 list_for_each_entry(psb, sblist, list) {
14121 /* Set up the sge entry */
14122 sgl_pg_pairs->sgl_pg0_addr_lo =
14123 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14124 sgl_pg_pairs->sgl_pg0_addr_hi =
14125 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14126 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14127 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14128 else
14129 pdma_phys_bpl1 = 0;
14130 sgl_pg_pairs->sgl_pg1_addr_lo =
14131 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14132 sgl_pg_pairs->sgl_pg1_addr_hi =
14133 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14134 /* Keep the first xritag on the list */
14135 if (pg_pairs == 0)
14136 xritag_start = psb->cur_iocbq.sli4_xritag;
14137 sgl_pg_pairs++;
14138 pg_pairs++;
14139 }
14140 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14141 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14142 /* Perform endian conversion if necessary */
14143 sgl->word0 = cpu_to_le32(sgl->word0);
14144
14145 if (!phba->sli4_hba.intr_enable)
14146 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14147 else {
James Smarta183a152011-10-10 21:32:43 -040014148 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014149 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14150 }
14151 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14152 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14153 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14154 if (rc != MBX_TIMEOUT)
14155 lpfc_sli4_mbox_cmd_free(phba, mbox);
14156 if (shdr_status || shdr_add_status || rc) {
14157 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14158 "2564 POST_SGL_BLOCK mailbox command failed "
14159 "status x%x add_status x%x mbx status x%x\n",
14160 shdr_status, shdr_add_status, rc);
14161 rc = -ENXIO;
14162 }
14163 return rc;
14164}
14165
14166/**
14167 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14168 * @phba: pointer to lpfc_hba struct that the frame was received on
14169 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14170 *
14171 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14172 * valid type of frame that the LPFC driver will handle. This function will
14173 * return a zero if the frame is a valid frame or a non zero value when the
14174 * frame does not pass the check.
14175 **/
14176static int
14177lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14178{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014179 /* make rctl_names static to save stack space */
14180 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014181 char *type_names[] = FC_TYPE_NAMES_INIT;
14182 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014183 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014184
14185 switch (fc_hdr->fh_r_ctl) {
14186 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14187 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14188 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14189 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14190 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14191 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14192 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14193 case FC_RCTL_DD_CMD_STATUS: /* command status */
14194 case FC_RCTL_ELS_REQ: /* extended link services request */
14195 case FC_RCTL_ELS_REP: /* extended link services reply */
14196 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14197 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14198 case FC_RCTL_BA_NOP: /* basic link service NOP */
14199 case FC_RCTL_BA_ABTS: /* basic link service abort */
14200 case FC_RCTL_BA_RMC: /* remove connection */
14201 case FC_RCTL_BA_ACC: /* basic accept */
14202 case FC_RCTL_BA_RJT: /* basic reject */
14203 case FC_RCTL_BA_PRMT:
14204 case FC_RCTL_ACK_1: /* acknowledge_1 */
14205 case FC_RCTL_ACK_0: /* acknowledge_0 */
14206 case FC_RCTL_P_RJT: /* port reject */
14207 case FC_RCTL_F_RJT: /* fabric reject */
14208 case FC_RCTL_P_BSY: /* port busy */
14209 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14210 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14211 case FC_RCTL_LCR: /* link credit reset */
14212 case FC_RCTL_END: /* end */
14213 break;
14214 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14215 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14216 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14217 return lpfc_fc_frame_check(phba, fc_hdr);
14218 default:
14219 goto drop;
14220 }
14221 switch (fc_hdr->fh_type) {
14222 case FC_TYPE_BLS:
14223 case FC_TYPE_ELS:
14224 case FC_TYPE_FCP:
14225 case FC_TYPE_CT:
14226 break;
14227 case FC_TYPE_IP:
14228 case FC_TYPE_ILS:
14229 default:
14230 goto drop;
14231 }
James Smart546fc852011-03-11 16:06:29 -050014232
James Smart4f774512009-05-22 14:52:35 -040014233 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014234 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14235 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14236 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14237 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014238 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14239 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014240 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14241 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014242 return 0;
14243drop:
14244 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14245 "2539 Dropped frame rctl:%s type:%s\n",
14246 rctl_names[fc_hdr->fh_r_ctl],
14247 type_names[fc_hdr->fh_type]);
14248 return 1;
14249}
14250
14251/**
14252 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14253 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14254 *
14255 * This function processes the FC header to retrieve the VFI from the VF
14256 * header, if one exists. This function will return the VFI if one exists
14257 * or 0 if no VSAN Header exists.
14258 **/
14259static uint32_t
14260lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14261{
14262 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14263
14264 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14265 return 0;
14266 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14267}
14268
14269/**
14270 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14271 * @phba: Pointer to the HBA structure to search for the vport on
14272 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14273 * @fcfi: The FC Fabric ID that the frame came from
14274 *
14275 * This function searches the @phba for a vport that matches the content of the
14276 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14277 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14278 * returns the matching vport pointer or NULL if unable to match frame to a
14279 * vport.
14280 **/
14281static struct lpfc_vport *
14282lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14283 uint16_t fcfi)
14284{
14285 struct lpfc_vport **vports;
14286 struct lpfc_vport *vport = NULL;
14287 int i;
14288 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14289 fc_hdr->fh_d_id[1] << 8 |
14290 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014291
James Smartbf086112011-08-21 21:48:13 -040014292 if (did == Fabric_DID)
14293 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014294 if ((phba->pport->fc_flag & FC_PT2PT) &&
14295 !(phba->link_state == LPFC_HBA_READY))
14296 return phba->pport;
14297
James Smart4f774512009-05-22 14:52:35 -040014298 vports = lpfc_create_vport_work_array(phba);
14299 if (vports != NULL)
14300 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14301 if (phba->fcf.fcfi == fcfi &&
14302 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14303 vports[i]->fc_myDID == did) {
14304 vport = vports[i];
14305 break;
14306 }
14307 }
14308 lpfc_destroy_vport_work_array(phba, vports);
14309 return vport;
14310}
14311
14312/**
James Smart45ed1192009-10-02 15:17:02 -040014313 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14314 * @vport: The vport to work on.
14315 *
14316 * This function updates the receive sequence time stamp for this vport. The
14317 * receive sequence time stamp indicates the time that the last frame of the
14318 * the sequence that has been idle for the longest amount of time was received.
14319 * the driver uses this time stamp to indicate if any received sequences have
14320 * timed out.
14321 **/
14322void
14323lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14324{
14325 struct lpfc_dmabuf *h_buf;
14326 struct hbq_dmabuf *dmabuf = NULL;
14327
14328 /* get the oldest sequence on the rcv list */
14329 h_buf = list_get_first(&vport->rcv_buffer_list,
14330 struct lpfc_dmabuf, list);
14331 if (!h_buf)
14332 return;
14333 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14334 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14335}
14336
14337/**
14338 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14339 * @vport: The vport that the received sequences were sent to.
14340 *
14341 * This function cleans up all outstanding received sequences. This is called
14342 * by the driver when a link event or user action invalidates all the received
14343 * sequences.
14344 **/
14345void
14346lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14347{
14348 struct lpfc_dmabuf *h_buf, *hnext;
14349 struct lpfc_dmabuf *d_buf, *dnext;
14350 struct hbq_dmabuf *dmabuf = NULL;
14351
14352 /* start with the oldest sequence on the rcv list */
14353 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14354 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14355 list_del_init(&dmabuf->hbuf.list);
14356 list_for_each_entry_safe(d_buf, dnext,
14357 &dmabuf->dbuf.list, list) {
14358 list_del_init(&d_buf->list);
14359 lpfc_in_buf_free(vport->phba, d_buf);
14360 }
14361 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14362 }
14363}
14364
14365/**
14366 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14367 * @vport: The vport that the received sequences were sent to.
14368 *
14369 * This function determines whether any received sequences have timed out by
14370 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14371 * indicates that there is at least one timed out sequence this routine will
14372 * go through the received sequences one at a time from most inactive to most
14373 * active to determine which ones need to be cleaned up. Once it has determined
14374 * that a sequence needs to be cleaned up it will simply free up the resources
14375 * without sending an abort.
14376 **/
14377void
14378lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14379{
14380 struct lpfc_dmabuf *h_buf, *hnext;
14381 struct lpfc_dmabuf *d_buf, *dnext;
14382 struct hbq_dmabuf *dmabuf = NULL;
14383 unsigned long timeout;
14384 int abort_count = 0;
14385
14386 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14387 vport->rcv_buffer_time_stamp);
14388 if (list_empty(&vport->rcv_buffer_list) ||
14389 time_before(jiffies, timeout))
14390 return;
14391 /* start with the oldest sequence on the rcv list */
14392 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14393 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14394 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14395 dmabuf->time_stamp);
14396 if (time_before(jiffies, timeout))
14397 break;
14398 abort_count++;
14399 list_del_init(&dmabuf->hbuf.list);
14400 list_for_each_entry_safe(d_buf, dnext,
14401 &dmabuf->dbuf.list, list) {
14402 list_del_init(&d_buf->list);
14403 lpfc_in_buf_free(vport->phba, d_buf);
14404 }
14405 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14406 }
14407 if (abort_count)
14408 lpfc_update_rcv_time_stamp(vport);
14409}
14410
14411/**
James Smart4f774512009-05-22 14:52:35 -040014412 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14413 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14414 *
14415 * This function searches through the existing incomplete sequences that have
14416 * been sent to this @vport. If the frame matches one of the incomplete
14417 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14418 * make up that sequence. If no sequence is found that matches this frame then
14419 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14420 * This function returns a pointer to the first dmabuf in the sequence list that
14421 * the frame was linked to.
14422 **/
14423static struct hbq_dmabuf *
14424lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14425{
14426 struct fc_frame_header *new_hdr;
14427 struct fc_frame_header *temp_hdr;
14428 struct lpfc_dmabuf *d_buf;
14429 struct lpfc_dmabuf *h_buf;
14430 struct hbq_dmabuf *seq_dmabuf = NULL;
14431 struct hbq_dmabuf *temp_dmabuf = NULL;
14432
James Smart4d9ab992009-10-02 15:16:39 -040014433 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014434 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014435 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14436 /* Use the hdr_buf to find the sequence that this frame belongs to */
14437 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14438 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14439 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14440 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14441 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14442 continue;
14443 /* found a pending sequence that matches this frame */
14444 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14445 break;
14446 }
14447 if (!seq_dmabuf) {
14448 /*
14449 * This indicates first frame received for this sequence.
14450 * Queue the buffer on the vport's rcv_buffer_list.
14451 */
14452 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040014453 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014454 return dmabuf;
14455 }
14456 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050014457 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14458 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040014459 list_del_init(&seq_dmabuf->hbuf.list);
14460 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14461 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014462 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014463 return dmabuf;
14464 }
James Smart45ed1192009-10-02 15:17:02 -040014465 /* move this sequence to the tail to indicate a young sequence */
14466 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14467 seq_dmabuf->time_stamp = jiffies;
14468 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014469 if (list_empty(&seq_dmabuf->dbuf.list)) {
14470 temp_hdr = dmabuf->hbuf.virt;
14471 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14472 return seq_dmabuf;
14473 }
James Smart4f774512009-05-22 14:52:35 -040014474 /* find the correct place in the sequence to insert this frame */
14475 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14476 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14477 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14478 /*
14479 * If the frame's sequence count is greater than the frame on
14480 * the list then insert the frame right after this frame
14481 */
James Smarteeead812009-12-21 17:01:23 -050014482 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14483 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014484 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14485 return seq_dmabuf;
14486 }
14487 }
14488 return NULL;
14489}
14490
14491/**
James Smart6669f9b2009-10-02 15:16:45 -040014492 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14493 * @vport: pointer to a vitural port
14494 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14495 *
14496 * This function tries to abort from the partially assembed sequence, described
14497 * by the information from basic abbort @dmabuf. It checks to see whether such
14498 * partially assembled sequence held by the driver. If so, it shall free up all
14499 * the frames from the partially assembled sequence.
14500 *
14501 * Return
14502 * true -- if there is matching partially assembled sequence present and all
14503 * the frames freed with the sequence;
14504 * false -- if there is no matching partially assembled sequence present so
14505 * nothing got aborted in the lower layer driver
14506 **/
14507static bool
14508lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14509 struct hbq_dmabuf *dmabuf)
14510{
14511 struct fc_frame_header *new_hdr;
14512 struct fc_frame_header *temp_hdr;
14513 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14514 struct hbq_dmabuf *seq_dmabuf = NULL;
14515
14516 /* Use the hdr_buf to find the sequence that matches this frame */
14517 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14518 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14519 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14520 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14521 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14522 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14523 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14524 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14525 continue;
14526 /* found a pending sequence that matches this frame */
14527 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14528 break;
14529 }
14530
14531 /* Free up all the frames from the partially assembled sequence */
14532 if (seq_dmabuf) {
14533 list_for_each_entry_safe(d_buf, n_buf,
14534 &seq_dmabuf->dbuf.list, list) {
14535 list_del_init(&d_buf->list);
14536 lpfc_in_buf_free(vport->phba, d_buf);
14537 }
14538 return true;
14539 }
14540 return false;
14541}
14542
14543/**
James Smart6dd9e312013-01-03 15:43:37 -050014544 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14545 * @vport: pointer to a vitural port
14546 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14547 *
14548 * This function tries to abort from the assembed sequence from upper level
14549 * protocol, described by the information from basic abbort @dmabuf. It
14550 * checks to see whether such pending context exists at upper level protocol.
14551 * If so, it shall clean up the pending context.
14552 *
14553 * Return
14554 * true -- if there is matching pending context of the sequence cleaned
14555 * at ulp;
14556 * false -- if there is no matching pending context of the sequence present
14557 * at ulp.
14558 **/
14559static bool
14560lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14561{
14562 struct lpfc_hba *phba = vport->phba;
14563 int handled;
14564
14565 /* Accepting abort at ulp with SLI4 only */
14566 if (phba->sli_rev < LPFC_SLI_REV4)
14567 return false;
14568
14569 /* Register all caring upper level protocols to attend abort */
14570 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14571 if (handled)
14572 return true;
14573
14574 return false;
14575}
14576
14577/**
James Smart546fc852011-03-11 16:06:29 -050014578 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014579 * @phba: Pointer to HBA context object.
14580 * @cmd_iocbq: pointer to the command iocbq structure.
14581 * @rsp_iocbq: pointer to the response iocbq structure.
14582 *
James Smart546fc852011-03-11 16:06:29 -050014583 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014584 * event. It properly releases the memory allocated to the sequence abort
14585 * accept iocb.
14586 **/
14587static void
James Smart546fc852011-03-11 16:06:29 -050014588lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014589 struct lpfc_iocbq *cmd_iocbq,
14590 struct lpfc_iocbq *rsp_iocbq)
14591{
James Smart6dd9e312013-01-03 15:43:37 -050014592 struct lpfc_nodelist *ndlp;
14593
14594 if (cmd_iocbq) {
14595 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14596 lpfc_nlp_put(ndlp);
14597 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014598 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050014599 }
James Smart6b5151f2012-01-18 16:24:06 -050014600
14601 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14602 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14604 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14605 rsp_iocbq->iocb.ulpStatus,
14606 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014607}
14608
14609/**
James Smart6d368e52011-05-24 11:44:12 -040014610 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14611 * @phba: Pointer to HBA context object.
14612 * @xri: xri id in transaction.
14613 *
14614 * This function validates the xri maps to the known range of XRIs allocated an
14615 * used by the driver.
14616 **/
James Smart7851fe22011-07-22 18:36:52 -040014617uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014618lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14619 uint16_t xri)
14620{
14621 int i;
14622
14623 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14624 if (xri == phba->sli4_hba.xri_ids[i])
14625 return i;
14626 }
14627 return NO_XRI;
14628}
14629
James Smart6d368e52011-05-24 11:44:12 -040014630/**
James Smart546fc852011-03-11 16:06:29 -050014631 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014632 * @phba: Pointer to HBA context object.
14633 * @fc_hdr: pointer to a FC frame header.
14634 *
James Smart546fc852011-03-11 16:06:29 -050014635 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014636 * event after aborting the sequence handling.
14637 **/
14638static void
James Smart6dd9e312013-01-03 15:43:37 -050014639lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14640 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040014641{
James Smart6dd9e312013-01-03 15:43:37 -050014642 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040014643 struct lpfc_iocbq *ctiocb = NULL;
14644 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014645 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014646 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014647 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014648 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014649
14650 if (!lpfc_is_link_up(phba))
14651 return;
14652
14653 sid = sli4_sid_from_fc_hdr(fc_hdr);
14654 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014655 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014656
James Smart6dd9e312013-01-03 15:43:37 -050014657 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040014658 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050014659 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14660 if (!ndlp) {
14661 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14662 "1268 Failed to allocate ndlp for "
14663 "oxid:x%x SID:x%x\n", oxid, sid);
14664 return;
14665 }
14666 lpfc_nlp_init(vport, ndlp, sid);
14667 /* Put ndlp onto pport node list */
14668 lpfc_enqueue_node(vport, ndlp);
14669 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14670 /* re-setup ndlp without removing from node list */
14671 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14672 if (!ndlp) {
14673 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14674 "3275 Failed to active ndlp found "
14675 "for oxid:x%x SID:x%x\n", oxid, sid);
14676 return;
14677 }
James Smart6669f9b2009-10-02 15:16:45 -040014678 }
14679
James Smart546fc852011-03-11 16:06:29 -050014680 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014681 ctiocb = lpfc_sli_get_iocbq(phba);
14682 if (!ctiocb)
14683 return;
14684
James Smart5ffc2662009-11-18 15:39:44 -050014685 /* Extract the F_CTL field from FC_HDR */
14686 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14687
James Smart6669f9b2009-10-02 15:16:45 -040014688 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014689 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014690 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014691 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14692 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14693 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14694
14695 /* Fill in the rest of iocb fields */
14696 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14697 icmd->ulpBdeCount = 0;
14698 icmd->ulpLe = 1;
14699 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014700 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050014701 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014702
James Smart6669f9b2009-10-02 15:16:45 -040014703 ctiocb->iocb_cmpl = NULL;
14704 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014705 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014706 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014707 ctiocb->sli4_xritag = NO_XRI;
14708
James Smartee0f4fe2012-05-09 21:19:14 -040014709 if (fctl & FC_FC_EX_CTX)
14710 /* Exchange responder sent the abort so we
14711 * own the oxid.
14712 */
14713 xri = oxid;
14714 else
14715 xri = rxid;
14716 lxri = lpfc_sli4_xri_inrange(phba, xri);
14717 if (lxri != NO_XRI)
14718 lpfc_set_rrq_active(phba, ndlp, lxri,
14719 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050014720 /* For BA_ABTS from exchange responder, if the logical xri with
14721 * the oxid maps to the FCP XRI range, the port no longer has
14722 * that exchange context, send a BLS_RJT. Override the IOCB for
14723 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050014724 */
James Smart6dd9e312013-01-03 15:43:37 -050014725 if ((fctl & FC_FC_EX_CTX) &&
14726 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14727 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14728 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14729 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14730 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14731 }
14732
14733 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14734 * the driver no longer has that exchange, send a BLS_RJT. Override
14735 * the IOCB for a BA_RJT.
14736 */
14737 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050014738 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14739 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14740 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14741 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14742 }
James Smart6669f9b2009-10-02 15:16:45 -040014743
James Smart5ffc2662009-11-18 15:39:44 -050014744 if (fctl & FC_FC_EX_CTX) {
14745 /* ABTS sent by responder to CT exchange, construction
14746 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14747 * field and RX_ID from ABTS for RX_ID field.
14748 */
James Smart546fc852011-03-11 16:06:29 -050014749 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014750 } else {
14751 /* ABTS sent by initiator to CT exchange, construction
14752 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014753 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014754 */
James Smart546fc852011-03-11 16:06:29 -050014755 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014756 }
James Smartf09c3ac2012-03-01 22:33:29 -050014757 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014758 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014759
James Smart546fc852011-03-11 16:06:29 -050014760 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050014761 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14762 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14763 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050014764
14765 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14766 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050014767 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14768 "2925 Failed to issue CT ABTS RSP x%x on "
14769 "xri x%x, Data x%x\n",
14770 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14771 phba->link_state);
14772 lpfc_nlp_put(ndlp);
14773 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050014774 lpfc_sli_release_iocbq(phba, ctiocb);
14775 }
James Smart6669f9b2009-10-02 15:16:45 -040014776}
14777
14778/**
14779 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14780 * @vport: Pointer to the vport on which this sequence was received
14781 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14782 *
14783 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14784 * receive sequence is only partially assembed by the driver, it shall abort
14785 * the partially assembled frames for the sequence. Otherwise, if the
14786 * unsolicited receive sequence has been completely assembled and passed to
14787 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14788 * unsolicited sequence has been aborted. After that, it will issue a basic
14789 * accept to accept the abort.
14790 **/
14791void
14792lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14793 struct hbq_dmabuf *dmabuf)
14794{
14795 struct lpfc_hba *phba = vport->phba;
14796 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014797 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050014798 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040014799
James Smart6669f9b2009-10-02 15:16:45 -040014800 /* Make a copy of fc_hdr before the dmabuf being released */
14801 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014802 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014803
James Smart5ffc2662009-11-18 15:39:44 -050014804 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050014805 /* ABTS by responder to exchange, no cleanup needed */
14806 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050014807 } else {
James Smart6dd9e312013-01-03 15:43:37 -050014808 /* ABTS by initiator to exchange, need to do cleanup */
14809 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14810 if (aborted == false)
14811 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050014812 }
James Smart6dd9e312013-01-03 15:43:37 -050014813 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14814
14815 /* Respond with BA_ACC or BA_RJT accordingly */
14816 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040014817}
14818
14819/**
James Smart4f774512009-05-22 14:52:35 -040014820 * lpfc_seq_complete - Indicates if a sequence is complete
14821 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14822 *
14823 * This function checks the sequence, starting with the frame described by
14824 * @dmabuf, to see if all the frames associated with this sequence are present.
14825 * the frames associated with this sequence are linked to the @dmabuf using the
14826 * dbuf list. This function looks for two major things. 1) That the first frame
14827 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14828 * set. 3) That there are no holes in the sequence count. The function will
14829 * return 1 when the sequence is complete, otherwise it will return 0.
14830 **/
14831static int
14832lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14833{
14834 struct fc_frame_header *hdr;
14835 struct lpfc_dmabuf *d_buf;
14836 struct hbq_dmabuf *seq_dmabuf;
14837 uint32_t fctl;
14838 int seq_count = 0;
14839
14840 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14841 /* make sure first fame of sequence has a sequence count of zero */
14842 if (hdr->fh_seq_cnt != seq_count)
14843 return 0;
14844 fctl = (hdr->fh_f_ctl[0] << 16 |
14845 hdr->fh_f_ctl[1] << 8 |
14846 hdr->fh_f_ctl[2]);
14847 /* If last frame of sequence we can return success. */
14848 if (fctl & FC_FC_END_SEQ)
14849 return 1;
14850 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14851 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14852 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14853 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014854 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014855 return 0;
14856 fctl = (hdr->fh_f_ctl[0] << 16 |
14857 hdr->fh_f_ctl[1] << 8 |
14858 hdr->fh_f_ctl[2]);
14859 /* If last frame of sequence we can return success. */
14860 if (fctl & FC_FC_END_SEQ)
14861 return 1;
14862 }
14863 return 0;
14864}
14865
14866/**
14867 * lpfc_prep_seq - Prep sequence for ULP processing
14868 * @vport: Pointer to the vport on which this sequence was received
14869 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14870 *
14871 * This function takes a sequence, described by a list of frames, and creates
14872 * a list of iocbq structures to describe the sequence. This iocbq list will be
14873 * used to issue to the generic unsolicited sequence handler. This routine
14874 * returns a pointer to the first iocbq in the list. If the function is unable
14875 * to allocate an iocbq then it throw out the received frames that were not
14876 * able to be described and return a pointer to the first iocbq. If unable to
14877 * allocate any iocbqs (including the first) this function will return NULL.
14878 **/
14879static struct lpfc_iocbq *
14880lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14881{
James Smart7851fe22011-07-22 18:36:52 -040014882 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014883 struct lpfc_dmabuf *d_buf, *n_buf;
14884 struct lpfc_iocbq *first_iocbq, *iocbq;
14885 struct fc_frame_header *fc_hdr;
14886 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014887 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014888 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014889
14890 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14891 /* remove from receive buffer list */
14892 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014893 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014894 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014895 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014896 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014897 /* Get an iocbq struct to fill in. */
14898 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14899 if (first_iocbq) {
14900 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014901 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014902 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014903
14904 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14905 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14906 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14907 first_iocbq->iocb.un.rcvels.parmRo =
14908 sli4_did_from_fc_hdr(fc_hdr);
14909 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14910 } else
14911 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014912 first_iocbq->iocb.ulpContext = NO_XRI;
14913 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14914 be16_to_cpu(fc_hdr->fh_ox_id);
14915 /* iocbq is prepped for internal consumption. Physical vpi. */
14916 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14917 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014918 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040014919 tot_len = bf_get(lpfc_rcqe_length,
14920 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14921
James Smart4f774512009-05-22 14:52:35 -040014922 first_iocbq->context2 = &seq_dmabuf->dbuf;
14923 first_iocbq->context3 = NULL;
14924 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040014925 if (tot_len > LPFC_DATA_BUF_SIZE)
14926 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040014927 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040014928 else
14929 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
14930
James Smart4f774512009-05-22 14:52:35 -040014931 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040014932
James Smart7851fe22011-07-22 18:36:52 -040014933 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014934 }
14935 iocbq = first_iocbq;
14936 /*
14937 * Each IOCBq can have two Buffers assigned, so go through the list
14938 * of buffers for this sequence and save two buffers in each IOCBq
14939 */
14940 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14941 if (!iocbq) {
14942 lpfc_in_buf_free(vport->phba, d_buf);
14943 continue;
14944 }
14945 if (!iocbq->context3) {
14946 iocbq->context3 = d_buf;
14947 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040014948 /* We need to get the size out of the right CQE */
14949 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14950 len = bf_get(lpfc_rcqe_length,
14951 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040014952 pbde = (struct ulp_bde64 *)
14953 &iocbq->iocb.unsli3.sli3Words[4];
14954 if (len > LPFC_DATA_BUF_SIZE)
14955 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14956 else
14957 pbde->tus.f.bdeSize = len;
14958
James Smart7851fe22011-07-22 18:36:52 -040014959 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14960 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014961 } else {
14962 iocbq = lpfc_sli_get_iocbq(vport->phba);
14963 if (!iocbq) {
14964 if (first_iocbq) {
14965 first_iocbq->iocb.ulpStatus =
14966 IOSTAT_FCP_RSP_ERROR;
14967 first_iocbq->iocb.un.ulpWord[4] =
14968 IOERR_NO_RESOURCES;
14969 }
14970 lpfc_in_buf_free(vport->phba, d_buf);
14971 continue;
14972 }
James Smart7851fe22011-07-22 18:36:52 -040014973 /* We need to get the size out of the right CQE */
14974 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14975 len = bf_get(lpfc_rcqe_length,
14976 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040014977 iocbq->context2 = d_buf;
14978 iocbq->context3 = NULL;
14979 iocbq->iocb.ulpBdeCount = 1;
14980 if (len > LPFC_DATA_BUF_SIZE)
14981 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14982 LPFC_DATA_BUF_SIZE;
14983 else
14984 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
14985
James Smart7851fe22011-07-22 18:36:52 -040014986 tot_len += len;
14987 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14988
James Smart4f774512009-05-22 14:52:35 -040014989 iocbq->iocb.un.rcvels.remoteID = sid;
14990 list_add_tail(&iocbq->list, &first_iocbq->list);
14991 }
14992 }
14993 return first_iocbq;
14994}
14995
James Smart6669f9b2009-10-02 15:16:45 -040014996static void
14997lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14998 struct hbq_dmabuf *seq_dmabuf)
14999{
15000 struct fc_frame_header *fc_hdr;
15001 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15002 struct lpfc_hba *phba = vport->phba;
15003
15004 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15005 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15006 if (!iocbq) {
15007 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15008 "2707 Ring %d handler: Failed to allocate "
15009 "iocb Rctl x%x Type x%x received\n",
15010 LPFC_ELS_RING,
15011 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15012 return;
15013 }
15014 if (!lpfc_complete_unsol_iocb(phba,
15015 &phba->sli.ring[LPFC_ELS_RING],
15016 iocbq, fc_hdr->fh_r_ctl,
15017 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015019 "2540 Ring %d handler: unexpected Rctl "
15020 "x%x Type x%x received\n",
15021 LPFC_ELS_RING,
15022 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15023
15024 /* Free iocb created in lpfc_prep_seq */
15025 list_for_each_entry_safe(curr_iocb, next_iocb,
15026 &iocbq->list, list) {
15027 list_del_init(&curr_iocb->list);
15028 lpfc_sli_release_iocbq(phba, curr_iocb);
15029 }
15030 lpfc_sli_release_iocbq(phba, iocbq);
15031}
15032
James Smart4f774512009-05-22 14:52:35 -040015033/**
15034 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15035 * @phba: Pointer to HBA context object.
15036 *
15037 * This function is called with no lock held. This function processes all
15038 * the received buffers and gives it to upper layers when a received buffer
15039 * indicates that it is the final frame in the sequence. The interrupt
15040 * service routine processes received buffers at interrupt contexts and adds
15041 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15042 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15043 * appropriate receive function when the final frame in a sequence is received.
15044 **/
James Smart4d9ab992009-10-02 15:16:39 -040015045void
15046lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15047 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015048{
James Smart4d9ab992009-10-02 15:16:39 -040015049 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015050 struct fc_frame_header *fc_hdr;
15051 struct lpfc_vport *vport;
15052 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015053 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015054
James Smart4f774512009-05-22 14:52:35 -040015055 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015056 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15057 /* check to see if this a valid type of frame */
15058 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15059 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15060 return;
15061 }
James Smart7851fe22011-07-22 18:36:52 -040015062 if ((bf_get(lpfc_cqe_code,
15063 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15064 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15065 &dmabuf->cq_event.cqe.rcqe_cmpl);
15066 else
15067 fcfi = bf_get(lpfc_rcqe_fcf_id,
15068 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015069
James Smart4d9ab992009-10-02 15:16:39 -040015070 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015071 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015072 /* throw out the frame */
15073 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15074 return;
15075 }
James Smart939723a2012-05-09 21:19:03 -040015076
15077 /* d_id this frame is directed to */
15078 did = sli4_did_from_fc_hdr(fc_hdr);
15079
15080 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15081 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15082 (did != Fabric_DID)) {
15083 /*
15084 * Throw out the frame if we are not pt2pt.
15085 * The pt2pt protocol allows for discovery frames
15086 * to be received without a registered VPI.
15087 */
15088 if (!(vport->fc_flag & FC_PT2PT) ||
15089 (phba->link_state == LPFC_HBA_READY)) {
15090 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15091 return;
15092 }
15093 }
15094
James Smart6669f9b2009-10-02 15:16:45 -040015095 /* Handle the basic abort sequence (BA_ABTS) event */
15096 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15097 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15098 return;
15099 }
15100
James Smart4d9ab992009-10-02 15:16:39 -040015101 /* Link this frame */
15102 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15103 if (!seq_dmabuf) {
15104 /* unable to add frame to vport - throw it out */
15105 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15106 return;
15107 }
15108 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015109 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015110 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015111
James Smart6669f9b2009-10-02 15:16:45 -040015112 /* Send the complete sequence to the upper layer protocol */
15113 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015114}
James Smart6fb120a2009-05-22 14:52:59 -040015115
15116/**
15117 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15118 * @phba: pointer to lpfc hba data structure.
15119 *
15120 * This routine is invoked to post rpi header templates to the
15121 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015122 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15123 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015124 *
15125 * This routine does not require any locks. It's usage is expected
15126 * to be driver load or reset recovery when the driver is
15127 * sequential.
15128 *
15129 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015130 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015131 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015132 * When this error occurs, the driver is not guaranteed
15133 * to have any rpi regions posted to the device and
15134 * must either attempt to repost the regions or take a
15135 * fatal error.
15136 **/
15137int
15138lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15139{
15140 struct lpfc_rpi_hdr *rpi_page;
15141 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015142 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015143
James Smart6d368e52011-05-24 11:44:12 -040015144 /* SLI4 ports that support extents do not require RPI headers. */
15145 if (!phba->sli4_hba.rpi_hdrs_in_use)
15146 goto exit;
15147 if (phba->sli4_hba.extents_in_use)
15148 return -EIO;
15149
James Smart6fb120a2009-05-22 14:52:59 -040015150 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015151 /*
15152 * Assign the rpi headers a physical rpi only if the driver
15153 * has not initialized those resources. A port reset only
15154 * needs the headers posted.
15155 */
15156 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15157 LPFC_RPI_RSRC_RDY)
15158 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15159
James Smart6fb120a2009-05-22 14:52:59 -040015160 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15161 if (rc != MBX_SUCCESS) {
15162 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15163 "2008 Error %d posting all rpi "
15164 "headers\n", rc);
15165 rc = -EIO;
15166 break;
15167 }
15168 }
15169
James Smart6d368e52011-05-24 11:44:12 -040015170 exit:
15171 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15172 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015173 return rc;
15174}
15175
15176/**
15177 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15178 * @phba: pointer to lpfc hba data structure.
15179 * @rpi_page: pointer to the rpi memory region.
15180 *
15181 * This routine is invoked to post a single rpi header to the
15182 * HBA consistent with the SLI-4 interface spec. This memory region
15183 * maps up to 64 rpi context regions.
15184 *
15185 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015186 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015187 * -ENOMEM - No available memory
15188 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015189 **/
15190int
15191lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15192{
15193 LPFC_MBOXQ_t *mboxq;
15194 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15195 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015196 uint32_t shdr_status, shdr_add_status;
15197 union lpfc_sli4_cfg_shdr *shdr;
15198
James Smart6d368e52011-05-24 11:44:12 -040015199 /* SLI4 ports that support extents do not require RPI headers. */
15200 if (!phba->sli4_hba.rpi_hdrs_in_use)
15201 return rc;
15202 if (phba->sli4_hba.extents_in_use)
15203 return -EIO;
15204
James Smart6fb120a2009-05-22 14:52:59 -040015205 /* The port is notified of the header region via a mailbox command. */
15206 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15207 if (!mboxq) {
15208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15209 "2001 Unable to allocate memory for issuing "
15210 "SLI_CONFIG_SPECIAL mailbox command\n");
15211 return -ENOMEM;
15212 }
15213
15214 /* Post all rpi memory regions to the port. */
15215 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015216 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15217 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15218 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015219 sizeof(struct lpfc_sli4_cfg_mhdr),
15220 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015221
15222
15223 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015224 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15225 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015226 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15227 hdr_tmpl, rpi_page->page_count);
15228
James Smart6fb120a2009-05-22 14:52:59 -040015229 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15230 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015231 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015232 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15233 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15234 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15235 if (rc != MBX_TIMEOUT)
15236 mempool_free(mboxq, phba->mbox_mem_pool);
15237 if (shdr_status || shdr_add_status || rc) {
15238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15239 "2514 POST_RPI_HDR mailbox failed with "
15240 "status x%x add_status x%x, mbx status x%x\n",
15241 shdr_status, shdr_add_status, rc);
15242 rc = -ENXIO;
15243 }
15244 return rc;
15245}
15246
15247/**
15248 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15249 * @phba: pointer to lpfc hba data structure.
15250 *
15251 * This routine is invoked to post rpi header templates to the
15252 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015253 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15254 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015255 *
15256 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015257 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015258 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15259 **/
15260int
15261lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15262{
James Smart6d368e52011-05-24 11:44:12 -040015263 unsigned long rpi;
15264 uint16_t max_rpi, rpi_limit;
15265 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015266 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015267 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015268
15269 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040015270 rpi_limit = phba->sli4_hba.next_rpi;
15271
15272 /*
James Smart6d368e52011-05-24 11:44:12 -040015273 * Fetch the next logical rpi. Because this index is logical,
15274 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015275 */
James Smart4902b382013-10-10 12:20:35 -040015276 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015277 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15278 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015279 rpi = LPFC_RPI_ALLOC_ERROR;
15280 else {
15281 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15282 phba->sli4_hba.max_cfg_param.rpi_used++;
15283 phba->sli4_hba.rpi_count++;
15284 }
15285
15286 /*
15287 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015288 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015289 */
15290 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15291 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015292 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015293 return rpi;
15294 }
15295
15296 /*
James Smart6d368e52011-05-24 11:44:12 -040015297 * RPI header postings are not required for SLI4 ports capable of
15298 * extents.
15299 */
15300 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015301 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015302 return rpi;
15303 }
15304
15305 /*
James Smart6fb120a2009-05-22 14:52:59 -040015306 * If the driver is running low on rpi resources, allocate another
15307 * page now. Note that the next_rpi value is used because
15308 * it represents how many are actually in use whereas max_rpi notes
15309 * how many are supported max by the device.
15310 */
James Smart6d368e52011-05-24 11:44:12 -040015311 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015312 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015313 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15314 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15315 if (!rpi_hdr) {
15316 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15317 "2002 Error Could not grow rpi "
15318 "count\n");
15319 } else {
James Smart6d368e52011-05-24 11:44:12 -040015320 lrpi = rpi_hdr->start_rpi;
15321 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015322 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15323 }
15324 }
15325
15326 return rpi;
15327}
15328
15329/**
15330 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15331 * @phba: pointer to lpfc hba data structure.
15332 *
15333 * This routine is invoked to release an rpi to the pool of
15334 * available rpis maintained by the driver.
15335 **/
15336void
James Smartd7c47992010-06-08 18:31:54 -040015337__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15338{
15339 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15340 phba->sli4_hba.rpi_count--;
15341 phba->sli4_hba.max_cfg_param.rpi_used--;
15342 }
15343}
15344
15345/**
15346 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15347 * @phba: pointer to lpfc hba data structure.
15348 *
15349 * This routine is invoked to release an rpi to the pool of
15350 * available rpis maintained by the driver.
15351 **/
15352void
James Smart6fb120a2009-05-22 14:52:59 -040015353lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15354{
15355 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015356 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015357 spin_unlock_irq(&phba->hbalock);
15358}
15359
15360/**
15361 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15362 * @phba: pointer to lpfc hba data structure.
15363 *
15364 * This routine is invoked to remove the memory region that
15365 * provided rpi via a bitmask.
15366 **/
15367void
15368lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15369{
15370 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040015371 kfree(phba->sli4_hba.rpi_ids);
15372 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040015373}
15374
15375/**
15376 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15377 * @phba: pointer to lpfc hba data structure.
15378 *
15379 * This routine is invoked to remove the memory region that
15380 * provided rpi via a bitmask.
15381 **/
15382int
James Smart6b5151f2012-01-18 16:24:06 -050015383lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15384 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040015385{
15386 LPFC_MBOXQ_t *mboxq;
15387 struct lpfc_hba *phba = ndlp->phba;
15388 int rc;
15389
15390 /* The port is notified of the header region via a mailbox command. */
15391 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15392 if (!mboxq)
15393 return -ENOMEM;
15394
15395 /* Post all rpi memory regions to the port. */
15396 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050015397 if (cmpl) {
15398 mboxq->mbox_cmpl = cmpl;
15399 mboxq->context1 = arg;
15400 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050015401 } else
15402 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050015403 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040015404 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15405 if (rc == MBX_NOT_FINISHED) {
15406 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15407 "2010 Resume RPI Mailbox failed "
15408 "status %d, mbxStatus x%x\n", rc,
15409 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15410 mempool_free(mboxq, phba->mbox_mem_pool);
15411 return -EIO;
15412 }
15413 return 0;
15414}
15415
15416/**
15417 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050015418 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040015419 *
James Smart76a95d72010-11-20 23:11:48 -050015420 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040015421 *
15422 * Returns:
15423 * 0 success
15424 * -Evalue otherwise
15425 **/
15426int
James Smart76a95d72010-11-20 23:11:48 -050015427lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040015428{
15429 LPFC_MBOXQ_t *mboxq;
15430 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040015431 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040015432 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050015433 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040015434 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15435 if (!mboxq)
15436 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050015437 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040015438 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040015439 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040015440 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050015441 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040015442 "2022 INIT VPI Mailbox failed "
15443 "status %d, mbxStatus x%x\n", rc,
15444 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040015445 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040015446 }
James Smart6a9c52c2009-10-02 15:16:51 -040015447 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050015448 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040015449
15450 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040015451}
15452
15453/**
15454 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15455 * @phba: pointer to lpfc hba data structure.
15456 * @mboxq: Pointer to mailbox object.
15457 *
15458 * This routine is invoked to manually add a single FCF record. The caller
15459 * must pass a completely initialized FCF_Record. This routine takes
15460 * care of the nonembedded mailbox operations.
15461 **/
15462static void
15463lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15464{
15465 void *virt_addr;
15466 union lpfc_sli4_cfg_shdr *shdr;
15467 uint32_t shdr_status, shdr_add_status;
15468
15469 virt_addr = mboxq->sge_array->addr[0];
15470 /* The IOCTL status is embedded in the mailbox subheader. */
15471 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15472 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15473 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15474
15475 if ((shdr_status || shdr_add_status) &&
15476 (shdr_status != STATUS_FCF_IN_USE))
15477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15478 "2558 ADD_FCF_RECORD mailbox failed with "
15479 "status x%x add_status x%x\n",
15480 shdr_status, shdr_add_status);
15481
15482 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15483}
15484
15485/**
15486 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15487 * @phba: pointer to lpfc hba data structure.
15488 * @fcf_record: pointer to the initialized fcf record to add.
15489 *
15490 * This routine is invoked to manually add a single FCF record. The caller
15491 * must pass a completely initialized FCF_Record. This routine takes
15492 * care of the nonembedded mailbox operations.
15493 **/
15494int
15495lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15496{
15497 int rc = 0;
15498 LPFC_MBOXQ_t *mboxq;
15499 uint8_t *bytep;
15500 void *virt_addr;
15501 dma_addr_t phys_addr;
15502 struct lpfc_mbx_sge sge;
15503 uint32_t alloc_len, req_len;
15504 uint32_t fcfindex;
15505
15506 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15507 if (!mboxq) {
15508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15509 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15510 return -ENOMEM;
15511 }
15512
15513 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15514 sizeof(uint32_t);
15515
15516 /* Allocate DMA memory and set up the non-embedded mailbox command */
15517 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15518 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15519 req_len, LPFC_SLI4_MBX_NEMBED);
15520 if (alloc_len < req_len) {
15521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15522 "2523 Allocated DMA memory size (x%x) is "
15523 "less than the requested DMA memory "
15524 "size (x%x)\n", alloc_len, req_len);
15525 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15526 return -ENOMEM;
15527 }
15528
15529 /*
15530 * Get the first SGE entry from the non-embedded DMA memory. This
15531 * routine only uses a single SGE.
15532 */
15533 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15534 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040015535 virt_addr = mboxq->sge_array->addr[0];
15536 /*
15537 * Configure the FCF record for FCFI 0. This is the driver's
15538 * hardcoded default and gets used in nonFIP mode.
15539 */
15540 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15541 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15542 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15543
15544 /*
15545 * Copy the fcf_index and the FCF Record Data. The data starts after
15546 * the FCoE header plus word10. The data copy needs to be endian
15547 * correct.
15548 */
15549 bytep += sizeof(uint32_t);
15550 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15551 mboxq->vport = phba->pport;
15552 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15553 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15554 if (rc == MBX_NOT_FINISHED) {
15555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15556 "2515 ADD_FCF_RECORD mailbox failed with "
15557 "status 0x%x\n", rc);
15558 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15559 rc = -EIO;
15560 } else
15561 rc = 0;
15562
15563 return rc;
15564}
15565
15566/**
15567 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15568 * @phba: pointer to lpfc hba data structure.
15569 * @fcf_record: pointer to the fcf record to write the default data.
15570 * @fcf_index: FCF table entry index.
15571 *
15572 * This routine is invoked to build the driver's default FCF record. The
15573 * values used are hardcoded. This routine handles memory initialization.
15574 *
15575 **/
15576void
15577lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15578 struct fcf_record *fcf_record,
15579 uint16_t fcf_index)
15580{
15581 memset(fcf_record, 0, sizeof(struct fcf_record));
15582 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15583 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15584 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15585 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15586 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15587 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15588 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15589 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15590 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15591 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15592 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15593 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15594 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015595 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015596 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15597 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15598 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15599 /* Set the VLAN bit map */
15600 if (phba->valid_vlan) {
15601 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15602 = 1 << (phba->vlan_id % 8);
15603 }
15604}
15605
15606/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015607 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015608 * @phba: pointer to lpfc hba data structure.
15609 * @fcf_index: FCF table entry offset.
15610 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015611 * This routine is invoked to scan the entire FCF table by reading FCF
15612 * record and processing it one at a time starting from the @fcf_index
15613 * for initial FCF discovery or fast FCF failover rediscovery.
15614 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015615 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015616 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015617 **/
15618int
James Smart0c9ab6f2010-02-26 14:15:57 -050015619lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015620{
15621 int rc = 0, error;
15622 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015623
James Smart32b97932009-07-19 10:01:21 -040015624 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015625 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015626 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15627 if (!mboxq) {
15628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15629 "2000 Failed to allocate mbox for "
15630 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015631 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015632 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015633 }
James Smartecfd03c2010-02-12 14:41:27 -050015634 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015635 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015636 if (rc) {
15637 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015638 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015639 }
James Smartecfd03c2010-02-12 14:41:27 -050015640 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015641 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015642 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015643
15644 spin_lock_irq(&phba->hbalock);
15645 phba->hba_flag |= FCF_TS_INPROG;
15646 spin_unlock_irq(&phba->hbalock);
15647
James Smart6fb120a2009-05-22 14:52:59 -040015648 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015649 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015650 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015651 else {
James Smart38b92ef2010-08-04 16:11:39 -040015652 /* Reset eligible FCF count for new scan */
15653 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015654 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015655 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015656 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015657fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015658 if (error) {
15659 if (mboxq)
15660 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015661 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015662 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015663 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015664 spin_unlock_irq(&phba->hbalock);
15665 }
James Smart6fb120a2009-05-22 14:52:59 -040015666 return error;
15667}
James Smarta0c87cb2009-07-19 10:01:10 -040015668
15669/**
James Smarta93ff372010-10-22 11:06:08 -040015670 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015671 * @phba: pointer to lpfc hba data structure.
15672 * @fcf_index: FCF table entry offset.
15673 *
15674 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015675 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015676 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015677 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015678 * otherwise.
15679 **/
15680int
15681lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15682{
15683 int rc = 0, error;
15684 LPFC_MBOXQ_t *mboxq;
15685
15686 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15687 if (!mboxq) {
15688 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15689 "2763 Failed to allocate mbox for "
15690 "READ_FCF cmd\n");
15691 error = -ENOMEM;
15692 goto fail_fcf_read;
15693 }
15694 /* Construct the read FCF record mailbox command */
15695 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15696 if (rc) {
15697 error = -EINVAL;
15698 goto fail_fcf_read;
15699 }
15700 /* Issue the mailbox command asynchronously */
15701 mboxq->vport = phba->pport;
15702 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15703 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15704 if (rc == MBX_NOT_FINISHED)
15705 error = -EIO;
15706 else
15707 error = 0;
15708
15709fail_fcf_read:
15710 if (error && mboxq)
15711 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15712 return error;
15713}
15714
15715/**
15716 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15717 * @phba: pointer to lpfc hba data structure.
15718 * @fcf_index: FCF table entry offset.
15719 *
15720 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015721 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015722 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015723 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015724 * otherwise.
15725 **/
15726int
15727lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15728{
15729 int rc = 0, error;
15730 LPFC_MBOXQ_t *mboxq;
15731
15732 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15733 if (!mboxq) {
15734 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15735 "2758 Failed to allocate mbox for "
15736 "READ_FCF cmd\n");
15737 error = -ENOMEM;
15738 goto fail_fcf_read;
15739 }
15740 /* Construct the read FCF record mailbox command */
15741 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15742 if (rc) {
15743 error = -EINVAL;
15744 goto fail_fcf_read;
15745 }
15746 /* Issue the mailbox command asynchronously */
15747 mboxq->vport = phba->pport;
15748 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15749 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15750 if (rc == MBX_NOT_FINISHED)
15751 error = -EIO;
15752 else
15753 error = 0;
15754
15755fail_fcf_read:
15756 if (error && mboxq)
15757 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15758 return error;
15759}
15760
15761/**
James Smart7d791df2011-07-22 18:37:52 -040015762 * lpfc_check_next_fcf_pri
15763 * phba pointer to the lpfc_hba struct for this port.
15764 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15765 * routine when the rr_bmask is empty. The FCF indecies are put into the
15766 * rr_bmask based on their priority level. Starting from the highest priority
15767 * to the lowest. The most likely FCF candidate will be in the highest
15768 * priority group. When this routine is called it searches the fcf_pri list for
15769 * next lowest priority group and repopulates the rr_bmask with only those
15770 * fcf_indexes.
15771 * returns:
15772 * 1=success 0=failure
15773 **/
15774int
15775lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15776{
15777 uint16_t next_fcf_pri;
15778 uint16_t last_index;
15779 struct lpfc_fcf_pri *fcf_pri;
15780 int rc;
15781 int ret = 0;
15782
15783 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15784 LPFC_SLI4_FCF_TBL_INDX_MAX);
15785 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15786 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050015787
15788 /* Verify the priority list has 2 or more entries */
15789 spin_lock_irq(&phba->hbalock);
15790 if (list_empty(&phba->fcf.fcf_pri_list) ||
15791 list_is_singular(&phba->fcf.fcf_pri_list)) {
15792 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040015793 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15794 "3061 Last IDX %d\n", last_index);
15795 return 0; /* Empty rr list */
15796 }
James Smart25626692013-03-01 16:36:54 -050015797 spin_unlock_irq(&phba->hbalock);
15798
James Smart7d791df2011-07-22 18:37:52 -040015799 next_fcf_pri = 0;
15800 /*
15801 * Clear the rr_bmask and set all of the bits that are at this
15802 * priority.
15803 */
15804 memset(phba->fcf.fcf_rr_bmask, 0,
15805 sizeof(*phba->fcf.fcf_rr_bmask));
15806 spin_lock_irq(&phba->hbalock);
15807 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15808 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15809 continue;
15810 /*
15811 * the 1st priority that has not FLOGI failed
15812 * will be the highest.
15813 */
15814 if (!next_fcf_pri)
15815 next_fcf_pri = fcf_pri->fcf_rec.priority;
15816 spin_unlock_irq(&phba->hbalock);
15817 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15818 rc = lpfc_sli4_fcf_rr_index_set(phba,
15819 fcf_pri->fcf_rec.fcf_index);
15820 if (rc)
15821 return 0;
15822 }
15823 spin_lock_irq(&phba->hbalock);
15824 }
15825 /*
15826 * if next_fcf_pri was not set above and the list is not empty then
15827 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070015828 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040015829 */
15830 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15831 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15832 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15833 /*
15834 * the 1st priority that has not FLOGI failed
15835 * will be the highest.
15836 */
15837 if (!next_fcf_pri)
15838 next_fcf_pri = fcf_pri->fcf_rec.priority;
15839 spin_unlock_irq(&phba->hbalock);
15840 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15841 rc = lpfc_sli4_fcf_rr_index_set(phba,
15842 fcf_pri->fcf_rec.fcf_index);
15843 if (rc)
15844 return 0;
15845 }
15846 spin_lock_irq(&phba->hbalock);
15847 }
15848 } else
15849 ret = 1;
15850 spin_unlock_irq(&phba->hbalock);
15851
15852 return ret;
15853}
15854/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015855 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15856 * @phba: pointer to lpfc hba data structure.
15857 *
15858 * This routine is to get the next eligible FCF record index in a round
15859 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015860 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015861 * shall be returned, otherwise, the next eligible FCF record's index
15862 * shall be returned.
15863 **/
15864uint16_t
15865lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15866{
15867 uint16_t next_fcf_index;
15868
James Smart421c6622013-01-03 15:44:16 -050015869initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040015870 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050015871 next_fcf_index = phba->fcf.current_rec.fcf_indx;
15872
James Smart7d791df2011-07-22 18:37:52 -040015873next_priority:
James Smart421c6622013-01-03 15:44:16 -050015874 /* Determine the next fcf index to check */
15875 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015876 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15877 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015878 next_fcf_index);
15879
James Smart0c9ab6f2010-02-26 14:15:57 -050015880 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015881 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15882 /*
15883 * If we have wrapped then we need to clear the bits that
15884 * have been tested so that we can detect when we should
15885 * change the priority level.
15886 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015887 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15888 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015889 }
15890
James Smart0c9ab6f2010-02-26 14:15:57 -050015891
James Smart3804dc82010-07-14 15:31:37 -040015892 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015893 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15894 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15895 /*
15896 * If next fcf index is not found check if there are lower
15897 * Priority level fcf's in the fcf_priority list.
15898 * Set up the rr_bmask with all of the avaiable fcf bits
15899 * at that level and continue the selection process.
15900 */
15901 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050015902 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040015903 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15904 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015905 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15906 return LPFC_FCOE_FCF_NEXT_NONE;
15907 else {
15908 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15909 "3063 Only FCF available idx %d, flag %x\n",
15910 next_fcf_index,
15911 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15912 return next_fcf_index;
15913 }
James Smart3804dc82010-07-14 15:31:37 -040015914 }
15915
James Smart7d791df2011-07-22 18:37:52 -040015916 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15917 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15918 LPFC_FCF_FLOGI_FAILED)
15919 goto next_priority;
15920
James Smart3804dc82010-07-14 15:31:37 -040015921 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015922 "2845 Get next roundrobin failover FCF (x%x)\n",
15923 next_fcf_index);
15924
James Smart0c9ab6f2010-02-26 14:15:57 -050015925 return next_fcf_index;
15926}
15927
15928/**
15929 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15930 * @phba: pointer to lpfc hba data structure.
15931 *
15932 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015933 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015934 * does not go beyond the range of the driver allocated bmask dimension
15935 * before setting the bit.
15936 *
15937 * Returns 0 if the index bit successfully set, otherwise, it returns
15938 * -EINVAL.
15939 **/
15940int
15941lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15942{
15943 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15944 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015945 "2610 FCF (x%x) reached driver's book "
15946 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015947 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15948 return -EINVAL;
15949 }
15950 /* Set the eligible FCF record index bmask */
15951 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15952
James Smart3804dc82010-07-14 15:31:37 -040015953 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015954 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015955 "bmask\n", fcf_index);
15956
James Smart0c9ab6f2010-02-26 14:15:57 -050015957 return 0;
15958}
15959
15960/**
James Smart3804dc82010-07-14 15:31:37 -040015961 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015962 * @phba: pointer to lpfc hba data structure.
15963 *
15964 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015965 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015966 * does not go beyond the range of the driver allocated bmask dimension
15967 * before clearing the bit.
15968 **/
15969void
15970lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15971{
James Smart9a803a72013-09-06 12:17:56 -040015972 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050015973 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15974 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015975 "2762 FCF (x%x) reached driver's book "
15976 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015977 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15978 return;
15979 }
15980 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015981 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040015982 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
15983 list) {
James Smart7d791df2011-07-22 18:37:52 -040015984 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15985 list_del_init(&fcf_pri->list);
15986 break;
15987 }
15988 }
15989 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015990 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015991
15992 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015993 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015994 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015995}
15996
15997/**
James Smartecfd03c2010-02-12 14:41:27 -050015998 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15999 * @phba: pointer to lpfc hba data structure.
16000 *
16001 * This routine is the completion routine for the rediscover FCF table mailbox
16002 * command. If the mailbox command returned failure, it will try to stop the
16003 * FCF rediscover wait timer.
16004 **/
16005void
16006lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16007{
16008 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16009 uint32_t shdr_status, shdr_add_status;
16010
16011 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16012
16013 shdr_status = bf_get(lpfc_mbox_hdr_status,
16014 &redisc_fcf->header.cfg_shdr.response);
16015 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16016 &redisc_fcf->header.cfg_shdr.response);
16017 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016018 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016019 "2746 Requesting for FCF rediscovery failed "
16020 "status x%x add_status x%x\n",
16021 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016022 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016023 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016024 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016025 spin_unlock_irq(&phba->hbalock);
16026 /*
16027 * CVL event triggered FCF rediscover request failed,
16028 * last resort to re-try current registered FCF entry.
16029 */
16030 lpfc_retry_pport_discovery(phba);
16031 } else {
16032 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016033 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016034 spin_unlock_irq(&phba->hbalock);
16035 /*
16036 * DEAD FCF event triggered FCF rediscover request
16037 * failed, last resort to fail over as a link down
16038 * to FCF registration.
16039 */
16040 lpfc_sli4_fcf_dead_failthrough(phba);
16041 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016042 } else {
16043 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016044 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016045 /*
16046 * Start FCF rediscovery wait timer for pending FCF
16047 * before rescan FCF record table.
16048 */
16049 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016050 }
James Smartecfd03c2010-02-12 14:41:27 -050016051
16052 mempool_free(mbox, phba->mbox_mem_pool);
16053}
16054
16055/**
James Smart3804dc82010-07-14 15:31:37 -040016056 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016057 * @phba: pointer to lpfc hba data structure.
16058 *
16059 * This routine is invoked to request for rediscovery of the entire FCF table
16060 * by the port.
16061 **/
16062int
16063lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16064{
16065 LPFC_MBOXQ_t *mbox;
16066 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16067 int rc, length;
16068
James Smart0c9ab6f2010-02-26 14:15:57 -050016069 /* Cancel retry delay timers to all vports before FCF rediscover */
16070 lpfc_cancel_all_vport_retry_delay_timer(phba);
16071
James Smartecfd03c2010-02-12 14:41:27 -050016072 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16073 if (!mbox) {
16074 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16075 "2745 Failed to allocate mbox for "
16076 "requesting FCF rediscover.\n");
16077 return -ENOMEM;
16078 }
16079
16080 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16081 sizeof(struct lpfc_sli4_cfg_mhdr));
16082 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16083 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16084 length, LPFC_SLI4_MBX_EMBED);
16085
16086 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16087 /* Set count to 0 for invalidating the entire FCF database */
16088 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16089
16090 /* Issue the mailbox command asynchronously */
16091 mbox->vport = phba->pport;
16092 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16093 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16094
16095 if (rc == MBX_NOT_FINISHED) {
16096 mempool_free(mbox, phba->mbox_mem_pool);
16097 return -EIO;
16098 }
16099 return 0;
16100}
16101
16102/**
James Smartfc2b9892010-02-26 14:15:29 -050016103 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16104 * @phba: pointer to lpfc hba data structure.
16105 *
16106 * This function is the failover routine as a last resort to the FCF DEAD
16107 * event when driver failed to perform fast FCF failover.
16108 **/
16109void
16110lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16111{
16112 uint32_t link_state;
16113
16114 /*
16115 * Last resort as FCF DEAD event failover will treat this as
16116 * a link down, but save the link state because we don't want
16117 * it to be changed to Link Down unless it is already down.
16118 */
16119 link_state = phba->link_state;
16120 lpfc_linkdown(phba);
16121 phba->link_state = link_state;
16122
16123 /* Unregister FCF if no devices connected to it */
16124 lpfc_unregister_unused_fcf(phba);
16125}
16126
16127/**
James Smart026abb82011-12-13 13:20:45 -050016128 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016129 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016130 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016131 *
James Smart026abb82011-12-13 13:20:45 -050016132 * This function gets SLI3 port configure region 23 data through memory dump
16133 * mailbox command. When it successfully retrieves data, the size of the data
16134 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016135 **/
James Smart026abb82011-12-13 13:20:45 -050016136static uint32_t
16137lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016138{
16139 LPFC_MBOXQ_t *pmb = NULL;
16140 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016141 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016142 int rc;
16143
James Smart026abb82011-12-13 13:20:45 -050016144 if (!rgn23_data)
16145 return 0;
16146
James Smarta0c87cb2009-07-19 10:01:10 -040016147 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16148 if (!pmb) {
16149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016150 "2600 failed to allocate mailbox memory\n");
16151 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016152 }
16153 mb = &pmb->u.mb;
16154
James Smarta0c87cb2009-07-19 10:01:10 -040016155 do {
16156 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16157 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16158
16159 if (rc != MBX_SUCCESS) {
16160 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016161 "2601 failed to read config "
16162 "region 23, rc 0x%x Status 0x%x\n",
16163 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016164 mb->un.varDmp.word_cnt = 0;
16165 }
16166 /*
16167 * dump mem may return a zero when finished or we got a
16168 * mailbox error, either way we are done.
16169 */
16170 if (mb->un.varDmp.word_cnt == 0)
16171 break;
16172 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16173 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16174
16175 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016176 rgn23_data + offset,
16177 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016178 offset += mb->un.varDmp.word_cnt;
16179 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16180
James Smart026abb82011-12-13 13:20:45 -050016181 mempool_free(pmb, phba->mbox_mem_pool);
16182 return offset;
16183}
16184
16185/**
16186 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16187 * @phba: pointer to lpfc hba data structure.
16188 * @rgn23_data: pointer to configure region 23 data.
16189 *
16190 * This function gets SLI4 port configure region 23 data through memory dump
16191 * mailbox command. When it successfully retrieves data, the size of the data
16192 * will be returned, otherwise, 0 will be returned.
16193 **/
16194static uint32_t
16195lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16196{
16197 LPFC_MBOXQ_t *mboxq = NULL;
16198 struct lpfc_dmabuf *mp = NULL;
16199 struct lpfc_mqe *mqe;
16200 uint32_t data_length = 0;
16201 int rc;
16202
16203 if (!rgn23_data)
16204 return 0;
16205
16206 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16207 if (!mboxq) {
16208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16209 "3105 failed to allocate mailbox memory\n");
16210 return 0;
16211 }
16212
16213 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16214 goto out;
16215 mqe = &mboxq->u.mqe;
16216 mp = (struct lpfc_dmabuf *) mboxq->context1;
16217 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16218 if (rc)
16219 goto out;
16220 data_length = mqe->un.mb_words[5];
16221 if (data_length == 0)
16222 goto out;
16223 if (data_length > DMP_RGN23_SIZE) {
16224 data_length = 0;
16225 goto out;
16226 }
16227 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16228out:
16229 mempool_free(mboxq, phba->mbox_mem_pool);
16230 if (mp) {
16231 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16232 kfree(mp);
16233 }
16234 return data_length;
16235}
16236
16237/**
16238 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16239 * @phba: pointer to lpfc hba data structure.
16240 *
16241 * This function read region 23 and parse TLV for port status to
16242 * decide if the user disaled the port. If the TLV indicates the
16243 * port is disabled, the hba_flag is set accordingly.
16244 **/
16245void
16246lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16247{
16248 uint8_t *rgn23_data = NULL;
16249 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16250 uint32_t offset = 0;
16251
16252 /* Get adapter Region 23 data */
16253 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16254 if (!rgn23_data)
16255 goto out;
16256
16257 if (phba->sli_rev < LPFC_SLI_REV4)
16258 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16259 else {
16260 if_type = bf_get(lpfc_sli_intf_if_type,
16261 &phba->sli4_hba.sli_intf);
16262 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16263 goto out;
16264 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16265 }
James Smarta0c87cb2009-07-19 10:01:10 -040016266
16267 if (!data_size)
16268 goto out;
16269
16270 /* Check the region signature first */
16271 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16273 "2619 Config region 23 has bad signature\n");
16274 goto out;
16275 }
16276 offset += 4;
16277
16278 /* Check the data structure version */
16279 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16281 "2620 Config region 23 has bad version\n");
16282 goto out;
16283 }
16284 offset += 4;
16285
16286 /* Parse TLV entries in the region */
16287 while (offset < data_size) {
16288 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16289 break;
16290 /*
16291 * If the TLV is not driver specific TLV or driver id is
16292 * not linux driver id, skip the record.
16293 */
16294 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16295 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16296 (rgn23_data[offset + 3] != 0)) {
16297 offset += rgn23_data[offset + 1] * 4 + 4;
16298 continue;
16299 }
16300
16301 /* Driver found a driver specific TLV in the config region */
16302 sub_tlv_len = rgn23_data[offset + 1] * 4;
16303 offset += 4;
16304 tlv_offset = 0;
16305
16306 /*
16307 * Search for configured port state sub-TLV.
16308 */
16309 while ((offset < data_size) &&
16310 (tlv_offset < sub_tlv_len)) {
16311 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16312 offset += 4;
16313 tlv_offset += 4;
16314 break;
16315 }
16316 if (rgn23_data[offset] != PORT_STE_TYPE) {
16317 offset += rgn23_data[offset + 1] * 4 + 4;
16318 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16319 continue;
16320 }
16321
16322 /* This HBA contains PORT_STE configured */
16323 if (!rgn23_data[offset + 2])
16324 phba->hba_flag |= LINK_DISABLED;
16325
16326 goto out;
16327 }
16328 }
James Smart026abb82011-12-13 13:20:45 -050016329
James Smarta0c87cb2009-07-19 10:01:10 -040016330out:
James Smarta0c87cb2009-07-19 10:01:10 -040016331 kfree(rgn23_data);
16332 return;
16333}
James Smart695a8142010-01-26 23:08:03 -050016334
16335/**
James Smart52d52442011-05-24 11:42:45 -040016336 * lpfc_wr_object - write an object to the firmware
16337 * @phba: HBA structure that indicates port to create a queue on.
16338 * @dmabuf_list: list of dmabufs to write to the port.
16339 * @size: the total byte value of the objects to write to the port.
16340 * @offset: the current offset to be used to start the transfer.
16341 *
16342 * This routine will create a wr_object mailbox command to send to the port.
16343 * the mailbox command will be constructed using the dma buffers described in
16344 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16345 * BDEs that the imbedded mailbox can support. The @offset variable will be
16346 * used to indicate the starting offset of the transfer and will also return
16347 * the offset after the write object mailbox has completed. @size is used to
16348 * determine the end of the object and whether the eof bit should be set.
16349 *
16350 * Return 0 is successful and offset will contain the the new offset to use
16351 * for the next write.
16352 * Return negative value for error cases.
16353 **/
16354int
16355lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16356 uint32_t size, uint32_t *offset)
16357{
16358 struct lpfc_mbx_wr_object *wr_object;
16359 LPFC_MBOXQ_t *mbox;
16360 int rc = 0, i = 0;
16361 uint32_t shdr_status, shdr_add_status;
16362 uint32_t mbox_tmo;
16363 union lpfc_sli4_cfg_shdr *shdr;
16364 struct lpfc_dmabuf *dmabuf;
16365 uint32_t written = 0;
16366
16367 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16368 if (!mbox)
16369 return -ENOMEM;
16370
16371 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16372 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16373 sizeof(struct lpfc_mbx_wr_object) -
16374 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16375
16376 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16377 wr_object->u.request.write_offset = *offset;
16378 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16379 wr_object->u.request.object_name[0] =
16380 cpu_to_le32(wr_object->u.request.object_name[0]);
16381 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16382 list_for_each_entry(dmabuf, dmabuf_list, list) {
16383 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16384 break;
16385 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16386 wr_object->u.request.bde[i].addrHigh =
16387 putPaddrHigh(dmabuf->phys);
16388 if (written + SLI4_PAGE_SIZE >= size) {
16389 wr_object->u.request.bde[i].tus.f.bdeSize =
16390 (size - written);
16391 written += (size - written);
16392 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16393 } else {
16394 wr_object->u.request.bde[i].tus.f.bdeSize =
16395 SLI4_PAGE_SIZE;
16396 written += SLI4_PAGE_SIZE;
16397 }
16398 i++;
16399 }
16400 wr_object->u.request.bde_count = i;
16401 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16402 if (!phba->sli4_hba.intr_enable)
16403 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16404 else {
James Smarta183a152011-10-10 21:32:43 -040016405 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040016406 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16407 }
16408 /* The IOCTL status is embedded in the mailbox subheader. */
16409 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16410 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16411 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16412 if (rc != MBX_TIMEOUT)
16413 mempool_free(mbox, phba->mbox_mem_pool);
16414 if (shdr_status || shdr_add_status || rc) {
16415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16416 "3025 Write Object mailbox failed with "
16417 "status x%x add_status x%x, mbx status x%x\n",
16418 shdr_status, shdr_add_status, rc);
16419 rc = -ENXIO;
16420 } else
16421 *offset += wr_object->u.response.actual_write_length;
16422 return rc;
16423}
16424
16425/**
James Smart695a8142010-01-26 23:08:03 -050016426 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16427 * @vport: pointer to vport data structure.
16428 *
16429 * This function iterate through the mailboxq and clean up all REG_LOGIN
16430 * and REG_VPI mailbox commands associated with the vport. This function
16431 * is called when driver want to restart discovery of the vport due to
16432 * a Clear Virtual Link event.
16433 **/
16434void
16435lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16436{
16437 struct lpfc_hba *phba = vport->phba;
16438 LPFC_MBOXQ_t *mb, *nextmb;
16439 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040016440 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040016441 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040016442 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040016443 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050016444 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050016445
James Smartd439d282010-09-29 11:18:45 -040016446 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050016447 spin_lock_irq(&phba->hbalock);
16448 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16449 if (mb->vport != vport)
16450 continue;
16451
16452 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16453 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16454 continue;
16455
James Smartd439d282010-09-29 11:18:45 -040016456 list_del(&mb->list);
16457 list_add_tail(&mb->list, &mbox_cmd_list);
16458 }
16459 /* Clean up active mailbox command with the vport */
16460 mb = phba->sli.mbox_active;
16461 if (mb && (mb->vport == vport)) {
16462 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16463 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16464 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16465 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16466 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16467 /* Put reference count for delayed processing */
16468 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16469 /* Unregister the RPI when mailbox complete */
16470 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16471 }
16472 }
James Smart63e801c2010-11-20 23:14:19 -050016473 /* Cleanup any mailbox completions which are not yet processed */
16474 do {
16475 restart_loop = 0;
16476 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16477 /*
16478 * If this mailox is already processed or it is
16479 * for another vport ignore it.
16480 */
16481 if ((mb->vport != vport) ||
16482 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16483 continue;
16484
16485 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16486 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16487 continue;
16488
16489 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16490 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16491 ndlp = (struct lpfc_nodelist *)mb->context2;
16492 /* Unregister the RPI when mailbox complete */
16493 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16494 restart_loop = 1;
16495 spin_unlock_irq(&phba->hbalock);
16496 spin_lock(shost->host_lock);
16497 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16498 spin_unlock(shost->host_lock);
16499 spin_lock_irq(&phba->hbalock);
16500 break;
16501 }
16502 }
16503 } while (restart_loop);
16504
James Smartd439d282010-09-29 11:18:45 -040016505 spin_unlock_irq(&phba->hbalock);
16506
16507 /* Release the cleaned-up mailbox commands */
16508 while (!list_empty(&mbox_cmd_list)) {
16509 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050016510 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16511 mp = (struct lpfc_dmabuf *) (mb->context1);
16512 if (mp) {
16513 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16514 kfree(mp);
16515 }
James Smart78730cf2010-04-06 15:06:30 -040016516 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040016517 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040016518 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016519 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040016520 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016521 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040016522 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040016523 }
James Smart695a8142010-01-26 23:08:03 -050016524 }
James Smart695a8142010-01-26 23:08:03 -050016525 mempool_free(mb, phba->mbox_mem_pool);
16526 }
James Smartd439d282010-09-29 11:18:45 -040016527
16528 /* Release the ndlp with the cleaned-up active mailbox command */
16529 if (act_mbx_ndlp) {
16530 spin_lock(shost->host_lock);
16531 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16532 spin_unlock(shost->host_lock);
16533 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050016534 }
James Smart695a8142010-01-26 23:08:03 -050016535}
16536
James Smart2a9bf3d2010-06-07 15:24:45 -040016537/**
16538 * lpfc_drain_txq - Drain the txq
16539 * @phba: Pointer to HBA context object.
16540 *
16541 * This function attempt to submit IOCBs on the txq
16542 * to the adapter. For SLI4 adapters, the txq contains
16543 * ELS IOCBs that have been deferred because the there
16544 * are no SGLs. This congestion can occur with large
16545 * vport counts during node discovery.
16546 **/
16547
16548uint32_t
16549lpfc_drain_txq(struct lpfc_hba *phba)
16550{
16551 LIST_HEAD(completions);
16552 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16553 struct lpfc_iocbq *piocbq = 0;
16554 unsigned long iflags = 0;
16555 char *fail_msg = NULL;
16556 struct lpfc_sglq *sglq;
16557 union lpfc_wqe wqe;
James Smart0e9bb8d2013-03-01 16:35:12 -050016558 int txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040016559
James Smart398d81c2013-05-31 17:04:19 -040016560 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050016561 list_for_each_entry(piocbq, &pring->txq, list) {
16562 txq_cnt++;
16563 }
16564
16565 if (txq_cnt > pring->txq_max)
16566 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016567
James Smart398d81c2013-05-31 17:04:19 -040016568 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016569
James Smart0e9bb8d2013-03-01 16:35:12 -050016570 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040016571 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016572
James Smart19ca7602010-11-20 23:11:55 -050016573 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016574 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040016575 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040016576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16577 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050016578 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040016579 break;
16580 }
James Smart19ca7602010-11-20 23:11:55 -050016581 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016582 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016583 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040016584 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016585 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016586 }
James Smart0e9bb8d2013-03-01 16:35:12 -050016587 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040016588
16589 /* The xri and iocb resources secured,
16590 * attempt to issue request
16591 */
James Smart6d368e52011-05-24 11:44:12 -040016592 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016593 piocbq->sli4_xritag = sglq->sli4_xritag;
16594 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16595 fail_msg = "to convert bpl to sgl";
16596 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16597 fail_msg = "to convert iocb to wqe";
16598 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16599 fail_msg = " - Wq is full";
16600 else
16601 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16602
16603 if (fail_msg) {
16604 /* Failed means we can't issue and need to cancel */
16605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16606 "2822 IOCB failed %s iotag 0x%x "
16607 "xri 0x%x\n",
16608 fail_msg,
16609 piocbq->iotag, piocbq->sli4_xritag);
16610 list_add_tail(&piocbq->list, &completions);
16611 }
James Smart398d81c2013-05-31 17:04:19 -040016612 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016613 }
16614
James Smart2a9bf3d2010-06-07 15:24:45 -040016615 /* Cancel all the IOCBs that cannot be issued */
16616 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16617 IOERR_SLI_ABORTED);
16618
James Smart0e9bb8d2013-03-01 16:35:12 -050016619 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016620}