blob: 73da1c039b9d5cb05790d16a3be80c3014d493ba [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);
James Smartf0d9bcc2010-10-22 11:07:09 -04007842 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7843 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7844 LPFC_WQE_LENLOC_WORD4);
7845 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7846 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05007847 if (iocbq->iocb_flag & LPFC_IO_DIF) {
7848 iocbq->iocb_flag &= ~LPFC_IO_DIF;
7849 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
7850 }
7851 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04007852 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04007853 case CMD_FCP_IREAD64_CR:
7854 /* word3 iocb=iotag wqe=payload_offset_len */
7855 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7856 wqe->fcp_iread.payload_offset_len =
7857 xmit_len + sizeof(struct fcp_rsp);
7858 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7859 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7860 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7861 iocbq->iocb.ulpFCP2Rcvy);
7862 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007863 /* Always open the exchange */
7864 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007865 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7866 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7867 LPFC_WQE_LENLOC_WORD4);
7868 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7869 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05007870 if (iocbq->iocb_flag & LPFC_IO_DIF) {
7871 iocbq->iocb_flag &= ~LPFC_IO_DIF;
7872 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
7873 }
7874 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04007875 break;
James Smartf1126682009-06-10 17:22:44 -04007876 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007877 /* word3 iocb=IO_TAG wqe=reserved */
7878 wqe->fcp_icmd.rsrvd3 = 0;
7879 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007880 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007881 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7882 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7883 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7884 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7885 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7886 LPFC_WQE_LENLOC_NONE);
7887 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007888 break;
James Smart4f774512009-05-22 14:52:35 -04007889 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007890 /* For this command calculate the xmit length of the
7891 * request bde.
7892 */
7893 xmit_len = 0;
7894 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7895 sizeof(struct ulp_bde64);
7896 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007897 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007898 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7899 break;
James Smart63e801c2010-11-20 23:14:19 -05007900 xmit_len += bde.tus.f.bdeSize;
7901 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007902 /* word3 iocb=IO_TAG wqe=request_payload_len */
7903 wqe->gen_req.request_payload_len = xmit_len;
7904 /* word4 iocb=parameter wqe=relative_offset memcpy */
7905 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007906 /* word6 context tag copied in memcpy */
7907 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7908 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7910 "2015 Invalid CT %x command 0x%x\n",
7911 ct, iocbq->iocb.ulpCommand);
7912 return IOCB_ERROR;
7913 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007914 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7915 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7916 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7917 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7918 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7919 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7920 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7921 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007922 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007923 break;
James Smart4f774512009-05-22 14:52:35 -04007924 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007925 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007926 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007927 /* word3 iocb=iotag32 wqe=response_payload_len */
7928 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007929 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007930 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007931 /* word5 iocb=rsvd wge=did */
7932 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7933 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007934 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7935 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7936 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7937 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007938 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04007939 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007940 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007941 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007942 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7943 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7944 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7945 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7946 LPFC_WQE_LENLOC_WORD3);
7947 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007948 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7949 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05007950 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7951 iocbq->context2)->virt);
7952 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7953 bf_set(els_req64_sp, &wqe->els_req, 1);
7954 bf_set(els_req64_sid, &wqe->els_req,
7955 iocbq->vport->fc_myDID);
7956 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7957 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7958 phba->vpi_ids[phba->pport->vpi]);
7959 }
James Smart4f774512009-05-22 14:52:35 -04007960 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007961 break;
James Smart4f774512009-05-22 14:52:35 -04007962 case CMD_CLOSE_XRI_CN:
7963 case CMD_ABORT_XRI_CN:
7964 case CMD_ABORT_XRI_CX:
7965 /* words 0-2 memcpy should be 0 rserved */
7966 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007967 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7968 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7969 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7970 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7971 } else
7972 fip = 0;
7973
7974 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007975 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007976 * The link is down, or the command was ELS_FIP
7977 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007978 * on the wire.
7979 */
7980 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7981 else
7982 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7983 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007984 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7985 wqe->abort_cmd.rsrvd5 = 0;
7986 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007987 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7988 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007989 /*
7990 * The abort handler will send us CMD_ABORT_XRI_CN or
7991 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7992 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007993 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7994 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7995 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7996 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007997 cmnd = CMD_ABORT_XRI_CX;
7998 command_type = OTHER_COMMAND;
7999 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008000 break;
James Smart6669f9b2009-10-02 15:16:45 -04008001 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008002 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008003 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008004 * we re-construct this WQE here based on information in
8005 * iocbq from scratch.
8006 */
8007 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008008 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008009 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008010 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8011 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008012 LPFC_ABTS_UNSOL_INT) {
8013 /* ABTS sent by initiator to CT exchange, the
8014 * RX_ID field will be filled with the newly
8015 * allocated responder XRI.
8016 */
8017 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8018 iocbq->sli4_xritag);
8019 } else {
8020 /* ABTS sent by responder to CT exchange, the
8021 * RX_ID field will be filled with the responder
8022 * RX_ID from ABTS.
8023 */
8024 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008025 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008026 }
James Smart6669f9b2009-10-02 15:16:45 -04008027 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8028 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008029
8030 /* Use CT=VPI */
8031 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8032 ndlp->nlp_DID);
8033 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8034 iocbq->iocb.ulpContext);
8035 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008036 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008037 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008038 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8039 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8040 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008041 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8042 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008043 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8044 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8045 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8046 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8047 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8048 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8049 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8050 }
8051
James Smart7851fe22011-07-22 18:36:52 -04008052 break;
James Smart4f774512009-05-22 14:52:35 -04008053 case CMD_XRI_ABORTED_CX:
8054 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008055 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8056 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8057 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8058 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8059 default:
8060 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8061 "2014 Invalid command 0x%x\n",
8062 iocbq->iocb.ulpCommand);
8063 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008064 break;
James Smart4f774512009-05-22 14:52:35 -04008065 }
James Smart6d368e52011-05-24 11:44:12 -04008066
James Smartf0d9bcc2010-10-22 11:07:09 -04008067 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8068 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8069 wqe->generic.wqe_com.abort_tag = abort_tag;
8070 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8071 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8072 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8073 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008074 return 0;
8075}
8076
8077/**
8078 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8079 * @phba: Pointer to HBA context object.
8080 * @ring_number: SLI ring number to issue iocb on.
8081 * @piocb: Pointer to command iocb.
8082 * @flag: Flag indicating if this command can be put into txq.
8083 *
8084 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8085 * an iocb command to an HBA with SLI-4 interface spec.
8086 *
8087 * This function is called with hbalock held. The function will return success
8088 * after it successfully submit the iocb to firmware or after adding to the
8089 * txq.
8090 **/
8091static int
8092__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8093 struct lpfc_iocbq *piocb, uint32_t flag)
8094{
8095 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008096 union lpfc_wqe wqe;
8097 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008098
8099 if (piocb->sli4_xritag == NO_XRI) {
8100 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008101 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008102 sglq = NULL;
8103 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008104 if (pring->txq_cnt) {
8105 if (!(flag & SLI_IOCB_RET_IOCB)) {
8106 __lpfc_sli_ringtx_put(phba,
8107 pring, piocb);
8108 return IOCB_SUCCESS;
8109 } else {
8110 return IOCB_BUSY;
8111 }
8112 } else {
James Smart6d368e52011-05-24 11:44:12 -04008113 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008114 if (!sglq) {
8115 if (!(flag & SLI_IOCB_RET_IOCB)) {
8116 __lpfc_sli_ringtx_put(phba,
8117 pring,
8118 piocb);
8119 return IOCB_SUCCESS;
8120 } else
8121 return IOCB_BUSY;
8122 }
8123 }
James Smart4f774512009-05-22 14:52:35 -04008124 }
8125 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008126 /* These IO's already have an XRI and a mapped sgl. */
8127 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008128 } else {
James Smart6d368e52011-05-24 11:44:12 -04008129 /*
8130 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008131 * sglq is on the active list
8132 */
8133 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8134 if (!sglq)
8135 return IOCB_ERROR;
8136 }
8137
8138 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008139 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008140 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008141 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008142 return IOCB_ERROR;
8143 }
8144
8145 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8146 return IOCB_ERROR;
8147
James Smart341af102010-01-26 23:07:37 -05008148 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8149 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008150 /*
8151 * For FCP command IOCB, get a new WQ index to distribute
8152 * WQE across the WQsr. On the other hand, for abort IOCB,
8153 * it carries the same WQ index to the original command
8154 * IOCB.
8155 */
James Smart341af102010-01-26 23:07:37 -05008156 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008157 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05008158 if (unlikely(!phba->sli4_hba.fcp_wq))
8159 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008160 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8161 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008162 return IOCB_ERROR;
8163 } else {
8164 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8165 return IOCB_ERROR;
8166 }
8167 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8168
8169 return 0;
8170}
8171
8172/**
James Smart3772a992009-05-22 14:50:54 -04008173 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8174 *
8175 * This routine wraps the actual lockless version for issusing IOCB function
8176 * pointer from the lpfc_hba struct.
8177 *
8178 * Return codes:
8179 * IOCB_ERROR - Error
8180 * IOCB_SUCCESS - Success
8181 * IOCB_BUSY - Busy
8182 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008183int
James Smart3772a992009-05-22 14:50:54 -04008184__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8185 struct lpfc_iocbq *piocb, uint32_t flag)
8186{
8187 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8188}
8189
8190/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008191 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008192 * @phba: The hba struct for which this call is being executed.
8193 * @dev_grp: The HBA PCI-Device group number.
8194 *
8195 * This routine sets up the SLI interface API function jump table in @phba
8196 * struct.
8197 * Returns: 0 - success, -ENODEV - failure.
8198 **/
8199int
8200lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8201{
8202
8203 switch (dev_grp) {
8204 case LPFC_PCI_DEV_LP:
8205 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8206 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8207 break;
James Smart4f774512009-05-22 14:52:35 -04008208 case LPFC_PCI_DEV_OC:
8209 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8210 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8211 break;
James Smart3772a992009-05-22 14:50:54 -04008212 default:
8213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8214 "1419 Invalid HBA PCI-device group: 0x%x\n",
8215 dev_grp);
8216 return -ENODEV;
8217 break;
8218 }
8219 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8220 return 0;
8221}
James Smart92d7f7b2007-06-17 19:56:38 -05008222
James Smarte59058c2008-08-24 21:49:00 -04008223/**
James Smart3621a712009-04-06 18:47:14 -04008224 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008225 * @phba: Pointer to HBA context object.
8226 * @pring: Pointer to driver SLI ring object.
8227 * @piocb: Pointer to command iocb.
8228 * @flag: Flag indicating if this command can be put into txq.
8229 *
8230 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8231 * function. This function gets the hbalock and calls
8232 * __lpfc_sli_issue_iocb function and will return the error returned
8233 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8234 * functions which do not hold hbalock.
8235 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008236int
James Smart3772a992009-05-22 14:50:54 -04008237lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008238 struct lpfc_iocbq *piocb, uint32_t flag)
8239{
8240 unsigned long iflags;
8241 int rc;
8242
8243 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04008244 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05008245 spin_unlock_irqrestore(&phba->hbalock, iflags);
8246
8247 return rc;
8248}
8249
James Smarte59058c2008-08-24 21:49:00 -04008250/**
James Smart3621a712009-04-06 18:47:14 -04008251 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008252 * @phba: Pointer to HBA context object.
8253 *
8254 * This function is called while driver attaches with the
8255 * HBA to setup the extra ring. The extra ring is used
8256 * only when driver needs to support target mode functionality
8257 * or IP over FC functionalities.
8258 *
8259 * This function is called with no lock held.
8260 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008261static int
8262lpfc_extra_ring_setup( struct lpfc_hba *phba)
8263{
8264 struct lpfc_sli *psli;
8265 struct lpfc_sli_ring *pring;
8266
8267 psli = &phba->sli;
8268
8269 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008270
8271 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008272 pring = &psli->ring[psli->fcp_ring];
8273 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
James Smarta4bc3372006-12-02 13:34:16 -05008278 /* and give them to the extra ring */
8279 pring = &psli->ring[psli->extra_ring];
8280
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008281 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8282 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8283 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8284 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8285
8286 /* Setup default profile for this ring */
8287 pring->iotag_max = 4096;
8288 pring->num_mask = 1;
8289 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008290 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8291 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008292 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8293 return 0;
8294}
8295
James Smartcb69f7d2011-12-13 13:21:57 -05008296/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8297 * @vport: pointer to virtual port object.
8298 * @ndlp: nodelist pointer for the impacted rport.
8299 *
8300 * The driver calls this routine in response to a XRI ABORT CQE
8301 * event from the port. In this event, the driver is required to
8302 * recover its login to the rport even though its login may be valid
8303 * from the driver's perspective. The failed ABTS notice from the
8304 * port indicates the rport is not responding.
8305 */
8306static void
8307lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8308 struct lpfc_nodelist *ndlp)
8309{
8310 struct Scsi_Host *shost;
8311 struct lpfc_hba *phba;
8312 unsigned long flags = 0;
8313
8314 shost = lpfc_shost_from_vport(vport);
8315 phba = vport->phba;
8316 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8317 lpfc_printf_log(phba, KERN_INFO,
8318 LOG_SLI, "3093 No rport recovery needed. "
8319 "rport in state 0x%x\n",
8320 ndlp->nlp_state);
8321 return;
8322 }
8323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8324 "3094 Start rport recovery on shost id 0x%x "
8325 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8326 "flags 0x%x\n",
8327 shost->host_no, ndlp->nlp_DID,
8328 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8329 ndlp->nlp_flag);
8330 /*
8331 * The rport is not responding. Don't attempt ADISC recovery.
8332 * Remove the FCP-2 flag to force a PLOGI.
8333 */
8334 spin_lock_irqsave(shost->host_lock, flags);
8335 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8336 spin_unlock_irqrestore(shost->host_lock, flags);
8337 lpfc_disc_state_machine(vport, ndlp, NULL,
8338 NLP_EVT_DEVICE_RECOVERY);
8339 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8340 spin_lock_irqsave(shost->host_lock, flags);
8341 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8342 spin_unlock_irqrestore(shost->host_lock, flags);
8343 lpfc_disc_start(vport);
8344}
8345
8346/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8347 * @phba: Pointer to HBA context object.
8348 * @iocbq: Pointer to iocb object.
8349 *
8350 * The async_event handler calls this routine when it receives
8351 * an ASYNC_STATUS_CN event from the port. The port generates
8352 * this event when an Abort Sequence request to an rport fails
8353 * twice in succession. The abort could be originated by the
8354 * driver or by the port. The ABTS could have been for an ELS
8355 * or FCP IO. The port only generates this event when an ABTS
8356 * fails to complete after one retry.
8357 */
8358static void
8359lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8360 struct lpfc_iocbq *iocbq)
8361{
8362 struct lpfc_nodelist *ndlp = NULL;
8363 uint16_t rpi = 0, vpi = 0;
8364 struct lpfc_vport *vport = NULL;
8365
8366 /* The rpi in the ulpContext is vport-sensitive. */
8367 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8368 rpi = iocbq->iocb.ulpContext;
8369
8370 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8371 "3092 Port generated ABTS async event "
8372 "on vpi %d rpi %d status 0x%x\n",
8373 vpi, rpi, iocbq->iocb.ulpStatus);
8374
8375 vport = lpfc_find_vport_by_vpid(phba, vpi);
8376 if (!vport)
8377 goto err_exit;
8378 ndlp = lpfc_findnode_rpi(vport, rpi);
8379 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8380 goto err_exit;
8381
8382 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8383 lpfc_sli_abts_recover_port(vport, ndlp);
8384 return;
8385
8386 err_exit:
8387 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8388 "3095 Event Context not found, no "
8389 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8390 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8391 vpi, rpi);
8392}
8393
8394/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8395 * @phba: pointer to HBA context object.
8396 * @ndlp: nodelist pointer for the impacted rport.
8397 * @axri: pointer to the wcqe containing the failed exchange.
8398 *
8399 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8400 * port. The port generates this event when an abort exchange request to an
8401 * rport fails twice in succession with no reply. The abort could be originated
8402 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8403 */
8404void
8405lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8406 struct lpfc_nodelist *ndlp,
8407 struct sli4_wcqe_xri_aborted *axri)
8408{
8409 struct lpfc_vport *vport;
8410
James Smart6b5151f2012-01-18 16:24:06 -05008411 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008412 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8413 "3115 Node Context not found, driver "
8414 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008415 return;
8416 }
8417
James Smartcb69f7d2011-12-13 13:21:57 -05008418 vport = ndlp->vport;
8419 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8420 "3116 Port generated FCP XRI ABORT event on "
8421 "vpi %d rpi %d xri x%x status 0x%x\n",
8422 ndlp->vport->vpi, ndlp->nlp_rpi,
8423 bf_get(lpfc_wcqe_xa_xri, axri),
8424 bf_get(lpfc_wcqe_xa_status, axri));
8425
8426 if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
8427 lpfc_sli_abts_recover_port(vport, ndlp);
8428}
8429
James Smarte59058c2008-08-24 21:49:00 -04008430/**
James Smart3621a712009-04-06 18:47:14 -04008431 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008432 * @phba: Pointer to HBA context object.
8433 * @pring: Pointer to driver SLI ring object.
8434 * @iocbq: Pointer to iocb object.
8435 *
8436 * This function is called by the slow ring event handler
8437 * function when there is an ASYNC event iocb in the ring.
8438 * This function is called with no lock held.
8439 * Currently this function handles only temperature related
8440 * ASYNC events. The function decodes the temperature sensor
8441 * event message and posts events for the management applications.
8442 **/
James Smart98c9ea52007-10-27 13:37:33 -04008443static void
James Smart57127f12007-10-27 13:37:05 -04008444lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8445 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8446{
8447 IOCB_t *icmd;
8448 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008449 struct temp_event temp_event_data;
8450 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008451 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008452
8453 icmd = &iocbq->iocb;
8454 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008455
James Smartcb69f7d2011-12-13 13:21:57 -05008456 switch (evt_code) {
8457 case ASYNC_TEMP_WARN:
8458 case ASYNC_TEMP_SAFE:
8459 temp_event_data.data = (uint32_t) icmd->ulpContext;
8460 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8461 if (evt_code == ASYNC_TEMP_WARN) {
8462 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8463 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8464 "0347 Adapter is very hot, please take "
8465 "corrective action. temperature : %d Celsius\n",
8466 (uint32_t) icmd->ulpContext);
8467 } else {
8468 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8469 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8470 "0340 Adapter temperature is OK now. "
8471 "temperature : %d Celsius\n",
8472 (uint32_t) icmd->ulpContext);
8473 }
8474
8475 /* Send temperature change event to applications */
8476 shost = lpfc_shost_from_vport(phba->pport);
8477 fc_host_post_vendor_event(shost, fc_get_event_number(),
8478 sizeof(temp_event_data), (char *) &temp_event_data,
8479 LPFC_NL_VENDOR_ID);
8480 break;
8481 case ASYNC_STATUS_CN:
8482 lpfc_sli_abts_err_handler(phba, iocbq);
8483 break;
8484 default:
James Smarta257bf92009-04-06 18:48:10 -04008485 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008486 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008487 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008488 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008489 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8490 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8491 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8492 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008493 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008494 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8495 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8496 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8497 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8498
James Smartcb69f7d2011-12-13 13:21:57 -05008499 break;
James Smart57127f12007-10-27 13:37:05 -04008500 }
James Smart57127f12007-10-27 13:37:05 -04008501}
8502
8503
James Smarte59058c2008-08-24 21:49:00 -04008504/**
James Smart3621a712009-04-06 18:47:14 -04008505 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008506 * @phba: Pointer to HBA context object.
8507 *
8508 * lpfc_sli_setup sets up rings of the SLI interface with
8509 * number of iocbs per ring and iotags. This function is
8510 * called while driver attach to the HBA and before the
8511 * interrupts are enabled. So there is no need for locking.
8512 *
8513 * This function always returns 0.
8514 **/
dea31012005-04-17 16:05:31 -05008515int
8516lpfc_sli_setup(struct lpfc_hba *phba)
8517{
James Smarted957682007-06-17 19:56:37 -05008518 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008519 struct lpfc_sli *psli = &phba->sli;
8520 struct lpfc_sli_ring *pring;
8521
8522 psli->num_rings = MAX_CONFIGURED_RINGS;
8523 psli->sli_flag = 0;
8524 psli->fcp_ring = LPFC_FCP_RING;
8525 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008526 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008527
James Bottomley604a3e32005-10-29 10:28:33 -05008528 psli->iocbq_lookup = NULL;
8529 psli->iocbq_lookup_len = 0;
8530 psli->last_iotag = 0;
8531
dea31012005-04-17 16:05:31 -05008532 for (i = 0; i < psli->num_rings; i++) {
8533 pring = &psli->ring[i];
8534 switch (i) {
8535 case LPFC_FCP_RING: /* ring 0 - FCP */
8536 /* numCiocb and numRiocb are used in config_port */
8537 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8538 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8539 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8540 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8541 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8542 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008543 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008544 SLI3_IOCB_CMD_SIZE :
8545 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008546 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008547 SLI3_IOCB_RSP_SIZE :
8548 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008549 pring->iotag_ctr = 0;
8550 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008551 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008552 pring->fast_iotag = pring->iotag_max;
8553 pring->num_mask = 0;
8554 break;
James Smarta4bc3372006-12-02 13:34:16 -05008555 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008556 /* numCiocb and numRiocb are used in config_port */
8557 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8558 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008559 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008560 SLI3_IOCB_CMD_SIZE :
8561 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008562 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008563 SLI3_IOCB_RSP_SIZE :
8564 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008565 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008566 pring->num_mask = 0;
8567 break;
8568 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8569 /* numCiocb and numRiocb are used in config_port */
8570 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8571 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008572 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008573 SLI3_IOCB_CMD_SIZE :
8574 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008575 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008576 SLI3_IOCB_RSP_SIZE :
8577 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008578 pring->fast_iotag = 0;
8579 pring->iotag_ctr = 0;
8580 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008581 pring->lpfc_sli_rcv_async_status =
8582 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008583 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008584 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008585 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8586 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008587 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008588 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008589 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008590 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8591 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008592 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008593 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008594 pring->prt[2].profile = 0; /* Mask 2 */
8595 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008596 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008597 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008598 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008599 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008600 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008601 pring->prt[3].profile = 0; /* Mask 3 */
8602 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008603 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008604 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008605 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008606 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008607 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008608 /* abort unsolicited sequence */
8609 pring->prt[4].profile = 0; /* Mask 4 */
8610 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8611 pring->prt[4].type = FC_TYPE_BLS;
8612 pring->prt[4].lpfc_sli_rcv_unsol_event =
8613 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008614 break;
8615 }
James Smarted957682007-06-17 19:56:37 -05008616 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008617 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008618 }
James Smarted957682007-06-17 19:56:37 -05008619 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008620 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008621 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8622 "SLI2 SLIM Data: x%x x%lx\n",
8623 phba->brd_no, totiocbsize,
8624 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008625 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008626 if (phba->cfg_multi_ring_support == 2)
8627 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008628
8629 return 0;
8630}
8631
James Smarte59058c2008-08-24 21:49:00 -04008632/**
James Smart3621a712009-04-06 18:47:14 -04008633 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008634 * @phba: Pointer to HBA context object.
8635 *
8636 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8637 * ring. This function also initializes ring indices of each ring.
8638 * This function is called during the initialization of the SLI
8639 * interface of an HBA.
8640 * This function is called with no lock held and always returns
8641 * 1.
8642 **/
dea31012005-04-17 16:05:31 -05008643int
James Smart2e0fef82007-06-17 19:56:36 -05008644lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008645{
8646 struct lpfc_sli *psli;
8647 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008648 int i;
dea31012005-04-17 16:05:31 -05008649
8650 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008651 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008652 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008653 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008654 /* Initialize list headers for txq and txcmplq as double linked lists */
8655 for (i = 0; i < psli->num_rings; i++) {
8656 pring = &psli->ring[i];
8657 pring->ringno = i;
8658 pring->next_cmdidx = 0;
8659 pring->local_getidx = 0;
8660 pring->cmdidx = 0;
8661 INIT_LIST_HEAD(&pring->txq);
8662 INIT_LIST_HEAD(&pring->txcmplq);
8663 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008664 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008665 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008666 }
James Smart2e0fef82007-06-17 19:56:36 -05008667 spin_unlock_irq(&phba->hbalock);
8668 return 1;
dea31012005-04-17 16:05:31 -05008669}
8670
James Smarte59058c2008-08-24 21:49:00 -04008671/**
James Smart04c68492009-05-22 14:52:52 -04008672 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8673 * @phba: Pointer to HBA context object.
8674 *
8675 * This routine flushes the mailbox command subsystem. It will unconditionally
8676 * flush all the mailbox commands in the three possible stages in the mailbox
8677 * command sub-system: pending mailbox command queue; the outstanding mailbox
8678 * command; and completed mailbox command queue. It is caller's responsibility
8679 * to make sure that the driver is in the proper state to flush the mailbox
8680 * command sub-system. Namely, the posting of mailbox commands into the
8681 * pending mailbox command queue from the various clients must be stopped;
8682 * either the HBA is in a state that it will never works on the outstanding
8683 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8684 * mailbox command has been completed.
8685 **/
8686static void
8687lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8688{
8689 LIST_HEAD(completions);
8690 struct lpfc_sli *psli = &phba->sli;
8691 LPFC_MBOXQ_t *pmb;
8692 unsigned long iflag;
8693
8694 /* Flush all the mailbox commands in the mbox system */
8695 spin_lock_irqsave(&phba->hbalock, iflag);
8696 /* The pending mailbox command queue */
8697 list_splice_init(&phba->sli.mboxq, &completions);
8698 /* The outstanding active mailbox command */
8699 if (psli->mbox_active) {
8700 list_add_tail(&psli->mbox_active->list, &completions);
8701 psli->mbox_active = NULL;
8702 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8703 }
8704 /* The completed mailbox command queue */
8705 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8706 spin_unlock_irqrestore(&phba->hbalock, iflag);
8707
8708 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8709 while (!list_empty(&completions)) {
8710 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8711 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8712 if (pmb->mbox_cmpl)
8713 pmb->mbox_cmpl(phba, pmb);
8714 }
8715}
8716
8717/**
James Smart3621a712009-04-06 18:47:14 -04008718 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008719 * @vport: Pointer to virtual port object.
8720 *
8721 * lpfc_sli_host_down is called to clean up the resources
8722 * associated with a vport before destroying virtual
8723 * port data structures.
8724 * This function does following operations:
8725 * - Free discovery resources associated with this virtual
8726 * port.
8727 * - Free iocbs associated with this virtual port in
8728 * the txq.
8729 * - Send abort for all iocb commands associated with this
8730 * vport in txcmplq.
8731 *
8732 * This function is called with no lock held and always returns 1.
8733 **/
dea31012005-04-17 16:05:31 -05008734int
James Smart92d7f7b2007-06-17 19:56:38 -05008735lpfc_sli_host_down(struct lpfc_vport *vport)
8736{
James Smart858c9f62007-06-17 19:56:39 -05008737 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008738 struct lpfc_hba *phba = vport->phba;
8739 struct lpfc_sli *psli = &phba->sli;
8740 struct lpfc_sli_ring *pring;
8741 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008742 int i;
8743 unsigned long flags = 0;
8744 uint16_t prev_pring_flag;
8745
8746 lpfc_cleanup_discovery_resources(vport);
8747
8748 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008749 for (i = 0; i < psli->num_rings; i++) {
8750 pring = &psli->ring[i];
8751 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008752 /* Only slow rings */
8753 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008754 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008755 /* Set the lpfc data pending flag */
8756 set_bit(LPFC_DATA_READY, &phba->data_flags);
8757 }
James Smart92d7f7b2007-06-17 19:56:38 -05008758 /*
8759 * Error everything on the txq since these iocbs have not been
8760 * given to the FW yet.
8761 */
James Smart92d7f7b2007-06-17 19:56:38 -05008762 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8763 if (iocb->vport != vport)
8764 continue;
James Smart858c9f62007-06-17 19:56:39 -05008765 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008766 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008767 }
8768
8769 /* Next issue ABTS for everything on the txcmplq */
8770 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8771 list) {
8772 if (iocb->vport != vport)
8773 continue;
8774 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8775 }
8776
8777 pring->flag = prev_pring_flag;
8778 }
8779
8780 spin_unlock_irqrestore(&phba->hbalock, flags);
8781
James Smarta257bf92009-04-06 18:48:10 -04008782 /* Cancel all the IOCBs from the completions list */
8783 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8784 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008785 return 1;
8786}
8787
James Smarte59058c2008-08-24 21:49:00 -04008788/**
James Smart3621a712009-04-06 18:47:14 -04008789 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008790 * @phba: Pointer to HBA context object.
8791 *
8792 * This function cleans up all iocb, buffers, mailbox commands
8793 * while shutting down the HBA. This function is called with no
8794 * lock held and always returns 1.
8795 * This function does the following to cleanup driver resources:
8796 * - Free discovery resources for each virtual port
8797 * - Cleanup any pending fabric iocbs
8798 * - Iterate through the iocb txq and free each entry
8799 * in the list.
8800 * - Free up any buffer posted to the HBA
8801 * - Free mailbox commands in the mailbox queue.
8802 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008803int
James Smart2e0fef82007-06-17 19:56:36 -05008804lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008805{
James Smart2534ba72007-04-25 09:52:20 -04008806 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008807 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008808 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008809 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008810 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008811 int i;
8812
8813 /* Shutdown the mailbox command sub-system */
8814 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008815
dea31012005-04-17 16:05:31 -05008816 lpfc_hba_down_prep(phba);
8817
James Smart92d7f7b2007-06-17 19:56:38 -05008818 lpfc_fabric_abort_hba(phba);
8819
James Smart2e0fef82007-06-17 19:56:36 -05008820 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008821 for (i = 0; i < psli->num_rings; i++) {
8822 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008823 /* Only slow rings */
8824 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008825 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008826 /* Set the lpfc data pending flag */
8827 set_bit(LPFC_DATA_READY, &phba->data_flags);
8828 }
dea31012005-04-17 16:05:31 -05008829
8830 /*
8831 * Error everything on the txq since these iocbs have not been
8832 * given to the FW yet.
8833 */
James Smart2534ba72007-04-25 09:52:20 -04008834 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008835 pring->txq_cnt = 0;
8836
dea31012005-04-17 16:05:31 -05008837 }
James Smart2e0fef82007-06-17 19:56:36 -05008838 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008839
James Smarta257bf92009-04-06 18:48:10 -04008840 /* Cancel all the IOCBs from the completions list */
8841 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8842 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008843
James Smart0ff10d42008-01-11 01:52:36 -05008844 spin_lock_irqsave(&phba->hbalock, flags);
8845 list_splice_init(&phba->elsbuf, &completions);
8846 phba->elsbuf_cnt = 0;
8847 phba->elsbuf_prev_cnt = 0;
8848 spin_unlock_irqrestore(&phba->hbalock, flags);
8849
8850 while (!list_empty(&completions)) {
8851 list_remove_head(&completions, buf_ptr,
8852 struct lpfc_dmabuf, list);
8853 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8854 kfree(buf_ptr);
8855 }
8856
dea31012005-04-17 16:05:31 -05008857 /* Return any active mbox cmds */
8858 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008859
James Smartda0436e2009-05-22 14:51:39 -04008860 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008861 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008862 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008863
James Smartda0436e2009-05-22 14:51:39 -04008864 return 1;
8865}
James Smart92d7f7b2007-06-17 19:56:38 -05008866
James Smartda0436e2009-05-22 14:51:39 -04008867/**
James Smart3621a712009-04-06 18:47:14 -04008868 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008869 * @srcp: Source memory pointer.
8870 * @destp: Destination memory pointer.
8871 * @cnt: Number of words required to be copied.
8872 *
8873 * This function is used for copying data between driver memory
8874 * and the SLI memory. This function also changes the endianness
8875 * of each word if native endianness is different from SLI
8876 * endianness. This function can be called with or without
8877 * lock.
8878 **/
dea31012005-04-17 16:05:31 -05008879void
8880lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8881{
8882 uint32_t *src = srcp;
8883 uint32_t *dest = destp;
8884 uint32_t ldata;
8885 int i;
8886
8887 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8888 ldata = *src;
8889 ldata = le32_to_cpu(ldata);
8890 *dest = ldata;
8891 src++;
8892 dest++;
8893 }
8894}
8895
James Smarte59058c2008-08-24 21:49:00 -04008896
8897/**
James Smarta0c87cb2009-07-19 10:01:10 -04008898 * lpfc_sli_bemem_bcopy - SLI memory copy function
8899 * @srcp: Source memory pointer.
8900 * @destp: Destination memory pointer.
8901 * @cnt: Number of words required to be copied.
8902 *
8903 * This function is used for copying data between a data structure
8904 * with big endian representation to local endianness.
8905 * This function can be called with or without lock.
8906 **/
8907void
8908lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8909{
8910 uint32_t *src = srcp;
8911 uint32_t *dest = destp;
8912 uint32_t ldata;
8913 int i;
8914
8915 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8916 ldata = *src;
8917 ldata = be32_to_cpu(ldata);
8918 *dest = ldata;
8919 src++;
8920 dest++;
8921 }
8922}
8923
8924/**
James Smart3621a712009-04-06 18:47:14 -04008925 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008926 * @phba: Pointer to HBA context object.
8927 * @pring: Pointer to driver SLI ring object.
8928 * @mp: Pointer to driver buffer object.
8929 *
8930 * This function is called with no lock held.
8931 * It always return zero after adding the buffer to the postbufq
8932 * buffer list.
8933 **/
dea31012005-04-17 16:05:31 -05008934int
James Smart2e0fef82007-06-17 19:56:36 -05008935lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8936 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008937{
8938 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8939 later */
James Smart2e0fef82007-06-17 19:56:36 -05008940 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008941 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008942 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008943 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008944 return 0;
8945}
8946
James Smarte59058c2008-08-24 21:49:00 -04008947/**
James Smart3621a712009-04-06 18:47:14 -04008948 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008949 * @phba: Pointer to HBA context object.
8950 *
8951 * When HBQ is enabled, buffers are searched based on tags. This function
8952 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8953 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8954 * does not conflict with tags of buffer posted for unsolicited events.
8955 * The function returns the allocated tag. The function is called with
8956 * no locks held.
8957 **/
James Smart76bb24e2007-10-27 13:38:00 -04008958uint32_t
8959lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8960{
8961 spin_lock_irq(&phba->hbalock);
8962 phba->buffer_tag_count++;
8963 /*
8964 * Always set the QUE_BUFTAG_BIT to distiguish between
8965 * a tag assigned by HBQ.
8966 */
8967 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8968 spin_unlock_irq(&phba->hbalock);
8969 return phba->buffer_tag_count;
8970}
8971
James Smarte59058c2008-08-24 21:49:00 -04008972/**
James Smart3621a712009-04-06 18:47:14 -04008973 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008974 * @phba: Pointer to HBA context object.
8975 * @pring: Pointer to driver SLI ring object.
8976 * @tag: Buffer tag.
8977 *
8978 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8979 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8980 * iocb is posted to the response ring with the tag of the buffer.
8981 * This function searches the pring->postbufq list using the tag
8982 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8983 * iocb. If the buffer is found then lpfc_dmabuf object of the
8984 * buffer is returned to the caller else NULL is returned.
8985 * This function is called with no lock held.
8986 **/
James Smart76bb24e2007-10-27 13:38:00 -04008987struct lpfc_dmabuf *
8988lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8989 uint32_t tag)
8990{
8991 struct lpfc_dmabuf *mp, *next_mp;
8992 struct list_head *slp = &pring->postbufq;
8993
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008994 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008995 spin_lock_irq(&phba->hbalock);
8996 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8997 if (mp->buffer_tag == tag) {
8998 list_del_init(&mp->list);
8999 pring->postbufq_cnt--;
9000 spin_unlock_irq(&phba->hbalock);
9001 return mp;
9002 }
9003 }
9004
9005 spin_unlock_irq(&phba->hbalock);
9006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009007 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009008 "ring %d Data x%lx x%p x%p x%x\n",
9009 pring->ringno, (unsigned long) tag,
9010 slp->next, slp->prev, pring->postbufq_cnt);
9011
9012 return NULL;
9013}
dea31012005-04-17 16:05:31 -05009014
James Smarte59058c2008-08-24 21:49:00 -04009015/**
James Smart3621a712009-04-06 18:47:14 -04009016 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009017 * @phba: Pointer to HBA context object.
9018 * @pring: Pointer to driver SLI ring object.
9019 * @phys: DMA address of the buffer.
9020 *
9021 * This function searches the buffer list using the dma_address
9022 * of unsolicited event to find the driver's lpfc_dmabuf object
9023 * corresponding to the dma_address. The function returns the
9024 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9025 * This function is called by the ct and els unsolicited event
9026 * handlers to get the buffer associated with the unsolicited
9027 * event.
9028 *
9029 * This function is called with no lock held.
9030 **/
dea31012005-04-17 16:05:31 -05009031struct lpfc_dmabuf *
9032lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9033 dma_addr_t phys)
9034{
9035 struct lpfc_dmabuf *mp, *next_mp;
9036 struct list_head *slp = &pring->postbufq;
9037
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009038 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009039 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009040 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9041 if (mp->phys == phys) {
9042 list_del_init(&mp->list);
9043 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009044 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009045 return mp;
9046 }
9047 }
9048
James Smart2e0fef82007-06-17 19:56:36 -05009049 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009051 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009052 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009053 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009054 slp->next, slp->prev, pring->postbufq_cnt);
9055 return NULL;
9056}
9057
James Smarte59058c2008-08-24 21:49:00 -04009058/**
James Smart3621a712009-04-06 18:47:14 -04009059 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009060 * @phba: Pointer to HBA context object.
9061 * @cmdiocb: Pointer to driver command iocb object.
9062 * @rspiocb: Pointer to driver response iocb object.
9063 *
9064 * This function is the completion handler for the abort iocbs for
9065 * ELS commands. This function is called from the ELS ring event
9066 * handler with no lock held. This function frees memory resources
9067 * associated with the abort iocb.
9068 **/
dea31012005-04-17 16:05:31 -05009069static void
James Smart2e0fef82007-06-17 19:56:36 -05009070lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9071 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009072{
James Smart2e0fef82007-06-17 19:56:36 -05009073 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009074 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009075 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009076
9077 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009078
9079 /*
9080 * Assume that the port already completed and returned, or
9081 * will return the iocb. Just Log the message.
9082 */
James Smart2680eea2007-04-25 09:52:55 -04009083 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9084 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9085
James Smart2e0fef82007-06-17 19:56:36 -05009086 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009087 if (phba->sli_rev < LPFC_SLI_REV4) {
9088 if (abort_iotag != 0 &&
9089 abort_iotag <= phba->sli.last_iotag)
9090 abort_iocb =
9091 phba->sli.iocbq_lookup[abort_iotag];
9092 } else
9093 /* For sli4 the abort_tag is the XRI,
9094 * so the abort routine puts the iotag of the iocb
9095 * being aborted in the context field of the abort
9096 * IOCB.
9097 */
9098 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009099
James Smart2a9bf3d2010-06-07 15:24:45 -04009100 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9101 "0327 Cannot abort els iocb %p "
9102 "with tag %x context %x, abort status %x, "
9103 "abort code %x\n",
9104 abort_iocb, abort_iotag, abort_context,
9105 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009106
James Smartff78d8f2011-12-13 13:21:35 -05009107 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009108 }
James Bottomley604a3e32005-10-29 10:28:33 -05009109 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009110 return;
9111}
9112
James Smarte59058c2008-08-24 21:49:00 -04009113/**
James Smart3621a712009-04-06 18:47:14 -04009114 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009115 * @phba: Pointer to HBA context object.
9116 * @cmdiocb: Pointer to driver command iocb object.
9117 * @rspiocb: Pointer to driver response iocb object.
9118 *
9119 * The function is called from SLI ring event handler with no
9120 * lock held. This function is the completion handler for ELS commands
9121 * which are aborted. The function frees memory resources used for
9122 * the aborted ELS commands.
9123 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009124static void
9125lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9126 struct lpfc_iocbq *rspiocb)
9127{
9128 IOCB_t *irsp = &rspiocb->iocb;
9129
9130 /* ELS cmd tag <ulpIoTag> completes */
9131 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009132 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009133 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009134 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009135 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009136 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9137 lpfc_ct_free_iocb(phba, cmdiocb);
9138 else
9139 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009140 return;
9141}
9142
James Smarte59058c2008-08-24 21:49:00 -04009143/**
James Smart5af5eee2010-10-22 11:06:38 -04009144 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009145 * @phba: Pointer to HBA context object.
9146 * @pring: Pointer to driver SLI ring object.
9147 * @cmdiocb: Pointer to driver command iocb object.
9148 *
James Smart5af5eee2010-10-22 11:06:38 -04009149 * This function issues an abort iocb for the provided command iocb down to
9150 * the port. Other than the case the outstanding command iocb is an abort
9151 * request, this function issues abort out unconditionally. This function is
9152 * called with hbalock held. The function returns 0 when it fails due to
9153 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009154 **/
James Smart5af5eee2010-10-22 11:06:38 -04009155static int
9156lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009157 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009158{
James Smart2e0fef82007-06-17 19:56:36 -05009159 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009160 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009161 IOCB_t *icmd = NULL;
9162 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009163 int retval;
James Smart07951072007-04-25 09:51:38 -04009164
James Smart92d7f7b2007-06-17 19:56:38 -05009165 /*
9166 * There are certain command types we don't want to abort. And we
9167 * don't want to abort commands that are already in the process of
9168 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009169 */
9170 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009171 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009172 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9173 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009174 return 0;
9175
dea31012005-04-17 16:05:31 -05009176 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009177 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009178 if (abtsiocbp == NULL)
9179 return 0;
dea31012005-04-17 16:05:31 -05009180
James Smart07951072007-04-25 09:51:38 -04009181 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009182 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009183 */
9184 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9185
dea31012005-04-17 16:05:31 -05009186 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009187 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9188 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009189 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009190 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009191 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9192 }
James Smartda0436e2009-05-22 14:51:39 -04009193 else
9194 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009195 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009196 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009197
James Smart5ffc2662009-11-18 15:39:44 -05009198 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9199 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009200 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9201 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009202
James Smart2e0fef82007-06-17 19:56:36 -05009203 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009204 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9205 else
9206 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9207
9208 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009209
James Smarte8b62012007-08-02 11:10:09 -04009210 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9211 "0339 Abort xri x%x, original iotag x%x, "
9212 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009213 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009214 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009215 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04009216 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04009217
James Smartd7c255b2008-08-24 21:50:00 -04009218 if (retval)
9219 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009220
9221 /*
9222 * Caller to this routine should check for IOCB_ERROR
9223 * and handle it properly. This routine no longer removes
9224 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9225 */
9226 return retval;
9227}
9228
9229/**
9230 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9231 * @phba: Pointer to HBA context object.
9232 * @pring: Pointer to driver SLI ring object.
9233 * @cmdiocb: Pointer to driver command iocb object.
9234 *
9235 * This function issues an abort iocb for the provided command iocb. In case
9236 * of unloading, the abort iocb will not be issued to commands on the ELS
9237 * ring. Instead, the callback function shall be changed to those commands
9238 * so that nothing happens when them finishes. This function is called with
9239 * hbalock held. The function returns 0 when the command iocb is an abort
9240 * request.
9241 **/
9242int
9243lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9244 struct lpfc_iocbq *cmdiocb)
9245{
9246 struct lpfc_vport *vport = cmdiocb->vport;
9247 int retval = IOCB_ERROR;
9248 IOCB_t *icmd = NULL;
9249
9250 /*
9251 * There are certain command types we don't want to abort. And we
9252 * don't want to abort commands that are already in the process of
9253 * being aborted.
9254 */
9255 icmd = &cmdiocb->iocb;
9256 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9257 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9258 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9259 return 0;
9260
9261 /*
9262 * If we're unloading, don't abort iocb on the ELS ring, but change
9263 * the callback so that nothing happens when it finishes.
9264 */
9265 if ((vport->load_flag & FC_UNLOADING) &&
9266 (pring->ringno == LPFC_ELS_RING)) {
9267 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9268 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9269 else
9270 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9271 goto abort_iotag_exit;
9272 }
9273
9274 /* Now, we try to issue the abort to the cmdiocb out */
9275 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9276
James Smart07951072007-04-25 09:51:38 -04009277abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009278 /*
9279 * Caller to this routine should check for IOCB_ERROR
9280 * and handle it properly. This routine no longer removes
9281 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009282 */
James Smart2e0fef82007-06-17 19:56:36 -05009283 return retval;
dea31012005-04-17 16:05:31 -05009284}
9285
James Smarte59058c2008-08-24 21:49:00 -04009286/**
James Smart5af5eee2010-10-22 11:06:38 -04009287 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9288 * @phba: Pointer to HBA context object.
9289 * @pring: Pointer to driver SLI ring object.
9290 *
9291 * This function aborts all iocbs in the given ring and frees all the iocb
9292 * objects in txq. This function issues abort iocbs unconditionally for all
9293 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9294 * to complete before the return of this function. The caller is not required
9295 * to hold any locks.
9296 **/
9297static void
9298lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9299{
9300 LIST_HEAD(completions);
9301 struct lpfc_iocbq *iocb, *next_iocb;
9302
9303 if (pring->ringno == LPFC_ELS_RING)
9304 lpfc_fabric_abort_hba(phba);
9305
9306 spin_lock_irq(&phba->hbalock);
9307
9308 /* Take off all the iocbs on txq for cancelling */
9309 list_splice_init(&pring->txq, &completions);
9310 pring->txq_cnt = 0;
9311
9312 /* Next issue ABTS for everything on the txcmplq */
9313 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9314 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9315
9316 spin_unlock_irq(&phba->hbalock);
9317
9318 /* Cancel all the IOCBs from the completions list */
9319 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9320 IOERR_SLI_ABORTED);
9321}
9322
9323/**
9324 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9325 * @phba: pointer to lpfc HBA data structure.
9326 *
9327 * This routine will abort all pending and outstanding iocbs to an HBA.
9328 **/
9329void
9330lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9331{
9332 struct lpfc_sli *psli = &phba->sli;
9333 struct lpfc_sli_ring *pring;
9334 int i;
9335
9336 for (i = 0; i < psli->num_rings; i++) {
9337 pring = &psli->ring[i];
9338 lpfc_sli_iocb_ring_abort(phba, pring);
9339 }
9340}
9341
9342/**
James Smart3621a712009-04-06 18:47:14 -04009343 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009344 * @iocbq: Pointer to driver iocb object.
9345 * @vport: Pointer to driver virtual port object.
9346 * @tgt_id: SCSI ID of the target.
9347 * @lun_id: LUN ID of the scsi device.
9348 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9349 *
James Smart3621a712009-04-06 18:47:14 -04009350 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009351 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9352 * 0 if the filtering criteria is met for the given iocb and will return
9353 * 1 if the filtering criteria is not met.
9354 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9355 * given iocb is for the SCSI device specified by vport, tgt_id and
9356 * lun_id parameter.
9357 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9358 * given iocb is for the SCSI target specified by vport and tgt_id
9359 * parameters.
9360 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9361 * given iocb is for the SCSI host associated with the given vport.
9362 * This function is called with no locks held.
9363 **/
dea31012005-04-17 16:05:31 -05009364static int
James Smart51ef4c22007-08-02 11:10:31 -04009365lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9366 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009367 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009368{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009369 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009370 int rc = 1;
9371
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009372 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9373 return rc;
9374
James Smart51ef4c22007-08-02 11:10:31 -04009375 if (iocbq->vport != vport)
9376 return rc;
9377
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009378 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009379
James Smart495a7142008-06-14 22:52:59 -04009380 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009381 return rc;
9382
9383 switch (ctx_cmd) {
9384 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009385 if ((lpfc_cmd->rdata->pnode) &&
9386 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9387 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009388 rc = 0;
9389 break;
9390 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009391 if ((lpfc_cmd->rdata->pnode) &&
9392 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009393 rc = 0;
9394 break;
dea31012005-04-17 16:05:31 -05009395 case LPFC_CTX_HOST:
9396 rc = 0;
9397 break;
9398 default:
9399 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009400 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009401 break;
9402 }
9403
9404 return rc;
9405}
9406
James Smarte59058c2008-08-24 21:49:00 -04009407/**
James Smart3621a712009-04-06 18:47:14 -04009408 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009409 * @vport: Pointer to virtual port.
9410 * @tgt_id: SCSI ID of the target.
9411 * @lun_id: LUN ID of the scsi device.
9412 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9413 *
9414 * This function returns number of FCP commands pending for the vport.
9415 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9416 * commands pending on the vport associated with SCSI device specified
9417 * by tgt_id and lun_id parameters.
9418 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9419 * commands pending on the vport associated with SCSI target specified
9420 * by tgt_id parameter.
9421 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9422 * commands pending on the vport.
9423 * This function returns the number of iocbs which satisfy the filter.
9424 * This function is called without any lock held.
9425 **/
dea31012005-04-17 16:05:31 -05009426int
James Smart51ef4c22007-08-02 11:10:31 -04009427lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9428 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009429{
James Smart51ef4c22007-08-02 11:10:31 -04009430 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009431 struct lpfc_iocbq *iocbq;
9432 int sum, i;
dea31012005-04-17 16:05:31 -05009433
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009434 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9435 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009436
James Smart51ef4c22007-08-02 11:10:31 -04009437 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9438 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009439 sum++;
dea31012005-04-17 16:05:31 -05009440 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009441
dea31012005-04-17 16:05:31 -05009442 return sum;
9443}
9444
James Smarte59058c2008-08-24 21:49:00 -04009445/**
James Smart3621a712009-04-06 18:47:14 -04009446 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009447 * @phba: Pointer to HBA context object
9448 * @cmdiocb: Pointer to command iocb object.
9449 * @rspiocb: Pointer to response iocb object.
9450 *
9451 * This function is called when an aborted FCP iocb completes. This
9452 * function is called by the ring event handler with no lock held.
9453 * This function frees the iocb.
9454 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009455void
James Smart2e0fef82007-06-17 19:56:36 -05009456lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9457 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009458{
James Smartcb69f7d2011-12-13 13:21:57 -05009459 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9460 "3096 ABORT_XRI_CN completing on xri x%x "
9461 "original iotag x%x, abort cmd iotag x%x "
9462 "status 0x%x, reason 0x%x\n",
9463 cmdiocb->iocb.un.acxri.abortContextTag,
9464 cmdiocb->iocb.un.acxri.abortIoTag,
9465 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9466 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009467 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009468 return;
9469}
9470
James Smarte59058c2008-08-24 21:49:00 -04009471/**
James Smart3621a712009-04-06 18:47:14 -04009472 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009473 * @vport: Pointer to virtual port.
9474 * @pring: Pointer to driver SLI ring object.
9475 * @tgt_id: SCSI ID of the target.
9476 * @lun_id: LUN ID of the scsi device.
9477 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9478 *
9479 * This function sends an abort command for every SCSI command
9480 * associated with the given virtual port pending on the ring
9481 * filtered by lpfc_sli_validate_fcp_iocb function.
9482 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9483 * FCP iocbs associated with lun specified by tgt_id and lun_id
9484 * parameters
9485 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9486 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9487 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9488 * FCP iocbs associated with virtual port.
9489 * This function returns number of iocbs it failed to abort.
9490 * This function is called with no locks held.
9491 **/
dea31012005-04-17 16:05:31 -05009492int
James Smart51ef4c22007-08-02 11:10:31 -04009493lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9494 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009495{
James Smart51ef4c22007-08-02 11:10:31 -04009496 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009497 struct lpfc_iocbq *iocbq;
9498 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009499 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009500 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009501 int i;
dea31012005-04-17 16:05:31 -05009502
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009503 for (i = 1; i <= phba->sli.last_iotag; i++) {
9504 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009505
James Smart51ef4c22007-08-02 11:10:31 -04009506 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009507 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009508 continue;
9509
9510 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009511 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009512 if (abtsiocb == NULL) {
9513 errcnt++;
9514 continue;
9515 }
dea31012005-04-17 16:05:31 -05009516
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009517 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009518 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9519 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009520 if (phba->sli_rev == LPFC_SLI_REV4)
9521 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9522 else
9523 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009524 abtsiocb->iocb.ulpLe = 1;
9525 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009526 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009527
James Smart5ffc2662009-11-18 15:39:44 -05009528 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9529 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009530 if (iocbq->iocb_flag & LPFC_IO_FCP)
9531 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009532
James Smart2e0fef82007-06-17 19:56:36 -05009533 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009534 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9535 else
9536 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9537
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009538 /* Setup callback routine and issue the command. */
9539 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009540 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9541 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009542 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009543 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009544 errcnt++;
9545 continue;
9546 }
9547 }
9548
9549 return errcnt;
9550}
9551
James Smarte59058c2008-08-24 21:49:00 -04009552/**
James Smart3621a712009-04-06 18:47:14 -04009553 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009554 * @phba: Pointer to HBA context object.
9555 * @cmdiocbq: Pointer to command iocb.
9556 * @rspiocbq: Pointer to response iocb.
9557 *
9558 * This function is the completion handler for iocbs issued using
9559 * lpfc_sli_issue_iocb_wait function. This function is called by the
9560 * ring event handler function without any lock held. This function
9561 * can be called from both worker thread context and interrupt
9562 * context. This function also can be called from other thread which
9563 * cleans up the SLI layer objects.
9564 * This function copy the contents of the response iocb to the
9565 * response iocb memory object provided by the caller of
9566 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9567 * sleeps for the iocb completion.
9568 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009569static void
9570lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9571 struct lpfc_iocbq *cmdiocbq,
9572 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009573{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009574 wait_queue_head_t *pdone_q;
9575 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009576 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009577
James Smart2e0fef82007-06-17 19:56:36 -05009578 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009579 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9580 if (cmdiocbq->context2 && rspiocbq)
9581 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9582 &rspiocbq->iocb, sizeof(IOCB_t));
9583
James Smart0f65ff62010-02-26 14:14:23 -05009584 /* Set the exchange busy flag for task management commands */
9585 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9586 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9587 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9588 cur_iocbq);
9589 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9590 }
9591
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009592 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009593 if (pdone_q)
9594 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009595 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009596 return;
9597}
9598
James Smarte59058c2008-08-24 21:49:00 -04009599/**
James Smartd11e31d2009-06-10 17:23:06 -04009600 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9601 * @phba: Pointer to HBA context object..
9602 * @piocbq: Pointer to command iocb.
9603 * @flag: Flag to test.
9604 *
9605 * This routine grabs the hbalock and then test the iocb_flag to
9606 * see if the passed in flag is set.
9607 * Returns:
9608 * 1 if flag is set.
9609 * 0 if flag is not set.
9610 **/
9611static int
9612lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9613 struct lpfc_iocbq *piocbq, uint32_t flag)
9614{
9615 unsigned long iflags;
9616 int ret;
9617
9618 spin_lock_irqsave(&phba->hbalock, iflags);
9619 ret = piocbq->iocb_flag & flag;
9620 spin_unlock_irqrestore(&phba->hbalock, iflags);
9621 return ret;
9622
9623}
9624
9625/**
James Smart3621a712009-04-06 18:47:14 -04009626 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009627 * @phba: Pointer to HBA context object..
9628 * @pring: Pointer to sli ring.
9629 * @piocb: Pointer to command iocb.
9630 * @prspiocbq: Pointer to response iocb.
9631 * @timeout: Timeout in number of seconds.
9632 *
9633 * This function issues the iocb to firmware and waits for the
9634 * iocb to complete. If the iocb command is not
9635 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9636 * Caller should not free the iocb resources if this function
9637 * returns IOCB_TIMEDOUT.
9638 * The function waits for the iocb completion using an
9639 * non-interruptible wait.
9640 * This function will sleep while waiting for iocb completion.
9641 * So, this function should not be called from any context which
9642 * does not allow sleeping. Due to the same reason, this function
9643 * cannot be called with interrupt disabled.
9644 * This function assumes that the iocb completions occur while
9645 * this function sleep. So, this function cannot be called from
9646 * the thread which process iocb completion for this ring.
9647 * This function clears the iocb_flag of the iocb object before
9648 * issuing the iocb and the iocb completion handler sets this
9649 * flag and wakes this thread when the iocb completes.
9650 * The contents of the response iocb will be copied to prspiocbq
9651 * by the completion handler when the command completes.
9652 * This function returns IOCB_SUCCESS when success.
9653 * This function is called with no lock held.
9654 **/
dea31012005-04-17 16:05:31 -05009655int
James Smart2e0fef82007-06-17 19:56:36 -05009656lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009657 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009658 struct lpfc_iocbq *piocb,
9659 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009660 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009661{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009662 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009663 long timeleft, timeout_req = 0;
9664 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009665 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009666 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009667 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009668 * If the caller has provided a response iocbq buffer, then context2
9669 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009670 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009671 if (prspiocbq) {
9672 if (piocb->context2)
9673 return IOCB_ERROR;
9674 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009675 }
9676
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009677 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9678 piocb->context_un.wait_queue = &done_q;
9679 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009680
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009681 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009682 if (lpfc_readl(phba->HCregaddr, &creg_val))
9683 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009684 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9685 writel(creg_val, phba->HCregaddr);
9686 readl(phba->HCregaddr); /* flush */
9687 }
9688
James Smart2a9bf3d2010-06-07 15:24:45 -04009689 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9690 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009691 if (retval == IOCB_SUCCESS) {
9692 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009693 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009694 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009695 timeout_req);
dea31012005-04-17 16:05:31 -05009696
James Smart7054a602007-04-25 09:52:34 -04009697 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9698 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009699 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009700 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009702 "0338 IOCB wait timeout error - no "
9703 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009704 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009705 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009706 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009707 "0330 IOCB wake NOT set, "
9708 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009709 timeout, (timeleft / jiffies));
9710 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009711 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009712 } else if (retval == IOCB_BUSY) {
9713 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9714 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9715 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9716 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009717 } else {
9718 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009719 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009720 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009721 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009722 }
9723
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009724 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009725 if (lpfc_readl(phba->HCregaddr, &creg_val))
9726 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009727 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9728 writel(creg_val, phba->HCregaddr);
9729 readl(phba->HCregaddr); /* flush */
9730 }
9731
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009732 if (prspiocbq)
9733 piocb->context2 = NULL;
9734
9735 piocb->context_un.wait_queue = NULL;
9736 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009737 return retval;
9738}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009739
James Smarte59058c2008-08-24 21:49:00 -04009740/**
James Smart3621a712009-04-06 18:47:14 -04009741 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009742 * @phba: Pointer to HBA context object.
9743 * @pmboxq: Pointer to driver mailbox object.
9744 * @timeout: Timeout in number of seconds.
9745 *
9746 * This function issues the mailbox to firmware and waits for the
9747 * mailbox command to complete. If the mailbox command is not
9748 * completed within timeout seconds, it returns MBX_TIMEOUT.
9749 * The function waits for the mailbox completion using an
9750 * interruptible wait. If the thread is woken up due to a
9751 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9752 * should not free the mailbox resources, if this function returns
9753 * MBX_TIMEOUT.
9754 * This function will sleep while waiting for mailbox completion.
9755 * So, this function should not be called from any context which
9756 * does not allow sleeping. Due to the same reason, this function
9757 * cannot be called with interrupt disabled.
9758 * This function assumes that the mailbox completion occurs while
9759 * this function sleep. So, this function cannot be called from
9760 * the worker thread which processes mailbox completion.
9761 * This function is called in the context of HBA management
9762 * applications.
9763 * This function returns MBX_SUCCESS when successful.
9764 * This function is called with no lock held.
9765 **/
dea31012005-04-17 16:05:31 -05009766int
James Smart2e0fef82007-06-17 19:56:36 -05009767lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009768 uint32_t timeout)
9769{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009770 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009771 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009772 unsigned long flag;
dea31012005-04-17 16:05:31 -05009773
9774 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009775 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009776 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009777
James Smart495a7142008-06-14 22:52:59 -04009778 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009779 /* setup wake call as IOCB callback */
9780 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9781 /* setup context field to pass wait_queue pointer to wake function */
9782 pmboxq->context1 = &done_q;
9783
dea31012005-04-17 16:05:31 -05009784 /* now issue the command */
9785 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009786 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009787 wait_event_interruptible_timeout(done_q,
9788 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9789 timeout * HZ);
9790
James Smart858c9f62007-06-17 19:56:39 -05009791 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009792 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009793 /*
9794 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9795 * else do not free the resources.
9796 */
James Smartd7c47992010-06-08 18:31:54 -04009797 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009798 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009799 lpfc_sli4_swap_str(phba, pmboxq);
9800 } else {
James Smart7054a602007-04-25 09:52:34 -04009801 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009802 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9803 }
9804 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009805 }
9806
dea31012005-04-17 16:05:31 -05009807 return retval;
9808}
9809
James Smarte59058c2008-08-24 21:49:00 -04009810/**
James Smart3772a992009-05-22 14:50:54 -04009811 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009812 * @phba: Pointer to HBA context.
9813 *
James Smart3772a992009-05-22 14:50:54 -04009814 * This function is called to shutdown the driver's mailbox sub-system.
9815 * It first marks the mailbox sub-system is in a block state to prevent
9816 * the asynchronous mailbox command from issued off the pending mailbox
9817 * command queue. If the mailbox command sub-system shutdown is due to
9818 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9819 * the mailbox sub-system flush routine to forcefully bring down the
9820 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9821 * as with offline or HBA function reset), this routine will wait for the
9822 * outstanding mailbox command to complete before invoking the mailbox
9823 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009824 **/
James Smart3772a992009-05-22 14:50:54 -04009825void
9826lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009827{
James Smart3772a992009-05-22 14:50:54 -04009828 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -04009829 unsigned long timeout;
9830
James Smarta183a152011-10-10 21:32:43 -04009831 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smart3772a992009-05-22 14:50:54 -04009832 spin_lock_irq(&phba->hbalock);
9833 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9834 spin_unlock_irq(&phba->hbalock);
9835
9836 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9837 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009838 /* Determine how long we might wait for the active mailbox
9839 * command to be gracefully completed by firmware.
9840 */
James Smarta183a152011-10-10 21:32:43 -04009841 if (phba->sli.mbox_active)
9842 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9843 phba->sli.mbox_active) *
9844 1000) + jiffies;
9845 spin_unlock_irq(&phba->hbalock);
9846
James Smart3772a992009-05-22 14:50:54 -04009847 while (phba->sli.mbox_active) {
9848 /* Check active mailbox complete status every 2ms */
9849 msleep(2);
9850 if (time_after(jiffies, timeout))
9851 /* Timeout, let the mailbox flush routine to
9852 * forcefully release active mailbox command
9853 */
9854 break;
9855 }
9856 }
9857 lpfc_sli_mbox_sys_flush(phba);
9858}
9859
9860/**
9861 * lpfc_sli_eratt_read - read sli-3 error attention events
9862 * @phba: Pointer to HBA context.
9863 *
9864 * This function is called to read the SLI3 device error attention registers
9865 * for possible error attention events. The caller must hold the hostlock
9866 * with spin_lock_irq().
9867 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009868 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009869 * Register and returns 0 otherwise.
9870 **/
9871static int
9872lpfc_sli_eratt_read(struct lpfc_hba *phba)
9873{
James Smarted957682007-06-17 19:56:37 -05009874 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009875
James Smart3772a992009-05-22 14:50:54 -04009876 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009877 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9878 goto unplug_err;
9879
James Smart3772a992009-05-22 14:50:54 -04009880 if (ha_copy & HA_ERATT) {
9881 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009882 if (lpfc_sli_read_hs(phba))
9883 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009884
James Smart3772a992009-05-22 14:50:54 -04009885 /* Check if there is a deferred error condition is active */
9886 if ((HS_FFER1 & phba->work_hs) &&
9887 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009888 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009889 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009890 /* Clear all interrupt enable conditions */
9891 writel(0, phba->HCregaddr);
9892 readl(phba->HCregaddr);
9893 }
9894
9895 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009896 phba->work_ha |= HA_ERATT;
9897 /* Indicate polling handles this ERATT */
9898 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009899 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009900 }
James Smart3772a992009-05-22 14:50:54 -04009901 return 0;
James Smart9940b972011-03-11 16:06:12 -05009902
9903unplug_err:
9904 /* Set the driver HS work bitmap */
9905 phba->work_hs |= UNPLUG_ERR;
9906 /* Set the driver HA work bitmap */
9907 phba->work_ha |= HA_ERATT;
9908 /* Indicate polling handles this ERATT */
9909 phba->hba_flag |= HBA_ERATT_HANDLED;
9910 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009911}
9912
James Smarte59058c2008-08-24 21:49:00 -04009913/**
James Smartda0436e2009-05-22 14:51:39 -04009914 * lpfc_sli4_eratt_read - read sli-4 error attention events
9915 * @phba: Pointer to HBA context.
9916 *
9917 * This function is called to read the SLI4 device error attention registers
9918 * for possible error attention events. The caller must hold the hostlock
9919 * with spin_lock_irq().
9920 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009921 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009922 * Register and returns 0 otherwise.
9923 **/
9924static int
9925lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9926{
9927 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009928 uint32_t if_type, portsmphr;
9929 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009930
James Smart2fcee4b2010-12-15 17:57:46 -05009931 /*
9932 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009933 * registers for error attention. This can be changed later.
9934 */
James Smart2fcee4b2010-12-15 17:57:46 -05009935 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9936 switch (if_type) {
9937 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009938 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9939 &uerr_sta_lo) ||
9940 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9941 &uerr_sta_hi)) {
9942 phba->work_hs |= UNPLUG_ERR;
9943 phba->work_ha |= HA_ERATT;
9944 phba->hba_flag |= HBA_ERATT_HANDLED;
9945 return 1;
9946 }
James Smart2fcee4b2010-12-15 17:57:46 -05009947 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9948 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9949 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9950 "1423 HBA Unrecoverable error: "
9951 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9952 "ue_mask_lo_reg=0x%x, "
9953 "ue_mask_hi_reg=0x%x\n",
9954 uerr_sta_lo, uerr_sta_hi,
9955 phba->sli4_hba.ue_mask_lo,
9956 phba->sli4_hba.ue_mask_hi);
9957 phba->work_status[0] = uerr_sta_lo;
9958 phba->work_status[1] = uerr_sta_hi;
9959 phba->work_ha |= HA_ERATT;
9960 phba->hba_flag |= HBA_ERATT_HANDLED;
9961 return 1;
9962 }
9963 break;
9964 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009965 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9966 &portstat_reg.word0) ||
9967 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9968 &portsmphr)){
9969 phba->work_hs |= UNPLUG_ERR;
9970 phba->work_ha |= HA_ERATT;
9971 phba->hba_flag |= HBA_ERATT_HANDLED;
9972 return 1;
9973 }
James Smart2fcee4b2010-12-15 17:57:46 -05009974 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9975 phba->work_status[0] =
9976 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9977 phba->work_status[1] =
9978 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -05009980 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -05009981 "port status reg 0x%x, "
9982 "port smphr reg 0x%x, "
9983 "error 1=0x%x, error 2=0x%x\n",
9984 portstat_reg.word0,
9985 portsmphr,
9986 phba->work_status[0],
9987 phba->work_status[1]);
9988 phba->work_ha |= HA_ERATT;
9989 phba->hba_flag |= HBA_ERATT_HANDLED;
9990 return 1;
9991 }
9992 break;
9993 case LPFC_SLI_INTF_IF_TYPE_1:
9994 default:
James Smarta747c9c2009-11-18 15:41:10 -05009995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009996 "2886 HBA Error Attention on unsupported "
9997 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009998 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009999 }
James Smart2fcee4b2010-12-15 17:57:46 -050010000
James Smartda0436e2009-05-22 14:51:39 -040010001 return 0;
10002}
10003
10004/**
James Smart3621a712009-04-06 18:47:14 -040010005 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010006 * @phba: Pointer to HBA context.
10007 *
James Smart3772a992009-05-22 14:50:54 -040010008 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010009 * error attention register bit for error attention events.
10010 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010011 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010012 * Register and returns 0 otherwise.
10013 **/
10014int
10015lpfc_sli_check_eratt(struct lpfc_hba *phba)
10016{
10017 uint32_t ha_copy;
10018
10019 /* If somebody is waiting to handle an eratt, don't process it
10020 * here. The brdkill function will do this.
10021 */
10022 if (phba->link_flag & LS_IGNORE_ERATT)
10023 return 0;
10024
10025 /* Check if interrupt handler handles this ERATT */
10026 spin_lock_irq(&phba->hbalock);
10027 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10028 /* Interrupt handler has handled ERATT */
10029 spin_unlock_irq(&phba->hbalock);
10030 return 0;
10031 }
10032
James Smarta257bf92009-04-06 18:48:10 -040010033 /*
10034 * If there is deferred error attention, do not check for error
10035 * attention
10036 */
10037 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10038 spin_unlock_irq(&phba->hbalock);
10039 return 0;
10040 }
10041
James Smart3772a992009-05-22 14:50:54 -040010042 /* If PCI channel is offline, don't process it */
10043 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010044 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010045 return 0;
10046 }
10047
10048 switch (phba->sli_rev) {
10049 case LPFC_SLI_REV2:
10050 case LPFC_SLI_REV3:
10051 /* Read chip Host Attention (HA) register */
10052 ha_copy = lpfc_sli_eratt_read(phba);
10053 break;
James Smartda0436e2009-05-22 14:51:39 -040010054 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010055 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010056 ha_copy = lpfc_sli4_eratt_read(phba);
10057 break;
James Smart3772a992009-05-22 14:50:54 -040010058 default:
10059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10060 "0299 Invalid SLI revision (%d)\n",
10061 phba->sli_rev);
10062 ha_copy = 0;
10063 break;
James Smart93996272008-08-24 21:50:30 -040010064 }
10065 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010066
10067 return ha_copy;
10068}
10069
10070/**
10071 * lpfc_intr_state_check - Check device state for interrupt handling
10072 * @phba: Pointer to HBA context.
10073 *
10074 * This inline routine checks whether a device or its PCI slot is in a state
10075 * that the interrupt should be handled.
10076 *
10077 * This function returns 0 if the device or the PCI slot is in a state that
10078 * interrupt should be handled, otherwise -EIO.
10079 */
10080static inline int
10081lpfc_intr_state_check(struct lpfc_hba *phba)
10082{
10083 /* If the pci channel is offline, ignore all the interrupts */
10084 if (unlikely(pci_channel_offline(phba->pcidev)))
10085 return -EIO;
10086
10087 /* Update device level interrupt statistics */
10088 phba->sli.slistat.sli_intr++;
10089
10090 /* Ignore all interrupts during initialization. */
10091 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10092 return -EIO;
10093
James Smart93996272008-08-24 21:50:30 -040010094 return 0;
10095}
10096
10097/**
James Smart3772a992009-05-22 14:50:54 -040010098 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010099 * @irq: Interrupt number.
10100 * @dev_id: The device context pointer.
10101 *
James Smart93996272008-08-24 21:50:30 -040010102 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010103 * service routine when device with SLI-3 interface spec is enabled with
10104 * MSI-X multi-message interrupt mode and there are slow-path events in
10105 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10106 * interrupt mode, this function is called as part of the device-level
10107 * interrupt handler. When the PCI slot is in error recovery or the HBA
10108 * is undergoing initialization, the interrupt handler will not process
10109 * the interrupt. The link attention and ELS ring attention events are
10110 * handled by the worker thread. The interrupt handler signals the worker
10111 * thread and returns for these events. This function is called without
10112 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010113 * structures.
10114 *
10115 * This function returns IRQ_HANDLED when interrupt is handled else it
10116 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010117 **/
dea31012005-04-17 16:05:31 -050010118irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010119lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010120{
James Smart2e0fef82007-06-17 19:56:36 -050010121 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010122 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010123 uint32_t work_ha_copy;
10124 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010125 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010126 uint32_t control;
10127
James Smart92d7f7b2007-06-17 19:56:38 -050010128 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010129 struct lpfc_vport *vport;
10130 struct lpfc_nodelist *ndlp;
10131 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010132 LPFC_MBOXQ_t *pmb;
10133 int rc;
10134
dea31012005-04-17 16:05:31 -050010135 /*
10136 * Get the driver's phba structure from the dev_id and
10137 * assume the HBA is not interrupting.
10138 */
James Smart93996272008-08-24 21:50:30 -040010139 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010140
10141 if (unlikely(!phba))
10142 return IRQ_NONE;
10143
dea31012005-04-17 16:05:31 -050010144 /*
James Smart93996272008-08-24 21:50:30 -040010145 * Stuff needs to be attented to when this function is invoked as an
10146 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010147 */
James Smart93996272008-08-24 21:50:30 -040010148 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010149 /* Check device state for handling interrupt */
10150 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010151 return IRQ_NONE;
10152 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010153 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010154 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10155 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010156 /* If somebody is waiting to handle an eratt don't process it
10157 * here. The brdkill function will do this.
10158 */
10159 if (phba->link_flag & LS_IGNORE_ERATT)
10160 ha_copy &= ~HA_ERATT;
10161 /* Check the need for handling ERATT in interrupt handler */
10162 if (ha_copy & HA_ERATT) {
10163 if (phba->hba_flag & HBA_ERATT_HANDLED)
10164 /* ERATT polling has handled ERATT */
10165 ha_copy &= ~HA_ERATT;
10166 else
10167 /* Indicate interrupt handler handles ERATT */
10168 phba->hba_flag |= HBA_ERATT_HANDLED;
10169 }
James Smarta257bf92009-04-06 18:48:10 -040010170
10171 /*
10172 * If there is deferred error attention, do not check for any
10173 * interrupt.
10174 */
10175 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010176 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010177 return IRQ_NONE;
10178 }
10179
James Smart93996272008-08-24 21:50:30 -040010180 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010181 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10182 goto unplug_error;
10183
James Smarta747c9c2009-11-18 15:41:10 -050010184 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10185 HC_LAINT_ENA | HC_ERINT_ENA),
10186 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010187 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10188 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010189 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010190 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010191 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010192 } else
10193 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010194
dea31012005-04-17 16:05:31 -050010195 work_ha_copy = ha_copy & phba->work_ha_mask;
10196
James Smart93996272008-08-24 21:50:30 -040010197 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010198 if (work_ha_copy & HA_LATT) {
10199 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10200 /*
10201 * Turn off Link Attention interrupts
10202 * until CLEAR_LA done
10203 */
James Smart5b75da22008-12-04 22:39:35 -050010204 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010205 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010206 if (lpfc_readl(phba->HCregaddr, &control))
10207 goto unplug_error;
dea31012005-04-17 16:05:31 -050010208 control &= ~HC_LAINT_ENA;
10209 writel(control, phba->HCregaddr);
10210 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010211 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010212 }
10213 else
10214 work_ha_copy &= ~HA_LATT;
10215 }
10216
James Smart93996272008-08-24 21:50:30 -040010217 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010218 /*
10219 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10220 * the only slow ring.
10221 */
10222 status = (work_ha_copy &
10223 (HA_RXMASK << (4*LPFC_ELS_RING)));
10224 status >>= (4*LPFC_ELS_RING);
10225 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010226 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010227 if (lpfc_readl(phba->HCregaddr, &control))
10228 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010229
10230 lpfc_debugfs_slow_ring_trc(phba,
10231 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10232 control, status,
10233 (uint32_t)phba->sli.slistat.sli_intr);
10234
James Smart858c9f62007-06-17 19:56:39 -050010235 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010236 lpfc_debugfs_slow_ring_trc(phba,
10237 "ISR Disable ring:"
10238 "pwork:x%x hawork:x%x wait:x%x",
10239 phba->work_ha, work_ha_copy,
10240 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010241 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010242
James Smart858c9f62007-06-17 19:56:39 -050010243 control &=
10244 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010245 writel(control, phba->HCregaddr);
10246 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010247 }
James Smarta58cbd52007-08-02 11:09:43 -040010248 else {
10249 lpfc_debugfs_slow_ring_trc(phba,
10250 "ISR slow ring: pwork:"
10251 "x%x hawork:x%x wait:x%x",
10252 phba->work_ha, work_ha_copy,
10253 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010254 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010255 }
James Smart5b75da22008-12-04 22:39:35 -050010256 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010257 }
10258 }
James Smart5b75da22008-12-04 22:39:35 -050010259 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010260 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010261 if (lpfc_sli_read_hs(phba))
10262 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010263 /*
10264 * Check if there is a deferred error condition
10265 * is active
10266 */
10267 if ((HS_FFER1 & phba->work_hs) &&
10268 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010269 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10270 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010271 phba->hba_flag |= DEFER_ERATT;
10272 /* Clear all interrupt enable conditions */
10273 writel(0, phba->HCregaddr);
10274 readl(phba->HCregaddr);
10275 }
10276 }
10277
James Smart93996272008-08-24 21:50:30 -040010278 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010279 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010280 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010281 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010282 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010283
10284 /* First check out the status word */
10285 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10286 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010287 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010288 /*
10289 * Stray Mailbox Interrupt, mbxCommand <cmd>
10290 * mbxStatus <status>
10291 */
James Smart09372822008-01-11 01:52:54 -050010292 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010293 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010294 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010295 "Interrupt mbxCommand x%x "
10296 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010297 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010298 pmbox->mbxCommand,
10299 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010300 /* clear mailbox attention bit */
10301 work_ha_copy &= ~HA_MBATT;
10302 } else {
James Smart97eab632008-04-07 10:16:05 -040010303 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010304 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010305 phba->last_completion_time = jiffies;
10306 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010307 if (pmb->mbox_cmpl) {
10308 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10309 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010310 if (pmb->out_ext_byte_len &&
10311 pmb->context2)
10312 lpfc_sli_pcimem_bcopy(
10313 phba->mbox_ext,
10314 pmb->context2,
10315 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010316 }
James Smart09372822008-01-11 01:52:54 -050010317 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10318 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10319
10320 lpfc_debugfs_disc_trc(vport,
10321 LPFC_DISC_TRC_MBOX_VPORT,
10322 "MBOX dflt rpi: : "
10323 "status:x%x rpi:x%x",
10324 (uint32_t)pmbox->mbxStatus,
10325 pmbox->un.varWords[0], 0);
10326
10327 if (!pmbox->mbxStatus) {
10328 mp = (struct lpfc_dmabuf *)
10329 (pmb->context1);
10330 ndlp = (struct lpfc_nodelist *)
10331 pmb->context2;
10332
10333 /* Reg_LOGIN of dflt RPI was
10334 * successful. new lets get
10335 * rid of the RPI using the
10336 * same mbox buffer.
10337 */
10338 lpfc_unreg_login(phba,
10339 vport->vpi,
10340 pmbox->un.varWords[0],
10341 pmb);
10342 pmb->mbox_cmpl =
10343 lpfc_mbx_cmpl_dflt_rpi;
10344 pmb->context1 = mp;
10345 pmb->context2 = ndlp;
10346 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010347 rc = lpfc_sli_issue_mbox(phba,
10348 pmb,
10349 MBX_NOWAIT);
10350 if (rc != MBX_BUSY)
10351 lpfc_printf_log(phba,
10352 KERN_ERR,
10353 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010354 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010355 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010356 if (rc != MBX_NOT_FINISHED)
10357 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010358 }
10359 }
James Smart5b75da22008-12-04 22:39:35 -050010360 spin_lock_irqsave(
10361 &phba->pport->work_port_lock,
10362 iflag);
James Smart09372822008-01-11 01:52:54 -050010363 phba->pport->work_port_events &=
10364 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010365 spin_unlock_irqrestore(
10366 &phba->pport->work_port_lock,
10367 iflag);
James Smart09372822008-01-11 01:52:54 -050010368 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010369 }
James Smart97eab632008-04-07 10:16:05 -040010370 } else
James Smart5b75da22008-12-04 22:39:35 -050010371 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010372
James Smart92d7f7b2007-06-17 19:56:38 -050010373 if ((work_ha_copy & HA_MBATT) &&
10374 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010375send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010376 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010377 do {
10378 rc = lpfc_sli_issue_mbox(phba, NULL,
10379 MBX_NOWAIT);
10380 } while (rc == MBX_NOT_FINISHED);
10381 if (rc != MBX_SUCCESS)
10382 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10383 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010384 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010385 }
10386
James Smart5b75da22008-12-04 22:39:35 -050010387 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010388 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010389 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010390 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010391 }
James Smart93996272008-08-24 21:50:30 -040010392 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010393unplug_error:
10394 spin_unlock_irqrestore(&phba->hbalock, iflag);
10395 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010396
James Smart3772a992009-05-22 14:50:54 -040010397} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010398
10399/**
James Smart3772a992009-05-22 14:50:54 -040010400 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010401 * @irq: Interrupt number.
10402 * @dev_id: The device context pointer.
10403 *
10404 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010405 * service routine when device with SLI-3 interface spec is enabled with
10406 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10407 * ring event in the HBA. However, when the device is enabled with either
10408 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10409 * device-level interrupt handler. When the PCI slot is in error recovery
10410 * or the HBA is undergoing initialization, the interrupt handler will not
10411 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10412 * the intrrupt context. This function is called without any lock held.
10413 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010414 *
10415 * This function returns IRQ_HANDLED when interrupt is handled else it
10416 * returns IRQ_NONE.
10417 **/
10418irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010419lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010420{
10421 struct lpfc_hba *phba;
10422 uint32_t ha_copy;
10423 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010424 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010425
10426 /* Get the driver's phba structure from the dev_id and
10427 * assume the HBA is not interrupting.
10428 */
10429 phba = (struct lpfc_hba *) dev_id;
10430
10431 if (unlikely(!phba))
10432 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010433
10434 /*
James Smart93996272008-08-24 21:50:30 -040010435 * Stuff needs to be attented to when this function is invoked as an
10436 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010437 */
James Smart93996272008-08-24 21:50:30 -040010438 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010439 /* Check device state for handling interrupt */
10440 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010441 return IRQ_NONE;
10442 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010443 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10444 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010445 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010446 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010447 /*
10448 * If there is deferred error attention, do not check for
10449 * any interrupt.
10450 */
10451 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010452 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010453 return IRQ_NONE;
10454 }
James Smart93996272008-08-24 21:50:30 -040010455 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10456 phba->HAregaddr);
10457 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010458 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010459 } else
10460 ha_copy = phba->ha_copy;
10461
10462 /*
10463 * Process all events on FCP ring. Take the optimized path for FCP IO.
10464 */
10465 ha_copy &= ~(phba->work_ha_mask);
10466
10467 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010468 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010469 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010470 lpfc_sli_handle_fast_ring_event(phba,
10471 &phba->sli.ring[LPFC_FCP_RING],
10472 status);
James Smarta4bc3372006-12-02 13:34:16 -050010473
10474 if (phba->cfg_multi_ring_support == 2) {
10475 /*
James Smart93996272008-08-24 21:50:30 -040010476 * Process all events on extra ring. Take the optimized path
10477 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010478 */
James Smart93996272008-08-24 21:50:30 -040010479 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010480 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010481 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010482 lpfc_sli_handle_fast_ring_event(phba,
10483 &phba->sli.ring[LPFC_EXTRA_RING],
10484 status);
10485 }
10486 }
dea31012005-04-17 16:05:31 -050010487 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010488} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010489
James Smart93996272008-08-24 21:50:30 -040010490/**
James Smart3772a992009-05-22 14:50:54 -040010491 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010492 * @irq: Interrupt number.
10493 * @dev_id: The device context pointer.
10494 *
James Smart3772a992009-05-22 14:50:54 -040010495 * This function is the HBA device-level interrupt handler to device with
10496 * SLI-3 interface spec, called from the PCI layer when either MSI or
10497 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10498 * requires driver attention. This function invokes the slow-path interrupt
10499 * attention handling function and fast-path interrupt attention handling
10500 * function in turn to process the relevant HBA attention events. This
10501 * function is called without any lock held. It gets the hbalock to access
10502 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010503 *
10504 * This function returns IRQ_HANDLED when interrupt is handled, else it
10505 * returns IRQ_NONE.
10506 **/
10507irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010508lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010509{
10510 struct lpfc_hba *phba;
10511 irqreturn_t sp_irq_rc, fp_irq_rc;
10512 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010513 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010514
10515 /*
10516 * Get the driver's phba structure from the dev_id and
10517 * assume the HBA is not interrupting.
10518 */
10519 phba = (struct lpfc_hba *) dev_id;
10520
10521 if (unlikely(!phba))
10522 return IRQ_NONE;
10523
James Smart3772a992009-05-22 14:50:54 -040010524 /* Check device state for handling interrupt */
10525 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010526 return IRQ_NONE;
10527
10528 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010529 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10530 spin_unlock(&phba->hbalock);
10531 return IRQ_HANDLED;
10532 }
10533
James Smart93996272008-08-24 21:50:30 -040010534 if (unlikely(!phba->ha_copy)) {
10535 spin_unlock(&phba->hbalock);
10536 return IRQ_NONE;
10537 } else if (phba->ha_copy & HA_ERATT) {
10538 if (phba->hba_flag & HBA_ERATT_HANDLED)
10539 /* ERATT polling has handled ERATT */
10540 phba->ha_copy &= ~HA_ERATT;
10541 else
10542 /* Indicate interrupt handler handles ERATT */
10543 phba->hba_flag |= HBA_ERATT_HANDLED;
10544 }
10545
James Smarta257bf92009-04-06 18:48:10 -040010546 /*
10547 * If there is deferred error attention, do not check for any interrupt.
10548 */
10549 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010550 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010551 return IRQ_NONE;
10552 }
10553
James Smart93996272008-08-24 21:50:30 -040010554 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010555 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10556 spin_unlock(&phba->hbalock);
10557 return IRQ_HANDLED;
10558 }
James Smarta747c9c2009-11-18 15:41:10 -050010559 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10560 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10561 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010562 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010563 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010564 readl(phba->HAregaddr); /* flush */
10565 spin_unlock(&phba->hbalock);
10566
10567 /*
10568 * Invokes slow-path host attention interrupt handling as appropriate.
10569 */
10570
10571 /* status of events with mailbox and link attention */
10572 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10573
10574 /* status of events with ELS ring */
10575 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10576 status2 >>= (4*LPFC_ELS_RING);
10577
10578 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010579 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010580 else
10581 sp_irq_rc = IRQ_NONE;
10582
10583 /*
10584 * Invoke fast-path host attention interrupt handling as appropriate.
10585 */
10586
10587 /* status of events with FCP ring */
10588 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10589 status1 >>= (4*LPFC_FCP_RING);
10590
10591 /* status of events with extra ring */
10592 if (phba->cfg_multi_ring_support == 2) {
10593 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10594 status2 >>= (4*LPFC_EXTRA_RING);
10595 } else
10596 status2 = 0;
10597
10598 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010599 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010600 else
10601 fp_irq_rc = IRQ_NONE;
10602
10603 /* Return device-level interrupt handling status */
10604 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010605} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010606
10607/**
10608 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10609 * @phba: pointer to lpfc hba data structure.
10610 *
10611 * This routine is invoked by the worker thread to process all the pending
10612 * SLI4 FCP abort XRI events.
10613 **/
10614void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10615{
10616 struct lpfc_cq_event *cq_event;
10617
10618 /* First, declare the fcp xri abort event has been handled */
10619 spin_lock_irq(&phba->hbalock);
10620 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10621 spin_unlock_irq(&phba->hbalock);
10622 /* Now, handle all the fcp xri abort events */
10623 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10624 /* Get the first event from the head of the event queue */
10625 spin_lock_irq(&phba->hbalock);
10626 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10627 cq_event, struct lpfc_cq_event, list);
10628 spin_unlock_irq(&phba->hbalock);
10629 /* Notify aborted XRI for FCP work queue */
10630 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10631 /* Free the event processed back to the free pool */
10632 lpfc_sli4_cq_event_release(phba, cq_event);
10633 }
10634}
10635
10636/**
10637 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10638 * @phba: pointer to lpfc hba data structure.
10639 *
10640 * This routine is invoked by the worker thread to process all the pending
10641 * SLI4 els abort xri events.
10642 **/
10643void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10644{
10645 struct lpfc_cq_event *cq_event;
10646
10647 /* First, declare the els xri abort event has been handled */
10648 spin_lock_irq(&phba->hbalock);
10649 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10650 spin_unlock_irq(&phba->hbalock);
10651 /* Now, handle all the els xri abort events */
10652 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10653 /* Get the first event from the head of the event queue */
10654 spin_lock_irq(&phba->hbalock);
10655 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10656 cq_event, struct lpfc_cq_event, list);
10657 spin_unlock_irq(&phba->hbalock);
10658 /* Notify aborted XRI for ELS work queue */
10659 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10660 /* Free the event processed back to the free pool */
10661 lpfc_sli4_cq_event_release(phba, cq_event);
10662 }
10663}
10664
James Smart341af102010-01-26 23:07:37 -050010665/**
10666 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10667 * @phba: pointer to lpfc hba data structure
10668 * @pIocbIn: pointer to the rspiocbq
10669 * @pIocbOut: pointer to the cmdiocbq
10670 * @wcqe: pointer to the complete wcqe
10671 *
10672 * This routine transfers the fields of a command iocbq to a response iocbq
10673 * by copying all the IOCB fields from command iocbq and transferring the
10674 * completion status information from the complete wcqe.
10675 **/
James Smart4f774512009-05-22 14:52:35 -040010676static void
James Smart341af102010-01-26 23:07:37 -050010677lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10678 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010679 struct lpfc_iocbq *pIocbOut,
10680 struct lpfc_wcqe_complete *wcqe)
10681{
James Smart341af102010-01-26 23:07:37 -050010682 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010683 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010684 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10685
10686 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10687 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010688 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010689 status = bf_get(lpfc_wcqe_c_status, wcqe);
10690 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010691 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10692 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10693 pIocbIn->iocb.un.fcpi.fcpi_parm =
10694 pIocbOut->iocb.un.fcpi.fcpi_parm -
10695 wcqe->total_data_placed;
10696 else
10697 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010698 else {
James Smart4f774512009-05-22 14:52:35 -040010699 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010700 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10701 }
James Smart341af102010-01-26 23:07:37 -050010702
James Smartacd68592012-01-18 16:25:09 -050010703 /* Convert BG errors for completion status */
10704 if (status == CQE_STATUS_DI_ERROR) {
10705 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10706
10707 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10708 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10709 else
10710 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10711
10712 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10713 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10714 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10715 BGS_GUARD_ERR_MASK;
10716 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10717 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10718 BGS_APPTAG_ERR_MASK;
10719 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10720 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10721 BGS_REFTAG_ERR_MASK;
10722
10723 /* Check to see if there was any good data before the error */
10724 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10725 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10726 BGS_HI_WATER_MARK_PRESENT_MASK;
10727 pIocbIn->iocb.unsli3.sli3_bg.bghm =
10728 wcqe->total_data_placed;
10729 }
10730
10731 /*
10732 * Set ALL the error bits to indicate we don't know what
10733 * type of error it is.
10734 */
10735 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10736 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10737 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10738 BGS_GUARD_ERR_MASK);
10739 }
10740
James Smart341af102010-01-26 23:07:37 -050010741 /* Pick up HBA exchange busy condition */
10742 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10743 spin_lock_irqsave(&phba->hbalock, iflags);
10744 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10745 spin_unlock_irqrestore(&phba->hbalock, iflags);
10746 }
James Smart4f774512009-05-22 14:52:35 -040010747}
10748
10749/**
James Smart45ed1192009-10-02 15:17:02 -040010750 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10751 * @phba: Pointer to HBA context object.
10752 * @wcqe: Pointer to work-queue completion queue entry.
10753 *
10754 * This routine handles an ELS work-queue completion event and construct
10755 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10756 * discovery engine to handle.
10757 *
10758 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10759 **/
10760static struct lpfc_iocbq *
10761lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10762 struct lpfc_iocbq *irspiocbq)
10763{
10764 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10765 struct lpfc_iocbq *cmdiocbq;
10766 struct lpfc_wcqe_complete *wcqe;
10767 unsigned long iflags;
10768
10769 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10770 spin_lock_irqsave(&phba->hbalock, iflags);
10771 pring->stats.iocb_event++;
10772 /* Look up the ELS command IOCB and create pseudo response IOCB */
10773 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10774 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10775 spin_unlock_irqrestore(&phba->hbalock, iflags);
10776
10777 if (unlikely(!cmdiocbq)) {
10778 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10779 "0386 ELS complete with no corresponding "
10780 "cmdiocb: iotag (%d)\n",
10781 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10782 lpfc_sli_release_iocbq(phba, irspiocbq);
10783 return NULL;
10784 }
10785
10786 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010787 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010788
10789 return irspiocbq;
10790}
10791
10792/**
James Smart04c68492009-05-22 14:52:52 -040010793 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10794 * @phba: Pointer to HBA context object.
10795 * @cqe: Pointer to mailbox completion queue entry.
10796 *
10797 * This routine process a mailbox completion queue entry with asynchrous
10798 * event.
10799 *
10800 * Return: true if work posted to worker thread, otherwise false.
10801 **/
10802static bool
10803lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10804{
10805 struct lpfc_cq_event *cq_event;
10806 unsigned long iflags;
10807
10808 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10809 "0392 Async Event: word0:x%x, word1:x%x, "
10810 "word2:x%x, word3:x%x\n", mcqe->word0,
10811 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10812
10813 /* Allocate a new internal CQ_EVENT entry */
10814 cq_event = lpfc_sli4_cq_event_alloc(phba);
10815 if (!cq_event) {
10816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10817 "0394 Failed to allocate CQ_EVENT entry\n");
10818 return false;
10819 }
10820
10821 /* Move the CQE into an asynchronous event entry */
10822 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10823 spin_lock_irqsave(&phba->hbalock, iflags);
10824 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10825 /* Set the async event flag */
10826 phba->hba_flag |= ASYNC_EVENT;
10827 spin_unlock_irqrestore(&phba->hbalock, iflags);
10828
10829 return true;
10830}
10831
10832/**
10833 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10834 * @phba: Pointer to HBA context object.
10835 * @cqe: Pointer to mailbox completion queue entry.
10836 *
10837 * This routine process a mailbox completion queue entry with mailbox
10838 * completion event.
10839 *
10840 * Return: true if work posted to worker thread, otherwise false.
10841 **/
10842static bool
10843lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10844{
10845 uint32_t mcqe_status;
10846 MAILBOX_t *mbox, *pmbox;
10847 struct lpfc_mqe *mqe;
10848 struct lpfc_vport *vport;
10849 struct lpfc_nodelist *ndlp;
10850 struct lpfc_dmabuf *mp;
10851 unsigned long iflags;
10852 LPFC_MBOXQ_t *pmb;
10853 bool workposted = false;
10854 int rc;
10855
10856 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10857 if (!bf_get(lpfc_trailer_completed, mcqe))
10858 goto out_no_mqe_complete;
10859
10860 /* Get the reference to the active mbox command */
10861 spin_lock_irqsave(&phba->hbalock, iflags);
10862 pmb = phba->sli.mbox_active;
10863 if (unlikely(!pmb)) {
10864 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10865 "1832 No pending MBOX command to handle\n");
10866 spin_unlock_irqrestore(&phba->hbalock, iflags);
10867 goto out_no_mqe_complete;
10868 }
10869 spin_unlock_irqrestore(&phba->hbalock, iflags);
10870 mqe = &pmb->u.mqe;
10871 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10872 mbox = phba->mbox;
10873 vport = pmb->vport;
10874
10875 /* Reset heartbeat timer */
10876 phba->last_completion_time = jiffies;
10877 del_timer(&phba->sli.mbox_tmo);
10878
10879 /* Move mbox data to caller's mailbox region, do endian swapping */
10880 if (pmb->mbox_cmpl && mbox)
10881 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040010882
James Smart73d91e52011-10-10 21:32:10 -040010883 /*
10884 * For mcqe errors, conditionally move a modified error code to
10885 * the mbox so that the error will not be missed.
10886 */
10887 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10888 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10889 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10890 bf_set(lpfc_mqe_status, mqe,
10891 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10892 }
James Smart04c68492009-05-22 14:52:52 -040010893 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10894 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10895 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10896 "MBOX dflt rpi: status:x%x rpi:x%x",
10897 mcqe_status,
10898 pmbox->un.varWords[0], 0);
10899 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10900 mp = (struct lpfc_dmabuf *)(pmb->context1);
10901 ndlp = (struct lpfc_nodelist *)pmb->context2;
10902 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10903 * RID of the PPI using the same mbox buffer.
10904 */
10905 lpfc_unreg_login(phba, vport->vpi,
10906 pmbox->un.varWords[0], pmb);
10907 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10908 pmb->context1 = mp;
10909 pmb->context2 = ndlp;
10910 pmb->vport = vport;
10911 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10912 if (rc != MBX_BUSY)
10913 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10914 LOG_SLI, "0385 rc should "
10915 "have been MBX_BUSY\n");
10916 if (rc != MBX_NOT_FINISHED)
10917 goto send_current_mbox;
10918 }
10919 }
10920 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10921 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10922 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10923
10924 /* There is mailbox completion work to do */
10925 spin_lock_irqsave(&phba->hbalock, iflags);
10926 __lpfc_mbox_cmpl_put(phba, pmb);
10927 phba->work_ha |= HA_MBATT;
10928 spin_unlock_irqrestore(&phba->hbalock, iflags);
10929 workposted = true;
10930
10931send_current_mbox:
10932 spin_lock_irqsave(&phba->hbalock, iflags);
10933 /* Release the mailbox command posting token */
10934 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10935 /* Setting active mailbox pointer need to be in sync to flag clear */
10936 phba->sli.mbox_active = NULL;
10937 spin_unlock_irqrestore(&phba->hbalock, iflags);
10938 /* Wake up worker thread to post the next pending mailbox command */
10939 lpfc_worker_wake_up(phba);
10940out_no_mqe_complete:
10941 if (bf_get(lpfc_trailer_consumed, mcqe))
10942 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10943 return workposted;
10944}
10945
10946/**
10947 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10948 * @phba: Pointer to HBA context object.
10949 * @cqe: Pointer to mailbox completion queue entry.
10950 *
10951 * This routine process a mailbox completion queue entry, it invokes the
10952 * proper mailbox complete handling or asynchrous event handling routine
10953 * according to the MCQE's async bit.
10954 *
10955 * Return: true if work posted to worker thread, otherwise false.
10956 **/
10957static bool
10958lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10959{
10960 struct lpfc_mcqe mcqe;
10961 bool workposted;
10962
10963 /* Copy the mailbox MCQE and convert endian order as needed */
10964 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10965
10966 /* Invoke the proper event handling routine */
10967 if (!bf_get(lpfc_trailer_async, &mcqe))
10968 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10969 else
10970 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10971 return workposted;
10972}
10973
10974/**
James Smart4f774512009-05-22 14:52:35 -040010975 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10976 * @phba: Pointer to HBA context object.
10977 * @wcqe: Pointer to work-queue completion queue entry.
10978 *
10979 * This routine handles an ELS work-queue completion event.
10980 *
10981 * Return: true if work posted to worker thread, otherwise false.
10982 **/
10983static bool
10984lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10985 struct lpfc_wcqe_complete *wcqe)
10986{
James Smart4f774512009-05-22 14:52:35 -040010987 struct lpfc_iocbq *irspiocbq;
10988 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010989 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010990
James Smart45ed1192009-10-02 15:17:02 -040010991 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010992 irspiocbq = lpfc_sli_get_iocbq(phba);
10993 if (!irspiocbq) {
10994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010995 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10996 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10997 pring->txq_cnt, phba->iocb_cnt,
10998 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10999 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011000 return false;
James Smart4f774512009-05-22 14:52:35 -040011001 }
James Smart4f774512009-05-22 14:52:35 -040011002
James Smart45ed1192009-10-02 15:17:02 -040011003 /* Save off the slow-path queue event for work thread to process */
11004 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011005 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011006 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011007 &phba->sli4_hba.sp_queue_event);
11008 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011009 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011010
James Smart45ed1192009-10-02 15:17:02 -040011011 return true;
James Smart4f774512009-05-22 14:52:35 -040011012}
11013
11014/**
11015 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11016 * @phba: Pointer to HBA context object.
11017 * @wcqe: Pointer to work-queue completion queue entry.
11018 *
11019 * This routine handles slow-path WQ entry comsumed event by invoking the
11020 * proper WQ release routine to the slow-path WQ.
11021 **/
11022static void
11023lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11024 struct lpfc_wcqe_release *wcqe)
11025{
James Smart2e90f4b2011-12-13 13:22:37 -050011026 /* sanity check on queue memory */
11027 if (unlikely(!phba->sli4_hba.els_wq))
11028 return;
James Smart4f774512009-05-22 14:52:35 -040011029 /* Check for the slow-path ELS work queue */
11030 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11031 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11032 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11033 else
11034 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11035 "2579 Slow-path wqe consume event carries "
11036 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11037 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11038 phba->sli4_hba.els_wq->queue_id);
11039}
11040
11041/**
11042 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11043 * @phba: Pointer to HBA context object.
11044 * @cq: Pointer to a WQ completion queue.
11045 * @wcqe: Pointer to work-queue completion queue entry.
11046 *
11047 * This routine handles an XRI abort event.
11048 *
11049 * Return: true if work posted to worker thread, otherwise false.
11050 **/
11051static bool
11052lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11053 struct lpfc_queue *cq,
11054 struct sli4_wcqe_xri_aborted *wcqe)
11055{
11056 bool workposted = false;
11057 struct lpfc_cq_event *cq_event;
11058 unsigned long iflags;
11059
11060 /* Allocate a new internal CQ_EVENT entry */
11061 cq_event = lpfc_sli4_cq_event_alloc(phba);
11062 if (!cq_event) {
11063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11064 "0602 Failed to allocate CQ_EVENT entry\n");
11065 return false;
11066 }
11067
11068 /* Move the CQE into the proper xri abort event list */
11069 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11070 switch (cq->subtype) {
11071 case LPFC_FCP:
11072 spin_lock_irqsave(&phba->hbalock, iflags);
11073 list_add_tail(&cq_event->list,
11074 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11075 /* Set the fcp xri abort event flag */
11076 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11077 spin_unlock_irqrestore(&phba->hbalock, iflags);
11078 workposted = true;
11079 break;
11080 case LPFC_ELS:
11081 spin_lock_irqsave(&phba->hbalock, iflags);
11082 list_add_tail(&cq_event->list,
11083 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11084 /* Set the els xri abort event flag */
11085 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11086 spin_unlock_irqrestore(&phba->hbalock, iflags);
11087 workposted = true;
11088 break;
11089 default:
11090 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11091 "0603 Invalid work queue CQE subtype (x%x)\n",
11092 cq->subtype);
11093 workposted = false;
11094 break;
11095 }
11096 return workposted;
11097}
11098
11099/**
James Smart4d9ab992009-10-02 15:16:39 -040011100 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011101 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011102 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011103 *
James Smart4d9ab992009-10-02 15:16:39 -040011104 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011105 *
11106 * Return: true if work posted to worker thread, otherwise false.
11107 **/
11108static bool
James Smart4d9ab992009-10-02 15:16:39 -040011109lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11110{
11111 bool workposted = false;
11112 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11113 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11114 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011115 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011116 unsigned long iflags;
11117
James Smart2e90f4b2011-12-13 13:22:37 -050011118 /* sanity check on queue memory */
11119 if (unlikely(!hrq) || unlikely(!drq))
11120 return workposted;
11121
James Smart7851fe22011-07-22 18:36:52 -040011122 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11123 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11124 else
11125 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11126 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011127 goto out;
11128
11129 status = bf_get(lpfc_rcqe_status, rcqe);
11130 switch (status) {
11131 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11132 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11133 "2537 Receive Frame Truncated!!\n");
11134 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011135 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011136 spin_lock_irqsave(&phba->hbalock, iflags);
11137 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11138 if (!dma_buf) {
11139 spin_unlock_irqrestore(&phba->hbalock, iflags);
11140 goto out;
11141 }
11142 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11143 /* save off the frame for the word thread to process */
11144 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011145 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011146 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011147 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011148 spin_unlock_irqrestore(&phba->hbalock, iflags);
11149 workposted = true;
11150 break;
11151 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11152 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11153 /* Post more buffers if possible */
11154 spin_lock_irqsave(&phba->hbalock, iflags);
11155 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11156 spin_unlock_irqrestore(&phba->hbalock, iflags);
11157 workposted = true;
11158 break;
11159 }
11160out:
11161 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011162}
11163
11164/**
11165 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11166 * @phba: Pointer to HBA context object.
11167 * @cq: Pointer to the completion queue.
11168 * @wcqe: Pointer to a completion queue entry.
11169 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011170 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011171 * entry.
11172 *
11173 * Return: true if work posted to worker thread, otherwise false.
11174 **/
11175static bool
11176lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011177 struct lpfc_cqe *cqe)
11178{
James Smart45ed1192009-10-02 15:17:02 -040011179 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011180 bool workposted = false;
11181
11182 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011183 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011184
11185 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011186 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011187 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011188 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011189 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011190 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011191 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011192 break;
11193 case CQE_CODE_RELEASE_WQE:
11194 /* Process the WQ release event */
11195 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011196 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011197 break;
11198 case CQE_CODE_XRI_ABORTED:
11199 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011200 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011201 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011202 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011203 break;
James Smart4d9ab992009-10-02 15:16:39 -040011204 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011205 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011206 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011207 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011208 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011209 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011210 break;
James Smart4f774512009-05-22 14:52:35 -040011211 default:
11212 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11213 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011214 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011215 break;
11216 }
11217 return workposted;
11218}
11219
11220/**
James Smart4f774512009-05-22 14:52:35 -040011221 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11222 * @phba: Pointer to HBA context object.
11223 * @eqe: Pointer to fast-path event queue entry.
11224 *
11225 * This routine process a event queue entry from the slow-path event queue.
11226 * It will check the MajorCode and MinorCode to determine this is for a
11227 * completion event on a completion queue, if not, an error shall be logged
11228 * and just return. Otherwise, it will get to the corresponding completion
11229 * queue and process all the entries on that completion queue, rearm the
11230 * completion queue, and then return.
11231 *
11232 **/
11233static void
11234lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11235{
11236 struct lpfc_queue *cq = NULL, *childq, *speq;
11237 struct lpfc_cqe *cqe;
11238 bool workposted = false;
11239 int ecount = 0;
11240 uint16_t cqid;
11241
James Smartcb5172e2010-03-15 11:25:07 -040011242 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011243 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11244 "0359 Not a valid slow-path completion "
11245 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011246 bf_get_le32(lpfc_eqe_major_code, eqe),
11247 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011248 return;
11249 }
11250
11251 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011252 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011253
11254 /* Search for completion queue pointer matching this cqid */
11255 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011256 /* sanity check on queue memory */
11257 if (unlikely(!speq))
11258 return;
James Smart4f774512009-05-22 14:52:35 -040011259 list_for_each_entry(childq, &speq->child_list, list) {
11260 if (childq->queue_id == cqid) {
11261 cq = childq;
11262 break;
11263 }
11264 }
11265 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011266 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11267 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11268 "0365 Slow-path CQ identifier "
11269 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011270 return;
11271 }
11272
11273 /* Process all the entries to the CQ */
11274 switch (cq->type) {
11275 case LPFC_MCQ:
11276 while ((cqe = lpfc_sli4_cq_get(cq))) {
11277 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011278 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011279 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11280 }
11281 break;
11282 case LPFC_WCQ:
11283 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011284 if (cq->subtype == LPFC_FCP)
11285 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11286 cqe);
11287 else
11288 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11289 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011290 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011291 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11292 }
11293 break;
11294 default:
11295 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11296 "0370 Invalid completion queue type (%d)\n",
11297 cq->type);
11298 return;
11299 }
11300
11301 /* Catch the no cq entry condition, log an error */
11302 if (unlikely(ecount == 0))
11303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11304 "0371 No entry from the CQ: identifier "
11305 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11306
11307 /* In any case, flash and re-arm the RCQ */
11308 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11309
11310 /* wake up worker thread if there are works to be done */
11311 if (workposted)
11312 lpfc_worker_wake_up(phba);
11313}
11314
11315/**
11316 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11317 * @eqe: Pointer to fast-path completion queue entry.
11318 *
11319 * This routine process a fast-path work queue completion entry from fast-path
11320 * event queue for FCP command response completion.
11321 **/
11322static void
11323lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11324 struct lpfc_wcqe_complete *wcqe)
11325{
11326 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11327 struct lpfc_iocbq *cmdiocbq;
11328 struct lpfc_iocbq irspiocbq;
11329 unsigned long iflags;
11330
11331 spin_lock_irqsave(&phba->hbalock, iflags);
11332 pring->stats.iocb_event++;
11333 spin_unlock_irqrestore(&phba->hbalock, iflags);
11334
11335 /* Check for response status */
11336 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11337 /* If resource errors reported from HBA, reduce queue
11338 * depth of the SCSI device.
11339 */
11340 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11341 IOSTAT_LOCAL_REJECT) &&
11342 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11343 phba->lpfc_rampdown_queue_depth(phba);
11344 }
11345 /* Log the error status */
11346 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11347 "0373 FCP complete error: status=x%x, "
11348 "hw_status=x%x, total_data_specified=%d, "
11349 "parameter=x%x, word3=x%x\n",
11350 bf_get(lpfc_wcqe_c_status, wcqe),
11351 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11352 wcqe->total_data_placed, wcqe->parameter,
11353 wcqe->word3);
11354 }
11355
11356 /* Look up the FCP command IOCB and create pseudo response IOCB */
11357 spin_lock_irqsave(&phba->hbalock, iflags);
11358 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11359 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11360 spin_unlock_irqrestore(&phba->hbalock, iflags);
11361 if (unlikely(!cmdiocbq)) {
11362 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11363 "0374 FCP complete with no corresponding "
11364 "cmdiocb: iotag (%d)\n",
11365 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11366 return;
11367 }
11368 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11369 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11370 "0375 FCP cmdiocb not callback function "
11371 "iotag: (%d)\n",
11372 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11373 return;
11374 }
11375
11376 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011377 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011378
James Smart0f65ff62010-02-26 14:14:23 -050011379 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11380 spin_lock_irqsave(&phba->hbalock, iflags);
11381 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11382 spin_unlock_irqrestore(&phba->hbalock, iflags);
11383 }
11384
James Smart4f774512009-05-22 14:52:35 -040011385 /* Pass the cmd_iocb and the rsp state to the upper layer */
11386 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11387}
11388
11389/**
11390 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11391 * @phba: Pointer to HBA context object.
11392 * @cq: Pointer to completion queue.
11393 * @wcqe: Pointer to work-queue completion queue entry.
11394 *
11395 * This routine handles an fast-path WQ entry comsumed event by invoking the
11396 * proper WQ release routine to the slow-path WQ.
11397 **/
11398static void
11399lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11400 struct lpfc_wcqe_release *wcqe)
11401{
11402 struct lpfc_queue *childwq;
11403 bool wqid_matched = false;
11404 uint16_t fcp_wqid;
11405
11406 /* Check for fast-path FCP work queue release */
11407 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11408 list_for_each_entry(childwq, &cq->child_list, list) {
11409 if (childwq->queue_id == fcp_wqid) {
11410 lpfc_sli4_wq_release(childwq,
11411 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11412 wqid_matched = true;
11413 break;
11414 }
11415 }
11416 /* Report warning log message if no match found */
11417 if (wqid_matched != true)
11418 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11419 "2580 Fast-path wqe consume event carries "
11420 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11421}
11422
11423/**
11424 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11425 * @cq: Pointer to the completion queue.
11426 * @eqe: Pointer to fast-path completion queue entry.
11427 *
11428 * This routine process a fast-path work queue completion entry from fast-path
11429 * event queue for FCP command response completion.
11430 **/
11431static int
11432lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11433 struct lpfc_cqe *cqe)
11434{
11435 struct lpfc_wcqe_release wcqe;
11436 bool workposted = false;
11437
11438 /* Copy the work queue CQE and convert endian order if needed */
11439 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11440
11441 /* Check and process for different type of WCQE and dispatch */
11442 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11443 case CQE_CODE_COMPL_WQE:
11444 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011445 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011446 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11447 (struct lpfc_wcqe_complete *)&wcqe);
11448 break;
11449 case CQE_CODE_RELEASE_WQE:
11450 /* Process the WQ release event */
11451 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11452 (struct lpfc_wcqe_release *)&wcqe);
11453 break;
11454 case CQE_CODE_XRI_ABORTED:
11455 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011456 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011457 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11458 (struct sli4_wcqe_xri_aborted *)&wcqe);
11459 break;
11460 default:
11461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11462 "0144 Not a valid WCQE code: x%x\n",
11463 bf_get(lpfc_wcqe_c_code, &wcqe));
11464 break;
11465 }
11466 return workposted;
11467}
11468
11469/**
11470 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11471 * @phba: Pointer to HBA context object.
11472 * @eqe: Pointer to fast-path event queue entry.
11473 *
11474 * This routine process a event queue entry from the fast-path event queue.
11475 * It will check the MajorCode and MinorCode to determine this is for a
11476 * completion event on a completion queue, if not, an error shall be logged
11477 * and just return. Otherwise, it will get to the corresponding completion
11478 * queue and process all the entries on the completion queue, rearm the
11479 * completion queue, and then return.
11480 **/
11481static void
11482lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11483 uint32_t fcp_cqidx)
11484{
11485 struct lpfc_queue *cq;
11486 struct lpfc_cqe *cqe;
11487 bool workposted = false;
11488 uint16_t cqid;
11489 int ecount = 0;
11490
James Smartcb5172e2010-03-15 11:25:07 -040011491 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011492 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11493 "0366 Not a valid fast-path completion "
11494 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011495 bf_get_le32(lpfc_eqe_major_code, eqe),
11496 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011497 return;
11498 }
11499
James Smart2e90f4b2011-12-13 13:22:37 -050011500 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11501 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11502 "3146 Fast-path completion queues "
11503 "does not exist\n");
11504 return;
11505 }
James Smart4f774512009-05-22 14:52:35 -040011506 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11507 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011508 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11509 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11510 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011511 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011512 return;
11513 }
11514
11515 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011516 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011517 if (unlikely(cqid != cq->queue_id)) {
11518 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11519 "0368 Miss-matched fast-path completion "
11520 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11521 cqid, cq->queue_id);
11522 return;
11523 }
11524
11525 /* Process all the entries to the CQ */
11526 while ((cqe = lpfc_sli4_cq_get(cq))) {
11527 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011528 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011529 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11530 }
11531
11532 /* Catch the no cq entry condition */
11533 if (unlikely(ecount == 0))
11534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11535 "0369 No entry from fast-path completion "
11536 "queue fcpcqid=%d\n", cq->queue_id);
11537
11538 /* In any case, flash and re-arm the CQ */
11539 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11540
11541 /* wake up worker thread if there are works to be done */
11542 if (workposted)
11543 lpfc_worker_wake_up(phba);
11544}
11545
11546static void
11547lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11548{
11549 struct lpfc_eqe *eqe;
11550
11551 /* walk all the EQ entries and drop on the floor */
11552 while ((eqe = lpfc_sli4_eq_get(eq)))
11553 ;
11554
11555 /* Clear and re-arm the EQ */
11556 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11557}
11558
11559/**
11560 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11561 * @irq: Interrupt number.
11562 * @dev_id: The device context pointer.
11563 *
11564 * This function is directly called from the PCI layer as an interrupt
11565 * service routine when device with SLI-4 interface spec is enabled with
11566 * MSI-X multi-message interrupt mode and there are slow-path events in
11567 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11568 * interrupt mode, this function is called as part of the device-level
11569 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11570 * undergoing initialization, the interrupt handler will not process the
11571 * interrupt. The link attention and ELS ring attention events are handled
11572 * by the worker thread. The interrupt handler signals the worker thread
11573 * and returns for these events. This function is called without any lock
11574 * held. It gets the hbalock to access and update SLI data structures.
11575 *
11576 * This function returns IRQ_HANDLED when interrupt is handled else it
11577 * returns IRQ_NONE.
11578 **/
11579irqreturn_t
11580lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11581{
11582 struct lpfc_hba *phba;
11583 struct lpfc_queue *speq;
11584 struct lpfc_eqe *eqe;
11585 unsigned long iflag;
11586 int ecount = 0;
11587
11588 /*
11589 * Get the driver's phba structure from the dev_id
11590 */
11591 phba = (struct lpfc_hba *)dev_id;
11592
11593 if (unlikely(!phba))
11594 return IRQ_NONE;
11595
11596 /* Get to the EQ struct associated with this vector */
11597 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011598 if (unlikely(!speq))
11599 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011600
11601 /* Check device state for handling interrupt */
11602 if (unlikely(lpfc_intr_state_check(phba))) {
11603 /* Check again for link_state with lock held */
11604 spin_lock_irqsave(&phba->hbalock, iflag);
11605 if (phba->link_state < LPFC_LINK_DOWN)
11606 /* Flush, clear interrupt, and rearm the EQ */
11607 lpfc_sli4_eq_flush(phba, speq);
11608 spin_unlock_irqrestore(&phba->hbalock, iflag);
11609 return IRQ_NONE;
11610 }
11611
11612 /*
11613 * Process all the event on FCP slow-path EQ
11614 */
11615 while ((eqe = lpfc_sli4_eq_get(speq))) {
11616 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011617 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011618 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11619 }
11620
11621 /* Always clear and re-arm the slow-path EQ */
11622 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11623
11624 /* Catch the no cq entry condition */
11625 if (unlikely(ecount == 0)) {
11626 if (phba->intr_type == MSIX)
11627 /* MSI-X treated interrupt served as no EQ share INT */
11628 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11629 "0357 MSI-X interrupt with no EQE\n");
11630 else
11631 /* Non MSI-X treated on interrupt as EQ share INT */
11632 return IRQ_NONE;
11633 }
11634
11635 return IRQ_HANDLED;
11636} /* lpfc_sli4_sp_intr_handler */
11637
11638/**
11639 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11640 * @irq: Interrupt number.
11641 * @dev_id: The device context pointer.
11642 *
11643 * This function is directly called from the PCI layer as an interrupt
11644 * service routine when device with SLI-4 interface spec is enabled with
11645 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11646 * ring event in the HBA. However, when the device is enabled with either
11647 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11648 * device-level interrupt handler. When the PCI slot is in error recovery
11649 * or the HBA is undergoing initialization, the interrupt handler will not
11650 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11651 * the intrrupt context. This function is called without any lock held.
11652 * It gets the hbalock to access and update SLI data structures. Note that,
11653 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11654 * equal to that of FCP CQ index.
11655 *
11656 * This function returns IRQ_HANDLED when interrupt is handled else it
11657 * returns IRQ_NONE.
11658 **/
11659irqreturn_t
11660lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11661{
11662 struct lpfc_hba *phba;
11663 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11664 struct lpfc_queue *fpeq;
11665 struct lpfc_eqe *eqe;
11666 unsigned long iflag;
11667 int ecount = 0;
11668 uint32_t fcp_eqidx;
11669
11670 /* Get the driver's phba structure from the dev_id */
11671 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11672 phba = fcp_eq_hdl->phba;
11673 fcp_eqidx = fcp_eq_hdl->idx;
11674
11675 if (unlikely(!phba))
11676 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011677 if (unlikely(!phba->sli4_hba.fp_eq))
11678 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011679
11680 /* Get to the EQ struct associated with this vector */
11681 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011682 if (unlikely(!fpeq))
11683 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011684
11685 /* Check device state for handling interrupt */
11686 if (unlikely(lpfc_intr_state_check(phba))) {
11687 /* Check again for link_state with lock held */
11688 spin_lock_irqsave(&phba->hbalock, iflag);
11689 if (phba->link_state < LPFC_LINK_DOWN)
11690 /* Flush, clear interrupt, and rearm the EQ */
11691 lpfc_sli4_eq_flush(phba, fpeq);
11692 spin_unlock_irqrestore(&phba->hbalock, iflag);
11693 return IRQ_NONE;
11694 }
11695
11696 /*
11697 * Process all the event on FCP fast-path EQ
11698 */
11699 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11700 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011701 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011702 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11703 }
11704
11705 /* Always clear and re-arm the fast-path EQ */
11706 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11707
11708 if (unlikely(ecount == 0)) {
11709 if (phba->intr_type == MSIX)
11710 /* MSI-X treated interrupt served as no EQ share INT */
11711 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11712 "0358 MSI-X interrupt with no EQE\n");
11713 else
11714 /* Non MSI-X treated on interrupt as EQ share INT */
11715 return IRQ_NONE;
11716 }
11717
11718 return IRQ_HANDLED;
11719} /* lpfc_sli4_fp_intr_handler */
11720
11721/**
11722 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11723 * @irq: Interrupt number.
11724 * @dev_id: The device context pointer.
11725 *
11726 * This function is the device-level interrupt handler to device with SLI-4
11727 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11728 * interrupt mode is enabled and there is an event in the HBA which requires
11729 * driver attention. This function invokes the slow-path interrupt attention
11730 * handling function and fast-path interrupt attention handling function in
11731 * turn to process the relevant HBA attention events. This function is called
11732 * without any lock held. It gets the hbalock to access and update SLI data
11733 * structures.
11734 *
11735 * This function returns IRQ_HANDLED when interrupt is handled, else it
11736 * returns IRQ_NONE.
11737 **/
11738irqreturn_t
11739lpfc_sli4_intr_handler(int irq, void *dev_id)
11740{
11741 struct lpfc_hba *phba;
11742 irqreturn_t sp_irq_rc, fp_irq_rc;
11743 bool fp_handled = false;
11744 uint32_t fcp_eqidx;
11745
11746 /* Get the driver's phba structure from the dev_id */
11747 phba = (struct lpfc_hba *)dev_id;
11748
11749 if (unlikely(!phba))
11750 return IRQ_NONE;
11751
11752 /*
11753 * Invokes slow-path host attention interrupt handling as appropriate.
11754 */
11755 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11756
11757 /*
11758 * Invoke fast-path host attention interrupt handling as appropriate.
11759 */
11760 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11761 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11762 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11763 if (fp_irq_rc == IRQ_HANDLED)
11764 fp_handled |= true;
11765 }
11766
11767 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11768} /* lpfc_sli4_intr_handler */
11769
11770/**
11771 * lpfc_sli4_queue_free - free a queue structure and associated memory
11772 * @queue: The queue structure to free.
11773 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011774 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011775 * the host resident queue. This function must be called after destroying the
11776 * queue on the HBA.
11777 **/
11778void
11779lpfc_sli4_queue_free(struct lpfc_queue *queue)
11780{
11781 struct lpfc_dmabuf *dmabuf;
11782
11783 if (!queue)
11784 return;
11785
11786 while (!list_empty(&queue->page_list)) {
11787 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11788 list);
James Smart49198b32010-04-06 15:04:33 -040011789 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011790 dmabuf->virt, dmabuf->phys);
11791 kfree(dmabuf);
11792 }
11793 kfree(queue);
11794 return;
11795}
11796
11797/**
11798 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11799 * @phba: The HBA that this queue is being created on.
11800 * @entry_size: The size of each queue entry for this queue.
11801 * @entry count: The number of entries that this queue will handle.
11802 *
11803 * This function allocates a queue structure and the DMAable memory used for
11804 * the host resident queue. This function must be called before creating the
11805 * queue on the HBA.
11806 **/
11807struct lpfc_queue *
11808lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11809 uint32_t entry_count)
11810{
11811 struct lpfc_queue *queue;
11812 struct lpfc_dmabuf *dmabuf;
11813 int x, total_qe_count;
11814 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011815 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011816
James Smartcb5172e2010-03-15 11:25:07 -040011817 if (!phba->sli4_hba.pc_sli4_params.supported)
11818 hw_page_size = SLI4_PAGE_SIZE;
11819
James Smart4f774512009-05-22 14:52:35 -040011820 queue = kzalloc(sizeof(struct lpfc_queue) +
11821 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11822 if (!queue)
11823 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011824 queue->page_count = (ALIGN(entry_size * entry_count,
11825 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011826 INIT_LIST_HEAD(&queue->list);
11827 INIT_LIST_HEAD(&queue->page_list);
11828 INIT_LIST_HEAD(&queue->child_list);
11829 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11830 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11831 if (!dmabuf)
11832 goto out_fail;
11833 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011834 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011835 GFP_KERNEL);
11836 if (!dmabuf->virt) {
11837 kfree(dmabuf);
11838 goto out_fail;
11839 }
James Smartcb5172e2010-03-15 11:25:07 -040011840 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011841 dmabuf->buffer_tag = x;
11842 list_add_tail(&dmabuf->list, &queue->page_list);
11843 /* initialize queue's entry array */
11844 dma_pointer = dmabuf->virt;
11845 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011846 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011847 total_qe_count++, dma_pointer += entry_size) {
11848 queue->qe[total_qe_count].address = dma_pointer;
11849 }
11850 }
11851 queue->entry_size = entry_size;
11852 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040011853
11854 /*
11855 * entry_repost is calculated based on the number of entries in the
11856 * queue. This works out except for RQs. If buffers are NOT initially
11857 * posted for every RQE, entry_repost should be adjusted accordingly.
11858 */
11859 queue->entry_repost = (entry_count >> 3);
11860 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11861 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040011862 queue->phba = phba;
11863
11864 return queue;
11865out_fail:
11866 lpfc_sli4_queue_free(queue);
11867 return NULL;
11868}
11869
11870/**
11871 * lpfc_eq_create - Create an Event Queue on the HBA
11872 * @phba: HBA structure that indicates port to create a queue on.
11873 * @eq: The queue structure to use to create the event queue.
11874 * @imax: The maximum interrupt per second limit.
11875 *
11876 * This function creates an event queue, as detailed in @eq, on a port,
11877 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11878 *
11879 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11880 * is used to get the entry count and entry size that are necessary to
11881 * determine the number of pages to allocate and use for this queue. This
11882 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11883 * event queue. This function is asynchronous and will wait for the mailbox
11884 * command to finish before continuing.
11885 *
11886 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011887 * memory this function will return -ENOMEM. If the queue create mailbox command
11888 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011889 **/
11890uint32_t
11891lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11892{
11893 struct lpfc_mbx_eq_create *eq_create;
11894 LPFC_MBOXQ_t *mbox;
11895 int rc, length, status = 0;
11896 struct lpfc_dmabuf *dmabuf;
11897 uint32_t shdr_status, shdr_add_status;
11898 union lpfc_sli4_cfg_shdr *shdr;
11899 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011900 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11901
James Smart2e90f4b2011-12-13 13:22:37 -050011902 /* sanity check on queue memory */
11903 if (!eq)
11904 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040011905 if (!phba->sli4_hba.pc_sli4_params.supported)
11906 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011907
11908 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11909 if (!mbox)
11910 return -ENOMEM;
11911 length = (sizeof(struct lpfc_mbx_eq_create) -
11912 sizeof(struct lpfc_sli4_cfg_mhdr));
11913 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11914 LPFC_MBOX_OPCODE_EQ_CREATE,
11915 length, LPFC_SLI4_MBX_EMBED);
11916 eq_create = &mbox->u.mqe.un.eq_create;
11917 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11918 eq->page_count);
11919 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11920 LPFC_EQE_SIZE);
11921 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11922 /* Calculate delay multiper from maximum interrupt per second */
11923 dmult = LPFC_DMULT_CONST/imax - 1;
11924 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11925 dmult);
11926 switch (eq->entry_count) {
11927 default:
11928 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11929 "0360 Unsupported EQ count. (%d)\n",
11930 eq->entry_count);
11931 if (eq->entry_count < 256)
11932 return -EINVAL;
11933 /* otherwise default to smallest count (drop through) */
11934 case 256:
11935 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11936 LPFC_EQ_CNT_256);
11937 break;
11938 case 512:
11939 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11940 LPFC_EQ_CNT_512);
11941 break;
11942 case 1024:
11943 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11944 LPFC_EQ_CNT_1024);
11945 break;
11946 case 2048:
11947 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11948 LPFC_EQ_CNT_2048);
11949 break;
11950 case 4096:
11951 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11952 LPFC_EQ_CNT_4096);
11953 break;
11954 }
11955 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011956 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011957 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11958 putPaddrLow(dmabuf->phys);
11959 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11960 putPaddrHigh(dmabuf->phys);
11961 }
11962 mbox->vport = phba->pport;
11963 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11964 mbox->context1 = NULL;
11965 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11966 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11967 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11968 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11969 if (shdr_status || shdr_add_status || rc) {
11970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11971 "2500 EQ_CREATE mailbox failed with "
11972 "status x%x add_status x%x, mbx status x%x\n",
11973 shdr_status, shdr_add_status, rc);
11974 status = -ENXIO;
11975 }
11976 eq->type = LPFC_EQ;
11977 eq->subtype = LPFC_NONE;
11978 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11979 if (eq->queue_id == 0xFFFF)
11980 status = -ENXIO;
11981 eq->host_index = 0;
11982 eq->hba_index = 0;
11983
James Smart8fa38512009-07-19 10:01:03 -040011984 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011985 return status;
11986}
11987
11988/**
11989 * lpfc_cq_create - Create a Completion Queue on the HBA
11990 * @phba: HBA structure that indicates port to create a queue on.
11991 * @cq: The queue structure to use to create the completion queue.
11992 * @eq: The event queue to bind this completion queue to.
11993 *
11994 * This function creates a completion queue, as detailed in @wq, on a port,
11995 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11996 *
11997 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11998 * is used to get the entry count and entry size that are necessary to
11999 * determine the number of pages to allocate and use for this queue. The @eq
12000 * is used to indicate which event queue to bind this completion queue to. This
12001 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12002 * completion queue. This function is asynchronous and will wait for the mailbox
12003 * command to finish before continuing.
12004 *
12005 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012006 * memory this function will return -ENOMEM. If the queue create mailbox command
12007 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012008 **/
12009uint32_t
12010lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12011 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12012{
12013 struct lpfc_mbx_cq_create *cq_create;
12014 struct lpfc_dmabuf *dmabuf;
12015 LPFC_MBOXQ_t *mbox;
12016 int rc, length, status = 0;
12017 uint32_t shdr_status, shdr_add_status;
12018 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012019 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12020
James Smart2e90f4b2011-12-13 13:22:37 -050012021 /* sanity check on queue memory */
12022 if (!cq || !eq)
12023 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012024 if (!phba->sli4_hba.pc_sli4_params.supported)
12025 hw_page_size = SLI4_PAGE_SIZE;
12026
James Smart4f774512009-05-22 14:52:35 -040012027 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12028 if (!mbox)
12029 return -ENOMEM;
12030 length = (sizeof(struct lpfc_mbx_cq_create) -
12031 sizeof(struct lpfc_sli4_cfg_mhdr));
12032 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12033 LPFC_MBOX_OPCODE_CQ_CREATE,
12034 length, LPFC_SLI4_MBX_EMBED);
12035 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012036 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012037 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12038 cq->page_count);
12039 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12040 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012041 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12042 phba->sli4_hba.pc_sli4_params.cqv);
12043 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012044 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12045 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012046 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12047 eq->queue_id);
12048 } else {
12049 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12050 eq->queue_id);
12051 }
James Smart4f774512009-05-22 14:52:35 -040012052 switch (cq->entry_count) {
12053 default:
12054 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12055 "0361 Unsupported CQ count. (%d)\n",
12056 cq->entry_count);
12057 if (cq->entry_count < 256)
12058 return -EINVAL;
12059 /* otherwise default to smallest count (drop through) */
12060 case 256:
12061 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12062 LPFC_CQ_CNT_256);
12063 break;
12064 case 512:
12065 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12066 LPFC_CQ_CNT_512);
12067 break;
12068 case 1024:
12069 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12070 LPFC_CQ_CNT_1024);
12071 break;
12072 }
12073 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012074 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012075 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12076 putPaddrLow(dmabuf->phys);
12077 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12078 putPaddrHigh(dmabuf->phys);
12079 }
12080 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12081
12082 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012083 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12084 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12085 if (shdr_status || shdr_add_status || rc) {
12086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12087 "2501 CQ_CREATE mailbox failed with "
12088 "status x%x add_status x%x, mbx status x%x\n",
12089 shdr_status, shdr_add_status, rc);
12090 status = -ENXIO;
12091 goto out;
12092 }
12093 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12094 if (cq->queue_id == 0xFFFF) {
12095 status = -ENXIO;
12096 goto out;
12097 }
12098 /* link the cq onto the parent eq child list */
12099 list_add_tail(&cq->list, &eq->child_list);
12100 /* Set up completion queue's type and subtype */
12101 cq->type = type;
12102 cq->subtype = subtype;
12103 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012104 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012105 cq->host_index = 0;
12106 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012107
James Smart8fa38512009-07-19 10:01:03 -040012108out:
12109 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012110 return status;
12111}
12112
12113/**
James Smartb19a0612010-04-06 14:48:51 -040012114 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012115 * @phba: HBA structure that indicates port to create a queue on.
12116 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012117 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12118 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012119 *
James Smartb19a0612010-04-06 14:48:51 -040012120 * This function provides failback (fb) functionality when the
12121 * mq_create_ext fails on older FW generations. It's purpose is identical
12122 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012123 *
James Smartb19a0612010-04-06 14:48:51 -040012124 * This routine cannot fail as all attributes were previously accessed and
12125 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012126 **/
James Smartb19a0612010-04-06 14:48:51 -040012127static void
12128lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12129 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012130{
12131 struct lpfc_mbx_mq_create *mq_create;
12132 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012133 int length;
James Smart04c68492009-05-22 14:52:52 -040012134
James Smart04c68492009-05-22 14:52:52 -040012135 length = (sizeof(struct lpfc_mbx_mq_create) -
12136 sizeof(struct lpfc_sli4_cfg_mhdr));
12137 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12138 LPFC_MBOX_OPCODE_MQ_CREATE,
12139 length, LPFC_SLI4_MBX_EMBED);
12140 mq_create = &mbox->u.mqe.un.mq_create;
12141 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012142 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012143 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012144 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012145 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12146 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012147 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012148 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12149 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012150 break;
12151 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012152 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12153 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012154 break;
12155 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012156 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12157 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012158 break;
12159 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012160 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12161 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012162 break;
12163 }
12164 list_for_each_entry(dmabuf, &mq->page_list, list) {
12165 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012166 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012167 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012168 putPaddrHigh(dmabuf->phys);
12169 }
12170}
12171
12172/**
12173 * lpfc_mq_create - Create a mailbox Queue on the HBA
12174 * @phba: HBA structure that indicates port to create a queue on.
12175 * @mq: The queue structure to use to create the mailbox queue.
12176 * @cq: The completion queue to associate with this cq.
12177 * @subtype: The queue's subtype.
12178 *
12179 * This function creates a mailbox queue, as detailed in @mq, on a port,
12180 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12181 *
12182 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12183 * is used to get the entry count and entry size that are necessary to
12184 * determine the number of pages to allocate and use for this queue. This
12185 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12186 * mailbox queue. This function is asynchronous and will wait for the mailbox
12187 * command to finish before continuing.
12188 *
12189 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012190 * memory this function will return -ENOMEM. If the queue create mailbox command
12191 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012192 **/
12193int32_t
12194lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12195 struct lpfc_queue *cq, uint32_t subtype)
12196{
12197 struct lpfc_mbx_mq_create *mq_create;
12198 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12199 struct lpfc_dmabuf *dmabuf;
12200 LPFC_MBOXQ_t *mbox;
12201 int rc, length, status = 0;
12202 uint32_t shdr_status, shdr_add_status;
12203 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012204 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012205
James Smart2e90f4b2011-12-13 13:22:37 -050012206 /* sanity check on queue memory */
12207 if (!mq || !cq)
12208 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012209 if (!phba->sli4_hba.pc_sli4_params.supported)
12210 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012211
12212 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12213 if (!mbox)
12214 return -ENOMEM;
12215 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12216 sizeof(struct lpfc_sli4_cfg_mhdr));
12217 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12218 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12219 length, LPFC_SLI4_MBX_EMBED);
12220
12221 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012222 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012223 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12224 &mq_create_ext->u.request, mq->page_count);
12225 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12226 &mq_create_ext->u.request, 1);
12227 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012228 &mq_create_ext->u.request, 1);
12229 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12230 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012231 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12232 &mq_create_ext->u.request, 1);
12233 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12234 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012235 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012236 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12237 phba->sli4_hba.pc_sli4_params.mqv);
12238 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12239 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12240 cq->queue_id);
12241 else
12242 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12243 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012244 switch (mq->entry_count) {
12245 default:
12246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12247 "0362 Unsupported MQ count. (%d)\n",
12248 mq->entry_count);
12249 if (mq->entry_count < 16)
12250 return -EINVAL;
12251 /* otherwise default to smallest count (drop through) */
12252 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012253 bf_set(lpfc_mq_context_ring_size,
12254 &mq_create_ext->u.request.context,
12255 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012256 break;
12257 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012258 bf_set(lpfc_mq_context_ring_size,
12259 &mq_create_ext->u.request.context,
12260 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012261 break;
12262 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012263 bf_set(lpfc_mq_context_ring_size,
12264 &mq_create_ext->u.request.context,
12265 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012266 break;
12267 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012268 bf_set(lpfc_mq_context_ring_size,
12269 &mq_create_ext->u.request.context,
12270 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012271 break;
12272 }
12273 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012274 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012275 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12276 putPaddrLow(dmabuf->phys);
12277 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012278 putPaddrHigh(dmabuf->phys);
12279 }
12280 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012281 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12282 &mq_create_ext->u.response);
12283 if (rc != MBX_SUCCESS) {
12284 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12285 "2795 MQ_CREATE_EXT failed with "
12286 "status x%x. Failback to MQ_CREATE.\n",
12287 rc);
12288 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12289 mq_create = &mbox->u.mqe.un.mq_create;
12290 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12291 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12292 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12293 &mq_create->u.response);
12294 }
12295
James Smart04c68492009-05-22 14:52:52 -040012296 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012297 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12298 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12299 if (shdr_status || shdr_add_status || rc) {
12300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12301 "2502 MQ_CREATE mailbox failed with "
12302 "status x%x add_status x%x, mbx status x%x\n",
12303 shdr_status, shdr_add_status, rc);
12304 status = -ENXIO;
12305 goto out;
12306 }
James Smart04c68492009-05-22 14:52:52 -040012307 if (mq->queue_id == 0xFFFF) {
12308 status = -ENXIO;
12309 goto out;
12310 }
12311 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012312 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012313 mq->subtype = subtype;
12314 mq->host_index = 0;
12315 mq->hba_index = 0;
12316
12317 /* link the mq onto the parent cq child list */
12318 list_add_tail(&mq->list, &cq->child_list);
12319out:
James Smart8fa38512009-07-19 10:01:03 -040012320 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012321 return status;
12322}
12323
12324/**
James Smart4f774512009-05-22 14:52:35 -040012325 * lpfc_wq_create - Create a Work Queue on the HBA
12326 * @phba: HBA structure that indicates port to create a queue on.
12327 * @wq: The queue structure to use to create the work queue.
12328 * @cq: The completion queue to bind this work queue to.
12329 * @subtype: The subtype of the work queue indicating its functionality.
12330 *
12331 * This function creates a work queue, as detailed in @wq, on a port, described
12332 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12333 *
12334 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12335 * is used to get the entry count and entry size that are necessary to
12336 * determine the number of pages to allocate and use for this queue. The @cq
12337 * is used to indicate which completion queue to bind this work queue to. This
12338 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12339 * work queue. This function is asynchronous and will wait for the mailbox
12340 * command to finish before continuing.
12341 *
12342 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012343 * memory this function will return -ENOMEM. If the queue create mailbox command
12344 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012345 **/
12346uint32_t
12347lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12348 struct lpfc_queue *cq, uint32_t subtype)
12349{
12350 struct lpfc_mbx_wq_create *wq_create;
12351 struct lpfc_dmabuf *dmabuf;
12352 LPFC_MBOXQ_t *mbox;
12353 int rc, length, status = 0;
12354 uint32_t shdr_status, shdr_add_status;
12355 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012356 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012357 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012358
James Smart2e90f4b2011-12-13 13:22:37 -050012359 /* sanity check on queue memory */
12360 if (!wq || !cq)
12361 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012362 if (!phba->sli4_hba.pc_sli4_params.supported)
12363 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012364
12365 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12366 if (!mbox)
12367 return -ENOMEM;
12368 length = (sizeof(struct lpfc_mbx_wq_create) -
12369 sizeof(struct lpfc_sli4_cfg_mhdr));
12370 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12371 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12372 length, LPFC_SLI4_MBX_EMBED);
12373 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012374 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012375 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12376 wq->page_count);
12377 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12378 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012379 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12380 phba->sli4_hba.pc_sli4_params.wqv);
12381 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12382 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12383 wq->entry_count);
12384 switch (wq->entry_size) {
12385 default:
12386 case 64:
12387 bf_set(lpfc_mbx_wq_create_wqe_size,
12388 &wq_create->u.request_1,
12389 LPFC_WQ_WQE_SIZE_64);
12390 break;
12391 case 128:
12392 bf_set(lpfc_mbx_wq_create_wqe_size,
12393 &wq_create->u.request_1,
12394 LPFC_WQ_WQE_SIZE_128);
12395 break;
12396 }
12397 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12398 (PAGE_SIZE/SLI4_PAGE_SIZE));
12399 page = wq_create->u.request_1.page;
12400 } else {
12401 page = wq_create->u.request.page;
12402 }
James Smart4f774512009-05-22 14:52:35 -040012403 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012404 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012405 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12406 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012407 }
12408 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12409 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012410 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12411 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12412 if (shdr_status || shdr_add_status || rc) {
12413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12414 "2503 WQ_CREATE mailbox failed with "
12415 "status x%x add_status x%x, mbx status x%x\n",
12416 shdr_status, shdr_add_status, rc);
12417 status = -ENXIO;
12418 goto out;
12419 }
12420 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12421 if (wq->queue_id == 0xFFFF) {
12422 status = -ENXIO;
12423 goto out;
12424 }
12425 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012426 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012427 wq->subtype = subtype;
12428 wq->host_index = 0;
12429 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012430 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012431
12432 /* link the wq onto the parent cq child list */
12433 list_add_tail(&wq->list, &cq->child_list);
12434out:
James Smart8fa38512009-07-19 10:01:03 -040012435 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012436 return status;
12437}
12438
12439/**
James Smart73d91e52011-10-10 21:32:10 -040012440 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12441 * @phba: HBA structure that indicates port to create a queue on.
12442 * @rq: The queue structure to use for the receive queue.
12443 * @qno: The associated HBQ number
12444 *
12445 *
12446 * For SLI4 we need to adjust the RQ repost value based on
12447 * the number of buffers that are initially posted to the RQ.
12448 */
12449void
12450lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12451{
12452 uint32_t cnt;
12453
James Smart2e90f4b2011-12-13 13:22:37 -050012454 /* sanity check on queue memory */
12455 if (!rq)
12456 return;
James Smart73d91e52011-10-10 21:32:10 -040012457 cnt = lpfc_hbq_defs[qno]->entry_count;
12458
12459 /* Recalc repost for RQs based on buffers initially posted */
12460 cnt = (cnt >> 3);
12461 if (cnt < LPFC_QUEUE_MIN_REPOST)
12462 cnt = LPFC_QUEUE_MIN_REPOST;
12463
12464 rq->entry_repost = cnt;
12465}
12466
12467/**
James Smart4f774512009-05-22 14:52:35 -040012468 * lpfc_rq_create - Create a Receive Queue on the HBA
12469 * @phba: HBA structure that indicates port to create a queue on.
12470 * @hrq: The queue structure to use to create the header receive queue.
12471 * @drq: The queue structure to use to create the data receive queue.
12472 * @cq: The completion queue to bind this work queue to.
12473 *
12474 * This function creates a receive buffer queue pair , as detailed in @hrq and
12475 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12476 * to the HBA.
12477 *
12478 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12479 * struct is used to get the entry count that is necessary to determine the
12480 * number of pages to use for this queue. The @cq is used to indicate which
12481 * completion queue to bind received buffers that are posted to these queues to.
12482 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12483 * receive queue pair. This function is asynchronous and will wait for the
12484 * mailbox command to finish before continuing.
12485 *
12486 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012487 * memory this function will return -ENOMEM. If the queue create mailbox command
12488 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012489 **/
12490uint32_t
12491lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12492 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12493{
12494 struct lpfc_mbx_rq_create *rq_create;
12495 struct lpfc_dmabuf *dmabuf;
12496 LPFC_MBOXQ_t *mbox;
12497 int rc, length, status = 0;
12498 uint32_t shdr_status, shdr_add_status;
12499 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012500 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12501
James Smart2e90f4b2011-12-13 13:22:37 -050012502 /* sanity check on queue memory */
12503 if (!hrq || !drq || !cq)
12504 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012505 if (!phba->sli4_hba.pc_sli4_params.supported)
12506 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012507
12508 if (hrq->entry_count != drq->entry_count)
12509 return -EINVAL;
12510 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12511 if (!mbox)
12512 return -ENOMEM;
12513 length = (sizeof(struct lpfc_mbx_rq_create) -
12514 sizeof(struct lpfc_sli4_cfg_mhdr));
12515 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12516 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12517 length, LPFC_SLI4_MBX_EMBED);
12518 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012519 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12520 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12521 phba->sli4_hba.pc_sli4_params.rqv);
12522 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12523 bf_set(lpfc_rq_context_rqe_count_1,
12524 &rq_create->u.request.context,
12525 hrq->entry_count);
12526 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012527 bf_set(lpfc_rq_context_rqe_size,
12528 &rq_create->u.request.context,
12529 LPFC_RQE_SIZE_8);
12530 bf_set(lpfc_rq_context_page_size,
12531 &rq_create->u.request.context,
12532 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012533 } else {
12534 switch (hrq->entry_count) {
12535 default:
12536 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12537 "2535 Unsupported RQ count. (%d)\n",
12538 hrq->entry_count);
12539 if (hrq->entry_count < 512)
12540 return -EINVAL;
12541 /* otherwise default to smallest count (drop through) */
12542 case 512:
12543 bf_set(lpfc_rq_context_rqe_count,
12544 &rq_create->u.request.context,
12545 LPFC_RQ_RING_SIZE_512);
12546 break;
12547 case 1024:
12548 bf_set(lpfc_rq_context_rqe_count,
12549 &rq_create->u.request.context,
12550 LPFC_RQ_RING_SIZE_1024);
12551 break;
12552 case 2048:
12553 bf_set(lpfc_rq_context_rqe_count,
12554 &rq_create->u.request.context,
12555 LPFC_RQ_RING_SIZE_2048);
12556 break;
12557 case 4096:
12558 bf_set(lpfc_rq_context_rqe_count,
12559 &rq_create->u.request.context,
12560 LPFC_RQ_RING_SIZE_4096);
12561 break;
12562 }
12563 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12564 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012565 }
12566 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12567 cq->queue_id);
12568 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12569 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012570 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012571 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012572 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12573 putPaddrLow(dmabuf->phys);
12574 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12575 putPaddrHigh(dmabuf->phys);
12576 }
12577 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12578 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012579 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12580 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12581 if (shdr_status || shdr_add_status || rc) {
12582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12583 "2504 RQ_CREATE mailbox failed with "
12584 "status x%x add_status x%x, mbx status x%x\n",
12585 shdr_status, shdr_add_status, rc);
12586 status = -ENXIO;
12587 goto out;
12588 }
12589 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12590 if (hrq->queue_id == 0xFFFF) {
12591 status = -ENXIO;
12592 goto out;
12593 }
12594 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012595 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012596 hrq->subtype = subtype;
12597 hrq->host_index = 0;
12598 hrq->hba_index = 0;
12599
12600 /* now create the data queue */
12601 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12602 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12603 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012604 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12605 phba->sli4_hba.pc_sli4_params.rqv);
12606 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12607 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012608 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012609 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012610 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12611 LPFC_RQE_SIZE_8);
12612 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12613 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012614 } else {
12615 switch (drq->entry_count) {
12616 default:
12617 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12618 "2536 Unsupported RQ count. (%d)\n",
12619 drq->entry_count);
12620 if (drq->entry_count < 512)
12621 return -EINVAL;
12622 /* otherwise default to smallest count (drop through) */
12623 case 512:
12624 bf_set(lpfc_rq_context_rqe_count,
12625 &rq_create->u.request.context,
12626 LPFC_RQ_RING_SIZE_512);
12627 break;
12628 case 1024:
12629 bf_set(lpfc_rq_context_rqe_count,
12630 &rq_create->u.request.context,
12631 LPFC_RQ_RING_SIZE_1024);
12632 break;
12633 case 2048:
12634 bf_set(lpfc_rq_context_rqe_count,
12635 &rq_create->u.request.context,
12636 LPFC_RQ_RING_SIZE_2048);
12637 break;
12638 case 4096:
12639 bf_set(lpfc_rq_context_rqe_count,
12640 &rq_create->u.request.context,
12641 LPFC_RQ_RING_SIZE_4096);
12642 break;
12643 }
12644 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12645 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012646 }
12647 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12648 cq->queue_id);
12649 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12650 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012651 list_for_each_entry(dmabuf, &drq->page_list, list) {
12652 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12653 putPaddrLow(dmabuf->phys);
12654 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12655 putPaddrHigh(dmabuf->phys);
12656 }
12657 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12658 /* The IOCTL status is embedded in the mailbox subheader. */
12659 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12660 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12661 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12662 if (shdr_status || shdr_add_status || rc) {
12663 status = -ENXIO;
12664 goto out;
12665 }
12666 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12667 if (drq->queue_id == 0xFFFF) {
12668 status = -ENXIO;
12669 goto out;
12670 }
12671 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012672 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012673 drq->subtype = subtype;
12674 drq->host_index = 0;
12675 drq->hba_index = 0;
12676
12677 /* link the header and data RQs onto the parent cq child list */
12678 list_add_tail(&hrq->list, &cq->child_list);
12679 list_add_tail(&drq->list, &cq->child_list);
12680
12681out:
James Smart8fa38512009-07-19 10:01:03 -040012682 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012683 return status;
12684}
12685
12686/**
12687 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12688 * @eq: The queue structure associated with the queue to destroy.
12689 *
12690 * This function destroys a queue, as detailed in @eq by sending an mailbox
12691 * command, specific to the type of queue, to the HBA.
12692 *
12693 * The @eq struct is used to get the queue ID of the queue to destroy.
12694 *
12695 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012696 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012697 **/
12698uint32_t
12699lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12700{
12701 LPFC_MBOXQ_t *mbox;
12702 int rc, length, status = 0;
12703 uint32_t shdr_status, shdr_add_status;
12704 union lpfc_sli4_cfg_shdr *shdr;
12705
James Smart2e90f4b2011-12-13 13:22:37 -050012706 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012707 if (!eq)
12708 return -ENODEV;
12709 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12710 if (!mbox)
12711 return -ENOMEM;
12712 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12713 sizeof(struct lpfc_sli4_cfg_mhdr));
12714 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12715 LPFC_MBOX_OPCODE_EQ_DESTROY,
12716 length, LPFC_SLI4_MBX_EMBED);
12717 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12718 eq->queue_id);
12719 mbox->vport = eq->phba->pport;
12720 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12721
12722 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12723 /* The IOCTL status is embedded in the mailbox subheader. */
12724 shdr = (union lpfc_sli4_cfg_shdr *)
12725 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12726 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12727 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12728 if (shdr_status || shdr_add_status || rc) {
12729 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12730 "2505 EQ_DESTROY mailbox failed with "
12731 "status x%x add_status x%x, mbx status x%x\n",
12732 shdr_status, shdr_add_status, rc);
12733 status = -ENXIO;
12734 }
12735
12736 /* Remove eq from any list */
12737 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012738 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012739 return status;
12740}
12741
12742/**
12743 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12744 * @cq: The queue structure associated with the queue to destroy.
12745 *
12746 * This function destroys a queue, as detailed in @cq by sending an mailbox
12747 * command, specific to the type of queue, to the HBA.
12748 *
12749 * The @cq struct is used to get the queue ID of the queue to destroy.
12750 *
12751 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012752 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012753 **/
12754uint32_t
12755lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12756{
12757 LPFC_MBOXQ_t *mbox;
12758 int rc, length, status = 0;
12759 uint32_t shdr_status, shdr_add_status;
12760 union lpfc_sli4_cfg_shdr *shdr;
12761
James Smart2e90f4b2011-12-13 13:22:37 -050012762 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012763 if (!cq)
12764 return -ENODEV;
12765 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12766 if (!mbox)
12767 return -ENOMEM;
12768 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12769 sizeof(struct lpfc_sli4_cfg_mhdr));
12770 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12771 LPFC_MBOX_OPCODE_CQ_DESTROY,
12772 length, LPFC_SLI4_MBX_EMBED);
12773 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12774 cq->queue_id);
12775 mbox->vport = cq->phba->pport;
12776 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12777 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12778 /* The IOCTL status is embedded in the mailbox subheader. */
12779 shdr = (union lpfc_sli4_cfg_shdr *)
12780 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12781 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12782 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12783 if (shdr_status || shdr_add_status || rc) {
12784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12785 "2506 CQ_DESTROY mailbox failed with "
12786 "status x%x add_status x%x, mbx status x%x\n",
12787 shdr_status, shdr_add_status, rc);
12788 status = -ENXIO;
12789 }
12790 /* Remove cq from any list */
12791 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012792 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012793 return status;
12794}
12795
12796/**
James Smart04c68492009-05-22 14:52:52 -040012797 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12798 * @qm: The queue structure associated with the queue to destroy.
12799 *
12800 * This function destroys a queue, as detailed in @mq by sending an mailbox
12801 * command, specific to the type of queue, to the HBA.
12802 *
12803 * The @mq struct is used to get the queue ID of the queue to destroy.
12804 *
12805 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012806 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012807 **/
12808uint32_t
12809lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12810{
12811 LPFC_MBOXQ_t *mbox;
12812 int rc, length, status = 0;
12813 uint32_t shdr_status, shdr_add_status;
12814 union lpfc_sli4_cfg_shdr *shdr;
12815
James Smart2e90f4b2011-12-13 13:22:37 -050012816 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040012817 if (!mq)
12818 return -ENODEV;
12819 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12820 if (!mbox)
12821 return -ENOMEM;
12822 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12823 sizeof(struct lpfc_sli4_cfg_mhdr));
12824 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12825 LPFC_MBOX_OPCODE_MQ_DESTROY,
12826 length, LPFC_SLI4_MBX_EMBED);
12827 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12828 mq->queue_id);
12829 mbox->vport = mq->phba->pport;
12830 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12831 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12832 /* The IOCTL status is embedded in the mailbox subheader. */
12833 shdr = (union lpfc_sli4_cfg_shdr *)
12834 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12835 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12836 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12837 if (shdr_status || shdr_add_status || rc) {
12838 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12839 "2507 MQ_DESTROY mailbox failed with "
12840 "status x%x add_status x%x, mbx status x%x\n",
12841 shdr_status, shdr_add_status, rc);
12842 status = -ENXIO;
12843 }
12844 /* Remove mq from any list */
12845 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012846 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012847 return status;
12848}
12849
12850/**
James Smart4f774512009-05-22 14:52:35 -040012851 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12852 * @wq: The queue structure associated with the queue to destroy.
12853 *
12854 * This function destroys a queue, as detailed in @wq by sending an mailbox
12855 * command, specific to the type of queue, to the HBA.
12856 *
12857 * The @wq struct is used to get the queue ID of the queue to destroy.
12858 *
12859 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012860 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012861 **/
12862uint32_t
12863lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12864{
12865 LPFC_MBOXQ_t *mbox;
12866 int rc, length, status = 0;
12867 uint32_t shdr_status, shdr_add_status;
12868 union lpfc_sli4_cfg_shdr *shdr;
12869
James Smart2e90f4b2011-12-13 13:22:37 -050012870 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012871 if (!wq)
12872 return -ENODEV;
12873 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12874 if (!mbox)
12875 return -ENOMEM;
12876 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12877 sizeof(struct lpfc_sli4_cfg_mhdr));
12878 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12879 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12880 length, LPFC_SLI4_MBX_EMBED);
12881 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12882 wq->queue_id);
12883 mbox->vport = wq->phba->pport;
12884 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12885 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12886 shdr = (union lpfc_sli4_cfg_shdr *)
12887 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12888 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12889 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12890 if (shdr_status || shdr_add_status || rc) {
12891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12892 "2508 WQ_DESTROY mailbox failed with "
12893 "status x%x add_status x%x, mbx status x%x\n",
12894 shdr_status, shdr_add_status, rc);
12895 status = -ENXIO;
12896 }
12897 /* Remove wq from any list */
12898 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012899 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012900 return status;
12901}
12902
12903/**
12904 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12905 * @rq: The queue structure associated with the queue to destroy.
12906 *
12907 * This function destroys a queue, as detailed in @rq by sending an mailbox
12908 * command, specific to the type of queue, to the HBA.
12909 *
12910 * The @rq struct is used to get the queue ID of the queue to destroy.
12911 *
12912 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012913 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012914 **/
12915uint32_t
12916lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12917 struct lpfc_queue *drq)
12918{
12919 LPFC_MBOXQ_t *mbox;
12920 int rc, length, status = 0;
12921 uint32_t shdr_status, shdr_add_status;
12922 union lpfc_sli4_cfg_shdr *shdr;
12923
James Smart2e90f4b2011-12-13 13:22:37 -050012924 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012925 if (!hrq || !drq)
12926 return -ENODEV;
12927 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12928 if (!mbox)
12929 return -ENOMEM;
12930 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012931 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012932 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12933 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12934 length, LPFC_SLI4_MBX_EMBED);
12935 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12936 hrq->queue_id);
12937 mbox->vport = hrq->phba->pport;
12938 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12939 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12940 /* The IOCTL status is embedded in the mailbox subheader. */
12941 shdr = (union lpfc_sli4_cfg_shdr *)
12942 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12943 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12944 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12945 if (shdr_status || shdr_add_status || rc) {
12946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12947 "2509 RQ_DESTROY mailbox failed with "
12948 "status x%x add_status x%x, mbx status x%x\n",
12949 shdr_status, shdr_add_status, rc);
12950 if (rc != MBX_TIMEOUT)
12951 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12952 return -ENXIO;
12953 }
12954 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12955 drq->queue_id);
12956 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12957 shdr = (union lpfc_sli4_cfg_shdr *)
12958 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12959 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12960 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12961 if (shdr_status || shdr_add_status || rc) {
12962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12963 "2510 RQ_DESTROY mailbox failed with "
12964 "status x%x add_status x%x, mbx status x%x\n",
12965 shdr_status, shdr_add_status, rc);
12966 status = -ENXIO;
12967 }
12968 list_del_init(&hrq->list);
12969 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012970 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012971 return status;
12972}
12973
12974/**
12975 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12976 * @phba: The virtual port for which this call being executed.
12977 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12978 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12979 * @xritag: the xritag that ties this io to the SGL pages.
12980 *
12981 * This routine will post the sgl pages for the IO that has the xritag
12982 * that is in the iocbq structure. The xritag is assigned during iocbq
12983 * creation and persists for as long as the driver is loaded.
12984 * if the caller has fewer than 256 scatter gather segments to map then
12985 * pdma_phys_addr1 should be 0.
12986 * If the caller needs to map more than 256 scatter gather segment then
12987 * pdma_phys_addr1 should be a valid physical address.
12988 * physical address for SGLs must be 64 byte aligned.
12989 * If you are going to map 2 SGL's then the first one must have 256 entries
12990 * the second sgl can have between 1 and 256 entries.
12991 *
12992 * Return codes:
12993 * 0 - Success
12994 * -ENXIO, -ENOMEM - Failure
12995 **/
12996int
12997lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12998 dma_addr_t pdma_phys_addr0,
12999 dma_addr_t pdma_phys_addr1,
13000 uint16_t xritag)
13001{
13002 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13003 LPFC_MBOXQ_t *mbox;
13004 int rc;
13005 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013006 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013007 union lpfc_sli4_cfg_shdr *shdr;
13008
13009 if (xritag == NO_XRI) {
13010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13011 "0364 Invalid param:\n");
13012 return -EINVAL;
13013 }
13014
13015 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13016 if (!mbox)
13017 return -ENOMEM;
13018
13019 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13020 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13021 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013022 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013023
13024 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13025 &mbox->u.mqe.un.post_sgl_pages;
13026 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13027 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13028
13029 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13030 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13031 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13032 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13033
13034 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13035 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13036 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13037 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13038 if (!phba->sli4_hba.intr_enable)
13039 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013040 else {
James Smarta183a152011-10-10 21:32:43 -040013041 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013042 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13043 }
James Smart4f774512009-05-22 14:52:35 -040013044 /* The IOCTL status is embedded in the mailbox subheader. */
13045 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13046 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13047 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13048 if (rc != MBX_TIMEOUT)
13049 mempool_free(mbox, phba->mbox_mem_pool);
13050 if (shdr_status || shdr_add_status || rc) {
13051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13052 "2511 POST_SGL mailbox failed with "
13053 "status x%x add_status x%x, mbx status x%x\n",
13054 shdr_status, shdr_add_status, rc);
13055 rc = -ENXIO;
13056 }
13057 return 0;
13058}
James Smart4f774512009-05-22 14:52:35 -040013059
13060/**
James Smart88a2cfb2011-07-22 18:36:33 -040013061 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013062 * @phba: pointer to lpfc hba data structure.
13063 *
13064 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013065 * HBA consistent with the SLI-4 interface spec. This routine
13066 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13067 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013068 *
James Smart88a2cfb2011-07-22 18:36:33 -040013069 * Returns
13070 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13071 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13072 **/
James Smart6d368e52011-05-24 11:44:12 -040013073uint16_t
13074lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13075{
13076 unsigned long xri;
13077
13078 /*
13079 * Fetch the next logical xri. Because this index is logical,
13080 * the driver starts at 0 each time.
13081 */
13082 spin_lock_irq(&phba->hbalock);
13083 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13084 phba->sli4_hba.max_cfg_param.max_xri, 0);
13085 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13086 spin_unlock_irq(&phba->hbalock);
13087 return NO_XRI;
13088 } else {
13089 set_bit(xri, phba->sli4_hba.xri_bmask);
13090 phba->sli4_hba.max_cfg_param.xri_used++;
13091 phba->sli4_hba.xri_count++;
13092 }
13093
13094 spin_unlock_irq(&phba->hbalock);
13095 return xri;
13096}
13097
13098/**
13099 * lpfc_sli4_free_xri - Release an xri for reuse.
13100 * @phba: pointer to lpfc hba data structure.
13101 *
13102 * This routine is invoked to release an xri to the pool of
13103 * available rpis maintained by the driver.
13104 **/
13105void
13106__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13107{
13108 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
13109 phba->sli4_hba.xri_count--;
13110 phba->sli4_hba.max_cfg_param.xri_used--;
13111 }
13112}
13113
13114/**
13115 * lpfc_sli4_free_xri - Release an xri for reuse.
13116 * @phba: pointer to lpfc hba data structure.
13117 *
13118 * This routine is invoked to release an xri to the pool of
13119 * available rpis maintained by the driver.
13120 **/
13121void
13122lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13123{
13124 spin_lock_irq(&phba->hbalock);
13125 __lpfc_sli4_free_xri(phba, xri);
13126 spin_unlock_irq(&phba->hbalock);
13127}
13128
13129/**
James Smart4f774512009-05-22 14:52:35 -040013130 * lpfc_sli4_next_xritag - Get an xritag for the io
13131 * @phba: Pointer to HBA context object.
13132 *
13133 * This function gets an xritag for the iocb. If there is no unused xritag
13134 * it will return 0xffff.
13135 * The function returns the allocated xritag if successful, else returns zero.
13136 * Zero is not a valid xritag.
13137 * The caller is not required to hold any lock.
13138 **/
13139uint16_t
13140lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13141{
James Smart6d368e52011-05-24 11:44:12 -040013142 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013143
James Smart6d368e52011-05-24 11:44:12 -040013144 xri_index = lpfc_sli4_alloc_xri(phba);
13145 if (xri_index != NO_XRI)
13146 return xri_index;
13147
13148 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040013149 "2004 Failed to allocate XRI.last XRITAG is %d"
13150 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040013151 xri_index,
James Smart4f774512009-05-22 14:52:35 -040013152 phba->sli4_hba.max_cfg_param.max_xri,
13153 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040013154 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040013155}
13156
13157/**
James Smart6d368e52011-05-24 11:44:12 -040013158 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013159 * @phba: pointer to lpfc hba data structure.
13160 *
13161 * This routine is invoked to post a block of driver's sgl pages to the
13162 * HBA using non-embedded mailbox command. No Lock is held. This routine
13163 * is only called when the driver is loading and after all IO has been
13164 * stopped.
13165 **/
13166int
James Smart6d368e52011-05-24 11:44:12 -040013167lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040013168{
13169 struct lpfc_sglq *sglq_entry;
13170 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13171 struct sgl_page_pairs *sgl_pg_pairs;
13172 void *viraddr;
13173 LPFC_MBOXQ_t *mbox;
13174 uint32_t reqlen, alloclen, pg_pairs;
13175 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040013176 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040013177 int els_xri_cnt, rc = 0;
13178 uint32_t shdr_status, shdr_add_status;
13179 union lpfc_sli4_cfg_shdr *shdr;
13180
13181 /* The number of sgls to be posted */
13182 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13183
13184 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13185 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013186 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013187 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13188 "2559 Block sgl registration required DMA "
13189 "size (%d) great than a page\n", reqlen);
13190 return -ENOMEM;
13191 }
13192 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013193 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013194 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013195
13196 /* Allocate DMA memory and set up the non-embedded mailbox command */
13197 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13198 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13199 LPFC_SLI4_MBX_NEMBED);
13200
13201 if (alloclen < reqlen) {
13202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13203 "0285 Allocated DMA memory size (%d) is "
13204 "less than the requested DMA memory "
13205 "size (%d)\n", alloclen, reqlen);
13206 lpfc_sli4_mbox_cmd_free(phba, mbox);
13207 return -ENOMEM;
13208 }
James Smart4f774512009-05-22 14:52:35 -040013209 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013210 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013211 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13212 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13213
13214 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
13215 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040013216
13217 /*
13218 * Assign the sglq a physical xri only if the driver has not
13219 * initialized those resources. A port reset only needs
13220 * the sglq's posted.
13221 */
13222 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
13223 LPFC_XRI_RSRC_RDY) {
13224 lxri = lpfc_sli4_next_xritag(phba);
13225 if (lxri == NO_XRI) {
13226 lpfc_sli4_mbox_cmd_free(phba, mbox);
13227 return -ENOMEM;
13228 }
13229 sglq_entry->sli4_lxritag = lxri;
13230 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
13231 }
13232
James Smart4f774512009-05-22 14:52:35 -040013233 /* Set up the sge entry */
13234 sgl_pg_pairs->sgl_pg0_addr_lo =
13235 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13236 sgl_pg_pairs->sgl_pg0_addr_hi =
13237 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13238 sgl_pg_pairs->sgl_pg1_addr_lo =
13239 cpu_to_le32(putPaddrLow(0));
13240 sgl_pg_pairs->sgl_pg1_addr_hi =
13241 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013242
James Smart4f774512009-05-22 14:52:35 -040013243 /* Keep the first xritag on the list */
13244 if (pg_pairs == 0)
13245 xritag_start = sglq_entry->sli4_xritag;
13246 sgl_pg_pairs++;
13247 }
James Smart6d368e52011-05-24 11:44:12 -040013248
13249 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013250 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040013251 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013252 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013253 if (!phba->sli4_hba.intr_enable)
13254 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13255 else {
James Smarta183a152011-10-10 21:32:43 -040013256 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013257 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13258 }
13259 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13260 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13261 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13262 if (rc != MBX_TIMEOUT)
13263 lpfc_sli4_mbox_cmd_free(phba, mbox);
13264 if (shdr_status || shdr_add_status || rc) {
13265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13266 "2513 POST_SGL_BLOCK mailbox command failed "
13267 "status x%x add_status x%x mbx status x%x\n",
13268 shdr_status, shdr_add_status, rc);
13269 rc = -ENXIO;
13270 }
James Smart6d368e52011-05-24 11:44:12 -040013271
13272 if (rc == 0)
13273 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13274 LPFC_XRI_RSRC_RDY);
13275 return rc;
13276}
13277
13278/**
13279 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
13280 * @phba: pointer to lpfc hba data structure.
13281 *
13282 * This routine is invoked to post a block of driver's sgl pages to the
13283 * HBA using non-embedded mailbox command. No Lock is held. This routine
13284 * is only called when the driver is loading and after all IO has been
13285 * stopped.
13286 **/
13287int
13288lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13289{
13290 struct lpfc_sglq *sglq_entry;
13291 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13292 struct sgl_page_pairs *sgl_pg_pairs;
13293 void *viraddr;
13294 LPFC_MBOXQ_t *mbox;
13295 uint32_t reqlen, alloclen, index;
13296 uint32_t mbox_tmo;
13297 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13298 uint16_t xritag_start = 0, lxri = 0;
13299 struct lpfc_rsrc_blks *rsrc_blk;
13300 int cnt, ttl_cnt, rc = 0;
13301 int loop_cnt;
13302 uint32_t shdr_status, shdr_add_status;
13303 union lpfc_sli4_cfg_shdr *shdr;
13304
13305 /* The number of sgls to be posted */
13306 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13307
13308 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13309 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13310 if (reqlen > SLI4_PAGE_SIZE) {
13311 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13312 "2989 Block sgl registration required DMA "
13313 "size (%d) great than a page\n", reqlen);
13314 return -ENOMEM;
13315 }
13316
13317 cnt = 0;
13318 ttl_cnt = 0;
13319 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13320 list) {
13321 rsrc_start = rsrc_blk->rsrc_start;
13322 rsrc_size = rsrc_blk->rsrc_size;
13323
13324 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13325 "3014 Working ELS Extent start %d, cnt %d\n",
13326 rsrc_start, rsrc_size);
13327
13328 loop_cnt = min(els_xri_cnt, rsrc_size);
13329 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13330 loop_cnt = els_xri_cnt - ttl_cnt;
13331 ttl_cnt = els_xri_cnt;
13332 }
13333
13334 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13335 if (!mbox)
13336 return -ENOMEM;
13337 /*
13338 * Allocate DMA memory and set up the non-embedded mailbox
13339 * command.
13340 */
13341 alloclen = lpfc_sli4_config(phba, mbox,
13342 LPFC_MBOX_SUBSYSTEM_FCOE,
13343 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13344 reqlen, LPFC_SLI4_MBX_NEMBED);
13345 if (alloclen < reqlen) {
13346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13347 "2987 Allocated DMA memory size (%d) "
13348 "is less than the requested DMA memory "
13349 "size (%d)\n", alloclen, reqlen);
13350 lpfc_sli4_mbox_cmd_free(phba, mbox);
13351 return -ENOMEM;
13352 }
13353
13354 /* Set up the SGL pages in the non-embedded DMA pages */
13355 viraddr = mbox->sge_array->addr[0];
13356 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13357 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13358
13359 /*
13360 * The starting resource may not begin at zero. Control
13361 * the loop variants via the block resource parameters,
13362 * but handle the sge pointers with a zero-based index
13363 * that doesn't get reset per loop pass.
13364 */
13365 for (index = rsrc_start;
13366 index < rsrc_start + loop_cnt;
13367 index++) {
13368 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13369
13370 /*
13371 * Assign the sglq a physical xri only if the driver
13372 * has not initialized those resources. A port reset
13373 * only needs the sglq's posted.
13374 */
13375 if (bf_get(lpfc_xri_rsrc_rdy,
13376 &phba->sli4_hba.sli4_flags) !=
13377 LPFC_XRI_RSRC_RDY) {
13378 lxri = lpfc_sli4_next_xritag(phba);
13379 if (lxri == NO_XRI) {
13380 lpfc_sli4_mbox_cmd_free(phba, mbox);
13381 rc = -ENOMEM;
13382 goto err_exit;
13383 }
13384 sglq_entry->sli4_lxritag = lxri;
13385 sglq_entry->sli4_xritag =
13386 phba->sli4_hba.xri_ids[lxri];
13387 }
13388
13389 /* Set up the sge entry */
13390 sgl_pg_pairs->sgl_pg0_addr_lo =
13391 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13392 sgl_pg_pairs->sgl_pg0_addr_hi =
13393 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13394 sgl_pg_pairs->sgl_pg1_addr_lo =
13395 cpu_to_le32(putPaddrLow(0));
13396 sgl_pg_pairs->sgl_pg1_addr_hi =
13397 cpu_to_le32(putPaddrHigh(0));
13398
13399 /* Track the starting physical XRI for the mailbox. */
13400 if (index == rsrc_start)
13401 xritag_start = sglq_entry->sli4_xritag;
13402 sgl_pg_pairs++;
13403 cnt++;
13404 }
13405
13406 /* Complete initialization and perform endian conversion. */
13407 rsrc_blk->rsrc_used += loop_cnt;
13408 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13409 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13410 sgl->word0 = cpu_to_le32(sgl->word0);
13411
13412 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13413 "3015 Post ELS Extent SGL, start %d, "
13414 "cnt %d, used %d\n",
13415 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13416 if (!phba->sli4_hba.intr_enable)
13417 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13418 else {
James Smarta183a152011-10-10 21:32:43 -040013419 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013420 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13421 }
13422 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13423 shdr_status = bf_get(lpfc_mbox_hdr_status,
13424 &shdr->response);
13425 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13426 &shdr->response);
13427 if (rc != MBX_TIMEOUT)
13428 lpfc_sli4_mbox_cmd_free(phba, mbox);
13429 if (shdr_status || shdr_add_status || rc) {
13430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13431 "2988 POST_SGL_BLOCK mailbox "
13432 "command failed status x%x "
13433 "add_status x%x mbx status x%x\n",
13434 shdr_status, shdr_add_status, rc);
13435 rc = -ENXIO;
13436 goto err_exit;
13437 }
13438 if (ttl_cnt >= els_xri_cnt)
13439 break;
13440 }
13441
13442 err_exit:
13443 if (rc == 0)
13444 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13445 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040013446 return rc;
13447}
13448
13449/**
13450 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13451 * @phba: pointer to lpfc hba data structure.
13452 * @sblist: pointer to scsi buffer list.
13453 * @count: number of scsi buffers on the list.
13454 *
13455 * This routine is invoked to post a block of @count scsi sgl pages from a
13456 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13457 * No Lock is held.
13458 *
13459 **/
13460int
13461lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13462 int cnt)
13463{
13464 struct lpfc_scsi_buf *psb;
13465 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13466 struct sgl_page_pairs *sgl_pg_pairs;
13467 void *viraddr;
13468 LPFC_MBOXQ_t *mbox;
13469 uint32_t reqlen, alloclen, pg_pairs;
13470 uint32_t mbox_tmo;
13471 uint16_t xritag_start = 0;
13472 int rc = 0;
13473 uint32_t shdr_status, shdr_add_status;
13474 dma_addr_t pdma_phys_bpl1;
13475 union lpfc_sli4_cfg_shdr *shdr;
13476
13477 /* Calculate the requested length of the dma memory */
13478 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13479 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013480 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013481 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13482 "0217 Block sgl registration required DMA "
13483 "size (%d) great than a page\n", reqlen);
13484 return -ENOMEM;
13485 }
13486 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13487 if (!mbox) {
13488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13489 "0283 Failed to allocate mbox cmd memory\n");
13490 return -ENOMEM;
13491 }
13492
13493 /* Allocate DMA memory and set up the non-embedded mailbox command */
13494 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13495 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13496 LPFC_SLI4_MBX_NEMBED);
13497
13498 if (alloclen < reqlen) {
13499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13500 "2561 Allocated DMA memory size (%d) is "
13501 "less than the requested DMA memory "
13502 "size (%d)\n", alloclen, reqlen);
13503 lpfc_sli4_mbox_cmd_free(phba, mbox);
13504 return -ENOMEM;
13505 }
James Smart6d368e52011-05-24 11:44:12 -040013506
James Smart4f774512009-05-22 14:52:35 -040013507 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013508 viraddr = mbox->sge_array->addr[0];
13509
13510 /* Set up the SGL pages in the non-embedded DMA pages */
13511 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13512 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13513
13514 pg_pairs = 0;
13515 list_for_each_entry(psb, sblist, list) {
13516 /* Set up the sge entry */
13517 sgl_pg_pairs->sgl_pg0_addr_lo =
13518 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13519 sgl_pg_pairs->sgl_pg0_addr_hi =
13520 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13521 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13522 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13523 else
13524 pdma_phys_bpl1 = 0;
13525 sgl_pg_pairs->sgl_pg1_addr_lo =
13526 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13527 sgl_pg_pairs->sgl_pg1_addr_hi =
13528 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13529 /* Keep the first xritag on the list */
13530 if (pg_pairs == 0)
13531 xritag_start = psb->cur_iocbq.sli4_xritag;
13532 sgl_pg_pairs++;
13533 pg_pairs++;
13534 }
13535 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13536 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13537 /* Perform endian conversion if necessary */
13538 sgl->word0 = cpu_to_le32(sgl->word0);
13539
13540 if (!phba->sli4_hba.intr_enable)
13541 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13542 else {
James Smarta183a152011-10-10 21:32:43 -040013543 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013544 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13545 }
13546 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13547 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13548 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13549 if (rc != MBX_TIMEOUT)
13550 lpfc_sli4_mbox_cmd_free(phba, mbox);
13551 if (shdr_status || shdr_add_status || rc) {
13552 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13553 "2564 POST_SGL_BLOCK mailbox command failed "
13554 "status x%x add_status x%x mbx status x%x\n",
13555 shdr_status, shdr_add_status, rc);
13556 rc = -ENXIO;
13557 }
13558 return rc;
13559}
13560
13561/**
James Smart6d368e52011-05-24 11:44:12 -040013562 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13563 * @phba: pointer to lpfc hba data structure.
13564 * @sblist: pointer to scsi buffer list.
13565 * @count: number of scsi buffers on the list.
13566 *
13567 * This routine is invoked to post a block of @count scsi sgl pages from a
13568 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13569 * No Lock is held.
13570 *
13571 **/
13572int
13573lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13574 int cnt)
13575{
13576 struct lpfc_scsi_buf *psb = NULL;
13577 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13578 struct sgl_page_pairs *sgl_pg_pairs;
13579 void *viraddr;
13580 LPFC_MBOXQ_t *mbox;
13581 uint32_t reqlen, alloclen, pg_pairs;
13582 uint32_t mbox_tmo;
13583 uint16_t xri_start = 0, scsi_xri_start;
13584 uint16_t rsrc_range;
13585 int rc = 0, avail_cnt;
13586 uint32_t shdr_status, shdr_add_status;
13587 dma_addr_t pdma_phys_bpl1;
13588 union lpfc_sli4_cfg_shdr *shdr;
13589 struct lpfc_rsrc_blks *rsrc_blk;
13590 uint32_t xri_cnt = 0;
13591
13592 /* Calculate the total requested length of the dma memory */
13593 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13594 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13595 if (reqlen > SLI4_PAGE_SIZE) {
13596 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13597 "2932 Block sgl registration required DMA "
13598 "size (%d) great than a page\n", reqlen);
13599 return -ENOMEM;
13600 }
13601
13602 /*
13603 * The use of extents requires the driver to post the sgl headers
13604 * in multiple postings to meet the contiguous resource assignment.
13605 */
13606 psb = list_prepare_entry(psb, sblist, list);
13607 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13608 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13609 list) {
13610 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13611 if (rsrc_range < scsi_xri_start)
13612 continue;
13613 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13614 continue;
13615 else
13616 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13617
13618 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13619 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13620 /*
13621 * Allocate DMA memory and set up the non-embedded mailbox
13622 * command. The mbox is used to post an SGL page per loop
13623 * but the DMA memory has a use-once semantic so the mailbox
13624 * is used and freed per loop pass.
13625 */
13626 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13627 if (!mbox) {
13628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13629 "2933 Failed to allocate mbox cmd "
13630 "memory\n");
13631 return -ENOMEM;
13632 }
13633 alloclen = lpfc_sli4_config(phba, mbox,
13634 LPFC_MBOX_SUBSYSTEM_FCOE,
13635 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13636 reqlen,
13637 LPFC_SLI4_MBX_NEMBED);
13638 if (alloclen < reqlen) {
13639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13640 "2934 Allocated DMA memory size (%d) "
13641 "is less than the requested DMA memory "
13642 "size (%d)\n", alloclen, reqlen);
13643 lpfc_sli4_mbox_cmd_free(phba, mbox);
13644 return -ENOMEM;
13645 }
13646
13647 /* Get the first SGE entry from the non-embedded DMA memory */
13648 viraddr = mbox->sge_array->addr[0];
13649
13650 /* Set up the SGL pages in the non-embedded DMA pages */
13651 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13652 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13653
13654 /* pg_pairs tracks posted SGEs per loop iteration. */
13655 pg_pairs = 0;
13656 list_for_each_entry_continue(psb, sblist, list) {
13657 /* Set up the sge entry */
13658 sgl_pg_pairs->sgl_pg0_addr_lo =
13659 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13660 sgl_pg_pairs->sgl_pg0_addr_hi =
13661 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13662 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13663 pdma_phys_bpl1 = psb->dma_phys_bpl +
13664 SGL_PAGE_SIZE;
13665 else
13666 pdma_phys_bpl1 = 0;
13667 sgl_pg_pairs->sgl_pg1_addr_lo =
13668 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13669 sgl_pg_pairs->sgl_pg1_addr_hi =
13670 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13671 /* Keep the first xri for this extent. */
13672 if (pg_pairs == 0)
13673 xri_start = psb->cur_iocbq.sli4_xritag;
13674 sgl_pg_pairs++;
13675 pg_pairs++;
13676 xri_cnt++;
13677
13678 /*
13679 * Track two exit conditions - the loop has constructed
13680 * all of the caller's SGE pairs or all available
13681 * resource IDs in this extent are consumed.
13682 */
13683 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13684 break;
13685 }
13686 rsrc_blk->rsrc_used += pg_pairs;
13687 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13688 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13689
13690 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13691 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13692 "blk use %d\n",
13693 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13694 /* Perform endian conversion if necessary */
13695 sgl->word0 = cpu_to_le32(sgl->word0);
13696 if (!phba->sli4_hba.intr_enable)
13697 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13698 else {
James Smarta183a152011-10-10 21:32:43 -040013699 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013700 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13701 }
13702 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13703 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13704 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13705 &shdr->response);
13706 if (rc != MBX_TIMEOUT)
13707 lpfc_sli4_mbox_cmd_free(phba, mbox);
13708 if (shdr_status || shdr_add_status || rc) {
13709 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13710 "2935 POST_SGL_BLOCK mailbox command "
13711 "failed status x%x add_status x%x "
13712 "mbx status x%x\n",
13713 shdr_status, shdr_add_status, rc);
13714 return -ENXIO;
13715 }
13716
13717 /* Post only what is requested. */
13718 if (xri_cnt >= cnt)
13719 break;
13720 }
13721 return rc;
13722}
13723
13724/**
James Smart4f774512009-05-22 14:52:35 -040013725 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13726 * @phba: pointer to lpfc_hba struct that the frame was received on
13727 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13728 *
13729 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13730 * valid type of frame that the LPFC driver will handle. This function will
13731 * return a zero if the frame is a valid frame or a non zero value when the
13732 * frame does not pass the check.
13733 **/
13734static int
13735lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13736{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013737 /* make rctl_names static to save stack space */
13738 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013739 char *type_names[] = FC_TYPE_NAMES_INIT;
13740 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013741 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013742
13743 switch (fc_hdr->fh_r_ctl) {
13744 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13745 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13746 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13747 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13748 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13749 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13750 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13751 case FC_RCTL_DD_CMD_STATUS: /* command status */
13752 case FC_RCTL_ELS_REQ: /* extended link services request */
13753 case FC_RCTL_ELS_REP: /* extended link services reply */
13754 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13755 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13756 case FC_RCTL_BA_NOP: /* basic link service NOP */
13757 case FC_RCTL_BA_ABTS: /* basic link service abort */
13758 case FC_RCTL_BA_RMC: /* remove connection */
13759 case FC_RCTL_BA_ACC: /* basic accept */
13760 case FC_RCTL_BA_RJT: /* basic reject */
13761 case FC_RCTL_BA_PRMT:
13762 case FC_RCTL_ACK_1: /* acknowledge_1 */
13763 case FC_RCTL_ACK_0: /* acknowledge_0 */
13764 case FC_RCTL_P_RJT: /* port reject */
13765 case FC_RCTL_F_RJT: /* fabric reject */
13766 case FC_RCTL_P_BSY: /* port busy */
13767 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13768 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13769 case FC_RCTL_LCR: /* link credit reset */
13770 case FC_RCTL_END: /* end */
13771 break;
13772 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13773 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13774 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13775 return lpfc_fc_frame_check(phba, fc_hdr);
13776 default:
13777 goto drop;
13778 }
13779 switch (fc_hdr->fh_type) {
13780 case FC_TYPE_BLS:
13781 case FC_TYPE_ELS:
13782 case FC_TYPE_FCP:
13783 case FC_TYPE_CT:
13784 break;
13785 case FC_TYPE_IP:
13786 case FC_TYPE_ILS:
13787 default:
13788 goto drop;
13789 }
James Smart546fc852011-03-11 16:06:29 -050013790
James Smart4f774512009-05-22 14:52:35 -040013791 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013792 "2538 Received frame rctl:%s type:%s "
13793 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013794 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013795 type_names[fc_hdr->fh_type],
13796 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13797 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13798 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013799 return 0;
13800drop:
13801 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13802 "2539 Dropped frame rctl:%s type:%s\n",
13803 rctl_names[fc_hdr->fh_r_ctl],
13804 type_names[fc_hdr->fh_type]);
13805 return 1;
13806}
13807
13808/**
13809 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13810 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13811 *
13812 * This function processes the FC header to retrieve the VFI from the VF
13813 * header, if one exists. This function will return the VFI if one exists
13814 * or 0 if no VSAN Header exists.
13815 **/
13816static uint32_t
13817lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13818{
13819 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13820
13821 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13822 return 0;
13823 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13824}
13825
13826/**
13827 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13828 * @phba: Pointer to the HBA structure to search for the vport on
13829 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13830 * @fcfi: The FC Fabric ID that the frame came from
13831 *
13832 * This function searches the @phba for a vport that matches the content of the
13833 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13834 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13835 * returns the matching vport pointer or NULL if unable to match frame to a
13836 * vport.
13837 **/
13838static struct lpfc_vport *
13839lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13840 uint16_t fcfi)
13841{
13842 struct lpfc_vport **vports;
13843 struct lpfc_vport *vport = NULL;
13844 int i;
13845 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13846 fc_hdr->fh_d_id[1] << 8 |
13847 fc_hdr->fh_d_id[2]);
James Smartbf086112011-08-21 21:48:13 -040013848 if (did == Fabric_DID)
13849 return phba->pport;
James Smart4f774512009-05-22 14:52:35 -040013850 vports = lpfc_create_vport_work_array(phba);
13851 if (vports != NULL)
13852 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13853 if (phba->fcf.fcfi == fcfi &&
13854 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13855 vports[i]->fc_myDID == did) {
13856 vport = vports[i];
13857 break;
13858 }
13859 }
13860 lpfc_destroy_vport_work_array(phba, vports);
13861 return vport;
13862}
13863
13864/**
James Smart45ed1192009-10-02 15:17:02 -040013865 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13866 * @vport: The vport to work on.
13867 *
13868 * This function updates the receive sequence time stamp for this vport. The
13869 * receive sequence time stamp indicates the time that the last frame of the
13870 * the sequence that has been idle for the longest amount of time was received.
13871 * the driver uses this time stamp to indicate if any received sequences have
13872 * timed out.
13873 **/
13874void
13875lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13876{
13877 struct lpfc_dmabuf *h_buf;
13878 struct hbq_dmabuf *dmabuf = NULL;
13879
13880 /* get the oldest sequence on the rcv list */
13881 h_buf = list_get_first(&vport->rcv_buffer_list,
13882 struct lpfc_dmabuf, list);
13883 if (!h_buf)
13884 return;
13885 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13886 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13887}
13888
13889/**
13890 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13891 * @vport: The vport that the received sequences were sent to.
13892 *
13893 * This function cleans up all outstanding received sequences. This is called
13894 * by the driver when a link event or user action invalidates all the received
13895 * sequences.
13896 **/
13897void
13898lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13899{
13900 struct lpfc_dmabuf *h_buf, *hnext;
13901 struct lpfc_dmabuf *d_buf, *dnext;
13902 struct hbq_dmabuf *dmabuf = NULL;
13903
13904 /* start with the oldest sequence on the rcv list */
13905 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13906 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13907 list_del_init(&dmabuf->hbuf.list);
13908 list_for_each_entry_safe(d_buf, dnext,
13909 &dmabuf->dbuf.list, list) {
13910 list_del_init(&d_buf->list);
13911 lpfc_in_buf_free(vport->phba, d_buf);
13912 }
13913 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13914 }
13915}
13916
13917/**
13918 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13919 * @vport: The vport that the received sequences were sent to.
13920 *
13921 * This function determines whether any received sequences have timed out by
13922 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13923 * indicates that there is at least one timed out sequence this routine will
13924 * go through the received sequences one at a time from most inactive to most
13925 * active to determine which ones need to be cleaned up. Once it has determined
13926 * that a sequence needs to be cleaned up it will simply free up the resources
13927 * without sending an abort.
13928 **/
13929void
13930lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13931{
13932 struct lpfc_dmabuf *h_buf, *hnext;
13933 struct lpfc_dmabuf *d_buf, *dnext;
13934 struct hbq_dmabuf *dmabuf = NULL;
13935 unsigned long timeout;
13936 int abort_count = 0;
13937
13938 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13939 vport->rcv_buffer_time_stamp);
13940 if (list_empty(&vport->rcv_buffer_list) ||
13941 time_before(jiffies, timeout))
13942 return;
13943 /* start with the oldest sequence on the rcv list */
13944 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13945 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13946 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13947 dmabuf->time_stamp);
13948 if (time_before(jiffies, timeout))
13949 break;
13950 abort_count++;
13951 list_del_init(&dmabuf->hbuf.list);
13952 list_for_each_entry_safe(d_buf, dnext,
13953 &dmabuf->dbuf.list, list) {
13954 list_del_init(&d_buf->list);
13955 lpfc_in_buf_free(vport->phba, d_buf);
13956 }
13957 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13958 }
13959 if (abort_count)
13960 lpfc_update_rcv_time_stamp(vport);
13961}
13962
13963/**
James Smart4f774512009-05-22 14:52:35 -040013964 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13965 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13966 *
13967 * This function searches through the existing incomplete sequences that have
13968 * been sent to this @vport. If the frame matches one of the incomplete
13969 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13970 * make up that sequence. If no sequence is found that matches this frame then
13971 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13972 * This function returns a pointer to the first dmabuf in the sequence list that
13973 * the frame was linked to.
13974 **/
13975static struct hbq_dmabuf *
13976lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13977{
13978 struct fc_frame_header *new_hdr;
13979 struct fc_frame_header *temp_hdr;
13980 struct lpfc_dmabuf *d_buf;
13981 struct lpfc_dmabuf *h_buf;
13982 struct hbq_dmabuf *seq_dmabuf = NULL;
13983 struct hbq_dmabuf *temp_dmabuf = NULL;
13984
James Smart4d9ab992009-10-02 15:16:39 -040013985 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013986 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013987 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13988 /* Use the hdr_buf to find the sequence that this frame belongs to */
13989 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13990 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13991 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13992 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13993 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13994 continue;
13995 /* found a pending sequence that matches this frame */
13996 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13997 break;
13998 }
13999 if (!seq_dmabuf) {
14000 /*
14001 * This indicates first frame received for this sequence.
14002 * Queue the buffer on the vport's rcv_buffer_list.
14003 */
14004 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040014005 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014006 return dmabuf;
14007 }
14008 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050014009 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14010 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040014011 list_del_init(&seq_dmabuf->hbuf.list);
14012 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14013 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014014 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014015 return dmabuf;
14016 }
James Smart45ed1192009-10-02 15:17:02 -040014017 /* move this sequence to the tail to indicate a young sequence */
14018 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14019 seq_dmabuf->time_stamp = jiffies;
14020 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014021 if (list_empty(&seq_dmabuf->dbuf.list)) {
14022 temp_hdr = dmabuf->hbuf.virt;
14023 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14024 return seq_dmabuf;
14025 }
James Smart4f774512009-05-22 14:52:35 -040014026 /* find the correct place in the sequence to insert this frame */
14027 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14028 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14029 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14030 /*
14031 * If the frame's sequence count is greater than the frame on
14032 * the list then insert the frame right after this frame
14033 */
James Smarteeead812009-12-21 17:01:23 -050014034 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14035 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014036 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14037 return seq_dmabuf;
14038 }
14039 }
14040 return NULL;
14041}
14042
14043/**
James Smart6669f9b2009-10-02 15:16:45 -040014044 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14045 * @vport: pointer to a vitural port
14046 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14047 *
14048 * This function tries to abort from the partially assembed sequence, described
14049 * by the information from basic abbort @dmabuf. It checks to see whether such
14050 * partially assembled sequence held by the driver. If so, it shall free up all
14051 * the frames from the partially assembled sequence.
14052 *
14053 * Return
14054 * true -- if there is matching partially assembled sequence present and all
14055 * the frames freed with the sequence;
14056 * false -- if there is no matching partially assembled sequence present so
14057 * nothing got aborted in the lower layer driver
14058 **/
14059static bool
14060lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14061 struct hbq_dmabuf *dmabuf)
14062{
14063 struct fc_frame_header *new_hdr;
14064 struct fc_frame_header *temp_hdr;
14065 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14066 struct hbq_dmabuf *seq_dmabuf = NULL;
14067
14068 /* Use the hdr_buf to find the sequence that matches this frame */
14069 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14070 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14071 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14072 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14073 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14074 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14075 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14076 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14077 continue;
14078 /* found a pending sequence that matches this frame */
14079 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14080 break;
14081 }
14082
14083 /* Free up all the frames from the partially assembled sequence */
14084 if (seq_dmabuf) {
14085 list_for_each_entry_safe(d_buf, n_buf,
14086 &seq_dmabuf->dbuf.list, list) {
14087 list_del_init(&d_buf->list);
14088 lpfc_in_buf_free(vport->phba, d_buf);
14089 }
14090 return true;
14091 }
14092 return false;
14093}
14094
14095/**
James Smart546fc852011-03-11 16:06:29 -050014096 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014097 * @phba: Pointer to HBA context object.
14098 * @cmd_iocbq: pointer to the command iocbq structure.
14099 * @rsp_iocbq: pointer to the response iocbq structure.
14100 *
James Smart546fc852011-03-11 16:06:29 -050014101 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014102 * event. It properly releases the memory allocated to the sequence abort
14103 * accept iocb.
14104 **/
14105static void
James Smart546fc852011-03-11 16:06:29 -050014106lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014107 struct lpfc_iocbq *cmd_iocbq,
14108 struct lpfc_iocbq *rsp_iocbq)
14109{
14110 if (cmd_iocbq)
14111 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014112
14113 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14114 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14116 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14117 rsp_iocbq->iocb.ulpStatus,
14118 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014119}
14120
14121/**
James Smart6d368e52011-05-24 11:44:12 -040014122 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14123 * @phba: Pointer to HBA context object.
14124 * @xri: xri id in transaction.
14125 *
14126 * This function validates the xri maps to the known range of XRIs allocated an
14127 * used by the driver.
14128 **/
James Smart7851fe22011-07-22 18:36:52 -040014129uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014130lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14131 uint16_t xri)
14132{
14133 int i;
14134
14135 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14136 if (xri == phba->sli4_hba.xri_ids[i])
14137 return i;
14138 }
14139 return NO_XRI;
14140}
14141
14142
14143/**
James Smart546fc852011-03-11 16:06:29 -050014144 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014145 * @phba: Pointer to HBA context object.
14146 * @fc_hdr: pointer to a FC frame header.
14147 *
James Smart546fc852011-03-11 16:06:29 -050014148 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014149 * event after aborting the sequence handling.
14150 **/
14151static void
James Smart546fc852011-03-11 16:06:29 -050014152lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014153 struct fc_frame_header *fc_hdr)
14154{
14155 struct lpfc_iocbq *ctiocb = NULL;
14156 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050014157 uint16_t oxid, rxid;
14158 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014159 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014160 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014161
14162 if (!lpfc_is_link_up(phba))
14163 return;
14164
14165 sid = sli4_sid_from_fc_hdr(fc_hdr);
14166 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014167 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014168
14169 ndlp = lpfc_findnode_did(phba->pport, sid);
14170 if (!ndlp) {
14171 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14172 "1268 Find ndlp returned NULL for oxid:x%x "
14173 "SID:x%x\n", oxid, sid);
14174 return;
14175 }
James Smart6d368e52011-05-24 11:44:12 -040014176 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050014177 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040014178
James Smart546fc852011-03-11 16:06:29 -050014179 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014180 ctiocb = lpfc_sli_get_iocbq(phba);
14181 if (!ctiocb)
14182 return;
14183
James Smart5ffc2662009-11-18 15:39:44 -050014184 /* Extract the F_CTL field from FC_HDR */
14185 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14186
James Smart6669f9b2009-10-02 15:16:45 -040014187 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014188 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014189 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014190 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14191 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14192 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14193
14194 /* Fill in the rest of iocb fields */
14195 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14196 icmd->ulpBdeCount = 0;
14197 icmd->ulpLe = 1;
14198 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014199 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014200 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014201
James Smart6669f9b2009-10-02 15:16:45 -040014202 ctiocb->iocb_cmpl = NULL;
14203 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014204 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014205 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014206 ctiocb->sli4_xritag = NO_XRI;
14207
14208 /* If the oxid maps to the FCP XRI range or if it is out of range,
14209 * send a BLS_RJT. The driver no longer has that exchange.
14210 * Override the IOCB for a BA_RJT.
14211 */
14212 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
14213 phba->sli4_hba.max_cfg_param.xri_base) ||
14214 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
14215 phba->sli4_hba.max_cfg_param.xri_base)) {
14216 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14217 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14218 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14219 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14220 }
James Smart6669f9b2009-10-02 15:16:45 -040014221
James Smart5ffc2662009-11-18 15:39:44 -050014222 if (fctl & FC_FC_EX_CTX) {
14223 /* ABTS sent by responder to CT exchange, construction
14224 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14225 * field and RX_ID from ABTS for RX_ID field.
14226 */
James Smart546fc852011-03-11 16:06:29 -050014227 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14228 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050014229 } else {
14230 /* ABTS sent by initiator to CT exchange, construction
14231 * of BA_ACC will need to allocate a new XRI as for the
14232 * XRI_TAG and RX_ID fields.
14233 */
James Smart546fc852011-03-11 16:06:29 -050014234 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14235 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050014236 }
James Smart546fc852011-03-11 16:06:29 -050014237 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014238
James Smart546fc852011-03-11 16:06:29 -050014239 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014240 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014241 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14242 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14243
14244 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14245 if (rc == IOCB_ERROR) {
14246 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14247 "2925 Failed to issue CT ABTS RSP x%x on "
14248 "xri x%x, Data x%x\n",
14249 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14250 phba->link_state);
14251 lpfc_sli_release_iocbq(phba, ctiocb);
14252 }
James Smart6669f9b2009-10-02 15:16:45 -040014253}
14254
14255/**
14256 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14257 * @vport: Pointer to the vport on which this sequence was received
14258 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14259 *
14260 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14261 * receive sequence is only partially assembed by the driver, it shall abort
14262 * the partially assembled frames for the sequence. Otherwise, if the
14263 * unsolicited receive sequence has been completely assembled and passed to
14264 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14265 * unsolicited sequence has been aborted. After that, it will issue a basic
14266 * accept to accept the abort.
14267 **/
14268void
14269lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14270 struct hbq_dmabuf *dmabuf)
14271{
14272 struct lpfc_hba *phba = vport->phba;
14273 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014274 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014275 bool abts_par;
14276
James Smart6669f9b2009-10-02 15:16:45 -040014277 /* Make a copy of fc_hdr before the dmabuf being released */
14278 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014279 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014280
James Smart5ffc2662009-11-18 15:39:44 -050014281 if (fctl & FC_FC_EX_CTX) {
14282 /*
14283 * ABTS sent by responder to exchange, just free the buffer
14284 */
James Smart6669f9b2009-10-02 15:16:45 -040014285 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014286 } else {
14287 /*
14288 * ABTS sent by initiator to exchange, need to do cleanup
14289 */
14290 /* Try to abort partially assembled seq */
14291 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14292
14293 /* Send abort to ULP if partially seq abort failed */
14294 if (abts_par == false)
14295 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14296 else
14297 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14298 }
James Smart6669f9b2009-10-02 15:16:45 -040014299 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014300 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014301}
14302
14303/**
James Smart4f774512009-05-22 14:52:35 -040014304 * lpfc_seq_complete - Indicates if a sequence is complete
14305 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14306 *
14307 * This function checks the sequence, starting with the frame described by
14308 * @dmabuf, to see if all the frames associated with this sequence are present.
14309 * the frames associated with this sequence are linked to the @dmabuf using the
14310 * dbuf list. This function looks for two major things. 1) That the first frame
14311 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14312 * set. 3) That there are no holes in the sequence count. The function will
14313 * return 1 when the sequence is complete, otherwise it will return 0.
14314 **/
14315static int
14316lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14317{
14318 struct fc_frame_header *hdr;
14319 struct lpfc_dmabuf *d_buf;
14320 struct hbq_dmabuf *seq_dmabuf;
14321 uint32_t fctl;
14322 int seq_count = 0;
14323
14324 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14325 /* make sure first fame of sequence has a sequence count of zero */
14326 if (hdr->fh_seq_cnt != seq_count)
14327 return 0;
14328 fctl = (hdr->fh_f_ctl[0] << 16 |
14329 hdr->fh_f_ctl[1] << 8 |
14330 hdr->fh_f_ctl[2]);
14331 /* If last frame of sequence we can return success. */
14332 if (fctl & FC_FC_END_SEQ)
14333 return 1;
14334 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14335 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14336 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14337 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014338 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014339 return 0;
14340 fctl = (hdr->fh_f_ctl[0] << 16 |
14341 hdr->fh_f_ctl[1] << 8 |
14342 hdr->fh_f_ctl[2]);
14343 /* If last frame of sequence we can return success. */
14344 if (fctl & FC_FC_END_SEQ)
14345 return 1;
14346 }
14347 return 0;
14348}
14349
14350/**
14351 * lpfc_prep_seq - Prep sequence for ULP processing
14352 * @vport: Pointer to the vport on which this sequence was received
14353 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14354 *
14355 * This function takes a sequence, described by a list of frames, and creates
14356 * a list of iocbq structures to describe the sequence. This iocbq list will be
14357 * used to issue to the generic unsolicited sequence handler. This routine
14358 * returns a pointer to the first iocbq in the list. If the function is unable
14359 * to allocate an iocbq then it throw out the received frames that were not
14360 * able to be described and return a pointer to the first iocbq. If unable to
14361 * allocate any iocbqs (including the first) this function will return NULL.
14362 **/
14363static struct lpfc_iocbq *
14364lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14365{
James Smart7851fe22011-07-22 18:36:52 -040014366 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014367 struct lpfc_dmabuf *d_buf, *n_buf;
14368 struct lpfc_iocbq *first_iocbq, *iocbq;
14369 struct fc_frame_header *fc_hdr;
14370 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014371 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014372 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014373
14374 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14375 /* remove from receive buffer list */
14376 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014377 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014378 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014379 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014380 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014381 /* Get an iocbq struct to fill in. */
14382 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14383 if (first_iocbq) {
14384 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014385 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014386 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14387 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014388 first_iocbq->iocb.ulpContext = NO_XRI;
14389 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14390 be16_to_cpu(fc_hdr->fh_ox_id);
14391 /* iocbq is prepped for internal consumption. Physical vpi. */
14392 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14393 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014394 /* put the first buffer into the first IOCBq */
14395 first_iocbq->context2 = &seq_dmabuf->dbuf;
14396 first_iocbq->context3 = NULL;
14397 first_iocbq->iocb.ulpBdeCount = 1;
14398 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14399 LPFC_DATA_BUF_SIZE;
14400 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014401 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014402 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014403 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014404 }
14405 iocbq = first_iocbq;
14406 /*
14407 * Each IOCBq can have two Buffers assigned, so go through the list
14408 * of buffers for this sequence and save two buffers in each IOCBq
14409 */
14410 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14411 if (!iocbq) {
14412 lpfc_in_buf_free(vport->phba, d_buf);
14413 continue;
14414 }
14415 if (!iocbq->context3) {
14416 iocbq->context3 = d_buf;
14417 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014418 pbde = (struct ulp_bde64 *)
14419 &iocbq->iocb.unsli3.sli3Words[4];
14420 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014421
14422 /* We need to get the size out of the right CQE */
14423 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14424 len = bf_get(lpfc_rcqe_length,
14425 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14426 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14427 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014428 } else {
14429 iocbq = lpfc_sli_get_iocbq(vport->phba);
14430 if (!iocbq) {
14431 if (first_iocbq) {
14432 first_iocbq->iocb.ulpStatus =
14433 IOSTAT_FCP_RSP_ERROR;
14434 first_iocbq->iocb.un.ulpWord[4] =
14435 IOERR_NO_RESOURCES;
14436 }
14437 lpfc_in_buf_free(vport->phba, d_buf);
14438 continue;
14439 }
14440 iocbq->context2 = d_buf;
14441 iocbq->context3 = NULL;
14442 iocbq->iocb.ulpBdeCount = 1;
14443 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14444 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014445
14446 /* We need to get the size out of the right CQE */
14447 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14448 len = bf_get(lpfc_rcqe_length,
14449 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14450 tot_len += len;
14451 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14452
James Smart4f774512009-05-22 14:52:35 -040014453 iocbq->iocb.un.rcvels.remoteID = sid;
14454 list_add_tail(&iocbq->list, &first_iocbq->list);
14455 }
14456 }
14457 return first_iocbq;
14458}
14459
James Smart6669f9b2009-10-02 15:16:45 -040014460static void
14461lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14462 struct hbq_dmabuf *seq_dmabuf)
14463{
14464 struct fc_frame_header *fc_hdr;
14465 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14466 struct lpfc_hba *phba = vport->phba;
14467
14468 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14469 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14470 if (!iocbq) {
14471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14472 "2707 Ring %d handler: Failed to allocate "
14473 "iocb Rctl x%x Type x%x received\n",
14474 LPFC_ELS_RING,
14475 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14476 return;
14477 }
14478 if (!lpfc_complete_unsol_iocb(phba,
14479 &phba->sli.ring[LPFC_ELS_RING],
14480 iocbq, fc_hdr->fh_r_ctl,
14481 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014482 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014483 "2540 Ring %d handler: unexpected Rctl "
14484 "x%x Type x%x received\n",
14485 LPFC_ELS_RING,
14486 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14487
14488 /* Free iocb created in lpfc_prep_seq */
14489 list_for_each_entry_safe(curr_iocb, next_iocb,
14490 &iocbq->list, list) {
14491 list_del_init(&curr_iocb->list);
14492 lpfc_sli_release_iocbq(phba, curr_iocb);
14493 }
14494 lpfc_sli_release_iocbq(phba, iocbq);
14495}
14496
James Smart4f774512009-05-22 14:52:35 -040014497/**
14498 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14499 * @phba: Pointer to HBA context object.
14500 *
14501 * This function is called with no lock held. This function processes all
14502 * the received buffers and gives it to upper layers when a received buffer
14503 * indicates that it is the final frame in the sequence. The interrupt
14504 * service routine processes received buffers at interrupt contexts and adds
14505 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14506 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14507 * appropriate receive function when the final frame in a sequence is received.
14508 **/
James Smart4d9ab992009-10-02 15:16:39 -040014509void
14510lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14511 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014512{
James Smart4d9ab992009-10-02 15:16:39 -040014513 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014514 struct fc_frame_header *fc_hdr;
14515 struct lpfc_vport *vport;
14516 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040014517
James Smart4f774512009-05-22 14:52:35 -040014518 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014519 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14520 /* check to see if this a valid type of frame */
14521 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14522 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14523 return;
14524 }
James Smart7851fe22011-07-22 18:36:52 -040014525 if ((bf_get(lpfc_cqe_code,
14526 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14527 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14528 &dmabuf->cq_event.cqe.rcqe_cmpl);
14529 else
14530 fcfi = bf_get(lpfc_rcqe_fcf_id,
14531 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4d9ab992009-10-02 15:16:39 -040014532 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050014533 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040014534 /* throw out the frame */
14535 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14536 return;
14537 }
James Smart6669f9b2009-10-02 15:16:45 -040014538 /* Handle the basic abort sequence (BA_ABTS) event */
14539 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14540 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14541 return;
14542 }
14543
James Smart4d9ab992009-10-02 15:16:39 -040014544 /* Link this frame */
14545 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14546 if (!seq_dmabuf) {
14547 /* unable to add frame to vport - throw it out */
14548 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14549 return;
14550 }
14551 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014552 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014553 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014554
James Smart6669f9b2009-10-02 15:16:45 -040014555 /* Send the complete sequence to the upper layer protocol */
14556 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014557}
James Smart6fb120a2009-05-22 14:52:59 -040014558
14559/**
14560 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14561 * @phba: pointer to lpfc hba data structure.
14562 *
14563 * This routine is invoked to post rpi header templates to the
14564 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014565 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14566 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014567 *
14568 * This routine does not require any locks. It's usage is expected
14569 * to be driver load or reset recovery when the driver is
14570 * sequential.
14571 *
14572 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014573 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014574 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014575 * When this error occurs, the driver is not guaranteed
14576 * to have any rpi regions posted to the device and
14577 * must either attempt to repost the regions or take a
14578 * fatal error.
14579 **/
14580int
14581lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14582{
14583 struct lpfc_rpi_hdr *rpi_page;
14584 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014585 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014586
James Smart6d368e52011-05-24 11:44:12 -040014587 /* SLI4 ports that support extents do not require RPI headers. */
14588 if (!phba->sli4_hba.rpi_hdrs_in_use)
14589 goto exit;
14590 if (phba->sli4_hba.extents_in_use)
14591 return -EIO;
14592
James Smart6fb120a2009-05-22 14:52:59 -040014593 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014594 /*
14595 * Assign the rpi headers a physical rpi only if the driver
14596 * has not initialized those resources. A port reset only
14597 * needs the headers posted.
14598 */
14599 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14600 LPFC_RPI_RSRC_RDY)
14601 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14602
James Smart6fb120a2009-05-22 14:52:59 -040014603 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14604 if (rc != MBX_SUCCESS) {
14605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14606 "2008 Error %d posting all rpi "
14607 "headers\n", rc);
14608 rc = -EIO;
14609 break;
14610 }
14611 }
14612
James Smart6d368e52011-05-24 11:44:12 -040014613 exit:
14614 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14615 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014616 return rc;
14617}
14618
14619/**
14620 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14621 * @phba: pointer to lpfc hba data structure.
14622 * @rpi_page: pointer to the rpi memory region.
14623 *
14624 * This routine is invoked to post a single rpi header to the
14625 * HBA consistent with the SLI-4 interface spec. This memory region
14626 * maps up to 64 rpi context regions.
14627 *
14628 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014629 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014630 * -ENOMEM - No available memory
14631 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014632 **/
14633int
14634lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14635{
14636 LPFC_MBOXQ_t *mboxq;
14637 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14638 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014639 uint32_t shdr_status, shdr_add_status;
14640 union lpfc_sli4_cfg_shdr *shdr;
14641
James Smart6d368e52011-05-24 11:44:12 -040014642 /* SLI4 ports that support extents do not require RPI headers. */
14643 if (!phba->sli4_hba.rpi_hdrs_in_use)
14644 return rc;
14645 if (phba->sli4_hba.extents_in_use)
14646 return -EIO;
14647
James Smart6fb120a2009-05-22 14:52:59 -040014648 /* The port is notified of the header region via a mailbox command. */
14649 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14650 if (!mboxq) {
14651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14652 "2001 Unable to allocate memory for issuing "
14653 "SLI_CONFIG_SPECIAL mailbox command\n");
14654 return -ENOMEM;
14655 }
14656
14657 /* Post all rpi memory regions to the port. */
14658 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014659 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14660 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14661 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014662 sizeof(struct lpfc_sli4_cfg_mhdr),
14663 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014664
14665
14666 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014667 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14668 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014669 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14670 hdr_tmpl, rpi_page->page_count);
14671
James Smart6fb120a2009-05-22 14:52:59 -040014672 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14673 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014674 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014675 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14676 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14677 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14678 if (rc != MBX_TIMEOUT)
14679 mempool_free(mboxq, phba->mbox_mem_pool);
14680 if (shdr_status || shdr_add_status || rc) {
14681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14682 "2514 POST_RPI_HDR mailbox failed with "
14683 "status x%x add_status x%x, mbx status x%x\n",
14684 shdr_status, shdr_add_status, rc);
14685 rc = -ENXIO;
14686 }
14687 return rc;
14688}
14689
14690/**
14691 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14692 * @phba: pointer to lpfc hba data structure.
14693 *
14694 * This routine is invoked to post rpi header templates to the
14695 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014696 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14697 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014698 *
14699 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014700 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014701 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14702 **/
14703int
14704lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14705{
James Smart6d368e52011-05-24 11:44:12 -040014706 unsigned long rpi;
14707 uint16_t max_rpi, rpi_limit;
14708 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014709 struct lpfc_rpi_hdr *rpi_hdr;
14710
14711 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014712 rpi_limit = phba->sli4_hba.next_rpi;
14713
14714 /*
James Smart6d368e52011-05-24 11:44:12 -040014715 * Fetch the next logical rpi. Because this index is logical,
14716 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014717 */
14718 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014719 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14720 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014721 rpi = LPFC_RPI_ALLOC_ERROR;
14722 else {
14723 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14724 phba->sli4_hba.max_cfg_param.rpi_used++;
14725 phba->sli4_hba.rpi_count++;
14726 }
14727
14728 /*
14729 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014730 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014731 */
14732 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14733 (phba->sli4_hba.rpi_count >= max_rpi)) {
14734 spin_unlock_irq(&phba->hbalock);
14735 return rpi;
14736 }
14737
14738 /*
James Smart6d368e52011-05-24 11:44:12 -040014739 * RPI header postings are not required for SLI4 ports capable of
14740 * extents.
14741 */
14742 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14743 spin_unlock_irq(&phba->hbalock);
14744 return rpi;
14745 }
14746
14747 /*
James Smart6fb120a2009-05-22 14:52:59 -040014748 * If the driver is running low on rpi resources, allocate another
14749 * page now. Note that the next_rpi value is used because
14750 * it represents how many are actually in use whereas max_rpi notes
14751 * how many are supported max by the device.
14752 */
James Smart6d368e52011-05-24 11:44:12 -040014753 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014754 spin_unlock_irq(&phba->hbalock);
14755 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14756 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14757 if (!rpi_hdr) {
14758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14759 "2002 Error Could not grow rpi "
14760 "count\n");
14761 } else {
James Smart6d368e52011-05-24 11:44:12 -040014762 lrpi = rpi_hdr->start_rpi;
14763 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014764 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14765 }
14766 }
14767
14768 return rpi;
14769}
14770
14771/**
14772 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14773 * @phba: pointer to lpfc hba data structure.
14774 *
14775 * This routine is invoked to release an rpi to the pool of
14776 * available rpis maintained by the driver.
14777 **/
14778void
James Smartd7c47992010-06-08 18:31:54 -040014779__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14780{
14781 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14782 phba->sli4_hba.rpi_count--;
14783 phba->sli4_hba.max_cfg_param.rpi_used--;
14784 }
14785}
14786
14787/**
14788 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14789 * @phba: pointer to lpfc hba data structure.
14790 *
14791 * This routine is invoked to release an rpi to the pool of
14792 * available rpis maintained by the driver.
14793 **/
14794void
James Smart6fb120a2009-05-22 14:52:59 -040014795lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14796{
14797 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014798 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014799 spin_unlock_irq(&phba->hbalock);
14800}
14801
14802/**
14803 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14804 * @phba: pointer to lpfc hba data structure.
14805 *
14806 * This routine is invoked to remove the memory region that
14807 * provided rpi via a bitmask.
14808 **/
14809void
14810lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14811{
14812 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014813 kfree(phba->sli4_hba.rpi_ids);
14814 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014815}
14816
14817/**
14818 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14819 * @phba: pointer to lpfc hba data structure.
14820 *
14821 * This routine is invoked to remove the memory region that
14822 * provided rpi via a bitmask.
14823 **/
14824int
James Smart6b5151f2012-01-18 16:24:06 -050014825lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14826 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014827{
14828 LPFC_MBOXQ_t *mboxq;
14829 struct lpfc_hba *phba = ndlp->phba;
14830 int rc;
14831
14832 /* The port is notified of the header region via a mailbox command. */
14833 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14834 if (!mboxq)
14835 return -ENOMEM;
14836
14837 /* Post all rpi memory regions to the port. */
14838 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014839 if (cmpl) {
14840 mboxq->mbox_cmpl = cmpl;
14841 mboxq->context1 = arg;
14842 mboxq->context2 = ndlp;
14843 }
14844 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014845 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14846 if (rc == MBX_NOT_FINISHED) {
14847 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14848 "2010 Resume RPI Mailbox failed "
14849 "status %d, mbxStatus x%x\n", rc,
14850 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14851 mempool_free(mboxq, phba->mbox_mem_pool);
14852 return -EIO;
14853 }
14854 return 0;
14855}
14856
14857/**
14858 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014859 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014860 *
James Smart76a95d72010-11-20 23:11:48 -050014861 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014862 *
14863 * Returns:
14864 * 0 success
14865 * -Evalue otherwise
14866 **/
14867int
James Smart76a95d72010-11-20 23:11:48 -050014868lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014869{
14870 LPFC_MBOXQ_t *mboxq;
14871 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014872 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014873 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014874 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014875 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14876 if (!mboxq)
14877 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014878 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014879 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014880 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014881 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014882 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014883 "2022 INIT VPI Mailbox failed "
14884 "status %d, mbxStatus x%x\n", rc,
14885 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014886 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014887 }
James Smart6a9c52c2009-10-02 15:16:51 -040014888 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014889 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014890
14891 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014892}
14893
14894/**
14895 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14896 * @phba: pointer to lpfc hba data structure.
14897 * @mboxq: Pointer to mailbox object.
14898 *
14899 * This routine is invoked to manually add a single FCF record. The caller
14900 * must pass a completely initialized FCF_Record. This routine takes
14901 * care of the nonembedded mailbox operations.
14902 **/
14903static void
14904lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14905{
14906 void *virt_addr;
14907 union lpfc_sli4_cfg_shdr *shdr;
14908 uint32_t shdr_status, shdr_add_status;
14909
14910 virt_addr = mboxq->sge_array->addr[0];
14911 /* The IOCTL status is embedded in the mailbox subheader. */
14912 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14913 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14914 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14915
14916 if ((shdr_status || shdr_add_status) &&
14917 (shdr_status != STATUS_FCF_IN_USE))
14918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14919 "2558 ADD_FCF_RECORD mailbox failed with "
14920 "status x%x add_status x%x\n",
14921 shdr_status, shdr_add_status);
14922
14923 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14924}
14925
14926/**
14927 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14928 * @phba: pointer to lpfc hba data structure.
14929 * @fcf_record: pointer to the initialized fcf record to add.
14930 *
14931 * This routine is invoked to manually add a single FCF record. The caller
14932 * must pass a completely initialized FCF_Record. This routine takes
14933 * care of the nonembedded mailbox operations.
14934 **/
14935int
14936lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14937{
14938 int rc = 0;
14939 LPFC_MBOXQ_t *mboxq;
14940 uint8_t *bytep;
14941 void *virt_addr;
14942 dma_addr_t phys_addr;
14943 struct lpfc_mbx_sge sge;
14944 uint32_t alloc_len, req_len;
14945 uint32_t fcfindex;
14946
14947 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14948 if (!mboxq) {
14949 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14950 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14951 return -ENOMEM;
14952 }
14953
14954 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14955 sizeof(uint32_t);
14956
14957 /* Allocate DMA memory and set up the non-embedded mailbox command */
14958 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14959 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14960 req_len, LPFC_SLI4_MBX_NEMBED);
14961 if (alloc_len < req_len) {
14962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14963 "2523 Allocated DMA memory size (x%x) is "
14964 "less than the requested DMA memory "
14965 "size (x%x)\n", alloc_len, req_len);
14966 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14967 return -ENOMEM;
14968 }
14969
14970 /*
14971 * Get the first SGE entry from the non-embedded DMA memory. This
14972 * routine only uses a single SGE.
14973 */
14974 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14975 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014976 virt_addr = mboxq->sge_array->addr[0];
14977 /*
14978 * Configure the FCF record for FCFI 0. This is the driver's
14979 * hardcoded default and gets used in nonFIP mode.
14980 */
14981 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14982 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14983 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14984
14985 /*
14986 * Copy the fcf_index and the FCF Record Data. The data starts after
14987 * the FCoE header plus word10. The data copy needs to be endian
14988 * correct.
14989 */
14990 bytep += sizeof(uint32_t);
14991 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14992 mboxq->vport = phba->pport;
14993 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14994 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14995 if (rc == MBX_NOT_FINISHED) {
14996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14997 "2515 ADD_FCF_RECORD mailbox failed with "
14998 "status 0x%x\n", rc);
14999 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15000 rc = -EIO;
15001 } else
15002 rc = 0;
15003
15004 return rc;
15005}
15006
15007/**
15008 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15009 * @phba: pointer to lpfc hba data structure.
15010 * @fcf_record: pointer to the fcf record to write the default data.
15011 * @fcf_index: FCF table entry index.
15012 *
15013 * This routine is invoked to build the driver's default FCF record. The
15014 * values used are hardcoded. This routine handles memory initialization.
15015 *
15016 **/
15017void
15018lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15019 struct fcf_record *fcf_record,
15020 uint16_t fcf_index)
15021{
15022 memset(fcf_record, 0, sizeof(struct fcf_record));
15023 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15024 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15025 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15026 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15027 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15028 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15029 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15030 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15031 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15032 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15033 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15034 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15035 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015036 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015037 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15038 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15039 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15040 /* Set the VLAN bit map */
15041 if (phba->valid_vlan) {
15042 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15043 = 1 << (phba->vlan_id % 8);
15044 }
15045}
15046
15047/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015048 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015049 * @phba: pointer to lpfc hba data structure.
15050 * @fcf_index: FCF table entry offset.
15051 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015052 * This routine is invoked to scan the entire FCF table by reading FCF
15053 * record and processing it one at a time starting from the @fcf_index
15054 * for initial FCF discovery or fast FCF failover rediscovery.
15055 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015056 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015057 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015058 **/
15059int
James Smart0c9ab6f2010-02-26 14:15:57 -050015060lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015061{
15062 int rc = 0, error;
15063 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015064
James Smart32b97932009-07-19 10:01:21 -040015065 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015066 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15067 if (!mboxq) {
15068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15069 "2000 Failed to allocate mbox for "
15070 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015071 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015072 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015073 }
James Smartecfd03c2010-02-12 14:41:27 -050015074 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015075 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015076 if (rc) {
15077 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015078 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015079 }
James Smartecfd03c2010-02-12 14:41:27 -050015080 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015081 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015082 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015083
15084 spin_lock_irq(&phba->hbalock);
15085 phba->hba_flag |= FCF_TS_INPROG;
15086 spin_unlock_irq(&phba->hbalock);
15087
James Smart6fb120a2009-05-22 14:52:59 -040015088 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015089 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015090 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015091 else {
James Smart38b92ef2010-08-04 16:11:39 -040015092 /* Reset eligible FCF count for new scan */
15093 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015094 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015095 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015096 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015097fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015098 if (error) {
15099 if (mboxq)
15100 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015101 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015102 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015103 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015104 spin_unlock_irq(&phba->hbalock);
15105 }
James Smart6fb120a2009-05-22 14:52:59 -040015106 return error;
15107}
James Smarta0c87cb2009-07-19 10:01:10 -040015108
15109/**
James Smarta93ff372010-10-22 11:06:08 -040015110 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015111 * @phba: pointer to lpfc hba data structure.
15112 * @fcf_index: FCF table entry offset.
15113 *
15114 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015115 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015116 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015117 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015118 * otherwise.
15119 **/
15120int
15121lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15122{
15123 int rc = 0, error;
15124 LPFC_MBOXQ_t *mboxq;
15125
15126 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15127 if (!mboxq) {
15128 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15129 "2763 Failed to allocate mbox for "
15130 "READ_FCF cmd\n");
15131 error = -ENOMEM;
15132 goto fail_fcf_read;
15133 }
15134 /* Construct the read FCF record mailbox command */
15135 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15136 if (rc) {
15137 error = -EINVAL;
15138 goto fail_fcf_read;
15139 }
15140 /* Issue the mailbox command asynchronously */
15141 mboxq->vport = phba->pport;
15142 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15143 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15144 if (rc == MBX_NOT_FINISHED)
15145 error = -EIO;
15146 else
15147 error = 0;
15148
15149fail_fcf_read:
15150 if (error && mboxq)
15151 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15152 return error;
15153}
15154
15155/**
15156 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15157 * @phba: pointer to lpfc hba data structure.
15158 * @fcf_index: FCF table entry offset.
15159 *
15160 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015161 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015162 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015163 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015164 * otherwise.
15165 **/
15166int
15167lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15168{
15169 int rc = 0, error;
15170 LPFC_MBOXQ_t *mboxq;
15171
15172 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15173 if (!mboxq) {
15174 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15175 "2758 Failed to allocate mbox for "
15176 "READ_FCF cmd\n");
15177 error = -ENOMEM;
15178 goto fail_fcf_read;
15179 }
15180 /* Construct the read FCF record mailbox command */
15181 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15182 if (rc) {
15183 error = -EINVAL;
15184 goto fail_fcf_read;
15185 }
15186 /* Issue the mailbox command asynchronously */
15187 mboxq->vport = phba->pport;
15188 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15189 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15190 if (rc == MBX_NOT_FINISHED)
15191 error = -EIO;
15192 else
15193 error = 0;
15194
15195fail_fcf_read:
15196 if (error && mboxq)
15197 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15198 return error;
15199}
15200
15201/**
James Smart7d791df2011-07-22 18:37:52 -040015202 * lpfc_check_next_fcf_pri
15203 * phba pointer to the lpfc_hba struct for this port.
15204 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15205 * routine when the rr_bmask is empty. The FCF indecies are put into the
15206 * rr_bmask based on their priority level. Starting from the highest priority
15207 * to the lowest. The most likely FCF candidate will be in the highest
15208 * priority group. When this routine is called it searches the fcf_pri list for
15209 * next lowest priority group and repopulates the rr_bmask with only those
15210 * fcf_indexes.
15211 * returns:
15212 * 1=success 0=failure
15213 **/
15214int
15215lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15216{
15217 uint16_t next_fcf_pri;
15218 uint16_t last_index;
15219 struct lpfc_fcf_pri *fcf_pri;
15220 int rc;
15221 int ret = 0;
15222
15223 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15224 LPFC_SLI4_FCF_TBL_INDX_MAX);
15225 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15226 "3060 Last IDX %d\n", last_index);
15227 if (list_empty(&phba->fcf.fcf_pri_list)) {
15228 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15229 "3061 Last IDX %d\n", last_index);
15230 return 0; /* Empty rr list */
15231 }
15232 next_fcf_pri = 0;
15233 /*
15234 * Clear the rr_bmask and set all of the bits that are at this
15235 * priority.
15236 */
15237 memset(phba->fcf.fcf_rr_bmask, 0,
15238 sizeof(*phba->fcf.fcf_rr_bmask));
15239 spin_lock_irq(&phba->hbalock);
15240 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15241 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15242 continue;
15243 /*
15244 * the 1st priority that has not FLOGI failed
15245 * will be the highest.
15246 */
15247 if (!next_fcf_pri)
15248 next_fcf_pri = fcf_pri->fcf_rec.priority;
15249 spin_unlock_irq(&phba->hbalock);
15250 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15251 rc = lpfc_sli4_fcf_rr_index_set(phba,
15252 fcf_pri->fcf_rec.fcf_index);
15253 if (rc)
15254 return 0;
15255 }
15256 spin_lock_irq(&phba->hbalock);
15257 }
15258 /*
15259 * if next_fcf_pri was not set above and the list is not empty then
15260 * we have failed flogis on all of them. So reset flogi failed
15261 * and start at the begining.
15262 */
15263 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15264 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15265 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15266 /*
15267 * the 1st priority that has not FLOGI failed
15268 * will be the highest.
15269 */
15270 if (!next_fcf_pri)
15271 next_fcf_pri = fcf_pri->fcf_rec.priority;
15272 spin_unlock_irq(&phba->hbalock);
15273 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15274 rc = lpfc_sli4_fcf_rr_index_set(phba,
15275 fcf_pri->fcf_rec.fcf_index);
15276 if (rc)
15277 return 0;
15278 }
15279 spin_lock_irq(&phba->hbalock);
15280 }
15281 } else
15282 ret = 1;
15283 spin_unlock_irq(&phba->hbalock);
15284
15285 return ret;
15286}
15287/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015288 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15289 * @phba: pointer to lpfc hba data structure.
15290 *
15291 * This routine is to get the next eligible FCF record index in a round
15292 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015293 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015294 * shall be returned, otherwise, the next eligible FCF record's index
15295 * shall be returned.
15296 **/
15297uint16_t
15298lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15299{
15300 uint16_t next_fcf_index;
15301
James Smart3804dc82010-07-14 15:31:37 -040015302 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015303next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015304 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15305 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015306 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15307 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015308 next_fcf_index);
15309
James Smart0c9ab6f2010-02-26 14:15:57 -050015310 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015311 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15312 /*
15313 * If we have wrapped then we need to clear the bits that
15314 * have been tested so that we can detect when we should
15315 * change the priority level.
15316 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015317 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15318 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015319 }
15320
James Smart0c9ab6f2010-02-26 14:15:57 -050015321
James Smart3804dc82010-07-14 15:31:37 -040015322 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015323 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15324 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15325 /*
15326 * If next fcf index is not found check if there are lower
15327 * Priority level fcf's in the fcf_priority list.
15328 * Set up the rr_bmask with all of the avaiable fcf bits
15329 * at that level and continue the selection process.
15330 */
15331 if (lpfc_check_next_fcf_pri_level(phba))
15332 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015333 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15334 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015335 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15336 return LPFC_FCOE_FCF_NEXT_NONE;
15337 else {
15338 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15339 "3063 Only FCF available idx %d, flag %x\n",
15340 next_fcf_index,
15341 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15342 return next_fcf_index;
15343 }
James Smart3804dc82010-07-14 15:31:37 -040015344 }
15345
James Smart7d791df2011-07-22 18:37:52 -040015346 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15347 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15348 LPFC_FCF_FLOGI_FAILED)
15349 goto next_priority;
15350
James Smart3804dc82010-07-14 15:31:37 -040015351 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015352 "2845 Get next roundrobin failover FCF (x%x)\n",
15353 next_fcf_index);
15354
James Smart0c9ab6f2010-02-26 14:15:57 -050015355 return next_fcf_index;
15356}
15357
15358/**
15359 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15360 * @phba: pointer to lpfc hba data structure.
15361 *
15362 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015363 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015364 * does not go beyond the range of the driver allocated bmask dimension
15365 * before setting the bit.
15366 *
15367 * Returns 0 if the index bit successfully set, otherwise, it returns
15368 * -EINVAL.
15369 **/
15370int
15371lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15372{
15373 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15374 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015375 "2610 FCF (x%x) reached driver's book "
15376 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015377 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15378 return -EINVAL;
15379 }
15380 /* Set the eligible FCF record index bmask */
15381 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15382
James Smart3804dc82010-07-14 15:31:37 -040015383 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015384 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015385 "bmask\n", fcf_index);
15386
James Smart0c9ab6f2010-02-26 14:15:57 -050015387 return 0;
15388}
15389
15390/**
James Smart3804dc82010-07-14 15:31:37 -040015391 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015392 * @phba: pointer to lpfc hba data structure.
15393 *
15394 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015395 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015396 * does not go beyond the range of the driver allocated bmask dimension
15397 * before clearing the bit.
15398 **/
15399void
15400lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15401{
James Smart7d791df2011-07-22 18:37:52 -040015402 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015403 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15404 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015405 "2762 FCF (x%x) reached driver's book "
15406 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015407 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15408 return;
15409 }
15410 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015411 spin_lock_irq(&phba->hbalock);
15412 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15413 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15414 list_del_init(&fcf_pri->list);
15415 break;
15416 }
15417 }
15418 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015419 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015420
15421 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015422 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015423 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015424}
15425
15426/**
James Smartecfd03c2010-02-12 14:41:27 -050015427 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15428 * @phba: pointer to lpfc hba data structure.
15429 *
15430 * This routine is the completion routine for the rediscover FCF table mailbox
15431 * command. If the mailbox command returned failure, it will try to stop the
15432 * FCF rediscover wait timer.
15433 **/
15434void
15435lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15436{
15437 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15438 uint32_t shdr_status, shdr_add_status;
15439
15440 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15441
15442 shdr_status = bf_get(lpfc_mbox_hdr_status,
15443 &redisc_fcf->header.cfg_shdr.response);
15444 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15445 &redisc_fcf->header.cfg_shdr.response);
15446 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015447 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015448 "2746 Requesting for FCF rediscovery failed "
15449 "status x%x add_status x%x\n",
15450 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015451 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015452 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015453 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015454 spin_unlock_irq(&phba->hbalock);
15455 /*
15456 * CVL event triggered FCF rediscover request failed,
15457 * last resort to re-try current registered FCF entry.
15458 */
15459 lpfc_retry_pport_discovery(phba);
15460 } else {
15461 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015462 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015463 spin_unlock_irq(&phba->hbalock);
15464 /*
15465 * DEAD FCF event triggered FCF rediscover request
15466 * failed, last resort to fail over as a link down
15467 * to FCF registration.
15468 */
15469 lpfc_sli4_fcf_dead_failthrough(phba);
15470 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015471 } else {
15472 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015473 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015474 /*
15475 * Start FCF rediscovery wait timer for pending FCF
15476 * before rescan FCF record table.
15477 */
15478 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015479 }
James Smartecfd03c2010-02-12 14:41:27 -050015480
15481 mempool_free(mbox, phba->mbox_mem_pool);
15482}
15483
15484/**
James Smart3804dc82010-07-14 15:31:37 -040015485 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015486 * @phba: pointer to lpfc hba data structure.
15487 *
15488 * This routine is invoked to request for rediscovery of the entire FCF table
15489 * by the port.
15490 **/
15491int
15492lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15493{
15494 LPFC_MBOXQ_t *mbox;
15495 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15496 int rc, length;
15497
James Smart0c9ab6f2010-02-26 14:15:57 -050015498 /* Cancel retry delay timers to all vports before FCF rediscover */
15499 lpfc_cancel_all_vport_retry_delay_timer(phba);
15500
James Smartecfd03c2010-02-12 14:41:27 -050015501 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15502 if (!mbox) {
15503 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15504 "2745 Failed to allocate mbox for "
15505 "requesting FCF rediscover.\n");
15506 return -ENOMEM;
15507 }
15508
15509 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15510 sizeof(struct lpfc_sli4_cfg_mhdr));
15511 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15512 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15513 length, LPFC_SLI4_MBX_EMBED);
15514
15515 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15516 /* Set count to 0 for invalidating the entire FCF database */
15517 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15518
15519 /* Issue the mailbox command asynchronously */
15520 mbox->vport = phba->pport;
15521 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15522 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15523
15524 if (rc == MBX_NOT_FINISHED) {
15525 mempool_free(mbox, phba->mbox_mem_pool);
15526 return -EIO;
15527 }
15528 return 0;
15529}
15530
15531/**
James Smartfc2b9892010-02-26 14:15:29 -050015532 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15533 * @phba: pointer to lpfc hba data structure.
15534 *
15535 * This function is the failover routine as a last resort to the FCF DEAD
15536 * event when driver failed to perform fast FCF failover.
15537 **/
15538void
15539lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15540{
15541 uint32_t link_state;
15542
15543 /*
15544 * Last resort as FCF DEAD event failover will treat this as
15545 * a link down, but save the link state because we don't want
15546 * it to be changed to Link Down unless it is already down.
15547 */
15548 link_state = phba->link_state;
15549 lpfc_linkdown(phba);
15550 phba->link_state = link_state;
15551
15552 /* Unregister FCF if no devices connected to it */
15553 lpfc_unregister_unused_fcf(phba);
15554}
15555
15556/**
James Smart026abb82011-12-13 13:20:45 -050015557 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015558 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015559 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015560 *
James Smart026abb82011-12-13 13:20:45 -050015561 * This function gets SLI3 port configure region 23 data through memory dump
15562 * mailbox command. When it successfully retrieves data, the size of the data
15563 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015564 **/
James Smart026abb82011-12-13 13:20:45 -050015565static uint32_t
15566lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015567{
15568 LPFC_MBOXQ_t *pmb = NULL;
15569 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015570 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015571 int rc;
15572
James Smart026abb82011-12-13 13:20:45 -050015573 if (!rgn23_data)
15574 return 0;
15575
James Smarta0c87cb2009-07-19 10:01:10 -040015576 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15577 if (!pmb) {
15578 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015579 "2600 failed to allocate mailbox memory\n");
15580 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015581 }
15582 mb = &pmb->u.mb;
15583
James Smarta0c87cb2009-07-19 10:01:10 -040015584 do {
15585 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15586 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15587
15588 if (rc != MBX_SUCCESS) {
15589 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015590 "2601 failed to read config "
15591 "region 23, rc 0x%x Status 0x%x\n",
15592 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015593 mb->un.varDmp.word_cnt = 0;
15594 }
15595 /*
15596 * dump mem may return a zero when finished or we got a
15597 * mailbox error, either way we are done.
15598 */
15599 if (mb->un.varDmp.word_cnt == 0)
15600 break;
15601 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15602 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15603
15604 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015605 rgn23_data + offset,
15606 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015607 offset += mb->un.varDmp.word_cnt;
15608 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15609
James Smart026abb82011-12-13 13:20:45 -050015610 mempool_free(pmb, phba->mbox_mem_pool);
15611 return offset;
15612}
15613
15614/**
15615 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15616 * @phba: pointer to lpfc hba data structure.
15617 * @rgn23_data: pointer to configure region 23 data.
15618 *
15619 * This function gets SLI4 port configure region 23 data through memory dump
15620 * mailbox command. When it successfully retrieves data, the size of the data
15621 * will be returned, otherwise, 0 will be returned.
15622 **/
15623static uint32_t
15624lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15625{
15626 LPFC_MBOXQ_t *mboxq = NULL;
15627 struct lpfc_dmabuf *mp = NULL;
15628 struct lpfc_mqe *mqe;
15629 uint32_t data_length = 0;
15630 int rc;
15631
15632 if (!rgn23_data)
15633 return 0;
15634
15635 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15636 if (!mboxq) {
15637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15638 "3105 failed to allocate mailbox memory\n");
15639 return 0;
15640 }
15641
15642 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15643 goto out;
15644 mqe = &mboxq->u.mqe;
15645 mp = (struct lpfc_dmabuf *) mboxq->context1;
15646 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15647 if (rc)
15648 goto out;
15649 data_length = mqe->un.mb_words[5];
15650 if (data_length == 0)
15651 goto out;
15652 if (data_length > DMP_RGN23_SIZE) {
15653 data_length = 0;
15654 goto out;
15655 }
15656 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15657out:
15658 mempool_free(mboxq, phba->mbox_mem_pool);
15659 if (mp) {
15660 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15661 kfree(mp);
15662 }
15663 return data_length;
15664}
15665
15666/**
15667 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15668 * @phba: pointer to lpfc hba data structure.
15669 *
15670 * This function read region 23 and parse TLV for port status to
15671 * decide if the user disaled the port. If the TLV indicates the
15672 * port is disabled, the hba_flag is set accordingly.
15673 **/
15674void
15675lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15676{
15677 uint8_t *rgn23_data = NULL;
15678 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15679 uint32_t offset = 0;
15680
15681 /* Get adapter Region 23 data */
15682 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15683 if (!rgn23_data)
15684 goto out;
15685
15686 if (phba->sli_rev < LPFC_SLI_REV4)
15687 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15688 else {
15689 if_type = bf_get(lpfc_sli_intf_if_type,
15690 &phba->sli4_hba.sli_intf);
15691 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15692 goto out;
15693 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15694 }
James Smarta0c87cb2009-07-19 10:01:10 -040015695
15696 if (!data_size)
15697 goto out;
15698
15699 /* Check the region signature first */
15700 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15702 "2619 Config region 23 has bad signature\n");
15703 goto out;
15704 }
15705 offset += 4;
15706
15707 /* Check the data structure version */
15708 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15710 "2620 Config region 23 has bad version\n");
15711 goto out;
15712 }
15713 offset += 4;
15714
15715 /* Parse TLV entries in the region */
15716 while (offset < data_size) {
15717 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15718 break;
15719 /*
15720 * If the TLV is not driver specific TLV or driver id is
15721 * not linux driver id, skip the record.
15722 */
15723 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15724 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15725 (rgn23_data[offset + 3] != 0)) {
15726 offset += rgn23_data[offset + 1] * 4 + 4;
15727 continue;
15728 }
15729
15730 /* Driver found a driver specific TLV in the config region */
15731 sub_tlv_len = rgn23_data[offset + 1] * 4;
15732 offset += 4;
15733 tlv_offset = 0;
15734
15735 /*
15736 * Search for configured port state sub-TLV.
15737 */
15738 while ((offset < data_size) &&
15739 (tlv_offset < sub_tlv_len)) {
15740 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15741 offset += 4;
15742 tlv_offset += 4;
15743 break;
15744 }
15745 if (rgn23_data[offset] != PORT_STE_TYPE) {
15746 offset += rgn23_data[offset + 1] * 4 + 4;
15747 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15748 continue;
15749 }
15750
15751 /* This HBA contains PORT_STE configured */
15752 if (!rgn23_data[offset + 2])
15753 phba->hba_flag |= LINK_DISABLED;
15754
15755 goto out;
15756 }
15757 }
James Smart026abb82011-12-13 13:20:45 -050015758
James Smarta0c87cb2009-07-19 10:01:10 -040015759out:
James Smarta0c87cb2009-07-19 10:01:10 -040015760 kfree(rgn23_data);
15761 return;
15762}
James Smart695a8142010-01-26 23:08:03 -050015763
15764/**
James Smart52d52442011-05-24 11:42:45 -040015765 * lpfc_wr_object - write an object to the firmware
15766 * @phba: HBA structure that indicates port to create a queue on.
15767 * @dmabuf_list: list of dmabufs to write to the port.
15768 * @size: the total byte value of the objects to write to the port.
15769 * @offset: the current offset to be used to start the transfer.
15770 *
15771 * This routine will create a wr_object mailbox command to send to the port.
15772 * the mailbox command will be constructed using the dma buffers described in
15773 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15774 * BDEs that the imbedded mailbox can support. The @offset variable will be
15775 * used to indicate the starting offset of the transfer and will also return
15776 * the offset after the write object mailbox has completed. @size is used to
15777 * determine the end of the object and whether the eof bit should be set.
15778 *
15779 * Return 0 is successful and offset will contain the the new offset to use
15780 * for the next write.
15781 * Return negative value for error cases.
15782 **/
15783int
15784lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15785 uint32_t size, uint32_t *offset)
15786{
15787 struct lpfc_mbx_wr_object *wr_object;
15788 LPFC_MBOXQ_t *mbox;
15789 int rc = 0, i = 0;
15790 uint32_t shdr_status, shdr_add_status;
15791 uint32_t mbox_tmo;
15792 union lpfc_sli4_cfg_shdr *shdr;
15793 struct lpfc_dmabuf *dmabuf;
15794 uint32_t written = 0;
15795
15796 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15797 if (!mbox)
15798 return -ENOMEM;
15799
15800 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15801 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15802 sizeof(struct lpfc_mbx_wr_object) -
15803 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15804
15805 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15806 wr_object->u.request.write_offset = *offset;
15807 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15808 wr_object->u.request.object_name[0] =
15809 cpu_to_le32(wr_object->u.request.object_name[0]);
15810 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15811 list_for_each_entry(dmabuf, dmabuf_list, list) {
15812 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15813 break;
15814 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15815 wr_object->u.request.bde[i].addrHigh =
15816 putPaddrHigh(dmabuf->phys);
15817 if (written + SLI4_PAGE_SIZE >= size) {
15818 wr_object->u.request.bde[i].tus.f.bdeSize =
15819 (size - written);
15820 written += (size - written);
15821 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15822 } else {
15823 wr_object->u.request.bde[i].tus.f.bdeSize =
15824 SLI4_PAGE_SIZE;
15825 written += SLI4_PAGE_SIZE;
15826 }
15827 i++;
15828 }
15829 wr_object->u.request.bde_count = i;
15830 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15831 if (!phba->sli4_hba.intr_enable)
15832 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15833 else {
James Smarta183a152011-10-10 21:32:43 -040015834 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015835 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15836 }
15837 /* The IOCTL status is embedded in the mailbox subheader. */
15838 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15839 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15840 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15841 if (rc != MBX_TIMEOUT)
15842 mempool_free(mbox, phba->mbox_mem_pool);
15843 if (shdr_status || shdr_add_status || rc) {
15844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15845 "3025 Write Object mailbox failed with "
15846 "status x%x add_status x%x, mbx status x%x\n",
15847 shdr_status, shdr_add_status, rc);
15848 rc = -ENXIO;
15849 } else
15850 *offset += wr_object->u.response.actual_write_length;
15851 return rc;
15852}
15853
15854/**
James Smart695a8142010-01-26 23:08:03 -050015855 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15856 * @vport: pointer to vport data structure.
15857 *
15858 * This function iterate through the mailboxq and clean up all REG_LOGIN
15859 * and REG_VPI mailbox commands associated with the vport. This function
15860 * is called when driver want to restart discovery of the vport due to
15861 * a Clear Virtual Link event.
15862 **/
15863void
15864lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15865{
15866 struct lpfc_hba *phba = vport->phba;
15867 LPFC_MBOXQ_t *mb, *nextmb;
15868 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015869 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015870 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015871 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015872 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015873 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015874
James Smartd439d282010-09-29 11:18:45 -040015875 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015876 spin_lock_irq(&phba->hbalock);
15877 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15878 if (mb->vport != vport)
15879 continue;
15880
15881 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15882 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15883 continue;
15884
James Smartd439d282010-09-29 11:18:45 -040015885 list_del(&mb->list);
15886 list_add_tail(&mb->list, &mbox_cmd_list);
15887 }
15888 /* Clean up active mailbox command with the vport */
15889 mb = phba->sli.mbox_active;
15890 if (mb && (mb->vport == vport)) {
15891 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15892 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15893 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15894 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15895 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15896 /* Put reference count for delayed processing */
15897 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15898 /* Unregister the RPI when mailbox complete */
15899 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15900 }
15901 }
James Smart63e801c2010-11-20 23:14:19 -050015902 /* Cleanup any mailbox completions which are not yet processed */
15903 do {
15904 restart_loop = 0;
15905 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15906 /*
15907 * If this mailox is already processed or it is
15908 * for another vport ignore it.
15909 */
15910 if ((mb->vport != vport) ||
15911 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15912 continue;
15913
15914 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15915 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15916 continue;
15917
15918 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15919 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15920 ndlp = (struct lpfc_nodelist *)mb->context2;
15921 /* Unregister the RPI when mailbox complete */
15922 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15923 restart_loop = 1;
15924 spin_unlock_irq(&phba->hbalock);
15925 spin_lock(shost->host_lock);
15926 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15927 spin_unlock(shost->host_lock);
15928 spin_lock_irq(&phba->hbalock);
15929 break;
15930 }
15931 }
15932 } while (restart_loop);
15933
James Smartd439d282010-09-29 11:18:45 -040015934 spin_unlock_irq(&phba->hbalock);
15935
15936 /* Release the cleaned-up mailbox commands */
15937 while (!list_empty(&mbox_cmd_list)) {
15938 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015939 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15940 mp = (struct lpfc_dmabuf *) (mb->context1);
15941 if (mp) {
15942 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15943 kfree(mp);
15944 }
James Smart78730cf2010-04-06 15:06:30 -040015945 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015946 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015947 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015948 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015949 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015950 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015951 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015952 }
James Smart695a8142010-01-26 23:08:03 -050015953 }
James Smart695a8142010-01-26 23:08:03 -050015954 mempool_free(mb, phba->mbox_mem_pool);
15955 }
James Smartd439d282010-09-29 11:18:45 -040015956
15957 /* Release the ndlp with the cleaned-up active mailbox command */
15958 if (act_mbx_ndlp) {
15959 spin_lock(shost->host_lock);
15960 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15961 spin_unlock(shost->host_lock);
15962 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015963 }
James Smart695a8142010-01-26 23:08:03 -050015964}
15965
James Smart2a9bf3d2010-06-07 15:24:45 -040015966/**
15967 * lpfc_drain_txq - Drain the txq
15968 * @phba: Pointer to HBA context object.
15969 *
15970 * This function attempt to submit IOCBs on the txq
15971 * to the adapter. For SLI4 adapters, the txq contains
15972 * ELS IOCBs that have been deferred because the there
15973 * are no SGLs. This congestion can occur with large
15974 * vport counts during node discovery.
15975 **/
15976
15977uint32_t
15978lpfc_drain_txq(struct lpfc_hba *phba)
15979{
15980 LIST_HEAD(completions);
15981 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15982 struct lpfc_iocbq *piocbq = 0;
15983 unsigned long iflags = 0;
15984 char *fail_msg = NULL;
15985 struct lpfc_sglq *sglq;
15986 union lpfc_wqe wqe;
15987
15988 spin_lock_irqsave(&phba->hbalock, iflags);
15989 if (pring->txq_cnt > pring->txq_max)
15990 pring->txq_max = pring->txq_cnt;
15991
15992 spin_unlock_irqrestore(&phba->hbalock, iflags);
15993
15994 while (pring->txq_cnt) {
15995 spin_lock_irqsave(&phba->hbalock, iflags);
15996
James Smart19ca7602010-11-20 23:11:55 -050015997 piocbq = lpfc_sli_ringtx_get(phba, pring);
15998 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015999 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016000 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016001 spin_unlock_irqrestore(&phba->hbalock, iflags);
16002 break;
16003 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040016004 if (!piocbq) {
16005 /* The txq_cnt out of sync. This should
16006 * never happen
16007 */
16008 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040016009 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040016010 spin_unlock_irqrestore(&phba->hbalock, iflags);
16011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16012 "2823 txq empty and txq_cnt is %d\n ",
16013 pring->txq_cnt);
16014 break;
16015 }
16016 }
16017
16018 /* The xri and iocb resources secured,
16019 * attempt to issue request
16020 */
James Smart6d368e52011-05-24 11:44:12 -040016021 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016022 piocbq->sli4_xritag = sglq->sli4_xritag;
16023 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16024 fail_msg = "to convert bpl to sgl";
16025 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16026 fail_msg = "to convert iocb to wqe";
16027 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16028 fail_msg = " - Wq is full";
16029 else
16030 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16031
16032 if (fail_msg) {
16033 /* Failed means we can't issue and need to cancel */
16034 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16035 "2822 IOCB failed %s iotag 0x%x "
16036 "xri 0x%x\n",
16037 fail_msg,
16038 piocbq->iotag, piocbq->sli4_xritag);
16039 list_add_tail(&piocbq->list, &completions);
16040 }
16041 spin_unlock_irqrestore(&phba->hbalock, iflags);
16042 }
16043
James Smart2a9bf3d2010-06-07 15:24:45 -040016044 /* Cancel all the IOCBs that cannot be issued */
16045 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16046 IOERR_SLI_ABORTED);
16047
16048 return pring->txq_cnt;
16049}