blob: b0fe153452aa38f8adf95c74c25824f6bec6e459 [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 Smartbdcd2b92012-03-01 22:33:52 -05004 * Copyright (C) 2004-2012 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 Smart05580562011-05-24 11:40:48 -040074
James Smart4f774512009-05-22 14:52:35 -040075static IOCB_t *
76lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
77{
78 return &iocbq->iocb;
79}
80
81/**
82 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
83 * @q: The Work Queue to operate on.
84 * @wqe: The work Queue Entry to put on the Work queue.
85 *
86 * This routine will copy the contents of @wqe to the next available entry on
87 * the @q. This function will then ring the Work Queue Doorbell to signal the
88 * HBA to start processing the Work Queue Entry. This function returns 0 if
89 * successful. If no entries are available on @q then this function will return
90 * -ENOMEM.
91 * The caller is expected to hold the hbalock when calling this routine.
92 **/
93static uint32_t
94lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
95{
James Smart2e90f4b2011-12-13 13:22:37 -050096 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040097 struct lpfc_register doorbell;
98 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -040099 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400100
James Smart2e90f4b2011-12-13 13:22:37 -0500101 /* sanity check on queue memory */
102 if (unlikely(!q))
103 return -ENOMEM;
104 temp_wqe = q->qe[q->host_index].wqe;
105
James Smart4f774512009-05-22 14:52:35 -0400106 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400107 idx = ((q->host_index + 1) % q->entry_count);
108 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400109 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400110 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400111 }
112 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400113 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500114 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400115 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500116 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
117 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400118 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
119
120 /* Update the host index before invoking device */
121 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400122
123 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400124
125 /* Ring Doorbell */
126 doorbell.word0 = 0;
127 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
128 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
129 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
130 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400131
132 return 0;
133}
134
135/**
136 * lpfc_sli4_wq_release - Updates internal hba index for WQ
137 * @q: The Work Queue to operate on.
138 * @index: The index to advance the hba index to.
139 *
140 * This routine will update the HBA index of a queue to reflect consumption of
141 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
142 * an entry the host calls this function to update the queue's internal
143 * pointers. This routine returns the number of entries that were consumed by
144 * the HBA.
145 **/
146static uint32_t
147lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
148{
149 uint32_t released = 0;
150
James Smart2e90f4b2011-12-13 13:22:37 -0500151 /* sanity check on queue memory */
152 if (unlikely(!q))
153 return 0;
154
James Smart4f774512009-05-22 14:52:35 -0400155 if (q->hba_index == index)
156 return 0;
157 do {
158 q->hba_index = ((q->hba_index + 1) % q->entry_count);
159 released++;
160 } while (q->hba_index != index);
161 return released;
162}
163
164/**
165 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
166 * @q: The Mailbox Queue to operate on.
167 * @wqe: The Mailbox Queue Entry to put on the Work queue.
168 *
169 * This routine will copy the contents of @mqe to the next available entry on
170 * the @q. This function will then ring the Work Queue Doorbell to signal the
171 * HBA to start processing the Work Queue Entry. This function returns 0 if
172 * successful. If no entries are available on @q then this function will return
173 * -ENOMEM.
174 * The caller is expected to hold the hbalock when calling this routine.
175 **/
176static uint32_t
177lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
178{
James Smart2e90f4b2011-12-13 13:22:37 -0500179 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400180 struct lpfc_register doorbell;
181 uint32_t host_index;
182
James Smart2e90f4b2011-12-13 13:22:37 -0500183 /* sanity check on queue memory */
184 if (unlikely(!q))
185 return -ENOMEM;
186 temp_mqe = q->qe[q->host_index].mqe;
187
James Smart4f774512009-05-22 14:52:35 -0400188 /* If the host has not yet processed the next entry then we are done */
189 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
190 return -ENOMEM;
191 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
192 /* Save off the mailbox pointer for completion */
193 q->phba->mbox = (MAILBOX_t *)temp_mqe;
194
195 /* Update the host index before invoking device */
196 host_index = q->host_index;
197 q->host_index = ((q->host_index + 1) % q->entry_count);
198
199 /* Ring Doorbell */
200 doorbell.word0 = 0;
201 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
202 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
203 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400204 return 0;
205}
206
207/**
208 * lpfc_sli4_mq_release - Updates internal hba index for MQ
209 * @q: The Mailbox Queue to operate on.
210 *
211 * This routine will update the HBA index of a queue to reflect consumption of
212 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
213 * an entry the host calls this function to update the queue's internal
214 * pointers. This routine returns the number of entries that were consumed by
215 * the HBA.
216 **/
217static uint32_t
218lpfc_sli4_mq_release(struct lpfc_queue *q)
219{
James Smart2e90f4b2011-12-13 13:22:37 -0500220 /* sanity check on queue memory */
221 if (unlikely(!q))
222 return 0;
223
James Smart4f774512009-05-22 14:52:35 -0400224 /* Clear the mailbox pointer for completion */
225 q->phba->mbox = NULL;
226 q->hba_index = ((q->hba_index + 1) % q->entry_count);
227 return 1;
228}
229
230/**
231 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
232 * @q: The Event Queue to get the first valid EQE from
233 *
234 * This routine will get the first valid Event Queue Entry from @q, update
235 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
236 * the Queue (no more work to do), or the Queue is full of EQEs that have been
237 * processed, but not popped back to the HBA then this routine will return NULL.
238 **/
239static struct lpfc_eqe *
240lpfc_sli4_eq_get(struct lpfc_queue *q)
241{
James Smart2e90f4b2011-12-13 13:22:37 -0500242 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400243 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500244
245 /* sanity check on queue memory */
246 if (unlikely(!q))
247 return NULL;
248 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400249
250 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400251 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400252 return NULL;
253 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400254 idx = ((q->hba_index + 1) % q->entry_count);
255 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400256 return NULL;
257
James Smart027140e2012-08-03 12:35:44 -0400258 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400259 return eqe;
260}
261
262/**
James Smartba20c852012-08-03 12:36:52 -0400263 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
264 * @q: The Event Queue to disable interrupts
265 *
266 **/
267static inline void
268lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
269{
270 struct lpfc_register doorbell;
271
272 doorbell.word0 = 0;
273 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
274 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
275 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
276 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
277 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
278 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
279}
280
281/**
James Smart4f774512009-05-22 14:52:35 -0400282 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
283 * @q: The Event Queue that the host has completed processing for.
284 * @arm: Indicates whether the host wants to arms this CQ.
285 *
286 * This routine will mark all Event Queue Entries on @q, from the last
287 * known completed entry to the last entry that was processed, as completed
288 * by clearing the valid bit for each completion queue entry. Then it will
289 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
290 * The internal host index in the @q will be updated by this routine to indicate
291 * that the host has finished processing the entries. The @arm parameter
292 * indicates that the queue should be rearmed when ringing the doorbell.
293 *
294 * This function will return the number of EQEs that were popped.
295 **/
296uint32_t
297lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
298{
299 uint32_t released = 0;
300 struct lpfc_eqe *temp_eqe;
301 struct lpfc_register doorbell;
302
James Smart2e90f4b2011-12-13 13:22:37 -0500303 /* sanity check on queue memory */
304 if (unlikely(!q))
305 return 0;
306
James Smart4f774512009-05-22 14:52:35 -0400307 /* while there are valid entries */
308 while (q->hba_index != q->host_index) {
309 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400310 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400311 released++;
312 q->host_index = ((q->host_index + 1) % q->entry_count);
313 }
314 if (unlikely(released == 0 && !arm))
315 return 0;
316
317 /* ring doorbell for number popped */
318 doorbell.word0 = 0;
319 if (arm) {
320 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
321 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
322 }
323 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
324 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500325 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
326 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
327 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400328 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500329 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
330 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
331 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400332 return released;
333}
334
335/**
336 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
337 * @q: The Completion Queue to get the first valid CQE from
338 *
339 * This routine will get the first valid Completion Queue Entry from @q, update
340 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
341 * the Queue (no more work to do), or the Queue is full of CQEs that have been
342 * processed, but not popped back to the HBA then this routine will return NULL.
343 **/
344static struct lpfc_cqe *
345lpfc_sli4_cq_get(struct lpfc_queue *q)
346{
347 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400348 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400349
James Smart2e90f4b2011-12-13 13:22:37 -0500350 /* sanity check on queue memory */
351 if (unlikely(!q))
352 return NULL;
353
James Smart4f774512009-05-22 14:52:35 -0400354 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400355 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400356 return NULL;
357 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400358 idx = ((q->hba_index + 1) % q->entry_count);
359 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400360 return NULL;
361
362 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400363 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400364 return cqe;
365}
366
367/**
368 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
369 * @q: The Completion Queue that the host has completed processing for.
370 * @arm: Indicates whether the host wants to arms this CQ.
371 *
372 * This routine will mark all Completion queue entries on @q, from the last
373 * known completed entry to the last entry that was processed, as completed
374 * by clearing the valid bit for each completion queue entry. Then it will
375 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
376 * The internal host index in the @q will be updated by this routine to indicate
377 * that the host has finished processing the entries. The @arm parameter
378 * indicates that the queue should be rearmed when ringing the doorbell.
379 *
380 * This function will return the number of CQEs that were released.
381 **/
382uint32_t
383lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
384{
385 uint32_t released = 0;
386 struct lpfc_cqe *temp_qe;
387 struct lpfc_register doorbell;
388
James Smart2e90f4b2011-12-13 13:22:37 -0500389 /* sanity check on queue memory */
390 if (unlikely(!q))
391 return 0;
James Smart4f774512009-05-22 14:52:35 -0400392 /* while there are valid entries */
393 while (q->hba_index != q->host_index) {
394 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400395 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400396 released++;
397 q->host_index = ((q->host_index + 1) % q->entry_count);
398 }
399 if (unlikely(released == 0 && !arm))
400 return 0;
401
402 /* ring doorbell for number popped */
403 doorbell.word0 = 0;
404 if (arm)
405 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
406 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
407 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500408 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
409 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
410 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400411 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
412 return released;
413}
414
415/**
416 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
417 * @q: The Header Receive Queue to operate on.
418 * @wqe: The Receive Queue Entry to put on the Receive queue.
419 *
420 * This routine will copy the contents of @wqe to the next available entry on
421 * the @q. This function will then ring the Receive Queue Doorbell to signal the
422 * HBA to start processing the Receive Queue Entry. This function returns the
423 * index that the rqe was copied to if successful. If no entries are available
424 * on @q then this function will return -ENOMEM.
425 * The caller is expected to hold the hbalock when calling this routine.
426 **/
427static int
428lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
429 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
430{
James Smart2e90f4b2011-12-13 13:22:37 -0500431 struct lpfc_rqe *temp_hrqe;
432 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400433 struct lpfc_register doorbell;
434 int put_index = hq->host_index;
435
James Smart2e90f4b2011-12-13 13:22:37 -0500436 /* sanity check on queue memory */
437 if (unlikely(!hq) || unlikely(!dq))
438 return -ENOMEM;
439 temp_hrqe = hq->qe[hq->host_index].rqe;
440 temp_drqe = dq->qe[dq->host_index].rqe;
441
James Smart4f774512009-05-22 14:52:35 -0400442 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
443 return -EINVAL;
444 if (hq->host_index != dq->host_index)
445 return -EINVAL;
446 /* If the host has not yet processed the next entry then we are done */
447 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
448 return -EBUSY;
449 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
450 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
451
452 /* Update the host index to point to the next slot */
453 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
454 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
455
456 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400457 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400458 doorbell.word0 = 0;
459 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400460 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400461 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
462 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
463 }
464 return put_index;
465}
466
467/**
468 * lpfc_sli4_rq_release - Updates internal hba index for RQ
469 * @q: The Header Receive Queue to operate on.
470 *
471 * This routine will update the HBA index of a queue to reflect consumption of
472 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
473 * consumed an entry the host calls this function to update the queue's
474 * internal pointers. This routine returns the number of entries that were
475 * consumed by the HBA.
476 **/
477static uint32_t
478lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
479{
James Smart2e90f4b2011-12-13 13:22:37 -0500480 /* sanity check on queue memory */
481 if (unlikely(!hq) || unlikely(!dq))
482 return 0;
483
James Smart4f774512009-05-22 14:52:35 -0400484 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
485 return 0;
486 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
487 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
488 return 1;
489}
490
James Smarte59058c2008-08-24 21:49:00 -0400491/**
James Smart3621a712009-04-06 18:47:14 -0400492 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400493 * @phba: Pointer to HBA context object.
494 * @pring: Pointer to driver SLI ring object.
495 *
496 * This function returns pointer to next command iocb entry
497 * in the command ring. The caller must hold hbalock to prevent
498 * other threads consume the next command iocb.
499 * SLI-2/SLI-3 provide different sized iocbs.
500 **/
James Smarted957682007-06-17 19:56:37 -0500501static inline IOCB_t *
502lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
503{
James Smart7e56aa22012-08-03 12:35:34 -0400504 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
505 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500506}
507
James Smarte59058c2008-08-24 21:49:00 -0400508/**
James Smart3621a712009-04-06 18:47:14 -0400509 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400510 * @phba: Pointer to HBA context object.
511 * @pring: Pointer to driver SLI ring object.
512 *
513 * This function returns pointer to next response iocb entry
514 * in the response ring. The caller must hold hbalock to make sure
515 * that no other thread consume the next response iocb.
516 * SLI-2/SLI-3 provide different sized iocbs.
517 **/
James Smarted957682007-06-17 19:56:37 -0500518static inline IOCB_t *
519lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
520{
James Smart7e56aa22012-08-03 12:35:34 -0400521 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
522 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500523}
524
James Smarte59058c2008-08-24 21:49:00 -0400525/**
James Smart3621a712009-04-06 18:47:14 -0400526 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400527 * @phba: Pointer to HBA context object.
528 *
529 * This function is called with hbalock held. This function
530 * allocates a new driver iocb object from the iocb pool. If the
531 * allocation is successful, it returns pointer to the newly
532 * allocated iocb object else it returns NULL.
533 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400534struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500535__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400536{
537 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
538 struct lpfc_iocbq * iocbq = NULL;
539
540 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400541 if (iocbq)
542 phba->iocb_cnt++;
543 if (phba->iocb_cnt > phba->iocb_max)
544 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400545 return iocbq;
546}
547
James Smarte59058c2008-08-24 21:49:00 -0400548/**
James Smartda0436e2009-05-22 14:51:39 -0400549 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
550 * @phba: Pointer to HBA context object.
551 * @xritag: XRI value.
552 *
553 * This function clears the sglq pointer from the array of acive
554 * sglq's. The xritag that is passed in is used to index into the
555 * array. Before the xritag can be used it needs to be adjusted
556 * by subtracting the xribase.
557 *
558 * Returns sglq ponter = success, NULL = Failure.
559 **/
560static struct lpfc_sglq *
561__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
562{
James Smartda0436e2009-05-22 14:51:39 -0400563 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400564
565 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
566 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400567 return sglq;
568}
569
570/**
571 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
572 * @phba: Pointer to HBA context object.
573 * @xritag: XRI value.
574 *
575 * This function returns the sglq pointer from the array of acive
576 * sglq's. The xritag that is passed in is used to index into the
577 * array. Before the xritag can be used it needs to be adjusted
578 * by subtracting the xribase.
579 *
580 * Returns sglq ponter = success, NULL = Failure.
581 **/
James Smart0f65ff62010-02-26 14:14:23 -0500582struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400583__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
584{
James Smartda0436e2009-05-22 14:51:39 -0400585 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400586
587 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400588 return sglq;
589}
590
591/**
James Smart1151e3e2011-02-16 12:39:35 -0500592 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500593 * @phba: Pointer to HBA context object.
594 * @xritag: xri used in this exchange.
595 * @rrq: The RRQ to be cleared.
596 *
James Smart19ca7602010-11-20 23:11:55 -0500597 **/
James Smart1151e3e2011-02-16 12:39:35 -0500598void
599lpfc_clr_rrq_active(struct lpfc_hba *phba,
600 uint16_t xritag,
601 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500602{
James Smart1151e3e2011-02-16 12:39:35 -0500603 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500604
James Smart1151e3e2011-02-16 12:39:35 -0500605 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
606 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500607
608 /* The target DID could have been swapped (cable swap)
609 * we should use the ndlp from the findnode if it is
610 * available.
611 */
James Smart1151e3e2011-02-16 12:39:35 -0500612 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500613 ndlp = rrq->ndlp;
614
James Smart1151e3e2011-02-16 12:39:35 -0500615 if (!ndlp)
616 goto out;
617
James Smart6d368e52011-05-24 11:44:12 -0400618 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500619 rrq->send_rrq = 0;
620 rrq->xritag = 0;
621 rrq->rrq_stop_time = 0;
622 }
James Smart1151e3e2011-02-16 12:39:35 -0500623out:
James Smart19ca7602010-11-20 23:11:55 -0500624 mempool_free(rrq, phba->rrq_pool);
625}
626
627/**
628 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
629 * @phba: Pointer to HBA context object.
630 *
631 * This function is called with hbalock held. This function
632 * Checks if stop_time (ratov from setting rrq active) has
633 * been reached, if it has and the send_rrq flag is set then
634 * it will call lpfc_send_rrq. If the send_rrq flag is not set
635 * then it will just call the routine to clear the rrq and
636 * free the rrq resource.
637 * The timer is set to the next rrq that is going to expire before
638 * leaving the routine.
639 *
640 **/
641void
642lpfc_handle_rrq_active(struct lpfc_hba *phba)
643{
644 struct lpfc_node_rrq *rrq;
645 struct lpfc_node_rrq *nextrrq;
646 unsigned long next_time;
647 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500648 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500649
650 spin_lock_irqsave(&phba->hbalock, iflags);
651 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
652 next_time = jiffies + HZ * (phba->fc_ratov + 1);
653 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500654 &phba->active_rrq_list, list) {
655 if (time_after(jiffies, rrq->rrq_stop_time))
656 list_move(&rrq->list, &send_rrq);
657 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500658 next_time = rrq->rrq_stop_time;
659 }
660 spin_unlock_irqrestore(&phba->hbalock, iflags);
661 if (!list_empty(&phba->active_rrq_list))
662 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500663 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
664 list_del(&rrq->list);
665 if (!rrq->send_rrq)
666 /* this call will free the rrq */
667 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
668 else if (lpfc_send_rrq(phba, rrq)) {
669 /* if we send the rrq then the completion handler
670 * will clear the bit in the xribitmap.
671 */
672 lpfc_clr_rrq_active(phba, rrq->xritag,
673 rrq);
674 }
675 }
James Smart19ca7602010-11-20 23:11:55 -0500676}
677
678/**
679 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
680 * @vport: Pointer to vport context object.
681 * @xri: The xri used in the exchange.
682 * @did: The targets DID for this exchange.
683 *
684 * returns NULL = rrq not found in the phba->active_rrq_list.
685 * rrq = rrq for this xri and target.
686 **/
687struct lpfc_node_rrq *
688lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
689{
690 struct lpfc_hba *phba = vport->phba;
691 struct lpfc_node_rrq *rrq;
692 struct lpfc_node_rrq *nextrrq;
693 unsigned long iflags;
694
695 if (phba->sli_rev != LPFC_SLI_REV4)
696 return NULL;
697 spin_lock_irqsave(&phba->hbalock, iflags);
698 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
699 if (rrq->vport == vport && rrq->xritag == xri &&
700 rrq->nlp_DID == did){
701 list_del(&rrq->list);
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return rrq;
704 }
705 }
706 spin_unlock_irqrestore(&phba->hbalock, iflags);
707 return NULL;
708}
709
710/**
711 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
712 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500713 * @ndlp: Pointer to the lpfc_node_list structure.
714 * If ndlp is NULL Remove all active RRQs for this vport from the
715 * phba->active_rrq_list and clear the rrq.
716 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500717 **/
718void
James Smart1151e3e2011-02-16 12:39:35 -0500719lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500720
721{
722 struct lpfc_hba *phba = vport->phba;
723 struct lpfc_node_rrq *rrq;
724 struct lpfc_node_rrq *nextrrq;
725 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500726 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500727
728 if (phba->sli_rev != LPFC_SLI_REV4)
729 return;
James Smart1151e3e2011-02-16 12:39:35 -0500730 if (!ndlp) {
731 lpfc_sli4_vport_delete_els_xri_aborted(vport);
732 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500733 }
James Smart1151e3e2011-02-16 12:39:35 -0500734 spin_lock_irqsave(&phba->hbalock, iflags);
735 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
736 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
737 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500738 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500739
740 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
741 list_del(&rrq->list);
742 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
743 }
James Smart19ca7602010-11-20 23:11:55 -0500744}
745
746/**
747 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
748 * @phba: Pointer to HBA context object.
749 *
750 * Remove all rrqs from the phba->active_rrq_list and free them by
751 * calling __lpfc_clr_active_rrq
752 *
753 **/
754void
755lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
756{
757 struct lpfc_node_rrq *rrq;
758 struct lpfc_node_rrq *nextrrq;
759 unsigned long next_time;
760 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500761 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500762
763 if (phba->sli_rev != LPFC_SLI_REV4)
764 return;
765 spin_lock_irqsave(&phba->hbalock, iflags);
766 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
767 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500768 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500769 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500770
771 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
772 list_del(&rrq->list);
773 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
774 }
James Smart19ca7602010-11-20 23:11:55 -0500775 if (!list_empty(&phba->active_rrq_list))
776 mod_timer(&phba->rrq_tmr, next_time);
777}
778
779
780/**
James Smart1151e3e2011-02-16 12:39:35 -0500781 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500782 * @phba: Pointer to HBA context object.
783 * @ndlp: Targets nodelist pointer for this exchange.
784 * @xritag the xri in the bitmap to test.
785 *
786 * This function is called with hbalock held. This function
787 * returns 0 = rrq not active for this xri
788 * 1 = rrq is valid for this xri.
789 **/
James Smart1151e3e2011-02-16 12:39:35 -0500790int
791lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500792 uint16_t xritag)
793{
James Smart19ca7602010-11-20 23:11:55 -0500794 if (!ndlp)
795 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400796 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500797 return 1;
798 else
799 return 0;
800}
801
802/**
803 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
804 * @phba: Pointer to HBA context object.
805 * @ndlp: nodelist pointer for this target.
806 * @xritag: xri used in this exchange.
807 * @rxid: Remote Exchange ID.
808 * @send_rrq: Flag used to determine if we should send rrq els cmd.
809 *
810 * This function takes the hbalock.
811 * The active bit is always set in the active rrq xri_bitmap even
812 * if there is no slot avaiable for the other rrq information.
813 *
814 * returns 0 rrq actived for this xri
815 * < 0 No memory or invalid ndlp.
816 **/
817int
818lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500819 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500820{
James Smart19ca7602010-11-20 23:11:55 -0500821 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500822 struct lpfc_node_rrq *rrq;
823 int empty;
824
825 if (!ndlp)
826 return -EINVAL;
827
828 if (!phba->cfg_enable_rrq)
829 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500830
831 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500832 if (phba->pport->load_flag & FC_UNLOADING) {
833 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
834 goto out;
835 }
836
837 /*
838 * set the active bit even if there is no mem available.
839 */
840 if (NLP_CHK_FREE_REQ(ndlp))
841 goto out;
842
843 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
844 goto out;
845
846 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
847 goto out;
848
James Smart19ca7602010-11-20 23:11:55 -0500849 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500850 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
851 if (!rrq) {
852 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
853 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
854 " DID:0x%x Send:%d\n",
855 xritag, rxid, ndlp->nlp_DID, send_rrq);
856 return -EINVAL;
857 }
858 rrq->send_rrq = send_rrq;
859 rrq->xritag = xritag;
860 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
861 rrq->ndlp = ndlp;
862 rrq->nlp_DID = ndlp->nlp_DID;
863 rrq->vport = ndlp->vport;
864 rrq->rxid = rxid;
865 rrq->send_rrq = send_rrq;
866 spin_lock_irqsave(&phba->hbalock, iflags);
867 empty = list_empty(&phba->active_rrq_list);
868 list_add_tail(&rrq->list, &phba->active_rrq_list);
869 phba->hba_flag |= HBA_RRQ_ACTIVE;
870 if (empty)
871 lpfc_worker_wake_up(phba);
872 spin_unlock_irqrestore(&phba->hbalock, iflags);
873 return 0;
874out:
875 spin_unlock_irqrestore(&phba->hbalock, iflags);
876 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
877 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
878 " DID:0x%x Send:%d\n",
879 xritag, rxid, ndlp->nlp_DID, send_rrq);
880 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500881}
882
883/**
James Smartda0436e2009-05-22 14:51:39 -0400884 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
885 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500886 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400887 *
888 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400889 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400890 * list is not empty then it is successful, it returns pointer to the newly
891 * allocated sglq object else it returns NULL.
892 **/
893static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500894__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400895{
896 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
897 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500898 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500899 struct lpfc_scsi_buf *lpfc_cmd;
900 struct lpfc_nodelist *ndlp;
901 int found = 0;
902
903 if (piocbq->iocb_flag & LPFC_IO_FCP) {
904 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
905 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500906 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
907 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500908 ndlp = piocbq->context_un.ndlp;
James Smart93d1379e2012-05-09 21:19:34 -0400909 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
910 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
911 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500912 else
913 ndlp = piocbq->context1;
914
James Smartda0436e2009-05-22 14:51:39 -0400915 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500916 start_sglq = sglq;
917 while (!found) {
918 if (!sglq)
919 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400920 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500921 /* This xri has an rrq outstanding for this DID.
922 * put it back in the list and get another xri.
923 */
924 list_add_tail(&sglq->list, lpfc_sgl_list);
925 sglq = NULL;
926 list_remove_head(lpfc_sgl_list, sglq,
927 struct lpfc_sglq, list);
928 if (sglq == start_sglq) {
929 sglq = NULL;
930 break;
931 } else
932 continue;
933 }
934 sglq->ndlp = ndlp;
935 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400936 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500937 sglq->state = SGL_ALLOCATED;
938 }
James Smartda0436e2009-05-22 14:51:39 -0400939 return sglq;
940}
941
942/**
James Smart3621a712009-04-06 18:47:14 -0400943 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400944 * @phba: Pointer to HBA context object.
945 *
946 * This function is called with no lock held. This function
947 * allocates a new driver iocb object from the iocb pool. If the
948 * allocation is successful, it returns pointer to the newly
949 * allocated iocb object else it returns NULL.
950 **/
James Smart2e0fef82007-06-17 19:56:36 -0500951struct lpfc_iocbq *
952lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500953{
James Smart2e0fef82007-06-17 19:56:36 -0500954 struct lpfc_iocbq * iocbq = NULL;
955 unsigned long iflags;
956
957 spin_lock_irqsave(&phba->hbalock, iflags);
958 iocbq = __lpfc_sli_get_iocbq(phba);
959 spin_unlock_irqrestore(&phba->hbalock, iflags);
960 return iocbq;
961}
962
James Smarte59058c2008-08-24 21:49:00 -0400963/**
James Smart4f774512009-05-22 14:52:35 -0400964 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
965 * @phba: Pointer to HBA context object.
966 * @iocbq: Pointer to driver iocb object.
967 *
968 * This function is called with hbalock held to release driver
969 * iocb object to the iocb pool. The iotag in the iocb object
970 * does not change for each use of the iocb object. This function
971 * clears all other fields of the iocb object when it is freed.
972 * The sqlq structure that holds the xritag and phys and virtual
973 * mappings for the scatter gather list is retrieved from the
974 * active array of sglq. The get of the sglq pointer also clears
975 * the entry in the array. If the status of the IO indiactes that
976 * this IO was aborted then the sglq entry it put on the
977 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
978 * IO has good status or fails for any other reason then the sglq
979 * entry is added to the free list (lpfc_sgl_list).
980 **/
981static void
982__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
983{
984 struct lpfc_sglq *sglq;
985 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400986 unsigned long iflag = 0;
987 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400988
989 if (iocbq->sli4_xritag == NO_XRI)
990 sglq = NULL;
991 else
James Smart6d368e52011-05-24 11:44:12 -0400992 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
993
James Smart4f774512009-05-22 14:52:35 -0400994 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500995 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
996 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400997 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
998 iflag);
999 list_add(&sglq->list,
1000 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1001 spin_unlock_irqrestore(
1002 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001003 } else {
1004 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001005 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001006 list_add_tail(&sglq->list,
1007 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -04001008
1009 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -04001010 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -04001011 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001012 }
James Smart4f774512009-05-22 14:52:35 -04001013 }
1014
1015
1016 /*
1017 * Clean all volatile data fields, preserve iotag and node struct.
1018 */
1019 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001020 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001021 iocbq->sli4_xritag = NO_XRI;
1022 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1023}
1024
James Smart2a9bf3d2010-06-07 15:24:45 -04001025
James Smart4f774512009-05-22 14:52:35 -04001026/**
James Smart3772a992009-05-22 14:50:54 -04001027 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1028 * @phba: Pointer to HBA context object.
1029 * @iocbq: Pointer to driver iocb object.
1030 *
1031 * This function is called with hbalock held to release driver
1032 * iocb object to the iocb pool. The iotag in the iocb object
1033 * does not change for each use of the iocb object. This function
1034 * clears all other fields of the iocb object when it is freed.
1035 **/
1036static void
1037__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1038{
1039 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1040
1041 /*
1042 * Clean all volatile data fields, preserve iotag and node struct.
1043 */
1044 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1045 iocbq->sli4_xritag = NO_XRI;
1046 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1047}
1048
1049/**
James Smart3621a712009-04-06 18:47:14 -04001050 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001051 * @phba: Pointer to HBA context object.
1052 * @iocbq: Pointer to driver iocb object.
1053 *
1054 * This function is called with hbalock held to release driver
1055 * iocb object to the iocb pool. The iotag in the iocb object
1056 * does not change for each use of the iocb object. This function
1057 * clears all other fields of the iocb object when it is freed.
1058 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001059static void
James Smart2e0fef82007-06-17 19:56:36 -05001060__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1061{
James Smart3772a992009-05-22 14:50:54 -04001062 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001063 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001064}
1065
James Smarte59058c2008-08-24 21:49:00 -04001066/**
James Smart3621a712009-04-06 18:47:14 -04001067 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001068 * @phba: Pointer to HBA context object.
1069 * @iocbq: Pointer to driver iocb object.
1070 *
1071 * This function is called with no lock held to release the iocb to
1072 * iocb pool.
1073 **/
James Smart2e0fef82007-06-17 19:56:36 -05001074void
1075lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1076{
1077 unsigned long iflags;
1078
1079 /*
1080 * Clean all volatile data fields, preserve iotag and node struct.
1081 */
1082 spin_lock_irqsave(&phba->hbalock, iflags);
1083 __lpfc_sli_release_iocbq(phba, iocbq);
1084 spin_unlock_irqrestore(&phba->hbalock, iflags);
1085}
1086
James Smarte59058c2008-08-24 21:49:00 -04001087/**
James Smarta257bf92009-04-06 18:48:10 -04001088 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1089 * @phba: Pointer to HBA context object.
1090 * @iocblist: List of IOCBs.
1091 * @ulpstatus: ULP status in IOCB command field.
1092 * @ulpWord4: ULP word-4 in IOCB command field.
1093 *
1094 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1095 * on the list by invoking the complete callback function associated with the
1096 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1097 * fields.
1098 **/
1099void
1100lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1101 uint32_t ulpstatus, uint32_t ulpWord4)
1102{
1103 struct lpfc_iocbq *piocb;
1104
1105 while (!list_empty(iocblist)) {
1106 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1107
1108 if (!piocb->iocb_cmpl)
1109 lpfc_sli_release_iocbq(phba, piocb);
1110 else {
1111 piocb->iocb.ulpStatus = ulpstatus;
1112 piocb->iocb.un.ulpWord[4] = ulpWord4;
1113 (piocb->iocb_cmpl) (phba, piocb, piocb);
1114 }
1115 }
1116 return;
1117}
1118
1119/**
James Smart3621a712009-04-06 18:47:14 -04001120 * lpfc_sli_iocb_cmd_type - Get the iocb type
1121 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001122 *
1123 * This function is called by ring event handler function to get the iocb type.
1124 * This function translates the iocb command to an iocb command type used to
1125 * decide the final disposition of each completed IOCB.
1126 * The function returns
1127 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1128 * LPFC_SOL_IOCB if it is a solicited iocb completion
1129 * LPFC_ABORT_IOCB if it is an abort iocb
1130 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1131 *
1132 * The caller is not required to hold any lock.
1133 **/
dea31012005-04-17 16:05:31 -05001134static lpfc_iocb_type
1135lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1136{
1137 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1138
1139 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1140 return 0;
1141
1142 switch (iocb_cmnd) {
1143 case CMD_XMIT_SEQUENCE_CR:
1144 case CMD_XMIT_SEQUENCE_CX:
1145 case CMD_XMIT_BCAST_CN:
1146 case CMD_XMIT_BCAST_CX:
1147 case CMD_ELS_REQUEST_CR:
1148 case CMD_ELS_REQUEST_CX:
1149 case CMD_CREATE_XRI_CR:
1150 case CMD_CREATE_XRI_CX:
1151 case CMD_GET_RPI_CN:
1152 case CMD_XMIT_ELS_RSP_CX:
1153 case CMD_GET_RPI_CR:
1154 case CMD_FCP_IWRITE_CR:
1155 case CMD_FCP_IWRITE_CX:
1156 case CMD_FCP_IREAD_CR:
1157 case CMD_FCP_IREAD_CX:
1158 case CMD_FCP_ICMND_CR:
1159 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001160 case CMD_FCP_TSEND_CX:
1161 case CMD_FCP_TRSP_CX:
1162 case CMD_FCP_TRECEIVE_CX:
1163 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001164 case CMD_ADAPTER_MSG:
1165 case CMD_ADAPTER_DUMP:
1166 case CMD_XMIT_SEQUENCE64_CR:
1167 case CMD_XMIT_SEQUENCE64_CX:
1168 case CMD_XMIT_BCAST64_CN:
1169 case CMD_XMIT_BCAST64_CX:
1170 case CMD_ELS_REQUEST64_CR:
1171 case CMD_ELS_REQUEST64_CX:
1172 case CMD_FCP_IWRITE64_CR:
1173 case CMD_FCP_IWRITE64_CX:
1174 case CMD_FCP_IREAD64_CR:
1175 case CMD_FCP_IREAD64_CX:
1176 case CMD_FCP_ICMND64_CR:
1177 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001178 case CMD_FCP_TSEND64_CX:
1179 case CMD_FCP_TRSP64_CX:
1180 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001181 case CMD_GEN_REQUEST64_CR:
1182 case CMD_GEN_REQUEST64_CX:
1183 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001184 case DSSCMD_IWRITE64_CR:
1185 case DSSCMD_IWRITE64_CX:
1186 case DSSCMD_IREAD64_CR:
1187 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001188 type = LPFC_SOL_IOCB;
1189 break;
1190 case CMD_ABORT_XRI_CN:
1191 case CMD_ABORT_XRI_CX:
1192 case CMD_CLOSE_XRI_CN:
1193 case CMD_CLOSE_XRI_CX:
1194 case CMD_XRI_ABORTED_CX:
1195 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001196 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001197 type = LPFC_ABORT_IOCB;
1198 break;
1199 case CMD_RCV_SEQUENCE_CX:
1200 case CMD_RCV_ELS_REQ_CX:
1201 case CMD_RCV_SEQUENCE64_CX:
1202 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001203 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001204 case CMD_IOCB_RCV_SEQ64_CX:
1205 case CMD_IOCB_RCV_ELS64_CX:
1206 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001207 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001208 type = LPFC_UNSOL_IOCB;
1209 break;
James Smart3163f722008-02-08 18:50:25 -05001210 case CMD_IOCB_XMIT_MSEQ64_CR:
1211 case CMD_IOCB_XMIT_MSEQ64_CX:
1212 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1213 case CMD_IOCB_RCV_ELS_LIST64_CX:
1214 case CMD_IOCB_CLOSE_EXTENDED_CN:
1215 case CMD_IOCB_ABORT_EXTENDED_CN:
1216 case CMD_IOCB_RET_HBQE64_CN:
1217 case CMD_IOCB_FCP_IBIDIR64_CR:
1218 case CMD_IOCB_FCP_IBIDIR64_CX:
1219 case CMD_IOCB_FCP_ITASKMGT64_CX:
1220 case CMD_IOCB_LOGENTRY_CN:
1221 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1222 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001223 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001224 type = LPFC_UNKNOWN_IOCB;
1225 break;
dea31012005-04-17 16:05:31 -05001226 default:
1227 type = LPFC_UNKNOWN_IOCB;
1228 break;
1229 }
1230
1231 return type;
1232}
1233
James Smarte59058c2008-08-24 21:49:00 -04001234/**
James Smart3621a712009-04-06 18:47:14 -04001235 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001236 * @phba: Pointer to HBA context object.
1237 *
1238 * This function is called from SLI initialization code
1239 * to configure every ring of the HBA's SLI interface. The
1240 * caller is not required to hold any lock. This function issues
1241 * a config_ring mailbox command for each ring.
1242 * This function returns zero if successful else returns a negative
1243 * error code.
1244 **/
dea31012005-04-17 16:05:31 -05001245static int
James Smarted957682007-06-17 19:56:37 -05001246lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001247{
1248 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001249 LPFC_MBOXQ_t *pmb;
1250 MAILBOX_t *pmbox;
1251 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001252
James Smarted957682007-06-17 19:56:37 -05001253 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1254 if (!pmb)
1255 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001256 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001257 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001258 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001259 lpfc_config_ring(phba, i, pmb);
1260 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1261 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001263 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001264 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1265 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001266 rc, pmbox->mbxCommand,
1267 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001268 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001269 ret = -ENXIO;
1270 break;
dea31012005-04-17 16:05:31 -05001271 }
1272 }
James Smarted957682007-06-17 19:56:37 -05001273 mempool_free(pmb, phba->mbox_mem_pool);
1274 return ret;
dea31012005-04-17 16:05:31 -05001275}
1276
James Smarte59058c2008-08-24 21:49:00 -04001277/**
James Smart3621a712009-04-06 18:47:14 -04001278 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001279 * @phba: Pointer to HBA context object.
1280 * @pring: Pointer to driver SLI ring object.
1281 * @piocb: Pointer to the driver iocb object.
1282 *
1283 * This function is called with hbalock held. The function adds the
1284 * new iocb to txcmplq of the given ring. This function always returns
1285 * 0. If this function is called for ELS ring, this function checks if
1286 * there is a vport associated with the ELS command. This function also
1287 * starts els_tmofunc timer if this is an ELS command.
1288 **/
dea31012005-04-17 16:05:31 -05001289static int
James Smart2e0fef82007-06-17 19:56:36 -05001290lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1291 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001292{
dea31012005-04-17 16:05:31 -05001293 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001294 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
dea31012005-04-17 16:05:31 -05001295 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001296 if (pring->txcmplq_cnt > pring->txcmplq_max)
1297 pring->txcmplq_max = pring->txcmplq_cnt;
1298
James Smart92d7f7b2007-06-17 19:56:38 -05001299 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1300 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1301 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1302 if (!piocb->vport)
1303 BUG();
1304 else
1305 mod_timer(&piocb->vport->els_tmofunc,
1306 jiffies + HZ * (phba->fc_ratov << 1));
1307 }
1308
dea31012005-04-17 16:05:31 -05001309
James Smart2e0fef82007-06-17 19:56:36 -05001310 return 0;
dea31012005-04-17 16:05:31 -05001311}
1312
James Smarte59058c2008-08-24 21:49:00 -04001313/**
James Smart3621a712009-04-06 18:47:14 -04001314 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001315 * @phba: Pointer to HBA context object.
1316 * @pring: Pointer to driver SLI ring object.
1317 *
1318 * This function is called with hbalock held to get next
1319 * iocb in txq of the given ring. If there is any iocb in
1320 * the txq, the function returns first iocb in the list after
1321 * removing the iocb from the list, else it returns NULL.
1322 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001323struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001324lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001325{
dea31012005-04-17 16:05:31 -05001326 struct lpfc_iocbq *cmd_iocb;
1327
James Smart858c9f62007-06-17 19:56:39 -05001328 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1329 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001330 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001331 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001332}
1333
James Smarte59058c2008-08-24 21:49:00 -04001334/**
James Smart3621a712009-04-06 18:47:14 -04001335 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001336 * @phba: Pointer to HBA context object.
1337 * @pring: Pointer to driver SLI ring object.
1338 *
1339 * This function is called with hbalock held and the caller must post the
1340 * iocb without releasing the lock. If the caller releases the lock,
1341 * iocb slot returned by the function is not guaranteed to be available.
1342 * The function returns pointer to the next available iocb slot if there
1343 * is available slot in the ring, else it returns NULL.
1344 * If the get index of the ring is ahead of the put index, the function
1345 * will post an error attention event to the worker thread to take the
1346 * HBA to offline state.
1347 **/
dea31012005-04-17 16:05:31 -05001348static IOCB_t *
1349lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1350{
James Smart34b02dc2008-08-24 21:49:55 -04001351 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001352 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1353 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1354 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1355 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001356
James Smart7e56aa22012-08-03 12:35:34 -04001357 if (unlikely(pring->sli.sli3.local_getidx ==
1358 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001359
James Smart7e56aa22012-08-03 12:35:34 -04001360 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001361
James Smart7e56aa22012-08-03 12:35:34 -04001362 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001363 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001364 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001365 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001366 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001367 pring->sli.sli3.local_getidx,
1368 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001369
James Smart2e0fef82007-06-17 19:56:36 -05001370 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001371 /*
1372 * All error attention handlers are posted to
1373 * worker thread
1374 */
1375 phba->work_ha |= HA_ERATT;
1376 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001377
James Smart5e9d9b82008-06-14 22:52:53 -04001378 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001379
1380 return NULL;
1381 }
1382
James Smart7e56aa22012-08-03 12:35:34 -04001383 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001384 return NULL;
1385 }
1386
James Smarted957682007-06-17 19:56:37 -05001387 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001388}
1389
James Smarte59058c2008-08-24 21:49:00 -04001390/**
James Smart3621a712009-04-06 18:47:14 -04001391 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001392 * @phba: Pointer to HBA context object.
1393 * @iocbq: Pointer to driver iocb object.
1394 *
1395 * This function gets an iotag for the iocb. If there is no unused iotag and
1396 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1397 * array and assigns a new iotag.
1398 * The function returns the allocated iotag if successful, else returns zero.
1399 * Zero is not a valid iotag.
1400 * The caller is not required to hold any lock.
1401 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001402uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001403lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001404{
James Smart2e0fef82007-06-17 19:56:36 -05001405 struct lpfc_iocbq **new_arr;
1406 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001407 size_t new_len;
1408 struct lpfc_sli *psli = &phba->sli;
1409 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001410
James Smart2e0fef82007-06-17 19:56:36 -05001411 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001412 iotag = psli->last_iotag;
1413 if(++iotag < psli->iocbq_lookup_len) {
1414 psli->last_iotag = iotag;
1415 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001416 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001417 iocbq->iotag = iotag;
1418 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001419 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001420 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1421 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001422 spin_unlock_irq(&phba->hbalock);
1423 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001424 GFP_KERNEL);
1425 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001426 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001427 old_arr = psli->iocbq_lookup;
1428 if (new_len <= psli->iocbq_lookup_len) {
1429 /* highly unprobable case */
1430 kfree(new_arr);
1431 iotag = psli->last_iotag;
1432 if(++iotag < psli->iocbq_lookup_len) {
1433 psli->last_iotag = iotag;
1434 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001435 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001436 iocbq->iotag = iotag;
1437 return iotag;
1438 }
James Smart2e0fef82007-06-17 19:56:36 -05001439 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001440 return 0;
1441 }
1442 if (psli->iocbq_lookup)
1443 memcpy(new_arr, old_arr,
1444 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001445 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001446 psli->iocbq_lookup = new_arr;
1447 psli->iocbq_lookup_len = new_len;
1448 psli->last_iotag = iotag;
1449 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001450 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001451 iocbq->iotag = iotag;
1452 kfree(old_arr);
1453 return iotag;
1454 }
James Smart8f6d98d2006-08-01 07:34:00 -04001455 } else
James Smart2e0fef82007-06-17 19:56:36 -05001456 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001457
James Smartbc739052010-08-04 16:11:18 -04001458 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001459 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1460 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001461
James Bottomley604a3e32005-10-29 10:28:33 -05001462 return 0;
dea31012005-04-17 16:05:31 -05001463}
1464
James Smarte59058c2008-08-24 21:49:00 -04001465/**
James Smart3621a712009-04-06 18:47:14 -04001466 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001467 * @phba: Pointer to HBA context object.
1468 * @pring: Pointer to driver SLI ring object.
1469 * @iocb: Pointer to iocb slot in the ring.
1470 * @nextiocb: Pointer to driver iocb object which need to be
1471 * posted to firmware.
1472 *
1473 * This function is called with hbalock held to post a new iocb to
1474 * the firmware. This function copies the new iocb to ring iocb slot and
1475 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1476 * a completion call back for this iocb else the function will free the
1477 * iocb object.
1478 **/
dea31012005-04-17 16:05:31 -05001479static void
1480lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1481 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1482{
1483 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001484 * Set up an iotag
dea31012005-04-17 16:05:31 -05001485 */
James Bottomley604a3e32005-10-29 10:28:33 -05001486 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001487
James Smarte2a0a9d2008-12-04 22:40:02 -05001488
James Smarta58cbd52007-08-02 11:09:43 -04001489 if (pring->ringno == LPFC_ELS_RING) {
1490 lpfc_debugfs_slow_ring_trc(phba,
1491 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1492 *(((uint32_t *) &nextiocb->iocb) + 4),
1493 *(((uint32_t *) &nextiocb->iocb) + 6),
1494 *(((uint32_t *) &nextiocb->iocb) + 7));
1495 }
1496
dea31012005-04-17 16:05:31 -05001497 /*
1498 * Issue iocb command to adapter
1499 */
James Smart92d7f7b2007-06-17 19:56:38 -05001500 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001501 wmb();
1502 pring->stats.iocb_cmd++;
1503
1504 /*
1505 * If there is no completion routine to call, we can release the
1506 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1507 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1508 */
1509 if (nextiocb->iocb_cmpl)
1510 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001511 else
James Smart2e0fef82007-06-17 19:56:36 -05001512 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001513
1514 /*
1515 * Let the HBA know what IOCB slot will be the next one the
1516 * driver will put a command into.
1517 */
James Smart7e56aa22012-08-03 12:35:34 -04001518 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1519 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001520}
1521
James Smarte59058c2008-08-24 21:49:00 -04001522/**
James Smart3621a712009-04-06 18:47:14 -04001523 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001524 * @phba: Pointer to HBA context object.
1525 * @pring: Pointer to driver SLI ring object.
1526 *
1527 * The caller is not required to hold any lock for calling this function.
1528 * This function updates the chip attention bits for the ring to inform firmware
1529 * that there are pending work to be done for this ring and requests an
1530 * interrupt when there is space available in the ring. This function is
1531 * called when the driver is unable to post more iocbs to the ring due
1532 * to unavailability of space in the ring.
1533 **/
dea31012005-04-17 16:05:31 -05001534static void
James Smart2e0fef82007-06-17 19:56:36 -05001535lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001536{
1537 int ringno = pring->ringno;
1538
1539 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1540
1541 wmb();
1542
1543 /*
1544 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1545 * The HBA will tell us when an IOCB entry is available.
1546 */
1547 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1548 readl(phba->CAregaddr); /* flush */
1549
1550 pring->stats.iocb_cmd_full++;
1551}
1552
James Smarte59058c2008-08-24 21:49:00 -04001553/**
James Smart3621a712009-04-06 18:47:14 -04001554 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001555 * @phba: Pointer to HBA context object.
1556 * @pring: Pointer to driver SLI ring object.
1557 *
1558 * This function updates the chip attention register bit for the
1559 * given ring to inform HBA that there is more work to be done
1560 * in this ring. The caller is not required to hold any lock.
1561 **/
dea31012005-04-17 16:05:31 -05001562static void
James Smart2e0fef82007-06-17 19:56:36 -05001563lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001564{
1565 int ringno = pring->ringno;
1566
1567 /*
1568 * Tell the HBA that there is work to do in this ring.
1569 */
James Smart34b02dc2008-08-24 21:49:55 -04001570 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1571 wmb();
1572 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1573 readl(phba->CAregaddr); /* flush */
1574 }
dea31012005-04-17 16:05:31 -05001575}
1576
James Smarte59058c2008-08-24 21:49:00 -04001577/**
James Smart3621a712009-04-06 18:47:14 -04001578 * lpfc_sli_resume_iocb - Process iocbs in the txq
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 is called with hbalock held to post pending iocbs
1583 * in the txq to the firmware. This function is called when driver
1584 * detects space available in the ring.
1585 **/
dea31012005-04-17 16:05:31 -05001586static void
James Smart2e0fef82007-06-17 19:56:36 -05001587lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001588{
1589 IOCB_t *iocb;
1590 struct lpfc_iocbq *nextiocb;
1591
1592 /*
1593 * Check to see if:
1594 * (a) there is anything on the txq to send
1595 * (b) link is up
1596 * (c) link attention events can be processed (fcp ring only)
1597 * (d) IOCB processing is not blocked by the outstanding mbox command.
1598 */
1599 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001600 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001601 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001602 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001603
1604 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1605 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1606 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1607
1608 if (iocb)
1609 lpfc_sli_update_ring(phba, pring);
1610 else
1611 lpfc_sli_update_full_ring(phba, pring);
1612 }
1613
1614 return;
1615}
1616
James Smarte59058c2008-08-24 21:49:00 -04001617/**
James Smart3621a712009-04-06 18:47:14 -04001618 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001619 * @phba: Pointer to HBA context object.
1620 * @hbqno: HBQ number.
1621 *
1622 * This function is called with hbalock held to get the next
1623 * available slot for the given HBQ. If there is free slot
1624 * available for the HBQ it will return pointer to the next available
1625 * HBQ entry else it will return NULL.
1626 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001627static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001628lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1629{
1630 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1631
1632 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1633 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1634 hbqp->next_hbqPutIdx = 0;
1635
1636 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001637 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001638 uint32_t getidx = le32_to_cpu(raw_index);
1639
1640 hbqp->local_hbqGetIdx = getidx;
1641
1642 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1643 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001644 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001645 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001646 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001647 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001648 hbqp->entry_count);
1649
1650 phba->link_state = LPFC_HBA_ERROR;
1651 return NULL;
1652 }
1653
1654 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1655 return NULL;
1656 }
1657
James Smart51ef4c22007-08-02 11:10:31 -04001658 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1659 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001660}
1661
James Smarte59058c2008-08-24 21:49:00 -04001662/**
James Smart3621a712009-04-06 18:47:14 -04001663 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001664 * @phba: Pointer to HBA context object.
1665 *
1666 * This function is called with no lock held to free all the
1667 * hbq buffers while uninitializing the SLI interface. It also
1668 * frees the HBQ buffers returned by the firmware but not yet
1669 * processed by the upper layers.
1670 **/
James Smarted957682007-06-17 19:56:37 -05001671void
1672lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1673{
James Smart92d7f7b2007-06-17 19:56:38 -05001674 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1675 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001676 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001677 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001678 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001679
James Smart51ef4c22007-08-02 11:10:31 -04001680 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001681 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001682 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001683 for (i = 0; i < hbq_count; ++i) {
1684 list_for_each_entry_safe(dmabuf, next_dmabuf,
1685 &phba->hbqs[i].hbq_buffer_list, list) {
1686 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1687 list_del(&hbq_buf->dbuf.list);
1688 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1689 }
James Smarta8adb832007-10-27 13:37:53 -04001690 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001691 }
James Smart3163f722008-02-08 18:50:25 -05001692 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001693 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1694 list) {
James Smart3163f722008-02-08 18:50:25 -05001695 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1696 list_del(&hbq_buf->dbuf.list);
1697 if (hbq_buf->tag == -1) {
1698 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1699 (phba, hbq_buf);
1700 } else {
1701 hbqno = hbq_buf->tag >> 16;
1702 if (hbqno >= LPFC_MAX_HBQS)
1703 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1704 (phba, hbq_buf);
1705 else
1706 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1707 hbq_buf);
1708 }
1709 }
1710
1711 /* Mark the HBQs not in use */
1712 phba->hbq_in_use = 0;
1713 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001714}
1715
James Smarte59058c2008-08-24 21:49:00 -04001716/**
James Smart3621a712009-04-06 18:47:14 -04001717 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001718 * @phba: Pointer to HBA context object.
1719 * @hbqno: HBQ number.
1720 * @hbq_buf: Pointer to HBQ buffer.
1721 *
1722 * This function is called with the hbalock held to post a
1723 * hbq buffer to the firmware. If the function finds an empty
1724 * slot in the HBQ, it will post the buffer. The function will return
1725 * pointer to the hbq entry if it successfully post the buffer
1726 * else it will return NULL.
1727 **/
James Smart3772a992009-05-22 14:50:54 -04001728static int
James Smarted957682007-06-17 19:56:37 -05001729lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001730 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001731{
James Smart3772a992009-05-22 14:50:54 -04001732 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1733}
1734
1735/**
1736 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1737 * @phba: Pointer to HBA context object.
1738 * @hbqno: HBQ number.
1739 * @hbq_buf: Pointer to HBQ buffer.
1740 *
1741 * This function is called with the hbalock held to post a hbq buffer to the
1742 * firmware. If the function finds an empty slot in the HBQ, it will post the
1743 * buffer and place it on the hbq_buffer_list. The function will return zero if
1744 * it successfully post the buffer else it will return an error.
1745 **/
1746static int
1747lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1748 struct hbq_dmabuf *hbq_buf)
1749{
James Smarted957682007-06-17 19:56:37 -05001750 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001751 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001752
1753 /* Get next HBQ entry slot to use */
1754 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1755 if (hbqe) {
1756 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1757
James Smart92d7f7b2007-06-17 19:56:38 -05001758 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1759 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001760 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001761 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001762 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1763 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1764 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001765 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1766 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001767 /* flush */
James Smarted957682007-06-17 19:56:37 -05001768 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001769 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001770 return 0;
1771 } else
1772 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001773}
1774
James Smart4f774512009-05-22 14:52:35 -04001775/**
1776 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1777 * @phba: Pointer to HBA context object.
1778 * @hbqno: HBQ number.
1779 * @hbq_buf: Pointer to HBQ buffer.
1780 *
1781 * This function is called with the hbalock held to post an RQE to the SLI4
1782 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1783 * the hbq_buffer_list and return zero, otherwise it will return an error.
1784 **/
1785static int
1786lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1787 struct hbq_dmabuf *hbq_buf)
1788{
1789 int rc;
1790 struct lpfc_rqe hrqe;
1791 struct lpfc_rqe drqe;
1792
1793 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1794 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1795 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1796 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1797 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1798 &hrqe, &drqe);
1799 if (rc < 0)
1800 return rc;
1801 hbq_buf->tag = rc;
1802 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1803 return 0;
1804}
1805
James Smarte59058c2008-08-24 21:49:00 -04001806/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001807static struct lpfc_hbq_init lpfc_els_hbq = {
1808 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001809 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001810 .mask_count = 0,
1811 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001812 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001813 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001814 .init_count = 40,
1815 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001816};
James Smarted957682007-06-17 19:56:37 -05001817
James Smarte59058c2008-08-24 21:49:00 -04001818/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001819static struct lpfc_hbq_init lpfc_extra_hbq = {
1820 .rn = 1,
1821 .entry_count = 200,
1822 .mask_count = 0,
1823 .profile = 0,
1824 .ring_mask = (1 << LPFC_EXTRA_RING),
1825 .buffer_count = 0,
1826 .init_count = 0,
1827 .add_count = 5,
1828};
1829
James Smarte59058c2008-08-24 21:49:00 -04001830/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001831struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001832 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001833 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001834};
1835
James Smarte59058c2008-08-24 21:49:00 -04001836/**
James Smart3621a712009-04-06 18:47:14 -04001837 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001838 * @phba: Pointer to HBA context object.
1839 * @hbqno: HBQ number.
1840 * @count: Number of HBQ buffers to be posted.
1841 *
James Smartd7c255b2008-08-24 21:50:00 -04001842 * This function is called with no lock held to post more hbq buffers to the
1843 * given HBQ. The function returns the number of HBQ buffers successfully
1844 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001845 **/
James Smart311464e2007-08-02 11:10:37 -04001846static int
James Smart92d7f7b2007-06-17 19:56:38 -05001847lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1848{
James Smartd7c255b2008-08-24 21:50:00 -04001849 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001850 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001851 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001852 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001853 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001854 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001855
James Smartd7c255b2008-08-24 21:50:00 -04001856 if ((phba->hbqs[hbqno].buffer_count + count) >
1857 lpfc_hbq_defs[hbqno]->entry_count)
1858 count = lpfc_hbq_defs[hbqno]->entry_count -
1859 phba->hbqs[hbqno].buffer_count;
1860 if (!count)
1861 return 0;
1862 /* Allocate HBQ entries */
1863 for (i = 0; i < count; i++) {
1864 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1865 if (!hbq_buffer)
1866 break;
1867 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1868 }
James Smart3163f722008-02-08 18:50:25 -05001869 /* Check whether HBQ is still in use */
1870 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001871 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001872 goto err;
1873 while (!list_empty(&hbq_buf_list)) {
1874 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1875 dbuf.list);
1876 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1877 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001878 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001879 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001880 posted++;
1881 } else
James Smart51ef4c22007-08-02 11:10:31 -04001882 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001883 }
James Smart3163f722008-02-08 18:50:25 -05001884 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001885 return posted;
1886err:
1887 spin_unlock_irqrestore(&phba->hbalock, flags);
1888 while (!list_empty(&hbq_buf_list)) {
1889 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1890 dbuf.list);
1891 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1892 }
James Smart92d7f7b2007-06-17 19:56:38 -05001893 return 0;
James Smarted957682007-06-17 19:56:37 -05001894}
1895
James Smarte59058c2008-08-24 21:49:00 -04001896/**
James Smart3621a712009-04-06 18:47:14 -04001897 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001898 * @phba: Pointer to HBA context object.
1899 * @qno: HBQ number.
1900 *
1901 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001902 * is called with no lock held. The function returns the number of HBQ entries
1903 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001904 **/
James Smarted957682007-06-17 19:56:37 -05001905int
James Smart92d7f7b2007-06-17 19:56:38 -05001906lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001907{
James Smartdef9c7a2009-12-21 17:02:28 -05001908 if (phba->sli_rev == LPFC_SLI_REV4)
1909 return 0;
1910 else
1911 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1912 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001913}
1914
James Smarte59058c2008-08-24 21:49:00 -04001915/**
James Smart3621a712009-04-06 18:47:14 -04001916 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001917 * @phba: Pointer to HBA context object.
1918 * @qno: HBQ queue number.
1919 *
1920 * This function is called from SLI initialization code path with
1921 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001922 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001923 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001924static int
James Smart92d7f7b2007-06-17 19:56:38 -05001925lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001926{
James Smartdef9c7a2009-12-21 17:02:28 -05001927 if (phba->sli_rev == LPFC_SLI_REV4)
1928 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001929 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001930 else
1931 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1932 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001933}
1934
James Smarte59058c2008-08-24 21:49:00 -04001935/**
James Smart3772a992009-05-22 14:50:54 -04001936 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1937 * @phba: Pointer to HBA context object.
1938 * @hbqno: HBQ number.
1939 *
1940 * This function removes the first hbq buffer on an hbq list and returns a
1941 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1942 **/
1943static struct hbq_dmabuf *
1944lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1945{
1946 struct lpfc_dmabuf *d_buf;
1947
1948 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1949 if (!d_buf)
1950 return NULL;
1951 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1952}
1953
1954/**
James Smart3621a712009-04-06 18:47:14 -04001955 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001956 * @phba: Pointer to HBA context object.
1957 * @tag: Tag of the hbq buffer.
1958 *
1959 * This function is called with hbalock held. This function searches
1960 * for the hbq buffer associated with the given tag in the hbq buffer
1961 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1962 * it returns NULL.
1963 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001964static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001965lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1966{
James Smart92d7f7b2007-06-17 19:56:38 -05001967 struct lpfc_dmabuf *d_buf;
1968 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001969 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001970
James Smart51ef4c22007-08-02 11:10:31 -04001971 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001972 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001973 return NULL;
1974
James Smart3772a992009-05-22 14:50:54 -04001975 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001976 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001977 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001978 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001979 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001980 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001981 }
1982 }
James Smart3772a992009-05-22 14:50:54 -04001983 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001985 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001986 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001987 return NULL;
James Smarted957682007-06-17 19:56:37 -05001988}
1989
James Smarte59058c2008-08-24 21:49:00 -04001990/**
James Smart3621a712009-04-06 18:47:14 -04001991 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001992 * @phba: Pointer to HBA context object.
1993 * @hbq_buffer: Pointer to HBQ buffer.
1994 *
1995 * This function is called with hbalock. This function gives back
1996 * the hbq buffer to firmware. If the HBQ does not have space to
1997 * post the buffer, it will free the buffer.
1998 **/
James Smarted957682007-06-17 19:56:37 -05001999void
James Smart51ef4c22007-08-02 11:10:31 -04002000lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002001{
2002 uint32_t hbqno;
2003
James Smart51ef4c22007-08-02 11:10:31 -04002004 if (hbq_buffer) {
2005 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002006 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002007 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002008 }
2009}
2010
James Smarte59058c2008-08-24 21:49:00 -04002011/**
James Smart3621a712009-04-06 18:47:14 -04002012 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002013 * @mbxCommand: mailbox command code.
2014 *
2015 * This function is called by the mailbox event handler function to verify
2016 * that the completed mailbox command is a legitimate mailbox command. If the
2017 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2018 * and the mailbox event handler will take the HBA offline.
2019 **/
dea31012005-04-17 16:05:31 -05002020static int
2021lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2022{
2023 uint8_t ret;
2024
2025 switch (mbxCommand) {
2026 case MBX_LOAD_SM:
2027 case MBX_READ_NV:
2028 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002029 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002030 case MBX_RUN_BIU_DIAG:
2031 case MBX_INIT_LINK:
2032 case MBX_DOWN_LINK:
2033 case MBX_CONFIG_LINK:
2034 case MBX_CONFIG_RING:
2035 case MBX_RESET_RING:
2036 case MBX_READ_CONFIG:
2037 case MBX_READ_RCONFIG:
2038 case MBX_READ_SPARM:
2039 case MBX_READ_STATUS:
2040 case MBX_READ_RPI:
2041 case MBX_READ_XRI:
2042 case MBX_READ_REV:
2043 case MBX_READ_LNK_STAT:
2044 case MBX_REG_LOGIN:
2045 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002046 case MBX_CLEAR_LA:
2047 case MBX_DUMP_MEMORY:
2048 case MBX_DUMP_CONTEXT:
2049 case MBX_RUN_DIAGS:
2050 case MBX_RESTART:
2051 case MBX_UPDATE_CFG:
2052 case MBX_DOWN_LOAD:
2053 case MBX_DEL_LD_ENTRY:
2054 case MBX_RUN_PROGRAM:
2055 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002056 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002057 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002058 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002059 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002060 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002061 case MBX_LOAD_AREA:
2062 case MBX_RUN_BIU_DIAG64:
2063 case MBX_CONFIG_PORT:
2064 case MBX_READ_SPARM64:
2065 case MBX_READ_RPI64:
2066 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002067 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002068 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002069 case MBX_SET_DEBUG:
2070 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002071 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002072 case MBX_REG_VPI:
2073 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002074 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002075 case MBX_PORT_CAPABILITIES:
2076 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002077 case MBX_SLI4_CONFIG:
2078 case MBX_SLI4_REQ_FTRS:
2079 case MBX_REG_FCFI:
2080 case MBX_UNREG_FCFI:
2081 case MBX_REG_VFI:
2082 case MBX_UNREG_VFI:
2083 case MBX_INIT_VPI:
2084 case MBX_INIT_VFI:
2085 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002086 case MBX_READ_EVENT_LOG_STATUS:
2087 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002088 case MBX_SECURITY_MGMT:
2089 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002090 ret = mbxCommand;
2091 break;
2092 default:
2093 ret = MBX_SHUTDOWN;
2094 break;
2095 }
James Smart2e0fef82007-06-17 19:56:36 -05002096 return ret;
dea31012005-04-17 16:05:31 -05002097}
James Smarte59058c2008-08-24 21:49:00 -04002098
2099/**
James Smart3621a712009-04-06 18:47:14 -04002100 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002101 * @phba: Pointer to HBA context object.
2102 * @pmboxq: Pointer to mailbox command.
2103 *
2104 * This is completion handler function for mailbox commands issued from
2105 * lpfc_sli_issue_mbox_wait function. This function is called by the
2106 * mailbox event handler function with no lock held. This function
2107 * will wake up thread waiting on the wait queue pointed by context1
2108 * of the mailbox.
2109 **/
James Smart04c68492009-05-22 14:52:52 -04002110void
James Smart2e0fef82007-06-17 19:56:36 -05002111lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002112{
2113 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002114 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002115
2116 /*
2117 * If pdone_q is empty, the driver thread gave up waiting and
2118 * continued running.
2119 */
James Smart7054a602007-04-25 09:52:34 -04002120 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002121 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002122 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2123 if (pdone_q)
2124 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002125 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002126 return;
2127}
2128
James Smarte59058c2008-08-24 21:49:00 -04002129
2130/**
James Smart3621a712009-04-06 18:47:14 -04002131 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002132 * @phba: Pointer to HBA context object.
2133 * @pmb: Pointer to mailbox object.
2134 *
2135 * This function is the default mailbox completion handler. It
2136 * frees the memory resources associated with the completed mailbox
2137 * command. If the completed command is a REG_LOGIN mailbox command,
2138 * this function will issue a UREG_LOGIN to re-claim the RPI.
2139 **/
dea31012005-04-17 16:05:31 -05002140void
James Smart2e0fef82007-06-17 19:56:36 -05002141lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002142{
James Smartd439d282010-09-29 11:18:45 -04002143 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002144 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002145 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002146 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002147 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002148 int rc;
2149
dea31012005-04-17 16:05:31 -05002150 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002151
dea31012005-04-17 16:05:31 -05002152 if (mp) {
2153 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2154 kfree(mp);
2155 }
James Smart7054a602007-04-25 09:52:34 -04002156
2157 /*
2158 * If a REG_LOGIN succeeded after node is destroyed or node
2159 * is in re-discovery driver need to cleanup the RPI.
2160 */
James Smart2e0fef82007-06-17 19:56:36 -05002161 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002162 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2163 !pmb->u.mb.mbxStatus) {
2164 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002165 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002166 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002167 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002168 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2169 if (rc != MBX_NOT_FINISHED)
2170 return;
2171 }
2172
James Smart695a8142010-01-26 23:08:03 -05002173 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2174 !(phba->pport->load_flag & FC_UNLOADING) &&
2175 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002176 shost = lpfc_shost_from_vport(vport);
2177 spin_lock_irq(shost->host_lock);
2178 vport->vpi_state |= LPFC_VPI_REGISTERED;
2179 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2180 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002181 }
2182
James Smartd439d282010-09-29 11:18:45 -04002183 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2184 ndlp = (struct lpfc_nodelist *)pmb->context2;
2185 lpfc_nlp_put(ndlp);
2186 pmb->context2 = NULL;
2187 }
2188
James Smartdcf2a4e2010-09-29 11:18:53 -04002189 /* Check security permission status on INIT_LINK mailbox command */
2190 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2191 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2193 "2860 SLI authentication is required "
2194 "for INIT_LINK but has not done yet\n");
2195
James Smart04c68492009-05-22 14:52:52 -04002196 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2197 lpfc_sli4_mbox_cmd_free(phba, pmb);
2198 else
2199 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002200}
2201
James Smarte59058c2008-08-24 21:49:00 -04002202/**
James Smart3621a712009-04-06 18:47:14 -04002203 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002204 * @phba: Pointer to HBA context object.
2205 *
2206 * This function is called with no lock held. This function processes all
2207 * the completed mailbox commands and gives it to upper layers. The interrupt
2208 * service routine processes mailbox completion interrupt and adds completed
2209 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2210 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2211 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2212 * function returns the mailbox commands to the upper layer by calling the
2213 * completion handler function of each mailbox.
2214 **/
dea31012005-04-17 16:05:31 -05002215int
James Smart2e0fef82007-06-17 19:56:36 -05002216lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002217{
James Smart92d7f7b2007-06-17 19:56:38 -05002218 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002219 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002220 int rc;
2221 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002222
2223 phba->sli.slistat.mbox_event++;
2224
James Smart92d7f7b2007-06-17 19:56:38 -05002225 /* Get all completed mailboxe buffers into the cmplq */
2226 spin_lock_irq(&phba->hbalock);
2227 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2228 spin_unlock_irq(&phba->hbalock);
2229
dea31012005-04-17 16:05:31 -05002230 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002231 do {
2232 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2233 if (pmb == NULL)
2234 break;
2235
James Smart04c68492009-05-22 14:52:52 -04002236 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002237
James Smart858c9f62007-06-17 19:56:39 -05002238 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2239 if (pmb->vport) {
2240 lpfc_debugfs_disc_trc(pmb->vport,
2241 LPFC_DISC_TRC_MBOX_VPORT,
2242 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2243 (uint32_t)pmbox->mbxCommand,
2244 pmbox->un.varWords[0],
2245 pmbox->un.varWords[1]);
2246 }
2247 else {
2248 lpfc_debugfs_disc_trc(phba->pport,
2249 LPFC_DISC_TRC_MBOX,
2250 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2251 (uint32_t)pmbox->mbxCommand,
2252 pmbox->un.varWords[0],
2253 pmbox->un.varWords[1]);
2254 }
2255 }
2256
dea31012005-04-17 16:05:31 -05002257 /*
2258 * It is a fatal error if unknown mbox command completion.
2259 */
2260 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2261 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002262 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002264 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002265 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002266 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002267 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002268 lpfc_sli_config_mbox_subsys_get(phba,
2269 pmb),
2270 lpfc_sli_config_mbox_opcode_get(phba,
2271 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002272 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002273 phba->work_hs = HS_FFER3;
2274 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002275 continue;
dea31012005-04-17 16:05:31 -05002276 }
2277
dea31012005-04-17 16:05:31 -05002278 if (pmbox->mbxStatus) {
2279 phba->sli.slistat.mbox_stat_err++;
2280 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2281 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002282 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002283 LOG_MBOX | LOG_SLI,
2284 "(%d):0305 Mbox cmd cmpl "
2285 "error - RETRYing Data: x%x "
2286 "(x%x/x%x) x%x x%x x%x\n",
2287 pmb->vport ? pmb->vport->vpi : 0,
2288 pmbox->mbxCommand,
2289 lpfc_sli_config_mbox_subsys_get(phba,
2290 pmb),
2291 lpfc_sli_config_mbox_opcode_get(phba,
2292 pmb),
2293 pmbox->mbxStatus,
2294 pmbox->un.varWords[0],
2295 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002296 pmbox->mbxStatus = 0;
2297 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002298 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002299 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002300 continue;
dea31012005-04-17 16:05:31 -05002301 }
2302 }
2303
2304 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002305 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002306 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002307 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002308 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002309 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002310 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2311 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002312 pmb->mbox_cmpl,
2313 *((uint32_t *) pmbox),
2314 pmbox->un.varWords[0],
2315 pmbox->un.varWords[1],
2316 pmbox->un.varWords[2],
2317 pmbox->un.varWords[3],
2318 pmbox->un.varWords[4],
2319 pmbox->un.varWords[5],
2320 pmbox->un.varWords[6],
2321 pmbox->un.varWords[7]);
2322
James Smart92d7f7b2007-06-17 19:56:38 -05002323 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002324 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002325 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002326 return 0;
dea31012005-04-17 16:05:31 -05002327}
James Smart92d7f7b2007-06-17 19:56:38 -05002328
James Smarte59058c2008-08-24 21:49:00 -04002329/**
James Smart3621a712009-04-06 18:47:14 -04002330 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002331 * @phba: Pointer to HBA context object.
2332 * @pring: Pointer to driver SLI ring object.
2333 * @tag: buffer tag.
2334 *
2335 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2336 * is set in the tag the buffer is posted for a particular exchange,
2337 * the function will return the buffer without replacing the buffer.
2338 * If the buffer is for unsolicited ELS or CT traffic, this function
2339 * returns the buffer and also posts another buffer to the firmware.
2340 **/
James Smart76bb24e2007-10-27 13:38:00 -04002341static struct lpfc_dmabuf *
2342lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002343 struct lpfc_sli_ring *pring,
2344 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002345{
James Smart9f1e1b52008-12-04 22:39:40 -05002346 struct hbq_dmabuf *hbq_entry;
2347
James Smart76bb24e2007-10-27 13:38:00 -04002348 if (tag & QUE_BUFTAG_BIT)
2349 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002350 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2351 if (!hbq_entry)
2352 return NULL;
2353 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002354}
James Smart57127f12007-10-27 13:37:05 -04002355
James Smart3772a992009-05-22 14:50:54 -04002356/**
2357 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2358 * @phba: Pointer to HBA context object.
2359 * @pring: Pointer to driver SLI ring object.
2360 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2361 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2362 * @fch_type: the type for the first frame of the sequence.
2363 *
2364 * This function is called with no lock held. This function uses the r_ctl and
2365 * type of the received sequence to find the correct callback function to call
2366 * to process the sequence.
2367 **/
2368static int
2369lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2370 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2371 uint32_t fch_type)
2372{
2373 int i;
2374
2375 /* unSolicited Responses */
2376 if (pring->prt[0].profile) {
2377 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2378 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2379 saveq);
2380 return 1;
2381 }
2382 /* We must search, based on rctl / type
2383 for the right routine */
2384 for (i = 0; i < pring->num_mask; i++) {
2385 if ((pring->prt[i].rctl == fch_r_ctl) &&
2386 (pring->prt[i].type == fch_type)) {
2387 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2388 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2389 (phba, pring, saveq);
2390 return 1;
2391 }
2392 }
2393 return 0;
2394}
James Smarte59058c2008-08-24 21:49:00 -04002395
2396/**
James Smart3621a712009-04-06 18:47:14 -04002397 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002398 * @phba: Pointer to HBA context object.
2399 * @pring: Pointer to driver SLI ring object.
2400 * @saveq: Pointer to the unsolicited iocb.
2401 *
2402 * This function is called with no lock held by the ring event handler
2403 * when there is an unsolicited iocb posted to the response ring by the
2404 * firmware. This function gets the buffer associated with the iocbs
2405 * and calls the event handler for the ring. This function handles both
2406 * qring buffers and hbq buffers.
2407 * When the function returns 1 the caller can free the iocb object otherwise
2408 * upper layer functions will free the iocb objects.
2409 **/
dea31012005-04-17 16:05:31 -05002410static int
2411lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2412 struct lpfc_iocbq *saveq)
2413{
2414 IOCB_t * irsp;
2415 WORD5 * w5p;
2416 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002417 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002418 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002419 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002420
2421 match = 0;
2422 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002423
2424 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2425 if (pring->lpfc_sli_rcv_async_status)
2426 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2427 else
2428 lpfc_printf_log(phba,
2429 KERN_WARNING,
2430 LOG_SLI,
2431 "0316 Ring %d handler: unexpected "
2432 "ASYNC_STATUS iocb received evt_code "
2433 "0x%x\n",
2434 pring->ringno,
2435 irsp->un.asyncstat.evt_code);
2436 return 1;
2437 }
2438
James Smart3163f722008-02-08 18:50:25 -05002439 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2440 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2441 if (irsp->ulpBdeCount > 0) {
2442 dmzbuf = lpfc_sli_get_buff(phba, pring,
2443 irsp->un.ulpWord[3]);
2444 lpfc_in_buf_free(phba, dmzbuf);
2445 }
2446
2447 if (irsp->ulpBdeCount > 1) {
2448 dmzbuf = lpfc_sli_get_buff(phba, pring,
2449 irsp->unsli3.sli3Words[3]);
2450 lpfc_in_buf_free(phba, dmzbuf);
2451 }
2452
2453 if (irsp->ulpBdeCount > 2) {
2454 dmzbuf = lpfc_sli_get_buff(phba, pring,
2455 irsp->unsli3.sli3Words[7]);
2456 lpfc_in_buf_free(phba, dmzbuf);
2457 }
2458
2459 return 1;
2460 }
2461
James Smart92d7f7b2007-06-17 19:56:38 -05002462 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002463 if (irsp->ulpBdeCount != 0) {
2464 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002465 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002466 if (!saveq->context2)
2467 lpfc_printf_log(phba,
2468 KERN_ERR,
2469 LOG_SLI,
2470 "0341 Ring %d Cannot find buffer for "
2471 "an unsolicited iocb. tag 0x%x\n",
2472 pring->ringno,
2473 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002474 }
2475 if (irsp->ulpBdeCount == 2) {
2476 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002477 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002478 if (!saveq->context3)
2479 lpfc_printf_log(phba,
2480 KERN_ERR,
2481 LOG_SLI,
2482 "0342 Ring %d Cannot find buffer for an"
2483 " unsolicited iocb. tag 0x%x\n",
2484 pring->ringno,
2485 irsp->unsli3.sli3Words[7]);
2486 }
2487 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002488 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002489 if (irsp->ulpBdeCount != 0) {
2490 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2491 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002492 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002493 lpfc_printf_log(phba,
2494 KERN_ERR,
2495 LOG_SLI,
2496 "0343 Ring %d Cannot find "
2497 "buffer for an unsolicited iocb"
2498 ". tag 0x%x\n", pring->ringno,
2499 irsp->un.ulpWord[3]);
2500 }
2501 if (irsp->ulpBdeCount == 2) {
2502 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2503 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002504 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002505 lpfc_printf_log(phba,
2506 KERN_ERR,
2507 LOG_SLI,
2508 "0344 Ring %d Cannot find "
2509 "buffer for an unsolicited "
2510 "iocb. tag 0x%x\n",
2511 pring->ringno,
2512 irsp->unsli3.sli3Words[7]);
2513 }
2514 }
James Smart92d7f7b2007-06-17 19:56:38 -05002515 }
James Smart9c2face2008-01-11 01:53:18 -05002516 if (irsp->ulpBdeCount != 0 &&
2517 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2518 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2519 int found = 0;
2520
2521 /* search continue save q for same XRI */
2522 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002523 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2524 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002525 list_add_tail(&saveq->list, &iocbq->list);
2526 found = 1;
2527 break;
2528 }
2529 }
2530 if (!found)
2531 list_add_tail(&saveq->clist,
2532 &pring->iocb_continue_saveq);
2533 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2534 list_del_init(&iocbq->clist);
2535 saveq = iocbq;
2536 irsp = &(saveq->iocb);
2537 } else
2538 return 0;
2539 }
2540 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2541 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2542 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002543 Rctl = FC_RCTL_ELS_REQ;
2544 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002545 } else {
2546 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2547 Rctl = w5p->hcsw.Rctl;
2548 Type = w5p->hcsw.Type;
2549
2550 /* Firmware Workaround */
2551 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2552 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2553 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002554 Rctl = FC_RCTL_ELS_REQ;
2555 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002556 w5p->hcsw.Rctl = Rctl;
2557 w5p->hcsw.Type = Type;
2558 }
2559 }
James Smart92d7f7b2007-06-17 19:56:38 -05002560
James Smart3772a992009-05-22 14:50:54 -04002561 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002562 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002563 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002564 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002565 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002566
James Smart92d7f7b2007-06-17 19:56:38 -05002567 return 1;
dea31012005-04-17 16:05:31 -05002568}
2569
James Smarte59058c2008-08-24 21:49:00 -04002570/**
James Smart3621a712009-04-06 18:47:14 -04002571 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002572 * @phba: Pointer to HBA context object.
2573 * @pring: Pointer to driver SLI ring object.
2574 * @prspiocb: Pointer to response iocb object.
2575 *
2576 * This function looks up the iocb_lookup table to get the command iocb
2577 * corresponding to the given response iocb using the iotag of the
2578 * response iocb. This function is called with the hbalock held.
2579 * This function returns the command iocb object if it finds the command
2580 * iocb else returns NULL.
2581 **/
dea31012005-04-17 16:05:31 -05002582static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002583lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2584 struct lpfc_sli_ring *pring,
2585 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002586{
dea31012005-04-17 16:05:31 -05002587 struct lpfc_iocbq *cmd_iocb = NULL;
2588 uint16_t iotag;
2589
James Bottomley604a3e32005-10-29 10:28:33 -05002590 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002591
James Bottomley604a3e32005-10-29 10:28:33 -05002592 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2593 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002594 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002595 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart2a9bf3d2010-06-07 15:24:45 -04002596 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002597 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002598 }
James Bottomley604a3e32005-10-29 10:28:33 -05002599 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002600 }
2601
dea31012005-04-17 16:05:31 -05002602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002603 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002604 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002605 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002606 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002607 return NULL;
2608}
2609
James Smarte59058c2008-08-24 21:49:00 -04002610/**
James Smart3772a992009-05-22 14:50:54 -04002611 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2612 * @phba: Pointer to HBA context object.
2613 * @pring: Pointer to driver SLI ring object.
2614 * @iotag: IOCB tag.
2615 *
2616 * This function looks up the iocb_lookup table to get the command iocb
2617 * corresponding to the given iotag. This function is called with the
2618 * hbalock held.
2619 * This function returns the command iocb object if it finds the command
2620 * iocb else returns NULL.
2621 **/
2622static struct lpfc_iocbq *
2623lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2624 struct lpfc_sli_ring *pring, uint16_t iotag)
2625{
2626 struct lpfc_iocbq *cmd_iocb;
2627
2628 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2629 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002630 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2631 /* remove from txcmpl queue list */
2632 list_del_init(&cmd_iocb->list);
2633 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002634 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002635 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002636 }
James Smart3772a992009-05-22 14:50:54 -04002637 }
James Smart3772a992009-05-22 14:50:54 -04002638 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2639 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2640 iotag, phba->sli.last_iotag);
2641 return NULL;
2642}
2643
2644/**
James Smart3621a712009-04-06 18:47:14 -04002645 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002646 * @phba: Pointer to HBA context object.
2647 * @pring: Pointer to driver SLI ring object.
2648 * @saveq: Pointer to the response iocb to be processed.
2649 *
2650 * This function is called by the ring event handler for non-fcp
2651 * rings when there is a new response iocb in the response ring.
2652 * The caller is not required to hold any locks. This function
2653 * gets the command iocb associated with the response iocb and
2654 * calls the completion handler for the command iocb. If there
2655 * is no completion handler, the function will free the resources
2656 * associated with command iocb. If the response iocb is for
2657 * an already aborted command iocb, the status of the completion
2658 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2659 * This function always returns 1.
2660 **/
dea31012005-04-17 16:05:31 -05002661static int
James Smart2e0fef82007-06-17 19:56:36 -05002662lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002663 struct lpfc_iocbq *saveq)
2664{
James Smart2e0fef82007-06-17 19:56:36 -05002665 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002666 int rc = 1;
2667 unsigned long iflag;
2668
2669 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002670 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002671 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002672 spin_unlock_irqrestore(&phba->hbalock, iflag);
2673
dea31012005-04-17 16:05:31 -05002674 if (cmdiocbp) {
2675 if (cmdiocbp->iocb_cmpl) {
2676 /*
James Smartea2151b2008-09-07 11:52:10 -04002677 * If an ELS command failed send an event to mgmt
2678 * application.
2679 */
2680 if (saveq->iocb.ulpStatus &&
2681 (pring->ringno == LPFC_ELS_RING) &&
2682 (cmdiocbp->iocb.ulpCommand ==
2683 CMD_ELS_REQUEST64_CR))
2684 lpfc_send_els_failure_event(phba,
2685 cmdiocbp, saveq);
2686
2687 /*
dea31012005-04-17 16:05:31 -05002688 * Post all ELS completions to the worker thread.
2689 * All other are passed to the completion callback.
2690 */
2691 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002692 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2693 (cmdiocbp->iocb_flag &
2694 LPFC_DRIVER_ABORTED)) {
2695 spin_lock_irqsave(&phba->hbalock,
2696 iflag);
James Smart07951072007-04-25 09:51:38 -04002697 cmdiocbp->iocb_flag &=
2698 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002699 spin_unlock_irqrestore(&phba->hbalock,
2700 iflag);
James Smart07951072007-04-25 09:51:38 -04002701 saveq->iocb.ulpStatus =
2702 IOSTAT_LOCAL_REJECT;
2703 saveq->iocb.un.ulpWord[4] =
2704 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002705
2706 /* Firmware could still be in progress
2707 * of DMAing payload, so don't free data
2708 * buffer till after a hbeat.
2709 */
James Smart341af102010-01-26 23:07:37 -05002710 spin_lock_irqsave(&phba->hbalock,
2711 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002712 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002713 spin_unlock_irqrestore(&phba->hbalock,
2714 iflag);
2715 }
James Smart0f65ff62010-02-26 14:14:23 -05002716 if (phba->sli_rev == LPFC_SLI_REV4) {
2717 if (saveq->iocb_flag &
2718 LPFC_EXCHANGE_BUSY) {
2719 /* Set cmdiocb flag for the
2720 * exchange busy so sgl (xri)
2721 * will not be released until
2722 * the abort xri is received
2723 * from hba.
2724 */
2725 spin_lock_irqsave(
2726 &phba->hbalock, iflag);
2727 cmdiocbp->iocb_flag |=
2728 LPFC_EXCHANGE_BUSY;
2729 spin_unlock_irqrestore(
2730 &phba->hbalock, iflag);
2731 }
2732 if (cmdiocbp->iocb_flag &
2733 LPFC_DRIVER_ABORTED) {
2734 /*
2735 * Clear LPFC_DRIVER_ABORTED
2736 * bit in case it was driver
2737 * initiated abort.
2738 */
2739 spin_lock_irqsave(
2740 &phba->hbalock, iflag);
2741 cmdiocbp->iocb_flag &=
2742 ~LPFC_DRIVER_ABORTED;
2743 spin_unlock_irqrestore(
2744 &phba->hbalock, iflag);
2745 cmdiocbp->iocb.ulpStatus =
2746 IOSTAT_LOCAL_REJECT;
2747 cmdiocbp->iocb.un.ulpWord[4] =
2748 IOERR_ABORT_REQUESTED;
2749 /*
2750 * For SLI4, irsiocb contains
2751 * NO_XRI in sli_xritag, it
2752 * shall not affect releasing
2753 * sgl (xri) process.
2754 */
2755 saveq->iocb.ulpStatus =
2756 IOSTAT_LOCAL_REJECT;
2757 saveq->iocb.un.ulpWord[4] =
2758 IOERR_SLI_ABORTED;
2759 spin_lock_irqsave(
2760 &phba->hbalock, iflag);
2761 saveq->iocb_flag |=
2762 LPFC_DELAY_MEM_FREE;
2763 spin_unlock_irqrestore(
2764 &phba->hbalock, iflag);
2765 }
James Smart07951072007-04-25 09:51:38 -04002766 }
dea31012005-04-17 16:05:31 -05002767 }
James Smart2e0fef82007-06-17 19:56:36 -05002768 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002769 } else
2770 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002771 } else {
2772 /*
2773 * Unknown initiating command based on the response iotag.
2774 * This could be the case on the ELS ring because of
2775 * lpfc_els_abort().
2776 */
2777 if (pring->ringno != LPFC_ELS_RING) {
2778 /*
2779 * Ring <ringno> handler: unexpected completion IoTag
2780 * <IoTag>
2781 */
James Smarta257bf92009-04-06 18:48:10 -04002782 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002783 "0322 Ring %d handler: "
2784 "unexpected completion IoTag x%x "
2785 "Data: x%x x%x x%x x%x\n",
2786 pring->ringno,
2787 saveq->iocb.ulpIoTag,
2788 saveq->iocb.ulpStatus,
2789 saveq->iocb.un.ulpWord[4],
2790 saveq->iocb.ulpCommand,
2791 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002792 }
2793 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002794
dea31012005-04-17 16:05:31 -05002795 return rc;
2796}
2797
James Smarte59058c2008-08-24 21:49:00 -04002798/**
James Smart3621a712009-04-06 18:47:14 -04002799 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002800 * @phba: Pointer to HBA context object.
2801 * @pring: Pointer to driver SLI ring object.
2802 *
2803 * This function is called from the iocb ring event handlers when
2804 * put pointer is ahead of the get pointer for a ring. This function signal
2805 * an error attention condition to the worker thread and the worker
2806 * thread will transition the HBA to offline state.
2807 **/
James Smart2e0fef82007-06-17 19:56:36 -05002808static void
2809lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002810{
James Smart34b02dc2008-08-24 21:49:55 -04002811 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002812 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002813 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002814 * rsp ring <portRspMax>
2815 */
2816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002817 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002818 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002819 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002820 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002821
James Smart2e0fef82007-06-17 19:56:36 -05002822 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002823
2824 /*
2825 * All error attention handlers are posted to
2826 * worker thread
2827 */
2828 phba->work_ha |= HA_ERATT;
2829 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002830
James Smart5e9d9b82008-06-14 22:52:53 -04002831 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002832
2833 return;
2834}
2835
James Smarte59058c2008-08-24 21:49:00 -04002836/**
James Smart3621a712009-04-06 18:47:14 -04002837 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002838 * @ptr: Pointer to address of HBA context object.
2839 *
2840 * This function is invoked by the Error Attention polling timer when the
2841 * timer times out. It will check the SLI Error Attention register for
2842 * possible attention events. If so, it will post an Error Attention event
2843 * and wake up worker thread to process it. Otherwise, it will set up the
2844 * Error Attention polling timer for the next poll.
2845 **/
2846void lpfc_poll_eratt(unsigned long ptr)
2847{
2848 struct lpfc_hba *phba;
James Smartaa6fbb72012-08-03 12:36:03 -04002849 uint32_t eratt = 0, rem;
2850 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002851
2852 phba = (struct lpfc_hba *)ptr;
2853
James Smartaa6fbb72012-08-03 12:36:03 -04002854 /* Here we will also keep track of interrupts per sec of the hba */
2855 sli_intr = phba->sli.slistat.sli_intr;
2856
2857 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2858 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2859 sli_intr);
2860 else
2861 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2862
2863 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2864 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2865 phba->sli.slistat.sli_ips = cnt;
2866
2867 phba->sli.slistat.sli_prev_intr = sli_intr;
2868
James Smart93996272008-08-24 21:50:30 -04002869 /* Check chip HA register for error event */
2870 eratt = lpfc_sli_check_eratt(phba);
2871
2872 if (eratt)
2873 /* Tell the worker thread there is work to do */
2874 lpfc_worker_wake_up(phba);
2875 else
2876 /* Restart the timer for next eratt poll */
2877 mod_timer(&phba->eratt_poll, jiffies +
2878 HZ * LPFC_ERATT_POLL_INTERVAL);
2879 return;
2880}
2881
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002882
James Smarte59058c2008-08-24 21:49:00 -04002883/**
James Smart3621a712009-04-06 18:47:14 -04002884 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002885 * @phba: Pointer to HBA context object.
2886 * @pring: Pointer to driver SLI ring object.
2887 * @mask: Host attention register mask for this ring.
2888 *
2889 * This function is called from the interrupt context when there is a ring
2890 * event for the fcp ring. The caller does not hold any lock.
2891 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002892 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002893 * LE bit set. The function will call the completion handler of the command iocb
2894 * if the response iocb indicates a completion for a command iocb or it is
2895 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2896 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002897 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002898 * to check it explicitly.
2899 */
2900int
James Smart2e0fef82007-06-17 19:56:36 -05002901lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2902 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002903{
James Smart34b02dc2008-08-24 21:49:55 -04002904 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002905 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002906 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002907 struct lpfc_iocbq *cmdiocbq = NULL;
2908 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002909 uint32_t status;
2910 uint32_t portRspPut, portRspMax;
2911 int rc = 1;
2912 lpfc_iocb_type type;
2913 unsigned long iflag;
2914 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002915
James Smart2e0fef82007-06-17 19:56:36 -05002916 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002917 pring->stats.iocb_event++;
2918
dea31012005-04-17 16:05:31 -05002919 /*
2920 * The next available response entry should never exceed the maximum
2921 * entries. If it does, treat it as an adapter hardware error.
2922 */
James Smart7e56aa22012-08-03 12:35:34 -04002923 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002924 portRspPut = le32_to_cpu(pgp->rspPutInx);
2925 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002926 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002927 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002928 return 1;
2929 }
James Smart45ed1192009-10-02 15:17:02 -04002930 if (phba->fcp_ring_in_use) {
2931 spin_unlock_irqrestore(&phba->hbalock, iflag);
2932 return 1;
2933 } else
2934 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002935
2936 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002937 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002938 /*
2939 * Fetch an entry off the ring and copy it into a local data
2940 * structure. The copy involves a byte-swap since the
2941 * network byte order and pci byte orders are different.
2942 */
James Smarted957682007-06-17 19:56:37 -05002943 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002944 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002945
James Smart7e56aa22012-08-03 12:35:34 -04002946 if (++pring->sli.sli3.rspidx >= portRspMax)
2947 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002948
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002949 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2950 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002951 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002952 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002953 irsp = &rspiocbq.iocb;
2954
dea31012005-04-17 16:05:31 -05002955 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2956 pring->stats.iocb_rsp++;
2957 rsp_cmpl++;
2958
2959 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002960 /*
2961 * If resource errors reported from HBA, reduce
2962 * queuedepths of the SCSI device.
2963 */
2964 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2965 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2966 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002967 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002968 spin_lock_irqsave(&phba->hbalock, iflag);
2969 }
2970
dea31012005-04-17 16:05:31 -05002971 /* Rsp ring <ringno> error: IOCB */
2972 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002973 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002974 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002975 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002976 irsp->un.ulpWord[0],
2977 irsp->un.ulpWord[1],
2978 irsp->un.ulpWord[2],
2979 irsp->un.ulpWord[3],
2980 irsp->un.ulpWord[4],
2981 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002982 *(uint32_t *)&irsp->un1,
2983 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002984 }
2985
2986 switch (type) {
2987 case LPFC_ABORT_IOCB:
2988 case LPFC_SOL_IOCB:
2989 /*
2990 * Idle exchange closed via ABTS from port. No iocb
2991 * resources need to be recovered.
2992 */
2993 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002995 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002996 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002997 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002998 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002999 break;
3000 }
3001
James Bottomley604a3e32005-10-29 10:28:33 -05003002 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3003 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003004 if (unlikely(!cmdiocbq))
3005 break;
3006 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3007 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3008 if (cmdiocbq->iocb_cmpl) {
3009 spin_unlock_irqrestore(&phba->hbalock, iflag);
3010 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3011 &rspiocbq);
3012 spin_lock_irqsave(&phba->hbalock, iflag);
3013 }
dea31012005-04-17 16:05:31 -05003014 break;
James Smarta4bc3372006-12-02 13:34:16 -05003015 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003016 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003017 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003018 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003019 break;
dea31012005-04-17 16:05:31 -05003020 default:
3021 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3022 char adaptermsg[LPFC_MAX_ADPTMSG];
3023 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3024 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3025 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003026 dev_warn(&((phba->pcidev)->dev),
3027 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003028 phba->brd_no, adaptermsg);
3029 } else {
3030 /* Unknown IOCB command */
3031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003032 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003033 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003034 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003035 irsp->ulpStatus,
3036 irsp->ulpIoTag,
3037 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003038 }
3039 break;
3040 }
3041
3042 /*
3043 * The response IOCB has been processed. Update the ring
3044 * pointer in SLIM. If the port response put pointer has not
3045 * been updated, sync the pgp->rspPutInx and fetch the new port
3046 * response put pointer.
3047 */
James Smart7e56aa22012-08-03 12:35:34 -04003048 writel(pring->sli.sli3.rspidx,
3049 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003050
James Smart7e56aa22012-08-03 12:35:34 -04003051 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003052 portRspPut = le32_to_cpu(pgp->rspPutInx);
3053 }
3054
3055 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3056 pring->stats.iocb_rsp_full++;
3057 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3058 writel(status, phba->CAregaddr);
3059 readl(phba->CAregaddr);
3060 }
3061 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3062 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3063 pring->stats.iocb_cmd_empty++;
3064
3065 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003066 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003067 lpfc_sli_resume_iocb(phba, pring);
3068
3069 if ((pring->lpfc_sli_cmd_available))
3070 (pring->lpfc_sli_cmd_available) (phba, pring);
3071
3072 }
3073
James Smart45ed1192009-10-02 15:17:02 -04003074 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003075 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003076 return rc;
3077}
3078
James Smarte59058c2008-08-24 21:49:00 -04003079/**
James Smart3772a992009-05-22 14:50:54 -04003080 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3081 * @phba: Pointer to HBA context object.
3082 * @pring: Pointer to driver SLI ring object.
3083 * @rspiocbp: Pointer to driver response IOCB object.
3084 *
3085 * This function is called from the worker thread when there is a slow-path
3086 * response IOCB to process. This function chains all the response iocbs until
3087 * seeing the iocb with the LE bit set. The function will call
3088 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3089 * completion of a command iocb. The function will call the
3090 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3091 * The function frees the resources or calls the completion handler if this
3092 * iocb is an abort completion. The function returns NULL when the response
3093 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3094 * this function shall chain the iocb on to the iocb_continueq and return the
3095 * response iocb passed in.
3096 **/
3097static struct lpfc_iocbq *
3098lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3099 struct lpfc_iocbq *rspiocbp)
3100{
3101 struct lpfc_iocbq *saveq;
3102 struct lpfc_iocbq *cmdiocbp;
3103 struct lpfc_iocbq *next_iocb;
3104 IOCB_t *irsp = NULL;
3105 uint32_t free_saveq;
3106 uint8_t iocb_cmd_type;
3107 lpfc_iocb_type type;
3108 unsigned long iflag;
3109 int rc;
3110
3111 spin_lock_irqsave(&phba->hbalock, iflag);
3112 /* First add the response iocb to the countinueq list */
3113 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3114 pring->iocb_continueq_cnt++;
3115
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003116 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003117 irsp = &rspiocbp->iocb;
3118 if (irsp->ulpLe) {
3119 /*
3120 * By default, the driver expects to free all resources
3121 * associated with this iocb completion.
3122 */
3123 free_saveq = 1;
3124 saveq = list_get_first(&pring->iocb_continueq,
3125 struct lpfc_iocbq, list);
3126 irsp = &(saveq->iocb);
3127 list_del_init(&pring->iocb_continueq);
3128 pring->iocb_continueq_cnt = 0;
3129
3130 pring->stats.iocb_rsp++;
3131
3132 /*
3133 * If resource errors reported from HBA, reduce
3134 * queuedepths of the SCSI device.
3135 */
3136 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3137 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3138 spin_unlock_irqrestore(&phba->hbalock, iflag);
3139 phba->lpfc_rampdown_queue_depth(phba);
3140 spin_lock_irqsave(&phba->hbalock, iflag);
3141 }
3142
3143 if (irsp->ulpStatus) {
3144 /* Rsp ring <ringno> error: IOCB */
3145 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3146 "0328 Rsp Ring %d error: "
3147 "IOCB Data: "
3148 "x%x x%x x%x x%x "
3149 "x%x x%x x%x x%x "
3150 "x%x x%x x%x x%x "
3151 "x%x x%x x%x x%x\n",
3152 pring->ringno,
3153 irsp->un.ulpWord[0],
3154 irsp->un.ulpWord[1],
3155 irsp->un.ulpWord[2],
3156 irsp->un.ulpWord[3],
3157 irsp->un.ulpWord[4],
3158 irsp->un.ulpWord[5],
3159 *(((uint32_t *) irsp) + 6),
3160 *(((uint32_t *) irsp) + 7),
3161 *(((uint32_t *) irsp) + 8),
3162 *(((uint32_t *) irsp) + 9),
3163 *(((uint32_t *) irsp) + 10),
3164 *(((uint32_t *) irsp) + 11),
3165 *(((uint32_t *) irsp) + 12),
3166 *(((uint32_t *) irsp) + 13),
3167 *(((uint32_t *) irsp) + 14),
3168 *(((uint32_t *) irsp) + 15));
3169 }
3170
3171 /*
3172 * Fetch the IOCB command type and call the correct completion
3173 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3174 * get freed back to the lpfc_iocb_list by the discovery
3175 * kernel thread.
3176 */
3177 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3178 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3179 switch (type) {
3180 case LPFC_SOL_IOCB:
3181 spin_unlock_irqrestore(&phba->hbalock, iflag);
3182 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3183 spin_lock_irqsave(&phba->hbalock, iflag);
3184 break;
3185
3186 case LPFC_UNSOL_IOCB:
3187 spin_unlock_irqrestore(&phba->hbalock, iflag);
3188 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3189 spin_lock_irqsave(&phba->hbalock, iflag);
3190 if (!rc)
3191 free_saveq = 0;
3192 break;
3193
3194 case LPFC_ABORT_IOCB:
3195 cmdiocbp = NULL;
3196 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3197 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3198 saveq);
3199 if (cmdiocbp) {
3200 /* Call the specified completion routine */
3201 if (cmdiocbp->iocb_cmpl) {
3202 spin_unlock_irqrestore(&phba->hbalock,
3203 iflag);
3204 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3205 saveq);
3206 spin_lock_irqsave(&phba->hbalock,
3207 iflag);
3208 } else
3209 __lpfc_sli_release_iocbq(phba,
3210 cmdiocbp);
3211 }
3212 break;
3213
3214 case LPFC_UNKNOWN_IOCB:
3215 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3216 char adaptermsg[LPFC_MAX_ADPTMSG];
3217 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3218 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3219 MAX_MSG_DATA);
3220 dev_warn(&((phba->pcidev)->dev),
3221 "lpfc%d: %s\n",
3222 phba->brd_no, adaptermsg);
3223 } else {
3224 /* Unknown IOCB command */
3225 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3226 "0335 Unknown IOCB "
3227 "command Data: x%x "
3228 "x%x x%x x%x\n",
3229 irsp->ulpCommand,
3230 irsp->ulpStatus,
3231 irsp->ulpIoTag,
3232 irsp->ulpContext);
3233 }
3234 break;
3235 }
3236
3237 if (free_saveq) {
3238 list_for_each_entry_safe(rspiocbp, next_iocb,
3239 &saveq->list, list) {
3240 list_del(&rspiocbp->list);
3241 __lpfc_sli_release_iocbq(phba, rspiocbp);
3242 }
3243 __lpfc_sli_release_iocbq(phba, saveq);
3244 }
3245 rspiocbp = NULL;
3246 }
3247 spin_unlock_irqrestore(&phba->hbalock, iflag);
3248 return rspiocbp;
3249}
3250
3251/**
3252 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003253 * @phba: Pointer to HBA context object.
3254 * @pring: Pointer to driver SLI ring object.
3255 * @mask: Host attention register mask for this ring.
3256 *
James Smart3772a992009-05-22 14:50:54 -04003257 * This routine wraps the actual slow_ring event process routine from the
3258 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003259 **/
James Smart3772a992009-05-22 14:50:54 -04003260void
James Smart2e0fef82007-06-17 19:56:36 -05003261lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3262 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003263{
James Smart3772a992009-05-22 14:50:54 -04003264 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3265}
3266
3267/**
3268 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3269 * @phba: Pointer to HBA context object.
3270 * @pring: Pointer to driver SLI ring object.
3271 * @mask: Host attention register mask for this ring.
3272 *
3273 * This function is called from the worker thread when there is a ring event
3274 * for non-fcp rings. The caller does not hold any lock. The function will
3275 * remove each response iocb in the response ring and calls the handle
3276 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3277 **/
3278static void
3279lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3280 struct lpfc_sli_ring *pring, uint32_t mask)
3281{
James Smart34b02dc2008-08-24 21:49:55 -04003282 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003283 IOCB_t *entry;
3284 IOCB_t *irsp = NULL;
3285 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003286 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003287 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003288 uint32_t status;
dea31012005-04-17 16:05:31 -05003289
James Smart34b02dc2008-08-24 21:49:55 -04003290 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003291 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003292 pring->stats.iocb_event++;
3293
dea31012005-04-17 16:05:31 -05003294 /*
3295 * The next available response entry should never exceed the maximum
3296 * entries. If it does, treat it as an adapter hardware error.
3297 */
James Smart7e56aa22012-08-03 12:35:34 -04003298 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003299 portRspPut = le32_to_cpu(pgp->rspPutInx);
3300 if (portRspPut >= portRspMax) {
3301 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003302 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003303 * rsp ring <portRspMax>
3304 */
James Smarted957682007-06-17 19:56:37 -05003305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003306 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003307 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003308 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003309
James Smart2e0fef82007-06-17 19:56:36 -05003310 phba->link_state = LPFC_HBA_ERROR;
3311 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003312
3313 phba->work_hs = HS_FFER3;
3314 lpfc_handle_eratt(phba);
3315
James Smart3772a992009-05-22 14:50:54 -04003316 return;
dea31012005-04-17 16:05:31 -05003317 }
3318
3319 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003320 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003321 /*
3322 * Build a completion list and call the appropriate handler.
3323 * The process is to get the next available response iocb, get
3324 * a free iocb from the list, copy the response data into the
3325 * free iocb, insert to the continuation list, and update the
3326 * next response index to slim. This process makes response
3327 * iocb's in the ring available to DMA as fast as possible but
3328 * pays a penalty for a copy operation. Since the iocb is
3329 * only 32 bytes, this penalty is considered small relative to
3330 * the PCI reads for register values and a slim write. When
3331 * the ulpLe field is set, the entire Command has been
3332 * received.
3333 */
James Smarted957682007-06-17 19:56:37 -05003334 entry = lpfc_resp_iocb(phba, pring);
3335
James Smart858c9f62007-06-17 19:56:39 -05003336 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003337 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003338 if (rspiocbp == NULL) {
3339 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003340 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003341 break;
3342 }
3343
James Smarted957682007-06-17 19:56:37 -05003344 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3345 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003346 irsp = &rspiocbp->iocb;
3347
James Smart7e56aa22012-08-03 12:35:34 -04003348 if (++pring->sli.sli3.rspidx >= portRspMax)
3349 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003350
James Smarta58cbd52007-08-02 11:09:43 -04003351 if (pring->ringno == LPFC_ELS_RING) {
3352 lpfc_debugfs_slow_ring_trc(phba,
3353 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3354 *(((uint32_t *) irsp) + 4),
3355 *(((uint32_t *) irsp) + 6),
3356 *(((uint32_t *) irsp) + 7));
3357 }
3358
James Smart7e56aa22012-08-03 12:35:34 -04003359 writel(pring->sli.sli3.rspidx,
3360 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003361
James Smart3772a992009-05-22 14:50:54 -04003362 spin_unlock_irqrestore(&phba->hbalock, iflag);
3363 /* Handle the response IOCB */
3364 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3365 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003366
3367 /*
3368 * If the port response put pointer has not been updated, sync
3369 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3370 * response put pointer.
3371 */
James Smart7e56aa22012-08-03 12:35:34 -04003372 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003373 portRspPut = le32_to_cpu(pgp->rspPutInx);
3374 }
James Smart7e56aa22012-08-03 12:35:34 -04003375 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003376
James Smart92d7f7b2007-06-17 19:56:38 -05003377 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003378 /* At least one response entry has been freed */
3379 pring->stats.iocb_rsp_full++;
3380 /* SET RxRE_RSP in Chip Att register */
3381 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3382 writel(status, phba->CAregaddr);
3383 readl(phba->CAregaddr); /* flush */
3384 }
3385 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3386 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3387 pring->stats.iocb_cmd_empty++;
3388
3389 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003390 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003391 lpfc_sli_resume_iocb(phba, pring);
3392
3393 if ((pring->lpfc_sli_cmd_available))
3394 (pring->lpfc_sli_cmd_available) (phba, pring);
3395
3396 }
3397
James Smart2e0fef82007-06-17 19:56:36 -05003398 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003399 return;
dea31012005-04-17 16:05:31 -05003400}
3401
James Smarte59058c2008-08-24 21:49:00 -04003402/**
James Smart4f774512009-05-22 14:52:35 -04003403 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3404 * @phba: Pointer to HBA context object.
3405 * @pring: Pointer to driver SLI ring object.
3406 * @mask: Host attention register mask for this ring.
3407 *
3408 * This function is called from the worker thread when there is a pending
3409 * ELS response iocb on the driver internal slow-path response iocb worker
3410 * queue. The caller does not hold any lock. The function will remove each
3411 * response iocb from the response worker queue and calls the handle
3412 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3413 **/
3414static void
3415lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3416 struct lpfc_sli_ring *pring, uint32_t mask)
3417{
3418 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003419 struct hbq_dmabuf *dmabuf;
3420 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003421 unsigned long iflag;
3422
James Smart45ed1192009-10-02 15:17:02 -04003423 spin_lock_irqsave(&phba->hbalock, iflag);
3424 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3425 spin_unlock_irqrestore(&phba->hbalock, iflag);
3426 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003427 /* Get the response iocb from the head of work queue */
3428 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003429 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003430 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003431 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003432
3433 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3434 case CQE_CODE_COMPL_WQE:
3435 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3436 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003437 /* Translate ELS WCQE to response IOCBQ */
3438 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3439 irspiocbq);
3440 if (irspiocbq)
3441 lpfc_sli_sp_handle_rspiocb(phba, pring,
3442 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003443 break;
3444 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003445 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003446 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3447 cq_event);
3448 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3449 break;
3450 default:
3451 break;
3452 }
James Smart4f774512009-05-22 14:52:35 -04003453 }
3454}
3455
3456/**
James Smart3621a712009-04-06 18:47:14 -04003457 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003458 * @phba: Pointer to HBA context object.
3459 * @pring: Pointer to driver SLI ring object.
3460 *
3461 * This function aborts all iocbs in the given ring and frees all the iocb
3462 * objects in txq. This function issues an abort iocb for all the iocb commands
3463 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3464 * the return of this function. The caller is not required to hold any locks.
3465 **/
James Smart2e0fef82007-06-17 19:56:36 -05003466void
dea31012005-04-17 16:05:31 -05003467lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3468{
James Smart2534ba72007-04-25 09:52:20 -04003469 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003470 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003471
James Smart92d7f7b2007-06-17 19:56:38 -05003472 if (pring->ringno == LPFC_ELS_RING) {
3473 lpfc_fabric_abort_hba(phba);
3474 }
3475
dea31012005-04-17 16:05:31 -05003476 /* Error everything on txq and txcmplq
3477 * First do the txq.
3478 */
James Smart2e0fef82007-06-17 19:56:36 -05003479 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003480 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003481 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003482
3483 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003484 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3485 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3486
James Smart2e0fef82007-06-17 19:56:36 -05003487 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003488
James Smarta257bf92009-04-06 18:48:10 -04003489 /* Cancel all the IOCBs from the completions list */
3490 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3491 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003492}
3493
James Smarte59058c2008-08-24 21:49:00 -04003494/**
James Smart3621a712009-04-06 18:47:14 -04003495 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003496 * @phba: Pointer to HBA context object.
3497 *
3498 * This function flushes all iocbs in the fcp ring and frees all the iocb
3499 * objects in txq and txcmplq. This function will not issue abort iocbs
3500 * for all the iocb commands in txcmplq, they will just be returned with
3501 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3502 * slot has been permanently disabled.
3503 **/
3504void
3505lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3506{
3507 LIST_HEAD(txq);
3508 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003509 struct lpfc_sli *psli = &phba->sli;
3510 struct lpfc_sli_ring *pring;
3511
3512 /* Currently, only one fcp ring */
3513 pring = &psli->ring[psli->fcp_ring];
3514
3515 spin_lock_irq(&phba->hbalock);
3516 /* Retrieve everything on txq */
3517 list_splice_init(&pring->txq, &txq);
3518 pring->txq_cnt = 0;
3519
3520 /* Retrieve everything on the txcmplq */
3521 list_splice_init(&pring->txcmplq, &txcmplq);
3522 pring->txcmplq_cnt = 0;
James Smart4f2e66c2012-05-09 21:17:07 -04003523
3524 /* Indicate the I/O queues are flushed */
3525 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003526 spin_unlock_irq(&phba->hbalock);
3527
3528 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003529 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3530 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003531
3532 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003533 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3534 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003535}
3536
3537/**
James Smart3772a992009-05-22 14:50:54 -04003538 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003539 * @phba: Pointer to HBA context object.
3540 * @mask: Bit mask to be checked.
3541 *
3542 * This function reads the host status register and compares
3543 * with the provided bit mask to check if HBA completed
3544 * the restart. This function will wait in a loop for the
3545 * HBA to complete restart. If the HBA does not restart within
3546 * 15 iterations, the function will reset the HBA again. The
3547 * function returns 1 when HBA fail to restart otherwise returns
3548 * zero.
3549 **/
James Smart3772a992009-05-22 14:50:54 -04003550static int
3551lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003552{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003553 uint32_t status;
3554 int i = 0;
3555 int retval = 0;
dea31012005-04-17 16:05:31 -05003556
Jamie Wellnitz41415862006-02-28 19:25:27 -05003557 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003558 if (lpfc_readl(phba->HSregaddr, &status))
3559 return 1;
dea31012005-04-17 16:05:31 -05003560
Jamie Wellnitz41415862006-02-28 19:25:27 -05003561 /*
3562 * Check status register every 100ms for 5 retries, then every
3563 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3564 * every 2.5 sec for 4.
3565 * Break our of the loop if errors occurred during init.
3566 */
3567 while (((status & mask) != mask) &&
3568 !(status & HS_FFERM) &&
3569 i++ < 20) {
dea31012005-04-17 16:05:31 -05003570
Jamie Wellnitz41415862006-02-28 19:25:27 -05003571 if (i <= 5)
3572 msleep(10);
3573 else if (i <= 10)
3574 msleep(500);
3575 else
3576 msleep(2500);
dea31012005-04-17 16:05:31 -05003577
Jamie Wellnitz41415862006-02-28 19:25:27 -05003578 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003579 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003580 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003581 lpfc_sli_brdrestart(phba);
3582 }
3583 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003584 if (lpfc_readl(phba->HSregaddr, &status)) {
3585 retval = 1;
3586 break;
3587 }
dea31012005-04-17 16:05:31 -05003588 }
dea31012005-04-17 16:05:31 -05003589
Jamie Wellnitz41415862006-02-28 19:25:27 -05003590 /* Check to see if any errors occurred during init */
3591 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3593 "2751 Adapter failed to restart, "
3594 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3595 status,
3596 readl(phba->MBslimaddr + 0xa8),
3597 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003598 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003599 retval = 1;
3600 }
dea31012005-04-17 16:05:31 -05003601
Jamie Wellnitz41415862006-02-28 19:25:27 -05003602 return retval;
dea31012005-04-17 16:05:31 -05003603}
3604
James Smartda0436e2009-05-22 14:51:39 -04003605/**
3606 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3607 * @phba: Pointer to HBA context object.
3608 * @mask: Bit mask to be checked.
3609 *
3610 * This function checks the host status register to check if HBA is
3611 * ready. This function will wait in a loop for the HBA to be ready
3612 * If the HBA is not ready , the function will will reset the HBA PCI
3613 * function again. The function returns 1 when HBA fail to be ready
3614 * otherwise returns zero.
3615 **/
3616static int
3617lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3618{
3619 uint32_t status;
3620 int retval = 0;
3621
3622 /* Read the HBA Host Status Register */
3623 status = lpfc_sli4_post_status_check(phba);
3624
3625 if (status) {
3626 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3627 lpfc_sli_brdrestart(phba);
3628 status = lpfc_sli4_post_status_check(phba);
3629 }
3630
3631 /* Check to see if any errors occurred during init */
3632 if (status) {
3633 phba->link_state = LPFC_HBA_ERROR;
3634 retval = 1;
3635 } else
3636 phba->sli4_hba.intr_enable = 0;
3637
3638 return retval;
3639}
3640
3641/**
3642 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3643 * @phba: Pointer to HBA context object.
3644 * @mask: Bit mask to be checked.
3645 *
3646 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3647 * from the API jump table function pointer from the lpfc_hba struct.
3648 **/
3649int
3650lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3651{
3652 return phba->lpfc_sli_brdready(phba, mask);
3653}
3654
James Smart92908312006-03-07 15:04:13 -05003655#define BARRIER_TEST_PATTERN (0xdeadbeef)
3656
James Smarte59058c2008-08-24 21:49:00 -04003657/**
James Smart3621a712009-04-06 18:47:14 -04003658 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003659 * @phba: Pointer to HBA context object.
3660 *
James Smart1b511972011-12-13 13:23:09 -05003661 * This function is called before resetting an HBA. This function is called
3662 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003663 **/
James Smart2e0fef82007-06-17 19:56:36 -05003664void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003665{
James Smart65a29c12006-07-06 15:50:50 -04003666 uint32_t __iomem *resp_buf;
3667 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003668 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003669 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003670 int i;
3671 uint8_t hdrtype;
3672
3673 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3674 if (hdrtype != 0x80 ||
3675 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3676 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3677 return;
3678
3679 /*
3680 * Tell the other part of the chip to suspend temporarily all
3681 * its DMA activity.
3682 */
James Smart65a29c12006-07-06 15:50:50 -04003683 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003684
3685 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003686 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3687 return;
James Smart92908312006-03-07 15:04:13 -05003688 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3689 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003690 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003691
James Smart9940b972011-03-11 16:06:12 -05003692 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3693 return;
3694 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003695 /* Clear Chip error bit */
3696 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003697 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003698 }
3699
3700 mbox = 0;
3701 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3702 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3703
3704 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003705 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003706 writel(mbox, mbox_buf);
3707
James Smart9940b972011-03-11 16:06:12 -05003708 for (i = 0; i < 50; i++) {
3709 if (lpfc_readl((resp_buf + 1), &resp_data))
3710 return;
3711 if (resp_data != ~(BARRIER_TEST_PATTERN))
3712 mdelay(1);
3713 else
3714 break;
3715 }
3716 resp_data = 0;
3717 if (lpfc_readl((resp_buf + 1), &resp_data))
3718 return;
3719 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003720 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003721 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003722 goto restore_hc;
3723 else
3724 goto clear_errat;
3725 }
3726
3727 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003728 resp_data = 0;
3729 for (i = 0; i < 500; i++) {
3730 if (lpfc_readl(resp_buf, &resp_data))
3731 return;
3732 if (resp_data != mbox)
3733 mdelay(1);
3734 else
3735 break;
3736 }
James Smart92908312006-03-07 15:04:13 -05003737
3738clear_errat:
3739
James Smart9940b972011-03-11 16:06:12 -05003740 while (++i < 500) {
3741 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3742 return;
3743 if (!(ha_copy & HA_ERATT))
3744 mdelay(1);
3745 else
3746 break;
3747 }
James Smart92908312006-03-07 15:04:13 -05003748
3749 if (readl(phba->HAregaddr) & HA_ERATT) {
3750 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003751 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003752 }
3753
3754restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003755 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003756 writel(hc_copy, phba->HCregaddr);
3757 readl(phba->HCregaddr); /* flush */
3758}
3759
James Smarte59058c2008-08-24 21:49:00 -04003760/**
James Smart3621a712009-04-06 18:47:14 -04003761 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003762 * @phba: Pointer to HBA context object.
3763 *
3764 * This function issues a kill_board mailbox command and waits for
3765 * the error attention interrupt. This function is called for stopping
3766 * the firmware processing. The caller is not required to hold any
3767 * locks. This function calls lpfc_hba_down_post function to free
3768 * any pending commands after the kill. The function will return 1 when it
3769 * fails to kill the board else will return 0.
3770 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003771int
James Smart2e0fef82007-06-17 19:56:36 -05003772lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003773{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003774 struct lpfc_sli *psli;
3775 LPFC_MBOXQ_t *pmb;
3776 uint32_t status;
3777 uint32_t ha_copy;
3778 int retval;
3779 int i = 0;
3780
3781 psli = &phba->sli;
3782
3783 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003784 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003785 "0329 Kill HBA Data: x%x x%x\n",
3786 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003787
James Smart98c9ea52007-10-27 13:37:33 -04003788 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3789 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003790 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003791
3792 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003793 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003794 if (lpfc_readl(phba->HCregaddr, &status)) {
3795 spin_unlock_irq(&phba->hbalock);
3796 mempool_free(pmb, phba->mbox_mem_pool);
3797 return 1;
3798 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003799 status &= ~HC_ERINT_ENA;
3800 writel(status, phba->HCregaddr);
3801 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003802 phba->link_flag |= LS_IGNORE_ERATT;
3803 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003804
3805 lpfc_kill_board(phba, pmb);
3806 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3807 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3808
3809 if (retval != MBX_SUCCESS) {
3810 if (retval != MBX_BUSY)
3811 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3813 "2752 KILL_BOARD command failed retval %d\n",
3814 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003815 spin_lock_irq(&phba->hbalock);
3816 phba->link_flag &= ~LS_IGNORE_ERATT;
3817 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003818 return 1;
3819 }
3820
James Smartf4b4c682009-05-22 14:53:12 -04003821 spin_lock_irq(&phba->hbalock);
3822 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3823 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003824
Jamie Wellnitz41415862006-02-28 19:25:27 -05003825 mempool_free(pmb, phba->mbox_mem_pool);
3826
3827 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3828 * attention every 100ms for 3 seconds. If we don't get ERATT after
3829 * 3 seconds we still set HBA_ERROR state because the status of the
3830 * board is now undefined.
3831 */
James Smart9940b972011-03-11 16:06:12 -05003832 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3833 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003834 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3835 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003836 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3837 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003838 }
3839
3840 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003841 if (ha_copy & HA_ERATT) {
3842 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003843 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003844 }
James Smart2e0fef82007-06-17 19:56:36 -05003845 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003846 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003847 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003848 phba->link_flag &= ~LS_IGNORE_ERATT;
3849 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003850
Jamie Wellnitz41415862006-02-28 19:25:27 -05003851 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003852 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003853
James Smart2e0fef82007-06-17 19:56:36 -05003854 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003855}
3856
James Smarte59058c2008-08-24 21:49:00 -04003857/**
James Smart3772a992009-05-22 14:50:54 -04003858 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003859 * @phba: Pointer to HBA context object.
3860 *
3861 * This function resets the HBA by writing HC_INITFF to the control
3862 * register. After the HBA resets, this function resets all the iocb ring
3863 * indices. This function disables PCI layer parity checking during
3864 * the reset.
3865 * This function returns 0 always.
3866 * The caller is not required to hold any locks.
3867 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003868int
James Smart2e0fef82007-06-17 19:56:36 -05003869lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003870{
3871 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003872 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003873 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003874 int i;
dea31012005-04-17 16:05:31 -05003875
Jamie Wellnitz41415862006-02-28 19:25:27 -05003876 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003877
Jamie Wellnitz41415862006-02-28 19:25:27 -05003878 /* Reset HBA */
3879 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003880 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003881 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003882
3883 /* perform board reset */
3884 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003885 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003886 phba->pport->fc_myDID = 0;
3887 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003888
Jamie Wellnitz41415862006-02-28 19:25:27 -05003889 /* Turn off parity checking and serr during the physical reset */
3890 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3891 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3892 (cfg_value &
3893 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3894
James Smart3772a992009-05-22 14:50:54 -04003895 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3896
Jamie Wellnitz41415862006-02-28 19:25:27 -05003897 /* Now toggle INITFF bit in the Host Control Register */
3898 writel(HC_INITFF, phba->HCregaddr);
3899 mdelay(1);
3900 readl(phba->HCregaddr); /* flush */
3901 writel(0, phba->HCregaddr);
3902 readl(phba->HCregaddr); /* flush */
3903
3904 /* Restore PCI cmd register */
3905 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003906
3907 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003908 for (i = 0; i < psli->num_rings; i++) {
3909 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003910 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003911 pring->sli.sli3.rspidx = 0;
3912 pring->sli.sli3.next_cmdidx = 0;
3913 pring->sli.sli3.local_getidx = 0;
3914 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003915 pring->missbufcnt = 0;
3916 }
dea31012005-04-17 16:05:31 -05003917
James Smart2e0fef82007-06-17 19:56:36 -05003918 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003919 return 0;
3920}
3921
James Smarte59058c2008-08-24 21:49:00 -04003922/**
James Smartda0436e2009-05-22 14:51:39 -04003923 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3924 * @phba: Pointer to HBA context object.
3925 *
3926 * This function resets a SLI4 HBA. This function disables PCI layer parity
3927 * checking during resets the device. The caller is not required to hold
3928 * any locks.
3929 *
3930 * This function returns 0 always.
3931 **/
3932int
3933lpfc_sli4_brdreset(struct lpfc_hba *phba)
3934{
3935 struct lpfc_sli *psli = &phba->sli;
3936 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003937 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003938
3939 /* Reset HBA */
3940 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3941 "0295 Reset HBA Data: x%x x%x\n",
3942 phba->pport->port_state, psli->sli_flag);
3943
3944 /* perform board reset */
3945 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003946 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003947 phba->pport->fc_myDID = 0;
3948 phba->pport->fc_prevDID = 0;
3949
James Smartda0436e2009-05-22 14:51:39 -04003950 spin_lock_irq(&phba->hbalock);
3951 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3952 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003953 spin_unlock_irq(&phba->hbalock);
3954
3955 /* Now physically reset the device */
3956 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3957 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003958
3959 /* Turn off parity checking and serr during the physical reset */
3960 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3961 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3962 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3963
James Smartda0436e2009-05-22 14:51:39 -04003964 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003965 lpfc_sli4_queue_destroy(phba);
James Smart27b01b82012-05-09 21:19:44 -04003966 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04003967
James Smartbe858b62010-12-15 17:57:20 -05003968 /* Restore PCI cmd register */
3969 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3970
James Smart27b01b82012-05-09 21:19:44 -04003971 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003972}
3973
3974/**
3975 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003976 * @phba: Pointer to HBA context object.
3977 *
3978 * This function is called in the SLI initialization code path to
3979 * restart the HBA. The caller is not required to hold any lock.
3980 * This function writes MBX_RESTART mailbox command to the SLIM and
3981 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3982 * function to free any pending commands. The function enables
3983 * POST only during the first initialization. The function returns zero.
3984 * The function does not guarantee completion of MBX_RESTART mailbox
3985 * command before the return of this function.
3986 **/
James Smartda0436e2009-05-22 14:51:39 -04003987static int
3988lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003989{
3990 MAILBOX_t *mb;
3991 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003992 volatile uint32_t word0;
3993 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003994 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003995
James Smart2e0fef82007-06-17 19:56:36 -05003996 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003997
James Smart0d878412009-10-02 15:16:56 -04003998 /* Take PCIe device Advanced Error Reporting (AER) state */
3999 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4000
Jamie Wellnitz41415862006-02-28 19:25:27 -05004001 psli = &phba->sli;
4002
4003 /* Restart HBA */
4004 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004005 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004006 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004007
4008 word0 = 0;
4009 mb = (MAILBOX_t *) &word0;
4010 mb->mbxCommand = MBX_RESTART;
4011 mb->mbxHc = 1;
4012
James Smart92908312006-03-07 15:04:13 -05004013 lpfc_reset_barrier(phba);
4014
Jamie Wellnitz41415862006-02-28 19:25:27 -05004015 to_slim = phba->MBslimaddr;
4016 writel(*(uint32_t *) mb, to_slim);
4017 readl(to_slim); /* flush */
4018
4019 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004020 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004021 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004022 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004023 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004024 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004025 writel(*(uint32_t *) mb, to_slim);
4026 readl(to_slim); /* flush */
4027
4028 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004029 phba->pport->stopped = 0;
4030 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004031 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004032 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004033
James Smart64ba8812006-08-02 15:24:34 -04004034 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4035 psli->stats_start = get_seconds();
4036
James Smarteaf15d52008-12-04 22:39:29 -05004037 /* Give the INITFF and Post time to settle. */
4038 mdelay(100);
dea31012005-04-17 16:05:31 -05004039
James Smart0d878412009-10-02 15:16:56 -04004040 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4041 if (hba_aer_enabled)
4042 pci_disable_pcie_error_reporting(phba->pcidev);
4043
Jamie Wellnitz41415862006-02-28 19:25:27 -05004044 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004045
4046 return 0;
4047}
4048
James Smarte59058c2008-08-24 21:49:00 -04004049/**
James Smartda0436e2009-05-22 14:51:39 -04004050 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4051 * @phba: Pointer to HBA context object.
4052 *
4053 * This function is called in the SLI initialization code path to restart
4054 * a SLI4 HBA. The caller is not required to hold any lock.
4055 * At the end of the function, it calls lpfc_hba_down_post function to
4056 * free any pending commands.
4057 **/
4058static int
4059lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4060{
4061 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004062 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004063 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004064
4065 /* Restart HBA */
4066 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4067 "0296 Restart HBA Data: x%x x%x\n",
4068 phba->pport->port_state, psli->sli_flag);
4069
James Smart75baf692010-06-08 18:31:21 -04004070 /* Take PCIe device Advanced Error Reporting (AER) state */
4071 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4072
James Smart27b01b82012-05-09 21:19:44 -04004073 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004074
4075 spin_lock_irq(&phba->hbalock);
4076 phba->pport->stopped = 0;
4077 phba->link_state = LPFC_INIT_START;
4078 phba->hba_flag = 0;
4079 spin_unlock_irq(&phba->hbalock);
4080
4081 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4082 psli->stats_start = get_seconds();
4083
James Smart75baf692010-06-08 18:31:21 -04004084 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4085 if (hba_aer_enabled)
4086 pci_disable_pcie_error_reporting(phba->pcidev);
4087
James Smartda0436e2009-05-22 14:51:39 -04004088 lpfc_hba_down_post(phba);
4089
James Smart27b01b82012-05-09 21:19:44 -04004090 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004091}
4092
4093/**
4094 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4095 * @phba: Pointer to HBA context object.
4096 *
4097 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4098 * API jump table function pointer from the lpfc_hba struct.
4099**/
4100int
4101lpfc_sli_brdrestart(struct lpfc_hba *phba)
4102{
4103 return phba->lpfc_sli_brdrestart(phba);
4104}
4105
4106/**
James Smart3621a712009-04-06 18:47:14 -04004107 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004108 * @phba: Pointer to HBA context object.
4109 *
4110 * This function is called after a HBA restart to wait for successful
4111 * restart of the HBA. Successful restart of the HBA is indicated by
4112 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4113 * iteration, the function will restart the HBA again. The function returns
4114 * zero if HBA successfully restarted else returns negative error code.
4115 **/
dea31012005-04-17 16:05:31 -05004116static int
4117lpfc_sli_chipset_init(struct lpfc_hba *phba)
4118{
4119 uint32_t status, i = 0;
4120
4121 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004122 if (lpfc_readl(phba->HSregaddr, &status))
4123 return -EIO;
dea31012005-04-17 16:05:31 -05004124
4125 /* Check status register to see what current state is */
4126 i = 0;
4127 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4128
James Smartdcf2a4e2010-09-29 11:18:53 -04004129 /* Check every 10ms for 10 retries, then every 100ms for 90
4130 * retries, then every 1 sec for 50 retires for a total of
4131 * ~60 seconds before reset the board again and check every
4132 * 1 sec for 50 retries. The up to 60 seconds before the
4133 * board ready is required by the Falcon FIPS zeroization
4134 * complete, and any reset the board in between shall cause
4135 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004136 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004137 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004138 /* Adapter failed to init, timeout, status reg
4139 <status> */
James Smarted957682007-06-17 19:56:37 -05004140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004141 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004142 "timeout, status reg x%x, "
4143 "FW Data: A8 x%x AC x%x\n", status,
4144 readl(phba->MBslimaddr + 0xa8),
4145 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004146 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004147 return -ETIMEDOUT;
4148 }
4149
4150 /* Check to see if any errors occurred during init */
4151 if (status & HS_FFERM) {
4152 /* ERROR: During chipset initialization */
4153 /* Adapter failed to init, chipset, status reg
4154 <status> */
James Smarted957682007-06-17 19:56:37 -05004155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004156 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004157 "chipset, status reg x%x, "
4158 "FW Data: A8 x%x AC x%x\n", status,
4159 readl(phba->MBslimaddr + 0xa8),
4160 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004161 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004162 return -EIO;
4163 }
4164
James Smartdcf2a4e2010-09-29 11:18:53 -04004165 if (i <= 10)
dea31012005-04-17 16:05:31 -05004166 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004167 else if (i <= 100)
4168 msleep(100);
4169 else
4170 msleep(1000);
dea31012005-04-17 16:05:31 -05004171
James Smartdcf2a4e2010-09-29 11:18:53 -04004172 if (i == 150) {
4173 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004174 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004175 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004176 }
4177 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004178 if (lpfc_readl(phba->HSregaddr, &status))
4179 return -EIO;
dea31012005-04-17 16:05:31 -05004180 }
4181
4182 /* Check to see if any errors occurred during init */
4183 if (status & HS_FFERM) {
4184 /* ERROR: During chipset initialization */
4185 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004186 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004187 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004188 "status reg x%x, "
4189 "FW Data: A8 x%x AC x%x\n", status,
4190 readl(phba->MBslimaddr + 0xa8),
4191 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004192 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004193 return -EIO;
4194 }
4195
4196 /* Clear all interrupt enable conditions */
4197 writel(0, phba->HCregaddr);
4198 readl(phba->HCregaddr); /* flush */
4199
4200 /* setup host attn register */
4201 writel(0xffffffff, phba->HAregaddr);
4202 readl(phba->HAregaddr); /* flush */
4203 return 0;
4204}
4205
James Smarte59058c2008-08-24 21:49:00 -04004206/**
James Smart3621a712009-04-06 18:47:14 -04004207 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004208 *
4209 * This function calculates and returns the number of HBQs required to be
4210 * configured.
4211 **/
James Smart78b2d852007-08-02 11:10:21 -04004212int
James Smarted957682007-06-17 19:56:37 -05004213lpfc_sli_hbq_count(void)
4214{
James Smart92d7f7b2007-06-17 19:56:38 -05004215 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004216}
4217
James Smarte59058c2008-08-24 21:49:00 -04004218/**
James Smart3621a712009-04-06 18:47:14 -04004219 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004220 *
4221 * This function adds the number of hbq entries in every HBQ to get
4222 * the total number of hbq entries required for the HBA and returns
4223 * the total count.
4224 **/
James Smarted957682007-06-17 19:56:37 -05004225static int
4226lpfc_sli_hbq_entry_count(void)
4227{
4228 int hbq_count = lpfc_sli_hbq_count();
4229 int count = 0;
4230 int i;
4231
4232 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004233 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004234 return count;
4235}
4236
James Smarte59058c2008-08-24 21:49:00 -04004237/**
James Smart3621a712009-04-06 18:47:14 -04004238 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004239 *
4240 * This function calculates amount of memory required for all hbq entries
4241 * to be configured and returns the total memory required.
4242 **/
dea31012005-04-17 16:05:31 -05004243int
James Smarted957682007-06-17 19:56:37 -05004244lpfc_sli_hbq_size(void)
4245{
4246 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4247}
4248
James Smarte59058c2008-08-24 21:49:00 -04004249/**
James Smart3621a712009-04-06 18:47:14 -04004250 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004251 * @phba: Pointer to HBA context object.
4252 *
4253 * This function is called during the SLI initialization to configure
4254 * all the HBQs and post buffers to the HBQ. The caller is not
4255 * required to hold any locks. This function will return zero if successful
4256 * else it will return negative error code.
4257 **/
James Smarted957682007-06-17 19:56:37 -05004258static int
4259lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4260{
4261 int hbq_count = lpfc_sli_hbq_count();
4262 LPFC_MBOXQ_t *pmb;
4263 MAILBOX_t *pmbox;
4264 uint32_t hbqno;
4265 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004266
James Smart92d7f7b2007-06-17 19:56:38 -05004267 /* Get a Mailbox buffer to setup mailbox
4268 * commands for HBA initialization
4269 */
James Smarted957682007-06-17 19:56:37 -05004270 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4271
4272 if (!pmb)
4273 return -ENOMEM;
4274
James Smart04c68492009-05-22 14:52:52 -04004275 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004276
4277 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4278 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004279 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004280
4281 hbq_entry_index = 0;
4282 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4283 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4284 phba->hbqs[hbqno].hbqPutIdx = 0;
4285 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4286 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004287 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004288 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4289 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004290 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4291
4292 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4293 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4294 mbxStatus <status>, ring <num> */
4295
4296 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004297 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004298 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004299 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004300 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004301 pmbox->mbxStatus, hbqno);
4302
4303 phba->link_state = LPFC_HBA_ERROR;
4304 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004305 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004306 }
4307 }
4308 phba->hbq_count = hbq_count;
4309
James Smarted957682007-06-17 19:56:37 -05004310 mempool_free(pmb, phba->mbox_mem_pool);
4311
James Smart92d7f7b2007-06-17 19:56:38 -05004312 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004313 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4314 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004315 return 0;
4316}
4317
James Smarte59058c2008-08-24 21:49:00 -04004318/**
James Smart4f774512009-05-22 14:52:35 -04004319 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4320 * @phba: Pointer to HBA context object.
4321 *
4322 * This function is called during the SLI initialization to configure
4323 * all the HBQs and post buffers to the HBQ. The caller is not
4324 * required to hold any locks. This function will return zero if successful
4325 * else it will return negative error code.
4326 **/
4327static int
4328lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4329{
4330 phba->hbq_in_use = 1;
4331 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4332 phba->hbq_count = 1;
4333 /* Initially populate or replenish the HBQs */
4334 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4335 return 0;
4336}
4337
4338/**
James Smart3621a712009-04-06 18:47:14 -04004339 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004340 * @phba: Pointer to HBA context object.
4341 * @sli_mode: sli mode - 2/3
4342 *
4343 * This function is called by the sli intialization code path
4344 * to issue config_port mailbox command. This function restarts the
4345 * HBA firmware and issues a config_port mailbox command to configure
4346 * the SLI interface in the sli mode specified by sli_mode
4347 * variable. The caller is not required to hold any locks.
4348 * The function returns 0 if successful, else returns negative error
4349 * code.
4350 **/
James Smart93996272008-08-24 21:50:30 -04004351int
4352lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004353{
4354 LPFC_MBOXQ_t *pmb;
4355 uint32_t resetcount = 0, rc = 0, done = 0;
4356
4357 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4358 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004359 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004360 return -ENOMEM;
4361 }
4362
James Smarted957682007-06-17 19:56:37 -05004363 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004364 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004365 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004366 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004367 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004368 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004369 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004370 rc = lpfc_sli_chipset_init(phba);
4371 if (rc)
4372 break;
4373
James Smart2e0fef82007-06-17 19:56:36 -05004374 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004375 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004376 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004377 resetcount++;
4378
James Smarted957682007-06-17 19:56:37 -05004379 /* Call pre CONFIG_PORT mailbox command initialization. A
4380 * value of 0 means the call was successful. Any other
4381 * nonzero value is a failure, but if ERESTART is returned,
4382 * the driver may reset the HBA and try again.
4383 */
dea31012005-04-17 16:05:31 -05004384 rc = lpfc_config_port_prep(phba);
4385 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004386 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004387 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004388 } else if (rc)
dea31012005-04-17 16:05:31 -05004389 break;
James Smart6d368e52011-05-24 11:44:12 -04004390
James Smart2e0fef82007-06-17 19:56:36 -05004391 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004392 lpfc_config_port(phba, pmb);
4393 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004394 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4395 LPFC_SLI3_HBQ_ENABLED |
4396 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004397 LPFC_SLI3_BG_ENABLED |
4398 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004399 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004401 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004402 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004403 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004404 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004405 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004406 spin_unlock_irq(&phba->hbalock);
4407 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004408 } else {
4409 /* Allow asynchronous mailbox command to go through */
4410 spin_lock_irq(&phba->hbalock);
4411 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4412 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004413 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004414
4415 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4416 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4417 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4418 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004419 }
dea31012005-04-17 16:05:31 -05004420 }
James Smarted957682007-06-17 19:56:37 -05004421 if (!done) {
4422 rc = -EINVAL;
4423 goto do_prep_failed;
4424 }
James Smart04c68492009-05-22 14:52:52 -04004425 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4426 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004427 rc = -ENXIO;
4428 goto do_prep_failed;
4429 }
James Smart04c68492009-05-22 14:52:52 -04004430 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004431 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004432 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4433 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4434 phba->max_vpi : phba->max_vports;
4435
James Smart34b02dc2008-08-24 21:49:55 -04004436 } else
4437 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004438 phba->fips_level = 0;
4439 phba->fips_spec_rev = 0;
4440 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004441 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004442 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4443 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4445 "2850 Security Crypto Active. FIPS x%d "
4446 "(Spec Rev: x%d)",
4447 phba->fips_level, phba->fips_spec_rev);
4448 }
4449 if (pmb->u.mb.un.varCfgPort.sec_err) {
4450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4451 "2856 Config Port Security Crypto "
4452 "Error: x%x ",
4453 pmb->u.mb.un.varCfgPort.sec_err);
4454 }
James Smart04c68492009-05-22 14:52:52 -04004455 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004456 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004457 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004458 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004459
4460 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4461 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004462
4463 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004464 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004465 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4466 else
4467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4468 "0443 Adapter did not grant "
4469 "BlockGuard\n");
4470 }
James Smart34b02dc2008-08-24 21:49:55 -04004471 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004472 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004473 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004474 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004475 }
James Smart92d7f7b2007-06-17 19:56:38 -05004476do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004477 mempool_free(pmb, phba->mbox_mem_pool);
4478 return rc;
4479}
4480
James Smarte59058c2008-08-24 21:49:00 -04004481
4482/**
James Smart3621a712009-04-06 18:47:14 -04004483 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004484 * @phba: Pointer to HBA context object.
4485 *
4486 * This function is the main SLI intialization function. This function
4487 * is called by the HBA intialization code, HBA reset code and HBA
4488 * error attention handler code. Caller is not required to hold any
4489 * locks. This function issues config_port mailbox command to configure
4490 * the SLI, setup iocb rings and HBQ rings. In the end the function
4491 * calls the config_port_post function to issue init_link mailbox
4492 * command and to start the discovery. The function will return zero
4493 * if successful, else it will return negative error code.
4494 **/
James Smarted957682007-06-17 19:56:37 -05004495int
4496lpfc_sli_hba_setup(struct lpfc_hba *phba)
4497{
4498 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004499 int mode = 3, i;
4500 int longs;
James Smarted957682007-06-17 19:56:37 -05004501
4502 switch (lpfc_sli_mode) {
4503 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004504 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004506 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004507 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004508 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004509 break;
4510 }
James Smarted957682007-06-17 19:56:37 -05004511 mode = 2;
4512 break;
4513 case 0:
4514 case 3:
4515 break;
4516 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004518 "1819 Unrecognized lpfc_sli_mode "
4519 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004520
4521 break;
4522 }
4523
James Smart93996272008-08-24 21:50:30 -04004524 rc = lpfc_sli_config_port(phba, mode);
4525
James Smarted957682007-06-17 19:56:37 -05004526 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004528 "1820 Unable to select SLI-3. "
4529 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004530 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004531 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004532 if (rc)
dea31012005-04-17 16:05:31 -05004533 goto lpfc_sli_hba_setup_error;
4534
James Smart0d878412009-10-02 15:16:56 -04004535 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4536 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4537 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4538 if (!rc) {
4539 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4540 "2709 This device supports "
4541 "Advanced Error Reporting (AER)\n");
4542 spin_lock_irq(&phba->hbalock);
4543 phba->hba_flag |= HBA_AER_ENABLED;
4544 spin_unlock_irq(&phba->hbalock);
4545 } else {
4546 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4547 "2708 This device does not support "
4548 "Advanced Error Reporting (AER)\n");
4549 phba->cfg_aer_support = 0;
4550 }
4551 }
4552
James Smarted957682007-06-17 19:56:37 -05004553 if (phba->sli_rev == 3) {
4554 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4555 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004556 } else {
4557 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4558 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004559 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004560 }
4561
4562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004563 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4564 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004565 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004566
4567 if (rc)
4568 goto lpfc_sli_hba_setup_error;
4569
James Smart6d368e52011-05-24 11:44:12 -04004570 /* Initialize VPIs. */
4571 if (phba->sli_rev == LPFC_SLI_REV3) {
4572 /*
4573 * The VPI bitmask and physical ID array are allocated
4574 * and initialized once only - at driver load. A port
4575 * reset doesn't need to reinitialize this memory.
4576 */
4577 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4578 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4579 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4580 GFP_KERNEL);
4581 if (!phba->vpi_bmask) {
4582 rc = -ENOMEM;
4583 goto lpfc_sli_hba_setup_error;
4584 }
4585
4586 phba->vpi_ids = kzalloc(
4587 (phba->max_vpi+1) * sizeof(uint16_t),
4588 GFP_KERNEL);
4589 if (!phba->vpi_ids) {
4590 kfree(phba->vpi_bmask);
4591 rc = -ENOMEM;
4592 goto lpfc_sli_hba_setup_error;
4593 }
4594 for (i = 0; i < phba->max_vpi; i++)
4595 phba->vpi_ids[i] = i;
4596 }
4597 }
4598
James Smart93996272008-08-24 21:50:30 -04004599 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004600 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4601 rc = lpfc_sli_hbq_setup(phba);
4602 if (rc)
4603 goto lpfc_sli_hba_setup_error;
4604 }
James Smart04c68492009-05-22 14:52:52 -04004605 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004606 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004607 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004608
4609 rc = lpfc_config_port_post(phba);
4610 if (rc)
4611 goto lpfc_sli_hba_setup_error;
4612
James Smarted957682007-06-17 19:56:37 -05004613 return rc;
4614
James Smart92d7f7b2007-06-17 19:56:38 -05004615lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004616 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004618 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004619 return rc;
4620}
4621
James Smartda0436e2009-05-22 14:51:39 -04004622/**
4623 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4624 * @phba: Pointer to HBA context object.
4625 * @mboxq: mailbox pointer.
4626 * This function issue a dump mailbox command to read config region
4627 * 23 and parse the records in the region and populate driver
4628 * data structure.
4629 **/
4630static int
James Smartff78d8f2011-12-13 13:21:35 -05004631lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004632{
James Smartff78d8f2011-12-13 13:21:35 -05004633 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004634 struct lpfc_dmabuf *mp;
4635 struct lpfc_mqe *mqe;
4636 uint32_t data_length;
4637 int rc;
4638
4639 /* Program the default value of vlan_id and fc_map */
4640 phba->valid_vlan = 0;
4641 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4642 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4643 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4644
James Smartff78d8f2011-12-13 13:21:35 -05004645 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4646 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004647 return -ENOMEM;
4648
James Smartff78d8f2011-12-13 13:21:35 -05004649 mqe = &mboxq->u.mqe;
4650 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4651 rc = -ENOMEM;
4652 goto out_free_mboxq;
4653 }
4654
James Smartda0436e2009-05-22 14:51:39 -04004655 mp = (struct lpfc_dmabuf *) mboxq->context1;
4656 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4657
4658 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4659 "(%d):2571 Mailbox cmd x%x Status x%x "
4660 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4661 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4662 "CQ: x%x x%x x%x x%x\n",
4663 mboxq->vport ? mboxq->vport->vpi : 0,
4664 bf_get(lpfc_mqe_command, mqe),
4665 bf_get(lpfc_mqe_status, mqe),
4666 mqe->un.mb_words[0], mqe->un.mb_words[1],
4667 mqe->un.mb_words[2], mqe->un.mb_words[3],
4668 mqe->un.mb_words[4], mqe->un.mb_words[5],
4669 mqe->un.mb_words[6], mqe->un.mb_words[7],
4670 mqe->un.mb_words[8], mqe->un.mb_words[9],
4671 mqe->un.mb_words[10], mqe->un.mb_words[11],
4672 mqe->un.mb_words[12], mqe->un.mb_words[13],
4673 mqe->un.mb_words[14], mqe->un.mb_words[15],
4674 mqe->un.mb_words[16], mqe->un.mb_words[50],
4675 mboxq->mcqe.word0,
4676 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4677 mboxq->mcqe.trailer);
4678
4679 if (rc) {
4680 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4681 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004682 rc = -EIO;
4683 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004684 }
4685 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004686 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004687 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4688 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004689 rc = -EIO;
4690 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004691 }
James Smartda0436e2009-05-22 14:51:39 -04004692
4693 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4694 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4695 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004696 rc = 0;
4697
4698out_free_mboxq:
4699 mempool_free(mboxq, phba->mbox_mem_pool);
4700 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004701}
4702
4703/**
4704 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4705 * @phba: pointer to lpfc hba data structure.
4706 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4707 * @vpd: pointer to the memory to hold resulting port vpd data.
4708 * @vpd_size: On input, the number of bytes allocated to @vpd.
4709 * On output, the number of data bytes in @vpd.
4710 *
4711 * This routine executes a READ_REV SLI4 mailbox command. In
4712 * addition, this routine gets the port vpd data.
4713 *
4714 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004715 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004716 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004717 **/
4718static int
4719lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4720 uint8_t *vpd, uint32_t *vpd_size)
4721{
4722 int rc = 0;
4723 uint32_t dma_size;
4724 struct lpfc_dmabuf *dmabuf;
4725 struct lpfc_mqe *mqe;
4726
4727 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4728 if (!dmabuf)
4729 return -ENOMEM;
4730
4731 /*
4732 * Get a DMA buffer for the vpd data resulting from the READ_REV
4733 * mailbox command.
4734 */
4735 dma_size = *vpd_size;
4736 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4737 dma_size,
4738 &dmabuf->phys,
4739 GFP_KERNEL);
4740 if (!dmabuf->virt) {
4741 kfree(dmabuf);
4742 return -ENOMEM;
4743 }
4744 memset(dmabuf->virt, 0, dma_size);
4745
4746 /*
4747 * The SLI4 implementation of READ_REV conflicts at word1,
4748 * bits 31:16 and SLI4 adds vpd functionality not present
4749 * in SLI3. This code corrects the conflicts.
4750 */
4751 lpfc_read_rev(phba, mboxq);
4752 mqe = &mboxq->u.mqe;
4753 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4754 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4755 mqe->un.read_rev.word1 &= 0x0000FFFF;
4756 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4757 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4758
4759 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4760 if (rc) {
4761 dma_free_coherent(&phba->pcidev->dev, dma_size,
4762 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004763 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004764 return -EIO;
4765 }
4766
James Smartda0436e2009-05-22 14:51:39 -04004767 /*
4768 * The available vpd length cannot be bigger than the
4769 * DMA buffer passed to the port. Catch the less than
4770 * case and update the caller's size.
4771 */
4772 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4773 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4774
James Smartd7c47992010-06-08 18:31:54 -04004775 memcpy(vpd, dmabuf->virt, *vpd_size);
4776
James Smartda0436e2009-05-22 14:51:39 -04004777 dma_free_coherent(&phba->pcidev->dev, dma_size,
4778 dmabuf->virt, dmabuf->phys);
4779 kfree(dmabuf);
4780 return 0;
4781}
4782
4783/**
James Smartcd1c8302011-10-10 21:33:25 -04004784 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4785 * @phba: pointer to lpfc hba data structure.
4786 *
4787 * This routine retrieves SLI4 device physical port name this PCI function
4788 * is attached to.
4789 *
4790 * Return codes
4791 * 0 - sucessful
4792 * otherwise - failed to retrieve physical port name
4793 **/
4794static int
4795lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4796{
4797 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004798 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4799 struct lpfc_controller_attribute *cntl_attr;
4800 struct lpfc_mbx_get_port_name *get_port_name;
4801 void *virtaddr = NULL;
4802 uint32_t alloclen, reqlen;
4803 uint32_t shdr_status, shdr_add_status;
4804 union lpfc_sli4_cfg_shdr *shdr;
4805 char cport_name = 0;
4806 int rc;
4807
4808 /* We assume nothing at this point */
4809 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4810 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4811
4812 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4813 if (!mboxq)
4814 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004815 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004816 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4817 lpfc_sli4_read_config(phba);
4818 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4819 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004820
4821 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4822 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4823 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4824 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4825 LPFC_SLI4_MBX_NEMBED);
4826 if (alloclen < reqlen) {
4827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4828 "3084 Allocated DMA memory size (%d) is "
4829 "less than the requested DMA memory size "
4830 "(%d)\n", alloclen, reqlen);
4831 rc = -ENOMEM;
4832 goto out_free_mboxq;
4833 }
4834 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4835 virtaddr = mboxq->sge_array->addr[0];
4836 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4837 shdr = &mbx_cntl_attr->cfg_shdr;
4838 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4839 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4840 if (shdr_status || shdr_add_status || rc) {
4841 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4842 "3085 Mailbox x%x (x%x/x%x) failed, "
4843 "rc:x%x, status:x%x, add_status:x%x\n",
4844 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4845 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4846 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4847 rc, shdr_status, shdr_add_status);
4848 rc = -ENXIO;
4849 goto out_free_mboxq;
4850 }
4851 cntl_attr = &mbx_cntl_attr->cntl_attr;
4852 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4853 phba->sli4_hba.lnk_info.lnk_tp =
4854 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4855 phba->sli4_hba.lnk_info.lnk_no =
4856 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4857 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4858 "3086 lnk_type:%d, lnk_numb:%d\n",
4859 phba->sli4_hba.lnk_info.lnk_tp,
4860 phba->sli4_hba.lnk_info.lnk_no);
4861
4862retrieve_ppname:
4863 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4864 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4865 sizeof(struct lpfc_mbx_get_port_name) -
4866 sizeof(struct lpfc_sli4_cfg_mhdr),
4867 LPFC_SLI4_MBX_EMBED);
4868 get_port_name = &mboxq->u.mqe.un.get_port_name;
4869 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4870 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4871 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4872 phba->sli4_hba.lnk_info.lnk_tp);
4873 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4874 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4875 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4876 if (shdr_status || shdr_add_status || rc) {
4877 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4878 "3087 Mailbox x%x (x%x/x%x) failed: "
4879 "rc:x%x, status:x%x, add_status:x%x\n",
4880 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4881 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4882 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4883 rc, shdr_status, shdr_add_status);
4884 rc = -ENXIO;
4885 goto out_free_mboxq;
4886 }
4887 switch (phba->sli4_hba.lnk_info.lnk_no) {
4888 case LPFC_LINK_NUMBER_0:
4889 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4890 &get_port_name->u.response);
4891 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4892 break;
4893 case LPFC_LINK_NUMBER_1:
4894 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4895 &get_port_name->u.response);
4896 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4897 break;
4898 case LPFC_LINK_NUMBER_2:
4899 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4900 &get_port_name->u.response);
4901 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4902 break;
4903 case LPFC_LINK_NUMBER_3:
4904 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4905 &get_port_name->u.response);
4906 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4907 break;
4908 default:
4909 break;
4910 }
4911
4912 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4913 phba->Port[0] = cport_name;
4914 phba->Port[1] = '\0';
4915 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4916 "3091 SLI get port name: %s\n", phba->Port);
4917 }
4918
4919out_free_mboxq:
4920 if (rc != MBX_TIMEOUT) {
4921 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4922 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4923 else
4924 mempool_free(mboxq, phba->mbox_mem_pool);
4925 }
4926 return rc;
4927}
4928
4929/**
James Smartda0436e2009-05-22 14:51:39 -04004930 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4931 * @phba: pointer to lpfc hba data structure.
4932 *
4933 * This routine is called to explicitly arm the SLI4 device's completion and
4934 * event queues
4935 **/
4936static void
4937lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4938{
4939 uint8_t fcp_eqidx;
4940
4941 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4942 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004943 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004944 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04004945 do {
James Smart2e90f4b2011-12-13 13:22:37 -05004946 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4947 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04004948 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05004949 }
James Smart67d12732012-08-03 12:36:13 -04004950 if (phba->sli4_hba.hba_eq) {
4951 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05004952 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04004953 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05004954 LPFC_QUEUE_REARM);
4955 }
James Smartda0436e2009-05-22 14:51:39 -04004956}
4957
4958/**
James Smart6d368e52011-05-24 11:44:12 -04004959 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4960 * @phba: Pointer to HBA context object.
4961 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004962 * @extnt_count: buffer to hold port available extent count.
4963 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004964 *
James Smartb76f2dc2011-07-22 18:37:42 -04004965 * This function calls the port and retrievs the number of available
4966 * extents and their size for a particular extent type.
4967 *
4968 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004969 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004970int
James Smart6d368e52011-05-24 11:44:12 -04004971lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4972 uint16_t *extnt_count, uint16_t *extnt_size)
4973{
4974 int rc = 0;
4975 uint32_t length;
4976 uint32_t mbox_tmo;
4977 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4978 LPFC_MBOXQ_t *mbox;
4979
4980 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4981 if (!mbox)
4982 return -ENOMEM;
4983
4984 /* Find out how many extents are available for this resource type */
4985 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4986 sizeof(struct lpfc_sli4_cfg_mhdr));
4987 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4988 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4989 length, LPFC_SLI4_MBX_EMBED);
4990
4991 /* Send an extents count of 0 - the GET doesn't use it. */
4992 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4993 LPFC_SLI4_MBX_EMBED);
4994 if (unlikely(rc)) {
4995 rc = -EIO;
4996 goto err_exit;
4997 }
4998
4999 if (!phba->sli4_hba.intr_enable)
5000 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5001 else {
James Smarta183a152011-10-10 21:32:43 -04005002 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005003 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5004 }
5005 if (unlikely(rc)) {
5006 rc = -EIO;
5007 goto err_exit;
5008 }
5009
5010 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5011 if (bf_get(lpfc_mbox_hdr_status,
5012 &rsrc_info->header.cfg_shdr.response)) {
5013 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5014 "2930 Failed to get resource extents "
5015 "Status 0x%x Add'l Status 0x%x\n",
5016 bf_get(lpfc_mbox_hdr_status,
5017 &rsrc_info->header.cfg_shdr.response),
5018 bf_get(lpfc_mbox_hdr_add_status,
5019 &rsrc_info->header.cfg_shdr.response));
5020 rc = -EIO;
5021 goto err_exit;
5022 }
5023
5024 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5025 &rsrc_info->u.rsp);
5026 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5027 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005028
5029 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5030 "3162 Retrieved extents type-%d from port: count:%d, "
5031 "size:%d\n", type, *extnt_count, *extnt_size);
5032
5033err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005034 mempool_free(mbox, phba->mbox_mem_pool);
5035 return rc;
5036}
5037
5038/**
5039 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5040 * @phba: Pointer to HBA context object.
5041 * @type: The extent type to check.
5042 *
5043 * This function reads the current available extents from the port and checks
5044 * if the extent count or extent size has changed since the last access.
5045 * Callers use this routine post port reset to understand if there is a
5046 * extent reprovisioning requirement.
5047 *
5048 * Returns:
5049 * -Error: error indicates problem.
5050 * 1: Extent count or size has changed.
5051 * 0: No changes.
5052 **/
5053static int
5054lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5055{
5056 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5057 uint16_t size_diff, rsrc_ext_size;
5058 int rc = 0;
5059 struct lpfc_rsrc_blks *rsrc_entry;
5060 struct list_head *rsrc_blk_list = NULL;
5061
5062 size_diff = 0;
5063 curr_ext_cnt = 0;
5064 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5065 &rsrc_ext_cnt,
5066 &rsrc_ext_size);
5067 if (unlikely(rc))
5068 return -EIO;
5069
5070 switch (type) {
5071 case LPFC_RSC_TYPE_FCOE_RPI:
5072 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5073 break;
5074 case LPFC_RSC_TYPE_FCOE_VPI:
5075 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5076 break;
5077 case LPFC_RSC_TYPE_FCOE_XRI:
5078 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5079 break;
5080 case LPFC_RSC_TYPE_FCOE_VFI:
5081 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5082 break;
5083 default:
5084 break;
5085 }
5086
5087 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5088 curr_ext_cnt++;
5089 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5090 size_diff++;
5091 }
5092
5093 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5094 rc = 1;
5095
5096 return rc;
5097}
5098
5099/**
5100 * lpfc_sli4_cfg_post_extnts -
5101 * @phba: Pointer to HBA context object.
5102 * @extnt_cnt - number of available extents.
5103 * @type - the extent type (rpi, xri, vfi, vpi).
5104 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5105 * @mbox - pointer to the caller's allocated mailbox structure.
5106 *
5107 * This function executes the extents allocation request. It also
5108 * takes care of the amount of memory needed to allocate or get the
5109 * allocated extents. It is the caller's responsibility to evaluate
5110 * the response.
5111 *
5112 * Returns:
5113 * -Error: Error value describes the condition found.
5114 * 0: if successful
5115 **/
5116static int
James Smart8a9d2e82012-05-09 21:16:12 -04005117lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005118 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5119{
5120 int rc = 0;
5121 uint32_t req_len;
5122 uint32_t emb_len;
5123 uint32_t alloc_len, mbox_tmo;
5124
5125 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005126 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005127
5128 /*
5129 * Calculate the size of an embedded mailbox. The uint32_t
5130 * accounts for extents-specific word.
5131 */
5132 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5133 sizeof(uint32_t);
5134
5135 /*
5136 * Presume the allocation and response will fit into an embedded
5137 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5138 */
5139 *emb = LPFC_SLI4_MBX_EMBED;
5140 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005141 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005142 sizeof(union lpfc_sli4_cfg_shdr) +
5143 sizeof(uint32_t);
5144 *emb = LPFC_SLI4_MBX_NEMBED;
5145 }
5146
5147 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5148 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5149 req_len, *emb);
5150 if (alloc_len < req_len) {
5151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005152 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005153 "less than the requested DMA memory "
5154 "size (x%x)\n", alloc_len, req_len);
5155 return -ENOMEM;
5156 }
James Smart8a9d2e82012-05-09 21:16:12 -04005157 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005158 if (unlikely(rc))
5159 return -EIO;
5160
5161 if (!phba->sli4_hba.intr_enable)
5162 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5163 else {
James Smarta183a152011-10-10 21:32:43 -04005164 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005165 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5166 }
5167
5168 if (unlikely(rc))
5169 rc = -EIO;
5170 return rc;
5171}
5172
5173/**
5174 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5175 * @phba: Pointer to HBA context object.
5176 * @type: The resource extent type to allocate.
5177 *
5178 * This function allocates the number of elements for the specified
5179 * resource type.
5180 **/
5181static int
5182lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5183{
5184 bool emb = false;
5185 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5186 uint16_t rsrc_id, rsrc_start, j, k;
5187 uint16_t *ids;
5188 int i, rc;
5189 unsigned long longs;
5190 unsigned long *bmask;
5191 struct lpfc_rsrc_blks *rsrc_blks;
5192 LPFC_MBOXQ_t *mbox;
5193 uint32_t length;
5194 struct lpfc_id_range *id_array = NULL;
5195 void *virtaddr = NULL;
5196 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5197 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5198 struct list_head *ext_blk_list;
5199
5200 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5201 &rsrc_cnt,
5202 &rsrc_size);
5203 if (unlikely(rc))
5204 return -EIO;
5205
5206 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5207 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5208 "3009 No available Resource Extents "
5209 "for resource type 0x%x: Count: 0x%x, "
5210 "Size 0x%x\n", type, rsrc_cnt,
5211 rsrc_size);
5212 return -ENOMEM;
5213 }
5214
James Smart8a9d2e82012-05-09 21:16:12 -04005215 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5216 "2903 Post resource extents type-0x%x: "
5217 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005218
5219 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5220 if (!mbox)
5221 return -ENOMEM;
5222
James Smart8a9d2e82012-05-09 21:16:12 -04005223 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005224 if (unlikely(rc)) {
5225 rc = -EIO;
5226 goto err_exit;
5227 }
5228
5229 /*
5230 * Figure out where the response is located. Then get local pointers
5231 * to the response data. The port does not guarantee to respond to
5232 * all extents counts request so update the local variable with the
5233 * allocated count from the port.
5234 */
5235 if (emb == LPFC_SLI4_MBX_EMBED) {
5236 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5237 id_array = &rsrc_ext->u.rsp.id[0];
5238 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5239 } else {
5240 virtaddr = mbox->sge_array->addr[0];
5241 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5242 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5243 id_array = &n_rsrc->id;
5244 }
5245
5246 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5247 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5248
5249 /*
5250 * Based on the resource size and count, correct the base and max
5251 * resource values.
5252 */
5253 length = sizeof(struct lpfc_rsrc_blks);
5254 switch (type) {
5255 case LPFC_RSC_TYPE_FCOE_RPI:
5256 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5257 sizeof(unsigned long),
5258 GFP_KERNEL);
5259 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5260 rc = -ENOMEM;
5261 goto err_exit;
5262 }
5263 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5264 sizeof(uint16_t),
5265 GFP_KERNEL);
5266 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5267 kfree(phba->sli4_hba.rpi_bmask);
5268 rc = -ENOMEM;
5269 goto err_exit;
5270 }
5271
5272 /*
5273 * The next_rpi was initialized with the maximum available
5274 * count but the port may allocate a smaller number. Catch
5275 * that case and update the next_rpi.
5276 */
5277 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5278
5279 /* Initialize local ptrs for common extent processing later. */
5280 bmask = phba->sli4_hba.rpi_bmask;
5281 ids = phba->sli4_hba.rpi_ids;
5282 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5283 break;
5284 case LPFC_RSC_TYPE_FCOE_VPI:
5285 phba->vpi_bmask = kzalloc(longs *
5286 sizeof(unsigned long),
5287 GFP_KERNEL);
5288 if (unlikely(!phba->vpi_bmask)) {
5289 rc = -ENOMEM;
5290 goto err_exit;
5291 }
5292 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5293 sizeof(uint16_t),
5294 GFP_KERNEL);
5295 if (unlikely(!phba->vpi_ids)) {
5296 kfree(phba->vpi_bmask);
5297 rc = -ENOMEM;
5298 goto err_exit;
5299 }
5300
5301 /* Initialize local ptrs for common extent processing later. */
5302 bmask = phba->vpi_bmask;
5303 ids = phba->vpi_ids;
5304 ext_blk_list = &phba->lpfc_vpi_blk_list;
5305 break;
5306 case LPFC_RSC_TYPE_FCOE_XRI:
5307 phba->sli4_hba.xri_bmask = kzalloc(longs *
5308 sizeof(unsigned long),
5309 GFP_KERNEL);
5310 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5311 rc = -ENOMEM;
5312 goto err_exit;
5313 }
James Smart8a9d2e82012-05-09 21:16:12 -04005314 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005315 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5316 sizeof(uint16_t),
5317 GFP_KERNEL);
5318 if (unlikely(!phba->sli4_hba.xri_ids)) {
5319 kfree(phba->sli4_hba.xri_bmask);
5320 rc = -ENOMEM;
5321 goto err_exit;
5322 }
5323
5324 /* Initialize local ptrs for common extent processing later. */
5325 bmask = phba->sli4_hba.xri_bmask;
5326 ids = phba->sli4_hba.xri_ids;
5327 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5328 break;
5329 case LPFC_RSC_TYPE_FCOE_VFI:
5330 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5331 sizeof(unsigned long),
5332 GFP_KERNEL);
5333 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5334 rc = -ENOMEM;
5335 goto err_exit;
5336 }
5337 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5338 sizeof(uint16_t),
5339 GFP_KERNEL);
5340 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5341 kfree(phba->sli4_hba.vfi_bmask);
5342 rc = -ENOMEM;
5343 goto err_exit;
5344 }
5345
5346 /* Initialize local ptrs for common extent processing later. */
5347 bmask = phba->sli4_hba.vfi_bmask;
5348 ids = phba->sli4_hba.vfi_ids;
5349 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5350 break;
5351 default:
5352 /* Unsupported Opcode. Fail call. */
5353 id_array = NULL;
5354 bmask = NULL;
5355 ids = NULL;
5356 ext_blk_list = NULL;
5357 goto err_exit;
5358 }
5359
5360 /*
5361 * Complete initializing the extent configuration with the
5362 * allocated ids assigned to this function. The bitmask serves
5363 * as an index into the array and manages the available ids. The
5364 * array just stores the ids communicated to the port via the wqes.
5365 */
5366 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5367 if ((i % 2) == 0)
5368 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5369 &id_array[k]);
5370 else
5371 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5372 &id_array[k]);
5373
5374 rsrc_blks = kzalloc(length, GFP_KERNEL);
5375 if (unlikely(!rsrc_blks)) {
5376 rc = -ENOMEM;
5377 kfree(bmask);
5378 kfree(ids);
5379 goto err_exit;
5380 }
5381 rsrc_blks->rsrc_start = rsrc_id;
5382 rsrc_blks->rsrc_size = rsrc_size;
5383 list_add_tail(&rsrc_blks->list, ext_blk_list);
5384 rsrc_start = rsrc_id;
5385 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5386 phba->sli4_hba.scsi_xri_start = rsrc_start +
5387 lpfc_sli4_get_els_iocb_cnt(phba);
5388
5389 while (rsrc_id < (rsrc_start + rsrc_size)) {
5390 ids[j] = rsrc_id;
5391 rsrc_id++;
5392 j++;
5393 }
5394 /* Entire word processed. Get next word.*/
5395 if ((i % 2) == 1)
5396 k++;
5397 }
5398 err_exit:
5399 lpfc_sli4_mbox_cmd_free(phba, mbox);
5400 return rc;
5401}
5402
5403/**
5404 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5405 * @phba: Pointer to HBA context object.
5406 * @type: the extent's type.
5407 *
5408 * This function deallocates all extents of a particular resource type.
5409 * SLI4 does not allow for deallocating a particular extent range. It
5410 * is the caller's responsibility to release all kernel memory resources.
5411 **/
5412static int
5413lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5414{
5415 int rc;
5416 uint32_t length, mbox_tmo = 0;
5417 LPFC_MBOXQ_t *mbox;
5418 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5419 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5420
5421 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5422 if (!mbox)
5423 return -ENOMEM;
5424
5425 /*
5426 * This function sends an embedded mailbox because it only sends the
5427 * the resource type. All extents of this type are released by the
5428 * port.
5429 */
5430 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5431 sizeof(struct lpfc_sli4_cfg_mhdr));
5432 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5433 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5434 length, LPFC_SLI4_MBX_EMBED);
5435
5436 /* Send an extents count of 0 - the dealloc doesn't use it. */
5437 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5438 LPFC_SLI4_MBX_EMBED);
5439 if (unlikely(rc)) {
5440 rc = -EIO;
5441 goto out_free_mbox;
5442 }
5443 if (!phba->sli4_hba.intr_enable)
5444 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5445 else {
James Smarta183a152011-10-10 21:32:43 -04005446 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005447 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5448 }
5449 if (unlikely(rc)) {
5450 rc = -EIO;
5451 goto out_free_mbox;
5452 }
5453
5454 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5455 if (bf_get(lpfc_mbox_hdr_status,
5456 &dealloc_rsrc->header.cfg_shdr.response)) {
5457 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5458 "2919 Failed to release resource extents "
5459 "for type %d - Status 0x%x Add'l Status 0x%x. "
5460 "Resource memory not released.\n",
5461 type,
5462 bf_get(lpfc_mbox_hdr_status,
5463 &dealloc_rsrc->header.cfg_shdr.response),
5464 bf_get(lpfc_mbox_hdr_add_status,
5465 &dealloc_rsrc->header.cfg_shdr.response));
5466 rc = -EIO;
5467 goto out_free_mbox;
5468 }
5469
5470 /* Release kernel memory resources for the specific type. */
5471 switch (type) {
5472 case LPFC_RSC_TYPE_FCOE_VPI:
5473 kfree(phba->vpi_bmask);
5474 kfree(phba->vpi_ids);
5475 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5476 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5477 &phba->lpfc_vpi_blk_list, list) {
5478 list_del_init(&rsrc_blk->list);
5479 kfree(rsrc_blk);
5480 }
5481 break;
5482 case LPFC_RSC_TYPE_FCOE_XRI:
5483 kfree(phba->sli4_hba.xri_bmask);
5484 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005485 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5486 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5487 list_del_init(&rsrc_blk->list);
5488 kfree(rsrc_blk);
5489 }
5490 break;
5491 case LPFC_RSC_TYPE_FCOE_VFI:
5492 kfree(phba->sli4_hba.vfi_bmask);
5493 kfree(phba->sli4_hba.vfi_ids);
5494 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5495 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5496 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5497 list_del_init(&rsrc_blk->list);
5498 kfree(rsrc_blk);
5499 }
5500 break;
5501 case LPFC_RSC_TYPE_FCOE_RPI:
5502 /* RPI bitmask and physical id array are cleaned up earlier. */
5503 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5504 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5505 list_del_init(&rsrc_blk->list);
5506 kfree(rsrc_blk);
5507 }
5508 break;
5509 default:
5510 break;
5511 }
5512
5513 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5514
5515 out_free_mbox:
5516 mempool_free(mbox, phba->mbox_mem_pool);
5517 return rc;
5518}
5519
5520/**
5521 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5522 * @phba: Pointer to HBA context object.
5523 *
5524 * This function allocates all SLI4 resource identifiers.
5525 **/
5526int
5527lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5528{
5529 int i, rc, error = 0;
5530 uint16_t count, base;
5531 unsigned long longs;
5532
James Smartff78d8f2011-12-13 13:21:35 -05005533 if (!phba->sli4_hba.rpi_hdrs_in_use)
5534 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005535 if (phba->sli4_hba.extents_in_use) {
5536 /*
5537 * The port supports resource extents. The XRI, VPI, VFI, RPI
5538 * resource extent count must be read and allocated before
5539 * provisioning the resource id arrays.
5540 */
5541 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5542 LPFC_IDX_RSRC_RDY) {
5543 /*
5544 * Extent-based resources are set - the driver could
5545 * be in a port reset. Figure out if any corrective
5546 * actions need to be taken.
5547 */
5548 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5549 LPFC_RSC_TYPE_FCOE_VFI);
5550 if (rc != 0)
5551 error++;
5552 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5553 LPFC_RSC_TYPE_FCOE_VPI);
5554 if (rc != 0)
5555 error++;
5556 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5557 LPFC_RSC_TYPE_FCOE_XRI);
5558 if (rc != 0)
5559 error++;
5560 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5561 LPFC_RSC_TYPE_FCOE_RPI);
5562 if (rc != 0)
5563 error++;
5564
5565 /*
5566 * It's possible that the number of resources
5567 * provided to this port instance changed between
5568 * resets. Detect this condition and reallocate
5569 * resources. Otherwise, there is no action.
5570 */
5571 if (error) {
5572 lpfc_printf_log(phba, KERN_INFO,
5573 LOG_MBOX | LOG_INIT,
5574 "2931 Detected extent resource "
5575 "change. Reallocating all "
5576 "extents.\n");
5577 rc = lpfc_sli4_dealloc_extent(phba,
5578 LPFC_RSC_TYPE_FCOE_VFI);
5579 rc = lpfc_sli4_dealloc_extent(phba,
5580 LPFC_RSC_TYPE_FCOE_VPI);
5581 rc = lpfc_sli4_dealloc_extent(phba,
5582 LPFC_RSC_TYPE_FCOE_XRI);
5583 rc = lpfc_sli4_dealloc_extent(phba,
5584 LPFC_RSC_TYPE_FCOE_RPI);
5585 } else
5586 return 0;
5587 }
5588
5589 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5590 if (unlikely(rc))
5591 goto err_exit;
5592
5593 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5594 if (unlikely(rc))
5595 goto err_exit;
5596
5597 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5598 if (unlikely(rc))
5599 goto err_exit;
5600
5601 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5602 if (unlikely(rc))
5603 goto err_exit;
5604 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5605 LPFC_IDX_RSRC_RDY);
5606 return rc;
5607 } else {
5608 /*
5609 * The port does not support resource extents. The XRI, VPI,
5610 * VFI, RPI resource ids were determined from READ_CONFIG.
5611 * Just allocate the bitmasks and provision the resource id
5612 * arrays. If a port reset is active, the resources don't
5613 * need any action - just exit.
5614 */
5615 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005616 LPFC_IDX_RSRC_RDY) {
5617 lpfc_sli4_dealloc_resource_identifiers(phba);
5618 lpfc_sli4_remove_rpis(phba);
5619 }
James Smart6d368e52011-05-24 11:44:12 -04005620 /* RPIs. */
5621 count = phba->sli4_hba.max_cfg_param.max_rpi;
5622 base = phba->sli4_hba.max_cfg_param.rpi_base;
5623 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5624 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5625 sizeof(unsigned long),
5626 GFP_KERNEL);
5627 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5628 rc = -ENOMEM;
5629 goto err_exit;
5630 }
5631 phba->sli4_hba.rpi_ids = kzalloc(count *
5632 sizeof(uint16_t),
5633 GFP_KERNEL);
5634 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5635 rc = -ENOMEM;
5636 goto free_rpi_bmask;
5637 }
5638
5639 for (i = 0; i < count; i++)
5640 phba->sli4_hba.rpi_ids[i] = base + i;
5641
5642 /* VPIs. */
5643 count = phba->sli4_hba.max_cfg_param.max_vpi;
5644 base = phba->sli4_hba.max_cfg_param.vpi_base;
5645 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5646 phba->vpi_bmask = kzalloc(longs *
5647 sizeof(unsigned long),
5648 GFP_KERNEL);
5649 if (unlikely(!phba->vpi_bmask)) {
5650 rc = -ENOMEM;
5651 goto free_rpi_ids;
5652 }
5653 phba->vpi_ids = kzalloc(count *
5654 sizeof(uint16_t),
5655 GFP_KERNEL);
5656 if (unlikely(!phba->vpi_ids)) {
5657 rc = -ENOMEM;
5658 goto free_vpi_bmask;
5659 }
5660
5661 for (i = 0; i < count; i++)
5662 phba->vpi_ids[i] = base + i;
5663
5664 /* XRIs. */
5665 count = phba->sli4_hba.max_cfg_param.max_xri;
5666 base = phba->sli4_hba.max_cfg_param.xri_base;
5667 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5668 phba->sli4_hba.xri_bmask = kzalloc(longs *
5669 sizeof(unsigned long),
5670 GFP_KERNEL);
5671 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5672 rc = -ENOMEM;
5673 goto free_vpi_ids;
5674 }
James Smart41899be2012-03-01 22:34:19 -05005675 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005676 phba->sli4_hba.xri_ids = kzalloc(count *
5677 sizeof(uint16_t),
5678 GFP_KERNEL);
5679 if (unlikely(!phba->sli4_hba.xri_ids)) {
5680 rc = -ENOMEM;
5681 goto free_xri_bmask;
5682 }
5683
5684 for (i = 0; i < count; i++)
5685 phba->sli4_hba.xri_ids[i] = base + i;
5686
5687 /* VFIs. */
5688 count = phba->sli4_hba.max_cfg_param.max_vfi;
5689 base = phba->sli4_hba.max_cfg_param.vfi_base;
5690 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5691 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5692 sizeof(unsigned long),
5693 GFP_KERNEL);
5694 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5695 rc = -ENOMEM;
5696 goto free_xri_ids;
5697 }
5698 phba->sli4_hba.vfi_ids = kzalloc(count *
5699 sizeof(uint16_t),
5700 GFP_KERNEL);
5701 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5702 rc = -ENOMEM;
5703 goto free_vfi_bmask;
5704 }
5705
5706 for (i = 0; i < count; i++)
5707 phba->sli4_hba.vfi_ids[i] = base + i;
5708
5709 /*
5710 * Mark all resources ready. An HBA reset doesn't need
5711 * to reset the initialization.
5712 */
5713 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5714 LPFC_IDX_RSRC_RDY);
5715 return 0;
5716 }
5717
5718 free_vfi_bmask:
5719 kfree(phba->sli4_hba.vfi_bmask);
5720 free_xri_ids:
5721 kfree(phba->sli4_hba.xri_ids);
5722 free_xri_bmask:
5723 kfree(phba->sli4_hba.xri_bmask);
5724 free_vpi_ids:
5725 kfree(phba->vpi_ids);
5726 free_vpi_bmask:
5727 kfree(phba->vpi_bmask);
5728 free_rpi_ids:
5729 kfree(phba->sli4_hba.rpi_ids);
5730 free_rpi_bmask:
5731 kfree(phba->sli4_hba.rpi_bmask);
5732 err_exit:
5733 return rc;
5734}
5735
5736/**
5737 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5738 * @phba: Pointer to HBA context object.
5739 *
5740 * This function allocates the number of elements for the specified
5741 * resource type.
5742 **/
5743int
5744lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5745{
5746 if (phba->sli4_hba.extents_in_use) {
5747 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5748 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5749 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5750 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5751 } else {
5752 kfree(phba->vpi_bmask);
5753 kfree(phba->vpi_ids);
5754 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5755 kfree(phba->sli4_hba.xri_bmask);
5756 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005757 kfree(phba->sli4_hba.vfi_bmask);
5758 kfree(phba->sli4_hba.vfi_ids);
5759 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5760 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5761 }
5762
5763 return 0;
5764}
5765
5766/**
James Smartb76f2dc2011-07-22 18:37:42 -04005767 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5768 * @phba: Pointer to HBA context object.
5769 * @type: The resource extent type.
5770 * @extnt_count: buffer to hold port extent count response
5771 * @extnt_size: buffer to hold port extent size response.
5772 *
5773 * This function calls the port to read the host allocated extents
5774 * for a particular type.
5775 **/
5776int
5777lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5778 uint16_t *extnt_cnt, uint16_t *extnt_size)
5779{
5780 bool emb;
5781 int rc = 0;
5782 uint16_t curr_blks = 0;
5783 uint32_t req_len, emb_len;
5784 uint32_t alloc_len, mbox_tmo;
5785 struct list_head *blk_list_head;
5786 struct lpfc_rsrc_blks *rsrc_blk;
5787 LPFC_MBOXQ_t *mbox;
5788 void *virtaddr = NULL;
5789 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5790 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5791 union lpfc_sli4_cfg_shdr *shdr;
5792
5793 switch (type) {
5794 case LPFC_RSC_TYPE_FCOE_VPI:
5795 blk_list_head = &phba->lpfc_vpi_blk_list;
5796 break;
5797 case LPFC_RSC_TYPE_FCOE_XRI:
5798 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5799 break;
5800 case LPFC_RSC_TYPE_FCOE_VFI:
5801 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5802 break;
5803 case LPFC_RSC_TYPE_FCOE_RPI:
5804 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5805 break;
5806 default:
5807 return -EIO;
5808 }
5809
5810 /* Count the number of extents currently allocatd for this type. */
5811 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5812 if (curr_blks == 0) {
5813 /*
5814 * The GET_ALLOCATED mailbox does not return the size,
5815 * just the count. The size should be just the size
5816 * stored in the current allocated block and all sizes
5817 * for an extent type are the same so set the return
5818 * value now.
5819 */
5820 *extnt_size = rsrc_blk->rsrc_size;
5821 }
5822 curr_blks++;
5823 }
5824
5825 /* Calculate the total requested length of the dma memory. */
5826 req_len = curr_blks * sizeof(uint16_t);
5827
5828 /*
5829 * Calculate the size of an embedded mailbox. The uint32_t
5830 * accounts for extents-specific word.
5831 */
5832 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5833 sizeof(uint32_t);
5834
5835 /*
5836 * Presume the allocation and response will fit into an embedded
5837 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5838 */
5839 emb = LPFC_SLI4_MBX_EMBED;
5840 req_len = emb_len;
5841 if (req_len > emb_len) {
5842 req_len = curr_blks * sizeof(uint16_t) +
5843 sizeof(union lpfc_sli4_cfg_shdr) +
5844 sizeof(uint32_t);
5845 emb = LPFC_SLI4_MBX_NEMBED;
5846 }
5847
5848 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5849 if (!mbox)
5850 return -ENOMEM;
5851 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5852
5853 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5854 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5855 req_len, emb);
5856 if (alloc_len < req_len) {
5857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5858 "2983 Allocated DMA memory size (x%x) is "
5859 "less than the requested DMA memory "
5860 "size (x%x)\n", alloc_len, req_len);
5861 rc = -ENOMEM;
5862 goto err_exit;
5863 }
5864 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5865 if (unlikely(rc)) {
5866 rc = -EIO;
5867 goto err_exit;
5868 }
5869
5870 if (!phba->sli4_hba.intr_enable)
5871 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5872 else {
James Smarta183a152011-10-10 21:32:43 -04005873 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005874 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5875 }
5876
5877 if (unlikely(rc)) {
5878 rc = -EIO;
5879 goto err_exit;
5880 }
5881
5882 /*
5883 * Figure out where the response is located. Then get local pointers
5884 * to the response data. The port does not guarantee to respond to
5885 * all extents counts request so update the local variable with the
5886 * allocated count from the port.
5887 */
5888 if (emb == LPFC_SLI4_MBX_EMBED) {
5889 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5890 shdr = &rsrc_ext->header.cfg_shdr;
5891 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5892 } else {
5893 virtaddr = mbox->sge_array->addr[0];
5894 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5895 shdr = &n_rsrc->cfg_shdr;
5896 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5897 }
5898
5899 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5900 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5901 "2984 Failed to read allocated resources "
5902 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5903 type,
5904 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5905 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5906 rc = -EIO;
5907 goto err_exit;
5908 }
5909 err_exit:
5910 lpfc_sli4_mbox_cmd_free(phba, mbox);
5911 return rc;
5912}
5913
5914/**
James Smart8a9d2e82012-05-09 21:16:12 -04005915 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5916 * @phba: pointer to lpfc hba data structure.
5917 *
5918 * This routine walks the list of els buffers that have been allocated and
5919 * repost them to the port by using SGL block post. This is needed after a
5920 * pci_function_reset/warm_start or start. It attempts to construct blocks
5921 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5922 * SGL block post mailbox commands to post them to the port. For single els
5923 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5924 * mailbox command for posting.
5925 *
5926 * Returns: 0 = success, non-zero failure.
5927 **/
5928static int
5929lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5930{
5931 struct lpfc_sglq *sglq_entry = NULL;
5932 struct lpfc_sglq *sglq_entry_next = NULL;
5933 struct lpfc_sglq *sglq_entry_first = NULL;
5934 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5935 int last_xritag = NO_XRI;
5936 LIST_HEAD(prep_sgl_list);
5937 LIST_HEAD(blck_sgl_list);
5938 LIST_HEAD(allc_sgl_list);
5939 LIST_HEAD(post_sgl_list);
5940 LIST_HEAD(free_sgl_list);
5941
5942 spin_lock(&phba->hbalock);
5943 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5944 spin_unlock(&phba->hbalock);
5945
5946 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5947 &allc_sgl_list, list) {
5948 list_del_init(&sglq_entry->list);
5949 block_cnt++;
5950 if ((last_xritag != NO_XRI) &&
5951 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5952 /* a hole in xri block, form a sgl posting block */
5953 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5954 post_cnt = block_cnt - 1;
5955 /* prepare list for next posting block */
5956 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5957 block_cnt = 1;
5958 } else {
5959 /* prepare list for next posting block */
5960 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5961 /* enough sgls for non-embed sgl mbox command */
5962 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5963 list_splice_init(&prep_sgl_list,
5964 &blck_sgl_list);
5965 post_cnt = block_cnt;
5966 block_cnt = 0;
5967 }
5968 }
5969 num_posted++;
5970
5971 /* keep track of last sgl's xritag */
5972 last_xritag = sglq_entry->sli4_xritag;
5973
5974 /* end of repost sgl list condition for els buffers */
5975 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5976 if (post_cnt == 0) {
5977 list_splice_init(&prep_sgl_list,
5978 &blck_sgl_list);
5979 post_cnt = block_cnt;
5980 } else if (block_cnt == 1) {
5981 status = lpfc_sli4_post_sgl(phba,
5982 sglq_entry->phys, 0,
5983 sglq_entry->sli4_xritag);
5984 if (!status) {
5985 /* successful, put sgl to posted list */
5986 list_add_tail(&sglq_entry->list,
5987 &post_sgl_list);
5988 } else {
5989 /* Failure, put sgl to free list */
5990 lpfc_printf_log(phba, KERN_WARNING,
5991 LOG_SLI,
5992 "3159 Failed to post els "
5993 "sgl, xritag:x%x\n",
5994 sglq_entry->sli4_xritag);
5995 list_add_tail(&sglq_entry->list,
5996 &free_sgl_list);
5997 spin_lock_irq(&phba->hbalock);
5998 phba->sli4_hba.els_xri_cnt--;
5999 spin_unlock_irq(&phba->hbalock);
6000 }
6001 }
6002 }
6003
6004 /* continue until a nembed page worth of sgls */
6005 if (post_cnt == 0)
6006 continue;
6007
6008 /* post the els buffer list sgls as a block */
6009 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6010 post_cnt);
6011
6012 if (!status) {
6013 /* success, put sgl list to posted sgl list */
6014 list_splice_init(&blck_sgl_list, &post_sgl_list);
6015 } else {
6016 /* Failure, put sgl list to free sgl list */
6017 sglq_entry_first = list_first_entry(&blck_sgl_list,
6018 struct lpfc_sglq,
6019 list);
6020 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6021 "3160 Failed to post els sgl-list, "
6022 "xritag:x%x-x%x\n",
6023 sglq_entry_first->sli4_xritag,
6024 (sglq_entry_first->sli4_xritag +
6025 post_cnt - 1));
6026 list_splice_init(&blck_sgl_list, &free_sgl_list);
6027 spin_lock_irq(&phba->hbalock);
6028 phba->sli4_hba.els_xri_cnt -= post_cnt;
6029 spin_unlock_irq(&phba->hbalock);
6030 }
6031
6032 /* don't reset xirtag due to hole in xri block */
6033 if (block_cnt == 0)
6034 last_xritag = NO_XRI;
6035
6036 /* reset els sgl post count for next round of posting */
6037 post_cnt = 0;
6038 }
6039
6040 /* free the els sgls failed to post */
6041 lpfc_free_sgl_list(phba, &free_sgl_list);
6042
6043 /* push els sgls posted to the availble list */
6044 if (!list_empty(&post_sgl_list)) {
6045 spin_lock(&phba->hbalock);
6046 list_splice_init(&post_sgl_list,
6047 &phba->sli4_hba.lpfc_sgl_list);
6048 spin_unlock(&phba->hbalock);
6049 } else {
6050 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6051 "3161 Failure to post els sgl to port.\n");
6052 return -EIO;
6053 }
6054 return 0;
6055}
6056
6057/**
James Smartda0436e2009-05-22 14:51:39 -04006058 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6059 * @phba: Pointer to HBA context object.
6060 *
6061 * This function is the main SLI4 device intialization PCI function. This
6062 * function is called by the HBA intialization code, HBA reset code and
6063 * HBA error attention handler code. Caller is not required to hold any
6064 * locks.
6065 **/
6066int
6067lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6068{
6069 int rc;
6070 LPFC_MBOXQ_t *mboxq;
6071 struct lpfc_mqe *mqe;
6072 uint8_t *vpd;
6073 uint32_t vpd_size;
6074 uint32_t ftr_rsp = 0;
6075 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6076 struct lpfc_vport *vport = phba->pport;
6077 struct lpfc_dmabuf *mp;
6078
6079 /* Perform a PCI function reset to start from clean */
6080 rc = lpfc_pci_function_reset(phba);
6081 if (unlikely(rc))
6082 return -ENODEV;
6083
6084 /* Check the HBA Host Status Register for readyness */
6085 rc = lpfc_sli4_post_status_check(phba);
6086 if (unlikely(rc))
6087 return -ENODEV;
6088 else {
6089 spin_lock_irq(&phba->hbalock);
6090 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6091 spin_unlock_irq(&phba->hbalock);
6092 }
6093
6094 /*
6095 * Allocate a single mailbox container for initializing the
6096 * port.
6097 */
6098 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6099 if (!mboxq)
6100 return -ENOMEM;
6101
James Smartda0436e2009-05-22 14:51:39 -04006102 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006103 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006104 vpd = kzalloc(vpd_size, GFP_KERNEL);
6105 if (!vpd) {
6106 rc = -ENOMEM;
6107 goto out_free_mbox;
6108 }
6109
6110 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006111 if (unlikely(rc)) {
6112 kfree(vpd);
6113 goto out_free_mbox;
6114 }
James Smartda0436e2009-05-22 14:51:39 -04006115 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006116 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6117 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006118 phba->hba_flag |= HBA_FCOE_MODE;
6119 else
6120 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006121
6122 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6123 LPFC_DCBX_CEE_MODE)
6124 phba->hba_flag |= HBA_FIP_SUPPORT;
6125 else
6126 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6127
James Smart4f2e66c2012-05-09 21:17:07 -04006128 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6129
James Smartc31098c2011-04-16 11:03:33 -04006130 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006131 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6132 "0376 READ_REV Error. SLI Level %d "
6133 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006134 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006135 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006136 kfree(vpd);
6137 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006138 }
James Smartcd1c8302011-10-10 21:33:25 -04006139
6140 /*
James Smartff78d8f2011-12-13 13:21:35 -05006141 * Continue initialization with default values even if driver failed
6142 * to read FCoE param config regions, only read parameters if the
6143 * board is FCoE
6144 */
6145 if (phba->hba_flag & HBA_FCOE_MODE &&
6146 lpfc_sli4_read_fcoe_params(phba))
6147 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6148 "2570 Failed to read FCoE parameters\n");
6149
6150 /*
James Smartcd1c8302011-10-10 21:33:25 -04006151 * Retrieve sli4 device physical port name, failure of doing it
6152 * is considered as non-fatal.
6153 */
6154 rc = lpfc_sli4_retrieve_pport_name(phba);
6155 if (!rc)
6156 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6157 "3080 Successful retrieving SLI4 device "
6158 "physical port name: %s.\n", phba->Port);
6159
James Smartda0436e2009-05-22 14:51:39 -04006160 /*
6161 * Evaluate the read rev and vpd data. Populate the driver
6162 * state with the results. If this routine fails, the failure
6163 * is not fatal as the driver will use generic values.
6164 */
6165 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6166 if (unlikely(!rc)) {
6167 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6168 "0377 Error %d parsing vpd. "
6169 "Using defaults.\n", rc);
6170 rc = 0;
6171 }
James Smart76a95d72010-11-20 23:11:48 -05006172 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006173
James Smartf1126682009-06-10 17:22:44 -04006174 /* Save information as VPD data */
6175 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6176 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6177 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6178 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6179 &mqe->un.read_rev);
6180 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6181 &mqe->un.read_rev);
6182 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6183 &mqe->un.read_rev);
6184 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6185 &mqe->un.read_rev);
6186 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6187 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6188 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6189 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6190 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6191 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6192 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6193 "(%d):0380 READ_REV Status x%x "
6194 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6195 mboxq->vport ? mboxq->vport->vpi : 0,
6196 bf_get(lpfc_mqe_status, mqe),
6197 phba->vpd.rev.opFwName,
6198 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6199 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006200
6201 /*
6202 * Discover the port's supported feature set and match it against the
6203 * hosts requests.
6204 */
6205 lpfc_request_features(phba, mboxq);
6206 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6207 if (unlikely(rc)) {
6208 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006209 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006210 }
6211
6212 /*
6213 * The port must support FCP initiator mode as this is the
6214 * only mode running in the host.
6215 */
6216 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6217 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6218 "0378 No support for fcpi mode.\n");
6219 ftr_rsp++;
6220 }
James Smartfedd3b72011-02-16 12:39:24 -05006221 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6222 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6223 else
6224 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006225 /*
6226 * If the port cannot support the host's requested features
6227 * then turn off the global config parameters to disable the
6228 * feature in the driver. This is not a fatal error.
6229 */
James Smartbf086112011-08-21 21:48:13 -04006230 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6231 if (phba->cfg_enable_bg) {
6232 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6233 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6234 else
6235 ftr_rsp++;
6236 }
James Smartda0436e2009-05-22 14:51:39 -04006237
6238 if (phba->max_vpi && phba->cfg_enable_npiv &&
6239 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6240 ftr_rsp++;
6241
6242 if (ftr_rsp) {
6243 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6244 "0379 Feature Mismatch Data: x%08x %08x "
6245 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6246 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6247 phba->cfg_enable_npiv, phba->max_vpi);
6248 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6249 phba->cfg_enable_bg = 0;
6250 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6251 phba->cfg_enable_npiv = 0;
6252 }
6253
6254 /* These SLI3 features are assumed in SLI4 */
6255 spin_lock_irq(&phba->hbalock);
6256 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6257 spin_unlock_irq(&phba->hbalock);
6258
James Smart6d368e52011-05-24 11:44:12 -04006259 /*
6260 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6261 * calls depends on these resources to complete port setup.
6262 */
6263 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6264 if (rc) {
6265 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6266 "2920 Failed to alloc Resource IDs "
6267 "rc = x%x\n", rc);
6268 goto out_free_mbox;
6269 }
6270
James Smartda0436e2009-05-22 14:51:39 -04006271 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006272 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6273 if (rc) {
6274 phba->link_state = LPFC_HBA_ERROR;
6275 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006276 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006277 }
6278
James Smartda0436e2009-05-22 14:51:39 -04006279 mboxq->vport = vport;
6280 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6281 mp = (struct lpfc_dmabuf *) mboxq->context1;
6282 if (rc == MBX_SUCCESS) {
6283 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6284 rc = 0;
6285 }
6286
6287 /*
6288 * This memory was allocated by the lpfc_read_sparam routine. Release
6289 * it to the mbuf pool.
6290 */
6291 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6292 kfree(mp);
6293 mboxq->context1 = NULL;
6294 if (unlikely(rc)) {
6295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6296 "0382 READ_SPARAM command failed "
6297 "status %d, mbxStatus x%x\n",
6298 rc, bf_get(lpfc_mqe_status, mqe));
6299 phba->link_state = LPFC_HBA_ERROR;
6300 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006301 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006302 }
6303
James Smart05580562011-05-24 11:40:48 -04006304 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006305
6306 /* Update the fc_host data structures with new wwn. */
6307 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6308 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6309
James Smart8a9d2e82012-05-09 21:16:12 -04006310 /* update host els and scsi xri-sgl sizes and mappings */
6311 rc = lpfc_sli4_xri_sgl_update(phba);
6312 if (unlikely(rc)) {
6313 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6314 "1400 Failed to update xri-sgl size and "
6315 "mapping: %d\n", rc);
6316 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006317 }
6318
James Smart8a9d2e82012-05-09 21:16:12 -04006319 /* register the els sgl pool to the port */
6320 rc = lpfc_sli4_repost_els_sgl_list(phba);
6321 if (unlikely(rc)) {
6322 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6323 "0582 Error %d during els sgl post "
6324 "operation\n", rc);
6325 rc = -ENODEV;
6326 goto out_free_mbox;
6327 }
6328
6329 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006330 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6331 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006332 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006333 "0383 Error %d during scsi sgl post "
6334 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006335 /* Some Scsi buffers were moved to the abort scsi list */
6336 /* A pci function reset will repost them */
6337 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006338 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006339 }
6340
6341 /* Post the rpi header region to the device. */
6342 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6343 if (unlikely(rc)) {
6344 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6345 "0393 Error %d during rpi post operation\n",
6346 rc);
6347 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006348 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006349 }
James Smart97f2ecf2012-03-01 22:35:23 -05006350 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006351
James Smart5350d872011-10-10 21:33:49 -04006352 /* Create all the SLI4 queues */
6353 rc = lpfc_sli4_queue_create(phba);
6354 if (rc) {
6355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6356 "3089 Failed to allocate queues\n");
6357 rc = -ENODEV;
6358 goto out_stop_timers;
6359 }
James Smartda0436e2009-05-22 14:51:39 -04006360 /* Set up all the queues to the device */
6361 rc = lpfc_sli4_queue_setup(phba);
6362 if (unlikely(rc)) {
6363 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6364 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006365 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006366 }
6367
6368 /* Arm the CQs and then EQs on device */
6369 lpfc_sli4_arm_cqeq_intr(phba);
6370
6371 /* Indicate device interrupt mode */
6372 phba->sli4_hba.intr_enable = 1;
6373
6374 /* Allow asynchronous mailbox command to go through */
6375 spin_lock_irq(&phba->hbalock);
6376 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6377 spin_unlock_irq(&phba->hbalock);
6378
6379 /* Post receive buffers to the device */
6380 lpfc_sli4_rb_setup(phba);
6381
James Smartfc2b9892010-02-26 14:15:29 -05006382 /* Reset HBA FCF states after HBA reset */
6383 phba->fcf.fcf_flag = 0;
6384 phba->fcf.current_rec.flag = 0;
6385
James Smartda0436e2009-05-22 14:51:39 -04006386 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006387 mod_timer(&vport->els_tmofunc,
6388 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006389
6390 /* Start heart beat timer */
6391 mod_timer(&phba->hb_tmofunc,
6392 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6393 phba->hb_outstanding = 0;
6394 phba->last_completion_time = jiffies;
6395
6396 /* Start error attention (ERATT) polling timer */
6397 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6398
James Smart75baf692010-06-08 18:31:21 -04006399 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6400 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6401 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6402 if (!rc) {
6403 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6404 "2829 This device supports "
6405 "Advanced Error Reporting (AER)\n");
6406 spin_lock_irq(&phba->hbalock);
6407 phba->hba_flag |= HBA_AER_ENABLED;
6408 spin_unlock_irq(&phba->hbalock);
6409 } else {
6410 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6411 "2830 This device does not support "
6412 "Advanced Error Reporting (AER)\n");
6413 phba->cfg_aer_support = 0;
6414 }
James Smart0a96e972011-07-22 18:37:28 -04006415 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006416 }
6417
James Smart76a95d72010-11-20 23:11:48 -05006418 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6419 /*
6420 * The FC Port needs to register FCFI (index 0)
6421 */
6422 lpfc_reg_fcfi(phba, mboxq);
6423 mboxq->vport = phba->pport;
6424 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b062011-04-16 11:03:17 -04006425 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006426 goto out_unset_queue;
James Smart9589b062011-04-16 11:03:17 -04006427 rc = 0;
6428 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6429 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006430
6431 /* Check if the port is configured to be disabled */
6432 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006433 }
James Smart026abb82011-12-13 13:20:45 -05006434
James Smartda0436e2009-05-22 14:51:39 -04006435 /*
6436 * The port is ready, set the host's link state to LINK_DOWN
6437 * in preparation for link interrupts.
6438 */
James Smartda0436e2009-05-22 14:51:39 -04006439 spin_lock_irq(&phba->hbalock);
6440 phba->link_state = LPFC_LINK_DOWN;
6441 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006442 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6443 (phba->hba_flag & LINK_DISABLED)) {
6444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6445 "3103 Adapter Link is disabled.\n");
6446 lpfc_down_link(phba, mboxq);
6447 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6448 if (rc != MBX_SUCCESS) {
6449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6450 "3104 Adapter failed to issue "
6451 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6452 goto out_unset_queue;
6453 }
6454 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006455 /* don't perform init_link on SLI4 FC port loopback test */
6456 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6457 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6458 if (rc)
6459 goto out_unset_queue;
6460 }
James Smart5350d872011-10-10 21:33:49 -04006461 }
6462 mempool_free(mboxq, phba->mbox_mem_pool);
6463 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006464out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006465 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006466 lpfc_sli4_queue_unset(phba);
6467out_destroy_queue:
6468 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006469out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006470 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006471out_free_mbox:
6472 mempool_free(mboxq, phba->mbox_mem_pool);
6473 return rc;
6474}
James Smarte59058c2008-08-24 21:49:00 -04006475
6476/**
James Smart3621a712009-04-06 18:47:14 -04006477 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006478 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006479 *
James Smarte59058c2008-08-24 21:49:00 -04006480 * This is the callback function for mailbox timer. The mailbox
6481 * timer is armed when a new mailbox command is issued and the timer
6482 * is deleted when the mailbox complete. The function is called by
6483 * the kernel timer code when a mailbox does not complete within
6484 * expected time. This function wakes up the worker thread to
6485 * process the mailbox timeout and returns. All the processing is
6486 * done by the worker thread function lpfc_mbox_timeout_handler.
6487 **/
dea31012005-04-17 16:05:31 -05006488void
6489lpfc_mbox_timeout(unsigned long ptr)
6490{
James Smart92d7f7b2007-06-17 19:56:38 -05006491 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006492 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006493 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006494
James Smart2e0fef82007-06-17 19:56:36 -05006495 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006496 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006497 if (!tmo_posted)
6498 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6499 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6500
James Smart5e9d9b82008-06-14 22:52:53 -04006501 if (!tmo_posted)
6502 lpfc_worker_wake_up(phba);
6503 return;
dea31012005-04-17 16:05:31 -05006504}
6505
James Smarte59058c2008-08-24 21:49:00 -04006506
6507/**
James Smart3621a712009-04-06 18:47:14 -04006508 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006509 * @phba: Pointer to HBA context object.
6510 *
6511 * This function is called from worker thread when a mailbox command times out.
6512 * The caller is not required to hold any locks. This function will reset the
6513 * HBA and recover all the pending commands.
6514 **/
dea31012005-04-17 16:05:31 -05006515void
6516lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6517{
James Smart2e0fef82007-06-17 19:56:36 -05006518 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006519 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006520 struct lpfc_sli *psli = &phba->sli;
6521 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006522
James Smarta257bf92009-04-06 18:48:10 -04006523 /* Check the pmbox pointer first. There is a race condition
6524 * between the mbox timeout handler getting executed in the
6525 * worklist and the mailbox actually completing. When this
6526 * race condition occurs, the mbox_active will be NULL.
6527 */
6528 spin_lock_irq(&phba->hbalock);
6529 if (pmbox == NULL) {
6530 lpfc_printf_log(phba, KERN_WARNING,
6531 LOG_MBOX | LOG_SLI,
6532 "0353 Active Mailbox cleared - mailbox timeout "
6533 "exiting\n");
6534 spin_unlock_irq(&phba->hbalock);
6535 return;
6536 }
6537
dea31012005-04-17 16:05:31 -05006538 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006539 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006540 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006541 mb->mbxCommand,
6542 phba->pport->port_state,
6543 phba->sli.sli_flag,
6544 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006545 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006546
James Smart1dcb58e2007-04-25 09:51:30 -04006547 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6548 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006549 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006550 */
James Smart2e0fef82007-06-17 19:56:36 -05006551 spin_lock_irq(&phba->pport->work_port_lock);
6552 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6553 spin_unlock_irq(&phba->pport->work_port_lock);
6554 spin_lock_irq(&phba->hbalock);
6555 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006556 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006557 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006558
6559 pring = &psli->ring[psli->fcp_ring];
6560 lpfc_sli_abort_iocb_ring(phba, pring);
6561
6562 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006563 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006564
6565 /* Reset the HBA device */
6566 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006567}
6568
James Smarte59058c2008-08-24 21:49:00 -04006569/**
James Smart3772a992009-05-22 14:50:54 -04006570 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006571 * @phba: Pointer to HBA context object.
6572 * @pmbox: Pointer to mailbox object.
6573 * @flag: Flag indicating how the mailbox need to be processed.
6574 *
6575 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006576 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6577 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006578 * The mailbox command can be submitted in polling mode, in which case
6579 * this function will wait in a polling loop for the completion of the
6580 * mailbox.
6581 * If the mailbox is submitted in no_wait mode (not polling) the
6582 * function will submit the command and returns immediately without waiting
6583 * for the mailbox completion. The no_wait is supported only when HBA
6584 * is in SLI2/SLI3 mode - interrupts are enabled.
6585 * The SLI interface allows only one mailbox pending at a time. If the
6586 * mailbox is issued in polling mode and there is already a mailbox
6587 * pending, then the function will return an error. If the mailbox is issued
6588 * in NO_WAIT mode and there is a mailbox pending already, the function
6589 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6590 * The sli layer owns the mailbox object until the completion of mailbox
6591 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6592 * return codes the caller owns the mailbox command after the return of
6593 * the function.
6594 **/
James Smart3772a992009-05-22 14:50:54 -04006595static int
6596lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6597 uint32_t flag)
dea31012005-04-17 16:05:31 -05006598{
dea31012005-04-17 16:05:31 -05006599 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006600 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006601 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006602 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006603 int i;
James Smart09372822008-01-11 01:52:54 -05006604 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006605 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006606 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006607 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006608 int processing_queue = 0;
6609
6610 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6611 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006612 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006613 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006614 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6615 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6616 return MBX_SUCCESS;
6617 }
James Smart58da1ff2008-04-07 10:15:56 -04006618 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006619 pmbox = lpfc_mbox_get(phba);
6620 if (!pmbox) {
6621 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6622 return MBX_SUCCESS;
6623 }
6624 }
dea31012005-04-17 16:05:31 -05006625
James Smarted957682007-06-17 19:56:37 -05006626 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006627 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006628 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006629 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006630 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006631 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006632 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006633 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006634 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006635 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006636 }
6637 }
6638
Linas Vepstas8d63f372007-02-14 14:28:36 -06006639 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006640 if (unlikely(pci_channel_offline(phba->pcidev))) {
6641 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6642 goto out_not_finished;
6643 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006644
James Smarta257bf92009-04-06 18:48:10 -04006645 /* If HBA has a deferred error attention, fail the iocb. */
6646 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6647 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6648 goto out_not_finished;
6649 }
6650
dea31012005-04-17 16:05:31 -05006651 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006652
James Smart3772a992009-05-22 14:50:54 -04006653 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006654 status = MBX_SUCCESS;
6655
James Smart2e0fef82007-06-17 19:56:36 -05006656 if (phba->link_state == LPFC_HBA_ERROR) {
6657 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006658
6659 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006660 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6661 "(%d):0311 Mailbox command x%x cannot "
6662 "issue Data: x%x x%x\n",
6663 pmbox->vport ? pmbox->vport->vpi : 0,
6664 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006665 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006666 }
6667
James Smart9940b972011-03-11 16:06:12 -05006668 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6669 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6670 !(hc_copy & HC_MBINT_ENA)) {
6671 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006673 "(%d):2528 Mailbox command x%x cannot "
6674 "issue Data: x%x x%x\n",
6675 pmbox->vport ? pmbox->vport->vpi : 0,
6676 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006677 goto out_not_finished;
6678 }
James Smart92908312006-03-07 15:04:13 -05006679 }
6680
dea31012005-04-17 16:05:31 -05006681 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6682 /* Polling for a mbox command when another one is already active
6683 * is not allowed in SLI. Also, the driver must have established
6684 * SLI2 mode to queue and process multiple mbox commands.
6685 */
6686
6687 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006688 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006689
6690 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006691 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6692 "(%d):2529 Mailbox command x%x "
6693 "cannot issue Data: x%x x%x\n",
6694 pmbox->vport ? pmbox->vport->vpi : 0,
6695 pmbox->u.mb.mbxCommand,
6696 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006697 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006698 }
6699
James Smart3772a992009-05-22 14:50:54 -04006700 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006701 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006702 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006703 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6704 "(%d):2530 Mailbox command x%x "
6705 "cannot issue Data: x%x x%x\n",
6706 pmbox->vport ? pmbox->vport->vpi : 0,
6707 pmbox->u.mb.mbxCommand,
6708 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006709 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006710 }
6711
dea31012005-04-17 16:05:31 -05006712 /* Another mailbox command is still being processed, queue this
6713 * command to be processed later.
6714 */
6715 lpfc_mbox_put(phba, pmbox);
6716
6717 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006718 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006719 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006720 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006721 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6722 mb->mbxCommand, phba->pport->port_state,
6723 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006724
6725 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006726 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006727
James Smart858c9f62007-06-17 19:56:39 -05006728 if (pmbox->vport) {
6729 lpfc_debugfs_disc_trc(pmbox->vport,
6730 LPFC_DISC_TRC_MBOX_VPORT,
6731 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6732 (uint32_t)mb->mbxCommand,
6733 mb->un.varWords[0], mb->un.varWords[1]);
6734 }
6735 else {
6736 lpfc_debugfs_disc_trc(phba->pport,
6737 LPFC_DISC_TRC_MBOX,
6738 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6739 (uint32_t)mb->mbxCommand,
6740 mb->un.varWords[0], mb->un.varWords[1]);
6741 }
6742
James Smart2e0fef82007-06-17 19:56:36 -05006743 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006744 }
6745
dea31012005-04-17 16:05:31 -05006746 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6747
6748 /* If we are not polling, we MUST be in SLI2 mode */
6749 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006750 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006751 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006752 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006753 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006754 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006755 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6756 "(%d):2531 Mailbox command x%x "
6757 "cannot issue Data: x%x x%x\n",
6758 pmbox->vport ? pmbox->vport->vpi : 0,
6759 pmbox->u.mb.mbxCommand,
6760 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006761 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006762 }
6763 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006764 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006765 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006766 }
6767
6768 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006769 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006770 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006771 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006772 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006773 mb->mbxCommand, phba->pport->port_state,
6774 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006775
James Smart858c9f62007-06-17 19:56:39 -05006776 if (mb->mbxCommand != MBX_HEARTBEAT) {
6777 if (pmbox->vport) {
6778 lpfc_debugfs_disc_trc(pmbox->vport,
6779 LPFC_DISC_TRC_MBOX_VPORT,
6780 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6781 (uint32_t)mb->mbxCommand,
6782 mb->un.varWords[0], mb->un.varWords[1]);
6783 }
6784 else {
6785 lpfc_debugfs_disc_trc(phba->pport,
6786 LPFC_DISC_TRC_MBOX,
6787 "MBOX Send: cmd:x%x mb:x%x x%x",
6788 (uint32_t)mb->mbxCommand,
6789 mb->un.varWords[0], mb->un.varWords[1]);
6790 }
6791 }
6792
dea31012005-04-17 16:05:31 -05006793 psli->slistat.mbox_cmd++;
6794 evtctr = psli->slistat.mbox_event;
6795
6796 /* next set own bit for the adapter and copy over command word */
6797 mb->mbxOwner = OWN_CHIP;
6798
James Smart3772a992009-05-22 14:50:54 -04006799 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006800 /* Populate mbox extension offset word. */
6801 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6802 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6803 = (uint8_t *)phba->mbox_ext
6804 - (uint8_t *)phba->mbox;
6805 }
6806
6807 /* Copy the mailbox extension data */
6808 if (pmbox->in_ext_byte_len && pmbox->context2) {
6809 lpfc_sli_pcimem_bcopy(pmbox->context2,
6810 (uint8_t *)phba->mbox_ext,
6811 pmbox->in_ext_byte_len);
6812 }
6813 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006814 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006815 } else {
James Smart7a470272010-03-15 11:25:20 -04006816 /* Populate mbox extension offset word. */
6817 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6818 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6819 = MAILBOX_HBA_EXT_OFFSET;
6820
6821 /* Copy the mailbox extension data */
6822 if (pmbox->in_ext_byte_len && pmbox->context2) {
6823 lpfc_memcpy_to_slim(phba->MBslimaddr +
6824 MAILBOX_HBA_EXT_OFFSET,
6825 pmbox->context2, pmbox->in_ext_byte_len);
6826
6827 }
James Smart92908312006-03-07 15:04:13 -05006828 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006829 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006830 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006831 }
6832
6833 /* First copy mbox command data to HBA SLIM, skip past first
6834 word */
6835 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6836 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6837 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6838
6839 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006840 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006841 to_slim = phba->MBslimaddr;
6842 writel(ldata, to_slim);
6843 readl(to_slim); /* flush */
6844
6845 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6846 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006847 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006848 }
6849 }
6850
6851 wmb();
dea31012005-04-17 16:05:31 -05006852
6853 switch (flag) {
6854 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006855 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006856 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006857 /* Interrupt board to do it */
6858 writel(CA_MBATT, phba->CAregaddr);
6859 readl(phba->CAregaddr); /* flush */
6860 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006861 break;
6862
6863 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006864 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006865 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006866 /* Interrupt board to do it */
6867 writel(CA_MBATT, phba->CAregaddr);
6868 readl(phba->CAregaddr); /* flush */
6869
James Smart3772a992009-05-22 14:50:54 -04006870 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006871 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006872 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006873 word0 = le32_to_cpu(word0);
6874 } else {
6875 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006876 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6877 spin_unlock_irqrestore(&phba->hbalock,
6878 drvr_flag);
6879 goto out_not_finished;
6880 }
dea31012005-04-17 16:05:31 -05006881 }
6882
6883 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006884 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6885 spin_unlock_irqrestore(&phba->hbalock,
6886 drvr_flag);
6887 goto out_not_finished;
6888 }
James Smarta183a152011-10-10 21:32:43 -04006889 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6890 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006891 i = 0;
dea31012005-04-17 16:05:31 -05006892 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006893 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6894 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006895 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006896 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006897 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006898 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006899 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006900 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006901 }
6902
6903 /* Check if we took a mbox interrupt while we were
6904 polling */
6905 if (((word0 & OWN_CHIP) != OWN_CHIP)
6906 && (evtctr != psli->slistat.mbox_event))
6907 break;
6908
James Smart09372822008-01-11 01:52:54 -05006909 if (i++ > 10) {
6910 spin_unlock_irqrestore(&phba->hbalock,
6911 drvr_flag);
6912 msleep(1);
6913 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6914 }
dea31012005-04-17 16:05:31 -05006915
James Smart3772a992009-05-22 14:50:54 -04006916 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006917 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006918 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006919 word0 = le32_to_cpu(word0);
6920 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6921 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006922 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006923 /* Check real SLIM for any errors */
6924 slimword0 = readl(phba->MBslimaddr);
6925 slimmb = (MAILBOX_t *) & slimword0;
6926 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6927 && slimmb->mbxStatus) {
6928 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006929 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006930 word0 = slimword0;
6931 }
6932 }
6933 } else {
6934 /* First copy command data */
6935 word0 = readl(phba->MBslimaddr);
6936 }
6937 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006938 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6939 spin_unlock_irqrestore(&phba->hbalock,
6940 drvr_flag);
6941 goto out_not_finished;
6942 }
dea31012005-04-17 16:05:31 -05006943 }
6944
James Smart3772a992009-05-22 14:50:54 -04006945 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006946 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006947 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006948 /* Copy the mailbox extension data */
6949 if (pmbox->out_ext_byte_len && pmbox->context2) {
6950 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6951 pmbox->context2,
6952 pmbox->out_ext_byte_len);
6953 }
dea31012005-04-17 16:05:31 -05006954 } else {
6955 /* First copy command data */
6956 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6957 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006958 /* Copy the mailbox extension data */
6959 if (pmbox->out_ext_byte_len && pmbox->context2) {
6960 lpfc_memcpy_from_slim(pmbox->context2,
6961 phba->MBslimaddr +
6962 MAILBOX_HBA_EXT_OFFSET,
6963 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006964 }
6965 }
6966
6967 writel(HA_MBATT, phba->HAregaddr);
6968 readl(phba->HAregaddr); /* flush */
6969
6970 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6971 status = mb->mbxStatus;
6972 }
6973
James Smart2e0fef82007-06-17 19:56:36 -05006974 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6975 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006976
6977out_not_finished:
6978 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006979 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006980 lpfc_mbox_cmpl_put(phba, pmbox);
6981 }
6982 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006983}
6984
James Smarte59058c2008-08-24 21:49:00 -04006985/**
James Smartf1126682009-06-10 17:22:44 -04006986 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6987 * @phba: Pointer to HBA context object.
6988 *
6989 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6990 * the driver internal pending mailbox queue. It will then try to wait out the
6991 * possible outstanding mailbox command before return.
6992 *
6993 * Returns:
6994 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6995 * the outstanding mailbox command timed out.
6996 **/
6997static int
6998lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6999{
7000 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007001 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007002 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007003
7004 /* Mark the asynchronous mailbox command posting as blocked */
7005 spin_lock_irq(&phba->hbalock);
7006 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007007 /* Determine how long we might wait for the active mailbox
7008 * command to be gracefully completed by firmware.
7009 */
James Smarta183a152011-10-10 21:32:43 -04007010 if (phba->sli.mbox_active)
7011 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7012 phba->sli.mbox_active) *
7013 1000) + jiffies;
7014 spin_unlock_irq(&phba->hbalock);
7015
James Smartf1126682009-06-10 17:22:44 -04007016 /* Wait for the outstnading mailbox command to complete */
7017 while (phba->sli.mbox_active) {
7018 /* Check active mailbox complete status every 2ms */
7019 msleep(2);
7020 if (time_after(jiffies, timeout)) {
7021 /* Timeout, marked the outstanding cmd not complete */
7022 rc = 1;
7023 break;
7024 }
7025 }
7026
7027 /* Can not cleanly block async mailbox command, fails it */
7028 if (rc) {
7029 spin_lock_irq(&phba->hbalock);
7030 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7031 spin_unlock_irq(&phba->hbalock);
7032 }
7033 return rc;
7034}
7035
7036/**
7037 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7038 * @phba: Pointer to HBA context object.
7039 *
7040 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7041 * commands from the driver internal pending mailbox queue. It makes sure
7042 * that there is no outstanding mailbox command before resuming posting
7043 * asynchronous mailbox commands. If, for any reason, there is outstanding
7044 * mailbox command, it will try to wait it out before resuming asynchronous
7045 * mailbox command posting.
7046 **/
7047static void
7048lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7049{
7050 struct lpfc_sli *psli = &phba->sli;
7051
7052 spin_lock_irq(&phba->hbalock);
7053 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7054 /* Asynchronous mailbox posting is not blocked, do nothing */
7055 spin_unlock_irq(&phba->hbalock);
7056 return;
7057 }
7058
7059 /* Outstanding synchronous mailbox command is guaranteed to be done,
7060 * successful or timeout, after timing-out the outstanding mailbox
7061 * command shall always be removed, so just unblock posting async
7062 * mailbox command and resume
7063 */
7064 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7065 spin_unlock_irq(&phba->hbalock);
7066
7067 /* wake up worker thread to post asynchronlous mailbox command */
7068 lpfc_worker_wake_up(phba);
7069}
7070
7071/**
James Smartda0436e2009-05-22 14:51:39 -04007072 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7073 * @phba: Pointer to HBA context object.
7074 * @mboxq: Pointer to mailbox object.
7075 *
7076 * The function posts a mailbox to the port. The mailbox is expected
7077 * to be comletely filled in and ready for the port to operate on it.
7078 * This routine executes a synchronous completion operation on the
7079 * mailbox by polling for its completion.
7080 *
7081 * The caller must not be holding any locks when calling this routine.
7082 *
7083 * Returns:
7084 * MBX_SUCCESS - mailbox posted successfully
7085 * Any of the MBX error values.
7086 **/
7087static int
7088lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7089{
7090 int rc = MBX_SUCCESS;
7091 unsigned long iflag;
7092 uint32_t db_ready;
7093 uint32_t mcqe_status;
7094 uint32_t mbx_cmnd;
7095 unsigned long timeout;
7096 struct lpfc_sli *psli = &phba->sli;
7097 struct lpfc_mqe *mb = &mboxq->u.mqe;
7098 struct lpfc_bmbx_create *mbox_rgn;
7099 struct dma_address *dma_address;
7100 struct lpfc_register bmbx_reg;
7101
7102 /*
7103 * Only one mailbox can be active to the bootstrap mailbox region
7104 * at a time and there is no queueing provided.
7105 */
7106 spin_lock_irqsave(&phba->hbalock, iflag);
7107 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7108 spin_unlock_irqrestore(&phba->hbalock, iflag);
7109 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007110 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007111 "cannot issue Data: x%x x%x\n",
7112 mboxq->vport ? mboxq->vport->vpi : 0,
7113 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007114 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7115 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007116 psli->sli_flag, MBX_POLL);
7117 return MBXERR_ERROR;
7118 }
7119 /* The server grabs the token and owns it until release */
7120 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7121 phba->sli.mbox_active = mboxq;
7122 spin_unlock_irqrestore(&phba->hbalock, iflag);
7123
7124 /*
7125 * Initialize the bootstrap memory region to avoid stale data areas
7126 * in the mailbox post. Then copy the caller's mailbox contents to
7127 * the bmbx mailbox region.
7128 */
7129 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7130 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7131 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7132 sizeof(struct lpfc_mqe));
7133
7134 /* Post the high mailbox dma address to the port and wait for ready. */
7135 dma_address = &phba->sli4_hba.bmbx.dma_address;
7136 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7137
James Smarta183a152011-10-10 21:32:43 -04007138 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007139 * 1000) + jiffies;
7140 do {
7141 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7142 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7143 if (!db_ready)
7144 msleep(2);
7145
7146 if (time_after(jiffies, timeout)) {
7147 rc = MBXERR_ERROR;
7148 goto exit;
7149 }
7150 } while (!db_ready);
7151
7152 /* Post the low mailbox dma address to the port. */
7153 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04007154 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007155 * 1000) + jiffies;
7156 do {
7157 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7158 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7159 if (!db_ready)
7160 msleep(2);
7161
7162 if (time_after(jiffies, timeout)) {
7163 rc = MBXERR_ERROR;
7164 goto exit;
7165 }
7166 } while (!db_ready);
7167
7168 /*
7169 * Read the CQ to ensure the mailbox has completed.
7170 * If so, update the mailbox status so that the upper layers
7171 * can complete the request normally.
7172 */
7173 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7174 sizeof(struct lpfc_mqe));
7175 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7176 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7177 sizeof(struct lpfc_mcqe));
7178 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007179 /*
7180 * When the CQE status indicates a failure and the mailbox status
7181 * indicates success then copy the CQE status into the mailbox status
7182 * (and prefix it with x4000).
7183 */
James Smartda0436e2009-05-22 14:51:39 -04007184 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007185 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7186 bf_set(lpfc_mqe_status, mb,
7187 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007188 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007189 } else
7190 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007191
7192 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007193 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007194 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7195 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007196 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7197 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7198 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007199 bf_get(lpfc_mqe_status, mb),
7200 mb->un.mb_words[0], mb->un.mb_words[1],
7201 mb->un.mb_words[2], mb->un.mb_words[3],
7202 mb->un.mb_words[4], mb->un.mb_words[5],
7203 mb->un.mb_words[6], mb->un.mb_words[7],
7204 mb->un.mb_words[8], mb->un.mb_words[9],
7205 mb->un.mb_words[10], mb->un.mb_words[11],
7206 mb->un.mb_words[12], mboxq->mcqe.word0,
7207 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7208 mboxq->mcqe.trailer);
7209exit:
7210 /* We are holding the token, no needed for lock when release */
7211 spin_lock_irqsave(&phba->hbalock, iflag);
7212 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7213 phba->sli.mbox_active = NULL;
7214 spin_unlock_irqrestore(&phba->hbalock, iflag);
7215 return rc;
7216}
7217
7218/**
7219 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7220 * @phba: Pointer to HBA context object.
7221 * @pmbox: Pointer to mailbox object.
7222 * @flag: Flag indicating how the mailbox need to be processed.
7223 *
7224 * This function is called by discovery code and HBA management code to submit
7225 * a mailbox command to firmware with SLI-4 interface spec.
7226 *
7227 * Return codes the caller owns the mailbox command after the return of the
7228 * function.
7229 **/
7230static int
7231lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7232 uint32_t flag)
7233{
7234 struct lpfc_sli *psli = &phba->sli;
7235 unsigned long iflags;
7236 int rc;
7237
James Smartb76f2dc2011-07-22 18:37:42 -04007238 /* dump from issue mailbox command if setup */
7239 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7240
James Smart8fa38512009-07-19 10:01:03 -04007241 rc = lpfc_mbox_dev_check(phba);
7242 if (unlikely(rc)) {
7243 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007244 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007245 "cannot issue Data: x%x x%x\n",
7246 mboxq->vport ? mboxq->vport->vpi : 0,
7247 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007248 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7249 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007250 psli->sli_flag, flag);
7251 goto out_not_finished;
7252 }
7253
James Smartda0436e2009-05-22 14:51:39 -04007254 /* Detect polling mode and jump to a handler */
7255 if (!phba->sli4_hba.intr_enable) {
7256 if (flag == MBX_POLL)
7257 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7258 else
7259 rc = -EIO;
7260 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007261 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007262 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007263 "(x%x/x%x) failure: "
7264 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7265 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007266 mboxq->vport ? mboxq->vport->vpi : 0,
7267 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007268 lpfc_sli_config_mbox_subsys_get(phba,
7269 mboxq),
7270 lpfc_sli_config_mbox_opcode_get(phba,
7271 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007272 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7273 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7274 bf_get(lpfc_mcqe_ext_status,
7275 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007276 psli->sli_flag, flag);
7277 return rc;
7278 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007279 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7280 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007281 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007282 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007283 mboxq->vport ? mboxq->vport->vpi : 0,
7284 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007285 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7286 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007287 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007288 /* Try to block the asynchronous mailbox posting */
7289 rc = lpfc_sli4_async_mbox_block(phba);
7290 if (!rc) {
7291 /* Successfully blocked, now issue sync mbox cmd */
7292 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7293 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007294 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007295 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007296 "(%d):2597 Sync Mailbox command "
7297 "x%x (x%x/x%x) failure: "
7298 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7299 "Data: x%x x%x\n,",
7300 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007301 mboxq->u.mb.mbxCommand,
7302 lpfc_sli_config_mbox_subsys_get(phba,
7303 mboxq),
7304 lpfc_sli_config_mbox_opcode_get(phba,
7305 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007306 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7307 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7308 bf_get(lpfc_mcqe_ext_status,
7309 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007310 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007311 /* Unblock the async mailbox posting afterward */
7312 lpfc_sli4_async_mbox_unblock(phba);
7313 }
7314 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007315 }
7316
7317 /* Now, interrupt mode asynchrous mailbox command */
7318 rc = lpfc_mbox_cmd_check(phba, mboxq);
7319 if (rc) {
7320 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007321 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007322 "cannot issue Data: x%x x%x\n",
7323 mboxq->vport ? mboxq->vport->vpi : 0,
7324 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007325 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7326 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007327 psli->sli_flag, flag);
7328 goto out_not_finished;
7329 }
James Smartda0436e2009-05-22 14:51:39 -04007330
7331 /* Put the mailbox command to the driver internal FIFO */
7332 psli->slistat.mbox_busy++;
7333 spin_lock_irqsave(&phba->hbalock, iflags);
7334 lpfc_mbox_put(phba, mboxq);
7335 spin_unlock_irqrestore(&phba->hbalock, iflags);
7336 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7337 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007338 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007339 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7340 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007341 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7342 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007343 phba->pport->port_state,
7344 psli->sli_flag, MBX_NOWAIT);
7345 /* Wake up worker thread to transport mailbox command from head */
7346 lpfc_worker_wake_up(phba);
7347
7348 return MBX_BUSY;
7349
7350out_not_finished:
7351 return MBX_NOT_FINISHED;
7352}
7353
7354/**
7355 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7356 * @phba: Pointer to HBA context object.
7357 *
7358 * This function is called by worker thread to send a mailbox command to
7359 * SLI4 HBA firmware.
7360 *
7361 **/
7362int
7363lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7364{
7365 struct lpfc_sli *psli = &phba->sli;
7366 LPFC_MBOXQ_t *mboxq;
7367 int rc = MBX_SUCCESS;
7368 unsigned long iflags;
7369 struct lpfc_mqe *mqe;
7370 uint32_t mbx_cmnd;
7371
7372 /* Check interrupt mode before post async mailbox command */
7373 if (unlikely(!phba->sli4_hba.intr_enable))
7374 return MBX_NOT_FINISHED;
7375
7376 /* Check for mailbox command service token */
7377 spin_lock_irqsave(&phba->hbalock, iflags);
7378 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7379 spin_unlock_irqrestore(&phba->hbalock, iflags);
7380 return MBX_NOT_FINISHED;
7381 }
7382 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7383 spin_unlock_irqrestore(&phba->hbalock, iflags);
7384 return MBX_NOT_FINISHED;
7385 }
7386 if (unlikely(phba->sli.mbox_active)) {
7387 spin_unlock_irqrestore(&phba->hbalock, iflags);
7388 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7389 "0384 There is pending active mailbox cmd\n");
7390 return MBX_NOT_FINISHED;
7391 }
7392 /* Take the mailbox command service token */
7393 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7394
7395 /* Get the next mailbox command from head of queue */
7396 mboxq = lpfc_mbox_get(phba);
7397
7398 /* If no more mailbox command waiting for post, we're done */
7399 if (!mboxq) {
7400 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7401 spin_unlock_irqrestore(&phba->hbalock, iflags);
7402 return MBX_SUCCESS;
7403 }
7404 phba->sli.mbox_active = mboxq;
7405 spin_unlock_irqrestore(&phba->hbalock, iflags);
7406
7407 /* Check device readiness for posting mailbox command */
7408 rc = lpfc_mbox_dev_check(phba);
7409 if (unlikely(rc))
7410 /* Driver clean routine will clean up pending mailbox */
7411 goto out_not_finished;
7412
7413 /* Prepare the mbox command to be posted */
7414 mqe = &mboxq->u.mqe;
7415 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7416
7417 /* Start timer for the mbox_tmo and log some mailbox post messages */
7418 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007419 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007420
7421 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007422 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007423 "x%x x%x\n",
7424 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007425 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7426 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007427 phba->pport->port_state, psli->sli_flag);
7428
7429 if (mbx_cmnd != MBX_HEARTBEAT) {
7430 if (mboxq->vport) {
7431 lpfc_debugfs_disc_trc(mboxq->vport,
7432 LPFC_DISC_TRC_MBOX_VPORT,
7433 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7434 mbx_cmnd, mqe->un.mb_words[0],
7435 mqe->un.mb_words[1]);
7436 } else {
7437 lpfc_debugfs_disc_trc(phba->pport,
7438 LPFC_DISC_TRC_MBOX,
7439 "MBOX Send: cmd:x%x mb:x%x x%x",
7440 mbx_cmnd, mqe->un.mb_words[0],
7441 mqe->un.mb_words[1]);
7442 }
7443 }
7444 psli->slistat.mbox_cmd++;
7445
7446 /* Post the mailbox command to the port */
7447 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7448 if (rc != MBX_SUCCESS) {
7449 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007450 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007451 "cannot issue Data: x%x x%x\n",
7452 mboxq->vport ? mboxq->vport->vpi : 0,
7453 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007454 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7455 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007456 psli->sli_flag, MBX_NOWAIT);
7457 goto out_not_finished;
7458 }
7459
7460 return rc;
7461
7462out_not_finished:
7463 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007464 if (phba->sli.mbox_active) {
7465 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7466 __lpfc_mbox_cmpl_put(phba, mboxq);
7467 /* Release the token */
7468 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7469 phba->sli.mbox_active = NULL;
7470 }
James Smartda0436e2009-05-22 14:51:39 -04007471 spin_unlock_irqrestore(&phba->hbalock, iflags);
7472
7473 return MBX_NOT_FINISHED;
7474}
7475
7476/**
7477 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7478 * @phba: Pointer to HBA context object.
7479 * @pmbox: Pointer to mailbox object.
7480 * @flag: Flag indicating how the mailbox need to be processed.
7481 *
7482 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7483 * the API jump table function pointer from the lpfc_hba struct.
7484 *
7485 * Return codes the caller owns the mailbox command after the return of the
7486 * function.
7487 **/
7488int
7489lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7490{
7491 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7492}
7493
7494/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007495 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007496 * @phba: The hba struct for which this call is being executed.
7497 * @dev_grp: The HBA PCI-Device group number.
7498 *
7499 * This routine sets up the mbox interface API function jump table in @phba
7500 * struct.
7501 * Returns: 0 - success, -ENODEV - failure.
7502 **/
7503int
7504lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7505{
7506
7507 switch (dev_grp) {
7508 case LPFC_PCI_DEV_LP:
7509 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7510 phba->lpfc_sli_handle_slow_ring_event =
7511 lpfc_sli_handle_slow_ring_event_s3;
7512 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7513 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7514 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7515 break;
7516 case LPFC_PCI_DEV_OC:
7517 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7518 phba->lpfc_sli_handle_slow_ring_event =
7519 lpfc_sli_handle_slow_ring_event_s4;
7520 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7521 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7522 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7523 break;
7524 default:
7525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7526 "1420 Invalid HBA PCI-device group: 0x%x\n",
7527 dev_grp);
7528 return -ENODEV;
7529 break;
7530 }
7531 return 0;
7532}
7533
7534/**
James Smart3621a712009-04-06 18:47:14 -04007535 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007536 * @phba: Pointer to HBA context object.
7537 * @pring: Pointer to driver SLI ring object.
7538 * @piocb: Pointer to address of newly added command iocb.
7539 *
7540 * This function is called with hbalock held to add a command
7541 * iocb to the txq when SLI layer cannot submit the command iocb
7542 * to the ring.
7543 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007544void
James Smart92d7f7b2007-06-17 19:56:38 -05007545__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007546 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007547{
7548 /* Insert the caller's iocb in the txq tail for later processing. */
7549 list_add_tail(&piocb->list, &pring->txq);
7550 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007551}
7552
James Smarte59058c2008-08-24 21:49:00 -04007553/**
James Smart3621a712009-04-06 18:47:14 -04007554 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007555 * @phba: Pointer to HBA context object.
7556 * @pring: Pointer to driver SLI ring object.
7557 * @piocb: Pointer to address of newly added command iocb.
7558 *
7559 * This function is called with hbalock held before a new
7560 * iocb is submitted to the firmware. This function checks
7561 * txq to flush the iocbs in txq to Firmware before
7562 * submitting new iocbs to the Firmware.
7563 * If there are iocbs in the txq which need to be submitted
7564 * to firmware, lpfc_sli_next_iocb returns the first element
7565 * of the txq after dequeuing it from txq.
7566 * If there is no iocb in the txq then the function will return
7567 * *piocb and *piocb is set to NULL. Caller needs to check
7568 * *piocb to find if there are more commands in the txq.
7569 **/
dea31012005-04-17 16:05:31 -05007570static struct lpfc_iocbq *
7571lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007572 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007573{
7574 struct lpfc_iocbq * nextiocb;
7575
7576 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7577 if (!nextiocb) {
7578 nextiocb = *piocb;
7579 *piocb = NULL;
7580 }
7581
7582 return nextiocb;
7583}
7584
James Smarte59058c2008-08-24 21:49:00 -04007585/**
James Smart3772a992009-05-22 14:50:54 -04007586 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007587 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007588 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007589 * @piocb: Pointer to command iocb.
7590 * @flag: Flag indicating if this command can be put into txq.
7591 *
James Smart3772a992009-05-22 14:50:54 -04007592 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7593 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7594 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7595 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7596 * this function allows only iocbs for posting buffers. This function finds
7597 * next available slot in the command ring and posts the command to the
7598 * available slot and writes the port attention register to request HBA start
7599 * processing new iocb. If there is no slot available in the ring and
7600 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7601 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007602 *
James Smart3772a992009-05-22 14:50:54 -04007603 * This function is called with hbalock held. The function will return success
7604 * after it successfully submit the iocb to firmware or after adding to the
7605 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007606 **/
James Smart98c9ea52007-10-27 13:37:33 -04007607static int
James Smart3772a992009-05-22 14:50:54 -04007608__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007609 struct lpfc_iocbq *piocb, uint32_t flag)
7610{
7611 struct lpfc_iocbq *nextiocb;
7612 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007613 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007614
James Smart92d7f7b2007-06-17 19:56:38 -05007615 if (piocb->iocb_cmpl && (!piocb->vport) &&
7616 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7617 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7618 lpfc_printf_log(phba, KERN_ERR,
7619 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007620 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007621 piocb->iocb.ulpCommand);
7622 dump_stack();
7623 return IOCB_ERROR;
7624 }
7625
7626
Linas Vepstas8d63f372007-02-14 14:28:36 -06007627 /* If the PCI channel is in offline state, do not post iocbs. */
7628 if (unlikely(pci_channel_offline(phba->pcidev)))
7629 return IOCB_ERROR;
7630
James Smarta257bf92009-04-06 18:48:10 -04007631 /* If HBA has a deferred error attention, fail the iocb. */
7632 if (unlikely(phba->hba_flag & DEFER_ERATT))
7633 return IOCB_ERROR;
7634
dea31012005-04-17 16:05:31 -05007635 /*
7636 * We should never get an IOCB if we are in a < LINK_DOWN state
7637 */
James Smart2e0fef82007-06-17 19:56:36 -05007638 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007639 return IOCB_ERROR;
7640
7641 /*
7642 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007643 * outstanding event.
dea31012005-04-17 16:05:31 -05007644 */
James Smart0b727fe2007-10-27 13:37:25 -04007645 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007646 goto iocb_busy;
7647
James Smart2e0fef82007-06-17 19:56:36 -05007648 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007649 /*
James Smart2680eea2007-04-25 09:52:55 -04007650 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007651 * can be issued if the link is not up.
7652 */
7653 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007654 case CMD_GEN_REQUEST64_CR:
7655 case CMD_GEN_REQUEST64_CX:
7656 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7657 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007658 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007659 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7660 MENLO_TRANSPORT_TYPE))
7661
7662 goto iocb_busy;
7663 break;
dea31012005-04-17 16:05:31 -05007664 case CMD_QUE_RING_BUF_CN:
7665 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007666 /*
7667 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7668 * completion, iocb_cmpl MUST be 0.
7669 */
7670 if (piocb->iocb_cmpl)
7671 piocb->iocb_cmpl = NULL;
7672 /*FALLTHROUGH*/
7673 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007674 case CMD_CLOSE_XRI_CN:
7675 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007676 break;
7677 default:
7678 goto iocb_busy;
7679 }
7680
7681 /*
7682 * For FCP commands, we must be in a state where we can process link
7683 * attention events.
7684 */
7685 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007686 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007687 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007688 }
dea31012005-04-17 16:05:31 -05007689
dea31012005-04-17 16:05:31 -05007690 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7691 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7692 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7693
7694 if (iocb)
7695 lpfc_sli_update_ring(phba, pring);
7696 else
7697 lpfc_sli_update_full_ring(phba, pring);
7698
7699 if (!piocb)
7700 return IOCB_SUCCESS;
7701
7702 goto out_busy;
7703
7704 iocb_busy:
7705 pring->stats.iocb_cmd_delay++;
7706
7707 out_busy:
7708
7709 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007710 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007711 return IOCB_SUCCESS;
7712 }
7713
7714 return IOCB_BUSY;
7715}
7716
James Smart3772a992009-05-22 14:50:54 -04007717/**
James Smart4f774512009-05-22 14:52:35 -04007718 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7719 * @phba: Pointer to HBA context object.
7720 * @piocb: Pointer to command iocb.
7721 * @sglq: Pointer to the scatter gather queue object.
7722 *
7723 * This routine converts the bpl or bde that is in the IOCB
7724 * to a sgl list for the sli4 hardware. The physical address
7725 * of the bpl/bde is converted back to a virtual address.
7726 * If the IOCB contains a BPL then the list of BDE's is
7727 * converted to sli4_sge's. If the IOCB contains a single
7728 * BDE then it is converted to a single sli_sge.
7729 * The IOCB is still in cpu endianess so the contents of
7730 * the bpl can be used without byte swapping.
7731 *
7732 * Returns valid XRI = Success, NO_XRI = Failure.
7733**/
7734static uint16_t
7735lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7736 struct lpfc_sglq *sglq)
7737{
7738 uint16_t xritag = NO_XRI;
7739 struct ulp_bde64 *bpl = NULL;
7740 struct ulp_bde64 bde;
7741 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007742 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007743 IOCB_t *icmd;
7744 int numBdes = 0;
7745 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007746 uint32_t offset = 0; /* accumulated offset in the sg request list */
7747 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007748
7749 if (!piocbq || !sglq)
7750 return xritag;
7751
7752 sgl = (struct sli4_sge *)sglq->sgl;
7753 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007754 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7755 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007756 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7757 numBdes = icmd->un.genreq64.bdl.bdeSize /
7758 sizeof(struct ulp_bde64);
7759 /* The addrHigh and addrLow fields within the IOCB
7760 * have not been byteswapped yet so there is no
7761 * need to swap them back.
7762 */
James Smart1b511972011-12-13 13:23:09 -05007763 if (piocbq->context3)
7764 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7765 else
7766 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007767
James Smart1b511972011-12-13 13:23:09 -05007768 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007769 if (!bpl)
7770 return xritag;
7771
7772 for (i = 0; i < numBdes; i++) {
7773 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007774 sgl->addr_hi = bpl->addrHigh;
7775 sgl->addr_lo = bpl->addrLow;
7776
James Smart05580562011-05-24 11:40:48 -04007777 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007778 if ((i+1) == numBdes)
7779 bf_set(lpfc_sli4_sge_last, sgl, 1);
7780 else
7781 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007782 /* swap the size field back to the cpu so we
7783 * can assign it to the sgl.
7784 */
7785 bde.tus.w = le32_to_cpu(bpl->tus.w);
7786 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007787 /* The offsets in the sgl need to be accumulated
7788 * separately for the request and reply lists.
7789 * The request is always first, the reply follows.
7790 */
7791 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7792 /* add up the reply sg entries */
7793 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7794 inbound++;
7795 /* first inbound? reset the offset */
7796 if (inbound == 1)
7797 offset = 0;
7798 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007799 bf_set(lpfc_sli4_sge_type, sgl,
7800 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007801 offset += bde.tus.f.bdeSize;
7802 }
James Smart546fc852011-03-11 16:06:29 -05007803 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007804 bpl++;
7805 sgl++;
7806 }
7807 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7808 /* The addrHigh and addrLow fields of the BDE have not
7809 * been byteswapped yet so they need to be swapped
7810 * before putting them in the sgl.
7811 */
7812 sgl->addr_hi =
7813 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7814 sgl->addr_lo =
7815 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007816 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007817 bf_set(lpfc_sli4_sge_last, sgl, 1);
7818 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007819 sgl->sge_len =
7820 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007821 }
7822 return sglq->sli4_xritag;
7823}
7824
7825/**
7826 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7827 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007828 *
James Smarta93ff372010-10-22 11:06:08 -04007829 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007830 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7831 * held.
James Smart4f774512009-05-22 14:52:35 -04007832 *
7833 * Return: index into SLI4 fast-path FCP queue index.
7834 **/
James Smart2a76a282012-08-03 12:35:54 -04007835static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007836lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007837{
James Smart2a76a282012-08-03 12:35:54 -04007838 int i;
James Smart4f774512009-05-22 14:52:35 -04007839
James Smart49aa1432012-08-03 12:36:42 -04007840 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
7841 i = smp_processor_id();
7842 else
7843 i = atomic_add_return(1, &phba->fcp_qidx);
7844
James Smart67d12732012-08-03 12:36:13 -04007845 i = (i % phba->cfg_fcp_io_channel);
James Smart2a76a282012-08-03 12:35:54 -04007846 return i;
James Smart4f774512009-05-22 14:52:35 -04007847}
7848
7849/**
7850 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7851 * @phba: Pointer to HBA context object.
7852 * @piocb: Pointer to command iocb.
7853 * @wqe: Pointer to the work queue entry.
7854 *
7855 * This routine converts the iocb command to its Work Queue Entry
7856 * equivalent. The wqe pointer should not have any fields set when
7857 * this routine is called because it will memcpy over them.
7858 * This routine does not set the CQ_ID or the WQEC bits in the
7859 * wqe.
7860 *
7861 * Returns: 0 = Success, IOCB_ERROR = Failure.
7862 **/
7863static int
7864lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7865 union lpfc_wqe *wqe)
7866{
James Smart5ffc2662009-11-18 15:39:44 -05007867 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007868 uint8_t ct = 0;
7869 uint32_t fip;
7870 uint32_t abort_tag;
7871 uint8_t command_type = ELS_COMMAND_NON_FIP;
7872 uint8_t cmnd;
7873 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007874 uint16_t abrt_iotag;
7875 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007876 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007877 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007878 int numBdes, i;
7879 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007880 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007881 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007882 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007883
James Smart45ed1192009-10-02 15:17:02 -04007884 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007885 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007886 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007887 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007888 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007889 command_type = ELS_COMMAND_FIP;
7890 else
7891 command_type = ELS_COMMAND_NON_FIP;
7892
James Smart4f774512009-05-22 14:52:35 -04007893 /* Some of the fields are in the right position already */
7894 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7895 abort_tag = (uint32_t) iocbq->iotag;
7896 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007897 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007898 /* words0-2 bpl convert bde */
7899 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007900 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7901 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007902 bpl = (struct ulp_bde64 *)
7903 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7904 if (!bpl)
7905 return IOCB_ERROR;
7906
7907 /* Should already be byte swapped. */
7908 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7909 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7910 /* swap the size field back to the cpu so we
7911 * can assign it to the sgl.
7912 */
7913 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007914 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7915 total_len = 0;
7916 for (i = 0; i < numBdes; i++) {
7917 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7918 total_len += bde.tus.f.bdeSize;
7919 }
James Smart4f774512009-05-22 14:52:35 -04007920 } else
James Smart5ffc2662009-11-18 15:39:44 -05007921 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007922
7923 iocbq->iocb.ulpIoTag = iocbq->iotag;
7924 cmnd = iocbq->iocb.ulpCommand;
7925
7926 switch (iocbq->iocb.ulpCommand) {
7927 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04007928 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7929 ndlp = iocbq->context_un.ndlp;
7930 else
7931 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007932 if (!iocbq->iocb.ulpLe) {
7933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7934 "2007 Only Limited Edition cmd Format"
7935 " supported 0x%x\n",
7936 iocbq->iocb.ulpCommand);
7937 return IOCB_ERROR;
7938 }
James Smartff78d8f2011-12-13 13:21:35 -05007939
James Smart5ffc2662009-11-18 15:39:44 -05007940 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007941 /* Els_reguest64 has a TMO */
7942 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7943 iocbq->iocb.ulpTimeout);
7944 /* Need a VF for word 4 set the vf bit*/
7945 bf_set(els_req64_vf, &wqe->els_req, 0);
7946 /* And a VFID for word 12 */
7947 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007948 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007949 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7950 iocbq->iocb.ulpContext);
7951 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7952 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007953 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007954 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007955 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7956 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007957 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7958 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007959 if_type = bf_get(lpfc_sli_intf_if_type,
7960 &phba->sli4_hba.sli_intf);
7961 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007962 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007963 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007964 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05007965 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05007966 *pcmd == ELS_CMD_PLOGI)) {
7967 bf_set(els_req64_sp, &wqe->els_req, 1);
7968 bf_set(els_req64_sid, &wqe->els_req,
7969 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04007970 if ((*pcmd == ELS_CMD_FLOGI) &&
7971 !(phba->fc_topology ==
7972 LPFC_TOPOLOGY_LOOP))
7973 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05007974 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7975 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04007976 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007977 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007978 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7979 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7980 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7981 }
James Smartc8685952009-11-18 15:39:16 -05007982 }
James Smart6d368e52011-05-24 11:44:12 -04007983 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7984 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007985 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7986 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7987 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7988 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7989 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7990 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007991 break;
James Smart5ffc2662009-11-18 15:39:44 -05007992 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007993 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7994 iocbq->iocb.un.ulpWord[3]);
7995 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007996 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007997 /* The entire sequence is transmitted for this IOCB */
7998 xmit_len = total_len;
7999 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008000 if (phba->link_flag & LS_LOOPBACK_MODE)
8001 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008002 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008003 /* word3 iocb=io_tag32 wqe=reserved */
8004 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008005 /* word4 relative_offset memcpy */
8006 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008007 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8008 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8009 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8010 LPFC_WQE_IOD_WRITE);
8011 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8012 LPFC_WQE_LENLOC_WORD12);
8013 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008014 wqe->xmit_sequence.xmit_len = xmit_len;
8015 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008016 break;
James Smart4f774512009-05-22 14:52:35 -04008017 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008018 /* word3 iocb=iotag32 wqe=seq_payload_len */
8019 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008020 /* word4 iocb=rsvd wqe=rsvd */
8021 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8022 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008023 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008024 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008025 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8026 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8027 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8028 LPFC_WQE_LENLOC_WORD3);
8029 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008030 break;
James Smart4f774512009-05-22 14:52:35 -04008031 case CMD_FCP_IWRITE64_CR:
8032 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008033 /* word3 iocb=iotag wqe=payload_offset_len */
8034 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8035 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05008036 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04008037 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8038 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8039 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8040 iocbq->iocb.ulpFCP2Rcvy);
8041 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8042 /* Always open the exchange */
8043 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008044 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8045 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8046 LPFC_WQE_LENLOC_WORD4);
8047 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8048 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008049 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8050 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8051 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8052 }
8053 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008054 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008055 case CMD_FCP_IREAD64_CR:
8056 /* word3 iocb=iotag wqe=payload_offset_len */
8057 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8058 wqe->fcp_iread.payload_offset_len =
8059 xmit_len + sizeof(struct fcp_rsp);
8060 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8061 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8062 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8063 iocbq->iocb.ulpFCP2Rcvy);
8064 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008065 /* Always open the exchange */
8066 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008067 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8068 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8069 LPFC_WQE_LENLOC_WORD4);
8070 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8071 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008072 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8073 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8074 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8075 }
8076 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008077 break;
James Smartf1126682009-06-10 17:22:44 -04008078 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008079 /* word3 iocb=IO_TAG wqe=reserved */
8080 wqe->fcp_icmd.rsrvd3 = 0;
8081 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008082 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008083 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8084 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8085 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8086 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8087 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8088 LPFC_WQE_LENLOC_NONE);
8089 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008090 break;
James Smart4f774512009-05-22 14:52:35 -04008091 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008092 /* For this command calculate the xmit length of the
8093 * request bde.
8094 */
8095 xmit_len = 0;
8096 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8097 sizeof(struct ulp_bde64);
8098 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008099 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008100 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8101 break;
James Smart63e801c2010-11-20 23:14:19 -05008102 xmit_len += bde.tus.f.bdeSize;
8103 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008104 /* word3 iocb=IO_TAG wqe=request_payload_len */
8105 wqe->gen_req.request_payload_len = xmit_len;
8106 /* word4 iocb=parameter wqe=relative_offset memcpy */
8107 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008108 /* word6 context tag copied in memcpy */
8109 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8110 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8112 "2015 Invalid CT %x command 0x%x\n",
8113 ct, iocbq->iocb.ulpCommand);
8114 return IOCB_ERROR;
8115 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008116 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8117 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8118 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8119 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8120 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8121 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8122 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8123 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008124 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008125 break;
James Smart4f774512009-05-22 14:52:35 -04008126 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008127 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008128 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008129 /* word3 iocb=iotag32 wqe=response_payload_len */
8130 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008131 /* word4 */
8132 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008133 /* word5 iocb=rsvd wge=did */
8134 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008135 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8136
8137 if_type = bf_get(lpfc_sli_intf_if_type,
8138 &phba->sli4_hba.sli_intf);
8139 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8140 if (iocbq->vport->fc_flag & FC_PT2PT) {
8141 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8142 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8143 iocbq->vport->fc_myDID);
8144 if (iocbq->vport->fc_myDID == Fabric_DID) {
8145 bf_set(wqe_els_did,
8146 &wqe->xmit_els_rsp.wqe_dest, 0);
8147 }
8148 }
8149 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008150 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8151 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8152 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8153 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008154 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008155 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008156 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008157 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008158 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8159 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8160 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8161 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8162 LPFC_WQE_LENLOC_WORD3);
8163 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008164 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8165 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008166 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8167 iocbq->context2)->virt);
8168 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008169 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8170 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008171 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008172 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8173 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008174 phba->vpi_ids[phba->pport->vpi]);
8175 }
James Smart4f774512009-05-22 14:52:35 -04008176 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008177 break;
James Smart4f774512009-05-22 14:52:35 -04008178 case CMD_CLOSE_XRI_CN:
8179 case CMD_ABORT_XRI_CN:
8180 case CMD_ABORT_XRI_CX:
8181 /* words 0-2 memcpy should be 0 rserved */
8182 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008183 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8184 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8185 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8186 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8187 } else
8188 fip = 0;
8189
8190 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008191 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008192 * The link is down, or the command was ELS_FIP
8193 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008194 * on the wire.
8195 */
8196 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8197 else
8198 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8199 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008200 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8201 wqe->abort_cmd.rsrvd5 = 0;
8202 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008203 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8204 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008205 /*
8206 * The abort handler will send us CMD_ABORT_XRI_CN or
8207 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8208 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008209 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8210 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8211 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8212 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008213 cmnd = CMD_ABORT_XRI_CX;
8214 command_type = OTHER_COMMAND;
8215 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008216 break;
James Smart6669f9b2009-10-02 15:16:45 -04008217 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008218 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008219 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008220 * we re-construct this WQE here based on information in
8221 * iocbq from scratch.
8222 */
8223 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008224 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008225 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008226 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8227 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008228 LPFC_ABTS_UNSOL_INT) {
8229 /* ABTS sent by initiator to CT exchange, the
8230 * RX_ID field will be filled with the newly
8231 * allocated responder XRI.
8232 */
8233 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8234 iocbq->sli4_xritag);
8235 } else {
8236 /* ABTS sent by responder to CT exchange, the
8237 * RX_ID field will be filled with the responder
8238 * RX_ID from ABTS.
8239 */
8240 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008241 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008242 }
James Smart6669f9b2009-10-02 15:16:45 -04008243 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8244 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008245
8246 /* Use CT=VPI */
8247 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8248 ndlp->nlp_DID);
8249 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8250 iocbq->iocb.ulpContext);
8251 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008252 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008253 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008254 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8255 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8256 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008257 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8258 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008259 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8260 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8261 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8262 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8263 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8264 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8265 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8266 }
8267
James Smart7851fe22011-07-22 18:36:52 -04008268 break;
James Smart4f774512009-05-22 14:52:35 -04008269 case CMD_XRI_ABORTED_CX:
8270 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008271 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8272 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8273 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8274 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8275 default:
8276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8277 "2014 Invalid command 0x%x\n",
8278 iocbq->iocb.ulpCommand);
8279 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008280 break;
James Smart4f774512009-05-22 14:52:35 -04008281 }
James Smart6d368e52011-05-24 11:44:12 -04008282
James Smartf0d9bcc2010-10-22 11:07:09 -04008283 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8284 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8285 wqe->generic.wqe_com.abort_tag = abort_tag;
8286 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8287 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8288 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8289 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008290 return 0;
8291}
8292
8293/**
8294 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8295 * @phba: Pointer to HBA context object.
8296 * @ring_number: SLI ring number to issue iocb on.
8297 * @piocb: Pointer to command iocb.
8298 * @flag: Flag indicating if this command can be put into txq.
8299 *
8300 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8301 * an iocb command to an HBA with SLI-4 interface spec.
8302 *
8303 * This function is called with hbalock held. The function will return success
8304 * after it successfully submit the iocb to firmware or after adding to the
8305 * txq.
8306 **/
8307static int
8308__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8309 struct lpfc_iocbq *piocb, uint32_t flag)
8310{
8311 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008312 union lpfc_wqe wqe;
8313 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008314
8315 if (piocb->sli4_xritag == NO_XRI) {
8316 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008317 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008318 sglq = NULL;
8319 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008320 if (pring->txq_cnt) {
8321 if (!(flag & SLI_IOCB_RET_IOCB)) {
8322 __lpfc_sli_ringtx_put(phba,
8323 pring, piocb);
8324 return IOCB_SUCCESS;
8325 } else {
8326 return IOCB_BUSY;
8327 }
8328 } else {
James Smart6d368e52011-05-24 11:44:12 -04008329 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008330 if (!sglq) {
8331 if (!(flag & SLI_IOCB_RET_IOCB)) {
8332 __lpfc_sli_ringtx_put(phba,
8333 pring,
8334 piocb);
8335 return IOCB_SUCCESS;
8336 } else
8337 return IOCB_BUSY;
8338 }
8339 }
James Smart4f774512009-05-22 14:52:35 -04008340 }
8341 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008342 /* These IO's already have an XRI and a mapped sgl. */
8343 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008344 } else {
James Smart6d368e52011-05-24 11:44:12 -04008345 /*
8346 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008347 * sglq is on the active list
8348 */
8349 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8350 if (!sglq)
8351 return IOCB_ERROR;
8352 }
8353
8354 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008355 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008356 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008357 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008358 return IOCB_ERROR;
8359 }
8360
8361 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8362 return IOCB_ERROR;
8363
James Smart341af102010-01-26 23:07:37 -05008364 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8365 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008366 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8367 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008368 return IOCB_ERROR;
8369 } else {
8370 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8371 return IOCB_ERROR;
8372 }
8373 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8374
8375 return 0;
8376}
8377
8378/**
James Smart3772a992009-05-22 14:50:54 -04008379 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8380 *
8381 * This routine wraps the actual lockless version for issusing IOCB function
8382 * pointer from the lpfc_hba struct.
8383 *
8384 * Return codes:
8385 * IOCB_ERROR - Error
8386 * IOCB_SUCCESS - Success
8387 * IOCB_BUSY - Busy
8388 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008389int
James Smart3772a992009-05-22 14:50:54 -04008390__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8391 struct lpfc_iocbq *piocb, uint32_t flag)
8392{
8393 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8394}
8395
8396/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008397 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008398 * @phba: The hba struct for which this call is being executed.
8399 * @dev_grp: The HBA PCI-Device group number.
8400 *
8401 * This routine sets up the SLI interface API function jump table in @phba
8402 * struct.
8403 * Returns: 0 - success, -ENODEV - failure.
8404 **/
8405int
8406lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8407{
8408
8409 switch (dev_grp) {
8410 case LPFC_PCI_DEV_LP:
8411 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8412 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8413 break;
James Smart4f774512009-05-22 14:52:35 -04008414 case LPFC_PCI_DEV_OC:
8415 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8416 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8417 break;
James Smart3772a992009-05-22 14:50:54 -04008418 default:
8419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8420 "1419 Invalid HBA PCI-device group: 0x%x\n",
8421 dev_grp);
8422 return -ENODEV;
8423 break;
8424 }
8425 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8426 return 0;
8427}
James Smart92d7f7b2007-06-17 19:56:38 -05008428
James Smarte59058c2008-08-24 21:49:00 -04008429/**
James Smart3621a712009-04-06 18:47:14 -04008430 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008431 * @phba: Pointer to HBA context object.
8432 * @pring: Pointer to driver SLI ring object.
8433 * @piocb: Pointer to command iocb.
8434 * @flag: Flag indicating if this command can be put into txq.
8435 *
8436 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8437 * function. This function gets the hbalock and calls
8438 * __lpfc_sli_issue_iocb function and will return the error returned
8439 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8440 * functions which do not hold hbalock.
8441 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008442int
James Smart3772a992009-05-22 14:50:54 -04008443lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008444 struct lpfc_iocbq *piocb, uint32_t flag)
8445{
James Smartba20c852012-08-03 12:36:52 -04008446 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008447 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008448 struct lpfc_queue *fpeq;
8449 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008450 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008451 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008452
James Smart7e56aa22012-08-03 12:35:34 -04008453 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008454 if (piocb->iocb_flag & LPFC_IO_FCP) {
8455 if (unlikely(!phba->sli4_hba.fcp_wq))
8456 return IOCB_ERROR;
8457 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8458 piocb->fcp_wqidx = idx;
8459 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
James Smartba20c852012-08-03 12:36:52 -04008460
8461 pring = &phba->sli.ring[ring_number];
8462 spin_lock_irqsave(&pring->ring_lock, iflags);
8463 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8464 flag);
8465 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8466
8467 if (lpfc_fcp_look_ahead) {
8468 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8469
8470 if (atomic_dec_and_test(&fcp_eq_hdl->
8471 fcp_eq_in_use)) {
8472
8473 /* Get associated EQ with this index */
8474 fpeq = phba->sli4_hba.hba_eq[idx];
8475
8476 /* Turn off interrupts from this EQ */
8477 lpfc_sli4_eq_clr_intr(fpeq);
8478
8479 /*
8480 * Process all the events on FCP EQ
8481 */
8482 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8483 lpfc_sli4_hba_handle_eqe(phba,
8484 eqe, idx);
8485 fpeq->EQ_processed++;
8486 }
8487
8488 /* Always clear and re-arm the EQ */
8489 lpfc_sli4_eq_release(fpeq,
8490 LPFC_QUEUE_REARM);
8491 }
8492 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8493 }
8494 } else {
8495 pring = &phba->sli.ring[ring_number];
8496 spin_lock_irqsave(&pring->ring_lock, iflags);
8497 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8498 flag);
8499 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8500
James Smart2a76a282012-08-03 12:35:54 -04008501 }
James Smart7e56aa22012-08-03 12:35:34 -04008502 } else {
8503 /* For now, SLI2/3 will still use hbalock */
8504 spin_lock_irqsave(&phba->hbalock, iflags);
8505 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8506 spin_unlock_irqrestore(&phba->hbalock, iflags);
8507 }
James Smart92d7f7b2007-06-17 19:56:38 -05008508 return rc;
8509}
8510
James Smarte59058c2008-08-24 21:49:00 -04008511/**
James Smart3621a712009-04-06 18:47:14 -04008512 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008513 * @phba: Pointer to HBA context object.
8514 *
8515 * This function is called while driver attaches with the
8516 * HBA to setup the extra ring. The extra ring is used
8517 * only when driver needs to support target mode functionality
8518 * or IP over FC functionalities.
8519 *
8520 * This function is called with no lock held.
8521 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008522static int
8523lpfc_extra_ring_setup( struct lpfc_hba *phba)
8524{
8525 struct lpfc_sli *psli;
8526 struct lpfc_sli_ring *pring;
8527
8528 psli = &phba->sli;
8529
8530 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008531
8532 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008533 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008534 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8535 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8536 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8537 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008538
James Smarta4bc3372006-12-02 13:34:16 -05008539 /* and give them to the extra ring */
8540 pring = &psli->ring[psli->extra_ring];
8541
James Smart7e56aa22012-08-03 12:35:34 -04008542 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8543 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8544 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8545 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008546
8547 /* Setup default profile for this ring */
8548 pring->iotag_max = 4096;
8549 pring->num_mask = 1;
8550 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008551 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8552 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008553 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8554 return 0;
8555}
8556
James Smartcb69f7d2011-12-13 13:21:57 -05008557/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8558 * @vport: pointer to virtual port object.
8559 * @ndlp: nodelist pointer for the impacted rport.
8560 *
8561 * The driver calls this routine in response to a XRI ABORT CQE
8562 * event from the port. In this event, the driver is required to
8563 * recover its login to the rport even though its login may be valid
8564 * from the driver's perspective. The failed ABTS notice from the
8565 * port indicates the rport is not responding.
8566 */
8567static void
8568lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8569 struct lpfc_nodelist *ndlp)
8570{
8571 struct Scsi_Host *shost;
8572 struct lpfc_hba *phba;
8573 unsigned long flags = 0;
8574
8575 shost = lpfc_shost_from_vport(vport);
8576 phba = vport->phba;
8577 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8578 lpfc_printf_log(phba, KERN_INFO,
8579 LOG_SLI, "3093 No rport recovery needed. "
8580 "rport in state 0x%x\n",
8581 ndlp->nlp_state);
8582 return;
8583 }
8584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8585 "3094 Start rport recovery on shost id 0x%x "
8586 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8587 "flags 0x%x\n",
8588 shost->host_no, ndlp->nlp_DID,
8589 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8590 ndlp->nlp_flag);
8591 /*
8592 * The rport is not responding. Don't attempt ADISC recovery.
8593 * Remove the FCP-2 flag to force a PLOGI.
8594 */
8595 spin_lock_irqsave(shost->host_lock, flags);
8596 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8597 spin_unlock_irqrestore(shost->host_lock, flags);
8598 lpfc_disc_state_machine(vport, ndlp, NULL,
8599 NLP_EVT_DEVICE_RECOVERY);
8600 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8601 spin_lock_irqsave(shost->host_lock, flags);
8602 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8603 spin_unlock_irqrestore(shost->host_lock, flags);
8604 lpfc_disc_start(vport);
8605}
8606
8607/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8608 * @phba: Pointer to HBA context object.
8609 * @iocbq: Pointer to iocb object.
8610 *
8611 * The async_event handler calls this routine when it receives
8612 * an ASYNC_STATUS_CN event from the port. The port generates
8613 * this event when an Abort Sequence request to an rport fails
8614 * twice in succession. The abort could be originated by the
8615 * driver or by the port. The ABTS could have been for an ELS
8616 * or FCP IO. The port only generates this event when an ABTS
8617 * fails to complete after one retry.
8618 */
8619static void
8620lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8621 struct lpfc_iocbq *iocbq)
8622{
8623 struct lpfc_nodelist *ndlp = NULL;
8624 uint16_t rpi = 0, vpi = 0;
8625 struct lpfc_vport *vport = NULL;
8626
8627 /* The rpi in the ulpContext is vport-sensitive. */
8628 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8629 rpi = iocbq->iocb.ulpContext;
8630
8631 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8632 "3092 Port generated ABTS async event "
8633 "on vpi %d rpi %d status 0x%x\n",
8634 vpi, rpi, iocbq->iocb.ulpStatus);
8635
8636 vport = lpfc_find_vport_by_vpid(phba, vpi);
8637 if (!vport)
8638 goto err_exit;
8639 ndlp = lpfc_findnode_rpi(vport, rpi);
8640 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8641 goto err_exit;
8642
8643 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8644 lpfc_sli_abts_recover_port(vport, ndlp);
8645 return;
8646
8647 err_exit:
8648 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8649 "3095 Event Context not found, no "
8650 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8651 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8652 vpi, rpi);
8653}
8654
8655/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8656 * @phba: pointer to HBA context object.
8657 * @ndlp: nodelist pointer for the impacted rport.
8658 * @axri: pointer to the wcqe containing the failed exchange.
8659 *
8660 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8661 * port. The port generates this event when an abort exchange request to an
8662 * rport fails twice in succession with no reply. The abort could be originated
8663 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8664 */
8665void
8666lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8667 struct lpfc_nodelist *ndlp,
8668 struct sli4_wcqe_xri_aborted *axri)
8669{
8670 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008671 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008672
James Smart6b5151f2012-01-18 16:24:06 -05008673 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8675 "3115 Node Context not found, driver "
8676 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008677 return;
8678 }
8679
James Smartcb69f7d2011-12-13 13:21:57 -05008680 vport = ndlp->vport;
8681 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8682 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008683 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008684 ndlp->vport->vpi, ndlp->nlp_rpi,
8685 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008686 bf_get(lpfc_wcqe_xa_status, axri),
8687 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008688
James Smart5c1db2a2012-03-01 22:34:36 -05008689 /*
8690 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8691 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8692 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8693 */
8694 ext_status = axri->parameter & WCQE_PARAM_MASK;
8695 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8696 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008697 lpfc_sli_abts_recover_port(vport, ndlp);
8698}
8699
James Smarte59058c2008-08-24 21:49:00 -04008700/**
James Smart3621a712009-04-06 18:47:14 -04008701 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008702 * @phba: Pointer to HBA context object.
8703 * @pring: Pointer to driver SLI ring object.
8704 * @iocbq: Pointer to iocb object.
8705 *
8706 * This function is called by the slow ring event handler
8707 * function when there is an ASYNC event iocb in the ring.
8708 * This function is called with no lock held.
8709 * Currently this function handles only temperature related
8710 * ASYNC events. The function decodes the temperature sensor
8711 * event message and posts events for the management applications.
8712 **/
James Smart98c9ea52007-10-27 13:37:33 -04008713static void
James Smart57127f12007-10-27 13:37:05 -04008714lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8715 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8716{
8717 IOCB_t *icmd;
8718 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008719 struct temp_event temp_event_data;
8720 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008721 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008722
8723 icmd = &iocbq->iocb;
8724 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008725
James Smartcb69f7d2011-12-13 13:21:57 -05008726 switch (evt_code) {
8727 case ASYNC_TEMP_WARN:
8728 case ASYNC_TEMP_SAFE:
8729 temp_event_data.data = (uint32_t) icmd->ulpContext;
8730 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8731 if (evt_code == ASYNC_TEMP_WARN) {
8732 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8733 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8734 "0347 Adapter is very hot, please take "
8735 "corrective action. temperature : %d Celsius\n",
8736 (uint32_t) icmd->ulpContext);
8737 } else {
8738 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8739 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8740 "0340 Adapter temperature is OK now. "
8741 "temperature : %d Celsius\n",
8742 (uint32_t) icmd->ulpContext);
8743 }
8744
8745 /* Send temperature change event to applications */
8746 shost = lpfc_shost_from_vport(phba->pport);
8747 fc_host_post_vendor_event(shost, fc_get_event_number(),
8748 sizeof(temp_event_data), (char *) &temp_event_data,
8749 LPFC_NL_VENDOR_ID);
8750 break;
8751 case ASYNC_STATUS_CN:
8752 lpfc_sli_abts_err_handler(phba, iocbq);
8753 break;
8754 default:
James Smarta257bf92009-04-06 18:48:10 -04008755 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008757 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008758 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008759 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8760 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8761 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8762 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008763 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008764 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8765 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8766 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8767 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8768
James Smartcb69f7d2011-12-13 13:21:57 -05008769 break;
James Smart57127f12007-10-27 13:37:05 -04008770 }
James Smart57127f12007-10-27 13:37:05 -04008771}
8772
8773
James Smarte59058c2008-08-24 21:49:00 -04008774/**
James Smart3621a712009-04-06 18:47:14 -04008775 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008776 * @phba: Pointer to HBA context object.
8777 *
8778 * lpfc_sli_setup sets up rings of the SLI interface with
8779 * number of iocbs per ring and iotags. This function is
8780 * called while driver attach to the HBA and before the
8781 * interrupts are enabled. So there is no need for locking.
8782 *
8783 * This function always returns 0.
8784 **/
dea31012005-04-17 16:05:31 -05008785int
8786lpfc_sli_setup(struct lpfc_hba *phba)
8787{
James Smarted957682007-06-17 19:56:37 -05008788 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008789 struct lpfc_sli *psli = &phba->sli;
8790 struct lpfc_sli_ring *pring;
8791
James Smart2a76a282012-08-03 12:35:54 -04008792 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8793 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04008794 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05008795 psli->sli_flag = 0;
8796 psli->fcp_ring = LPFC_FCP_RING;
8797 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008798 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008799
James Bottomley604a3e32005-10-29 10:28:33 -05008800 psli->iocbq_lookup = NULL;
8801 psli->iocbq_lookup_len = 0;
8802 psli->last_iotag = 0;
8803
dea31012005-04-17 16:05:31 -05008804 for (i = 0; i < psli->num_rings; i++) {
8805 pring = &psli->ring[i];
8806 switch (i) {
8807 case LPFC_FCP_RING: /* ring 0 - FCP */
8808 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008809 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8810 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8811 pring->sli.sli3.numCiocb +=
8812 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8813 pring->sli.sli3.numRiocb +=
8814 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8815 pring->sli.sli3.numCiocb +=
8816 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8817 pring->sli.sli3.numRiocb +=
8818 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8819 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008820 SLI3_IOCB_CMD_SIZE :
8821 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008822 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008823 SLI3_IOCB_RSP_SIZE :
8824 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008825 pring->iotag_ctr = 0;
8826 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008827 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008828 pring->fast_iotag = pring->iotag_max;
8829 pring->num_mask = 0;
8830 break;
James Smarta4bc3372006-12-02 13:34:16 -05008831 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008832 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008833 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8834 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8835 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008836 SLI3_IOCB_CMD_SIZE :
8837 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008838 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008839 SLI3_IOCB_RSP_SIZE :
8840 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008841 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008842 pring->num_mask = 0;
8843 break;
8844 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8845 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008846 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8847 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8848 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008849 SLI3_IOCB_CMD_SIZE :
8850 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008851 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008852 SLI3_IOCB_RSP_SIZE :
8853 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008854 pring->fast_iotag = 0;
8855 pring->iotag_ctr = 0;
8856 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008857 pring->lpfc_sli_rcv_async_status =
8858 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008859 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008860 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008861 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8862 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008863 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008864 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008865 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008866 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8867 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008868 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008869 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008870 pring->prt[2].profile = 0; /* Mask 2 */
8871 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008872 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008873 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008874 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008875 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008876 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008877 pring->prt[3].profile = 0; /* Mask 3 */
8878 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008879 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008880 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008881 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008882 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008883 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008884 /* abort unsolicited sequence */
8885 pring->prt[4].profile = 0; /* Mask 4 */
8886 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8887 pring->prt[4].type = FC_TYPE_BLS;
8888 pring->prt[4].lpfc_sli_rcv_unsol_event =
8889 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008890 break;
8891 }
James Smart7e56aa22012-08-03 12:35:34 -04008892 totiocbsize += (pring->sli.sli3.numCiocb *
8893 pring->sli.sli3.sizeCiocb) +
8894 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05008895 }
James Smarted957682007-06-17 19:56:37 -05008896 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008897 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008898 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8899 "SLI2 SLIM Data: x%x x%lx\n",
8900 phba->brd_no, totiocbsize,
8901 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008902 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008903 if (phba->cfg_multi_ring_support == 2)
8904 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008905
8906 return 0;
8907}
8908
James Smarte59058c2008-08-24 21:49:00 -04008909/**
James Smart3621a712009-04-06 18:47:14 -04008910 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008911 * @phba: Pointer to HBA context object.
8912 *
8913 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8914 * ring. This function also initializes ring indices of each ring.
8915 * This function is called during the initialization of the SLI
8916 * interface of an HBA.
8917 * This function is called with no lock held and always returns
8918 * 1.
8919 **/
dea31012005-04-17 16:05:31 -05008920int
James Smart2e0fef82007-06-17 19:56:36 -05008921lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008922{
8923 struct lpfc_sli *psli;
8924 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008925 int i;
dea31012005-04-17 16:05:31 -05008926
8927 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008928 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008929 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008930 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008931 /* Initialize list headers for txq and txcmplq as double linked lists */
8932 for (i = 0; i < psli->num_rings; i++) {
8933 pring = &psli->ring[i];
8934 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04008935 pring->sli.sli3.next_cmdidx = 0;
8936 pring->sli.sli3.local_getidx = 0;
8937 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05008938 INIT_LIST_HEAD(&pring->txq);
8939 INIT_LIST_HEAD(&pring->txcmplq);
8940 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008941 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008942 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04008943 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05008944 }
James Smart2e0fef82007-06-17 19:56:36 -05008945 spin_unlock_irq(&phba->hbalock);
8946 return 1;
dea31012005-04-17 16:05:31 -05008947}
8948
James Smarte59058c2008-08-24 21:49:00 -04008949/**
James Smart04c68492009-05-22 14:52:52 -04008950 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8951 * @phba: Pointer to HBA context object.
8952 *
8953 * This routine flushes the mailbox command subsystem. It will unconditionally
8954 * flush all the mailbox commands in the three possible stages in the mailbox
8955 * command sub-system: pending mailbox command queue; the outstanding mailbox
8956 * command; and completed mailbox command queue. It is caller's responsibility
8957 * to make sure that the driver is in the proper state to flush the mailbox
8958 * command sub-system. Namely, the posting of mailbox commands into the
8959 * pending mailbox command queue from the various clients must be stopped;
8960 * either the HBA is in a state that it will never works on the outstanding
8961 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8962 * mailbox command has been completed.
8963 **/
8964static void
8965lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8966{
8967 LIST_HEAD(completions);
8968 struct lpfc_sli *psli = &phba->sli;
8969 LPFC_MBOXQ_t *pmb;
8970 unsigned long iflag;
8971
8972 /* Flush all the mailbox commands in the mbox system */
8973 spin_lock_irqsave(&phba->hbalock, iflag);
8974 /* The pending mailbox command queue */
8975 list_splice_init(&phba->sli.mboxq, &completions);
8976 /* The outstanding active mailbox command */
8977 if (psli->mbox_active) {
8978 list_add_tail(&psli->mbox_active->list, &completions);
8979 psli->mbox_active = NULL;
8980 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8981 }
8982 /* The completed mailbox command queue */
8983 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8984 spin_unlock_irqrestore(&phba->hbalock, iflag);
8985
8986 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8987 while (!list_empty(&completions)) {
8988 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8989 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8990 if (pmb->mbox_cmpl)
8991 pmb->mbox_cmpl(phba, pmb);
8992 }
8993}
8994
8995/**
James Smart3621a712009-04-06 18:47:14 -04008996 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008997 * @vport: Pointer to virtual port object.
8998 *
8999 * lpfc_sli_host_down is called to clean up the resources
9000 * associated with a vport before destroying virtual
9001 * port data structures.
9002 * This function does following operations:
9003 * - Free discovery resources associated with this virtual
9004 * port.
9005 * - Free iocbs associated with this virtual port in
9006 * the txq.
9007 * - Send abort for all iocb commands associated with this
9008 * vport in txcmplq.
9009 *
9010 * This function is called with no lock held and always returns 1.
9011 **/
dea31012005-04-17 16:05:31 -05009012int
James Smart92d7f7b2007-06-17 19:56:38 -05009013lpfc_sli_host_down(struct lpfc_vport *vport)
9014{
James Smart858c9f62007-06-17 19:56:39 -05009015 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009016 struct lpfc_hba *phba = vport->phba;
9017 struct lpfc_sli *psli = &phba->sli;
9018 struct lpfc_sli_ring *pring;
9019 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009020 int i;
9021 unsigned long flags = 0;
9022 uint16_t prev_pring_flag;
9023
9024 lpfc_cleanup_discovery_resources(vport);
9025
9026 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009027 for (i = 0; i < psli->num_rings; i++) {
9028 pring = &psli->ring[i];
9029 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009030 /* Only slow rings */
9031 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009032 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009033 /* Set the lpfc data pending flag */
9034 set_bit(LPFC_DATA_READY, &phba->data_flags);
9035 }
James Smart92d7f7b2007-06-17 19:56:38 -05009036 /*
9037 * Error everything on the txq since these iocbs have not been
9038 * given to the FW yet.
9039 */
James Smart92d7f7b2007-06-17 19:56:38 -05009040 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9041 if (iocb->vport != vport)
9042 continue;
James Smart858c9f62007-06-17 19:56:39 -05009043 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009044 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05009045 }
9046
9047 /* Next issue ABTS for everything on the txcmplq */
9048 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9049 list) {
9050 if (iocb->vport != vport)
9051 continue;
9052 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9053 }
9054
9055 pring->flag = prev_pring_flag;
9056 }
9057
9058 spin_unlock_irqrestore(&phba->hbalock, flags);
9059
James Smarta257bf92009-04-06 18:48:10 -04009060 /* Cancel all the IOCBs from the completions list */
9061 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9062 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009063 return 1;
9064}
9065
James Smarte59058c2008-08-24 21:49:00 -04009066/**
James Smart3621a712009-04-06 18:47:14 -04009067 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009068 * @phba: Pointer to HBA context object.
9069 *
9070 * This function cleans up all iocb, buffers, mailbox commands
9071 * while shutting down the HBA. This function is called with no
9072 * lock held and always returns 1.
9073 * This function does the following to cleanup driver resources:
9074 * - Free discovery resources for each virtual port
9075 * - Cleanup any pending fabric iocbs
9076 * - Iterate through the iocb txq and free each entry
9077 * in the list.
9078 * - Free up any buffer posted to the HBA
9079 * - Free mailbox commands in the mailbox queue.
9080 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009081int
James Smart2e0fef82007-06-17 19:56:36 -05009082lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009083{
James Smart2534ba72007-04-25 09:52:20 -04009084 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009085 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009086 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009087 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009088 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009089 int i;
9090
9091 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009092 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009093
dea31012005-04-17 16:05:31 -05009094 lpfc_hba_down_prep(phba);
9095
James Smart92d7f7b2007-06-17 19:56:38 -05009096 lpfc_fabric_abort_hba(phba);
9097
James Smart2e0fef82007-06-17 19:56:36 -05009098 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009099 for (i = 0; i < psli->num_rings; i++) {
9100 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009101 /* Only slow rings */
9102 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009103 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009104 /* Set the lpfc data pending flag */
9105 set_bit(LPFC_DATA_READY, &phba->data_flags);
9106 }
dea31012005-04-17 16:05:31 -05009107
9108 /*
9109 * Error everything on the txq since these iocbs have not been
9110 * given to the FW yet.
9111 */
James Smart2534ba72007-04-25 09:52:20 -04009112 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009113 pring->txq_cnt = 0;
9114
dea31012005-04-17 16:05:31 -05009115 }
James Smart2e0fef82007-06-17 19:56:36 -05009116 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009117
James Smarta257bf92009-04-06 18:48:10 -04009118 /* Cancel all the IOCBs from the completions list */
9119 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9120 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009121
James Smart0ff10d42008-01-11 01:52:36 -05009122 spin_lock_irqsave(&phba->hbalock, flags);
9123 list_splice_init(&phba->elsbuf, &completions);
9124 phba->elsbuf_cnt = 0;
9125 phba->elsbuf_prev_cnt = 0;
9126 spin_unlock_irqrestore(&phba->hbalock, flags);
9127
9128 while (!list_empty(&completions)) {
9129 list_remove_head(&completions, buf_ptr,
9130 struct lpfc_dmabuf, list);
9131 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9132 kfree(buf_ptr);
9133 }
9134
dea31012005-04-17 16:05:31 -05009135 /* Return any active mbox cmds */
9136 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009137
James Smartda0436e2009-05-22 14:51:39 -04009138 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009139 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009140 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009141
James Smartda0436e2009-05-22 14:51:39 -04009142 return 1;
9143}
James Smart92d7f7b2007-06-17 19:56:38 -05009144
James Smartda0436e2009-05-22 14:51:39 -04009145/**
James Smart3621a712009-04-06 18:47:14 -04009146 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009147 * @srcp: Source memory pointer.
9148 * @destp: Destination memory pointer.
9149 * @cnt: Number of words required to be copied.
9150 *
9151 * This function is used for copying data between driver memory
9152 * and the SLI memory. This function also changes the endianness
9153 * of each word if native endianness is different from SLI
9154 * endianness. This function can be called with or without
9155 * lock.
9156 **/
dea31012005-04-17 16:05:31 -05009157void
9158lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9159{
9160 uint32_t *src = srcp;
9161 uint32_t *dest = destp;
9162 uint32_t ldata;
9163 int i;
9164
9165 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9166 ldata = *src;
9167 ldata = le32_to_cpu(ldata);
9168 *dest = ldata;
9169 src++;
9170 dest++;
9171 }
9172}
9173
James Smarte59058c2008-08-24 21:49:00 -04009174
9175/**
James Smarta0c87cb2009-07-19 10:01:10 -04009176 * lpfc_sli_bemem_bcopy - SLI memory copy function
9177 * @srcp: Source memory pointer.
9178 * @destp: Destination memory pointer.
9179 * @cnt: Number of words required to be copied.
9180 *
9181 * This function is used for copying data between a data structure
9182 * with big endian representation to local endianness.
9183 * This function can be called with or without lock.
9184 **/
9185void
9186lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9187{
9188 uint32_t *src = srcp;
9189 uint32_t *dest = destp;
9190 uint32_t ldata;
9191 int i;
9192
9193 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9194 ldata = *src;
9195 ldata = be32_to_cpu(ldata);
9196 *dest = ldata;
9197 src++;
9198 dest++;
9199 }
9200}
9201
9202/**
James Smart3621a712009-04-06 18:47:14 -04009203 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009204 * @phba: Pointer to HBA context object.
9205 * @pring: Pointer to driver SLI ring object.
9206 * @mp: Pointer to driver buffer object.
9207 *
9208 * This function is called with no lock held.
9209 * It always return zero after adding the buffer to the postbufq
9210 * buffer list.
9211 **/
dea31012005-04-17 16:05:31 -05009212int
James Smart2e0fef82007-06-17 19:56:36 -05009213lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9214 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009215{
9216 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9217 later */
James Smart2e0fef82007-06-17 19:56:36 -05009218 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009219 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009220 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009221 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009222 return 0;
9223}
9224
James Smarte59058c2008-08-24 21:49:00 -04009225/**
James Smart3621a712009-04-06 18:47:14 -04009226 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009227 * @phba: Pointer to HBA context object.
9228 *
9229 * When HBQ is enabled, buffers are searched based on tags. This function
9230 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9231 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9232 * does not conflict with tags of buffer posted for unsolicited events.
9233 * The function returns the allocated tag. The function is called with
9234 * no locks held.
9235 **/
James Smart76bb24e2007-10-27 13:38:00 -04009236uint32_t
9237lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9238{
9239 spin_lock_irq(&phba->hbalock);
9240 phba->buffer_tag_count++;
9241 /*
9242 * Always set the QUE_BUFTAG_BIT to distiguish between
9243 * a tag assigned by HBQ.
9244 */
9245 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9246 spin_unlock_irq(&phba->hbalock);
9247 return phba->buffer_tag_count;
9248}
9249
James Smarte59058c2008-08-24 21:49:00 -04009250/**
James Smart3621a712009-04-06 18:47:14 -04009251 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009252 * @phba: Pointer to HBA context object.
9253 * @pring: Pointer to driver SLI ring object.
9254 * @tag: Buffer tag.
9255 *
9256 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9257 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9258 * iocb is posted to the response ring with the tag of the buffer.
9259 * This function searches the pring->postbufq list using the tag
9260 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9261 * iocb. If the buffer is found then lpfc_dmabuf object of the
9262 * buffer is returned to the caller else NULL is returned.
9263 * This function is called with no lock held.
9264 **/
James Smart76bb24e2007-10-27 13:38:00 -04009265struct lpfc_dmabuf *
9266lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9267 uint32_t tag)
9268{
9269 struct lpfc_dmabuf *mp, *next_mp;
9270 struct list_head *slp = &pring->postbufq;
9271
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009272 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009273 spin_lock_irq(&phba->hbalock);
9274 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9275 if (mp->buffer_tag == tag) {
9276 list_del_init(&mp->list);
9277 pring->postbufq_cnt--;
9278 spin_unlock_irq(&phba->hbalock);
9279 return mp;
9280 }
9281 }
9282
9283 spin_unlock_irq(&phba->hbalock);
9284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009285 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009286 "ring %d Data x%lx x%p x%p x%x\n",
9287 pring->ringno, (unsigned long) tag,
9288 slp->next, slp->prev, pring->postbufq_cnt);
9289
9290 return NULL;
9291}
dea31012005-04-17 16:05:31 -05009292
James Smarte59058c2008-08-24 21:49:00 -04009293/**
James Smart3621a712009-04-06 18:47:14 -04009294 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009295 * @phba: Pointer to HBA context object.
9296 * @pring: Pointer to driver SLI ring object.
9297 * @phys: DMA address of the buffer.
9298 *
9299 * This function searches the buffer list using the dma_address
9300 * of unsolicited event to find the driver's lpfc_dmabuf object
9301 * corresponding to the dma_address. The function returns the
9302 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9303 * This function is called by the ct and els unsolicited event
9304 * handlers to get the buffer associated with the unsolicited
9305 * event.
9306 *
9307 * This function is called with no lock held.
9308 **/
dea31012005-04-17 16:05:31 -05009309struct lpfc_dmabuf *
9310lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9311 dma_addr_t phys)
9312{
9313 struct lpfc_dmabuf *mp, *next_mp;
9314 struct list_head *slp = &pring->postbufq;
9315
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009316 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009317 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009318 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9319 if (mp->phys == phys) {
9320 list_del_init(&mp->list);
9321 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009322 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009323 return mp;
9324 }
9325 }
9326
James Smart2e0fef82007-06-17 19:56:36 -05009327 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009329 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009330 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009331 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009332 slp->next, slp->prev, pring->postbufq_cnt);
9333 return NULL;
9334}
9335
James Smarte59058c2008-08-24 21:49:00 -04009336/**
James Smart3621a712009-04-06 18:47:14 -04009337 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009338 * @phba: Pointer to HBA context object.
9339 * @cmdiocb: Pointer to driver command iocb object.
9340 * @rspiocb: Pointer to driver response iocb object.
9341 *
9342 * This function is the completion handler for the abort iocbs for
9343 * ELS commands. This function is called from the ELS ring event
9344 * handler with no lock held. This function frees memory resources
9345 * associated with the abort iocb.
9346 **/
dea31012005-04-17 16:05:31 -05009347static void
James Smart2e0fef82007-06-17 19:56:36 -05009348lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9349 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009350{
James Smart2e0fef82007-06-17 19:56:36 -05009351 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009352 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009353 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009354
9355 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009356
9357 /*
9358 * Assume that the port already completed and returned, or
9359 * will return the iocb. Just Log the message.
9360 */
James Smart2680eea2007-04-25 09:52:55 -04009361 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9362 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9363
James Smart2e0fef82007-06-17 19:56:36 -05009364 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009365 if (phba->sli_rev < LPFC_SLI_REV4) {
9366 if (abort_iotag != 0 &&
9367 abort_iotag <= phba->sli.last_iotag)
9368 abort_iocb =
9369 phba->sli.iocbq_lookup[abort_iotag];
9370 } else
9371 /* For sli4 the abort_tag is the XRI,
9372 * so the abort routine puts the iotag of the iocb
9373 * being aborted in the context field of the abort
9374 * IOCB.
9375 */
9376 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009377
James Smart2a9bf3d2010-06-07 15:24:45 -04009378 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9379 "0327 Cannot abort els iocb %p "
9380 "with tag %x context %x, abort status %x, "
9381 "abort code %x\n",
9382 abort_iocb, abort_iotag, abort_context,
9383 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009384
James Smartff78d8f2011-12-13 13:21:35 -05009385 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009386 }
James Bottomley604a3e32005-10-29 10:28:33 -05009387 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009388 return;
9389}
9390
James Smarte59058c2008-08-24 21:49:00 -04009391/**
James Smart3621a712009-04-06 18:47:14 -04009392 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009393 * @phba: Pointer to HBA context object.
9394 * @cmdiocb: Pointer to driver command iocb object.
9395 * @rspiocb: Pointer to driver response iocb object.
9396 *
9397 * The function is called from SLI ring event handler with no
9398 * lock held. This function is the completion handler for ELS commands
9399 * which are aborted. The function frees memory resources used for
9400 * the aborted ELS commands.
9401 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009402static void
9403lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9404 struct lpfc_iocbq *rspiocb)
9405{
9406 IOCB_t *irsp = &rspiocb->iocb;
9407
9408 /* ELS cmd tag <ulpIoTag> completes */
9409 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009410 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009411 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009412 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009413 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009414 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9415 lpfc_ct_free_iocb(phba, cmdiocb);
9416 else
9417 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009418 return;
9419}
9420
James Smarte59058c2008-08-24 21:49:00 -04009421/**
James Smart5af5eee2010-10-22 11:06:38 -04009422 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009423 * @phba: Pointer to HBA context object.
9424 * @pring: Pointer to driver SLI ring object.
9425 * @cmdiocb: Pointer to driver command iocb object.
9426 *
James Smart5af5eee2010-10-22 11:06:38 -04009427 * This function issues an abort iocb for the provided command iocb down to
9428 * the port. Other than the case the outstanding command iocb is an abort
9429 * request, this function issues abort out unconditionally. This function is
9430 * called with hbalock held. The function returns 0 when it fails due to
9431 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009432 **/
James Smart5af5eee2010-10-22 11:06:38 -04009433static int
9434lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009435 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009436{
James Smart2e0fef82007-06-17 19:56:36 -05009437 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009438 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009439 IOCB_t *icmd = NULL;
9440 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009441 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009442 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009443
James Smart92d7f7b2007-06-17 19:56:38 -05009444 /*
9445 * There are certain command types we don't want to abort. And we
9446 * don't want to abort commands that are already in the process of
9447 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009448 */
9449 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009450 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009451 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9452 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009453 return 0;
9454
dea31012005-04-17 16:05:31 -05009455 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009456 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009457 if (abtsiocbp == NULL)
9458 return 0;
dea31012005-04-17 16:05:31 -05009459
James Smart07951072007-04-25 09:51:38 -04009460 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009461 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009462 */
9463 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9464
dea31012005-04-17 16:05:31 -05009465 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009466 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9467 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009468 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009469 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009470 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9471 }
James Smartda0436e2009-05-22 14:51:39 -04009472 else
9473 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009474 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009475 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009476
James Smart5ffc2662009-11-18 15:39:44 -05009477 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9478 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009479 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9480 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009481
James Smart2e0fef82007-06-17 19:56:36 -05009482 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009483 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9484 else
9485 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9486
9487 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009488
James Smarte8b62012007-08-02 11:10:09 -04009489 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9490 "0339 Abort xri x%x, original iotag x%x, "
9491 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009492 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009493 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009494 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009495
9496 if (phba->sli_rev == LPFC_SLI_REV4) {
9497 /* Note: both hbalock and ring_lock need to be set here */
9498 spin_lock_irqsave(&pring->ring_lock, iflags);
9499 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9500 abtsiocbp, 0);
9501 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9502 } else {
9503 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9504 abtsiocbp, 0);
9505 }
James Smart07951072007-04-25 09:51:38 -04009506
James Smartd7c255b2008-08-24 21:50:00 -04009507 if (retval)
9508 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009509
9510 /*
9511 * Caller to this routine should check for IOCB_ERROR
9512 * and handle it properly. This routine no longer removes
9513 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9514 */
9515 return retval;
9516}
9517
9518/**
9519 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9520 * @phba: Pointer to HBA context object.
9521 * @pring: Pointer to driver SLI ring object.
9522 * @cmdiocb: Pointer to driver command iocb object.
9523 *
9524 * This function issues an abort iocb for the provided command iocb. In case
9525 * of unloading, the abort iocb will not be issued to commands on the ELS
9526 * ring. Instead, the callback function shall be changed to those commands
9527 * so that nothing happens when them finishes. This function is called with
9528 * hbalock held. The function returns 0 when the command iocb is an abort
9529 * request.
9530 **/
9531int
9532lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9533 struct lpfc_iocbq *cmdiocb)
9534{
9535 struct lpfc_vport *vport = cmdiocb->vport;
9536 int retval = IOCB_ERROR;
9537 IOCB_t *icmd = NULL;
9538
9539 /*
9540 * There are certain command types we don't want to abort. And we
9541 * don't want to abort commands that are already in the process of
9542 * being aborted.
9543 */
9544 icmd = &cmdiocb->iocb;
9545 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9546 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9547 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9548 return 0;
9549
9550 /*
9551 * If we're unloading, don't abort iocb on the ELS ring, but change
9552 * the callback so that nothing happens when it finishes.
9553 */
9554 if ((vport->load_flag & FC_UNLOADING) &&
9555 (pring->ringno == LPFC_ELS_RING)) {
9556 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9557 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9558 else
9559 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9560 goto abort_iotag_exit;
9561 }
9562
9563 /* Now, we try to issue the abort to the cmdiocb out */
9564 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9565
James Smart07951072007-04-25 09:51:38 -04009566abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009567 /*
9568 * Caller to this routine should check for IOCB_ERROR
9569 * and handle it properly. This routine no longer removes
9570 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009571 */
James Smart2e0fef82007-06-17 19:56:36 -05009572 return retval;
dea31012005-04-17 16:05:31 -05009573}
9574
James Smarte59058c2008-08-24 21:49:00 -04009575/**
James Smart5af5eee2010-10-22 11:06:38 -04009576 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9577 * @phba: Pointer to HBA context object.
9578 * @pring: Pointer to driver SLI ring object.
9579 *
9580 * This function aborts all iocbs in the given ring and frees all the iocb
9581 * objects in txq. This function issues abort iocbs unconditionally for all
9582 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9583 * to complete before the return of this function. The caller is not required
9584 * to hold any locks.
9585 **/
9586static void
9587lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9588{
9589 LIST_HEAD(completions);
9590 struct lpfc_iocbq *iocb, *next_iocb;
9591
9592 if (pring->ringno == LPFC_ELS_RING)
9593 lpfc_fabric_abort_hba(phba);
9594
9595 spin_lock_irq(&phba->hbalock);
9596
9597 /* Take off all the iocbs on txq for cancelling */
9598 list_splice_init(&pring->txq, &completions);
9599 pring->txq_cnt = 0;
9600
9601 /* Next issue ABTS for everything on the txcmplq */
9602 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9603 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9604
9605 spin_unlock_irq(&phba->hbalock);
9606
9607 /* Cancel all the IOCBs from the completions list */
9608 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9609 IOERR_SLI_ABORTED);
9610}
9611
9612/**
9613 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9614 * @phba: pointer to lpfc HBA data structure.
9615 *
9616 * This routine will abort all pending and outstanding iocbs to an HBA.
9617 **/
9618void
9619lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9620{
9621 struct lpfc_sli *psli = &phba->sli;
9622 struct lpfc_sli_ring *pring;
9623 int i;
9624
9625 for (i = 0; i < psli->num_rings; i++) {
9626 pring = &psli->ring[i];
9627 lpfc_sli_iocb_ring_abort(phba, pring);
9628 }
9629}
9630
9631/**
James Smart3621a712009-04-06 18:47:14 -04009632 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009633 * @iocbq: Pointer to driver iocb object.
9634 * @vport: Pointer to driver virtual port object.
9635 * @tgt_id: SCSI ID of the target.
9636 * @lun_id: LUN ID of the scsi device.
9637 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9638 *
James Smart3621a712009-04-06 18:47:14 -04009639 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009640 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9641 * 0 if the filtering criteria is met for the given iocb and will return
9642 * 1 if the filtering criteria is not met.
9643 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9644 * given iocb is for the SCSI device specified by vport, tgt_id and
9645 * lun_id parameter.
9646 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9647 * given iocb is for the SCSI target specified by vport and tgt_id
9648 * parameters.
9649 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9650 * given iocb is for the SCSI host associated with the given vport.
9651 * This function is called with no locks held.
9652 **/
dea31012005-04-17 16:05:31 -05009653static int
James Smart51ef4c22007-08-02 11:10:31 -04009654lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9655 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009656 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009657{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009658 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009659 int rc = 1;
9660
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009661 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9662 return rc;
9663
James Smart51ef4c22007-08-02 11:10:31 -04009664 if (iocbq->vport != vport)
9665 return rc;
9666
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009667 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009668
James Smart495a7142008-06-14 22:52:59 -04009669 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009670 return rc;
9671
9672 switch (ctx_cmd) {
9673 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009674 if ((lpfc_cmd->rdata->pnode) &&
9675 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9676 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009677 rc = 0;
9678 break;
9679 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009680 if ((lpfc_cmd->rdata->pnode) &&
9681 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009682 rc = 0;
9683 break;
dea31012005-04-17 16:05:31 -05009684 case LPFC_CTX_HOST:
9685 rc = 0;
9686 break;
9687 default:
9688 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009689 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009690 break;
9691 }
9692
9693 return rc;
9694}
9695
James Smarte59058c2008-08-24 21:49:00 -04009696/**
James Smart3621a712009-04-06 18:47:14 -04009697 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009698 * @vport: Pointer to virtual port.
9699 * @tgt_id: SCSI ID of the target.
9700 * @lun_id: LUN ID of the scsi device.
9701 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9702 *
9703 * This function returns number of FCP commands pending for the vport.
9704 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9705 * commands pending on the vport associated with SCSI device specified
9706 * by tgt_id and lun_id parameters.
9707 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9708 * commands pending on the vport associated with SCSI target specified
9709 * by tgt_id parameter.
9710 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9711 * commands pending on the vport.
9712 * This function returns the number of iocbs which satisfy the filter.
9713 * This function is called without any lock held.
9714 **/
dea31012005-04-17 16:05:31 -05009715int
James Smart51ef4c22007-08-02 11:10:31 -04009716lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9717 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009718{
James Smart51ef4c22007-08-02 11:10:31 -04009719 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009720 struct lpfc_iocbq *iocbq;
9721 int sum, i;
dea31012005-04-17 16:05:31 -05009722
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009723 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9724 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009725
James Smart51ef4c22007-08-02 11:10:31 -04009726 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9727 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009728 sum++;
dea31012005-04-17 16:05:31 -05009729 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009730
dea31012005-04-17 16:05:31 -05009731 return sum;
9732}
9733
James Smarte59058c2008-08-24 21:49:00 -04009734/**
James Smart3621a712009-04-06 18:47:14 -04009735 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009736 * @phba: Pointer to HBA context object
9737 * @cmdiocb: Pointer to command iocb object.
9738 * @rspiocb: Pointer to response iocb object.
9739 *
9740 * This function is called when an aborted FCP iocb completes. This
9741 * function is called by the ring event handler with no lock held.
9742 * This function frees the iocb.
9743 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009744void
James Smart2e0fef82007-06-17 19:56:36 -05009745lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9746 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009747{
James Smartcb69f7d2011-12-13 13:21:57 -05009748 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9749 "3096 ABORT_XRI_CN completing on xri x%x "
9750 "original iotag x%x, abort cmd iotag x%x "
9751 "status 0x%x, reason 0x%x\n",
9752 cmdiocb->iocb.un.acxri.abortContextTag,
9753 cmdiocb->iocb.un.acxri.abortIoTag,
9754 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9755 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009756 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009757 return;
9758}
9759
James Smarte59058c2008-08-24 21:49:00 -04009760/**
James Smart3621a712009-04-06 18:47:14 -04009761 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009762 * @vport: Pointer to virtual port.
9763 * @pring: Pointer to driver SLI ring object.
9764 * @tgt_id: SCSI ID of the target.
9765 * @lun_id: LUN ID of the scsi device.
9766 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9767 *
9768 * This function sends an abort command for every SCSI command
9769 * associated with the given virtual port pending on the ring
9770 * filtered by lpfc_sli_validate_fcp_iocb function.
9771 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9772 * FCP iocbs associated with lun specified by tgt_id and lun_id
9773 * parameters
9774 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9775 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9776 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9777 * FCP iocbs associated with virtual port.
9778 * This function returns number of iocbs it failed to abort.
9779 * This function is called with no locks held.
9780 **/
dea31012005-04-17 16:05:31 -05009781int
James Smart51ef4c22007-08-02 11:10:31 -04009782lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9783 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009784{
James Smart51ef4c22007-08-02 11:10:31 -04009785 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009786 struct lpfc_iocbq *iocbq;
9787 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009788 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009789 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009790 int i;
dea31012005-04-17 16:05:31 -05009791
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009792 for (i = 1; i <= phba->sli.last_iotag; i++) {
9793 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009794
James Smart51ef4c22007-08-02 11:10:31 -04009795 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009796 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009797 continue;
9798
9799 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009800 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009801 if (abtsiocb == NULL) {
9802 errcnt++;
9803 continue;
9804 }
dea31012005-04-17 16:05:31 -05009805
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009806 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009807 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9808 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009809 if (phba->sli_rev == LPFC_SLI_REV4)
9810 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9811 else
9812 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009813 abtsiocb->iocb.ulpLe = 1;
9814 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009815 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009816
James Smart5ffc2662009-11-18 15:39:44 -05009817 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9818 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009819 if (iocbq->iocb_flag & LPFC_IO_FCP)
9820 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009821
James Smart2e0fef82007-06-17 19:56:36 -05009822 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009823 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9824 else
9825 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9826
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009827 /* Setup callback routine and issue the command. */
9828 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009829 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9830 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009831 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009832 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009833 errcnt++;
9834 continue;
9835 }
9836 }
9837
9838 return errcnt;
9839}
9840
James Smarte59058c2008-08-24 21:49:00 -04009841/**
James Smart3621a712009-04-06 18:47:14 -04009842 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009843 * @phba: Pointer to HBA context object.
9844 * @cmdiocbq: Pointer to command iocb.
9845 * @rspiocbq: Pointer to response iocb.
9846 *
9847 * This function is the completion handler for iocbs issued using
9848 * lpfc_sli_issue_iocb_wait function. This function is called by the
9849 * ring event handler function without any lock held. This function
9850 * can be called from both worker thread context and interrupt
9851 * context. This function also can be called from other thread which
9852 * cleans up the SLI layer objects.
9853 * This function copy the contents of the response iocb to the
9854 * response iocb memory object provided by the caller of
9855 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9856 * sleeps for the iocb completion.
9857 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009858static void
9859lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9860 struct lpfc_iocbq *cmdiocbq,
9861 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009862{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009863 wait_queue_head_t *pdone_q;
9864 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009865 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009866
James Smart2e0fef82007-06-17 19:56:36 -05009867 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009868 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9869 if (cmdiocbq->context2 && rspiocbq)
9870 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9871 &rspiocbq->iocb, sizeof(IOCB_t));
9872
James Smart0f65ff62010-02-26 14:14:23 -05009873 /* Set the exchange busy flag for task management commands */
9874 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9875 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9876 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9877 cur_iocbq);
9878 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9879 }
9880
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009881 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009882 if (pdone_q)
9883 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009884 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009885 return;
9886}
9887
James Smarte59058c2008-08-24 21:49:00 -04009888/**
James Smartd11e31d2009-06-10 17:23:06 -04009889 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9890 * @phba: Pointer to HBA context object..
9891 * @piocbq: Pointer to command iocb.
9892 * @flag: Flag to test.
9893 *
9894 * This routine grabs the hbalock and then test the iocb_flag to
9895 * see if the passed in flag is set.
9896 * Returns:
9897 * 1 if flag is set.
9898 * 0 if flag is not set.
9899 **/
9900static int
9901lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9902 struct lpfc_iocbq *piocbq, uint32_t flag)
9903{
9904 unsigned long iflags;
9905 int ret;
9906
9907 spin_lock_irqsave(&phba->hbalock, iflags);
9908 ret = piocbq->iocb_flag & flag;
9909 spin_unlock_irqrestore(&phba->hbalock, iflags);
9910 return ret;
9911
9912}
9913
9914/**
James Smart3621a712009-04-06 18:47:14 -04009915 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009916 * @phba: Pointer to HBA context object..
9917 * @pring: Pointer to sli ring.
9918 * @piocb: Pointer to command iocb.
9919 * @prspiocbq: Pointer to response iocb.
9920 * @timeout: Timeout in number of seconds.
9921 *
9922 * This function issues the iocb to firmware and waits for the
9923 * iocb to complete. If the iocb command is not
9924 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9925 * Caller should not free the iocb resources if this function
9926 * returns IOCB_TIMEDOUT.
9927 * The function waits for the iocb completion using an
9928 * non-interruptible wait.
9929 * This function will sleep while waiting for iocb completion.
9930 * So, this function should not be called from any context which
9931 * does not allow sleeping. Due to the same reason, this function
9932 * cannot be called with interrupt disabled.
9933 * This function assumes that the iocb completions occur while
9934 * this function sleep. So, this function cannot be called from
9935 * the thread which process iocb completion for this ring.
9936 * This function clears the iocb_flag of the iocb object before
9937 * issuing the iocb and the iocb completion handler sets this
9938 * flag and wakes this thread when the iocb completes.
9939 * The contents of the response iocb will be copied to prspiocbq
9940 * by the completion handler when the command completes.
9941 * This function returns IOCB_SUCCESS when success.
9942 * This function is called with no lock held.
9943 **/
dea31012005-04-17 16:05:31 -05009944int
James Smart2e0fef82007-06-17 19:56:36 -05009945lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009946 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009947 struct lpfc_iocbq *piocb,
9948 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009949 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009950{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009951 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009952 long timeleft, timeout_req = 0;
9953 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009954 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009955 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009956 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009957 * If the caller has provided a response iocbq buffer, then context2
9958 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009959 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009960 if (prspiocbq) {
9961 if (piocb->context2)
9962 return IOCB_ERROR;
9963 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009964 }
9965
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009966 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9967 piocb->context_un.wait_queue = &done_q;
9968 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009969
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009970 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009971 if (lpfc_readl(phba->HCregaddr, &creg_val))
9972 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009973 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9974 writel(creg_val, phba->HCregaddr);
9975 readl(phba->HCregaddr); /* flush */
9976 }
9977
James Smart2a9bf3d2010-06-07 15:24:45 -04009978 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9979 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009980 if (retval == IOCB_SUCCESS) {
9981 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009982 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009983 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009984 timeout_req);
dea31012005-04-17 16:05:31 -05009985
James Smart7054a602007-04-25 09:52:34 -04009986 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9987 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009988 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009989 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009991 "0338 IOCB wait timeout error - no "
9992 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009993 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009994 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009996 "0330 IOCB wake NOT set, "
9997 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009998 timeout, (timeleft / jiffies));
9999 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010000 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010001 } else if (retval == IOCB_BUSY) {
10002 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10003 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10004 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
10005 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010006 } else {
10007 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010008 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010009 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010010 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010011 }
10012
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010013 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010014 if (lpfc_readl(phba->HCregaddr, &creg_val))
10015 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010016 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10017 writel(creg_val, phba->HCregaddr);
10018 readl(phba->HCregaddr); /* flush */
10019 }
10020
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010021 if (prspiocbq)
10022 piocb->context2 = NULL;
10023
10024 piocb->context_un.wait_queue = NULL;
10025 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010026 return retval;
10027}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010028
James Smarte59058c2008-08-24 21:49:00 -040010029/**
James Smart3621a712009-04-06 18:47:14 -040010030 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010031 * @phba: Pointer to HBA context object.
10032 * @pmboxq: Pointer to driver mailbox object.
10033 * @timeout: Timeout in number of seconds.
10034 *
10035 * This function issues the mailbox to firmware and waits for the
10036 * mailbox command to complete. If the mailbox command is not
10037 * completed within timeout seconds, it returns MBX_TIMEOUT.
10038 * The function waits for the mailbox completion using an
10039 * interruptible wait. If the thread is woken up due to a
10040 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10041 * should not free the mailbox resources, if this function returns
10042 * MBX_TIMEOUT.
10043 * This function will sleep while waiting for mailbox completion.
10044 * So, this function should not be called from any context which
10045 * does not allow sleeping. Due to the same reason, this function
10046 * cannot be called with interrupt disabled.
10047 * This function assumes that the mailbox completion occurs while
10048 * this function sleep. So, this function cannot be called from
10049 * the worker thread which processes mailbox completion.
10050 * This function is called in the context of HBA management
10051 * applications.
10052 * This function returns MBX_SUCCESS when successful.
10053 * This function is called with no lock held.
10054 **/
dea31012005-04-17 16:05:31 -050010055int
James Smart2e0fef82007-06-17 19:56:36 -050010056lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010057 uint32_t timeout)
10058{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010059 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -050010060 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010061 unsigned long flag;
dea31012005-04-17 16:05:31 -050010062
10063 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -040010064 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -050010065 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -050010066
James Smart495a7142008-06-14 22:52:59 -040010067 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010068 /* setup wake call as IOCB callback */
10069 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10070 /* setup context field to pass wait_queue pointer to wake function */
10071 pmboxq->context1 = &done_q;
10072
dea31012005-04-17 16:05:31 -050010073 /* now issue the command */
10074 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010075 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010076 wait_event_interruptible_timeout(done_q,
10077 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10078 timeout * HZ);
10079
James Smart858c9f62007-06-17 19:56:39 -050010080 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010081 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -040010082 /*
10083 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10084 * else do not free the resources.
10085 */
James Smartd7c47992010-06-08 18:31:54 -040010086 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010087 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010088 lpfc_sli4_swap_str(phba, pmboxq);
10089 } else {
James Smart7054a602007-04-25 09:52:34 -040010090 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010091 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10092 }
10093 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010094 }
10095
dea31012005-04-17 16:05:31 -050010096 return retval;
10097}
10098
James Smarte59058c2008-08-24 21:49:00 -040010099/**
James Smart3772a992009-05-22 14:50:54 -040010100 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010101 * @phba: Pointer to HBA context.
10102 *
James Smart3772a992009-05-22 14:50:54 -040010103 * This function is called to shutdown the driver's mailbox sub-system.
10104 * It first marks the mailbox sub-system is in a block state to prevent
10105 * the asynchronous mailbox command from issued off the pending mailbox
10106 * command queue. If the mailbox command sub-system shutdown is due to
10107 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10108 * the mailbox sub-system flush routine to forcefully bring down the
10109 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10110 * as with offline or HBA function reset), this routine will wait for the
10111 * outstanding mailbox command to complete before invoking the mailbox
10112 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010113 **/
James Smart3772a992009-05-22 14:50:54 -040010114void
James Smart618a5232012-06-12 13:54:36 -040010115lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010116{
James Smart3772a992009-05-22 14:50:54 -040010117 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010118 unsigned long timeout;
10119
James Smart618a5232012-06-12 13:54:36 -040010120 if (mbx_action == LPFC_MBX_NO_WAIT) {
10121 /* delay 100ms for port state */
10122 msleep(100);
10123 lpfc_sli_mbox_sys_flush(phba);
10124 return;
10125 }
James Smarta183a152011-10-10 21:32:43 -040010126 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010127
James Smart3772a992009-05-22 14:50:54 -040010128 spin_lock_irq(&phba->hbalock);
10129 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010130
10131 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010132 /* Determine how long we might wait for the active mailbox
10133 * command to be gracefully completed by firmware.
10134 */
James Smarta183a152011-10-10 21:32:43 -040010135 if (phba->sli.mbox_active)
10136 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10137 phba->sli.mbox_active) *
10138 1000) + jiffies;
10139 spin_unlock_irq(&phba->hbalock);
10140
James Smart3772a992009-05-22 14:50:54 -040010141 while (phba->sli.mbox_active) {
10142 /* Check active mailbox complete status every 2ms */
10143 msleep(2);
10144 if (time_after(jiffies, timeout))
10145 /* Timeout, let the mailbox flush routine to
10146 * forcefully release active mailbox command
10147 */
10148 break;
10149 }
James Smartd7069f02012-03-01 22:36:29 -050010150 } else
10151 spin_unlock_irq(&phba->hbalock);
10152
James Smart3772a992009-05-22 14:50:54 -040010153 lpfc_sli_mbox_sys_flush(phba);
10154}
10155
10156/**
10157 * lpfc_sli_eratt_read - read sli-3 error attention events
10158 * @phba: Pointer to HBA context.
10159 *
10160 * This function is called to read the SLI3 device error attention registers
10161 * for possible error attention events. The caller must hold the hostlock
10162 * with spin_lock_irq().
10163 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010164 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010165 * Register and returns 0 otherwise.
10166 **/
10167static int
10168lpfc_sli_eratt_read(struct lpfc_hba *phba)
10169{
James Smarted957682007-06-17 19:56:37 -050010170 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010171
James Smart3772a992009-05-22 14:50:54 -040010172 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010173 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10174 goto unplug_err;
10175
James Smart3772a992009-05-22 14:50:54 -040010176 if (ha_copy & HA_ERATT) {
10177 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010178 if (lpfc_sli_read_hs(phba))
10179 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010180
James Smart3772a992009-05-22 14:50:54 -040010181 /* Check if there is a deferred error condition is active */
10182 if ((HS_FFER1 & phba->work_hs) &&
10183 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010184 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010185 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010186 /* Clear all interrupt enable conditions */
10187 writel(0, phba->HCregaddr);
10188 readl(phba->HCregaddr);
10189 }
10190
10191 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010192 phba->work_ha |= HA_ERATT;
10193 /* Indicate polling handles this ERATT */
10194 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010195 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010196 }
James Smart3772a992009-05-22 14:50:54 -040010197 return 0;
James Smart9940b972011-03-11 16:06:12 -050010198
10199unplug_err:
10200 /* Set the driver HS work bitmap */
10201 phba->work_hs |= UNPLUG_ERR;
10202 /* Set the driver HA work bitmap */
10203 phba->work_ha |= HA_ERATT;
10204 /* Indicate polling handles this ERATT */
10205 phba->hba_flag |= HBA_ERATT_HANDLED;
10206 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010207}
10208
James Smarte59058c2008-08-24 21:49:00 -040010209/**
James Smartda0436e2009-05-22 14:51:39 -040010210 * lpfc_sli4_eratt_read - read sli-4 error attention events
10211 * @phba: Pointer to HBA context.
10212 *
10213 * This function is called to read the SLI4 device error attention registers
10214 * for possible error attention events. The caller must hold the hostlock
10215 * with spin_lock_irq().
10216 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010217 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010218 * Register and returns 0 otherwise.
10219 **/
10220static int
10221lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10222{
10223 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010224 uint32_t if_type, portsmphr;
10225 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010226
James Smart2fcee4b2010-12-15 17:57:46 -050010227 /*
10228 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010229 * registers for error attention. This can be changed later.
10230 */
James Smart2fcee4b2010-12-15 17:57:46 -050010231 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10232 switch (if_type) {
10233 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010234 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10235 &uerr_sta_lo) ||
10236 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10237 &uerr_sta_hi)) {
10238 phba->work_hs |= UNPLUG_ERR;
10239 phba->work_ha |= HA_ERATT;
10240 phba->hba_flag |= HBA_ERATT_HANDLED;
10241 return 1;
10242 }
James Smart2fcee4b2010-12-15 17:57:46 -050010243 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10244 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10246 "1423 HBA Unrecoverable error: "
10247 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10248 "ue_mask_lo_reg=0x%x, "
10249 "ue_mask_hi_reg=0x%x\n",
10250 uerr_sta_lo, uerr_sta_hi,
10251 phba->sli4_hba.ue_mask_lo,
10252 phba->sli4_hba.ue_mask_hi);
10253 phba->work_status[0] = uerr_sta_lo;
10254 phba->work_status[1] = uerr_sta_hi;
10255 phba->work_ha |= HA_ERATT;
10256 phba->hba_flag |= HBA_ERATT_HANDLED;
10257 return 1;
10258 }
10259 break;
10260 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010261 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10262 &portstat_reg.word0) ||
10263 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10264 &portsmphr)){
10265 phba->work_hs |= UNPLUG_ERR;
10266 phba->work_ha |= HA_ERATT;
10267 phba->hba_flag |= HBA_ERATT_HANDLED;
10268 return 1;
10269 }
James Smart2fcee4b2010-12-15 17:57:46 -050010270 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10271 phba->work_status[0] =
10272 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10273 phba->work_status[1] =
10274 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010276 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010277 "port status reg 0x%x, "
10278 "port smphr reg 0x%x, "
10279 "error 1=0x%x, error 2=0x%x\n",
10280 portstat_reg.word0,
10281 portsmphr,
10282 phba->work_status[0],
10283 phba->work_status[1]);
10284 phba->work_ha |= HA_ERATT;
10285 phba->hba_flag |= HBA_ERATT_HANDLED;
10286 return 1;
10287 }
10288 break;
10289 case LPFC_SLI_INTF_IF_TYPE_1:
10290 default:
James Smarta747c9c2009-11-18 15:41:10 -050010291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010292 "2886 HBA Error Attention on unsupported "
10293 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010294 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010295 }
James Smart2fcee4b2010-12-15 17:57:46 -050010296
James Smartda0436e2009-05-22 14:51:39 -040010297 return 0;
10298}
10299
10300/**
James Smart3621a712009-04-06 18:47:14 -040010301 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010302 * @phba: Pointer to HBA context.
10303 *
James Smart3772a992009-05-22 14:50:54 -040010304 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010305 * error attention register bit for error attention events.
10306 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010307 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010308 * Register and returns 0 otherwise.
10309 **/
10310int
10311lpfc_sli_check_eratt(struct lpfc_hba *phba)
10312{
10313 uint32_t ha_copy;
10314
10315 /* If somebody is waiting to handle an eratt, don't process it
10316 * here. The brdkill function will do this.
10317 */
10318 if (phba->link_flag & LS_IGNORE_ERATT)
10319 return 0;
10320
10321 /* Check if interrupt handler handles this ERATT */
10322 spin_lock_irq(&phba->hbalock);
10323 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10324 /* Interrupt handler has handled ERATT */
10325 spin_unlock_irq(&phba->hbalock);
10326 return 0;
10327 }
10328
James Smarta257bf92009-04-06 18:48:10 -040010329 /*
10330 * If there is deferred error attention, do not check for error
10331 * attention
10332 */
10333 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10334 spin_unlock_irq(&phba->hbalock);
10335 return 0;
10336 }
10337
James Smart3772a992009-05-22 14:50:54 -040010338 /* If PCI channel is offline, don't process it */
10339 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010340 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010341 return 0;
10342 }
10343
10344 switch (phba->sli_rev) {
10345 case LPFC_SLI_REV2:
10346 case LPFC_SLI_REV3:
10347 /* Read chip Host Attention (HA) register */
10348 ha_copy = lpfc_sli_eratt_read(phba);
10349 break;
James Smartda0436e2009-05-22 14:51:39 -040010350 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010351 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010352 ha_copy = lpfc_sli4_eratt_read(phba);
10353 break;
James Smart3772a992009-05-22 14:50:54 -040010354 default:
10355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10356 "0299 Invalid SLI revision (%d)\n",
10357 phba->sli_rev);
10358 ha_copy = 0;
10359 break;
James Smart93996272008-08-24 21:50:30 -040010360 }
10361 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010362
10363 return ha_copy;
10364}
10365
10366/**
10367 * lpfc_intr_state_check - Check device state for interrupt handling
10368 * @phba: Pointer to HBA context.
10369 *
10370 * This inline routine checks whether a device or its PCI slot is in a state
10371 * that the interrupt should be handled.
10372 *
10373 * This function returns 0 if the device or the PCI slot is in a state that
10374 * interrupt should be handled, otherwise -EIO.
10375 */
10376static inline int
10377lpfc_intr_state_check(struct lpfc_hba *phba)
10378{
10379 /* If the pci channel is offline, ignore all the interrupts */
10380 if (unlikely(pci_channel_offline(phba->pcidev)))
10381 return -EIO;
10382
10383 /* Update device level interrupt statistics */
10384 phba->sli.slistat.sli_intr++;
10385
10386 /* Ignore all interrupts during initialization. */
10387 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10388 return -EIO;
10389
James Smart93996272008-08-24 21:50:30 -040010390 return 0;
10391}
10392
10393/**
James Smart3772a992009-05-22 14:50:54 -040010394 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010395 * @irq: Interrupt number.
10396 * @dev_id: The device context pointer.
10397 *
James Smart93996272008-08-24 21:50:30 -040010398 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010399 * service routine when device with SLI-3 interface spec is enabled with
10400 * MSI-X multi-message interrupt mode and there are slow-path events in
10401 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10402 * interrupt mode, this function is called as part of the device-level
10403 * interrupt handler. When the PCI slot is in error recovery or the HBA
10404 * is undergoing initialization, the interrupt handler will not process
10405 * the interrupt. The link attention and ELS ring attention events are
10406 * handled by the worker thread. The interrupt handler signals the worker
10407 * thread and returns for these events. This function is called without
10408 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010409 * structures.
10410 *
10411 * This function returns IRQ_HANDLED when interrupt is handled else it
10412 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010413 **/
dea31012005-04-17 16:05:31 -050010414irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010415lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010416{
James Smart2e0fef82007-06-17 19:56:36 -050010417 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010418 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010419 uint32_t work_ha_copy;
10420 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010421 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010422 uint32_t control;
10423
James Smart92d7f7b2007-06-17 19:56:38 -050010424 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010425 struct lpfc_vport *vport;
10426 struct lpfc_nodelist *ndlp;
10427 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010428 LPFC_MBOXQ_t *pmb;
10429 int rc;
10430
dea31012005-04-17 16:05:31 -050010431 /*
10432 * Get the driver's phba structure from the dev_id and
10433 * assume the HBA is not interrupting.
10434 */
James Smart93996272008-08-24 21:50:30 -040010435 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010436
10437 if (unlikely(!phba))
10438 return IRQ_NONE;
10439
dea31012005-04-17 16:05:31 -050010440 /*
James Smart93996272008-08-24 21:50:30 -040010441 * Stuff needs to be attented to when this function is invoked as an
10442 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010443 */
James Smart93996272008-08-24 21:50:30 -040010444 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010445 /* Check device state for handling interrupt */
10446 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010447 return IRQ_NONE;
10448 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010449 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010450 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10451 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010452 /* If somebody is waiting to handle an eratt don't process it
10453 * here. The brdkill function will do this.
10454 */
10455 if (phba->link_flag & LS_IGNORE_ERATT)
10456 ha_copy &= ~HA_ERATT;
10457 /* Check the need for handling ERATT in interrupt handler */
10458 if (ha_copy & HA_ERATT) {
10459 if (phba->hba_flag & HBA_ERATT_HANDLED)
10460 /* ERATT polling has handled ERATT */
10461 ha_copy &= ~HA_ERATT;
10462 else
10463 /* Indicate interrupt handler handles ERATT */
10464 phba->hba_flag |= HBA_ERATT_HANDLED;
10465 }
James Smarta257bf92009-04-06 18:48:10 -040010466
10467 /*
10468 * If there is deferred error attention, do not check for any
10469 * interrupt.
10470 */
10471 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010472 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010473 return IRQ_NONE;
10474 }
10475
James Smart93996272008-08-24 21:50:30 -040010476 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010477 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10478 goto unplug_error;
10479
James Smarta747c9c2009-11-18 15:41:10 -050010480 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10481 HC_LAINT_ENA | HC_ERINT_ENA),
10482 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010483 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10484 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010485 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010486 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010487 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010488 } else
10489 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010490
dea31012005-04-17 16:05:31 -050010491 work_ha_copy = ha_copy & phba->work_ha_mask;
10492
James Smart93996272008-08-24 21:50:30 -040010493 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010494 if (work_ha_copy & HA_LATT) {
10495 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10496 /*
10497 * Turn off Link Attention interrupts
10498 * until CLEAR_LA done
10499 */
James Smart5b75da22008-12-04 22:39:35 -050010500 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010501 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010502 if (lpfc_readl(phba->HCregaddr, &control))
10503 goto unplug_error;
dea31012005-04-17 16:05:31 -050010504 control &= ~HC_LAINT_ENA;
10505 writel(control, phba->HCregaddr);
10506 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010507 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010508 }
10509 else
10510 work_ha_copy &= ~HA_LATT;
10511 }
10512
James Smart93996272008-08-24 21:50:30 -040010513 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010514 /*
10515 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10516 * the only slow ring.
10517 */
10518 status = (work_ha_copy &
10519 (HA_RXMASK << (4*LPFC_ELS_RING)));
10520 status >>= (4*LPFC_ELS_RING);
10521 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010522 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010523 if (lpfc_readl(phba->HCregaddr, &control))
10524 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010525
10526 lpfc_debugfs_slow_ring_trc(phba,
10527 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10528 control, status,
10529 (uint32_t)phba->sli.slistat.sli_intr);
10530
James Smart858c9f62007-06-17 19:56:39 -050010531 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010532 lpfc_debugfs_slow_ring_trc(phba,
10533 "ISR Disable ring:"
10534 "pwork:x%x hawork:x%x wait:x%x",
10535 phba->work_ha, work_ha_copy,
10536 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010537 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010538
James Smart858c9f62007-06-17 19:56:39 -050010539 control &=
10540 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010541 writel(control, phba->HCregaddr);
10542 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010543 }
James Smarta58cbd52007-08-02 11:09:43 -040010544 else {
10545 lpfc_debugfs_slow_ring_trc(phba,
10546 "ISR slow ring: pwork:"
10547 "x%x hawork:x%x wait:x%x",
10548 phba->work_ha, work_ha_copy,
10549 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010550 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010551 }
James Smart5b75da22008-12-04 22:39:35 -050010552 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010553 }
10554 }
James Smart5b75da22008-12-04 22:39:35 -050010555 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010556 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010557 if (lpfc_sli_read_hs(phba))
10558 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010559 /*
10560 * Check if there is a deferred error condition
10561 * is active
10562 */
10563 if ((HS_FFER1 & phba->work_hs) &&
10564 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010565 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10566 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010567 phba->hba_flag |= DEFER_ERATT;
10568 /* Clear all interrupt enable conditions */
10569 writel(0, phba->HCregaddr);
10570 readl(phba->HCregaddr);
10571 }
10572 }
10573
James Smart93996272008-08-24 21:50:30 -040010574 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010575 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010576 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010577 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010578 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010579
10580 /* First check out the status word */
10581 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10582 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010583 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010584 /*
10585 * Stray Mailbox Interrupt, mbxCommand <cmd>
10586 * mbxStatus <status>
10587 */
James Smart09372822008-01-11 01:52:54 -050010588 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010589 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010590 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010591 "Interrupt mbxCommand x%x "
10592 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010593 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010594 pmbox->mbxCommand,
10595 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010596 /* clear mailbox attention bit */
10597 work_ha_copy &= ~HA_MBATT;
10598 } else {
James Smart97eab632008-04-07 10:16:05 -040010599 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010600 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010601 phba->last_completion_time = jiffies;
10602 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010603 if (pmb->mbox_cmpl) {
10604 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10605 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010606 if (pmb->out_ext_byte_len &&
10607 pmb->context2)
10608 lpfc_sli_pcimem_bcopy(
10609 phba->mbox_ext,
10610 pmb->context2,
10611 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010612 }
James Smart09372822008-01-11 01:52:54 -050010613 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10614 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10615
10616 lpfc_debugfs_disc_trc(vport,
10617 LPFC_DISC_TRC_MBOX_VPORT,
10618 "MBOX dflt rpi: : "
10619 "status:x%x rpi:x%x",
10620 (uint32_t)pmbox->mbxStatus,
10621 pmbox->un.varWords[0], 0);
10622
10623 if (!pmbox->mbxStatus) {
10624 mp = (struct lpfc_dmabuf *)
10625 (pmb->context1);
10626 ndlp = (struct lpfc_nodelist *)
10627 pmb->context2;
10628
10629 /* Reg_LOGIN of dflt RPI was
10630 * successful. new lets get
10631 * rid of the RPI using the
10632 * same mbox buffer.
10633 */
10634 lpfc_unreg_login(phba,
10635 vport->vpi,
10636 pmbox->un.varWords[0],
10637 pmb);
10638 pmb->mbox_cmpl =
10639 lpfc_mbx_cmpl_dflt_rpi;
10640 pmb->context1 = mp;
10641 pmb->context2 = ndlp;
10642 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010643 rc = lpfc_sli_issue_mbox(phba,
10644 pmb,
10645 MBX_NOWAIT);
10646 if (rc != MBX_BUSY)
10647 lpfc_printf_log(phba,
10648 KERN_ERR,
10649 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010650 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010651 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010652 if (rc != MBX_NOT_FINISHED)
10653 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010654 }
10655 }
James Smart5b75da22008-12-04 22:39:35 -050010656 spin_lock_irqsave(
10657 &phba->pport->work_port_lock,
10658 iflag);
James Smart09372822008-01-11 01:52:54 -050010659 phba->pport->work_port_events &=
10660 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010661 spin_unlock_irqrestore(
10662 &phba->pport->work_port_lock,
10663 iflag);
James Smart09372822008-01-11 01:52:54 -050010664 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010665 }
James Smart97eab632008-04-07 10:16:05 -040010666 } else
James Smart5b75da22008-12-04 22:39:35 -050010667 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010668
James Smart92d7f7b2007-06-17 19:56:38 -050010669 if ((work_ha_copy & HA_MBATT) &&
10670 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010671send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010672 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010673 do {
10674 rc = lpfc_sli_issue_mbox(phba, NULL,
10675 MBX_NOWAIT);
10676 } while (rc == MBX_NOT_FINISHED);
10677 if (rc != MBX_SUCCESS)
10678 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10679 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010680 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010681 }
10682
James Smart5b75da22008-12-04 22:39:35 -050010683 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010684 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010685 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010686 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010687 }
James Smart93996272008-08-24 21:50:30 -040010688 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010689unplug_error:
10690 spin_unlock_irqrestore(&phba->hbalock, iflag);
10691 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010692
James Smart3772a992009-05-22 14:50:54 -040010693} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010694
10695/**
James Smart3772a992009-05-22 14:50:54 -040010696 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010697 * @irq: Interrupt number.
10698 * @dev_id: The device context pointer.
10699 *
10700 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010701 * service routine when device with SLI-3 interface spec is enabled with
10702 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10703 * ring event in the HBA. However, when the device is enabled with either
10704 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10705 * device-level interrupt handler. When the PCI slot is in error recovery
10706 * or the HBA is undergoing initialization, the interrupt handler will not
10707 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10708 * the intrrupt context. This function is called without any lock held.
10709 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010710 *
10711 * This function returns IRQ_HANDLED when interrupt is handled else it
10712 * returns IRQ_NONE.
10713 **/
10714irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010715lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010716{
10717 struct lpfc_hba *phba;
10718 uint32_t ha_copy;
10719 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010720 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010721
10722 /* Get the driver's phba structure from the dev_id and
10723 * assume the HBA is not interrupting.
10724 */
10725 phba = (struct lpfc_hba *) dev_id;
10726
10727 if (unlikely(!phba))
10728 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010729
10730 /*
James Smart93996272008-08-24 21:50:30 -040010731 * Stuff needs to be attented to when this function is invoked as an
10732 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010733 */
James Smart93996272008-08-24 21:50:30 -040010734 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010735 /* Check device state for handling interrupt */
10736 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010737 return IRQ_NONE;
10738 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010739 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10740 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010741 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010742 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010743 /*
10744 * If there is deferred error attention, do not check for
10745 * any interrupt.
10746 */
10747 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010748 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010749 return IRQ_NONE;
10750 }
James Smart93996272008-08-24 21:50:30 -040010751 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10752 phba->HAregaddr);
10753 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010754 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010755 } else
10756 ha_copy = phba->ha_copy;
10757
10758 /*
10759 * Process all events on FCP ring. Take the optimized path for FCP IO.
10760 */
10761 ha_copy &= ~(phba->work_ha_mask);
10762
10763 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010764 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010765 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010766 lpfc_sli_handle_fast_ring_event(phba,
10767 &phba->sli.ring[LPFC_FCP_RING],
10768 status);
James Smarta4bc3372006-12-02 13:34:16 -050010769
10770 if (phba->cfg_multi_ring_support == 2) {
10771 /*
James Smart93996272008-08-24 21:50:30 -040010772 * Process all events on extra ring. Take the optimized path
10773 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010774 */
James Smart93996272008-08-24 21:50:30 -040010775 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010776 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010777 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010778 lpfc_sli_handle_fast_ring_event(phba,
10779 &phba->sli.ring[LPFC_EXTRA_RING],
10780 status);
10781 }
10782 }
dea31012005-04-17 16:05:31 -050010783 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010784} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010785
James Smart93996272008-08-24 21:50:30 -040010786/**
James Smart3772a992009-05-22 14:50:54 -040010787 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010788 * @irq: Interrupt number.
10789 * @dev_id: The device context pointer.
10790 *
James Smart3772a992009-05-22 14:50:54 -040010791 * This function is the HBA device-level interrupt handler to device with
10792 * SLI-3 interface spec, called from the PCI layer when either MSI or
10793 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10794 * requires driver attention. This function invokes the slow-path interrupt
10795 * attention handling function and fast-path interrupt attention handling
10796 * function in turn to process the relevant HBA attention events. This
10797 * function is called without any lock held. It gets the hbalock to access
10798 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010799 *
10800 * This function returns IRQ_HANDLED when interrupt is handled, else it
10801 * returns IRQ_NONE.
10802 **/
10803irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010804lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010805{
10806 struct lpfc_hba *phba;
10807 irqreturn_t sp_irq_rc, fp_irq_rc;
10808 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010809 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010810
10811 /*
10812 * Get the driver's phba structure from the dev_id and
10813 * assume the HBA is not interrupting.
10814 */
10815 phba = (struct lpfc_hba *) dev_id;
10816
10817 if (unlikely(!phba))
10818 return IRQ_NONE;
10819
James Smart3772a992009-05-22 14:50:54 -040010820 /* Check device state for handling interrupt */
10821 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010822 return IRQ_NONE;
10823
10824 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010825 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10826 spin_unlock(&phba->hbalock);
10827 return IRQ_HANDLED;
10828 }
10829
James Smart93996272008-08-24 21:50:30 -040010830 if (unlikely(!phba->ha_copy)) {
10831 spin_unlock(&phba->hbalock);
10832 return IRQ_NONE;
10833 } else if (phba->ha_copy & HA_ERATT) {
10834 if (phba->hba_flag & HBA_ERATT_HANDLED)
10835 /* ERATT polling has handled ERATT */
10836 phba->ha_copy &= ~HA_ERATT;
10837 else
10838 /* Indicate interrupt handler handles ERATT */
10839 phba->hba_flag |= HBA_ERATT_HANDLED;
10840 }
10841
James Smarta257bf92009-04-06 18:48:10 -040010842 /*
10843 * If there is deferred error attention, do not check for any interrupt.
10844 */
10845 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010846 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010847 return IRQ_NONE;
10848 }
10849
James Smart93996272008-08-24 21:50:30 -040010850 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010851 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10852 spin_unlock(&phba->hbalock);
10853 return IRQ_HANDLED;
10854 }
James Smarta747c9c2009-11-18 15:41:10 -050010855 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10856 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10857 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010858 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010859 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010860 readl(phba->HAregaddr); /* flush */
10861 spin_unlock(&phba->hbalock);
10862
10863 /*
10864 * Invokes slow-path host attention interrupt handling as appropriate.
10865 */
10866
10867 /* status of events with mailbox and link attention */
10868 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10869
10870 /* status of events with ELS ring */
10871 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10872 status2 >>= (4*LPFC_ELS_RING);
10873
10874 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010875 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010876 else
10877 sp_irq_rc = IRQ_NONE;
10878
10879 /*
10880 * Invoke fast-path host attention interrupt handling as appropriate.
10881 */
10882
10883 /* status of events with FCP ring */
10884 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10885 status1 >>= (4*LPFC_FCP_RING);
10886
10887 /* status of events with extra ring */
10888 if (phba->cfg_multi_ring_support == 2) {
10889 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10890 status2 >>= (4*LPFC_EXTRA_RING);
10891 } else
10892 status2 = 0;
10893
10894 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010895 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010896 else
10897 fp_irq_rc = IRQ_NONE;
10898
10899 /* Return device-level interrupt handling status */
10900 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010901} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010902
10903/**
10904 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10905 * @phba: pointer to lpfc hba data structure.
10906 *
10907 * This routine is invoked by the worker thread to process all the pending
10908 * SLI4 FCP abort XRI events.
10909 **/
10910void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10911{
10912 struct lpfc_cq_event *cq_event;
10913
10914 /* First, declare the fcp xri abort event has been handled */
10915 spin_lock_irq(&phba->hbalock);
10916 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10917 spin_unlock_irq(&phba->hbalock);
10918 /* Now, handle all the fcp xri abort events */
10919 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10920 /* Get the first event from the head of the event queue */
10921 spin_lock_irq(&phba->hbalock);
10922 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10923 cq_event, struct lpfc_cq_event, list);
10924 spin_unlock_irq(&phba->hbalock);
10925 /* Notify aborted XRI for FCP work queue */
10926 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10927 /* Free the event processed back to the free pool */
10928 lpfc_sli4_cq_event_release(phba, cq_event);
10929 }
10930}
10931
10932/**
10933 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10934 * @phba: pointer to lpfc hba data structure.
10935 *
10936 * This routine is invoked by the worker thread to process all the pending
10937 * SLI4 els abort xri events.
10938 **/
10939void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10940{
10941 struct lpfc_cq_event *cq_event;
10942
10943 /* First, declare the els xri abort event has been handled */
10944 spin_lock_irq(&phba->hbalock);
10945 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10946 spin_unlock_irq(&phba->hbalock);
10947 /* Now, handle all the els xri abort events */
10948 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10949 /* Get the first event from the head of the event queue */
10950 spin_lock_irq(&phba->hbalock);
10951 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10952 cq_event, struct lpfc_cq_event, list);
10953 spin_unlock_irq(&phba->hbalock);
10954 /* Notify aborted XRI for ELS work queue */
10955 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10956 /* Free the event processed back to the free pool */
10957 lpfc_sli4_cq_event_release(phba, cq_event);
10958 }
10959}
10960
James Smart341af102010-01-26 23:07:37 -050010961/**
10962 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10963 * @phba: pointer to lpfc hba data structure
10964 * @pIocbIn: pointer to the rspiocbq
10965 * @pIocbOut: pointer to the cmdiocbq
10966 * @wcqe: pointer to the complete wcqe
10967 *
10968 * This routine transfers the fields of a command iocbq to a response iocbq
10969 * by copying all the IOCB fields from command iocbq and transferring the
10970 * completion status information from the complete wcqe.
10971 **/
James Smart4f774512009-05-22 14:52:35 -040010972static void
James Smart341af102010-01-26 23:07:37 -050010973lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10974 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010975 struct lpfc_iocbq *pIocbOut,
10976 struct lpfc_wcqe_complete *wcqe)
10977{
James Smart341af102010-01-26 23:07:37 -050010978 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010979 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010980 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10981
10982 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10983 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010984 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010985 status = bf_get(lpfc_wcqe_c_status, wcqe);
10986 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010987 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10988 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10989 pIocbIn->iocb.un.fcpi.fcpi_parm =
10990 pIocbOut->iocb.un.fcpi.fcpi_parm -
10991 wcqe->total_data_placed;
10992 else
10993 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010994 else {
James Smart4f774512009-05-22 14:52:35 -040010995 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010996 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10997 }
James Smart341af102010-01-26 23:07:37 -050010998
James Smartacd68592012-01-18 16:25:09 -050010999 /* Convert BG errors for completion status */
11000 if (status == CQE_STATUS_DI_ERROR) {
11001 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11002
11003 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11004 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11005 else
11006 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11007
11008 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11009 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11010 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11011 BGS_GUARD_ERR_MASK;
11012 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11013 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11014 BGS_APPTAG_ERR_MASK;
11015 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11016 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11017 BGS_REFTAG_ERR_MASK;
11018
11019 /* Check to see if there was any good data before the error */
11020 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11021 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11022 BGS_HI_WATER_MARK_PRESENT_MASK;
11023 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11024 wcqe->total_data_placed;
11025 }
11026
11027 /*
11028 * Set ALL the error bits to indicate we don't know what
11029 * type of error it is.
11030 */
11031 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11032 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11033 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11034 BGS_GUARD_ERR_MASK);
11035 }
11036
James Smart341af102010-01-26 23:07:37 -050011037 /* Pick up HBA exchange busy condition */
11038 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11039 spin_lock_irqsave(&phba->hbalock, iflags);
11040 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11041 spin_unlock_irqrestore(&phba->hbalock, iflags);
11042 }
James Smart4f774512009-05-22 14:52:35 -040011043}
11044
11045/**
James Smart45ed1192009-10-02 15:17:02 -040011046 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11047 * @phba: Pointer to HBA context object.
11048 * @wcqe: Pointer to work-queue completion queue entry.
11049 *
11050 * This routine handles an ELS work-queue completion event and construct
11051 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11052 * discovery engine to handle.
11053 *
11054 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11055 **/
11056static struct lpfc_iocbq *
11057lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11058 struct lpfc_iocbq *irspiocbq)
11059{
11060 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11061 struct lpfc_iocbq *cmdiocbq;
11062 struct lpfc_wcqe_complete *wcqe;
11063 unsigned long iflags;
11064
11065 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011066 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011067 pring->stats.iocb_event++;
11068 /* Look up the ELS command IOCB and create pseudo response IOCB */
11069 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11070 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011071 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011072
11073 if (unlikely(!cmdiocbq)) {
11074 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11075 "0386 ELS complete with no corresponding "
11076 "cmdiocb: iotag (%d)\n",
11077 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11078 lpfc_sli_release_iocbq(phba, irspiocbq);
11079 return NULL;
11080 }
11081
11082 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011083 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011084
11085 return irspiocbq;
11086}
11087
11088/**
James Smart04c68492009-05-22 14:52:52 -040011089 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11090 * @phba: Pointer to HBA context object.
11091 * @cqe: Pointer to mailbox completion queue entry.
11092 *
11093 * This routine process a mailbox completion queue entry with asynchrous
11094 * event.
11095 *
11096 * Return: true if work posted to worker thread, otherwise false.
11097 **/
11098static bool
11099lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11100{
11101 struct lpfc_cq_event *cq_event;
11102 unsigned long iflags;
11103
11104 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11105 "0392 Async Event: word0:x%x, word1:x%x, "
11106 "word2:x%x, word3:x%x\n", mcqe->word0,
11107 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11108
11109 /* Allocate a new internal CQ_EVENT entry */
11110 cq_event = lpfc_sli4_cq_event_alloc(phba);
11111 if (!cq_event) {
11112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11113 "0394 Failed to allocate CQ_EVENT entry\n");
11114 return false;
11115 }
11116
11117 /* Move the CQE into an asynchronous event entry */
11118 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11119 spin_lock_irqsave(&phba->hbalock, iflags);
11120 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11121 /* Set the async event flag */
11122 phba->hba_flag |= ASYNC_EVENT;
11123 spin_unlock_irqrestore(&phba->hbalock, iflags);
11124
11125 return true;
11126}
11127
11128/**
11129 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11130 * @phba: Pointer to HBA context object.
11131 * @cqe: Pointer to mailbox completion queue entry.
11132 *
11133 * This routine process a mailbox completion queue entry with mailbox
11134 * completion event.
11135 *
11136 * Return: true if work posted to worker thread, otherwise false.
11137 **/
11138static bool
11139lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11140{
11141 uint32_t mcqe_status;
11142 MAILBOX_t *mbox, *pmbox;
11143 struct lpfc_mqe *mqe;
11144 struct lpfc_vport *vport;
11145 struct lpfc_nodelist *ndlp;
11146 struct lpfc_dmabuf *mp;
11147 unsigned long iflags;
11148 LPFC_MBOXQ_t *pmb;
11149 bool workposted = false;
11150 int rc;
11151
11152 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11153 if (!bf_get(lpfc_trailer_completed, mcqe))
11154 goto out_no_mqe_complete;
11155
11156 /* Get the reference to the active mbox command */
11157 spin_lock_irqsave(&phba->hbalock, iflags);
11158 pmb = phba->sli.mbox_active;
11159 if (unlikely(!pmb)) {
11160 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11161 "1832 No pending MBOX command to handle\n");
11162 spin_unlock_irqrestore(&phba->hbalock, iflags);
11163 goto out_no_mqe_complete;
11164 }
11165 spin_unlock_irqrestore(&phba->hbalock, iflags);
11166 mqe = &pmb->u.mqe;
11167 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11168 mbox = phba->mbox;
11169 vport = pmb->vport;
11170
11171 /* Reset heartbeat timer */
11172 phba->last_completion_time = jiffies;
11173 del_timer(&phba->sli.mbox_tmo);
11174
11175 /* Move mbox data to caller's mailbox region, do endian swapping */
11176 if (pmb->mbox_cmpl && mbox)
11177 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011178
James Smart73d91e52011-10-10 21:32:10 -040011179 /*
11180 * For mcqe errors, conditionally move a modified error code to
11181 * the mbox so that the error will not be missed.
11182 */
11183 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11184 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11185 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11186 bf_set(lpfc_mqe_status, mqe,
11187 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11188 }
James Smart04c68492009-05-22 14:52:52 -040011189 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11190 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11191 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11192 "MBOX dflt rpi: status:x%x rpi:x%x",
11193 mcqe_status,
11194 pmbox->un.varWords[0], 0);
11195 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11196 mp = (struct lpfc_dmabuf *)(pmb->context1);
11197 ndlp = (struct lpfc_nodelist *)pmb->context2;
11198 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11199 * RID of the PPI using the same mbox buffer.
11200 */
11201 lpfc_unreg_login(phba, vport->vpi,
11202 pmbox->un.varWords[0], pmb);
11203 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11204 pmb->context1 = mp;
11205 pmb->context2 = ndlp;
11206 pmb->vport = vport;
11207 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11208 if (rc != MBX_BUSY)
11209 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11210 LOG_SLI, "0385 rc should "
11211 "have been MBX_BUSY\n");
11212 if (rc != MBX_NOT_FINISHED)
11213 goto send_current_mbox;
11214 }
11215 }
11216 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11217 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11218 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11219
11220 /* There is mailbox completion work to do */
11221 spin_lock_irqsave(&phba->hbalock, iflags);
11222 __lpfc_mbox_cmpl_put(phba, pmb);
11223 phba->work_ha |= HA_MBATT;
11224 spin_unlock_irqrestore(&phba->hbalock, iflags);
11225 workposted = true;
11226
11227send_current_mbox:
11228 spin_lock_irqsave(&phba->hbalock, iflags);
11229 /* Release the mailbox command posting token */
11230 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11231 /* Setting active mailbox pointer need to be in sync to flag clear */
11232 phba->sli.mbox_active = NULL;
11233 spin_unlock_irqrestore(&phba->hbalock, iflags);
11234 /* Wake up worker thread to post the next pending mailbox command */
11235 lpfc_worker_wake_up(phba);
11236out_no_mqe_complete:
11237 if (bf_get(lpfc_trailer_consumed, mcqe))
11238 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11239 return workposted;
11240}
11241
11242/**
11243 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11244 * @phba: Pointer to HBA context object.
11245 * @cqe: Pointer to mailbox completion queue entry.
11246 *
11247 * This routine process a mailbox completion queue entry, it invokes the
11248 * proper mailbox complete handling or asynchrous event handling routine
11249 * according to the MCQE's async bit.
11250 *
11251 * Return: true if work posted to worker thread, otherwise false.
11252 **/
11253static bool
11254lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11255{
11256 struct lpfc_mcqe mcqe;
11257 bool workposted;
11258
11259 /* Copy the mailbox MCQE and convert endian order as needed */
11260 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11261
11262 /* Invoke the proper event handling routine */
11263 if (!bf_get(lpfc_trailer_async, &mcqe))
11264 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11265 else
11266 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11267 return workposted;
11268}
11269
11270/**
James Smart4f774512009-05-22 14:52:35 -040011271 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11272 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011273 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011274 * @wcqe: Pointer to work-queue completion queue entry.
11275 *
11276 * This routine handles an ELS work-queue completion event.
11277 *
11278 * Return: true if work posted to worker thread, otherwise false.
11279 **/
11280static bool
James Smart2a76a282012-08-03 12:35:54 -040011281lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011282 struct lpfc_wcqe_complete *wcqe)
11283{
James Smart4f774512009-05-22 14:52:35 -040011284 struct lpfc_iocbq *irspiocbq;
11285 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011286 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011287
James Smart45ed1192009-10-02 15:17:02 -040011288 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011289 irspiocbq = lpfc_sli_get_iocbq(phba);
11290 if (!irspiocbq) {
11291 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011292 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11293 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11294 pring->txq_cnt, phba->iocb_cnt,
11295 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11296 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011297 return false;
James Smart4f774512009-05-22 14:52:35 -040011298 }
James Smart4f774512009-05-22 14:52:35 -040011299
James Smart45ed1192009-10-02 15:17:02 -040011300 /* Save off the slow-path queue event for work thread to process */
11301 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011302 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011303 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011304 &phba->sli4_hba.sp_queue_event);
11305 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011306 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011307
James Smart45ed1192009-10-02 15:17:02 -040011308 return true;
James Smart4f774512009-05-22 14:52:35 -040011309}
11310
11311/**
11312 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11313 * @phba: Pointer to HBA context object.
11314 * @wcqe: Pointer to work-queue completion queue entry.
11315 *
11316 * This routine handles slow-path WQ entry comsumed event by invoking the
11317 * proper WQ release routine to the slow-path WQ.
11318 **/
11319static void
11320lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11321 struct lpfc_wcqe_release *wcqe)
11322{
James Smart2e90f4b2011-12-13 13:22:37 -050011323 /* sanity check on queue memory */
11324 if (unlikely(!phba->sli4_hba.els_wq))
11325 return;
James Smart4f774512009-05-22 14:52:35 -040011326 /* Check for the slow-path ELS work queue */
11327 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11328 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11329 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11330 else
11331 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11332 "2579 Slow-path wqe consume event carries "
11333 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11334 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11335 phba->sli4_hba.els_wq->queue_id);
11336}
11337
11338/**
11339 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11340 * @phba: Pointer to HBA context object.
11341 * @cq: Pointer to a WQ completion queue.
11342 * @wcqe: Pointer to work-queue completion queue entry.
11343 *
11344 * This routine handles an XRI abort event.
11345 *
11346 * Return: true if work posted to worker thread, otherwise false.
11347 **/
11348static bool
11349lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11350 struct lpfc_queue *cq,
11351 struct sli4_wcqe_xri_aborted *wcqe)
11352{
11353 bool workposted = false;
11354 struct lpfc_cq_event *cq_event;
11355 unsigned long iflags;
11356
11357 /* Allocate a new internal CQ_EVENT entry */
11358 cq_event = lpfc_sli4_cq_event_alloc(phba);
11359 if (!cq_event) {
11360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11361 "0602 Failed to allocate CQ_EVENT entry\n");
11362 return false;
11363 }
11364
11365 /* Move the CQE into the proper xri abort event list */
11366 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11367 switch (cq->subtype) {
11368 case LPFC_FCP:
11369 spin_lock_irqsave(&phba->hbalock, iflags);
11370 list_add_tail(&cq_event->list,
11371 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11372 /* Set the fcp xri abort event flag */
11373 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11374 spin_unlock_irqrestore(&phba->hbalock, iflags);
11375 workposted = true;
11376 break;
11377 case LPFC_ELS:
11378 spin_lock_irqsave(&phba->hbalock, iflags);
11379 list_add_tail(&cq_event->list,
11380 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11381 /* Set the els xri abort event flag */
11382 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11383 spin_unlock_irqrestore(&phba->hbalock, iflags);
11384 workposted = true;
11385 break;
11386 default:
11387 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11388 "0603 Invalid work queue CQE subtype (x%x)\n",
11389 cq->subtype);
11390 workposted = false;
11391 break;
11392 }
11393 return workposted;
11394}
11395
11396/**
James Smart4d9ab992009-10-02 15:16:39 -040011397 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011398 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011399 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011400 *
James Smart4d9ab992009-10-02 15:16:39 -040011401 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011402 *
11403 * Return: true if work posted to worker thread, otherwise false.
11404 **/
11405static bool
James Smart4d9ab992009-10-02 15:16:39 -040011406lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11407{
11408 bool workposted = false;
11409 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11410 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11411 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011412 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011413 unsigned long iflags;
11414
James Smart2e90f4b2011-12-13 13:22:37 -050011415 /* sanity check on queue memory */
11416 if (unlikely(!hrq) || unlikely(!drq))
11417 return workposted;
11418
James Smart7851fe22011-07-22 18:36:52 -040011419 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11420 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11421 else
11422 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11423 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011424 goto out;
11425
11426 status = bf_get(lpfc_rcqe_status, rcqe);
11427 switch (status) {
11428 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11430 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011431 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011432 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011433 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011434 spin_lock_irqsave(&phba->hbalock, iflags);
11435 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11436 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011437 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011438 spin_unlock_irqrestore(&phba->hbalock, iflags);
11439 goto out;
11440 }
James Smartb84daac2012-08-03 12:35:13 -040011441 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011442 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11443 /* save off the frame for the word thread to process */
11444 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011445 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011446 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011447 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011448 spin_unlock_irqrestore(&phba->hbalock, iflags);
11449 workposted = true;
11450 break;
11451 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11452 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011453 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011454 /* Post more buffers if possible */
11455 spin_lock_irqsave(&phba->hbalock, iflags);
11456 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11457 spin_unlock_irqrestore(&phba->hbalock, iflags);
11458 workposted = true;
11459 break;
11460 }
11461out:
11462 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011463}
11464
11465/**
11466 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11467 * @phba: Pointer to HBA context object.
11468 * @cq: Pointer to the completion queue.
11469 * @wcqe: Pointer to a completion queue entry.
11470 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011471 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011472 * entry.
11473 *
11474 * Return: true if work posted to worker thread, otherwise false.
11475 **/
11476static bool
11477lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011478 struct lpfc_cqe *cqe)
11479{
James Smart45ed1192009-10-02 15:17:02 -040011480 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011481 bool workposted = false;
11482
11483 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011484 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011485
11486 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011487 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011488 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011489 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011490 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011491 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011492 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011493 break;
11494 case CQE_CODE_RELEASE_WQE:
11495 /* Process the WQ release event */
11496 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011497 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011498 break;
11499 case CQE_CODE_XRI_ABORTED:
11500 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011501 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011502 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011503 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011504 break;
James Smart4d9ab992009-10-02 15:16:39 -040011505 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011506 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011507 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011508 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011509 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011510 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011511 break;
James Smart4f774512009-05-22 14:52:35 -040011512 default:
11513 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11514 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011515 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011516 break;
11517 }
11518 return workposted;
11519}
11520
11521/**
James Smart4f774512009-05-22 14:52:35 -040011522 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11523 * @phba: Pointer to HBA context object.
11524 * @eqe: Pointer to fast-path event queue entry.
11525 *
11526 * This routine process a event queue entry from the slow-path event queue.
11527 * It will check the MajorCode and MinorCode to determine this is for a
11528 * completion event on a completion queue, if not, an error shall be logged
11529 * and just return. Otherwise, it will get to the corresponding completion
11530 * queue and process all the entries on that completion queue, rearm the
11531 * completion queue, and then return.
11532 *
11533 **/
11534static void
James Smart67d12732012-08-03 12:36:13 -040011535lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11536 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040011537{
James Smart67d12732012-08-03 12:36:13 -040011538 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040011539 struct lpfc_cqe *cqe;
11540 bool workposted = false;
11541 int ecount = 0;
11542 uint16_t cqid;
11543
James Smart4f774512009-05-22 14:52:35 -040011544 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011545 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011546
James Smart4f774512009-05-22 14:52:35 -040011547 list_for_each_entry(childq, &speq->child_list, list) {
11548 if (childq->queue_id == cqid) {
11549 cq = childq;
11550 break;
11551 }
11552 }
11553 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011554 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11556 "0365 Slow-path CQ identifier "
11557 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011558 return;
11559 }
11560
11561 /* Process all the entries to the CQ */
11562 switch (cq->type) {
11563 case LPFC_MCQ:
11564 while ((cqe = lpfc_sli4_cq_get(cq))) {
11565 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011566 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011567 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011568 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011569 }
11570 break;
11571 case LPFC_WCQ:
11572 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011573 if (cq->subtype == LPFC_FCP)
11574 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11575 cqe);
11576 else
11577 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11578 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011579 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011580 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11581 }
James Smartb84daac2012-08-03 12:35:13 -040011582
11583 /* Track the max number of CQEs processed in 1 EQ */
11584 if (ecount > cq->CQ_max_cqe)
11585 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011586 break;
11587 default:
11588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11589 "0370 Invalid completion queue type (%d)\n",
11590 cq->type);
11591 return;
11592 }
11593
11594 /* Catch the no cq entry condition, log an error */
11595 if (unlikely(ecount == 0))
11596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11597 "0371 No entry from the CQ: identifier "
11598 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11599
11600 /* In any case, flash and re-arm the RCQ */
11601 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11602
11603 /* wake up worker thread if there are works to be done */
11604 if (workposted)
11605 lpfc_worker_wake_up(phba);
11606}
11607
11608/**
11609 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011610 * @phba: Pointer to HBA context object.
11611 * @cq: Pointer to associated CQ
11612 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011613 *
11614 * This routine process a fast-path work queue completion entry from fast-path
11615 * event queue for FCP command response completion.
11616 **/
11617static void
James Smart2a76a282012-08-03 12:35:54 -040011618lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011619 struct lpfc_wcqe_complete *wcqe)
11620{
James Smart2a76a282012-08-03 12:35:54 -040011621 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011622 struct lpfc_iocbq *cmdiocbq;
11623 struct lpfc_iocbq irspiocbq;
11624 unsigned long iflags;
11625
James Smart4f774512009-05-22 14:52:35 -040011626 /* Check for response status */
11627 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11628 /* If resource errors reported from HBA, reduce queue
11629 * depth of the SCSI device.
11630 */
11631 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11632 IOSTAT_LOCAL_REJECT) &&
11633 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11634 phba->lpfc_rampdown_queue_depth(phba);
11635 }
11636 /* Log the error status */
11637 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11638 "0373 FCP complete error: status=x%x, "
11639 "hw_status=x%x, total_data_specified=%d, "
11640 "parameter=x%x, word3=x%x\n",
11641 bf_get(lpfc_wcqe_c_status, wcqe),
11642 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11643 wcqe->total_data_placed, wcqe->parameter,
11644 wcqe->word3);
11645 }
11646
11647 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011648 spin_lock_irqsave(&pring->ring_lock, iflags);
11649 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011650 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11651 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011652 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011653 if (unlikely(!cmdiocbq)) {
11654 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11655 "0374 FCP complete with no corresponding "
11656 "cmdiocb: iotag (%d)\n",
11657 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11658 return;
11659 }
11660 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11661 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11662 "0375 FCP cmdiocb not callback function "
11663 "iotag: (%d)\n",
11664 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11665 return;
11666 }
11667
11668 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011669 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011670
James Smart0f65ff62010-02-26 14:14:23 -050011671 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11672 spin_lock_irqsave(&phba->hbalock, iflags);
11673 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11674 spin_unlock_irqrestore(&phba->hbalock, iflags);
11675 }
11676
James Smart4f774512009-05-22 14:52:35 -040011677 /* Pass the cmd_iocb and the rsp state to the upper layer */
11678 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11679}
11680
11681/**
11682 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11683 * @phba: Pointer to HBA context object.
11684 * @cq: Pointer to completion queue.
11685 * @wcqe: Pointer to work-queue completion queue entry.
11686 *
11687 * This routine handles an fast-path WQ entry comsumed event by invoking the
11688 * proper WQ release routine to the slow-path WQ.
11689 **/
11690static void
11691lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11692 struct lpfc_wcqe_release *wcqe)
11693{
11694 struct lpfc_queue *childwq;
11695 bool wqid_matched = false;
11696 uint16_t fcp_wqid;
11697
11698 /* Check for fast-path FCP work queue release */
11699 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11700 list_for_each_entry(childwq, &cq->child_list, list) {
11701 if (childwq->queue_id == fcp_wqid) {
11702 lpfc_sli4_wq_release(childwq,
11703 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11704 wqid_matched = true;
11705 break;
11706 }
11707 }
11708 /* Report warning log message if no match found */
11709 if (wqid_matched != true)
11710 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11711 "2580 Fast-path wqe consume event carries "
11712 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11713}
11714
11715/**
11716 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11717 * @cq: Pointer to the completion queue.
11718 * @eqe: Pointer to fast-path completion queue entry.
11719 *
11720 * This routine process a fast-path work queue completion entry from fast-path
11721 * event queue for FCP command response completion.
11722 **/
11723static int
11724lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11725 struct lpfc_cqe *cqe)
11726{
11727 struct lpfc_wcqe_release wcqe;
11728 bool workposted = false;
11729
11730 /* Copy the work queue CQE and convert endian order if needed */
11731 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11732
11733 /* Check and process for different type of WCQE and dispatch */
11734 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11735 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011736 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011737 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011738 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011739 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040011740 (struct lpfc_wcqe_complete *)&wcqe);
11741 break;
11742 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011743 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011744 /* Process the WQ release event */
11745 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11746 (struct lpfc_wcqe_release *)&wcqe);
11747 break;
11748 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011749 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011750 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011751 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011752 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11753 (struct sli4_wcqe_xri_aborted *)&wcqe);
11754 break;
11755 default:
11756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11757 "0144 Not a valid WCQE code: x%x\n",
11758 bf_get(lpfc_wcqe_c_code, &wcqe));
11759 break;
11760 }
11761 return workposted;
11762}
11763
11764/**
James Smart67d12732012-08-03 12:36:13 -040011765 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040011766 * @phba: Pointer to HBA context object.
11767 * @eqe: Pointer to fast-path event queue entry.
11768 *
11769 * This routine process a event queue entry from the fast-path event queue.
11770 * It will check the MajorCode and MinorCode to determine this is for a
11771 * completion event on a completion queue, if not, an error shall be logged
11772 * and just return. Otherwise, it will get to the corresponding completion
11773 * queue and process all the entries on the completion queue, rearm the
11774 * completion queue, and then return.
11775 **/
11776static void
James Smart67d12732012-08-03 12:36:13 -040011777lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11778 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040011779{
11780 struct lpfc_queue *cq;
11781 struct lpfc_cqe *cqe;
11782 bool workposted = false;
11783 uint16_t cqid;
11784 int ecount = 0;
11785
James Smartcb5172e2010-03-15 11:25:07 -040011786 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011787 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040011788 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040011789 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011790 bf_get_le32(lpfc_eqe_major_code, eqe),
11791 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011792 return;
11793 }
11794
James Smart67d12732012-08-03 12:36:13 -040011795 /* Get the reference to the corresponding CQ */
11796 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11797
11798 /* Check if this is a Slow path event */
11799 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
11800 lpfc_sli4_sp_handle_eqe(phba, eqe,
11801 phba->sli4_hba.hba_eq[qidx]);
11802 return;
11803 }
11804
James Smart2e90f4b2011-12-13 13:22:37 -050011805 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11806 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11807 "3146 Fast-path completion queues "
11808 "does not exist\n");
11809 return;
11810 }
James Smart67d12732012-08-03 12:36:13 -040011811 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040011812 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011813 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11815 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040011816 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040011817 return;
11818 }
11819
James Smart4f774512009-05-22 14:52:35 -040011820 if (unlikely(cqid != cq->queue_id)) {
11821 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11822 "0368 Miss-matched fast-path completion "
11823 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11824 cqid, cq->queue_id);
11825 return;
11826 }
11827
11828 /* Process all the entries to the CQ */
11829 while ((cqe = lpfc_sli4_cq_get(cq))) {
11830 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011831 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011832 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11833 }
11834
James Smartb84daac2012-08-03 12:35:13 -040011835 /* Track the max number of CQEs processed in 1 EQ */
11836 if (ecount > cq->CQ_max_cqe)
11837 cq->CQ_max_cqe = ecount;
11838
James Smart4f774512009-05-22 14:52:35 -040011839 /* Catch the no cq entry condition */
11840 if (unlikely(ecount == 0))
11841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11842 "0369 No entry from fast-path completion "
11843 "queue fcpcqid=%d\n", cq->queue_id);
11844
11845 /* In any case, flash and re-arm the CQ */
11846 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11847
11848 /* wake up worker thread if there are works to be done */
11849 if (workposted)
11850 lpfc_worker_wake_up(phba);
11851}
11852
11853static void
11854lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11855{
11856 struct lpfc_eqe *eqe;
11857
11858 /* walk all the EQ entries and drop on the floor */
11859 while ((eqe = lpfc_sli4_eq_get(eq)))
11860 ;
11861
11862 /* Clear and re-arm the EQ */
11863 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11864}
11865
11866/**
James Smart67d12732012-08-03 12:36:13 -040011867 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040011868 * @irq: Interrupt number.
11869 * @dev_id: The device context pointer.
11870 *
11871 * This function is directly called from the PCI layer as an interrupt
11872 * service routine when device with SLI-4 interface spec is enabled with
11873 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11874 * ring event in the HBA. However, when the device is enabled with either
11875 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11876 * device-level interrupt handler. When the PCI slot is in error recovery
11877 * or the HBA is undergoing initialization, the interrupt handler will not
11878 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11879 * the intrrupt context. This function is called without any lock held.
11880 * It gets the hbalock to access and update SLI data structures. Note that,
11881 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11882 * equal to that of FCP CQ index.
11883 *
James Smart67d12732012-08-03 12:36:13 -040011884 * The link attention and ELS ring attention events are handled
11885 * by the worker thread. The interrupt handler signals the worker thread
11886 * and returns for these events. This function is called without any lock
11887 * held. It gets the hbalock to access and update SLI data structures.
11888 *
James Smart4f774512009-05-22 14:52:35 -040011889 * This function returns IRQ_HANDLED when interrupt is handled else it
11890 * returns IRQ_NONE.
11891 **/
11892irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040011893lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040011894{
11895 struct lpfc_hba *phba;
11896 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11897 struct lpfc_queue *fpeq;
11898 struct lpfc_eqe *eqe;
11899 unsigned long iflag;
11900 int ecount = 0;
11901 uint32_t fcp_eqidx;
11902
11903 /* Get the driver's phba structure from the dev_id */
11904 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11905 phba = fcp_eq_hdl->phba;
11906 fcp_eqidx = fcp_eq_hdl->idx;
11907
11908 if (unlikely(!phba))
11909 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040011910 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040011911 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011912
11913 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040011914 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011915 if (unlikely(!fpeq))
11916 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011917
James Smartba20c852012-08-03 12:36:52 -040011918 if (lpfc_fcp_look_ahead) {
11919 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
11920 lpfc_sli4_eq_clr_intr(fpeq);
11921 else {
11922 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11923 return IRQ_NONE;
11924 }
11925 }
11926
James Smart4f774512009-05-22 14:52:35 -040011927 /* Check device state for handling interrupt */
11928 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011929 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011930 /* Check again for link_state with lock held */
11931 spin_lock_irqsave(&phba->hbalock, iflag);
11932 if (phba->link_state < LPFC_LINK_DOWN)
11933 /* Flush, clear interrupt, and rearm the EQ */
11934 lpfc_sli4_eq_flush(phba, fpeq);
11935 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040011936 if (lpfc_fcp_look_ahead)
11937 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040011938 return IRQ_NONE;
11939 }
11940
11941 /*
11942 * Process all the event on FCP fast-path EQ
11943 */
11944 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smart67d12732012-08-03 12:36:13 -040011945 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011946 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011947 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011948 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011949 }
11950
James Smartb84daac2012-08-03 12:35:13 -040011951 /* Track the max number of EQEs processed in 1 intr */
11952 if (ecount > fpeq->EQ_max_eqe)
11953 fpeq->EQ_max_eqe = ecount;
11954
James Smart4f774512009-05-22 14:52:35 -040011955 /* Always clear and re-arm the fast-path EQ */
11956 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11957
11958 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011959 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040011960
11961 if (lpfc_fcp_look_ahead) {
11962 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11963 return IRQ_NONE;
11964 }
11965
James Smart4f774512009-05-22 14:52:35 -040011966 if (phba->intr_type == MSIX)
11967 /* MSI-X treated interrupt served as no EQ share INT */
11968 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11969 "0358 MSI-X interrupt with no EQE\n");
11970 else
11971 /* Non MSI-X treated on interrupt as EQ share INT */
11972 return IRQ_NONE;
11973 }
11974
James Smartba20c852012-08-03 12:36:52 -040011975 if (lpfc_fcp_look_ahead)
11976 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040011977 return IRQ_HANDLED;
11978} /* lpfc_sli4_fp_intr_handler */
11979
11980/**
11981 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11982 * @irq: Interrupt number.
11983 * @dev_id: The device context pointer.
11984 *
11985 * This function is the device-level interrupt handler to device with SLI-4
11986 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11987 * interrupt mode is enabled and there is an event in the HBA which requires
11988 * driver attention. This function invokes the slow-path interrupt attention
11989 * handling function and fast-path interrupt attention handling function in
11990 * turn to process the relevant HBA attention events. This function is called
11991 * without any lock held. It gets the hbalock to access and update SLI data
11992 * structures.
11993 *
11994 * This function returns IRQ_HANDLED when interrupt is handled, else it
11995 * returns IRQ_NONE.
11996 **/
11997irqreturn_t
11998lpfc_sli4_intr_handler(int irq, void *dev_id)
11999{
12000 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012001 irqreturn_t hba_irq_rc;
12002 bool hba_handled = false;
James Smart4f774512009-05-22 14:52:35 -040012003 uint32_t fcp_eqidx;
12004
12005 /* Get the driver's phba structure from the dev_id */
12006 phba = (struct lpfc_hba *)dev_id;
12007
12008 if (unlikely(!phba))
12009 return IRQ_NONE;
12010
12011 /*
James Smart4f774512009-05-22 14:52:35 -040012012 * Invoke fast-path host attention interrupt handling as appropriate.
12013 */
James Smart67d12732012-08-03 12:36:13 -040012014 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12015 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012016 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012017 if (hba_irq_rc == IRQ_HANDLED)
12018 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012019 }
12020
James Smart67d12732012-08-03 12:36:13 -040012021 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012022} /* lpfc_sli4_intr_handler */
12023
12024/**
12025 * lpfc_sli4_queue_free - free a queue structure and associated memory
12026 * @queue: The queue structure to free.
12027 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012028 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012029 * the host resident queue. This function must be called after destroying the
12030 * queue on the HBA.
12031 **/
12032void
12033lpfc_sli4_queue_free(struct lpfc_queue *queue)
12034{
12035 struct lpfc_dmabuf *dmabuf;
12036
12037 if (!queue)
12038 return;
12039
12040 while (!list_empty(&queue->page_list)) {
12041 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12042 list);
James Smart49198b32010-04-06 15:04:33 -040012043 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012044 dmabuf->virt, dmabuf->phys);
12045 kfree(dmabuf);
12046 }
12047 kfree(queue);
12048 return;
12049}
12050
12051/**
12052 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12053 * @phba: The HBA that this queue is being created on.
12054 * @entry_size: The size of each queue entry for this queue.
12055 * @entry count: The number of entries that this queue will handle.
12056 *
12057 * This function allocates a queue structure and the DMAable memory used for
12058 * the host resident queue. This function must be called before creating the
12059 * queue on the HBA.
12060 **/
12061struct lpfc_queue *
12062lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12063 uint32_t entry_count)
12064{
12065 struct lpfc_queue *queue;
12066 struct lpfc_dmabuf *dmabuf;
12067 int x, total_qe_count;
12068 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012069 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012070
James Smartcb5172e2010-03-15 11:25:07 -040012071 if (!phba->sli4_hba.pc_sli4_params.supported)
12072 hw_page_size = SLI4_PAGE_SIZE;
12073
James Smart4f774512009-05-22 14:52:35 -040012074 queue = kzalloc(sizeof(struct lpfc_queue) +
12075 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12076 if (!queue)
12077 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012078 queue->page_count = (ALIGN(entry_size * entry_count,
12079 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012080 INIT_LIST_HEAD(&queue->list);
12081 INIT_LIST_HEAD(&queue->page_list);
12082 INIT_LIST_HEAD(&queue->child_list);
12083 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12084 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12085 if (!dmabuf)
12086 goto out_fail;
12087 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012088 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012089 GFP_KERNEL);
12090 if (!dmabuf->virt) {
12091 kfree(dmabuf);
12092 goto out_fail;
12093 }
James Smartcb5172e2010-03-15 11:25:07 -040012094 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012095 dmabuf->buffer_tag = x;
12096 list_add_tail(&dmabuf->list, &queue->page_list);
12097 /* initialize queue's entry array */
12098 dma_pointer = dmabuf->virt;
12099 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012100 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012101 total_qe_count++, dma_pointer += entry_size) {
12102 queue->qe[total_qe_count].address = dma_pointer;
12103 }
12104 }
12105 queue->entry_size = entry_size;
12106 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012107
12108 /*
12109 * entry_repost is calculated based on the number of entries in the
12110 * queue. This works out except for RQs. If buffers are NOT initially
12111 * posted for every RQE, entry_repost should be adjusted accordingly.
12112 */
12113 queue->entry_repost = (entry_count >> 3);
12114 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12115 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012116 queue->phba = phba;
12117
12118 return queue;
12119out_fail:
12120 lpfc_sli4_queue_free(queue);
12121 return NULL;
12122}
12123
12124/**
James Smart173edbb2012-06-12 13:54:50 -040012125 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12126 * @phba: HBA structure that indicates port to create a queue on.
12127 * @startq: The starting FCP EQ to modify
12128 *
12129 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12130 *
12131 * The @phba struct is used to send mailbox command to HBA. The @startq
12132 * is used to get the starting FCP EQ to change.
12133 * This function is asynchronous and will wait for the mailbox
12134 * command to finish before continuing.
12135 *
12136 * On success this function will return a zero. If unable to allocate enough
12137 * memory this function will return -ENOMEM. If the queue create mailbox command
12138 * fails this function will return -ENXIO.
12139 **/
12140uint32_t
12141lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12142{
12143 struct lpfc_mbx_modify_eq_delay *eq_delay;
12144 LPFC_MBOXQ_t *mbox;
12145 struct lpfc_queue *eq;
12146 int cnt, rc, length, status = 0;
12147 uint32_t shdr_status, shdr_add_status;
12148 int fcp_eqidx;
12149 union lpfc_sli4_cfg_shdr *shdr;
12150 uint16_t dmult;
12151
James Smart67d12732012-08-03 12:36:13 -040012152 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040012153 return 0;
12154
12155 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12156 if (!mbox)
12157 return -ENOMEM;
12158 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12159 sizeof(struct lpfc_sli4_cfg_mhdr));
12160 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12161 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12162 length, LPFC_SLI4_MBX_EMBED);
12163 eq_delay = &mbox->u.mqe.un.eq_delay;
12164
12165 /* Calculate delay multiper from maximum interrupt per second */
James Smartbf8dae82012-08-03 12:36:24 -040012166 dmult = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12167 dmult = LPFC_DMULT_CONST/dmult - 1;
James Smart173edbb2012-06-12 13:54:50 -040012168
12169 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040012170 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040012171 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040012172 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040012173 if (!eq)
12174 continue;
12175 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12176 eq_delay->u.request.eq[cnt].phase = 0;
12177 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12178 cnt++;
12179 if (cnt >= LPFC_MAX_EQ_DELAY)
12180 break;
12181 }
12182 eq_delay->u.request.num_eq = cnt;
12183
12184 mbox->vport = phba->pport;
12185 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12186 mbox->context1 = NULL;
12187 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12188 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12189 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12190 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12191 if (shdr_status || shdr_add_status || rc) {
12192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12193 "2512 MODIFY_EQ_DELAY mailbox failed with "
12194 "status x%x add_status x%x, mbx status x%x\n",
12195 shdr_status, shdr_add_status, rc);
12196 status = -ENXIO;
12197 }
12198 mempool_free(mbox, phba->mbox_mem_pool);
12199 return status;
12200}
12201
12202/**
James Smart4f774512009-05-22 14:52:35 -040012203 * lpfc_eq_create - Create an Event Queue on the HBA
12204 * @phba: HBA structure that indicates port to create a queue on.
12205 * @eq: The queue structure to use to create the event queue.
12206 * @imax: The maximum interrupt per second limit.
12207 *
12208 * This function creates an event queue, as detailed in @eq, on a port,
12209 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12210 *
12211 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12212 * is used to get the entry count and entry size that are necessary to
12213 * determine the number of pages to allocate and use for this queue. This
12214 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12215 * event queue. This function is asynchronous and will wait for the mailbox
12216 * command to finish before continuing.
12217 *
12218 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012219 * memory this function will return -ENOMEM. If the queue create mailbox command
12220 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012221 **/
12222uint32_t
12223lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12224{
12225 struct lpfc_mbx_eq_create *eq_create;
12226 LPFC_MBOXQ_t *mbox;
12227 int rc, length, status = 0;
12228 struct lpfc_dmabuf *dmabuf;
12229 uint32_t shdr_status, shdr_add_status;
12230 union lpfc_sli4_cfg_shdr *shdr;
12231 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012232 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12233
James Smart2e90f4b2011-12-13 13:22:37 -050012234 /* sanity check on queue memory */
12235 if (!eq)
12236 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012237 if (!phba->sli4_hba.pc_sli4_params.supported)
12238 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012239
12240 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12241 if (!mbox)
12242 return -ENOMEM;
12243 length = (sizeof(struct lpfc_mbx_eq_create) -
12244 sizeof(struct lpfc_sli4_cfg_mhdr));
12245 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12246 LPFC_MBOX_OPCODE_EQ_CREATE,
12247 length, LPFC_SLI4_MBX_EMBED);
12248 eq_create = &mbox->u.mqe.un.eq_create;
12249 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12250 eq->page_count);
12251 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12252 LPFC_EQE_SIZE);
12253 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12254 /* Calculate delay multiper from maximum interrupt per second */
12255 dmult = LPFC_DMULT_CONST/imax - 1;
12256 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12257 dmult);
12258 switch (eq->entry_count) {
12259 default:
12260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12261 "0360 Unsupported EQ count. (%d)\n",
12262 eq->entry_count);
12263 if (eq->entry_count < 256)
12264 return -EINVAL;
12265 /* otherwise default to smallest count (drop through) */
12266 case 256:
12267 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12268 LPFC_EQ_CNT_256);
12269 break;
12270 case 512:
12271 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12272 LPFC_EQ_CNT_512);
12273 break;
12274 case 1024:
12275 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12276 LPFC_EQ_CNT_1024);
12277 break;
12278 case 2048:
12279 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12280 LPFC_EQ_CNT_2048);
12281 break;
12282 case 4096:
12283 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12284 LPFC_EQ_CNT_4096);
12285 break;
12286 }
12287 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012288 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012289 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12290 putPaddrLow(dmabuf->phys);
12291 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12292 putPaddrHigh(dmabuf->phys);
12293 }
12294 mbox->vport = phba->pport;
12295 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12296 mbox->context1 = NULL;
12297 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12298 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12301 if (shdr_status || shdr_add_status || rc) {
12302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12303 "2500 EQ_CREATE mailbox failed with "
12304 "status x%x add_status x%x, mbx status x%x\n",
12305 shdr_status, shdr_add_status, rc);
12306 status = -ENXIO;
12307 }
12308 eq->type = LPFC_EQ;
12309 eq->subtype = LPFC_NONE;
12310 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12311 if (eq->queue_id == 0xFFFF)
12312 status = -ENXIO;
12313 eq->host_index = 0;
12314 eq->hba_index = 0;
12315
James Smart8fa38512009-07-19 10:01:03 -040012316 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012317 return status;
12318}
12319
12320/**
12321 * lpfc_cq_create - Create a Completion Queue on the HBA
12322 * @phba: HBA structure that indicates port to create a queue on.
12323 * @cq: The queue structure to use to create the completion queue.
12324 * @eq: The event queue to bind this completion queue to.
12325 *
12326 * This function creates a completion queue, as detailed in @wq, on a port,
12327 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12328 *
12329 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12330 * is used to get the entry count and entry size that are necessary to
12331 * determine the number of pages to allocate and use for this queue. The @eq
12332 * is used to indicate which event queue to bind this completion queue to. This
12333 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12334 * completion queue. This function is asynchronous and will wait for the mailbox
12335 * command to finish before continuing.
12336 *
12337 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012338 * memory this function will return -ENOMEM. If the queue create mailbox command
12339 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012340 **/
12341uint32_t
12342lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12343 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12344{
12345 struct lpfc_mbx_cq_create *cq_create;
12346 struct lpfc_dmabuf *dmabuf;
12347 LPFC_MBOXQ_t *mbox;
12348 int rc, length, status = 0;
12349 uint32_t shdr_status, shdr_add_status;
12350 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012351 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12352
James Smart2e90f4b2011-12-13 13:22:37 -050012353 /* sanity check on queue memory */
12354 if (!cq || !eq)
12355 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012356 if (!phba->sli4_hba.pc_sli4_params.supported)
12357 hw_page_size = SLI4_PAGE_SIZE;
12358
James Smart4f774512009-05-22 14:52:35 -040012359 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12360 if (!mbox)
12361 return -ENOMEM;
12362 length = (sizeof(struct lpfc_mbx_cq_create) -
12363 sizeof(struct lpfc_sli4_cfg_mhdr));
12364 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12365 LPFC_MBOX_OPCODE_CQ_CREATE,
12366 length, LPFC_SLI4_MBX_EMBED);
12367 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012368 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012369 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12370 cq->page_count);
12371 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12372 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012373 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12374 phba->sli4_hba.pc_sli4_params.cqv);
12375 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012376 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12377 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012378 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12379 eq->queue_id);
12380 } else {
12381 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12382 eq->queue_id);
12383 }
James Smart4f774512009-05-22 14:52:35 -040012384 switch (cq->entry_count) {
12385 default:
12386 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12387 "0361 Unsupported CQ count. (%d)\n",
12388 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012389 if (cq->entry_count < 256) {
12390 status = -EINVAL;
12391 goto out;
12392 }
James Smart4f774512009-05-22 14:52:35 -040012393 /* otherwise default to smallest count (drop through) */
12394 case 256:
12395 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12396 LPFC_CQ_CNT_256);
12397 break;
12398 case 512:
12399 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12400 LPFC_CQ_CNT_512);
12401 break;
12402 case 1024:
12403 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12404 LPFC_CQ_CNT_1024);
12405 break;
12406 }
12407 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012408 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012409 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12410 putPaddrLow(dmabuf->phys);
12411 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12412 putPaddrHigh(dmabuf->phys);
12413 }
12414 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12415
12416 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012417 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12418 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12419 if (shdr_status || shdr_add_status || rc) {
12420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12421 "2501 CQ_CREATE mailbox failed with "
12422 "status x%x add_status x%x, mbx status x%x\n",
12423 shdr_status, shdr_add_status, rc);
12424 status = -ENXIO;
12425 goto out;
12426 }
12427 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12428 if (cq->queue_id == 0xFFFF) {
12429 status = -ENXIO;
12430 goto out;
12431 }
12432 /* link the cq onto the parent eq child list */
12433 list_add_tail(&cq->list, &eq->child_list);
12434 /* Set up completion queue's type and subtype */
12435 cq->type = type;
12436 cq->subtype = subtype;
12437 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012438 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012439 cq->host_index = 0;
12440 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012441
James Smart8fa38512009-07-19 10:01:03 -040012442out:
12443 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012444 return status;
12445}
12446
12447/**
James Smartb19a0612010-04-06 14:48:51 -040012448 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012449 * @phba: HBA structure that indicates port to create a queue on.
12450 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012451 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12452 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012453 *
James Smartb19a0612010-04-06 14:48:51 -040012454 * This function provides failback (fb) functionality when the
12455 * mq_create_ext fails on older FW generations. It's purpose is identical
12456 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012457 *
James Smartb19a0612010-04-06 14:48:51 -040012458 * This routine cannot fail as all attributes were previously accessed and
12459 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012460 **/
James Smartb19a0612010-04-06 14:48:51 -040012461static void
12462lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12463 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012464{
12465 struct lpfc_mbx_mq_create *mq_create;
12466 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012467 int length;
James Smart04c68492009-05-22 14:52:52 -040012468
James Smart04c68492009-05-22 14:52:52 -040012469 length = (sizeof(struct lpfc_mbx_mq_create) -
12470 sizeof(struct lpfc_sli4_cfg_mhdr));
12471 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12472 LPFC_MBOX_OPCODE_MQ_CREATE,
12473 length, LPFC_SLI4_MBX_EMBED);
12474 mq_create = &mbox->u.mqe.un.mq_create;
12475 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012476 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012477 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012478 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012479 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12480 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012481 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012482 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12483 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012484 break;
12485 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012486 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12487 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012488 break;
12489 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012490 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12491 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012492 break;
12493 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012494 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12495 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012496 break;
12497 }
12498 list_for_each_entry(dmabuf, &mq->page_list, list) {
12499 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012500 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012501 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012502 putPaddrHigh(dmabuf->phys);
12503 }
12504}
12505
12506/**
12507 * lpfc_mq_create - Create a mailbox Queue on the HBA
12508 * @phba: HBA structure that indicates port to create a queue on.
12509 * @mq: The queue structure to use to create the mailbox queue.
12510 * @cq: The completion queue to associate with this cq.
12511 * @subtype: The queue's subtype.
12512 *
12513 * This function creates a mailbox queue, as detailed in @mq, on a port,
12514 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12515 *
12516 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12517 * is used to get the entry count and entry size that are necessary to
12518 * determine the number of pages to allocate and use for this queue. This
12519 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12520 * mailbox queue. This function is asynchronous and will wait for the mailbox
12521 * command to finish before continuing.
12522 *
12523 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012524 * memory this function will return -ENOMEM. If the queue create mailbox command
12525 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012526 **/
12527int32_t
12528lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12529 struct lpfc_queue *cq, uint32_t subtype)
12530{
12531 struct lpfc_mbx_mq_create *mq_create;
12532 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12533 struct lpfc_dmabuf *dmabuf;
12534 LPFC_MBOXQ_t *mbox;
12535 int rc, length, status = 0;
12536 uint32_t shdr_status, shdr_add_status;
12537 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012538 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012539
James Smart2e90f4b2011-12-13 13:22:37 -050012540 /* sanity check on queue memory */
12541 if (!mq || !cq)
12542 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012543 if (!phba->sli4_hba.pc_sli4_params.supported)
12544 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012545
12546 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12547 if (!mbox)
12548 return -ENOMEM;
12549 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12550 sizeof(struct lpfc_sli4_cfg_mhdr));
12551 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12552 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12553 length, LPFC_SLI4_MBX_EMBED);
12554
12555 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012556 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012557 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12558 &mq_create_ext->u.request, mq->page_count);
12559 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12560 &mq_create_ext->u.request, 1);
12561 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012562 &mq_create_ext->u.request, 1);
12563 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12564 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012565 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12566 &mq_create_ext->u.request, 1);
12567 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12568 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012569 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012570 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12571 phba->sli4_hba.pc_sli4_params.mqv);
12572 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12573 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12574 cq->queue_id);
12575 else
12576 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12577 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012578 switch (mq->entry_count) {
12579 default:
12580 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12581 "0362 Unsupported MQ count. (%d)\n",
12582 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012583 if (mq->entry_count < 16) {
12584 status = -EINVAL;
12585 goto out;
12586 }
James Smartb19a0612010-04-06 14:48:51 -040012587 /* otherwise default to smallest count (drop through) */
12588 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012589 bf_set(lpfc_mq_context_ring_size,
12590 &mq_create_ext->u.request.context,
12591 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012592 break;
12593 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012594 bf_set(lpfc_mq_context_ring_size,
12595 &mq_create_ext->u.request.context,
12596 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012597 break;
12598 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012599 bf_set(lpfc_mq_context_ring_size,
12600 &mq_create_ext->u.request.context,
12601 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012602 break;
12603 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012604 bf_set(lpfc_mq_context_ring_size,
12605 &mq_create_ext->u.request.context,
12606 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012607 break;
12608 }
12609 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012610 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012611 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12612 putPaddrLow(dmabuf->phys);
12613 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012614 putPaddrHigh(dmabuf->phys);
12615 }
12616 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012617 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12618 &mq_create_ext->u.response);
12619 if (rc != MBX_SUCCESS) {
12620 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12621 "2795 MQ_CREATE_EXT failed with "
12622 "status x%x. Failback to MQ_CREATE.\n",
12623 rc);
12624 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12625 mq_create = &mbox->u.mqe.un.mq_create;
12626 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12627 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12628 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12629 &mq_create->u.response);
12630 }
12631
James Smart04c68492009-05-22 14:52:52 -040012632 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012633 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12634 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12635 if (shdr_status || shdr_add_status || rc) {
12636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12637 "2502 MQ_CREATE mailbox failed with "
12638 "status x%x add_status x%x, mbx status x%x\n",
12639 shdr_status, shdr_add_status, rc);
12640 status = -ENXIO;
12641 goto out;
12642 }
James Smart04c68492009-05-22 14:52:52 -040012643 if (mq->queue_id == 0xFFFF) {
12644 status = -ENXIO;
12645 goto out;
12646 }
12647 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012648 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012649 mq->subtype = subtype;
12650 mq->host_index = 0;
12651 mq->hba_index = 0;
12652
12653 /* link the mq onto the parent cq child list */
12654 list_add_tail(&mq->list, &cq->child_list);
12655out:
James Smart8fa38512009-07-19 10:01:03 -040012656 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012657 return status;
12658}
12659
12660/**
James Smart4f774512009-05-22 14:52:35 -040012661 * lpfc_wq_create - Create a Work Queue on the HBA
12662 * @phba: HBA structure that indicates port to create a queue on.
12663 * @wq: The queue structure to use to create the work queue.
12664 * @cq: The completion queue to bind this work queue to.
12665 * @subtype: The subtype of the work queue indicating its functionality.
12666 *
12667 * This function creates a work queue, as detailed in @wq, on a port, described
12668 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12669 *
12670 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12671 * is used to get the entry count and entry size that are necessary to
12672 * determine the number of pages to allocate and use for this queue. The @cq
12673 * is used to indicate which completion queue to bind this work queue to. This
12674 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12675 * work queue. This function is asynchronous and will wait for the mailbox
12676 * command to finish before continuing.
12677 *
12678 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012679 * memory this function will return -ENOMEM. If the queue create mailbox command
12680 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012681 **/
12682uint32_t
12683lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12684 struct lpfc_queue *cq, uint32_t subtype)
12685{
12686 struct lpfc_mbx_wq_create *wq_create;
12687 struct lpfc_dmabuf *dmabuf;
12688 LPFC_MBOXQ_t *mbox;
12689 int rc, length, status = 0;
12690 uint32_t shdr_status, shdr_add_status;
12691 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012692 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012693 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012694
James Smart2e90f4b2011-12-13 13:22:37 -050012695 /* sanity check on queue memory */
12696 if (!wq || !cq)
12697 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012698 if (!phba->sli4_hba.pc_sli4_params.supported)
12699 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012700
12701 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12702 if (!mbox)
12703 return -ENOMEM;
12704 length = (sizeof(struct lpfc_mbx_wq_create) -
12705 sizeof(struct lpfc_sli4_cfg_mhdr));
12706 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12707 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12708 length, LPFC_SLI4_MBX_EMBED);
12709 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012710 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012711 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12712 wq->page_count);
12713 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12714 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012715 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12716 phba->sli4_hba.pc_sli4_params.wqv);
12717 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12718 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12719 wq->entry_count);
12720 switch (wq->entry_size) {
12721 default:
12722 case 64:
12723 bf_set(lpfc_mbx_wq_create_wqe_size,
12724 &wq_create->u.request_1,
12725 LPFC_WQ_WQE_SIZE_64);
12726 break;
12727 case 128:
12728 bf_set(lpfc_mbx_wq_create_wqe_size,
12729 &wq_create->u.request_1,
12730 LPFC_WQ_WQE_SIZE_128);
12731 break;
12732 }
12733 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12734 (PAGE_SIZE/SLI4_PAGE_SIZE));
12735 page = wq_create->u.request_1.page;
12736 } else {
12737 page = wq_create->u.request.page;
12738 }
James Smart4f774512009-05-22 14:52:35 -040012739 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012740 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012741 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12742 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012743 }
12744 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12745 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012746 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12747 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12748 if (shdr_status || shdr_add_status || rc) {
12749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12750 "2503 WQ_CREATE mailbox failed with "
12751 "status x%x add_status x%x, mbx status x%x\n",
12752 shdr_status, shdr_add_status, rc);
12753 status = -ENXIO;
12754 goto out;
12755 }
12756 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12757 if (wq->queue_id == 0xFFFF) {
12758 status = -ENXIO;
12759 goto out;
12760 }
12761 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012762 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012763 wq->subtype = subtype;
12764 wq->host_index = 0;
12765 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012766 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012767
12768 /* link the wq onto the parent cq child list */
12769 list_add_tail(&wq->list, &cq->child_list);
12770out:
James Smart8fa38512009-07-19 10:01:03 -040012771 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012772 return status;
12773}
12774
12775/**
James Smart73d91e52011-10-10 21:32:10 -040012776 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12777 * @phba: HBA structure that indicates port to create a queue on.
12778 * @rq: The queue structure to use for the receive queue.
12779 * @qno: The associated HBQ number
12780 *
12781 *
12782 * For SLI4 we need to adjust the RQ repost value based on
12783 * the number of buffers that are initially posted to the RQ.
12784 */
12785void
12786lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12787{
12788 uint32_t cnt;
12789
James Smart2e90f4b2011-12-13 13:22:37 -050012790 /* sanity check on queue memory */
12791 if (!rq)
12792 return;
James Smart73d91e52011-10-10 21:32:10 -040012793 cnt = lpfc_hbq_defs[qno]->entry_count;
12794
12795 /* Recalc repost for RQs based on buffers initially posted */
12796 cnt = (cnt >> 3);
12797 if (cnt < LPFC_QUEUE_MIN_REPOST)
12798 cnt = LPFC_QUEUE_MIN_REPOST;
12799
12800 rq->entry_repost = cnt;
12801}
12802
12803/**
James Smart4f774512009-05-22 14:52:35 -040012804 * lpfc_rq_create - Create a Receive Queue on the HBA
12805 * @phba: HBA structure that indicates port to create a queue on.
12806 * @hrq: The queue structure to use to create the header receive queue.
12807 * @drq: The queue structure to use to create the data receive queue.
12808 * @cq: The completion queue to bind this work queue to.
12809 *
12810 * This function creates a receive buffer queue pair , as detailed in @hrq and
12811 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12812 * to the HBA.
12813 *
12814 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12815 * struct is used to get the entry count that is necessary to determine the
12816 * number of pages to use for this queue. The @cq is used to indicate which
12817 * completion queue to bind received buffers that are posted to these queues to.
12818 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12819 * receive queue pair. This function is asynchronous and will wait for the
12820 * mailbox command to finish before continuing.
12821 *
12822 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012823 * memory this function will return -ENOMEM. If the queue create mailbox command
12824 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012825 **/
12826uint32_t
12827lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12828 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12829{
12830 struct lpfc_mbx_rq_create *rq_create;
12831 struct lpfc_dmabuf *dmabuf;
12832 LPFC_MBOXQ_t *mbox;
12833 int rc, length, status = 0;
12834 uint32_t shdr_status, shdr_add_status;
12835 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012836 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12837
James Smart2e90f4b2011-12-13 13:22:37 -050012838 /* sanity check on queue memory */
12839 if (!hrq || !drq || !cq)
12840 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012841 if (!phba->sli4_hba.pc_sli4_params.supported)
12842 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012843
12844 if (hrq->entry_count != drq->entry_count)
12845 return -EINVAL;
12846 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12847 if (!mbox)
12848 return -ENOMEM;
12849 length = (sizeof(struct lpfc_mbx_rq_create) -
12850 sizeof(struct lpfc_sli4_cfg_mhdr));
12851 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12852 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12853 length, LPFC_SLI4_MBX_EMBED);
12854 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012855 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12856 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12857 phba->sli4_hba.pc_sli4_params.rqv);
12858 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12859 bf_set(lpfc_rq_context_rqe_count_1,
12860 &rq_create->u.request.context,
12861 hrq->entry_count);
12862 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012863 bf_set(lpfc_rq_context_rqe_size,
12864 &rq_create->u.request.context,
12865 LPFC_RQE_SIZE_8);
12866 bf_set(lpfc_rq_context_page_size,
12867 &rq_create->u.request.context,
12868 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012869 } else {
12870 switch (hrq->entry_count) {
12871 default:
12872 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12873 "2535 Unsupported RQ count. (%d)\n",
12874 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012875 if (hrq->entry_count < 512) {
12876 status = -EINVAL;
12877 goto out;
12878 }
James Smart5a6f1332011-03-11 16:05:35 -050012879 /* otherwise default to smallest count (drop through) */
12880 case 512:
12881 bf_set(lpfc_rq_context_rqe_count,
12882 &rq_create->u.request.context,
12883 LPFC_RQ_RING_SIZE_512);
12884 break;
12885 case 1024:
12886 bf_set(lpfc_rq_context_rqe_count,
12887 &rq_create->u.request.context,
12888 LPFC_RQ_RING_SIZE_1024);
12889 break;
12890 case 2048:
12891 bf_set(lpfc_rq_context_rqe_count,
12892 &rq_create->u.request.context,
12893 LPFC_RQ_RING_SIZE_2048);
12894 break;
12895 case 4096:
12896 bf_set(lpfc_rq_context_rqe_count,
12897 &rq_create->u.request.context,
12898 LPFC_RQ_RING_SIZE_4096);
12899 break;
12900 }
12901 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12902 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012903 }
12904 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12905 cq->queue_id);
12906 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12907 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012908 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012909 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012910 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12911 putPaddrLow(dmabuf->phys);
12912 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12913 putPaddrHigh(dmabuf->phys);
12914 }
12915 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12916 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012917 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12918 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12919 if (shdr_status || shdr_add_status || rc) {
12920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12921 "2504 RQ_CREATE mailbox failed with "
12922 "status x%x add_status x%x, mbx status x%x\n",
12923 shdr_status, shdr_add_status, rc);
12924 status = -ENXIO;
12925 goto out;
12926 }
12927 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12928 if (hrq->queue_id == 0xFFFF) {
12929 status = -ENXIO;
12930 goto out;
12931 }
12932 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012933 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012934 hrq->subtype = subtype;
12935 hrq->host_index = 0;
12936 hrq->hba_index = 0;
12937
12938 /* now create the data queue */
12939 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12940 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12941 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012942 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12943 phba->sli4_hba.pc_sli4_params.rqv);
12944 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12945 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012946 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012947 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012948 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12949 LPFC_RQE_SIZE_8);
12950 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12951 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012952 } else {
12953 switch (drq->entry_count) {
12954 default:
12955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12956 "2536 Unsupported RQ count. (%d)\n",
12957 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012958 if (drq->entry_count < 512) {
12959 status = -EINVAL;
12960 goto out;
12961 }
James Smart5a6f1332011-03-11 16:05:35 -050012962 /* otherwise default to smallest count (drop through) */
12963 case 512:
12964 bf_set(lpfc_rq_context_rqe_count,
12965 &rq_create->u.request.context,
12966 LPFC_RQ_RING_SIZE_512);
12967 break;
12968 case 1024:
12969 bf_set(lpfc_rq_context_rqe_count,
12970 &rq_create->u.request.context,
12971 LPFC_RQ_RING_SIZE_1024);
12972 break;
12973 case 2048:
12974 bf_set(lpfc_rq_context_rqe_count,
12975 &rq_create->u.request.context,
12976 LPFC_RQ_RING_SIZE_2048);
12977 break;
12978 case 4096:
12979 bf_set(lpfc_rq_context_rqe_count,
12980 &rq_create->u.request.context,
12981 LPFC_RQ_RING_SIZE_4096);
12982 break;
12983 }
12984 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12985 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012986 }
12987 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12988 cq->queue_id);
12989 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12990 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012991 list_for_each_entry(dmabuf, &drq->page_list, list) {
12992 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12993 putPaddrLow(dmabuf->phys);
12994 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12995 putPaddrHigh(dmabuf->phys);
12996 }
12997 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12998 /* The IOCTL status is embedded in the mailbox subheader. */
12999 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13000 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13002 if (shdr_status || shdr_add_status || rc) {
13003 status = -ENXIO;
13004 goto out;
13005 }
13006 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13007 if (drq->queue_id == 0xFFFF) {
13008 status = -ENXIO;
13009 goto out;
13010 }
13011 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013012 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013013 drq->subtype = subtype;
13014 drq->host_index = 0;
13015 drq->hba_index = 0;
13016
13017 /* link the header and data RQs onto the parent cq child list */
13018 list_add_tail(&hrq->list, &cq->child_list);
13019 list_add_tail(&drq->list, &cq->child_list);
13020
13021out:
James Smart8fa38512009-07-19 10:01:03 -040013022 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013023 return status;
13024}
13025
13026/**
13027 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13028 * @eq: The queue structure associated with the queue to destroy.
13029 *
13030 * This function destroys a queue, as detailed in @eq by sending an mailbox
13031 * command, specific to the type of queue, to the HBA.
13032 *
13033 * The @eq struct is used to get the queue ID of the queue to destroy.
13034 *
13035 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013036 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013037 **/
13038uint32_t
13039lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13040{
13041 LPFC_MBOXQ_t *mbox;
13042 int rc, length, status = 0;
13043 uint32_t shdr_status, shdr_add_status;
13044 union lpfc_sli4_cfg_shdr *shdr;
13045
James Smart2e90f4b2011-12-13 13:22:37 -050013046 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013047 if (!eq)
13048 return -ENODEV;
13049 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13050 if (!mbox)
13051 return -ENOMEM;
13052 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13053 sizeof(struct lpfc_sli4_cfg_mhdr));
13054 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13055 LPFC_MBOX_OPCODE_EQ_DESTROY,
13056 length, LPFC_SLI4_MBX_EMBED);
13057 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13058 eq->queue_id);
13059 mbox->vport = eq->phba->pport;
13060 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13061
13062 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13063 /* The IOCTL status is embedded in the mailbox subheader. */
13064 shdr = (union lpfc_sli4_cfg_shdr *)
13065 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13066 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13067 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13068 if (shdr_status || shdr_add_status || rc) {
13069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13070 "2505 EQ_DESTROY mailbox failed with "
13071 "status x%x add_status x%x, mbx status x%x\n",
13072 shdr_status, shdr_add_status, rc);
13073 status = -ENXIO;
13074 }
13075
13076 /* Remove eq from any list */
13077 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013078 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013079 return status;
13080}
13081
13082/**
13083 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13084 * @cq: The queue structure associated with the queue to destroy.
13085 *
13086 * This function destroys a queue, as detailed in @cq by sending an mailbox
13087 * command, specific to the type of queue, to the HBA.
13088 *
13089 * The @cq struct is used to get the queue ID of the queue to destroy.
13090 *
13091 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013092 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013093 **/
13094uint32_t
13095lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13096{
13097 LPFC_MBOXQ_t *mbox;
13098 int rc, length, status = 0;
13099 uint32_t shdr_status, shdr_add_status;
13100 union lpfc_sli4_cfg_shdr *shdr;
13101
James Smart2e90f4b2011-12-13 13:22:37 -050013102 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013103 if (!cq)
13104 return -ENODEV;
13105 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13106 if (!mbox)
13107 return -ENOMEM;
13108 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13109 sizeof(struct lpfc_sli4_cfg_mhdr));
13110 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13111 LPFC_MBOX_OPCODE_CQ_DESTROY,
13112 length, LPFC_SLI4_MBX_EMBED);
13113 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13114 cq->queue_id);
13115 mbox->vport = cq->phba->pport;
13116 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13117 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13118 /* The IOCTL status is embedded in the mailbox subheader. */
13119 shdr = (union lpfc_sli4_cfg_shdr *)
13120 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13121 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13122 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13123 if (shdr_status || shdr_add_status || rc) {
13124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13125 "2506 CQ_DESTROY mailbox failed with "
13126 "status x%x add_status x%x, mbx status x%x\n",
13127 shdr_status, shdr_add_status, rc);
13128 status = -ENXIO;
13129 }
13130 /* Remove cq from any list */
13131 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013132 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013133 return status;
13134}
13135
13136/**
James Smart04c68492009-05-22 14:52:52 -040013137 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13138 * @qm: The queue structure associated with the queue to destroy.
13139 *
13140 * This function destroys a queue, as detailed in @mq by sending an mailbox
13141 * command, specific to the type of queue, to the HBA.
13142 *
13143 * The @mq struct is used to get the queue ID of the queue to destroy.
13144 *
13145 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013146 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013147 **/
13148uint32_t
13149lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13150{
13151 LPFC_MBOXQ_t *mbox;
13152 int rc, length, status = 0;
13153 uint32_t shdr_status, shdr_add_status;
13154 union lpfc_sli4_cfg_shdr *shdr;
13155
James Smart2e90f4b2011-12-13 13:22:37 -050013156 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013157 if (!mq)
13158 return -ENODEV;
13159 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13160 if (!mbox)
13161 return -ENOMEM;
13162 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13163 sizeof(struct lpfc_sli4_cfg_mhdr));
13164 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13165 LPFC_MBOX_OPCODE_MQ_DESTROY,
13166 length, LPFC_SLI4_MBX_EMBED);
13167 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13168 mq->queue_id);
13169 mbox->vport = mq->phba->pport;
13170 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13171 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13172 /* The IOCTL status is embedded in the mailbox subheader. */
13173 shdr = (union lpfc_sli4_cfg_shdr *)
13174 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13175 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13176 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13177 if (shdr_status || shdr_add_status || rc) {
13178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13179 "2507 MQ_DESTROY mailbox failed with "
13180 "status x%x add_status x%x, mbx status x%x\n",
13181 shdr_status, shdr_add_status, rc);
13182 status = -ENXIO;
13183 }
13184 /* Remove mq from any list */
13185 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013186 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013187 return status;
13188}
13189
13190/**
James Smart4f774512009-05-22 14:52:35 -040013191 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13192 * @wq: The queue structure associated with the queue to destroy.
13193 *
13194 * This function destroys a queue, as detailed in @wq by sending an mailbox
13195 * command, specific to the type of queue, to the HBA.
13196 *
13197 * The @wq struct is used to get the queue ID of the queue to destroy.
13198 *
13199 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013200 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013201 **/
13202uint32_t
13203lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13204{
13205 LPFC_MBOXQ_t *mbox;
13206 int rc, length, status = 0;
13207 uint32_t shdr_status, shdr_add_status;
13208 union lpfc_sli4_cfg_shdr *shdr;
13209
James Smart2e90f4b2011-12-13 13:22:37 -050013210 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013211 if (!wq)
13212 return -ENODEV;
13213 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13214 if (!mbox)
13215 return -ENOMEM;
13216 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13217 sizeof(struct lpfc_sli4_cfg_mhdr));
13218 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13219 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13220 length, LPFC_SLI4_MBX_EMBED);
13221 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13222 wq->queue_id);
13223 mbox->vport = wq->phba->pport;
13224 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13225 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13226 shdr = (union lpfc_sli4_cfg_shdr *)
13227 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13228 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13229 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13230 if (shdr_status || shdr_add_status || rc) {
13231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13232 "2508 WQ_DESTROY mailbox failed with "
13233 "status x%x add_status x%x, mbx status x%x\n",
13234 shdr_status, shdr_add_status, rc);
13235 status = -ENXIO;
13236 }
13237 /* Remove wq from any list */
13238 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013239 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013240 return status;
13241}
13242
13243/**
13244 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13245 * @rq: The queue structure associated with the queue to destroy.
13246 *
13247 * This function destroys a queue, as detailed in @rq by sending an mailbox
13248 * command, specific to the type of queue, to the HBA.
13249 *
13250 * The @rq struct is used to get the queue ID of the queue to destroy.
13251 *
13252 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013253 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013254 **/
13255uint32_t
13256lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13257 struct lpfc_queue *drq)
13258{
13259 LPFC_MBOXQ_t *mbox;
13260 int rc, length, status = 0;
13261 uint32_t shdr_status, shdr_add_status;
13262 union lpfc_sli4_cfg_shdr *shdr;
13263
James Smart2e90f4b2011-12-13 13:22:37 -050013264 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013265 if (!hrq || !drq)
13266 return -ENODEV;
13267 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13268 if (!mbox)
13269 return -ENOMEM;
13270 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013271 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013272 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13273 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13274 length, LPFC_SLI4_MBX_EMBED);
13275 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13276 hrq->queue_id);
13277 mbox->vport = hrq->phba->pport;
13278 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13279 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13280 /* The IOCTL status is embedded in the mailbox subheader. */
13281 shdr = (union lpfc_sli4_cfg_shdr *)
13282 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13283 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13284 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13285 if (shdr_status || shdr_add_status || rc) {
13286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13287 "2509 RQ_DESTROY mailbox failed with "
13288 "status x%x add_status x%x, mbx status x%x\n",
13289 shdr_status, shdr_add_status, rc);
13290 if (rc != MBX_TIMEOUT)
13291 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13292 return -ENXIO;
13293 }
13294 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13295 drq->queue_id);
13296 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13297 shdr = (union lpfc_sli4_cfg_shdr *)
13298 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13301 if (shdr_status || shdr_add_status || rc) {
13302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13303 "2510 RQ_DESTROY mailbox failed with "
13304 "status x%x add_status x%x, mbx status x%x\n",
13305 shdr_status, shdr_add_status, rc);
13306 status = -ENXIO;
13307 }
13308 list_del_init(&hrq->list);
13309 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013310 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013311 return status;
13312}
13313
13314/**
13315 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13316 * @phba: The virtual port for which this call being executed.
13317 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13318 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13319 * @xritag: the xritag that ties this io to the SGL pages.
13320 *
13321 * This routine will post the sgl pages for the IO that has the xritag
13322 * that is in the iocbq structure. The xritag is assigned during iocbq
13323 * creation and persists for as long as the driver is loaded.
13324 * if the caller has fewer than 256 scatter gather segments to map then
13325 * pdma_phys_addr1 should be 0.
13326 * If the caller needs to map more than 256 scatter gather segment then
13327 * pdma_phys_addr1 should be a valid physical address.
13328 * physical address for SGLs must be 64 byte aligned.
13329 * If you are going to map 2 SGL's then the first one must have 256 entries
13330 * the second sgl can have between 1 and 256 entries.
13331 *
13332 * Return codes:
13333 * 0 - Success
13334 * -ENXIO, -ENOMEM - Failure
13335 **/
13336int
13337lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13338 dma_addr_t pdma_phys_addr0,
13339 dma_addr_t pdma_phys_addr1,
13340 uint16_t xritag)
13341{
13342 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13343 LPFC_MBOXQ_t *mbox;
13344 int rc;
13345 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013346 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013347 union lpfc_sli4_cfg_shdr *shdr;
13348
13349 if (xritag == NO_XRI) {
13350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13351 "0364 Invalid param:\n");
13352 return -EINVAL;
13353 }
13354
13355 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13356 if (!mbox)
13357 return -ENOMEM;
13358
13359 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13360 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13361 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013362 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013363
13364 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13365 &mbox->u.mqe.un.post_sgl_pages;
13366 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13367 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13368
13369 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13370 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13371 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13372 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13373
13374 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13375 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13376 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13377 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13378 if (!phba->sli4_hba.intr_enable)
13379 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013380 else {
James Smarta183a152011-10-10 21:32:43 -040013381 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013382 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13383 }
James Smart4f774512009-05-22 14:52:35 -040013384 /* The IOCTL status is embedded in the mailbox subheader. */
13385 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13386 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13387 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13388 if (rc != MBX_TIMEOUT)
13389 mempool_free(mbox, phba->mbox_mem_pool);
13390 if (shdr_status || shdr_add_status || rc) {
13391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13392 "2511 POST_SGL mailbox failed with "
13393 "status x%x add_status x%x, mbx status x%x\n",
13394 shdr_status, shdr_add_status, rc);
13395 rc = -ENXIO;
13396 }
13397 return 0;
13398}
James Smart4f774512009-05-22 14:52:35 -040013399
13400/**
James Smart88a2cfb2011-07-22 18:36:33 -040013401 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013402 * @phba: pointer to lpfc hba data structure.
13403 *
13404 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013405 * HBA consistent with the SLI-4 interface spec. This routine
13406 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13407 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013408 *
James Smart88a2cfb2011-07-22 18:36:33 -040013409 * Returns
13410 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13411 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13412 **/
James Smart6d368e52011-05-24 11:44:12 -040013413uint16_t
13414lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13415{
13416 unsigned long xri;
13417
13418 /*
13419 * Fetch the next logical xri. Because this index is logical,
13420 * the driver starts at 0 each time.
13421 */
13422 spin_lock_irq(&phba->hbalock);
13423 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13424 phba->sli4_hba.max_cfg_param.max_xri, 0);
13425 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13426 spin_unlock_irq(&phba->hbalock);
13427 return NO_XRI;
13428 } else {
13429 set_bit(xri, phba->sli4_hba.xri_bmask);
13430 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013431 }
James Smart6d368e52011-05-24 11:44:12 -040013432 spin_unlock_irq(&phba->hbalock);
13433 return xri;
13434}
13435
13436/**
13437 * lpfc_sli4_free_xri - Release an xri for reuse.
13438 * @phba: pointer to lpfc hba data structure.
13439 *
13440 * This routine is invoked to release an xri to the pool of
13441 * available rpis maintained by the driver.
13442 **/
13443void
13444__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13445{
13446 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013447 phba->sli4_hba.max_cfg_param.xri_used--;
13448 }
13449}
13450
13451/**
13452 * lpfc_sli4_free_xri - Release an xri for reuse.
13453 * @phba: pointer to lpfc hba data structure.
13454 *
13455 * This routine is invoked to release an xri to the pool of
13456 * available rpis maintained by the driver.
13457 **/
13458void
13459lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13460{
13461 spin_lock_irq(&phba->hbalock);
13462 __lpfc_sli4_free_xri(phba, xri);
13463 spin_unlock_irq(&phba->hbalock);
13464}
13465
13466/**
James Smart4f774512009-05-22 14:52:35 -040013467 * lpfc_sli4_next_xritag - Get an xritag for the io
13468 * @phba: Pointer to HBA context object.
13469 *
13470 * This function gets an xritag for the iocb. If there is no unused xritag
13471 * it will return 0xffff.
13472 * The function returns the allocated xritag if successful, else returns zero.
13473 * Zero is not a valid xritag.
13474 * The caller is not required to hold any lock.
13475 **/
13476uint16_t
13477lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13478{
James Smart6d368e52011-05-24 11:44:12 -040013479 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013480
James Smart6d368e52011-05-24 11:44:12 -040013481 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013482 if (xri_index == NO_XRI)
13483 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13484 "2004 Failed to allocate XRI.last XRITAG is %d"
13485 " Max XRI is %d, Used XRI is %d\n",
13486 xri_index,
13487 phba->sli4_hba.max_cfg_param.max_xri,
13488 phba->sli4_hba.max_cfg_param.xri_used);
13489 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013490}
13491
13492/**
James Smart6d368e52011-05-24 11:44:12 -040013493 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013494 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013495 * @post_sgl_list: pointer to els sgl entry list.
13496 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013497 *
13498 * This routine is invoked to post a block of driver's sgl pages to the
13499 * HBA using non-embedded mailbox command. No Lock is held. This routine
13500 * is only called when the driver is loading and after all IO has been
13501 * stopped.
13502 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013503static int
13504lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13505 struct list_head *post_sgl_list,
13506 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013507{
James Smart8a9d2e82012-05-09 21:16:12 -040013508 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013509 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13510 struct sgl_page_pairs *sgl_pg_pairs;
13511 void *viraddr;
13512 LPFC_MBOXQ_t *mbox;
13513 uint32_t reqlen, alloclen, pg_pairs;
13514 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013515 uint16_t xritag_start = 0;
13516 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013517 uint32_t shdr_status, shdr_add_status;
13518 union lpfc_sli4_cfg_shdr *shdr;
13519
James Smart8a9d2e82012-05-09 21:16:12 -040013520 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013521 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013522 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013523 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13524 "2559 Block sgl registration required DMA "
13525 "size (%d) great than a page\n", reqlen);
13526 return -ENOMEM;
13527 }
13528 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013529 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013530 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013531
13532 /* Allocate DMA memory and set up the non-embedded mailbox command */
13533 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13534 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13535 LPFC_SLI4_MBX_NEMBED);
13536
13537 if (alloclen < reqlen) {
13538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13539 "0285 Allocated DMA memory size (%d) is "
13540 "less than the requested DMA memory "
13541 "size (%d)\n", alloclen, reqlen);
13542 lpfc_sli4_mbox_cmd_free(phba, mbox);
13543 return -ENOMEM;
13544 }
James Smart4f774512009-05-22 14:52:35 -040013545 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013546 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013547 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13548 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13549
James Smart8a9d2e82012-05-09 21:16:12 -040013550 pg_pairs = 0;
13551 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013552 /* Set up the sge entry */
13553 sgl_pg_pairs->sgl_pg0_addr_lo =
13554 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13555 sgl_pg_pairs->sgl_pg0_addr_hi =
13556 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13557 sgl_pg_pairs->sgl_pg1_addr_lo =
13558 cpu_to_le32(putPaddrLow(0));
13559 sgl_pg_pairs->sgl_pg1_addr_hi =
13560 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013561
James Smart4f774512009-05-22 14:52:35 -040013562 /* Keep the first xritag on the list */
13563 if (pg_pairs == 0)
13564 xritag_start = sglq_entry->sli4_xritag;
13565 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013566 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013567 }
James Smart6d368e52011-05-24 11:44:12 -040013568
13569 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013570 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013571 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013572 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013573 if (!phba->sli4_hba.intr_enable)
13574 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13575 else {
James Smarta183a152011-10-10 21:32:43 -040013576 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013577 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13578 }
13579 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->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 (rc != MBX_TIMEOUT)
13583 lpfc_sli4_mbox_cmd_free(phba, mbox);
13584 if (shdr_status || shdr_add_status || rc) {
13585 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13586 "2513 POST_SGL_BLOCK mailbox command failed "
13587 "status x%x add_status x%x mbx status x%x\n",
13588 shdr_status, shdr_add_status, rc);
13589 rc = -ENXIO;
13590 }
13591 return rc;
13592}
13593
13594/**
13595 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13596 * @phba: pointer to lpfc hba data structure.
13597 * @sblist: pointer to scsi buffer list.
13598 * @count: number of scsi buffers on the list.
13599 *
13600 * This routine is invoked to post a block of @count scsi sgl pages from a
13601 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13602 * No Lock is held.
13603 *
13604 **/
13605int
James Smart8a9d2e82012-05-09 21:16:12 -040013606lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13607 struct list_head *sblist,
13608 int count)
James Smart4f774512009-05-22 14:52:35 -040013609{
13610 struct lpfc_scsi_buf *psb;
13611 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13612 struct sgl_page_pairs *sgl_pg_pairs;
13613 void *viraddr;
13614 LPFC_MBOXQ_t *mbox;
13615 uint32_t reqlen, alloclen, pg_pairs;
13616 uint32_t mbox_tmo;
13617 uint16_t xritag_start = 0;
13618 int rc = 0;
13619 uint32_t shdr_status, shdr_add_status;
13620 dma_addr_t pdma_phys_bpl1;
13621 union lpfc_sli4_cfg_shdr *shdr;
13622
13623 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013624 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013625 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013626 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013627 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13628 "0217 Block sgl registration required DMA "
13629 "size (%d) great than a page\n", reqlen);
13630 return -ENOMEM;
13631 }
13632 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13633 if (!mbox) {
13634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13635 "0283 Failed to allocate mbox cmd memory\n");
13636 return -ENOMEM;
13637 }
13638
13639 /* Allocate DMA memory and set up the non-embedded mailbox command */
13640 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13641 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13642 LPFC_SLI4_MBX_NEMBED);
13643
13644 if (alloclen < reqlen) {
13645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13646 "2561 Allocated DMA memory size (%d) is "
13647 "less than the requested DMA memory "
13648 "size (%d)\n", alloclen, reqlen);
13649 lpfc_sli4_mbox_cmd_free(phba, mbox);
13650 return -ENOMEM;
13651 }
James Smart6d368e52011-05-24 11:44:12 -040013652
James Smart4f774512009-05-22 14:52:35 -040013653 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013654 viraddr = mbox->sge_array->addr[0];
13655
13656 /* Set up the SGL pages in the non-embedded DMA pages */
13657 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13658 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13659
13660 pg_pairs = 0;
13661 list_for_each_entry(psb, sblist, list) {
13662 /* Set up the sge entry */
13663 sgl_pg_pairs->sgl_pg0_addr_lo =
13664 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13665 sgl_pg_pairs->sgl_pg0_addr_hi =
13666 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13667 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13668 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13669 else
13670 pdma_phys_bpl1 = 0;
13671 sgl_pg_pairs->sgl_pg1_addr_lo =
13672 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13673 sgl_pg_pairs->sgl_pg1_addr_hi =
13674 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13675 /* Keep the first xritag on the list */
13676 if (pg_pairs == 0)
13677 xritag_start = psb->cur_iocbq.sli4_xritag;
13678 sgl_pg_pairs++;
13679 pg_pairs++;
13680 }
13681 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13682 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13683 /* Perform endian conversion if necessary */
13684 sgl->word0 = cpu_to_le32(sgl->word0);
13685
13686 if (!phba->sli4_hba.intr_enable)
13687 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13688 else {
James Smarta183a152011-10-10 21:32:43 -040013689 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013690 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13691 }
13692 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13693 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13694 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13695 if (rc != MBX_TIMEOUT)
13696 lpfc_sli4_mbox_cmd_free(phba, mbox);
13697 if (shdr_status || shdr_add_status || rc) {
13698 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13699 "2564 POST_SGL_BLOCK mailbox command failed "
13700 "status x%x add_status x%x mbx status x%x\n",
13701 shdr_status, shdr_add_status, rc);
13702 rc = -ENXIO;
13703 }
13704 return rc;
13705}
13706
13707/**
13708 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13709 * @phba: pointer to lpfc_hba struct that the frame was received on
13710 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13711 *
13712 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13713 * valid type of frame that the LPFC driver will handle. This function will
13714 * return a zero if the frame is a valid frame or a non zero value when the
13715 * frame does not pass the check.
13716 **/
13717static int
13718lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13719{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013720 /* make rctl_names static to save stack space */
13721 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013722 char *type_names[] = FC_TYPE_NAMES_INIT;
13723 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013724 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013725
13726 switch (fc_hdr->fh_r_ctl) {
13727 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13728 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13729 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13730 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13731 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13732 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13733 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13734 case FC_RCTL_DD_CMD_STATUS: /* command status */
13735 case FC_RCTL_ELS_REQ: /* extended link services request */
13736 case FC_RCTL_ELS_REP: /* extended link services reply */
13737 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13738 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13739 case FC_RCTL_BA_NOP: /* basic link service NOP */
13740 case FC_RCTL_BA_ABTS: /* basic link service abort */
13741 case FC_RCTL_BA_RMC: /* remove connection */
13742 case FC_RCTL_BA_ACC: /* basic accept */
13743 case FC_RCTL_BA_RJT: /* basic reject */
13744 case FC_RCTL_BA_PRMT:
13745 case FC_RCTL_ACK_1: /* acknowledge_1 */
13746 case FC_RCTL_ACK_0: /* acknowledge_0 */
13747 case FC_RCTL_P_RJT: /* port reject */
13748 case FC_RCTL_F_RJT: /* fabric reject */
13749 case FC_RCTL_P_BSY: /* port busy */
13750 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13751 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13752 case FC_RCTL_LCR: /* link credit reset */
13753 case FC_RCTL_END: /* end */
13754 break;
13755 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13756 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13757 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13758 return lpfc_fc_frame_check(phba, fc_hdr);
13759 default:
13760 goto drop;
13761 }
13762 switch (fc_hdr->fh_type) {
13763 case FC_TYPE_BLS:
13764 case FC_TYPE_ELS:
13765 case FC_TYPE_FCP:
13766 case FC_TYPE_CT:
13767 break;
13768 case FC_TYPE_IP:
13769 case FC_TYPE_ILS:
13770 default:
13771 goto drop;
13772 }
James Smart546fc852011-03-11 16:06:29 -050013773
James Smart4f774512009-05-22 14:52:35 -040013774 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013775 "2538 Received frame rctl:%s type:%s "
13776 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013777 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013778 type_names[fc_hdr->fh_type],
13779 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13780 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13781 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013782 return 0;
13783drop:
13784 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13785 "2539 Dropped frame rctl:%s type:%s\n",
13786 rctl_names[fc_hdr->fh_r_ctl],
13787 type_names[fc_hdr->fh_type]);
13788 return 1;
13789}
13790
13791/**
13792 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13793 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13794 *
13795 * This function processes the FC header to retrieve the VFI from the VF
13796 * header, if one exists. This function will return the VFI if one exists
13797 * or 0 if no VSAN Header exists.
13798 **/
13799static uint32_t
13800lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13801{
13802 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13803
13804 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13805 return 0;
13806 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13807}
13808
13809/**
13810 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13811 * @phba: Pointer to the HBA structure to search for the vport on
13812 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13813 * @fcfi: The FC Fabric ID that the frame came from
13814 *
13815 * This function searches the @phba for a vport that matches the content of the
13816 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13817 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13818 * returns the matching vport pointer or NULL if unable to match frame to a
13819 * vport.
13820 **/
13821static struct lpfc_vport *
13822lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13823 uint16_t fcfi)
13824{
13825 struct lpfc_vport **vports;
13826 struct lpfc_vport *vport = NULL;
13827 int i;
13828 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13829 fc_hdr->fh_d_id[1] << 8 |
13830 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013831
James Smartbf086112011-08-21 21:48:13 -040013832 if (did == Fabric_DID)
13833 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013834 if ((phba->pport->fc_flag & FC_PT2PT) &&
13835 !(phba->link_state == LPFC_HBA_READY))
13836 return phba->pport;
13837
James Smart4f774512009-05-22 14:52:35 -040013838 vports = lpfc_create_vport_work_array(phba);
13839 if (vports != NULL)
13840 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13841 if (phba->fcf.fcfi == fcfi &&
13842 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13843 vports[i]->fc_myDID == did) {
13844 vport = vports[i];
13845 break;
13846 }
13847 }
13848 lpfc_destroy_vport_work_array(phba, vports);
13849 return vport;
13850}
13851
13852/**
James Smart45ed1192009-10-02 15:17:02 -040013853 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13854 * @vport: The vport to work on.
13855 *
13856 * This function updates the receive sequence time stamp for this vport. The
13857 * receive sequence time stamp indicates the time that the last frame of the
13858 * the sequence that has been idle for the longest amount of time was received.
13859 * the driver uses this time stamp to indicate if any received sequences have
13860 * timed out.
13861 **/
13862void
13863lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13864{
13865 struct lpfc_dmabuf *h_buf;
13866 struct hbq_dmabuf *dmabuf = NULL;
13867
13868 /* get the oldest sequence on the rcv list */
13869 h_buf = list_get_first(&vport->rcv_buffer_list,
13870 struct lpfc_dmabuf, list);
13871 if (!h_buf)
13872 return;
13873 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13874 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13875}
13876
13877/**
13878 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13879 * @vport: The vport that the received sequences were sent to.
13880 *
13881 * This function cleans up all outstanding received sequences. This is called
13882 * by the driver when a link event or user action invalidates all the received
13883 * sequences.
13884 **/
13885void
13886lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13887{
13888 struct lpfc_dmabuf *h_buf, *hnext;
13889 struct lpfc_dmabuf *d_buf, *dnext;
13890 struct hbq_dmabuf *dmabuf = NULL;
13891
13892 /* start with the oldest sequence on the rcv list */
13893 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13894 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13895 list_del_init(&dmabuf->hbuf.list);
13896 list_for_each_entry_safe(d_buf, dnext,
13897 &dmabuf->dbuf.list, list) {
13898 list_del_init(&d_buf->list);
13899 lpfc_in_buf_free(vport->phba, d_buf);
13900 }
13901 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13902 }
13903}
13904
13905/**
13906 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13907 * @vport: The vport that the received sequences were sent to.
13908 *
13909 * This function determines whether any received sequences have timed out by
13910 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13911 * indicates that there is at least one timed out sequence this routine will
13912 * go through the received sequences one at a time from most inactive to most
13913 * active to determine which ones need to be cleaned up. Once it has determined
13914 * that a sequence needs to be cleaned up it will simply free up the resources
13915 * without sending an abort.
13916 **/
13917void
13918lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13919{
13920 struct lpfc_dmabuf *h_buf, *hnext;
13921 struct lpfc_dmabuf *d_buf, *dnext;
13922 struct hbq_dmabuf *dmabuf = NULL;
13923 unsigned long timeout;
13924 int abort_count = 0;
13925
13926 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13927 vport->rcv_buffer_time_stamp);
13928 if (list_empty(&vport->rcv_buffer_list) ||
13929 time_before(jiffies, timeout))
13930 return;
13931 /* start with the oldest sequence on the rcv list */
13932 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13933 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13934 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13935 dmabuf->time_stamp);
13936 if (time_before(jiffies, timeout))
13937 break;
13938 abort_count++;
13939 list_del_init(&dmabuf->hbuf.list);
13940 list_for_each_entry_safe(d_buf, dnext,
13941 &dmabuf->dbuf.list, list) {
13942 list_del_init(&d_buf->list);
13943 lpfc_in_buf_free(vport->phba, d_buf);
13944 }
13945 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13946 }
13947 if (abort_count)
13948 lpfc_update_rcv_time_stamp(vport);
13949}
13950
13951/**
James Smart4f774512009-05-22 14:52:35 -040013952 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13953 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13954 *
13955 * This function searches through the existing incomplete sequences that have
13956 * been sent to this @vport. If the frame matches one of the incomplete
13957 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13958 * make up that sequence. If no sequence is found that matches this frame then
13959 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13960 * This function returns a pointer to the first dmabuf in the sequence list that
13961 * the frame was linked to.
13962 **/
13963static struct hbq_dmabuf *
13964lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13965{
13966 struct fc_frame_header *new_hdr;
13967 struct fc_frame_header *temp_hdr;
13968 struct lpfc_dmabuf *d_buf;
13969 struct lpfc_dmabuf *h_buf;
13970 struct hbq_dmabuf *seq_dmabuf = NULL;
13971 struct hbq_dmabuf *temp_dmabuf = NULL;
13972
James Smart4d9ab992009-10-02 15:16:39 -040013973 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013974 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013975 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13976 /* Use the hdr_buf to find the sequence that this frame belongs to */
13977 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13978 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13979 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13980 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13981 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13982 continue;
13983 /* found a pending sequence that matches this frame */
13984 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13985 break;
13986 }
13987 if (!seq_dmabuf) {
13988 /*
13989 * This indicates first frame received for this sequence.
13990 * Queue the buffer on the vport's rcv_buffer_list.
13991 */
13992 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013993 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013994 return dmabuf;
13995 }
13996 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013997 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13998 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013999 list_del_init(&seq_dmabuf->hbuf.list);
14000 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14001 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014002 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014003 return dmabuf;
14004 }
James Smart45ed1192009-10-02 15:17:02 -040014005 /* move this sequence to the tail to indicate a young sequence */
14006 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14007 seq_dmabuf->time_stamp = jiffies;
14008 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014009 if (list_empty(&seq_dmabuf->dbuf.list)) {
14010 temp_hdr = dmabuf->hbuf.virt;
14011 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14012 return seq_dmabuf;
14013 }
James Smart4f774512009-05-22 14:52:35 -040014014 /* find the correct place in the sequence to insert this frame */
14015 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14016 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14017 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14018 /*
14019 * If the frame's sequence count is greater than the frame on
14020 * the list then insert the frame right after this frame
14021 */
James Smarteeead812009-12-21 17:01:23 -050014022 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14023 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014024 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14025 return seq_dmabuf;
14026 }
14027 }
14028 return NULL;
14029}
14030
14031/**
James Smart6669f9b2009-10-02 15:16:45 -040014032 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14033 * @vport: pointer to a vitural port
14034 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14035 *
14036 * This function tries to abort from the partially assembed sequence, described
14037 * by the information from basic abbort @dmabuf. It checks to see whether such
14038 * partially assembled sequence held by the driver. If so, it shall free up all
14039 * the frames from the partially assembled sequence.
14040 *
14041 * Return
14042 * true -- if there is matching partially assembled sequence present and all
14043 * the frames freed with the sequence;
14044 * false -- if there is no matching partially assembled sequence present so
14045 * nothing got aborted in the lower layer driver
14046 **/
14047static bool
14048lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14049 struct hbq_dmabuf *dmabuf)
14050{
14051 struct fc_frame_header *new_hdr;
14052 struct fc_frame_header *temp_hdr;
14053 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14054 struct hbq_dmabuf *seq_dmabuf = NULL;
14055
14056 /* Use the hdr_buf to find the sequence that matches this frame */
14057 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14058 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14059 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14060 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14061 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14062 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14063 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14064 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14065 continue;
14066 /* found a pending sequence that matches this frame */
14067 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14068 break;
14069 }
14070
14071 /* Free up all the frames from the partially assembled sequence */
14072 if (seq_dmabuf) {
14073 list_for_each_entry_safe(d_buf, n_buf,
14074 &seq_dmabuf->dbuf.list, list) {
14075 list_del_init(&d_buf->list);
14076 lpfc_in_buf_free(vport->phba, d_buf);
14077 }
14078 return true;
14079 }
14080 return false;
14081}
14082
14083/**
James Smart546fc852011-03-11 16:06:29 -050014084 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014085 * @phba: Pointer to HBA context object.
14086 * @cmd_iocbq: pointer to the command iocbq structure.
14087 * @rsp_iocbq: pointer to the response iocbq structure.
14088 *
James Smart546fc852011-03-11 16:06:29 -050014089 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014090 * event. It properly releases the memory allocated to the sequence abort
14091 * accept iocb.
14092 **/
14093static void
James Smart546fc852011-03-11 16:06:29 -050014094lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014095 struct lpfc_iocbq *cmd_iocbq,
14096 struct lpfc_iocbq *rsp_iocbq)
14097{
14098 if (cmd_iocbq)
14099 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014100
14101 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14102 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14104 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14105 rsp_iocbq->iocb.ulpStatus,
14106 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014107}
14108
14109/**
James Smart6d368e52011-05-24 11:44:12 -040014110 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14111 * @phba: Pointer to HBA context object.
14112 * @xri: xri id in transaction.
14113 *
14114 * This function validates the xri maps to the known range of XRIs allocated an
14115 * used by the driver.
14116 **/
James Smart7851fe22011-07-22 18:36:52 -040014117uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014118lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14119 uint16_t xri)
14120{
14121 int i;
14122
14123 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14124 if (xri == phba->sli4_hba.xri_ids[i])
14125 return i;
14126 }
14127 return NO_XRI;
14128}
14129
James Smart6d368e52011-05-24 11:44:12 -040014130/**
James Smart546fc852011-03-11 16:06:29 -050014131 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014132 * @phba: Pointer to HBA context object.
14133 * @fc_hdr: pointer to a FC frame header.
14134 *
James Smart546fc852011-03-11 16:06:29 -050014135 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014136 * event after aborting the sequence handling.
14137 **/
14138static void
James Smart546fc852011-03-11 16:06:29 -050014139lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014140 struct fc_frame_header *fc_hdr)
14141{
14142 struct lpfc_iocbq *ctiocb = NULL;
14143 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014144 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014145 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014146 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014147 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014148
14149 if (!lpfc_is_link_up(phba))
14150 return;
14151
14152 sid = sli4_sid_from_fc_hdr(fc_hdr);
14153 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014154 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014155
14156 ndlp = lpfc_findnode_did(phba->pport, sid);
14157 if (!ndlp) {
14158 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14159 "1268 Find ndlp returned NULL for oxid:x%x "
14160 "SID:x%x\n", oxid, sid);
14161 return;
14162 }
14163
James Smart546fc852011-03-11 16:06:29 -050014164 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014165 ctiocb = lpfc_sli_get_iocbq(phba);
14166 if (!ctiocb)
14167 return;
14168
James Smart5ffc2662009-11-18 15:39:44 -050014169 /* Extract the F_CTL field from FC_HDR */
14170 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14171
James Smart6669f9b2009-10-02 15:16:45 -040014172 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014173 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014174 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014175 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14176 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14177 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14178
14179 /* Fill in the rest of iocb fields */
14180 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14181 icmd->ulpBdeCount = 0;
14182 icmd->ulpLe = 1;
14183 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014184 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014185 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014186
James Smart6669f9b2009-10-02 15:16:45 -040014187 ctiocb->iocb_cmpl = NULL;
14188 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014189 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014190 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014191 ctiocb->sli4_xritag = NO_XRI;
14192
James Smartee0f4fe2012-05-09 21:19:14 -040014193 if (fctl & FC_FC_EX_CTX)
14194 /* Exchange responder sent the abort so we
14195 * own the oxid.
14196 */
14197 xri = oxid;
14198 else
14199 xri = rxid;
14200 lxri = lpfc_sli4_xri_inrange(phba, xri);
14201 if (lxri != NO_XRI)
14202 lpfc_set_rrq_active(phba, ndlp, lxri,
14203 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014204 /* If the oxid maps to the FCP XRI range or if it is out of range,
14205 * send a BLS_RJT. The driver no longer has that exchange.
14206 * Override the IOCB for a BA_RJT.
14207 */
James Smartee0f4fe2012-05-09 21:19:14 -040014208 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014209 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014210 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014211 phba->sli4_hba.max_cfg_param.xri_base)) {
14212 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14213 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14214 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14215 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14216 }
James Smart6669f9b2009-10-02 15:16:45 -040014217
James Smart5ffc2662009-11-18 15:39:44 -050014218 if (fctl & FC_FC_EX_CTX) {
14219 /* ABTS sent by responder to CT exchange, construction
14220 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14221 * field and RX_ID from ABTS for RX_ID field.
14222 */
James Smart546fc852011-03-11 16:06:29 -050014223 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014224 } else {
14225 /* ABTS sent by initiator to CT exchange, construction
14226 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014227 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014228 */
James Smart546fc852011-03-11 16:06:29 -050014229 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014230 }
James Smartf09c3ac2012-03-01 22:33:29 -050014231 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014232 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014233
James Smart546fc852011-03-11 16:06:29 -050014234 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014235 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014236 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14237 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14238
14239 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14240 if (rc == IOCB_ERROR) {
14241 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14242 "2925 Failed to issue CT ABTS RSP x%x on "
14243 "xri x%x, Data x%x\n",
14244 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14245 phba->link_state);
14246 lpfc_sli_release_iocbq(phba, ctiocb);
14247 }
James Smart6669f9b2009-10-02 15:16:45 -040014248}
14249
14250/**
14251 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14252 * @vport: Pointer to the vport on which this sequence was received
14253 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14254 *
14255 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14256 * receive sequence is only partially assembed by the driver, it shall abort
14257 * the partially assembled frames for the sequence. Otherwise, if the
14258 * unsolicited receive sequence has been completely assembled and passed to
14259 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14260 * unsolicited sequence has been aborted. After that, it will issue a basic
14261 * accept to accept the abort.
14262 **/
14263void
14264lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14265 struct hbq_dmabuf *dmabuf)
14266{
14267 struct lpfc_hba *phba = vport->phba;
14268 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014269 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014270 bool abts_par;
14271
James Smart6669f9b2009-10-02 15:16:45 -040014272 /* Make a copy of fc_hdr before the dmabuf being released */
14273 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014274 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014275
James Smart5ffc2662009-11-18 15:39:44 -050014276 if (fctl & FC_FC_EX_CTX) {
14277 /*
14278 * ABTS sent by responder to exchange, just free the buffer
14279 */
James Smart6669f9b2009-10-02 15:16:45 -040014280 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014281 } else {
14282 /*
14283 * ABTS sent by initiator to exchange, need to do cleanup
14284 */
14285 /* Try to abort partially assembled seq */
14286 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14287
14288 /* Send abort to ULP if partially seq abort failed */
14289 if (abts_par == false)
14290 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14291 else
14292 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14293 }
James Smart6669f9b2009-10-02 15:16:45 -040014294 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014295 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014296}
14297
14298/**
James Smart4f774512009-05-22 14:52:35 -040014299 * lpfc_seq_complete - Indicates if a sequence is complete
14300 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14301 *
14302 * This function checks the sequence, starting with the frame described by
14303 * @dmabuf, to see if all the frames associated with this sequence are present.
14304 * the frames associated with this sequence are linked to the @dmabuf using the
14305 * dbuf list. This function looks for two major things. 1) That the first frame
14306 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14307 * set. 3) That there are no holes in the sequence count. The function will
14308 * return 1 when the sequence is complete, otherwise it will return 0.
14309 **/
14310static int
14311lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14312{
14313 struct fc_frame_header *hdr;
14314 struct lpfc_dmabuf *d_buf;
14315 struct hbq_dmabuf *seq_dmabuf;
14316 uint32_t fctl;
14317 int seq_count = 0;
14318
14319 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14320 /* make sure first fame of sequence has a sequence count of zero */
14321 if (hdr->fh_seq_cnt != seq_count)
14322 return 0;
14323 fctl = (hdr->fh_f_ctl[0] << 16 |
14324 hdr->fh_f_ctl[1] << 8 |
14325 hdr->fh_f_ctl[2]);
14326 /* If last frame of sequence we can return success. */
14327 if (fctl & FC_FC_END_SEQ)
14328 return 1;
14329 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14330 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14331 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14332 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014333 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014334 return 0;
14335 fctl = (hdr->fh_f_ctl[0] << 16 |
14336 hdr->fh_f_ctl[1] << 8 |
14337 hdr->fh_f_ctl[2]);
14338 /* If last frame of sequence we can return success. */
14339 if (fctl & FC_FC_END_SEQ)
14340 return 1;
14341 }
14342 return 0;
14343}
14344
14345/**
14346 * lpfc_prep_seq - Prep sequence for ULP processing
14347 * @vport: Pointer to the vport on which this sequence was received
14348 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14349 *
14350 * This function takes a sequence, described by a list of frames, and creates
14351 * a list of iocbq structures to describe the sequence. This iocbq list will be
14352 * used to issue to the generic unsolicited sequence handler. This routine
14353 * returns a pointer to the first iocbq in the list. If the function is unable
14354 * to allocate an iocbq then it throw out the received frames that were not
14355 * able to be described and return a pointer to the first iocbq. If unable to
14356 * allocate any iocbqs (including the first) this function will return NULL.
14357 **/
14358static struct lpfc_iocbq *
14359lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14360{
James Smart7851fe22011-07-22 18:36:52 -040014361 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014362 struct lpfc_dmabuf *d_buf, *n_buf;
14363 struct lpfc_iocbq *first_iocbq, *iocbq;
14364 struct fc_frame_header *fc_hdr;
14365 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014366 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014367 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014368
14369 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14370 /* remove from receive buffer list */
14371 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014372 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014373 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014374 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014375 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014376 /* Get an iocbq struct to fill in. */
14377 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14378 if (first_iocbq) {
14379 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014380 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014381 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014382
14383 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14384 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14385 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14386 first_iocbq->iocb.un.rcvels.parmRo =
14387 sli4_did_from_fc_hdr(fc_hdr);
14388 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14389 } else
14390 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014391 first_iocbq->iocb.ulpContext = NO_XRI;
14392 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14393 be16_to_cpu(fc_hdr->fh_ox_id);
14394 /* iocbq is prepped for internal consumption. Physical vpi. */
14395 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14396 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014397 /* put the first buffer into the first IOCBq */
14398 first_iocbq->context2 = &seq_dmabuf->dbuf;
14399 first_iocbq->context3 = NULL;
14400 first_iocbq->iocb.ulpBdeCount = 1;
14401 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14402 LPFC_DATA_BUF_SIZE;
14403 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014404 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014405 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014406 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014407 }
14408 iocbq = first_iocbq;
14409 /*
14410 * Each IOCBq can have two Buffers assigned, so go through the list
14411 * of buffers for this sequence and save two buffers in each IOCBq
14412 */
14413 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14414 if (!iocbq) {
14415 lpfc_in_buf_free(vport->phba, d_buf);
14416 continue;
14417 }
14418 if (!iocbq->context3) {
14419 iocbq->context3 = d_buf;
14420 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014421 pbde = (struct ulp_bde64 *)
14422 &iocbq->iocb.unsli3.sli3Words[4];
14423 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014424
14425 /* We need to get the size out of the right CQE */
14426 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14427 len = bf_get(lpfc_rcqe_length,
14428 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14429 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14430 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014431 } else {
14432 iocbq = lpfc_sli_get_iocbq(vport->phba);
14433 if (!iocbq) {
14434 if (first_iocbq) {
14435 first_iocbq->iocb.ulpStatus =
14436 IOSTAT_FCP_RSP_ERROR;
14437 first_iocbq->iocb.un.ulpWord[4] =
14438 IOERR_NO_RESOURCES;
14439 }
14440 lpfc_in_buf_free(vport->phba, d_buf);
14441 continue;
14442 }
14443 iocbq->context2 = d_buf;
14444 iocbq->context3 = NULL;
14445 iocbq->iocb.ulpBdeCount = 1;
14446 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14447 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014448
14449 /* We need to get the size out of the right CQE */
14450 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14451 len = bf_get(lpfc_rcqe_length,
14452 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14453 tot_len += len;
14454 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14455
James Smart4f774512009-05-22 14:52:35 -040014456 iocbq->iocb.un.rcvels.remoteID = sid;
14457 list_add_tail(&iocbq->list, &first_iocbq->list);
14458 }
14459 }
14460 return first_iocbq;
14461}
14462
James Smart6669f9b2009-10-02 15:16:45 -040014463static void
14464lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14465 struct hbq_dmabuf *seq_dmabuf)
14466{
14467 struct fc_frame_header *fc_hdr;
14468 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14469 struct lpfc_hba *phba = vport->phba;
14470
14471 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14472 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14473 if (!iocbq) {
14474 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14475 "2707 Ring %d handler: Failed to allocate "
14476 "iocb Rctl x%x Type x%x received\n",
14477 LPFC_ELS_RING,
14478 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14479 return;
14480 }
14481 if (!lpfc_complete_unsol_iocb(phba,
14482 &phba->sli.ring[LPFC_ELS_RING],
14483 iocbq, fc_hdr->fh_r_ctl,
14484 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014485 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014486 "2540 Ring %d handler: unexpected Rctl "
14487 "x%x Type x%x received\n",
14488 LPFC_ELS_RING,
14489 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14490
14491 /* Free iocb created in lpfc_prep_seq */
14492 list_for_each_entry_safe(curr_iocb, next_iocb,
14493 &iocbq->list, list) {
14494 list_del_init(&curr_iocb->list);
14495 lpfc_sli_release_iocbq(phba, curr_iocb);
14496 }
14497 lpfc_sli_release_iocbq(phba, iocbq);
14498}
14499
James Smart4f774512009-05-22 14:52:35 -040014500/**
14501 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14502 * @phba: Pointer to HBA context object.
14503 *
14504 * This function is called with no lock held. This function processes all
14505 * the received buffers and gives it to upper layers when a received buffer
14506 * indicates that it is the final frame in the sequence. The interrupt
14507 * service routine processes received buffers at interrupt contexts and adds
14508 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14509 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14510 * appropriate receive function when the final frame in a sequence is received.
14511 **/
James Smart4d9ab992009-10-02 15:16:39 -040014512void
14513lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14514 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014515{
James Smart4d9ab992009-10-02 15:16:39 -040014516 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014517 struct fc_frame_header *fc_hdr;
14518 struct lpfc_vport *vport;
14519 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014520 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014521
James Smart4f774512009-05-22 14:52:35 -040014522 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014523 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14524 /* check to see if this a valid type of frame */
14525 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14526 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14527 return;
14528 }
James Smart7851fe22011-07-22 18:36:52 -040014529 if ((bf_get(lpfc_cqe_code,
14530 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14531 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14532 &dmabuf->cq_event.cqe.rcqe_cmpl);
14533 else
14534 fcfi = bf_get(lpfc_rcqe_fcf_id,
14535 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014536
James Smart4d9ab992009-10-02 15:16:39 -040014537 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014538 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014539 /* throw out the frame */
14540 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14541 return;
14542 }
James Smart939723a2012-05-09 21:19:03 -040014543
14544 /* d_id this frame is directed to */
14545 did = sli4_did_from_fc_hdr(fc_hdr);
14546
14547 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14548 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14549 (did != Fabric_DID)) {
14550 /*
14551 * Throw out the frame if we are not pt2pt.
14552 * The pt2pt protocol allows for discovery frames
14553 * to be received without a registered VPI.
14554 */
14555 if (!(vport->fc_flag & FC_PT2PT) ||
14556 (phba->link_state == LPFC_HBA_READY)) {
14557 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14558 return;
14559 }
14560 }
14561
James Smart6669f9b2009-10-02 15:16:45 -040014562 /* Handle the basic abort sequence (BA_ABTS) event */
14563 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14564 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14565 return;
14566 }
14567
James Smart4d9ab992009-10-02 15:16:39 -040014568 /* Link this frame */
14569 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14570 if (!seq_dmabuf) {
14571 /* unable to add frame to vport - throw it out */
14572 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14573 return;
14574 }
14575 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014576 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014577 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014578
James Smart6669f9b2009-10-02 15:16:45 -040014579 /* Send the complete sequence to the upper layer protocol */
14580 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014581}
James Smart6fb120a2009-05-22 14:52:59 -040014582
14583/**
14584 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14585 * @phba: pointer to lpfc hba data structure.
14586 *
14587 * This routine is invoked to post rpi header templates to the
14588 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014589 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14590 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014591 *
14592 * This routine does not require any locks. It's usage is expected
14593 * to be driver load or reset recovery when the driver is
14594 * sequential.
14595 *
14596 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014597 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014598 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014599 * When this error occurs, the driver is not guaranteed
14600 * to have any rpi regions posted to the device and
14601 * must either attempt to repost the regions or take a
14602 * fatal error.
14603 **/
14604int
14605lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14606{
14607 struct lpfc_rpi_hdr *rpi_page;
14608 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014609 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014610
James Smart6d368e52011-05-24 11:44:12 -040014611 /* SLI4 ports that support extents do not require RPI headers. */
14612 if (!phba->sli4_hba.rpi_hdrs_in_use)
14613 goto exit;
14614 if (phba->sli4_hba.extents_in_use)
14615 return -EIO;
14616
James Smart6fb120a2009-05-22 14:52:59 -040014617 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014618 /*
14619 * Assign the rpi headers a physical rpi only if the driver
14620 * has not initialized those resources. A port reset only
14621 * needs the headers posted.
14622 */
14623 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14624 LPFC_RPI_RSRC_RDY)
14625 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14626
James Smart6fb120a2009-05-22 14:52:59 -040014627 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14628 if (rc != MBX_SUCCESS) {
14629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14630 "2008 Error %d posting all rpi "
14631 "headers\n", rc);
14632 rc = -EIO;
14633 break;
14634 }
14635 }
14636
James Smart6d368e52011-05-24 11:44:12 -040014637 exit:
14638 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14639 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014640 return rc;
14641}
14642
14643/**
14644 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14645 * @phba: pointer to lpfc hba data structure.
14646 * @rpi_page: pointer to the rpi memory region.
14647 *
14648 * This routine is invoked to post a single rpi header to the
14649 * HBA consistent with the SLI-4 interface spec. This memory region
14650 * maps up to 64 rpi context regions.
14651 *
14652 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014653 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014654 * -ENOMEM - No available memory
14655 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014656 **/
14657int
14658lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14659{
14660 LPFC_MBOXQ_t *mboxq;
14661 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14662 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014663 uint32_t shdr_status, shdr_add_status;
14664 union lpfc_sli4_cfg_shdr *shdr;
14665
James Smart6d368e52011-05-24 11:44:12 -040014666 /* SLI4 ports that support extents do not require RPI headers. */
14667 if (!phba->sli4_hba.rpi_hdrs_in_use)
14668 return rc;
14669 if (phba->sli4_hba.extents_in_use)
14670 return -EIO;
14671
James Smart6fb120a2009-05-22 14:52:59 -040014672 /* The port is notified of the header region via a mailbox command. */
14673 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14674 if (!mboxq) {
14675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14676 "2001 Unable to allocate memory for issuing "
14677 "SLI_CONFIG_SPECIAL mailbox command\n");
14678 return -ENOMEM;
14679 }
14680
14681 /* Post all rpi memory regions to the port. */
14682 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014683 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14684 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14685 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014686 sizeof(struct lpfc_sli4_cfg_mhdr),
14687 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014688
14689
14690 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014691 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14692 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014693 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14694 hdr_tmpl, rpi_page->page_count);
14695
James Smart6fb120a2009-05-22 14:52:59 -040014696 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14697 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014698 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014699 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14700 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14702 if (rc != MBX_TIMEOUT)
14703 mempool_free(mboxq, phba->mbox_mem_pool);
14704 if (shdr_status || shdr_add_status || rc) {
14705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14706 "2514 POST_RPI_HDR mailbox failed with "
14707 "status x%x add_status x%x, mbx status x%x\n",
14708 shdr_status, shdr_add_status, rc);
14709 rc = -ENXIO;
14710 }
14711 return rc;
14712}
14713
14714/**
14715 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14716 * @phba: pointer to lpfc hba data structure.
14717 *
14718 * This routine is invoked to post rpi header templates to the
14719 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014720 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14721 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014722 *
14723 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014724 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014725 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14726 **/
14727int
14728lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14729{
James Smart6d368e52011-05-24 11:44:12 -040014730 unsigned long rpi;
14731 uint16_t max_rpi, rpi_limit;
14732 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014733 struct lpfc_rpi_hdr *rpi_hdr;
14734
14735 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014736 rpi_limit = phba->sli4_hba.next_rpi;
14737
14738 /*
James Smart6d368e52011-05-24 11:44:12 -040014739 * Fetch the next logical rpi. Because this index is logical,
14740 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014741 */
14742 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014743 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14744 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014745 rpi = LPFC_RPI_ALLOC_ERROR;
14746 else {
14747 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14748 phba->sli4_hba.max_cfg_param.rpi_used++;
14749 phba->sli4_hba.rpi_count++;
14750 }
14751
14752 /*
14753 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014754 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014755 */
14756 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14757 (phba->sli4_hba.rpi_count >= max_rpi)) {
14758 spin_unlock_irq(&phba->hbalock);
14759 return rpi;
14760 }
14761
14762 /*
James Smart6d368e52011-05-24 11:44:12 -040014763 * RPI header postings are not required for SLI4 ports capable of
14764 * extents.
14765 */
14766 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14767 spin_unlock_irq(&phba->hbalock);
14768 return rpi;
14769 }
14770
14771 /*
James Smart6fb120a2009-05-22 14:52:59 -040014772 * If the driver is running low on rpi resources, allocate another
14773 * page now. Note that the next_rpi value is used because
14774 * it represents how many are actually in use whereas max_rpi notes
14775 * how many are supported max by the device.
14776 */
James Smart6d368e52011-05-24 11:44:12 -040014777 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014778 spin_unlock_irq(&phba->hbalock);
14779 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14780 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14781 if (!rpi_hdr) {
14782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14783 "2002 Error Could not grow rpi "
14784 "count\n");
14785 } else {
James Smart6d368e52011-05-24 11:44:12 -040014786 lrpi = rpi_hdr->start_rpi;
14787 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014788 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14789 }
14790 }
14791
14792 return rpi;
14793}
14794
14795/**
14796 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14797 * @phba: pointer to lpfc hba data structure.
14798 *
14799 * This routine is invoked to release an rpi to the pool of
14800 * available rpis maintained by the driver.
14801 **/
14802void
James Smartd7c47992010-06-08 18:31:54 -040014803__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14804{
14805 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14806 phba->sli4_hba.rpi_count--;
14807 phba->sli4_hba.max_cfg_param.rpi_used--;
14808 }
14809}
14810
14811/**
14812 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14813 * @phba: pointer to lpfc hba data structure.
14814 *
14815 * This routine is invoked to release an rpi to the pool of
14816 * available rpis maintained by the driver.
14817 **/
14818void
James Smart6fb120a2009-05-22 14:52:59 -040014819lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14820{
14821 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014822 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014823 spin_unlock_irq(&phba->hbalock);
14824}
14825
14826/**
14827 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14828 * @phba: pointer to lpfc hba data structure.
14829 *
14830 * This routine is invoked to remove the memory region that
14831 * provided rpi via a bitmask.
14832 **/
14833void
14834lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14835{
14836 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014837 kfree(phba->sli4_hba.rpi_ids);
14838 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014839}
14840
14841/**
14842 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14843 * @phba: pointer to lpfc hba data structure.
14844 *
14845 * This routine is invoked to remove the memory region that
14846 * provided rpi via a bitmask.
14847 **/
14848int
James Smart6b5151f2012-01-18 16:24:06 -050014849lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14850 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014851{
14852 LPFC_MBOXQ_t *mboxq;
14853 struct lpfc_hba *phba = ndlp->phba;
14854 int rc;
14855
14856 /* The port is notified of the header region via a mailbox command. */
14857 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14858 if (!mboxq)
14859 return -ENOMEM;
14860
14861 /* Post all rpi memory regions to the port. */
14862 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014863 if (cmpl) {
14864 mboxq->mbox_cmpl = cmpl;
14865 mboxq->context1 = arg;
14866 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014867 } else
14868 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014869 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014870 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14871 if (rc == MBX_NOT_FINISHED) {
14872 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14873 "2010 Resume RPI Mailbox failed "
14874 "status %d, mbxStatus x%x\n", rc,
14875 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14876 mempool_free(mboxq, phba->mbox_mem_pool);
14877 return -EIO;
14878 }
14879 return 0;
14880}
14881
14882/**
14883 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014884 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014885 *
James Smart76a95d72010-11-20 23:11:48 -050014886 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014887 *
14888 * Returns:
14889 * 0 success
14890 * -Evalue otherwise
14891 **/
14892int
James Smart76a95d72010-11-20 23:11:48 -050014893lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014894{
14895 LPFC_MBOXQ_t *mboxq;
14896 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014897 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014898 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014899 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014900 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14901 if (!mboxq)
14902 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014903 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014904 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014905 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014906 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014907 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014908 "2022 INIT VPI Mailbox failed "
14909 "status %d, mbxStatus x%x\n", rc,
14910 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014911 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014912 }
James Smart6a9c52c2009-10-02 15:16:51 -040014913 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014914 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014915
14916 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014917}
14918
14919/**
14920 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14921 * @phba: pointer to lpfc hba data structure.
14922 * @mboxq: Pointer to mailbox object.
14923 *
14924 * This routine is invoked to manually add a single FCF record. The caller
14925 * must pass a completely initialized FCF_Record. This routine takes
14926 * care of the nonembedded mailbox operations.
14927 **/
14928static void
14929lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14930{
14931 void *virt_addr;
14932 union lpfc_sli4_cfg_shdr *shdr;
14933 uint32_t shdr_status, shdr_add_status;
14934
14935 virt_addr = mboxq->sge_array->addr[0];
14936 /* The IOCTL status is embedded in the mailbox subheader. */
14937 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14938 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14939 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14940
14941 if ((shdr_status || shdr_add_status) &&
14942 (shdr_status != STATUS_FCF_IN_USE))
14943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14944 "2558 ADD_FCF_RECORD mailbox failed with "
14945 "status x%x add_status x%x\n",
14946 shdr_status, shdr_add_status);
14947
14948 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14949}
14950
14951/**
14952 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14953 * @phba: pointer to lpfc hba data structure.
14954 * @fcf_record: pointer to the initialized fcf record to add.
14955 *
14956 * This routine is invoked to manually add a single FCF record. The caller
14957 * must pass a completely initialized FCF_Record. This routine takes
14958 * care of the nonembedded mailbox operations.
14959 **/
14960int
14961lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14962{
14963 int rc = 0;
14964 LPFC_MBOXQ_t *mboxq;
14965 uint8_t *bytep;
14966 void *virt_addr;
14967 dma_addr_t phys_addr;
14968 struct lpfc_mbx_sge sge;
14969 uint32_t alloc_len, req_len;
14970 uint32_t fcfindex;
14971
14972 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14973 if (!mboxq) {
14974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14975 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14976 return -ENOMEM;
14977 }
14978
14979 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14980 sizeof(uint32_t);
14981
14982 /* Allocate DMA memory and set up the non-embedded mailbox command */
14983 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14984 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14985 req_len, LPFC_SLI4_MBX_NEMBED);
14986 if (alloc_len < req_len) {
14987 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14988 "2523 Allocated DMA memory size (x%x) is "
14989 "less than the requested DMA memory "
14990 "size (x%x)\n", alloc_len, req_len);
14991 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14992 return -ENOMEM;
14993 }
14994
14995 /*
14996 * Get the first SGE entry from the non-embedded DMA memory. This
14997 * routine only uses a single SGE.
14998 */
14999 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15000 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040015001 virt_addr = mboxq->sge_array->addr[0];
15002 /*
15003 * Configure the FCF record for FCFI 0. This is the driver's
15004 * hardcoded default and gets used in nonFIP mode.
15005 */
15006 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15007 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15008 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15009
15010 /*
15011 * Copy the fcf_index and the FCF Record Data. The data starts after
15012 * the FCoE header plus word10. The data copy needs to be endian
15013 * correct.
15014 */
15015 bytep += sizeof(uint32_t);
15016 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15017 mboxq->vport = phba->pport;
15018 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15019 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15020 if (rc == MBX_NOT_FINISHED) {
15021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15022 "2515 ADD_FCF_RECORD mailbox failed with "
15023 "status 0x%x\n", rc);
15024 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15025 rc = -EIO;
15026 } else
15027 rc = 0;
15028
15029 return rc;
15030}
15031
15032/**
15033 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15034 * @phba: pointer to lpfc hba data structure.
15035 * @fcf_record: pointer to the fcf record to write the default data.
15036 * @fcf_index: FCF table entry index.
15037 *
15038 * This routine is invoked to build the driver's default FCF record. The
15039 * values used are hardcoded. This routine handles memory initialization.
15040 *
15041 **/
15042void
15043lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15044 struct fcf_record *fcf_record,
15045 uint16_t fcf_index)
15046{
15047 memset(fcf_record, 0, sizeof(struct fcf_record));
15048 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15049 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15050 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15051 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15052 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15053 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15054 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15055 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15056 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15057 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15058 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15059 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15060 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015061 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015062 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15063 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15064 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15065 /* Set the VLAN bit map */
15066 if (phba->valid_vlan) {
15067 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15068 = 1 << (phba->vlan_id % 8);
15069 }
15070}
15071
15072/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015073 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015074 * @phba: pointer to lpfc hba data structure.
15075 * @fcf_index: FCF table entry offset.
15076 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015077 * This routine is invoked to scan the entire FCF table by reading FCF
15078 * record and processing it one at a time starting from the @fcf_index
15079 * for initial FCF discovery or fast FCF failover rediscovery.
15080 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015081 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015082 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015083 **/
15084int
James Smart0c9ab6f2010-02-26 14:15:57 -050015085lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015086{
15087 int rc = 0, error;
15088 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015089
James Smart32b97932009-07-19 10:01:21 -040015090 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015091 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015092 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15093 if (!mboxq) {
15094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15095 "2000 Failed to allocate mbox for "
15096 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015097 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015098 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015099 }
James Smartecfd03c2010-02-12 14:41:27 -050015100 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015101 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015102 if (rc) {
15103 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015104 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015105 }
James Smartecfd03c2010-02-12 14:41:27 -050015106 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015107 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015108 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015109
15110 spin_lock_irq(&phba->hbalock);
15111 phba->hba_flag |= FCF_TS_INPROG;
15112 spin_unlock_irq(&phba->hbalock);
15113
James Smart6fb120a2009-05-22 14:52:59 -040015114 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015115 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015116 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015117 else {
James Smart38b92ef2010-08-04 16:11:39 -040015118 /* Reset eligible FCF count for new scan */
15119 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015120 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015121 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015122 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015123fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015124 if (error) {
15125 if (mboxq)
15126 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015127 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015128 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015129 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015130 spin_unlock_irq(&phba->hbalock);
15131 }
James Smart6fb120a2009-05-22 14:52:59 -040015132 return error;
15133}
James Smarta0c87cb2009-07-19 10:01:10 -040015134
15135/**
James Smarta93ff372010-10-22 11:06:08 -040015136 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015137 * @phba: pointer to lpfc hba data structure.
15138 * @fcf_index: FCF table entry offset.
15139 *
15140 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015141 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015142 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015143 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015144 * otherwise.
15145 **/
15146int
15147lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15148{
15149 int rc = 0, error;
15150 LPFC_MBOXQ_t *mboxq;
15151
15152 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15153 if (!mboxq) {
15154 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15155 "2763 Failed to allocate mbox for "
15156 "READ_FCF cmd\n");
15157 error = -ENOMEM;
15158 goto fail_fcf_read;
15159 }
15160 /* Construct the read FCF record mailbox command */
15161 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15162 if (rc) {
15163 error = -EINVAL;
15164 goto fail_fcf_read;
15165 }
15166 /* Issue the mailbox command asynchronously */
15167 mboxq->vport = phba->pport;
15168 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15169 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15170 if (rc == MBX_NOT_FINISHED)
15171 error = -EIO;
15172 else
15173 error = 0;
15174
15175fail_fcf_read:
15176 if (error && mboxq)
15177 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15178 return error;
15179}
15180
15181/**
15182 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15183 * @phba: pointer to lpfc hba data structure.
15184 * @fcf_index: FCF table entry offset.
15185 *
15186 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015187 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015188 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015189 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015190 * otherwise.
15191 **/
15192int
15193lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15194{
15195 int rc = 0, error;
15196 LPFC_MBOXQ_t *mboxq;
15197
15198 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15199 if (!mboxq) {
15200 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15201 "2758 Failed to allocate mbox for "
15202 "READ_FCF cmd\n");
15203 error = -ENOMEM;
15204 goto fail_fcf_read;
15205 }
15206 /* Construct the read FCF record mailbox command */
15207 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15208 if (rc) {
15209 error = -EINVAL;
15210 goto fail_fcf_read;
15211 }
15212 /* Issue the mailbox command asynchronously */
15213 mboxq->vport = phba->pport;
15214 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15215 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15216 if (rc == MBX_NOT_FINISHED)
15217 error = -EIO;
15218 else
15219 error = 0;
15220
15221fail_fcf_read:
15222 if (error && mboxq)
15223 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15224 return error;
15225}
15226
15227/**
James Smart7d791df2011-07-22 18:37:52 -040015228 * lpfc_check_next_fcf_pri
15229 * phba pointer to the lpfc_hba struct for this port.
15230 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15231 * routine when the rr_bmask is empty. The FCF indecies are put into the
15232 * rr_bmask based on their priority level. Starting from the highest priority
15233 * to the lowest. The most likely FCF candidate will be in the highest
15234 * priority group. When this routine is called it searches the fcf_pri list for
15235 * next lowest priority group and repopulates the rr_bmask with only those
15236 * fcf_indexes.
15237 * returns:
15238 * 1=success 0=failure
15239 **/
15240int
15241lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15242{
15243 uint16_t next_fcf_pri;
15244 uint16_t last_index;
15245 struct lpfc_fcf_pri *fcf_pri;
15246 int rc;
15247 int ret = 0;
15248
15249 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15250 LPFC_SLI4_FCF_TBL_INDX_MAX);
15251 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15252 "3060 Last IDX %d\n", last_index);
15253 if (list_empty(&phba->fcf.fcf_pri_list)) {
15254 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15255 "3061 Last IDX %d\n", last_index);
15256 return 0; /* Empty rr list */
15257 }
15258 next_fcf_pri = 0;
15259 /*
15260 * Clear the rr_bmask and set all of the bits that are at this
15261 * priority.
15262 */
15263 memset(phba->fcf.fcf_rr_bmask, 0,
15264 sizeof(*phba->fcf.fcf_rr_bmask));
15265 spin_lock_irq(&phba->hbalock);
15266 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15267 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15268 continue;
15269 /*
15270 * the 1st priority that has not FLOGI failed
15271 * will be the highest.
15272 */
15273 if (!next_fcf_pri)
15274 next_fcf_pri = fcf_pri->fcf_rec.priority;
15275 spin_unlock_irq(&phba->hbalock);
15276 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15277 rc = lpfc_sli4_fcf_rr_index_set(phba,
15278 fcf_pri->fcf_rec.fcf_index);
15279 if (rc)
15280 return 0;
15281 }
15282 spin_lock_irq(&phba->hbalock);
15283 }
15284 /*
15285 * if next_fcf_pri was not set above and the list is not empty then
15286 * we have failed flogis on all of them. So reset flogi failed
15287 * and start at the begining.
15288 */
15289 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15290 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15291 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15292 /*
15293 * the 1st priority that has not FLOGI failed
15294 * will be the highest.
15295 */
15296 if (!next_fcf_pri)
15297 next_fcf_pri = fcf_pri->fcf_rec.priority;
15298 spin_unlock_irq(&phba->hbalock);
15299 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15300 rc = lpfc_sli4_fcf_rr_index_set(phba,
15301 fcf_pri->fcf_rec.fcf_index);
15302 if (rc)
15303 return 0;
15304 }
15305 spin_lock_irq(&phba->hbalock);
15306 }
15307 } else
15308 ret = 1;
15309 spin_unlock_irq(&phba->hbalock);
15310
15311 return ret;
15312}
15313/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015314 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15315 * @phba: pointer to lpfc hba data structure.
15316 *
15317 * This routine is to get the next eligible FCF record index in a round
15318 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015319 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015320 * shall be returned, otherwise, the next eligible FCF record's index
15321 * shall be returned.
15322 **/
15323uint16_t
15324lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15325{
15326 uint16_t next_fcf_index;
15327
James Smart3804dc82010-07-14 15:31:37 -040015328 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015329next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015330 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15331 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015332 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15333 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015334 next_fcf_index);
15335
James Smart0c9ab6f2010-02-26 14:15:57 -050015336 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015337 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15338 /*
15339 * If we have wrapped then we need to clear the bits that
15340 * have been tested so that we can detect when we should
15341 * change the priority level.
15342 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015343 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15344 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015345 }
15346
James Smart0c9ab6f2010-02-26 14:15:57 -050015347
James Smart3804dc82010-07-14 15:31:37 -040015348 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015349 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15350 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15351 /*
15352 * If next fcf index is not found check if there are lower
15353 * Priority level fcf's in the fcf_priority list.
15354 * Set up the rr_bmask with all of the avaiable fcf bits
15355 * at that level and continue the selection process.
15356 */
15357 if (lpfc_check_next_fcf_pri_level(phba))
15358 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015359 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15360 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015361 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15362 return LPFC_FCOE_FCF_NEXT_NONE;
15363 else {
15364 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15365 "3063 Only FCF available idx %d, flag %x\n",
15366 next_fcf_index,
15367 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15368 return next_fcf_index;
15369 }
James Smart3804dc82010-07-14 15:31:37 -040015370 }
15371
James Smart7d791df2011-07-22 18:37:52 -040015372 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15373 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15374 LPFC_FCF_FLOGI_FAILED)
15375 goto next_priority;
15376
James Smart3804dc82010-07-14 15:31:37 -040015377 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015378 "2845 Get next roundrobin failover FCF (x%x)\n",
15379 next_fcf_index);
15380
James Smart0c9ab6f2010-02-26 14:15:57 -050015381 return next_fcf_index;
15382}
15383
15384/**
15385 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15386 * @phba: pointer to lpfc hba data structure.
15387 *
15388 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015389 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015390 * does not go beyond the range of the driver allocated bmask dimension
15391 * before setting the bit.
15392 *
15393 * Returns 0 if the index bit successfully set, otherwise, it returns
15394 * -EINVAL.
15395 **/
15396int
15397lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15398{
15399 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15400 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015401 "2610 FCF (x%x) reached driver's book "
15402 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015403 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15404 return -EINVAL;
15405 }
15406 /* Set the eligible FCF record index bmask */
15407 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15408
James Smart3804dc82010-07-14 15:31:37 -040015409 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015410 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015411 "bmask\n", fcf_index);
15412
James Smart0c9ab6f2010-02-26 14:15:57 -050015413 return 0;
15414}
15415
15416/**
James Smart3804dc82010-07-14 15:31:37 -040015417 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015418 * @phba: pointer to lpfc hba data structure.
15419 *
15420 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015421 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015422 * does not go beyond the range of the driver allocated bmask dimension
15423 * before clearing the bit.
15424 **/
15425void
15426lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15427{
James Smart7d791df2011-07-22 18:37:52 -040015428 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015429 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15430 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015431 "2762 FCF (x%x) reached driver's book "
15432 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015433 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15434 return;
15435 }
15436 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015437 spin_lock_irq(&phba->hbalock);
15438 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15439 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15440 list_del_init(&fcf_pri->list);
15441 break;
15442 }
15443 }
15444 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015445 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015446
15447 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015448 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015449 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015450}
15451
15452/**
James Smartecfd03c2010-02-12 14:41:27 -050015453 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15454 * @phba: pointer to lpfc hba data structure.
15455 *
15456 * This routine is the completion routine for the rediscover FCF table mailbox
15457 * command. If the mailbox command returned failure, it will try to stop the
15458 * FCF rediscover wait timer.
15459 **/
15460void
15461lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15462{
15463 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15464 uint32_t shdr_status, shdr_add_status;
15465
15466 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15467
15468 shdr_status = bf_get(lpfc_mbox_hdr_status,
15469 &redisc_fcf->header.cfg_shdr.response);
15470 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15471 &redisc_fcf->header.cfg_shdr.response);
15472 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015473 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015474 "2746 Requesting for FCF rediscovery failed "
15475 "status x%x add_status x%x\n",
15476 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015477 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015478 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015479 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015480 spin_unlock_irq(&phba->hbalock);
15481 /*
15482 * CVL event triggered FCF rediscover request failed,
15483 * last resort to re-try current registered FCF entry.
15484 */
15485 lpfc_retry_pport_discovery(phba);
15486 } else {
15487 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015488 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015489 spin_unlock_irq(&phba->hbalock);
15490 /*
15491 * DEAD FCF event triggered FCF rediscover request
15492 * failed, last resort to fail over as a link down
15493 * to FCF registration.
15494 */
15495 lpfc_sli4_fcf_dead_failthrough(phba);
15496 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015497 } else {
15498 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015499 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015500 /*
15501 * Start FCF rediscovery wait timer for pending FCF
15502 * before rescan FCF record table.
15503 */
15504 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015505 }
James Smartecfd03c2010-02-12 14:41:27 -050015506
15507 mempool_free(mbox, phba->mbox_mem_pool);
15508}
15509
15510/**
James Smart3804dc82010-07-14 15:31:37 -040015511 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015512 * @phba: pointer to lpfc hba data structure.
15513 *
15514 * This routine is invoked to request for rediscovery of the entire FCF table
15515 * by the port.
15516 **/
15517int
15518lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15519{
15520 LPFC_MBOXQ_t *mbox;
15521 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15522 int rc, length;
15523
James Smart0c9ab6f2010-02-26 14:15:57 -050015524 /* Cancel retry delay timers to all vports before FCF rediscover */
15525 lpfc_cancel_all_vport_retry_delay_timer(phba);
15526
James Smartecfd03c2010-02-12 14:41:27 -050015527 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15528 if (!mbox) {
15529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15530 "2745 Failed to allocate mbox for "
15531 "requesting FCF rediscover.\n");
15532 return -ENOMEM;
15533 }
15534
15535 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15536 sizeof(struct lpfc_sli4_cfg_mhdr));
15537 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15538 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15539 length, LPFC_SLI4_MBX_EMBED);
15540
15541 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15542 /* Set count to 0 for invalidating the entire FCF database */
15543 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15544
15545 /* Issue the mailbox command asynchronously */
15546 mbox->vport = phba->pport;
15547 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15548 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15549
15550 if (rc == MBX_NOT_FINISHED) {
15551 mempool_free(mbox, phba->mbox_mem_pool);
15552 return -EIO;
15553 }
15554 return 0;
15555}
15556
15557/**
James Smartfc2b9892010-02-26 14:15:29 -050015558 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15559 * @phba: pointer to lpfc hba data structure.
15560 *
15561 * This function is the failover routine as a last resort to the FCF DEAD
15562 * event when driver failed to perform fast FCF failover.
15563 **/
15564void
15565lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15566{
15567 uint32_t link_state;
15568
15569 /*
15570 * Last resort as FCF DEAD event failover will treat this as
15571 * a link down, but save the link state because we don't want
15572 * it to be changed to Link Down unless it is already down.
15573 */
15574 link_state = phba->link_state;
15575 lpfc_linkdown(phba);
15576 phba->link_state = link_state;
15577
15578 /* Unregister FCF if no devices connected to it */
15579 lpfc_unregister_unused_fcf(phba);
15580}
15581
15582/**
James Smart026abb82011-12-13 13:20:45 -050015583 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015584 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015585 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015586 *
James Smart026abb82011-12-13 13:20:45 -050015587 * This function gets SLI3 port configure region 23 data through memory dump
15588 * mailbox command. When it successfully retrieves data, the size of the data
15589 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015590 **/
James Smart026abb82011-12-13 13:20:45 -050015591static uint32_t
15592lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015593{
15594 LPFC_MBOXQ_t *pmb = NULL;
15595 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015596 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015597 int rc;
15598
James Smart026abb82011-12-13 13:20:45 -050015599 if (!rgn23_data)
15600 return 0;
15601
James Smarta0c87cb2009-07-19 10:01:10 -040015602 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15603 if (!pmb) {
15604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015605 "2600 failed to allocate mailbox memory\n");
15606 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015607 }
15608 mb = &pmb->u.mb;
15609
James Smarta0c87cb2009-07-19 10:01:10 -040015610 do {
15611 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15612 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15613
15614 if (rc != MBX_SUCCESS) {
15615 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015616 "2601 failed to read config "
15617 "region 23, rc 0x%x Status 0x%x\n",
15618 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015619 mb->un.varDmp.word_cnt = 0;
15620 }
15621 /*
15622 * dump mem may return a zero when finished or we got a
15623 * mailbox error, either way we are done.
15624 */
15625 if (mb->un.varDmp.word_cnt == 0)
15626 break;
15627 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15628 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15629
15630 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015631 rgn23_data + offset,
15632 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015633 offset += mb->un.varDmp.word_cnt;
15634 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15635
James Smart026abb82011-12-13 13:20:45 -050015636 mempool_free(pmb, phba->mbox_mem_pool);
15637 return offset;
15638}
15639
15640/**
15641 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15642 * @phba: pointer to lpfc hba data structure.
15643 * @rgn23_data: pointer to configure region 23 data.
15644 *
15645 * This function gets SLI4 port configure region 23 data through memory dump
15646 * mailbox command. When it successfully retrieves data, the size of the data
15647 * will be returned, otherwise, 0 will be returned.
15648 **/
15649static uint32_t
15650lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15651{
15652 LPFC_MBOXQ_t *mboxq = NULL;
15653 struct lpfc_dmabuf *mp = NULL;
15654 struct lpfc_mqe *mqe;
15655 uint32_t data_length = 0;
15656 int rc;
15657
15658 if (!rgn23_data)
15659 return 0;
15660
15661 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15662 if (!mboxq) {
15663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15664 "3105 failed to allocate mailbox memory\n");
15665 return 0;
15666 }
15667
15668 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15669 goto out;
15670 mqe = &mboxq->u.mqe;
15671 mp = (struct lpfc_dmabuf *) mboxq->context1;
15672 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15673 if (rc)
15674 goto out;
15675 data_length = mqe->un.mb_words[5];
15676 if (data_length == 0)
15677 goto out;
15678 if (data_length > DMP_RGN23_SIZE) {
15679 data_length = 0;
15680 goto out;
15681 }
15682 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15683out:
15684 mempool_free(mboxq, phba->mbox_mem_pool);
15685 if (mp) {
15686 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15687 kfree(mp);
15688 }
15689 return data_length;
15690}
15691
15692/**
15693 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15694 * @phba: pointer to lpfc hba data structure.
15695 *
15696 * This function read region 23 and parse TLV for port status to
15697 * decide if the user disaled the port. If the TLV indicates the
15698 * port is disabled, the hba_flag is set accordingly.
15699 **/
15700void
15701lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15702{
15703 uint8_t *rgn23_data = NULL;
15704 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15705 uint32_t offset = 0;
15706
15707 /* Get adapter Region 23 data */
15708 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15709 if (!rgn23_data)
15710 goto out;
15711
15712 if (phba->sli_rev < LPFC_SLI_REV4)
15713 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15714 else {
15715 if_type = bf_get(lpfc_sli_intf_if_type,
15716 &phba->sli4_hba.sli_intf);
15717 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15718 goto out;
15719 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15720 }
James Smarta0c87cb2009-07-19 10:01:10 -040015721
15722 if (!data_size)
15723 goto out;
15724
15725 /* Check the region signature first */
15726 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15728 "2619 Config region 23 has bad signature\n");
15729 goto out;
15730 }
15731 offset += 4;
15732
15733 /* Check the data structure version */
15734 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15736 "2620 Config region 23 has bad version\n");
15737 goto out;
15738 }
15739 offset += 4;
15740
15741 /* Parse TLV entries in the region */
15742 while (offset < data_size) {
15743 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15744 break;
15745 /*
15746 * If the TLV is not driver specific TLV or driver id is
15747 * not linux driver id, skip the record.
15748 */
15749 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15750 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15751 (rgn23_data[offset + 3] != 0)) {
15752 offset += rgn23_data[offset + 1] * 4 + 4;
15753 continue;
15754 }
15755
15756 /* Driver found a driver specific TLV in the config region */
15757 sub_tlv_len = rgn23_data[offset + 1] * 4;
15758 offset += 4;
15759 tlv_offset = 0;
15760
15761 /*
15762 * Search for configured port state sub-TLV.
15763 */
15764 while ((offset < data_size) &&
15765 (tlv_offset < sub_tlv_len)) {
15766 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15767 offset += 4;
15768 tlv_offset += 4;
15769 break;
15770 }
15771 if (rgn23_data[offset] != PORT_STE_TYPE) {
15772 offset += rgn23_data[offset + 1] * 4 + 4;
15773 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15774 continue;
15775 }
15776
15777 /* This HBA contains PORT_STE configured */
15778 if (!rgn23_data[offset + 2])
15779 phba->hba_flag |= LINK_DISABLED;
15780
15781 goto out;
15782 }
15783 }
James Smart026abb82011-12-13 13:20:45 -050015784
James Smarta0c87cb2009-07-19 10:01:10 -040015785out:
James Smarta0c87cb2009-07-19 10:01:10 -040015786 kfree(rgn23_data);
15787 return;
15788}
James Smart695a8142010-01-26 23:08:03 -050015789
15790/**
James Smart52d52442011-05-24 11:42:45 -040015791 * lpfc_wr_object - write an object to the firmware
15792 * @phba: HBA structure that indicates port to create a queue on.
15793 * @dmabuf_list: list of dmabufs to write to the port.
15794 * @size: the total byte value of the objects to write to the port.
15795 * @offset: the current offset to be used to start the transfer.
15796 *
15797 * This routine will create a wr_object mailbox command to send to the port.
15798 * the mailbox command will be constructed using the dma buffers described in
15799 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15800 * BDEs that the imbedded mailbox can support. The @offset variable will be
15801 * used to indicate the starting offset of the transfer and will also return
15802 * the offset after the write object mailbox has completed. @size is used to
15803 * determine the end of the object and whether the eof bit should be set.
15804 *
15805 * Return 0 is successful and offset will contain the the new offset to use
15806 * for the next write.
15807 * Return negative value for error cases.
15808 **/
15809int
15810lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15811 uint32_t size, uint32_t *offset)
15812{
15813 struct lpfc_mbx_wr_object *wr_object;
15814 LPFC_MBOXQ_t *mbox;
15815 int rc = 0, i = 0;
15816 uint32_t shdr_status, shdr_add_status;
15817 uint32_t mbox_tmo;
15818 union lpfc_sli4_cfg_shdr *shdr;
15819 struct lpfc_dmabuf *dmabuf;
15820 uint32_t written = 0;
15821
15822 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15823 if (!mbox)
15824 return -ENOMEM;
15825
15826 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15827 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15828 sizeof(struct lpfc_mbx_wr_object) -
15829 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15830
15831 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15832 wr_object->u.request.write_offset = *offset;
15833 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15834 wr_object->u.request.object_name[0] =
15835 cpu_to_le32(wr_object->u.request.object_name[0]);
15836 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15837 list_for_each_entry(dmabuf, dmabuf_list, list) {
15838 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15839 break;
15840 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15841 wr_object->u.request.bde[i].addrHigh =
15842 putPaddrHigh(dmabuf->phys);
15843 if (written + SLI4_PAGE_SIZE >= size) {
15844 wr_object->u.request.bde[i].tus.f.bdeSize =
15845 (size - written);
15846 written += (size - written);
15847 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15848 } else {
15849 wr_object->u.request.bde[i].tus.f.bdeSize =
15850 SLI4_PAGE_SIZE;
15851 written += SLI4_PAGE_SIZE;
15852 }
15853 i++;
15854 }
15855 wr_object->u.request.bde_count = i;
15856 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15857 if (!phba->sli4_hba.intr_enable)
15858 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15859 else {
James Smarta183a152011-10-10 21:32:43 -040015860 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015861 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15862 }
15863 /* The IOCTL status is embedded in the mailbox subheader. */
15864 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15865 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15866 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15867 if (rc != MBX_TIMEOUT)
15868 mempool_free(mbox, phba->mbox_mem_pool);
15869 if (shdr_status || shdr_add_status || rc) {
15870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15871 "3025 Write Object mailbox failed with "
15872 "status x%x add_status x%x, mbx status x%x\n",
15873 shdr_status, shdr_add_status, rc);
15874 rc = -ENXIO;
15875 } else
15876 *offset += wr_object->u.response.actual_write_length;
15877 return rc;
15878}
15879
15880/**
James Smart695a8142010-01-26 23:08:03 -050015881 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15882 * @vport: pointer to vport data structure.
15883 *
15884 * This function iterate through the mailboxq and clean up all REG_LOGIN
15885 * and REG_VPI mailbox commands associated with the vport. This function
15886 * is called when driver want to restart discovery of the vport due to
15887 * a Clear Virtual Link event.
15888 **/
15889void
15890lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15891{
15892 struct lpfc_hba *phba = vport->phba;
15893 LPFC_MBOXQ_t *mb, *nextmb;
15894 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015895 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015896 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015897 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015898 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015899 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015900
James Smartd439d282010-09-29 11:18:45 -040015901 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015902 spin_lock_irq(&phba->hbalock);
15903 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15904 if (mb->vport != vport)
15905 continue;
15906
15907 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15908 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15909 continue;
15910
James Smartd439d282010-09-29 11:18:45 -040015911 list_del(&mb->list);
15912 list_add_tail(&mb->list, &mbox_cmd_list);
15913 }
15914 /* Clean up active mailbox command with the vport */
15915 mb = phba->sli.mbox_active;
15916 if (mb && (mb->vport == vport)) {
15917 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15918 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15919 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15920 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15921 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15922 /* Put reference count for delayed processing */
15923 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15924 /* Unregister the RPI when mailbox complete */
15925 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15926 }
15927 }
James Smart63e801c2010-11-20 23:14:19 -050015928 /* Cleanup any mailbox completions which are not yet processed */
15929 do {
15930 restart_loop = 0;
15931 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15932 /*
15933 * If this mailox is already processed or it is
15934 * for another vport ignore it.
15935 */
15936 if ((mb->vport != vport) ||
15937 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15938 continue;
15939
15940 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15941 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15942 continue;
15943
15944 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15945 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15946 ndlp = (struct lpfc_nodelist *)mb->context2;
15947 /* Unregister the RPI when mailbox complete */
15948 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15949 restart_loop = 1;
15950 spin_unlock_irq(&phba->hbalock);
15951 spin_lock(shost->host_lock);
15952 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15953 spin_unlock(shost->host_lock);
15954 spin_lock_irq(&phba->hbalock);
15955 break;
15956 }
15957 }
15958 } while (restart_loop);
15959
James Smartd439d282010-09-29 11:18:45 -040015960 spin_unlock_irq(&phba->hbalock);
15961
15962 /* Release the cleaned-up mailbox commands */
15963 while (!list_empty(&mbox_cmd_list)) {
15964 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015965 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15966 mp = (struct lpfc_dmabuf *) (mb->context1);
15967 if (mp) {
15968 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15969 kfree(mp);
15970 }
James Smart78730cf2010-04-06 15:06:30 -040015971 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015972 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015973 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015974 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015975 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015976 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015977 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015978 }
James Smart695a8142010-01-26 23:08:03 -050015979 }
James Smart695a8142010-01-26 23:08:03 -050015980 mempool_free(mb, phba->mbox_mem_pool);
15981 }
James Smartd439d282010-09-29 11:18:45 -040015982
15983 /* Release the ndlp with the cleaned-up active mailbox command */
15984 if (act_mbx_ndlp) {
15985 spin_lock(shost->host_lock);
15986 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15987 spin_unlock(shost->host_lock);
15988 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015989 }
James Smart695a8142010-01-26 23:08:03 -050015990}
15991
James Smart2a9bf3d2010-06-07 15:24:45 -040015992/**
15993 * lpfc_drain_txq - Drain the txq
15994 * @phba: Pointer to HBA context object.
15995 *
15996 * This function attempt to submit IOCBs on the txq
15997 * to the adapter. For SLI4 adapters, the txq contains
15998 * ELS IOCBs that have been deferred because the there
15999 * are no SGLs. This congestion can occur with large
16000 * vport counts during node discovery.
16001 **/
16002
16003uint32_t
16004lpfc_drain_txq(struct lpfc_hba *phba)
16005{
16006 LIST_HEAD(completions);
16007 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16008 struct lpfc_iocbq *piocbq = 0;
16009 unsigned long iflags = 0;
16010 char *fail_msg = NULL;
16011 struct lpfc_sglq *sglq;
16012 union lpfc_wqe wqe;
16013
16014 spin_lock_irqsave(&phba->hbalock, iflags);
16015 if (pring->txq_cnt > pring->txq_max)
16016 pring->txq_max = pring->txq_cnt;
16017
16018 spin_unlock_irqrestore(&phba->hbalock, iflags);
16019
16020 while (pring->txq_cnt) {
16021 spin_lock_irqsave(&phba->hbalock, iflags);
16022
James Smart19ca7602010-11-20 23:11:55 -050016023 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016024 if (!piocbq) {
16025 spin_unlock_irqrestore(&phba->hbalock, iflags);
16026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16027 "2823 txq empty and txq_cnt is %d\n ",
16028 pring->txq_cnt);
16029 break;
16030 }
James Smart19ca7602010-11-20 23:11:55 -050016031 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016032 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016033 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016034 spin_unlock_irqrestore(&phba->hbalock, iflags);
16035 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016036 }
16037
16038 /* The xri and iocb resources secured,
16039 * attempt to issue request
16040 */
James Smart6d368e52011-05-24 11:44:12 -040016041 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016042 piocbq->sli4_xritag = sglq->sli4_xritag;
16043 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16044 fail_msg = "to convert bpl to sgl";
16045 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16046 fail_msg = "to convert iocb to wqe";
16047 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16048 fail_msg = " - Wq is full";
16049 else
16050 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16051
16052 if (fail_msg) {
16053 /* Failed means we can't issue and need to cancel */
16054 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16055 "2822 IOCB failed %s iotag 0x%x "
16056 "xri 0x%x\n",
16057 fail_msg,
16058 piocbq->iotag, piocbq->sli4_xritag);
16059 list_add_tail(&piocbq->list, &completions);
16060 }
16061 spin_unlock_irqrestore(&phba->hbalock, iflags);
16062 }
16063
James Smart2a9bf3d2010-06-07 15:24:45 -040016064 /* Cancel all the IOCBs that cannot be issued */
16065 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16066 IOERR_SLI_ABORTED);
16067
16068 return pring->txq_cnt;
16069}