blob: 1a391e2df3b3d46a8ee5976ff66e089df2cf72e6 [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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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 *);
70
James Smart4f774512009-05-22 14:52:35 -040071static IOCB_t *
72lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73{
74 return &iocbq->iocb;
75}
76
77/**
78 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79 * @q: The Work Queue to operate on.
80 * @wqe: The work Queue Entry to put on the Work queue.
81 *
82 * This routine will copy the contents of @wqe to the next available entry on
83 * the @q. This function will then ring the Work Queue Doorbell to signal the
84 * HBA to start processing the Work Queue Entry. This function returns 0 if
85 * successful. If no entries are available on @q then this function will return
86 * -ENOMEM.
87 * The caller is expected to hold the hbalock when calling this routine.
88 **/
89static uint32_t
90lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91{
James Smart2e90f4b2011-12-13 13:22:37 -050092 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040093 struct lpfc_register doorbell;
94 uint32_t host_index;
95
James Smart2e90f4b2011-12-13 13:22:37 -050096 /* sanity check on queue memory */
97 if (unlikely(!q))
98 return -ENOMEM;
99 temp_wqe = q->qe[q->host_index].wqe;
100
James Smart4f774512009-05-22 14:52:35 -0400101 /* If the host has not yet processed the next entry then we are done */
102 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
103 return -ENOMEM;
104 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500105 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400106 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500107 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
108 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400109 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
110
111 /* Update the host index before invoking device */
112 host_index = q->host_index;
113 q->host_index = ((q->host_index + 1) % q->entry_count);
114
115 /* Ring Doorbell */
116 doorbell.word0 = 0;
117 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
118 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
119 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
120 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
121 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
122
123 return 0;
124}
125
126/**
127 * lpfc_sli4_wq_release - Updates internal hba index for WQ
128 * @q: The Work Queue to operate on.
129 * @index: The index to advance the hba index to.
130 *
131 * This routine will update the HBA index of a queue to reflect consumption of
132 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
133 * an entry the host calls this function to update the queue's internal
134 * pointers. This routine returns the number of entries that were consumed by
135 * the HBA.
136 **/
137static uint32_t
138lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
139{
140 uint32_t released = 0;
141
James Smart2e90f4b2011-12-13 13:22:37 -0500142 /* sanity check on queue memory */
143 if (unlikely(!q))
144 return 0;
145
James Smart4f774512009-05-22 14:52:35 -0400146 if (q->hba_index == index)
147 return 0;
148 do {
149 q->hba_index = ((q->hba_index + 1) % q->entry_count);
150 released++;
151 } while (q->hba_index != index);
152 return released;
153}
154
155/**
156 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
157 * @q: The Mailbox Queue to operate on.
158 * @wqe: The Mailbox Queue Entry to put on the Work queue.
159 *
160 * This routine will copy the contents of @mqe to the next available entry on
161 * the @q. This function will then ring the Work Queue Doorbell to signal the
162 * HBA to start processing the Work Queue Entry. This function returns 0 if
163 * successful. If no entries are available on @q then this function will return
164 * -ENOMEM.
165 * The caller is expected to hold the hbalock when calling this routine.
166 **/
167static uint32_t
168lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
169{
James Smart2e90f4b2011-12-13 13:22:37 -0500170 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400171 struct lpfc_register doorbell;
172 uint32_t host_index;
173
James Smart2e90f4b2011-12-13 13:22:37 -0500174 /* sanity check on queue memory */
175 if (unlikely(!q))
176 return -ENOMEM;
177 temp_mqe = q->qe[q->host_index].mqe;
178
James Smart4f774512009-05-22 14:52:35 -0400179 /* If the host has not yet processed the next entry then we are done */
180 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
181 return -ENOMEM;
182 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
183 /* Save off the mailbox pointer for completion */
184 q->phba->mbox = (MAILBOX_t *)temp_mqe;
185
186 /* Update the host index before invoking device */
187 host_index = q->host_index;
188 q->host_index = ((q->host_index + 1) % q->entry_count);
189
190 /* Ring Doorbell */
191 doorbell.word0 = 0;
192 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
193 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
194 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
195 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
196 return 0;
197}
198
199/**
200 * lpfc_sli4_mq_release - Updates internal hba index for MQ
201 * @q: The Mailbox Queue to operate on.
202 *
203 * This routine will update the HBA index of a queue to reflect consumption of
204 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
205 * an entry the host calls this function to update the queue's internal
206 * pointers. This routine returns the number of entries that were consumed by
207 * the HBA.
208 **/
209static uint32_t
210lpfc_sli4_mq_release(struct lpfc_queue *q)
211{
James Smart2e90f4b2011-12-13 13:22:37 -0500212 /* sanity check on queue memory */
213 if (unlikely(!q))
214 return 0;
215
James Smart4f774512009-05-22 14:52:35 -0400216 /* Clear the mailbox pointer for completion */
217 q->phba->mbox = NULL;
218 q->hba_index = ((q->hba_index + 1) % q->entry_count);
219 return 1;
220}
221
222/**
223 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
224 * @q: The Event Queue to get the first valid EQE from
225 *
226 * This routine will get the first valid Event Queue Entry from @q, update
227 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
228 * the Queue (no more work to do), or the Queue is full of EQEs that have been
229 * processed, but not popped back to the HBA then this routine will return NULL.
230 **/
231static struct lpfc_eqe *
232lpfc_sli4_eq_get(struct lpfc_queue *q)
233{
James Smart2e90f4b2011-12-13 13:22:37 -0500234 struct lpfc_eqe *eqe;
235
236 /* sanity check on queue memory */
237 if (unlikely(!q))
238 return NULL;
239 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400240
241 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400242 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400243 return NULL;
244 /* If the host has not yet processed the next entry then we are done */
245 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
246 return NULL;
247
248 q->hba_index = ((q->hba_index + 1) % q->entry_count);
249 return eqe;
250}
251
252/**
253 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
254 * @q: The Event Queue that the host has completed processing for.
255 * @arm: Indicates whether the host wants to arms this CQ.
256 *
257 * This routine will mark all Event Queue Entries on @q, from the last
258 * known completed entry to the last entry that was processed, as completed
259 * by clearing the valid bit for each completion queue entry. Then it will
260 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
261 * The internal host index in the @q will be updated by this routine to indicate
262 * that the host has finished processing the entries. The @arm parameter
263 * indicates that the queue should be rearmed when ringing the doorbell.
264 *
265 * This function will return the number of EQEs that were popped.
266 **/
267uint32_t
268lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
269{
270 uint32_t released = 0;
271 struct lpfc_eqe *temp_eqe;
272 struct lpfc_register doorbell;
273
James Smart2e90f4b2011-12-13 13:22:37 -0500274 /* sanity check on queue memory */
275 if (unlikely(!q))
276 return 0;
277
James Smart4f774512009-05-22 14:52:35 -0400278 /* while there are valid entries */
279 while (q->hba_index != q->host_index) {
280 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400281 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400282 released++;
283 q->host_index = ((q->host_index + 1) % q->entry_count);
284 }
285 if (unlikely(released == 0 && !arm))
286 return 0;
287
288 /* ring doorbell for number popped */
289 doorbell.word0 = 0;
290 if (arm) {
291 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
292 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
293 }
294 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
295 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500296 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
297 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
298 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400299 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500300 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
301 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
302 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400303 return released;
304}
305
306/**
307 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
308 * @q: The Completion Queue to get the first valid CQE from
309 *
310 * This routine will get the first valid Completion Queue Entry from @q, update
311 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
312 * the Queue (no more work to do), or the Queue is full of CQEs that have been
313 * processed, but not popped back to the HBA then this routine will return NULL.
314 **/
315static struct lpfc_cqe *
316lpfc_sli4_cq_get(struct lpfc_queue *q)
317{
318 struct lpfc_cqe *cqe;
319
James Smart2e90f4b2011-12-13 13:22:37 -0500320 /* sanity check on queue memory */
321 if (unlikely(!q))
322 return NULL;
323
James Smart4f774512009-05-22 14:52:35 -0400324 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400325 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400326 return NULL;
327 /* If the host has not yet processed the next entry then we are done */
328 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
329 return NULL;
330
331 cqe = q->qe[q->hba_index].cqe;
332 q->hba_index = ((q->hba_index + 1) % q->entry_count);
333 return cqe;
334}
335
336/**
337 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
338 * @q: The Completion Queue that the host has completed processing for.
339 * @arm: Indicates whether the host wants to arms this CQ.
340 *
341 * This routine will mark all Completion queue entries on @q, from the last
342 * known completed entry to the last entry that was processed, as completed
343 * by clearing the valid bit for each completion queue entry. Then it will
344 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
345 * The internal host index in the @q will be updated by this routine to indicate
346 * that the host has finished processing the entries. The @arm parameter
347 * indicates that the queue should be rearmed when ringing the doorbell.
348 *
349 * This function will return the number of CQEs that were released.
350 **/
351uint32_t
352lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
353{
354 uint32_t released = 0;
355 struct lpfc_cqe *temp_qe;
356 struct lpfc_register doorbell;
357
James Smart2e90f4b2011-12-13 13:22:37 -0500358 /* sanity check on queue memory */
359 if (unlikely(!q))
360 return 0;
James Smart4f774512009-05-22 14:52:35 -0400361 /* while there are valid entries */
362 while (q->hba_index != q->host_index) {
363 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400364 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400365 released++;
366 q->host_index = ((q->host_index + 1) % q->entry_count);
367 }
368 if (unlikely(released == 0 && !arm))
369 return 0;
370
371 /* ring doorbell for number popped */
372 doorbell.word0 = 0;
373 if (arm)
374 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
375 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
376 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500377 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
378 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
379 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400380 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
381 return released;
382}
383
384/**
385 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
386 * @q: The Header Receive Queue to operate on.
387 * @wqe: The Receive Queue Entry to put on the Receive queue.
388 *
389 * This routine will copy the contents of @wqe to the next available entry on
390 * the @q. This function will then ring the Receive Queue Doorbell to signal the
391 * HBA to start processing the Receive Queue Entry. This function returns the
392 * index that the rqe was copied to if successful. If no entries are available
393 * on @q then this function will return -ENOMEM.
394 * The caller is expected to hold the hbalock when calling this routine.
395 **/
396static int
397lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
398 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
399{
James Smart2e90f4b2011-12-13 13:22:37 -0500400 struct lpfc_rqe *temp_hrqe;
401 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400402 struct lpfc_register doorbell;
403 int put_index = hq->host_index;
404
James Smart2e90f4b2011-12-13 13:22:37 -0500405 /* sanity check on queue memory */
406 if (unlikely(!hq) || unlikely(!dq))
407 return -ENOMEM;
408 temp_hrqe = hq->qe[hq->host_index].rqe;
409 temp_drqe = dq->qe[dq->host_index].rqe;
410
James Smart4f774512009-05-22 14:52:35 -0400411 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
412 return -EINVAL;
413 if (hq->host_index != dq->host_index)
414 return -EINVAL;
415 /* If the host has not yet processed the next entry then we are done */
416 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
417 return -EBUSY;
418 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
419 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
420
421 /* Update the host index to point to the next slot */
422 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
423 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
424
425 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400426 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400427 doorbell.word0 = 0;
428 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400429 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400430 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
431 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
432 }
433 return put_index;
434}
435
436/**
437 * lpfc_sli4_rq_release - Updates internal hba index for RQ
438 * @q: The Header Receive Queue to operate on.
439 *
440 * This routine will update the HBA index of a queue to reflect consumption of
441 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
442 * consumed an entry the host calls this function to update the queue's
443 * internal pointers. This routine returns the number of entries that were
444 * consumed by the HBA.
445 **/
446static uint32_t
447lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
448{
James Smart2e90f4b2011-12-13 13:22:37 -0500449 /* sanity check on queue memory */
450 if (unlikely(!hq) || unlikely(!dq))
451 return 0;
452
James Smart4f774512009-05-22 14:52:35 -0400453 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
454 return 0;
455 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
456 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
457 return 1;
458}
459
James Smarte59058c2008-08-24 21:49:00 -0400460/**
James Smart3621a712009-04-06 18:47:14 -0400461 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400462 * @phba: Pointer to HBA context object.
463 * @pring: Pointer to driver SLI ring object.
464 *
465 * This function returns pointer to next command iocb entry
466 * in the command ring. The caller must hold hbalock to prevent
467 * other threads consume the next command iocb.
468 * SLI-2/SLI-3 provide different sized iocbs.
469 **/
James Smarted957682007-06-17 19:56:37 -0500470static inline IOCB_t *
471lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
472{
473 return (IOCB_t *) (((char *) pring->cmdringaddr) +
474 pring->cmdidx * phba->iocb_cmd_size);
475}
476
James Smarte59058c2008-08-24 21:49:00 -0400477/**
James Smart3621a712009-04-06 18:47:14 -0400478 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400479 * @phba: Pointer to HBA context object.
480 * @pring: Pointer to driver SLI ring object.
481 *
482 * This function returns pointer to next response iocb entry
483 * in the response ring. The caller must hold hbalock to make sure
484 * that no other thread consume the next response iocb.
485 * SLI-2/SLI-3 provide different sized iocbs.
486 **/
James Smarted957682007-06-17 19:56:37 -0500487static inline IOCB_t *
488lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
489{
490 return (IOCB_t *) (((char *) pring->rspringaddr) +
491 pring->rspidx * phba->iocb_rsp_size);
492}
493
James Smarte59058c2008-08-24 21:49:00 -0400494/**
James Smart3621a712009-04-06 18:47:14 -0400495 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400496 * @phba: Pointer to HBA context object.
497 *
498 * This function is called with hbalock held. This function
499 * allocates a new driver iocb object from the iocb pool. If the
500 * allocation is successful, it returns pointer to the newly
501 * allocated iocb object else it returns NULL.
502 **/
James Smart2e0fef82007-06-17 19:56:36 -0500503static struct lpfc_iocbq *
504__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400505{
506 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
507 struct lpfc_iocbq * iocbq = NULL;
508
509 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400510 if (iocbq)
511 phba->iocb_cnt++;
512 if (phba->iocb_cnt > phba->iocb_max)
513 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400514 return iocbq;
515}
516
James Smarte59058c2008-08-24 21:49:00 -0400517/**
James Smartda0436e2009-05-22 14:51:39 -0400518 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
519 * @phba: Pointer to HBA context object.
520 * @xritag: XRI value.
521 *
522 * This function clears the sglq pointer from the array of acive
523 * sglq's. The xritag that is passed in is used to index into the
524 * array. Before the xritag can be used it needs to be adjusted
525 * by subtracting the xribase.
526 *
527 * Returns sglq ponter = success, NULL = Failure.
528 **/
529static struct lpfc_sglq *
530__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
531{
James Smartda0436e2009-05-22 14:51:39 -0400532 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400533
534 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
535 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400536 return sglq;
537}
538
539/**
540 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
541 * @phba: Pointer to HBA context object.
542 * @xritag: XRI value.
543 *
544 * This function returns the sglq pointer from the array of acive
545 * sglq's. The xritag that is passed in is used to index into the
546 * array. Before the xritag can be used it needs to be adjusted
547 * by subtracting the xribase.
548 *
549 * Returns sglq ponter = success, NULL = Failure.
550 **/
James Smart0f65ff62010-02-26 14:14:23 -0500551struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400552__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
553{
James Smartda0436e2009-05-22 14:51:39 -0400554 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400555
556 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400557 return sglq;
558}
559
560/**
James Smart19ca7602010-11-20 23:11:55 -0500561 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
562 * @phba: Pointer to HBA context object.
563 * @ndlp: nodelist pointer for this target.
564 * @xritag: xri used in this exchange.
565 * @rxid: Remote Exchange ID.
566 * @send_rrq: Flag used to determine if we should send rrq els cmd.
567 *
568 * This function is called with hbalock held.
569 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
570 * rrq struct and adds it to the active_rrq_list.
571 *
572 * returns 0 for rrq slot for this xri
573 * < 0 Were not able to get rrq mem or invalid parameter.
574 **/
575static int
576__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
577 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
578{
James Smart19ca7602010-11-20 23:11:55 -0500579 struct lpfc_node_rrq *rrq;
580 int empty;
James Smart1151e3e2011-02-16 12:39:35 -0500581 uint32_t did = 0;
582
583
584 if (!ndlp)
585 return -EINVAL;
586
587 if (!phba->cfg_enable_rrq)
588 return -EINVAL;
589
590 if (phba->pport->load_flag & FC_UNLOADING) {
591 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
592 goto out;
593 }
594 did = ndlp->nlp_DID;
James Smart19ca7602010-11-20 23:11:55 -0500595
596 /*
597 * set the active bit even if there is no mem available.
598 */
James Smart1151e3e2011-02-16 12:39:35 -0500599 if (NLP_CHK_FREE_REQ(ndlp))
600 goto out;
601
602 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
603 goto out;
604
James Smart6d368e52011-05-24 11:44:12 -0400605 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart1151e3e2011-02-16 12:39:35 -0500606 goto out;
607
James Smart19ca7602010-11-20 23:11:55 -0500608 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
609 if (rrq) {
610 rrq->send_rrq = send_rrq;
James Smart7851fe22011-07-22 18:36:52 -0400611 rrq->xritag = xritag;
James Smart19ca7602010-11-20 23:11:55 -0500612 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
613 rrq->ndlp = ndlp;
614 rrq->nlp_DID = ndlp->nlp_DID;
615 rrq->vport = ndlp->vport;
616 rrq->rxid = rxid;
617 empty = list_empty(&phba->active_rrq_list);
James Smart1151e3e2011-02-16 12:39:35 -0500618 rrq->send_rrq = send_rrq;
James Smart19ca7602010-11-20 23:11:55 -0500619 list_add_tail(&rrq->list, &phba->active_rrq_list);
620 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
621 phba->hba_flag |= HBA_RRQ_ACTIVE;
622 if (empty)
623 lpfc_worker_wake_up(phba);
624 }
625 return 0;
626 }
James Smart1151e3e2011-02-16 12:39:35 -0500627out:
628 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
629 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
630 " DID:0x%x Send:%d\n",
631 xritag, rxid, did, send_rrq);
632 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500633}
634
635/**
James Smart1151e3e2011-02-16 12:39:35 -0500636 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500637 * @phba: Pointer to HBA context object.
638 * @xritag: xri used in this exchange.
639 * @rrq: The RRQ to be cleared.
640 *
James Smart19ca7602010-11-20 23:11:55 -0500641 **/
James Smart1151e3e2011-02-16 12:39:35 -0500642void
643lpfc_clr_rrq_active(struct lpfc_hba *phba,
644 uint16_t xritag,
645 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500646{
James Smart1151e3e2011-02-16 12:39:35 -0500647 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500648
James Smart1151e3e2011-02-16 12:39:35 -0500649 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
650 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500651
652 /* The target DID could have been swapped (cable swap)
653 * we should use the ndlp from the findnode if it is
654 * available.
655 */
James Smart1151e3e2011-02-16 12:39:35 -0500656 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500657 ndlp = rrq->ndlp;
658
James Smart1151e3e2011-02-16 12:39:35 -0500659 if (!ndlp)
660 goto out;
661
James Smart6d368e52011-05-24 11:44:12 -0400662 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500663 rrq->send_rrq = 0;
664 rrq->xritag = 0;
665 rrq->rrq_stop_time = 0;
666 }
James Smart1151e3e2011-02-16 12:39:35 -0500667out:
James Smart19ca7602010-11-20 23:11:55 -0500668 mempool_free(rrq, phba->rrq_pool);
669}
670
671/**
672 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
673 * @phba: Pointer to HBA context object.
674 *
675 * This function is called with hbalock held. This function
676 * Checks if stop_time (ratov from setting rrq active) has
677 * been reached, if it has and the send_rrq flag is set then
678 * it will call lpfc_send_rrq. If the send_rrq flag is not set
679 * then it will just call the routine to clear the rrq and
680 * free the rrq resource.
681 * The timer is set to the next rrq that is going to expire before
682 * leaving the routine.
683 *
684 **/
685void
686lpfc_handle_rrq_active(struct lpfc_hba *phba)
687{
688 struct lpfc_node_rrq *rrq;
689 struct lpfc_node_rrq *nextrrq;
690 unsigned long next_time;
691 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500692 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500693
694 spin_lock_irqsave(&phba->hbalock, iflags);
695 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
696 next_time = jiffies + HZ * (phba->fc_ratov + 1);
697 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500698 &phba->active_rrq_list, list) {
699 if (time_after(jiffies, rrq->rrq_stop_time))
700 list_move(&rrq->list, &send_rrq);
701 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500702 next_time = rrq->rrq_stop_time;
703 }
704 spin_unlock_irqrestore(&phba->hbalock, iflags);
705 if (!list_empty(&phba->active_rrq_list))
706 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500707 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
708 list_del(&rrq->list);
709 if (!rrq->send_rrq)
710 /* this call will free the rrq */
711 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
712 else if (lpfc_send_rrq(phba, rrq)) {
713 /* if we send the rrq then the completion handler
714 * will clear the bit in the xribitmap.
715 */
716 lpfc_clr_rrq_active(phba, rrq->xritag,
717 rrq);
718 }
719 }
James Smart19ca7602010-11-20 23:11:55 -0500720}
721
722/**
723 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
724 * @vport: Pointer to vport context object.
725 * @xri: The xri used in the exchange.
726 * @did: The targets DID for this exchange.
727 *
728 * returns NULL = rrq not found in the phba->active_rrq_list.
729 * rrq = rrq for this xri and target.
730 **/
731struct lpfc_node_rrq *
732lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
733{
734 struct lpfc_hba *phba = vport->phba;
735 struct lpfc_node_rrq *rrq;
736 struct lpfc_node_rrq *nextrrq;
737 unsigned long iflags;
738
739 if (phba->sli_rev != LPFC_SLI_REV4)
740 return NULL;
741 spin_lock_irqsave(&phba->hbalock, iflags);
742 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
743 if (rrq->vport == vport && rrq->xritag == xri &&
744 rrq->nlp_DID == did){
745 list_del(&rrq->list);
746 spin_unlock_irqrestore(&phba->hbalock, iflags);
747 return rrq;
748 }
749 }
750 spin_unlock_irqrestore(&phba->hbalock, iflags);
751 return NULL;
752}
753
754/**
755 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
756 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500757 * @ndlp: Pointer to the lpfc_node_list structure.
758 * If ndlp is NULL Remove all active RRQs for this vport from the
759 * phba->active_rrq_list and clear the rrq.
760 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500761 **/
762void
James Smart1151e3e2011-02-16 12:39:35 -0500763lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500764
765{
766 struct lpfc_hba *phba = vport->phba;
767 struct lpfc_node_rrq *rrq;
768 struct lpfc_node_rrq *nextrrq;
769 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500770 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500771
772 if (phba->sli_rev != LPFC_SLI_REV4)
773 return;
James Smart1151e3e2011-02-16 12:39:35 -0500774 if (!ndlp) {
775 lpfc_sli4_vport_delete_els_xri_aborted(vport);
776 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500777 }
James Smart1151e3e2011-02-16 12:39:35 -0500778 spin_lock_irqsave(&phba->hbalock, iflags);
779 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
780 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
781 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500782 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500783
784 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
785 list_del(&rrq->list);
786 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
787 }
James Smart19ca7602010-11-20 23:11:55 -0500788}
789
790/**
791 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
792 * @phba: Pointer to HBA context object.
793 *
794 * Remove all rrqs from the phba->active_rrq_list and free them by
795 * calling __lpfc_clr_active_rrq
796 *
797 **/
798void
799lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
800{
801 struct lpfc_node_rrq *rrq;
802 struct lpfc_node_rrq *nextrrq;
803 unsigned long next_time;
804 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500805 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500806
807 if (phba->sli_rev != LPFC_SLI_REV4)
808 return;
809 spin_lock_irqsave(&phba->hbalock, iflags);
810 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
811 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500812 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500813 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500814
815 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
816 list_del(&rrq->list);
817 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
818 }
James Smart19ca7602010-11-20 23:11:55 -0500819 if (!list_empty(&phba->active_rrq_list))
820 mod_timer(&phba->rrq_tmr, next_time);
821}
822
823
824/**
James Smart1151e3e2011-02-16 12:39:35 -0500825 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500826 * @phba: Pointer to HBA context object.
827 * @ndlp: Targets nodelist pointer for this exchange.
828 * @xritag the xri in the bitmap to test.
829 *
830 * This function is called with hbalock held. This function
831 * returns 0 = rrq not active for this xri
832 * 1 = rrq is valid for this xri.
833 **/
James Smart1151e3e2011-02-16 12:39:35 -0500834int
835lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500836 uint16_t xritag)
837{
James Smart19ca7602010-11-20 23:11:55 -0500838 if (!ndlp)
839 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400840 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500841 return 1;
842 else
843 return 0;
844}
845
846/**
847 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
848 * @phba: Pointer to HBA context object.
849 * @ndlp: nodelist pointer for this target.
850 * @xritag: xri used in this exchange.
851 * @rxid: Remote Exchange ID.
852 * @send_rrq: Flag used to determine if we should send rrq els cmd.
853 *
854 * This function takes the hbalock.
855 * The active bit is always set in the active rrq xri_bitmap even
856 * if there is no slot avaiable for the other rrq information.
857 *
858 * returns 0 rrq actived for this xri
859 * < 0 No memory or invalid ndlp.
860 **/
861int
862lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
863 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
864{
865 int ret;
866 unsigned long iflags;
867
868 spin_lock_irqsave(&phba->hbalock, iflags);
869 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
870 spin_unlock_irqrestore(&phba->hbalock, iflags);
871 return ret;
872}
873
874/**
James Smartda0436e2009-05-22 14:51:39 -0400875 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
876 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500877 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400878 *
879 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400880 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400881 * list is not empty then it is successful, it returns pointer to the newly
882 * allocated sglq object else it returns NULL.
883 **/
884static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500885__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400886{
887 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
888 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500889 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500890 struct lpfc_scsi_buf *lpfc_cmd;
891 struct lpfc_nodelist *ndlp;
892 int found = 0;
893
894 if (piocbq->iocb_flag & LPFC_IO_FCP) {
895 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
896 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500897 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
898 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500899 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500900 else
901 ndlp = piocbq->context1;
902
James Smartda0436e2009-05-22 14:51:39 -0400903 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500904 start_sglq = sglq;
905 while (!found) {
906 if (!sglq)
907 return NULL;
James Smart1151e3e2011-02-16 12:39:35 -0500908 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500909 /* This xri has an rrq outstanding for this DID.
910 * put it back in the list and get another xri.
911 */
912 list_add_tail(&sglq->list, lpfc_sgl_list);
913 sglq = NULL;
914 list_remove_head(lpfc_sgl_list, sglq,
915 struct lpfc_sglq, list);
916 if (sglq == start_sglq) {
917 sglq = NULL;
918 break;
919 } else
920 continue;
921 }
922 sglq->ndlp = ndlp;
923 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400924 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500925 sglq->state = SGL_ALLOCATED;
926 }
James Smartda0436e2009-05-22 14:51:39 -0400927 return sglq;
928}
929
930/**
James Smart3621a712009-04-06 18:47:14 -0400931 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400932 * @phba: Pointer to HBA context object.
933 *
934 * This function is called with no lock held. This function
935 * allocates a new driver iocb object from the iocb pool. If the
936 * allocation is successful, it returns pointer to the newly
937 * allocated iocb object else it returns NULL.
938 **/
James Smart2e0fef82007-06-17 19:56:36 -0500939struct lpfc_iocbq *
940lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500941{
James Smart2e0fef82007-06-17 19:56:36 -0500942 struct lpfc_iocbq * iocbq = NULL;
943 unsigned long iflags;
944
945 spin_lock_irqsave(&phba->hbalock, iflags);
946 iocbq = __lpfc_sli_get_iocbq(phba);
947 spin_unlock_irqrestore(&phba->hbalock, iflags);
948 return iocbq;
949}
950
James Smarte59058c2008-08-24 21:49:00 -0400951/**
James Smart4f774512009-05-22 14:52:35 -0400952 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
953 * @phba: Pointer to HBA context object.
954 * @iocbq: Pointer to driver iocb object.
955 *
956 * This function is called with hbalock held to release driver
957 * iocb object to the iocb pool. The iotag in the iocb object
958 * does not change for each use of the iocb object. This function
959 * clears all other fields of the iocb object when it is freed.
960 * The sqlq structure that holds the xritag and phys and virtual
961 * mappings for the scatter gather list is retrieved from the
962 * active array of sglq. The get of the sglq pointer also clears
963 * the entry in the array. If the status of the IO indiactes that
964 * this IO was aborted then the sglq entry it put on the
965 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
966 * IO has good status or fails for any other reason then the sglq
967 * entry is added to the free list (lpfc_sgl_list).
968 **/
969static void
970__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
971{
972 struct lpfc_sglq *sglq;
973 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400974 unsigned long iflag = 0;
975 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400976
977 if (iocbq->sli4_xritag == NO_XRI)
978 sglq = NULL;
979 else
James Smart6d368e52011-05-24 11:44:12 -0400980 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
981
James Smart4f774512009-05-22 14:52:35 -0400982 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500983 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
984 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400985 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
986 iflag);
987 list_add(&sglq->list,
988 &phba->sli4_hba.lpfc_abts_els_sgl_list);
989 spin_unlock_irqrestore(
990 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500991 } else {
992 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500993 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500994 list_add_tail(&sglq->list,
995 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400996
997 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400998 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400999 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001000 }
James Smart4f774512009-05-22 14:52:35 -04001001 }
1002
1003
1004 /*
1005 * Clean all volatile data fields, preserve iotag and node struct.
1006 */
1007 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001008 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001009 iocbq->sli4_xritag = NO_XRI;
1010 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1011}
1012
James Smart2a9bf3d2010-06-07 15:24:45 -04001013
James Smart4f774512009-05-22 14:52:35 -04001014/**
James Smart3772a992009-05-22 14:50:54 -04001015 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1016 * @phba: Pointer to HBA context object.
1017 * @iocbq: Pointer to driver iocb object.
1018 *
1019 * This function is called with hbalock held to release driver
1020 * iocb object to the iocb pool. The iotag in the iocb object
1021 * does not change for each use of the iocb object. This function
1022 * clears all other fields of the iocb object when it is freed.
1023 **/
1024static void
1025__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1026{
1027 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1028
1029 /*
1030 * Clean all volatile data fields, preserve iotag and node struct.
1031 */
1032 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1033 iocbq->sli4_xritag = NO_XRI;
1034 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1035}
1036
1037/**
James Smart3621a712009-04-06 18:47:14 -04001038 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001039 * @phba: Pointer to HBA context object.
1040 * @iocbq: Pointer to driver iocb object.
1041 *
1042 * This function is called with hbalock held to release driver
1043 * iocb object to the iocb pool. The iotag in the iocb object
1044 * does not change for each use of the iocb object. This function
1045 * clears all other fields of the iocb object when it is freed.
1046 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001047static void
James Smart2e0fef82007-06-17 19:56:36 -05001048__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1049{
James Smart3772a992009-05-22 14:50:54 -04001050 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001051 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001052}
1053
James Smarte59058c2008-08-24 21:49:00 -04001054/**
James Smart3621a712009-04-06 18:47:14 -04001055 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001056 * @phba: Pointer to HBA context object.
1057 * @iocbq: Pointer to driver iocb object.
1058 *
1059 * This function is called with no lock held to release the iocb to
1060 * iocb pool.
1061 **/
James Smart2e0fef82007-06-17 19:56:36 -05001062void
1063lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1064{
1065 unsigned long iflags;
1066
1067 /*
1068 * Clean all volatile data fields, preserve iotag and node struct.
1069 */
1070 spin_lock_irqsave(&phba->hbalock, iflags);
1071 __lpfc_sli_release_iocbq(phba, iocbq);
1072 spin_unlock_irqrestore(&phba->hbalock, iflags);
1073}
1074
James Smarte59058c2008-08-24 21:49:00 -04001075/**
James Smarta257bf92009-04-06 18:48:10 -04001076 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1077 * @phba: Pointer to HBA context object.
1078 * @iocblist: List of IOCBs.
1079 * @ulpstatus: ULP status in IOCB command field.
1080 * @ulpWord4: ULP word-4 in IOCB command field.
1081 *
1082 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1083 * on the list by invoking the complete callback function associated with the
1084 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1085 * fields.
1086 **/
1087void
1088lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1089 uint32_t ulpstatus, uint32_t ulpWord4)
1090{
1091 struct lpfc_iocbq *piocb;
1092
1093 while (!list_empty(iocblist)) {
1094 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1095
1096 if (!piocb->iocb_cmpl)
1097 lpfc_sli_release_iocbq(phba, piocb);
1098 else {
1099 piocb->iocb.ulpStatus = ulpstatus;
1100 piocb->iocb.un.ulpWord[4] = ulpWord4;
1101 (piocb->iocb_cmpl) (phba, piocb, piocb);
1102 }
1103 }
1104 return;
1105}
1106
1107/**
James Smart3621a712009-04-06 18:47:14 -04001108 * lpfc_sli_iocb_cmd_type - Get the iocb type
1109 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001110 *
1111 * This function is called by ring event handler function to get the iocb type.
1112 * This function translates the iocb command to an iocb command type used to
1113 * decide the final disposition of each completed IOCB.
1114 * The function returns
1115 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1116 * LPFC_SOL_IOCB if it is a solicited iocb completion
1117 * LPFC_ABORT_IOCB if it is an abort iocb
1118 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1119 *
1120 * The caller is not required to hold any lock.
1121 **/
dea31012005-04-17 16:05:31 -05001122static lpfc_iocb_type
1123lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1124{
1125 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1126
1127 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1128 return 0;
1129
1130 switch (iocb_cmnd) {
1131 case CMD_XMIT_SEQUENCE_CR:
1132 case CMD_XMIT_SEQUENCE_CX:
1133 case CMD_XMIT_BCAST_CN:
1134 case CMD_XMIT_BCAST_CX:
1135 case CMD_ELS_REQUEST_CR:
1136 case CMD_ELS_REQUEST_CX:
1137 case CMD_CREATE_XRI_CR:
1138 case CMD_CREATE_XRI_CX:
1139 case CMD_GET_RPI_CN:
1140 case CMD_XMIT_ELS_RSP_CX:
1141 case CMD_GET_RPI_CR:
1142 case CMD_FCP_IWRITE_CR:
1143 case CMD_FCP_IWRITE_CX:
1144 case CMD_FCP_IREAD_CR:
1145 case CMD_FCP_IREAD_CX:
1146 case CMD_FCP_ICMND_CR:
1147 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001148 case CMD_FCP_TSEND_CX:
1149 case CMD_FCP_TRSP_CX:
1150 case CMD_FCP_TRECEIVE_CX:
1151 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001152 case CMD_ADAPTER_MSG:
1153 case CMD_ADAPTER_DUMP:
1154 case CMD_XMIT_SEQUENCE64_CR:
1155 case CMD_XMIT_SEQUENCE64_CX:
1156 case CMD_XMIT_BCAST64_CN:
1157 case CMD_XMIT_BCAST64_CX:
1158 case CMD_ELS_REQUEST64_CR:
1159 case CMD_ELS_REQUEST64_CX:
1160 case CMD_FCP_IWRITE64_CR:
1161 case CMD_FCP_IWRITE64_CX:
1162 case CMD_FCP_IREAD64_CR:
1163 case CMD_FCP_IREAD64_CX:
1164 case CMD_FCP_ICMND64_CR:
1165 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001166 case CMD_FCP_TSEND64_CX:
1167 case CMD_FCP_TRSP64_CX:
1168 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001169 case CMD_GEN_REQUEST64_CR:
1170 case CMD_GEN_REQUEST64_CX:
1171 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001172 case DSSCMD_IWRITE64_CR:
1173 case DSSCMD_IWRITE64_CX:
1174 case DSSCMD_IREAD64_CR:
1175 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001176 type = LPFC_SOL_IOCB;
1177 break;
1178 case CMD_ABORT_XRI_CN:
1179 case CMD_ABORT_XRI_CX:
1180 case CMD_CLOSE_XRI_CN:
1181 case CMD_CLOSE_XRI_CX:
1182 case CMD_XRI_ABORTED_CX:
1183 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001184 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001185 type = LPFC_ABORT_IOCB;
1186 break;
1187 case CMD_RCV_SEQUENCE_CX:
1188 case CMD_RCV_ELS_REQ_CX:
1189 case CMD_RCV_SEQUENCE64_CX:
1190 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001191 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001192 case CMD_IOCB_RCV_SEQ64_CX:
1193 case CMD_IOCB_RCV_ELS64_CX:
1194 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001195 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001196 type = LPFC_UNSOL_IOCB;
1197 break;
James Smart3163f722008-02-08 18:50:25 -05001198 case CMD_IOCB_XMIT_MSEQ64_CR:
1199 case CMD_IOCB_XMIT_MSEQ64_CX:
1200 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1201 case CMD_IOCB_RCV_ELS_LIST64_CX:
1202 case CMD_IOCB_CLOSE_EXTENDED_CN:
1203 case CMD_IOCB_ABORT_EXTENDED_CN:
1204 case CMD_IOCB_RET_HBQE64_CN:
1205 case CMD_IOCB_FCP_IBIDIR64_CR:
1206 case CMD_IOCB_FCP_IBIDIR64_CX:
1207 case CMD_IOCB_FCP_ITASKMGT64_CX:
1208 case CMD_IOCB_LOGENTRY_CN:
1209 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1210 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001211 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001212 type = LPFC_UNKNOWN_IOCB;
1213 break;
dea31012005-04-17 16:05:31 -05001214 default:
1215 type = LPFC_UNKNOWN_IOCB;
1216 break;
1217 }
1218
1219 return type;
1220}
1221
James Smarte59058c2008-08-24 21:49:00 -04001222/**
James Smart3621a712009-04-06 18:47:14 -04001223 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001224 * @phba: Pointer to HBA context object.
1225 *
1226 * This function is called from SLI initialization code
1227 * to configure every ring of the HBA's SLI interface. The
1228 * caller is not required to hold any lock. This function issues
1229 * a config_ring mailbox command for each ring.
1230 * This function returns zero if successful else returns a negative
1231 * error code.
1232 **/
dea31012005-04-17 16:05:31 -05001233static int
James Smarted957682007-06-17 19:56:37 -05001234lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001235{
1236 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001237 LPFC_MBOXQ_t *pmb;
1238 MAILBOX_t *pmbox;
1239 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001240
James Smarted957682007-06-17 19:56:37 -05001241 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1242 if (!pmb)
1243 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001244 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001245 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001246 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001247 lpfc_config_ring(phba, i, pmb);
1248 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1249 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001251 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001252 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1253 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001254 rc, pmbox->mbxCommand,
1255 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001256 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001257 ret = -ENXIO;
1258 break;
dea31012005-04-17 16:05:31 -05001259 }
1260 }
James Smarted957682007-06-17 19:56:37 -05001261 mempool_free(pmb, phba->mbox_mem_pool);
1262 return ret;
dea31012005-04-17 16:05:31 -05001263}
1264
James Smarte59058c2008-08-24 21:49:00 -04001265/**
James Smart3621a712009-04-06 18:47:14 -04001266 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001267 * @phba: Pointer to HBA context object.
1268 * @pring: Pointer to driver SLI ring object.
1269 * @piocb: Pointer to the driver iocb object.
1270 *
1271 * This function is called with hbalock held. The function adds the
1272 * new iocb to txcmplq of the given ring. This function always returns
1273 * 0. If this function is called for ELS ring, this function checks if
1274 * there is a vport associated with the ELS command. This function also
1275 * starts els_tmofunc timer if this is an ELS command.
1276 **/
dea31012005-04-17 16:05:31 -05001277static int
James Smart2e0fef82007-06-17 19:56:36 -05001278lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1279 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001280{
dea31012005-04-17 16:05:31 -05001281 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001282 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001283 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001284 if (pring->txcmplq_cnt > pring->txcmplq_max)
1285 pring->txcmplq_max = pring->txcmplq_cnt;
1286
James Smart92d7f7b2007-06-17 19:56:38 -05001287 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1288 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1289 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1290 if (!piocb->vport)
1291 BUG();
1292 else
1293 mod_timer(&piocb->vport->els_tmofunc,
1294 jiffies + HZ * (phba->fc_ratov << 1));
1295 }
1296
dea31012005-04-17 16:05:31 -05001297
James Smart2e0fef82007-06-17 19:56:36 -05001298 return 0;
dea31012005-04-17 16:05:31 -05001299}
1300
James Smarte59058c2008-08-24 21:49:00 -04001301/**
James Smart3621a712009-04-06 18:47:14 -04001302 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001303 * @phba: Pointer to HBA context object.
1304 * @pring: Pointer to driver SLI ring object.
1305 *
1306 * This function is called with hbalock held to get next
1307 * iocb in txq of the given ring. If there is any iocb in
1308 * the txq, the function returns first iocb in the list after
1309 * removing the iocb from the list, else it returns NULL.
1310 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001311struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001312lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001313{
dea31012005-04-17 16:05:31 -05001314 struct lpfc_iocbq *cmd_iocb;
1315
James Smart858c9f62007-06-17 19:56:39 -05001316 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1317 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001318 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001319 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001320}
1321
James Smarte59058c2008-08-24 21:49:00 -04001322/**
James Smart3621a712009-04-06 18:47:14 -04001323 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001324 * @phba: Pointer to HBA context object.
1325 * @pring: Pointer to driver SLI ring object.
1326 *
1327 * This function is called with hbalock held and the caller must post the
1328 * iocb without releasing the lock. If the caller releases the lock,
1329 * iocb slot returned by the function is not guaranteed to be available.
1330 * The function returns pointer to the next available iocb slot if there
1331 * is available slot in the ring, else it returns NULL.
1332 * If the get index of the ring is ahead of the put index, the function
1333 * will post an error attention event to the worker thread to take the
1334 * HBA to offline state.
1335 **/
dea31012005-04-17 16:05:31 -05001336static IOCB_t *
1337lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1338{
James Smart34b02dc2008-08-24 21:49:55 -04001339 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001340 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001341 if ((pring->next_cmdidx == pring->cmdidx) &&
1342 (++pring->next_cmdidx >= max_cmd_idx))
1343 pring->next_cmdidx = 0;
1344
1345 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1346
1347 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1348
1349 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001351 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001352 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001353 pring->ringno,
dea31012005-04-17 16:05:31 -05001354 pring->local_getidx, max_cmd_idx);
1355
James Smart2e0fef82007-06-17 19:56:36 -05001356 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001357 /*
1358 * All error attention handlers are posted to
1359 * worker thread
1360 */
1361 phba->work_ha |= HA_ERATT;
1362 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001363
James Smart5e9d9b82008-06-14 22:52:53 -04001364 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001365
1366 return NULL;
1367 }
1368
1369 if (pring->local_getidx == pring->next_cmdidx)
1370 return NULL;
1371 }
1372
James Smarted957682007-06-17 19:56:37 -05001373 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001374}
1375
James Smarte59058c2008-08-24 21:49:00 -04001376/**
James Smart3621a712009-04-06 18:47:14 -04001377 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001378 * @phba: Pointer to HBA context object.
1379 * @iocbq: Pointer to driver iocb object.
1380 *
1381 * This function gets an iotag for the iocb. If there is no unused iotag and
1382 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1383 * array and assigns a new iotag.
1384 * The function returns the allocated iotag if successful, else returns zero.
1385 * Zero is not a valid iotag.
1386 * The caller is not required to hold any lock.
1387 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001388uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001389lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001390{
James Smart2e0fef82007-06-17 19:56:36 -05001391 struct lpfc_iocbq **new_arr;
1392 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001393 size_t new_len;
1394 struct lpfc_sli *psli = &phba->sli;
1395 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001396
James Smart2e0fef82007-06-17 19:56:36 -05001397 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001398 iotag = psli->last_iotag;
1399 if(++iotag < psli->iocbq_lookup_len) {
1400 psli->last_iotag = iotag;
1401 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001402 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001403 iocbq->iotag = iotag;
1404 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001405 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001406 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1407 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001408 spin_unlock_irq(&phba->hbalock);
1409 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001410 GFP_KERNEL);
1411 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001412 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001413 old_arr = psli->iocbq_lookup;
1414 if (new_len <= psli->iocbq_lookup_len) {
1415 /* highly unprobable case */
1416 kfree(new_arr);
1417 iotag = psli->last_iotag;
1418 if(++iotag < psli->iocbq_lookup_len) {
1419 psli->last_iotag = iotag;
1420 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001421 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001422 iocbq->iotag = iotag;
1423 return iotag;
1424 }
James Smart2e0fef82007-06-17 19:56:36 -05001425 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001426 return 0;
1427 }
1428 if (psli->iocbq_lookup)
1429 memcpy(new_arr, old_arr,
1430 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001431 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001432 psli->iocbq_lookup = new_arr;
1433 psli->iocbq_lookup_len = new_len;
1434 psli->last_iotag = iotag;
1435 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001436 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001437 iocbq->iotag = iotag;
1438 kfree(old_arr);
1439 return iotag;
1440 }
James Smart8f6d98d2006-08-01 07:34:00 -04001441 } else
James Smart2e0fef82007-06-17 19:56:36 -05001442 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001443
James Smartbc739052010-08-04 16:11:18 -04001444 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001445 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1446 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001447
James Bottomley604a3e32005-10-29 10:28:33 -05001448 return 0;
dea31012005-04-17 16:05:31 -05001449}
1450
James Smarte59058c2008-08-24 21:49:00 -04001451/**
James Smart3621a712009-04-06 18:47:14 -04001452 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001453 * @phba: Pointer to HBA context object.
1454 * @pring: Pointer to driver SLI ring object.
1455 * @iocb: Pointer to iocb slot in the ring.
1456 * @nextiocb: Pointer to driver iocb object which need to be
1457 * posted to firmware.
1458 *
1459 * This function is called with hbalock held to post a new iocb to
1460 * the firmware. This function copies the new iocb to ring iocb slot and
1461 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1462 * a completion call back for this iocb else the function will free the
1463 * iocb object.
1464 **/
dea31012005-04-17 16:05:31 -05001465static void
1466lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1467 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1468{
1469 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001470 * Set up an iotag
dea31012005-04-17 16:05:31 -05001471 */
James Bottomley604a3e32005-10-29 10:28:33 -05001472 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001473
James Smarte2a0a9d2008-12-04 22:40:02 -05001474
James Smarta58cbd52007-08-02 11:09:43 -04001475 if (pring->ringno == LPFC_ELS_RING) {
1476 lpfc_debugfs_slow_ring_trc(phba,
1477 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1478 *(((uint32_t *) &nextiocb->iocb) + 4),
1479 *(((uint32_t *) &nextiocb->iocb) + 6),
1480 *(((uint32_t *) &nextiocb->iocb) + 7));
1481 }
1482
dea31012005-04-17 16:05:31 -05001483 /*
1484 * Issue iocb command to adapter
1485 */
James Smart92d7f7b2007-06-17 19:56:38 -05001486 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001487 wmb();
1488 pring->stats.iocb_cmd++;
1489
1490 /*
1491 * If there is no completion routine to call, we can release the
1492 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1493 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1494 */
1495 if (nextiocb->iocb_cmpl)
1496 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001497 else
James Smart2e0fef82007-06-17 19:56:36 -05001498 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001499
1500 /*
1501 * Let the HBA know what IOCB slot will be the next one the
1502 * driver will put a command into.
1503 */
1504 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001505 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001506}
1507
James Smarte59058c2008-08-24 21:49:00 -04001508/**
James Smart3621a712009-04-06 18:47:14 -04001509 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001510 * @phba: Pointer to HBA context object.
1511 * @pring: Pointer to driver SLI ring object.
1512 *
1513 * The caller is not required to hold any lock for calling this function.
1514 * This function updates the chip attention bits for the ring to inform firmware
1515 * that there are pending work to be done for this ring and requests an
1516 * interrupt when there is space available in the ring. This function is
1517 * called when the driver is unable to post more iocbs to the ring due
1518 * to unavailability of space in the ring.
1519 **/
dea31012005-04-17 16:05:31 -05001520static void
James Smart2e0fef82007-06-17 19:56:36 -05001521lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001522{
1523 int ringno = pring->ringno;
1524
1525 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1526
1527 wmb();
1528
1529 /*
1530 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1531 * The HBA will tell us when an IOCB entry is available.
1532 */
1533 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1534 readl(phba->CAregaddr); /* flush */
1535
1536 pring->stats.iocb_cmd_full++;
1537}
1538
James Smarte59058c2008-08-24 21:49:00 -04001539/**
James Smart3621a712009-04-06 18:47:14 -04001540 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001541 * @phba: Pointer to HBA context object.
1542 * @pring: Pointer to driver SLI ring object.
1543 *
1544 * This function updates the chip attention register bit for the
1545 * given ring to inform HBA that there is more work to be done
1546 * in this ring. The caller is not required to hold any lock.
1547 **/
dea31012005-04-17 16:05:31 -05001548static void
James Smart2e0fef82007-06-17 19:56:36 -05001549lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001550{
1551 int ringno = pring->ringno;
1552
1553 /*
1554 * Tell the HBA that there is work to do in this ring.
1555 */
James Smart34b02dc2008-08-24 21:49:55 -04001556 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1557 wmb();
1558 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1559 readl(phba->CAregaddr); /* flush */
1560 }
dea31012005-04-17 16:05:31 -05001561}
1562
James Smarte59058c2008-08-24 21:49:00 -04001563/**
James Smart3621a712009-04-06 18:47:14 -04001564 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001565 * @phba: Pointer to HBA context object.
1566 * @pring: Pointer to driver SLI ring object.
1567 *
1568 * This function is called with hbalock held to post pending iocbs
1569 * in the txq to the firmware. This function is called when driver
1570 * detects space available in the ring.
1571 **/
dea31012005-04-17 16:05:31 -05001572static void
James Smart2e0fef82007-06-17 19:56:36 -05001573lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001574{
1575 IOCB_t *iocb;
1576 struct lpfc_iocbq *nextiocb;
1577
1578 /*
1579 * Check to see if:
1580 * (a) there is anything on the txq to send
1581 * (b) link is up
1582 * (c) link attention events can be processed (fcp ring only)
1583 * (d) IOCB processing is not blocked by the outstanding mbox command.
1584 */
1585 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001586 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001587 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001588 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001589
1590 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1591 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1592 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1593
1594 if (iocb)
1595 lpfc_sli_update_ring(phba, pring);
1596 else
1597 lpfc_sli_update_full_ring(phba, pring);
1598 }
1599
1600 return;
1601}
1602
James Smarte59058c2008-08-24 21:49:00 -04001603/**
James Smart3621a712009-04-06 18:47:14 -04001604 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001605 * @phba: Pointer to HBA context object.
1606 * @hbqno: HBQ number.
1607 *
1608 * This function is called with hbalock held to get the next
1609 * available slot for the given HBQ. If there is free slot
1610 * available for the HBQ it will return pointer to the next available
1611 * HBQ entry else it will return NULL.
1612 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001613static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001614lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1615{
1616 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1617
1618 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1619 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1620 hbqp->next_hbqPutIdx = 0;
1621
1622 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001623 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001624 uint32_t getidx = le32_to_cpu(raw_index);
1625
1626 hbqp->local_hbqGetIdx = getidx;
1627
1628 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1629 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001630 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001631 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001632 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001633 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001634 hbqp->entry_count);
1635
1636 phba->link_state = LPFC_HBA_ERROR;
1637 return NULL;
1638 }
1639
1640 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1641 return NULL;
1642 }
1643
James Smart51ef4c22007-08-02 11:10:31 -04001644 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1645 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001646}
1647
James Smarte59058c2008-08-24 21:49:00 -04001648/**
James Smart3621a712009-04-06 18:47:14 -04001649 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001650 * @phba: Pointer to HBA context object.
1651 *
1652 * This function is called with no lock held to free all the
1653 * hbq buffers while uninitializing the SLI interface. It also
1654 * frees the HBQ buffers returned by the firmware but not yet
1655 * processed by the upper layers.
1656 **/
James Smarted957682007-06-17 19:56:37 -05001657void
1658lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1659{
James Smart92d7f7b2007-06-17 19:56:38 -05001660 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1661 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001662 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001663 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001664 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001665
James Smart51ef4c22007-08-02 11:10:31 -04001666 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001667 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001668 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001669 for (i = 0; i < hbq_count; ++i) {
1670 list_for_each_entry_safe(dmabuf, next_dmabuf,
1671 &phba->hbqs[i].hbq_buffer_list, list) {
1672 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1673 list_del(&hbq_buf->dbuf.list);
1674 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1675 }
James Smarta8adb832007-10-27 13:37:53 -04001676 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001677 }
James Smart3163f722008-02-08 18:50:25 -05001678 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001679 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1680 list) {
James Smart3163f722008-02-08 18:50:25 -05001681 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1682 list_del(&hbq_buf->dbuf.list);
1683 if (hbq_buf->tag == -1) {
1684 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1685 (phba, hbq_buf);
1686 } else {
1687 hbqno = hbq_buf->tag >> 16;
1688 if (hbqno >= LPFC_MAX_HBQS)
1689 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1690 (phba, hbq_buf);
1691 else
1692 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1693 hbq_buf);
1694 }
1695 }
1696
1697 /* Mark the HBQs not in use */
1698 phba->hbq_in_use = 0;
1699 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001700}
1701
James Smarte59058c2008-08-24 21:49:00 -04001702/**
James Smart3621a712009-04-06 18:47:14 -04001703 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001704 * @phba: Pointer to HBA context object.
1705 * @hbqno: HBQ number.
1706 * @hbq_buf: Pointer to HBQ buffer.
1707 *
1708 * This function is called with the hbalock held to post a
1709 * hbq buffer to the firmware. If the function finds an empty
1710 * slot in the HBQ, it will post the buffer. The function will return
1711 * pointer to the hbq entry if it successfully post the buffer
1712 * else it will return NULL.
1713 **/
James Smart3772a992009-05-22 14:50:54 -04001714static int
James Smarted957682007-06-17 19:56:37 -05001715lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001716 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001717{
James Smart3772a992009-05-22 14:50:54 -04001718 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1719}
1720
1721/**
1722 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1723 * @phba: Pointer to HBA context object.
1724 * @hbqno: HBQ number.
1725 * @hbq_buf: Pointer to HBQ buffer.
1726 *
1727 * This function is called with the hbalock held to post a hbq buffer to the
1728 * firmware. If the function finds an empty slot in the HBQ, it will post the
1729 * buffer and place it on the hbq_buffer_list. The function will return zero if
1730 * it successfully post the buffer else it will return an error.
1731 **/
1732static int
1733lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1734 struct hbq_dmabuf *hbq_buf)
1735{
James Smarted957682007-06-17 19:56:37 -05001736 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001737 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001738
1739 /* Get next HBQ entry slot to use */
1740 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1741 if (hbqe) {
1742 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1743
James Smart92d7f7b2007-06-17 19:56:38 -05001744 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1745 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001746 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001747 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001748 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1749 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1750 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001751 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1752 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001753 /* flush */
James Smarted957682007-06-17 19:56:37 -05001754 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001755 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001756 return 0;
1757 } else
1758 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001759}
1760
James Smart4f774512009-05-22 14:52:35 -04001761/**
1762 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1763 * @phba: Pointer to HBA context object.
1764 * @hbqno: HBQ number.
1765 * @hbq_buf: Pointer to HBQ buffer.
1766 *
1767 * This function is called with the hbalock held to post an RQE to the SLI4
1768 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1769 * the hbq_buffer_list and return zero, otherwise it will return an error.
1770 **/
1771static int
1772lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1773 struct hbq_dmabuf *hbq_buf)
1774{
1775 int rc;
1776 struct lpfc_rqe hrqe;
1777 struct lpfc_rqe drqe;
1778
1779 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1780 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1781 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1782 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1783 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1784 &hrqe, &drqe);
1785 if (rc < 0)
1786 return rc;
1787 hbq_buf->tag = rc;
1788 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1789 return 0;
1790}
1791
James Smarte59058c2008-08-24 21:49:00 -04001792/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001793static struct lpfc_hbq_init lpfc_els_hbq = {
1794 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001795 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001796 .mask_count = 0,
1797 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001798 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001799 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001800 .init_count = 40,
1801 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001802};
James Smarted957682007-06-17 19:56:37 -05001803
James Smarte59058c2008-08-24 21:49:00 -04001804/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001805static struct lpfc_hbq_init lpfc_extra_hbq = {
1806 .rn = 1,
1807 .entry_count = 200,
1808 .mask_count = 0,
1809 .profile = 0,
1810 .ring_mask = (1 << LPFC_EXTRA_RING),
1811 .buffer_count = 0,
1812 .init_count = 0,
1813 .add_count = 5,
1814};
1815
James Smarte59058c2008-08-24 21:49:00 -04001816/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001817struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001818 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001819 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001820};
1821
James Smarte59058c2008-08-24 21:49:00 -04001822/**
James Smart3621a712009-04-06 18:47:14 -04001823 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001824 * @phba: Pointer to HBA context object.
1825 * @hbqno: HBQ number.
1826 * @count: Number of HBQ buffers to be posted.
1827 *
James Smartd7c255b2008-08-24 21:50:00 -04001828 * This function is called with no lock held to post more hbq buffers to the
1829 * given HBQ. The function returns the number of HBQ buffers successfully
1830 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001831 **/
James Smart311464e2007-08-02 11:10:37 -04001832static int
James Smart92d7f7b2007-06-17 19:56:38 -05001833lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1834{
James Smartd7c255b2008-08-24 21:50:00 -04001835 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001836 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001837 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001838 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001839 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001840 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001841
James Smartd7c255b2008-08-24 21:50:00 -04001842 if ((phba->hbqs[hbqno].buffer_count + count) >
1843 lpfc_hbq_defs[hbqno]->entry_count)
1844 count = lpfc_hbq_defs[hbqno]->entry_count -
1845 phba->hbqs[hbqno].buffer_count;
1846 if (!count)
1847 return 0;
1848 /* Allocate HBQ entries */
1849 for (i = 0; i < count; i++) {
1850 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1851 if (!hbq_buffer)
1852 break;
1853 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1854 }
James Smart3163f722008-02-08 18:50:25 -05001855 /* Check whether HBQ is still in use */
1856 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001857 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001858 goto err;
1859 while (!list_empty(&hbq_buf_list)) {
1860 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1861 dbuf.list);
1862 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1863 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001864 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001865 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001866 posted++;
1867 } else
James Smart51ef4c22007-08-02 11:10:31 -04001868 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001869 }
James Smart3163f722008-02-08 18:50:25 -05001870 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001871 return posted;
1872err:
1873 spin_unlock_irqrestore(&phba->hbalock, flags);
1874 while (!list_empty(&hbq_buf_list)) {
1875 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1876 dbuf.list);
1877 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1878 }
James Smart92d7f7b2007-06-17 19:56:38 -05001879 return 0;
James Smarted957682007-06-17 19:56:37 -05001880}
1881
James Smarte59058c2008-08-24 21:49:00 -04001882/**
James Smart3621a712009-04-06 18:47:14 -04001883 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001884 * @phba: Pointer to HBA context object.
1885 * @qno: HBQ number.
1886 *
1887 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001888 * is called with no lock held. The function returns the number of HBQ entries
1889 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001890 **/
James Smarted957682007-06-17 19:56:37 -05001891int
James Smart92d7f7b2007-06-17 19:56:38 -05001892lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001893{
James Smartdef9c7a2009-12-21 17:02:28 -05001894 if (phba->sli_rev == LPFC_SLI_REV4)
1895 return 0;
1896 else
1897 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1898 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001899}
1900
James Smarte59058c2008-08-24 21:49:00 -04001901/**
James Smart3621a712009-04-06 18:47:14 -04001902 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001903 * @phba: Pointer to HBA context object.
1904 * @qno: HBQ queue number.
1905 *
1906 * This function is called from SLI initialization code path with
1907 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001908 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001909 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001910static int
James Smart92d7f7b2007-06-17 19:56:38 -05001911lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001912{
James Smartdef9c7a2009-12-21 17:02:28 -05001913 if (phba->sli_rev == LPFC_SLI_REV4)
1914 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001915 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001916 else
1917 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1918 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001919}
1920
James Smarte59058c2008-08-24 21:49:00 -04001921/**
James Smart3772a992009-05-22 14:50:54 -04001922 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1923 * @phba: Pointer to HBA context object.
1924 * @hbqno: HBQ number.
1925 *
1926 * This function removes the first hbq buffer on an hbq list and returns a
1927 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1928 **/
1929static struct hbq_dmabuf *
1930lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1931{
1932 struct lpfc_dmabuf *d_buf;
1933
1934 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1935 if (!d_buf)
1936 return NULL;
1937 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1938}
1939
1940/**
James Smart3621a712009-04-06 18:47:14 -04001941 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001942 * @phba: Pointer to HBA context object.
1943 * @tag: Tag of the hbq buffer.
1944 *
1945 * This function is called with hbalock held. This function searches
1946 * for the hbq buffer associated with the given tag in the hbq buffer
1947 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1948 * it returns NULL.
1949 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001950static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001951lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1952{
James Smart92d7f7b2007-06-17 19:56:38 -05001953 struct lpfc_dmabuf *d_buf;
1954 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001955 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001956
James Smart51ef4c22007-08-02 11:10:31 -04001957 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001958 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001959 return NULL;
1960
James Smart3772a992009-05-22 14:50:54 -04001961 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001962 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001963 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001964 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001965 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001966 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001967 }
1968 }
James Smart3772a992009-05-22 14:50:54 -04001969 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001971 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001972 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001973 return NULL;
James Smarted957682007-06-17 19:56:37 -05001974}
1975
James Smarte59058c2008-08-24 21:49:00 -04001976/**
James Smart3621a712009-04-06 18:47:14 -04001977 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001978 * @phba: Pointer to HBA context object.
1979 * @hbq_buffer: Pointer to HBQ buffer.
1980 *
1981 * This function is called with hbalock. This function gives back
1982 * the hbq buffer to firmware. If the HBQ does not have space to
1983 * post the buffer, it will free the buffer.
1984 **/
James Smarted957682007-06-17 19:56:37 -05001985void
James Smart51ef4c22007-08-02 11:10:31 -04001986lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001987{
1988 uint32_t hbqno;
1989
James Smart51ef4c22007-08-02 11:10:31 -04001990 if (hbq_buffer) {
1991 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001992 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001993 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001994 }
1995}
1996
James Smarte59058c2008-08-24 21:49:00 -04001997/**
James Smart3621a712009-04-06 18:47:14 -04001998 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001999 * @mbxCommand: mailbox command code.
2000 *
2001 * This function is called by the mailbox event handler function to verify
2002 * that the completed mailbox command is a legitimate mailbox command. If the
2003 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2004 * and the mailbox event handler will take the HBA offline.
2005 **/
dea31012005-04-17 16:05:31 -05002006static int
2007lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2008{
2009 uint8_t ret;
2010
2011 switch (mbxCommand) {
2012 case MBX_LOAD_SM:
2013 case MBX_READ_NV:
2014 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002015 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002016 case MBX_RUN_BIU_DIAG:
2017 case MBX_INIT_LINK:
2018 case MBX_DOWN_LINK:
2019 case MBX_CONFIG_LINK:
2020 case MBX_CONFIG_RING:
2021 case MBX_RESET_RING:
2022 case MBX_READ_CONFIG:
2023 case MBX_READ_RCONFIG:
2024 case MBX_READ_SPARM:
2025 case MBX_READ_STATUS:
2026 case MBX_READ_RPI:
2027 case MBX_READ_XRI:
2028 case MBX_READ_REV:
2029 case MBX_READ_LNK_STAT:
2030 case MBX_REG_LOGIN:
2031 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002032 case MBX_CLEAR_LA:
2033 case MBX_DUMP_MEMORY:
2034 case MBX_DUMP_CONTEXT:
2035 case MBX_RUN_DIAGS:
2036 case MBX_RESTART:
2037 case MBX_UPDATE_CFG:
2038 case MBX_DOWN_LOAD:
2039 case MBX_DEL_LD_ENTRY:
2040 case MBX_RUN_PROGRAM:
2041 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002042 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002043 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002044 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002045 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002046 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002047 case MBX_LOAD_AREA:
2048 case MBX_RUN_BIU_DIAG64:
2049 case MBX_CONFIG_PORT:
2050 case MBX_READ_SPARM64:
2051 case MBX_READ_RPI64:
2052 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002053 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002054 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002055 case MBX_SET_DEBUG:
2056 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002057 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002058 case MBX_REG_VPI:
2059 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002060 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002061 case MBX_PORT_CAPABILITIES:
2062 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002063 case MBX_SLI4_CONFIG:
2064 case MBX_SLI4_REQ_FTRS:
2065 case MBX_REG_FCFI:
2066 case MBX_UNREG_FCFI:
2067 case MBX_REG_VFI:
2068 case MBX_UNREG_VFI:
2069 case MBX_INIT_VPI:
2070 case MBX_INIT_VFI:
2071 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002072 case MBX_READ_EVENT_LOG_STATUS:
2073 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002074 case MBX_SECURITY_MGMT:
2075 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002076 ret = mbxCommand;
2077 break;
2078 default:
2079 ret = MBX_SHUTDOWN;
2080 break;
2081 }
James Smart2e0fef82007-06-17 19:56:36 -05002082 return ret;
dea31012005-04-17 16:05:31 -05002083}
James Smarte59058c2008-08-24 21:49:00 -04002084
2085/**
James Smart3621a712009-04-06 18:47:14 -04002086 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002087 * @phba: Pointer to HBA context object.
2088 * @pmboxq: Pointer to mailbox command.
2089 *
2090 * This is completion handler function for mailbox commands issued from
2091 * lpfc_sli_issue_mbox_wait function. This function is called by the
2092 * mailbox event handler function with no lock held. This function
2093 * will wake up thread waiting on the wait queue pointed by context1
2094 * of the mailbox.
2095 **/
James Smart04c68492009-05-22 14:52:52 -04002096void
James Smart2e0fef82007-06-17 19:56:36 -05002097lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002098{
2099 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002100 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002101
2102 /*
2103 * If pdone_q is empty, the driver thread gave up waiting and
2104 * continued running.
2105 */
James Smart7054a602007-04-25 09:52:34 -04002106 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002107 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002108 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2109 if (pdone_q)
2110 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002111 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002112 return;
2113}
2114
James Smarte59058c2008-08-24 21:49:00 -04002115
2116/**
James Smart3621a712009-04-06 18:47:14 -04002117 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002118 * @phba: Pointer to HBA context object.
2119 * @pmb: Pointer to mailbox object.
2120 *
2121 * This function is the default mailbox completion handler. It
2122 * frees the memory resources associated with the completed mailbox
2123 * command. If the completed command is a REG_LOGIN mailbox command,
2124 * this function will issue a UREG_LOGIN to re-claim the RPI.
2125 **/
dea31012005-04-17 16:05:31 -05002126void
James Smart2e0fef82007-06-17 19:56:36 -05002127lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002128{
James Smartd439d282010-09-29 11:18:45 -04002129 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002130 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002131 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002132 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002133 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002134 int rc;
2135
dea31012005-04-17 16:05:31 -05002136 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002137
dea31012005-04-17 16:05:31 -05002138 if (mp) {
2139 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2140 kfree(mp);
2141 }
James Smart7054a602007-04-25 09:52:34 -04002142
2143 /*
2144 * If a REG_LOGIN succeeded after node is destroyed or node
2145 * is in re-discovery driver need to cleanup the RPI.
2146 */
James Smart2e0fef82007-06-17 19:56:36 -05002147 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002148 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2149 !pmb->u.mb.mbxStatus) {
2150 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002151 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002152 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002153 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002154 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2155 if (rc != MBX_NOT_FINISHED)
2156 return;
2157 }
2158
James Smart695a8142010-01-26 23:08:03 -05002159 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2160 !(phba->pport->load_flag & FC_UNLOADING) &&
2161 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002162 shost = lpfc_shost_from_vport(vport);
2163 spin_lock_irq(shost->host_lock);
2164 vport->vpi_state |= LPFC_VPI_REGISTERED;
2165 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2166 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002167 }
2168
James Smartd439d282010-09-29 11:18:45 -04002169 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2170 ndlp = (struct lpfc_nodelist *)pmb->context2;
2171 lpfc_nlp_put(ndlp);
2172 pmb->context2 = NULL;
2173 }
2174
James Smartdcf2a4e2010-09-29 11:18:53 -04002175 /* Check security permission status on INIT_LINK mailbox command */
2176 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2177 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2178 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2179 "2860 SLI authentication is required "
2180 "for INIT_LINK but has not done yet\n");
2181
James Smart04c68492009-05-22 14:52:52 -04002182 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2183 lpfc_sli4_mbox_cmd_free(phba, pmb);
2184 else
2185 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002186}
2187
James Smarte59058c2008-08-24 21:49:00 -04002188/**
James Smart3621a712009-04-06 18:47:14 -04002189 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002190 * @phba: Pointer to HBA context object.
2191 *
2192 * This function is called with no lock held. This function processes all
2193 * the completed mailbox commands and gives it to upper layers. The interrupt
2194 * service routine processes mailbox completion interrupt and adds completed
2195 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2196 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2197 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2198 * function returns the mailbox commands to the upper layer by calling the
2199 * completion handler function of each mailbox.
2200 **/
dea31012005-04-17 16:05:31 -05002201int
James Smart2e0fef82007-06-17 19:56:36 -05002202lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002203{
James Smart92d7f7b2007-06-17 19:56:38 -05002204 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002205 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002206 int rc;
2207 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002208
2209 phba->sli.slistat.mbox_event++;
2210
James Smart92d7f7b2007-06-17 19:56:38 -05002211 /* Get all completed mailboxe buffers into the cmplq */
2212 spin_lock_irq(&phba->hbalock);
2213 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2214 spin_unlock_irq(&phba->hbalock);
2215
dea31012005-04-17 16:05:31 -05002216 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002217 do {
2218 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2219 if (pmb == NULL)
2220 break;
2221
James Smart04c68492009-05-22 14:52:52 -04002222 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002223
James Smart858c9f62007-06-17 19:56:39 -05002224 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2225 if (pmb->vport) {
2226 lpfc_debugfs_disc_trc(pmb->vport,
2227 LPFC_DISC_TRC_MBOX_VPORT,
2228 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2229 (uint32_t)pmbox->mbxCommand,
2230 pmbox->un.varWords[0],
2231 pmbox->un.varWords[1]);
2232 }
2233 else {
2234 lpfc_debugfs_disc_trc(phba->pport,
2235 LPFC_DISC_TRC_MBOX,
2236 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2237 (uint32_t)pmbox->mbxCommand,
2238 pmbox->un.varWords[0],
2239 pmbox->un.varWords[1]);
2240 }
2241 }
2242
dea31012005-04-17 16:05:31 -05002243 /*
2244 * It is a fatal error if unknown mbox command completion.
2245 */
2246 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2247 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002248 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002249 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002250 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002251 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002252 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002253 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002254 lpfc_sli_config_mbox_subsys_get(phba,
2255 pmb),
2256 lpfc_sli_config_mbox_opcode_get(phba,
2257 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002258 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002259 phba->work_hs = HS_FFER3;
2260 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002261 continue;
dea31012005-04-17 16:05:31 -05002262 }
2263
dea31012005-04-17 16:05:31 -05002264 if (pmbox->mbxStatus) {
2265 phba->sli.slistat.mbox_stat_err++;
2266 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2267 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002268 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002269 LOG_MBOX | LOG_SLI,
2270 "(%d):0305 Mbox cmd cmpl "
2271 "error - RETRYing Data: x%x "
2272 "(x%x/x%x) x%x x%x x%x\n",
2273 pmb->vport ? pmb->vport->vpi : 0,
2274 pmbox->mbxCommand,
2275 lpfc_sli_config_mbox_subsys_get(phba,
2276 pmb),
2277 lpfc_sli_config_mbox_opcode_get(phba,
2278 pmb),
2279 pmbox->mbxStatus,
2280 pmbox->un.varWords[0],
2281 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002282 pmbox->mbxStatus = 0;
2283 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002284 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002285 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002286 continue;
dea31012005-04-17 16:05:31 -05002287 }
2288 }
2289
2290 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002291 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002292 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002293 "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 -05002294 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002295 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002296 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2297 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002298 pmb->mbox_cmpl,
2299 *((uint32_t *) pmbox),
2300 pmbox->un.varWords[0],
2301 pmbox->un.varWords[1],
2302 pmbox->un.varWords[2],
2303 pmbox->un.varWords[3],
2304 pmbox->un.varWords[4],
2305 pmbox->un.varWords[5],
2306 pmbox->un.varWords[6],
2307 pmbox->un.varWords[7]);
2308
James Smart92d7f7b2007-06-17 19:56:38 -05002309 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002310 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002311 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002312 return 0;
dea31012005-04-17 16:05:31 -05002313}
James Smart92d7f7b2007-06-17 19:56:38 -05002314
James Smarte59058c2008-08-24 21:49:00 -04002315/**
James Smart3621a712009-04-06 18:47:14 -04002316 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002317 * @phba: Pointer to HBA context object.
2318 * @pring: Pointer to driver SLI ring object.
2319 * @tag: buffer tag.
2320 *
2321 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2322 * is set in the tag the buffer is posted for a particular exchange,
2323 * the function will return the buffer without replacing the buffer.
2324 * If the buffer is for unsolicited ELS or CT traffic, this function
2325 * returns the buffer and also posts another buffer to the firmware.
2326 **/
James Smart76bb24e2007-10-27 13:38:00 -04002327static struct lpfc_dmabuf *
2328lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002329 struct lpfc_sli_ring *pring,
2330 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002331{
James Smart9f1e1b52008-12-04 22:39:40 -05002332 struct hbq_dmabuf *hbq_entry;
2333
James Smart76bb24e2007-10-27 13:38:00 -04002334 if (tag & QUE_BUFTAG_BIT)
2335 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002336 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2337 if (!hbq_entry)
2338 return NULL;
2339 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002340}
James Smart57127f12007-10-27 13:37:05 -04002341
James Smart3772a992009-05-22 14:50:54 -04002342/**
2343 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2344 * @phba: Pointer to HBA context object.
2345 * @pring: Pointer to driver SLI ring object.
2346 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2347 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2348 * @fch_type: the type for the first frame of the sequence.
2349 *
2350 * This function is called with no lock held. This function uses the r_ctl and
2351 * type of the received sequence to find the correct callback function to call
2352 * to process the sequence.
2353 **/
2354static int
2355lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2356 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2357 uint32_t fch_type)
2358{
2359 int i;
2360
2361 /* unSolicited Responses */
2362 if (pring->prt[0].profile) {
2363 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2364 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2365 saveq);
2366 return 1;
2367 }
2368 /* We must search, based on rctl / type
2369 for the right routine */
2370 for (i = 0; i < pring->num_mask; i++) {
2371 if ((pring->prt[i].rctl == fch_r_ctl) &&
2372 (pring->prt[i].type == fch_type)) {
2373 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2374 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2375 (phba, pring, saveq);
2376 return 1;
2377 }
2378 }
2379 return 0;
2380}
James Smarte59058c2008-08-24 21:49:00 -04002381
2382/**
James Smart3621a712009-04-06 18:47:14 -04002383 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002384 * @phba: Pointer to HBA context object.
2385 * @pring: Pointer to driver SLI ring object.
2386 * @saveq: Pointer to the unsolicited iocb.
2387 *
2388 * This function is called with no lock held by the ring event handler
2389 * when there is an unsolicited iocb posted to the response ring by the
2390 * firmware. This function gets the buffer associated with the iocbs
2391 * and calls the event handler for the ring. This function handles both
2392 * qring buffers and hbq buffers.
2393 * When the function returns 1 the caller can free the iocb object otherwise
2394 * upper layer functions will free the iocb objects.
2395 **/
dea31012005-04-17 16:05:31 -05002396static int
2397lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2398 struct lpfc_iocbq *saveq)
2399{
2400 IOCB_t * irsp;
2401 WORD5 * w5p;
2402 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002403 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002404 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002405 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002406
2407 match = 0;
2408 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002409
2410 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2411 if (pring->lpfc_sli_rcv_async_status)
2412 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2413 else
2414 lpfc_printf_log(phba,
2415 KERN_WARNING,
2416 LOG_SLI,
2417 "0316 Ring %d handler: unexpected "
2418 "ASYNC_STATUS iocb received evt_code "
2419 "0x%x\n",
2420 pring->ringno,
2421 irsp->un.asyncstat.evt_code);
2422 return 1;
2423 }
2424
James Smart3163f722008-02-08 18:50:25 -05002425 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2426 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2427 if (irsp->ulpBdeCount > 0) {
2428 dmzbuf = lpfc_sli_get_buff(phba, pring,
2429 irsp->un.ulpWord[3]);
2430 lpfc_in_buf_free(phba, dmzbuf);
2431 }
2432
2433 if (irsp->ulpBdeCount > 1) {
2434 dmzbuf = lpfc_sli_get_buff(phba, pring,
2435 irsp->unsli3.sli3Words[3]);
2436 lpfc_in_buf_free(phba, dmzbuf);
2437 }
2438
2439 if (irsp->ulpBdeCount > 2) {
2440 dmzbuf = lpfc_sli_get_buff(phba, pring,
2441 irsp->unsli3.sli3Words[7]);
2442 lpfc_in_buf_free(phba, dmzbuf);
2443 }
2444
2445 return 1;
2446 }
2447
James Smart92d7f7b2007-06-17 19:56:38 -05002448 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002449 if (irsp->ulpBdeCount != 0) {
2450 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002451 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002452 if (!saveq->context2)
2453 lpfc_printf_log(phba,
2454 KERN_ERR,
2455 LOG_SLI,
2456 "0341 Ring %d Cannot find buffer for "
2457 "an unsolicited iocb. tag 0x%x\n",
2458 pring->ringno,
2459 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002460 }
2461 if (irsp->ulpBdeCount == 2) {
2462 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002463 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002464 if (!saveq->context3)
2465 lpfc_printf_log(phba,
2466 KERN_ERR,
2467 LOG_SLI,
2468 "0342 Ring %d Cannot find buffer for an"
2469 " unsolicited iocb. tag 0x%x\n",
2470 pring->ringno,
2471 irsp->unsli3.sli3Words[7]);
2472 }
2473 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002474 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002475 if (irsp->ulpBdeCount != 0) {
2476 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2477 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002478 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002479 lpfc_printf_log(phba,
2480 KERN_ERR,
2481 LOG_SLI,
2482 "0343 Ring %d Cannot find "
2483 "buffer for an unsolicited iocb"
2484 ". tag 0x%x\n", pring->ringno,
2485 irsp->un.ulpWord[3]);
2486 }
2487 if (irsp->ulpBdeCount == 2) {
2488 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2489 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002490 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002491 lpfc_printf_log(phba,
2492 KERN_ERR,
2493 LOG_SLI,
2494 "0344 Ring %d Cannot find "
2495 "buffer for an unsolicited "
2496 "iocb. tag 0x%x\n",
2497 pring->ringno,
2498 irsp->unsli3.sli3Words[7]);
2499 }
2500 }
James Smart92d7f7b2007-06-17 19:56:38 -05002501 }
James Smart9c2face2008-01-11 01:53:18 -05002502 if (irsp->ulpBdeCount != 0 &&
2503 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2504 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2505 int found = 0;
2506
2507 /* search continue save q for same XRI */
2508 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002509 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2510 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002511 list_add_tail(&saveq->list, &iocbq->list);
2512 found = 1;
2513 break;
2514 }
2515 }
2516 if (!found)
2517 list_add_tail(&saveq->clist,
2518 &pring->iocb_continue_saveq);
2519 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2520 list_del_init(&iocbq->clist);
2521 saveq = iocbq;
2522 irsp = &(saveq->iocb);
2523 } else
2524 return 0;
2525 }
2526 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2527 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2528 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002529 Rctl = FC_RCTL_ELS_REQ;
2530 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002531 } else {
2532 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2533 Rctl = w5p->hcsw.Rctl;
2534 Type = w5p->hcsw.Type;
2535
2536 /* Firmware Workaround */
2537 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2538 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2539 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002540 Rctl = FC_RCTL_ELS_REQ;
2541 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002542 w5p->hcsw.Rctl = Rctl;
2543 w5p->hcsw.Type = Type;
2544 }
2545 }
James Smart92d7f7b2007-06-17 19:56:38 -05002546
James Smart3772a992009-05-22 14:50:54 -04002547 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002548 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002549 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002550 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002551 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002552
James Smart92d7f7b2007-06-17 19:56:38 -05002553 return 1;
dea31012005-04-17 16:05:31 -05002554}
2555
James Smarte59058c2008-08-24 21:49:00 -04002556/**
James Smart3621a712009-04-06 18:47:14 -04002557 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002558 * @phba: Pointer to HBA context object.
2559 * @pring: Pointer to driver SLI ring object.
2560 * @prspiocb: Pointer to response iocb object.
2561 *
2562 * This function looks up the iocb_lookup table to get the command iocb
2563 * corresponding to the given response iocb using the iotag of the
2564 * response iocb. This function is called with the hbalock held.
2565 * This function returns the command iocb object if it finds the command
2566 * iocb else returns NULL.
2567 **/
dea31012005-04-17 16:05:31 -05002568static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002569lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2570 struct lpfc_sli_ring *pring,
2571 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002572{
dea31012005-04-17 16:05:31 -05002573 struct lpfc_iocbq *cmd_iocb = NULL;
2574 uint16_t iotag;
2575
James Bottomley604a3e32005-10-29 10:28:33 -05002576 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002577
James Bottomley604a3e32005-10-29 10:28:33 -05002578 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2579 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002580 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002581 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2582 pring->txcmplq_cnt--;
2583 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2584 }
James Bottomley604a3e32005-10-29 10:28:33 -05002585 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002586 }
2587
dea31012005-04-17 16:05:31 -05002588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002589 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002590 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002591 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002592 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002593 return NULL;
2594}
2595
James Smarte59058c2008-08-24 21:49:00 -04002596/**
James Smart3772a992009-05-22 14:50:54 -04002597 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2598 * @phba: Pointer to HBA context object.
2599 * @pring: Pointer to driver SLI ring object.
2600 * @iotag: IOCB tag.
2601 *
2602 * This function looks up the iocb_lookup table to get the command iocb
2603 * corresponding to the given iotag. This function is called with the
2604 * hbalock held.
2605 * This function returns the command iocb object if it finds the command
2606 * iocb else returns NULL.
2607 **/
2608static struct lpfc_iocbq *
2609lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2610 struct lpfc_sli_ring *pring, uint16_t iotag)
2611{
2612 struct lpfc_iocbq *cmd_iocb;
2613
2614 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2615 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2616 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002617 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2618 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2619 pring->txcmplq_cnt--;
2620 }
James Smart3772a992009-05-22 14:50:54 -04002621 return cmd_iocb;
2622 }
2623
2624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2625 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2626 iotag, phba->sli.last_iotag);
2627 return NULL;
2628}
2629
2630/**
James Smart3621a712009-04-06 18:47:14 -04002631 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002632 * @phba: Pointer to HBA context object.
2633 * @pring: Pointer to driver SLI ring object.
2634 * @saveq: Pointer to the response iocb to be processed.
2635 *
2636 * This function is called by the ring event handler for non-fcp
2637 * rings when there is a new response iocb in the response ring.
2638 * The caller is not required to hold any locks. This function
2639 * gets the command iocb associated with the response iocb and
2640 * calls the completion handler for the command iocb. If there
2641 * is no completion handler, the function will free the resources
2642 * associated with command iocb. If the response iocb is for
2643 * an already aborted command iocb, the status of the completion
2644 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2645 * This function always returns 1.
2646 **/
dea31012005-04-17 16:05:31 -05002647static int
James Smart2e0fef82007-06-17 19:56:36 -05002648lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002649 struct lpfc_iocbq *saveq)
2650{
James Smart2e0fef82007-06-17 19:56:36 -05002651 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002652 int rc = 1;
2653 unsigned long iflag;
2654
2655 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002656 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002657 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002658 spin_unlock_irqrestore(&phba->hbalock, iflag);
2659
dea31012005-04-17 16:05:31 -05002660 if (cmdiocbp) {
2661 if (cmdiocbp->iocb_cmpl) {
2662 /*
James Smartea2151b2008-09-07 11:52:10 -04002663 * If an ELS command failed send an event to mgmt
2664 * application.
2665 */
2666 if (saveq->iocb.ulpStatus &&
2667 (pring->ringno == LPFC_ELS_RING) &&
2668 (cmdiocbp->iocb.ulpCommand ==
2669 CMD_ELS_REQUEST64_CR))
2670 lpfc_send_els_failure_event(phba,
2671 cmdiocbp, saveq);
2672
2673 /*
dea31012005-04-17 16:05:31 -05002674 * Post all ELS completions to the worker thread.
2675 * All other are passed to the completion callback.
2676 */
2677 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002678 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2679 (cmdiocbp->iocb_flag &
2680 LPFC_DRIVER_ABORTED)) {
2681 spin_lock_irqsave(&phba->hbalock,
2682 iflag);
James Smart07951072007-04-25 09:51:38 -04002683 cmdiocbp->iocb_flag &=
2684 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002685 spin_unlock_irqrestore(&phba->hbalock,
2686 iflag);
James Smart07951072007-04-25 09:51:38 -04002687 saveq->iocb.ulpStatus =
2688 IOSTAT_LOCAL_REJECT;
2689 saveq->iocb.un.ulpWord[4] =
2690 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002691
2692 /* Firmware could still be in progress
2693 * of DMAing payload, so don't free data
2694 * buffer till after a hbeat.
2695 */
James Smart341af102010-01-26 23:07:37 -05002696 spin_lock_irqsave(&phba->hbalock,
2697 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002698 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002699 spin_unlock_irqrestore(&phba->hbalock,
2700 iflag);
2701 }
James Smart0f65ff62010-02-26 14:14:23 -05002702 if (phba->sli_rev == LPFC_SLI_REV4) {
2703 if (saveq->iocb_flag &
2704 LPFC_EXCHANGE_BUSY) {
2705 /* Set cmdiocb flag for the
2706 * exchange busy so sgl (xri)
2707 * will not be released until
2708 * the abort xri is received
2709 * from hba.
2710 */
2711 spin_lock_irqsave(
2712 &phba->hbalock, iflag);
2713 cmdiocbp->iocb_flag |=
2714 LPFC_EXCHANGE_BUSY;
2715 spin_unlock_irqrestore(
2716 &phba->hbalock, iflag);
2717 }
2718 if (cmdiocbp->iocb_flag &
2719 LPFC_DRIVER_ABORTED) {
2720 /*
2721 * Clear LPFC_DRIVER_ABORTED
2722 * bit in case it was driver
2723 * initiated abort.
2724 */
2725 spin_lock_irqsave(
2726 &phba->hbalock, iflag);
2727 cmdiocbp->iocb_flag &=
2728 ~LPFC_DRIVER_ABORTED;
2729 spin_unlock_irqrestore(
2730 &phba->hbalock, iflag);
2731 cmdiocbp->iocb.ulpStatus =
2732 IOSTAT_LOCAL_REJECT;
2733 cmdiocbp->iocb.un.ulpWord[4] =
2734 IOERR_ABORT_REQUESTED;
2735 /*
2736 * For SLI4, irsiocb contains
2737 * NO_XRI in sli_xritag, it
2738 * shall not affect releasing
2739 * sgl (xri) process.
2740 */
2741 saveq->iocb.ulpStatus =
2742 IOSTAT_LOCAL_REJECT;
2743 saveq->iocb.un.ulpWord[4] =
2744 IOERR_SLI_ABORTED;
2745 spin_lock_irqsave(
2746 &phba->hbalock, iflag);
2747 saveq->iocb_flag |=
2748 LPFC_DELAY_MEM_FREE;
2749 spin_unlock_irqrestore(
2750 &phba->hbalock, iflag);
2751 }
James Smart07951072007-04-25 09:51:38 -04002752 }
dea31012005-04-17 16:05:31 -05002753 }
James Smart2e0fef82007-06-17 19:56:36 -05002754 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002755 } else
2756 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002757 } else {
2758 /*
2759 * Unknown initiating command based on the response iotag.
2760 * This could be the case on the ELS ring because of
2761 * lpfc_els_abort().
2762 */
2763 if (pring->ringno != LPFC_ELS_RING) {
2764 /*
2765 * Ring <ringno> handler: unexpected completion IoTag
2766 * <IoTag>
2767 */
James Smarta257bf92009-04-06 18:48:10 -04002768 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002769 "0322 Ring %d handler: "
2770 "unexpected completion IoTag x%x "
2771 "Data: x%x x%x x%x x%x\n",
2772 pring->ringno,
2773 saveq->iocb.ulpIoTag,
2774 saveq->iocb.ulpStatus,
2775 saveq->iocb.un.ulpWord[4],
2776 saveq->iocb.ulpCommand,
2777 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002778 }
2779 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002780
dea31012005-04-17 16:05:31 -05002781 return rc;
2782}
2783
James Smarte59058c2008-08-24 21:49:00 -04002784/**
James Smart3621a712009-04-06 18:47:14 -04002785 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002786 * @phba: Pointer to HBA context object.
2787 * @pring: Pointer to driver SLI ring object.
2788 *
2789 * This function is called from the iocb ring event handlers when
2790 * put pointer is ahead of the get pointer for a ring. This function signal
2791 * an error attention condition to the worker thread and the worker
2792 * thread will transition the HBA to offline state.
2793 **/
James Smart2e0fef82007-06-17 19:56:36 -05002794static void
2795lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002796{
James Smart34b02dc2008-08-24 21:49:55 -04002797 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002798 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002799 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002800 * rsp ring <portRspMax>
2801 */
2802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002803 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002804 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002805 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002806 pring->numRiocb);
2807
James Smart2e0fef82007-06-17 19:56:36 -05002808 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002809
2810 /*
2811 * All error attention handlers are posted to
2812 * worker thread
2813 */
2814 phba->work_ha |= HA_ERATT;
2815 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002816
James Smart5e9d9b82008-06-14 22:52:53 -04002817 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002818
2819 return;
2820}
2821
James Smarte59058c2008-08-24 21:49:00 -04002822/**
James Smart3621a712009-04-06 18:47:14 -04002823 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002824 * @ptr: Pointer to address of HBA context object.
2825 *
2826 * This function is invoked by the Error Attention polling timer when the
2827 * timer times out. It will check the SLI Error Attention register for
2828 * possible attention events. If so, it will post an Error Attention event
2829 * and wake up worker thread to process it. Otherwise, it will set up the
2830 * Error Attention polling timer for the next poll.
2831 **/
2832void lpfc_poll_eratt(unsigned long ptr)
2833{
2834 struct lpfc_hba *phba;
2835 uint32_t eratt = 0;
2836
2837 phba = (struct lpfc_hba *)ptr;
2838
2839 /* Check chip HA register for error event */
2840 eratt = lpfc_sli_check_eratt(phba);
2841
2842 if (eratt)
2843 /* Tell the worker thread there is work to do */
2844 lpfc_worker_wake_up(phba);
2845 else
2846 /* Restart the timer for next eratt poll */
2847 mod_timer(&phba->eratt_poll, jiffies +
2848 HZ * LPFC_ERATT_POLL_INTERVAL);
2849 return;
2850}
2851
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002852
James Smarte59058c2008-08-24 21:49:00 -04002853/**
James Smart3621a712009-04-06 18:47:14 -04002854 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002855 * @phba: Pointer to HBA context object.
2856 * @pring: Pointer to driver SLI ring object.
2857 * @mask: Host attention register mask for this ring.
2858 *
2859 * This function is called from the interrupt context when there is a ring
2860 * event for the fcp ring. The caller does not hold any lock.
2861 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002862 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002863 * LE bit set. The function will call the completion handler of the command iocb
2864 * if the response iocb indicates a completion for a command iocb or it is
2865 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2866 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002867 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002868 * to check it explicitly.
2869 */
2870int
James Smart2e0fef82007-06-17 19:56:36 -05002871lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2872 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002873{
James Smart34b02dc2008-08-24 21:49:55 -04002874 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002875 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002876 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002877 struct lpfc_iocbq *cmdiocbq = NULL;
2878 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002879 uint32_t status;
2880 uint32_t portRspPut, portRspMax;
2881 int rc = 1;
2882 lpfc_iocb_type type;
2883 unsigned long iflag;
2884 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002885
James Smart2e0fef82007-06-17 19:56:36 -05002886 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002887 pring->stats.iocb_event++;
2888
dea31012005-04-17 16:05:31 -05002889 /*
2890 * The next available response entry should never exceed the maximum
2891 * entries. If it does, treat it as an adapter hardware error.
2892 */
2893 portRspMax = pring->numRiocb;
2894 portRspPut = le32_to_cpu(pgp->rspPutInx);
2895 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002896 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002897 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002898 return 1;
2899 }
James Smart45ed1192009-10-02 15:17:02 -04002900 if (phba->fcp_ring_in_use) {
2901 spin_unlock_irqrestore(&phba->hbalock, iflag);
2902 return 1;
2903 } else
2904 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002905
2906 rmb();
2907 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002908 /*
2909 * Fetch an entry off the ring and copy it into a local data
2910 * structure. The copy involves a byte-swap since the
2911 * network byte order and pci byte orders are different.
2912 */
James Smarted957682007-06-17 19:56:37 -05002913 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002914 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002915
2916 if (++pring->rspidx >= portRspMax)
2917 pring->rspidx = 0;
2918
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002919 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2920 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002921 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002922 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002923 irsp = &rspiocbq.iocb;
2924
dea31012005-04-17 16:05:31 -05002925 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2926 pring->stats.iocb_rsp++;
2927 rsp_cmpl++;
2928
2929 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002930 /*
2931 * If resource errors reported from HBA, reduce
2932 * queuedepths of the SCSI device.
2933 */
2934 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2935 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2936 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002937 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002938 spin_lock_irqsave(&phba->hbalock, iflag);
2939 }
2940
dea31012005-04-17 16:05:31 -05002941 /* Rsp ring <ringno> error: IOCB */
2942 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002943 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002944 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002945 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002946 irsp->un.ulpWord[0],
2947 irsp->un.ulpWord[1],
2948 irsp->un.ulpWord[2],
2949 irsp->un.ulpWord[3],
2950 irsp->un.ulpWord[4],
2951 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002952 *(uint32_t *)&irsp->un1,
2953 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002954 }
2955
2956 switch (type) {
2957 case LPFC_ABORT_IOCB:
2958 case LPFC_SOL_IOCB:
2959 /*
2960 * Idle exchange closed via ABTS from port. No iocb
2961 * resources need to be recovered.
2962 */
2963 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002964 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002965 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002966 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002967 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002968 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002969 break;
2970 }
2971
James Bottomley604a3e32005-10-29 10:28:33 -05002972 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2973 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002974 if (unlikely(!cmdiocbq))
2975 break;
2976 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2977 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2978 if (cmdiocbq->iocb_cmpl) {
2979 spin_unlock_irqrestore(&phba->hbalock, iflag);
2980 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2981 &rspiocbq);
2982 spin_lock_irqsave(&phba->hbalock, iflag);
2983 }
dea31012005-04-17 16:05:31 -05002984 break;
James Smarta4bc3372006-12-02 13:34:16 -05002985 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002986 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002987 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002988 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002989 break;
dea31012005-04-17 16:05:31 -05002990 default:
2991 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2992 char adaptermsg[LPFC_MAX_ADPTMSG];
2993 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2994 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2995 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002996 dev_warn(&((phba->pcidev)->dev),
2997 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002998 phba->brd_no, adaptermsg);
2999 } else {
3000 /* Unknown IOCB command */
3001 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003002 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003003 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003004 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003005 irsp->ulpStatus,
3006 irsp->ulpIoTag,
3007 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003008 }
3009 break;
3010 }
3011
3012 /*
3013 * The response IOCB has been processed. Update the ring
3014 * pointer in SLIM. If the port response put pointer has not
3015 * been updated, sync the pgp->rspPutInx and fetch the new port
3016 * response put pointer.
3017 */
James Smarted957682007-06-17 19:56:37 -05003018 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003019
3020 if (pring->rspidx == portRspPut)
3021 portRspPut = le32_to_cpu(pgp->rspPutInx);
3022 }
3023
3024 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3025 pring->stats.iocb_rsp_full++;
3026 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3027 writel(status, phba->CAregaddr);
3028 readl(phba->CAregaddr);
3029 }
3030 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3031 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3032 pring->stats.iocb_cmd_empty++;
3033
3034 /* Force update of the local copy of cmdGetInx */
3035 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3036 lpfc_sli_resume_iocb(phba, pring);
3037
3038 if ((pring->lpfc_sli_cmd_available))
3039 (pring->lpfc_sli_cmd_available) (phba, pring);
3040
3041 }
3042
James Smart45ed1192009-10-02 15:17:02 -04003043 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003044 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003045 return rc;
3046}
3047
James Smarte59058c2008-08-24 21:49:00 -04003048/**
James Smart3772a992009-05-22 14:50:54 -04003049 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3050 * @phba: Pointer to HBA context object.
3051 * @pring: Pointer to driver SLI ring object.
3052 * @rspiocbp: Pointer to driver response IOCB object.
3053 *
3054 * This function is called from the worker thread when there is a slow-path
3055 * response IOCB to process. This function chains all the response iocbs until
3056 * seeing the iocb with the LE bit set. The function will call
3057 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3058 * completion of a command iocb. The function will call the
3059 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3060 * The function frees the resources or calls the completion handler if this
3061 * iocb is an abort completion. The function returns NULL when the response
3062 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3063 * this function shall chain the iocb on to the iocb_continueq and return the
3064 * response iocb passed in.
3065 **/
3066static struct lpfc_iocbq *
3067lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3068 struct lpfc_iocbq *rspiocbp)
3069{
3070 struct lpfc_iocbq *saveq;
3071 struct lpfc_iocbq *cmdiocbp;
3072 struct lpfc_iocbq *next_iocb;
3073 IOCB_t *irsp = NULL;
3074 uint32_t free_saveq;
3075 uint8_t iocb_cmd_type;
3076 lpfc_iocb_type type;
3077 unsigned long iflag;
3078 int rc;
3079
3080 spin_lock_irqsave(&phba->hbalock, iflag);
3081 /* First add the response iocb to the countinueq list */
3082 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3083 pring->iocb_continueq_cnt++;
3084
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003085 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003086 irsp = &rspiocbp->iocb;
3087 if (irsp->ulpLe) {
3088 /*
3089 * By default, the driver expects to free all resources
3090 * associated with this iocb completion.
3091 */
3092 free_saveq = 1;
3093 saveq = list_get_first(&pring->iocb_continueq,
3094 struct lpfc_iocbq, list);
3095 irsp = &(saveq->iocb);
3096 list_del_init(&pring->iocb_continueq);
3097 pring->iocb_continueq_cnt = 0;
3098
3099 pring->stats.iocb_rsp++;
3100
3101 /*
3102 * If resource errors reported from HBA, reduce
3103 * queuedepths of the SCSI device.
3104 */
3105 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3106 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3107 spin_unlock_irqrestore(&phba->hbalock, iflag);
3108 phba->lpfc_rampdown_queue_depth(phba);
3109 spin_lock_irqsave(&phba->hbalock, iflag);
3110 }
3111
3112 if (irsp->ulpStatus) {
3113 /* Rsp ring <ringno> error: IOCB */
3114 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3115 "0328 Rsp Ring %d error: "
3116 "IOCB Data: "
3117 "x%x x%x x%x x%x "
3118 "x%x x%x x%x x%x "
3119 "x%x x%x x%x x%x "
3120 "x%x x%x x%x x%x\n",
3121 pring->ringno,
3122 irsp->un.ulpWord[0],
3123 irsp->un.ulpWord[1],
3124 irsp->un.ulpWord[2],
3125 irsp->un.ulpWord[3],
3126 irsp->un.ulpWord[4],
3127 irsp->un.ulpWord[5],
3128 *(((uint32_t *) irsp) + 6),
3129 *(((uint32_t *) irsp) + 7),
3130 *(((uint32_t *) irsp) + 8),
3131 *(((uint32_t *) irsp) + 9),
3132 *(((uint32_t *) irsp) + 10),
3133 *(((uint32_t *) irsp) + 11),
3134 *(((uint32_t *) irsp) + 12),
3135 *(((uint32_t *) irsp) + 13),
3136 *(((uint32_t *) irsp) + 14),
3137 *(((uint32_t *) irsp) + 15));
3138 }
3139
3140 /*
3141 * Fetch the IOCB command type and call the correct completion
3142 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3143 * get freed back to the lpfc_iocb_list by the discovery
3144 * kernel thread.
3145 */
3146 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3147 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3148 switch (type) {
3149 case LPFC_SOL_IOCB:
3150 spin_unlock_irqrestore(&phba->hbalock, iflag);
3151 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3152 spin_lock_irqsave(&phba->hbalock, iflag);
3153 break;
3154
3155 case LPFC_UNSOL_IOCB:
3156 spin_unlock_irqrestore(&phba->hbalock, iflag);
3157 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3158 spin_lock_irqsave(&phba->hbalock, iflag);
3159 if (!rc)
3160 free_saveq = 0;
3161 break;
3162
3163 case LPFC_ABORT_IOCB:
3164 cmdiocbp = NULL;
3165 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3166 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3167 saveq);
3168 if (cmdiocbp) {
3169 /* Call the specified completion routine */
3170 if (cmdiocbp->iocb_cmpl) {
3171 spin_unlock_irqrestore(&phba->hbalock,
3172 iflag);
3173 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3174 saveq);
3175 spin_lock_irqsave(&phba->hbalock,
3176 iflag);
3177 } else
3178 __lpfc_sli_release_iocbq(phba,
3179 cmdiocbp);
3180 }
3181 break;
3182
3183 case LPFC_UNKNOWN_IOCB:
3184 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3185 char adaptermsg[LPFC_MAX_ADPTMSG];
3186 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3187 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3188 MAX_MSG_DATA);
3189 dev_warn(&((phba->pcidev)->dev),
3190 "lpfc%d: %s\n",
3191 phba->brd_no, adaptermsg);
3192 } else {
3193 /* Unknown IOCB command */
3194 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3195 "0335 Unknown IOCB "
3196 "command Data: x%x "
3197 "x%x x%x x%x\n",
3198 irsp->ulpCommand,
3199 irsp->ulpStatus,
3200 irsp->ulpIoTag,
3201 irsp->ulpContext);
3202 }
3203 break;
3204 }
3205
3206 if (free_saveq) {
3207 list_for_each_entry_safe(rspiocbp, next_iocb,
3208 &saveq->list, list) {
3209 list_del(&rspiocbp->list);
3210 __lpfc_sli_release_iocbq(phba, rspiocbp);
3211 }
3212 __lpfc_sli_release_iocbq(phba, saveq);
3213 }
3214 rspiocbp = NULL;
3215 }
3216 spin_unlock_irqrestore(&phba->hbalock, iflag);
3217 return rspiocbp;
3218}
3219
3220/**
3221 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003222 * @phba: Pointer to HBA context object.
3223 * @pring: Pointer to driver SLI ring object.
3224 * @mask: Host attention register mask for this ring.
3225 *
James Smart3772a992009-05-22 14:50:54 -04003226 * This routine wraps the actual slow_ring event process routine from the
3227 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003228 **/
James Smart3772a992009-05-22 14:50:54 -04003229void
James Smart2e0fef82007-06-17 19:56:36 -05003230lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3231 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003232{
James Smart3772a992009-05-22 14:50:54 -04003233 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3234}
3235
3236/**
3237 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3238 * @phba: Pointer to HBA context object.
3239 * @pring: Pointer to driver SLI ring object.
3240 * @mask: Host attention register mask for this ring.
3241 *
3242 * This function is called from the worker thread when there is a ring event
3243 * for non-fcp rings. The caller does not hold any lock. The function will
3244 * remove each response iocb in the response ring and calls the handle
3245 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3246 **/
3247static void
3248lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3249 struct lpfc_sli_ring *pring, uint32_t mask)
3250{
James Smart34b02dc2008-08-24 21:49:55 -04003251 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003252 IOCB_t *entry;
3253 IOCB_t *irsp = NULL;
3254 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003255 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003256 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003257 uint32_t status;
dea31012005-04-17 16:05:31 -05003258
James Smart34b02dc2008-08-24 21:49:55 -04003259 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003260 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003261 pring->stats.iocb_event++;
3262
dea31012005-04-17 16:05:31 -05003263 /*
3264 * The next available response entry should never exceed the maximum
3265 * entries. If it does, treat it as an adapter hardware error.
3266 */
3267 portRspMax = pring->numRiocb;
3268 portRspPut = le32_to_cpu(pgp->rspPutInx);
3269 if (portRspPut >= portRspMax) {
3270 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003271 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003272 * rsp ring <portRspMax>
3273 */
James Smarted957682007-06-17 19:56:37 -05003274 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003275 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003276 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003277 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003278
James Smart2e0fef82007-06-17 19:56:36 -05003279 phba->link_state = LPFC_HBA_ERROR;
3280 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003281
3282 phba->work_hs = HS_FFER3;
3283 lpfc_handle_eratt(phba);
3284
James Smart3772a992009-05-22 14:50:54 -04003285 return;
dea31012005-04-17 16:05:31 -05003286 }
3287
3288 rmb();
dea31012005-04-17 16:05:31 -05003289 while (pring->rspidx != portRspPut) {
3290 /*
3291 * Build a completion list and call the appropriate handler.
3292 * The process is to get the next available response iocb, get
3293 * a free iocb from the list, copy the response data into the
3294 * free iocb, insert to the continuation list, and update the
3295 * next response index to slim. This process makes response
3296 * iocb's in the ring available to DMA as fast as possible but
3297 * pays a penalty for a copy operation. Since the iocb is
3298 * only 32 bytes, this penalty is considered small relative to
3299 * the PCI reads for register values and a slim write. When
3300 * the ulpLe field is set, the entire Command has been
3301 * received.
3302 */
James Smarted957682007-06-17 19:56:37 -05003303 entry = lpfc_resp_iocb(phba, pring);
3304
James Smart858c9f62007-06-17 19:56:39 -05003305 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003306 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003307 if (rspiocbp == NULL) {
3308 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003309 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003310 break;
3311 }
3312
James Smarted957682007-06-17 19:56:37 -05003313 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3314 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003315 irsp = &rspiocbp->iocb;
3316
3317 if (++pring->rspidx >= portRspMax)
3318 pring->rspidx = 0;
3319
James Smarta58cbd52007-08-02 11:09:43 -04003320 if (pring->ringno == LPFC_ELS_RING) {
3321 lpfc_debugfs_slow_ring_trc(phba,
3322 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3323 *(((uint32_t *) irsp) + 4),
3324 *(((uint32_t *) irsp) + 6),
3325 *(((uint32_t *) irsp) + 7));
3326 }
3327
James Smarted957682007-06-17 19:56:37 -05003328 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003329
James Smart3772a992009-05-22 14:50:54 -04003330 spin_unlock_irqrestore(&phba->hbalock, iflag);
3331 /* Handle the response IOCB */
3332 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3333 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003334
3335 /*
3336 * If the port response put pointer has not been updated, sync
3337 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3338 * response put pointer.
3339 */
3340 if (pring->rspidx == portRspPut) {
3341 portRspPut = le32_to_cpu(pgp->rspPutInx);
3342 }
3343 } /* while (pring->rspidx != portRspPut) */
3344
James Smart92d7f7b2007-06-17 19:56:38 -05003345 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003346 /* At least one response entry has been freed */
3347 pring->stats.iocb_rsp_full++;
3348 /* SET RxRE_RSP in Chip Att register */
3349 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3350 writel(status, phba->CAregaddr);
3351 readl(phba->CAregaddr); /* flush */
3352 }
3353 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3354 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3355 pring->stats.iocb_cmd_empty++;
3356
3357 /* Force update of the local copy of cmdGetInx */
3358 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3359 lpfc_sli_resume_iocb(phba, pring);
3360
3361 if ((pring->lpfc_sli_cmd_available))
3362 (pring->lpfc_sli_cmd_available) (phba, pring);
3363
3364 }
3365
James Smart2e0fef82007-06-17 19:56:36 -05003366 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003367 return;
dea31012005-04-17 16:05:31 -05003368}
3369
James Smarte59058c2008-08-24 21:49:00 -04003370/**
James Smart4f774512009-05-22 14:52:35 -04003371 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3372 * @phba: Pointer to HBA context object.
3373 * @pring: Pointer to driver SLI ring object.
3374 * @mask: Host attention register mask for this ring.
3375 *
3376 * This function is called from the worker thread when there is a pending
3377 * ELS response iocb on the driver internal slow-path response iocb worker
3378 * queue. The caller does not hold any lock. The function will remove each
3379 * response iocb from the response worker queue and calls the handle
3380 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3381 **/
3382static void
3383lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3384 struct lpfc_sli_ring *pring, uint32_t mask)
3385{
3386 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003387 struct hbq_dmabuf *dmabuf;
3388 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003389 unsigned long iflag;
3390
James Smart45ed1192009-10-02 15:17:02 -04003391 spin_lock_irqsave(&phba->hbalock, iflag);
3392 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3393 spin_unlock_irqrestore(&phba->hbalock, iflag);
3394 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003395 /* Get the response iocb from the head of work queue */
3396 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003397 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003398 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003399 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003400
3401 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3402 case CQE_CODE_COMPL_WQE:
3403 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3404 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003405 /* Translate ELS WCQE to response IOCBQ */
3406 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3407 irspiocbq);
3408 if (irspiocbq)
3409 lpfc_sli_sp_handle_rspiocb(phba, pring,
3410 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003411 break;
3412 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003413 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003414 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3415 cq_event);
3416 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3417 break;
3418 default:
3419 break;
3420 }
James Smart4f774512009-05-22 14:52:35 -04003421 }
3422}
3423
3424/**
James Smart3621a712009-04-06 18:47:14 -04003425 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003426 * @phba: Pointer to HBA context object.
3427 * @pring: Pointer to driver SLI ring object.
3428 *
3429 * This function aborts all iocbs in the given ring and frees all the iocb
3430 * objects in txq. This function issues an abort iocb for all the iocb commands
3431 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3432 * the return of this function. The caller is not required to hold any locks.
3433 **/
James Smart2e0fef82007-06-17 19:56:36 -05003434void
dea31012005-04-17 16:05:31 -05003435lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3436{
James Smart2534ba72007-04-25 09:52:20 -04003437 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003438 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003439
James Smart92d7f7b2007-06-17 19:56:38 -05003440 if (pring->ringno == LPFC_ELS_RING) {
3441 lpfc_fabric_abort_hba(phba);
3442 }
3443
dea31012005-04-17 16:05:31 -05003444 /* Error everything on txq and txcmplq
3445 * First do the txq.
3446 */
James Smart2e0fef82007-06-17 19:56:36 -05003447 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003448 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003449 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003450
3451 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003452 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3453 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3454
James Smart2e0fef82007-06-17 19:56:36 -05003455 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003456
James Smarta257bf92009-04-06 18:48:10 -04003457 /* Cancel all the IOCBs from the completions list */
3458 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3459 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003460}
3461
James Smarte59058c2008-08-24 21:49:00 -04003462/**
James Smart3621a712009-04-06 18:47:14 -04003463 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003464 * @phba: Pointer to HBA context object.
3465 *
3466 * This function flushes all iocbs in the fcp ring and frees all the iocb
3467 * objects in txq and txcmplq. This function will not issue abort iocbs
3468 * for all the iocb commands in txcmplq, they will just be returned with
3469 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3470 * slot has been permanently disabled.
3471 **/
3472void
3473lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3474{
3475 LIST_HEAD(txq);
3476 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003477 struct lpfc_sli *psli = &phba->sli;
3478 struct lpfc_sli_ring *pring;
3479
3480 /* Currently, only one fcp ring */
3481 pring = &psli->ring[psli->fcp_ring];
3482
3483 spin_lock_irq(&phba->hbalock);
3484 /* Retrieve everything on txq */
3485 list_splice_init(&pring->txq, &txq);
3486 pring->txq_cnt = 0;
3487
3488 /* Retrieve everything on the txcmplq */
3489 list_splice_init(&pring->txcmplq, &txcmplq);
3490 pring->txcmplq_cnt = 0;
3491 spin_unlock_irq(&phba->hbalock);
3492
3493 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003494 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3495 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003496
3497 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003498 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3499 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003500}
3501
3502/**
James Smart3772a992009-05-22 14:50:54 -04003503 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003504 * @phba: Pointer to HBA context object.
3505 * @mask: Bit mask to be checked.
3506 *
3507 * This function reads the host status register and compares
3508 * with the provided bit mask to check if HBA completed
3509 * the restart. This function will wait in a loop for the
3510 * HBA to complete restart. If the HBA does not restart within
3511 * 15 iterations, the function will reset the HBA again. The
3512 * function returns 1 when HBA fail to restart otherwise returns
3513 * zero.
3514 **/
James Smart3772a992009-05-22 14:50:54 -04003515static int
3516lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003517{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003518 uint32_t status;
3519 int i = 0;
3520 int retval = 0;
dea31012005-04-17 16:05:31 -05003521
Jamie Wellnitz41415862006-02-28 19:25:27 -05003522 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003523 if (lpfc_readl(phba->HSregaddr, &status))
3524 return 1;
dea31012005-04-17 16:05:31 -05003525
Jamie Wellnitz41415862006-02-28 19:25:27 -05003526 /*
3527 * Check status register every 100ms for 5 retries, then every
3528 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3529 * every 2.5 sec for 4.
3530 * Break our of the loop if errors occurred during init.
3531 */
3532 while (((status & mask) != mask) &&
3533 !(status & HS_FFERM) &&
3534 i++ < 20) {
dea31012005-04-17 16:05:31 -05003535
Jamie Wellnitz41415862006-02-28 19:25:27 -05003536 if (i <= 5)
3537 msleep(10);
3538 else if (i <= 10)
3539 msleep(500);
3540 else
3541 msleep(2500);
dea31012005-04-17 16:05:31 -05003542
Jamie Wellnitz41415862006-02-28 19:25:27 -05003543 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003544 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003545 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003546 lpfc_sli_brdrestart(phba);
3547 }
3548 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003549 if (lpfc_readl(phba->HSregaddr, &status)) {
3550 retval = 1;
3551 break;
3552 }
dea31012005-04-17 16:05:31 -05003553 }
dea31012005-04-17 16:05:31 -05003554
Jamie Wellnitz41415862006-02-28 19:25:27 -05003555 /* Check to see if any errors occurred during init */
3556 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3558 "2751 Adapter failed to restart, "
3559 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3560 status,
3561 readl(phba->MBslimaddr + 0xa8),
3562 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003563 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003564 retval = 1;
3565 }
dea31012005-04-17 16:05:31 -05003566
Jamie Wellnitz41415862006-02-28 19:25:27 -05003567 return retval;
dea31012005-04-17 16:05:31 -05003568}
3569
James Smartda0436e2009-05-22 14:51:39 -04003570/**
3571 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3572 * @phba: Pointer to HBA context object.
3573 * @mask: Bit mask to be checked.
3574 *
3575 * This function checks the host status register to check if HBA is
3576 * ready. This function will wait in a loop for the HBA to be ready
3577 * If the HBA is not ready , the function will will reset the HBA PCI
3578 * function again. The function returns 1 when HBA fail to be ready
3579 * otherwise returns zero.
3580 **/
3581static int
3582lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3583{
3584 uint32_t status;
3585 int retval = 0;
3586
3587 /* Read the HBA Host Status Register */
3588 status = lpfc_sli4_post_status_check(phba);
3589
3590 if (status) {
3591 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3592 lpfc_sli_brdrestart(phba);
3593 status = lpfc_sli4_post_status_check(phba);
3594 }
3595
3596 /* Check to see if any errors occurred during init */
3597 if (status) {
3598 phba->link_state = LPFC_HBA_ERROR;
3599 retval = 1;
3600 } else
3601 phba->sli4_hba.intr_enable = 0;
3602
3603 return retval;
3604}
3605
3606/**
3607 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3608 * @phba: Pointer to HBA context object.
3609 * @mask: Bit mask to be checked.
3610 *
3611 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3612 * from the API jump table function pointer from the lpfc_hba struct.
3613 **/
3614int
3615lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3616{
3617 return phba->lpfc_sli_brdready(phba, mask);
3618}
3619
James Smart92908312006-03-07 15:04:13 -05003620#define BARRIER_TEST_PATTERN (0xdeadbeef)
3621
James Smarte59058c2008-08-24 21:49:00 -04003622/**
James Smart3621a712009-04-06 18:47:14 -04003623 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003624 * @phba: Pointer to HBA context object.
3625 *
James Smart1b511972011-12-13 13:23:09 -05003626 * This function is called before resetting an HBA. This function is called
3627 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003628 **/
James Smart2e0fef82007-06-17 19:56:36 -05003629void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003630{
James Smart65a29c12006-07-06 15:50:50 -04003631 uint32_t __iomem *resp_buf;
3632 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003633 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003634 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003635 int i;
3636 uint8_t hdrtype;
3637
3638 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3639 if (hdrtype != 0x80 ||
3640 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3641 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3642 return;
3643
3644 /*
3645 * Tell the other part of the chip to suspend temporarily all
3646 * its DMA activity.
3647 */
James Smart65a29c12006-07-06 15:50:50 -04003648 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003649
3650 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003651 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3652 return;
James Smart92908312006-03-07 15:04:13 -05003653 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3654 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003655 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003656
James Smart9940b972011-03-11 16:06:12 -05003657 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3658 return;
3659 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003660 /* Clear Chip error bit */
3661 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003662 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003663 }
3664
3665 mbox = 0;
3666 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3667 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3668
3669 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003670 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003671 writel(mbox, mbox_buf);
3672
James Smart9940b972011-03-11 16:06:12 -05003673 for (i = 0; i < 50; i++) {
3674 if (lpfc_readl((resp_buf + 1), &resp_data))
3675 return;
3676 if (resp_data != ~(BARRIER_TEST_PATTERN))
3677 mdelay(1);
3678 else
3679 break;
3680 }
3681 resp_data = 0;
3682 if (lpfc_readl((resp_buf + 1), &resp_data))
3683 return;
3684 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003685 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003686 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003687 goto restore_hc;
3688 else
3689 goto clear_errat;
3690 }
3691
3692 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003693 resp_data = 0;
3694 for (i = 0; i < 500; i++) {
3695 if (lpfc_readl(resp_buf, &resp_data))
3696 return;
3697 if (resp_data != mbox)
3698 mdelay(1);
3699 else
3700 break;
3701 }
James Smart92908312006-03-07 15:04:13 -05003702
3703clear_errat:
3704
James Smart9940b972011-03-11 16:06:12 -05003705 while (++i < 500) {
3706 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3707 return;
3708 if (!(ha_copy & HA_ERATT))
3709 mdelay(1);
3710 else
3711 break;
3712 }
James Smart92908312006-03-07 15:04:13 -05003713
3714 if (readl(phba->HAregaddr) & HA_ERATT) {
3715 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003716 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003717 }
3718
3719restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003720 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003721 writel(hc_copy, phba->HCregaddr);
3722 readl(phba->HCregaddr); /* flush */
3723}
3724
James Smarte59058c2008-08-24 21:49:00 -04003725/**
James Smart3621a712009-04-06 18:47:14 -04003726 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003727 * @phba: Pointer to HBA context object.
3728 *
3729 * This function issues a kill_board mailbox command and waits for
3730 * the error attention interrupt. This function is called for stopping
3731 * the firmware processing. The caller is not required to hold any
3732 * locks. This function calls lpfc_hba_down_post function to free
3733 * any pending commands after the kill. The function will return 1 when it
3734 * fails to kill the board else will return 0.
3735 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003736int
James Smart2e0fef82007-06-17 19:56:36 -05003737lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003738{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003739 struct lpfc_sli *psli;
3740 LPFC_MBOXQ_t *pmb;
3741 uint32_t status;
3742 uint32_t ha_copy;
3743 int retval;
3744 int i = 0;
3745
3746 psli = &phba->sli;
3747
3748 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003749 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003750 "0329 Kill HBA Data: x%x x%x\n",
3751 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003752
James Smart98c9ea52007-10-27 13:37:33 -04003753 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3754 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003755 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003756
3757 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003758 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003759 if (lpfc_readl(phba->HCregaddr, &status)) {
3760 spin_unlock_irq(&phba->hbalock);
3761 mempool_free(pmb, phba->mbox_mem_pool);
3762 return 1;
3763 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003764 status &= ~HC_ERINT_ENA;
3765 writel(status, phba->HCregaddr);
3766 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003767 phba->link_flag |= LS_IGNORE_ERATT;
3768 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003769
3770 lpfc_kill_board(phba, pmb);
3771 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3772 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3773
3774 if (retval != MBX_SUCCESS) {
3775 if (retval != MBX_BUSY)
3776 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003777 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3778 "2752 KILL_BOARD command failed retval %d\n",
3779 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003780 spin_lock_irq(&phba->hbalock);
3781 phba->link_flag &= ~LS_IGNORE_ERATT;
3782 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003783 return 1;
3784 }
3785
James Smartf4b4c682009-05-22 14:53:12 -04003786 spin_lock_irq(&phba->hbalock);
3787 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3788 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003789
Jamie Wellnitz41415862006-02-28 19:25:27 -05003790 mempool_free(pmb, phba->mbox_mem_pool);
3791
3792 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3793 * attention every 100ms for 3 seconds. If we don't get ERATT after
3794 * 3 seconds we still set HBA_ERROR state because the status of the
3795 * board is now undefined.
3796 */
James Smart9940b972011-03-11 16:06:12 -05003797 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3798 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003799 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3800 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003801 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3802 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003803 }
3804
3805 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003806 if (ha_copy & HA_ERATT) {
3807 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003808 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003809 }
James Smart2e0fef82007-06-17 19:56:36 -05003810 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003811 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003812 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003813 phba->link_flag &= ~LS_IGNORE_ERATT;
3814 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003815
Jamie Wellnitz41415862006-02-28 19:25:27 -05003816 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003817 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003818
James Smart2e0fef82007-06-17 19:56:36 -05003819 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003820}
3821
James Smarte59058c2008-08-24 21:49:00 -04003822/**
James Smart3772a992009-05-22 14:50:54 -04003823 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003824 * @phba: Pointer to HBA context object.
3825 *
3826 * This function resets the HBA by writing HC_INITFF to the control
3827 * register. After the HBA resets, this function resets all the iocb ring
3828 * indices. This function disables PCI layer parity checking during
3829 * the reset.
3830 * This function returns 0 always.
3831 * The caller is not required to hold any locks.
3832 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003833int
James Smart2e0fef82007-06-17 19:56:36 -05003834lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003835{
3836 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003837 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003838 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003839 int i;
dea31012005-04-17 16:05:31 -05003840
Jamie Wellnitz41415862006-02-28 19:25:27 -05003841 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003842
Jamie Wellnitz41415862006-02-28 19:25:27 -05003843 /* Reset HBA */
3844 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003845 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003846 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003847
3848 /* perform board reset */
3849 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003850 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003851 phba->pport->fc_myDID = 0;
3852 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003853
Jamie Wellnitz41415862006-02-28 19:25:27 -05003854 /* Turn off parity checking and serr during the physical reset */
3855 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3856 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3857 (cfg_value &
3858 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3859
James Smart3772a992009-05-22 14:50:54 -04003860 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3861
Jamie Wellnitz41415862006-02-28 19:25:27 -05003862 /* Now toggle INITFF bit in the Host Control Register */
3863 writel(HC_INITFF, phba->HCregaddr);
3864 mdelay(1);
3865 readl(phba->HCregaddr); /* flush */
3866 writel(0, phba->HCregaddr);
3867 readl(phba->HCregaddr); /* flush */
3868
3869 /* Restore PCI cmd register */
3870 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003871
3872 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003873 for (i = 0; i < psli->num_rings; i++) {
3874 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003875 pring->flag = 0;
3876 pring->rspidx = 0;
3877 pring->next_cmdidx = 0;
3878 pring->local_getidx = 0;
3879 pring->cmdidx = 0;
3880 pring->missbufcnt = 0;
3881 }
dea31012005-04-17 16:05:31 -05003882
James Smart2e0fef82007-06-17 19:56:36 -05003883 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003884 return 0;
3885}
3886
James Smarte59058c2008-08-24 21:49:00 -04003887/**
James Smartda0436e2009-05-22 14:51:39 -04003888 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3889 * @phba: Pointer to HBA context object.
3890 *
3891 * This function resets a SLI4 HBA. This function disables PCI layer parity
3892 * checking during resets the device. The caller is not required to hold
3893 * any locks.
3894 *
3895 * This function returns 0 always.
3896 **/
3897int
3898lpfc_sli4_brdreset(struct lpfc_hba *phba)
3899{
3900 struct lpfc_sli *psli = &phba->sli;
3901 uint16_t cfg_value;
James Smartda0436e2009-05-22 14:51:39 -04003902
3903 /* Reset HBA */
3904 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3905 "0295 Reset HBA Data: x%x x%x\n",
3906 phba->pport->port_state, psli->sli_flag);
3907
3908 /* perform board reset */
3909 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003910 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003911 phba->pport->fc_myDID = 0;
3912 phba->pport->fc_prevDID = 0;
3913
James Smartda0436e2009-05-22 14:51:39 -04003914 spin_lock_irq(&phba->hbalock);
3915 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3916 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003917 spin_unlock_irq(&phba->hbalock);
3918
3919 /* Now physically reset the device */
3920 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3921 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003922
3923 /* Turn off parity checking and serr during the physical reset */
3924 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3925 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3926 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3927
James Smartda0436e2009-05-22 14:51:39 -04003928 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003929 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04003930 lpfc_pci_function_reset(phba);
3931
James Smartbe858b62010-12-15 17:57:20 -05003932 /* Restore PCI cmd register */
3933 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3934
James Smartda0436e2009-05-22 14:51:39 -04003935 return 0;
3936}
3937
3938/**
3939 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003940 * @phba: Pointer to HBA context object.
3941 *
3942 * This function is called in the SLI initialization code path to
3943 * restart the HBA. The caller is not required to hold any lock.
3944 * This function writes MBX_RESTART mailbox command to the SLIM and
3945 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3946 * function to free any pending commands. The function enables
3947 * POST only during the first initialization. The function returns zero.
3948 * The function does not guarantee completion of MBX_RESTART mailbox
3949 * command before the return of this function.
3950 **/
James Smartda0436e2009-05-22 14:51:39 -04003951static int
3952lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003953{
3954 MAILBOX_t *mb;
3955 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003956 volatile uint32_t word0;
3957 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003958 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003959
James Smart2e0fef82007-06-17 19:56:36 -05003960 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003961
James Smart0d878412009-10-02 15:16:56 -04003962 /* Take PCIe device Advanced Error Reporting (AER) state */
3963 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3964
Jamie Wellnitz41415862006-02-28 19:25:27 -05003965 psli = &phba->sli;
3966
3967 /* Restart HBA */
3968 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003969 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003970 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003971
3972 word0 = 0;
3973 mb = (MAILBOX_t *) &word0;
3974 mb->mbxCommand = MBX_RESTART;
3975 mb->mbxHc = 1;
3976
James Smart92908312006-03-07 15:04:13 -05003977 lpfc_reset_barrier(phba);
3978
Jamie Wellnitz41415862006-02-28 19:25:27 -05003979 to_slim = phba->MBslimaddr;
3980 writel(*(uint32_t *) mb, to_slim);
3981 readl(to_slim); /* flush */
3982
3983 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003984 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003985 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003986 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003987 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003988 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003989 writel(*(uint32_t *) mb, to_slim);
3990 readl(to_slim); /* flush */
3991
3992 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003993 phba->pport->stopped = 0;
3994 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003995 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003996 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003997
James Smart64ba8812006-08-02 15:24:34 -04003998 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3999 psli->stats_start = get_seconds();
4000
James Smarteaf15d52008-12-04 22:39:29 -05004001 /* Give the INITFF and Post time to settle. */
4002 mdelay(100);
dea31012005-04-17 16:05:31 -05004003
James Smart0d878412009-10-02 15:16:56 -04004004 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4005 if (hba_aer_enabled)
4006 pci_disable_pcie_error_reporting(phba->pcidev);
4007
Jamie Wellnitz41415862006-02-28 19:25:27 -05004008 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004009
4010 return 0;
4011}
4012
James Smarte59058c2008-08-24 21:49:00 -04004013/**
James Smartda0436e2009-05-22 14:51:39 -04004014 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4015 * @phba: Pointer to HBA context object.
4016 *
4017 * This function is called in the SLI initialization code path to restart
4018 * a SLI4 HBA. The caller is not required to hold any lock.
4019 * At the end of the function, it calls lpfc_hba_down_post function to
4020 * free any pending commands.
4021 **/
4022static int
4023lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4024{
4025 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004026 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04004027
4028 /* Restart HBA */
4029 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4030 "0296 Restart HBA Data: x%x x%x\n",
4031 phba->pport->port_state, psli->sli_flag);
4032
James Smart75baf692010-06-08 18:31:21 -04004033 /* Take PCIe device Advanced Error Reporting (AER) state */
4034 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4035
James Smartda0436e2009-05-22 14:51:39 -04004036 lpfc_sli4_brdreset(phba);
4037
4038 spin_lock_irq(&phba->hbalock);
4039 phba->pport->stopped = 0;
4040 phba->link_state = LPFC_INIT_START;
4041 phba->hba_flag = 0;
4042 spin_unlock_irq(&phba->hbalock);
4043
4044 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4045 psli->stats_start = get_seconds();
4046
James Smart75baf692010-06-08 18:31:21 -04004047 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4048 if (hba_aer_enabled)
4049 pci_disable_pcie_error_reporting(phba->pcidev);
4050
James Smartda0436e2009-05-22 14:51:39 -04004051 lpfc_hba_down_post(phba);
4052
4053 return 0;
4054}
4055
4056/**
4057 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4058 * @phba: Pointer to HBA context object.
4059 *
4060 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4061 * API jump table function pointer from the lpfc_hba struct.
4062**/
4063int
4064lpfc_sli_brdrestart(struct lpfc_hba *phba)
4065{
4066 return phba->lpfc_sli_brdrestart(phba);
4067}
4068
4069/**
James Smart3621a712009-04-06 18:47:14 -04004070 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004071 * @phba: Pointer to HBA context object.
4072 *
4073 * This function is called after a HBA restart to wait for successful
4074 * restart of the HBA. Successful restart of the HBA is indicated by
4075 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4076 * iteration, the function will restart the HBA again. The function returns
4077 * zero if HBA successfully restarted else returns negative error code.
4078 **/
dea31012005-04-17 16:05:31 -05004079static int
4080lpfc_sli_chipset_init(struct lpfc_hba *phba)
4081{
4082 uint32_t status, i = 0;
4083
4084 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004085 if (lpfc_readl(phba->HSregaddr, &status))
4086 return -EIO;
dea31012005-04-17 16:05:31 -05004087
4088 /* Check status register to see what current state is */
4089 i = 0;
4090 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4091
James Smartdcf2a4e2010-09-29 11:18:53 -04004092 /* Check every 10ms for 10 retries, then every 100ms for 90
4093 * retries, then every 1 sec for 50 retires for a total of
4094 * ~60 seconds before reset the board again and check every
4095 * 1 sec for 50 retries. The up to 60 seconds before the
4096 * board ready is required by the Falcon FIPS zeroization
4097 * complete, and any reset the board in between shall cause
4098 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004099 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004100 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004101 /* Adapter failed to init, timeout, status reg
4102 <status> */
James Smarted957682007-06-17 19:56:37 -05004103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004104 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004105 "timeout, status reg x%x, "
4106 "FW Data: A8 x%x AC x%x\n", status,
4107 readl(phba->MBslimaddr + 0xa8),
4108 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004109 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004110 return -ETIMEDOUT;
4111 }
4112
4113 /* Check to see if any errors occurred during init */
4114 if (status & HS_FFERM) {
4115 /* ERROR: During chipset initialization */
4116 /* Adapter failed to init, chipset, status reg
4117 <status> */
James Smarted957682007-06-17 19:56:37 -05004118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004119 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004120 "chipset, status reg x%x, "
4121 "FW Data: A8 x%x AC x%x\n", status,
4122 readl(phba->MBslimaddr + 0xa8),
4123 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004124 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004125 return -EIO;
4126 }
4127
James Smartdcf2a4e2010-09-29 11:18:53 -04004128 if (i <= 10)
dea31012005-04-17 16:05:31 -05004129 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004130 else if (i <= 100)
4131 msleep(100);
4132 else
4133 msleep(1000);
dea31012005-04-17 16:05:31 -05004134
James Smartdcf2a4e2010-09-29 11:18:53 -04004135 if (i == 150) {
4136 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004137 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004138 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004139 }
4140 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004141 if (lpfc_readl(phba->HSregaddr, &status))
4142 return -EIO;
dea31012005-04-17 16:05:31 -05004143 }
4144
4145 /* Check to see if any errors occurred during init */
4146 if (status & HS_FFERM) {
4147 /* ERROR: During chipset initialization */
4148 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004150 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004151 "status reg x%x, "
4152 "FW Data: A8 x%x AC x%x\n", status,
4153 readl(phba->MBslimaddr + 0xa8),
4154 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004155 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004156 return -EIO;
4157 }
4158
4159 /* Clear all interrupt enable conditions */
4160 writel(0, phba->HCregaddr);
4161 readl(phba->HCregaddr); /* flush */
4162
4163 /* setup host attn register */
4164 writel(0xffffffff, phba->HAregaddr);
4165 readl(phba->HAregaddr); /* flush */
4166 return 0;
4167}
4168
James Smarte59058c2008-08-24 21:49:00 -04004169/**
James Smart3621a712009-04-06 18:47:14 -04004170 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004171 *
4172 * This function calculates and returns the number of HBQs required to be
4173 * configured.
4174 **/
James Smart78b2d852007-08-02 11:10:21 -04004175int
James Smarted957682007-06-17 19:56:37 -05004176lpfc_sli_hbq_count(void)
4177{
James Smart92d7f7b2007-06-17 19:56:38 -05004178 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004179}
4180
James Smarte59058c2008-08-24 21:49:00 -04004181/**
James Smart3621a712009-04-06 18:47:14 -04004182 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004183 *
4184 * This function adds the number of hbq entries in every HBQ to get
4185 * the total number of hbq entries required for the HBA and returns
4186 * the total count.
4187 **/
James Smarted957682007-06-17 19:56:37 -05004188static int
4189lpfc_sli_hbq_entry_count(void)
4190{
4191 int hbq_count = lpfc_sli_hbq_count();
4192 int count = 0;
4193 int i;
4194
4195 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004196 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004197 return count;
4198}
4199
James Smarte59058c2008-08-24 21:49:00 -04004200/**
James Smart3621a712009-04-06 18:47:14 -04004201 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004202 *
4203 * This function calculates amount of memory required for all hbq entries
4204 * to be configured and returns the total memory required.
4205 **/
dea31012005-04-17 16:05:31 -05004206int
James Smarted957682007-06-17 19:56:37 -05004207lpfc_sli_hbq_size(void)
4208{
4209 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4210}
4211
James Smarte59058c2008-08-24 21:49:00 -04004212/**
James Smart3621a712009-04-06 18:47:14 -04004213 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004214 * @phba: Pointer to HBA context object.
4215 *
4216 * This function is called during the SLI initialization to configure
4217 * all the HBQs and post buffers to the HBQ. The caller is not
4218 * required to hold any locks. This function will return zero if successful
4219 * else it will return negative error code.
4220 **/
James Smarted957682007-06-17 19:56:37 -05004221static int
4222lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4223{
4224 int hbq_count = lpfc_sli_hbq_count();
4225 LPFC_MBOXQ_t *pmb;
4226 MAILBOX_t *pmbox;
4227 uint32_t hbqno;
4228 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004229
James Smart92d7f7b2007-06-17 19:56:38 -05004230 /* Get a Mailbox buffer to setup mailbox
4231 * commands for HBA initialization
4232 */
James Smarted957682007-06-17 19:56:37 -05004233 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4234
4235 if (!pmb)
4236 return -ENOMEM;
4237
James Smart04c68492009-05-22 14:52:52 -04004238 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004239
4240 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4241 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004242 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004243
4244 hbq_entry_index = 0;
4245 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4246 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4247 phba->hbqs[hbqno].hbqPutIdx = 0;
4248 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4249 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004250 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004251 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4252 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004253 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4254
4255 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4256 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4257 mbxStatus <status>, ring <num> */
4258
4259 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004260 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004261 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004262 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004263 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004264 pmbox->mbxStatus, hbqno);
4265
4266 phba->link_state = LPFC_HBA_ERROR;
4267 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004268 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004269 }
4270 }
4271 phba->hbq_count = hbq_count;
4272
James Smarted957682007-06-17 19:56:37 -05004273 mempool_free(pmb, phba->mbox_mem_pool);
4274
James Smart92d7f7b2007-06-17 19:56:38 -05004275 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004276 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4277 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004278 return 0;
4279}
4280
James Smarte59058c2008-08-24 21:49:00 -04004281/**
James Smart4f774512009-05-22 14:52:35 -04004282 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4283 * @phba: Pointer to HBA context object.
4284 *
4285 * This function is called during the SLI initialization to configure
4286 * all the HBQs and post buffers to the HBQ. The caller is not
4287 * required to hold any locks. This function will return zero if successful
4288 * else it will return negative error code.
4289 **/
4290static int
4291lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4292{
4293 phba->hbq_in_use = 1;
4294 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4295 phba->hbq_count = 1;
4296 /* Initially populate or replenish the HBQs */
4297 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4298 return 0;
4299}
4300
4301/**
James Smart3621a712009-04-06 18:47:14 -04004302 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004303 * @phba: Pointer to HBA context object.
4304 * @sli_mode: sli mode - 2/3
4305 *
4306 * This function is called by the sli intialization code path
4307 * to issue config_port mailbox command. This function restarts the
4308 * HBA firmware and issues a config_port mailbox command to configure
4309 * the SLI interface in the sli mode specified by sli_mode
4310 * variable. The caller is not required to hold any locks.
4311 * The function returns 0 if successful, else returns negative error
4312 * code.
4313 **/
James Smart93996272008-08-24 21:50:30 -04004314int
4315lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004316{
4317 LPFC_MBOXQ_t *pmb;
4318 uint32_t resetcount = 0, rc = 0, done = 0;
4319
4320 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4321 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004322 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004323 return -ENOMEM;
4324 }
4325
James Smarted957682007-06-17 19:56:37 -05004326 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004327 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004328 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004329 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004330 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004331 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004332 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004333 rc = lpfc_sli_chipset_init(phba);
4334 if (rc)
4335 break;
4336
James Smart2e0fef82007-06-17 19:56:36 -05004337 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004338 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004339 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004340 resetcount++;
4341
James Smarted957682007-06-17 19:56:37 -05004342 /* Call pre CONFIG_PORT mailbox command initialization. A
4343 * value of 0 means the call was successful. Any other
4344 * nonzero value is a failure, but if ERESTART is returned,
4345 * the driver may reset the HBA and try again.
4346 */
dea31012005-04-17 16:05:31 -05004347 rc = lpfc_config_port_prep(phba);
4348 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004349 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004350 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004351 } else if (rc)
dea31012005-04-17 16:05:31 -05004352 break;
James Smart6d368e52011-05-24 11:44:12 -04004353
James Smart2e0fef82007-06-17 19:56:36 -05004354 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004355 lpfc_config_port(phba, pmb);
4356 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004357 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4358 LPFC_SLI3_HBQ_ENABLED |
4359 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004360 LPFC_SLI3_BG_ENABLED |
4361 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004362 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004364 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004365 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004366 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004367 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004368 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004369 spin_unlock_irq(&phba->hbalock);
4370 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004371 } else {
4372 /* Allow asynchronous mailbox command to go through */
4373 spin_lock_irq(&phba->hbalock);
4374 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4375 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004376 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004377
4378 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4379 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4380 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4381 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004382 }
dea31012005-04-17 16:05:31 -05004383 }
James Smarted957682007-06-17 19:56:37 -05004384 if (!done) {
4385 rc = -EINVAL;
4386 goto do_prep_failed;
4387 }
James Smart04c68492009-05-22 14:52:52 -04004388 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4389 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004390 rc = -ENXIO;
4391 goto do_prep_failed;
4392 }
James Smart04c68492009-05-22 14:52:52 -04004393 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004394 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004395 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4396 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4397 phba->max_vpi : phba->max_vports;
4398
James Smart34b02dc2008-08-24 21:49:55 -04004399 } else
4400 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004401 phba->fips_level = 0;
4402 phba->fips_spec_rev = 0;
4403 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004404 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004405 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4406 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4407 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4408 "2850 Security Crypto Active. FIPS x%d "
4409 "(Spec Rev: x%d)",
4410 phba->fips_level, phba->fips_spec_rev);
4411 }
4412 if (pmb->u.mb.un.varCfgPort.sec_err) {
4413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4414 "2856 Config Port Security Crypto "
4415 "Error: x%x ",
4416 pmb->u.mb.un.varCfgPort.sec_err);
4417 }
James Smart04c68492009-05-22 14:52:52 -04004418 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004419 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004420 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004421 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004422
4423 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4424 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004425
4426 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004427 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004428 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4429 else
4430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4431 "0443 Adapter did not grant "
4432 "BlockGuard\n");
4433 }
James Smart34b02dc2008-08-24 21:49:55 -04004434 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004435 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004436 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004437 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004438 }
James Smart92d7f7b2007-06-17 19:56:38 -05004439do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004440 mempool_free(pmb, phba->mbox_mem_pool);
4441 return rc;
4442}
4443
James Smarte59058c2008-08-24 21:49:00 -04004444
4445/**
James Smart3621a712009-04-06 18:47:14 -04004446 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004447 * @phba: Pointer to HBA context object.
4448 *
4449 * This function is the main SLI intialization function. This function
4450 * is called by the HBA intialization code, HBA reset code and HBA
4451 * error attention handler code. Caller is not required to hold any
4452 * locks. This function issues config_port mailbox command to configure
4453 * the SLI, setup iocb rings and HBQ rings. In the end the function
4454 * calls the config_port_post function to issue init_link mailbox
4455 * command and to start the discovery. The function will return zero
4456 * if successful, else it will return negative error code.
4457 **/
James Smarted957682007-06-17 19:56:37 -05004458int
4459lpfc_sli_hba_setup(struct lpfc_hba *phba)
4460{
4461 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004462 int mode = 3, i;
4463 int longs;
James Smarted957682007-06-17 19:56:37 -05004464
4465 switch (lpfc_sli_mode) {
4466 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004467 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004469 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004470 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004471 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004472 break;
4473 }
James Smarted957682007-06-17 19:56:37 -05004474 mode = 2;
4475 break;
4476 case 0:
4477 case 3:
4478 break;
4479 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004481 "1819 Unrecognized lpfc_sli_mode "
4482 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004483
4484 break;
4485 }
4486
James Smart93996272008-08-24 21:50:30 -04004487 rc = lpfc_sli_config_port(phba, mode);
4488
James Smarted957682007-06-17 19:56:37 -05004489 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004491 "1820 Unable to select SLI-3. "
4492 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004493 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004494 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004495 if (rc)
dea31012005-04-17 16:05:31 -05004496 goto lpfc_sli_hba_setup_error;
4497
James Smart0d878412009-10-02 15:16:56 -04004498 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4499 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4500 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4501 if (!rc) {
4502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4503 "2709 This device supports "
4504 "Advanced Error Reporting (AER)\n");
4505 spin_lock_irq(&phba->hbalock);
4506 phba->hba_flag |= HBA_AER_ENABLED;
4507 spin_unlock_irq(&phba->hbalock);
4508 } else {
4509 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4510 "2708 This device does not support "
4511 "Advanced Error Reporting (AER)\n");
4512 phba->cfg_aer_support = 0;
4513 }
4514 }
4515
James Smarted957682007-06-17 19:56:37 -05004516 if (phba->sli_rev == 3) {
4517 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4518 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004519 } else {
4520 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4521 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004522 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004523 }
4524
4525 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004526 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4527 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004528 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004529
4530 if (rc)
4531 goto lpfc_sli_hba_setup_error;
4532
James Smart6d368e52011-05-24 11:44:12 -04004533 /* Initialize VPIs. */
4534 if (phba->sli_rev == LPFC_SLI_REV3) {
4535 /*
4536 * The VPI bitmask and physical ID array are allocated
4537 * and initialized once only - at driver load. A port
4538 * reset doesn't need to reinitialize this memory.
4539 */
4540 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4541 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4542 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4543 GFP_KERNEL);
4544 if (!phba->vpi_bmask) {
4545 rc = -ENOMEM;
4546 goto lpfc_sli_hba_setup_error;
4547 }
4548
4549 phba->vpi_ids = kzalloc(
4550 (phba->max_vpi+1) * sizeof(uint16_t),
4551 GFP_KERNEL);
4552 if (!phba->vpi_ids) {
4553 kfree(phba->vpi_bmask);
4554 rc = -ENOMEM;
4555 goto lpfc_sli_hba_setup_error;
4556 }
4557 for (i = 0; i < phba->max_vpi; i++)
4558 phba->vpi_ids[i] = i;
4559 }
4560 }
4561
James Smart93996272008-08-24 21:50:30 -04004562 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004563 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4564 rc = lpfc_sli_hbq_setup(phba);
4565 if (rc)
4566 goto lpfc_sli_hba_setup_error;
4567 }
James Smart04c68492009-05-22 14:52:52 -04004568 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004569 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004570 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004571
4572 rc = lpfc_config_port_post(phba);
4573 if (rc)
4574 goto lpfc_sli_hba_setup_error;
4575
James Smarted957682007-06-17 19:56:37 -05004576 return rc;
4577
James Smart92d7f7b2007-06-17 19:56:38 -05004578lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004579 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004581 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004582 return rc;
4583}
4584
James Smartda0436e2009-05-22 14:51:39 -04004585/**
4586 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4587 * @phba: Pointer to HBA context object.
4588 * @mboxq: mailbox pointer.
4589 * This function issue a dump mailbox command to read config region
4590 * 23 and parse the records in the region and populate driver
4591 * data structure.
4592 **/
4593static int
James Smartff78d8f2011-12-13 13:21:35 -05004594lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004595{
James Smartff78d8f2011-12-13 13:21:35 -05004596 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004597 struct lpfc_dmabuf *mp;
4598 struct lpfc_mqe *mqe;
4599 uint32_t data_length;
4600 int rc;
4601
4602 /* Program the default value of vlan_id and fc_map */
4603 phba->valid_vlan = 0;
4604 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4605 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4606 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4607
James Smartff78d8f2011-12-13 13:21:35 -05004608 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4609 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004610 return -ENOMEM;
4611
James Smartff78d8f2011-12-13 13:21:35 -05004612 mqe = &mboxq->u.mqe;
4613 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4614 rc = -ENOMEM;
4615 goto out_free_mboxq;
4616 }
4617
James Smartda0436e2009-05-22 14:51:39 -04004618 mp = (struct lpfc_dmabuf *) mboxq->context1;
4619 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4620
4621 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4622 "(%d):2571 Mailbox cmd x%x Status x%x "
4623 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4624 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4625 "CQ: x%x x%x x%x x%x\n",
4626 mboxq->vport ? mboxq->vport->vpi : 0,
4627 bf_get(lpfc_mqe_command, mqe),
4628 bf_get(lpfc_mqe_status, mqe),
4629 mqe->un.mb_words[0], mqe->un.mb_words[1],
4630 mqe->un.mb_words[2], mqe->un.mb_words[3],
4631 mqe->un.mb_words[4], mqe->un.mb_words[5],
4632 mqe->un.mb_words[6], mqe->un.mb_words[7],
4633 mqe->un.mb_words[8], mqe->un.mb_words[9],
4634 mqe->un.mb_words[10], mqe->un.mb_words[11],
4635 mqe->un.mb_words[12], mqe->un.mb_words[13],
4636 mqe->un.mb_words[14], mqe->un.mb_words[15],
4637 mqe->un.mb_words[16], mqe->un.mb_words[50],
4638 mboxq->mcqe.word0,
4639 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4640 mboxq->mcqe.trailer);
4641
4642 if (rc) {
4643 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4644 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004645 rc = -EIO;
4646 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004647 }
4648 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004649 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004650 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4651 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004652 rc = -EIO;
4653 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004654 }
James Smartda0436e2009-05-22 14:51:39 -04004655
4656 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4657 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4658 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004659 rc = 0;
4660
4661out_free_mboxq:
4662 mempool_free(mboxq, phba->mbox_mem_pool);
4663 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004664}
4665
4666/**
4667 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4668 * @phba: pointer to lpfc hba data structure.
4669 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4670 * @vpd: pointer to the memory to hold resulting port vpd data.
4671 * @vpd_size: On input, the number of bytes allocated to @vpd.
4672 * On output, the number of data bytes in @vpd.
4673 *
4674 * This routine executes a READ_REV SLI4 mailbox command. In
4675 * addition, this routine gets the port vpd data.
4676 *
4677 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004678 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004679 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004680 **/
4681static int
4682lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4683 uint8_t *vpd, uint32_t *vpd_size)
4684{
4685 int rc = 0;
4686 uint32_t dma_size;
4687 struct lpfc_dmabuf *dmabuf;
4688 struct lpfc_mqe *mqe;
4689
4690 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4691 if (!dmabuf)
4692 return -ENOMEM;
4693
4694 /*
4695 * Get a DMA buffer for the vpd data resulting from the READ_REV
4696 * mailbox command.
4697 */
4698 dma_size = *vpd_size;
4699 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4700 dma_size,
4701 &dmabuf->phys,
4702 GFP_KERNEL);
4703 if (!dmabuf->virt) {
4704 kfree(dmabuf);
4705 return -ENOMEM;
4706 }
4707 memset(dmabuf->virt, 0, dma_size);
4708
4709 /*
4710 * The SLI4 implementation of READ_REV conflicts at word1,
4711 * bits 31:16 and SLI4 adds vpd functionality not present
4712 * in SLI3. This code corrects the conflicts.
4713 */
4714 lpfc_read_rev(phba, mboxq);
4715 mqe = &mboxq->u.mqe;
4716 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4717 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4718 mqe->un.read_rev.word1 &= 0x0000FFFF;
4719 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4720 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4721
4722 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4723 if (rc) {
4724 dma_free_coherent(&phba->pcidev->dev, dma_size,
4725 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004726 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004727 return -EIO;
4728 }
4729
James Smartda0436e2009-05-22 14:51:39 -04004730 /*
4731 * The available vpd length cannot be bigger than the
4732 * DMA buffer passed to the port. Catch the less than
4733 * case and update the caller's size.
4734 */
4735 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4736 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4737
James Smartd7c47992010-06-08 18:31:54 -04004738 memcpy(vpd, dmabuf->virt, *vpd_size);
4739
James Smartda0436e2009-05-22 14:51:39 -04004740 dma_free_coherent(&phba->pcidev->dev, dma_size,
4741 dmabuf->virt, dmabuf->phys);
4742 kfree(dmabuf);
4743 return 0;
4744}
4745
4746/**
James Smartcd1c8302011-10-10 21:33:25 -04004747 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4748 * @phba: pointer to lpfc hba data structure.
4749 *
4750 * This routine retrieves SLI4 device physical port name this PCI function
4751 * is attached to.
4752 *
4753 * Return codes
4754 * 0 - sucessful
4755 * otherwise - failed to retrieve physical port name
4756 **/
4757static int
4758lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4759{
4760 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004761 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4762 struct lpfc_controller_attribute *cntl_attr;
4763 struct lpfc_mbx_get_port_name *get_port_name;
4764 void *virtaddr = NULL;
4765 uint32_t alloclen, reqlen;
4766 uint32_t shdr_status, shdr_add_status;
4767 union lpfc_sli4_cfg_shdr *shdr;
4768 char cport_name = 0;
4769 int rc;
4770
4771 /* We assume nothing at this point */
4772 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4773 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4774
4775 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4776 if (!mboxq)
4777 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004778 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004779 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4780 lpfc_sli4_read_config(phba);
4781 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4782 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004783
4784 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4785 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4786 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4787 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4788 LPFC_SLI4_MBX_NEMBED);
4789 if (alloclen < reqlen) {
4790 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4791 "3084 Allocated DMA memory size (%d) is "
4792 "less than the requested DMA memory size "
4793 "(%d)\n", alloclen, reqlen);
4794 rc = -ENOMEM;
4795 goto out_free_mboxq;
4796 }
4797 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4798 virtaddr = mboxq->sge_array->addr[0];
4799 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4800 shdr = &mbx_cntl_attr->cfg_shdr;
4801 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4802 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4803 if (shdr_status || shdr_add_status || rc) {
4804 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4805 "3085 Mailbox x%x (x%x/x%x) failed, "
4806 "rc:x%x, status:x%x, add_status:x%x\n",
4807 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4808 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4809 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4810 rc, shdr_status, shdr_add_status);
4811 rc = -ENXIO;
4812 goto out_free_mboxq;
4813 }
4814 cntl_attr = &mbx_cntl_attr->cntl_attr;
4815 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4816 phba->sli4_hba.lnk_info.lnk_tp =
4817 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4818 phba->sli4_hba.lnk_info.lnk_no =
4819 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4820 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4821 "3086 lnk_type:%d, lnk_numb:%d\n",
4822 phba->sli4_hba.lnk_info.lnk_tp,
4823 phba->sli4_hba.lnk_info.lnk_no);
4824
4825retrieve_ppname:
4826 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4827 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4828 sizeof(struct lpfc_mbx_get_port_name) -
4829 sizeof(struct lpfc_sli4_cfg_mhdr),
4830 LPFC_SLI4_MBX_EMBED);
4831 get_port_name = &mboxq->u.mqe.un.get_port_name;
4832 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4833 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4834 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4835 phba->sli4_hba.lnk_info.lnk_tp);
4836 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4837 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4838 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4839 if (shdr_status || shdr_add_status || rc) {
4840 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4841 "3087 Mailbox x%x (x%x/x%x) failed: "
4842 "rc:x%x, status:x%x, add_status:x%x\n",
4843 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4844 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4845 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4846 rc, shdr_status, shdr_add_status);
4847 rc = -ENXIO;
4848 goto out_free_mboxq;
4849 }
4850 switch (phba->sli4_hba.lnk_info.lnk_no) {
4851 case LPFC_LINK_NUMBER_0:
4852 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4853 &get_port_name->u.response);
4854 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4855 break;
4856 case LPFC_LINK_NUMBER_1:
4857 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4858 &get_port_name->u.response);
4859 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4860 break;
4861 case LPFC_LINK_NUMBER_2:
4862 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4863 &get_port_name->u.response);
4864 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4865 break;
4866 case LPFC_LINK_NUMBER_3:
4867 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4868 &get_port_name->u.response);
4869 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4870 break;
4871 default:
4872 break;
4873 }
4874
4875 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4876 phba->Port[0] = cport_name;
4877 phba->Port[1] = '\0';
4878 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4879 "3091 SLI get port name: %s\n", phba->Port);
4880 }
4881
4882out_free_mboxq:
4883 if (rc != MBX_TIMEOUT) {
4884 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4885 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4886 else
4887 mempool_free(mboxq, phba->mbox_mem_pool);
4888 }
4889 return rc;
4890}
4891
4892/**
James Smartda0436e2009-05-22 14:51:39 -04004893 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4894 * @phba: pointer to lpfc hba data structure.
4895 *
4896 * This routine is called to explicitly arm the SLI4 device's completion and
4897 * event queues
4898 **/
4899static void
4900lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4901{
4902 uint8_t fcp_eqidx;
4903
4904 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4905 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004906 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004907 if (phba->sli4_hba.fcp_cq) {
4908 do
4909 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4910 LPFC_QUEUE_REARM);
4911 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4912 }
James Smartda0436e2009-05-22 14:51:39 -04004913 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
James Smart2e90f4b2011-12-13 13:22:37 -05004914 if (phba->sli4_hba.fp_eq) {
4915 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
4916 fcp_eqidx++)
4917 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4918 LPFC_QUEUE_REARM);
4919 }
James Smartda0436e2009-05-22 14:51:39 -04004920}
4921
4922/**
James Smart6d368e52011-05-24 11:44:12 -04004923 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4924 * @phba: Pointer to HBA context object.
4925 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004926 * @extnt_count: buffer to hold port available extent count.
4927 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004928 *
James Smartb76f2dc2011-07-22 18:37:42 -04004929 * This function calls the port and retrievs the number of available
4930 * extents and their size for a particular extent type.
4931 *
4932 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004933 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004934int
James Smart6d368e52011-05-24 11:44:12 -04004935lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4936 uint16_t *extnt_count, uint16_t *extnt_size)
4937{
4938 int rc = 0;
4939 uint32_t length;
4940 uint32_t mbox_tmo;
4941 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4942 LPFC_MBOXQ_t *mbox;
4943
4944 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4945 if (!mbox)
4946 return -ENOMEM;
4947
4948 /* Find out how many extents are available for this resource type */
4949 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4950 sizeof(struct lpfc_sli4_cfg_mhdr));
4951 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4952 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4953 length, LPFC_SLI4_MBX_EMBED);
4954
4955 /* Send an extents count of 0 - the GET doesn't use it. */
4956 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4957 LPFC_SLI4_MBX_EMBED);
4958 if (unlikely(rc)) {
4959 rc = -EIO;
4960 goto err_exit;
4961 }
4962
4963 if (!phba->sli4_hba.intr_enable)
4964 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4965 else {
James Smarta183a152011-10-10 21:32:43 -04004966 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004967 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4968 }
4969 if (unlikely(rc)) {
4970 rc = -EIO;
4971 goto err_exit;
4972 }
4973
4974 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4975 if (bf_get(lpfc_mbox_hdr_status,
4976 &rsrc_info->header.cfg_shdr.response)) {
4977 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4978 "2930 Failed to get resource extents "
4979 "Status 0x%x Add'l Status 0x%x\n",
4980 bf_get(lpfc_mbox_hdr_status,
4981 &rsrc_info->header.cfg_shdr.response),
4982 bf_get(lpfc_mbox_hdr_add_status,
4983 &rsrc_info->header.cfg_shdr.response));
4984 rc = -EIO;
4985 goto err_exit;
4986 }
4987
4988 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4989 &rsrc_info->u.rsp);
4990 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4991 &rsrc_info->u.rsp);
4992 err_exit:
4993 mempool_free(mbox, phba->mbox_mem_pool);
4994 return rc;
4995}
4996
4997/**
4998 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4999 * @phba: Pointer to HBA context object.
5000 * @type: The extent type to check.
5001 *
5002 * This function reads the current available extents from the port and checks
5003 * if the extent count or extent size has changed since the last access.
5004 * Callers use this routine post port reset to understand if there is a
5005 * extent reprovisioning requirement.
5006 *
5007 * Returns:
5008 * -Error: error indicates problem.
5009 * 1: Extent count or size has changed.
5010 * 0: No changes.
5011 **/
5012static int
5013lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5014{
5015 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5016 uint16_t size_diff, rsrc_ext_size;
5017 int rc = 0;
5018 struct lpfc_rsrc_blks *rsrc_entry;
5019 struct list_head *rsrc_blk_list = NULL;
5020
5021 size_diff = 0;
5022 curr_ext_cnt = 0;
5023 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5024 &rsrc_ext_cnt,
5025 &rsrc_ext_size);
5026 if (unlikely(rc))
5027 return -EIO;
5028
5029 switch (type) {
5030 case LPFC_RSC_TYPE_FCOE_RPI:
5031 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5032 break;
5033 case LPFC_RSC_TYPE_FCOE_VPI:
5034 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5035 break;
5036 case LPFC_RSC_TYPE_FCOE_XRI:
5037 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5038 break;
5039 case LPFC_RSC_TYPE_FCOE_VFI:
5040 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5041 break;
5042 default:
5043 break;
5044 }
5045
5046 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5047 curr_ext_cnt++;
5048 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5049 size_diff++;
5050 }
5051
5052 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5053 rc = 1;
5054
5055 return rc;
5056}
5057
5058/**
5059 * lpfc_sli4_cfg_post_extnts -
5060 * @phba: Pointer to HBA context object.
5061 * @extnt_cnt - number of available extents.
5062 * @type - the extent type (rpi, xri, vfi, vpi).
5063 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5064 * @mbox - pointer to the caller's allocated mailbox structure.
5065 *
5066 * This function executes the extents allocation request. It also
5067 * takes care of the amount of memory needed to allocate or get the
5068 * allocated extents. It is the caller's responsibility to evaluate
5069 * the response.
5070 *
5071 * Returns:
5072 * -Error: Error value describes the condition found.
5073 * 0: if successful
5074 **/
5075static int
5076lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
5077 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5078{
5079 int rc = 0;
5080 uint32_t req_len;
5081 uint32_t emb_len;
5082 uint32_t alloc_len, mbox_tmo;
5083
5084 /* Calculate the total requested length of the dma memory */
5085 req_len = *extnt_cnt * sizeof(uint16_t);
5086
5087 /*
5088 * Calculate the size of an embedded mailbox. The uint32_t
5089 * accounts for extents-specific word.
5090 */
5091 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5092 sizeof(uint32_t);
5093
5094 /*
5095 * Presume the allocation and response will fit into an embedded
5096 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5097 */
5098 *emb = LPFC_SLI4_MBX_EMBED;
5099 if (req_len > emb_len) {
5100 req_len = *extnt_cnt * sizeof(uint16_t) +
5101 sizeof(union lpfc_sli4_cfg_shdr) +
5102 sizeof(uint32_t);
5103 *emb = LPFC_SLI4_MBX_NEMBED;
5104 }
5105
5106 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5107 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5108 req_len, *emb);
5109 if (alloc_len < req_len) {
5110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005111 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005112 "less than the requested DMA memory "
5113 "size (x%x)\n", alloc_len, req_len);
5114 return -ENOMEM;
5115 }
5116 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
5117 if (unlikely(rc))
5118 return -EIO;
5119
5120 if (!phba->sli4_hba.intr_enable)
5121 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5122 else {
James Smarta183a152011-10-10 21:32:43 -04005123 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005124 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5125 }
5126
5127 if (unlikely(rc))
5128 rc = -EIO;
5129 return rc;
5130}
5131
5132/**
5133 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5134 * @phba: Pointer to HBA context object.
5135 * @type: The resource extent type to allocate.
5136 *
5137 * This function allocates the number of elements for the specified
5138 * resource type.
5139 **/
5140static int
5141lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5142{
5143 bool emb = false;
5144 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5145 uint16_t rsrc_id, rsrc_start, j, k;
5146 uint16_t *ids;
5147 int i, rc;
5148 unsigned long longs;
5149 unsigned long *bmask;
5150 struct lpfc_rsrc_blks *rsrc_blks;
5151 LPFC_MBOXQ_t *mbox;
5152 uint32_t length;
5153 struct lpfc_id_range *id_array = NULL;
5154 void *virtaddr = NULL;
5155 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5156 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5157 struct list_head *ext_blk_list;
5158
5159 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5160 &rsrc_cnt,
5161 &rsrc_size);
5162 if (unlikely(rc))
5163 return -EIO;
5164
5165 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5166 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5167 "3009 No available Resource Extents "
5168 "for resource type 0x%x: Count: 0x%x, "
5169 "Size 0x%x\n", type, rsrc_cnt,
5170 rsrc_size);
5171 return -ENOMEM;
5172 }
5173
5174 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
5175 "2903 Available Resource Extents "
5176 "for resource type 0x%x: Count: 0x%x, "
5177 "Size 0x%x\n", type, rsrc_cnt,
5178 rsrc_size);
5179
5180 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5181 if (!mbox)
5182 return -ENOMEM;
5183
5184 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
5185 if (unlikely(rc)) {
5186 rc = -EIO;
5187 goto err_exit;
5188 }
5189
5190 /*
5191 * Figure out where the response is located. Then get local pointers
5192 * to the response data. The port does not guarantee to respond to
5193 * all extents counts request so update the local variable with the
5194 * allocated count from the port.
5195 */
5196 if (emb == LPFC_SLI4_MBX_EMBED) {
5197 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5198 id_array = &rsrc_ext->u.rsp.id[0];
5199 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5200 } else {
5201 virtaddr = mbox->sge_array->addr[0];
5202 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5203 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5204 id_array = &n_rsrc->id;
5205 }
5206
5207 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5208 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5209
5210 /*
5211 * Based on the resource size and count, correct the base and max
5212 * resource values.
5213 */
5214 length = sizeof(struct lpfc_rsrc_blks);
5215 switch (type) {
5216 case LPFC_RSC_TYPE_FCOE_RPI:
5217 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5218 sizeof(unsigned long),
5219 GFP_KERNEL);
5220 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5221 rc = -ENOMEM;
5222 goto err_exit;
5223 }
5224 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5225 sizeof(uint16_t),
5226 GFP_KERNEL);
5227 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5228 kfree(phba->sli4_hba.rpi_bmask);
5229 rc = -ENOMEM;
5230 goto err_exit;
5231 }
5232
5233 /*
5234 * The next_rpi was initialized with the maximum available
5235 * count but the port may allocate a smaller number. Catch
5236 * that case and update the next_rpi.
5237 */
5238 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5239
5240 /* Initialize local ptrs for common extent processing later. */
5241 bmask = phba->sli4_hba.rpi_bmask;
5242 ids = phba->sli4_hba.rpi_ids;
5243 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5244 break;
5245 case LPFC_RSC_TYPE_FCOE_VPI:
5246 phba->vpi_bmask = kzalloc(longs *
5247 sizeof(unsigned long),
5248 GFP_KERNEL);
5249 if (unlikely(!phba->vpi_bmask)) {
5250 rc = -ENOMEM;
5251 goto err_exit;
5252 }
5253 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5254 sizeof(uint16_t),
5255 GFP_KERNEL);
5256 if (unlikely(!phba->vpi_ids)) {
5257 kfree(phba->vpi_bmask);
5258 rc = -ENOMEM;
5259 goto err_exit;
5260 }
5261
5262 /* Initialize local ptrs for common extent processing later. */
5263 bmask = phba->vpi_bmask;
5264 ids = phba->vpi_ids;
5265 ext_blk_list = &phba->lpfc_vpi_blk_list;
5266 break;
5267 case LPFC_RSC_TYPE_FCOE_XRI:
5268 phba->sli4_hba.xri_bmask = kzalloc(longs *
5269 sizeof(unsigned long),
5270 GFP_KERNEL);
5271 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5272 rc = -ENOMEM;
5273 goto err_exit;
5274 }
5275 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5276 sizeof(uint16_t),
5277 GFP_KERNEL);
5278 if (unlikely(!phba->sli4_hba.xri_ids)) {
5279 kfree(phba->sli4_hba.xri_bmask);
5280 rc = -ENOMEM;
5281 goto err_exit;
5282 }
5283
5284 /* Initialize local ptrs for common extent processing later. */
5285 bmask = phba->sli4_hba.xri_bmask;
5286 ids = phba->sli4_hba.xri_ids;
5287 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5288 break;
5289 case LPFC_RSC_TYPE_FCOE_VFI:
5290 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5291 sizeof(unsigned long),
5292 GFP_KERNEL);
5293 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5294 rc = -ENOMEM;
5295 goto err_exit;
5296 }
5297 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5298 sizeof(uint16_t),
5299 GFP_KERNEL);
5300 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5301 kfree(phba->sli4_hba.vfi_bmask);
5302 rc = -ENOMEM;
5303 goto err_exit;
5304 }
5305
5306 /* Initialize local ptrs for common extent processing later. */
5307 bmask = phba->sli4_hba.vfi_bmask;
5308 ids = phba->sli4_hba.vfi_ids;
5309 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5310 break;
5311 default:
5312 /* Unsupported Opcode. Fail call. */
5313 id_array = NULL;
5314 bmask = NULL;
5315 ids = NULL;
5316 ext_blk_list = NULL;
5317 goto err_exit;
5318 }
5319
5320 /*
5321 * Complete initializing the extent configuration with the
5322 * allocated ids assigned to this function. The bitmask serves
5323 * as an index into the array and manages the available ids. The
5324 * array just stores the ids communicated to the port via the wqes.
5325 */
5326 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5327 if ((i % 2) == 0)
5328 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5329 &id_array[k]);
5330 else
5331 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5332 &id_array[k]);
5333
5334 rsrc_blks = kzalloc(length, GFP_KERNEL);
5335 if (unlikely(!rsrc_blks)) {
5336 rc = -ENOMEM;
5337 kfree(bmask);
5338 kfree(ids);
5339 goto err_exit;
5340 }
5341 rsrc_blks->rsrc_start = rsrc_id;
5342 rsrc_blks->rsrc_size = rsrc_size;
5343 list_add_tail(&rsrc_blks->list, ext_blk_list);
5344 rsrc_start = rsrc_id;
5345 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5346 phba->sli4_hba.scsi_xri_start = rsrc_start +
5347 lpfc_sli4_get_els_iocb_cnt(phba);
5348
5349 while (rsrc_id < (rsrc_start + rsrc_size)) {
5350 ids[j] = rsrc_id;
5351 rsrc_id++;
5352 j++;
5353 }
5354 /* Entire word processed. Get next word.*/
5355 if ((i % 2) == 1)
5356 k++;
5357 }
5358 err_exit:
5359 lpfc_sli4_mbox_cmd_free(phba, mbox);
5360 return rc;
5361}
5362
5363/**
5364 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5365 * @phba: Pointer to HBA context object.
5366 * @type: the extent's type.
5367 *
5368 * This function deallocates all extents of a particular resource type.
5369 * SLI4 does not allow for deallocating a particular extent range. It
5370 * is the caller's responsibility to release all kernel memory resources.
5371 **/
5372static int
5373lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5374{
5375 int rc;
5376 uint32_t length, mbox_tmo = 0;
5377 LPFC_MBOXQ_t *mbox;
5378 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5379 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5380
5381 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5382 if (!mbox)
5383 return -ENOMEM;
5384
5385 /*
5386 * This function sends an embedded mailbox because it only sends the
5387 * the resource type. All extents of this type are released by the
5388 * port.
5389 */
5390 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5391 sizeof(struct lpfc_sli4_cfg_mhdr));
5392 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5393 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5394 length, LPFC_SLI4_MBX_EMBED);
5395
5396 /* Send an extents count of 0 - the dealloc doesn't use it. */
5397 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5398 LPFC_SLI4_MBX_EMBED);
5399 if (unlikely(rc)) {
5400 rc = -EIO;
5401 goto out_free_mbox;
5402 }
5403 if (!phba->sli4_hba.intr_enable)
5404 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5405 else {
James Smarta183a152011-10-10 21:32:43 -04005406 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005407 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5408 }
5409 if (unlikely(rc)) {
5410 rc = -EIO;
5411 goto out_free_mbox;
5412 }
5413
5414 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5415 if (bf_get(lpfc_mbox_hdr_status,
5416 &dealloc_rsrc->header.cfg_shdr.response)) {
5417 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5418 "2919 Failed to release resource extents "
5419 "for type %d - Status 0x%x Add'l Status 0x%x. "
5420 "Resource memory not released.\n",
5421 type,
5422 bf_get(lpfc_mbox_hdr_status,
5423 &dealloc_rsrc->header.cfg_shdr.response),
5424 bf_get(lpfc_mbox_hdr_add_status,
5425 &dealloc_rsrc->header.cfg_shdr.response));
5426 rc = -EIO;
5427 goto out_free_mbox;
5428 }
5429
5430 /* Release kernel memory resources for the specific type. */
5431 switch (type) {
5432 case LPFC_RSC_TYPE_FCOE_VPI:
5433 kfree(phba->vpi_bmask);
5434 kfree(phba->vpi_ids);
5435 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5436 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5437 &phba->lpfc_vpi_blk_list, list) {
5438 list_del_init(&rsrc_blk->list);
5439 kfree(rsrc_blk);
5440 }
5441 break;
5442 case LPFC_RSC_TYPE_FCOE_XRI:
5443 kfree(phba->sli4_hba.xri_bmask);
5444 kfree(phba->sli4_hba.xri_ids);
5445 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5446 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5447 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5448 list_del_init(&rsrc_blk->list);
5449 kfree(rsrc_blk);
5450 }
5451 break;
5452 case LPFC_RSC_TYPE_FCOE_VFI:
5453 kfree(phba->sli4_hba.vfi_bmask);
5454 kfree(phba->sli4_hba.vfi_ids);
5455 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5456 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5457 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5458 list_del_init(&rsrc_blk->list);
5459 kfree(rsrc_blk);
5460 }
5461 break;
5462 case LPFC_RSC_TYPE_FCOE_RPI:
5463 /* RPI bitmask and physical id array are cleaned up earlier. */
5464 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5465 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5466 list_del_init(&rsrc_blk->list);
5467 kfree(rsrc_blk);
5468 }
5469 break;
5470 default:
5471 break;
5472 }
5473
5474 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5475
5476 out_free_mbox:
5477 mempool_free(mbox, phba->mbox_mem_pool);
5478 return rc;
5479}
5480
5481/**
5482 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5483 * @phba: Pointer to HBA context object.
5484 *
5485 * This function allocates all SLI4 resource identifiers.
5486 **/
5487int
5488lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5489{
5490 int i, rc, error = 0;
5491 uint16_t count, base;
5492 unsigned long longs;
5493
James Smartff78d8f2011-12-13 13:21:35 -05005494 if (!phba->sli4_hba.rpi_hdrs_in_use)
5495 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005496 if (phba->sli4_hba.extents_in_use) {
5497 /*
5498 * The port supports resource extents. The XRI, VPI, VFI, RPI
5499 * resource extent count must be read and allocated before
5500 * provisioning the resource id arrays.
5501 */
5502 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5503 LPFC_IDX_RSRC_RDY) {
5504 /*
5505 * Extent-based resources are set - the driver could
5506 * be in a port reset. Figure out if any corrective
5507 * actions need to be taken.
5508 */
5509 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5510 LPFC_RSC_TYPE_FCOE_VFI);
5511 if (rc != 0)
5512 error++;
5513 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5514 LPFC_RSC_TYPE_FCOE_VPI);
5515 if (rc != 0)
5516 error++;
5517 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5518 LPFC_RSC_TYPE_FCOE_XRI);
5519 if (rc != 0)
5520 error++;
5521 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5522 LPFC_RSC_TYPE_FCOE_RPI);
5523 if (rc != 0)
5524 error++;
5525
5526 /*
5527 * It's possible that the number of resources
5528 * provided to this port instance changed between
5529 * resets. Detect this condition and reallocate
5530 * resources. Otherwise, there is no action.
5531 */
5532 if (error) {
5533 lpfc_printf_log(phba, KERN_INFO,
5534 LOG_MBOX | LOG_INIT,
5535 "2931 Detected extent resource "
5536 "change. Reallocating all "
5537 "extents.\n");
5538 rc = lpfc_sli4_dealloc_extent(phba,
5539 LPFC_RSC_TYPE_FCOE_VFI);
5540 rc = lpfc_sli4_dealloc_extent(phba,
5541 LPFC_RSC_TYPE_FCOE_VPI);
5542 rc = lpfc_sli4_dealloc_extent(phba,
5543 LPFC_RSC_TYPE_FCOE_XRI);
5544 rc = lpfc_sli4_dealloc_extent(phba,
5545 LPFC_RSC_TYPE_FCOE_RPI);
5546 } else
5547 return 0;
5548 }
5549
5550 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5551 if (unlikely(rc))
5552 goto err_exit;
5553
5554 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5555 if (unlikely(rc))
5556 goto err_exit;
5557
5558 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5559 if (unlikely(rc))
5560 goto err_exit;
5561
5562 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5563 if (unlikely(rc))
5564 goto err_exit;
5565 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5566 LPFC_IDX_RSRC_RDY);
5567 return rc;
5568 } else {
5569 /*
5570 * The port does not support resource extents. The XRI, VPI,
5571 * VFI, RPI resource ids were determined from READ_CONFIG.
5572 * Just allocate the bitmasks and provision the resource id
5573 * arrays. If a port reset is active, the resources don't
5574 * need any action - just exit.
5575 */
5576 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005577 LPFC_IDX_RSRC_RDY) {
5578 lpfc_sli4_dealloc_resource_identifiers(phba);
5579 lpfc_sli4_remove_rpis(phba);
5580 }
James Smart6d368e52011-05-24 11:44:12 -04005581 /* RPIs. */
5582 count = phba->sli4_hba.max_cfg_param.max_rpi;
5583 base = phba->sli4_hba.max_cfg_param.rpi_base;
5584 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5585 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5586 sizeof(unsigned long),
5587 GFP_KERNEL);
5588 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5589 rc = -ENOMEM;
5590 goto err_exit;
5591 }
5592 phba->sli4_hba.rpi_ids = kzalloc(count *
5593 sizeof(uint16_t),
5594 GFP_KERNEL);
5595 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5596 rc = -ENOMEM;
5597 goto free_rpi_bmask;
5598 }
5599
5600 for (i = 0; i < count; i++)
5601 phba->sli4_hba.rpi_ids[i] = base + i;
5602
James Smart6b5151f2012-01-18 16:24:06 -05005603 lpfc_sli4_node_prep(phba);
5604
James Smart6d368e52011-05-24 11:44:12 -04005605 /* VPIs. */
5606 count = phba->sli4_hba.max_cfg_param.max_vpi;
5607 base = phba->sli4_hba.max_cfg_param.vpi_base;
5608 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5609 phba->vpi_bmask = kzalloc(longs *
5610 sizeof(unsigned long),
5611 GFP_KERNEL);
5612 if (unlikely(!phba->vpi_bmask)) {
5613 rc = -ENOMEM;
5614 goto free_rpi_ids;
5615 }
5616 phba->vpi_ids = kzalloc(count *
5617 sizeof(uint16_t),
5618 GFP_KERNEL);
5619 if (unlikely(!phba->vpi_ids)) {
5620 rc = -ENOMEM;
5621 goto free_vpi_bmask;
5622 }
5623
5624 for (i = 0; i < count; i++)
5625 phba->vpi_ids[i] = base + i;
5626
5627 /* XRIs. */
5628 count = phba->sli4_hba.max_cfg_param.max_xri;
5629 base = phba->sli4_hba.max_cfg_param.xri_base;
5630 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5631 phba->sli4_hba.xri_bmask = kzalloc(longs *
5632 sizeof(unsigned long),
5633 GFP_KERNEL);
5634 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5635 rc = -ENOMEM;
5636 goto free_vpi_ids;
5637 }
5638 phba->sli4_hba.xri_ids = kzalloc(count *
5639 sizeof(uint16_t),
5640 GFP_KERNEL);
5641 if (unlikely(!phba->sli4_hba.xri_ids)) {
5642 rc = -ENOMEM;
5643 goto free_xri_bmask;
5644 }
5645
5646 for (i = 0; i < count; i++)
5647 phba->sli4_hba.xri_ids[i] = base + i;
5648
5649 /* VFIs. */
5650 count = phba->sli4_hba.max_cfg_param.max_vfi;
5651 base = phba->sli4_hba.max_cfg_param.vfi_base;
5652 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5653 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5654 sizeof(unsigned long),
5655 GFP_KERNEL);
5656 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5657 rc = -ENOMEM;
5658 goto free_xri_ids;
5659 }
5660 phba->sli4_hba.vfi_ids = kzalloc(count *
5661 sizeof(uint16_t),
5662 GFP_KERNEL);
5663 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5664 rc = -ENOMEM;
5665 goto free_vfi_bmask;
5666 }
5667
5668 for (i = 0; i < count; i++)
5669 phba->sli4_hba.vfi_ids[i] = base + i;
5670
5671 /*
5672 * Mark all resources ready. An HBA reset doesn't need
5673 * to reset the initialization.
5674 */
5675 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5676 LPFC_IDX_RSRC_RDY);
5677 return 0;
5678 }
5679
5680 free_vfi_bmask:
5681 kfree(phba->sli4_hba.vfi_bmask);
5682 free_xri_ids:
5683 kfree(phba->sli4_hba.xri_ids);
5684 free_xri_bmask:
5685 kfree(phba->sli4_hba.xri_bmask);
5686 free_vpi_ids:
5687 kfree(phba->vpi_ids);
5688 free_vpi_bmask:
5689 kfree(phba->vpi_bmask);
5690 free_rpi_ids:
5691 kfree(phba->sli4_hba.rpi_ids);
5692 free_rpi_bmask:
5693 kfree(phba->sli4_hba.rpi_bmask);
5694 err_exit:
5695 return rc;
5696}
5697
5698/**
5699 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5700 * @phba: Pointer to HBA context object.
5701 *
5702 * This function allocates the number of elements for the specified
5703 * resource type.
5704 **/
5705int
5706lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5707{
5708 if (phba->sli4_hba.extents_in_use) {
5709 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5710 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5711 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5712 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5713 } else {
5714 kfree(phba->vpi_bmask);
5715 kfree(phba->vpi_ids);
5716 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5717 kfree(phba->sli4_hba.xri_bmask);
5718 kfree(phba->sli4_hba.xri_ids);
5719 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5720 kfree(phba->sli4_hba.vfi_bmask);
5721 kfree(phba->sli4_hba.vfi_ids);
5722 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5723 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5724 }
5725
5726 return 0;
5727}
5728
5729/**
James Smartb76f2dc2011-07-22 18:37:42 -04005730 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5731 * @phba: Pointer to HBA context object.
5732 * @type: The resource extent type.
5733 * @extnt_count: buffer to hold port extent count response
5734 * @extnt_size: buffer to hold port extent size response.
5735 *
5736 * This function calls the port to read the host allocated extents
5737 * for a particular type.
5738 **/
5739int
5740lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5741 uint16_t *extnt_cnt, uint16_t *extnt_size)
5742{
5743 bool emb;
5744 int rc = 0;
5745 uint16_t curr_blks = 0;
5746 uint32_t req_len, emb_len;
5747 uint32_t alloc_len, mbox_tmo;
5748 struct list_head *blk_list_head;
5749 struct lpfc_rsrc_blks *rsrc_blk;
5750 LPFC_MBOXQ_t *mbox;
5751 void *virtaddr = NULL;
5752 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5753 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5754 union lpfc_sli4_cfg_shdr *shdr;
5755
5756 switch (type) {
5757 case LPFC_RSC_TYPE_FCOE_VPI:
5758 blk_list_head = &phba->lpfc_vpi_blk_list;
5759 break;
5760 case LPFC_RSC_TYPE_FCOE_XRI:
5761 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5762 break;
5763 case LPFC_RSC_TYPE_FCOE_VFI:
5764 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5765 break;
5766 case LPFC_RSC_TYPE_FCOE_RPI:
5767 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5768 break;
5769 default:
5770 return -EIO;
5771 }
5772
5773 /* Count the number of extents currently allocatd for this type. */
5774 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5775 if (curr_blks == 0) {
5776 /*
5777 * The GET_ALLOCATED mailbox does not return the size,
5778 * just the count. The size should be just the size
5779 * stored in the current allocated block and all sizes
5780 * for an extent type are the same so set the return
5781 * value now.
5782 */
5783 *extnt_size = rsrc_blk->rsrc_size;
5784 }
5785 curr_blks++;
5786 }
5787
5788 /* Calculate the total requested length of the dma memory. */
5789 req_len = curr_blks * sizeof(uint16_t);
5790
5791 /*
5792 * Calculate the size of an embedded mailbox. The uint32_t
5793 * accounts for extents-specific word.
5794 */
5795 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5796 sizeof(uint32_t);
5797
5798 /*
5799 * Presume the allocation and response will fit into an embedded
5800 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5801 */
5802 emb = LPFC_SLI4_MBX_EMBED;
5803 req_len = emb_len;
5804 if (req_len > emb_len) {
5805 req_len = curr_blks * sizeof(uint16_t) +
5806 sizeof(union lpfc_sli4_cfg_shdr) +
5807 sizeof(uint32_t);
5808 emb = LPFC_SLI4_MBX_NEMBED;
5809 }
5810
5811 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5812 if (!mbox)
5813 return -ENOMEM;
5814 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5815
5816 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5817 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5818 req_len, emb);
5819 if (alloc_len < req_len) {
5820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5821 "2983 Allocated DMA memory size (x%x) is "
5822 "less than the requested DMA memory "
5823 "size (x%x)\n", alloc_len, req_len);
5824 rc = -ENOMEM;
5825 goto err_exit;
5826 }
5827 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5828 if (unlikely(rc)) {
5829 rc = -EIO;
5830 goto err_exit;
5831 }
5832
5833 if (!phba->sli4_hba.intr_enable)
5834 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5835 else {
James Smarta183a152011-10-10 21:32:43 -04005836 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005837 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5838 }
5839
5840 if (unlikely(rc)) {
5841 rc = -EIO;
5842 goto err_exit;
5843 }
5844
5845 /*
5846 * Figure out where the response is located. Then get local pointers
5847 * to the response data. The port does not guarantee to respond to
5848 * all extents counts request so update the local variable with the
5849 * allocated count from the port.
5850 */
5851 if (emb == LPFC_SLI4_MBX_EMBED) {
5852 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5853 shdr = &rsrc_ext->header.cfg_shdr;
5854 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5855 } else {
5856 virtaddr = mbox->sge_array->addr[0];
5857 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5858 shdr = &n_rsrc->cfg_shdr;
5859 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5860 }
5861
5862 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5863 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5864 "2984 Failed to read allocated resources "
5865 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5866 type,
5867 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5868 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5869 rc = -EIO;
5870 goto err_exit;
5871 }
5872 err_exit:
5873 lpfc_sli4_mbox_cmd_free(phba, mbox);
5874 return rc;
5875}
5876
5877/**
James Smartda0436e2009-05-22 14:51:39 -04005878 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5879 * @phba: Pointer to HBA context object.
5880 *
5881 * This function is the main SLI4 device intialization PCI function. This
5882 * function is called by the HBA intialization code, HBA reset code and
5883 * HBA error attention handler code. Caller is not required to hold any
5884 * locks.
5885 **/
5886int
5887lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5888{
5889 int rc;
5890 LPFC_MBOXQ_t *mboxq;
5891 struct lpfc_mqe *mqe;
5892 uint8_t *vpd;
5893 uint32_t vpd_size;
5894 uint32_t ftr_rsp = 0;
5895 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5896 struct lpfc_vport *vport = phba->pport;
5897 struct lpfc_dmabuf *mp;
5898
5899 /* Perform a PCI function reset to start from clean */
5900 rc = lpfc_pci_function_reset(phba);
5901 if (unlikely(rc))
5902 return -ENODEV;
5903
5904 /* Check the HBA Host Status Register for readyness */
5905 rc = lpfc_sli4_post_status_check(phba);
5906 if (unlikely(rc))
5907 return -ENODEV;
5908 else {
5909 spin_lock_irq(&phba->hbalock);
5910 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5911 spin_unlock_irq(&phba->hbalock);
5912 }
5913
5914 /*
5915 * Allocate a single mailbox container for initializing the
5916 * port.
5917 */
5918 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5919 if (!mboxq)
5920 return -ENOMEM;
5921
James Smartda0436e2009-05-22 14:51:39 -04005922 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04005923 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04005924 vpd = kzalloc(vpd_size, GFP_KERNEL);
5925 if (!vpd) {
5926 rc = -ENOMEM;
5927 goto out_free_mbox;
5928 }
5929
5930 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05005931 if (unlikely(rc)) {
5932 kfree(vpd);
5933 goto out_free_mbox;
5934 }
James Smartda0436e2009-05-22 14:51:39 -04005935 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04005936 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5937 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05005938 phba->hba_flag |= HBA_FCOE_MODE;
5939 else
5940 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04005941
5942 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5943 LPFC_DCBX_CEE_MODE)
5944 phba->hba_flag |= HBA_FIP_SUPPORT;
5945 else
5946 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5947
James Smartc31098c2011-04-16 11:03:33 -04005948 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04005949 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5950 "0376 READ_REV Error. SLI Level %d "
5951 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05005952 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04005953 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005954 kfree(vpd);
5955 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005956 }
James Smartcd1c8302011-10-10 21:33:25 -04005957
5958 /*
James Smartff78d8f2011-12-13 13:21:35 -05005959 * Continue initialization with default values even if driver failed
5960 * to read FCoE param config regions, only read parameters if the
5961 * board is FCoE
5962 */
5963 if (phba->hba_flag & HBA_FCOE_MODE &&
5964 lpfc_sli4_read_fcoe_params(phba))
5965 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
5966 "2570 Failed to read FCoE parameters\n");
5967
5968 /*
James Smartcd1c8302011-10-10 21:33:25 -04005969 * Retrieve sli4 device physical port name, failure of doing it
5970 * is considered as non-fatal.
5971 */
5972 rc = lpfc_sli4_retrieve_pport_name(phba);
5973 if (!rc)
5974 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5975 "3080 Successful retrieving SLI4 device "
5976 "physical port name: %s.\n", phba->Port);
5977
James Smartda0436e2009-05-22 14:51:39 -04005978 /*
5979 * Evaluate the read rev and vpd data. Populate the driver
5980 * state with the results. If this routine fails, the failure
5981 * is not fatal as the driver will use generic values.
5982 */
5983 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5984 if (unlikely(!rc)) {
5985 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5986 "0377 Error %d parsing vpd. "
5987 "Using defaults.\n", rc);
5988 rc = 0;
5989 }
James Smart76a95d72010-11-20 23:11:48 -05005990 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04005991
James Smartf1126682009-06-10 17:22:44 -04005992 /* Save information as VPD data */
5993 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5994 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5995 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5996 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5997 &mqe->un.read_rev);
5998 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5999 &mqe->un.read_rev);
6000 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6001 &mqe->un.read_rev);
6002 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6003 &mqe->un.read_rev);
6004 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6005 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6006 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6007 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6008 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6009 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6010 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6011 "(%d):0380 READ_REV Status x%x "
6012 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6013 mboxq->vport ? mboxq->vport->vpi : 0,
6014 bf_get(lpfc_mqe_status, mqe),
6015 phba->vpd.rev.opFwName,
6016 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6017 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006018
6019 /*
6020 * Discover the port's supported feature set and match it against the
6021 * hosts requests.
6022 */
6023 lpfc_request_features(phba, mboxq);
6024 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6025 if (unlikely(rc)) {
6026 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006027 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006028 }
6029
6030 /*
6031 * The port must support FCP initiator mode as this is the
6032 * only mode running in the host.
6033 */
6034 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6035 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6036 "0378 No support for fcpi mode.\n");
6037 ftr_rsp++;
6038 }
James Smartfedd3b72011-02-16 12:39:24 -05006039 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6040 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6041 else
6042 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006043 /*
6044 * If the port cannot support the host's requested features
6045 * then turn off the global config parameters to disable the
6046 * feature in the driver. This is not a fatal error.
6047 */
James Smartbf086112011-08-21 21:48:13 -04006048 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6049 if (phba->cfg_enable_bg) {
6050 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6051 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6052 else
6053 ftr_rsp++;
6054 }
James Smartda0436e2009-05-22 14:51:39 -04006055
6056 if (phba->max_vpi && phba->cfg_enable_npiv &&
6057 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6058 ftr_rsp++;
6059
6060 if (ftr_rsp) {
6061 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6062 "0379 Feature Mismatch Data: x%08x %08x "
6063 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6064 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6065 phba->cfg_enable_npiv, phba->max_vpi);
6066 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6067 phba->cfg_enable_bg = 0;
6068 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6069 phba->cfg_enable_npiv = 0;
6070 }
6071
6072 /* These SLI3 features are assumed in SLI4 */
6073 spin_lock_irq(&phba->hbalock);
6074 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6075 spin_unlock_irq(&phba->hbalock);
6076
James Smart6d368e52011-05-24 11:44:12 -04006077 /*
6078 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6079 * calls depends on these resources to complete port setup.
6080 */
6081 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6082 if (rc) {
6083 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6084 "2920 Failed to alloc Resource IDs "
6085 "rc = x%x\n", rc);
6086 goto out_free_mbox;
6087 }
James Smartff78d8f2011-12-13 13:21:35 -05006088 /* update physical xri mappings in the scsi buffers */
6089 lpfc_scsi_buf_update(phba);
James Smart6d368e52011-05-24 11:44:12 -04006090
James Smartda0436e2009-05-22 14:51:39 -04006091 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006092 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6093 if (rc) {
6094 phba->link_state = LPFC_HBA_ERROR;
6095 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006096 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006097 }
6098
James Smartda0436e2009-05-22 14:51:39 -04006099 mboxq->vport = vport;
6100 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6101 mp = (struct lpfc_dmabuf *) mboxq->context1;
6102 if (rc == MBX_SUCCESS) {
6103 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6104 rc = 0;
6105 }
6106
6107 /*
6108 * This memory was allocated by the lpfc_read_sparam routine. Release
6109 * it to the mbuf pool.
6110 */
6111 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6112 kfree(mp);
6113 mboxq->context1 = NULL;
6114 if (unlikely(rc)) {
6115 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6116 "0382 READ_SPARAM command failed "
6117 "status %d, mbxStatus x%x\n",
6118 rc, bf_get(lpfc_mqe_status, mqe));
6119 phba->link_state = LPFC_HBA_ERROR;
6120 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006121 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006122 }
6123
James Smart05580562011-05-24 11:40:48 -04006124 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006125
6126 /* Update the fc_host data structures with new wwn. */
6127 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6128 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6129
6130 /* Register SGL pool to the device using non-embedded mailbox command */
James Smart6d368e52011-05-24 11:44:12 -04006131 if (!phba->sli4_hba.extents_in_use) {
6132 rc = lpfc_sli4_post_els_sgl_list(phba);
6133 if (unlikely(rc)) {
6134 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6135 "0582 Error %d during els sgl post "
6136 "operation\n", rc);
6137 rc = -ENODEV;
6138 goto out_free_mbox;
6139 }
6140 } else {
6141 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
6142 if (unlikely(rc)) {
6143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6144 "2560 Error %d during els sgl post "
6145 "operation\n", rc);
6146 rc = -ENODEV;
6147 goto out_free_mbox;
6148 }
James Smartda0436e2009-05-22 14:51:39 -04006149 }
6150
6151 /* Register SCSI SGL pool to the device */
6152 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6153 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006154 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006155 "0383 Error %d during scsi sgl post "
6156 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006157 /* Some Scsi buffers were moved to the abort scsi list */
6158 /* A pci function reset will repost them */
6159 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006160 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006161 }
6162
6163 /* Post the rpi header region to the device. */
6164 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6165 if (unlikely(rc)) {
6166 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6167 "0393 Error %d during rpi post operation\n",
6168 rc);
6169 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006170 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006171 }
James Smartda0436e2009-05-22 14:51:39 -04006172
James Smart5350d872011-10-10 21:33:49 -04006173 /* Create all the SLI4 queues */
6174 rc = lpfc_sli4_queue_create(phba);
6175 if (rc) {
6176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6177 "3089 Failed to allocate queues\n");
6178 rc = -ENODEV;
6179 goto out_stop_timers;
6180 }
James Smartda0436e2009-05-22 14:51:39 -04006181 /* Set up all the queues to the device */
6182 rc = lpfc_sli4_queue_setup(phba);
6183 if (unlikely(rc)) {
6184 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6185 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006186 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006187 }
6188
6189 /* Arm the CQs and then EQs on device */
6190 lpfc_sli4_arm_cqeq_intr(phba);
6191
6192 /* Indicate device interrupt mode */
6193 phba->sli4_hba.intr_enable = 1;
6194
6195 /* Allow asynchronous mailbox command to go through */
6196 spin_lock_irq(&phba->hbalock);
6197 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6198 spin_unlock_irq(&phba->hbalock);
6199
6200 /* Post receive buffers to the device */
6201 lpfc_sli4_rb_setup(phba);
6202
James Smartfc2b9892010-02-26 14:15:29 -05006203 /* Reset HBA FCF states after HBA reset */
6204 phba->fcf.fcf_flag = 0;
6205 phba->fcf.current_rec.flag = 0;
6206
James Smartda0436e2009-05-22 14:51:39 -04006207 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006208 mod_timer(&vport->els_tmofunc,
6209 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006210
6211 /* Start heart beat timer */
6212 mod_timer(&phba->hb_tmofunc,
6213 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6214 phba->hb_outstanding = 0;
6215 phba->last_completion_time = jiffies;
6216
6217 /* Start error attention (ERATT) polling timer */
6218 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6219
James Smart75baf692010-06-08 18:31:21 -04006220 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6221 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6222 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6223 if (!rc) {
6224 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6225 "2829 This device supports "
6226 "Advanced Error Reporting (AER)\n");
6227 spin_lock_irq(&phba->hbalock);
6228 phba->hba_flag |= HBA_AER_ENABLED;
6229 spin_unlock_irq(&phba->hbalock);
6230 } else {
6231 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6232 "2830 This device does not support "
6233 "Advanced Error Reporting (AER)\n");
6234 phba->cfg_aer_support = 0;
6235 }
James Smart0a96e972011-07-22 18:37:28 -04006236 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006237 }
6238
James Smart76a95d72010-11-20 23:11:48 -05006239 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6240 /*
6241 * The FC Port needs to register FCFI (index 0)
6242 */
6243 lpfc_reg_fcfi(phba, mboxq);
6244 mboxq->vport = phba->pport;
6245 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006246 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006247 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006248 rc = 0;
6249 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6250 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006251
6252 /* Check if the port is configured to be disabled */
6253 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006254 }
James Smart026abb82011-12-13 13:20:45 -05006255
James Smartda0436e2009-05-22 14:51:39 -04006256 /*
6257 * The port is ready, set the host's link state to LINK_DOWN
6258 * in preparation for link interrupts.
6259 */
James Smartda0436e2009-05-22 14:51:39 -04006260 spin_lock_irq(&phba->hbalock);
6261 phba->link_state = LPFC_LINK_DOWN;
6262 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006263 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6264 (phba->hba_flag & LINK_DISABLED)) {
6265 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6266 "3103 Adapter Link is disabled.\n");
6267 lpfc_down_link(phba, mboxq);
6268 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6269 if (rc != MBX_SUCCESS) {
6270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6271 "3104 Adapter failed to issue "
6272 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6273 goto out_unset_queue;
6274 }
6275 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006276 /* don't perform init_link on SLI4 FC port loopback test */
6277 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6278 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6279 if (rc)
6280 goto out_unset_queue;
6281 }
James Smart5350d872011-10-10 21:33:49 -04006282 }
6283 mempool_free(mboxq, phba->mbox_mem_pool);
6284 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006285out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006286 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006287 lpfc_sli4_queue_unset(phba);
6288out_destroy_queue:
6289 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006290out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006291 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006292out_free_mbox:
6293 mempool_free(mboxq, phba->mbox_mem_pool);
6294 return rc;
6295}
James Smarte59058c2008-08-24 21:49:00 -04006296
6297/**
James Smart3621a712009-04-06 18:47:14 -04006298 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006299 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006300 *
James Smarte59058c2008-08-24 21:49:00 -04006301 * This is the callback function for mailbox timer. The mailbox
6302 * timer is armed when a new mailbox command is issued and the timer
6303 * is deleted when the mailbox complete. The function is called by
6304 * the kernel timer code when a mailbox does not complete within
6305 * expected time. This function wakes up the worker thread to
6306 * process the mailbox timeout and returns. All the processing is
6307 * done by the worker thread function lpfc_mbox_timeout_handler.
6308 **/
dea31012005-04-17 16:05:31 -05006309void
6310lpfc_mbox_timeout(unsigned long ptr)
6311{
James Smart92d7f7b2007-06-17 19:56:38 -05006312 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006313 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006314 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006315
James Smart2e0fef82007-06-17 19:56:36 -05006316 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006317 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006318 if (!tmo_posted)
6319 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6320 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6321
James Smart5e9d9b82008-06-14 22:52:53 -04006322 if (!tmo_posted)
6323 lpfc_worker_wake_up(phba);
6324 return;
dea31012005-04-17 16:05:31 -05006325}
6326
James Smarte59058c2008-08-24 21:49:00 -04006327
6328/**
James Smart3621a712009-04-06 18:47:14 -04006329 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006330 * @phba: Pointer to HBA context object.
6331 *
6332 * This function is called from worker thread when a mailbox command times out.
6333 * The caller is not required to hold any locks. This function will reset the
6334 * HBA and recover all the pending commands.
6335 **/
dea31012005-04-17 16:05:31 -05006336void
6337lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6338{
James Smart2e0fef82007-06-17 19:56:36 -05006339 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006340 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006341 struct lpfc_sli *psli = &phba->sli;
6342 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006343
James Smarta257bf92009-04-06 18:48:10 -04006344 /* Check the pmbox pointer first. There is a race condition
6345 * between the mbox timeout handler getting executed in the
6346 * worklist and the mailbox actually completing. When this
6347 * race condition occurs, the mbox_active will be NULL.
6348 */
6349 spin_lock_irq(&phba->hbalock);
6350 if (pmbox == NULL) {
6351 lpfc_printf_log(phba, KERN_WARNING,
6352 LOG_MBOX | LOG_SLI,
6353 "0353 Active Mailbox cleared - mailbox timeout "
6354 "exiting\n");
6355 spin_unlock_irq(&phba->hbalock);
6356 return;
6357 }
6358
dea31012005-04-17 16:05:31 -05006359 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006360 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006361 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006362 mb->mbxCommand,
6363 phba->pport->port_state,
6364 phba->sli.sli_flag,
6365 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006366 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006367
James Smart1dcb58e2007-04-25 09:51:30 -04006368 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6369 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006370 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006371 */
James Smart2e0fef82007-06-17 19:56:36 -05006372 spin_lock_irq(&phba->pport->work_port_lock);
6373 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6374 spin_unlock_irq(&phba->pport->work_port_lock);
6375 spin_lock_irq(&phba->hbalock);
6376 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006377 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006378 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006379
6380 pring = &psli->ring[psli->fcp_ring];
6381 lpfc_sli_abort_iocb_ring(phba, pring);
6382
6383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006384 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006385
6386 /* Reset the HBA device */
6387 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006388}
6389
James Smarte59058c2008-08-24 21:49:00 -04006390/**
James Smart3772a992009-05-22 14:50:54 -04006391 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006392 * @phba: Pointer to HBA context object.
6393 * @pmbox: Pointer to mailbox object.
6394 * @flag: Flag indicating how the mailbox need to be processed.
6395 *
6396 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006397 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6398 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006399 * The mailbox command can be submitted in polling mode, in which case
6400 * this function will wait in a polling loop for the completion of the
6401 * mailbox.
6402 * If the mailbox is submitted in no_wait mode (not polling) the
6403 * function will submit the command and returns immediately without waiting
6404 * for the mailbox completion. The no_wait is supported only when HBA
6405 * is in SLI2/SLI3 mode - interrupts are enabled.
6406 * The SLI interface allows only one mailbox pending at a time. If the
6407 * mailbox is issued in polling mode and there is already a mailbox
6408 * pending, then the function will return an error. If the mailbox is issued
6409 * in NO_WAIT mode and there is a mailbox pending already, the function
6410 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6411 * The sli layer owns the mailbox object until the completion of mailbox
6412 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6413 * return codes the caller owns the mailbox command after the return of
6414 * the function.
6415 **/
James Smart3772a992009-05-22 14:50:54 -04006416static int
6417lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6418 uint32_t flag)
dea31012005-04-17 16:05:31 -05006419{
dea31012005-04-17 16:05:31 -05006420 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006421 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006422 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006423 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006424 int i;
James Smart09372822008-01-11 01:52:54 -05006425 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006426 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006427 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006428 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006429 int processing_queue = 0;
6430
6431 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6432 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006433 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006434 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006435 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6436 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6437 return MBX_SUCCESS;
6438 }
James Smart58da1ff2008-04-07 10:15:56 -04006439 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006440 pmbox = lpfc_mbox_get(phba);
6441 if (!pmbox) {
6442 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6443 return MBX_SUCCESS;
6444 }
6445 }
dea31012005-04-17 16:05:31 -05006446
James Smarted957682007-06-17 19:56:37 -05006447 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006448 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006449 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006450 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006451 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006452 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006453 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006454 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006455 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006456 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006457 }
6458 }
6459
Linas Vepstas8d63f372007-02-14 14:28:36 -06006460 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006461 if (unlikely(pci_channel_offline(phba->pcidev))) {
6462 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6463 goto out_not_finished;
6464 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006465
James Smarta257bf92009-04-06 18:48:10 -04006466 /* If HBA has a deferred error attention, fail the iocb. */
6467 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6468 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6469 goto out_not_finished;
6470 }
6471
dea31012005-04-17 16:05:31 -05006472 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006473
James Smart3772a992009-05-22 14:50:54 -04006474 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006475 status = MBX_SUCCESS;
6476
James Smart2e0fef82007-06-17 19:56:36 -05006477 if (phba->link_state == LPFC_HBA_ERROR) {
6478 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006479
6480 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6482 "(%d):0311 Mailbox command x%x cannot "
6483 "issue Data: x%x x%x\n",
6484 pmbox->vport ? pmbox->vport->vpi : 0,
6485 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006486 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006487 }
6488
James Smart9940b972011-03-11 16:06:12 -05006489 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6490 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6491 !(hc_copy & HC_MBINT_ENA)) {
6492 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006494 "(%d):2528 Mailbox command x%x cannot "
6495 "issue Data: x%x x%x\n",
6496 pmbox->vport ? pmbox->vport->vpi : 0,
6497 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006498 goto out_not_finished;
6499 }
James Smart92908312006-03-07 15:04:13 -05006500 }
6501
dea31012005-04-17 16:05:31 -05006502 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6503 /* Polling for a mbox command when another one is already active
6504 * is not allowed in SLI. Also, the driver must have established
6505 * SLI2 mode to queue and process multiple mbox commands.
6506 */
6507
6508 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006509 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006510
6511 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006512 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6513 "(%d):2529 Mailbox command x%x "
6514 "cannot issue Data: x%x x%x\n",
6515 pmbox->vport ? pmbox->vport->vpi : 0,
6516 pmbox->u.mb.mbxCommand,
6517 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006518 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006519 }
6520
James Smart3772a992009-05-22 14:50:54 -04006521 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006522 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006523 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6525 "(%d):2530 Mailbox command x%x "
6526 "cannot issue Data: x%x x%x\n",
6527 pmbox->vport ? pmbox->vport->vpi : 0,
6528 pmbox->u.mb.mbxCommand,
6529 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006530 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006531 }
6532
dea31012005-04-17 16:05:31 -05006533 /* Another mailbox command is still being processed, queue this
6534 * command to be processed later.
6535 */
6536 lpfc_mbox_put(phba, pmbox);
6537
6538 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006539 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006540 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006541 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006542 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6543 mb->mbxCommand, phba->pport->port_state,
6544 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006545
6546 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006547 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006548
James Smart858c9f62007-06-17 19:56:39 -05006549 if (pmbox->vport) {
6550 lpfc_debugfs_disc_trc(pmbox->vport,
6551 LPFC_DISC_TRC_MBOX_VPORT,
6552 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6553 (uint32_t)mb->mbxCommand,
6554 mb->un.varWords[0], mb->un.varWords[1]);
6555 }
6556 else {
6557 lpfc_debugfs_disc_trc(phba->pport,
6558 LPFC_DISC_TRC_MBOX,
6559 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6560 (uint32_t)mb->mbxCommand,
6561 mb->un.varWords[0], mb->un.varWords[1]);
6562 }
6563
James Smart2e0fef82007-06-17 19:56:36 -05006564 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006565 }
6566
dea31012005-04-17 16:05:31 -05006567 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6568
6569 /* If we are not polling, we MUST be in SLI2 mode */
6570 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006571 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006572 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006573 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006574 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006575 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006576 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6577 "(%d):2531 Mailbox command x%x "
6578 "cannot issue Data: x%x x%x\n",
6579 pmbox->vport ? pmbox->vport->vpi : 0,
6580 pmbox->u.mb.mbxCommand,
6581 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006582 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006583 }
6584 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006585 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006586 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006587 }
6588
6589 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006590 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006591 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006592 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006593 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006594 mb->mbxCommand, phba->pport->port_state,
6595 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006596
James Smart858c9f62007-06-17 19:56:39 -05006597 if (mb->mbxCommand != MBX_HEARTBEAT) {
6598 if (pmbox->vport) {
6599 lpfc_debugfs_disc_trc(pmbox->vport,
6600 LPFC_DISC_TRC_MBOX_VPORT,
6601 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6602 (uint32_t)mb->mbxCommand,
6603 mb->un.varWords[0], mb->un.varWords[1]);
6604 }
6605 else {
6606 lpfc_debugfs_disc_trc(phba->pport,
6607 LPFC_DISC_TRC_MBOX,
6608 "MBOX Send: cmd:x%x mb:x%x x%x",
6609 (uint32_t)mb->mbxCommand,
6610 mb->un.varWords[0], mb->un.varWords[1]);
6611 }
6612 }
6613
dea31012005-04-17 16:05:31 -05006614 psli->slistat.mbox_cmd++;
6615 evtctr = psli->slistat.mbox_event;
6616
6617 /* next set own bit for the adapter and copy over command word */
6618 mb->mbxOwner = OWN_CHIP;
6619
James Smart3772a992009-05-22 14:50:54 -04006620 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006621 /* Populate mbox extension offset word. */
6622 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6623 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6624 = (uint8_t *)phba->mbox_ext
6625 - (uint8_t *)phba->mbox;
6626 }
6627
6628 /* Copy the mailbox extension data */
6629 if (pmbox->in_ext_byte_len && pmbox->context2) {
6630 lpfc_sli_pcimem_bcopy(pmbox->context2,
6631 (uint8_t *)phba->mbox_ext,
6632 pmbox->in_ext_byte_len);
6633 }
6634 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006635 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006636 } else {
James Smart7a470272010-03-15 11:25:20 -04006637 /* Populate mbox extension offset word. */
6638 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6639 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6640 = MAILBOX_HBA_EXT_OFFSET;
6641
6642 /* Copy the mailbox extension data */
6643 if (pmbox->in_ext_byte_len && pmbox->context2) {
6644 lpfc_memcpy_to_slim(phba->MBslimaddr +
6645 MAILBOX_HBA_EXT_OFFSET,
6646 pmbox->context2, pmbox->in_ext_byte_len);
6647
6648 }
James Smart92908312006-03-07 15:04:13 -05006649 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006650 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006651 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006652 }
6653
6654 /* First copy mbox command data to HBA SLIM, skip past first
6655 word */
6656 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6657 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6658 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6659
6660 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006661 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006662 to_slim = phba->MBslimaddr;
6663 writel(ldata, to_slim);
6664 readl(to_slim); /* flush */
6665
6666 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6667 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006668 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006669 }
6670 }
6671
6672 wmb();
dea31012005-04-17 16:05:31 -05006673
6674 switch (flag) {
6675 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006676 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006677 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006678 /* Interrupt board to do it */
6679 writel(CA_MBATT, phba->CAregaddr);
6680 readl(phba->CAregaddr); /* flush */
6681 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006682 break;
6683
6684 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006685 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006686 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006687 /* Interrupt board to do it */
6688 writel(CA_MBATT, phba->CAregaddr);
6689 readl(phba->CAregaddr); /* flush */
6690
James Smart3772a992009-05-22 14:50:54 -04006691 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006692 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006693 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006694 word0 = le32_to_cpu(word0);
6695 } else {
6696 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006697 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6698 spin_unlock_irqrestore(&phba->hbalock,
6699 drvr_flag);
6700 goto out_not_finished;
6701 }
dea31012005-04-17 16:05:31 -05006702 }
6703
6704 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006705 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6706 spin_unlock_irqrestore(&phba->hbalock,
6707 drvr_flag);
6708 goto out_not_finished;
6709 }
James Smarta183a152011-10-10 21:32:43 -04006710 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6711 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006712 i = 0;
dea31012005-04-17 16:05:31 -05006713 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006714 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6715 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006716 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006717 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006718 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006719 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006720 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006721 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006722 }
6723
6724 /* Check if we took a mbox interrupt while we were
6725 polling */
6726 if (((word0 & OWN_CHIP) != OWN_CHIP)
6727 && (evtctr != psli->slistat.mbox_event))
6728 break;
6729
James Smart09372822008-01-11 01:52:54 -05006730 if (i++ > 10) {
6731 spin_unlock_irqrestore(&phba->hbalock,
6732 drvr_flag);
6733 msleep(1);
6734 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6735 }
dea31012005-04-17 16:05:31 -05006736
James Smart3772a992009-05-22 14:50:54 -04006737 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006738 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006739 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006740 word0 = le32_to_cpu(word0);
6741 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6742 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006743 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006744 /* Check real SLIM for any errors */
6745 slimword0 = readl(phba->MBslimaddr);
6746 slimmb = (MAILBOX_t *) & slimword0;
6747 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6748 && slimmb->mbxStatus) {
6749 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006750 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006751 word0 = slimword0;
6752 }
6753 }
6754 } else {
6755 /* First copy command data */
6756 word0 = readl(phba->MBslimaddr);
6757 }
6758 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006759 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6760 spin_unlock_irqrestore(&phba->hbalock,
6761 drvr_flag);
6762 goto out_not_finished;
6763 }
dea31012005-04-17 16:05:31 -05006764 }
6765
James Smart3772a992009-05-22 14:50:54 -04006766 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006767 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006768 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006769 /* Copy the mailbox extension data */
6770 if (pmbox->out_ext_byte_len && pmbox->context2) {
6771 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6772 pmbox->context2,
6773 pmbox->out_ext_byte_len);
6774 }
dea31012005-04-17 16:05:31 -05006775 } else {
6776 /* First copy command data */
6777 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6778 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006779 /* Copy the mailbox extension data */
6780 if (pmbox->out_ext_byte_len && pmbox->context2) {
6781 lpfc_memcpy_from_slim(pmbox->context2,
6782 phba->MBslimaddr +
6783 MAILBOX_HBA_EXT_OFFSET,
6784 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006785 }
6786 }
6787
6788 writel(HA_MBATT, phba->HAregaddr);
6789 readl(phba->HAregaddr); /* flush */
6790
6791 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6792 status = mb->mbxStatus;
6793 }
6794
James Smart2e0fef82007-06-17 19:56:36 -05006795 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6796 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006797
6798out_not_finished:
6799 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006800 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006801 lpfc_mbox_cmpl_put(phba, pmbox);
6802 }
6803 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006804}
6805
James Smarte59058c2008-08-24 21:49:00 -04006806/**
James Smartf1126682009-06-10 17:22:44 -04006807 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6808 * @phba: Pointer to HBA context object.
6809 *
6810 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6811 * the driver internal pending mailbox queue. It will then try to wait out the
6812 * possible outstanding mailbox command before return.
6813 *
6814 * Returns:
6815 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6816 * the outstanding mailbox command timed out.
6817 **/
6818static int
6819lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6820{
6821 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006822 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006823 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006824
6825 /* Mark the asynchronous mailbox command posting as blocked */
6826 spin_lock_irq(&phba->hbalock);
6827 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006828 /* Determine how long we might wait for the active mailbox
6829 * command to be gracefully completed by firmware.
6830 */
James Smarta183a152011-10-10 21:32:43 -04006831 if (phba->sli.mbox_active)
6832 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6833 phba->sli.mbox_active) *
6834 1000) + jiffies;
6835 spin_unlock_irq(&phba->hbalock);
6836
James Smartf1126682009-06-10 17:22:44 -04006837 /* Wait for the outstnading mailbox command to complete */
6838 while (phba->sli.mbox_active) {
6839 /* Check active mailbox complete status every 2ms */
6840 msleep(2);
6841 if (time_after(jiffies, timeout)) {
6842 /* Timeout, marked the outstanding cmd not complete */
6843 rc = 1;
6844 break;
6845 }
6846 }
6847
6848 /* Can not cleanly block async mailbox command, fails it */
6849 if (rc) {
6850 spin_lock_irq(&phba->hbalock);
6851 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6852 spin_unlock_irq(&phba->hbalock);
6853 }
6854 return rc;
6855}
6856
6857/**
6858 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6859 * @phba: Pointer to HBA context object.
6860 *
6861 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6862 * commands from the driver internal pending mailbox queue. It makes sure
6863 * that there is no outstanding mailbox command before resuming posting
6864 * asynchronous mailbox commands. If, for any reason, there is outstanding
6865 * mailbox command, it will try to wait it out before resuming asynchronous
6866 * mailbox command posting.
6867 **/
6868static void
6869lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6870{
6871 struct lpfc_sli *psli = &phba->sli;
6872
6873 spin_lock_irq(&phba->hbalock);
6874 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6875 /* Asynchronous mailbox posting is not blocked, do nothing */
6876 spin_unlock_irq(&phba->hbalock);
6877 return;
6878 }
6879
6880 /* Outstanding synchronous mailbox command is guaranteed to be done,
6881 * successful or timeout, after timing-out the outstanding mailbox
6882 * command shall always be removed, so just unblock posting async
6883 * mailbox command and resume
6884 */
6885 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6886 spin_unlock_irq(&phba->hbalock);
6887
6888 /* wake up worker thread to post asynchronlous mailbox command */
6889 lpfc_worker_wake_up(phba);
6890}
6891
6892/**
James Smartda0436e2009-05-22 14:51:39 -04006893 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6894 * @phba: Pointer to HBA context object.
6895 * @mboxq: Pointer to mailbox object.
6896 *
6897 * The function posts a mailbox to the port. The mailbox is expected
6898 * to be comletely filled in and ready for the port to operate on it.
6899 * This routine executes a synchronous completion operation on the
6900 * mailbox by polling for its completion.
6901 *
6902 * The caller must not be holding any locks when calling this routine.
6903 *
6904 * Returns:
6905 * MBX_SUCCESS - mailbox posted successfully
6906 * Any of the MBX error values.
6907 **/
6908static int
6909lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6910{
6911 int rc = MBX_SUCCESS;
6912 unsigned long iflag;
6913 uint32_t db_ready;
6914 uint32_t mcqe_status;
6915 uint32_t mbx_cmnd;
6916 unsigned long timeout;
6917 struct lpfc_sli *psli = &phba->sli;
6918 struct lpfc_mqe *mb = &mboxq->u.mqe;
6919 struct lpfc_bmbx_create *mbox_rgn;
6920 struct dma_address *dma_address;
6921 struct lpfc_register bmbx_reg;
6922
6923 /*
6924 * Only one mailbox can be active to the bootstrap mailbox region
6925 * at a time and there is no queueing provided.
6926 */
6927 spin_lock_irqsave(&phba->hbalock, iflag);
6928 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6929 spin_unlock_irqrestore(&phba->hbalock, iflag);
6930 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006931 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04006932 "cannot issue Data: x%x x%x\n",
6933 mboxq->vport ? mboxq->vport->vpi : 0,
6934 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006935 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6936 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006937 psli->sli_flag, MBX_POLL);
6938 return MBXERR_ERROR;
6939 }
6940 /* The server grabs the token and owns it until release */
6941 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6942 phba->sli.mbox_active = mboxq;
6943 spin_unlock_irqrestore(&phba->hbalock, iflag);
6944
6945 /*
6946 * Initialize the bootstrap memory region to avoid stale data areas
6947 * in the mailbox post. Then copy the caller's mailbox contents to
6948 * the bmbx mailbox region.
6949 */
6950 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6951 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6952 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6953 sizeof(struct lpfc_mqe));
6954
6955 /* Post the high mailbox dma address to the port and wait for ready. */
6956 dma_address = &phba->sli4_hba.bmbx.dma_address;
6957 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6958
James Smarta183a152011-10-10 21:32:43 -04006959 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006960 * 1000) + jiffies;
6961 do {
6962 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6963 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6964 if (!db_ready)
6965 msleep(2);
6966
6967 if (time_after(jiffies, timeout)) {
6968 rc = MBXERR_ERROR;
6969 goto exit;
6970 }
6971 } while (!db_ready);
6972
6973 /* Post the low mailbox dma address to the port. */
6974 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04006975 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006976 * 1000) + jiffies;
6977 do {
6978 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6979 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6980 if (!db_ready)
6981 msleep(2);
6982
6983 if (time_after(jiffies, timeout)) {
6984 rc = MBXERR_ERROR;
6985 goto exit;
6986 }
6987 } while (!db_ready);
6988
6989 /*
6990 * Read the CQ to ensure the mailbox has completed.
6991 * If so, update the mailbox status so that the upper layers
6992 * can complete the request normally.
6993 */
6994 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6995 sizeof(struct lpfc_mqe));
6996 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6997 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6998 sizeof(struct lpfc_mcqe));
6999 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007000 /*
7001 * When the CQE status indicates a failure and the mailbox status
7002 * indicates success then copy the CQE status into the mailbox status
7003 * (and prefix it with x4000).
7004 */
James Smartda0436e2009-05-22 14:51:39 -04007005 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007006 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7007 bf_set(lpfc_mqe_status, mb,
7008 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007009 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007010 } else
7011 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007012
7013 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007014 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007015 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7016 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007017 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7018 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7019 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007020 bf_get(lpfc_mqe_status, mb),
7021 mb->un.mb_words[0], mb->un.mb_words[1],
7022 mb->un.mb_words[2], mb->un.mb_words[3],
7023 mb->un.mb_words[4], mb->un.mb_words[5],
7024 mb->un.mb_words[6], mb->un.mb_words[7],
7025 mb->un.mb_words[8], mb->un.mb_words[9],
7026 mb->un.mb_words[10], mb->un.mb_words[11],
7027 mb->un.mb_words[12], mboxq->mcqe.word0,
7028 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7029 mboxq->mcqe.trailer);
7030exit:
7031 /* We are holding the token, no needed for lock when release */
7032 spin_lock_irqsave(&phba->hbalock, iflag);
7033 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7034 phba->sli.mbox_active = NULL;
7035 spin_unlock_irqrestore(&phba->hbalock, iflag);
7036 return rc;
7037}
7038
7039/**
7040 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7041 * @phba: Pointer to HBA context object.
7042 * @pmbox: Pointer to mailbox object.
7043 * @flag: Flag indicating how the mailbox need to be processed.
7044 *
7045 * This function is called by discovery code and HBA management code to submit
7046 * a mailbox command to firmware with SLI-4 interface spec.
7047 *
7048 * Return codes the caller owns the mailbox command after the return of the
7049 * function.
7050 **/
7051static int
7052lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7053 uint32_t flag)
7054{
7055 struct lpfc_sli *psli = &phba->sli;
7056 unsigned long iflags;
7057 int rc;
7058
James Smartb76f2dc2011-07-22 18:37:42 -04007059 /* dump from issue mailbox command if setup */
7060 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7061
James Smart8fa38512009-07-19 10:01:03 -04007062 rc = lpfc_mbox_dev_check(phba);
7063 if (unlikely(rc)) {
7064 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007065 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007066 "cannot issue Data: x%x x%x\n",
7067 mboxq->vport ? mboxq->vport->vpi : 0,
7068 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007069 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7070 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007071 psli->sli_flag, flag);
7072 goto out_not_finished;
7073 }
7074
James Smartda0436e2009-05-22 14:51:39 -04007075 /* Detect polling mode and jump to a handler */
7076 if (!phba->sli4_hba.intr_enable) {
7077 if (flag == MBX_POLL)
7078 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7079 else
7080 rc = -EIO;
7081 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007082 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007083 "(%d):2541 Mailbox command x%x "
James Smarta183a152011-10-10 21:32:43 -04007084 "(x%x/x%x) cannot issue Data: "
7085 "x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007086 mboxq->vport ? mboxq->vport->vpi : 0,
7087 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007088 lpfc_sli_config_mbox_subsys_get(phba,
7089 mboxq),
7090 lpfc_sli_config_mbox_opcode_get(phba,
7091 mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007092 psli->sli_flag, flag);
7093 return rc;
7094 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007095 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7096 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007097 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007098 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007099 mboxq->vport ? mboxq->vport->vpi : 0,
7100 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007101 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7102 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007103 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007104 /* Try to block the asynchronous mailbox posting */
7105 rc = lpfc_sli4_async_mbox_block(phba);
7106 if (!rc) {
7107 /* Successfully blocked, now issue sync mbox cmd */
7108 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7109 if (rc != MBX_SUCCESS)
7110 lpfc_printf_log(phba, KERN_ERR,
James Smarta183a152011-10-10 21:32:43 -04007111 LOG_MBOX | LOG_SLI,
7112 "(%d):2597 Mailbox command "
7113 "x%x (x%x/x%x) cannot issue "
7114 "Data: x%x x%x\n",
7115 mboxq->vport ?
7116 mboxq->vport->vpi : 0,
7117 mboxq->u.mb.mbxCommand,
7118 lpfc_sli_config_mbox_subsys_get(phba,
7119 mboxq),
7120 lpfc_sli_config_mbox_opcode_get(phba,
7121 mboxq),
7122 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007123 /* Unblock the async mailbox posting afterward */
7124 lpfc_sli4_async_mbox_unblock(phba);
7125 }
7126 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007127 }
7128
7129 /* Now, interrupt mode asynchrous mailbox command */
7130 rc = lpfc_mbox_cmd_check(phba, mboxq);
7131 if (rc) {
7132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007133 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007134 "cannot issue Data: x%x x%x\n",
7135 mboxq->vport ? mboxq->vport->vpi : 0,
7136 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007137 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7138 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007139 psli->sli_flag, flag);
7140 goto out_not_finished;
7141 }
James Smartda0436e2009-05-22 14:51:39 -04007142
7143 /* Put the mailbox command to the driver internal FIFO */
7144 psli->slistat.mbox_busy++;
7145 spin_lock_irqsave(&phba->hbalock, iflags);
7146 lpfc_mbox_put(phba, mboxq);
7147 spin_unlock_irqrestore(&phba->hbalock, iflags);
7148 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7149 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007150 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007151 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7152 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007153 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7154 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007155 phba->pport->port_state,
7156 psli->sli_flag, MBX_NOWAIT);
7157 /* Wake up worker thread to transport mailbox command from head */
7158 lpfc_worker_wake_up(phba);
7159
7160 return MBX_BUSY;
7161
7162out_not_finished:
7163 return MBX_NOT_FINISHED;
7164}
7165
7166/**
7167 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7168 * @phba: Pointer to HBA context object.
7169 *
7170 * This function is called by worker thread to send a mailbox command to
7171 * SLI4 HBA firmware.
7172 *
7173 **/
7174int
7175lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7176{
7177 struct lpfc_sli *psli = &phba->sli;
7178 LPFC_MBOXQ_t *mboxq;
7179 int rc = MBX_SUCCESS;
7180 unsigned long iflags;
7181 struct lpfc_mqe *mqe;
7182 uint32_t mbx_cmnd;
7183
7184 /* Check interrupt mode before post async mailbox command */
7185 if (unlikely(!phba->sli4_hba.intr_enable))
7186 return MBX_NOT_FINISHED;
7187
7188 /* Check for mailbox command service token */
7189 spin_lock_irqsave(&phba->hbalock, iflags);
7190 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7191 spin_unlock_irqrestore(&phba->hbalock, iflags);
7192 return MBX_NOT_FINISHED;
7193 }
7194 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7195 spin_unlock_irqrestore(&phba->hbalock, iflags);
7196 return MBX_NOT_FINISHED;
7197 }
7198 if (unlikely(phba->sli.mbox_active)) {
7199 spin_unlock_irqrestore(&phba->hbalock, iflags);
7200 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7201 "0384 There is pending active mailbox cmd\n");
7202 return MBX_NOT_FINISHED;
7203 }
7204 /* Take the mailbox command service token */
7205 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7206
7207 /* Get the next mailbox command from head of queue */
7208 mboxq = lpfc_mbox_get(phba);
7209
7210 /* If no more mailbox command waiting for post, we're done */
7211 if (!mboxq) {
7212 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7213 spin_unlock_irqrestore(&phba->hbalock, iflags);
7214 return MBX_SUCCESS;
7215 }
7216 phba->sli.mbox_active = mboxq;
7217 spin_unlock_irqrestore(&phba->hbalock, iflags);
7218
7219 /* Check device readiness for posting mailbox command */
7220 rc = lpfc_mbox_dev_check(phba);
7221 if (unlikely(rc))
7222 /* Driver clean routine will clean up pending mailbox */
7223 goto out_not_finished;
7224
7225 /* Prepare the mbox command to be posted */
7226 mqe = &mboxq->u.mqe;
7227 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7228
7229 /* Start timer for the mbox_tmo and log some mailbox post messages */
7230 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007231 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007232
7233 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007234 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007235 "x%x x%x\n",
7236 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007237 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7238 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007239 phba->pport->port_state, psli->sli_flag);
7240
7241 if (mbx_cmnd != MBX_HEARTBEAT) {
7242 if (mboxq->vport) {
7243 lpfc_debugfs_disc_trc(mboxq->vport,
7244 LPFC_DISC_TRC_MBOX_VPORT,
7245 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7246 mbx_cmnd, mqe->un.mb_words[0],
7247 mqe->un.mb_words[1]);
7248 } else {
7249 lpfc_debugfs_disc_trc(phba->pport,
7250 LPFC_DISC_TRC_MBOX,
7251 "MBOX Send: cmd:x%x mb:x%x x%x",
7252 mbx_cmnd, mqe->un.mb_words[0],
7253 mqe->un.mb_words[1]);
7254 }
7255 }
7256 psli->slistat.mbox_cmd++;
7257
7258 /* Post the mailbox command to the port */
7259 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7260 if (rc != MBX_SUCCESS) {
7261 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007262 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007263 "cannot issue Data: x%x x%x\n",
7264 mboxq->vport ? mboxq->vport->vpi : 0,
7265 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007266 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7267 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007268 psli->sli_flag, MBX_NOWAIT);
7269 goto out_not_finished;
7270 }
7271
7272 return rc;
7273
7274out_not_finished:
7275 spin_lock_irqsave(&phba->hbalock, iflags);
7276 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7277 __lpfc_mbox_cmpl_put(phba, mboxq);
7278 /* Release the token */
7279 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7280 phba->sli.mbox_active = NULL;
7281 spin_unlock_irqrestore(&phba->hbalock, iflags);
7282
7283 return MBX_NOT_FINISHED;
7284}
7285
7286/**
7287 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7288 * @phba: Pointer to HBA context object.
7289 * @pmbox: Pointer to mailbox object.
7290 * @flag: Flag indicating how the mailbox need to be processed.
7291 *
7292 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7293 * the API jump table function pointer from the lpfc_hba struct.
7294 *
7295 * Return codes the caller owns the mailbox command after the return of the
7296 * function.
7297 **/
7298int
7299lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7300{
7301 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7302}
7303
7304/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007305 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007306 * @phba: The hba struct for which this call is being executed.
7307 * @dev_grp: The HBA PCI-Device group number.
7308 *
7309 * This routine sets up the mbox interface API function jump table in @phba
7310 * struct.
7311 * Returns: 0 - success, -ENODEV - failure.
7312 **/
7313int
7314lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7315{
7316
7317 switch (dev_grp) {
7318 case LPFC_PCI_DEV_LP:
7319 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7320 phba->lpfc_sli_handle_slow_ring_event =
7321 lpfc_sli_handle_slow_ring_event_s3;
7322 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7323 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7324 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7325 break;
7326 case LPFC_PCI_DEV_OC:
7327 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7328 phba->lpfc_sli_handle_slow_ring_event =
7329 lpfc_sli_handle_slow_ring_event_s4;
7330 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7331 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7332 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7333 break;
7334 default:
7335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7336 "1420 Invalid HBA PCI-device group: 0x%x\n",
7337 dev_grp);
7338 return -ENODEV;
7339 break;
7340 }
7341 return 0;
7342}
7343
7344/**
James Smart3621a712009-04-06 18:47:14 -04007345 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007346 * @phba: Pointer to HBA context object.
7347 * @pring: Pointer to driver SLI ring object.
7348 * @piocb: Pointer to address of newly added command iocb.
7349 *
7350 * This function is called with hbalock held to add a command
7351 * iocb to the txq when SLI layer cannot submit the command iocb
7352 * to the ring.
7353 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007354void
James Smart92d7f7b2007-06-17 19:56:38 -05007355__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007356 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007357{
7358 /* Insert the caller's iocb in the txq tail for later processing. */
7359 list_add_tail(&piocb->list, &pring->txq);
7360 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007361}
7362
James Smarte59058c2008-08-24 21:49:00 -04007363/**
James Smart3621a712009-04-06 18:47:14 -04007364 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007365 * @phba: Pointer to HBA context object.
7366 * @pring: Pointer to driver SLI ring object.
7367 * @piocb: Pointer to address of newly added command iocb.
7368 *
7369 * This function is called with hbalock held before a new
7370 * iocb is submitted to the firmware. This function checks
7371 * txq to flush the iocbs in txq to Firmware before
7372 * submitting new iocbs to the Firmware.
7373 * If there are iocbs in the txq which need to be submitted
7374 * to firmware, lpfc_sli_next_iocb returns the first element
7375 * of the txq after dequeuing it from txq.
7376 * If there is no iocb in the txq then the function will return
7377 * *piocb and *piocb is set to NULL. Caller needs to check
7378 * *piocb to find if there are more commands in the txq.
7379 **/
dea31012005-04-17 16:05:31 -05007380static struct lpfc_iocbq *
7381lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007382 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007383{
7384 struct lpfc_iocbq * nextiocb;
7385
7386 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7387 if (!nextiocb) {
7388 nextiocb = *piocb;
7389 *piocb = NULL;
7390 }
7391
7392 return nextiocb;
7393}
7394
James Smarte59058c2008-08-24 21:49:00 -04007395/**
James Smart3772a992009-05-22 14:50:54 -04007396 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007397 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007398 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007399 * @piocb: Pointer to command iocb.
7400 * @flag: Flag indicating if this command can be put into txq.
7401 *
James Smart3772a992009-05-22 14:50:54 -04007402 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7403 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7404 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7405 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7406 * this function allows only iocbs for posting buffers. This function finds
7407 * next available slot in the command ring and posts the command to the
7408 * available slot and writes the port attention register to request HBA start
7409 * processing new iocb. If there is no slot available in the ring and
7410 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7411 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007412 *
James Smart3772a992009-05-22 14:50:54 -04007413 * This function is called with hbalock held. The function will return success
7414 * after it successfully submit the iocb to firmware or after adding to the
7415 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007416 **/
James Smart98c9ea52007-10-27 13:37:33 -04007417static int
James Smart3772a992009-05-22 14:50:54 -04007418__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007419 struct lpfc_iocbq *piocb, uint32_t flag)
7420{
7421 struct lpfc_iocbq *nextiocb;
7422 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007423 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007424
James Smart92d7f7b2007-06-17 19:56:38 -05007425 if (piocb->iocb_cmpl && (!piocb->vport) &&
7426 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7427 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7428 lpfc_printf_log(phba, KERN_ERR,
7429 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007430 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007431 piocb->iocb.ulpCommand);
7432 dump_stack();
7433 return IOCB_ERROR;
7434 }
7435
7436
Linas Vepstas8d63f372007-02-14 14:28:36 -06007437 /* If the PCI channel is in offline state, do not post iocbs. */
7438 if (unlikely(pci_channel_offline(phba->pcidev)))
7439 return IOCB_ERROR;
7440
James Smarta257bf92009-04-06 18:48:10 -04007441 /* If HBA has a deferred error attention, fail the iocb. */
7442 if (unlikely(phba->hba_flag & DEFER_ERATT))
7443 return IOCB_ERROR;
7444
dea31012005-04-17 16:05:31 -05007445 /*
7446 * We should never get an IOCB if we are in a < LINK_DOWN state
7447 */
James Smart2e0fef82007-06-17 19:56:36 -05007448 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007449 return IOCB_ERROR;
7450
7451 /*
7452 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007453 * outstanding event.
dea31012005-04-17 16:05:31 -05007454 */
James Smart0b727fe2007-10-27 13:37:25 -04007455 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007456 goto iocb_busy;
7457
James Smart2e0fef82007-06-17 19:56:36 -05007458 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007459 /*
James Smart2680eea2007-04-25 09:52:55 -04007460 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007461 * can be issued if the link is not up.
7462 */
7463 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007464 case CMD_GEN_REQUEST64_CR:
7465 case CMD_GEN_REQUEST64_CX:
7466 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7467 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007468 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007469 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7470 MENLO_TRANSPORT_TYPE))
7471
7472 goto iocb_busy;
7473 break;
dea31012005-04-17 16:05:31 -05007474 case CMD_QUE_RING_BUF_CN:
7475 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007476 /*
7477 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7478 * completion, iocb_cmpl MUST be 0.
7479 */
7480 if (piocb->iocb_cmpl)
7481 piocb->iocb_cmpl = NULL;
7482 /*FALLTHROUGH*/
7483 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007484 case CMD_CLOSE_XRI_CN:
7485 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007486 break;
7487 default:
7488 goto iocb_busy;
7489 }
7490
7491 /*
7492 * For FCP commands, we must be in a state where we can process link
7493 * attention events.
7494 */
7495 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007496 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007497 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007498 }
dea31012005-04-17 16:05:31 -05007499
dea31012005-04-17 16:05:31 -05007500 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7501 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7502 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7503
7504 if (iocb)
7505 lpfc_sli_update_ring(phba, pring);
7506 else
7507 lpfc_sli_update_full_ring(phba, pring);
7508
7509 if (!piocb)
7510 return IOCB_SUCCESS;
7511
7512 goto out_busy;
7513
7514 iocb_busy:
7515 pring->stats.iocb_cmd_delay++;
7516
7517 out_busy:
7518
7519 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007520 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007521 return IOCB_SUCCESS;
7522 }
7523
7524 return IOCB_BUSY;
7525}
7526
James Smart3772a992009-05-22 14:50:54 -04007527/**
James Smart4f774512009-05-22 14:52:35 -04007528 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7529 * @phba: Pointer to HBA context object.
7530 * @piocb: Pointer to command iocb.
7531 * @sglq: Pointer to the scatter gather queue object.
7532 *
7533 * This routine converts the bpl or bde that is in the IOCB
7534 * to a sgl list for the sli4 hardware. The physical address
7535 * of the bpl/bde is converted back to a virtual address.
7536 * If the IOCB contains a BPL then the list of BDE's is
7537 * converted to sli4_sge's. If the IOCB contains a single
7538 * BDE then it is converted to a single sli_sge.
7539 * The IOCB is still in cpu endianess so the contents of
7540 * the bpl can be used without byte swapping.
7541 *
7542 * Returns valid XRI = Success, NO_XRI = Failure.
7543**/
7544static uint16_t
7545lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7546 struct lpfc_sglq *sglq)
7547{
7548 uint16_t xritag = NO_XRI;
7549 struct ulp_bde64 *bpl = NULL;
7550 struct ulp_bde64 bde;
7551 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007552 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007553 IOCB_t *icmd;
7554 int numBdes = 0;
7555 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007556 uint32_t offset = 0; /* accumulated offset in the sg request list */
7557 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007558
7559 if (!piocbq || !sglq)
7560 return xritag;
7561
7562 sgl = (struct sli4_sge *)sglq->sgl;
7563 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007564 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7565 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007566 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7567 numBdes = icmd->un.genreq64.bdl.bdeSize /
7568 sizeof(struct ulp_bde64);
7569 /* The addrHigh and addrLow fields within the IOCB
7570 * have not been byteswapped yet so there is no
7571 * need to swap them back.
7572 */
James Smart1b511972011-12-13 13:23:09 -05007573 if (piocbq->context3)
7574 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7575 else
7576 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007577
James Smart1b511972011-12-13 13:23:09 -05007578 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007579 if (!bpl)
7580 return xritag;
7581
7582 for (i = 0; i < numBdes; i++) {
7583 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007584 sgl->addr_hi = bpl->addrHigh;
7585 sgl->addr_lo = bpl->addrLow;
7586
James Smart05580562011-05-24 11:40:48 -04007587 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007588 if ((i+1) == numBdes)
7589 bf_set(lpfc_sli4_sge_last, sgl, 1);
7590 else
7591 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007592 /* swap the size field back to the cpu so we
7593 * can assign it to the sgl.
7594 */
7595 bde.tus.w = le32_to_cpu(bpl->tus.w);
7596 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007597 /* The offsets in the sgl need to be accumulated
7598 * separately for the request and reply lists.
7599 * The request is always first, the reply follows.
7600 */
7601 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7602 /* add up the reply sg entries */
7603 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7604 inbound++;
7605 /* first inbound? reset the offset */
7606 if (inbound == 1)
7607 offset = 0;
7608 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007609 bf_set(lpfc_sli4_sge_type, sgl,
7610 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007611 offset += bde.tus.f.bdeSize;
7612 }
James Smart546fc852011-03-11 16:06:29 -05007613 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007614 bpl++;
7615 sgl++;
7616 }
7617 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7618 /* The addrHigh and addrLow fields of the BDE have not
7619 * been byteswapped yet so they need to be swapped
7620 * before putting them in the sgl.
7621 */
7622 sgl->addr_hi =
7623 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7624 sgl->addr_lo =
7625 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007626 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007627 bf_set(lpfc_sli4_sge_last, sgl, 1);
7628 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007629 sgl->sge_len =
7630 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007631 }
7632 return sglq->sli4_xritag;
7633}
7634
7635/**
7636 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7637 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007638 *
James Smarta93ff372010-10-22 11:06:08 -04007639 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007640 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7641 * held.
James Smart4f774512009-05-22 14:52:35 -04007642 *
7643 * Return: index into SLI4 fast-path FCP queue index.
7644 **/
7645static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007646lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007647{
James Smart8fa38512009-07-19 10:01:03 -04007648 ++phba->fcp_qidx;
7649 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7650 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007651
James Smart8fa38512009-07-19 10:01:03 -04007652 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007653}
7654
7655/**
7656 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7657 * @phba: Pointer to HBA context object.
7658 * @piocb: Pointer to command iocb.
7659 * @wqe: Pointer to the work queue entry.
7660 *
7661 * This routine converts the iocb command to its Work Queue Entry
7662 * equivalent. The wqe pointer should not have any fields set when
7663 * this routine is called because it will memcpy over them.
7664 * This routine does not set the CQ_ID or the WQEC bits in the
7665 * wqe.
7666 *
7667 * Returns: 0 = Success, IOCB_ERROR = Failure.
7668 **/
7669static int
7670lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7671 union lpfc_wqe *wqe)
7672{
James Smart5ffc2662009-11-18 15:39:44 -05007673 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007674 uint8_t ct = 0;
7675 uint32_t fip;
7676 uint32_t abort_tag;
7677 uint8_t command_type = ELS_COMMAND_NON_FIP;
7678 uint8_t cmnd;
7679 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007680 uint16_t abrt_iotag;
7681 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007682 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007683 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007684 int numBdes, i;
7685 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007686 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007687 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007688 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007689
James Smart45ed1192009-10-02 15:17:02 -04007690 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007691 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007692 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007693 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007694 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007695 command_type = ELS_COMMAND_FIP;
7696 else
7697 command_type = ELS_COMMAND_NON_FIP;
7698
James Smart4f774512009-05-22 14:52:35 -04007699 /* Some of the fields are in the right position already */
7700 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7701 abort_tag = (uint32_t) iocbq->iotag;
7702 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007703 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007704 /* words0-2 bpl convert bde */
7705 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007706 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7707 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007708 bpl = (struct ulp_bde64 *)
7709 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7710 if (!bpl)
7711 return IOCB_ERROR;
7712
7713 /* Should already be byte swapped. */
7714 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7715 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7716 /* swap the size field back to the cpu so we
7717 * can assign it to the sgl.
7718 */
7719 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007720 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7721 total_len = 0;
7722 for (i = 0; i < numBdes; i++) {
7723 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7724 total_len += bde.tus.f.bdeSize;
7725 }
James Smart4f774512009-05-22 14:52:35 -04007726 } else
James Smart5ffc2662009-11-18 15:39:44 -05007727 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007728
7729 iocbq->iocb.ulpIoTag = iocbq->iotag;
7730 cmnd = iocbq->iocb.ulpCommand;
7731
7732 switch (iocbq->iocb.ulpCommand) {
7733 case CMD_ELS_REQUEST64_CR:
James Smartc31098c2011-04-16 11:03:33 -04007734 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007735 if (!iocbq->iocb.ulpLe) {
7736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7737 "2007 Only Limited Edition cmd Format"
7738 " supported 0x%x\n",
7739 iocbq->iocb.ulpCommand);
7740 return IOCB_ERROR;
7741 }
James Smartff78d8f2011-12-13 13:21:35 -05007742
James Smart5ffc2662009-11-18 15:39:44 -05007743 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007744 /* Els_reguest64 has a TMO */
7745 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7746 iocbq->iocb.ulpTimeout);
7747 /* Need a VF for word 4 set the vf bit*/
7748 bf_set(els_req64_vf, &wqe->els_req, 0);
7749 /* And a VFID for word 12 */
7750 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007751 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007752 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7753 iocbq->iocb.ulpContext);
7754 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7755 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007756 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007757 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007758 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7759 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007760 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7761 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007762 if_type = bf_get(lpfc_sli_intf_if_type,
7763 &phba->sli4_hba.sli_intf);
7764 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007765 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007766 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007767 *pcmd == ELS_CMD_FDISC ||
James Smartff78d8f2011-12-13 13:21:35 -05007768 *pcmd == ELS_CMD_PLOGI)) {
7769 bf_set(els_req64_sp, &wqe->els_req, 1);
7770 bf_set(els_req64_sid, &wqe->els_req,
7771 iocbq->vport->fc_myDID);
7772 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7773 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7774 phba->vpi_ids[phba->pport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007775 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007776 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7777 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7778 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7779 }
James Smartc8685952009-11-18 15:39:16 -05007780 }
James Smart6d368e52011-05-24 11:44:12 -04007781 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7782 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007783 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7784 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7785 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7786 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7787 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7788 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007789 break;
James Smart5ffc2662009-11-18 15:39:44 -05007790 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007791 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7792 iocbq->iocb.un.ulpWord[3]);
7793 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007794 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007795 /* The entire sequence is transmitted for this IOCB */
7796 xmit_len = total_len;
7797 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05007798 if (phba->link_flag & LS_LOOPBACK_MODE)
7799 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04007800 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007801 /* word3 iocb=io_tag32 wqe=reserved */
7802 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007803 /* word4 relative_offset memcpy */
7804 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007805 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7806 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7807 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7808 LPFC_WQE_IOD_WRITE);
7809 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7810 LPFC_WQE_LENLOC_WORD12);
7811 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007812 wqe->xmit_sequence.xmit_len = xmit_len;
7813 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007814 break;
James Smart4f774512009-05-22 14:52:35 -04007815 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007816 /* word3 iocb=iotag32 wqe=seq_payload_len */
7817 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007818 /* word4 iocb=rsvd wqe=rsvd */
7819 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7820 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007821 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007822 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007823 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7824 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7825 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7826 LPFC_WQE_LENLOC_WORD3);
7827 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007828 break;
James Smart4f774512009-05-22 14:52:35 -04007829 case CMD_FCP_IWRITE64_CR:
7830 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007831 /* word3 iocb=iotag wqe=payload_offset_len */
7832 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7833 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007834 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007835 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7836 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7837 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7838 iocbq->iocb.ulpFCP2Rcvy);
7839 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7840 /* Always open the exchange */
7841 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7842 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7843 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7844 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7845 LPFC_WQE_LENLOC_WORD4);
7846 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7847 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007848 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04007849 case CMD_FCP_IREAD64_CR:
7850 /* word3 iocb=iotag wqe=payload_offset_len */
7851 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7852 wqe->fcp_iread.payload_offset_len =
7853 xmit_len + sizeof(struct fcp_rsp);
7854 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7855 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7856 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7857 iocbq->iocb.ulpFCP2Rcvy);
7858 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007859 /* Always open the exchange */
7860 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007861 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7862 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7863 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7864 LPFC_WQE_LENLOC_WORD4);
7865 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7866 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007867 break;
James Smartf1126682009-06-10 17:22:44 -04007868 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007869 /* word3 iocb=IO_TAG wqe=reserved */
7870 wqe->fcp_icmd.rsrvd3 = 0;
7871 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007872 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007873 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7874 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7875 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7876 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7877 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7878 LPFC_WQE_LENLOC_NONE);
7879 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007880 break;
James Smart4f774512009-05-22 14:52:35 -04007881 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007882 /* For this command calculate the xmit length of the
7883 * request bde.
7884 */
7885 xmit_len = 0;
7886 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7887 sizeof(struct ulp_bde64);
7888 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007889 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007890 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7891 break;
James Smart63e801c2010-11-20 23:14:19 -05007892 xmit_len += bde.tus.f.bdeSize;
7893 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007894 /* word3 iocb=IO_TAG wqe=request_payload_len */
7895 wqe->gen_req.request_payload_len = xmit_len;
7896 /* word4 iocb=parameter wqe=relative_offset memcpy */
7897 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007898 /* word6 context tag copied in memcpy */
7899 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7900 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7901 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7902 "2015 Invalid CT %x command 0x%x\n",
7903 ct, iocbq->iocb.ulpCommand);
7904 return IOCB_ERROR;
7905 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007906 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7907 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7908 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7909 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7910 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7911 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7912 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7913 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007914 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007915 break;
James Smart4f774512009-05-22 14:52:35 -04007916 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007917 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007918 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007919 /* word3 iocb=iotag32 wqe=response_payload_len */
7920 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007921 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007922 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007923 /* word5 iocb=rsvd wge=did */
7924 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7925 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007926 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7927 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7928 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7929 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007930 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04007931 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007932 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007933 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007934 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7935 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7936 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7937 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7938 LPFC_WQE_LENLOC_WORD3);
7939 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007940 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7941 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05007942 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7943 iocbq->context2)->virt);
7944 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7945 bf_set(els_req64_sp, &wqe->els_req, 1);
7946 bf_set(els_req64_sid, &wqe->els_req,
7947 iocbq->vport->fc_myDID);
7948 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7949 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7950 phba->vpi_ids[phba->pport->vpi]);
7951 }
James Smart4f774512009-05-22 14:52:35 -04007952 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007953 break;
James Smart4f774512009-05-22 14:52:35 -04007954 case CMD_CLOSE_XRI_CN:
7955 case CMD_ABORT_XRI_CN:
7956 case CMD_ABORT_XRI_CX:
7957 /* words 0-2 memcpy should be 0 rserved */
7958 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007959 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7960 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7961 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7962 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7963 } else
7964 fip = 0;
7965
7966 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007967 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007968 * The link is down, or the command was ELS_FIP
7969 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007970 * on the wire.
7971 */
7972 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7973 else
7974 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7975 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007976 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7977 wqe->abort_cmd.rsrvd5 = 0;
7978 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007979 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7980 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007981 /*
7982 * The abort handler will send us CMD_ABORT_XRI_CN or
7983 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7984 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007985 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7986 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7987 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7988 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007989 cmnd = CMD_ABORT_XRI_CX;
7990 command_type = OTHER_COMMAND;
7991 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04007992 break;
James Smart6669f9b2009-10-02 15:16:45 -04007993 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05007994 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05007995 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04007996 * we re-construct this WQE here based on information in
7997 * iocbq from scratch.
7998 */
7999 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008000 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008001 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008002 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8003 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008004 LPFC_ABTS_UNSOL_INT) {
8005 /* ABTS sent by initiator to CT exchange, the
8006 * RX_ID field will be filled with the newly
8007 * allocated responder XRI.
8008 */
8009 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8010 iocbq->sli4_xritag);
8011 } else {
8012 /* ABTS sent by responder to CT exchange, the
8013 * RX_ID field will be filled with the responder
8014 * RX_ID from ABTS.
8015 */
8016 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008017 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008018 }
James Smart6669f9b2009-10-02 15:16:45 -04008019 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8020 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008021
8022 /* Use CT=VPI */
8023 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8024 ndlp->nlp_DID);
8025 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8026 iocbq->iocb.ulpContext);
8027 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008028 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008029 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008030 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8031 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8032 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008033 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8034 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008035 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8036 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8037 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8038 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8039 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8040 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8041 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8042 }
8043
James Smart7851fe22011-07-22 18:36:52 -04008044 break;
James Smart4f774512009-05-22 14:52:35 -04008045 case CMD_XRI_ABORTED_CX:
8046 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008047 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8048 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8049 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8050 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8051 default:
8052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8053 "2014 Invalid command 0x%x\n",
8054 iocbq->iocb.ulpCommand);
8055 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008056 break;
James Smart4f774512009-05-22 14:52:35 -04008057 }
James Smart6d368e52011-05-24 11:44:12 -04008058
James Smartf0d9bcc2010-10-22 11:07:09 -04008059 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8060 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8061 wqe->generic.wqe_com.abort_tag = abort_tag;
8062 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8063 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8064 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8065 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008066 return 0;
8067}
8068
8069/**
8070 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8071 * @phba: Pointer to HBA context object.
8072 * @ring_number: SLI ring number to issue iocb on.
8073 * @piocb: Pointer to command iocb.
8074 * @flag: Flag indicating if this command can be put into txq.
8075 *
8076 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8077 * an iocb command to an HBA with SLI-4 interface spec.
8078 *
8079 * This function is called with hbalock held. The function will return success
8080 * after it successfully submit the iocb to firmware or after adding to the
8081 * txq.
8082 **/
8083static int
8084__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8085 struct lpfc_iocbq *piocb, uint32_t flag)
8086{
8087 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008088 union lpfc_wqe wqe;
8089 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008090
8091 if (piocb->sli4_xritag == NO_XRI) {
8092 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008093 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008094 sglq = NULL;
8095 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008096 if (pring->txq_cnt) {
8097 if (!(flag & SLI_IOCB_RET_IOCB)) {
8098 __lpfc_sli_ringtx_put(phba,
8099 pring, piocb);
8100 return IOCB_SUCCESS;
8101 } else {
8102 return IOCB_BUSY;
8103 }
8104 } else {
James Smart6d368e52011-05-24 11:44:12 -04008105 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008106 if (!sglq) {
8107 if (!(flag & SLI_IOCB_RET_IOCB)) {
8108 __lpfc_sli_ringtx_put(phba,
8109 pring,
8110 piocb);
8111 return IOCB_SUCCESS;
8112 } else
8113 return IOCB_BUSY;
8114 }
8115 }
James Smart4f774512009-05-22 14:52:35 -04008116 }
8117 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008118 /* These IO's already have an XRI and a mapped sgl. */
8119 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008120 } else {
James Smart6d368e52011-05-24 11:44:12 -04008121 /*
8122 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008123 * sglq is on the active list
8124 */
8125 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8126 if (!sglq)
8127 return IOCB_ERROR;
8128 }
8129
8130 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008131 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008132 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008133 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008134 return IOCB_ERROR;
8135 }
8136
8137 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8138 return IOCB_ERROR;
8139
James Smart341af102010-01-26 23:07:37 -05008140 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8141 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008142 /*
8143 * For FCP command IOCB, get a new WQ index to distribute
8144 * WQE across the WQsr. On the other hand, for abort IOCB,
8145 * it carries the same WQ index to the original command
8146 * IOCB.
8147 */
James Smart341af102010-01-26 23:07:37 -05008148 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008149 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05008150 if (unlikely(!phba->sli4_hba.fcp_wq))
8151 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008152 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8153 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008154 return IOCB_ERROR;
8155 } else {
8156 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8157 return IOCB_ERROR;
8158 }
8159 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8160
8161 return 0;
8162}
8163
8164/**
James Smart3772a992009-05-22 14:50:54 -04008165 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8166 *
8167 * This routine wraps the actual lockless version for issusing IOCB function
8168 * pointer from the lpfc_hba struct.
8169 *
8170 * Return codes:
8171 * IOCB_ERROR - Error
8172 * IOCB_SUCCESS - Success
8173 * IOCB_BUSY - Busy
8174 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008175int
James Smart3772a992009-05-22 14:50:54 -04008176__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8177 struct lpfc_iocbq *piocb, uint32_t flag)
8178{
8179 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8180}
8181
8182/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008183 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008184 * @phba: The hba struct for which this call is being executed.
8185 * @dev_grp: The HBA PCI-Device group number.
8186 *
8187 * This routine sets up the SLI interface API function jump table in @phba
8188 * struct.
8189 * Returns: 0 - success, -ENODEV - failure.
8190 **/
8191int
8192lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8193{
8194
8195 switch (dev_grp) {
8196 case LPFC_PCI_DEV_LP:
8197 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8198 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8199 break;
James Smart4f774512009-05-22 14:52:35 -04008200 case LPFC_PCI_DEV_OC:
8201 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8202 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8203 break;
James Smart3772a992009-05-22 14:50:54 -04008204 default:
8205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8206 "1419 Invalid HBA PCI-device group: 0x%x\n",
8207 dev_grp);
8208 return -ENODEV;
8209 break;
8210 }
8211 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8212 return 0;
8213}
James Smart92d7f7b2007-06-17 19:56:38 -05008214
James Smarte59058c2008-08-24 21:49:00 -04008215/**
James Smart3621a712009-04-06 18:47:14 -04008216 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008217 * @phba: Pointer to HBA context object.
8218 * @pring: Pointer to driver SLI ring object.
8219 * @piocb: Pointer to command iocb.
8220 * @flag: Flag indicating if this command can be put into txq.
8221 *
8222 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8223 * function. This function gets the hbalock and calls
8224 * __lpfc_sli_issue_iocb function and will return the error returned
8225 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8226 * functions which do not hold hbalock.
8227 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008228int
James Smart3772a992009-05-22 14:50:54 -04008229lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008230 struct lpfc_iocbq *piocb, uint32_t flag)
8231{
8232 unsigned long iflags;
8233 int rc;
8234
8235 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04008236 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05008237 spin_unlock_irqrestore(&phba->hbalock, iflags);
8238
8239 return rc;
8240}
8241
James Smarte59058c2008-08-24 21:49:00 -04008242/**
James Smart3621a712009-04-06 18:47:14 -04008243 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008244 * @phba: Pointer to HBA context object.
8245 *
8246 * This function is called while driver attaches with the
8247 * HBA to setup the extra ring. The extra ring is used
8248 * only when driver needs to support target mode functionality
8249 * or IP over FC functionalities.
8250 *
8251 * This function is called with no lock held.
8252 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008253static int
8254lpfc_extra_ring_setup( struct lpfc_hba *phba)
8255{
8256 struct lpfc_sli *psli;
8257 struct lpfc_sli_ring *pring;
8258
8259 psli = &phba->sli;
8260
8261 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008262
8263 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008264 pring = &psli->ring[psli->fcp_ring];
8265 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8266 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8267 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8268 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8269
James Smarta4bc3372006-12-02 13:34:16 -05008270 /* and give them to the extra ring */
8271 pring = &psli->ring[psli->extra_ring];
8272
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008273 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8274 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8275 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8276 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8277
8278 /* Setup default profile for this ring */
8279 pring->iotag_max = 4096;
8280 pring->num_mask = 1;
8281 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008282 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8283 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008284 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8285 return 0;
8286}
8287
James Smartcb69f7d2011-12-13 13:21:57 -05008288/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8289 * @vport: pointer to virtual port object.
8290 * @ndlp: nodelist pointer for the impacted rport.
8291 *
8292 * The driver calls this routine in response to a XRI ABORT CQE
8293 * event from the port. In this event, the driver is required to
8294 * recover its login to the rport even though its login may be valid
8295 * from the driver's perspective. The failed ABTS notice from the
8296 * port indicates the rport is not responding.
8297 */
8298static void
8299lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8300 struct lpfc_nodelist *ndlp)
8301{
8302 struct Scsi_Host *shost;
8303 struct lpfc_hba *phba;
8304 unsigned long flags = 0;
8305
8306 shost = lpfc_shost_from_vport(vport);
8307 phba = vport->phba;
8308 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8309 lpfc_printf_log(phba, KERN_INFO,
8310 LOG_SLI, "3093 No rport recovery needed. "
8311 "rport in state 0x%x\n",
8312 ndlp->nlp_state);
8313 return;
8314 }
8315 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8316 "3094 Start rport recovery on shost id 0x%x "
8317 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8318 "flags 0x%x\n",
8319 shost->host_no, ndlp->nlp_DID,
8320 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8321 ndlp->nlp_flag);
8322 /*
8323 * The rport is not responding. Don't attempt ADISC recovery.
8324 * Remove the FCP-2 flag to force a PLOGI.
8325 */
8326 spin_lock_irqsave(shost->host_lock, flags);
8327 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8328 spin_unlock_irqrestore(shost->host_lock, flags);
8329 lpfc_disc_state_machine(vport, ndlp, NULL,
8330 NLP_EVT_DEVICE_RECOVERY);
8331 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8332 spin_lock_irqsave(shost->host_lock, flags);
8333 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8334 spin_unlock_irqrestore(shost->host_lock, flags);
8335 lpfc_disc_start(vport);
8336}
8337
8338/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8339 * @phba: Pointer to HBA context object.
8340 * @iocbq: Pointer to iocb object.
8341 *
8342 * The async_event handler calls this routine when it receives
8343 * an ASYNC_STATUS_CN event from the port. The port generates
8344 * this event when an Abort Sequence request to an rport fails
8345 * twice in succession. The abort could be originated by the
8346 * driver or by the port. The ABTS could have been for an ELS
8347 * or FCP IO. The port only generates this event when an ABTS
8348 * fails to complete after one retry.
8349 */
8350static void
8351lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8352 struct lpfc_iocbq *iocbq)
8353{
8354 struct lpfc_nodelist *ndlp = NULL;
8355 uint16_t rpi = 0, vpi = 0;
8356 struct lpfc_vport *vport = NULL;
8357
8358 /* The rpi in the ulpContext is vport-sensitive. */
8359 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8360 rpi = iocbq->iocb.ulpContext;
8361
8362 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8363 "3092 Port generated ABTS async event "
8364 "on vpi %d rpi %d status 0x%x\n",
8365 vpi, rpi, iocbq->iocb.ulpStatus);
8366
8367 vport = lpfc_find_vport_by_vpid(phba, vpi);
8368 if (!vport)
8369 goto err_exit;
8370 ndlp = lpfc_findnode_rpi(vport, rpi);
8371 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8372 goto err_exit;
8373
8374 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8375 lpfc_sli_abts_recover_port(vport, ndlp);
8376 return;
8377
8378 err_exit:
8379 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8380 "3095 Event Context not found, no "
8381 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8382 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8383 vpi, rpi);
8384}
8385
8386/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8387 * @phba: pointer to HBA context object.
8388 * @ndlp: nodelist pointer for the impacted rport.
8389 * @axri: pointer to the wcqe containing the failed exchange.
8390 *
8391 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8392 * port. The port generates this event when an abort exchange request to an
8393 * rport fails twice in succession with no reply. The abort could be originated
8394 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8395 */
8396void
8397lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8398 struct lpfc_nodelist *ndlp,
8399 struct sli4_wcqe_xri_aborted *axri)
8400{
8401 struct lpfc_vport *vport;
8402
James Smart6b5151f2012-01-18 16:24:06 -05008403 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008404 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8405 "3115 Node Context not found, driver "
8406 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008407 return;
8408 }
8409
James Smartcb69f7d2011-12-13 13:21:57 -05008410 vport = ndlp->vport;
8411 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8412 "3116 Port generated FCP XRI ABORT event on "
8413 "vpi %d rpi %d xri x%x status 0x%x\n",
8414 ndlp->vport->vpi, ndlp->nlp_rpi,
8415 bf_get(lpfc_wcqe_xa_xri, axri),
8416 bf_get(lpfc_wcqe_xa_status, axri));
8417
8418 if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
8419 lpfc_sli_abts_recover_port(vport, ndlp);
8420}
8421
James Smarte59058c2008-08-24 21:49:00 -04008422/**
James Smart3621a712009-04-06 18:47:14 -04008423 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008424 * @phba: Pointer to HBA context object.
8425 * @pring: Pointer to driver SLI ring object.
8426 * @iocbq: Pointer to iocb object.
8427 *
8428 * This function is called by the slow ring event handler
8429 * function when there is an ASYNC event iocb in the ring.
8430 * This function is called with no lock held.
8431 * Currently this function handles only temperature related
8432 * ASYNC events. The function decodes the temperature sensor
8433 * event message and posts events for the management applications.
8434 **/
James Smart98c9ea52007-10-27 13:37:33 -04008435static void
James Smart57127f12007-10-27 13:37:05 -04008436lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8437 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8438{
8439 IOCB_t *icmd;
8440 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008441 struct temp_event temp_event_data;
8442 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008443 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008444
8445 icmd = &iocbq->iocb;
8446 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008447
James Smartcb69f7d2011-12-13 13:21:57 -05008448 switch (evt_code) {
8449 case ASYNC_TEMP_WARN:
8450 case ASYNC_TEMP_SAFE:
8451 temp_event_data.data = (uint32_t) icmd->ulpContext;
8452 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8453 if (evt_code == ASYNC_TEMP_WARN) {
8454 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8455 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8456 "0347 Adapter is very hot, please take "
8457 "corrective action. temperature : %d Celsius\n",
8458 (uint32_t) icmd->ulpContext);
8459 } else {
8460 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8461 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8462 "0340 Adapter temperature is OK now. "
8463 "temperature : %d Celsius\n",
8464 (uint32_t) icmd->ulpContext);
8465 }
8466
8467 /* Send temperature change event to applications */
8468 shost = lpfc_shost_from_vport(phba->pport);
8469 fc_host_post_vendor_event(shost, fc_get_event_number(),
8470 sizeof(temp_event_data), (char *) &temp_event_data,
8471 LPFC_NL_VENDOR_ID);
8472 break;
8473 case ASYNC_STATUS_CN:
8474 lpfc_sli_abts_err_handler(phba, iocbq);
8475 break;
8476 default:
James Smarta257bf92009-04-06 18:48:10 -04008477 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008479 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008480 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008481 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8482 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8483 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8484 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008485 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008486 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8487 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8488 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8489 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8490
James Smartcb69f7d2011-12-13 13:21:57 -05008491 break;
James Smart57127f12007-10-27 13:37:05 -04008492 }
James Smart57127f12007-10-27 13:37:05 -04008493}
8494
8495
James Smarte59058c2008-08-24 21:49:00 -04008496/**
James Smart3621a712009-04-06 18:47:14 -04008497 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008498 * @phba: Pointer to HBA context object.
8499 *
8500 * lpfc_sli_setup sets up rings of the SLI interface with
8501 * number of iocbs per ring and iotags. This function is
8502 * called while driver attach to the HBA and before the
8503 * interrupts are enabled. So there is no need for locking.
8504 *
8505 * This function always returns 0.
8506 **/
dea31012005-04-17 16:05:31 -05008507int
8508lpfc_sli_setup(struct lpfc_hba *phba)
8509{
James Smarted957682007-06-17 19:56:37 -05008510 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008511 struct lpfc_sli *psli = &phba->sli;
8512 struct lpfc_sli_ring *pring;
8513
8514 psli->num_rings = MAX_CONFIGURED_RINGS;
8515 psli->sli_flag = 0;
8516 psli->fcp_ring = LPFC_FCP_RING;
8517 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008518 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008519
James Bottomley604a3e32005-10-29 10:28:33 -05008520 psli->iocbq_lookup = NULL;
8521 psli->iocbq_lookup_len = 0;
8522 psli->last_iotag = 0;
8523
dea31012005-04-17 16:05:31 -05008524 for (i = 0; i < psli->num_rings; i++) {
8525 pring = &psli->ring[i];
8526 switch (i) {
8527 case LPFC_FCP_RING: /* ring 0 - FCP */
8528 /* numCiocb and numRiocb are used in config_port */
8529 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8530 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8531 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8532 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8533 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8534 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008535 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008536 SLI3_IOCB_CMD_SIZE :
8537 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008538 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008539 SLI3_IOCB_RSP_SIZE :
8540 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008541 pring->iotag_ctr = 0;
8542 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008543 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008544 pring->fast_iotag = pring->iotag_max;
8545 pring->num_mask = 0;
8546 break;
James Smarta4bc3372006-12-02 13:34:16 -05008547 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008548 /* numCiocb and numRiocb are used in config_port */
8549 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8550 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008551 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008552 SLI3_IOCB_CMD_SIZE :
8553 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008554 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008555 SLI3_IOCB_RSP_SIZE :
8556 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008557 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008558 pring->num_mask = 0;
8559 break;
8560 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8561 /* numCiocb and numRiocb are used in config_port */
8562 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8563 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008564 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008565 SLI3_IOCB_CMD_SIZE :
8566 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008567 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008568 SLI3_IOCB_RSP_SIZE :
8569 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008570 pring->fast_iotag = 0;
8571 pring->iotag_ctr = 0;
8572 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008573 pring->lpfc_sli_rcv_async_status =
8574 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008575 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008576 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008577 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8578 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008579 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008580 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008581 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008582 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8583 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008584 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008585 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008586 pring->prt[2].profile = 0; /* Mask 2 */
8587 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008588 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008589 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008590 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008591 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008592 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008593 pring->prt[3].profile = 0; /* Mask 3 */
8594 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008595 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008596 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008597 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008598 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008599 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008600 /* abort unsolicited sequence */
8601 pring->prt[4].profile = 0; /* Mask 4 */
8602 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8603 pring->prt[4].type = FC_TYPE_BLS;
8604 pring->prt[4].lpfc_sli_rcv_unsol_event =
8605 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008606 break;
8607 }
James Smarted957682007-06-17 19:56:37 -05008608 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008609 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008610 }
James Smarted957682007-06-17 19:56:37 -05008611 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008612 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008613 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8614 "SLI2 SLIM Data: x%x x%lx\n",
8615 phba->brd_no, totiocbsize,
8616 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008617 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008618 if (phba->cfg_multi_ring_support == 2)
8619 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008620
8621 return 0;
8622}
8623
James Smarte59058c2008-08-24 21:49:00 -04008624/**
James Smart3621a712009-04-06 18:47:14 -04008625 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008626 * @phba: Pointer to HBA context object.
8627 *
8628 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8629 * ring. This function also initializes ring indices of each ring.
8630 * This function is called during the initialization of the SLI
8631 * interface of an HBA.
8632 * This function is called with no lock held and always returns
8633 * 1.
8634 **/
dea31012005-04-17 16:05:31 -05008635int
James Smart2e0fef82007-06-17 19:56:36 -05008636lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008637{
8638 struct lpfc_sli *psli;
8639 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008640 int i;
dea31012005-04-17 16:05:31 -05008641
8642 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008643 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008644 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008645 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008646 /* Initialize list headers for txq and txcmplq as double linked lists */
8647 for (i = 0; i < psli->num_rings; i++) {
8648 pring = &psli->ring[i];
8649 pring->ringno = i;
8650 pring->next_cmdidx = 0;
8651 pring->local_getidx = 0;
8652 pring->cmdidx = 0;
8653 INIT_LIST_HEAD(&pring->txq);
8654 INIT_LIST_HEAD(&pring->txcmplq);
8655 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008656 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008657 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008658 }
James Smart2e0fef82007-06-17 19:56:36 -05008659 spin_unlock_irq(&phba->hbalock);
8660 return 1;
dea31012005-04-17 16:05:31 -05008661}
8662
James Smarte59058c2008-08-24 21:49:00 -04008663/**
James Smart04c68492009-05-22 14:52:52 -04008664 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8665 * @phba: Pointer to HBA context object.
8666 *
8667 * This routine flushes the mailbox command subsystem. It will unconditionally
8668 * flush all the mailbox commands in the three possible stages in the mailbox
8669 * command sub-system: pending mailbox command queue; the outstanding mailbox
8670 * command; and completed mailbox command queue. It is caller's responsibility
8671 * to make sure that the driver is in the proper state to flush the mailbox
8672 * command sub-system. Namely, the posting of mailbox commands into the
8673 * pending mailbox command queue from the various clients must be stopped;
8674 * either the HBA is in a state that it will never works on the outstanding
8675 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8676 * mailbox command has been completed.
8677 **/
8678static void
8679lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8680{
8681 LIST_HEAD(completions);
8682 struct lpfc_sli *psli = &phba->sli;
8683 LPFC_MBOXQ_t *pmb;
8684 unsigned long iflag;
8685
8686 /* Flush all the mailbox commands in the mbox system */
8687 spin_lock_irqsave(&phba->hbalock, iflag);
8688 /* The pending mailbox command queue */
8689 list_splice_init(&phba->sli.mboxq, &completions);
8690 /* The outstanding active mailbox command */
8691 if (psli->mbox_active) {
8692 list_add_tail(&psli->mbox_active->list, &completions);
8693 psli->mbox_active = NULL;
8694 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8695 }
8696 /* The completed mailbox command queue */
8697 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8698 spin_unlock_irqrestore(&phba->hbalock, iflag);
8699
8700 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8701 while (!list_empty(&completions)) {
8702 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8703 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8704 if (pmb->mbox_cmpl)
8705 pmb->mbox_cmpl(phba, pmb);
8706 }
8707}
8708
8709/**
James Smart3621a712009-04-06 18:47:14 -04008710 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008711 * @vport: Pointer to virtual port object.
8712 *
8713 * lpfc_sli_host_down is called to clean up the resources
8714 * associated with a vport before destroying virtual
8715 * port data structures.
8716 * This function does following operations:
8717 * - Free discovery resources associated with this virtual
8718 * port.
8719 * - Free iocbs associated with this virtual port in
8720 * the txq.
8721 * - Send abort for all iocb commands associated with this
8722 * vport in txcmplq.
8723 *
8724 * This function is called with no lock held and always returns 1.
8725 **/
dea31012005-04-17 16:05:31 -05008726int
James Smart92d7f7b2007-06-17 19:56:38 -05008727lpfc_sli_host_down(struct lpfc_vport *vport)
8728{
James Smart858c9f62007-06-17 19:56:39 -05008729 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008730 struct lpfc_hba *phba = vport->phba;
8731 struct lpfc_sli *psli = &phba->sli;
8732 struct lpfc_sli_ring *pring;
8733 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008734 int i;
8735 unsigned long flags = 0;
8736 uint16_t prev_pring_flag;
8737
8738 lpfc_cleanup_discovery_resources(vport);
8739
8740 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008741 for (i = 0; i < psli->num_rings; i++) {
8742 pring = &psli->ring[i];
8743 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008744 /* Only slow rings */
8745 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008746 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008747 /* Set the lpfc data pending flag */
8748 set_bit(LPFC_DATA_READY, &phba->data_flags);
8749 }
James Smart92d7f7b2007-06-17 19:56:38 -05008750 /*
8751 * Error everything on the txq since these iocbs have not been
8752 * given to the FW yet.
8753 */
James Smart92d7f7b2007-06-17 19:56:38 -05008754 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8755 if (iocb->vport != vport)
8756 continue;
James Smart858c9f62007-06-17 19:56:39 -05008757 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008758 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008759 }
8760
8761 /* Next issue ABTS for everything on the txcmplq */
8762 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8763 list) {
8764 if (iocb->vport != vport)
8765 continue;
8766 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8767 }
8768
8769 pring->flag = prev_pring_flag;
8770 }
8771
8772 spin_unlock_irqrestore(&phba->hbalock, flags);
8773
James Smarta257bf92009-04-06 18:48:10 -04008774 /* Cancel all the IOCBs from the completions list */
8775 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8776 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008777 return 1;
8778}
8779
James Smarte59058c2008-08-24 21:49:00 -04008780/**
James Smart3621a712009-04-06 18:47:14 -04008781 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008782 * @phba: Pointer to HBA context object.
8783 *
8784 * This function cleans up all iocb, buffers, mailbox commands
8785 * while shutting down the HBA. This function is called with no
8786 * lock held and always returns 1.
8787 * This function does the following to cleanup driver resources:
8788 * - Free discovery resources for each virtual port
8789 * - Cleanup any pending fabric iocbs
8790 * - Iterate through the iocb txq and free each entry
8791 * in the list.
8792 * - Free up any buffer posted to the HBA
8793 * - Free mailbox commands in the mailbox queue.
8794 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008795int
James Smart2e0fef82007-06-17 19:56:36 -05008796lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008797{
James Smart2534ba72007-04-25 09:52:20 -04008798 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008799 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008800 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008801 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008802 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008803 int i;
8804
8805 /* Shutdown the mailbox command sub-system */
8806 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008807
dea31012005-04-17 16:05:31 -05008808 lpfc_hba_down_prep(phba);
8809
James Smart92d7f7b2007-06-17 19:56:38 -05008810 lpfc_fabric_abort_hba(phba);
8811
James Smart2e0fef82007-06-17 19:56:36 -05008812 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008813 for (i = 0; i < psli->num_rings; i++) {
8814 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008815 /* Only slow rings */
8816 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008817 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008818 /* Set the lpfc data pending flag */
8819 set_bit(LPFC_DATA_READY, &phba->data_flags);
8820 }
dea31012005-04-17 16:05:31 -05008821
8822 /*
8823 * Error everything on the txq since these iocbs have not been
8824 * given to the FW yet.
8825 */
James Smart2534ba72007-04-25 09:52:20 -04008826 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008827 pring->txq_cnt = 0;
8828
dea31012005-04-17 16:05:31 -05008829 }
James Smart2e0fef82007-06-17 19:56:36 -05008830 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008831
James Smarta257bf92009-04-06 18:48:10 -04008832 /* Cancel all the IOCBs from the completions list */
8833 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8834 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008835
James Smart0ff10d42008-01-11 01:52:36 -05008836 spin_lock_irqsave(&phba->hbalock, flags);
8837 list_splice_init(&phba->elsbuf, &completions);
8838 phba->elsbuf_cnt = 0;
8839 phba->elsbuf_prev_cnt = 0;
8840 spin_unlock_irqrestore(&phba->hbalock, flags);
8841
8842 while (!list_empty(&completions)) {
8843 list_remove_head(&completions, buf_ptr,
8844 struct lpfc_dmabuf, list);
8845 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8846 kfree(buf_ptr);
8847 }
8848
dea31012005-04-17 16:05:31 -05008849 /* Return any active mbox cmds */
8850 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008851
James Smartda0436e2009-05-22 14:51:39 -04008852 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008853 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008854 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008855
James Smartda0436e2009-05-22 14:51:39 -04008856 return 1;
8857}
James Smart92d7f7b2007-06-17 19:56:38 -05008858
James Smartda0436e2009-05-22 14:51:39 -04008859/**
James Smart3621a712009-04-06 18:47:14 -04008860 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008861 * @srcp: Source memory pointer.
8862 * @destp: Destination memory pointer.
8863 * @cnt: Number of words required to be copied.
8864 *
8865 * This function is used for copying data between driver memory
8866 * and the SLI memory. This function also changes the endianness
8867 * of each word if native endianness is different from SLI
8868 * endianness. This function can be called with or without
8869 * lock.
8870 **/
dea31012005-04-17 16:05:31 -05008871void
8872lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8873{
8874 uint32_t *src = srcp;
8875 uint32_t *dest = destp;
8876 uint32_t ldata;
8877 int i;
8878
8879 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8880 ldata = *src;
8881 ldata = le32_to_cpu(ldata);
8882 *dest = ldata;
8883 src++;
8884 dest++;
8885 }
8886}
8887
James Smarte59058c2008-08-24 21:49:00 -04008888
8889/**
James Smarta0c87cb2009-07-19 10:01:10 -04008890 * lpfc_sli_bemem_bcopy - SLI memory copy function
8891 * @srcp: Source memory pointer.
8892 * @destp: Destination memory pointer.
8893 * @cnt: Number of words required to be copied.
8894 *
8895 * This function is used for copying data between a data structure
8896 * with big endian representation to local endianness.
8897 * This function can be called with or without lock.
8898 **/
8899void
8900lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8901{
8902 uint32_t *src = srcp;
8903 uint32_t *dest = destp;
8904 uint32_t ldata;
8905 int i;
8906
8907 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8908 ldata = *src;
8909 ldata = be32_to_cpu(ldata);
8910 *dest = ldata;
8911 src++;
8912 dest++;
8913 }
8914}
8915
8916/**
James Smart3621a712009-04-06 18:47:14 -04008917 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008918 * @phba: Pointer to HBA context object.
8919 * @pring: Pointer to driver SLI ring object.
8920 * @mp: Pointer to driver buffer object.
8921 *
8922 * This function is called with no lock held.
8923 * It always return zero after adding the buffer to the postbufq
8924 * buffer list.
8925 **/
dea31012005-04-17 16:05:31 -05008926int
James Smart2e0fef82007-06-17 19:56:36 -05008927lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8928 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008929{
8930 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8931 later */
James Smart2e0fef82007-06-17 19:56:36 -05008932 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008933 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008934 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008935 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008936 return 0;
8937}
8938
James Smarte59058c2008-08-24 21:49:00 -04008939/**
James Smart3621a712009-04-06 18:47:14 -04008940 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008941 * @phba: Pointer to HBA context object.
8942 *
8943 * When HBQ is enabled, buffers are searched based on tags. This function
8944 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8945 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8946 * does not conflict with tags of buffer posted for unsolicited events.
8947 * The function returns the allocated tag. The function is called with
8948 * no locks held.
8949 **/
James Smart76bb24e2007-10-27 13:38:00 -04008950uint32_t
8951lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8952{
8953 spin_lock_irq(&phba->hbalock);
8954 phba->buffer_tag_count++;
8955 /*
8956 * Always set the QUE_BUFTAG_BIT to distiguish between
8957 * a tag assigned by HBQ.
8958 */
8959 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8960 spin_unlock_irq(&phba->hbalock);
8961 return phba->buffer_tag_count;
8962}
8963
James Smarte59058c2008-08-24 21:49:00 -04008964/**
James Smart3621a712009-04-06 18:47:14 -04008965 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008966 * @phba: Pointer to HBA context object.
8967 * @pring: Pointer to driver SLI ring object.
8968 * @tag: Buffer tag.
8969 *
8970 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8971 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8972 * iocb is posted to the response ring with the tag of the buffer.
8973 * This function searches the pring->postbufq list using the tag
8974 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8975 * iocb. If the buffer is found then lpfc_dmabuf object of the
8976 * buffer is returned to the caller else NULL is returned.
8977 * This function is called with no lock held.
8978 **/
James Smart76bb24e2007-10-27 13:38:00 -04008979struct lpfc_dmabuf *
8980lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8981 uint32_t tag)
8982{
8983 struct lpfc_dmabuf *mp, *next_mp;
8984 struct list_head *slp = &pring->postbufq;
8985
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008986 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008987 spin_lock_irq(&phba->hbalock);
8988 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8989 if (mp->buffer_tag == tag) {
8990 list_del_init(&mp->list);
8991 pring->postbufq_cnt--;
8992 spin_unlock_irq(&phba->hbalock);
8993 return mp;
8994 }
8995 }
8996
8997 spin_unlock_irq(&phba->hbalock);
8998 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04008999 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009000 "ring %d Data x%lx x%p x%p x%x\n",
9001 pring->ringno, (unsigned long) tag,
9002 slp->next, slp->prev, pring->postbufq_cnt);
9003
9004 return NULL;
9005}
dea31012005-04-17 16:05:31 -05009006
James Smarte59058c2008-08-24 21:49:00 -04009007/**
James Smart3621a712009-04-06 18:47:14 -04009008 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009009 * @phba: Pointer to HBA context object.
9010 * @pring: Pointer to driver SLI ring object.
9011 * @phys: DMA address of the buffer.
9012 *
9013 * This function searches the buffer list using the dma_address
9014 * of unsolicited event to find the driver's lpfc_dmabuf object
9015 * corresponding to the dma_address. The function returns the
9016 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9017 * This function is called by the ct and els unsolicited event
9018 * handlers to get the buffer associated with the unsolicited
9019 * event.
9020 *
9021 * This function is called with no lock held.
9022 **/
dea31012005-04-17 16:05:31 -05009023struct lpfc_dmabuf *
9024lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9025 dma_addr_t phys)
9026{
9027 struct lpfc_dmabuf *mp, *next_mp;
9028 struct list_head *slp = &pring->postbufq;
9029
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009030 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009031 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009032 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9033 if (mp->phys == phys) {
9034 list_del_init(&mp->list);
9035 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009036 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009037 return mp;
9038 }
9039 }
9040
James Smart2e0fef82007-06-17 19:56:36 -05009041 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009043 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009044 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009045 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009046 slp->next, slp->prev, pring->postbufq_cnt);
9047 return NULL;
9048}
9049
James Smarte59058c2008-08-24 21:49:00 -04009050/**
James Smart3621a712009-04-06 18:47:14 -04009051 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009052 * @phba: Pointer to HBA context object.
9053 * @cmdiocb: Pointer to driver command iocb object.
9054 * @rspiocb: Pointer to driver response iocb object.
9055 *
9056 * This function is the completion handler for the abort iocbs for
9057 * ELS commands. This function is called from the ELS ring event
9058 * handler with no lock held. This function frees memory resources
9059 * associated with the abort iocb.
9060 **/
dea31012005-04-17 16:05:31 -05009061static void
James Smart2e0fef82007-06-17 19:56:36 -05009062lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9063 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009064{
James Smart2e0fef82007-06-17 19:56:36 -05009065 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009066 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009067 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009068
9069 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009070
9071 /*
9072 * Assume that the port already completed and returned, or
9073 * will return the iocb. Just Log the message.
9074 */
James Smart2680eea2007-04-25 09:52:55 -04009075 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9076 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9077
James Smart2e0fef82007-06-17 19:56:36 -05009078 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009079 if (phba->sli_rev < LPFC_SLI_REV4) {
9080 if (abort_iotag != 0 &&
9081 abort_iotag <= phba->sli.last_iotag)
9082 abort_iocb =
9083 phba->sli.iocbq_lookup[abort_iotag];
9084 } else
9085 /* For sli4 the abort_tag is the XRI,
9086 * so the abort routine puts the iotag of the iocb
9087 * being aborted in the context field of the abort
9088 * IOCB.
9089 */
9090 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009091
James Smart2a9bf3d2010-06-07 15:24:45 -04009092 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9093 "0327 Cannot abort els iocb %p "
9094 "with tag %x context %x, abort status %x, "
9095 "abort code %x\n",
9096 abort_iocb, abort_iotag, abort_context,
9097 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009098
James Smartff78d8f2011-12-13 13:21:35 -05009099 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009100 }
James Bottomley604a3e32005-10-29 10:28:33 -05009101 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009102 return;
9103}
9104
James Smarte59058c2008-08-24 21:49:00 -04009105/**
James Smart3621a712009-04-06 18:47:14 -04009106 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009107 * @phba: Pointer to HBA context object.
9108 * @cmdiocb: Pointer to driver command iocb object.
9109 * @rspiocb: Pointer to driver response iocb object.
9110 *
9111 * The function is called from SLI ring event handler with no
9112 * lock held. This function is the completion handler for ELS commands
9113 * which are aborted. The function frees memory resources used for
9114 * the aborted ELS commands.
9115 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009116static void
9117lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9118 struct lpfc_iocbq *rspiocb)
9119{
9120 IOCB_t *irsp = &rspiocb->iocb;
9121
9122 /* ELS cmd tag <ulpIoTag> completes */
9123 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009124 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009125 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009126 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009127 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009128 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9129 lpfc_ct_free_iocb(phba, cmdiocb);
9130 else
9131 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009132 return;
9133}
9134
James Smarte59058c2008-08-24 21:49:00 -04009135/**
James Smart5af5eee2010-10-22 11:06:38 -04009136 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009137 * @phba: Pointer to HBA context object.
9138 * @pring: Pointer to driver SLI ring object.
9139 * @cmdiocb: Pointer to driver command iocb object.
9140 *
James Smart5af5eee2010-10-22 11:06:38 -04009141 * This function issues an abort iocb for the provided command iocb down to
9142 * the port. Other than the case the outstanding command iocb is an abort
9143 * request, this function issues abort out unconditionally. This function is
9144 * called with hbalock held. The function returns 0 when it fails due to
9145 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009146 **/
James Smart5af5eee2010-10-22 11:06:38 -04009147static int
9148lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009149 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009150{
James Smart2e0fef82007-06-17 19:56:36 -05009151 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009152 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009153 IOCB_t *icmd = NULL;
9154 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009155 int retval;
James Smart07951072007-04-25 09:51:38 -04009156
James Smart92d7f7b2007-06-17 19:56:38 -05009157 /*
9158 * There are certain command types we don't want to abort. And we
9159 * don't want to abort commands that are already in the process of
9160 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009161 */
9162 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009163 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009164 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9165 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009166 return 0;
9167
dea31012005-04-17 16:05:31 -05009168 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009169 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009170 if (abtsiocbp == NULL)
9171 return 0;
dea31012005-04-17 16:05:31 -05009172
James Smart07951072007-04-25 09:51:38 -04009173 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009174 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009175 */
9176 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9177
dea31012005-04-17 16:05:31 -05009178 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009179 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9180 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009181 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009182 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009183 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9184 }
James Smartda0436e2009-05-22 14:51:39 -04009185 else
9186 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009187 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009188 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009189
James Smart5ffc2662009-11-18 15:39:44 -05009190 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9191 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009192 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9193 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009194
James Smart2e0fef82007-06-17 19:56:36 -05009195 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009196 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9197 else
9198 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9199
9200 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009201
James Smarte8b62012007-08-02 11:10:09 -04009202 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9203 "0339 Abort xri x%x, original iotag x%x, "
9204 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009205 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009206 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009207 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04009208 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04009209
James Smartd7c255b2008-08-24 21:50:00 -04009210 if (retval)
9211 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009212
9213 /*
9214 * Caller to this routine should check for IOCB_ERROR
9215 * and handle it properly. This routine no longer removes
9216 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9217 */
9218 return retval;
9219}
9220
9221/**
9222 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9223 * @phba: Pointer to HBA context object.
9224 * @pring: Pointer to driver SLI ring object.
9225 * @cmdiocb: Pointer to driver command iocb object.
9226 *
9227 * This function issues an abort iocb for the provided command iocb. In case
9228 * of unloading, the abort iocb will not be issued to commands on the ELS
9229 * ring. Instead, the callback function shall be changed to those commands
9230 * so that nothing happens when them finishes. This function is called with
9231 * hbalock held. The function returns 0 when the command iocb is an abort
9232 * request.
9233 **/
9234int
9235lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9236 struct lpfc_iocbq *cmdiocb)
9237{
9238 struct lpfc_vport *vport = cmdiocb->vport;
9239 int retval = IOCB_ERROR;
9240 IOCB_t *icmd = NULL;
9241
9242 /*
9243 * There are certain command types we don't want to abort. And we
9244 * don't want to abort commands that are already in the process of
9245 * being aborted.
9246 */
9247 icmd = &cmdiocb->iocb;
9248 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9249 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9250 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9251 return 0;
9252
9253 /*
9254 * If we're unloading, don't abort iocb on the ELS ring, but change
9255 * the callback so that nothing happens when it finishes.
9256 */
9257 if ((vport->load_flag & FC_UNLOADING) &&
9258 (pring->ringno == LPFC_ELS_RING)) {
9259 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9260 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9261 else
9262 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9263 goto abort_iotag_exit;
9264 }
9265
9266 /* Now, we try to issue the abort to the cmdiocb out */
9267 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9268
James Smart07951072007-04-25 09:51:38 -04009269abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009270 /*
9271 * Caller to this routine should check for IOCB_ERROR
9272 * and handle it properly. This routine no longer removes
9273 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009274 */
James Smart2e0fef82007-06-17 19:56:36 -05009275 return retval;
dea31012005-04-17 16:05:31 -05009276}
9277
James Smarte59058c2008-08-24 21:49:00 -04009278/**
James Smart5af5eee2010-10-22 11:06:38 -04009279 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9280 * @phba: Pointer to HBA context object.
9281 * @pring: Pointer to driver SLI ring object.
9282 *
9283 * This function aborts all iocbs in the given ring and frees all the iocb
9284 * objects in txq. This function issues abort iocbs unconditionally for all
9285 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9286 * to complete before the return of this function. The caller is not required
9287 * to hold any locks.
9288 **/
9289static void
9290lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9291{
9292 LIST_HEAD(completions);
9293 struct lpfc_iocbq *iocb, *next_iocb;
9294
9295 if (pring->ringno == LPFC_ELS_RING)
9296 lpfc_fabric_abort_hba(phba);
9297
9298 spin_lock_irq(&phba->hbalock);
9299
9300 /* Take off all the iocbs on txq for cancelling */
9301 list_splice_init(&pring->txq, &completions);
9302 pring->txq_cnt = 0;
9303
9304 /* Next issue ABTS for everything on the txcmplq */
9305 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9306 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9307
9308 spin_unlock_irq(&phba->hbalock);
9309
9310 /* Cancel all the IOCBs from the completions list */
9311 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9312 IOERR_SLI_ABORTED);
9313}
9314
9315/**
9316 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9317 * @phba: pointer to lpfc HBA data structure.
9318 *
9319 * This routine will abort all pending and outstanding iocbs to an HBA.
9320 **/
9321void
9322lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9323{
9324 struct lpfc_sli *psli = &phba->sli;
9325 struct lpfc_sli_ring *pring;
9326 int i;
9327
9328 for (i = 0; i < psli->num_rings; i++) {
9329 pring = &psli->ring[i];
9330 lpfc_sli_iocb_ring_abort(phba, pring);
9331 }
9332}
9333
9334/**
James Smart3621a712009-04-06 18:47:14 -04009335 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009336 * @iocbq: Pointer to driver iocb object.
9337 * @vport: Pointer to driver virtual port object.
9338 * @tgt_id: SCSI ID of the target.
9339 * @lun_id: LUN ID of the scsi device.
9340 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9341 *
James Smart3621a712009-04-06 18:47:14 -04009342 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009343 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9344 * 0 if the filtering criteria is met for the given iocb and will return
9345 * 1 if the filtering criteria is not met.
9346 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9347 * given iocb is for the SCSI device specified by vport, tgt_id and
9348 * lun_id parameter.
9349 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9350 * given iocb is for the SCSI target specified by vport and tgt_id
9351 * parameters.
9352 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9353 * given iocb is for the SCSI host associated with the given vport.
9354 * This function is called with no locks held.
9355 **/
dea31012005-04-17 16:05:31 -05009356static int
James Smart51ef4c22007-08-02 11:10:31 -04009357lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9358 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009359 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009360{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009361 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009362 int rc = 1;
9363
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009364 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9365 return rc;
9366
James Smart51ef4c22007-08-02 11:10:31 -04009367 if (iocbq->vport != vport)
9368 return rc;
9369
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009370 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009371
James Smart495a7142008-06-14 22:52:59 -04009372 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009373 return rc;
9374
9375 switch (ctx_cmd) {
9376 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009377 if ((lpfc_cmd->rdata->pnode) &&
9378 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9379 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009380 rc = 0;
9381 break;
9382 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009383 if ((lpfc_cmd->rdata->pnode) &&
9384 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009385 rc = 0;
9386 break;
dea31012005-04-17 16:05:31 -05009387 case LPFC_CTX_HOST:
9388 rc = 0;
9389 break;
9390 default:
9391 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009392 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009393 break;
9394 }
9395
9396 return rc;
9397}
9398
James Smarte59058c2008-08-24 21:49:00 -04009399/**
James Smart3621a712009-04-06 18:47:14 -04009400 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009401 * @vport: Pointer to virtual port.
9402 * @tgt_id: SCSI ID of the target.
9403 * @lun_id: LUN ID of the scsi device.
9404 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9405 *
9406 * This function returns number of FCP commands pending for the vport.
9407 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9408 * commands pending on the vport associated with SCSI device specified
9409 * by tgt_id and lun_id parameters.
9410 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9411 * commands pending on the vport associated with SCSI target specified
9412 * by tgt_id parameter.
9413 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9414 * commands pending on the vport.
9415 * This function returns the number of iocbs which satisfy the filter.
9416 * This function is called without any lock held.
9417 **/
dea31012005-04-17 16:05:31 -05009418int
James Smart51ef4c22007-08-02 11:10:31 -04009419lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9420 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009421{
James Smart51ef4c22007-08-02 11:10:31 -04009422 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009423 struct lpfc_iocbq *iocbq;
9424 int sum, i;
dea31012005-04-17 16:05:31 -05009425
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009426 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9427 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009428
James Smart51ef4c22007-08-02 11:10:31 -04009429 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9430 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009431 sum++;
dea31012005-04-17 16:05:31 -05009432 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009433
dea31012005-04-17 16:05:31 -05009434 return sum;
9435}
9436
James Smarte59058c2008-08-24 21:49:00 -04009437/**
James Smart3621a712009-04-06 18:47:14 -04009438 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009439 * @phba: Pointer to HBA context object
9440 * @cmdiocb: Pointer to command iocb object.
9441 * @rspiocb: Pointer to response iocb object.
9442 *
9443 * This function is called when an aborted FCP iocb completes. This
9444 * function is called by the ring event handler with no lock held.
9445 * This function frees the iocb.
9446 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009447void
James Smart2e0fef82007-06-17 19:56:36 -05009448lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9449 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009450{
James Smartcb69f7d2011-12-13 13:21:57 -05009451 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9452 "3096 ABORT_XRI_CN completing on xri x%x "
9453 "original iotag x%x, abort cmd iotag x%x "
9454 "status 0x%x, reason 0x%x\n",
9455 cmdiocb->iocb.un.acxri.abortContextTag,
9456 cmdiocb->iocb.un.acxri.abortIoTag,
9457 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9458 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009459 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009460 return;
9461}
9462
James Smarte59058c2008-08-24 21:49:00 -04009463/**
James Smart3621a712009-04-06 18:47:14 -04009464 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009465 * @vport: Pointer to virtual port.
9466 * @pring: Pointer to driver SLI ring object.
9467 * @tgt_id: SCSI ID of the target.
9468 * @lun_id: LUN ID of the scsi device.
9469 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9470 *
9471 * This function sends an abort command for every SCSI command
9472 * associated with the given virtual port pending on the ring
9473 * filtered by lpfc_sli_validate_fcp_iocb function.
9474 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9475 * FCP iocbs associated with lun specified by tgt_id and lun_id
9476 * parameters
9477 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9478 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9479 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9480 * FCP iocbs associated with virtual port.
9481 * This function returns number of iocbs it failed to abort.
9482 * This function is called with no locks held.
9483 **/
dea31012005-04-17 16:05:31 -05009484int
James Smart51ef4c22007-08-02 11:10:31 -04009485lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9486 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009487{
James Smart51ef4c22007-08-02 11:10:31 -04009488 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009489 struct lpfc_iocbq *iocbq;
9490 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009491 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009492 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009493 int i;
dea31012005-04-17 16:05:31 -05009494
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009495 for (i = 1; i <= phba->sli.last_iotag; i++) {
9496 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009497
James Smart51ef4c22007-08-02 11:10:31 -04009498 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009499 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009500 continue;
9501
9502 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009503 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009504 if (abtsiocb == NULL) {
9505 errcnt++;
9506 continue;
9507 }
dea31012005-04-17 16:05:31 -05009508
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009509 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009510 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9511 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009512 if (phba->sli_rev == LPFC_SLI_REV4)
9513 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9514 else
9515 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009516 abtsiocb->iocb.ulpLe = 1;
9517 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009518 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009519
James Smart5ffc2662009-11-18 15:39:44 -05009520 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9521 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009522 if (iocbq->iocb_flag & LPFC_IO_FCP)
9523 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009524
James Smart2e0fef82007-06-17 19:56:36 -05009525 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009526 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9527 else
9528 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9529
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009530 /* Setup callback routine and issue the command. */
9531 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009532 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9533 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009534 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009535 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009536 errcnt++;
9537 continue;
9538 }
9539 }
9540
9541 return errcnt;
9542}
9543
James Smarte59058c2008-08-24 21:49:00 -04009544/**
James Smart3621a712009-04-06 18:47:14 -04009545 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009546 * @phba: Pointer to HBA context object.
9547 * @cmdiocbq: Pointer to command iocb.
9548 * @rspiocbq: Pointer to response iocb.
9549 *
9550 * This function is the completion handler for iocbs issued using
9551 * lpfc_sli_issue_iocb_wait function. This function is called by the
9552 * ring event handler function without any lock held. This function
9553 * can be called from both worker thread context and interrupt
9554 * context. This function also can be called from other thread which
9555 * cleans up the SLI layer objects.
9556 * This function copy the contents of the response iocb to the
9557 * response iocb memory object provided by the caller of
9558 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9559 * sleeps for the iocb completion.
9560 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009561static void
9562lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9563 struct lpfc_iocbq *cmdiocbq,
9564 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009565{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009566 wait_queue_head_t *pdone_q;
9567 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009568 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009569
James Smart2e0fef82007-06-17 19:56:36 -05009570 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009571 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9572 if (cmdiocbq->context2 && rspiocbq)
9573 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9574 &rspiocbq->iocb, sizeof(IOCB_t));
9575
James Smart0f65ff62010-02-26 14:14:23 -05009576 /* Set the exchange busy flag for task management commands */
9577 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9578 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9579 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9580 cur_iocbq);
9581 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9582 }
9583
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009584 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009585 if (pdone_q)
9586 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009587 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009588 return;
9589}
9590
James Smarte59058c2008-08-24 21:49:00 -04009591/**
James Smartd11e31d2009-06-10 17:23:06 -04009592 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9593 * @phba: Pointer to HBA context object..
9594 * @piocbq: Pointer to command iocb.
9595 * @flag: Flag to test.
9596 *
9597 * This routine grabs the hbalock and then test the iocb_flag to
9598 * see if the passed in flag is set.
9599 * Returns:
9600 * 1 if flag is set.
9601 * 0 if flag is not set.
9602 **/
9603static int
9604lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9605 struct lpfc_iocbq *piocbq, uint32_t flag)
9606{
9607 unsigned long iflags;
9608 int ret;
9609
9610 spin_lock_irqsave(&phba->hbalock, iflags);
9611 ret = piocbq->iocb_flag & flag;
9612 spin_unlock_irqrestore(&phba->hbalock, iflags);
9613 return ret;
9614
9615}
9616
9617/**
James Smart3621a712009-04-06 18:47:14 -04009618 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009619 * @phba: Pointer to HBA context object..
9620 * @pring: Pointer to sli ring.
9621 * @piocb: Pointer to command iocb.
9622 * @prspiocbq: Pointer to response iocb.
9623 * @timeout: Timeout in number of seconds.
9624 *
9625 * This function issues the iocb to firmware and waits for the
9626 * iocb to complete. If the iocb command is not
9627 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9628 * Caller should not free the iocb resources if this function
9629 * returns IOCB_TIMEDOUT.
9630 * The function waits for the iocb completion using an
9631 * non-interruptible wait.
9632 * This function will sleep while waiting for iocb completion.
9633 * So, this function should not be called from any context which
9634 * does not allow sleeping. Due to the same reason, this function
9635 * cannot be called with interrupt disabled.
9636 * This function assumes that the iocb completions occur while
9637 * this function sleep. So, this function cannot be called from
9638 * the thread which process iocb completion for this ring.
9639 * This function clears the iocb_flag of the iocb object before
9640 * issuing the iocb and the iocb completion handler sets this
9641 * flag and wakes this thread when the iocb completes.
9642 * The contents of the response iocb will be copied to prspiocbq
9643 * by the completion handler when the command completes.
9644 * This function returns IOCB_SUCCESS when success.
9645 * This function is called with no lock held.
9646 **/
dea31012005-04-17 16:05:31 -05009647int
James Smart2e0fef82007-06-17 19:56:36 -05009648lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009649 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009650 struct lpfc_iocbq *piocb,
9651 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009652 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009653{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009654 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009655 long timeleft, timeout_req = 0;
9656 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009657 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009658 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009659 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009660 * If the caller has provided a response iocbq buffer, then context2
9661 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009662 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009663 if (prspiocbq) {
9664 if (piocb->context2)
9665 return IOCB_ERROR;
9666 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009667 }
9668
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009669 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9670 piocb->context_un.wait_queue = &done_q;
9671 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009672
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009673 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009674 if (lpfc_readl(phba->HCregaddr, &creg_val))
9675 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009676 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9677 writel(creg_val, phba->HCregaddr);
9678 readl(phba->HCregaddr); /* flush */
9679 }
9680
James Smart2a9bf3d2010-06-07 15:24:45 -04009681 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9682 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009683 if (retval == IOCB_SUCCESS) {
9684 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009685 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009686 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009687 timeout_req);
dea31012005-04-17 16:05:31 -05009688
James Smart7054a602007-04-25 09:52:34 -04009689 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9690 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009691 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009692 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009693 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009694 "0338 IOCB wait timeout error - no "
9695 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009696 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009697 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009698 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009699 "0330 IOCB wake NOT set, "
9700 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009701 timeout, (timeleft / jiffies));
9702 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009703 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009704 } else if (retval == IOCB_BUSY) {
9705 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9706 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9707 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9708 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009709 } else {
9710 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009711 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009712 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009713 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009714 }
9715
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009716 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009717 if (lpfc_readl(phba->HCregaddr, &creg_val))
9718 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009719 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9720 writel(creg_val, phba->HCregaddr);
9721 readl(phba->HCregaddr); /* flush */
9722 }
9723
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009724 if (prspiocbq)
9725 piocb->context2 = NULL;
9726
9727 piocb->context_un.wait_queue = NULL;
9728 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009729 return retval;
9730}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009731
James Smarte59058c2008-08-24 21:49:00 -04009732/**
James Smart3621a712009-04-06 18:47:14 -04009733 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009734 * @phba: Pointer to HBA context object.
9735 * @pmboxq: Pointer to driver mailbox object.
9736 * @timeout: Timeout in number of seconds.
9737 *
9738 * This function issues the mailbox to firmware and waits for the
9739 * mailbox command to complete. If the mailbox command is not
9740 * completed within timeout seconds, it returns MBX_TIMEOUT.
9741 * The function waits for the mailbox completion using an
9742 * interruptible wait. If the thread is woken up due to a
9743 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9744 * should not free the mailbox resources, if this function returns
9745 * MBX_TIMEOUT.
9746 * This function will sleep while waiting for mailbox completion.
9747 * So, this function should not be called from any context which
9748 * does not allow sleeping. Due to the same reason, this function
9749 * cannot be called with interrupt disabled.
9750 * This function assumes that the mailbox completion occurs while
9751 * this function sleep. So, this function cannot be called from
9752 * the worker thread which processes mailbox completion.
9753 * This function is called in the context of HBA management
9754 * applications.
9755 * This function returns MBX_SUCCESS when successful.
9756 * This function is called with no lock held.
9757 **/
dea31012005-04-17 16:05:31 -05009758int
James Smart2e0fef82007-06-17 19:56:36 -05009759lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009760 uint32_t timeout)
9761{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009762 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009763 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009764 unsigned long flag;
dea31012005-04-17 16:05:31 -05009765
9766 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009767 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009768 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009769
James Smart495a7142008-06-14 22:52:59 -04009770 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009771 /* setup wake call as IOCB callback */
9772 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9773 /* setup context field to pass wait_queue pointer to wake function */
9774 pmboxq->context1 = &done_q;
9775
dea31012005-04-17 16:05:31 -05009776 /* now issue the command */
9777 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009778 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009779 wait_event_interruptible_timeout(done_q,
9780 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9781 timeout * HZ);
9782
James Smart858c9f62007-06-17 19:56:39 -05009783 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009784 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009785 /*
9786 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9787 * else do not free the resources.
9788 */
James Smartd7c47992010-06-08 18:31:54 -04009789 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009790 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009791 lpfc_sli4_swap_str(phba, pmboxq);
9792 } else {
James Smart7054a602007-04-25 09:52:34 -04009793 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009794 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9795 }
9796 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009797 }
9798
dea31012005-04-17 16:05:31 -05009799 return retval;
9800}
9801
James Smarte59058c2008-08-24 21:49:00 -04009802/**
James Smart3772a992009-05-22 14:50:54 -04009803 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009804 * @phba: Pointer to HBA context.
9805 *
James Smart3772a992009-05-22 14:50:54 -04009806 * This function is called to shutdown the driver's mailbox sub-system.
9807 * It first marks the mailbox sub-system is in a block state to prevent
9808 * the asynchronous mailbox command from issued off the pending mailbox
9809 * command queue. If the mailbox command sub-system shutdown is due to
9810 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9811 * the mailbox sub-system flush routine to forcefully bring down the
9812 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9813 * as with offline or HBA function reset), this routine will wait for the
9814 * outstanding mailbox command to complete before invoking the mailbox
9815 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009816 **/
James Smart3772a992009-05-22 14:50:54 -04009817void
9818lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009819{
James Smart3772a992009-05-22 14:50:54 -04009820 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -04009821 unsigned long timeout;
9822
James Smarta183a152011-10-10 21:32:43 -04009823 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smart3772a992009-05-22 14:50:54 -04009824 spin_lock_irq(&phba->hbalock);
9825 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9826 spin_unlock_irq(&phba->hbalock);
9827
9828 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9829 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009830 /* Determine how long we might wait for the active mailbox
9831 * command to be gracefully completed by firmware.
9832 */
James Smarta183a152011-10-10 21:32:43 -04009833 if (phba->sli.mbox_active)
9834 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9835 phba->sli.mbox_active) *
9836 1000) + jiffies;
9837 spin_unlock_irq(&phba->hbalock);
9838
James Smart3772a992009-05-22 14:50:54 -04009839 while (phba->sli.mbox_active) {
9840 /* Check active mailbox complete status every 2ms */
9841 msleep(2);
9842 if (time_after(jiffies, timeout))
9843 /* Timeout, let the mailbox flush routine to
9844 * forcefully release active mailbox command
9845 */
9846 break;
9847 }
9848 }
9849 lpfc_sli_mbox_sys_flush(phba);
9850}
9851
9852/**
9853 * lpfc_sli_eratt_read - read sli-3 error attention events
9854 * @phba: Pointer to HBA context.
9855 *
9856 * This function is called to read the SLI3 device error attention registers
9857 * for possible error attention events. The caller must hold the hostlock
9858 * with spin_lock_irq().
9859 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009860 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009861 * Register and returns 0 otherwise.
9862 **/
9863static int
9864lpfc_sli_eratt_read(struct lpfc_hba *phba)
9865{
James Smarted957682007-06-17 19:56:37 -05009866 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009867
James Smart3772a992009-05-22 14:50:54 -04009868 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009869 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9870 goto unplug_err;
9871
James Smart3772a992009-05-22 14:50:54 -04009872 if (ha_copy & HA_ERATT) {
9873 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009874 if (lpfc_sli_read_hs(phba))
9875 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009876
James Smart3772a992009-05-22 14:50:54 -04009877 /* Check if there is a deferred error condition is active */
9878 if ((HS_FFER1 & phba->work_hs) &&
9879 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009880 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009881 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009882 /* Clear all interrupt enable conditions */
9883 writel(0, phba->HCregaddr);
9884 readl(phba->HCregaddr);
9885 }
9886
9887 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009888 phba->work_ha |= HA_ERATT;
9889 /* Indicate polling handles this ERATT */
9890 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009891 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009892 }
James Smart3772a992009-05-22 14:50:54 -04009893 return 0;
James Smart9940b972011-03-11 16:06:12 -05009894
9895unplug_err:
9896 /* Set the driver HS work bitmap */
9897 phba->work_hs |= UNPLUG_ERR;
9898 /* Set the driver HA work bitmap */
9899 phba->work_ha |= HA_ERATT;
9900 /* Indicate polling handles this ERATT */
9901 phba->hba_flag |= HBA_ERATT_HANDLED;
9902 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009903}
9904
James Smarte59058c2008-08-24 21:49:00 -04009905/**
James Smartda0436e2009-05-22 14:51:39 -04009906 * lpfc_sli4_eratt_read - read sli-4 error attention events
9907 * @phba: Pointer to HBA context.
9908 *
9909 * This function is called to read the SLI4 device error attention registers
9910 * for possible error attention events. The caller must hold the hostlock
9911 * with spin_lock_irq().
9912 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009913 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009914 * Register and returns 0 otherwise.
9915 **/
9916static int
9917lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9918{
9919 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009920 uint32_t if_type, portsmphr;
9921 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009922
James Smart2fcee4b2010-12-15 17:57:46 -05009923 /*
9924 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009925 * registers for error attention. This can be changed later.
9926 */
James Smart2fcee4b2010-12-15 17:57:46 -05009927 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9928 switch (if_type) {
9929 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009930 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9931 &uerr_sta_lo) ||
9932 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9933 &uerr_sta_hi)) {
9934 phba->work_hs |= UNPLUG_ERR;
9935 phba->work_ha |= HA_ERATT;
9936 phba->hba_flag |= HBA_ERATT_HANDLED;
9937 return 1;
9938 }
James Smart2fcee4b2010-12-15 17:57:46 -05009939 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9940 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9942 "1423 HBA Unrecoverable error: "
9943 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9944 "ue_mask_lo_reg=0x%x, "
9945 "ue_mask_hi_reg=0x%x\n",
9946 uerr_sta_lo, uerr_sta_hi,
9947 phba->sli4_hba.ue_mask_lo,
9948 phba->sli4_hba.ue_mask_hi);
9949 phba->work_status[0] = uerr_sta_lo;
9950 phba->work_status[1] = uerr_sta_hi;
9951 phba->work_ha |= HA_ERATT;
9952 phba->hba_flag |= HBA_ERATT_HANDLED;
9953 return 1;
9954 }
9955 break;
9956 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009957 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9958 &portstat_reg.word0) ||
9959 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9960 &portsmphr)){
9961 phba->work_hs |= UNPLUG_ERR;
9962 phba->work_ha |= HA_ERATT;
9963 phba->hba_flag |= HBA_ERATT_HANDLED;
9964 return 1;
9965 }
James Smart2fcee4b2010-12-15 17:57:46 -05009966 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9967 phba->work_status[0] =
9968 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9969 phba->work_status[1] =
9970 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -05009972 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -05009973 "port status reg 0x%x, "
9974 "port smphr reg 0x%x, "
9975 "error 1=0x%x, error 2=0x%x\n",
9976 portstat_reg.word0,
9977 portsmphr,
9978 phba->work_status[0],
9979 phba->work_status[1]);
9980 phba->work_ha |= HA_ERATT;
9981 phba->hba_flag |= HBA_ERATT_HANDLED;
9982 return 1;
9983 }
9984 break;
9985 case LPFC_SLI_INTF_IF_TYPE_1:
9986 default:
James Smarta747c9c2009-11-18 15:41:10 -05009987 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009988 "2886 HBA Error Attention on unsupported "
9989 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009990 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009991 }
James Smart2fcee4b2010-12-15 17:57:46 -05009992
James Smartda0436e2009-05-22 14:51:39 -04009993 return 0;
9994}
9995
9996/**
James Smart3621a712009-04-06 18:47:14 -04009997 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04009998 * @phba: Pointer to HBA context.
9999 *
James Smart3772a992009-05-22 14:50:54 -040010000 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010001 * error attention register bit for error attention events.
10002 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010003 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010004 * Register and returns 0 otherwise.
10005 **/
10006int
10007lpfc_sli_check_eratt(struct lpfc_hba *phba)
10008{
10009 uint32_t ha_copy;
10010
10011 /* If somebody is waiting to handle an eratt, don't process it
10012 * here. The brdkill function will do this.
10013 */
10014 if (phba->link_flag & LS_IGNORE_ERATT)
10015 return 0;
10016
10017 /* Check if interrupt handler handles this ERATT */
10018 spin_lock_irq(&phba->hbalock);
10019 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10020 /* Interrupt handler has handled ERATT */
10021 spin_unlock_irq(&phba->hbalock);
10022 return 0;
10023 }
10024
James Smarta257bf92009-04-06 18:48:10 -040010025 /*
10026 * If there is deferred error attention, do not check for error
10027 * attention
10028 */
10029 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10030 spin_unlock_irq(&phba->hbalock);
10031 return 0;
10032 }
10033
James Smart3772a992009-05-22 14:50:54 -040010034 /* If PCI channel is offline, don't process it */
10035 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010036 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010037 return 0;
10038 }
10039
10040 switch (phba->sli_rev) {
10041 case LPFC_SLI_REV2:
10042 case LPFC_SLI_REV3:
10043 /* Read chip Host Attention (HA) register */
10044 ha_copy = lpfc_sli_eratt_read(phba);
10045 break;
James Smartda0436e2009-05-22 14:51:39 -040010046 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010047 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010048 ha_copy = lpfc_sli4_eratt_read(phba);
10049 break;
James Smart3772a992009-05-22 14:50:54 -040010050 default:
10051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10052 "0299 Invalid SLI revision (%d)\n",
10053 phba->sli_rev);
10054 ha_copy = 0;
10055 break;
James Smart93996272008-08-24 21:50:30 -040010056 }
10057 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010058
10059 return ha_copy;
10060}
10061
10062/**
10063 * lpfc_intr_state_check - Check device state for interrupt handling
10064 * @phba: Pointer to HBA context.
10065 *
10066 * This inline routine checks whether a device or its PCI slot is in a state
10067 * that the interrupt should be handled.
10068 *
10069 * This function returns 0 if the device or the PCI slot is in a state that
10070 * interrupt should be handled, otherwise -EIO.
10071 */
10072static inline int
10073lpfc_intr_state_check(struct lpfc_hba *phba)
10074{
10075 /* If the pci channel is offline, ignore all the interrupts */
10076 if (unlikely(pci_channel_offline(phba->pcidev)))
10077 return -EIO;
10078
10079 /* Update device level interrupt statistics */
10080 phba->sli.slistat.sli_intr++;
10081
10082 /* Ignore all interrupts during initialization. */
10083 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10084 return -EIO;
10085
James Smart93996272008-08-24 21:50:30 -040010086 return 0;
10087}
10088
10089/**
James Smart3772a992009-05-22 14:50:54 -040010090 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010091 * @irq: Interrupt number.
10092 * @dev_id: The device context pointer.
10093 *
James Smart93996272008-08-24 21:50:30 -040010094 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010095 * service routine when device with SLI-3 interface spec is enabled with
10096 * MSI-X multi-message interrupt mode and there are slow-path events in
10097 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10098 * interrupt mode, this function is called as part of the device-level
10099 * interrupt handler. When the PCI slot is in error recovery or the HBA
10100 * is undergoing initialization, the interrupt handler will not process
10101 * the interrupt. The link attention and ELS ring attention events are
10102 * handled by the worker thread. The interrupt handler signals the worker
10103 * thread and returns for these events. This function is called without
10104 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010105 * structures.
10106 *
10107 * This function returns IRQ_HANDLED when interrupt is handled else it
10108 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010109 **/
dea31012005-04-17 16:05:31 -050010110irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010111lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010112{
James Smart2e0fef82007-06-17 19:56:36 -050010113 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010114 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010115 uint32_t work_ha_copy;
10116 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010117 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010118 uint32_t control;
10119
James Smart92d7f7b2007-06-17 19:56:38 -050010120 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010121 struct lpfc_vport *vport;
10122 struct lpfc_nodelist *ndlp;
10123 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010124 LPFC_MBOXQ_t *pmb;
10125 int rc;
10126
dea31012005-04-17 16:05:31 -050010127 /*
10128 * Get the driver's phba structure from the dev_id and
10129 * assume the HBA is not interrupting.
10130 */
James Smart93996272008-08-24 21:50:30 -040010131 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010132
10133 if (unlikely(!phba))
10134 return IRQ_NONE;
10135
dea31012005-04-17 16:05:31 -050010136 /*
James Smart93996272008-08-24 21:50:30 -040010137 * Stuff needs to be attented to when this function is invoked as an
10138 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010139 */
James Smart93996272008-08-24 21:50:30 -040010140 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010141 /* Check device state for handling interrupt */
10142 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010143 return IRQ_NONE;
10144 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010145 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010146 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10147 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010148 /* If somebody is waiting to handle an eratt don't process it
10149 * here. The brdkill function will do this.
10150 */
10151 if (phba->link_flag & LS_IGNORE_ERATT)
10152 ha_copy &= ~HA_ERATT;
10153 /* Check the need for handling ERATT in interrupt handler */
10154 if (ha_copy & HA_ERATT) {
10155 if (phba->hba_flag & HBA_ERATT_HANDLED)
10156 /* ERATT polling has handled ERATT */
10157 ha_copy &= ~HA_ERATT;
10158 else
10159 /* Indicate interrupt handler handles ERATT */
10160 phba->hba_flag |= HBA_ERATT_HANDLED;
10161 }
James Smarta257bf92009-04-06 18:48:10 -040010162
10163 /*
10164 * If there is deferred error attention, do not check for any
10165 * interrupt.
10166 */
10167 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010168 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010169 return IRQ_NONE;
10170 }
10171
James Smart93996272008-08-24 21:50:30 -040010172 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010173 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10174 goto unplug_error;
10175
James Smarta747c9c2009-11-18 15:41:10 -050010176 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10177 HC_LAINT_ENA | HC_ERINT_ENA),
10178 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010179 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10180 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010181 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010182 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010183 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010184 } else
10185 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010186
dea31012005-04-17 16:05:31 -050010187 work_ha_copy = ha_copy & phba->work_ha_mask;
10188
James Smart93996272008-08-24 21:50:30 -040010189 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010190 if (work_ha_copy & HA_LATT) {
10191 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10192 /*
10193 * Turn off Link Attention interrupts
10194 * until CLEAR_LA done
10195 */
James Smart5b75da22008-12-04 22:39:35 -050010196 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010197 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010198 if (lpfc_readl(phba->HCregaddr, &control))
10199 goto unplug_error;
dea31012005-04-17 16:05:31 -050010200 control &= ~HC_LAINT_ENA;
10201 writel(control, phba->HCregaddr);
10202 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010203 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010204 }
10205 else
10206 work_ha_copy &= ~HA_LATT;
10207 }
10208
James Smart93996272008-08-24 21:50:30 -040010209 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010210 /*
10211 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10212 * the only slow ring.
10213 */
10214 status = (work_ha_copy &
10215 (HA_RXMASK << (4*LPFC_ELS_RING)));
10216 status >>= (4*LPFC_ELS_RING);
10217 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010218 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010219 if (lpfc_readl(phba->HCregaddr, &control))
10220 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010221
10222 lpfc_debugfs_slow_ring_trc(phba,
10223 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10224 control, status,
10225 (uint32_t)phba->sli.slistat.sli_intr);
10226
James Smart858c9f62007-06-17 19:56:39 -050010227 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010228 lpfc_debugfs_slow_ring_trc(phba,
10229 "ISR Disable ring:"
10230 "pwork:x%x hawork:x%x wait:x%x",
10231 phba->work_ha, work_ha_copy,
10232 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010233 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010234
James Smart858c9f62007-06-17 19:56:39 -050010235 control &=
10236 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010237 writel(control, phba->HCregaddr);
10238 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010239 }
James Smarta58cbd52007-08-02 11:09:43 -040010240 else {
10241 lpfc_debugfs_slow_ring_trc(phba,
10242 "ISR slow ring: pwork:"
10243 "x%x hawork:x%x wait:x%x",
10244 phba->work_ha, work_ha_copy,
10245 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010246 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010247 }
James Smart5b75da22008-12-04 22:39:35 -050010248 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010249 }
10250 }
James Smart5b75da22008-12-04 22:39:35 -050010251 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010252 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010253 if (lpfc_sli_read_hs(phba))
10254 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010255 /*
10256 * Check if there is a deferred error condition
10257 * is active
10258 */
10259 if ((HS_FFER1 & phba->work_hs) &&
10260 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010261 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10262 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010263 phba->hba_flag |= DEFER_ERATT;
10264 /* Clear all interrupt enable conditions */
10265 writel(0, phba->HCregaddr);
10266 readl(phba->HCregaddr);
10267 }
10268 }
10269
James Smart93996272008-08-24 21:50:30 -040010270 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010271 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010272 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010273 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010274 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010275
10276 /* First check out the status word */
10277 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10278 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010279 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010280 /*
10281 * Stray Mailbox Interrupt, mbxCommand <cmd>
10282 * mbxStatus <status>
10283 */
James Smart09372822008-01-11 01:52:54 -050010284 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010285 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010286 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010287 "Interrupt mbxCommand x%x "
10288 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010289 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010290 pmbox->mbxCommand,
10291 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010292 /* clear mailbox attention bit */
10293 work_ha_copy &= ~HA_MBATT;
10294 } else {
James Smart97eab632008-04-07 10:16:05 -040010295 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010296 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010297 phba->last_completion_time = jiffies;
10298 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010299 if (pmb->mbox_cmpl) {
10300 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10301 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010302 if (pmb->out_ext_byte_len &&
10303 pmb->context2)
10304 lpfc_sli_pcimem_bcopy(
10305 phba->mbox_ext,
10306 pmb->context2,
10307 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010308 }
James Smart09372822008-01-11 01:52:54 -050010309 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10310 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10311
10312 lpfc_debugfs_disc_trc(vport,
10313 LPFC_DISC_TRC_MBOX_VPORT,
10314 "MBOX dflt rpi: : "
10315 "status:x%x rpi:x%x",
10316 (uint32_t)pmbox->mbxStatus,
10317 pmbox->un.varWords[0], 0);
10318
10319 if (!pmbox->mbxStatus) {
10320 mp = (struct lpfc_dmabuf *)
10321 (pmb->context1);
10322 ndlp = (struct lpfc_nodelist *)
10323 pmb->context2;
10324
10325 /* Reg_LOGIN of dflt RPI was
10326 * successful. new lets get
10327 * rid of the RPI using the
10328 * same mbox buffer.
10329 */
10330 lpfc_unreg_login(phba,
10331 vport->vpi,
10332 pmbox->un.varWords[0],
10333 pmb);
10334 pmb->mbox_cmpl =
10335 lpfc_mbx_cmpl_dflt_rpi;
10336 pmb->context1 = mp;
10337 pmb->context2 = ndlp;
10338 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010339 rc = lpfc_sli_issue_mbox(phba,
10340 pmb,
10341 MBX_NOWAIT);
10342 if (rc != MBX_BUSY)
10343 lpfc_printf_log(phba,
10344 KERN_ERR,
10345 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010346 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010347 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010348 if (rc != MBX_NOT_FINISHED)
10349 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010350 }
10351 }
James Smart5b75da22008-12-04 22:39:35 -050010352 spin_lock_irqsave(
10353 &phba->pport->work_port_lock,
10354 iflag);
James Smart09372822008-01-11 01:52:54 -050010355 phba->pport->work_port_events &=
10356 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010357 spin_unlock_irqrestore(
10358 &phba->pport->work_port_lock,
10359 iflag);
James Smart09372822008-01-11 01:52:54 -050010360 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010361 }
James Smart97eab632008-04-07 10:16:05 -040010362 } else
James Smart5b75da22008-12-04 22:39:35 -050010363 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010364
James Smart92d7f7b2007-06-17 19:56:38 -050010365 if ((work_ha_copy & HA_MBATT) &&
10366 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010367send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010368 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010369 do {
10370 rc = lpfc_sli_issue_mbox(phba, NULL,
10371 MBX_NOWAIT);
10372 } while (rc == MBX_NOT_FINISHED);
10373 if (rc != MBX_SUCCESS)
10374 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10375 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010376 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010377 }
10378
James Smart5b75da22008-12-04 22:39:35 -050010379 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010380 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010381 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010382 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010383 }
James Smart93996272008-08-24 21:50:30 -040010384 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010385unplug_error:
10386 spin_unlock_irqrestore(&phba->hbalock, iflag);
10387 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010388
James Smart3772a992009-05-22 14:50:54 -040010389} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010390
10391/**
James Smart3772a992009-05-22 14:50:54 -040010392 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010393 * @irq: Interrupt number.
10394 * @dev_id: The device context pointer.
10395 *
10396 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010397 * service routine when device with SLI-3 interface spec is enabled with
10398 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10399 * ring event in the HBA. However, when the device is enabled with either
10400 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10401 * device-level interrupt handler. When the PCI slot is in error recovery
10402 * or the HBA is undergoing initialization, the interrupt handler will not
10403 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10404 * the intrrupt context. This function is called without any lock held.
10405 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010406 *
10407 * This function returns IRQ_HANDLED when interrupt is handled else it
10408 * returns IRQ_NONE.
10409 **/
10410irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010411lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010412{
10413 struct lpfc_hba *phba;
10414 uint32_t ha_copy;
10415 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010416 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010417
10418 /* Get the driver's phba structure from the dev_id and
10419 * assume the HBA is not interrupting.
10420 */
10421 phba = (struct lpfc_hba *) dev_id;
10422
10423 if (unlikely(!phba))
10424 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010425
10426 /*
James Smart93996272008-08-24 21:50:30 -040010427 * Stuff needs to be attented to when this function is invoked as an
10428 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010429 */
James Smart93996272008-08-24 21:50:30 -040010430 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010431 /* Check device state for handling interrupt */
10432 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010433 return IRQ_NONE;
10434 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010435 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10436 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010437 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010438 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010439 /*
10440 * If there is deferred error attention, do not check for
10441 * any interrupt.
10442 */
10443 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010444 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010445 return IRQ_NONE;
10446 }
James Smart93996272008-08-24 21:50:30 -040010447 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10448 phba->HAregaddr);
10449 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010450 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010451 } else
10452 ha_copy = phba->ha_copy;
10453
10454 /*
10455 * Process all events on FCP ring. Take the optimized path for FCP IO.
10456 */
10457 ha_copy &= ~(phba->work_ha_mask);
10458
10459 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010460 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010461 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010462 lpfc_sli_handle_fast_ring_event(phba,
10463 &phba->sli.ring[LPFC_FCP_RING],
10464 status);
James Smarta4bc3372006-12-02 13:34:16 -050010465
10466 if (phba->cfg_multi_ring_support == 2) {
10467 /*
James Smart93996272008-08-24 21:50:30 -040010468 * Process all events on extra ring. Take the optimized path
10469 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010470 */
James Smart93996272008-08-24 21:50:30 -040010471 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010472 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010473 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010474 lpfc_sli_handle_fast_ring_event(phba,
10475 &phba->sli.ring[LPFC_EXTRA_RING],
10476 status);
10477 }
10478 }
dea31012005-04-17 16:05:31 -050010479 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010480} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010481
James Smart93996272008-08-24 21:50:30 -040010482/**
James Smart3772a992009-05-22 14:50:54 -040010483 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010484 * @irq: Interrupt number.
10485 * @dev_id: The device context pointer.
10486 *
James Smart3772a992009-05-22 14:50:54 -040010487 * This function is the HBA device-level interrupt handler to device with
10488 * SLI-3 interface spec, called from the PCI layer when either MSI or
10489 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10490 * requires driver attention. This function invokes the slow-path interrupt
10491 * attention handling function and fast-path interrupt attention handling
10492 * function in turn to process the relevant HBA attention events. This
10493 * function is called without any lock held. It gets the hbalock to access
10494 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010495 *
10496 * This function returns IRQ_HANDLED when interrupt is handled, else it
10497 * returns IRQ_NONE.
10498 **/
10499irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010500lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010501{
10502 struct lpfc_hba *phba;
10503 irqreturn_t sp_irq_rc, fp_irq_rc;
10504 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010505 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010506
10507 /*
10508 * Get the driver's phba structure from the dev_id and
10509 * assume the HBA is not interrupting.
10510 */
10511 phba = (struct lpfc_hba *) dev_id;
10512
10513 if (unlikely(!phba))
10514 return IRQ_NONE;
10515
James Smart3772a992009-05-22 14:50:54 -040010516 /* Check device state for handling interrupt */
10517 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010518 return IRQ_NONE;
10519
10520 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010521 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10522 spin_unlock(&phba->hbalock);
10523 return IRQ_HANDLED;
10524 }
10525
James Smart93996272008-08-24 21:50:30 -040010526 if (unlikely(!phba->ha_copy)) {
10527 spin_unlock(&phba->hbalock);
10528 return IRQ_NONE;
10529 } else if (phba->ha_copy & HA_ERATT) {
10530 if (phba->hba_flag & HBA_ERATT_HANDLED)
10531 /* ERATT polling has handled ERATT */
10532 phba->ha_copy &= ~HA_ERATT;
10533 else
10534 /* Indicate interrupt handler handles ERATT */
10535 phba->hba_flag |= HBA_ERATT_HANDLED;
10536 }
10537
James Smarta257bf92009-04-06 18:48:10 -040010538 /*
10539 * If there is deferred error attention, do not check for any interrupt.
10540 */
10541 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010542 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010543 return IRQ_NONE;
10544 }
10545
James Smart93996272008-08-24 21:50:30 -040010546 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010547 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10548 spin_unlock(&phba->hbalock);
10549 return IRQ_HANDLED;
10550 }
James Smarta747c9c2009-11-18 15:41:10 -050010551 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10552 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10553 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010554 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010555 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010556 readl(phba->HAregaddr); /* flush */
10557 spin_unlock(&phba->hbalock);
10558
10559 /*
10560 * Invokes slow-path host attention interrupt handling as appropriate.
10561 */
10562
10563 /* status of events with mailbox and link attention */
10564 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10565
10566 /* status of events with ELS ring */
10567 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10568 status2 >>= (4*LPFC_ELS_RING);
10569
10570 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010571 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010572 else
10573 sp_irq_rc = IRQ_NONE;
10574
10575 /*
10576 * Invoke fast-path host attention interrupt handling as appropriate.
10577 */
10578
10579 /* status of events with FCP ring */
10580 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10581 status1 >>= (4*LPFC_FCP_RING);
10582
10583 /* status of events with extra ring */
10584 if (phba->cfg_multi_ring_support == 2) {
10585 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10586 status2 >>= (4*LPFC_EXTRA_RING);
10587 } else
10588 status2 = 0;
10589
10590 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010591 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010592 else
10593 fp_irq_rc = IRQ_NONE;
10594
10595 /* Return device-level interrupt handling status */
10596 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010597} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010598
10599/**
10600 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10601 * @phba: pointer to lpfc hba data structure.
10602 *
10603 * This routine is invoked by the worker thread to process all the pending
10604 * SLI4 FCP abort XRI events.
10605 **/
10606void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10607{
10608 struct lpfc_cq_event *cq_event;
10609
10610 /* First, declare the fcp xri abort event has been handled */
10611 spin_lock_irq(&phba->hbalock);
10612 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10613 spin_unlock_irq(&phba->hbalock);
10614 /* Now, handle all the fcp xri abort events */
10615 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10616 /* Get the first event from the head of the event queue */
10617 spin_lock_irq(&phba->hbalock);
10618 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10619 cq_event, struct lpfc_cq_event, list);
10620 spin_unlock_irq(&phba->hbalock);
10621 /* Notify aborted XRI for FCP work queue */
10622 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10623 /* Free the event processed back to the free pool */
10624 lpfc_sli4_cq_event_release(phba, cq_event);
10625 }
10626}
10627
10628/**
10629 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10630 * @phba: pointer to lpfc hba data structure.
10631 *
10632 * This routine is invoked by the worker thread to process all the pending
10633 * SLI4 els abort xri events.
10634 **/
10635void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10636{
10637 struct lpfc_cq_event *cq_event;
10638
10639 /* First, declare the els xri abort event has been handled */
10640 spin_lock_irq(&phba->hbalock);
10641 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10642 spin_unlock_irq(&phba->hbalock);
10643 /* Now, handle all the els xri abort events */
10644 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10645 /* Get the first event from the head of the event queue */
10646 spin_lock_irq(&phba->hbalock);
10647 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10648 cq_event, struct lpfc_cq_event, list);
10649 spin_unlock_irq(&phba->hbalock);
10650 /* Notify aborted XRI for ELS work queue */
10651 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10652 /* Free the event processed back to the free pool */
10653 lpfc_sli4_cq_event_release(phba, cq_event);
10654 }
10655}
10656
James Smart341af102010-01-26 23:07:37 -050010657/**
10658 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10659 * @phba: pointer to lpfc hba data structure
10660 * @pIocbIn: pointer to the rspiocbq
10661 * @pIocbOut: pointer to the cmdiocbq
10662 * @wcqe: pointer to the complete wcqe
10663 *
10664 * This routine transfers the fields of a command iocbq to a response iocbq
10665 * by copying all the IOCB fields from command iocbq and transferring the
10666 * completion status information from the complete wcqe.
10667 **/
James Smart4f774512009-05-22 14:52:35 -040010668static void
James Smart341af102010-01-26 23:07:37 -050010669lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10670 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010671 struct lpfc_iocbq *pIocbOut,
10672 struct lpfc_wcqe_complete *wcqe)
10673{
James Smart341af102010-01-26 23:07:37 -050010674 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040010675 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10676
10677 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10678 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010679 /* Map WCQE parameters into irspiocb parameters */
10680 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10681 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10682 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10683 pIocbIn->iocb.un.fcpi.fcpi_parm =
10684 pIocbOut->iocb.un.fcpi.fcpi_parm -
10685 wcqe->total_data_placed;
10686 else
10687 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010688 else {
James Smart4f774512009-05-22 14:52:35 -040010689 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010690 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10691 }
James Smart341af102010-01-26 23:07:37 -050010692
10693 /* Pick up HBA exchange busy condition */
10694 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10695 spin_lock_irqsave(&phba->hbalock, iflags);
10696 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10697 spin_unlock_irqrestore(&phba->hbalock, iflags);
10698 }
James Smart4f774512009-05-22 14:52:35 -040010699}
10700
10701/**
James Smart45ed1192009-10-02 15:17:02 -040010702 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10703 * @phba: Pointer to HBA context object.
10704 * @wcqe: Pointer to work-queue completion queue entry.
10705 *
10706 * This routine handles an ELS work-queue completion event and construct
10707 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10708 * discovery engine to handle.
10709 *
10710 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10711 **/
10712static struct lpfc_iocbq *
10713lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10714 struct lpfc_iocbq *irspiocbq)
10715{
10716 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10717 struct lpfc_iocbq *cmdiocbq;
10718 struct lpfc_wcqe_complete *wcqe;
10719 unsigned long iflags;
10720
10721 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10722 spin_lock_irqsave(&phba->hbalock, iflags);
10723 pring->stats.iocb_event++;
10724 /* Look up the ELS command IOCB and create pseudo response IOCB */
10725 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10726 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10727 spin_unlock_irqrestore(&phba->hbalock, iflags);
10728
10729 if (unlikely(!cmdiocbq)) {
10730 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10731 "0386 ELS complete with no corresponding "
10732 "cmdiocb: iotag (%d)\n",
10733 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10734 lpfc_sli_release_iocbq(phba, irspiocbq);
10735 return NULL;
10736 }
10737
10738 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010739 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010740
10741 return irspiocbq;
10742}
10743
10744/**
James Smart04c68492009-05-22 14:52:52 -040010745 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10746 * @phba: Pointer to HBA context object.
10747 * @cqe: Pointer to mailbox completion queue entry.
10748 *
10749 * This routine process a mailbox completion queue entry with asynchrous
10750 * event.
10751 *
10752 * Return: true if work posted to worker thread, otherwise false.
10753 **/
10754static bool
10755lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10756{
10757 struct lpfc_cq_event *cq_event;
10758 unsigned long iflags;
10759
10760 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10761 "0392 Async Event: word0:x%x, word1:x%x, "
10762 "word2:x%x, word3:x%x\n", mcqe->word0,
10763 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10764
10765 /* Allocate a new internal CQ_EVENT entry */
10766 cq_event = lpfc_sli4_cq_event_alloc(phba);
10767 if (!cq_event) {
10768 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10769 "0394 Failed to allocate CQ_EVENT entry\n");
10770 return false;
10771 }
10772
10773 /* Move the CQE into an asynchronous event entry */
10774 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10775 spin_lock_irqsave(&phba->hbalock, iflags);
10776 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10777 /* Set the async event flag */
10778 phba->hba_flag |= ASYNC_EVENT;
10779 spin_unlock_irqrestore(&phba->hbalock, iflags);
10780
10781 return true;
10782}
10783
10784/**
10785 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10786 * @phba: Pointer to HBA context object.
10787 * @cqe: Pointer to mailbox completion queue entry.
10788 *
10789 * This routine process a mailbox completion queue entry with mailbox
10790 * completion event.
10791 *
10792 * Return: true if work posted to worker thread, otherwise false.
10793 **/
10794static bool
10795lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10796{
10797 uint32_t mcqe_status;
10798 MAILBOX_t *mbox, *pmbox;
10799 struct lpfc_mqe *mqe;
10800 struct lpfc_vport *vport;
10801 struct lpfc_nodelist *ndlp;
10802 struct lpfc_dmabuf *mp;
10803 unsigned long iflags;
10804 LPFC_MBOXQ_t *pmb;
10805 bool workposted = false;
10806 int rc;
10807
10808 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10809 if (!bf_get(lpfc_trailer_completed, mcqe))
10810 goto out_no_mqe_complete;
10811
10812 /* Get the reference to the active mbox command */
10813 spin_lock_irqsave(&phba->hbalock, iflags);
10814 pmb = phba->sli.mbox_active;
10815 if (unlikely(!pmb)) {
10816 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10817 "1832 No pending MBOX command to handle\n");
10818 spin_unlock_irqrestore(&phba->hbalock, iflags);
10819 goto out_no_mqe_complete;
10820 }
10821 spin_unlock_irqrestore(&phba->hbalock, iflags);
10822 mqe = &pmb->u.mqe;
10823 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10824 mbox = phba->mbox;
10825 vport = pmb->vport;
10826
10827 /* Reset heartbeat timer */
10828 phba->last_completion_time = jiffies;
10829 del_timer(&phba->sli.mbox_tmo);
10830
10831 /* Move mbox data to caller's mailbox region, do endian swapping */
10832 if (pmb->mbox_cmpl && mbox)
10833 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040010834
James Smart73d91e52011-10-10 21:32:10 -040010835 /*
10836 * For mcqe errors, conditionally move a modified error code to
10837 * the mbox so that the error will not be missed.
10838 */
10839 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10840 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10841 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10842 bf_set(lpfc_mqe_status, mqe,
10843 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10844 }
James Smart04c68492009-05-22 14:52:52 -040010845 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10846 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10847 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10848 "MBOX dflt rpi: status:x%x rpi:x%x",
10849 mcqe_status,
10850 pmbox->un.varWords[0], 0);
10851 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10852 mp = (struct lpfc_dmabuf *)(pmb->context1);
10853 ndlp = (struct lpfc_nodelist *)pmb->context2;
10854 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10855 * RID of the PPI using the same mbox buffer.
10856 */
10857 lpfc_unreg_login(phba, vport->vpi,
10858 pmbox->un.varWords[0], pmb);
10859 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10860 pmb->context1 = mp;
10861 pmb->context2 = ndlp;
10862 pmb->vport = vport;
10863 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10864 if (rc != MBX_BUSY)
10865 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10866 LOG_SLI, "0385 rc should "
10867 "have been MBX_BUSY\n");
10868 if (rc != MBX_NOT_FINISHED)
10869 goto send_current_mbox;
10870 }
10871 }
10872 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10873 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10874 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10875
10876 /* There is mailbox completion work to do */
10877 spin_lock_irqsave(&phba->hbalock, iflags);
10878 __lpfc_mbox_cmpl_put(phba, pmb);
10879 phba->work_ha |= HA_MBATT;
10880 spin_unlock_irqrestore(&phba->hbalock, iflags);
10881 workposted = true;
10882
10883send_current_mbox:
10884 spin_lock_irqsave(&phba->hbalock, iflags);
10885 /* Release the mailbox command posting token */
10886 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10887 /* Setting active mailbox pointer need to be in sync to flag clear */
10888 phba->sli.mbox_active = NULL;
10889 spin_unlock_irqrestore(&phba->hbalock, iflags);
10890 /* Wake up worker thread to post the next pending mailbox command */
10891 lpfc_worker_wake_up(phba);
10892out_no_mqe_complete:
10893 if (bf_get(lpfc_trailer_consumed, mcqe))
10894 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10895 return workposted;
10896}
10897
10898/**
10899 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10900 * @phba: Pointer to HBA context object.
10901 * @cqe: Pointer to mailbox completion queue entry.
10902 *
10903 * This routine process a mailbox completion queue entry, it invokes the
10904 * proper mailbox complete handling or asynchrous event handling routine
10905 * according to the MCQE's async bit.
10906 *
10907 * Return: true if work posted to worker thread, otherwise false.
10908 **/
10909static bool
10910lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10911{
10912 struct lpfc_mcqe mcqe;
10913 bool workposted;
10914
10915 /* Copy the mailbox MCQE and convert endian order as needed */
10916 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10917
10918 /* Invoke the proper event handling routine */
10919 if (!bf_get(lpfc_trailer_async, &mcqe))
10920 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10921 else
10922 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10923 return workposted;
10924}
10925
10926/**
James Smart4f774512009-05-22 14:52:35 -040010927 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10928 * @phba: Pointer to HBA context object.
10929 * @wcqe: Pointer to work-queue completion queue entry.
10930 *
10931 * This routine handles an ELS work-queue completion event.
10932 *
10933 * Return: true if work posted to worker thread, otherwise false.
10934 **/
10935static bool
10936lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10937 struct lpfc_wcqe_complete *wcqe)
10938{
James Smart4f774512009-05-22 14:52:35 -040010939 struct lpfc_iocbq *irspiocbq;
10940 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010941 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010942
James Smart45ed1192009-10-02 15:17:02 -040010943 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010944 irspiocbq = lpfc_sli_get_iocbq(phba);
10945 if (!irspiocbq) {
10946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010947 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10948 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10949 pring->txq_cnt, phba->iocb_cnt,
10950 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10951 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040010952 return false;
James Smart4f774512009-05-22 14:52:35 -040010953 }
James Smart4f774512009-05-22 14:52:35 -040010954
James Smart45ed1192009-10-02 15:17:02 -040010955 /* Save off the slow-path queue event for work thread to process */
10956 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040010957 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040010958 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010959 &phba->sli4_hba.sp_queue_event);
10960 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040010961 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040010962
James Smart45ed1192009-10-02 15:17:02 -040010963 return true;
James Smart4f774512009-05-22 14:52:35 -040010964}
10965
10966/**
10967 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10968 * @phba: Pointer to HBA context object.
10969 * @wcqe: Pointer to work-queue completion queue entry.
10970 *
10971 * This routine handles slow-path WQ entry comsumed event by invoking the
10972 * proper WQ release routine to the slow-path WQ.
10973 **/
10974static void
10975lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10976 struct lpfc_wcqe_release *wcqe)
10977{
James Smart2e90f4b2011-12-13 13:22:37 -050010978 /* sanity check on queue memory */
10979 if (unlikely(!phba->sli4_hba.els_wq))
10980 return;
James Smart4f774512009-05-22 14:52:35 -040010981 /* Check for the slow-path ELS work queue */
10982 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10983 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10984 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10985 else
10986 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10987 "2579 Slow-path wqe consume event carries "
10988 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10989 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10990 phba->sli4_hba.els_wq->queue_id);
10991}
10992
10993/**
10994 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10995 * @phba: Pointer to HBA context object.
10996 * @cq: Pointer to a WQ completion queue.
10997 * @wcqe: Pointer to work-queue completion queue entry.
10998 *
10999 * This routine handles an XRI abort event.
11000 *
11001 * Return: true if work posted to worker thread, otherwise false.
11002 **/
11003static bool
11004lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11005 struct lpfc_queue *cq,
11006 struct sli4_wcqe_xri_aborted *wcqe)
11007{
11008 bool workposted = false;
11009 struct lpfc_cq_event *cq_event;
11010 unsigned long iflags;
11011
11012 /* Allocate a new internal CQ_EVENT entry */
11013 cq_event = lpfc_sli4_cq_event_alloc(phba);
11014 if (!cq_event) {
11015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11016 "0602 Failed to allocate CQ_EVENT entry\n");
11017 return false;
11018 }
11019
11020 /* Move the CQE into the proper xri abort event list */
11021 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11022 switch (cq->subtype) {
11023 case LPFC_FCP:
11024 spin_lock_irqsave(&phba->hbalock, iflags);
11025 list_add_tail(&cq_event->list,
11026 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11027 /* Set the fcp xri abort event flag */
11028 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11029 spin_unlock_irqrestore(&phba->hbalock, iflags);
11030 workposted = true;
11031 break;
11032 case LPFC_ELS:
11033 spin_lock_irqsave(&phba->hbalock, iflags);
11034 list_add_tail(&cq_event->list,
11035 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11036 /* Set the els xri abort event flag */
11037 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11038 spin_unlock_irqrestore(&phba->hbalock, iflags);
11039 workposted = true;
11040 break;
11041 default:
11042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11043 "0603 Invalid work queue CQE subtype (x%x)\n",
11044 cq->subtype);
11045 workposted = false;
11046 break;
11047 }
11048 return workposted;
11049}
11050
11051/**
James Smart4d9ab992009-10-02 15:16:39 -040011052 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011053 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011054 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011055 *
James Smart4d9ab992009-10-02 15:16:39 -040011056 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011057 *
11058 * Return: true if work posted to worker thread, otherwise false.
11059 **/
11060static bool
James Smart4d9ab992009-10-02 15:16:39 -040011061lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11062{
11063 bool workposted = false;
11064 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11065 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11066 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011067 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011068 unsigned long iflags;
11069
James Smart2e90f4b2011-12-13 13:22:37 -050011070 /* sanity check on queue memory */
11071 if (unlikely(!hrq) || unlikely(!drq))
11072 return workposted;
11073
James Smart7851fe22011-07-22 18:36:52 -040011074 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11075 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11076 else
11077 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11078 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011079 goto out;
11080
11081 status = bf_get(lpfc_rcqe_status, rcqe);
11082 switch (status) {
11083 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11085 "2537 Receive Frame Truncated!!\n");
11086 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011087 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011088 spin_lock_irqsave(&phba->hbalock, iflags);
11089 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11090 if (!dma_buf) {
11091 spin_unlock_irqrestore(&phba->hbalock, iflags);
11092 goto out;
11093 }
11094 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11095 /* save off the frame for the word thread to process */
11096 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011097 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011098 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011099 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011100 spin_unlock_irqrestore(&phba->hbalock, iflags);
11101 workposted = true;
11102 break;
11103 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11104 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11105 /* Post more buffers if possible */
11106 spin_lock_irqsave(&phba->hbalock, iflags);
11107 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11108 spin_unlock_irqrestore(&phba->hbalock, iflags);
11109 workposted = true;
11110 break;
11111 }
11112out:
11113 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011114}
11115
11116/**
11117 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11118 * @phba: Pointer to HBA context object.
11119 * @cq: Pointer to the completion queue.
11120 * @wcqe: Pointer to a completion queue entry.
11121 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011122 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011123 * entry.
11124 *
11125 * Return: true if work posted to worker thread, otherwise false.
11126 **/
11127static bool
11128lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011129 struct lpfc_cqe *cqe)
11130{
James Smart45ed1192009-10-02 15:17:02 -040011131 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011132 bool workposted = false;
11133
11134 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011135 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011136
11137 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011138 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011139 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011140 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011141 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011142 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011143 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011144 break;
11145 case CQE_CODE_RELEASE_WQE:
11146 /* Process the WQ release event */
11147 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011148 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011149 break;
11150 case CQE_CODE_XRI_ABORTED:
11151 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011152 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011153 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011154 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011155 break;
James Smart4d9ab992009-10-02 15:16:39 -040011156 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011157 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011158 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011159 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011160 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011161 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011162 break;
James Smart4f774512009-05-22 14:52:35 -040011163 default:
11164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11165 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011166 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011167 break;
11168 }
11169 return workposted;
11170}
11171
11172/**
James Smart4f774512009-05-22 14:52:35 -040011173 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11174 * @phba: Pointer to HBA context object.
11175 * @eqe: Pointer to fast-path event queue entry.
11176 *
11177 * This routine process a event queue entry from the slow-path event queue.
11178 * It will check the MajorCode and MinorCode to determine this is for a
11179 * completion event on a completion queue, if not, an error shall be logged
11180 * and just return. Otherwise, it will get to the corresponding completion
11181 * queue and process all the entries on that completion queue, rearm the
11182 * completion queue, and then return.
11183 *
11184 **/
11185static void
11186lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11187{
11188 struct lpfc_queue *cq = NULL, *childq, *speq;
11189 struct lpfc_cqe *cqe;
11190 bool workposted = false;
11191 int ecount = 0;
11192 uint16_t cqid;
11193
James Smartcb5172e2010-03-15 11:25:07 -040011194 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011195 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11196 "0359 Not a valid slow-path completion "
11197 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011198 bf_get_le32(lpfc_eqe_major_code, eqe),
11199 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011200 return;
11201 }
11202
11203 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011204 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011205
11206 /* Search for completion queue pointer matching this cqid */
11207 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011208 /* sanity check on queue memory */
11209 if (unlikely(!speq))
11210 return;
James Smart4f774512009-05-22 14:52:35 -040011211 list_for_each_entry(childq, &speq->child_list, list) {
11212 if (childq->queue_id == cqid) {
11213 cq = childq;
11214 break;
11215 }
11216 }
11217 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011218 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11220 "0365 Slow-path CQ identifier "
11221 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011222 return;
11223 }
11224
11225 /* Process all the entries to the CQ */
11226 switch (cq->type) {
11227 case LPFC_MCQ:
11228 while ((cqe = lpfc_sli4_cq_get(cq))) {
11229 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011230 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011231 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11232 }
11233 break;
11234 case LPFC_WCQ:
11235 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011236 if (cq->subtype == LPFC_FCP)
11237 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11238 cqe);
11239 else
11240 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11241 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011242 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011243 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11244 }
11245 break;
11246 default:
11247 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11248 "0370 Invalid completion queue type (%d)\n",
11249 cq->type);
11250 return;
11251 }
11252
11253 /* Catch the no cq entry condition, log an error */
11254 if (unlikely(ecount == 0))
11255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11256 "0371 No entry from the CQ: identifier "
11257 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11258
11259 /* In any case, flash and re-arm the RCQ */
11260 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11261
11262 /* wake up worker thread if there are works to be done */
11263 if (workposted)
11264 lpfc_worker_wake_up(phba);
11265}
11266
11267/**
11268 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11269 * @eqe: Pointer to fast-path completion queue entry.
11270 *
11271 * This routine process a fast-path work queue completion entry from fast-path
11272 * event queue for FCP command response completion.
11273 **/
11274static void
11275lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11276 struct lpfc_wcqe_complete *wcqe)
11277{
11278 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11279 struct lpfc_iocbq *cmdiocbq;
11280 struct lpfc_iocbq irspiocbq;
11281 unsigned long iflags;
11282
11283 spin_lock_irqsave(&phba->hbalock, iflags);
11284 pring->stats.iocb_event++;
11285 spin_unlock_irqrestore(&phba->hbalock, iflags);
11286
11287 /* Check for response status */
11288 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11289 /* If resource errors reported from HBA, reduce queue
11290 * depth of the SCSI device.
11291 */
11292 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11293 IOSTAT_LOCAL_REJECT) &&
11294 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11295 phba->lpfc_rampdown_queue_depth(phba);
11296 }
11297 /* Log the error status */
11298 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11299 "0373 FCP complete error: status=x%x, "
11300 "hw_status=x%x, total_data_specified=%d, "
11301 "parameter=x%x, word3=x%x\n",
11302 bf_get(lpfc_wcqe_c_status, wcqe),
11303 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11304 wcqe->total_data_placed, wcqe->parameter,
11305 wcqe->word3);
11306 }
11307
11308 /* Look up the FCP command IOCB and create pseudo response IOCB */
11309 spin_lock_irqsave(&phba->hbalock, iflags);
11310 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11311 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11312 spin_unlock_irqrestore(&phba->hbalock, iflags);
11313 if (unlikely(!cmdiocbq)) {
11314 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11315 "0374 FCP complete with no corresponding "
11316 "cmdiocb: iotag (%d)\n",
11317 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11318 return;
11319 }
11320 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11321 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11322 "0375 FCP cmdiocb not callback function "
11323 "iotag: (%d)\n",
11324 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11325 return;
11326 }
11327
11328 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011329 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011330
James Smart0f65ff62010-02-26 14:14:23 -050011331 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11332 spin_lock_irqsave(&phba->hbalock, iflags);
11333 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11334 spin_unlock_irqrestore(&phba->hbalock, iflags);
11335 }
11336
James Smart4f774512009-05-22 14:52:35 -040011337 /* Pass the cmd_iocb and the rsp state to the upper layer */
11338 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11339}
11340
11341/**
11342 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11343 * @phba: Pointer to HBA context object.
11344 * @cq: Pointer to completion queue.
11345 * @wcqe: Pointer to work-queue completion queue entry.
11346 *
11347 * This routine handles an fast-path WQ entry comsumed event by invoking the
11348 * proper WQ release routine to the slow-path WQ.
11349 **/
11350static void
11351lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11352 struct lpfc_wcqe_release *wcqe)
11353{
11354 struct lpfc_queue *childwq;
11355 bool wqid_matched = false;
11356 uint16_t fcp_wqid;
11357
11358 /* Check for fast-path FCP work queue release */
11359 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11360 list_for_each_entry(childwq, &cq->child_list, list) {
11361 if (childwq->queue_id == fcp_wqid) {
11362 lpfc_sli4_wq_release(childwq,
11363 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11364 wqid_matched = true;
11365 break;
11366 }
11367 }
11368 /* Report warning log message if no match found */
11369 if (wqid_matched != true)
11370 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11371 "2580 Fast-path wqe consume event carries "
11372 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11373}
11374
11375/**
11376 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11377 * @cq: Pointer to the completion queue.
11378 * @eqe: Pointer to fast-path completion queue entry.
11379 *
11380 * This routine process a fast-path work queue completion entry from fast-path
11381 * event queue for FCP command response completion.
11382 **/
11383static int
11384lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11385 struct lpfc_cqe *cqe)
11386{
11387 struct lpfc_wcqe_release wcqe;
11388 bool workposted = false;
11389
11390 /* Copy the work queue CQE and convert endian order if needed */
11391 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11392
11393 /* Check and process for different type of WCQE and dispatch */
11394 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11395 case CQE_CODE_COMPL_WQE:
11396 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011397 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011398 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11399 (struct lpfc_wcqe_complete *)&wcqe);
11400 break;
11401 case CQE_CODE_RELEASE_WQE:
11402 /* Process the WQ release event */
11403 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11404 (struct lpfc_wcqe_release *)&wcqe);
11405 break;
11406 case CQE_CODE_XRI_ABORTED:
11407 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011408 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011409 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11410 (struct sli4_wcqe_xri_aborted *)&wcqe);
11411 break;
11412 default:
11413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11414 "0144 Not a valid WCQE code: x%x\n",
11415 bf_get(lpfc_wcqe_c_code, &wcqe));
11416 break;
11417 }
11418 return workposted;
11419}
11420
11421/**
11422 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11423 * @phba: Pointer to HBA context object.
11424 * @eqe: Pointer to fast-path event queue entry.
11425 *
11426 * This routine process a event queue entry from the fast-path event queue.
11427 * It will check the MajorCode and MinorCode to determine this is for a
11428 * completion event on a completion queue, if not, an error shall be logged
11429 * and just return. Otherwise, it will get to the corresponding completion
11430 * queue and process all the entries on the completion queue, rearm the
11431 * completion queue, and then return.
11432 **/
11433static void
11434lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11435 uint32_t fcp_cqidx)
11436{
11437 struct lpfc_queue *cq;
11438 struct lpfc_cqe *cqe;
11439 bool workposted = false;
11440 uint16_t cqid;
11441 int ecount = 0;
11442
James Smartcb5172e2010-03-15 11:25:07 -040011443 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011444 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11445 "0366 Not a valid fast-path completion "
11446 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011447 bf_get_le32(lpfc_eqe_major_code, eqe),
11448 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011449 return;
11450 }
11451
James Smart2e90f4b2011-12-13 13:22:37 -050011452 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11453 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11454 "3146 Fast-path completion queues "
11455 "does not exist\n");
11456 return;
11457 }
James Smart4f774512009-05-22 14:52:35 -040011458 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11459 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011460 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11462 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011463 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011464 return;
11465 }
11466
11467 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011468 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011469 if (unlikely(cqid != cq->queue_id)) {
11470 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11471 "0368 Miss-matched fast-path completion "
11472 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11473 cqid, cq->queue_id);
11474 return;
11475 }
11476
11477 /* Process all the entries to the CQ */
11478 while ((cqe = lpfc_sli4_cq_get(cq))) {
11479 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011480 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011481 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11482 }
11483
11484 /* Catch the no cq entry condition */
11485 if (unlikely(ecount == 0))
11486 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11487 "0369 No entry from fast-path completion "
11488 "queue fcpcqid=%d\n", cq->queue_id);
11489
11490 /* In any case, flash and re-arm the CQ */
11491 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11492
11493 /* wake up worker thread if there are works to be done */
11494 if (workposted)
11495 lpfc_worker_wake_up(phba);
11496}
11497
11498static void
11499lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11500{
11501 struct lpfc_eqe *eqe;
11502
11503 /* walk all the EQ entries and drop on the floor */
11504 while ((eqe = lpfc_sli4_eq_get(eq)))
11505 ;
11506
11507 /* Clear and re-arm the EQ */
11508 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11509}
11510
11511/**
11512 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11513 * @irq: Interrupt number.
11514 * @dev_id: The device context pointer.
11515 *
11516 * This function is directly called from the PCI layer as an interrupt
11517 * service routine when device with SLI-4 interface spec is enabled with
11518 * MSI-X multi-message interrupt mode and there are slow-path events in
11519 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11520 * interrupt mode, this function is called as part of the device-level
11521 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11522 * undergoing initialization, the interrupt handler will not process the
11523 * interrupt. The link attention and ELS ring attention events are handled
11524 * by the worker thread. The interrupt handler signals the worker thread
11525 * and returns for these events. This function is called without any lock
11526 * held. It gets the hbalock to access and update SLI data structures.
11527 *
11528 * This function returns IRQ_HANDLED when interrupt is handled else it
11529 * returns IRQ_NONE.
11530 **/
11531irqreturn_t
11532lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11533{
11534 struct lpfc_hba *phba;
11535 struct lpfc_queue *speq;
11536 struct lpfc_eqe *eqe;
11537 unsigned long iflag;
11538 int ecount = 0;
11539
11540 /*
11541 * Get the driver's phba structure from the dev_id
11542 */
11543 phba = (struct lpfc_hba *)dev_id;
11544
11545 if (unlikely(!phba))
11546 return IRQ_NONE;
11547
11548 /* Get to the EQ struct associated with this vector */
11549 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011550 if (unlikely(!speq))
11551 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011552
11553 /* Check device state for handling interrupt */
11554 if (unlikely(lpfc_intr_state_check(phba))) {
11555 /* Check again for link_state with lock held */
11556 spin_lock_irqsave(&phba->hbalock, iflag);
11557 if (phba->link_state < LPFC_LINK_DOWN)
11558 /* Flush, clear interrupt, and rearm the EQ */
11559 lpfc_sli4_eq_flush(phba, speq);
11560 spin_unlock_irqrestore(&phba->hbalock, iflag);
11561 return IRQ_NONE;
11562 }
11563
11564 /*
11565 * Process all the event on FCP slow-path EQ
11566 */
11567 while ((eqe = lpfc_sli4_eq_get(speq))) {
11568 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011569 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011570 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11571 }
11572
11573 /* Always clear and re-arm the slow-path EQ */
11574 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11575
11576 /* Catch the no cq entry condition */
11577 if (unlikely(ecount == 0)) {
11578 if (phba->intr_type == MSIX)
11579 /* MSI-X treated interrupt served as no EQ share INT */
11580 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11581 "0357 MSI-X interrupt with no EQE\n");
11582 else
11583 /* Non MSI-X treated on interrupt as EQ share INT */
11584 return IRQ_NONE;
11585 }
11586
11587 return IRQ_HANDLED;
11588} /* lpfc_sli4_sp_intr_handler */
11589
11590/**
11591 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11592 * @irq: Interrupt number.
11593 * @dev_id: The device context pointer.
11594 *
11595 * This function is directly called from the PCI layer as an interrupt
11596 * service routine when device with SLI-4 interface spec is enabled with
11597 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11598 * ring event in the HBA. However, when the device is enabled with either
11599 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11600 * device-level interrupt handler. When the PCI slot is in error recovery
11601 * or the HBA is undergoing initialization, the interrupt handler will not
11602 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11603 * the intrrupt context. This function is called without any lock held.
11604 * It gets the hbalock to access and update SLI data structures. Note that,
11605 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11606 * equal to that of FCP CQ index.
11607 *
11608 * This function returns IRQ_HANDLED when interrupt is handled else it
11609 * returns IRQ_NONE.
11610 **/
11611irqreturn_t
11612lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11613{
11614 struct lpfc_hba *phba;
11615 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11616 struct lpfc_queue *fpeq;
11617 struct lpfc_eqe *eqe;
11618 unsigned long iflag;
11619 int ecount = 0;
11620 uint32_t fcp_eqidx;
11621
11622 /* Get the driver's phba structure from the dev_id */
11623 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11624 phba = fcp_eq_hdl->phba;
11625 fcp_eqidx = fcp_eq_hdl->idx;
11626
11627 if (unlikely(!phba))
11628 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011629 if (unlikely(!phba->sli4_hba.fp_eq))
11630 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011631
11632 /* Get to the EQ struct associated with this vector */
11633 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011634 if (unlikely(!fpeq))
11635 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011636
11637 /* Check device state for handling interrupt */
11638 if (unlikely(lpfc_intr_state_check(phba))) {
11639 /* Check again for link_state with lock held */
11640 spin_lock_irqsave(&phba->hbalock, iflag);
11641 if (phba->link_state < LPFC_LINK_DOWN)
11642 /* Flush, clear interrupt, and rearm the EQ */
11643 lpfc_sli4_eq_flush(phba, fpeq);
11644 spin_unlock_irqrestore(&phba->hbalock, iflag);
11645 return IRQ_NONE;
11646 }
11647
11648 /*
11649 * Process all the event on FCP fast-path EQ
11650 */
11651 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11652 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011653 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011654 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11655 }
11656
11657 /* Always clear and re-arm the fast-path EQ */
11658 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11659
11660 if (unlikely(ecount == 0)) {
11661 if (phba->intr_type == MSIX)
11662 /* MSI-X treated interrupt served as no EQ share INT */
11663 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11664 "0358 MSI-X interrupt with no EQE\n");
11665 else
11666 /* Non MSI-X treated on interrupt as EQ share INT */
11667 return IRQ_NONE;
11668 }
11669
11670 return IRQ_HANDLED;
11671} /* lpfc_sli4_fp_intr_handler */
11672
11673/**
11674 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11675 * @irq: Interrupt number.
11676 * @dev_id: The device context pointer.
11677 *
11678 * This function is the device-level interrupt handler to device with SLI-4
11679 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11680 * interrupt mode is enabled and there is an event in the HBA which requires
11681 * driver attention. This function invokes the slow-path interrupt attention
11682 * handling function and fast-path interrupt attention handling function in
11683 * turn to process the relevant HBA attention events. This function is called
11684 * without any lock held. It gets the hbalock to access and update SLI data
11685 * structures.
11686 *
11687 * This function returns IRQ_HANDLED when interrupt is handled, else it
11688 * returns IRQ_NONE.
11689 **/
11690irqreturn_t
11691lpfc_sli4_intr_handler(int irq, void *dev_id)
11692{
11693 struct lpfc_hba *phba;
11694 irqreturn_t sp_irq_rc, fp_irq_rc;
11695 bool fp_handled = false;
11696 uint32_t fcp_eqidx;
11697
11698 /* Get the driver's phba structure from the dev_id */
11699 phba = (struct lpfc_hba *)dev_id;
11700
11701 if (unlikely(!phba))
11702 return IRQ_NONE;
11703
11704 /*
11705 * Invokes slow-path host attention interrupt handling as appropriate.
11706 */
11707 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11708
11709 /*
11710 * Invoke fast-path host attention interrupt handling as appropriate.
11711 */
11712 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11713 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11714 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11715 if (fp_irq_rc == IRQ_HANDLED)
11716 fp_handled |= true;
11717 }
11718
11719 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11720} /* lpfc_sli4_intr_handler */
11721
11722/**
11723 * lpfc_sli4_queue_free - free a queue structure and associated memory
11724 * @queue: The queue structure to free.
11725 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011726 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011727 * the host resident queue. This function must be called after destroying the
11728 * queue on the HBA.
11729 **/
11730void
11731lpfc_sli4_queue_free(struct lpfc_queue *queue)
11732{
11733 struct lpfc_dmabuf *dmabuf;
11734
11735 if (!queue)
11736 return;
11737
11738 while (!list_empty(&queue->page_list)) {
11739 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11740 list);
James Smart49198b32010-04-06 15:04:33 -040011741 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011742 dmabuf->virt, dmabuf->phys);
11743 kfree(dmabuf);
11744 }
11745 kfree(queue);
11746 return;
11747}
11748
11749/**
11750 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11751 * @phba: The HBA that this queue is being created on.
11752 * @entry_size: The size of each queue entry for this queue.
11753 * @entry count: The number of entries that this queue will handle.
11754 *
11755 * This function allocates a queue structure and the DMAable memory used for
11756 * the host resident queue. This function must be called before creating the
11757 * queue on the HBA.
11758 **/
11759struct lpfc_queue *
11760lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11761 uint32_t entry_count)
11762{
11763 struct lpfc_queue *queue;
11764 struct lpfc_dmabuf *dmabuf;
11765 int x, total_qe_count;
11766 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011767 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011768
James Smartcb5172e2010-03-15 11:25:07 -040011769 if (!phba->sli4_hba.pc_sli4_params.supported)
11770 hw_page_size = SLI4_PAGE_SIZE;
11771
James Smart4f774512009-05-22 14:52:35 -040011772 queue = kzalloc(sizeof(struct lpfc_queue) +
11773 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11774 if (!queue)
11775 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011776 queue->page_count = (ALIGN(entry_size * entry_count,
11777 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011778 INIT_LIST_HEAD(&queue->list);
11779 INIT_LIST_HEAD(&queue->page_list);
11780 INIT_LIST_HEAD(&queue->child_list);
11781 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11782 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11783 if (!dmabuf)
11784 goto out_fail;
11785 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011786 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011787 GFP_KERNEL);
11788 if (!dmabuf->virt) {
11789 kfree(dmabuf);
11790 goto out_fail;
11791 }
James Smartcb5172e2010-03-15 11:25:07 -040011792 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011793 dmabuf->buffer_tag = x;
11794 list_add_tail(&dmabuf->list, &queue->page_list);
11795 /* initialize queue's entry array */
11796 dma_pointer = dmabuf->virt;
11797 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011798 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011799 total_qe_count++, dma_pointer += entry_size) {
11800 queue->qe[total_qe_count].address = dma_pointer;
11801 }
11802 }
11803 queue->entry_size = entry_size;
11804 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040011805
11806 /*
11807 * entry_repost is calculated based on the number of entries in the
11808 * queue. This works out except for RQs. If buffers are NOT initially
11809 * posted for every RQE, entry_repost should be adjusted accordingly.
11810 */
11811 queue->entry_repost = (entry_count >> 3);
11812 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11813 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040011814 queue->phba = phba;
11815
11816 return queue;
11817out_fail:
11818 lpfc_sli4_queue_free(queue);
11819 return NULL;
11820}
11821
11822/**
11823 * lpfc_eq_create - Create an Event Queue on the HBA
11824 * @phba: HBA structure that indicates port to create a queue on.
11825 * @eq: The queue structure to use to create the event queue.
11826 * @imax: The maximum interrupt per second limit.
11827 *
11828 * This function creates an event queue, as detailed in @eq, on a port,
11829 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11830 *
11831 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11832 * is used to get the entry count and entry size that are necessary to
11833 * determine the number of pages to allocate and use for this queue. This
11834 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11835 * event queue. This function is asynchronous and will wait for the mailbox
11836 * command to finish before continuing.
11837 *
11838 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011839 * memory this function will return -ENOMEM. If the queue create mailbox command
11840 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011841 **/
11842uint32_t
11843lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11844{
11845 struct lpfc_mbx_eq_create *eq_create;
11846 LPFC_MBOXQ_t *mbox;
11847 int rc, length, status = 0;
11848 struct lpfc_dmabuf *dmabuf;
11849 uint32_t shdr_status, shdr_add_status;
11850 union lpfc_sli4_cfg_shdr *shdr;
11851 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011852 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11853
James Smart2e90f4b2011-12-13 13:22:37 -050011854 /* sanity check on queue memory */
11855 if (!eq)
11856 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040011857 if (!phba->sli4_hba.pc_sli4_params.supported)
11858 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011859
11860 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11861 if (!mbox)
11862 return -ENOMEM;
11863 length = (sizeof(struct lpfc_mbx_eq_create) -
11864 sizeof(struct lpfc_sli4_cfg_mhdr));
11865 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11866 LPFC_MBOX_OPCODE_EQ_CREATE,
11867 length, LPFC_SLI4_MBX_EMBED);
11868 eq_create = &mbox->u.mqe.un.eq_create;
11869 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11870 eq->page_count);
11871 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11872 LPFC_EQE_SIZE);
11873 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11874 /* Calculate delay multiper from maximum interrupt per second */
11875 dmult = LPFC_DMULT_CONST/imax - 1;
11876 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11877 dmult);
11878 switch (eq->entry_count) {
11879 default:
11880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11881 "0360 Unsupported EQ count. (%d)\n",
11882 eq->entry_count);
11883 if (eq->entry_count < 256)
11884 return -EINVAL;
11885 /* otherwise default to smallest count (drop through) */
11886 case 256:
11887 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11888 LPFC_EQ_CNT_256);
11889 break;
11890 case 512:
11891 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11892 LPFC_EQ_CNT_512);
11893 break;
11894 case 1024:
11895 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11896 LPFC_EQ_CNT_1024);
11897 break;
11898 case 2048:
11899 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11900 LPFC_EQ_CNT_2048);
11901 break;
11902 case 4096:
11903 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11904 LPFC_EQ_CNT_4096);
11905 break;
11906 }
11907 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011908 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011909 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11910 putPaddrLow(dmabuf->phys);
11911 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11912 putPaddrHigh(dmabuf->phys);
11913 }
11914 mbox->vport = phba->pport;
11915 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11916 mbox->context1 = NULL;
11917 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11918 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11919 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11920 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11921 if (shdr_status || shdr_add_status || rc) {
11922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11923 "2500 EQ_CREATE mailbox failed with "
11924 "status x%x add_status x%x, mbx status x%x\n",
11925 shdr_status, shdr_add_status, rc);
11926 status = -ENXIO;
11927 }
11928 eq->type = LPFC_EQ;
11929 eq->subtype = LPFC_NONE;
11930 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11931 if (eq->queue_id == 0xFFFF)
11932 status = -ENXIO;
11933 eq->host_index = 0;
11934 eq->hba_index = 0;
11935
James Smart8fa38512009-07-19 10:01:03 -040011936 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011937 return status;
11938}
11939
11940/**
11941 * lpfc_cq_create - Create a Completion Queue on the HBA
11942 * @phba: HBA structure that indicates port to create a queue on.
11943 * @cq: The queue structure to use to create the completion queue.
11944 * @eq: The event queue to bind this completion queue to.
11945 *
11946 * This function creates a completion queue, as detailed in @wq, on a port,
11947 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11948 *
11949 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11950 * is used to get the entry count and entry size that are necessary to
11951 * determine the number of pages to allocate and use for this queue. The @eq
11952 * is used to indicate which event queue to bind this completion queue to. This
11953 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11954 * completion queue. This function is asynchronous and will wait for the mailbox
11955 * command to finish before continuing.
11956 *
11957 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011958 * memory this function will return -ENOMEM. If the queue create mailbox command
11959 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011960 **/
11961uint32_t
11962lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11963 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11964{
11965 struct lpfc_mbx_cq_create *cq_create;
11966 struct lpfc_dmabuf *dmabuf;
11967 LPFC_MBOXQ_t *mbox;
11968 int rc, length, status = 0;
11969 uint32_t shdr_status, shdr_add_status;
11970 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011971 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11972
James Smart2e90f4b2011-12-13 13:22:37 -050011973 /* sanity check on queue memory */
11974 if (!cq || !eq)
11975 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040011976 if (!phba->sli4_hba.pc_sli4_params.supported)
11977 hw_page_size = SLI4_PAGE_SIZE;
11978
James Smart4f774512009-05-22 14:52:35 -040011979 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11980 if (!mbox)
11981 return -ENOMEM;
11982 length = (sizeof(struct lpfc_mbx_cq_create) -
11983 sizeof(struct lpfc_sli4_cfg_mhdr));
11984 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11985 LPFC_MBOX_OPCODE_CQ_CREATE,
11986 length, LPFC_SLI4_MBX_EMBED);
11987 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011988 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011989 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11990 cq->page_count);
11991 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11992 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011993 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11994 phba->sli4_hba.pc_sli4_params.cqv);
11995 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040011996 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11997 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011998 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11999 eq->queue_id);
12000 } else {
12001 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12002 eq->queue_id);
12003 }
James Smart4f774512009-05-22 14:52:35 -040012004 switch (cq->entry_count) {
12005 default:
12006 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12007 "0361 Unsupported CQ count. (%d)\n",
12008 cq->entry_count);
12009 if (cq->entry_count < 256)
12010 return -EINVAL;
12011 /* otherwise default to smallest count (drop through) */
12012 case 256:
12013 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12014 LPFC_CQ_CNT_256);
12015 break;
12016 case 512:
12017 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12018 LPFC_CQ_CNT_512);
12019 break;
12020 case 1024:
12021 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12022 LPFC_CQ_CNT_1024);
12023 break;
12024 }
12025 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012026 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012027 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12028 putPaddrLow(dmabuf->phys);
12029 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12030 putPaddrHigh(dmabuf->phys);
12031 }
12032 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12033
12034 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012035 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12036 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12037 if (shdr_status || shdr_add_status || rc) {
12038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12039 "2501 CQ_CREATE mailbox failed with "
12040 "status x%x add_status x%x, mbx status x%x\n",
12041 shdr_status, shdr_add_status, rc);
12042 status = -ENXIO;
12043 goto out;
12044 }
12045 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12046 if (cq->queue_id == 0xFFFF) {
12047 status = -ENXIO;
12048 goto out;
12049 }
12050 /* link the cq onto the parent eq child list */
12051 list_add_tail(&cq->list, &eq->child_list);
12052 /* Set up completion queue's type and subtype */
12053 cq->type = type;
12054 cq->subtype = subtype;
12055 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012056 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012057 cq->host_index = 0;
12058 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012059
James Smart8fa38512009-07-19 10:01:03 -040012060out:
12061 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012062 return status;
12063}
12064
12065/**
James Smartb19a0612010-04-06 14:48:51 -040012066 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012067 * @phba: HBA structure that indicates port to create a queue on.
12068 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012069 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12070 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012071 *
James Smartb19a0612010-04-06 14:48:51 -040012072 * This function provides failback (fb) functionality when the
12073 * mq_create_ext fails on older FW generations. It's purpose is identical
12074 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012075 *
James Smartb19a0612010-04-06 14:48:51 -040012076 * This routine cannot fail as all attributes were previously accessed and
12077 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012078 **/
James Smartb19a0612010-04-06 14:48:51 -040012079static void
12080lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12081 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012082{
12083 struct lpfc_mbx_mq_create *mq_create;
12084 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012085 int length;
James Smart04c68492009-05-22 14:52:52 -040012086
James Smart04c68492009-05-22 14:52:52 -040012087 length = (sizeof(struct lpfc_mbx_mq_create) -
12088 sizeof(struct lpfc_sli4_cfg_mhdr));
12089 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12090 LPFC_MBOX_OPCODE_MQ_CREATE,
12091 length, LPFC_SLI4_MBX_EMBED);
12092 mq_create = &mbox->u.mqe.un.mq_create;
12093 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012094 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012095 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012096 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012097 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12098 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012099 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012100 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12101 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012102 break;
12103 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012104 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12105 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012106 break;
12107 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012108 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12109 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012110 break;
12111 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012112 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12113 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012114 break;
12115 }
12116 list_for_each_entry(dmabuf, &mq->page_list, list) {
12117 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012118 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012119 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012120 putPaddrHigh(dmabuf->phys);
12121 }
12122}
12123
12124/**
12125 * lpfc_mq_create - Create a mailbox Queue on the HBA
12126 * @phba: HBA structure that indicates port to create a queue on.
12127 * @mq: The queue structure to use to create the mailbox queue.
12128 * @cq: The completion queue to associate with this cq.
12129 * @subtype: The queue's subtype.
12130 *
12131 * This function creates a mailbox queue, as detailed in @mq, on a port,
12132 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12133 *
12134 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12135 * is used to get the entry count and entry size that are necessary to
12136 * determine the number of pages to allocate and use for this queue. This
12137 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12138 * mailbox queue. This function is asynchronous and will wait for the mailbox
12139 * command to finish before continuing.
12140 *
12141 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012142 * memory this function will return -ENOMEM. If the queue create mailbox command
12143 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012144 **/
12145int32_t
12146lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12147 struct lpfc_queue *cq, uint32_t subtype)
12148{
12149 struct lpfc_mbx_mq_create *mq_create;
12150 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12151 struct lpfc_dmabuf *dmabuf;
12152 LPFC_MBOXQ_t *mbox;
12153 int rc, length, status = 0;
12154 uint32_t shdr_status, shdr_add_status;
12155 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012156 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012157
James Smart2e90f4b2011-12-13 13:22:37 -050012158 /* sanity check on queue memory */
12159 if (!mq || !cq)
12160 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012161 if (!phba->sli4_hba.pc_sli4_params.supported)
12162 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012163
12164 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12165 if (!mbox)
12166 return -ENOMEM;
12167 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12168 sizeof(struct lpfc_sli4_cfg_mhdr));
12169 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12170 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12171 length, LPFC_SLI4_MBX_EMBED);
12172
12173 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012174 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012175 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12176 &mq_create_ext->u.request, mq->page_count);
12177 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12178 &mq_create_ext->u.request, 1);
12179 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012180 &mq_create_ext->u.request, 1);
12181 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12182 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012183 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12184 &mq_create_ext->u.request, 1);
12185 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12186 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012187 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012188 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12189 phba->sli4_hba.pc_sli4_params.mqv);
12190 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12191 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12192 cq->queue_id);
12193 else
12194 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12195 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012196 switch (mq->entry_count) {
12197 default:
12198 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12199 "0362 Unsupported MQ count. (%d)\n",
12200 mq->entry_count);
12201 if (mq->entry_count < 16)
12202 return -EINVAL;
12203 /* otherwise default to smallest count (drop through) */
12204 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012205 bf_set(lpfc_mq_context_ring_size,
12206 &mq_create_ext->u.request.context,
12207 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012208 break;
12209 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012210 bf_set(lpfc_mq_context_ring_size,
12211 &mq_create_ext->u.request.context,
12212 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012213 break;
12214 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012215 bf_set(lpfc_mq_context_ring_size,
12216 &mq_create_ext->u.request.context,
12217 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012218 break;
12219 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012220 bf_set(lpfc_mq_context_ring_size,
12221 &mq_create_ext->u.request.context,
12222 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012223 break;
12224 }
12225 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012226 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012227 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12228 putPaddrLow(dmabuf->phys);
12229 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012230 putPaddrHigh(dmabuf->phys);
12231 }
12232 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012233 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12234 &mq_create_ext->u.response);
12235 if (rc != MBX_SUCCESS) {
12236 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12237 "2795 MQ_CREATE_EXT failed with "
12238 "status x%x. Failback to MQ_CREATE.\n",
12239 rc);
12240 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12241 mq_create = &mbox->u.mqe.un.mq_create;
12242 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12243 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12244 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12245 &mq_create->u.response);
12246 }
12247
James Smart04c68492009-05-22 14:52:52 -040012248 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012249 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12250 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12251 if (shdr_status || shdr_add_status || rc) {
12252 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12253 "2502 MQ_CREATE mailbox failed with "
12254 "status x%x add_status x%x, mbx status x%x\n",
12255 shdr_status, shdr_add_status, rc);
12256 status = -ENXIO;
12257 goto out;
12258 }
James Smart04c68492009-05-22 14:52:52 -040012259 if (mq->queue_id == 0xFFFF) {
12260 status = -ENXIO;
12261 goto out;
12262 }
12263 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012264 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012265 mq->subtype = subtype;
12266 mq->host_index = 0;
12267 mq->hba_index = 0;
12268
12269 /* link the mq onto the parent cq child list */
12270 list_add_tail(&mq->list, &cq->child_list);
12271out:
James Smart8fa38512009-07-19 10:01:03 -040012272 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012273 return status;
12274}
12275
12276/**
James Smart4f774512009-05-22 14:52:35 -040012277 * lpfc_wq_create - Create a Work Queue on the HBA
12278 * @phba: HBA structure that indicates port to create a queue on.
12279 * @wq: The queue structure to use to create the work queue.
12280 * @cq: The completion queue to bind this work queue to.
12281 * @subtype: The subtype of the work queue indicating its functionality.
12282 *
12283 * This function creates a work queue, as detailed in @wq, on a port, described
12284 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12285 *
12286 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12287 * is used to get the entry count and entry size that are necessary to
12288 * determine the number of pages to allocate and use for this queue. The @cq
12289 * is used to indicate which completion queue to bind this work queue to. This
12290 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12291 * work queue. This function is asynchronous and will wait for the mailbox
12292 * command to finish before continuing.
12293 *
12294 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012295 * memory this function will return -ENOMEM. If the queue create mailbox command
12296 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012297 **/
12298uint32_t
12299lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12300 struct lpfc_queue *cq, uint32_t subtype)
12301{
12302 struct lpfc_mbx_wq_create *wq_create;
12303 struct lpfc_dmabuf *dmabuf;
12304 LPFC_MBOXQ_t *mbox;
12305 int rc, length, status = 0;
12306 uint32_t shdr_status, shdr_add_status;
12307 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012308 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012309 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012310
James Smart2e90f4b2011-12-13 13:22:37 -050012311 /* sanity check on queue memory */
12312 if (!wq || !cq)
12313 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012314 if (!phba->sli4_hba.pc_sli4_params.supported)
12315 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012316
12317 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12318 if (!mbox)
12319 return -ENOMEM;
12320 length = (sizeof(struct lpfc_mbx_wq_create) -
12321 sizeof(struct lpfc_sli4_cfg_mhdr));
12322 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12323 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12324 length, LPFC_SLI4_MBX_EMBED);
12325 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012326 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012327 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12328 wq->page_count);
12329 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12330 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012331 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12332 phba->sli4_hba.pc_sli4_params.wqv);
12333 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12334 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12335 wq->entry_count);
12336 switch (wq->entry_size) {
12337 default:
12338 case 64:
12339 bf_set(lpfc_mbx_wq_create_wqe_size,
12340 &wq_create->u.request_1,
12341 LPFC_WQ_WQE_SIZE_64);
12342 break;
12343 case 128:
12344 bf_set(lpfc_mbx_wq_create_wqe_size,
12345 &wq_create->u.request_1,
12346 LPFC_WQ_WQE_SIZE_128);
12347 break;
12348 }
12349 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12350 (PAGE_SIZE/SLI4_PAGE_SIZE));
12351 page = wq_create->u.request_1.page;
12352 } else {
12353 page = wq_create->u.request.page;
12354 }
James Smart4f774512009-05-22 14:52:35 -040012355 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012356 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012357 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12358 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012359 }
12360 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12361 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012362 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12363 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12364 if (shdr_status || shdr_add_status || rc) {
12365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12366 "2503 WQ_CREATE mailbox failed with "
12367 "status x%x add_status x%x, mbx status x%x\n",
12368 shdr_status, shdr_add_status, rc);
12369 status = -ENXIO;
12370 goto out;
12371 }
12372 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12373 if (wq->queue_id == 0xFFFF) {
12374 status = -ENXIO;
12375 goto out;
12376 }
12377 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012378 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012379 wq->subtype = subtype;
12380 wq->host_index = 0;
12381 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012382 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012383
12384 /* link the wq onto the parent cq child list */
12385 list_add_tail(&wq->list, &cq->child_list);
12386out:
James Smart8fa38512009-07-19 10:01:03 -040012387 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012388 return status;
12389}
12390
12391/**
James Smart73d91e52011-10-10 21:32:10 -040012392 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12393 * @phba: HBA structure that indicates port to create a queue on.
12394 * @rq: The queue structure to use for the receive queue.
12395 * @qno: The associated HBQ number
12396 *
12397 *
12398 * For SLI4 we need to adjust the RQ repost value based on
12399 * the number of buffers that are initially posted to the RQ.
12400 */
12401void
12402lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12403{
12404 uint32_t cnt;
12405
James Smart2e90f4b2011-12-13 13:22:37 -050012406 /* sanity check on queue memory */
12407 if (!rq)
12408 return;
James Smart73d91e52011-10-10 21:32:10 -040012409 cnt = lpfc_hbq_defs[qno]->entry_count;
12410
12411 /* Recalc repost for RQs based on buffers initially posted */
12412 cnt = (cnt >> 3);
12413 if (cnt < LPFC_QUEUE_MIN_REPOST)
12414 cnt = LPFC_QUEUE_MIN_REPOST;
12415
12416 rq->entry_repost = cnt;
12417}
12418
12419/**
James Smart4f774512009-05-22 14:52:35 -040012420 * lpfc_rq_create - Create a Receive Queue on the HBA
12421 * @phba: HBA structure that indicates port to create a queue on.
12422 * @hrq: The queue structure to use to create the header receive queue.
12423 * @drq: The queue structure to use to create the data receive queue.
12424 * @cq: The completion queue to bind this work queue to.
12425 *
12426 * This function creates a receive buffer queue pair , as detailed in @hrq and
12427 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12428 * to the HBA.
12429 *
12430 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12431 * struct is used to get the entry count that is necessary to determine the
12432 * number of pages to use for this queue. The @cq is used to indicate which
12433 * completion queue to bind received buffers that are posted to these queues to.
12434 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12435 * receive queue pair. This function is asynchronous and will wait for the
12436 * mailbox command to finish before continuing.
12437 *
12438 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012439 * memory this function will return -ENOMEM. If the queue create mailbox command
12440 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012441 **/
12442uint32_t
12443lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12444 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12445{
12446 struct lpfc_mbx_rq_create *rq_create;
12447 struct lpfc_dmabuf *dmabuf;
12448 LPFC_MBOXQ_t *mbox;
12449 int rc, length, status = 0;
12450 uint32_t shdr_status, shdr_add_status;
12451 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012452 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12453
James Smart2e90f4b2011-12-13 13:22:37 -050012454 /* sanity check on queue memory */
12455 if (!hrq || !drq || !cq)
12456 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012457 if (!phba->sli4_hba.pc_sli4_params.supported)
12458 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012459
12460 if (hrq->entry_count != drq->entry_count)
12461 return -EINVAL;
12462 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12463 if (!mbox)
12464 return -ENOMEM;
12465 length = (sizeof(struct lpfc_mbx_rq_create) -
12466 sizeof(struct lpfc_sli4_cfg_mhdr));
12467 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12468 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12469 length, LPFC_SLI4_MBX_EMBED);
12470 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012471 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12472 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12473 phba->sli4_hba.pc_sli4_params.rqv);
12474 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12475 bf_set(lpfc_rq_context_rqe_count_1,
12476 &rq_create->u.request.context,
12477 hrq->entry_count);
12478 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012479 bf_set(lpfc_rq_context_rqe_size,
12480 &rq_create->u.request.context,
12481 LPFC_RQE_SIZE_8);
12482 bf_set(lpfc_rq_context_page_size,
12483 &rq_create->u.request.context,
12484 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012485 } else {
12486 switch (hrq->entry_count) {
12487 default:
12488 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12489 "2535 Unsupported RQ count. (%d)\n",
12490 hrq->entry_count);
12491 if (hrq->entry_count < 512)
12492 return -EINVAL;
12493 /* otherwise default to smallest count (drop through) */
12494 case 512:
12495 bf_set(lpfc_rq_context_rqe_count,
12496 &rq_create->u.request.context,
12497 LPFC_RQ_RING_SIZE_512);
12498 break;
12499 case 1024:
12500 bf_set(lpfc_rq_context_rqe_count,
12501 &rq_create->u.request.context,
12502 LPFC_RQ_RING_SIZE_1024);
12503 break;
12504 case 2048:
12505 bf_set(lpfc_rq_context_rqe_count,
12506 &rq_create->u.request.context,
12507 LPFC_RQ_RING_SIZE_2048);
12508 break;
12509 case 4096:
12510 bf_set(lpfc_rq_context_rqe_count,
12511 &rq_create->u.request.context,
12512 LPFC_RQ_RING_SIZE_4096);
12513 break;
12514 }
12515 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12516 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012517 }
12518 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12519 cq->queue_id);
12520 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12521 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012522 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012523 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012524 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12525 putPaddrLow(dmabuf->phys);
12526 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12527 putPaddrHigh(dmabuf->phys);
12528 }
12529 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12530 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012531 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12532 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12533 if (shdr_status || shdr_add_status || rc) {
12534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12535 "2504 RQ_CREATE mailbox failed with "
12536 "status x%x add_status x%x, mbx status x%x\n",
12537 shdr_status, shdr_add_status, rc);
12538 status = -ENXIO;
12539 goto out;
12540 }
12541 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12542 if (hrq->queue_id == 0xFFFF) {
12543 status = -ENXIO;
12544 goto out;
12545 }
12546 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012547 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012548 hrq->subtype = subtype;
12549 hrq->host_index = 0;
12550 hrq->hba_index = 0;
12551
12552 /* now create the data queue */
12553 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12554 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12555 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012556 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12557 phba->sli4_hba.pc_sli4_params.rqv);
12558 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12559 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012560 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012561 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012562 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12563 LPFC_RQE_SIZE_8);
12564 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12565 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012566 } else {
12567 switch (drq->entry_count) {
12568 default:
12569 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12570 "2536 Unsupported RQ count. (%d)\n",
12571 drq->entry_count);
12572 if (drq->entry_count < 512)
12573 return -EINVAL;
12574 /* otherwise default to smallest count (drop through) */
12575 case 512:
12576 bf_set(lpfc_rq_context_rqe_count,
12577 &rq_create->u.request.context,
12578 LPFC_RQ_RING_SIZE_512);
12579 break;
12580 case 1024:
12581 bf_set(lpfc_rq_context_rqe_count,
12582 &rq_create->u.request.context,
12583 LPFC_RQ_RING_SIZE_1024);
12584 break;
12585 case 2048:
12586 bf_set(lpfc_rq_context_rqe_count,
12587 &rq_create->u.request.context,
12588 LPFC_RQ_RING_SIZE_2048);
12589 break;
12590 case 4096:
12591 bf_set(lpfc_rq_context_rqe_count,
12592 &rq_create->u.request.context,
12593 LPFC_RQ_RING_SIZE_4096);
12594 break;
12595 }
12596 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12597 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012598 }
12599 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12600 cq->queue_id);
12601 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12602 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012603 list_for_each_entry(dmabuf, &drq->page_list, list) {
12604 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12605 putPaddrLow(dmabuf->phys);
12606 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12607 putPaddrHigh(dmabuf->phys);
12608 }
12609 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12610 /* The IOCTL status is embedded in the mailbox subheader. */
12611 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12612 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12613 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12614 if (shdr_status || shdr_add_status || rc) {
12615 status = -ENXIO;
12616 goto out;
12617 }
12618 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12619 if (drq->queue_id == 0xFFFF) {
12620 status = -ENXIO;
12621 goto out;
12622 }
12623 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012624 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012625 drq->subtype = subtype;
12626 drq->host_index = 0;
12627 drq->hba_index = 0;
12628
12629 /* link the header and data RQs onto the parent cq child list */
12630 list_add_tail(&hrq->list, &cq->child_list);
12631 list_add_tail(&drq->list, &cq->child_list);
12632
12633out:
James Smart8fa38512009-07-19 10:01:03 -040012634 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012635 return status;
12636}
12637
12638/**
12639 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12640 * @eq: The queue structure associated with the queue to destroy.
12641 *
12642 * This function destroys a queue, as detailed in @eq by sending an mailbox
12643 * command, specific to the type of queue, to the HBA.
12644 *
12645 * The @eq struct is used to get the queue ID of the queue to destroy.
12646 *
12647 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012648 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012649 **/
12650uint32_t
12651lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12652{
12653 LPFC_MBOXQ_t *mbox;
12654 int rc, length, status = 0;
12655 uint32_t shdr_status, shdr_add_status;
12656 union lpfc_sli4_cfg_shdr *shdr;
12657
James Smart2e90f4b2011-12-13 13:22:37 -050012658 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012659 if (!eq)
12660 return -ENODEV;
12661 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12662 if (!mbox)
12663 return -ENOMEM;
12664 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12665 sizeof(struct lpfc_sli4_cfg_mhdr));
12666 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12667 LPFC_MBOX_OPCODE_EQ_DESTROY,
12668 length, LPFC_SLI4_MBX_EMBED);
12669 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12670 eq->queue_id);
12671 mbox->vport = eq->phba->pport;
12672 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12673
12674 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12675 /* The IOCTL status is embedded in the mailbox subheader. */
12676 shdr = (union lpfc_sli4_cfg_shdr *)
12677 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12678 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12679 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12680 if (shdr_status || shdr_add_status || rc) {
12681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12682 "2505 EQ_DESTROY mailbox failed with "
12683 "status x%x add_status x%x, mbx status x%x\n",
12684 shdr_status, shdr_add_status, rc);
12685 status = -ENXIO;
12686 }
12687
12688 /* Remove eq from any list */
12689 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012690 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012691 return status;
12692}
12693
12694/**
12695 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12696 * @cq: The queue structure associated with the queue to destroy.
12697 *
12698 * This function destroys a queue, as detailed in @cq by sending an mailbox
12699 * command, specific to the type of queue, to the HBA.
12700 *
12701 * The @cq struct is used to get the queue ID of the queue to destroy.
12702 *
12703 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012704 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012705 **/
12706uint32_t
12707lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12708{
12709 LPFC_MBOXQ_t *mbox;
12710 int rc, length, status = 0;
12711 uint32_t shdr_status, shdr_add_status;
12712 union lpfc_sli4_cfg_shdr *shdr;
12713
James Smart2e90f4b2011-12-13 13:22:37 -050012714 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012715 if (!cq)
12716 return -ENODEV;
12717 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12718 if (!mbox)
12719 return -ENOMEM;
12720 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12721 sizeof(struct lpfc_sli4_cfg_mhdr));
12722 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12723 LPFC_MBOX_OPCODE_CQ_DESTROY,
12724 length, LPFC_SLI4_MBX_EMBED);
12725 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12726 cq->queue_id);
12727 mbox->vport = cq->phba->pport;
12728 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12729 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12730 /* The IOCTL status is embedded in the mailbox subheader. */
12731 shdr = (union lpfc_sli4_cfg_shdr *)
12732 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12733 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12734 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12735 if (shdr_status || shdr_add_status || rc) {
12736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12737 "2506 CQ_DESTROY mailbox failed with "
12738 "status x%x add_status x%x, mbx status x%x\n",
12739 shdr_status, shdr_add_status, rc);
12740 status = -ENXIO;
12741 }
12742 /* Remove cq from any list */
12743 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012744 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012745 return status;
12746}
12747
12748/**
James Smart04c68492009-05-22 14:52:52 -040012749 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12750 * @qm: The queue structure associated with the queue to destroy.
12751 *
12752 * This function destroys a queue, as detailed in @mq by sending an mailbox
12753 * command, specific to the type of queue, to the HBA.
12754 *
12755 * The @mq struct is used to get the queue ID of the queue to destroy.
12756 *
12757 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012758 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012759 **/
12760uint32_t
12761lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12762{
12763 LPFC_MBOXQ_t *mbox;
12764 int rc, length, status = 0;
12765 uint32_t shdr_status, shdr_add_status;
12766 union lpfc_sli4_cfg_shdr *shdr;
12767
James Smart2e90f4b2011-12-13 13:22:37 -050012768 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040012769 if (!mq)
12770 return -ENODEV;
12771 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12772 if (!mbox)
12773 return -ENOMEM;
12774 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12775 sizeof(struct lpfc_sli4_cfg_mhdr));
12776 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12777 LPFC_MBOX_OPCODE_MQ_DESTROY,
12778 length, LPFC_SLI4_MBX_EMBED);
12779 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12780 mq->queue_id);
12781 mbox->vport = mq->phba->pport;
12782 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12783 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12784 /* The IOCTL status is embedded in the mailbox subheader. */
12785 shdr = (union lpfc_sli4_cfg_shdr *)
12786 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12787 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12788 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12789 if (shdr_status || shdr_add_status || rc) {
12790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12791 "2507 MQ_DESTROY mailbox failed with "
12792 "status x%x add_status x%x, mbx status x%x\n",
12793 shdr_status, shdr_add_status, rc);
12794 status = -ENXIO;
12795 }
12796 /* Remove mq from any list */
12797 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012798 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012799 return status;
12800}
12801
12802/**
James Smart4f774512009-05-22 14:52:35 -040012803 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12804 * @wq: The queue structure associated with the queue to destroy.
12805 *
12806 * This function destroys a queue, as detailed in @wq by sending an mailbox
12807 * command, specific to the type of queue, to the HBA.
12808 *
12809 * The @wq struct is used to get the queue ID of the queue to destroy.
12810 *
12811 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012812 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012813 **/
12814uint32_t
12815lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12816{
12817 LPFC_MBOXQ_t *mbox;
12818 int rc, length, status = 0;
12819 uint32_t shdr_status, shdr_add_status;
12820 union lpfc_sli4_cfg_shdr *shdr;
12821
James Smart2e90f4b2011-12-13 13:22:37 -050012822 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012823 if (!wq)
12824 return -ENODEV;
12825 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12826 if (!mbox)
12827 return -ENOMEM;
12828 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12829 sizeof(struct lpfc_sli4_cfg_mhdr));
12830 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12831 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12832 length, LPFC_SLI4_MBX_EMBED);
12833 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12834 wq->queue_id);
12835 mbox->vport = wq->phba->pport;
12836 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12837 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12838 shdr = (union lpfc_sli4_cfg_shdr *)
12839 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12840 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12841 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12842 if (shdr_status || shdr_add_status || rc) {
12843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12844 "2508 WQ_DESTROY mailbox failed with "
12845 "status x%x add_status x%x, mbx status x%x\n",
12846 shdr_status, shdr_add_status, rc);
12847 status = -ENXIO;
12848 }
12849 /* Remove wq from any list */
12850 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012851 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012852 return status;
12853}
12854
12855/**
12856 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12857 * @rq: The queue structure associated with the queue to destroy.
12858 *
12859 * This function destroys a queue, as detailed in @rq by sending an mailbox
12860 * command, specific to the type of queue, to the HBA.
12861 *
12862 * The @rq struct is used to get the queue ID of the queue to destroy.
12863 *
12864 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012865 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012866 **/
12867uint32_t
12868lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12869 struct lpfc_queue *drq)
12870{
12871 LPFC_MBOXQ_t *mbox;
12872 int rc, length, status = 0;
12873 uint32_t shdr_status, shdr_add_status;
12874 union lpfc_sli4_cfg_shdr *shdr;
12875
James Smart2e90f4b2011-12-13 13:22:37 -050012876 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012877 if (!hrq || !drq)
12878 return -ENODEV;
12879 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12880 if (!mbox)
12881 return -ENOMEM;
12882 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012883 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012884 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12885 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12886 length, LPFC_SLI4_MBX_EMBED);
12887 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12888 hrq->queue_id);
12889 mbox->vport = hrq->phba->pport;
12890 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12891 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12892 /* The IOCTL status is embedded in the mailbox subheader. */
12893 shdr = (union lpfc_sli4_cfg_shdr *)
12894 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12895 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12896 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12897 if (shdr_status || shdr_add_status || rc) {
12898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12899 "2509 RQ_DESTROY mailbox failed with "
12900 "status x%x add_status x%x, mbx status x%x\n",
12901 shdr_status, shdr_add_status, rc);
12902 if (rc != MBX_TIMEOUT)
12903 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12904 return -ENXIO;
12905 }
12906 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12907 drq->queue_id);
12908 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12909 shdr = (union lpfc_sli4_cfg_shdr *)
12910 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12911 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12912 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12913 if (shdr_status || shdr_add_status || rc) {
12914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12915 "2510 RQ_DESTROY mailbox failed with "
12916 "status x%x add_status x%x, mbx status x%x\n",
12917 shdr_status, shdr_add_status, rc);
12918 status = -ENXIO;
12919 }
12920 list_del_init(&hrq->list);
12921 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012922 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012923 return status;
12924}
12925
12926/**
12927 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12928 * @phba: The virtual port for which this call being executed.
12929 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12930 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12931 * @xritag: the xritag that ties this io to the SGL pages.
12932 *
12933 * This routine will post the sgl pages for the IO that has the xritag
12934 * that is in the iocbq structure. The xritag is assigned during iocbq
12935 * creation and persists for as long as the driver is loaded.
12936 * if the caller has fewer than 256 scatter gather segments to map then
12937 * pdma_phys_addr1 should be 0.
12938 * If the caller needs to map more than 256 scatter gather segment then
12939 * pdma_phys_addr1 should be a valid physical address.
12940 * physical address for SGLs must be 64 byte aligned.
12941 * If you are going to map 2 SGL's then the first one must have 256 entries
12942 * the second sgl can have between 1 and 256 entries.
12943 *
12944 * Return codes:
12945 * 0 - Success
12946 * -ENXIO, -ENOMEM - Failure
12947 **/
12948int
12949lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12950 dma_addr_t pdma_phys_addr0,
12951 dma_addr_t pdma_phys_addr1,
12952 uint16_t xritag)
12953{
12954 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12955 LPFC_MBOXQ_t *mbox;
12956 int rc;
12957 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040012958 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040012959 union lpfc_sli4_cfg_shdr *shdr;
12960
12961 if (xritag == NO_XRI) {
12962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12963 "0364 Invalid param:\n");
12964 return -EINVAL;
12965 }
12966
12967 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12968 if (!mbox)
12969 return -ENOMEM;
12970
12971 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12972 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12973 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050012974 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040012975
12976 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12977 &mbox->u.mqe.un.post_sgl_pages;
12978 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12979 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12980
12981 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12982 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12983 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12984 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12985
12986 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12987 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12988 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12989 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12990 if (!phba->sli4_hba.intr_enable)
12991 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040012992 else {
James Smarta183a152011-10-10 21:32:43 -040012993 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040012994 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12995 }
James Smart4f774512009-05-22 14:52:35 -040012996 /* The IOCTL status is embedded in the mailbox subheader. */
12997 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12998 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12999 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13000 if (rc != MBX_TIMEOUT)
13001 mempool_free(mbox, phba->mbox_mem_pool);
13002 if (shdr_status || shdr_add_status || rc) {
13003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13004 "2511 POST_SGL mailbox failed with "
13005 "status x%x add_status x%x, mbx status x%x\n",
13006 shdr_status, shdr_add_status, rc);
13007 rc = -ENXIO;
13008 }
13009 return 0;
13010}
James Smart4f774512009-05-22 14:52:35 -040013011
13012/**
James Smart88a2cfb2011-07-22 18:36:33 -040013013 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013014 * @phba: pointer to lpfc hba data structure.
13015 *
13016 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013017 * HBA consistent with the SLI-4 interface spec. This routine
13018 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13019 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013020 *
James Smart88a2cfb2011-07-22 18:36:33 -040013021 * Returns
13022 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13023 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13024 **/
James Smart6d368e52011-05-24 11:44:12 -040013025uint16_t
13026lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13027{
13028 unsigned long xri;
13029
13030 /*
13031 * Fetch the next logical xri. Because this index is logical,
13032 * the driver starts at 0 each time.
13033 */
13034 spin_lock_irq(&phba->hbalock);
13035 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13036 phba->sli4_hba.max_cfg_param.max_xri, 0);
13037 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13038 spin_unlock_irq(&phba->hbalock);
13039 return NO_XRI;
13040 } else {
13041 set_bit(xri, phba->sli4_hba.xri_bmask);
13042 phba->sli4_hba.max_cfg_param.xri_used++;
13043 phba->sli4_hba.xri_count++;
13044 }
13045
13046 spin_unlock_irq(&phba->hbalock);
13047 return xri;
13048}
13049
13050/**
13051 * lpfc_sli4_free_xri - Release an xri for reuse.
13052 * @phba: pointer to lpfc hba data structure.
13053 *
13054 * This routine is invoked to release an xri to the pool of
13055 * available rpis maintained by the driver.
13056 **/
13057void
13058__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13059{
13060 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
13061 phba->sli4_hba.xri_count--;
13062 phba->sli4_hba.max_cfg_param.xri_used--;
13063 }
13064}
13065
13066/**
13067 * lpfc_sli4_free_xri - Release an xri for reuse.
13068 * @phba: pointer to lpfc hba data structure.
13069 *
13070 * This routine is invoked to release an xri to the pool of
13071 * available rpis maintained by the driver.
13072 **/
13073void
13074lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13075{
13076 spin_lock_irq(&phba->hbalock);
13077 __lpfc_sli4_free_xri(phba, xri);
13078 spin_unlock_irq(&phba->hbalock);
13079}
13080
13081/**
James Smart4f774512009-05-22 14:52:35 -040013082 * lpfc_sli4_next_xritag - Get an xritag for the io
13083 * @phba: Pointer to HBA context object.
13084 *
13085 * This function gets an xritag for the iocb. If there is no unused xritag
13086 * it will return 0xffff.
13087 * The function returns the allocated xritag if successful, else returns zero.
13088 * Zero is not a valid xritag.
13089 * The caller is not required to hold any lock.
13090 **/
13091uint16_t
13092lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13093{
James Smart6d368e52011-05-24 11:44:12 -040013094 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013095
James Smart6d368e52011-05-24 11:44:12 -040013096 xri_index = lpfc_sli4_alloc_xri(phba);
13097 if (xri_index != NO_XRI)
13098 return xri_index;
13099
13100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040013101 "2004 Failed to allocate XRI.last XRITAG is %d"
13102 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040013103 xri_index,
James Smart4f774512009-05-22 14:52:35 -040013104 phba->sli4_hba.max_cfg_param.max_xri,
13105 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040013106 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040013107}
13108
13109/**
James Smart6d368e52011-05-24 11:44:12 -040013110 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013111 * @phba: pointer to lpfc hba data structure.
13112 *
13113 * This routine is invoked to post a block of driver's sgl pages to the
13114 * HBA using non-embedded mailbox command. No Lock is held. This routine
13115 * is only called when the driver is loading and after all IO has been
13116 * stopped.
13117 **/
13118int
James Smart6d368e52011-05-24 11:44:12 -040013119lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040013120{
13121 struct lpfc_sglq *sglq_entry;
13122 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13123 struct sgl_page_pairs *sgl_pg_pairs;
13124 void *viraddr;
13125 LPFC_MBOXQ_t *mbox;
13126 uint32_t reqlen, alloclen, pg_pairs;
13127 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040013128 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040013129 int els_xri_cnt, rc = 0;
13130 uint32_t shdr_status, shdr_add_status;
13131 union lpfc_sli4_cfg_shdr *shdr;
13132
13133 /* The number of sgls to be posted */
13134 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13135
13136 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13137 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013138 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013139 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13140 "2559 Block sgl registration required DMA "
13141 "size (%d) great than a page\n", reqlen);
13142 return -ENOMEM;
13143 }
13144 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013145 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013146 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013147
13148 /* Allocate DMA memory and set up the non-embedded mailbox command */
13149 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13150 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13151 LPFC_SLI4_MBX_NEMBED);
13152
13153 if (alloclen < reqlen) {
13154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13155 "0285 Allocated DMA memory size (%d) is "
13156 "less than the requested DMA memory "
13157 "size (%d)\n", alloclen, reqlen);
13158 lpfc_sli4_mbox_cmd_free(phba, mbox);
13159 return -ENOMEM;
13160 }
James Smart4f774512009-05-22 14:52:35 -040013161 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013162 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013163 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13164 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13165
13166 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
13167 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040013168
13169 /*
13170 * Assign the sglq a physical xri only if the driver has not
13171 * initialized those resources. A port reset only needs
13172 * the sglq's posted.
13173 */
13174 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
13175 LPFC_XRI_RSRC_RDY) {
13176 lxri = lpfc_sli4_next_xritag(phba);
13177 if (lxri == NO_XRI) {
13178 lpfc_sli4_mbox_cmd_free(phba, mbox);
13179 return -ENOMEM;
13180 }
13181 sglq_entry->sli4_lxritag = lxri;
13182 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
13183 }
13184
James Smart4f774512009-05-22 14:52:35 -040013185 /* Set up the sge entry */
13186 sgl_pg_pairs->sgl_pg0_addr_lo =
13187 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13188 sgl_pg_pairs->sgl_pg0_addr_hi =
13189 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13190 sgl_pg_pairs->sgl_pg1_addr_lo =
13191 cpu_to_le32(putPaddrLow(0));
13192 sgl_pg_pairs->sgl_pg1_addr_hi =
13193 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013194
James Smart4f774512009-05-22 14:52:35 -040013195 /* Keep the first xritag on the list */
13196 if (pg_pairs == 0)
13197 xritag_start = sglq_entry->sli4_xritag;
13198 sgl_pg_pairs++;
13199 }
James Smart6d368e52011-05-24 11:44:12 -040013200
13201 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013202 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040013203 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013204 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013205 if (!phba->sli4_hba.intr_enable)
13206 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13207 else {
James Smarta183a152011-10-10 21:32:43 -040013208 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013209 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13210 }
13211 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13212 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13213 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13214 if (rc != MBX_TIMEOUT)
13215 lpfc_sli4_mbox_cmd_free(phba, mbox);
13216 if (shdr_status || shdr_add_status || rc) {
13217 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13218 "2513 POST_SGL_BLOCK mailbox command failed "
13219 "status x%x add_status x%x mbx status x%x\n",
13220 shdr_status, shdr_add_status, rc);
13221 rc = -ENXIO;
13222 }
James Smart6d368e52011-05-24 11:44:12 -040013223
13224 if (rc == 0)
13225 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13226 LPFC_XRI_RSRC_RDY);
13227 return rc;
13228}
13229
13230/**
13231 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
13232 * @phba: pointer to lpfc hba data structure.
13233 *
13234 * This routine is invoked to post a block of driver's sgl pages to the
13235 * HBA using non-embedded mailbox command. No Lock is held. This routine
13236 * is only called when the driver is loading and after all IO has been
13237 * stopped.
13238 **/
13239int
13240lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13241{
13242 struct lpfc_sglq *sglq_entry;
13243 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13244 struct sgl_page_pairs *sgl_pg_pairs;
13245 void *viraddr;
13246 LPFC_MBOXQ_t *mbox;
13247 uint32_t reqlen, alloclen, index;
13248 uint32_t mbox_tmo;
13249 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13250 uint16_t xritag_start = 0, lxri = 0;
13251 struct lpfc_rsrc_blks *rsrc_blk;
13252 int cnt, ttl_cnt, rc = 0;
13253 int loop_cnt;
13254 uint32_t shdr_status, shdr_add_status;
13255 union lpfc_sli4_cfg_shdr *shdr;
13256
13257 /* The number of sgls to be posted */
13258 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13259
13260 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13261 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13262 if (reqlen > SLI4_PAGE_SIZE) {
13263 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13264 "2989 Block sgl registration required DMA "
13265 "size (%d) great than a page\n", reqlen);
13266 return -ENOMEM;
13267 }
13268
13269 cnt = 0;
13270 ttl_cnt = 0;
13271 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13272 list) {
13273 rsrc_start = rsrc_blk->rsrc_start;
13274 rsrc_size = rsrc_blk->rsrc_size;
13275
13276 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13277 "3014 Working ELS Extent start %d, cnt %d\n",
13278 rsrc_start, rsrc_size);
13279
13280 loop_cnt = min(els_xri_cnt, rsrc_size);
13281 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13282 loop_cnt = els_xri_cnt - ttl_cnt;
13283 ttl_cnt = els_xri_cnt;
13284 }
13285
13286 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13287 if (!mbox)
13288 return -ENOMEM;
13289 /*
13290 * Allocate DMA memory and set up the non-embedded mailbox
13291 * command.
13292 */
13293 alloclen = lpfc_sli4_config(phba, mbox,
13294 LPFC_MBOX_SUBSYSTEM_FCOE,
13295 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13296 reqlen, LPFC_SLI4_MBX_NEMBED);
13297 if (alloclen < reqlen) {
13298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13299 "2987 Allocated DMA memory size (%d) "
13300 "is less than the requested DMA memory "
13301 "size (%d)\n", alloclen, reqlen);
13302 lpfc_sli4_mbox_cmd_free(phba, mbox);
13303 return -ENOMEM;
13304 }
13305
13306 /* Set up the SGL pages in the non-embedded DMA pages */
13307 viraddr = mbox->sge_array->addr[0];
13308 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13309 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13310
13311 /*
13312 * The starting resource may not begin at zero. Control
13313 * the loop variants via the block resource parameters,
13314 * but handle the sge pointers with a zero-based index
13315 * that doesn't get reset per loop pass.
13316 */
13317 for (index = rsrc_start;
13318 index < rsrc_start + loop_cnt;
13319 index++) {
13320 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13321
13322 /*
13323 * Assign the sglq a physical xri only if the driver
13324 * has not initialized those resources. A port reset
13325 * only needs the sglq's posted.
13326 */
13327 if (bf_get(lpfc_xri_rsrc_rdy,
13328 &phba->sli4_hba.sli4_flags) !=
13329 LPFC_XRI_RSRC_RDY) {
13330 lxri = lpfc_sli4_next_xritag(phba);
13331 if (lxri == NO_XRI) {
13332 lpfc_sli4_mbox_cmd_free(phba, mbox);
13333 rc = -ENOMEM;
13334 goto err_exit;
13335 }
13336 sglq_entry->sli4_lxritag = lxri;
13337 sglq_entry->sli4_xritag =
13338 phba->sli4_hba.xri_ids[lxri];
13339 }
13340
13341 /* Set up the sge entry */
13342 sgl_pg_pairs->sgl_pg0_addr_lo =
13343 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13344 sgl_pg_pairs->sgl_pg0_addr_hi =
13345 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13346 sgl_pg_pairs->sgl_pg1_addr_lo =
13347 cpu_to_le32(putPaddrLow(0));
13348 sgl_pg_pairs->sgl_pg1_addr_hi =
13349 cpu_to_le32(putPaddrHigh(0));
13350
13351 /* Track the starting physical XRI for the mailbox. */
13352 if (index == rsrc_start)
13353 xritag_start = sglq_entry->sli4_xritag;
13354 sgl_pg_pairs++;
13355 cnt++;
13356 }
13357
13358 /* Complete initialization and perform endian conversion. */
13359 rsrc_blk->rsrc_used += loop_cnt;
13360 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13361 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13362 sgl->word0 = cpu_to_le32(sgl->word0);
13363
13364 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13365 "3015 Post ELS Extent SGL, start %d, "
13366 "cnt %d, used %d\n",
13367 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13368 if (!phba->sli4_hba.intr_enable)
13369 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13370 else {
James Smarta183a152011-10-10 21:32:43 -040013371 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013372 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13373 }
13374 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13375 shdr_status = bf_get(lpfc_mbox_hdr_status,
13376 &shdr->response);
13377 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13378 &shdr->response);
13379 if (rc != MBX_TIMEOUT)
13380 lpfc_sli4_mbox_cmd_free(phba, mbox);
13381 if (shdr_status || shdr_add_status || rc) {
13382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13383 "2988 POST_SGL_BLOCK mailbox "
13384 "command failed status x%x "
13385 "add_status x%x mbx status x%x\n",
13386 shdr_status, shdr_add_status, rc);
13387 rc = -ENXIO;
13388 goto err_exit;
13389 }
13390 if (ttl_cnt >= els_xri_cnt)
13391 break;
13392 }
13393
13394 err_exit:
13395 if (rc == 0)
13396 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13397 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040013398 return rc;
13399}
13400
13401/**
13402 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13403 * @phba: pointer to lpfc hba data structure.
13404 * @sblist: pointer to scsi buffer list.
13405 * @count: number of scsi buffers on the list.
13406 *
13407 * This routine is invoked to post a block of @count scsi sgl pages from a
13408 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13409 * No Lock is held.
13410 *
13411 **/
13412int
13413lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13414 int cnt)
13415{
13416 struct lpfc_scsi_buf *psb;
13417 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13418 struct sgl_page_pairs *sgl_pg_pairs;
13419 void *viraddr;
13420 LPFC_MBOXQ_t *mbox;
13421 uint32_t reqlen, alloclen, pg_pairs;
13422 uint32_t mbox_tmo;
13423 uint16_t xritag_start = 0;
13424 int rc = 0;
13425 uint32_t shdr_status, shdr_add_status;
13426 dma_addr_t pdma_phys_bpl1;
13427 union lpfc_sli4_cfg_shdr *shdr;
13428
13429 /* Calculate the requested length of the dma memory */
13430 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13431 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013432 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013433 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13434 "0217 Block sgl registration required DMA "
13435 "size (%d) great than a page\n", reqlen);
13436 return -ENOMEM;
13437 }
13438 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13439 if (!mbox) {
13440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13441 "0283 Failed to allocate mbox cmd memory\n");
13442 return -ENOMEM;
13443 }
13444
13445 /* Allocate DMA memory and set up the non-embedded mailbox command */
13446 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13447 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13448 LPFC_SLI4_MBX_NEMBED);
13449
13450 if (alloclen < reqlen) {
13451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13452 "2561 Allocated DMA memory size (%d) is "
13453 "less than the requested DMA memory "
13454 "size (%d)\n", alloclen, reqlen);
13455 lpfc_sli4_mbox_cmd_free(phba, mbox);
13456 return -ENOMEM;
13457 }
James Smart6d368e52011-05-24 11:44:12 -040013458
James Smart4f774512009-05-22 14:52:35 -040013459 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013460 viraddr = mbox->sge_array->addr[0];
13461
13462 /* Set up the SGL pages in the non-embedded DMA pages */
13463 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13464 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13465
13466 pg_pairs = 0;
13467 list_for_each_entry(psb, sblist, list) {
13468 /* Set up the sge entry */
13469 sgl_pg_pairs->sgl_pg0_addr_lo =
13470 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13471 sgl_pg_pairs->sgl_pg0_addr_hi =
13472 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13473 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13474 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13475 else
13476 pdma_phys_bpl1 = 0;
13477 sgl_pg_pairs->sgl_pg1_addr_lo =
13478 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13479 sgl_pg_pairs->sgl_pg1_addr_hi =
13480 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13481 /* Keep the first xritag on the list */
13482 if (pg_pairs == 0)
13483 xritag_start = psb->cur_iocbq.sli4_xritag;
13484 sgl_pg_pairs++;
13485 pg_pairs++;
13486 }
13487 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13488 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13489 /* Perform endian conversion if necessary */
13490 sgl->word0 = cpu_to_le32(sgl->word0);
13491
13492 if (!phba->sli4_hba.intr_enable)
13493 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13494 else {
James Smarta183a152011-10-10 21:32:43 -040013495 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013496 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13497 }
13498 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13499 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13500 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13501 if (rc != MBX_TIMEOUT)
13502 lpfc_sli4_mbox_cmd_free(phba, mbox);
13503 if (shdr_status || shdr_add_status || rc) {
13504 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13505 "2564 POST_SGL_BLOCK mailbox command failed "
13506 "status x%x add_status x%x mbx status x%x\n",
13507 shdr_status, shdr_add_status, rc);
13508 rc = -ENXIO;
13509 }
13510 return rc;
13511}
13512
13513/**
James Smart6d368e52011-05-24 11:44:12 -040013514 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13515 * @phba: pointer to lpfc hba data structure.
13516 * @sblist: pointer to scsi buffer list.
13517 * @count: number of scsi buffers on the list.
13518 *
13519 * This routine is invoked to post a block of @count scsi sgl pages from a
13520 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13521 * No Lock is held.
13522 *
13523 **/
13524int
13525lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13526 int cnt)
13527{
13528 struct lpfc_scsi_buf *psb = NULL;
13529 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13530 struct sgl_page_pairs *sgl_pg_pairs;
13531 void *viraddr;
13532 LPFC_MBOXQ_t *mbox;
13533 uint32_t reqlen, alloclen, pg_pairs;
13534 uint32_t mbox_tmo;
13535 uint16_t xri_start = 0, scsi_xri_start;
13536 uint16_t rsrc_range;
13537 int rc = 0, avail_cnt;
13538 uint32_t shdr_status, shdr_add_status;
13539 dma_addr_t pdma_phys_bpl1;
13540 union lpfc_sli4_cfg_shdr *shdr;
13541 struct lpfc_rsrc_blks *rsrc_blk;
13542 uint32_t xri_cnt = 0;
13543
13544 /* Calculate the total requested length of the dma memory */
13545 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13546 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13547 if (reqlen > SLI4_PAGE_SIZE) {
13548 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13549 "2932 Block sgl registration required DMA "
13550 "size (%d) great than a page\n", reqlen);
13551 return -ENOMEM;
13552 }
13553
13554 /*
13555 * The use of extents requires the driver to post the sgl headers
13556 * in multiple postings to meet the contiguous resource assignment.
13557 */
13558 psb = list_prepare_entry(psb, sblist, list);
13559 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13560 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13561 list) {
13562 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13563 if (rsrc_range < scsi_xri_start)
13564 continue;
13565 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13566 continue;
13567 else
13568 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13569
13570 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13571 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13572 /*
13573 * Allocate DMA memory and set up the non-embedded mailbox
13574 * command. The mbox is used to post an SGL page per loop
13575 * but the DMA memory has a use-once semantic so the mailbox
13576 * is used and freed per loop pass.
13577 */
13578 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13579 if (!mbox) {
13580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13581 "2933 Failed to allocate mbox cmd "
13582 "memory\n");
13583 return -ENOMEM;
13584 }
13585 alloclen = lpfc_sli4_config(phba, mbox,
13586 LPFC_MBOX_SUBSYSTEM_FCOE,
13587 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13588 reqlen,
13589 LPFC_SLI4_MBX_NEMBED);
13590 if (alloclen < reqlen) {
13591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13592 "2934 Allocated DMA memory size (%d) "
13593 "is less than the requested DMA memory "
13594 "size (%d)\n", alloclen, reqlen);
13595 lpfc_sli4_mbox_cmd_free(phba, mbox);
13596 return -ENOMEM;
13597 }
13598
13599 /* Get the first SGE entry from the non-embedded DMA memory */
13600 viraddr = mbox->sge_array->addr[0];
13601
13602 /* Set up the SGL pages in the non-embedded DMA pages */
13603 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13604 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13605
13606 /* pg_pairs tracks posted SGEs per loop iteration. */
13607 pg_pairs = 0;
13608 list_for_each_entry_continue(psb, sblist, list) {
13609 /* Set up the sge entry */
13610 sgl_pg_pairs->sgl_pg0_addr_lo =
13611 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13612 sgl_pg_pairs->sgl_pg0_addr_hi =
13613 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13614 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13615 pdma_phys_bpl1 = psb->dma_phys_bpl +
13616 SGL_PAGE_SIZE;
13617 else
13618 pdma_phys_bpl1 = 0;
13619 sgl_pg_pairs->sgl_pg1_addr_lo =
13620 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13621 sgl_pg_pairs->sgl_pg1_addr_hi =
13622 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13623 /* Keep the first xri for this extent. */
13624 if (pg_pairs == 0)
13625 xri_start = psb->cur_iocbq.sli4_xritag;
13626 sgl_pg_pairs++;
13627 pg_pairs++;
13628 xri_cnt++;
13629
13630 /*
13631 * Track two exit conditions - the loop has constructed
13632 * all of the caller's SGE pairs or all available
13633 * resource IDs in this extent are consumed.
13634 */
13635 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13636 break;
13637 }
13638 rsrc_blk->rsrc_used += pg_pairs;
13639 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13640 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13641
13642 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13643 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13644 "blk use %d\n",
13645 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13646 /* Perform endian conversion if necessary */
13647 sgl->word0 = cpu_to_le32(sgl->word0);
13648 if (!phba->sli4_hba.intr_enable)
13649 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13650 else {
James Smarta183a152011-10-10 21:32:43 -040013651 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013652 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13653 }
13654 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13655 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13656 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13657 &shdr->response);
13658 if (rc != MBX_TIMEOUT)
13659 lpfc_sli4_mbox_cmd_free(phba, mbox);
13660 if (shdr_status || shdr_add_status || rc) {
13661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13662 "2935 POST_SGL_BLOCK mailbox command "
13663 "failed status x%x add_status x%x "
13664 "mbx status x%x\n",
13665 shdr_status, shdr_add_status, rc);
13666 return -ENXIO;
13667 }
13668
13669 /* Post only what is requested. */
13670 if (xri_cnt >= cnt)
13671 break;
13672 }
13673 return rc;
13674}
13675
13676/**
James Smart4f774512009-05-22 14:52:35 -040013677 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13678 * @phba: pointer to lpfc_hba struct that the frame was received on
13679 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13680 *
13681 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13682 * valid type of frame that the LPFC driver will handle. This function will
13683 * return a zero if the frame is a valid frame or a non zero value when the
13684 * frame does not pass the check.
13685 **/
13686static int
13687lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13688{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013689 /* make rctl_names static to save stack space */
13690 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013691 char *type_names[] = FC_TYPE_NAMES_INIT;
13692 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013693 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013694
13695 switch (fc_hdr->fh_r_ctl) {
13696 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13697 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13698 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13699 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13700 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13701 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13702 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13703 case FC_RCTL_DD_CMD_STATUS: /* command status */
13704 case FC_RCTL_ELS_REQ: /* extended link services request */
13705 case FC_RCTL_ELS_REP: /* extended link services reply */
13706 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13707 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13708 case FC_RCTL_BA_NOP: /* basic link service NOP */
13709 case FC_RCTL_BA_ABTS: /* basic link service abort */
13710 case FC_RCTL_BA_RMC: /* remove connection */
13711 case FC_RCTL_BA_ACC: /* basic accept */
13712 case FC_RCTL_BA_RJT: /* basic reject */
13713 case FC_RCTL_BA_PRMT:
13714 case FC_RCTL_ACK_1: /* acknowledge_1 */
13715 case FC_RCTL_ACK_0: /* acknowledge_0 */
13716 case FC_RCTL_P_RJT: /* port reject */
13717 case FC_RCTL_F_RJT: /* fabric reject */
13718 case FC_RCTL_P_BSY: /* port busy */
13719 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13720 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13721 case FC_RCTL_LCR: /* link credit reset */
13722 case FC_RCTL_END: /* end */
13723 break;
13724 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13725 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13726 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13727 return lpfc_fc_frame_check(phba, fc_hdr);
13728 default:
13729 goto drop;
13730 }
13731 switch (fc_hdr->fh_type) {
13732 case FC_TYPE_BLS:
13733 case FC_TYPE_ELS:
13734 case FC_TYPE_FCP:
13735 case FC_TYPE_CT:
13736 break;
13737 case FC_TYPE_IP:
13738 case FC_TYPE_ILS:
13739 default:
13740 goto drop;
13741 }
James Smart546fc852011-03-11 16:06:29 -050013742
James Smart4f774512009-05-22 14:52:35 -040013743 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013744 "2538 Received frame rctl:%s type:%s "
13745 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013746 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013747 type_names[fc_hdr->fh_type],
13748 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13749 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13750 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013751 return 0;
13752drop:
13753 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13754 "2539 Dropped frame rctl:%s type:%s\n",
13755 rctl_names[fc_hdr->fh_r_ctl],
13756 type_names[fc_hdr->fh_type]);
13757 return 1;
13758}
13759
13760/**
13761 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13762 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13763 *
13764 * This function processes the FC header to retrieve the VFI from the VF
13765 * header, if one exists. This function will return the VFI if one exists
13766 * or 0 if no VSAN Header exists.
13767 **/
13768static uint32_t
13769lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13770{
13771 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13772
13773 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13774 return 0;
13775 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13776}
13777
13778/**
13779 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13780 * @phba: Pointer to the HBA structure to search for the vport on
13781 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13782 * @fcfi: The FC Fabric ID that the frame came from
13783 *
13784 * This function searches the @phba for a vport that matches the content of the
13785 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13786 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13787 * returns the matching vport pointer or NULL if unable to match frame to a
13788 * vport.
13789 **/
13790static struct lpfc_vport *
13791lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13792 uint16_t fcfi)
13793{
13794 struct lpfc_vport **vports;
13795 struct lpfc_vport *vport = NULL;
13796 int i;
13797 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13798 fc_hdr->fh_d_id[1] << 8 |
13799 fc_hdr->fh_d_id[2]);
James Smartbf086112011-08-21 21:48:13 -040013800 if (did == Fabric_DID)
13801 return phba->pport;
James Smart4f774512009-05-22 14:52:35 -040013802 vports = lpfc_create_vport_work_array(phba);
13803 if (vports != NULL)
13804 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13805 if (phba->fcf.fcfi == fcfi &&
13806 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13807 vports[i]->fc_myDID == did) {
13808 vport = vports[i];
13809 break;
13810 }
13811 }
13812 lpfc_destroy_vport_work_array(phba, vports);
13813 return vport;
13814}
13815
13816/**
James Smart45ed1192009-10-02 15:17:02 -040013817 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13818 * @vport: The vport to work on.
13819 *
13820 * This function updates the receive sequence time stamp for this vport. The
13821 * receive sequence time stamp indicates the time that the last frame of the
13822 * the sequence that has been idle for the longest amount of time was received.
13823 * the driver uses this time stamp to indicate if any received sequences have
13824 * timed out.
13825 **/
13826void
13827lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13828{
13829 struct lpfc_dmabuf *h_buf;
13830 struct hbq_dmabuf *dmabuf = NULL;
13831
13832 /* get the oldest sequence on the rcv list */
13833 h_buf = list_get_first(&vport->rcv_buffer_list,
13834 struct lpfc_dmabuf, list);
13835 if (!h_buf)
13836 return;
13837 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13838 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13839}
13840
13841/**
13842 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13843 * @vport: The vport that the received sequences were sent to.
13844 *
13845 * This function cleans up all outstanding received sequences. This is called
13846 * by the driver when a link event or user action invalidates all the received
13847 * sequences.
13848 **/
13849void
13850lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13851{
13852 struct lpfc_dmabuf *h_buf, *hnext;
13853 struct lpfc_dmabuf *d_buf, *dnext;
13854 struct hbq_dmabuf *dmabuf = NULL;
13855
13856 /* start with the oldest sequence on the rcv list */
13857 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13858 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13859 list_del_init(&dmabuf->hbuf.list);
13860 list_for_each_entry_safe(d_buf, dnext,
13861 &dmabuf->dbuf.list, list) {
13862 list_del_init(&d_buf->list);
13863 lpfc_in_buf_free(vport->phba, d_buf);
13864 }
13865 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13866 }
13867}
13868
13869/**
13870 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13871 * @vport: The vport that the received sequences were sent to.
13872 *
13873 * This function determines whether any received sequences have timed out by
13874 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13875 * indicates that there is at least one timed out sequence this routine will
13876 * go through the received sequences one at a time from most inactive to most
13877 * active to determine which ones need to be cleaned up. Once it has determined
13878 * that a sequence needs to be cleaned up it will simply free up the resources
13879 * without sending an abort.
13880 **/
13881void
13882lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13883{
13884 struct lpfc_dmabuf *h_buf, *hnext;
13885 struct lpfc_dmabuf *d_buf, *dnext;
13886 struct hbq_dmabuf *dmabuf = NULL;
13887 unsigned long timeout;
13888 int abort_count = 0;
13889
13890 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13891 vport->rcv_buffer_time_stamp);
13892 if (list_empty(&vport->rcv_buffer_list) ||
13893 time_before(jiffies, timeout))
13894 return;
13895 /* start with the oldest sequence on the rcv list */
13896 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13897 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13898 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13899 dmabuf->time_stamp);
13900 if (time_before(jiffies, timeout))
13901 break;
13902 abort_count++;
13903 list_del_init(&dmabuf->hbuf.list);
13904 list_for_each_entry_safe(d_buf, dnext,
13905 &dmabuf->dbuf.list, list) {
13906 list_del_init(&d_buf->list);
13907 lpfc_in_buf_free(vport->phba, d_buf);
13908 }
13909 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13910 }
13911 if (abort_count)
13912 lpfc_update_rcv_time_stamp(vport);
13913}
13914
13915/**
James Smart4f774512009-05-22 14:52:35 -040013916 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13917 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13918 *
13919 * This function searches through the existing incomplete sequences that have
13920 * been sent to this @vport. If the frame matches one of the incomplete
13921 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13922 * make up that sequence. If no sequence is found that matches this frame then
13923 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13924 * This function returns a pointer to the first dmabuf in the sequence list that
13925 * the frame was linked to.
13926 **/
13927static struct hbq_dmabuf *
13928lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13929{
13930 struct fc_frame_header *new_hdr;
13931 struct fc_frame_header *temp_hdr;
13932 struct lpfc_dmabuf *d_buf;
13933 struct lpfc_dmabuf *h_buf;
13934 struct hbq_dmabuf *seq_dmabuf = NULL;
13935 struct hbq_dmabuf *temp_dmabuf = NULL;
13936
James Smart4d9ab992009-10-02 15:16:39 -040013937 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013938 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013939 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13940 /* Use the hdr_buf to find the sequence that this frame belongs to */
13941 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13942 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13943 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13944 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13945 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13946 continue;
13947 /* found a pending sequence that matches this frame */
13948 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13949 break;
13950 }
13951 if (!seq_dmabuf) {
13952 /*
13953 * This indicates first frame received for this sequence.
13954 * Queue the buffer on the vport's rcv_buffer_list.
13955 */
13956 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013957 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013958 return dmabuf;
13959 }
13960 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013961 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13962 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013963 list_del_init(&seq_dmabuf->hbuf.list);
13964 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13965 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013966 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013967 return dmabuf;
13968 }
James Smart45ed1192009-10-02 15:17:02 -040013969 /* move this sequence to the tail to indicate a young sequence */
13970 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13971 seq_dmabuf->time_stamp = jiffies;
13972 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013973 if (list_empty(&seq_dmabuf->dbuf.list)) {
13974 temp_hdr = dmabuf->hbuf.virt;
13975 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13976 return seq_dmabuf;
13977 }
James Smart4f774512009-05-22 14:52:35 -040013978 /* find the correct place in the sequence to insert this frame */
13979 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13980 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13981 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13982 /*
13983 * If the frame's sequence count is greater than the frame on
13984 * the list then insert the frame right after this frame
13985 */
James Smarteeead812009-12-21 17:01:23 -050013986 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13987 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013988 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13989 return seq_dmabuf;
13990 }
13991 }
13992 return NULL;
13993}
13994
13995/**
James Smart6669f9b2009-10-02 15:16:45 -040013996 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13997 * @vport: pointer to a vitural port
13998 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13999 *
14000 * This function tries to abort from the partially assembed sequence, described
14001 * by the information from basic abbort @dmabuf. It checks to see whether such
14002 * partially assembled sequence held by the driver. If so, it shall free up all
14003 * the frames from the partially assembled sequence.
14004 *
14005 * Return
14006 * true -- if there is matching partially assembled sequence present and all
14007 * the frames freed with the sequence;
14008 * false -- if there is no matching partially assembled sequence present so
14009 * nothing got aborted in the lower layer driver
14010 **/
14011static bool
14012lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14013 struct hbq_dmabuf *dmabuf)
14014{
14015 struct fc_frame_header *new_hdr;
14016 struct fc_frame_header *temp_hdr;
14017 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14018 struct hbq_dmabuf *seq_dmabuf = NULL;
14019
14020 /* Use the hdr_buf to find the sequence that matches this frame */
14021 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14022 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14023 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14024 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14025 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14026 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14027 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14028 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14029 continue;
14030 /* found a pending sequence that matches this frame */
14031 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14032 break;
14033 }
14034
14035 /* Free up all the frames from the partially assembled sequence */
14036 if (seq_dmabuf) {
14037 list_for_each_entry_safe(d_buf, n_buf,
14038 &seq_dmabuf->dbuf.list, list) {
14039 list_del_init(&d_buf->list);
14040 lpfc_in_buf_free(vport->phba, d_buf);
14041 }
14042 return true;
14043 }
14044 return false;
14045}
14046
14047/**
James Smart546fc852011-03-11 16:06:29 -050014048 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014049 * @phba: Pointer to HBA context object.
14050 * @cmd_iocbq: pointer to the command iocbq structure.
14051 * @rsp_iocbq: pointer to the response iocbq structure.
14052 *
James Smart546fc852011-03-11 16:06:29 -050014053 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014054 * event. It properly releases the memory allocated to the sequence abort
14055 * accept iocb.
14056 **/
14057static void
James Smart546fc852011-03-11 16:06:29 -050014058lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014059 struct lpfc_iocbq *cmd_iocbq,
14060 struct lpfc_iocbq *rsp_iocbq)
14061{
14062 if (cmd_iocbq)
14063 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014064
14065 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14066 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14067 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14068 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14069 rsp_iocbq->iocb.ulpStatus,
14070 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014071}
14072
14073/**
James Smart6d368e52011-05-24 11:44:12 -040014074 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14075 * @phba: Pointer to HBA context object.
14076 * @xri: xri id in transaction.
14077 *
14078 * This function validates the xri maps to the known range of XRIs allocated an
14079 * used by the driver.
14080 **/
James Smart7851fe22011-07-22 18:36:52 -040014081uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014082lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14083 uint16_t xri)
14084{
14085 int i;
14086
14087 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14088 if (xri == phba->sli4_hba.xri_ids[i])
14089 return i;
14090 }
14091 return NO_XRI;
14092}
14093
14094
14095/**
James Smart546fc852011-03-11 16:06:29 -050014096 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014097 * @phba: Pointer to HBA context object.
14098 * @fc_hdr: pointer to a FC frame header.
14099 *
James Smart546fc852011-03-11 16:06:29 -050014100 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014101 * event after aborting the sequence handling.
14102 **/
14103static void
James Smart546fc852011-03-11 16:06:29 -050014104lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014105 struct fc_frame_header *fc_hdr)
14106{
14107 struct lpfc_iocbq *ctiocb = NULL;
14108 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050014109 uint16_t oxid, rxid;
14110 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014111 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014112 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014113
14114 if (!lpfc_is_link_up(phba))
14115 return;
14116
14117 sid = sli4_sid_from_fc_hdr(fc_hdr);
14118 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014119 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014120
14121 ndlp = lpfc_findnode_did(phba->pport, sid);
14122 if (!ndlp) {
14123 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14124 "1268 Find ndlp returned NULL for oxid:x%x "
14125 "SID:x%x\n", oxid, sid);
14126 return;
14127 }
James Smart6d368e52011-05-24 11:44:12 -040014128 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050014129 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040014130
James Smart546fc852011-03-11 16:06:29 -050014131 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014132 ctiocb = lpfc_sli_get_iocbq(phba);
14133 if (!ctiocb)
14134 return;
14135
James Smart5ffc2662009-11-18 15:39:44 -050014136 /* Extract the F_CTL field from FC_HDR */
14137 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14138
James Smart6669f9b2009-10-02 15:16:45 -040014139 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014140 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014141 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014142 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14143 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14144 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14145
14146 /* Fill in the rest of iocb fields */
14147 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14148 icmd->ulpBdeCount = 0;
14149 icmd->ulpLe = 1;
14150 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014151 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014152 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014153
James Smart6669f9b2009-10-02 15:16:45 -040014154 ctiocb->iocb_cmpl = NULL;
14155 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014156 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014157 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014158 ctiocb->sli4_xritag = NO_XRI;
14159
14160 /* If the oxid maps to the FCP XRI range or if it is out of range,
14161 * send a BLS_RJT. The driver no longer has that exchange.
14162 * Override the IOCB for a BA_RJT.
14163 */
14164 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
14165 phba->sli4_hba.max_cfg_param.xri_base) ||
14166 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
14167 phba->sli4_hba.max_cfg_param.xri_base)) {
14168 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14169 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14170 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14171 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14172 }
James Smart6669f9b2009-10-02 15:16:45 -040014173
James Smart5ffc2662009-11-18 15:39:44 -050014174 if (fctl & FC_FC_EX_CTX) {
14175 /* ABTS sent by responder to CT exchange, construction
14176 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14177 * field and RX_ID from ABTS for RX_ID field.
14178 */
James Smart546fc852011-03-11 16:06:29 -050014179 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14180 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050014181 } else {
14182 /* ABTS sent by initiator to CT exchange, construction
14183 * of BA_ACC will need to allocate a new XRI as for the
14184 * XRI_TAG and RX_ID fields.
14185 */
James Smart546fc852011-03-11 16:06:29 -050014186 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14187 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050014188 }
James Smart546fc852011-03-11 16:06:29 -050014189 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014190
James Smart546fc852011-03-11 16:06:29 -050014191 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014192 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014193 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14194 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14195
14196 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14197 if (rc == IOCB_ERROR) {
14198 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14199 "2925 Failed to issue CT ABTS RSP x%x on "
14200 "xri x%x, Data x%x\n",
14201 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14202 phba->link_state);
14203 lpfc_sli_release_iocbq(phba, ctiocb);
14204 }
James Smart6669f9b2009-10-02 15:16:45 -040014205}
14206
14207/**
14208 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14209 * @vport: Pointer to the vport on which this sequence was received
14210 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14211 *
14212 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14213 * receive sequence is only partially assembed by the driver, it shall abort
14214 * the partially assembled frames for the sequence. Otherwise, if the
14215 * unsolicited receive sequence has been completely assembled and passed to
14216 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14217 * unsolicited sequence has been aborted. After that, it will issue a basic
14218 * accept to accept the abort.
14219 **/
14220void
14221lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14222 struct hbq_dmabuf *dmabuf)
14223{
14224 struct lpfc_hba *phba = vport->phba;
14225 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014226 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014227 bool abts_par;
14228
James Smart6669f9b2009-10-02 15:16:45 -040014229 /* Make a copy of fc_hdr before the dmabuf being released */
14230 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014231 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014232
James Smart5ffc2662009-11-18 15:39:44 -050014233 if (fctl & FC_FC_EX_CTX) {
14234 /*
14235 * ABTS sent by responder to exchange, just free the buffer
14236 */
James Smart6669f9b2009-10-02 15:16:45 -040014237 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014238 } else {
14239 /*
14240 * ABTS sent by initiator to exchange, need to do cleanup
14241 */
14242 /* Try to abort partially assembled seq */
14243 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14244
14245 /* Send abort to ULP if partially seq abort failed */
14246 if (abts_par == false)
14247 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14248 else
14249 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14250 }
James Smart6669f9b2009-10-02 15:16:45 -040014251 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014252 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014253}
14254
14255/**
James Smart4f774512009-05-22 14:52:35 -040014256 * lpfc_seq_complete - Indicates if a sequence is complete
14257 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14258 *
14259 * This function checks the sequence, starting with the frame described by
14260 * @dmabuf, to see if all the frames associated with this sequence are present.
14261 * the frames associated with this sequence are linked to the @dmabuf using the
14262 * dbuf list. This function looks for two major things. 1) That the first frame
14263 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14264 * set. 3) That there are no holes in the sequence count. The function will
14265 * return 1 when the sequence is complete, otherwise it will return 0.
14266 **/
14267static int
14268lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14269{
14270 struct fc_frame_header *hdr;
14271 struct lpfc_dmabuf *d_buf;
14272 struct hbq_dmabuf *seq_dmabuf;
14273 uint32_t fctl;
14274 int seq_count = 0;
14275
14276 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14277 /* make sure first fame of sequence has a sequence count of zero */
14278 if (hdr->fh_seq_cnt != seq_count)
14279 return 0;
14280 fctl = (hdr->fh_f_ctl[0] << 16 |
14281 hdr->fh_f_ctl[1] << 8 |
14282 hdr->fh_f_ctl[2]);
14283 /* If last frame of sequence we can return success. */
14284 if (fctl & FC_FC_END_SEQ)
14285 return 1;
14286 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14287 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14288 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14289 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014290 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014291 return 0;
14292 fctl = (hdr->fh_f_ctl[0] << 16 |
14293 hdr->fh_f_ctl[1] << 8 |
14294 hdr->fh_f_ctl[2]);
14295 /* If last frame of sequence we can return success. */
14296 if (fctl & FC_FC_END_SEQ)
14297 return 1;
14298 }
14299 return 0;
14300}
14301
14302/**
14303 * lpfc_prep_seq - Prep sequence for ULP processing
14304 * @vport: Pointer to the vport on which this sequence was received
14305 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14306 *
14307 * This function takes a sequence, described by a list of frames, and creates
14308 * a list of iocbq structures to describe the sequence. This iocbq list will be
14309 * used to issue to the generic unsolicited sequence handler. This routine
14310 * returns a pointer to the first iocbq in the list. If the function is unable
14311 * to allocate an iocbq then it throw out the received frames that were not
14312 * able to be described and return a pointer to the first iocbq. If unable to
14313 * allocate any iocbqs (including the first) this function will return NULL.
14314 **/
14315static struct lpfc_iocbq *
14316lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14317{
James Smart7851fe22011-07-22 18:36:52 -040014318 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014319 struct lpfc_dmabuf *d_buf, *n_buf;
14320 struct lpfc_iocbq *first_iocbq, *iocbq;
14321 struct fc_frame_header *fc_hdr;
14322 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014323 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014324 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014325
14326 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14327 /* remove from receive buffer list */
14328 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014329 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014330 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014331 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014332 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014333 /* Get an iocbq struct to fill in. */
14334 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14335 if (first_iocbq) {
14336 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014337 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014338 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14339 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014340 first_iocbq->iocb.ulpContext = NO_XRI;
14341 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14342 be16_to_cpu(fc_hdr->fh_ox_id);
14343 /* iocbq is prepped for internal consumption. Physical vpi. */
14344 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14345 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014346 /* put the first buffer into the first IOCBq */
14347 first_iocbq->context2 = &seq_dmabuf->dbuf;
14348 first_iocbq->context3 = NULL;
14349 first_iocbq->iocb.ulpBdeCount = 1;
14350 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14351 LPFC_DATA_BUF_SIZE;
14352 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014353 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014354 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014355 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014356 }
14357 iocbq = first_iocbq;
14358 /*
14359 * Each IOCBq can have two Buffers assigned, so go through the list
14360 * of buffers for this sequence and save two buffers in each IOCBq
14361 */
14362 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14363 if (!iocbq) {
14364 lpfc_in_buf_free(vport->phba, d_buf);
14365 continue;
14366 }
14367 if (!iocbq->context3) {
14368 iocbq->context3 = d_buf;
14369 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014370 pbde = (struct ulp_bde64 *)
14371 &iocbq->iocb.unsli3.sli3Words[4];
14372 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014373
14374 /* We need to get the size out of the right CQE */
14375 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14376 len = bf_get(lpfc_rcqe_length,
14377 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14378 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14379 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014380 } else {
14381 iocbq = lpfc_sli_get_iocbq(vport->phba);
14382 if (!iocbq) {
14383 if (first_iocbq) {
14384 first_iocbq->iocb.ulpStatus =
14385 IOSTAT_FCP_RSP_ERROR;
14386 first_iocbq->iocb.un.ulpWord[4] =
14387 IOERR_NO_RESOURCES;
14388 }
14389 lpfc_in_buf_free(vport->phba, d_buf);
14390 continue;
14391 }
14392 iocbq->context2 = d_buf;
14393 iocbq->context3 = NULL;
14394 iocbq->iocb.ulpBdeCount = 1;
14395 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14396 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014397
14398 /* We need to get the size out of the right CQE */
14399 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14400 len = bf_get(lpfc_rcqe_length,
14401 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14402 tot_len += len;
14403 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14404
James Smart4f774512009-05-22 14:52:35 -040014405 iocbq->iocb.un.rcvels.remoteID = sid;
14406 list_add_tail(&iocbq->list, &first_iocbq->list);
14407 }
14408 }
14409 return first_iocbq;
14410}
14411
James Smart6669f9b2009-10-02 15:16:45 -040014412static void
14413lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14414 struct hbq_dmabuf *seq_dmabuf)
14415{
14416 struct fc_frame_header *fc_hdr;
14417 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14418 struct lpfc_hba *phba = vport->phba;
14419
14420 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14421 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14422 if (!iocbq) {
14423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14424 "2707 Ring %d handler: Failed to allocate "
14425 "iocb Rctl x%x Type x%x received\n",
14426 LPFC_ELS_RING,
14427 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14428 return;
14429 }
14430 if (!lpfc_complete_unsol_iocb(phba,
14431 &phba->sli.ring[LPFC_ELS_RING],
14432 iocbq, fc_hdr->fh_r_ctl,
14433 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014434 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014435 "2540 Ring %d handler: unexpected Rctl "
14436 "x%x Type x%x received\n",
14437 LPFC_ELS_RING,
14438 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14439
14440 /* Free iocb created in lpfc_prep_seq */
14441 list_for_each_entry_safe(curr_iocb, next_iocb,
14442 &iocbq->list, list) {
14443 list_del_init(&curr_iocb->list);
14444 lpfc_sli_release_iocbq(phba, curr_iocb);
14445 }
14446 lpfc_sli_release_iocbq(phba, iocbq);
14447}
14448
James Smart4f774512009-05-22 14:52:35 -040014449/**
14450 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14451 * @phba: Pointer to HBA context object.
14452 *
14453 * This function is called with no lock held. This function processes all
14454 * the received buffers and gives it to upper layers when a received buffer
14455 * indicates that it is the final frame in the sequence. The interrupt
14456 * service routine processes received buffers at interrupt contexts and adds
14457 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14458 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14459 * appropriate receive function when the final frame in a sequence is received.
14460 **/
James Smart4d9ab992009-10-02 15:16:39 -040014461void
14462lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14463 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014464{
James Smart4d9ab992009-10-02 15:16:39 -040014465 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014466 struct fc_frame_header *fc_hdr;
14467 struct lpfc_vport *vport;
14468 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040014469
James Smart4f774512009-05-22 14:52:35 -040014470 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014471 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14472 /* check to see if this a valid type of frame */
14473 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14474 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14475 return;
14476 }
James Smart7851fe22011-07-22 18:36:52 -040014477 if ((bf_get(lpfc_cqe_code,
14478 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14479 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14480 &dmabuf->cq_event.cqe.rcqe_cmpl);
14481 else
14482 fcfi = bf_get(lpfc_rcqe_fcf_id,
14483 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4d9ab992009-10-02 15:16:39 -040014484 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050014485 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040014486 /* throw out the frame */
14487 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14488 return;
14489 }
James Smart6669f9b2009-10-02 15:16:45 -040014490 /* Handle the basic abort sequence (BA_ABTS) event */
14491 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14492 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14493 return;
14494 }
14495
James Smart4d9ab992009-10-02 15:16:39 -040014496 /* Link this frame */
14497 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14498 if (!seq_dmabuf) {
14499 /* unable to add frame to vport - throw it out */
14500 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14501 return;
14502 }
14503 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014504 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014505 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014506
James Smart6669f9b2009-10-02 15:16:45 -040014507 /* Send the complete sequence to the upper layer protocol */
14508 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014509}
James Smart6fb120a2009-05-22 14:52:59 -040014510
14511/**
14512 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14513 * @phba: pointer to lpfc hba data structure.
14514 *
14515 * This routine is invoked to post rpi header templates to the
14516 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014517 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14518 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014519 *
14520 * This routine does not require any locks. It's usage is expected
14521 * to be driver load or reset recovery when the driver is
14522 * sequential.
14523 *
14524 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014525 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014526 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014527 * When this error occurs, the driver is not guaranteed
14528 * to have any rpi regions posted to the device and
14529 * must either attempt to repost the regions or take a
14530 * fatal error.
14531 **/
14532int
14533lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14534{
14535 struct lpfc_rpi_hdr *rpi_page;
14536 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014537 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014538
James Smart6d368e52011-05-24 11:44:12 -040014539 /* SLI4 ports that support extents do not require RPI headers. */
14540 if (!phba->sli4_hba.rpi_hdrs_in_use)
14541 goto exit;
14542 if (phba->sli4_hba.extents_in_use)
14543 return -EIO;
14544
James Smart6fb120a2009-05-22 14:52:59 -040014545 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014546 /*
14547 * Assign the rpi headers a physical rpi only if the driver
14548 * has not initialized those resources. A port reset only
14549 * needs the headers posted.
14550 */
14551 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14552 LPFC_RPI_RSRC_RDY)
14553 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14554
James Smart6fb120a2009-05-22 14:52:59 -040014555 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14556 if (rc != MBX_SUCCESS) {
14557 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14558 "2008 Error %d posting all rpi "
14559 "headers\n", rc);
14560 rc = -EIO;
14561 break;
14562 }
14563 }
14564
James Smart6d368e52011-05-24 11:44:12 -040014565 exit:
14566 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14567 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014568 return rc;
14569}
14570
14571/**
14572 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14573 * @phba: pointer to lpfc hba data structure.
14574 * @rpi_page: pointer to the rpi memory region.
14575 *
14576 * This routine is invoked to post a single rpi header to the
14577 * HBA consistent with the SLI-4 interface spec. This memory region
14578 * maps up to 64 rpi context regions.
14579 *
14580 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014581 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014582 * -ENOMEM - No available memory
14583 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014584 **/
14585int
14586lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14587{
14588 LPFC_MBOXQ_t *mboxq;
14589 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14590 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014591 uint32_t shdr_status, shdr_add_status;
14592 union lpfc_sli4_cfg_shdr *shdr;
14593
James Smart6d368e52011-05-24 11:44:12 -040014594 /* SLI4 ports that support extents do not require RPI headers. */
14595 if (!phba->sli4_hba.rpi_hdrs_in_use)
14596 return rc;
14597 if (phba->sli4_hba.extents_in_use)
14598 return -EIO;
14599
James Smart6fb120a2009-05-22 14:52:59 -040014600 /* The port is notified of the header region via a mailbox command. */
14601 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14602 if (!mboxq) {
14603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14604 "2001 Unable to allocate memory for issuing "
14605 "SLI_CONFIG_SPECIAL mailbox command\n");
14606 return -ENOMEM;
14607 }
14608
14609 /* Post all rpi memory regions to the port. */
14610 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014611 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14612 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14613 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014614 sizeof(struct lpfc_sli4_cfg_mhdr),
14615 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014616
14617
14618 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014619 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14620 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014621 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14622 hdr_tmpl, rpi_page->page_count);
14623
James Smart6fb120a2009-05-22 14:52:59 -040014624 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14625 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014626 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014627 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14628 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14629 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14630 if (rc != MBX_TIMEOUT)
14631 mempool_free(mboxq, phba->mbox_mem_pool);
14632 if (shdr_status || shdr_add_status || rc) {
14633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14634 "2514 POST_RPI_HDR mailbox failed with "
14635 "status x%x add_status x%x, mbx status x%x\n",
14636 shdr_status, shdr_add_status, rc);
14637 rc = -ENXIO;
14638 }
14639 return rc;
14640}
14641
14642/**
14643 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14644 * @phba: pointer to lpfc hba data structure.
14645 *
14646 * This routine is invoked to post rpi header templates to the
14647 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014648 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14649 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014650 *
14651 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014652 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014653 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14654 **/
14655int
14656lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14657{
James Smart6d368e52011-05-24 11:44:12 -040014658 unsigned long rpi;
14659 uint16_t max_rpi, rpi_limit;
14660 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014661 struct lpfc_rpi_hdr *rpi_hdr;
14662
14663 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014664 rpi_limit = phba->sli4_hba.next_rpi;
14665
14666 /*
James Smart6d368e52011-05-24 11:44:12 -040014667 * Fetch the next logical rpi. Because this index is logical,
14668 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014669 */
14670 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014671 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14672 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014673 rpi = LPFC_RPI_ALLOC_ERROR;
14674 else {
14675 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14676 phba->sli4_hba.max_cfg_param.rpi_used++;
14677 phba->sli4_hba.rpi_count++;
14678 }
14679
14680 /*
14681 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014682 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014683 */
14684 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14685 (phba->sli4_hba.rpi_count >= max_rpi)) {
14686 spin_unlock_irq(&phba->hbalock);
14687 return rpi;
14688 }
14689
14690 /*
James Smart6d368e52011-05-24 11:44:12 -040014691 * RPI header postings are not required for SLI4 ports capable of
14692 * extents.
14693 */
14694 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14695 spin_unlock_irq(&phba->hbalock);
14696 return rpi;
14697 }
14698
14699 /*
James Smart6fb120a2009-05-22 14:52:59 -040014700 * If the driver is running low on rpi resources, allocate another
14701 * page now. Note that the next_rpi value is used because
14702 * it represents how many are actually in use whereas max_rpi notes
14703 * how many are supported max by the device.
14704 */
James Smart6d368e52011-05-24 11:44:12 -040014705 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014706 spin_unlock_irq(&phba->hbalock);
14707 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14708 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14709 if (!rpi_hdr) {
14710 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14711 "2002 Error Could not grow rpi "
14712 "count\n");
14713 } else {
James Smart6d368e52011-05-24 11:44:12 -040014714 lrpi = rpi_hdr->start_rpi;
14715 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014716 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14717 }
14718 }
14719
14720 return rpi;
14721}
14722
14723/**
14724 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14725 * @phba: pointer to lpfc hba data structure.
14726 *
14727 * This routine is invoked to release an rpi to the pool of
14728 * available rpis maintained by the driver.
14729 **/
14730void
James Smartd7c47992010-06-08 18:31:54 -040014731__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14732{
14733 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14734 phba->sli4_hba.rpi_count--;
14735 phba->sli4_hba.max_cfg_param.rpi_used--;
14736 }
14737}
14738
14739/**
14740 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14741 * @phba: pointer to lpfc hba data structure.
14742 *
14743 * This routine is invoked to release an rpi to the pool of
14744 * available rpis maintained by the driver.
14745 **/
14746void
James Smart6fb120a2009-05-22 14:52:59 -040014747lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14748{
14749 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014750 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014751 spin_unlock_irq(&phba->hbalock);
14752}
14753
14754/**
14755 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14756 * @phba: pointer to lpfc hba data structure.
14757 *
14758 * This routine is invoked to remove the memory region that
14759 * provided rpi via a bitmask.
14760 **/
14761void
14762lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14763{
14764 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014765 kfree(phba->sli4_hba.rpi_ids);
14766 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014767}
14768
14769/**
14770 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14771 * @phba: pointer to lpfc hba data structure.
14772 *
14773 * This routine is invoked to remove the memory region that
14774 * provided rpi via a bitmask.
14775 **/
14776int
James Smart6b5151f2012-01-18 16:24:06 -050014777lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14778 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014779{
14780 LPFC_MBOXQ_t *mboxq;
14781 struct lpfc_hba *phba = ndlp->phba;
14782 int rc;
14783
14784 /* The port is notified of the header region via a mailbox command. */
14785 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14786 if (!mboxq)
14787 return -ENOMEM;
14788
14789 /* Post all rpi memory regions to the port. */
14790 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014791 if (cmpl) {
14792 mboxq->mbox_cmpl = cmpl;
14793 mboxq->context1 = arg;
14794 mboxq->context2 = ndlp;
14795 }
14796 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014797 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14798 if (rc == MBX_NOT_FINISHED) {
14799 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14800 "2010 Resume RPI Mailbox failed "
14801 "status %d, mbxStatus x%x\n", rc,
14802 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14803 mempool_free(mboxq, phba->mbox_mem_pool);
14804 return -EIO;
14805 }
14806 return 0;
14807}
14808
14809/**
14810 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014811 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014812 *
James Smart76a95d72010-11-20 23:11:48 -050014813 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014814 *
14815 * Returns:
14816 * 0 success
14817 * -Evalue otherwise
14818 **/
14819int
James Smart76a95d72010-11-20 23:11:48 -050014820lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014821{
14822 LPFC_MBOXQ_t *mboxq;
14823 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014824 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014825 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014826 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014827 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14828 if (!mboxq)
14829 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014830 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014831 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014832 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014833 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014834 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014835 "2022 INIT VPI Mailbox failed "
14836 "status %d, mbxStatus x%x\n", rc,
14837 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014838 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014839 }
James Smart6a9c52c2009-10-02 15:16:51 -040014840 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014841 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014842
14843 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014844}
14845
14846/**
14847 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14848 * @phba: pointer to lpfc hba data structure.
14849 * @mboxq: Pointer to mailbox object.
14850 *
14851 * This routine is invoked to manually add a single FCF record. The caller
14852 * must pass a completely initialized FCF_Record. This routine takes
14853 * care of the nonembedded mailbox operations.
14854 **/
14855static void
14856lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14857{
14858 void *virt_addr;
14859 union lpfc_sli4_cfg_shdr *shdr;
14860 uint32_t shdr_status, shdr_add_status;
14861
14862 virt_addr = mboxq->sge_array->addr[0];
14863 /* The IOCTL status is embedded in the mailbox subheader. */
14864 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14865 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14866 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14867
14868 if ((shdr_status || shdr_add_status) &&
14869 (shdr_status != STATUS_FCF_IN_USE))
14870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14871 "2558 ADD_FCF_RECORD mailbox failed with "
14872 "status x%x add_status x%x\n",
14873 shdr_status, shdr_add_status);
14874
14875 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14876}
14877
14878/**
14879 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14880 * @phba: pointer to lpfc hba data structure.
14881 * @fcf_record: pointer to the initialized fcf record to add.
14882 *
14883 * This routine is invoked to manually add a single FCF record. The caller
14884 * must pass a completely initialized FCF_Record. This routine takes
14885 * care of the nonembedded mailbox operations.
14886 **/
14887int
14888lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14889{
14890 int rc = 0;
14891 LPFC_MBOXQ_t *mboxq;
14892 uint8_t *bytep;
14893 void *virt_addr;
14894 dma_addr_t phys_addr;
14895 struct lpfc_mbx_sge sge;
14896 uint32_t alloc_len, req_len;
14897 uint32_t fcfindex;
14898
14899 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14900 if (!mboxq) {
14901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14902 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14903 return -ENOMEM;
14904 }
14905
14906 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14907 sizeof(uint32_t);
14908
14909 /* Allocate DMA memory and set up the non-embedded mailbox command */
14910 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14911 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14912 req_len, LPFC_SLI4_MBX_NEMBED);
14913 if (alloc_len < req_len) {
14914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14915 "2523 Allocated DMA memory size (x%x) is "
14916 "less than the requested DMA memory "
14917 "size (x%x)\n", alloc_len, req_len);
14918 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14919 return -ENOMEM;
14920 }
14921
14922 /*
14923 * Get the first SGE entry from the non-embedded DMA memory. This
14924 * routine only uses a single SGE.
14925 */
14926 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14927 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014928 virt_addr = mboxq->sge_array->addr[0];
14929 /*
14930 * Configure the FCF record for FCFI 0. This is the driver's
14931 * hardcoded default and gets used in nonFIP mode.
14932 */
14933 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14934 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14935 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14936
14937 /*
14938 * Copy the fcf_index and the FCF Record Data. The data starts after
14939 * the FCoE header plus word10. The data copy needs to be endian
14940 * correct.
14941 */
14942 bytep += sizeof(uint32_t);
14943 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14944 mboxq->vport = phba->pport;
14945 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14946 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14947 if (rc == MBX_NOT_FINISHED) {
14948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14949 "2515 ADD_FCF_RECORD mailbox failed with "
14950 "status 0x%x\n", rc);
14951 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14952 rc = -EIO;
14953 } else
14954 rc = 0;
14955
14956 return rc;
14957}
14958
14959/**
14960 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14961 * @phba: pointer to lpfc hba data structure.
14962 * @fcf_record: pointer to the fcf record to write the default data.
14963 * @fcf_index: FCF table entry index.
14964 *
14965 * This routine is invoked to build the driver's default FCF record. The
14966 * values used are hardcoded. This routine handles memory initialization.
14967 *
14968 **/
14969void
14970lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14971 struct fcf_record *fcf_record,
14972 uint16_t fcf_index)
14973{
14974 memset(fcf_record, 0, sizeof(struct fcf_record));
14975 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14976 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14977 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14978 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14979 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14980 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14981 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14982 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14983 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14984 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14985 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14986 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14987 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014988 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014989 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14990 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14991 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14992 /* Set the VLAN bit map */
14993 if (phba->valid_vlan) {
14994 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14995 = 1 << (phba->vlan_id % 8);
14996 }
14997}
14998
14999/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015000 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015001 * @phba: pointer to lpfc hba data structure.
15002 * @fcf_index: FCF table entry offset.
15003 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015004 * This routine is invoked to scan the entire FCF table by reading FCF
15005 * record and processing it one at a time starting from the @fcf_index
15006 * for initial FCF discovery or fast FCF failover rediscovery.
15007 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015008 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015009 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015010 **/
15011int
James Smart0c9ab6f2010-02-26 14:15:57 -050015012lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015013{
15014 int rc = 0, error;
15015 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015016
James Smart32b97932009-07-19 10:01:21 -040015017 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015018 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15019 if (!mboxq) {
15020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15021 "2000 Failed to allocate mbox for "
15022 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015023 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015024 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015025 }
James Smartecfd03c2010-02-12 14:41:27 -050015026 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015027 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015028 if (rc) {
15029 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015030 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015031 }
James Smartecfd03c2010-02-12 14:41:27 -050015032 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015033 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015034 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015035
15036 spin_lock_irq(&phba->hbalock);
15037 phba->hba_flag |= FCF_TS_INPROG;
15038 spin_unlock_irq(&phba->hbalock);
15039
James Smart6fb120a2009-05-22 14:52:59 -040015040 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015041 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015042 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015043 else {
James Smart38b92ef2010-08-04 16:11:39 -040015044 /* Reset eligible FCF count for new scan */
15045 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015046 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015047 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015048 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015049fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015050 if (error) {
15051 if (mboxq)
15052 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015053 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015054 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015055 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015056 spin_unlock_irq(&phba->hbalock);
15057 }
James Smart6fb120a2009-05-22 14:52:59 -040015058 return error;
15059}
James Smarta0c87cb2009-07-19 10:01:10 -040015060
15061/**
James Smarta93ff372010-10-22 11:06:08 -040015062 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015063 * @phba: pointer to lpfc hba data structure.
15064 * @fcf_index: FCF table entry offset.
15065 *
15066 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015067 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015068 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015069 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015070 * otherwise.
15071 **/
15072int
15073lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15074{
15075 int rc = 0, error;
15076 LPFC_MBOXQ_t *mboxq;
15077
15078 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15079 if (!mboxq) {
15080 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15081 "2763 Failed to allocate mbox for "
15082 "READ_FCF cmd\n");
15083 error = -ENOMEM;
15084 goto fail_fcf_read;
15085 }
15086 /* Construct the read FCF record mailbox command */
15087 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15088 if (rc) {
15089 error = -EINVAL;
15090 goto fail_fcf_read;
15091 }
15092 /* Issue the mailbox command asynchronously */
15093 mboxq->vport = phba->pport;
15094 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15095 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15096 if (rc == MBX_NOT_FINISHED)
15097 error = -EIO;
15098 else
15099 error = 0;
15100
15101fail_fcf_read:
15102 if (error && mboxq)
15103 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15104 return error;
15105}
15106
15107/**
15108 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15109 * @phba: pointer to lpfc hba data structure.
15110 * @fcf_index: FCF table entry offset.
15111 *
15112 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015113 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015114 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015115 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015116 * otherwise.
15117 **/
15118int
15119lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15120{
15121 int rc = 0, error;
15122 LPFC_MBOXQ_t *mboxq;
15123
15124 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15125 if (!mboxq) {
15126 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15127 "2758 Failed to allocate mbox for "
15128 "READ_FCF cmd\n");
15129 error = -ENOMEM;
15130 goto fail_fcf_read;
15131 }
15132 /* Construct the read FCF record mailbox command */
15133 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15134 if (rc) {
15135 error = -EINVAL;
15136 goto fail_fcf_read;
15137 }
15138 /* Issue the mailbox command asynchronously */
15139 mboxq->vport = phba->pport;
15140 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15141 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15142 if (rc == MBX_NOT_FINISHED)
15143 error = -EIO;
15144 else
15145 error = 0;
15146
15147fail_fcf_read:
15148 if (error && mboxq)
15149 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15150 return error;
15151}
15152
15153/**
James Smart7d791df2011-07-22 18:37:52 -040015154 * lpfc_check_next_fcf_pri
15155 * phba pointer to the lpfc_hba struct for this port.
15156 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15157 * routine when the rr_bmask is empty. The FCF indecies are put into the
15158 * rr_bmask based on their priority level. Starting from the highest priority
15159 * to the lowest. The most likely FCF candidate will be in the highest
15160 * priority group. When this routine is called it searches the fcf_pri list for
15161 * next lowest priority group and repopulates the rr_bmask with only those
15162 * fcf_indexes.
15163 * returns:
15164 * 1=success 0=failure
15165 **/
15166int
15167lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15168{
15169 uint16_t next_fcf_pri;
15170 uint16_t last_index;
15171 struct lpfc_fcf_pri *fcf_pri;
15172 int rc;
15173 int ret = 0;
15174
15175 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15176 LPFC_SLI4_FCF_TBL_INDX_MAX);
15177 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15178 "3060 Last IDX %d\n", last_index);
15179 if (list_empty(&phba->fcf.fcf_pri_list)) {
15180 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15181 "3061 Last IDX %d\n", last_index);
15182 return 0; /* Empty rr list */
15183 }
15184 next_fcf_pri = 0;
15185 /*
15186 * Clear the rr_bmask and set all of the bits that are at this
15187 * priority.
15188 */
15189 memset(phba->fcf.fcf_rr_bmask, 0,
15190 sizeof(*phba->fcf.fcf_rr_bmask));
15191 spin_lock_irq(&phba->hbalock);
15192 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15193 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15194 continue;
15195 /*
15196 * the 1st priority that has not FLOGI failed
15197 * will be the highest.
15198 */
15199 if (!next_fcf_pri)
15200 next_fcf_pri = fcf_pri->fcf_rec.priority;
15201 spin_unlock_irq(&phba->hbalock);
15202 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15203 rc = lpfc_sli4_fcf_rr_index_set(phba,
15204 fcf_pri->fcf_rec.fcf_index);
15205 if (rc)
15206 return 0;
15207 }
15208 spin_lock_irq(&phba->hbalock);
15209 }
15210 /*
15211 * if next_fcf_pri was not set above and the list is not empty then
15212 * we have failed flogis on all of them. So reset flogi failed
15213 * and start at the begining.
15214 */
15215 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15216 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15217 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15218 /*
15219 * the 1st priority that has not FLOGI failed
15220 * will be the highest.
15221 */
15222 if (!next_fcf_pri)
15223 next_fcf_pri = fcf_pri->fcf_rec.priority;
15224 spin_unlock_irq(&phba->hbalock);
15225 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15226 rc = lpfc_sli4_fcf_rr_index_set(phba,
15227 fcf_pri->fcf_rec.fcf_index);
15228 if (rc)
15229 return 0;
15230 }
15231 spin_lock_irq(&phba->hbalock);
15232 }
15233 } else
15234 ret = 1;
15235 spin_unlock_irq(&phba->hbalock);
15236
15237 return ret;
15238}
15239/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015240 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15241 * @phba: pointer to lpfc hba data structure.
15242 *
15243 * This routine is to get the next eligible FCF record index in a round
15244 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015245 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015246 * shall be returned, otherwise, the next eligible FCF record's index
15247 * shall be returned.
15248 **/
15249uint16_t
15250lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15251{
15252 uint16_t next_fcf_index;
15253
James Smart3804dc82010-07-14 15:31:37 -040015254 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015255next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015256 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15257 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015258 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15259 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015260 next_fcf_index);
15261
James Smart0c9ab6f2010-02-26 14:15:57 -050015262 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015263 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15264 /*
15265 * If we have wrapped then we need to clear the bits that
15266 * have been tested so that we can detect when we should
15267 * change the priority level.
15268 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015269 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15270 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015271 }
15272
James Smart0c9ab6f2010-02-26 14:15:57 -050015273
James Smart3804dc82010-07-14 15:31:37 -040015274 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015275 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15276 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15277 /*
15278 * If next fcf index is not found check if there are lower
15279 * Priority level fcf's in the fcf_priority list.
15280 * Set up the rr_bmask with all of the avaiable fcf bits
15281 * at that level and continue the selection process.
15282 */
15283 if (lpfc_check_next_fcf_pri_level(phba))
15284 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015285 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15286 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015287 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15288 return LPFC_FCOE_FCF_NEXT_NONE;
15289 else {
15290 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15291 "3063 Only FCF available idx %d, flag %x\n",
15292 next_fcf_index,
15293 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15294 return next_fcf_index;
15295 }
James Smart3804dc82010-07-14 15:31:37 -040015296 }
15297
James Smart7d791df2011-07-22 18:37:52 -040015298 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15299 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15300 LPFC_FCF_FLOGI_FAILED)
15301 goto next_priority;
15302
James Smart3804dc82010-07-14 15:31:37 -040015303 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015304 "2845 Get next roundrobin failover FCF (x%x)\n",
15305 next_fcf_index);
15306
James Smart0c9ab6f2010-02-26 14:15:57 -050015307 return next_fcf_index;
15308}
15309
15310/**
15311 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15312 * @phba: pointer to lpfc hba data structure.
15313 *
15314 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015315 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015316 * does not go beyond the range of the driver allocated bmask dimension
15317 * before setting the bit.
15318 *
15319 * Returns 0 if the index bit successfully set, otherwise, it returns
15320 * -EINVAL.
15321 **/
15322int
15323lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15324{
15325 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15326 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015327 "2610 FCF (x%x) reached driver's book "
15328 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015329 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15330 return -EINVAL;
15331 }
15332 /* Set the eligible FCF record index bmask */
15333 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15334
James Smart3804dc82010-07-14 15:31:37 -040015335 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015336 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015337 "bmask\n", fcf_index);
15338
James Smart0c9ab6f2010-02-26 14:15:57 -050015339 return 0;
15340}
15341
15342/**
James Smart3804dc82010-07-14 15:31:37 -040015343 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015344 * @phba: pointer to lpfc hba data structure.
15345 *
15346 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015347 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015348 * does not go beyond the range of the driver allocated bmask dimension
15349 * before clearing the bit.
15350 **/
15351void
15352lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15353{
James Smart7d791df2011-07-22 18:37:52 -040015354 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015355 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15356 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015357 "2762 FCF (x%x) reached driver's book "
15358 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015359 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15360 return;
15361 }
15362 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015363 spin_lock_irq(&phba->hbalock);
15364 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15365 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15366 list_del_init(&fcf_pri->list);
15367 break;
15368 }
15369 }
15370 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015371 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015372
15373 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015374 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015375 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015376}
15377
15378/**
James Smartecfd03c2010-02-12 14:41:27 -050015379 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15380 * @phba: pointer to lpfc hba data structure.
15381 *
15382 * This routine is the completion routine for the rediscover FCF table mailbox
15383 * command. If the mailbox command returned failure, it will try to stop the
15384 * FCF rediscover wait timer.
15385 **/
15386void
15387lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15388{
15389 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15390 uint32_t shdr_status, shdr_add_status;
15391
15392 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15393
15394 shdr_status = bf_get(lpfc_mbox_hdr_status,
15395 &redisc_fcf->header.cfg_shdr.response);
15396 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15397 &redisc_fcf->header.cfg_shdr.response);
15398 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015399 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015400 "2746 Requesting for FCF rediscovery failed "
15401 "status x%x add_status x%x\n",
15402 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015403 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015404 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015405 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015406 spin_unlock_irq(&phba->hbalock);
15407 /*
15408 * CVL event triggered FCF rediscover request failed,
15409 * last resort to re-try current registered FCF entry.
15410 */
15411 lpfc_retry_pport_discovery(phba);
15412 } else {
15413 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015414 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015415 spin_unlock_irq(&phba->hbalock);
15416 /*
15417 * DEAD FCF event triggered FCF rediscover request
15418 * failed, last resort to fail over as a link down
15419 * to FCF registration.
15420 */
15421 lpfc_sli4_fcf_dead_failthrough(phba);
15422 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015423 } else {
15424 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015425 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015426 /*
15427 * Start FCF rediscovery wait timer for pending FCF
15428 * before rescan FCF record table.
15429 */
15430 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015431 }
James Smartecfd03c2010-02-12 14:41:27 -050015432
15433 mempool_free(mbox, phba->mbox_mem_pool);
15434}
15435
15436/**
James Smart3804dc82010-07-14 15:31:37 -040015437 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015438 * @phba: pointer to lpfc hba data structure.
15439 *
15440 * This routine is invoked to request for rediscovery of the entire FCF table
15441 * by the port.
15442 **/
15443int
15444lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15445{
15446 LPFC_MBOXQ_t *mbox;
15447 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15448 int rc, length;
15449
James Smart0c9ab6f2010-02-26 14:15:57 -050015450 /* Cancel retry delay timers to all vports before FCF rediscover */
15451 lpfc_cancel_all_vport_retry_delay_timer(phba);
15452
James Smartecfd03c2010-02-12 14:41:27 -050015453 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15454 if (!mbox) {
15455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15456 "2745 Failed to allocate mbox for "
15457 "requesting FCF rediscover.\n");
15458 return -ENOMEM;
15459 }
15460
15461 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15462 sizeof(struct lpfc_sli4_cfg_mhdr));
15463 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15464 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15465 length, LPFC_SLI4_MBX_EMBED);
15466
15467 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15468 /* Set count to 0 for invalidating the entire FCF database */
15469 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15470
15471 /* Issue the mailbox command asynchronously */
15472 mbox->vport = phba->pport;
15473 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15474 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15475
15476 if (rc == MBX_NOT_FINISHED) {
15477 mempool_free(mbox, phba->mbox_mem_pool);
15478 return -EIO;
15479 }
15480 return 0;
15481}
15482
15483/**
James Smartfc2b9892010-02-26 14:15:29 -050015484 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15485 * @phba: pointer to lpfc hba data structure.
15486 *
15487 * This function is the failover routine as a last resort to the FCF DEAD
15488 * event when driver failed to perform fast FCF failover.
15489 **/
15490void
15491lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15492{
15493 uint32_t link_state;
15494
15495 /*
15496 * Last resort as FCF DEAD event failover will treat this as
15497 * a link down, but save the link state because we don't want
15498 * it to be changed to Link Down unless it is already down.
15499 */
15500 link_state = phba->link_state;
15501 lpfc_linkdown(phba);
15502 phba->link_state = link_state;
15503
15504 /* Unregister FCF if no devices connected to it */
15505 lpfc_unregister_unused_fcf(phba);
15506}
15507
15508/**
James Smart026abb82011-12-13 13:20:45 -050015509 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015510 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015511 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015512 *
James Smart026abb82011-12-13 13:20:45 -050015513 * This function gets SLI3 port configure region 23 data through memory dump
15514 * mailbox command. When it successfully retrieves data, the size of the data
15515 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015516 **/
James Smart026abb82011-12-13 13:20:45 -050015517static uint32_t
15518lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015519{
15520 LPFC_MBOXQ_t *pmb = NULL;
15521 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015522 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015523 int rc;
15524
James Smart026abb82011-12-13 13:20:45 -050015525 if (!rgn23_data)
15526 return 0;
15527
James Smarta0c87cb2009-07-19 10:01:10 -040015528 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15529 if (!pmb) {
15530 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015531 "2600 failed to allocate mailbox memory\n");
15532 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015533 }
15534 mb = &pmb->u.mb;
15535
James Smarta0c87cb2009-07-19 10:01:10 -040015536 do {
15537 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15538 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15539
15540 if (rc != MBX_SUCCESS) {
15541 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015542 "2601 failed to read config "
15543 "region 23, rc 0x%x Status 0x%x\n",
15544 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015545 mb->un.varDmp.word_cnt = 0;
15546 }
15547 /*
15548 * dump mem may return a zero when finished or we got a
15549 * mailbox error, either way we are done.
15550 */
15551 if (mb->un.varDmp.word_cnt == 0)
15552 break;
15553 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15554 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15555
15556 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015557 rgn23_data + offset,
15558 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015559 offset += mb->un.varDmp.word_cnt;
15560 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15561
James Smart026abb82011-12-13 13:20:45 -050015562 mempool_free(pmb, phba->mbox_mem_pool);
15563 return offset;
15564}
15565
15566/**
15567 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15568 * @phba: pointer to lpfc hba data structure.
15569 * @rgn23_data: pointer to configure region 23 data.
15570 *
15571 * This function gets SLI4 port configure region 23 data through memory dump
15572 * mailbox command. When it successfully retrieves data, the size of the data
15573 * will be returned, otherwise, 0 will be returned.
15574 **/
15575static uint32_t
15576lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15577{
15578 LPFC_MBOXQ_t *mboxq = NULL;
15579 struct lpfc_dmabuf *mp = NULL;
15580 struct lpfc_mqe *mqe;
15581 uint32_t data_length = 0;
15582 int rc;
15583
15584 if (!rgn23_data)
15585 return 0;
15586
15587 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15588 if (!mboxq) {
15589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15590 "3105 failed to allocate mailbox memory\n");
15591 return 0;
15592 }
15593
15594 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15595 goto out;
15596 mqe = &mboxq->u.mqe;
15597 mp = (struct lpfc_dmabuf *) mboxq->context1;
15598 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15599 if (rc)
15600 goto out;
15601 data_length = mqe->un.mb_words[5];
15602 if (data_length == 0)
15603 goto out;
15604 if (data_length > DMP_RGN23_SIZE) {
15605 data_length = 0;
15606 goto out;
15607 }
15608 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15609out:
15610 mempool_free(mboxq, phba->mbox_mem_pool);
15611 if (mp) {
15612 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15613 kfree(mp);
15614 }
15615 return data_length;
15616}
15617
15618/**
15619 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15620 * @phba: pointer to lpfc hba data structure.
15621 *
15622 * This function read region 23 and parse TLV for port status to
15623 * decide if the user disaled the port. If the TLV indicates the
15624 * port is disabled, the hba_flag is set accordingly.
15625 **/
15626void
15627lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15628{
15629 uint8_t *rgn23_data = NULL;
15630 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15631 uint32_t offset = 0;
15632
15633 /* Get adapter Region 23 data */
15634 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15635 if (!rgn23_data)
15636 goto out;
15637
15638 if (phba->sli_rev < LPFC_SLI_REV4)
15639 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15640 else {
15641 if_type = bf_get(lpfc_sli_intf_if_type,
15642 &phba->sli4_hba.sli_intf);
15643 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15644 goto out;
15645 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15646 }
James Smarta0c87cb2009-07-19 10:01:10 -040015647
15648 if (!data_size)
15649 goto out;
15650
15651 /* Check the region signature first */
15652 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15654 "2619 Config region 23 has bad signature\n");
15655 goto out;
15656 }
15657 offset += 4;
15658
15659 /* Check the data structure version */
15660 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15662 "2620 Config region 23 has bad version\n");
15663 goto out;
15664 }
15665 offset += 4;
15666
15667 /* Parse TLV entries in the region */
15668 while (offset < data_size) {
15669 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15670 break;
15671 /*
15672 * If the TLV is not driver specific TLV or driver id is
15673 * not linux driver id, skip the record.
15674 */
15675 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15676 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15677 (rgn23_data[offset + 3] != 0)) {
15678 offset += rgn23_data[offset + 1] * 4 + 4;
15679 continue;
15680 }
15681
15682 /* Driver found a driver specific TLV in the config region */
15683 sub_tlv_len = rgn23_data[offset + 1] * 4;
15684 offset += 4;
15685 tlv_offset = 0;
15686
15687 /*
15688 * Search for configured port state sub-TLV.
15689 */
15690 while ((offset < data_size) &&
15691 (tlv_offset < sub_tlv_len)) {
15692 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15693 offset += 4;
15694 tlv_offset += 4;
15695 break;
15696 }
15697 if (rgn23_data[offset] != PORT_STE_TYPE) {
15698 offset += rgn23_data[offset + 1] * 4 + 4;
15699 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15700 continue;
15701 }
15702
15703 /* This HBA contains PORT_STE configured */
15704 if (!rgn23_data[offset + 2])
15705 phba->hba_flag |= LINK_DISABLED;
15706
15707 goto out;
15708 }
15709 }
James Smart026abb82011-12-13 13:20:45 -050015710
James Smarta0c87cb2009-07-19 10:01:10 -040015711out:
James Smarta0c87cb2009-07-19 10:01:10 -040015712 kfree(rgn23_data);
15713 return;
15714}
James Smart695a8142010-01-26 23:08:03 -050015715
15716/**
James Smart52d52442011-05-24 11:42:45 -040015717 * lpfc_wr_object - write an object to the firmware
15718 * @phba: HBA structure that indicates port to create a queue on.
15719 * @dmabuf_list: list of dmabufs to write to the port.
15720 * @size: the total byte value of the objects to write to the port.
15721 * @offset: the current offset to be used to start the transfer.
15722 *
15723 * This routine will create a wr_object mailbox command to send to the port.
15724 * the mailbox command will be constructed using the dma buffers described in
15725 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15726 * BDEs that the imbedded mailbox can support. The @offset variable will be
15727 * used to indicate the starting offset of the transfer and will also return
15728 * the offset after the write object mailbox has completed. @size is used to
15729 * determine the end of the object and whether the eof bit should be set.
15730 *
15731 * Return 0 is successful and offset will contain the the new offset to use
15732 * for the next write.
15733 * Return negative value for error cases.
15734 **/
15735int
15736lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15737 uint32_t size, uint32_t *offset)
15738{
15739 struct lpfc_mbx_wr_object *wr_object;
15740 LPFC_MBOXQ_t *mbox;
15741 int rc = 0, i = 0;
15742 uint32_t shdr_status, shdr_add_status;
15743 uint32_t mbox_tmo;
15744 union lpfc_sli4_cfg_shdr *shdr;
15745 struct lpfc_dmabuf *dmabuf;
15746 uint32_t written = 0;
15747
15748 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15749 if (!mbox)
15750 return -ENOMEM;
15751
15752 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15753 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15754 sizeof(struct lpfc_mbx_wr_object) -
15755 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15756
15757 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15758 wr_object->u.request.write_offset = *offset;
15759 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15760 wr_object->u.request.object_name[0] =
15761 cpu_to_le32(wr_object->u.request.object_name[0]);
15762 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15763 list_for_each_entry(dmabuf, dmabuf_list, list) {
15764 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15765 break;
15766 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15767 wr_object->u.request.bde[i].addrHigh =
15768 putPaddrHigh(dmabuf->phys);
15769 if (written + SLI4_PAGE_SIZE >= size) {
15770 wr_object->u.request.bde[i].tus.f.bdeSize =
15771 (size - written);
15772 written += (size - written);
15773 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15774 } else {
15775 wr_object->u.request.bde[i].tus.f.bdeSize =
15776 SLI4_PAGE_SIZE;
15777 written += SLI4_PAGE_SIZE;
15778 }
15779 i++;
15780 }
15781 wr_object->u.request.bde_count = i;
15782 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15783 if (!phba->sli4_hba.intr_enable)
15784 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15785 else {
James Smarta183a152011-10-10 21:32:43 -040015786 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015787 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15788 }
15789 /* The IOCTL status is embedded in the mailbox subheader. */
15790 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15791 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15792 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15793 if (rc != MBX_TIMEOUT)
15794 mempool_free(mbox, phba->mbox_mem_pool);
15795 if (shdr_status || shdr_add_status || rc) {
15796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15797 "3025 Write Object mailbox failed with "
15798 "status x%x add_status x%x, mbx status x%x\n",
15799 shdr_status, shdr_add_status, rc);
15800 rc = -ENXIO;
15801 } else
15802 *offset += wr_object->u.response.actual_write_length;
15803 return rc;
15804}
15805
15806/**
James Smart695a8142010-01-26 23:08:03 -050015807 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15808 * @vport: pointer to vport data structure.
15809 *
15810 * This function iterate through the mailboxq and clean up all REG_LOGIN
15811 * and REG_VPI mailbox commands associated with the vport. This function
15812 * is called when driver want to restart discovery of the vport due to
15813 * a Clear Virtual Link event.
15814 **/
15815void
15816lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15817{
15818 struct lpfc_hba *phba = vport->phba;
15819 LPFC_MBOXQ_t *mb, *nextmb;
15820 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015821 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015822 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015823 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015824 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015825 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015826
James Smartd439d282010-09-29 11:18:45 -040015827 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015828 spin_lock_irq(&phba->hbalock);
15829 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15830 if (mb->vport != vport)
15831 continue;
15832
15833 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15834 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15835 continue;
15836
James Smartd439d282010-09-29 11:18:45 -040015837 list_del(&mb->list);
15838 list_add_tail(&mb->list, &mbox_cmd_list);
15839 }
15840 /* Clean up active mailbox command with the vport */
15841 mb = phba->sli.mbox_active;
15842 if (mb && (mb->vport == vport)) {
15843 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15844 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15845 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15846 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15847 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15848 /* Put reference count for delayed processing */
15849 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15850 /* Unregister the RPI when mailbox complete */
15851 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15852 }
15853 }
James Smart63e801c2010-11-20 23:14:19 -050015854 /* Cleanup any mailbox completions which are not yet processed */
15855 do {
15856 restart_loop = 0;
15857 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15858 /*
15859 * If this mailox is already processed or it is
15860 * for another vport ignore it.
15861 */
15862 if ((mb->vport != vport) ||
15863 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15864 continue;
15865
15866 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15867 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15868 continue;
15869
15870 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15871 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15872 ndlp = (struct lpfc_nodelist *)mb->context2;
15873 /* Unregister the RPI when mailbox complete */
15874 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15875 restart_loop = 1;
15876 spin_unlock_irq(&phba->hbalock);
15877 spin_lock(shost->host_lock);
15878 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15879 spin_unlock(shost->host_lock);
15880 spin_lock_irq(&phba->hbalock);
15881 break;
15882 }
15883 }
15884 } while (restart_loop);
15885
James Smartd439d282010-09-29 11:18:45 -040015886 spin_unlock_irq(&phba->hbalock);
15887
15888 /* Release the cleaned-up mailbox commands */
15889 while (!list_empty(&mbox_cmd_list)) {
15890 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015891 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15892 mp = (struct lpfc_dmabuf *) (mb->context1);
15893 if (mp) {
15894 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15895 kfree(mp);
15896 }
James Smart78730cf2010-04-06 15:06:30 -040015897 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015898 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015899 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015900 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015901 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015902 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015903 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015904 }
James Smart695a8142010-01-26 23:08:03 -050015905 }
James Smart695a8142010-01-26 23:08:03 -050015906 mempool_free(mb, phba->mbox_mem_pool);
15907 }
James Smartd439d282010-09-29 11:18:45 -040015908
15909 /* Release the ndlp with the cleaned-up active mailbox command */
15910 if (act_mbx_ndlp) {
15911 spin_lock(shost->host_lock);
15912 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15913 spin_unlock(shost->host_lock);
15914 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015915 }
James Smart695a8142010-01-26 23:08:03 -050015916}
15917
James Smart2a9bf3d2010-06-07 15:24:45 -040015918/**
15919 * lpfc_drain_txq - Drain the txq
15920 * @phba: Pointer to HBA context object.
15921 *
15922 * This function attempt to submit IOCBs on the txq
15923 * to the adapter. For SLI4 adapters, the txq contains
15924 * ELS IOCBs that have been deferred because the there
15925 * are no SGLs. This congestion can occur with large
15926 * vport counts during node discovery.
15927 **/
15928
15929uint32_t
15930lpfc_drain_txq(struct lpfc_hba *phba)
15931{
15932 LIST_HEAD(completions);
15933 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15934 struct lpfc_iocbq *piocbq = 0;
15935 unsigned long iflags = 0;
15936 char *fail_msg = NULL;
15937 struct lpfc_sglq *sglq;
15938 union lpfc_wqe wqe;
15939
15940 spin_lock_irqsave(&phba->hbalock, iflags);
15941 if (pring->txq_cnt > pring->txq_max)
15942 pring->txq_max = pring->txq_cnt;
15943
15944 spin_unlock_irqrestore(&phba->hbalock, iflags);
15945
15946 while (pring->txq_cnt) {
15947 spin_lock_irqsave(&phba->hbalock, iflags);
15948
James Smart19ca7602010-11-20 23:11:55 -050015949 piocbq = lpfc_sli_ringtx_get(phba, pring);
15950 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015951 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015952 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015953 spin_unlock_irqrestore(&phba->hbalock, iflags);
15954 break;
15955 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040015956 if (!piocbq) {
15957 /* The txq_cnt out of sync. This should
15958 * never happen
15959 */
15960 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040015961 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040015962 spin_unlock_irqrestore(&phba->hbalock, iflags);
15963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15964 "2823 txq empty and txq_cnt is %d\n ",
15965 pring->txq_cnt);
15966 break;
15967 }
15968 }
15969
15970 /* The xri and iocb resources secured,
15971 * attempt to issue request
15972 */
James Smart6d368e52011-05-24 11:44:12 -040015973 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015974 piocbq->sli4_xritag = sglq->sli4_xritag;
15975 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15976 fail_msg = "to convert bpl to sgl";
15977 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15978 fail_msg = "to convert iocb to wqe";
15979 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15980 fail_msg = " - Wq is full";
15981 else
15982 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15983
15984 if (fail_msg) {
15985 /* Failed means we can't issue and need to cancel */
15986 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15987 "2822 IOCB failed %s iotag 0x%x "
15988 "xri 0x%x\n",
15989 fail_msg,
15990 piocbq->iotag, piocbq->sli4_xritag);
15991 list_add_tail(&piocbq->list, &completions);
15992 }
15993 spin_unlock_irqrestore(&phba->hbalock, iflags);
15994 }
15995
James Smart2a9bf3d2010-06-07 15:24:45 -040015996 /* Cancel all the IOCBs that cannot be issued */
15997 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15998 IOERR_SLI_ABORTED);
15999
16000 return pring->txq_cnt;
16001}