blob: 982bd40353ec55aebbb744d0ba61910e5a3b5104 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartbdcd2b92012-03-01 22:33:52 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040068static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040070static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
James Smart05580562011-05-24 11:40:48 -040072
James Smart4f774512009-05-22 14:52:35 -040073static IOCB_t *
74lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
75{
76 return &iocbq->iocb;
77}
78
79/**
80 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
81 * @q: The Work Queue to operate on.
82 * @wqe: The work Queue Entry to put on the Work queue.
83 *
84 * This routine will copy the contents of @wqe to the next available entry on
85 * the @q. This function will then ring the Work Queue Doorbell to signal the
86 * HBA to start processing the Work Queue Entry. This function returns 0 if
87 * successful. If no entries are available on @q then this function will return
88 * -ENOMEM.
89 * The caller is expected to hold the hbalock when calling this routine.
90 **/
91static uint32_t
92lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
93{
James Smart2e90f4b2011-12-13 13:22:37 -050094 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040095 struct lpfc_register doorbell;
96 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -040097 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -040098
James Smart2e90f4b2011-12-13 13:22:37 -050099 /* sanity check on queue memory */
100 if (unlikely(!q))
101 return -ENOMEM;
102 temp_wqe = q->qe[q->host_index].wqe;
103
James Smart4f774512009-05-22 14:52:35 -0400104 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400105 idx = ((q->host_index + 1) % q->entry_count);
106 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400107 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400108 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400109 }
110 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400111 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500112 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400113 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500114 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
115 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400116 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
117
118 /* Update the host index before invoking device */
119 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400120
121 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400122
123 /* Ring Doorbell */
124 doorbell.word0 = 0;
125 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
126 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
127 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
128 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400129
130 return 0;
131}
132
133/**
134 * lpfc_sli4_wq_release - Updates internal hba index for WQ
135 * @q: The Work Queue to operate on.
136 * @index: The index to advance the hba index to.
137 *
138 * This routine will update the HBA index of a queue to reflect consumption of
139 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
140 * an entry the host calls this function to update the queue's internal
141 * pointers. This routine returns the number of entries that were consumed by
142 * the HBA.
143 **/
144static uint32_t
145lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
146{
147 uint32_t released = 0;
148
James Smart2e90f4b2011-12-13 13:22:37 -0500149 /* sanity check on queue memory */
150 if (unlikely(!q))
151 return 0;
152
James Smart4f774512009-05-22 14:52:35 -0400153 if (q->hba_index == index)
154 return 0;
155 do {
156 q->hba_index = ((q->hba_index + 1) % q->entry_count);
157 released++;
158 } while (q->hba_index != index);
159 return released;
160}
161
162/**
163 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
164 * @q: The Mailbox Queue to operate on.
165 * @wqe: The Mailbox Queue Entry to put on the Work queue.
166 *
167 * This routine will copy the contents of @mqe to the next available entry on
168 * the @q. This function will then ring the Work Queue Doorbell to signal the
169 * HBA to start processing the Work Queue Entry. This function returns 0 if
170 * successful. If no entries are available on @q then this function will return
171 * -ENOMEM.
172 * The caller is expected to hold the hbalock when calling this routine.
173 **/
174static uint32_t
175lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
176{
James Smart2e90f4b2011-12-13 13:22:37 -0500177 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400178 struct lpfc_register doorbell;
179 uint32_t host_index;
180
James Smart2e90f4b2011-12-13 13:22:37 -0500181 /* sanity check on queue memory */
182 if (unlikely(!q))
183 return -ENOMEM;
184 temp_mqe = q->qe[q->host_index].mqe;
185
James Smart4f774512009-05-22 14:52:35 -0400186 /* If the host has not yet processed the next entry then we are done */
187 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
188 return -ENOMEM;
189 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
190 /* Save off the mailbox pointer for completion */
191 q->phba->mbox = (MAILBOX_t *)temp_mqe;
192
193 /* Update the host index before invoking device */
194 host_index = q->host_index;
195 q->host_index = ((q->host_index + 1) % q->entry_count);
196
197 /* Ring Doorbell */
198 doorbell.word0 = 0;
199 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
200 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
201 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400202 return 0;
203}
204
205/**
206 * lpfc_sli4_mq_release - Updates internal hba index for MQ
207 * @q: The Mailbox Queue to operate on.
208 *
209 * This routine will update the HBA index of a queue to reflect consumption of
210 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
211 * an entry the host calls this function to update the queue's internal
212 * pointers. This routine returns the number of entries that were consumed by
213 * the HBA.
214 **/
215static uint32_t
216lpfc_sli4_mq_release(struct lpfc_queue *q)
217{
James Smart2e90f4b2011-12-13 13:22:37 -0500218 /* sanity check on queue memory */
219 if (unlikely(!q))
220 return 0;
221
James Smart4f774512009-05-22 14:52:35 -0400222 /* Clear the mailbox pointer for completion */
223 q->phba->mbox = NULL;
224 q->hba_index = ((q->hba_index + 1) % q->entry_count);
225 return 1;
226}
227
228/**
229 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
230 * @q: The Event Queue to get the first valid EQE from
231 *
232 * This routine will get the first valid Event Queue Entry from @q, update
233 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
234 * the Queue (no more work to do), or the Queue is full of EQEs that have been
235 * processed, but not popped back to the HBA then this routine will return NULL.
236 **/
237static struct lpfc_eqe *
238lpfc_sli4_eq_get(struct lpfc_queue *q)
239{
James Smart2e90f4b2011-12-13 13:22:37 -0500240 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400241 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500242
243 /* sanity check on queue memory */
244 if (unlikely(!q))
245 return NULL;
246 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400247
248 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400249 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400250 return NULL;
251 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400252 idx = ((q->hba_index + 1) % q->entry_count);
253 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400254 return NULL;
255
James Smart027140e2012-08-03 12:35:44 -0400256 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400257 return eqe;
258}
259
260/**
261 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
262 * @q: The Event Queue that the host has completed processing for.
263 * @arm: Indicates whether the host wants to arms this CQ.
264 *
265 * This routine will mark all Event Queue Entries on @q, from the last
266 * known completed entry to the last entry that was processed, as completed
267 * by clearing the valid bit for each completion queue entry. Then it will
268 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
269 * The internal host index in the @q will be updated by this routine to indicate
270 * that the host has finished processing the entries. The @arm parameter
271 * indicates that the queue should be rearmed when ringing the doorbell.
272 *
273 * This function will return the number of EQEs that were popped.
274 **/
275uint32_t
276lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
277{
278 uint32_t released = 0;
279 struct lpfc_eqe *temp_eqe;
280 struct lpfc_register doorbell;
281
James Smart2e90f4b2011-12-13 13:22:37 -0500282 /* sanity check on queue memory */
283 if (unlikely(!q))
284 return 0;
285
James Smart4f774512009-05-22 14:52:35 -0400286 /* while there are valid entries */
287 while (q->hba_index != q->host_index) {
288 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400289 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400290 released++;
291 q->host_index = ((q->host_index + 1) % q->entry_count);
292 }
293 if (unlikely(released == 0 && !arm))
294 return 0;
295
296 /* ring doorbell for number popped */
297 doorbell.word0 = 0;
298 if (arm) {
299 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
300 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
301 }
302 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
303 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500304 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
305 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
306 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400307 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500308 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
309 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
310 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400311 return released;
312}
313
314/**
315 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
316 * @q: The Completion Queue to get the first valid CQE from
317 *
318 * This routine will get the first valid Completion Queue Entry from @q, update
319 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
320 * the Queue (no more work to do), or the Queue is full of CQEs that have been
321 * processed, but not popped back to the HBA then this routine will return NULL.
322 **/
323static struct lpfc_cqe *
324lpfc_sli4_cq_get(struct lpfc_queue *q)
325{
326 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400327 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400328
James Smart2e90f4b2011-12-13 13:22:37 -0500329 /* sanity check on queue memory */
330 if (unlikely(!q))
331 return NULL;
332
James Smart4f774512009-05-22 14:52:35 -0400333 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400334 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400335 return NULL;
336 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400337 idx = ((q->hba_index + 1) % q->entry_count);
338 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400339 return NULL;
340
341 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400342 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400343 return cqe;
344}
345
346/**
347 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
348 * @q: The Completion Queue that the host has completed processing for.
349 * @arm: Indicates whether the host wants to arms this CQ.
350 *
351 * This routine will mark all Completion queue entries on @q, from the last
352 * known completed entry to the last entry that was processed, as completed
353 * by clearing the valid bit for each completion queue entry. Then it will
354 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
355 * The internal host index in the @q will be updated by this routine to indicate
356 * that the host has finished processing the entries. The @arm parameter
357 * indicates that the queue should be rearmed when ringing the doorbell.
358 *
359 * This function will return the number of CQEs that were released.
360 **/
361uint32_t
362lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
363{
364 uint32_t released = 0;
365 struct lpfc_cqe *temp_qe;
366 struct lpfc_register doorbell;
367
James Smart2e90f4b2011-12-13 13:22:37 -0500368 /* sanity check on queue memory */
369 if (unlikely(!q))
370 return 0;
James Smart4f774512009-05-22 14:52:35 -0400371 /* while there are valid entries */
372 while (q->hba_index != q->host_index) {
373 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400374 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400375 released++;
376 q->host_index = ((q->host_index + 1) % q->entry_count);
377 }
378 if (unlikely(released == 0 && !arm))
379 return 0;
380
381 /* ring doorbell for number popped */
382 doorbell.word0 = 0;
383 if (arm)
384 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
385 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
386 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500387 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
388 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
389 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400390 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
391 return released;
392}
393
394/**
395 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
396 * @q: The Header Receive Queue to operate on.
397 * @wqe: The Receive Queue Entry to put on the Receive queue.
398 *
399 * This routine will copy the contents of @wqe to the next available entry on
400 * the @q. This function will then ring the Receive Queue Doorbell to signal the
401 * HBA to start processing the Receive Queue Entry. This function returns the
402 * index that the rqe was copied to if successful. If no entries are available
403 * on @q then this function will return -ENOMEM.
404 * The caller is expected to hold the hbalock when calling this routine.
405 **/
406static int
407lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
408 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
409{
James Smart2e90f4b2011-12-13 13:22:37 -0500410 struct lpfc_rqe *temp_hrqe;
411 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400412 struct lpfc_register doorbell;
413 int put_index = hq->host_index;
414
James Smart2e90f4b2011-12-13 13:22:37 -0500415 /* sanity check on queue memory */
416 if (unlikely(!hq) || unlikely(!dq))
417 return -ENOMEM;
418 temp_hrqe = hq->qe[hq->host_index].rqe;
419 temp_drqe = dq->qe[dq->host_index].rqe;
420
James Smart4f774512009-05-22 14:52:35 -0400421 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
422 return -EINVAL;
423 if (hq->host_index != dq->host_index)
424 return -EINVAL;
425 /* If the host has not yet processed the next entry then we are done */
426 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
427 return -EBUSY;
428 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
429 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
430
431 /* Update the host index to point to the next slot */
432 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
433 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
434
435 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400436 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400437 doorbell.word0 = 0;
438 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400439 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400440 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
441 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
442 }
443 return put_index;
444}
445
446/**
447 * lpfc_sli4_rq_release - Updates internal hba index for RQ
448 * @q: The Header Receive Queue to operate on.
449 *
450 * This routine will update the HBA index of a queue to reflect consumption of
451 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
452 * consumed an entry the host calls this function to update the queue's
453 * internal pointers. This routine returns the number of entries that were
454 * consumed by the HBA.
455 **/
456static uint32_t
457lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
458{
James Smart2e90f4b2011-12-13 13:22:37 -0500459 /* sanity check on queue memory */
460 if (unlikely(!hq) || unlikely(!dq))
461 return 0;
462
James Smart4f774512009-05-22 14:52:35 -0400463 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
464 return 0;
465 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
466 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
467 return 1;
468}
469
James Smarte59058c2008-08-24 21:49:00 -0400470/**
James Smart3621a712009-04-06 18:47:14 -0400471 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400472 * @phba: Pointer to HBA context object.
473 * @pring: Pointer to driver SLI ring object.
474 *
475 * This function returns pointer to next command iocb entry
476 * in the command ring. The caller must hold hbalock to prevent
477 * other threads consume the next command iocb.
478 * SLI-2/SLI-3 provide different sized iocbs.
479 **/
James Smarted957682007-06-17 19:56:37 -0500480static inline IOCB_t *
481lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
482{
James Smart7e56aa22012-08-03 12:35:34 -0400483 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
484 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500485}
486
James Smarte59058c2008-08-24 21:49:00 -0400487/**
James Smart3621a712009-04-06 18:47:14 -0400488 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400489 * @phba: Pointer to HBA context object.
490 * @pring: Pointer to driver SLI ring object.
491 *
492 * This function returns pointer to next response iocb entry
493 * in the response ring. The caller must hold hbalock to make sure
494 * that no other thread consume the next response iocb.
495 * SLI-2/SLI-3 provide different sized iocbs.
496 **/
James Smarted957682007-06-17 19:56:37 -0500497static inline IOCB_t *
498lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
499{
James Smart7e56aa22012-08-03 12:35:34 -0400500 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
501 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500502}
503
James Smarte59058c2008-08-24 21:49:00 -0400504/**
James Smart3621a712009-04-06 18:47:14 -0400505 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400506 * @phba: Pointer to HBA context object.
507 *
508 * This function is called with hbalock held. This function
509 * allocates a new driver iocb object from the iocb pool. If the
510 * allocation is successful, it returns pointer to the newly
511 * allocated iocb object else it returns NULL.
512 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400513struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500514__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400515{
516 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
517 struct lpfc_iocbq * iocbq = NULL;
518
519 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400520 if (iocbq)
521 phba->iocb_cnt++;
522 if (phba->iocb_cnt > phba->iocb_max)
523 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400524 return iocbq;
525}
526
James Smarte59058c2008-08-24 21:49:00 -0400527/**
James Smartda0436e2009-05-22 14:51:39 -0400528 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
529 * @phba: Pointer to HBA context object.
530 * @xritag: XRI value.
531 *
532 * This function clears the sglq pointer from the array of acive
533 * sglq's. The xritag that is passed in is used to index into the
534 * array. Before the xritag can be used it needs to be adjusted
535 * by subtracting the xribase.
536 *
537 * Returns sglq ponter = success, NULL = Failure.
538 **/
539static struct lpfc_sglq *
540__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
541{
James Smartda0436e2009-05-22 14:51:39 -0400542 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400543
544 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
545 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400546 return sglq;
547}
548
549/**
550 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
551 * @phba: Pointer to HBA context object.
552 * @xritag: XRI value.
553 *
554 * This function returns the sglq pointer from the array of acive
555 * sglq's. The xritag that is passed in is used to index into the
556 * array. Before the xritag can be used it needs to be adjusted
557 * by subtracting the xribase.
558 *
559 * Returns sglq ponter = success, NULL = Failure.
560 **/
James Smart0f65ff62010-02-26 14:14:23 -0500561struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400562__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
563{
James Smartda0436e2009-05-22 14:51:39 -0400564 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400565
566 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400567 return sglq;
568}
569
570/**
James Smart1151e3e2011-02-16 12:39:35 -0500571 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500572 * @phba: Pointer to HBA context object.
573 * @xritag: xri used in this exchange.
574 * @rrq: The RRQ to be cleared.
575 *
James Smart19ca7602010-11-20 23:11:55 -0500576 **/
James Smart1151e3e2011-02-16 12:39:35 -0500577void
578lpfc_clr_rrq_active(struct lpfc_hba *phba,
579 uint16_t xritag,
580 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500581{
James Smart1151e3e2011-02-16 12:39:35 -0500582 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500583
James Smart1151e3e2011-02-16 12:39:35 -0500584 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
585 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500586
587 /* The target DID could have been swapped (cable swap)
588 * we should use the ndlp from the findnode if it is
589 * available.
590 */
James Smart1151e3e2011-02-16 12:39:35 -0500591 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500592 ndlp = rrq->ndlp;
593
James Smart1151e3e2011-02-16 12:39:35 -0500594 if (!ndlp)
595 goto out;
596
James Smart6d368e52011-05-24 11:44:12 -0400597 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500598 rrq->send_rrq = 0;
599 rrq->xritag = 0;
600 rrq->rrq_stop_time = 0;
601 }
James Smart1151e3e2011-02-16 12:39:35 -0500602out:
James Smart19ca7602010-11-20 23:11:55 -0500603 mempool_free(rrq, phba->rrq_pool);
604}
605
606/**
607 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
608 * @phba: Pointer to HBA context object.
609 *
610 * This function is called with hbalock held. This function
611 * Checks if stop_time (ratov from setting rrq active) has
612 * been reached, if it has and the send_rrq flag is set then
613 * it will call lpfc_send_rrq. If the send_rrq flag is not set
614 * then it will just call the routine to clear the rrq and
615 * free the rrq resource.
616 * The timer is set to the next rrq that is going to expire before
617 * leaving the routine.
618 *
619 **/
620void
621lpfc_handle_rrq_active(struct lpfc_hba *phba)
622{
623 struct lpfc_node_rrq *rrq;
624 struct lpfc_node_rrq *nextrrq;
625 unsigned long next_time;
626 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500627 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500628
629 spin_lock_irqsave(&phba->hbalock, iflags);
630 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
631 next_time = jiffies + HZ * (phba->fc_ratov + 1);
632 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500633 &phba->active_rrq_list, list) {
634 if (time_after(jiffies, rrq->rrq_stop_time))
635 list_move(&rrq->list, &send_rrq);
636 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500637 next_time = rrq->rrq_stop_time;
638 }
639 spin_unlock_irqrestore(&phba->hbalock, iflags);
640 if (!list_empty(&phba->active_rrq_list))
641 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500642 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
643 list_del(&rrq->list);
644 if (!rrq->send_rrq)
645 /* this call will free the rrq */
646 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
647 else if (lpfc_send_rrq(phba, rrq)) {
648 /* if we send the rrq then the completion handler
649 * will clear the bit in the xribitmap.
650 */
651 lpfc_clr_rrq_active(phba, rrq->xritag,
652 rrq);
653 }
654 }
James Smart19ca7602010-11-20 23:11:55 -0500655}
656
657/**
658 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
659 * @vport: Pointer to vport context object.
660 * @xri: The xri used in the exchange.
661 * @did: The targets DID for this exchange.
662 *
663 * returns NULL = rrq not found in the phba->active_rrq_list.
664 * rrq = rrq for this xri and target.
665 **/
666struct lpfc_node_rrq *
667lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
668{
669 struct lpfc_hba *phba = vport->phba;
670 struct lpfc_node_rrq *rrq;
671 struct lpfc_node_rrq *nextrrq;
672 unsigned long iflags;
673
674 if (phba->sli_rev != LPFC_SLI_REV4)
675 return NULL;
676 spin_lock_irqsave(&phba->hbalock, iflags);
677 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
678 if (rrq->vport == vport && rrq->xritag == xri &&
679 rrq->nlp_DID == did){
680 list_del(&rrq->list);
681 spin_unlock_irqrestore(&phba->hbalock, iflags);
682 return rrq;
683 }
684 }
685 spin_unlock_irqrestore(&phba->hbalock, iflags);
686 return NULL;
687}
688
689/**
690 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
691 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500692 * @ndlp: Pointer to the lpfc_node_list structure.
693 * If ndlp is NULL Remove all active RRQs for this vport from the
694 * phba->active_rrq_list and clear the rrq.
695 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500696 **/
697void
James Smart1151e3e2011-02-16 12:39:35 -0500698lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500699
700{
701 struct lpfc_hba *phba = vport->phba;
702 struct lpfc_node_rrq *rrq;
703 struct lpfc_node_rrq *nextrrq;
704 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500705 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500706
707 if (phba->sli_rev != LPFC_SLI_REV4)
708 return;
James Smart1151e3e2011-02-16 12:39:35 -0500709 if (!ndlp) {
710 lpfc_sli4_vport_delete_els_xri_aborted(vport);
711 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500712 }
James Smart1151e3e2011-02-16 12:39:35 -0500713 spin_lock_irqsave(&phba->hbalock, iflags);
714 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
715 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
716 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500717 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500718
719 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
720 list_del(&rrq->list);
721 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
722 }
James Smart19ca7602010-11-20 23:11:55 -0500723}
724
725/**
726 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
727 * @phba: Pointer to HBA context object.
728 *
729 * Remove all rrqs from the phba->active_rrq_list and free them by
730 * calling __lpfc_clr_active_rrq
731 *
732 **/
733void
734lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
735{
736 struct lpfc_node_rrq *rrq;
737 struct lpfc_node_rrq *nextrrq;
738 unsigned long next_time;
739 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500740 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500741
742 if (phba->sli_rev != LPFC_SLI_REV4)
743 return;
744 spin_lock_irqsave(&phba->hbalock, iflags);
745 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
746 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500747 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500748 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500749
750 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
751 list_del(&rrq->list);
752 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
753 }
James Smart19ca7602010-11-20 23:11:55 -0500754 if (!list_empty(&phba->active_rrq_list))
755 mod_timer(&phba->rrq_tmr, next_time);
756}
757
758
759/**
James Smart1151e3e2011-02-16 12:39:35 -0500760 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500761 * @phba: Pointer to HBA context object.
762 * @ndlp: Targets nodelist pointer for this exchange.
763 * @xritag the xri in the bitmap to test.
764 *
765 * This function is called with hbalock held. This function
766 * returns 0 = rrq not active for this xri
767 * 1 = rrq is valid for this xri.
768 **/
James Smart1151e3e2011-02-16 12:39:35 -0500769int
770lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500771 uint16_t xritag)
772{
James Smart19ca7602010-11-20 23:11:55 -0500773 if (!ndlp)
774 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400775 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500776 return 1;
777 else
778 return 0;
779}
780
781/**
782 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
783 * @phba: Pointer to HBA context object.
784 * @ndlp: nodelist pointer for this target.
785 * @xritag: xri used in this exchange.
786 * @rxid: Remote Exchange ID.
787 * @send_rrq: Flag used to determine if we should send rrq els cmd.
788 *
789 * This function takes the hbalock.
790 * The active bit is always set in the active rrq xri_bitmap even
791 * if there is no slot avaiable for the other rrq information.
792 *
793 * returns 0 rrq actived for this xri
794 * < 0 No memory or invalid ndlp.
795 **/
796int
797lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500798 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500799{
James Smart19ca7602010-11-20 23:11:55 -0500800 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500801 struct lpfc_node_rrq *rrq;
802 int empty;
803
804 if (!ndlp)
805 return -EINVAL;
806
807 if (!phba->cfg_enable_rrq)
808 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500809
810 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500811 if (phba->pport->load_flag & FC_UNLOADING) {
812 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
813 goto out;
814 }
815
816 /*
817 * set the active bit even if there is no mem available.
818 */
819 if (NLP_CHK_FREE_REQ(ndlp))
820 goto out;
821
822 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
823 goto out;
824
825 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
826 goto out;
827
James Smart19ca7602010-11-20 23:11:55 -0500828 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500829 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
830 if (!rrq) {
831 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
832 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
833 " DID:0x%x Send:%d\n",
834 xritag, rxid, ndlp->nlp_DID, send_rrq);
835 return -EINVAL;
836 }
837 rrq->send_rrq = send_rrq;
838 rrq->xritag = xritag;
839 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
840 rrq->ndlp = ndlp;
841 rrq->nlp_DID = ndlp->nlp_DID;
842 rrq->vport = ndlp->vport;
843 rrq->rxid = rxid;
844 rrq->send_rrq = send_rrq;
845 spin_lock_irqsave(&phba->hbalock, iflags);
846 empty = list_empty(&phba->active_rrq_list);
847 list_add_tail(&rrq->list, &phba->active_rrq_list);
848 phba->hba_flag |= HBA_RRQ_ACTIVE;
849 if (empty)
850 lpfc_worker_wake_up(phba);
851 spin_unlock_irqrestore(&phba->hbalock, iflags);
852 return 0;
853out:
854 spin_unlock_irqrestore(&phba->hbalock, iflags);
855 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
856 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
857 " DID:0x%x Send:%d\n",
858 xritag, rxid, ndlp->nlp_DID, send_rrq);
859 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500860}
861
862/**
James Smartda0436e2009-05-22 14:51:39 -0400863 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
864 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500865 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400866 *
867 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400868 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400869 * list is not empty then it is successful, it returns pointer to the newly
870 * allocated sglq object else it returns NULL.
871 **/
872static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500873__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400874{
875 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
876 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500877 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500878 struct lpfc_scsi_buf *lpfc_cmd;
879 struct lpfc_nodelist *ndlp;
880 int found = 0;
881
882 if (piocbq->iocb_flag & LPFC_IO_FCP) {
883 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
884 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500885 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
886 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500887 ndlp = piocbq->context_un.ndlp;
James Smart93d1379e2012-05-09 21:19:34 -0400888 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
889 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
890 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500891 else
892 ndlp = piocbq->context1;
893
James Smartda0436e2009-05-22 14:51:39 -0400894 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500895 start_sglq = sglq;
896 while (!found) {
897 if (!sglq)
898 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400899 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500900 /* This xri has an rrq outstanding for this DID.
901 * put it back in the list and get another xri.
902 */
903 list_add_tail(&sglq->list, lpfc_sgl_list);
904 sglq = NULL;
905 list_remove_head(lpfc_sgl_list, sglq,
906 struct lpfc_sglq, list);
907 if (sglq == start_sglq) {
908 sglq = NULL;
909 break;
910 } else
911 continue;
912 }
913 sglq->ndlp = ndlp;
914 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400915 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500916 sglq->state = SGL_ALLOCATED;
917 }
James Smartda0436e2009-05-22 14:51:39 -0400918 return sglq;
919}
920
921/**
James Smart3621a712009-04-06 18:47:14 -0400922 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400923 * @phba: Pointer to HBA context object.
924 *
925 * This function is called with no lock held. This function
926 * allocates a new driver iocb object from the iocb pool. If the
927 * allocation is successful, it returns pointer to the newly
928 * allocated iocb object else it returns NULL.
929 **/
James Smart2e0fef82007-06-17 19:56:36 -0500930struct lpfc_iocbq *
931lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500932{
James Smart2e0fef82007-06-17 19:56:36 -0500933 struct lpfc_iocbq * iocbq = NULL;
934 unsigned long iflags;
935
936 spin_lock_irqsave(&phba->hbalock, iflags);
937 iocbq = __lpfc_sli_get_iocbq(phba);
938 spin_unlock_irqrestore(&phba->hbalock, iflags);
939 return iocbq;
940}
941
James Smarte59058c2008-08-24 21:49:00 -0400942/**
James Smart4f774512009-05-22 14:52:35 -0400943 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
944 * @phba: Pointer to HBA context object.
945 * @iocbq: Pointer to driver iocb object.
946 *
947 * This function is called with hbalock held to release driver
948 * iocb object to the iocb pool. The iotag in the iocb object
949 * does not change for each use of the iocb object. This function
950 * clears all other fields of the iocb object when it is freed.
951 * The sqlq structure that holds the xritag and phys and virtual
952 * mappings for the scatter gather list is retrieved from the
953 * active array of sglq. The get of the sglq pointer also clears
954 * the entry in the array. If the status of the IO indiactes that
955 * this IO was aborted then the sglq entry it put on the
956 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
957 * IO has good status or fails for any other reason then the sglq
958 * entry is added to the free list (lpfc_sgl_list).
959 **/
960static void
961__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
962{
963 struct lpfc_sglq *sglq;
964 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400965 unsigned long iflag = 0;
966 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400967
968 if (iocbq->sli4_xritag == NO_XRI)
969 sglq = NULL;
970 else
James Smart6d368e52011-05-24 11:44:12 -0400971 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
972
James Smart4f774512009-05-22 14:52:35 -0400973 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500974 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
975 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400976 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
977 iflag);
978 list_add(&sglq->list,
979 &phba->sli4_hba.lpfc_abts_els_sgl_list);
980 spin_unlock_irqrestore(
981 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500982 } else {
983 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500984 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500985 list_add_tail(&sglq->list,
986 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400987
988 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400989 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400990 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500991 }
James Smart4f774512009-05-22 14:52:35 -0400992 }
993
994
995 /*
996 * Clean all volatile data fields, preserve iotag and node struct.
997 */
998 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -0400999 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001000 iocbq->sli4_xritag = NO_XRI;
1001 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1002}
1003
James Smart2a9bf3d2010-06-07 15:24:45 -04001004
James Smart4f774512009-05-22 14:52:35 -04001005/**
James Smart3772a992009-05-22 14:50:54 -04001006 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1007 * @phba: Pointer to HBA context object.
1008 * @iocbq: Pointer to driver iocb object.
1009 *
1010 * This function is called with hbalock held to release driver
1011 * iocb object to the iocb pool. The iotag in the iocb object
1012 * does not change for each use of the iocb object. This function
1013 * clears all other fields of the iocb object when it is freed.
1014 **/
1015static void
1016__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1017{
1018 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1019
1020 /*
1021 * Clean all volatile data fields, preserve iotag and node struct.
1022 */
1023 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1024 iocbq->sli4_xritag = NO_XRI;
1025 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1026}
1027
1028/**
James Smart3621a712009-04-06 18:47:14 -04001029 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001030 * @phba: Pointer to HBA context object.
1031 * @iocbq: Pointer to driver iocb object.
1032 *
1033 * This function is called with hbalock held to release driver
1034 * iocb object to the iocb pool. The iotag in the iocb object
1035 * does not change for each use of the iocb object. This function
1036 * clears all other fields of the iocb object when it is freed.
1037 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001038static void
James Smart2e0fef82007-06-17 19:56:36 -05001039__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1040{
James Smart3772a992009-05-22 14:50:54 -04001041 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001042 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001043}
1044
James Smarte59058c2008-08-24 21:49:00 -04001045/**
James Smart3621a712009-04-06 18:47:14 -04001046 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001047 * @phba: Pointer to HBA context object.
1048 * @iocbq: Pointer to driver iocb object.
1049 *
1050 * This function is called with no lock held to release the iocb to
1051 * iocb pool.
1052 **/
James Smart2e0fef82007-06-17 19:56:36 -05001053void
1054lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1055{
1056 unsigned long iflags;
1057
1058 /*
1059 * Clean all volatile data fields, preserve iotag and node struct.
1060 */
1061 spin_lock_irqsave(&phba->hbalock, iflags);
1062 __lpfc_sli_release_iocbq(phba, iocbq);
1063 spin_unlock_irqrestore(&phba->hbalock, iflags);
1064}
1065
James Smarte59058c2008-08-24 21:49:00 -04001066/**
James Smarta257bf92009-04-06 18:48:10 -04001067 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1068 * @phba: Pointer to HBA context object.
1069 * @iocblist: List of IOCBs.
1070 * @ulpstatus: ULP status in IOCB command field.
1071 * @ulpWord4: ULP word-4 in IOCB command field.
1072 *
1073 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1074 * on the list by invoking the complete callback function associated with the
1075 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1076 * fields.
1077 **/
1078void
1079lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1080 uint32_t ulpstatus, uint32_t ulpWord4)
1081{
1082 struct lpfc_iocbq *piocb;
1083
1084 while (!list_empty(iocblist)) {
1085 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1086
1087 if (!piocb->iocb_cmpl)
1088 lpfc_sli_release_iocbq(phba, piocb);
1089 else {
1090 piocb->iocb.ulpStatus = ulpstatus;
1091 piocb->iocb.un.ulpWord[4] = ulpWord4;
1092 (piocb->iocb_cmpl) (phba, piocb, piocb);
1093 }
1094 }
1095 return;
1096}
1097
1098/**
James Smart3621a712009-04-06 18:47:14 -04001099 * lpfc_sli_iocb_cmd_type - Get the iocb type
1100 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001101 *
1102 * This function is called by ring event handler function to get the iocb type.
1103 * This function translates the iocb command to an iocb command type used to
1104 * decide the final disposition of each completed IOCB.
1105 * The function returns
1106 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1107 * LPFC_SOL_IOCB if it is a solicited iocb completion
1108 * LPFC_ABORT_IOCB if it is an abort iocb
1109 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1110 *
1111 * The caller is not required to hold any lock.
1112 **/
dea31012005-04-17 16:05:31 -05001113static lpfc_iocb_type
1114lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1115{
1116 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1117
1118 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1119 return 0;
1120
1121 switch (iocb_cmnd) {
1122 case CMD_XMIT_SEQUENCE_CR:
1123 case CMD_XMIT_SEQUENCE_CX:
1124 case CMD_XMIT_BCAST_CN:
1125 case CMD_XMIT_BCAST_CX:
1126 case CMD_ELS_REQUEST_CR:
1127 case CMD_ELS_REQUEST_CX:
1128 case CMD_CREATE_XRI_CR:
1129 case CMD_CREATE_XRI_CX:
1130 case CMD_GET_RPI_CN:
1131 case CMD_XMIT_ELS_RSP_CX:
1132 case CMD_GET_RPI_CR:
1133 case CMD_FCP_IWRITE_CR:
1134 case CMD_FCP_IWRITE_CX:
1135 case CMD_FCP_IREAD_CR:
1136 case CMD_FCP_IREAD_CX:
1137 case CMD_FCP_ICMND_CR:
1138 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001139 case CMD_FCP_TSEND_CX:
1140 case CMD_FCP_TRSP_CX:
1141 case CMD_FCP_TRECEIVE_CX:
1142 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001143 case CMD_ADAPTER_MSG:
1144 case CMD_ADAPTER_DUMP:
1145 case CMD_XMIT_SEQUENCE64_CR:
1146 case CMD_XMIT_SEQUENCE64_CX:
1147 case CMD_XMIT_BCAST64_CN:
1148 case CMD_XMIT_BCAST64_CX:
1149 case CMD_ELS_REQUEST64_CR:
1150 case CMD_ELS_REQUEST64_CX:
1151 case CMD_FCP_IWRITE64_CR:
1152 case CMD_FCP_IWRITE64_CX:
1153 case CMD_FCP_IREAD64_CR:
1154 case CMD_FCP_IREAD64_CX:
1155 case CMD_FCP_ICMND64_CR:
1156 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001157 case CMD_FCP_TSEND64_CX:
1158 case CMD_FCP_TRSP64_CX:
1159 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001160 case CMD_GEN_REQUEST64_CR:
1161 case CMD_GEN_REQUEST64_CX:
1162 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001163 case DSSCMD_IWRITE64_CR:
1164 case DSSCMD_IWRITE64_CX:
1165 case DSSCMD_IREAD64_CR:
1166 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001167 type = LPFC_SOL_IOCB;
1168 break;
1169 case CMD_ABORT_XRI_CN:
1170 case CMD_ABORT_XRI_CX:
1171 case CMD_CLOSE_XRI_CN:
1172 case CMD_CLOSE_XRI_CX:
1173 case CMD_XRI_ABORTED_CX:
1174 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001175 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001176 type = LPFC_ABORT_IOCB;
1177 break;
1178 case CMD_RCV_SEQUENCE_CX:
1179 case CMD_RCV_ELS_REQ_CX:
1180 case CMD_RCV_SEQUENCE64_CX:
1181 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001182 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001183 case CMD_IOCB_RCV_SEQ64_CX:
1184 case CMD_IOCB_RCV_ELS64_CX:
1185 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001186 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001187 type = LPFC_UNSOL_IOCB;
1188 break;
James Smart3163f722008-02-08 18:50:25 -05001189 case CMD_IOCB_XMIT_MSEQ64_CR:
1190 case CMD_IOCB_XMIT_MSEQ64_CX:
1191 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1192 case CMD_IOCB_RCV_ELS_LIST64_CX:
1193 case CMD_IOCB_CLOSE_EXTENDED_CN:
1194 case CMD_IOCB_ABORT_EXTENDED_CN:
1195 case CMD_IOCB_RET_HBQE64_CN:
1196 case CMD_IOCB_FCP_IBIDIR64_CR:
1197 case CMD_IOCB_FCP_IBIDIR64_CX:
1198 case CMD_IOCB_FCP_ITASKMGT64_CX:
1199 case CMD_IOCB_LOGENTRY_CN:
1200 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1201 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001202 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001203 type = LPFC_UNKNOWN_IOCB;
1204 break;
dea31012005-04-17 16:05:31 -05001205 default:
1206 type = LPFC_UNKNOWN_IOCB;
1207 break;
1208 }
1209
1210 return type;
1211}
1212
James Smarte59058c2008-08-24 21:49:00 -04001213/**
James Smart3621a712009-04-06 18:47:14 -04001214 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001215 * @phba: Pointer to HBA context object.
1216 *
1217 * This function is called from SLI initialization code
1218 * to configure every ring of the HBA's SLI interface. The
1219 * caller is not required to hold any lock. This function issues
1220 * a config_ring mailbox command for each ring.
1221 * This function returns zero if successful else returns a negative
1222 * error code.
1223 **/
dea31012005-04-17 16:05:31 -05001224static int
James Smarted957682007-06-17 19:56:37 -05001225lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001226{
1227 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001228 LPFC_MBOXQ_t *pmb;
1229 MAILBOX_t *pmbox;
1230 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001231
James Smarted957682007-06-17 19:56:37 -05001232 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1233 if (!pmb)
1234 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001235 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001236 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001237 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001238 lpfc_config_ring(phba, i, pmb);
1239 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1240 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001242 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001243 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1244 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001245 rc, pmbox->mbxCommand,
1246 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001247 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001248 ret = -ENXIO;
1249 break;
dea31012005-04-17 16:05:31 -05001250 }
1251 }
James Smarted957682007-06-17 19:56:37 -05001252 mempool_free(pmb, phba->mbox_mem_pool);
1253 return ret;
dea31012005-04-17 16:05:31 -05001254}
1255
James Smarte59058c2008-08-24 21:49:00 -04001256/**
James Smart3621a712009-04-06 18:47:14 -04001257 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001258 * @phba: Pointer to HBA context object.
1259 * @pring: Pointer to driver SLI ring object.
1260 * @piocb: Pointer to the driver iocb object.
1261 *
1262 * This function is called with hbalock held. The function adds the
1263 * new iocb to txcmplq of the given ring. This function always returns
1264 * 0. If this function is called for ELS ring, this function checks if
1265 * there is a vport associated with the ELS command. This function also
1266 * starts els_tmofunc timer if this is an ELS command.
1267 **/
dea31012005-04-17 16:05:31 -05001268static int
James Smart2e0fef82007-06-17 19:56:36 -05001269lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1270 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001271{
dea31012005-04-17 16:05:31 -05001272 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001273 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
dea31012005-04-17 16:05:31 -05001274 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001275 if (pring->txcmplq_cnt > pring->txcmplq_max)
1276 pring->txcmplq_max = pring->txcmplq_cnt;
1277
James Smart92d7f7b2007-06-17 19:56:38 -05001278 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1279 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1280 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1281 if (!piocb->vport)
1282 BUG();
1283 else
1284 mod_timer(&piocb->vport->els_tmofunc,
1285 jiffies + HZ * (phba->fc_ratov << 1));
1286 }
1287
dea31012005-04-17 16:05:31 -05001288
James Smart2e0fef82007-06-17 19:56:36 -05001289 return 0;
dea31012005-04-17 16:05:31 -05001290}
1291
James Smarte59058c2008-08-24 21:49:00 -04001292/**
James Smart3621a712009-04-06 18:47:14 -04001293 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001294 * @phba: Pointer to HBA context object.
1295 * @pring: Pointer to driver SLI ring object.
1296 *
1297 * This function is called with hbalock held to get next
1298 * iocb in txq of the given ring. If there is any iocb in
1299 * the txq, the function returns first iocb in the list after
1300 * removing the iocb from the list, else it returns NULL.
1301 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001302struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001303lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001304{
dea31012005-04-17 16:05:31 -05001305 struct lpfc_iocbq *cmd_iocb;
1306
James Smart858c9f62007-06-17 19:56:39 -05001307 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1308 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001309 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001310 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001311}
1312
James Smarte59058c2008-08-24 21:49:00 -04001313/**
James Smart3621a712009-04-06 18:47:14 -04001314 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001315 * @phba: Pointer to HBA context object.
1316 * @pring: Pointer to driver SLI ring object.
1317 *
1318 * This function is called with hbalock held and the caller must post the
1319 * iocb without releasing the lock. If the caller releases the lock,
1320 * iocb slot returned by the function is not guaranteed to be available.
1321 * The function returns pointer to the next available iocb slot if there
1322 * is available slot in the ring, else it returns NULL.
1323 * If the get index of the ring is ahead of the put index, the function
1324 * will post an error attention event to the worker thread to take the
1325 * HBA to offline state.
1326 **/
dea31012005-04-17 16:05:31 -05001327static IOCB_t *
1328lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1329{
James Smart34b02dc2008-08-24 21:49:55 -04001330 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001331 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1332 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1333 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1334 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001335
James Smart7e56aa22012-08-03 12:35:34 -04001336 if (unlikely(pring->sli.sli3.local_getidx ==
1337 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001338
James Smart7e56aa22012-08-03 12:35:34 -04001339 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001340
James Smart7e56aa22012-08-03 12:35:34 -04001341 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001342 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001343 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001344 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001345 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001346 pring->sli.sli3.local_getidx,
1347 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001348
James Smart2e0fef82007-06-17 19:56:36 -05001349 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001350 /*
1351 * All error attention handlers are posted to
1352 * worker thread
1353 */
1354 phba->work_ha |= HA_ERATT;
1355 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001356
James Smart5e9d9b82008-06-14 22:52:53 -04001357 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001358
1359 return NULL;
1360 }
1361
James Smart7e56aa22012-08-03 12:35:34 -04001362 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001363 return NULL;
1364 }
1365
James Smarted957682007-06-17 19:56:37 -05001366 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001367}
1368
James Smarte59058c2008-08-24 21:49:00 -04001369/**
James Smart3621a712009-04-06 18:47:14 -04001370 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001371 * @phba: Pointer to HBA context object.
1372 * @iocbq: Pointer to driver iocb object.
1373 *
1374 * This function gets an iotag for the iocb. If there is no unused iotag and
1375 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1376 * array and assigns a new iotag.
1377 * The function returns the allocated iotag if successful, else returns zero.
1378 * Zero is not a valid iotag.
1379 * The caller is not required to hold any lock.
1380 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001381uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001382lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001383{
James Smart2e0fef82007-06-17 19:56:36 -05001384 struct lpfc_iocbq **new_arr;
1385 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001386 size_t new_len;
1387 struct lpfc_sli *psli = &phba->sli;
1388 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001389
James Smart2e0fef82007-06-17 19:56:36 -05001390 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001391 iotag = psli->last_iotag;
1392 if(++iotag < psli->iocbq_lookup_len) {
1393 psli->last_iotag = iotag;
1394 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001395 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001396 iocbq->iotag = iotag;
1397 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001398 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001399 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1400 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001401 spin_unlock_irq(&phba->hbalock);
1402 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001403 GFP_KERNEL);
1404 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001405 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001406 old_arr = psli->iocbq_lookup;
1407 if (new_len <= psli->iocbq_lookup_len) {
1408 /* highly unprobable case */
1409 kfree(new_arr);
1410 iotag = psli->last_iotag;
1411 if(++iotag < psli->iocbq_lookup_len) {
1412 psli->last_iotag = iotag;
1413 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001414 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001415 iocbq->iotag = iotag;
1416 return iotag;
1417 }
James Smart2e0fef82007-06-17 19:56:36 -05001418 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001419 return 0;
1420 }
1421 if (psli->iocbq_lookup)
1422 memcpy(new_arr, old_arr,
1423 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001424 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001425 psli->iocbq_lookup = new_arr;
1426 psli->iocbq_lookup_len = new_len;
1427 psli->last_iotag = iotag;
1428 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001429 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001430 iocbq->iotag = iotag;
1431 kfree(old_arr);
1432 return iotag;
1433 }
James Smart8f6d98d2006-08-01 07:34:00 -04001434 } else
James Smart2e0fef82007-06-17 19:56:36 -05001435 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001436
James Smartbc739052010-08-04 16:11:18 -04001437 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001438 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1439 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001440
James Bottomley604a3e32005-10-29 10:28:33 -05001441 return 0;
dea31012005-04-17 16:05:31 -05001442}
1443
James Smarte59058c2008-08-24 21:49:00 -04001444/**
James Smart3621a712009-04-06 18:47:14 -04001445 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001446 * @phba: Pointer to HBA context object.
1447 * @pring: Pointer to driver SLI ring object.
1448 * @iocb: Pointer to iocb slot in the ring.
1449 * @nextiocb: Pointer to driver iocb object which need to be
1450 * posted to firmware.
1451 *
1452 * This function is called with hbalock held to post a new iocb to
1453 * the firmware. This function copies the new iocb to ring iocb slot and
1454 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1455 * a completion call back for this iocb else the function will free the
1456 * iocb object.
1457 **/
dea31012005-04-17 16:05:31 -05001458static void
1459lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1460 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1461{
1462 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001463 * Set up an iotag
dea31012005-04-17 16:05:31 -05001464 */
James Bottomley604a3e32005-10-29 10:28:33 -05001465 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001466
James Smarte2a0a9d2008-12-04 22:40:02 -05001467
James Smarta58cbd52007-08-02 11:09:43 -04001468 if (pring->ringno == LPFC_ELS_RING) {
1469 lpfc_debugfs_slow_ring_trc(phba,
1470 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1471 *(((uint32_t *) &nextiocb->iocb) + 4),
1472 *(((uint32_t *) &nextiocb->iocb) + 6),
1473 *(((uint32_t *) &nextiocb->iocb) + 7));
1474 }
1475
dea31012005-04-17 16:05:31 -05001476 /*
1477 * Issue iocb command to adapter
1478 */
James Smart92d7f7b2007-06-17 19:56:38 -05001479 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001480 wmb();
1481 pring->stats.iocb_cmd++;
1482
1483 /*
1484 * If there is no completion routine to call, we can release the
1485 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1486 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1487 */
1488 if (nextiocb->iocb_cmpl)
1489 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001490 else
James Smart2e0fef82007-06-17 19:56:36 -05001491 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001492
1493 /*
1494 * Let the HBA know what IOCB slot will be the next one the
1495 * driver will put a command into.
1496 */
James Smart7e56aa22012-08-03 12:35:34 -04001497 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1498 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001499}
1500
James Smarte59058c2008-08-24 21:49:00 -04001501/**
James Smart3621a712009-04-06 18:47:14 -04001502 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001503 * @phba: Pointer to HBA context object.
1504 * @pring: Pointer to driver SLI ring object.
1505 *
1506 * The caller is not required to hold any lock for calling this function.
1507 * This function updates the chip attention bits for the ring to inform firmware
1508 * that there are pending work to be done for this ring and requests an
1509 * interrupt when there is space available in the ring. This function is
1510 * called when the driver is unable to post more iocbs to the ring due
1511 * to unavailability of space in the ring.
1512 **/
dea31012005-04-17 16:05:31 -05001513static void
James Smart2e0fef82007-06-17 19:56:36 -05001514lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001515{
1516 int ringno = pring->ringno;
1517
1518 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1519
1520 wmb();
1521
1522 /*
1523 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1524 * The HBA will tell us when an IOCB entry is available.
1525 */
1526 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1527 readl(phba->CAregaddr); /* flush */
1528
1529 pring->stats.iocb_cmd_full++;
1530}
1531
James Smarte59058c2008-08-24 21:49:00 -04001532/**
James Smart3621a712009-04-06 18:47:14 -04001533 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001534 * @phba: Pointer to HBA context object.
1535 * @pring: Pointer to driver SLI ring object.
1536 *
1537 * This function updates the chip attention register bit for the
1538 * given ring to inform HBA that there is more work to be done
1539 * in this ring. The caller is not required to hold any lock.
1540 **/
dea31012005-04-17 16:05:31 -05001541static void
James Smart2e0fef82007-06-17 19:56:36 -05001542lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001543{
1544 int ringno = pring->ringno;
1545
1546 /*
1547 * Tell the HBA that there is work to do in this ring.
1548 */
James Smart34b02dc2008-08-24 21:49:55 -04001549 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1550 wmb();
1551 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1552 readl(phba->CAregaddr); /* flush */
1553 }
dea31012005-04-17 16:05:31 -05001554}
1555
James Smarte59058c2008-08-24 21:49:00 -04001556/**
James Smart3621a712009-04-06 18:47:14 -04001557 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001558 * @phba: Pointer to HBA context object.
1559 * @pring: Pointer to driver SLI ring object.
1560 *
1561 * This function is called with hbalock held to post pending iocbs
1562 * in the txq to the firmware. This function is called when driver
1563 * detects space available in the ring.
1564 **/
dea31012005-04-17 16:05:31 -05001565static void
James Smart2e0fef82007-06-17 19:56:36 -05001566lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001567{
1568 IOCB_t *iocb;
1569 struct lpfc_iocbq *nextiocb;
1570
1571 /*
1572 * Check to see if:
1573 * (a) there is anything on the txq to send
1574 * (b) link is up
1575 * (c) link attention events can be processed (fcp ring only)
1576 * (d) IOCB processing is not blocked by the outstanding mbox command.
1577 */
1578 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001579 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001580 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001581 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001582
1583 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1584 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1585 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1586
1587 if (iocb)
1588 lpfc_sli_update_ring(phba, pring);
1589 else
1590 lpfc_sli_update_full_ring(phba, pring);
1591 }
1592
1593 return;
1594}
1595
James Smarte59058c2008-08-24 21:49:00 -04001596/**
James Smart3621a712009-04-06 18:47:14 -04001597 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001598 * @phba: Pointer to HBA context object.
1599 * @hbqno: HBQ number.
1600 *
1601 * This function is called with hbalock held to get the next
1602 * available slot for the given HBQ. If there is free slot
1603 * available for the HBQ it will return pointer to the next available
1604 * HBQ entry else it will return NULL.
1605 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001606static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001607lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1608{
1609 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1610
1611 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1612 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1613 hbqp->next_hbqPutIdx = 0;
1614
1615 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001616 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001617 uint32_t getidx = le32_to_cpu(raw_index);
1618
1619 hbqp->local_hbqGetIdx = getidx;
1620
1621 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1622 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001623 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001624 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001625 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001626 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001627 hbqp->entry_count);
1628
1629 phba->link_state = LPFC_HBA_ERROR;
1630 return NULL;
1631 }
1632
1633 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1634 return NULL;
1635 }
1636
James Smart51ef4c22007-08-02 11:10:31 -04001637 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1638 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001639}
1640
James Smarte59058c2008-08-24 21:49:00 -04001641/**
James Smart3621a712009-04-06 18:47:14 -04001642 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001643 * @phba: Pointer to HBA context object.
1644 *
1645 * This function is called with no lock held to free all the
1646 * hbq buffers while uninitializing the SLI interface. It also
1647 * frees the HBQ buffers returned by the firmware but not yet
1648 * processed by the upper layers.
1649 **/
James Smarted957682007-06-17 19:56:37 -05001650void
1651lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1652{
James Smart92d7f7b2007-06-17 19:56:38 -05001653 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1654 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001655 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001656 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001657 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001658
James Smart51ef4c22007-08-02 11:10:31 -04001659 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001660 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001661 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001662 for (i = 0; i < hbq_count; ++i) {
1663 list_for_each_entry_safe(dmabuf, next_dmabuf,
1664 &phba->hbqs[i].hbq_buffer_list, list) {
1665 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1666 list_del(&hbq_buf->dbuf.list);
1667 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1668 }
James Smarta8adb832007-10-27 13:37:53 -04001669 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001670 }
James Smart3163f722008-02-08 18:50:25 -05001671 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001672 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1673 list) {
James Smart3163f722008-02-08 18:50:25 -05001674 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1675 list_del(&hbq_buf->dbuf.list);
1676 if (hbq_buf->tag == -1) {
1677 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1678 (phba, hbq_buf);
1679 } else {
1680 hbqno = hbq_buf->tag >> 16;
1681 if (hbqno >= LPFC_MAX_HBQS)
1682 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1683 (phba, hbq_buf);
1684 else
1685 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1686 hbq_buf);
1687 }
1688 }
1689
1690 /* Mark the HBQs not in use */
1691 phba->hbq_in_use = 0;
1692 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001693}
1694
James Smarte59058c2008-08-24 21:49:00 -04001695/**
James Smart3621a712009-04-06 18:47:14 -04001696 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001697 * @phba: Pointer to HBA context object.
1698 * @hbqno: HBQ number.
1699 * @hbq_buf: Pointer to HBQ buffer.
1700 *
1701 * This function is called with the hbalock held to post a
1702 * hbq buffer to the firmware. If the function finds an empty
1703 * slot in the HBQ, it will post the buffer. The function will return
1704 * pointer to the hbq entry if it successfully post the buffer
1705 * else it will return NULL.
1706 **/
James Smart3772a992009-05-22 14:50:54 -04001707static int
James Smarted957682007-06-17 19:56:37 -05001708lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001709 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001710{
James Smart3772a992009-05-22 14:50:54 -04001711 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1712}
1713
1714/**
1715 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1716 * @phba: Pointer to HBA context object.
1717 * @hbqno: HBQ number.
1718 * @hbq_buf: Pointer to HBQ buffer.
1719 *
1720 * This function is called with the hbalock held to post a hbq buffer to the
1721 * firmware. If the function finds an empty slot in the HBQ, it will post the
1722 * buffer and place it on the hbq_buffer_list. The function will return zero if
1723 * it successfully post the buffer else it will return an error.
1724 **/
1725static int
1726lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1727 struct hbq_dmabuf *hbq_buf)
1728{
James Smarted957682007-06-17 19:56:37 -05001729 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001730 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001731
1732 /* Get next HBQ entry slot to use */
1733 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1734 if (hbqe) {
1735 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1736
James Smart92d7f7b2007-06-17 19:56:38 -05001737 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1738 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001739 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001740 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001741 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1742 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1743 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001744 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1745 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001746 /* flush */
James Smarted957682007-06-17 19:56:37 -05001747 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001748 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001749 return 0;
1750 } else
1751 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001752}
1753
James Smart4f774512009-05-22 14:52:35 -04001754/**
1755 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1756 * @phba: Pointer to HBA context object.
1757 * @hbqno: HBQ number.
1758 * @hbq_buf: Pointer to HBQ buffer.
1759 *
1760 * This function is called with the hbalock held to post an RQE to the SLI4
1761 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1762 * the hbq_buffer_list and return zero, otherwise it will return an error.
1763 **/
1764static int
1765lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1766 struct hbq_dmabuf *hbq_buf)
1767{
1768 int rc;
1769 struct lpfc_rqe hrqe;
1770 struct lpfc_rqe drqe;
1771
1772 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1773 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1774 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1775 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1776 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1777 &hrqe, &drqe);
1778 if (rc < 0)
1779 return rc;
1780 hbq_buf->tag = rc;
1781 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1782 return 0;
1783}
1784
James Smarte59058c2008-08-24 21:49:00 -04001785/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001786static struct lpfc_hbq_init lpfc_els_hbq = {
1787 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001788 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001789 .mask_count = 0,
1790 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001791 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001792 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001793 .init_count = 40,
1794 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001795};
James Smarted957682007-06-17 19:56:37 -05001796
James Smarte59058c2008-08-24 21:49:00 -04001797/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001798static struct lpfc_hbq_init lpfc_extra_hbq = {
1799 .rn = 1,
1800 .entry_count = 200,
1801 .mask_count = 0,
1802 .profile = 0,
1803 .ring_mask = (1 << LPFC_EXTRA_RING),
1804 .buffer_count = 0,
1805 .init_count = 0,
1806 .add_count = 5,
1807};
1808
James Smarte59058c2008-08-24 21:49:00 -04001809/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001810struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001811 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001812 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001813};
1814
James Smarte59058c2008-08-24 21:49:00 -04001815/**
James Smart3621a712009-04-06 18:47:14 -04001816 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001817 * @phba: Pointer to HBA context object.
1818 * @hbqno: HBQ number.
1819 * @count: Number of HBQ buffers to be posted.
1820 *
James Smartd7c255b2008-08-24 21:50:00 -04001821 * This function is called with no lock held to post more hbq buffers to the
1822 * given HBQ. The function returns the number of HBQ buffers successfully
1823 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001824 **/
James Smart311464e2007-08-02 11:10:37 -04001825static int
James Smart92d7f7b2007-06-17 19:56:38 -05001826lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1827{
James Smartd7c255b2008-08-24 21:50:00 -04001828 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001829 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001830 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001831 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001832 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001833 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001834
James Smartd7c255b2008-08-24 21:50:00 -04001835 if ((phba->hbqs[hbqno].buffer_count + count) >
1836 lpfc_hbq_defs[hbqno]->entry_count)
1837 count = lpfc_hbq_defs[hbqno]->entry_count -
1838 phba->hbqs[hbqno].buffer_count;
1839 if (!count)
1840 return 0;
1841 /* Allocate HBQ entries */
1842 for (i = 0; i < count; i++) {
1843 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1844 if (!hbq_buffer)
1845 break;
1846 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1847 }
James Smart3163f722008-02-08 18:50:25 -05001848 /* Check whether HBQ is still in use */
1849 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001850 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001851 goto err;
1852 while (!list_empty(&hbq_buf_list)) {
1853 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1854 dbuf.list);
1855 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1856 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001857 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001858 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001859 posted++;
1860 } else
James Smart51ef4c22007-08-02 11:10:31 -04001861 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001862 }
James Smart3163f722008-02-08 18:50:25 -05001863 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001864 return posted;
1865err:
1866 spin_unlock_irqrestore(&phba->hbalock, flags);
1867 while (!list_empty(&hbq_buf_list)) {
1868 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1869 dbuf.list);
1870 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1871 }
James Smart92d7f7b2007-06-17 19:56:38 -05001872 return 0;
James Smarted957682007-06-17 19:56:37 -05001873}
1874
James Smarte59058c2008-08-24 21:49:00 -04001875/**
James Smart3621a712009-04-06 18:47:14 -04001876 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001877 * @phba: Pointer to HBA context object.
1878 * @qno: HBQ number.
1879 *
1880 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001881 * is called with no lock held. The function returns the number of HBQ entries
1882 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001883 **/
James Smarted957682007-06-17 19:56:37 -05001884int
James Smart92d7f7b2007-06-17 19:56:38 -05001885lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001886{
James Smartdef9c7a2009-12-21 17:02:28 -05001887 if (phba->sli_rev == LPFC_SLI_REV4)
1888 return 0;
1889 else
1890 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1891 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001892}
1893
James Smarte59058c2008-08-24 21:49:00 -04001894/**
James Smart3621a712009-04-06 18:47:14 -04001895 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001896 * @phba: Pointer to HBA context object.
1897 * @qno: HBQ queue number.
1898 *
1899 * This function is called from SLI initialization code path with
1900 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001901 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001902 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001903static int
James Smart92d7f7b2007-06-17 19:56:38 -05001904lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001905{
James Smartdef9c7a2009-12-21 17:02:28 -05001906 if (phba->sli_rev == LPFC_SLI_REV4)
1907 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001908 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001909 else
1910 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1911 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001912}
1913
James Smarte59058c2008-08-24 21:49:00 -04001914/**
James Smart3772a992009-05-22 14:50:54 -04001915 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1916 * @phba: Pointer to HBA context object.
1917 * @hbqno: HBQ number.
1918 *
1919 * This function removes the first hbq buffer on an hbq list and returns a
1920 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1921 **/
1922static struct hbq_dmabuf *
1923lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1924{
1925 struct lpfc_dmabuf *d_buf;
1926
1927 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1928 if (!d_buf)
1929 return NULL;
1930 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1931}
1932
1933/**
James Smart3621a712009-04-06 18:47:14 -04001934 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001935 * @phba: Pointer to HBA context object.
1936 * @tag: Tag of the hbq buffer.
1937 *
1938 * This function is called with hbalock held. This function searches
1939 * for the hbq buffer associated with the given tag in the hbq buffer
1940 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1941 * it returns NULL.
1942 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001943static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001944lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1945{
James Smart92d7f7b2007-06-17 19:56:38 -05001946 struct lpfc_dmabuf *d_buf;
1947 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001948 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001949
James Smart51ef4c22007-08-02 11:10:31 -04001950 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001951 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001952 return NULL;
1953
James Smart3772a992009-05-22 14:50:54 -04001954 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001955 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001956 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001957 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001958 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001959 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001960 }
1961 }
James Smart3772a992009-05-22 14:50:54 -04001962 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001964 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001965 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001966 return NULL;
James Smarted957682007-06-17 19:56:37 -05001967}
1968
James Smarte59058c2008-08-24 21:49:00 -04001969/**
James Smart3621a712009-04-06 18:47:14 -04001970 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001971 * @phba: Pointer to HBA context object.
1972 * @hbq_buffer: Pointer to HBQ buffer.
1973 *
1974 * This function is called with hbalock. This function gives back
1975 * the hbq buffer to firmware. If the HBQ does not have space to
1976 * post the buffer, it will free the buffer.
1977 **/
James Smarted957682007-06-17 19:56:37 -05001978void
James Smart51ef4c22007-08-02 11:10:31 -04001979lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001980{
1981 uint32_t hbqno;
1982
James Smart51ef4c22007-08-02 11:10:31 -04001983 if (hbq_buffer) {
1984 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001985 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001986 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001987 }
1988}
1989
James Smarte59058c2008-08-24 21:49:00 -04001990/**
James Smart3621a712009-04-06 18:47:14 -04001991 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001992 * @mbxCommand: mailbox command code.
1993 *
1994 * This function is called by the mailbox event handler function to verify
1995 * that the completed mailbox command is a legitimate mailbox command. If the
1996 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1997 * and the mailbox event handler will take the HBA offline.
1998 **/
dea31012005-04-17 16:05:31 -05001999static int
2000lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2001{
2002 uint8_t ret;
2003
2004 switch (mbxCommand) {
2005 case MBX_LOAD_SM:
2006 case MBX_READ_NV:
2007 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002008 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002009 case MBX_RUN_BIU_DIAG:
2010 case MBX_INIT_LINK:
2011 case MBX_DOWN_LINK:
2012 case MBX_CONFIG_LINK:
2013 case MBX_CONFIG_RING:
2014 case MBX_RESET_RING:
2015 case MBX_READ_CONFIG:
2016 case MBX_READ_RCONFIG:
2017 case MBX_READ_SPARM:
2018 case MBX_READ_STATUS:
2019 case MBX_READ_RPI:
2020 case MBX_READ_XRI:
2021 case MBX_READ_REV:
2022 case MBX_READ_LNK_STAT:
2023 case MBX_REG_LOGIN:
2024 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002025 case MBX_CLEAR_LA:
2026 case MBX_DUMP_MEMORY:
2027 case MBX_DUMP_CONTEXT:
2028 case MBX_RUN_DIAGS:
2029 case MBX_RESTART:
2030 case MBX_UPDATE_CFG:
2031 case MBX_DOWN_LOAD:
2032 case MBX_DEL_LD_ENTRY:
2033 case MBX_RUN_PROGRAM:
2034 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002035 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002036 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002037 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002038 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002039 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002040 case MBX_LOAD_AREA:
2041 case MBX_RUN_BIU_DIAG64:
2042 case MBX_CONFIG_PORT:
2043 case MBX_READ_SPARM64:
2044 case MBX_READ_RPI64:
2045 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002046 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002047 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002048 case MBX_SET_DEBUG:
2049 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002050 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002051 case MBX_REG_VPI:
2052 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002053 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002054 case MBX_PORT_CAPABILITIES:
2055 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002056 case MBX_SLI4_CONFIG:
2057 case MBX_SLI4_REQ_FTRS:
2058 case MBX_REG_FCFI:
2059 case MBX_UNREG_FCFI:
2060 case MBX_REG_VFI:
2061 case MBX_UNREG_VFI:
2062 case MBX_INIT_VPI:
2063 case MBX_INIT_VFI:
2064 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002065 case MBX_READ_EVENT_LOG_STATUS:
2066 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002067 case MBX_SECURITY_MGMT:
2068 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002069 ret = mbxCommand;
2070 break;
2071 default:
2072 ret = MBX_SHUTDOWN;
2073 break;
2074 }
James Smart2e0fef82007-06-17 19:56:36 -05002075 return ret;
dea31012005-04-17 16:05:31 -05002076}
James Smarte59058c2008-08-24 21:49:00 -04002077
2078/**
James Smart3621a712009-04-06 18:47:14 -04002079 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002080 * @phba: Pointer to HBA context object.
2081 * @pmboxq: Pointer to mailbox command.
2082 *
2083 * This is completion handler function for mailbox commands issued from
2084 * lpfc_sli_issue_mbox_wait function. This function is called by the
2085 * mailbox event handler function with no lock held. This function
2086 * will wake up thread waiting on the wait queue pointed by context1
2087 * of the mailbox.
2088 **/
James Smart04c68492009-05-22 14:52:52 -04002089void
James Smart2e0fef82007-06-17 19:56:36 -05002090lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002091{
2092 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002093 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002094
2095 /*
2096 * If pdone_q is empty, the driver thread gave up waiting and
2097 * continued running.
2098 */
James Smart7054a602007-04-25 09:52:34 -04002099 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002100 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002101 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2102 if (pdone_q)
2103 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002104 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002105 return;
2106}
2107
James Smarte59058c2008-08-24 21:49:00 -04002108
2109/**
James Smart3621a712009-04-06 18:47:14 -04002110 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002111 * @phba: Pointer to HBA context object.
2112 * @pmb: Pointer to mailbox object.
2113 *
2114 * This function is the default mailbox completion handler. It
2115 * frees the memory resources associated with the completed mailbox
2116 * command. If the completed command is a REG_LOGIN mailbox command,
2117 * this function will issue a UREG_LOGIN to re-claim the RPI.
2118 **/
dea31012005-04-17 16:05:31 -05002119void
James Smart2e0fef82007-06-17 19:56:36 -05002120lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002121{
James Smartd439d282010-09-29 11:18:45 -04002122 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002123 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002124 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002125 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002126 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002127 int rc;
2128
dea31012005-04-17 16:05:31 -05002129 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002130
dea31012005-04-17 16:05:31 -05002131 if (mp) {
2132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2133 kfree(mp);
2134 }
James Smart7054a602007-04-25 09:52:34 -04002135
2136 /*
2137 * If a REG_LOGIN succeeded after node is destroyed or node
2138 * is in re-discovery driver need to cleanup the RPI.
2139 */
James Smart2e0fef82007-06-17 19:56:36 -05002140 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002141 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2142 !pmb->u.mb.mbxStatus) {
2143 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002144 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002145 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002146 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002147 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2148 if (rc != MBX_NOT_FINISHED)
2149 return;
2150 }
2151
James Smart695a8142010-01-26 23:08:03 -05002152 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2153 !(phba->pport->load_flag & FC_UNLOADING) &&
2154 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002155 shost = lpfc_shost_from_vport(vport);
2156 spin_lock_irq(shost->host_lock);
2157 vport->vpi_state |= LPFC_VPI_REGISTERED;
2158 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2159 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002160 }
2161
James Smartd439d282010-09-29 11:18:45 -04002162 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2163 ndlp = (struct lpfc_nodelist *)pmb->context2;
2164 lpfc_nlp_put(ndlp);
2165 pmb->context2 = NULL;
2166 }
2167
James Smartdcf2a4e2010-09-29 11:18:53 -04002168 /* Check security permission status on INIT_LINK mailbox command */
2169 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2170 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2171 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2172 "2860 SLI authentication is required "
2173 "for INIT_LINK but has not done yet\n");
2174
James Smart04c68492009-05-22 14:52:52 -04002175 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2176 lpfc_sli4_mbox_cmd_free(phba, pmb);
2177 else
2178 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002179}
2180
James Smarte59058c2008-08-24 21:49:00 -04002181/**
James Smart3621a712009-04-06 18:47:14 -04002182 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002183 * @phba: Pointer to HBA context object.
2184 *
2185 * This function is called with no lock held. This function processes all
2186 * the completed mailbox commands and gives it to upper layers. The interrupt
2187 * service routine processes mailbox completion interrupt and adds completed
2188 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2189 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2190 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2191 * function returns the mailbox commands to the upper layer by calling the
2192 * completion handler function of each mailbox.
2193 **/
dea31012005-04-17 16:05:31 -05002194int
James Smart2e0fef82007-06-17 19:56:36 -05002195lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002196{
James Smart92d7f7b2007-06-17 19:56:38 -05002197 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002198 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002199 int rc;
2200 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002201
2202 phba->sli.slistat.mbox_event++;
2203
James Smart92d7f7b2007-06-17 19:56:38 -05002204 /* Get all completed mailboxe buffers into the cmplq */
2205 spin_lock_irq(&phba->hbalock);
2206 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2207 spin_unlock_irq(&phba->hbalock);
2208
dea31012005-04-17 16:05:31 -05002209 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002210 do {
2211 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2212 if (pmb == NULL)
2213 break;
2214
James Smart04c68492009-05-22 14:52:52 -04002215 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002216
James Smart858c9f62007-06-17 19:56:39 -05002217 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2218 if (pmb->vport) {
2219 lpfc_debugfs_disc_trc(pmb->vport,
2220 LPFC_DISC_TRC_MBOX_VPORT,
2221 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2222 (uint32_t)pmbox->mbxCommand,
2223 pmbox->un.varWords[0],
2224 pmbox->un.varWords[1]);
2225 }
2226 else {
2227 lpfc_debugfs_disc_trc(phba->pport,
2228 LPFC_DISC_TRC_MBOX,
2229 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2230 (uint32_t)pmbox->mbxCommand,
2231 pmbox->un.varWords[0],
2232 pmbox->un.varWords[1]);
2233 }
2234 }
2235
dea31012005-04-17 16:05:31 -05002236 /*
2237 * It is a fatal error if unknown mbox command completion.
2238 */
2239 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2240 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002241 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002242 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002243 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002244 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002245 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002246 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002247 lpfc_sli_config_mbox_subsys_get(phba,
2248 pmb),
2249 lpfc_sli_config_mbox_opcode_get(phba,
2250 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002251 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002252 phba->work_hs = HS_FFER3;
2253 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002254 continue;
dea31012005-04-17 16:05:31 -05002255 }
2256
dea31012005-04-17 16:05:31 -05002257 if (pmbox->mbxStatus) {
2258 phba->sli.slistat.mbox_stat_err++;
2259 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2260 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002261 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002262 LOG_MBOX | LOG_SLI,
2263 "(%d):0305 Mbox cmd cmpl "
2264 "error - RETRYing Data: x%x "
2265 "(x%x/x%x) x%x x%x x%x\n",
2266 pmb->vport ? pmb->vport->vpi : 0,
2267 pmbox->mbxCommand,
2268 lpfc_sli_config_mbox_subsys_get(phba,
2269 pmb),
2270 lpfc_sli_config_mbox_opcode_get(phba,
2271 pmb),
2272 pmbox->mbxStatus,
2273 pmbox->un.varWords[0],
2274 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002275 pmbox->mbxStatus = 0;
2276 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002277 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002278 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002279 continue;
dea31012005-04-17 16:05:31 -05002280 }
2281 }
2282
2283 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002284 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002285 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002286 "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 -05002287 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002288 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002289 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2290 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002291 pmb->mbox_cmpl,
2292 *((uint32_t *) pmbox),
2293 pmbox->un.varWords[0],
2294 pmbox->un.varWords[1],
2295 pmbox->un.varWords[2],
2296 pmbox->un.varWords[3],
2297 pmbox->un.varWords[4],
2298 pmbox->un.varWords[5],
2299 pmbox->un.varWords[6],
2300 pmbox->un.varWords[7]);
2301
James Smart92d7f7b2007-06-17 19:56:38 -05002302 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002303 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002304 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002305 return 0;
dea31012005-04-17 16:05:31 -05002306}
James Smart92d7f7b2007-06-17 19:56:38 -05002307
James Smarte59058c2008-08-24 21:49:00 -04002308/**
James Smart3621a712009-04-06 18:47:14 -04002309 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002310 * @phba: Pointer to HBA context object.
2311 * @pring: Pointer to driver SLI ring object.
2312 * @tag: buffer tag.
2313 *
2314 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2315 * is set in the tag the buffer is posted for a particular exchange,
2316 * the function will return the buffer without replacing the buffer.
2317 * If the buffer is for unsolicited ELS or CT traffic, this function
2318 * returns the buffer and also posts another buffer to the firmware.
2319 **/
James Smart76bb24e2007-10-27 13:38:00 -04002320static struct lpfc_dmabuf *
2321lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002322 struct lpfc_sli_ring *pring,
2323 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002324{
James Smart9f1e1b52008-12-04 22:39:40 -05002325 struct hbq_dmabuf *hbq_entry;
2326
James Smart76bb24e2007-10-27 13:38:00 -04002327 if (tag & QUE_BUFTAG_BIT)
2328 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002329 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2330 if (!hbq_entry)
2331 return NULL;
2332 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002333}
James Smart57127f12007-10-27 13:37:05 -04002334
James Smart3772a992009-05-22 14:50:54 -04002335/**
2336 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2337 * @phba: Pointer to HBA context object.
2338 * @pring: Pointer to driver SLI ring object.
2339 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2340 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2341 * @fch_type: the type for the first frame of the sequence.
2342 *
2343 * This function is called with no lock held. This function uses the r_ctl and
2344 * type of the received sequence to find the correct callback function to call
2345 * to process the sequence.
2346 **/
2347static int
2348lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2349 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2350 uint32_t fch_type)
2351{
2352 int i;
2353
2354 /* unSolicited Responses */
2355 if (pring->prt[0].profile) {
2356 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2357 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2358 saveq);
2359 return 1;
2360 }
2361 /* We must search, based on rctl / type
2362 for the right routine */
2363 for (i = 0; i < pring->num_mask; i++) {
2364 if ((pring->prt[i].rctl == fch_r_ctl) &&
2365 (pring->prt[i].type == fch_type)) {
2366 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2367 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2368 (phba, pring, saveq);
2369 return 1;
2370 }
2371 }
2372 return 0;
2373}
James Smarte59058c2008-08-24 21:49:00 -04002374
2375/**
James Smart3621a712009-04-06 18:47:14 -04002376 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002377 * @phba: Pointer to HBA context object.
2378 * @pring: Pointer to driver SLI ring object.
2379 * @saveq: Pointer to the unsolicited iocb.
2380 *
2381 * This function is called with no lock held by the ring event handler
2382 * when there is an unsolicited iocb posted to the response ring by the
2383 * firmware. This function gets the buffer associated with the iocbs
2384 * and calls the event handler for the ring. This function handles both
2385 * qring buffers and hbq buffers.
2386 * When the function returns 1 the caller can free the iocb object otherwise
2387 * upper layer functions will free the iocb objects.
2388 **/
dea31012005-04-17 16:05:31 -05002389static int
2390lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2391 struct lpfc_iocbq *saveq)
2392{
2393 IOCB_t * irsp;
2394 WORD5 * w5p;
2395 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002396 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002397 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002398 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002399
2400 match = 0;
2401 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002402
2403 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2404 if (pring->lpfc_sli_rcv_async_status)
2405 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2406 else
2407 lpfc_printf_log(phba,
2408 KERN_WARNING,
2409 LOG_SLI,
2410 "0316 Ring %d handler: unexpected "
2411 "ASYNC_STATUS iocb received evt_code "
2412 "0x%x\n",
2413 pring->ringno,
2414 irsp->un.asyncstat.evt_code);
2415 return 1;
2416 }
2417
James Smart3163f722008-02-08 18:50:25 -05002418 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2419 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2420 if (irsp->ulpBdeCount > 0) {
2421 dmzbuf = lpfc_sli_get_buff(phba, pring,
2422 irsp->un.ulpWord[3]);
2423 lpfc_in_buf_free(phba, dmzbuf);
2424 }
2425
2426 if (irsp->ulpBdeCount > 1) {
2427 dmzbuf = lpfc_sli_get_buff(phba, pring,
2428 irsp->unsli3.sli3Words[3]);
2429 lpfc_in_buf_free(phba, dmzbuf);
2430 }
2431
2432 if (irsp->ulpBdeCount > 2) {
2433 dmzbuf = lpfc_sli_get_buff(phba, pring,
2434 irsp->unsli3.sli3Words[7]);
2435 lpfc_in_buf_free(phba, dmzbuf);
2436 }
2437
2438 return 1;
2439 }
2440
James Smart92d7f7b2007-06-17 19:56:38 -05002441 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002442 if (irsp->ulpBdeCount != 0) {
2443 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002444 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002445 if (!saveq->context2)
2446 lpfc_printf_log(phba,
2447 KERN_ERR,
2448 LOG_SLI,
2449 "0341 Ring %d Cannot find buffer for "
2450 "an unsolicited iocb. tag 0x%x\n",
2451 pring->ringno,
2452 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002453 }
2454 if (irsp->ulpBdeCount == 2) {
2455 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002456 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002457 if (!saveq->context3)
2458 lpfc_printf_log(phba,
2459 KERN_ERR,
2460 LOG_SLI,
2461 "0342 Ring %d Cannot find buffer for an"
2462 " unsolicited iocb. tag 0x%x\n",
2463 pring->ringno,
2464 irsp->unsli3.sli3Words[7]);
2465 }
2466 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002467 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002468 if (irsp->ulpBdeCount != 0) {
2469 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2470 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002471 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002472 lpfc_printf_log(phba,
2473 KERN_ERR,
2474 LOG_SLI,
2475 "0343 Ring %d Cannot find "
2476 "buffer for an unsolicited iocb"
2477 ". tag 0x%x\n", pring->ringno,
2478 irsp->un.ulpWord[3]);
2479 }
2480 if (irsp->ulpBdeCount == 2) {
2481 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2482 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002483 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002484 lpfc_printf_log(phba,
2485 KERN_ERR,
2486 LOG_SLI,
2487 "0344 Ring %d Cannot find "
2488 "buffer for an unsolicited "
2489 "iocb. tag 0x%x\n",
2490 pring->ringno,
2491 irsp->unsli3.sli3Words[7]);
2492 }
2493 }
James Smart92d7f7b2007-06-17 19:56:38 -05002494 }
James Smart9c2face2008-01-11 01:53:18 -05002495 if (irsp->ulpBdeCount != 0 &&
2496 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2497 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2498 int found = 0;
2499
2500 /* search continue save q for same XRI */
2501 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002502 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2503 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002504 list_add_tail(&saveq->list, &iocbq->list);
2505 found = 1;
2506 break;
2507 }
2508 }
2509 if (!found)
2510 list_add_tail(&saveq->clist,
2511 &pring->iocb_continue_saveq);
2512 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2513 list_del_init(&iocbq->clist);
2514 saveq = iocbq;
2515 irsp = &(saveq->iocb);
2516 } else
2517 return 0;
2518 }
2519 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2520 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2521 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002522 Rctl = FC_RCTL_ELS_REQ;
2523 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002524 } else {
2525 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2526 Rctl = w5p->hcsw.Rctl;
2527 Type = w5p->hcsw.Type;
2528
2529 /* Firmware Workaround */
2530 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2531 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2532 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002533 Rctl = FC_RCTL_ELS_REQ;
2534 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002535 w5p->hcsw.Rctl = Rctl;
2536 w5p->hcsw.Type = Type;
2537 }
2538 }
James Smart92d7f7b2007-06-17 19:56:38 -05002539
James Smart3772a992009-05-22 14:50:54 -04002540 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002541 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002542 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002543 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002544 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002545
James Smart92d7f7b2007-06-17 19:56:38 -05002546 return 1;
dea31012005-04-17 16:05:31 -05002547}
2548
James Smarte59058c2008-08-24 21:49:00 -04002549/**
James Smart3621a712009-04-06 18:47:14 -04002550 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002551 * @phba: Pointer to HBA context object.
2552 * @pring: Pointer to driver SLI ring object.
2553 * @prspiocb: Pointer to response iocb object.
2554 *
2555 * This function looks up the iocb_lookup table to get the command iocb
2556 * corresponding to the given response iocb using the iotag of the
2557 * response iocb. This function is called with the hbalock held.
2558 * This function returns the command iocb object if it finds the command
2559 * iocb else returns NULL.
2560 **/
dea31012005-04-17 16:05:31 -05002561static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002562lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2563 struct lpfc_sli_ring *pring,
2564 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002565{
dea31012005-04-17 16:05:31 -05002566 struct lpfc_iocbq *cmd_iocb = NULL;
2567 uint16_t iotag;
2568
James Bottomley604a3e32005-10-29 10:28:33 -05002569 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002570
James Bottomley604a3e32005-10-29 10:28:33 -05002571 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2572 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002573 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002574 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart2a9bf3d2010-06-07 15:24:45 -04002575 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002576 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002577 }
James Bottomley604a3e32005-10-29 10:28:33 -05002578 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002579 }
2580
dea31012005-04-17 16:05:31 -05002581 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002582 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002583 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002584 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002585 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002586 return NULL;
2587}
2588
James Smarte59058c2008-08-24 21:49:00 -04002589/**
James Smart3772a992009-05-22 14:50:54 -04002590 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2591 * @phba: Pointer to HBA context object.
2592 * @pring: Pointer to driver SLI ring object.
2593 * @iotag: IOCB tag.
2594 *
2595 * This function looks up the iocb_lookup table to get the command iocb
2596 * corresponding to the given iotag. This function is called with the
2597 * hbalock held.
2598 * This function returns the command iocb object if it finds the command
2599 * iocb else returns NULL.
2600 **/
2601static struct lpfc_iocbq *
2602lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2603 struct lpfc_sli_ring *pring, uint16_t iotag)
2604{
2605 struct lpfc_iocbq *cmd_iocb;
2606
2607 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2608 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002609 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2610 /* remove from txcmpl queue list */
2611 list_del_init(&cmd_iocb->list);
2612 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002613 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002614 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002615 }
James Smart3772a992009-05-22 14:50:54 -04002616 }
James Smart3772a992009-05-22 14:50:54 -04002617 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2618 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2619 iotag, phba->sli.last_iotag);
2620 return NULL;
2621}
2622
2623/**
James Smart3621a712009-04-06 18:47:14 -04002624 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002625 * @phba: Pointer to HBA context object.
2626 * @pring: Pointer to driver SLI ring object.
2627 * @saveq: Pointer to the response iocb to be processed.
2628 *
2629 * This function is called by the ring event handler for non-fcp
2630 * rings when there is a new response iocb in the response ring.
2631 * The caller is not required to hold any locks. This function
2632 * gets the command iocb associated with the response iocb and
2633 * calls the completion handler for the command iocb. If there
2634 * is no completion handler, the function will free the resources
2635 * associated with command iocb. If the response iocb is for
2636 * an already aborted command iocb, the status of the completion
2637 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2638 * This function always returns 1.
2639 **/
dea31012005-04-17 16:05:31 -05002640static int
James Smart2e0fef82007-06-17 19:56:36 -05002641lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002642 struct lpfc_iocbq *saveq)
2643{
James Smart2e0fef82007-06-17 19:56:36 -05002644 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002645 int rc = 1;
2646 unsigned long iflag;
2647
2648 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002649 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002650 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002651 spin_unlock_irqrestore(&phba->hbalock, iflag);
2652
dea31012005-04-17 16:05:31 -05002653 if (cmdiocbp) {
2654 if (cmdiocbp->iocb_cmpl) {
2655 /*
James Smartea2151b2008-09-07 11:52:10 -04002656 * If an ELS command failed send an event to mgmt
2657 * application.
2658 */
2659 if (saveq->iocb.ulpStatus &&
2660 (pring->ringno == LPFC_ELS_RING) &&
2661 (cmdiocbp->iocb.ulpCommand ==
2662 CMD_ELS_REQUEST64_CR))
2663 lpfc_send_els_failure_event(phba,
2664 cmdiocbp, saveq);
2665
2666 /*
dea31012005-04-17 16:05:31 -05002667 * Post all ELS completions to the worker thread.
2668 * All other are passed to the completion callback.
2669 */
2670 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002671 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2672 (cmdiocbp->iocb_flag &
2673 LPFC_DRIVER_ABORTED)) {
2674 spin_lock_irqsave(&phba->hbalock,
2675 iflag);
James Smart07951072007-04-25 09:51:38 -04002676 cmdiocbp->iocb_flag &=
2677 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002678 spin_unlock_irqrestore(&phba->hbalock,
2679 iflag);
James Smart07951072007-04-25 09:51:38 -04002680 saveq->iocb.ulpStatus =
2681 IOSTAT_LOCAL_REJECT;
2682 saveq->iocb.un.ulpWord[4] =
2683 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002684
2685 /* Firmware could still be in progress
2686 * of DMAing payload, so don't free data
2687 * buffer till after a hbeat.
2688 */
James Smart341af102010-01-26 23:07:37 -05002689 spin_lock_irqsave(&phba->hbalock,
2690 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002691 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002692 spin_unlock_irqrestore(&phba->hbalock,
2693 iflag);
2694 }
James Smart0f65ff62010-02-26 14:14:23 -05002695 if (phba->sli_rev == LPFC_SLI_REV4) {
2696 if (saveq->iocb_flag &
2697 LPFC_EXCHANGE_BUSY) {
2698 /* Set cmdiocb flag for the
2699 * exchange busy so sgl (xri)
2700 * will not be released until
2701 * the abort xri is received
2702 * from hba.
2703 */
2704 spin_lock_irqsave(
2705 &phba->hbalock, iflag);
2706 cmdiocbp->iocb_flag |=
2707 LPFC_EXCHANGE_BUSY;
2708 spin_unlock_irqrestore(
2709 &phba->hbalock, iflag);
2710 }
2711 if (cmdiocbp->iocb_flag &
2712 LPFC_DRIVER_ABORTED) {
2713 /*
2714 * Clear LPFC_DRIVER_ABORTED
2715 * bit in case it was driver
2716 * initiated abort.
2717 */
2718 spin_lock_irqsave(
2719 &phba->hbalock, iflag);
2720 cmdiocbp->iocb_flag &=
2721 ~LPFC_DRIVER_ABORTED;
2722 spin_unlock_irqrestore(
2723 &phba->hbalock, iflag);
2724 cmdiocbp->iocb.ulpStatus =
2725 IOSTAT_LOCAL_REJECT;
2726 cmdiocbp->iocb.un.ulpWord[4] =
2727 IOERR_ABORT_REQUESTED;
2728 /*
2729 * For SLI4, irsiocb contains
2730 * NO_XRI in sli_xritag, it
2731 * shall not affect releasing
2732 * sgl (xri) process.
2733 */
2734 saveq->iocb.ulpStatus =
2735 IOSTAT_LOCAL_REJECT;
2736 saveq->iocb.un.ulpWord[4] =
2737 IOERR_SLI_ABORTED;
2738 spin_lock_irqsave(
2739 &phba->hbalock, iflag);
2740 saveq->iocb_flag |=
2741 LPFC_DELAY_MEM_FREE;
2742 spin_unlock_irqrestore(
2743 &phba->hbalock, iflag);
2744 }
James Smart07951072007-04-25 09:51:38 -04002745 }
dea31012005-04-17 16:05:31 -05002746 }
James Smart2e0fef82007-06-17 19:56:36 -05002747 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002748 } else
2749 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002750 } else {
2751 /*
2752 * Unknown initiating command based on the response iotag.
2753 * This could be the case on the ELS ring because of
2754 * lpfc_els_abort().
2755 */
2756 if (pring->ringno != LPFC_ELS_RING) {
2757 /*
2758 * Ring <ringno> handler: unexpected completion IoTag
2759 * <IoTag>
2760 */
James Smarta257bf92009-04-06 18:48:10 -04002761 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002762 "0322 Ring %d handler: "
2763 "unexpected completion IoTag x%x "
2764 "Data: x%x x%x x%x x%x\n",
2765 pring->ringno,
2766 saveq->iocb.ulpIoTag,
2767 saveq->iocb.ulpStatus,
2768 saveq->iocb.un.ulpWord[4],
2769 saveq->iocb.ulpCommand,
2770 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002771 }
2772 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002773
dea31012005-04-17 16:05:31 -05002774 return rc;
2775}
2776
James Smarte59058c2008-08-24 21:49:00 -04002777/**
James Smart3621a712009-04-06 18:47:14 -04002778 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002779 * @phba: Pointer to HBA context object.
2780 * @pring: Pointer to driver SLI ring object.
2781 *
2782 * This function is called from the iocb ring event handlers when
2783 * put pointer is ahead of the get pointer for a ring. This function signal
2784 * an error attention condition to the worker thread and the worker
2785 * thread will transition the HBA to offline state.
2786 **/
James Smart2e0fef82007-06-17 19:56:36 -05002787static void
2788lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002789{
James Smart34b02dc2008-08-24 21:49:55 -04002790 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002791 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002792 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002793 * rsp ring <portRspMax>
2794 */
2795 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002796 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002797 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002798 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002799 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002800
James Smart2e0fef82007-06-17 19:56:36 -05002801 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002802
2803 /*
2804 * All error attention handlers are posted to
2805 * worker thread
2806 */
2807 phba->work_ha |= HA_ERATT;
2808 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002809
James Smart5e9d9b82008-06-14 22:52:53 -04002810 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002811
2812 return;
2813}
2814
James Smarte59058c2008-08-24 21:49:00 -04002815/**
James Smart3621a712009-04-06 18:47:14 -04002816 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002817 * @ptr: Pointer to address of HBA context object.
2818 *
2819 * This function is invoked by the Error Attention polling timer when the
2820 * timer times out. It will check the SLI Error Attention register for
2821 * possible attention events. If so, it will post an Error Attention event
2822 * and wake up worker thread to process it. Otherwise, it will set up the
2823 * Error Attention polling timer for the next poll.
2824 **/
2825void lpfc_poll_eratt(unsigned long ptr)
2826{
2827 struct lpfc_hba *phba;
2828 uint32_t eratt = 0;
2829
2830 phba = (struct lpfc_hba *)ptr;
2831
2832 /* Check chip HA register for error event */
2833 eratt = lpfc_sli_check_eratt(phba);
2834
2835 if (eratt)
2836 /* Tell the worker thread there is work to do */
2837 lpfc_worker_wake_up(phba);
2838 else
2839 /* Restart the timer for next eratt poll */
2840 mod_timer(&phba->eratt_poll, jiffies +
2841 HZ * LPFC_ERATT_POLL_INTERVAL);
2842 return;
2843}
2844
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002845
James Smarte59058c2008-08-24 21:49:00 -04002846/**
James Smart3621a712009-04-06 18:47:14 -04002847 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002848 * @phba: Pointer to HBA context object.
2849 * @pring: Pointer to driver SLI ring object.
2850 * @mask: Host attention register mask for this ring.
2851 *
2852 * This function is called from the interrupt context when there is a ring
2853 * event for the fcp ring. The caller does not hold any lock.
2854 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002855 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002856 * LE bit set. The function will call the completion handler of the command iocb
2857 * if the response iocb indicates a completion for a command iocb or it is
2858 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2859 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002860 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002861 * to check it explicitly.
2862 */
2863int
James Smart2e0fef82007-06-17 19:56:36 -05002864lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2865 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002866{
James Smart34b02dc2008-08-24 21:49:55 -04002867 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002868 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002869 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002870 struct lpfc_iocbq *cmdiocbq = NULL;
2871 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002872 uint32_t status;
2873 uint32_t portRspPut, portRspMax;
2874 int rc = 1;
2875 lpfc_iocb_type type;
2876 unsigned long iflag;
2877 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002878
James Smart2e0fef82007-06-17 19:56:36 -05002879 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002880 pring->stats.iocb_event++;
2881
dea31012005-04-17 16:05:31 -05002882 /*
2883 * The next available response entry should never exceed the maximum
2884 * entries. If it does, treat it as an adapter hardware error.
2885 */
James Smart7e56aa22012-08-03 12:35:34 -04002886 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002887 portRspPut = le32_to_cpu(pgp->rspPutInx);
2888 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002889 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002890 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002891 return 1;
2892 }
James Smart45ed1192009-10-02 15:17:02 -04002893 if (phba->fcp_ring_in_use) {
2894 spin_unlock_irqrestore(&phba->hbalock, iflag);
2895 return 1;
2896 } else
2897 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002898
2899 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002900 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002901 /*
2902 * Fetch an entry off the ring and copy it into a local data
2903 * structure. The copy involves a byte-swap since the
2904 * network byte order and pci byte orders are different.
2905 */
James Smarted957682007-06-17 19:56:37 -05002906 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002907 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002908
James Smart7e56aa22012-08-03 12:35:34 -04002909 if (++pring->sli.sli3.rspidx >= portRspMax)
2910 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002911
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002912 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2913 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002914 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002915 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002916 irsp = &rspiocbq.iocb;
2917
dea31012005-04-17 16:05:31 -05002918 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2919 pring->stats.iocb_rsp++;
2920 rsp_cmpl++;
2921
2922 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002923 /*
2924 * If resource errors reported from HBA, reduce
2925 * queuedepths of the SCSI device.
2926 */
2927 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2928 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2929 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002930 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002931 spin_lock_irqsave(&phba->hbalock, iflag);
2932 }
2933
dea31012005-04-17 16:05:31 -05002934 /* Rsp ring <ringno> error: IOCB */
2935 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002936 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002937 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002938 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002939 irsp->un.ulpWord[0],
2940 irsp->un.ulpWord[1],
2941 irsp->un.ulpWord[2],
2942 irsp->un.ulpWord[3],
2943 irsp->un.ulpWord[4],
2944 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002945 *(uint32_t *)&irsp->un1,
2946 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002947 }
2948
2949 switch (type) {
2950 case LPFC_ABORT_IOCB:
2951 case LPFC_SOL_IOCB:
2952 /*
2953 * Idle exchange closed via ABTS from port. No iocb
2954 * resources need to be recovered.
2955 */
2956 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002957 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002958 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002959 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002960 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002961 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002962 break;
2963 }
2964
James Bottomley604a3e32005-10-29 10:28:33 -05002965 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2966 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002967 if (unlikely(!cmdiocbq))
2968 break;
2969 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2970 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2971 if (cmdiocbq->iocb_cmpl) {
2972 spin_unlock_irqrestore(&phba->hbalock, iflag);
2973 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2974 &rspiocbq);
2975 spin_lock_irqsave(&phba->hbalock, iflag);
2976 }
dea31012005-04-17 16:05:31 -05002977 break;
James Smarta4bc3372006-12-02 13:34:16 -05002978 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002979 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002980 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002981 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002982 break;
dea31012005-04-17 16:05:31 -05002983 default:
2984 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2985 char adaptermsg[LPFC_MAX_ADPTMSG];
2986 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2987 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2988 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002989 dev_warn(&((phba->pcidev)->dev),
2990 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002991 phba->brd_no, adaptermsg);
2992 } else {
2993 /* Unknown IOCB command */
2994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002995 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002996 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002997 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002998 irsp->ulpStatus,
2999 irsp->ulpIoTag,
3000 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003001 }
3002 break;
3003 }
3004
3005 /*
3006 * The response IOCB has been processed. Update the ring
3007 * pointer in SLIM. If the port response put pointer has not
3008 * been updated, sync the pgp->rspPutInx and fetch the new port
3009 * response put pointer.
3010 */
James Smart7e56aa22012-08-03 12:35:34 -04003011 writel(pring->sli.sli3.rspidx,
3012 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003013
James Smart7e56aa22012-08-03 12:35:34 -04003014 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003015 portRspPut = le32_to_cpu(pgp->rspPutInx);
3016 }
3017
3018 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3019 pring->stats.iocb_rsp_full++;
3020 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3021 writel(status, phba->CAregaddr);
3022 readl(phba->CAregaddr);
3023 }
3024 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3025 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3026 pring->stats.iocb_cmd_empty++;
3027
3028 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003029 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003030 lpfc_sli_resume_iocb(phba, pring);
3031
3032 if ((pring->lpfc_sli_cmd_available))
3033 (pring->lpfc_sli_cmd_available) (phba, pring);
3034
3035 }
3036
James Smart45ed1192009-10-02 15:17:02 -04003037 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003038 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003039 return rc;
3040}
3041
James Smarte59058c2008-08-24 21:49:00 -04003042/**
James Smart3772a992009-05-22 14:50:54 -04003043 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3044 * @phba: Pointer to HBA context object.
3045 * @pring: Pointer to driver SLI ring object.
3046 * @rspiocbp: Pointer to driver response IOCB object.
3047 *
3048 * This function is called from the worker thread when there is a slow-path
3049 * response IOCB to process. This function chains all the response iocbs until
3050 * seeing the iocb with the LE bit set. The function will call
3051 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3052 * completion of a command iocb. The function will call the
3053 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3054 * The function frees the resources or calls the completion handler if this
3055 * iocb is an abort completion. The function returns NULL when the response
3056 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3057 * this function shall chain the iocb on to the iocb_continueq and return the
3058 * response iocb passed in.
3059 **/
3060static struct lpfc_iocbq *
3061lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3062 struct lpfc_iocbq *rspiocbp)
3063{
3064 struct lpfc_iocbq *saveq;
3065 struct lpfc_iocbq *cmdiocbp;
3066 struct lpfc_iocbq *next_iocb;
3067 IOCB_t *irsp = NULL;
3068 uint32_t free_saveq;
3069 uint8_t iocb_cmd_type;
3070 lpfc_iocb_type type;
3071 unsigned long iflag;
3072 int rc;
3073
3074 spin_lock_irqsave(&phba->hbalock, iflag);
3075 /* First add the response iocb to the countinueq list */
3076 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3077 pring->iocb_continueq_cnt++;
3078
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003079 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003080 irsp = &rspiocbp->iocb;
3081 if (irsp->ulpLe) {
3082 /*
3083 * By default, the driver expects to free all resources
3084 * associated with this iocb completion.
3085 */
3086 free_saveq = 1;
3087 saveq = list_get_first(&pring->iocb_continueq,
3088 struct lpfc_iocbq, list);
3089 irsp = &(saveq->iocb);
3090 list_del_init(&pring->iocb_continueq);
3091 pring->iocb_continueq_cnt = 0;
3092
3093 pring->stats.iocb_rsp++;
3094
3095 /*
3096 * If resource errors reported from HBA, reduce
3097 * queuedepths of the SCSI device.
3098 */
3099 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3100 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3101 spin_unlock_irqrestore(&phba->hbalock, iflag);
3102 phba->lpfc_rampdown_queue_depth(phba);
3103 spin_lock_irqsave(&phba->hbalock, iflag);
3104 }
3105
3106 if (irsp->ulpStatus) {
3107 /* Rsp ring <ringno> error: IOCB */
3108 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3109 "0328 Rsp Ring %d error: "
3110 "IOCB Data: "
3111 "x%x x%x x%x x%x "
3112 "x%x x%x x%x x%x "
3113 "x%x x%x x%x x%x "
3114 "x%x x%x x%x x%x\n",
3115 pring->ringno,
3116 irsp->un.ulpWord[0],
3117 irsp->un.ulpWord[1],
3118 irsp->un.ulpWord[2],
3119 irsp->un.ulpWord[3],
3120 irsp->un.ulpWord[4],
3121 irsp->un.ulpWord[5],
3122 *(((uint32_t *) irsp) + 6),
3123 *(((uint32_t *) irsp) + 7),
3124 *(((uint32_t *) irsp) + 8),
3125 *(((uint32_t *) irsp) + 9),
3126 *(((uint32_t *) irsp) + 10),
3127 *(((uint32_t *) irsp) + 11),
3128 *(((uint32_t *) irsp) + 12),
3129 *(((uint32_t *) irsp) + 13),
3130 *(((uint32_t *) irsp) + 14),
3131 *(((uint32_t *) irsp) + 15));
3132 }
3133
3134 /*
3135 * Fetch the IOCB command type and call the correct completion
3136 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3137 * get freed back to the lpfc_iocb_list by the discovery
3138 * kernel thread.
3139 */
3140 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3141 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3142 switch (type) {
3143 case LPFC_SOL_IOCB:
3144 spin_unlock_irqrestore(&phba->hbalock, iflag);
3145 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3146 spin_lock_irqsave(&phba->hbalock, iflag);
3147 break;
3148
3149 case LPFC_UNSOL_IOCB:
3150 spin_unlock_irqrestore(&phba->hbalock, iflag);
3151 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3152 spin_lock_irqsave(&phba->hbalock, iflag);
3153 if (!rc)
3154 free_saveq = 0;
3155 break;
3156
3157 case LPFC_ABORT_IOCB:
3158 cmdiocbp = NULL;
3159 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3160 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3161 saveq);
3162 if (cmdiocbp) {
3163 /* Call the specified completion routine */
3164 if (cmdiocbp->iocb_cmpl) {
3165 spin_unlock_irqrestore(&phba->hbalock,
3166 iflag);
3167 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3168 saveq);
3169 spin_lock_irqsave(&phba->hbalock,
3170 iflag);
3171 } else
3172 __lpfc_sli_release_iocbq(phba,
3173 cmdiocbp);
3174 }
3175 break;
3176
3177 case LPFC_UNKNOWN_IOCB:
3178 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3179 char adaptermsg[LPFC_MAX_ADPTMSG];
3180 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3181 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3182 MAX_MSG_DATA);
3183 dev_warn(&((phba->pcidev)->dev),
3184 "lpfc%d: %s\n",
3185 phba->brd_no, adaptermsg);
3186 } else {
3187 /* Unknown IOCB command */
3188 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3189 "0335 Unknown IOCB "
3190 "command Data: x%x "
3191 "x%x x%x x%x\n",
3192 irsp->ulpCommand,
3193 irsp->ulpStatus,
3194 irsp->ulpIoTag,
3195 irsp->ulpContext);
3196 }
3197 break;
3198 }
3199
3200 if (free_saveq) {
3201 list_for_each_entry_safe(rspiocbp, next_iocb,
3202 &saveq->list, list) {
3203 list_del(&rspiocbp->list);
3204 __lpfc_sli_release_iocbq(phba, rspiocbp);
3205 }
3206 __lpfc_sli_release_iocbq(phba, saveq);
3207 }
3208 rspiocbp = NULL;
3209 }
3210 spin_unlock_irqrestore(&phba->hbalock, iflag);
3211 return rspiocbp;
3212}
3213
3214/**
3215 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003216 * @phba: Pointer to HBA context object.
3217 * @pring: Pointer to driver SLI ring object.
3218 * @mask: Host attention register mask for this ring.
3219 *
James Smart3772a992009-05-22 14:50:54 -04003220 * This routine wraps the actual slow_ring event process routine from the
3221 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003222 **/
James Smart3772a992009-05-22 14:50:54 -04003223void
James Smart2e0fef82007-06-17 19:56:36 -05003224lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3225 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003226{
James Smart3772a992009-05-22 14:50:54 -04003227 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3228}
3229
3230/**
3231 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3232 * @phba: Pointer to HBA context object.
3233 * @pring: Pointer to driver SLI ring object.
3234 * @mask: Host attention register mask for this ring.
3235 *
3236 * This function is called from the worker thread when there is a ring event
3237 * for non-fcp rings. The caller does not hold any lock. The function will
3238 * remove each response iocb in the response ring and calls the handle
3239 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3240 **/
3241static void
3242lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3243 struct lpfc_sli_ring *pring, uint32_t mask)
3244{
James Smart34b02dc2008-08-24 21:49:55 -04003245 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003246 IOCB_t *entry;
3247 IOCB_t *irsp = NULL;
3248 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003249 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003250 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003251 uint32_t status;
dea31012005-04-17 16:05:31 -05003252
James Smart34b02dc2008-08-24 21:49:55 -04003253 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003254 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003255 pring->stats.iocb_event++;
3256
dea31012005-04-17 16:05:31 -05003257 /*
3258 * The next available response entry should never exceed the maximum
3259 * entries. If it does, treat it as an adapter hardware error.
3260 */
James Smart7e56aa22012-08-03 12:35:34 -04003261 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003262 portRspPut = le32_to_cpu(pgp->rspPutInx);
3263 if (portRspPut >= portRspMax) {
3264 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003265 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003266 * rsp ring <portRspMax>
3267 */
James Smarted957682007-06-17 19:56:37 -05003268 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003269 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003270 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003271 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003272
James Smart2e0fef82007-06-17 19:56:36 -05003273 phba->link_state = LPFC_HBA_ERROR;
3274 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003275
3276 phba->work_hs = HS_FFER3;
3277 lpfc_handle_eratt(phba);
3278
James Smart3772a992009-05-22 14:50:54 -04003279 return;
dea31012005-04-17 16:05:31 -05003280 }
3281
3282 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003283 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003284 /*
3285 * Build a completion list and call the appropriate handler.
3286 * The process is to get the next available response iocb, get
3287 * a free iocb from the list, copy the response data into the
3288 * free iocb, insert to the continuation list, and update the
3289 * next response index to slim. This process makes response
3290 * iocb's in the ring available to DMA as fast as possible but
3291 * pays a penalty for a copy operation. Since the iocb is
3292 * only 32 bytes, this penalty is considered small relative to
3293 * the PCI reads for register values and a slim write. When
3294 * the ulpLe field is set, the entire Command has been
3295 * received.
3296 */
James Smarted957682007-06-17 19:56:37 -05003297 entry = lpfc_resp_iocb(phba, pring);
3298
James Smart858c9f62007-06-17 19:56:39 -05003299 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003300 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003301 if (rspiocbp == NULL) {
3302 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003303 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003304 break;
3305 }
3306
James Smarted957682007-06-17 19:56:37 -05003307 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3308 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003309 irsp = &rspiocbp->iocb;
3310
James Smart7e56aa22012-08-03 12:35:34 -04003311 if (++pring->sli.sli3.rspidx >= portRspMax)
3312 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003313
James Smarta58cbd52007-08-02 11:09:43 -04003314 if (pring->ringno == LPFC_ELS_RING) {
3315 lpfc_debugfs_slow_ring_trc(phba,
3316 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3317 *(((uint32_t *) irsp) + 4),
3318 *(((uint32_t *) irsp) + 6),
3319 *(((uint32_t *) irsp) + 7));
3320 }
3321
James Smart7e56aa22012-08-03 12:35:34 -04003322 writel(pring->sli.sli3.rspidx,
3323 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003324
James Smart3772a992009-05-22 14:50:54 -04003325 spin_unlock_irqrestore(&phba->hbalock, iflag);
3326 /* Handle the response IOCB */
3327 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3328 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003329
3330 /*
3331 * If the port response put pointer has not been updated, sync
3332 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3333 * response put pointer.
3334 */
James Smart7e56aa22012-08-03 12:35:34 -04003335 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003336 portRspPut = le32_to_cpu(pgp->rspPutInx);
3337 }
James Smart7e56aa22012-08-03 12:35:34 -04003338 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003339
James Smart92d7f7b2007-06-17 19:56:38 -05003340 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003341 /* At least one response entry has been freed */
3342 pring->stats.iocb_rsp_full++;
3343 /* SET RxRE_RSP in Chip Att register */
3344 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3345 writel(status, phba->CAregaddr);
3346 readl(phba->CAregaddr); /* flush */
3347 }
3348 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3349 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3350 pring->stats.iocb_cmd_empty++;
3351
3352 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003353 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003354 lpfc_sli_resume_iocb(phba, pring);
3355
3356 if ((pring->lpfc_sli_cmd_available))
3357 (pring->lpfc_sli_cmd_available) (phba, pring);
3358
3359 }
3360
James Smart2e0fef82007-06-17 19:56:36 -05003361 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003362 return;
dea31012005-04-17 16:05:31 -05003363}
3364
James Smarte59058c2008-08-24 21:49:00 -04003365/**
James Smart4f774512009-05-22 14:52:35 -04003366 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3367 * @phba: Pointer to HBA context object.
3368 * @pring: Pointer to driver SLI ring object.
3369 * @mask: Host attention register mask for this ring.
3370 *
3371 * This function is called from the worker thread when there is a pending
3372 * ELS response iocb on the driver internal slow-path response iocb worker
3373 * queue. The caller does not hold any lock. The function will remove each
3374 * response iocb from the response worker queue and calls the handle
3375 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3376 **/
3377static void
3378lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3379 struct lpfc_sli_ring *pring, uint32_t mask)
3380{
3381 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003382 struct hbq_dmabuf *dmabuf;
3383 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003384 unsigned long iflag;
3385
James Smart45ed1192009-10-02 15:17:02 -04003386 spin_lock_irqsave(&phba->hbalock, iflag);
3387 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3388 spin_unlock_irqrestore(&phba->hbalock, iflag);
3389 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003390 /* Get the response iocb from the head of work queue */
3391 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003392 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003393 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003394 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003395
3396 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3397 case CQE_CODE_COMPL_WQE:
3398 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3399 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003400 /* Translate ELS WCQE to response IOCBQ */
3401 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3402 irspiocbq);
3403 if (irspiocbq)
3404 lpfc_sli_sp_handle_rspiocb(phba, pring,
3405 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003406 break;
3407 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003408 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003409 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3410 cq_event);
3411 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3412 break;
3413 default:
3414 break;
3415 }
James Smart4f774512009-05-22 14:52:35 -04003416 }
3417}
3418
3419/**
James Smart3621a712009-04-06 18:47:14 -04003420 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003421 * @phba: Pointer to HBA context object.
3422 * @pring: Pointer to driver SLI ring object.
3423 *
3424 * This function aborts all iocbs in the given ring and frees all the iocb
3425 * objects in txq. This function issues an abort iocb for all the iocb commands
3426 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3427 * the return of this function. The caller is not required to hold any locks.
3428 **/
James Smart2e0fef82007-06-17 19:56:36 -05003429void
dea31012005-04-17 16:05:31 -05003430lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3431{
James Smart2534ba72007-04-25 09:52:20 -04003432 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003433 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003434
James Smart92d7f7b2007-06-17 19:56:38 -05003435 if (pring->ringno == LPFC_ELS_RING) {
3436 lpfc_fabric_abort_hba(phba);
3437 }
3438
dea31012005-04-17 16:05:31 -05003439 /* Error everything on txq and txcmplq
3440 * First do the txq.
3441 */
James Smart2e0fef82007-06-17 19:56:36 -05003442 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003443 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003444 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003445
3446 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003447 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3448 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3449
James Smart2e0fef82007-06-17 19:56:36 -05003450 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003451
James Smarta257bf92009-04-06 18:48:10 -04003452 /* Cancel all the IOCBs from the completions list */
3453 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3454 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003455}
3456
James Smarte59058c2008-08-24 21:49:00 -04003457/**
James Smart3621a712009-04-06 18:47:14 -04003458 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003459 * @phba: Pointer to HBA context object.
3460 *
3461 * This function flushes all iocbs in the fcp ring and frees all the iocb
3462 * objects in txq and txcmplq. This function will not issue abort iocbs
3463 * for all the iocb commands in txcmplq, they will just be returned with
3464 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3465 * slot has been permanently disabled.
3466 **/
3467void
3468lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3469{
3470 LIST_HEAD(txq);
3471 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003472 struct lpfc_sli *psli = &phba->sli;
3473 struct lpfc_sli_ring *pring;
3474
3475 /* Currently, only one fcp ring */
3476 pring = &psli->ring[psli->fcp_ring];
3477
3478 spin_lock_irq(&phba->hbalock);
3479 /* Retrieve everything on txq */
3480 list_splice_init(&pring->txq, &txq);
3481 pring->txq_cnt = 0;
3482
3483 /* Retrieve everything on the txcmplq */
3484 list_splice_init(&pring->txcmplq, &txcmplq);
3485 pring->txcmplq_cnt = 0;
James Smart4f2e66c2012-05-09 21:17:07 -04003486
3487 /* Indicate the I/O queues are flushed */
3488 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003489 spin_unlock_irq(&phba->hbalock);
3490
3491 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003492 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3493 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003494
3495 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003496 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3497 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003498}
3499
3500/**
James Smart3772a992009-05-22 14:50:54 -04003501 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003502 * @phba: Pointer to HBA context object.
3503 * @mask: Bit mask to be checked.
3504 *
3505 * This function reads the host status register and compares
3506 * with the provided bit mask to check if HBA completed
3507 * the restart. This function will wait in a loop for the
3508 * HBA to complete restart. If the HBA does not restart within
3509 * 15 iterations, the function will reset the HBA again. The
3510 * function returns 1 when HBA fail to restart otherwise returns
3511 * zero.
3512 **/
James Smart3772a992009-05-22 14:50:54 -04003513static int
3514lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003515{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003516 uint32_t status;
3517 int i = 0;
3518 int retval = 0;
dea31012005-04-17 16:05:31 -05003519
Jamie Wellnitz41415862006-02-28 19:25:27 -05003520 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003521 if (lpfc_readl(phba->HSregaddr, &status))
3522 return 1;
dea31012005-04-17 16:05:31 -05003523
Jamie Wellnitz41415862006-02-28 19:25:27 -05003524 /*
3525 * Check status register every 100ms for 5 retries, then every
3526 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3527 * every 2.5 sec for 4.
3528 * Break our of the loop if errors occurred during init.
3529 */
3530 while (((status & mask) != mask) &&
3531 !(status & HS_FFERM) &&
3532 i++ < 20) {
dea31012005-04-17 16:05:31 -05003533
Jamie Wellnitz41415862006-02-28 19:25:27 -05003534 if (i <= 5)
3535 msleep(10);
3536 else if (i <= 10)
3537 msleep(500);
3538 else
3539 msleep(2500);
dea31012005-04-17 16:05:31 -05003540
Jamie Wellnitz41415862006-02-28 19:25:27 -05003541 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003542 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003543 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003544 lpfc_sli_brdrestart(phba);
3545 }
3546 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003547 if (lpfc_readl(phba->HSregaddr, &status)) {
3548 retval = 1;
3549 break;
3550 }
dea31012005-04-17 16:05:31 -05003551 }
dea31012005-04-17 16:05:31 -05003552
Jamie Wellnitz41415862006-02-28 19:25:27 -05003553 /* Check to see if any errors occurred during init */
3554 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3556 "2751 Adapter failed to restart, "
3557 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3558 status,
3559 readl(phba->MBslimaddr + 0xa8),
3560 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003561 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003562 retval = 1;
3563 }
dea31012005-04-17 16:05:31 -05003564
Jamie Wellnitz41415862006-02-28 19:25:27 -05003565 return retval;
dea31012005-04-17 16:05:31 -05003566}
3567
James Smartda0436e2009-05-22 14:51:39 -04003568/**
3569 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3570 * @phba: Pointer to HBA context object.
3571 * @mask: Bit mask to be checked.
3572 *
3573 * This function checks the host status register to check if HBA is
3574 * ready. This function will wait in a loop for the HBA to be ready
3575 * If the HBA is not ready , the function will will reset the HBA PCI
3576 * function again. The function returns 1 when HBA fail to be ready
3577 * otherwise returns zero.
3578 **/
3579static int
3580lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3581{
3582 uint32_t status;
3583 int retval = 0;
3584
3585 /* Read the HBA Host Status Register */
3586 status = lpfc_sli4_post_status_check(phba);
3587
3588 if (status) {
3589 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3590 lpfc_sli_brdrestart(phba);
3591 status = lpfc_sli4_post_status_check(phba);
3592 }
3593
3594 /* Check to see if any errors occurred during init */
3595 if (status) {
3596 phba->link_state = LPFC_HBA_ERROR;
3597 retval = 1;
3598 } else
3599 phba->sli4_hba.intr_enable = 0;
3600
3601 return retval;
3602}
3603
3604/**
3605 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3606 * @phba: Pointer to HBA context object.
3607 * @mask: Bit mask to be checked.
3608 *
3609 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3610 * from the API jump table function pointer from the lpfc_hba struct.
3611 **/
3612int
3613lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3614{
3615 return phba->lpfc_sli_brdready(phba, mask);
3616}
3617
James Smart92908312006-03-07 15:04:13 -05003618#define BARRIER_TEST_PATTERN (0xdeadbeef)
3619
James Smarte59058c2008-08-24 21:49:00 -04003620/**
James Smart3621a712009-04-06 18:47:14 -04003621 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003622 * @phba: Pointer to HBA context object.
3623 *
James Smart1b511972011-12-13 13:23:09 -05003624 * This function is called before resetting an HBA. This function is called
3625 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003626 **/
James Smart2e0fef82007-06-17 19:56:36 -05003627void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003628{
James Smart65a29c12006-07-06 15:50:50 -04003629 uint32_t __iomem *resp_buf;
3630 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003631 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003632 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003633 int i;
3634 uint8_t hdrtype;
3635
3636 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3637 if (hdrtype != 0x80 ||
3638 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3639 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3640 return;
3641
3642 /*
3643 * Tell the other part of the chip to suspend temporarily all
3644 * its DMA activity.
3645 */
James Smart65a29c12006-07-06 15:50:50 -04003646 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003647
3648 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003649 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3650 return;
James Smart92908312006-03-07 15:04:13 -05003651 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3652 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003653 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003654
James Smart9940b972011-03-11 16:06:12 -05003655 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3656 return;
3657 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003658 /* Clear Chip error bit */
3659 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003660 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003661 }
3662
3663 mbox = 0;
3664 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3665 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3666
3667 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003668 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003669 writel(mbox, mbox_buf);
3670
James Smart9940b972011-03-11 16:06:12 -05003671 for (i = 0; i < 50; i++) {
3672 if (lpfc_readl((resp_buf + 1), &resp_data))
3673 return;
3674 if (resp_data != ~(BARRIER_TEST_PATTERN))
3675 mdelay(1);
3676 else
3677 break;
3678 }
3679 resp_data = 0;
3680 if (lpfc_readl((resp_buf + 1), &resp_data))
3681 return;
3682 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003683 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003684 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003685 goto restore_hc;
3686 else
3687 goto clear_errat;
3688 }
3689
3690 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003691 resp_data = 0;
3692 for (i = 0; i < 500; i++) {
3693 if (lpfc_readl(resp_buf, &resp_data))
3694 return;
3695 if (resp_data != mbox)
3696 mdelay(1);
3697 else
3698 break;
3699 }
James Smart92908312006-03-07 15:04:13 -05003700
3701clear_errat:
3702
James Smart9940b972011-03-11 16:06:12 -05003703 while (++i < 500) {
3704 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3705 return;
3706 if (!(ha_copy & HA_ERATT))
3707 mdelay(1);
3708 else
3709 break;
3710 }
James Smart92908312006-03-07 15:04:13 -05003711
3712 if (readl(phba->HAregaddr) & HA_ERATT) {
3713 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003714 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003715 }
3716
3717restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003718 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003719 writel(hc_copy, phba->HCregaddr);
3720 readl(phba->HCregaddr); /* flush */
3721}
3722
James Smarte59058c2008-08-24 21:49:00 -04003723/**
James Smart3621a712009-04-06 18:47:14 -04003724 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003725 * @phba: Pointer to HBA context object.
3726 *
3727 * This function issues a kill_board mailbox command and waits for
3728 * the error attention interrupt. This function is called for stopping
3729 * the firmware processing. The caller is not required to hold any
3730 * locks. This function calls lpfc_hba_down_post function to free
3731 * any pending commands after the kill. The function will return 1 when it
3732 * fails to kill the board else will return 0.
3733 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003734int
James Smart2e0fef82007-06-17 19:56:36 -05003735lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003736{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003737 struct lpfc_sli *psli;
3738 LPFC_MBOXQ_t *pmb;
3739 uint32_t status;
3740 uint32_t ha_copy;
3741 int retval;
3742 int i = 0;
3743
3744 psli = &phba->sli;
3745
3746 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003747 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003748 "0329 Kill HBA Data: x%x x%x\n",
3749 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003750
James Smart98c9ea52007-10-27 13:37:33 -04003751 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3752 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003753 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003754
3755 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003756 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003757 if (lpfc_readl(phba->HCregaddr, &status)) {
3758 spin_unlock_irq(&phba->hbalock);
3759 mempool_free(pmb, phba->mbox_mem_pool);
3760 return 1;
3761 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003762 status &= ~HC_ERINT_ENA;
3763 writel(status, phba->HCregaddr);
3764 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003765 phba->link_flag |= LS_IGNORE_ERATT;
3766 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003767
3768 lpfc_kill_board(phba, pmb);
3769 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3770 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3771
3772 if (retval != MBX_SUCCESS) {
3773 if (retval != MBX_BUSY)
3774 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003775 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3776 "2752 KILL_BOARD command failed retval %d\n",
3777 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003778 spin_lock_irq(&phba->hbalock);
3779 phba->link_flag &= ~LS_IGNORE_ERATT;
3780 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003781 return 1;
3782 }
3783
James Smartf4b4c682009-05-22 14:53:12 -04003784 spin_lock_irq(&phba->hbalock);
3785 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3786 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003787
Jamie Wellnitz41415862006-02-28 19:25:27 -05003788 mempool_free(pmb, phba->mbox_mem_pool);
3789
3790 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3791 * attention every 100ms for 3 seconds. If we don't get ERATT after
3792 * 3 seconds we still set HBA_ERROR state because the status of the
3793 * board is now undefined.
3794 */
James Smart9940b972011-03-11 16:06:12 -05003795 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3796 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003797 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3798 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003799 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3800 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003801 }
3802
3803 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003804 if (ha_copy & HA_ERATT) {
3805 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003806 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003807 }
James Smart2e0fef82007-06-17 19:56:36 -05003808 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003809 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003810 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003811 phba->link_flag &= ~LS_IGNORE_ERATT;
3812 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003813
Jamie Wellnitz41415862006-02-28 19:25:27 -05003814 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003815 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003816
James Smart2e0fef82007-06-17 19:56:36 -05003817 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003818}
3819
James Smarte59058c2008-08-24 21:49:00 -04003820/**
James Smart3772a992009-05-22 14:50:54 -04003821 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003822 * @phba: Pointer to HBA context object.
3823 *
3824 * This function resets the HBA by writing HC_INITFF to the control
3825 * register. After the HBA resets, this function resets all the iocb ring
3826 * indices. This function disables PCI layer parity checking during
3827 * the reset.
3828 * This function returns 0 always.
3829 * The caller is not required to hold any locks.
3830 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003831int
James Smart2e0fef82007-06-17 19:56:36 -05003832lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003833{
3834 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003835 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003836 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003837 int i;
dea31012005-04-17 16:05:31 -05003838
Jamie Wellnitz41415862006-02-28 19:25:27 -05003839 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003840
Jamie Wellnitz41415862006-02-28 19:25:27 -05003841 /* Reset HBA */
3842 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003843 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003844 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003845
3846 /* perform board reset */
3847 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003848 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003849 phba->pport->fc_myDID = 0;
3850 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003851
Jamie Wellnitz41415862006-02-28 19:25:27 -05003852 /* Turn off parity checking and serr during the physical reset */
3853 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3854 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3855 (cfg_value &
3856 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3857
James Smart3772a992009-05-22 14:50:54 -04003858 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3859
Jamie Wellnitz41415862006-02-28 19:25:27 -05003860 /* Now toggle INITFF bit in the Host Control Register */
3861 writel(HC_INITFF, phba->HCregaddr);
3862 mdelay(1);
3863 readl(phba->HCregaddr); /* flush */
3864 writel(0, phba->HCregaddr);
3865 readl(phba->HCregaddr); /* flush */
3866
3867 /* Restore PCI cmd register */
3868 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003869
3870 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003871 for (i = 0; i < psli->num_rings; i++) {
3872 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003873 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003874 pring->sli.sli3.rspidx = 0;
3875 pring->sli.sli3.next_cmdidx = 0;
3876 pring->sli.sli3.local_getidx = 0;
3877 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003878 pring->missbufcnt = 0;
3879 }
dea31012005-04-17 16:05:31 -05003880
James Smart2e0fef82007-06-17 19:56:36 -05003881 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003882 return 0;
3883}
3884
James Smarte59058c2008-08-24 21:49:00 -04003885/**
James Smartda0436e2009-05-22 14:51:39 -04003886 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3887 * @phba: Pointer to HBA context object.
3888 *
3889 * This function resets a SLI4 HBA. This function disables PCI layer parity
3890 * checking during resets the device. The caller is not required to hold
3891 * any locks.
3892 *
3893 * This function returns 0 always.
3894 **/
3895int
3896lpfc_sli4_brdreset(struct lpfc_hba *phba)
3897{
3898 struct lpfc_sli *psli = &phba->sli;
3899 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003900 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003901
3902 /* Reset HBA */
3903 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3904 "0295 Reset HBA Data: x%x x%x\n",
3905 phba->pport->port_state, psli->sli_flag);
3906
3907 /* perform board reset */
3908 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003909 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003910 phba->pport->fc_myDID = 0;
3911 phba->pport->fc_prevDID = 0;
3912
James Smartda0436e2009-05-22 14:51:39 -04003913 spin_lock_irq(&phba->hbalock);
3914 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3915 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003916 spin_unlock_irq(&phba->hbalock);
3917
3918 /* Now physically reset the device */
3919 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3920 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003921
3922 /* Turn off parity checking and serr during the physical reset */
3923 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3924 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3925 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3926
James Smartda0436e2009-05-22 14:51:39 -04003927 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003928 lpfc_sli4_queue_destroy(phba);
James Smart27b01b82012-05-09 21:19:44 -04003929 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04003930
James Smartbe858b62010-12-15 17:57:20 -05003931 /* Restore PCI cmd register */
3932 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3933
James Smart27b01b82012-05-09 21:19:44 -04003934 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003935}
3936
3937/**
3938 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003939 * @phba: Pointer to HBA context object.
3940 *
3941 * This function is called in the SLI initialization code path to
3942 * restart the HBA. The caller is not required to hold any lock.
3943 * This function writes MBX_RESTART mailbox command to the SLIM and
3944 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3945 * function to free any pending commands. The function enables
3946 * POST only during the first initialization. The function returns zero.
3947 * The function does not guarantee completion of MBX_RESTART mailbox
3948 * command before the return of this function.
3949 **/
James Smartda0436e2009-05-22 14:51:39 -04003950static int
3951lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952{
3953 MAILBOX_t *mb;
3954 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003955 volatile uint32_t word0;
3956 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003957 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003958
James Smart2e0fef82007-06-17 19:56:36 -05003959 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003960
James Smart0d878412009-10-02 15:16:56 -04003961 /* Take PCIe device Advanced Error Reporting (AER) state */
3962 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3963
Jamie Wellnitz41415862006-02-28 19:25:27 -05003964 psli = &phba->sli;
3965
3966 /* Restart HBA */
3967 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003968 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003969 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003970
3971 word0 = 0;
3972 mb = (MAILBOX_t *) &word0;
3973 mb->mbxCommand = MBX_RESTART;
3974 mb->mbxHc = 1;
3975
James Smart92908312006-03-07 15:04:13 -05003976 lpfc_reset_barrier(phba);
3977
Jamie Wellnitz41415862006-02-28 19:25:27 -05003978 to_slim = phba->MBslimaddr;
3979 writel(*(uint32_t *) mb, to_slim);
3980 readl(to_slim); /* flush */
3981
3982 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003983 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003984 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003985 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003986 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003987 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003988 writel(*(uint32_t *) mb, to_slim);
3989 readl(to_slim); /* flush */
3990
3991 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003992 phba->pport->stopped = 0;
3993 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003994 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003995 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003996
James Smart64ba8812006-08-02 15:24:34 -04003997 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3998 psli->stats_start = get_seconds();
3999
James Smarteaf15d52008-12-04 22:39:29 -05004000 /* Give the INITFF and Post time to settle. */
4001 mdelay(100);
dea31012005-04-17 16:05:31 -05004002
James Smart0d878412009-10-02 15:16:56 -04004003 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4004 if (hba_aer_enabled)
4005 pci_disable_pcie_error_reporting(phba->pcidev);
4006
Jamie Wellnitz41415862006-02-28 19:25:27 -05004007 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004008
4009 return 0;
4010}
4011
James Smarte59058c2008-08-24 21:49:00 -04004012/**
James Smartda0436e2009-05-22 14:51:39 -04004013 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4014 * @phba: Pointer to HBA context object.
4015 *
4016 * This function is called in the SLI initialization code path to restart
4017 * a SLI4 HBA. The caller is not required to hold any lock.
4018 * At the end of the function, it calls lpfc_hba_down_post function to
4019 * free any pending commands.
4020 **/
4021static int
4022lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4023{
4024 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004025 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004026 int rc;
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 Smart27b01b82012-05-09 21:19:44 -04004036 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004037
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
James Smart27b01b82012-05-09 21:19:44 -04004053 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004054}
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);
James Smart8a9d2e82012-05-09 21:16:12 -04004992
4993 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4994 "3162 Retrieved extents type-%d from port: count:%d, "
4995 "size:%d\n", type, *extnt_count, *extnt_size);
4996
4997err_exit:
James Smart6d368e52011-05-24 11:44:12 -04004998 mempool_free(mbox, phba->mbox_mem_pool);
4999 return rc;
5000}
5001
5002/**
5003 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5004 * @phba: Pointer to HBA context object.
5005 * @type: The extent type to check.
5006 *
5007 * This function reads the current available extents from the port and checks
5008 * if the extent count or extent size has changed since the last access.
5009 * Callers use this routine post port reset to understand if there is a
5010 * extent reprovisioning requirement.
5011 *
5012 * Returns:
5013 * -Error: error indicates problem.
5014 * 1: Extent count or size has changed.
5015 * 0: No changes.
5016 **/
5017static int
5018lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5019{
5020 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5021 uint16_t size_diff, rsrc_ext_size;
5022 int rc = 0;
5023 struct lpfc_rsrc_blks *rsrc_entry;
5024 struct list_head *rsrc_blk_list = NULL;
5025
5026 size_diff = 0;
5027 curr_ext_cnt = 0;
5028 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5029 &rsrc_ext_cnt,
5030 &rsrc_ext_size);
5031 if (unlikely(rc))
5032 return -EIO;
5033
5034 switch (type) {
5035 case LPFC_RSC_TYPE_FCOE_RPI:
5036 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5037 break;
5038 case LPFC_RSC_TYPE_FCOE_VPI:
5039 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5040 break;
5041 case LPFC_RSC_TYPE_FCOE_XRI:
5042 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5043 break;
5044 case LPFC_RSC_TYPE_FCOE_VFI:
5045 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5046 break;
5047 default:
5048 break;
5049 }
5050
5051 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5052 curr_ext_cnt++;
5053 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5054 size_diff++;
5055 }
5056
5057 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5058 rc = 1;
5059
5060 return rc;
5061}
5062
5063/**
5064 * lpfc_sli4_cfg_post_extnts -
5065 * @phba: Pointer to HBA context object.
5066 * @extnt_cnt - number of available extents.
5067 * @type - the extent type (rpi, xri, vfi, vpi).
5068 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5069 * @mbox - pointer to the caller's allocated mailbox structure.
5070 *
5071 * This function executes the extents allocation request. It also
5072 * takes care of the amount of memory needed to allocate or get the
5073 * allocated extents. It is the caller's responsibility to evaluate
5074 * the response.
5075 *
5076 * Returns:
5077 * -Error: Error value describes the condition found.
5078 * 0: if successful
5079 **/
5080static int
James Smart8a9d2e82012-05-09 21:16:12 -04005081lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005082 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5083{
5084 int rc = 0;
5085 uint32_t req_len;
5086 uint32_t emb_len;
5087 uint32_t alloc_len, mbox_tmo;
5088
5089 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005090 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005091
5092 /*
5093 * Calculate the size of an embedded mailbox. The uint32_t
5094 * accounts for extents-specific word.
5095 */
5096 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5097 sizeof(uint32_t);
5098
5099 /*
5100 * Presume the allocation and response will fit into an embedded
5101 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5102 */
5103 *emb = LPFC_SLI4_MBX_EMBED;
5104 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005105 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005106 sizeof(union lpfc_sli4_cfg_shdr) +
5107 sizeof(uint32_t);
5108 *emb = LPFC_SLI4_MBX_NEMBED;
5109 }
5110
5111 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5112 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5113 req_len, *emb);
5114 if (alloc_len < req_len) {
5115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005116 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005117 "less than the requested DMA memory "
5118 "size (x%x)\n", alloc_len, req_len);
5119 return -ENOMEM;
5120 }
James Smart8a9d2e82012-05-09 21:16:12 -04005121 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005122 if (unlikely(rc))
5123 return -EIO;
5124
5125 if (!phba->sli4_hba.intr_enable)
5126 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5127 else {
James Smarta183a152011-10-10 21:32:43 -04005128 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005129 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5130 }
5131
5132 if (unlikely(rc))
5133 rc = -EIO;
5134 return rc;
5135}
5136
5137/**
5138 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5139 * @phba: Pointer to HBA context object.
5140 * @type: The resource extent type to allocate.
5141 *
5142 * This function allocates the number of elements for the specified
5143 * resource type.
5144 **/
5145static int
5146lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5147{
5148 bool emb = false;
5149 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5150 uint16_t rsrc_id, rsrc_start, j, k;
5151 uint16_t *ids;
5152 int i, rc;
5153 unsigned long longs;
5154 unsigned long *bmask;
5155 struct lpfc_rsrc_blks *rsrc_blks;
5156 LPFC_MBOXQ_t *mbox;
5157 uint32_t length;
5158 struct lpfc_id_range *id_array = NULL;
5159 void *virtaddr = NULL;
5160 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5161 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5162 struct list_head *ext_blk_list;
5163
5164 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5165 &rsrc_cnt,
5166 &rsrc_size);
5167 if (unlikely(rc))
5168 return -EIO;
5169
5170 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5171 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5172 "3009 No available Resource Extents "
5173 "for resource type 0x%x: Count: 0x%x, "
5174 "Size 0x%x\n", type, rsrc_cnt,
5175 rsrc_size);
5176 return -ENOMEM;
5177 }
5178
James Smart8a9d2e82012-05-09 21:16:12 -04005179 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5180 "2903 Post resource extents type-0x%x: "
5181 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005182
5183 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5184 if (!mbox)
5185 return -ENOMEM;
5186
James Smart8a9d2e82012-05-09 21:16:12 -04005187 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005188 if (unlikely(rc)) {
5189 rc = -EIO;
5190 goto err_exit;
5191 }
5192
5193 /*
5194 * Figure out where the response is located. Then get local pointers
5195 * to the response data. The port does not guarantee to respond to
5196 * all extents counts request so update the local variable with the
5197 * allocated count from the port.
5198 */
5199 if (emb == LPFC_SLI4_MBX_EMBED) {
5200 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5201 id_array = &rsrc_ext->u.rsp.id[0];
5202 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5203 } else {
5204 virtaddr = mbox->sge_array->addr[0];
5205 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5206 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5207 id_array = &n_rsrc->id;
5208 }
5209
5210 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5211 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5212
5213 /*
5214 * Based on the resource size and count, correct the base and max
5215 * resource values.
5216 */
5217 length = sizeof(struct lpfc_rsrc_blks);
5218 switch (type) {
5219 case LPFC_RSC_TYPE_FCOE_RPI:
5220 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5221 sizeof(unsigned long),
5222 GFP_KERNEL);
5223 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5224 rc = -ENOMEM;
5225 goto err_exit;
5226 }
5227 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5228 sizeof(uint16_t),
5229 GFP_KERNEL);
5230 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5231 kfree(phba->sli4_hba.rpi_bmask);
5232 rc = -ENOMEM;
5233 goto err_exit;
5234 }
5235
5236 /*
5237 * The next_rpi was initialized with the maximum available
5238 * count but the port may allocate a smaller number. Catch
5239 * that case and update the next_rpi.
5240 */
5241 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5242
5243 /* Initialize local ptrs for common extent processing later. */
5244 bmask = phba->sli4_hba.rpi_bmask;
5245 ids = phba->sli4_hba.rpi_ids;
5246 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5247 break;
5248 case LPFC_RSC_TYPE_FCOE_VPI:
5249 phba->vpi_bmask = kzalloc(longs *
5250 sizeof(unsigned long),
5251 GFP_KERNEL);
5252 if (unlikely(!phba->vpi_bmask)) {
5253 rc = -ENOMEM;
5254 goto err_exit;
5255 }
5256 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5257 sizeof(uint16_t),
5258 GFP_KERNEL);
5259 if (unlikely(!phba->vpi_ids)) {
5260 kfree(phba->vpi_bmask);
5261 rc = -ENOMEM;
5262 goto err_exit;
5263 }
5264
5265 /* Initialize local ptrs for common extent processing later. */
5266 bmask = phba->vpi_bmask;
5267 ids = phba->vpi_ids;
5268 ext_blk_list = &phba->lpfc_vpi_blk_list;
5269 break;
5270 case LPFC_RSC_TYPE_FCOE_XRI:
5271 phba->sli4_hba.xri_bmask = kzalloc(longs *
5272 sizeof(unsigned long),
5273 GFP_KERNEL);
5274 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5275 rc = -ENOMEM;
5276 goto err_exit;
5277 }
James Smart8a9d2e82012-05-09 21:16:12 -04005278 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005279 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5280 sizeof(uint16_t),
5281 GFP_KERNEL);
5282 if (unlikely(!phba->sli4_hba.xri_ids)) {
5283 kfree(phba->sli4_hba.xri_bmask);
5284 rc = -ENOMEM;
5285 goto err_exit;
5286 }
5287
5288 /* Initialize local ptrs for common extent processing later. */
5289 bmask = phba->sli4_hba.xri_bmask;
5290 ids = phba->sli4_hba.xri_ids;
5291 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5292 break;
5293 case LPFC_RSC_TYPE_FCOE_VFI:
5294 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5295 sizeof(unsigned long),
5296 GFP_KERNEL);
5297 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5298 rc = -ENOMEM;
5299 goto err_exit;
5300 }
5301 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5302 sizeof(uint16_t),
5303 GFP_KERNEL);
5304 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5305 kfree(phba->sli4_hba.vfi_bmask);
5306 rc = -ENOMEM;
5307 goto err_exit;
5308 }
5309
5310 /* Initialize local ptrs for common extent processing later. */
5311 bmask = phba->sli4_hba.vfi_bmask;
5312 ids = phba->sli4_hba.vfi_ids;
5313 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5314 break;
5315 default:
5316 /* Unsupported Opcode. Fail call. */
5317 id_array = NULL;
5318 bmask = NULL;
5319 ids = NULL;
5320 ext_blk_list = NULL;
5321 goto err_exit;
5322 }
5323
5324 /*
5325 * Complete initializing the extent configuration with the
5326 * allocated ids assigned to this function. The bitmask serves
5327 * as an index into the array and manages the available ids. The
5328 * array just stores the ids communicated to the port via the wqes.
5329 */
5330 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5331 if ((i % 2) == 0)
5332 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5333 &id_array[k]);
5334 else
5335 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5336 &id_array[k]);
5337
5338 rsrc_blks = kzalloc(length, GFP_KERNEL);
5339 if (unlikely(!rsrc_blks)) {
5340 rc = -ENOMEM;
5341 kfree(bmask);
5342 kfree(ids);
5343 goto err_exit;
5344 }
5345 rsrc_blks->rsrc_start = rsrc_id;
5346 rsrc_blks->rsrc_size = rsrc_size;
5347 list_add_tail(&rsrc_blks->list, ext_blk_list);
5348 rsrc_start = rsrc_id;
5349 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5350 phba->sli4_hba.scsi_xri_start = rsrc_start +
5351 lpfc_sli4_get_els_iocb_cnt(phba);
5352
5353 while (rsrc_id < (rsrc_start + rsrc_size)) {
5354 ids[j] = rsrc_id;
5355 rsrc_id++;
5356 j++;
5357 }
5358 /* Entire word processed. Get next word.*/
5359 if ((i % 2) == 1)
5360 k++;
5361 }
5362 err_exit:
5363 lpfc_sli4_mbox_cmd_free(phba, mbox);
5364 return rc;
5365}
5366
5367/**
5368 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5369 * @phba: Pointer to HBA context object.
5370 * @type: the extent's type.
5371 *
5372 * This function deallocates all extents of a particular resource type.
5373 * SLI4 does not allow for deallocating a particular extent range. It
5374 * is the caller's responsibility to release all kernel memory resources.
5375 **/
5376static int
5377lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5378{
5379 int rc;
5380 uint32_t length, mbox_tmo = 0;
5381 LPFC_MBOXQ_t *mbox;
5382 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5383 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5384
5385 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5386 if (!mbox)
5387 return -ENOMEM;
5388
5389 /*
5390 * This function sends an embedded mailbox because it only sends the
5391 * the resource type. All extents of this type are released by the
5392 * port.
5393 */
5394 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5395 sizeof(struct lpfc_sli4_cfg_mhdr));
5396 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5397 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5398 length, LPFC_SLI4_MBX_EMBED);
5399
5400 /* Send an extents count of 0 - the dealloc doesn't use it. */
5401 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5402 LPFC_SLI4_MBX_EMBED);
5403 if (unlikely(rc)) {
5404 rc = -EIO;
5405 goto out_free_mbox;
5406 }
5407 if (!phba->sli4_hba.intr_enable)
5408 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5409 else {
James Smarta183a152011-10-10 21:32:43 -04005410 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005411 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5412 }
5413 if (unlikely(rc)) {
5414 rc = -EIO;
5415 goto out_free_mbox;
5416 }
5417
5418 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5419 if (bf_get(lpfc_mbox_hdr_status,
5420 &dealloc_rsrc->header.cfg_shdr.response)) {
5421 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5422 "2919 Failed to release resource extents "
5423 "for type %d - Status 0x%x Add'l Status 0x%x. "
5424 "Resource memory not released.\n",
5425 type,
5426 bf_get(lpfc_mbox_hdr_status,
5427 &dealloc_rsrc->header.cfg_shdr.response),
5428 bf_get(lpfc_mbox_hdr_add_status,
5429 &dealloc_rsrc->header.cfg_shdr.response));
5430 rc = -EIO;
5431 goto out_free_mbox;
5432 }
5433
5434 /* Release kernel memory resources for the specific type. */
5435 switch (type) {
5436 case LPFC_RSC_TYPE_FCOE_VPI:
5437 kfree(phba->vpi_bmask);
5438 kfree(phba->vpi_ids);
5439 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5440 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5441 &phba->lpfc_vpi_blk_list, list) {
5442 list_del_init(&rsrc_blk->list);
5443 kfree(rsrc_blk);
5444 }
5445 break;
5446 case LPFC_RSC_TYPE_FCOE_XRI:
5447 kfree(phba->sli4_hba.xri_bmask);
5448 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005449 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5450 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5451 list_del_init(&rsrc_blk->list);
5452 kfree(rsrc_blk);
5453 }
5454 break;
5455 case LPFC_RSC_TYPE_FCOE_VFI:
5456 kfree(phba->sli4_hba.vfi_bmask);
5457 kfree(phba->sli4_hba.vfi_ids);
5458 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5459 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5460 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5461 list_del_init(&rsrc_blk->list);
5462 kfree(rsrc_blk);
5463 }
5464 break;
5465 case LPFC_RSC_TYPE_FCOE_RPI:
5466 /* RPI bitmask and physical id array are cleaned up earlier. */
5467 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5468 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5469 list_del_init(&rsrc_blk->list);
5470 kfree(rsrc_blk);
5471 }
5472 break;
5473 default:
5474 break;
5475 }
5476
5477 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5478
5479 out_free_mbox:
5480 mempool_free(mbox, phba->mbox_mem_pool);
5481 return rc;
5482}
5483
5484/**
5485 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5486 * @phba: Pointer to HBA context object.
5487 *
5488 * This function allocates all SLI4 resource identifiers.
5489 **/
5490int
5491lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5492{
5493 int i, rc, error = 0;
5494 uint16_t count, base;
5495 unsigned long longs;
5496
James Smartff78d8f2011-12-13 13:21:35 -05005497 if (!phba->sli4_hba.rpi_hdrs_in_use)
5498 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005499 if (phba->sli4_hba.extents_in_use) {
5500 /*
5501 * The port supports resource extents. The XRI, VPI, VFI, RPI
5502 * resource extent count must be read and allocated before
5503 * provisioning the resource id arrays.
5504 */
5505 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5506 LPFC_IDX_RSRC_RDY) {
5507 /*
5508 * Extent-based resources are set - the driver could
5509 * be in a port reset. Figure out if any corrective
5510 * actions need to be taken.
5511 */
5512 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5513 LPFC_RSC_TYPE_FCOE_VFI);
5514 if (rc != 0)
5515 error++;
5516 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5517 LPFC_RSC_TYPE_FCOE_VPI);
5518 if (rc != 0)
5519 error++;
5520 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5521 LPFC_RSC_TYPE_FCOE_XRI);
5522 if (rc != 0)
5523 error++;
5524 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5525 LPFC_RSC_TYPE_FCOE_RPI);
5526 if (rc != 0)
5527 error++;
5528
5529 /*
5530 * It's possible that the number of resources
5531 * provided to this port instance changed between
5532 * resets. Detect this condition and reallocate
5533 * resources. Otherwise, there is no action.
5534 */
5535 if (error) {
5536 lpfc_printf_log(phba, KERN_INFO,
5537 LOG_MBOX | LOG_INIT,
5538 "2931 Detected extent resource "
5539 "change. Reallocating all "
5540 "extents.\n");
5541 rc = lpfc_sli4_dealloc_extent(phba,
5542 LPFC_RSC_TYPE_FCOE_VFI);
5543 rc = lpfc_sli4_dealloc_extent(phba,
5544 LPFC_RSC_TYPE_FCOE_VPI);
5545 rc = lpfc_sli4_dealloc_extent(phba,
5546 LPFC_RSC_TYPE_FCOE_XRI);
5547 rc = lpfc_sli4_dealloc_extent(phba,
5548 LPFC_RSC_TYPE_FCOE_RPI);
5549 } else
5550 return 0;
5551 }
5552
5553 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5554 if (unlikely(rc))
5555 goto err_exit;
5556
5557 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5558 if (unlikely(rc))
5559 goto err_exit;
5560
5561 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5562 if (unlikely(rc))
5563 goto err_exit;
5564
5565 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5566 if (unlikely(rc))
5567 goto err_exit;
5568 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5569 LPFC_IDX_RSRC_RDY);
5570 return rc;
5571 } else {
5572 /*
5573 * The port does not support resource extents. The XRI, VPI,
5574 * VFI, RPI resource ids were determined from READ_CONFIG.
5575 * Just allocate the bitmasks and provision the resource id
5576 * arrays. If a port reset is active, the resources don't
5577 * need any action - just exit.
5578 */
5579 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005580 LPFC_IDX_RSRC_RDY) {
5581 lpfc_sli4_dealloc_resource_identifiers(phba);
5582 lpfc_sli4_remove_rpis(phba);
5583 }
James Smart6d368e52011-05-24 11:44:12 -04005584 /* RPIs. */
5585 count = phba->sli4_hba.max_cfg_param.max_rpi;
5586 base = phba->sli4_hba.max_cfg_param.rpi_base;
5587 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5588 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5589 sizeof(unsigned long),
5590 GFP_KERNEL);
5591 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5592 rc = -ENOMEM;
5593 goto err_exit;
5594 }
5595 phba->sli4_hba.rpi_ids = kzalloc(count *
5596 sizeof(uint16_t),
5597 GFP_KERNEL);
5598 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5599 rc = -ENOMEM;
5600 goto free_rpi_bmask;
5601 }
5602
5603 for (i = 0; i < count; i++)
5604 phba->sli4_hba.rpi_ids[i] = base + i;
5605
5606 /* VPIs. */
5607 count = phba->sli4_hba.max_cfg_param.max_vpi;
5608 base = phba->sli4_hba.max_cfg_param.vpi_base;
5609 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5610 phba->vpi_bmask = kzalloc(longs *
5611 sizeof(unsigned long),
5612 GFP_KERNEL);
5613 if (unlikely(!phba->vpi_bmask)) {
5614 rc = -ENOMEM;
5615 goto free_rpi_ids;
5616 }
5617 phba->vpi_ids = kzalloc(count *
5618 sizeof(uint16_t),
5619 GFP_KERNEL);
5620 if (unlikely(!phba->vpi_ids)) {
5621 rc = -ENOMEM;
5622 goto free_vpi_bmask;
5623 }
5624
5625 for (i = 0; i < count; i++)
5626 phba->vpi_ids[i] = base + i;
5627
5628 /* XRIs. */
5629 count = phba->sli4_hba.max_cfg_param.max_xri;
5630 base = phba->sli4_hba.max_cfg_param.xri_base;
5631 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5632 phba->sli4_hba.xri_bmask = kzalloc(longs *
5633 sizeof(unsigned long),
5634 GFP_KERNEL);
5635 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5636 rc = -ENOMEM;
5637 goto free_vpi_ids;
5638 }
James Smart41899be2012-03-01 22:34:19 -05005639 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005640 phba->sli4_hba.xri_ids = kzalloc(count *
5641 sizeof(uint16_t),
5642 GFP_KERNEL);
5643 if (unlikely(!phba->sli4_hba.xri_ids)) {
5644 rc = -ENOMEM;
5645 goto free_xri_bmask;
5646 }
5647
5648 for (i = 0; i < count; i++)
5649 phba->sli4_hba.xri_ids[i] = base + i;
5650
5651 /* VFIs. */
5652 count = phba->sli4_hba.max_cfg_param.max_vfi;
5653 base = phba->sli4_hba.max_cfg_param.vfi_base;
5654 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5655 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5656 sizeof(unsigned long),
5657 GFP_KERNEL);
5658 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5659 rc = -ENOMEM;
5660 goto free_xri_ids;
5661 }
5662 phba->sli4_hba.vfi_ids = kzalloc(count *
5663 sizeof(uint16_t),
5664 GFP_KERNEL);
5665 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5666 rc = -ENOMEM;
5667 goto free_vfi_bmask;
5668 }
5669
5670 for (i = 0; i < count; i++)
5671 phba->sli4_hba.vfi_ids[i] = base + i;
5672
5673 /*
5674 * Mark all resources ready. An HBA reset doesn't need
5675 * to reset the initialization.
5676 */
5677 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5678 LPFC_IDX_RSRC_RDY);
5679 return 0;
5680 }
5681
5682 free_vfi_bmask:
5683 kfree(phba->sli4_hba.vfi_bmask);
5684 free_xri_ids:
5685 kfree(phba->sli4_hba.xri_ids);
5686 free_xri_bmask:
5687 kfree(phba->sli4_hba.xri_bmask);
5688 free_vpi_ids:
5689 kfree(phba->vpi_ids);
5690 free_vpi_bmask:
5691 kfree(phba->vpi_bmask);
5692 free_rpi_ids:
5693 kfree(phba->sli4_hba.rpi_ids);
5694 free_rpi_bmask:
5695 kfree(phba->sli4_hba.rpi_bmask);
5696 err_exit:
5697 return rc;
5698}
5699
5700/**
5701 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5702 * @phba: Pointer to HBA context object.
5703 *
5704 * This function allocates the number of elements for the specified
5705 * resource type.
5706 **/
5707int
5708lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5709{
5710 if (phba->sli4_hba.extents_in_use) {
5711 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5712 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5713 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5714 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5715 } else {
5716 kfree(phba->vpi_bmask);
5717 kfree(phba->vpi_ids);
5718 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5719 kfree(phba->sli4_hba.xri_bmask);
5720 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005721 kfree(phba->sli4_hba.vfi_bmask);
5722 kfree(phba->sli4_hba.vfi_ids);
5723 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5724 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5725 }
5726
5727 return 0;
5728}
5729
5730/**
James Smartb76f2dc2011-07-22 18:37:42 -04005731 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5732 * @phba: Pointer to HBA context object.
5733 * @type: The resource extent type.
5734 * @extnt_count: buffer to hold port extent count response
5735 * @extnt_size: buffer to hold port extent size response.
5736 *
5737 * This function calls the port to read the host allocated extents
5738 * for a particular type.
5739 **/
5740int
5741lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5742 uint16_t *extnt_cnt, uint16_t *extnt_size)
5743{
5744 bool emb;
5745 int rc = 0;
5746 uint16_t curr_blks = 0;
5747 uint32_t req_len, emb_len;
5748 uint32_t alloc_len, mbox_tmo;
5749 struct list_head *blk_list_head;
5750 struct lpfc_rsrc_blks *rsrc_blk;
5751 LPFC_MBOXQ_t *mbox;
5752 void *virtaddr = NULL;
5753 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5754 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5755 union lpfc_sli4_cfg_shdr *shdr;
5756
5757 switch (type) {
5758 case LPFC_RSC_TYPE_FCOE_VPI:
5759 blk_list_head = &phba->lpfc_vpi_blk_list;
5760 break;
5761 case LPFC_RSC_TYPE_FCOE_XRI:
5762 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5763 break;
5764 case LPFC_RSC_TYPE_FCOE_VFI:
5765 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5766 break;
5767 case LPFC_RSC_TYPE_FCOE_RPI:
5768 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5769 break;
5770 default:
5771 return -EIO;
5772 }
5773
5774 /* Count the number of extents currently allocatd for this type. */
5775 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5776 if (curr_blks == 0) {
5777 /*
5778 * The GET_ALLOCATED mailbox does not return the size,
5779 * just the count. The size should be just the size
5780 * stored in the current allocated block and all sizes
5781 * for an extent type are the same so set the return
5782 * value now.
5783 */
5784 *extnt_size = rsrc_blk->rsrc_size;
5785 }
5786 curr_blks++;
5787 }
5788
5789 /* Calculate the total requested length of the dma memory. */
5790 req_len = curr_blks * sizeof(uint16_t);
5791
5792 /*
5793 * Calculate the size of an embedded mailbox. The uint32_t
5794 * accounts for extents-specific word.
5795 */
5796 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5797 sizeof(uint32_t);
5798
5799 /*
5800 * Presume the allocation and response will fit into an embedded
5801 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5802 */
5803 emb = LPFC_SLI4_MBX_EMBED;
5804 req_len = emb_len;
5805 if (req_len > emb_len) {
5806 req_len = curr_blks * sizeof(uint16_t) +
5807 sizeof(union lpfc_sli4_cfg_shdr) +
5808 sizeof(uint32_t);
5809 emb = LPFC_SLI4_MBX_NEMBED;
5810 }
5811
5812 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5813 if (!mbox)
5814 return -ENOMEM;
5815 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5816
5817 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5818 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5819 req_len, emb);
5820 if (alloc_len < req_len) {
5821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5822 "2983 Allocated DMA memory size (x%x) is "
5823 "less than the requested DMA memory "
5824 "size (x%x)\n", alloc_len, req_len);
5825 rc = -ENOMEM;
5826 goto err_exit;
5827 }
5828 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5829 if (unlikely(rc)) {
5830 rc = -EIO;
5831 goto err_exit;
5832 }
5833
5834 if (!phba->sli4_hba.intr_enable)
5835 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5836 else {
James Smarta183a152011-10-10 21:32:43 -04005837 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005838 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5839 }
5840
5841 if (unlikely(rc)) {
5842 rc = -EIO;
5843 goto err_exit;
5844 }
5845
5846 /*
5847 * Figure out where the response is located. Then get local pointers
5848 * to the response data. The port does not guarantee to respond to
5849 * all extents counts request so update the local variable with the
5850 * allocated count from the port.
5851 */
5852 if (emb == LPFC_SLI4_MBX_EMBED) {
5853 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5854 shdr = &rsrc_ext->header.cfg_shdr;
5855 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5856 } else {
5857 virtaddr = mbox->sge_array->addr[0];
5858 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5859 shdr = &n_rsrc->cfg_shdr;
5860 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5861 }
5862
5863 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5864 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5865 "2984 Failed to read allocated resources "
5866 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5867 type,
5868 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5869 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5870 rc = -EIO;
5871 goto err_exit;
5872 }
5873 err_exit:
5874 lpfc_sli4_mbox_cmd_free(phba, mbox);
5875 return rc;
5876}
5877
5878/**
James Smart8a9d2e82012-05-09 21:16:12 -04005879 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5880 * @phba: pointer to lpfc hba data structure.
5881 *
5882 * This routine walks the list of els buffers that have been allocated and
5883 * repost them to the port by using SGL block post. This is needed after a
5884 * pci_function_reset/warm_start or start. It attempts to construct blocks
5885 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5886 * SGL block post mailbox commands to post them to the port. For single els
5887 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5888 * mailbox command for posting.
5889 *
5890 * Returns: 0 = success, non-zero failure.
5891 **/
5892static int
5893lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5894{
5895 struct lpfc_sglq *sglq_entry = NULL;
5896 struct lpfc_sglq *sglq_entry_next = NULL;
5897 struct lpfc_sglq *sglq_entry_first = NULL;
5898 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5899 int last_xritag = NO_XRI;
5900 LIST_HEAD(prep_sgl_list);
5901 LIST_HEAD(blck_sgl_list);
5902 LIST_HEAD(allc_sgl_list);
5903 LIST_HEAD(post_sgl_list);
5904 LIST_HEAD(free_sgl_list);
5905
5906 spin_lock(&phba->hbalock);
5907 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5908 spin_unlock(&phba->hbalock);
5909
5910 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5911 &allc_sgl_list, list) {
5912 list_del_init(&sglq_entry->list);
5913 block_cnt++;
5914 if ((last_xritag != NO_XRI) &&
5915 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5916 /* a hole in xri block, form a sgl posting block */
5917 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5918 post_cnt = block_cnt - 1;
5919 /* prepare list for next posting block */
5920 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5921 block_cnt = 1;
5922 } else {
5923 /* prepare list for next posting block */
5924 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5925 /* enough sgls for non-embed sgl mbox command */
5926 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5927 list_splice_init(&prep_sgl_list,
5928 &blck_sgl_list);
5929 post_cnt = block_cnt;
5930 block_cnt = 0;
5931 }
5932 }
5933 num_posted++;
5934
5935 /* keep track of last sgl's xritag */
5936 last_xritag = sglq_entry->sli4_xritag;
5937
5938 /* end of repost sgl list condition for els buffers */
5939 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5940 if (post_cnt == 0) {
5941 list_splice_init(&prep_sgl_list,
5942 &blck_sgl_list);
5943 post_cnt = block_cnt;
5944 } else if (block_cnt == 1) {
5945 status = lpfc_sli4_post_sgl(phba,
5946 sglq_entry->phys, 0,
5947 sglq_entry->sli4_xritag);
5948 if (!status) {
5949 /* successful, put sgl to posted list */
5950 list_add_tail(&sglq_entry->list,
5951 &post_sgl_list);
5952 } else {
5953 /* Failure, put sgl to free list */
5954 lpfc_printf_log(phba, KERN_WARNING,
5955 LOG_SLI,
5956 "3159 Failed to post els "
5957 "sgl, xritag:x%x\n",
5958 sglq_entry->sli4_xritag);
5959 list_add_tail(&sglq_entry->list,
5960 &free_sgl_list);
5961 spin_lock_irq(&phba->hbalock);
5962 phba->sli4_hba.els_xri_cnt--;
5963 spin_unlock_irq(&phba->hbalock);
5964 }
5965 }
5966 }
5967
5968 /* continue until a nembed page worth of sgls */
5969 if (post_cnt == 0)
5970 continue;
5971
5972 /* post the els buffer list sgls as a block */
5973 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
5974 post_cnt);
5975
5976 if (!status) {
5977 /* success, put sgl list to posted sgl list */
5978 list_splice_init(&blck_sgl_list, &post_sgl_list);
5979 } else {
5980 /* Failure, put sgl list to free sgl list */
5981 sglq_entry_first = list_first_entry(&blck_sgl_list,
5982 struct lpfc_sglq,
5983 list);
5984 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5985 "3160 Failed to post els sgl-list, "
5986 "xritag:x%x-x%x\n",
5987 sglq_entry_first->sli4_xritag,
5988 (sglq_entry_first->sli4_xritag +
5989 post_cnt - 1));
5990 list_splice_init(&blck_sgl_list, &free_sgl_list);
5991 spin_lock_irq(&phba->hbalock);
5992 phba->sli4_hba.els_xri_cnt -= post_cnt;
5993 spin_unlock_irq(&phba->hbalock);
5994 }
5995
5996 /* don't reset xirtag due to hole in xri block */
5997 if (block_cnt == 0)
5998 last_xritag = NO_XRI;
5999
6000 /* reset els sgl post count for next round of posting */
6001 post_cnt = 0;
6002 }
6003
6004 /* free the els sgls failed to post */
6005 lpfc_free_sgl_list(phba, &free_sgl_list);
6006
6007 /* push els sgls posted to the availble list */
6008 if (!list_empty(&post_sgl_list)) {
6009 spin_lock(&phba->hbalock);
6010 list_splice_init(&post_sgl_list,
6011 &phba->sli4_hba.lpfc_sgl_list);
6012 spin_unlock(&phba->hbalock);
6013 } else {
6014 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6015 "3161 Failure to post els sgl to port.\n");
6016 return -EIO;
6017 }
6018 return 0;
6019}
6020
6021/**
James Smartda0436e2009-05-22 14:51:39 -04006022 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6023 * @phba: Pointer to HBA context object.
6024 *
6025 * This function is the main SLI4 device intialization PCI function. This
6026 * function is called by the HBA intialization code, HBA reset code and
6027 * HBA error attention handler code. Caller is not required to hold any
6028 * locks.
6029 **/
6030int
6031lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6032{
6033 int rc;
6034 LPFC_MBOXQ_t *mboxq;
6035 struct lpfc_mqe *mqe;
6036 uint8_t *vpd;
6037 uint32_t vpd_size;
6038 uint32_t ftr_rsp = 0;
6039 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6040 struct lpfc_vport *vport = phba->pport;
6041 struct lpfc_dmabuf *mp;
6042
6043 /* Perform a PCI function reset to start from clean */
6044 rc = lpfc_pci_function_reset(phba);
6045 if (unlikely(rc))
6046 return -ENODEV;
6047
6048 /* Check the HBA Host Status Register for readyness */
6049 rc = lpfc_sli4_post_status_check(phba);
6050 if (unlikely(rc))
6051 return -ENODEV;
6052 else {
6053 spin_lock_irq(&phba->hbalock);
6054 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6055 spin_unlock_irq(&phba->hbalock);
6056 }
6057
6058 /*
6059 * Allocate a single mailbox container for initializing the
6060 * port.
6061 */
6062 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6063 if (!mboxq)
6064 return -ENOMEM;
6065
James Smartda0436e2009-05-22 14:51:39 -04006066 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006067 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006068 vpd = kzalloc(vpd_size, GFP_KERNEL);
6069 if (!vpd) {
6070 rc = -ENOMEM;
6071 goto out_free_mbox;
6072 }
6073
6074 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006075 if (unlikely(rc)) {
6076 kfree(vpd);
6077 goto out_free_mbox;
6078 }
James Smartda0436e2009-05-22 14:51:39 -04006079 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006080 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6081 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006082 phba->hba_flag |= HBA_FCOE_MODE;
6083 else
6084 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006085
6086 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6087 LPFC_DCBX_CEE_MODE)
6088 phba->hba_flag |= HBA_FIP_SUPPORT;
6089 else
6090 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6091
James Smart4f2e66c2012-05-09 21:17:07 -04006092 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6093
James Smartc31098c2011-04-16 11:03:33 -04006094 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006095 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6096 "0376 READ_REV Error. SLI Level %d "
6097 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006098 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006099 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006100 kfree(vpd);
6101 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006102 }
James Smartcd1c8302011-10-10 21:33:25 -04006103
6104 /*
James Smartff78d8f2011-12-13 13:21:35 -05006105 * Continue initialization with default values even if driver failed
6106 * to read FCoE param config regions, only read parameters if the
6107 * board is FCoE
6108 */
6109 if (phba->hba_flag & HBA_FCOE_MODE &&
6110 lpfc_sli4_read_fcoe_params(phba))
6111 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6112 "2570 Failed to read FCoE parameters\n");
6113
6114 /*
James Smartcd1c8302011-10-10 21:33:25 -04006115 * Retrieve sli4 device physical port name, failure of doing it
6116 * is considered as non-fatal.
6117 */
6118 rc = lpfc_sli4_retrieve_pport_name(phba);
6119 if (!rc)
6120 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6121 "3080 Successful retrieving SLI4 device "
6122 "physical port name: %s.\n", phba->Port);
6123
James Smartda0436e2009-05-22 14:51:39 -04006124 /*
6125 * Evaluate the read rev and vpd data. Populate the driver
6126 * state with the results. If this routine fails, the failure
6127 * is not fatal as the driver will use generic values.
6128 */
6129 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6130 if (unlikely(!rc)) {
6131 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6132 "0377 Error %d parsing vpd. "
6133 "Using defaults.\n", rc);
6134 rc = 0;
6135 }
James Smart76a95d72010-11-20 23:11:48 -05006136 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006137
James Smartf1126682009-06-10 17:22:44 -04006138 /* Save information as VPD data */
6139 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6140 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6141 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6142 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6143 &mqe->un.read_rev);
6144 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6145 &mqe->un.read_rev);
6146 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6147 &mqe->un.read_rev);
6148 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6149 &mqe->un.read_rev);
6150 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6151 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6152 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6153 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6154 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6155 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6156 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6157 "(%d):0380 READ_REV Status x%x "
6158 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6159 mboxq->vport ? mboxq->vport->vpi : 0,
6160 bf_get(lpfc_mqe_status, mqe),
6161 phba->vpd.rev.opFwName,
6162 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6163 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006164
6165 /*
6166 * Discover the port's supported feature set and match it against the
6167 * hosts requests.
6168 */
6169 lpfc_request_features(phba, mboxq);
6170 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6171 if (unlikely(rc)) {
6172 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006173 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006174 }
6175
6176 /*
6177 * The port must support FCP initiator mode as this is the
6178 * only mode running in the host.
6179 */
6180 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6181 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6182 "0378 No support for fcpi mode.\n");
6183 ftr_rsp++;
6184 }
James Smartfedd3b72011-02-16 12:39:24 -05006185 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6186 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6187 else
6188 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006189 /*
6190 * If the port cannot support the host's requested features
6191 * then turn off the global config parameters to disable the
6192 * feature in the driver. This is not a fatal error.
6193 */
James Smartbf086112011-08-21 21:48:13 -04006194 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6195 if (phba->cfg_enable_bg) {
6196 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6197 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6198 else
6199 ftr_rsp++;
6200 }
James Smartda0436e2009-05-22 14:51:39 -04006201
6202 if (phba->max_vpi && phba->cfg_enable_npiv &&
6203 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6204 ftr_rsp++;
6205
6206 if (ftr_rsp) {
6207 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6208 "0379 Feature Mismatch Data: x%08x %08x "
6209 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6210 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6211 phba->cfg_enable_npiv, phba->max_vpi);
6212 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6213 phba->cfg_enable_bg = 0;
6214 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6215 phba->cfg_enable_npiv = 0;
6216 }
6217
6218 /* These SLI3 features are assumed in SLI4 */
6219 spin_lock_irq(&phba->hbalock);
6220 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6221 spin_unlock_irq(&phba->hbalock);
6222
James Smart6d368e52011-05-24 11:44:12 -04006223 /*
6224 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6225 * calls depends on these resources to complete port setup.
6226 */
6227 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6228 if (rc) {
6229 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6230 "2920 Failed to alloc Resource IDs "
6231 "rc = x%x\n", rc);
6232 goto out_free_mbox;
6233 }
6234
James Smartda0436e2009-05-22 14:51:39 -04006235 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006236 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6237 if (rc) {
6238 phba->link_state = LPFC_HBA_ERROR;
6239 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006240 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006241 }
6242
James Smartda0436e2009-05-22 14:51:39 -04006243 mboxq->vport = vport;
6244 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6245 mp = (struct lpfc_dmabuf *) mboxq->context1;
6246 if (rc == MBX_SUCCESS) {
6247 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6248 rc = 0;
6249 }
6250
6251 /*
6252 * This memory was allocated by the lpfc_read_sparam routine. Release
6253 * it to the mbuf pool.
6254 */
6255 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6256 kfree(mp);
6257 mboxq->context1 = NULL;
6258 if (unlikely(rc)) {
6259 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6260 "0382 READ_SPARAM command failed "
6261 "status %d, mbxStatus x%x\n",
6262 rc, bf_get(lpfc_mqe_status, mqe));
6263 phba->link_state = LPFC_HBA_ERROR;
6264 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006265 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006266 }
6267
James Smart05580562011-05-24 11:40:48 -04006268 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006269
6270 /* Update the fc_host data structures with new wwn. */
6271 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6272 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6273
James Smart8a9d2e82012-05-09 21:16:12 -04006274 /* update host els and scsi xri-sgl sizes and mappings */
6275 rc = lpfc_sli4_xri_sgl_update(phba);
6276 if (unlikely(rc)) {
6277 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6278 "1400 Failed to update xri-sgl size and "
6279 "mapping: %d\n", rc);
6280 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006281 }
6282
James Smart8a9d2e82012-05-09 21:16:12 -04006283 /* register the els sgl pool to the port */
6284 rc = lpfc_sli4_repost_els_sgl_list(phba);
6285 if (unlikely(rc)) {
6286 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6287 "0582 Error %d during els sgl post "
6288 "operation\n", rc);
6289 rc = -ENODEV;
6290 goto out_free_mbox;
6291 }
6292
6293 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006294 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6295 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006297 "0383 Error %d during scsi sgl post "
6298 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006299 /* Some Scsi buffers were moved to the abort scsi list */
6300 /* A pci function reset will repost them */
6301 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006302 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006303 }
6304
6305 /* Post the rpi header region to the device. */
6306 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6307 if (unlikely(rc)) {
6308 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6309 "0393 Error %d during rpi post operation\n",
6310 rc);
6311 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006312 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006313 }
James Smart97f2ecf2012-03-01 22:35:23 -05006314 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006315
James Smart5350d872011-10-10 21:33:49 -04006316 /* Create all the SLI4 queues */
6317 rc = lpfc_sli4_queue_create(phba);
6318 if (rc) {
6319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6320 "3089 Failed to allocate queues\n");
6321 rc = -ENODEV;
6322 goto out_stop_timers;
6323 }
James Smartda0436e2009-05-22 14:51:39 -04006324 /* Set up all the queues to the device */
6325 rc = lpfc_sli4_queue_setup(phba);
6326 if (unlikely(rc)) {
6327 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6328 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006329 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006330 }
6331
6332 /* Arm the CQs and then EQs on device */
6333 lpfc_sli4_arm_cqeq_intr(phba);
6334
6335 /* Indicate device interrupt mode */
6336 phba->sli4_hba.intr_enable = 1;
6337
6338 /* Allow asynchronous mailbox command to go through */
6339 spin_lock_irq(&phba->hbalock);
6340 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6341 spin_unlock_irq(&phba->hbalock);
6342
6343 /* Post receive buffers to the device */
6344 lpfc_sli4_rb_setup(phba);
6345
James Smartfc2b9892010-02-26 14:15:29 -05006346 /* Reset HBA FCF states after HBA reset */
6347 phba->fcf.fcf_flag = 0;
6348 phba->fcf.current_rec.flag = 0;
6349
James Smartda0436e2009-05-22 14:51:39 -04006350 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006351 mod_timer(&vport->els_tmofunc,
6352 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006353
6354 /* Start heart beat timer */
6355 mod_timer(&phba->hb_tmofunc,
6356 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6357 phba->hb_outstanding = 0;
6358 phba->last_completion_time = jiffies;
6359
6360 /* Start error attention (ERATT) polling timer */
6361 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6362
James Smart75baf692010-06-08 18:31:21 -04006363 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6364 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6365 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6366 if (!rc) {
6367 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6368 "2829 This device supports "
6369 "Advanced Error Reporting (AER)\n");
6370 spin_lock_irq(&phba->hbalock);
6371 phba->hba_flag |= HBA_AER_ENABLED;
6372 spin_unlock_irq(&phba->hbalock);
6373 } else {
6374 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6375 "2830 This device does not support "
6376 "Advanced Error Reporting (AER)\n");
6377 phba->cfg_aer_support = 0;
6378 }
James Smart0a96e972011-07-22 18:37:28 -04006379 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006380 }
6381
James Smart76a95d72010-11-20 23:11:48 -05006382 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6383 /*
6384 * The FC Port needs to register FCFI (index 0)
6385 */
6386 lpfc_reg_fcfi(phba, mboxq);
6387 mboxq->vport = phba->pport;
6388 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006389 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006390 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006391 rc = 0;
6392 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6393 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006394
6395 /* Check if the port is configured to be disabled */
6396 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006397 }
James Smart026abb82011-12-13 13:20:45 -05006398
James Smartda0436e2009-05-22 14:51:39 -04006399 /*
6400 * The port is ready, set the host's link state to LINK_DOWN
6401 * in preparation for link interrupts.
6402 */
James Smartda0436e2009-05-22 14:51:39 -04006403 spin_lock_irq(&phba->hbalock);
6404 phba->link_state = LPFC_LINK_DOWN;
6405 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006406 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6407 (phba->hba_flag & LINK_DISABLED)) {
6408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6409 "3103 Adapter Link is disabled.\n");
6410 lpfc_down_link(phba, mboxq);
6411 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6412 if (rc != MBX_SUCCESS) {
6413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6414 "3104 Adapter failed to issue "
6415 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6416 goto out_unset_queue;
6417 }
6418 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006419 /* don't perform init_link on SLI4 FC port loopback test */
6420 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6421 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6422 if (rc)
6423 goto out_unset_queue;
6424 }
James Smart5350d872011-10-10 21:33:49 -04006425 }
6426 mempool_free(mboxq, phba->mbox_mem_pool);
6427 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006428out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006429 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006430 lpfc_sli4_queue_unset(phba);
6431out_destroy_queue:
6432 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006433out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006434 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006435out_free_mbox:
6436 mempool_free(mboxq, phba->mbox_mem_pool);
6437 return rc;
6438}
James Smarte59058c2008-08-24 21:49:00 -04006439
6440/**
James Smart3621a712009-04-06 18:47:14 -04006441 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006442 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006443 *
James Smarte59058c2008-08-24 21:49:00 -04006444 * This is the callback function for mailbox timer. The mailbox
6445 * timer is armed when a new mailbox command is issued and the timer
6446 * is deleted when the mailbox complete. The function is called by
6447 * the kernel timer code when a mailbox does not complete within
6448 * expected time. This function wakes up the worker thread to
6449 * process the mailbox timeout and returns. All the processing is
6450 * done by the worker thread function lpfc_mbox_timeout_handler.
6451 **/
dea31012005-04-17 16:05:31 -05006452void
6453lpfc_mbox_timeout(unsigned long ptr)
6454{
James Smart92d7f7b2007-06-17 19:56:38 -05006455 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006456 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006457 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006458
James Smart2e0fef82007-06-17 19:56:36 -05006459 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006460 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006461 if (!tmo_posted)
6462 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6463 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6464
James Smart5e9d9b82008-06-14 22:52:53 -04006465 if (!tmo_posted)
6466 lpfc_worker_wake_up(phba);
6467 return;
dea31012005-04-17 16:05:31 -05006468}
6469
James Smarte59058c2008-08-24 21:49:00 -04006470
6471/**
James Smart3621a712009-04-06 18:47:14 -04006472 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006473 * @phba: Pointer to HBA context object.
6474 *
6475 * This function is called from worker thread when a mailbox command times out.
6476 * The caller is not required to hold any locks. This function will reset the
6477 * HBA and recover all the pending commands.
6478 **/
dea31012005-04-17 16:05:31 -05006479void
6480lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6481{
James Smart2e0fef82007-06-17 19:56:36 -05006482 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006483 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006484 struct lpfc_sli *psli = &phba->sli;
6485 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006486
James Smarta257bf92009-04-06 18:48:10 -04006487 /* Check the pmbox pointer first. There is a race condition
6488 * between the mbox timeout handler getting executed in the
6489 * worklist and the mailbox actually completing. When this
6490 * race condition occurs, the mbox_active will be NULL.
6491 */
6492 spin_lock_irq(&phba->hbalock);
6493 if (pmbox == NULL) {
6494 lpfc_printf_log(phba, KERN_WARNING,
6495 LOG_MBOX | LOG_SLI,
6496 "0353 Active Mailbox cleared - mailbox timeout "
6497 "exiting\n");
6498 spin_unlock_irq(&phba->hbalock);
6499 return;
6500 }
6501
dea31012005-04-17 16:05:31 -05006502 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006504 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006505 mb->mbxCommand,
6506 phba->pport->port_state,
6507 phba->sli.sli_flag,
6508 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006509 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006510
James Smart1dcb58e2007-04-25 09:51:30 -04006511 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6512 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006513 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006514 */
James Smart2e0fef82007-06-17 19:56:36 -05006515 spin_lock_irq(&phba->pport->work_port_lock);
6516 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6517 spin_unlock_irq(&phba->pport->work_port_lock);
6518 spin_lock_irq(&phba->hbalock);
6519 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006520 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006521 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006522
6523 pring = &psli->ring[psli->fcp_ring];
6524 lpfc_sli_abort_iocb_ring(phba, pring);
6525
6526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006527 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006528
6529 /* Reset the HBA device */
6530 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006531}
6532
James Smarte59058c2008-08-24 21:49:00 -04006533/**
James Smart3772a992009-05-22 14:50:54 -04006534 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006535 * @phba: Pointer to HBA context object.
6536 * @pmbox: Pointer to mailbox object.
6537 * @flag: Flag indicating how the mailbox need to be processed.
6538 *
6539 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006540 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6541 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006542 * The mailbox command can be submitted in polling mode, in which case
6543 * this function will wait in a polling loop for the completion of the
6544 * mailbox.
6545 * If the mailbox is submitted in no_wait mode (not polling) the
6546 * function will submit the command and returns immediately without waiting
6547 * for the mailbox completion. The no_wait is supported only when HBA
6548 * is in SLI2/SLI3 mode - interrupts are enabled.
6549 * The SLI interface allows only one mailbox pending at a time. If the
6550 * mailbox is issued in polling mode and there is already a mailbox
6551 * pending, then the function will return an error. If the mailbox is issued
6552 * in NO_WAIT mode and there is a mailbox pending already, the function
6553 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6554 * The sli layer owns the mailbox object until the completion of mailbox
6555 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6556 * return codes the caller owns the mailbox command after the return of
6557 * the function.
6558 **/
James Smart3772a992009-05-22 14:50:54 -04006559static int
6560lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6561 uint32_t flag)
dea31012005-04-17 16:05:31 -05006562{
dea31012005-04-17 16:05:31 -05006563 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006564 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006565 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006566 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006567 int i;
James Smart09372822008-01-11 01:52:54 -05006568 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006569 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006570 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006571 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006572 int processing_queue = 0;
6573
6574 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6575 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006576 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006577 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006578 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6579 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6580 return MBX_SUCCESS;
6581 }
James Smart58da1ff2008-04-07 10:15:56 -04006582 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006583 pmbox = lpfc_mbox_get(phba);
6584 if (!pmbox) {
6585 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6586 return MBX_SUCCESS;
6587 }
6588 }
dea31012005-04-17 16:05:31 -05006589
James Smarted957682007-06-17 19:56:37 -05006590 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006591 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006592 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006593 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006594 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006595 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006596 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006597 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006598 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006599 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006600 }
6601 }
6602
Linas Vepstas8d63f372007-02-14 14:28:36 -06006603 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006604 if (unlikely(pci_channel_offline(phba->pcidev))) {
6605 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6606 goto out_not_finished;
6607 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006608
James Smarta257bf92009-04-06 18:48:10 -04006609 /* If HBA has a deferred error attention, fail the iocb. */
6610 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6611 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6612 goto out_not_finished;
6613 }
6614
dea31012005-04-17 16:05:31 -05006615 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006616
James Smart3772a992009-05-22 14:50:54 -04006617 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006618 status = MBX_SUCCESS;
6619
James Smart2e0fef82007-06-17 19:56:36 -05006620 if (phba->link_state == LPFC_HBA_ERROR) {
6621 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006622
6623 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006624 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6625 "(%d):0311 Mailbox command x%x cannot "
6626 "issue Data: x%x x%x\n",
6627 pmbox->vport ? pmbox->vport->vpi : 0,
6628 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006629 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006630 }
6631
James Smart9940b972011-03-11 16:06:12 -05006632 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6633 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6634 !(hc_copy & HC_MBINT_ENA)) {
6635 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6636 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006637 "(%d):2528 Mailbox command x%x cannot "
6638 "issue Data: x%x x%x\n",
6639 pmbox->vport ? pmbox->vport->vpi : 0,
6640 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006641 goto out_not_finished;
6642 }
James Smart92908312006-03-07 15:04:13 -05006643 }
6644
dea31012005-04-17 16:05:31 -05006645 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6646 /* Polling for a mbox command when another one is already active
6647 * is not allowed in SLI. Also, the driver must have established
6648 * SLI2 mode to queue and process multiple mbox commands.
6649 */
6650
6651 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006652 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006653
6654 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006655 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6656 "(%d):2529 Mailbox command x%x "
6657 "cannot issue Data: x%x x%x\n",
6658 pmbox->vport ? pmbox->vport->vpi : 0,
6659 pmbox->u.mb.mbxCommand,
6660 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006661 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006662 }
6663
James Smart3772a992009-05-22 14:50:54 -04006664 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006665 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006666 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006667 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6668 "(%d):2530 Mailbox command x%x "
6669 "cannot issue Data: x%x x%x\n",
6670 pmbox->vport ? pmbox->vport->vpi : 0,
6671 pmbox->u.mb.mbxCommand,
6672 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006673 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006674 }
6675
dea31012005-04-17 16:05:31 -05006676 /* Another mailbox command is still being processed, queue this
6677 * command to be processed later.
6678 */
6679 lpfc_mbox_put(phba, pmbox);
6680
6681 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006682 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006683 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006684 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006685 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6686 mb->mbxCommand, phba->pport->port_state,
6687 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006688
6689 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006690 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006691
James Smart858c9f62007-06-17 19:56:39 -05006692 if (pmbox->vport) {
6693 lpfc_debugfs_disc_trc(pmbox->vport,
6694 LPFC_DISC_TRC_MBOX_VPORT,
6695 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6696 (uint32_t)mb->mbxCommand,
6697 mb->un.varWords[0], mb->un.varWords[1]);
6698 }
6699 else {
6700 lpfc_debugfs_disc_trc(phba->pport,
6701 LPFC_DISC_TRC_MBOX,
6702 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6703 (uint32_t)mb->mbxCommand,
6704 mb->un.varWords[0], mb->un.varWords[1]);
6705 }
6706
James Smart2e0fef82007-06-17 19:56:36 -05006707 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006708 }
6709
dea31012005-04-17 16:05:31 -05006710 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6711
6712 /* If we are not polling, we MUST be in SLI2 mode */
6713 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006714 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006715 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006716 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006717 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006718 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006719 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6720 "(%d):2531 Mailbox command x%x "
6721 "cannot issue Data: x%x x%x\n",
6722 pmbox->vport ? pmbox->vport->vpi : 0,
6723 pmbox->u.mb.mbxCommand,
6724 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006725 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006726 }
6727 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006728 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006729 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006730 }
6731
6732 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006733 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006734 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006735 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006736 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006737 mb->mbxCommand, phba->pport->port_state,
6738 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006739
James Smart858c9f62007-06-17 19:56:39 -05006740 if (mb->mbxCommand != MBX_HEARTBEAT) {
6741 if (pmbox->vport) {
6742 lpfc_debugfs_disc_trc(pmbox->vport,
6743 LPFC_DISC_TRC_MBOX_VPORT,
6744 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6745 (uint32_t)mb->mbxCommand,
6746 mb->un.varWords[0], mb->un.varWords[1]);
6747 }
6748 else {
6749 lpfc_debugfs_disc_trc(phba->pport,
6750 LPFC_DISC_TRC_MBOX,
6751 "MBOX Send: cmd:x%x mb:x%x x%x",
6752 (uint32_t)mb->mbxCommand,
6753 mb->un.varWords[0], mb->un.varWords[1]);
6754 }
6755 }
6756
dea31012005-04-17 16:05:31 -05006757 psli->slistat.mbox_cmd++;
6758 evtctr = psli->slistat.mbox_event;
6759
6760 /* next set own bit for the adapter and copy over command word */
6761 mb->mbxOwner = OWN_CHIP;
6762
James Smart3772a992009-05-22 14:50:54 -04006763 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006764 /* Populate mbox extension offset word. */
6765 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6766 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6767 = (uint8_t *)phba->mbox_ext
6768 - (uint8_t *)phba->mbox;
6769 }
6770
6771 /* Copy the mailbox extension data */
6772 if (pmbox->in_ext_byte_len && pmbox->context2) {
6773 lpfc_sli_pcimem_bcopy(pmbox->context2,
6774 (uint8_t *)phba->mbox_ext,
6775 pmbox->in_ext_byte_len);
6776 }
6777 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006778 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006779 } else {
James Smart7a470272010-03-15 11:25:20 -04006780 /* Populate mbox extension offset word. */
6781 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6782 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6783 = MAILBOX_HBA_EXT_OFFSET;
6784
6785 /* Copy the mailbox extension data */
6786 if (pmbox->in_ext_byte_len && pmbox->context2) {
6787 lpfc_memcpy_to_slim(phba->MBslimaddr +
6788 MAILBOX_HBA_EXT_OFFSET,
6789 pmbox->context2, pmbox->in_ext_byte_len);
6790
6791 }
James Smart92908312006-03-07 15:04:13 -05006792 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006793 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006794 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006795 }
6796
6797 /* First copy mbox command data to HBA SLIM, skip past first
6798 word */
6799 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6800 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6801 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6802
6803 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006804 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006805 to_slim = phba->MBslimaddr;
6806 writel(ldata, to_slim);
6807 readl(to_slim); /* flush */
6808
6809 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6810 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006811 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006812 }
6813 }
6814
6815 wmb();
dea31012005-04-17 16:05:31 -05006816
6817 switch (flag) {
6818 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006819 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006820 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006821 /* Interrupt board to do it */
6822 writel(CA_MBATT, phba->CAregaddr);
6823 readl(phba->CAregaddr); /* flush */
6824 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006825 break;
6826
6827 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006828 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006829 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006830 /* Interrupt board to do it */
6831 writel(CA_MBATT, phba->CAregaddr);
6832 readl(phba->CAregaddr); /* flush */
6833
James Smart3772a992009-05-22 14:50:54 -04006834 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006835 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006836 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006837 word0 = le32_to_cpu(word0);
6838 } else {
6839 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006840 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6841 spin_unlock_irqrestore(&phba->hbalock,
6842 drvr_flag);
6843 goto out_not_finished;
6844 }
dea31012005-04-17 16:05:31 -05006845 }
6846
6847 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006848 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6849 spin_unlock_irqrestore(&phba->hbalock,
6850 drvr_flag);
6851 goto out_not_finished;
6852 }
James Smarta183a152011-10-10 21:32:43 -04006853 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6854 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006855 i = 0;
dea31012005-04-17 16:05:31 -05006856 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006857 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6858 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006859 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006860 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006861 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006862 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006863 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006864 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006865 }
6866
6867 /* Check if we took a mbox interrupt while we were
6868 polling */
6869 if (((word0 & OWN_CHIP) != OWN_CHIP)
6870 && (evtctr != psli->slistat.mbox_event))
6871 break;
6872
James Smart09372822008-01-11 01:52:54 -05006873 if (i++ > 10) {
6874 spin_unlock_irqrestore(&phba->hbalock,
6875 drvr_flag);
6876 msleep(1);
6877 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6878 }
dea31012005-04-17 16:05:31 -05006879
James Smart3772a992009-05-22 14:50:54 -04006880 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006881 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006882 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006883 word0 = le32_to_cpu(word0);
6884 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6885 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006886 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006887 /* Check real SLIM for any errors */
6888 slimword0 = readl(phba->MBslimaddr);
6889 slimmb = (MAILBOX_t *) & slimword0;
6890 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6891 && slimmb->mbxStatus) {
6892 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006893 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006894 word0 = slimword0;
6895 }
6896 }
6897 } else {
6898 /* First copy command data */
6899 word0 = readl(phba->MBslimaddr);
6900 }
6901 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006902 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6903 spin_unlock_irqrestore(&phba->hbalock,
6904 drvr_flag);
6905 goto out_not_finished;
6906 }
dea31012005-04-17 16:05:31 -05006907 }
6908
James Smart3772a992009-05-22 14:50:54 -04006909 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006910 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006911 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006912 /* Copy the mailbox extension data */
6913 if (pmbox->out_ext_byte_len && pmbox->context2) {
6914 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6915 pmbox->context2,
6916 pmbox->out_ext_byte_len);
6917 }
dea31012005-04-17 16:05:31 -05006918 } else {
6919 /* First copy command data */
6920 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6921 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006922 /* Copy the mailbox extension data */
6923 if (pmbox->out_ext_byte_len && pmbox->context2) {
6924 lpfc_memcpy_from_slim(pmbox->context2,
6925 phba->MBslimaddr +
6926 MAILBOX_HBA_EXT_OFFSET,
6927 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006928 }
6929 }
6930
6931 writel(HA_MBATT, phba->HAregaddr);
6932 readl(phba->HAregaddr); /* flush */
6933
6934 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6935 status = mb->mbxStatus;
6936 }
6937
James Smart2e0fef82007-06-17 19:56:36 -05006938 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6939 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006940
6941out_not_finished:
6942 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006943 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006944 lpfc_mbox_cmpl_put(phba, pmbox);
6945 }
6946 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006947}
6948
James Smarte59058c2008-08-24 21:49:00 -04006949/**
James Smartf1126682009-06-10 17:22:44 -04006950 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6951 * @phba: Pointer to HBA context object.
6952 *
6953 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6954 * the driver internal pending mailbox queue. It will then try to wait out the
6955 * possible outstanding mailbox command before return.
6956 *
6957 * Returns:
6958 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6959 * the outstanding mailbox command timed out.
6960 **/
6961static int
6962lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6963{
6964 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006965 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006966 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006967
6968 /* Mark the asynchronous mailbox command posting as blocked */
6969 spin_lock_irq(&phba->hbalock);
6970 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006971 /* Determine how long we might wait for the active mailbox
6972 * command to be gracefully completed by firmware.
6973 */
James Smarta183a152011-10-10 21:32:43 -04006974 if (phba->sli.mbox_active)
6975 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6976 phba->sli.mbox_active) *
6977 1000) + jiffies;
6978 spin_unlock_irq(&phba->hbalock);
6979
James Smartf1126682009-06-10 17:22:44 -04006980 /* Wait for the outstnading mailbox command to complete */
6981 while (phba->sli.mbox_active) {
6982 /* Check active mailbox complete status every 2ms */
6983 msleep(2);
6984 if (time_after(jiffies, timeout)) {
6985 /* Timeout, marked the outstanding cmd not complete */
6986 rc = 1;
6987 break;
6988 }
6989 }
6990
6991 /* Can not cleanly block async mailbox command, fails it */
6992 if (rc) {
6993 spin_lock_irq(&phba->hbalock);
6994 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6995 spin_unlock_irq(&phba->hbalock);
6996 }
6997 return rc;
6998}
6999
7000/**
7001 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7002 * @phba: Pointer to HBA context object.
7003 *
7004 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7005 * commands from the driver internal pending mailbox queue. It makes sure
7006 * that there is no outstanding mailbox command before resuming posting
7007 * asynchronous mailbox commands. If, for any reason, there is outstanding
7008 * mailbox command, it will try to wait it out before resuming asynchronous
7009 * mailbox command posting.
7010 **/
7011static void
7012lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7013{
7014 struct lpfc_sli *psli = &phba->sli;
7015
7016 spin_lock_irq(&phba->hbalock);
7017 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7018 /* Asynchronous mailbox posting is not blocked, do nothing */
7019 spin_unlock_irq(&phba->hbalock);
7020 return;
7021 }
7022
7023 /* Outstanding synchronous mailbox command is guaranteed to be done,
7024 * successful or timeout, after timing-out the outstanding mailbox
7025 * command shall always be removed, so just unblock posting async
7026 * mailbox command and resume
7027 */
7028 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7029 spin_unlock_irq(&phba->hbalock);
7030
7031 /* wake up worker thread to post asynchronlous mailbox command */
7032 lpfc_worker_wake_up(phba);
7033}
7034
7035/**
James Smartda0436e2009-05-22 14:51:39 -04007036 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7037 * @phba: Pointer to HBA context object.
7038 * @mboxq: Pointer to mailbox object.
7039 *
7040 * The function posts a mailbox to the port. The mailbox is expected
7041 * to be comletely filled in and ready for the port to operate on it.
7042 * This routine executes a synchronous completion operation on the
7043 * mailbox by polling for its completion.
7044 *
7045 * The caller must not be holding any locks when calling this routine.
7046 *
7047 * Returns:
7048 * MBX_SUCCESS - mailbox posted successfully
7049 * Any of the MBX error values.
7050 **/
7051static int
7052lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7053{
7054 int rc = MBX_SUCCESS;
7055 unsigned long iflag;
7056 uint32_t db_ready;
7057 uint32_t mcqe_status;
7058 uint32_t mbx_cmnd;
7059 unsigned long timeout;
7060 struct lpfc_sli *psli = &phba->sli;
7061 struct lpfc_mqe *mb = &mboxq->u.mqe;
7062 struct lpfc_bmbx_create *mbox_rgn;
7063 struct dma_address *dma_address;
7064 struct lpfc_register bmbx_reg;
7065
7066 /*
7067 * Only one mailbox can be active to the bootstrap mailbox region
7068 * at a time and there is no queueing provided.
7069 */
7070 spin_lock_irqsave(&phba->hbalock, iflag);
7071 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7072 spin_unlock_irqrestore(&phba->hbalock, iflag);
7073 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007074 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007075 "cannot issue Data: x%x x%x\n",
7076 mboxq->vport ? mboxq->vport->vpi : 0,
7077 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007078 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7079 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007080 psli->sli_flag, MBX_POLL);
7081 return MBXERR_ERROR;
7082 }
7083 /* The server grabs the token and owns it until release */
7084 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7085 phba->sli.mbox_active = mboxq;
7086 spin_unlock_irqrestore(&phba->hbalock, iflag);
7087
7088 /*
7089 * Initialize the bootstrap memory region to avoid stale data areas
7090 * in the mailbox post. Then copy the caller's mailbox contents to
7091 * the bmbx mailbox region.
7092 */
7093 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7094 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7095 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7096 sizeof(struct lpfc_mqe));
7097
7098 /* Post the high mailbox dma address to the port and wait for ready. */
7099 dma_address = &phba->sli4_hba.bmbx.dma_address;
7100 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7101
James Smarta183a152011-10-10 21:32:43 -04007102 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007103 * 1000) + jiffies;
7104 do {
7105 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7106 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7107 if (!db_ready)
7108 msleep(2);
7109
7110 if (time_after(jiffies, timeout)) {
7111 rc = MBXERR_ERROR;
7112 goto exit;
7113 }
7114 } while (!db_ready);
7115
7116 /* Post the low mailbox dma address to the port. */
7117 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04007118 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007119 * 1000) + jiffies;
7120 do {
7121 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7122 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7123 if (!db_ready)
7124 msleep(2);
7125
7126 if (time_after(jiffies, timeout)) {
7127 rc = MBXERR_ERROR;
7128 goto exit;
7129 }
7130 } while (!db_ready);
7131
7132 /*
7133 * Read the CQ to ensure the mailbox has completed.
7134 * If so, update the mailbox status so that the upper layers
7135 * can complete the request normally.
7136 */
7137 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7138 sizeof(struct lpfc_mqe));
7139 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7140 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7141 sizeof(struct lpfc_mcqe));
7142 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007143 /*
7144 * When the CQE status indicates a failure and the mailbox status
7145 * indicates success then copy the CQE status into the mailbox status
7146 * (and prefix it with x4000).
7147 */
James Smartda0436e2009-05-22 14:51:39 -04007148 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007149 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7150 bf_set(lpfc_mqe_status, mb,
7151 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007152 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007153 } else
7154 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007155
7156 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007157 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007158 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7159 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007160 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7161 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7162 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007163 bf_get(lpfc_mqe_status, mb),
7164 mb->un.mb_words[0], mb->un.mb_words[1],
7165 mb->un.mb_words[2], mb->un.mb_words[3],
7166 mb->un.mb_words[4], mb->un.mb_words[5],
7167 mb->un.mb_words[6], mb->un.mb_words[7],
7168 mb->un.mb_words[8], mb->un.mb_words[9],
7169 mb->un.mb_words[10], mb->un.mb_words[11],
7170 mb->un.mb_words[12], mboxq->mcqe.word0,
7171 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7172 mboxq->mcqe.trailer);
7173exit:
7174 /* We are holding the token, no needed for lock when release */
7175 spin_lock_irqsave(&phba->hbalock, iflag);
7176 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7177 phba->sli.mbox_active = NULL;
7178 spin_unlock_irqrestore(&phba->hbalock, iflag);
7179 return rc;
7180}
7181
7182/**
7183 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7184 * @phba: Pointer to HBA context object.
7185 * @pmbox: Pointer to mailbox object.
7186 * @flag: Flag indicating how the mailbox need to be processed.
7187 *
7188 * This function is called by discovery code and HBA management code to submit
7189 * a mailbox command to firmware with SLI-4 interface spec.
7190 *
7191 * Return codes the caller owns the mailbox command after the return of the
7192 * function.
7193 **/
7194static int
7195lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7196 uint32_t flag)
7197{
7198 struct lpfc_sli *psli = &phba->sli;
7199 unsigned long iflags;
7200 int rc;
7201
James Smartb76f2dc2011-07-22 18:37:42 -04007202 /* dump from issue mailbox command if setup */
7203 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7204
James Smart8fa38512009-07-19 10:01:03 -04007205 rc = lpfc_mbox_dev_check(phba);
7206 if (unlikely(rc)) {
7207 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007208 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007209 "cannot issue Data: x%x x%x\n",
7210 mboxq->vport ? mboxq->vport->vpi : 0,
7211 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007212 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7213 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007214 psli->sli_flag, flag);
7215 goto out_not_finished;
7216 }
7217
James Smartda0436e2009-05-22 14:51:39 -04007218 /* Detect polling mode and jump to a handler */
7219 if (!phba->sli4_hba.intr_enable) {
7220 if (flag == MBX_POLL)
7221 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7222 else
7223 rc = -EIO;
7224 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007225 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007226 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007227 "(x%x/x%x) failure: "
7228 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7229 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007230 mboxq->vport ? mboxq->vport->vpi : 0,
7231 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007232 lpfc_sli_config_mbox_subsys_get(phba,
7233 mboxq),
7234 lpfc_sli_config_mbox_opcode_get(phba,
7235 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007236 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7237 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7238 bf_get(lpfc_mcqe_ext_status,
7239 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007240 psli->sli_flag, flag);
7241 return rc;
7242 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007243 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7244 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007245 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007246 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007247 mboxq->vport ? mboxq->vport->vpi : 0,
7248 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007249 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7250 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007251 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007252 /* Try to block the asynchronous mailbox posting */
7253 rc = lpfc_sli4_async_mbox_block(phba);
7254 if (!rc) {
7255 /* Successfully blocked, now issue sync mbox cmd */
7256 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7257 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007258 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007259 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007260 "(%d):2597 Sync Mailbox command "
7261 "x%x (x%x/x%x) failure: "
7262 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7263 "Data: x%x x%x\n,",
7264 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007265 mboxq->u.mb.mbxCommand,
7266 lpfc_sli_config_mbox_subsys_get(phba,
7267 mboxq),
7268 lpfc_sli_config_mbox_opcode_get(phba,
7269 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007270 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7271 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7272 bf_get(lpfc_mcqe_ext_status,
7273 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007274 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007275 /* Unblock the async mailbox posting afterward */
7276 lpfc_sli4_async_mbox_unblock(phba);
7277 }
7278 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007279 }
7280
7281 /* Now, interrupt mode asynchrous mailbox command */
7282 rc = lpfc_mbox_cmd_check(phba, mboxq);
7283 if (rc) {
7284 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007285 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007286 "cannot issue Data: x%x x%x\n",
7287 mboxq->vport ? mboxq->vport->vpi : 0,
7288 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007289 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7290 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007291 psli->sli_flag, flag);
7292 goto out_not_finished;
7293 }
James Smartda0436e2009-05-22 14:51:39 -04007294
7295 /* Put the mailbox command to the driver internal FIFO */
7296 psli->slistat.mbox_busy++;
7297 spin_lock_irqsave(&phba->hbalock, iflags);
7298 lpfc_mbox_put(phba, mboxq);
7299 spin_unlock_irqrestore(&phba->hbalock, iflags);
7300 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7301 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007302 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007303 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7304 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007305 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7306 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007307 phba->pport->port_state,
7308 psli->sli_flag, MBX_NOWAIT);
7309 /* Wake up worker thread to transport mailbox command from head */
7310 lpfc_worker_wake_up(phba);
7311
7312 return MBX_BUSY;
7313
7314out_not_finished:
7315 return MBX_NOT_FINISHED;
7316}
7317
7318/**
7319 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7320 * @phba: Pointer to HBA context object.
7321 *
7322 * This function is called by worker thread to send a mailbox command to
7323 * SLI4 HBA firmware.
7324 *
7325 **/
7326int
7327lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7328{
7329 struct lpfc_sli *psli = &phba->sli;
7330 LPFC_MBOXQ_t *mboxq;
7331 int rc = MBX_SUCCESS;
7332 unsigned long iflags;
7333 struct lpfc_mqe *mqe;
7334 uint32_t mbx_cmnd;
7335
7336 /* Check interrupt mode before post async mailbox command */
7337 if (unlikely(!phba->sli4_hba.intr_enable))
7338 return MBX_NOT_FINISHED;
7339
7340 /* Check for mailbox command service token */
7341 spin_lock_irqsave(&phba->hbalock, iflags);
7342 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7343 spin_unlock_irqrestore(&phba->hbalock, iflags);
7344 return MBX_NOT_FINISHED;
7345 }
7346 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7347 spin_unlock_irqrestore(&phba->hbalock, iflags);
7348 return MBX_NOT_FINISHED;
7349 }
7350 if (unlikely(phba->sli.mbox_active)) {
7351 spin_unlock_irqrestore(&phba->hbalock, iflags);
7352 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7353 "0384 There is pending active mailbox cmd\n");
7354 return MBX_NOT_FINISHED;
7355 }
7356 /* Take the mailbox command service token */
7357 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7358
7359 /* Get the next mailbox command from head of queue */
7360 mboxq = lpfc_mbox_get(phba);
7361
7362 /* If no more mailbox command waiting for post, we're done */
7363 if (!mboxq) {
7364 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7365 spin_unlock_irqrestore(&phba->hbalock, iflags);
7366 return MBX_SUCCESS;
7367 }
7368 phba->sli.mbox_active = mboxq;
7369 spin_unlock_irqrestore(&phba->hbalock, iflags);
7370
7371 /* Check device readiness for posting mailbox command */
7372 rc = lpfc_mbox_dev_check(phba);
7373 if (unlikely(rc))
7374 /* Driver clean routine will clean up pending mailbox */
7375 goto out_not_finished;
7376
7377 /* Prepare the mbox command to be posted */
7378 mqe = &mboxq->u.mqe;
7379 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7380
7381 /* Start timer for the mbox_tmo and log some mailbox post messages */
7382 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007383 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007384
7385 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007386 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007387 "x%x x%x\n",
7388 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007389 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7390 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007391 phba->pport->port_state, psli->sli_flag);
7392
7393 if (mbx_cmnd != MBX_HEARTBEAT) {
7394 if (mboxq->vport) {
7395 lpfc_debugfs_disc_trc(mboxq->vport,
7396 LPFC_DISC_TRC_MBOX_VPORT,
7397 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7398 mbx_cmnd, mqe->un.mb_words[0],
7399 mqe->un.mb_words[1]);
7400 } else {
7401 lpfc_debugfs_disc_trc(phba->pport,
7402 LPFC_DISC_TRC_MBOX,
7403 "MBOX Send: cmd:x%x mb:x%x x%x",
7404 mbx_cmnd, mqe->un.mb_words[0],
7405 mqe->un.mb_words[1]);
7406 }
7407 }
7408 psli->slistat.mbox_cmd++;
7409
7410 /* Post the mailbox command to the port */
7411 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7412 if (rc != MBX_SUCCESS) {
7413 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007414 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007415 "cannot issue Data: x%x x%x\n",
7416 mboxq->vport ? mboxq->vport->vpi : 0,
7417 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007418 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7419 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007420 psli->sli_flag, MBX_NOWAIT);
7421 goto out_not_finished;
7422 }
7423
7424 return rc;
7425
7426out_not_finished:
7427 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007428 if (phba->sli.mbox_active) {
7429 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7430 __lpfc_mbox_cmpl_put(phba, mboxq);
7431 /* Release the token */
7432 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7433 phba->sli.mbox_active = NULL;
7434 }
James Smartda0436e2009-05-22 14:51:39 -04007435 spin_unlock_irqrestore(&phba->hbalock, iflags);
7436
7437 return MBX_NOT_FINISHED;
7438}
7439
7440/**
7441 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7442 * @phba: Pointer to HBA context object.
7443 * @pmbox: Pointer to mailbox object.
7444 * @flag: Flag indicating how the mailbox need to be processed.
7445 *
7446 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7447 * the API jump table function pointer from the lpfc_hba struct.
7448 *
7449 * Return codes the caller owns the mailbox command after the return of the
7450 * function.
7451 **/
7452int
7453lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7454{
7455 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7456}
7457
7458/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007459 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007460 * @phba: The hba struct for which this call is being executed.
7461 * @dev_grp: The HBA PCI-Device group number.
7462 *
7463 * This routine sets up the mbox interface API function jump table in @phba
7464 * struct.
7465 * Returns: 0 - success, -ENODEV - failure.
7466 **/
7467int
7468lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7469{
7470
7471 switch (dev_grp) {
7472 case LPFC_PCI_DEV_LP:
7473 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7474 phba->lpfc_sli_handle_slow_ring_event =
7475 lpfc_sli_handle_slow_ring_event_s3;
7476 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7477 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7478 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7479 break;
7480 case LPFC_PCI_DEV_OC:
7481 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7482 phba->lpfc_sli_handle_slow_ring_event =
7483 lpfc_sli_handle_slow_ring_event_s4;
7484 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7485 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7486 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7487 break;
7488 default:
7489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7490 "1420 Invalid HBA PCI-device group: 0x%x\n",
7491 dev_grp);
7492 return -ENODEV;
7493 break;
7494 }
7495 return 0;
7496}
7497
7498/**
James Smart3621a712009-04-06 18:47:14 -04007499 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007500 * @phba: Pointer to HBA context object.
7501 * @pring: Pointer to driver SLI ring object.
7502 * @piocb: Pointer to address of newly added command iocb.
7503 *
7504 * This function is called with hbalock held to add a command
7505 * iocb to the txq when SLI layer cannot submit the command iocb
7506 * to the ring.
7507 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007508void
James Smart92d7f7b2007-06-17 19:56:38 -05007509__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007510 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007511{
7512 /* Insert the caller's iocb in the txq tail for later processing. */
7513 list_add_tail(&piocb->list, &pring->txq);
7514 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007515}
7516
James Smarte59058c2008-08-24 21:49:00 -04007517/**
James Smart3621a712009-04-06 18:47:14 -04007518 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007519 * @phba: Pointer to HBA context object.
7520 * @pring: Pointer to driver SLI ring object.
7521 * @piocb: Pointer to address of newly added command iocb.
7522 *
7523 * This function is called with hbalock held before a new
7524 * iocb is submitted to the firmware. This function checks
7525 * txq to flush the iocbs in txq to Firmware before
7526 * submitting new iocbs to the Firmware.
7527 * If there are iocbs in the txq which need to be submitted
7528 * to firmware, lpfc_sli_next_iocb returns the first element
7529 * of the txq after dequeuing it from txq.
7530 * If there is no iocb in the txq then the function will return
7531 * *piocb and *piocb is set to NULL. Caller needs to check
7532 * *piocb to find if there are more commands in the txq.
7533 **/
dea31012005-04-17 16:05:31 -05007534static struct lpfc_iocbq *
7535lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007536 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007537{
7538 struct lpfc_iocbq * nextiocb;
7539
7540 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7541 if (!nextiocb) {
7542 nextiocb = *piocb;
7543 *piocb = NULL;
7544 }
7545
7546 return nextiocb;
7547}
7548
James Smarte59058c2008-08-24 21:49:00 -04007549/**
James Smart3772a992009-05-22 14:50:54 -04007550 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007551 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007552 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007553 * @piocb: Pointer to command iocb.
7554 * @flag: Flag indicating if this command can be put into txq.
7555 *
James Smart3772a992009-05-22 14:50:54 -04007556 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7557 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7558 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7559 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7560 * this function allows only iocbs for posting buffers. This function finds
7561 * next available slot in the command ring and posts the command to the
7562 * available slot and writes the port attention register to request HBA start
7563 * processing new iocb. If there is no slot available in the ring and
7564 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7565 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007566 *
James Smart3772a992009-05-22 14:50:54 -04007567 * This function is called with hbalock held. The function will return success
7568 * after it successfully submit the iocb to firmware or after adding to the
7569 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007570 **/
James Smart98c9ea52007-10-27 13:37:33 -04007571static int
James Smart3772a992009-05-22 14:50:54 -04007572__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007573 struct lpfc_iocbq *piocb, uint32_t flag)
7574{
7575 struct lpfc_iocbq *nextiocb;
7576 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007577 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007578
James Smart92d7f7b2007-06-17 19:56:38 -05007579 if (piocb->iocb_cmpl && (!piocb->vport) &&
7580 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7581 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7582 lpfc_printf_log(phba, KERN_ERR,
7583 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007584 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007585 piocb->iocb.ulpCommand);
7586 dump_stack();
7587 return IOCB_ERROR;
7588 }
7589
7590
Linas Vepstas8d63f372007-02-14 14:28:36 -06007591 /* If the PCI channel is in offline state, do not post iocbs. */
7592 if (unlikely(pci_channel_offline(phba->pcidev)))
7593 return IOCB_ERROR;
7594
James Smarta257bf92009-04-06 18:48:10 -04007595 /* If HBA has a deferred error attention, fail the iocb. */
7596 if (unlikely(phba->hba_flag & DEFER_ERATT))
7597 return IOCB_ERROR;
7598
dea31012005-04-17 16:05:31 -05007599 /*
7600 * We should never get an IOCB if we are in a < LINK_DOWN state
7601 */
James Smart2e0fef82007-06-17 19:56:36 -05007602 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007603 return IOCB_ERROR;
7604
7605 /*
7606 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007607 * outstanding event.
dea31012005-04-17 16:05:31 -05007608 */
James Smart0b727fe2007-10-27 13:37:25 -04007609 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007610 goto iocb_busy;
7611
James Smart2e0fef82007-06-17 19:56:36 -05007612 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007613 /*
James Smart2680eea2007-04-25 09:52:55 -04007614 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007615 * can be issued if the link is not up.
7616 */
7617 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007618 case CMD_GEN_REQUEST64_CR:
7619 case CMD_GEN_REQUEST64_CX:
7620 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7621 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007622 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007623 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7624 MENLO_TRANSPORT_TYPE))
7625
7626 goto iocb_busy;
7627 break;
dea31012005-04-17 16:05:31 -05007628 case CMD_QUE_RING_BUF_CN:
7629 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007630 /*
7631 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7632 * completion, iocb_cmpl MUST be 0.
7633 */
7634 if (piocb->iocb_cmpl)
7635 piocb->iocb_cmpl = NULL;
7636 /*FALLTHROUGH*/
7637 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007638 case CMD_CLOSE_XRI_CN:
7639 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007640 break;
7641 default:
7642 goto iocb_busy;
7643 }
7644
7645 /*
7646 * For FCP commands, we must be in a state where we can process link
7647 * attention events.
7648 */
7649 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007650 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007651 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007652 }
dea31012005-04-17 16:05:31 -05007653
dea31012005-04-17 16:05:31 -05007654 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7655 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7656 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7657
7658 if (iocb)
7659 lpfc_sli_update_ring(phba, pring);
7660 else
7661 lpfc_sli_update_full_ring(phba, pring);
7662
7663 if (!piocb)
7664 return IOCB_SUCCESS;
7665
7666 goto out_busy;
7667
7668 iocb_busy:
7669 pring->stats.iocb_cmd_delay++;
7670
7671 out_busy:
7672
7673 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007674 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007675 return IOCB_SUCCESS;
7676 }
7677
7678 return IOCB_BUSY;
7679}
7680
James Smart3772a992009-05-22 14:50:54 -04007681/**
James Smart4f774512009-05-22 14:52:35 -04007682 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7683 * @phba: Pointer to HBA context object.
7684 * @piocb: Pointer to command iocb.
7685 * @sglq: Pointer to the scatter gather queue object.
7686 *
7687 * This routine converts the bpl or bde that is in the IOCB
7688 * to a sgl list for the sli4 hardware. The physical address
7689 * of the bpl/bde is converted back to a virtual address.
7690 * If the IOCB contains a BPL then the list of BDE's is
7691 * converted to sli4_sge's. If the IOCB contains a single
7692 * BDE then it is converted to a single sli_sge.
7693 * The IOCB is still in cpu endianess so the contents of
7694 * the bpl can be used without byte swapping.
7695 *
7696 * Returns valid XRI = Success, NO_XRI = Failure.
7697**/
7698static uint16_t
7699lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7700 struct lpfc_sglq *sglq)
7701{
7702 uint16_t xritag = NO_XRI;
7703 struct ulp_bde64 *bpl = NULL;
7704 struct ulp_bde64 bde;
7705 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007706 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007707 IOCB_t *icmd;
7708 int numBdes = 0;
7709 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007710 uint32_t offset = 0; /* accumulated offset in the sg request list */
7711 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007712
7713 if (!piocbq || !sglq)
7714 return xritag;
7715
7716 sgl = (struct sli4_sge *)sglq->sgl;
7717 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007718 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7719 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007720 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7721 numBdes = icmd->un.genreq64.bdl.bdeSize /
7722 sizeof(struct ulp_bde64);
7723 /* The addrHigh and addrLow fields within the IOCB
7724 * have not been byteswapped yet so there is no
7725 * need to swap them back.
7726 */
James Smart1b511972011-12-13 13:23:09 -05007727 if (piocbq->context3)
7728 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7729 else
7730 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007731
James Smart1b511972011-12-13 13:23:09 -05007732 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007733 if (!bpl)
7734 return xritag;
7735
7736 for (i = 0; i < numBdes; i++) {
7737 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007738 sgl->addr_hi = bpl->addrHigh;
7739 sgl->addr_lo = bpl->addrLow;
7740
James Smart05580562011-05-24 11:40:48 -04007741 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007742 if ((i+1) == numBdes)
7743 bf_set(lpfc_sli4_sge_last, sgl, 1);
7744 else
7745 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007746 /* swap the size field back to the cpu so we
7747 * can assign it to the sgl.
7748 */
7749 bde.tus.w = le32_to_cpu(bpl->tus.w);
7750 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007751 /* The offsets in the sgl need to be accumulated
7752 * separately for the request and reply lists.
7753 * The request is always first, the reply follows.
7754 */
7755 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7756 /* add up the reply sg entries */
7757 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7758 inbound++;
7759 /* first inbound? reset the offset */
7760 if (inbound == 1)
7761 offset = 0;
7762 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007763 bf_set(lpfc_sli4_sge_type, sgl,
7764 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007765 offset += bde.tus.f.bdeSize;
7766 }
James Smart546fc852011-03-11 16:06:29 -05007767 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007768 bpl++;
7769 sgl++;
7770 }
7771 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7772 /* The addrHigh and addrLow fields of the BDE have not
7773 * been byteswapped yet so they need to be swapped
7774 * before putting them in the sgl.
7775 */
7776 sgl->addr_hi =
7777 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7778 sgl->addr_lo =
7779 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007780 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007781 bf_set(lpfc_sli4_sge_last, sgl, 1);
7782 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007783 sgl->sge_len =
7784 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007785 }
7786 return sglq->sli4_xritag;
7787}
7788
7789/**
7790 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7791 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007792 *
James Smarta93ff372010-10-22 11:06:08 -04007793 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007794 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7795 * held.
James Smart4f774512009-05-22 14:52:35 -04007796 *
7797 * Return: index into SLI4 fast-path FCP queue index.
7798 **/
James Smart2a76a282012-08-03 12:35:54 -04007799static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007800lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007801{
James Smart2a76a282012-08-03 12:35:54 -04007802 int i;
James Smart4f774512009-05-22 14:52:35 -04007803
James Smart2a76a282012-08-03 12:35:54 -04007804 i = atomic_add_return(1, &phba->fcp_qidx);
7805 i = (i % phba->cfg_fcp_wq_count);
7806 return i;
James Smart4f774512009-05-22 14:52:35 -04007807}
7808
7809/**
7810 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7811 * @phba: Pointer to HBA context object.
7812 * @piocb: Pointer to command iocb.
7813 * @wqe: Pointer to the work queue entry.
7814 *
7815 * This routine converts the iocb command to its Work Queue Entry
7816 * equivalent. The wqe pointer should not have any fields set when
7817 * this routine is called because it will memcpy over them.
7818 * This routine does not set the CQ_ID or the WQEC bits in the
7819 * wqe.
7820 *
7821 * Returns: 0 = Success, IOCB_ERROR = Failure.
7822 **/
7823static int
7824lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7825 union lpfc_wqe *wqe)
7826{
James Smart5ffc2662009-11-18 15:39:44 -05007827 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007828 uint8_t ct = 0;
7829 uint32_t fip;
7830 uint32_t abort_tag;
7831 uint8_t command_type = ELS_COMMAND_NON_FIP;
7832 uint8_t cmnd;
7833 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007834 uint16_t abrt_iotag;
7835 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007836 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007837 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007838 int numBdes, i;
7839 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007840 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007841 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007842 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007843
James Smart45ed1192009-10-02 15:17:02 -04007844 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007845 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007846 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007847 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007848 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007849 command_type = ELS_COMMAND_FIP;
7850 else
7851 command_type = ELS_COMMAND_NON_FIP;
7852
James Smart4f774512009-05-22 14:52:35 -04007853 /* Some of the fields are in the right position already */
7854 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7855 abort_tag = (uint32_t) iocbq->iotag;
7856 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007857 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007858 /* words0-2 bpl convert bde */
7859 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007860 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7861 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007862 bpl = (struct ulp_bde64 *)
7863 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7864 if (!bpl)
7865 return IOCB_ERROR;
7866
7867 /* Should already be byte swapped. */
7868 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7869 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7870 /* swap the size field back to the cpu so we
7871 * can assign it to the sgl.
7872 */
7873 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007874 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7875 total_len = 0;
7876 for (i = 0; i < numBdes; i++) {
7877 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7878 total_len += bde.tus.f.bdeSize;
7879 }
James Smart4f774512009-05-22 14:52:35 -04007880 } else
James Smart5ffc2662009-11-18 15:39:44 -05007881 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007882
7883 iocbq->iocb.ulpIoTag = iocbq->iotag;
7884 cmnd = iocbq->iocb.ulpCommand;
7885
7886 switch (iocbq->iocb.ulpCommand) {
7887 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04007888 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7889 ndlp = iocbq->context_un.ndlp;
7890 else
7891 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007892 if (!iocbq->iocb.ulpLe) {
7893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7894 "2007 Only Limited Edition cmd Format"
7895 " supported 0x%x\n",
7896 iocbq->iocb.ulpCommand);
7897 return IOCB_ERROR;
7898 }
James Smartff78d8f2011-12-13 13:21:35 -05007899
James Smart5ffc2662009-11-18 15:39:44 -05007900 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007901 /* Els_reguest64 has a TMO */
7902 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7903 iocbq->iocb.ulpTimeout);
7904 /* Need a VF for word 4 set the vf bit*/
7905 bf_set(els_req64_vf, &wqe->els_req, 0);
7906 /* And a VFID for word 12 */
7907 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007908 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007909 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7910 iocbq->iocb.ulpContext);
7911 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7912 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007913 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007914 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007915 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7916 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007917 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7918 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007919 if_type = bf_get(lpfc_sli_intf_if_type,
7920 &phba->sli4_hba.sli_intf);
7921 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007922 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007923 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007924 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05007925 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05007926 *pcmd == ELS_CMD_PLOGI)) {
7927 bf_set(els_req64_sp, &wqe->els_req, 1);
7928 bf_set(els_req64_sid, &wqe->els_req,
7929 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04007930 if ((*pcmd == ELS_CMD_FLOGI) &&
7931 !(phba->fc_topology ==
7932 LPFC_TOPOLOGY_LOOP))
7933 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05007934 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7935 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04007936 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007937 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007938 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7939 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7940 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7941 }
James Smartc8685952009-11-18 15:39:16 -05007942 }
James Smart6d368e52011-05-24 11:44:12 -04007943 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7944 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007945 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7946 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7947 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7948 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7949 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7950 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007951 break;
James Smart5ffc2662009-11-18 15:39:44 -05007952 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007953 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7954 iocbq->iocb.un.ulpWord[3]);
7955 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007956 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007957 /* The entire sequence is transmitted for this IOCB */
7958 xmit_len = total_len;
7959 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05007960 if (phba->link_flag & LS_LOOPBACK_MODE)
7961 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04007962 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007963 /* word3 iocb=io_tag32 wqe=reserved */
7964 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007965 /* word4 relative_offset memcpy */
7966 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007967 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7968 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7969 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7970 LPFC_WQE_IOD_WRITE);
7971 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7972 LPFC_WQE_LENLOC_WORD12);
7973 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007974 wqe->xmit_sequence.xmit_len = xmit_len;
7975 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007976 break;
James Smart4f774512009-05-22 14:52:35 -04007977 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007978 /* word3 iocb=iotag32 wqe=seq_payload_len */
7979 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007980 /* word4 iocb=rsvd wqe=rsvd */
7981 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7982 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007983 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007984 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007985 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7986 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7987 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7988 LPFC_WQE_LENLOC_WORD3);
7989 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007990 break;
James Smart4f774512009-05-22 14:52:35 -04007991 case CMD_FCP_IWRITE64_CR:
7992 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007993 /* word3 iocb=iotag wqe=payload_offset_len */
7994 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7995 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007996 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007997 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7998 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7999 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8000 iocbq->iocb.ulpFCP2Rcvy);
8001 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8002 /* Always open the exchange */
8003 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008004 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8005 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8006 LPFC_WQE_LENLOC_WORD4);
8007 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8008 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008009 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8010 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8011 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8012 }
8013 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008014 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008015 case CMD_FCP_IREAD64_CR:
8016 /* word3 iocb=iotag wqe=payload_offset_len */
8017 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8018 wqe->fcp_iread.payload_offset_len =
8019 xmit_len + sizeof(struct fcp_rsp);
8020 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8021 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8022 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8023 iocbq->iocb.ulpFCP2Rcvy);
8024 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008025 /* Always open the exchange */
8026 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008027 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8028 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8029 LPFC_WQE_LENLOC_WORD4);
8030 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8031 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008032 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8033 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8034 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8035 }
8036 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008037 break;
James Smartf1126682009-06-10 17:22:44 -04008038 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008039 /* word3 iocb=IO_TAG wqe=reserved */
8040 wqe->fcp_icmd.rsrvd3 = 0;
8041 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008042 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008043 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8044 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8045 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8046 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8047 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8048 LPFC_WQE_LENLOC_NONE);
8049 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008050 break;
James Smart4f774512009-05-22 14:52:35 -04008051 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008052 /* For this command calculate the xmit length of the
8053 * request bde.
8054 */
8055 xmit_len = 0;
8056 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8057 sizeof(struct ulp_bde64);
8058 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008059 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008060 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8061 break;
James Smart63e801c2010-11-20 23:14:19 -05008062 xmit_len += bde.tus.f.bdeSize;
8063 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008064 /* word3 iocb=IO_TAG wqe=request_payload_len */
8065 wqe->gen_req.request_payload_len = xmit_len;
8066 /* word4 iocb=parameter wqe=relative_offset memcpy */
8067 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008068 /* word6 context tag copied in memcpy */
8069 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8070 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8071 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8072 "2015 Invalid CT %x command 0x%x\n",
8073 ct, iocbq->iocb.ulpCommand);
8074 return IOCB_ERROR;
8075 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008076 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8077 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8078 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8079 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8080 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8081 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8082 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8083 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008084 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008085 break;
James Smart4f774512009-05-22 14:52:35 -04008086 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008087 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008088 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008089 /* word3 iocb=iotag32 wqe=response_payload_len */
8090 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008091 /* word4 */
8092 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008093 /* word5 iocb=rsvd wge=did */
8094 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008095 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8096
8097 if_type = bf_get(lpfc_sli_intf_if_type,
8098 &phba->sli4_hba.sli_intf);
8099 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8100 if (iocbq->vport->fc_flag & FC_PT2PT) {
8101 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8102 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8103 iocbq->vport->fc_myDID);
8104 if (iocbq->vport->fc_myDID == Fabric_DID) {
8105 bf_set(wqe_els_did,
8106 &wqe->xmit_els_rsp.wqe_dest, 0);
8107 }
8108 }
8109 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008110 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8111 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8112 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8113 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008114 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008115 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008116 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008117 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008118 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8119 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8120 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8121 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8122 LPFC_WQE_LENLOC_WORD3);
8123 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008124 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8125 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008126 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8127 iocbq->context2)->virt);
8128 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008129 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8130 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008131 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008132 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8133 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008134 phba->vpi_ids[phba->pport->vpi]);
8135 }
James Smart4f774512009-05-22 14:52:35 -04008136 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008137 break;
James Smart4f774512009-05-22 14:52:35 -04008138 case CMD_CLOSE_XRI_CN:
8139 case CMD_ABORT_XRI_CN:
8140 case CMD_ABORT_XRI_CX:
8141 /* words 0-2 memcpy should be 0 rserved */
8142 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008143 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8144 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8145 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8146 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8147 } else
8148 fip = 0;
8149
8150 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008151 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008152 * The link is down, or the command was ELS_FIP
8153 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008154 * on the wire.
8155 */
8156 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8157 else
8158 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8159 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008160 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8161 wqe->abort_cmd.rsrvd5 = 0;
8162 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008163 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8164 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008165 /*
8166 * The abort handler will send us CMD_ABORT_XRI_CN or
8167 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8168 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008169 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8170 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8171 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8172 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008173 cmnd = CMD_ABORT_XRI_CX;
8174 command_type = OTHER_COMMAND;
8175 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008176 break;
James Smart6669f9b2009-10-02 15:16:45 -04008177 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008178 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008179 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008180 * we re-construct this WQE here based on information in
8181 * iocbq from scratch.
8182 */
8183 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008184 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008185 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008186 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8187 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008188 LPFC_ABTS_UNSOL_INT) {
8189 /* ABTS sent by initiator to CT exchange, the
8190 * RX_ID field will be filled with the newly
8191 * allocated responder XRI.
8192 */
8193 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8194 iocbq->sli4_xritag);
8195 } else {
8196 /* ABTS sent by responder to CT exchange, the
8197 * RX_ID field will be filled with the responder
8198 * RX_ID from ABTS.
8199 */
8200 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008201 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008202 }
James Smart6669f9b2009-10-02 15:16:45 -04008203 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8204 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008205
8206 /* Use CT=VPI */
8207 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8208 ndlp->nlp_DID);
8209 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8210 iocbq->iocb.ulpContext);
8211 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008212 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008213 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008214 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8215 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8216 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008217 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8218 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008219 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8220 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8221 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8222 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8223 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8224 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8225 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8226 }
8227
James Smart7851fe22011-07-22 18:36:52 -04008228 break;
James Smart4f774512009-05-22 14:52:35 -04008229 case CMD_XRI_ABORTED_CX:
8230 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008231 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8232 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8233 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8234 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8235 default:
8236 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8237 "2014 Invalid command 0x%x\n",
8238 iocbq->iocb.ulpCommand);
8239 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008240 break;
James Smart4f774512009-05-22 14:52:35 -04008241 }
James Smart6d368e52011-05-24 11:44:12 -04008242
James Smartf0d9bcc2010-10-22 11:07:09 -04008243 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8244 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8245 wqe->generic.wqe_com.abort_tag = abort_tag;
8246 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8247 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8248 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8249 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008250 return 0;
8251}
8252
8253/**
8254 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8255 * @phba: Pointer to HBA context object.
8256 * @ring_number: SLI ring number to issue iocb on.
8257 * @piocb: Pointer to command iocb.
8258 * @flag: Flag indicating if this command can be put into txq.
8259 *
8260 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8261 * an iocb command to an HBA with SLI-4 interface spec.
8262 *
8263 * This function is called with hbalock held. The function will return success
8264 * after it successfully submit the iocb to firmware or after adding to the
8265 * txq.
8266 **/
8267static int
8268__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8269 struct lpfc_iocbq *piocb, uint32_t flag)
8270{
8271 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008272 union lpfc_wqe wqe;
8273 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008274
8275 if (piocb->sli4_xritag == NO_XRI) {
8276 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008277 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008278 sglq = NULL;
8279 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008280 if (pring->txq_cnt) {
8281 if (!(flag & SLI_IOCB_RET_IOCB)) {
8282 __lpfc_sli_ringtx_put(phba,
8283 pring, piocb);
8284 return IOCB_SUCCESS;
8285 } else {
8286 return IOCB_BUSY;
8287 }
8288 } else {
James Smart6d368e52011-05-24 11:44:12 -04008289 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008290 if (!sglq) {
8291 if (!(flag & SLI_IOCB_RET_IOCB)) {
8292 __lpfc_sli_ringtx_put(phba,
8293 pring,
8294 piocb);
8295 return IOCB_SUCCESS;
8296 } else
8297 return IOCB_BUSY;
8298 }
8299 }
James Smart4f774512009-05-22 14:52:35 -04008300 }
8301 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008302 /* These IO's already have an XRI and a mapped sgl. */
8303 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008304 } else {
James Smart6d368e52011-05-24 11:44:12 -04008305 /*
8306 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008307 * sglq is on the active list
8308 */
8309 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8310 if (!sglq)
8311 return IOCB_ERROR;
8312 }
8313
8314 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008315 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008316 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008317 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008318 return IOCB_ERROR;
8319 }
8320
8321 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8322 return IOCB_ERROR;
8323
James Smart341af102010-01-26 23:07:37 -05008324 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8325 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008326 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8327 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008328 return IOCB_ERROR;
8329 } else {
8330 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8331 return IOCB_ERROR;
8332 }
8333 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8334
8335 return 0;
8336}
8337
8338/**
James Smart3772a992009-05-22 14:50:54 -04008339 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8340 *
8341 * This routine wraps the actual lockless version for issusing IOCB function
8342 * pointer from the lpfc_hba struct.
8343 *
8344 * Return codes:
8345 * IOCB_ERROR - Error
8346 * IOCB_SUCCESS - Success
8347 * IOCB_BUSY - Busy
8348 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008349int
James Smart3772a992009-05-22 14:50:54 -04008350__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8351 struct lpfc_iocbq *piocb, uint32_t flag)
8352{
8353 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8354}
8355
8356/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008357 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008358 * @phba: The hba struct for which this call is being executed.
8359 * @dev_grp: The HBA PCI-Device group number.
8360 *
8361 * This routine sets up the SLI interface API function jump table in @phba
8362 * struct.
8363 * Returns: 0 - success, -ENODEV - failure.
8364 **/
8365int
8366lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8367{
8368
8369 switch (dev_grp) {
8370 case LPFC_PCI_DEV_LP:
8371 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8372 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8373 break;
James Smart4f774512009-05-22 14:52:35 -04008374 case LPFC_PCI_DEV_OC:
8375 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8376 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8377 break;
James Smart3772a992009-05-22 14:50:54 -04008378 default:
8379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8380 "1419 Invalid HBA PCI-device group: 0x%x\n",
8381 dev_grp);
8382 return -ENODEV;
8383 break;
8384 }
8385 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8386 return 0;
8387}
James Smart92d7f7b2007-06-17 19:56:38 -05008388
James Smarte59058c2008-08-24 21:49:00 -04008389/**
James Smart3621a712009-04-06 18:47:14 -04008390 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008391 * @phba: Pointer to HBA context object.
8392 * @pring: Pointer to driver SLI ring object.
8393 * @piocb: Pointer to command iocb.
8394 * @flag: Flag indicating if this command can be put into txq.
8395 *
8396 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8397 * function. This function gets the hbalock and calls
8398 * __lpfc_sli_issue_iocb function and will return the error returned
8399 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8400 * functions which do not hold hbalock.
8401 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008402int
James Smart3772a992009-05-22 14:50:54 -04008403lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008404 struct lpfc_iocbq *piocb, uint32_t flag)
8405{
James Smart2a76a282012-08-03 12:35:54 -04008406 struct lpfc_sli_ring *pring;
James Smart92d7f7b2007-06-17 19:56:38 -05008407 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008408 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008409
James Smart7e56aa22012-08-03 12:35:34 -04008410 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008411 if (piocb->iocb_flag & LPFC_IO_FCP) {
8412 if (unlikely(!phba->sli4_hba.fcp_wq))
8413 return IOCB_ERROR;
8414 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8415 piocb->fcp_wqidx = idx;
8416 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
8417 }
James Smart7e56aa22012-08-03 12:35:34 -04008418 pring = &phba->sli.ring[ring_number];
8419 spin_lock_irqsave(&pring->ring_lock, iflags);
8420 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8421 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8422 } else {
8423 /* For now, SLI2/3 will still use hbalock */
8424 spin_lock_irqsave(&phba->hbalock, iflags);
8425 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8426 spin_unlock_irqrestore(&phba->hbalock, iflags);
8427 }
James Smart92d7f7b2007-06-17 19:56:38 -05008428 return rc;
8429}
8430
James Smarte59058c2008-08-24 21:49:00 -04008431/**
James Smart3621a712009-04-06 18:47:14 -04008432 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008433 * @phba: Pointer to HBA context object.
8434 *
8435 * This function is called while driver attaches with the
8436 * HBA to setup the extra ring. The extra ring is used
8437 * only when driver needs to support target mode functionality
8438 * or IP over FC functionalities.
8439 *
8440 * This function is called with no lock held.
8441 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008442static int
8443lpfc_extra_ring_setup( struct lpfc_hba *phba)
8444{
8445 struct lpfc_sli *psli;
8446 struct lpfc_sli_ring *pring;
8447
8448 psli = &phba->sli;
8449
8450 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008451
8452 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008453 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008454 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8455 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8456 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8457 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008458
James Smarta4bc3372006-12-02 13:34:16 -05008459 /* and give them to the extra ring */
8460 pring = &psli->ring[psli->extra_ring];
8461
James Smart7e56aa22012-08-03 12:35:34 -04008462 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8463 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8464 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8465 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008466
8467 /* Setup default profile for this ring */
8468 pring->iotag_max = 4096;
8469 pring->num_mask = 1;
8470 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008471 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8472 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008473 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8474 return 0;
8475}
8476
James Smartcb69f7d2011-12-13 13:21:57 -05008477/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8478 * @vport: pointer to virtual port object.
8479 * @ndlp: nodelist pointer for the impacted rport.
8480 *
8481 * The driver calls this routine in response to a XRI ABORT CQE
8482 * event from the port. In this event, the driver is required to
8483 * recover its login to the rport even though its login may be valid
8484 * from the driver's perspective. The failed ABTS notice from the
8485 * port indicates the rport is not responding.
8486 */
8487static void
8488lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8489 struct lpfc_nodelist *ndlp)
8490{
8491 struct Scsi_Host *shost;
8492 struct lpfc_hba *phba;
8493 unsigned long flags = 0;
8494
8495 shost = lpfc_shost_from_vport(vport);
8496 phba = vport->phba;
8497 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8498 lpfc_printf_log(phba, KERN_INFO,
8499 LOG_SLI, "3093 No rport recovery needed. "
8500 "rport in state 0x%x\n",
8501 ndlp->nlp_state);
8502 return;
8503 }
8504 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8505 "3094 Start rport recovery on shost id 0x%x "
8506 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8507 "flags 0x%x\n",
8508 shost->host_no, ndlp->nlp_DID,
8509 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8510 ndlp->nlp_flag);
8511 /*
8512 * The rport is not responding. Don't attempt ADISC recovery.
8513 * Remove the FCP-2 flag to force a PLOGI.
8514 */
8515 spin_lock_irqsave(shost->host_lock, flags);
8516 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8517 spin_unlock_irqrestore(shost->host_lock, flags);
8518 lpfc_disc_state_machine(vport, ndlp, NULL,
8519 NLP_EVT_DEVICE_RECOVERY);
8520 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8521 spin_lock_irqsave(shost->host_lock, flags);
8522 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8523 spin_unlock_irqrestore(shost->host_lock, flags);
8524 lpfc_disc_start(vport);
8525}
8526
8527/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8528 * @phba: Pointer to HBA context object.
8529 * @iocbq: Pointer to iocb object.
8530 *
8531 * The async_event handler calls this routine when it receives
8532 * an ASYNC_STATUS_CN event from the port. The port generates
8533 * this event when an Abort Sequence request to an rport fails
8534 * twice in succession. The abort could be originated by the
8535 * driver or by the port. The ABTS could have been for an ELS
8536 * or FCP IO. The port only generates this event when an ABTS
8537 * fails to complete after one retry.
8538 */
8539static void
8540lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8541 struct lpfc_iocbq *iocbq)
8542{
8543 struct lpfc_nodelist *ndlp = NULL;
8544 uint16_t rpi = 0, vpi = 0;
8545 struct lpfc_vport *vport = NULL;
8546
8547 /* The rpi in the ulpContext is vport-sensitive. */
8548 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8549 rpi = iocbq->iocb.ulpContext;
8550
8551 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8552 "3092 Port generated ABTS async event "
8553 "on vpi %d rpi %d status 0x%x\n",
8554 vpi, rpi, iocbq->iocb.ulpStatus);
8555
8556 vport = lpfc_find_vport_by_vpid(phba, vpi);
8557 if (!vport)
8558 goto err_exit;
8559 ndlp = lpfc_findnode_rpi(vport, rpi);
8560 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8561 goto err_exit;
8562
8563 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8564 lpfc_sli_abts_recover_port(vport, ndlp);
8565 return;
8566
8567 err_exit:
8568 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8569 "3095 Event Context not found, no "
8570 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8571 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8572 vpi, rpi);
8573}
8574
8575/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8576 * @phba: pointer to HBA context object.
8577 * @ndlp: nodelist pointer for the impacted rport.
8578 * @axri: pointer to the wcqe containing the failed exchange.
8579 *
8580 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8581 * port. The port generates this event when an abort exchange request to an
8582 * rport fails twice in succession with no reply. The abort could be originated
8583 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8584 */
8585void
8586lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8587 struct lpfc_nodelist *ndlp,
8588 struct sli4_wcqe_xri_aborted *axri)
8589{
8590 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008591 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008592
James Smart6b5151f2012-01-18 16:24:06 -05008593 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008594 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8595 "3115 Node Context not found, driver "
8596 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008597 return;
8598 }
8599
James Smartcb69f7d2011-12-13 13:21:57 -05008600 vport = ndlp->vport;
8601 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8602 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008603 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008604 ndlp->vport->vpi, ndlp->nlp_rpi,
8605 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008606 bf_get(lpfc_wcqe_xa_status, axri),
8607 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008608
James Smart5c1db2a2012-03-01 22:34:36 -05008609 /*
8610 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8611 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8612 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8613 */
8614 ext_status = axri->parameter & WCQE_PARAM_MASK;
8615 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8616 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008617 lpfc_sli_abts_recover_port(vport, ndlp);
8618}
8619
James Smarte59058c2008-08-24 21:49:00 -04008620/**
James Smart3621a712009-04-06 18:47:14 -04008621 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008622 * @phba: Pointer to HBA context object.
8623 * @pring: Pointer to driver SLI ring object.
8624 * @iocbq: Pointer to iocb object.
8625 *
8626 * This function is called by the slow ring event handler
8627 * function when there is an ASYNC event iocb in the ring.
8628 * This function is called with no lock held.
8629 * Currently this function handles only temperature related
8630 * ASYNC events. The function decodes the temperature sensor
8631 * event message and posts events for the management applications.
8632 **/
James Smart98c9ea52007-10-27 13:37:33 -04008633static void
James Smart57127f12007-10-27 13:37:05 -04008634lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8635 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8636{
8637 IOCB_t *icmd;
8638 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008639 struct temp_event temp_event_data;
8640 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008641 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008642
8643 icmd = &iocbq->iocb;
8644 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008645
James Smartcb69f7d2011-12-13 13:21:57 -05008646 switch (evt_code) {
8647 case ASYNC_TEMP_WARN:
8648 case ASYNC_TEMP_SAFE:
8649 temp_event_data.data = (uint32_t) icmd->ulpContext;
8650 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8651 if (evt_code == ASYNC_TEMP_WARN) {
8652 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8653 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8654 "0347 Adapter is very hot, please take "
8655 "corrective action. temperature : %d Celsius\n",
8656 (uint32_t) icmd->ulpContext);
8657 } else {
8658 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8659 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8660 "0340 Adapter temperature is OK now. "
8661 "temperature : %d Celsius\n",
8662 (uint32_t) icmd->ulpContext);
8663 }
8664
8665 /* Send temperature change event to applications */
8666 shost = lpfc_shost_from_vport(phba->pport);
8667 fc_host_post_vendor_event(shost, fc_get_event_number(),
8668 sizeof(temp_event_data), (char *) &temp_event_data,
8669 LPFC_NL_VENDOR_ID);
8670 break;
8671 case ASYNC_STATUS_CN:
8672 lpfc_sli_abts_err_handler(phba, iocbq);
8673 break;
8674 default:
James Smarta257bf92009-04-06 18:48:10 -04008675 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008677 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008678 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008679 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8680 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8681 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8682 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008683 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008684 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8685 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8686 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8687 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8688
James Smartcb69f7d2011-12-13 13:21:57 -05008689 break;
James Smart57127f12007-10-27 13:37:05 -04008690 }
James Smart57127f12007-10-27 13:37:05 -04008691}
8692
8693
James Smarte59058c2008-08-24 21:49:00 -04008694/**
James Smart3621a712009-04-06 18:47:14 -04008695 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008696 * @phba: Pointer to HBA context object.
8697 *
8698 * lpfc_sli_setup sets up rings of the SLI interface with
8699 * number of iocbs per ring and iotags. This function is
8700 * called while driver attach to the HBA and before the
8701 * interrupts are enabled. So there is no need for locking.
8702 *
8703 * This function always returns 0.
8704 **/
dea31012005-04-17 16:05:31 -05008705int
8706lpfc_sli_setup(struct lpfc_hba *phba)
8707{
James Smarted957682007-06-17 19:56:37 -05008708 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008709 struct lpfc_sli *psli = &phba->sli;
8710 struct lpfc_sli_ring *pring;
8711
James Smart2a76a282012-08-03 12:35:54 -04008712 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8713 if (phba->sli_rev == LPFC_SLI_REV4)
8714 psli->num_rings += phba->cfg_fcp_eq_count;
dea31012005-04-17 16:05:31 -05008715 psli->sli_flag = 0;
8716 psli->fcp_ring = LPFC_FCP_RING;
8717 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008718 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008719
James Bottomley604a3e32005-10-29 10:28:33 -05008720 psli->iocbq_lookup = NULL;
8721 psli->iocbq_lookup_len = 0;
8722 psli->last_iotag = 0;
8723
dea31012005-04-17 16:05:31 -05008724 for (i = 0; i < psli->num_rings; i++) {
8725 pring = &psli->ring[i];
8726 switch (i) {
8727 case LPFC_FCP_RING: /* ring 0 - FCP */
8728 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008729 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8730 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8731 pring->sli.sli3.numCiocb +=
8732 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8733 pring->sli.sli3.numRiocb +=
8734 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8735 pring->sli.sli3.numCiocb +=
8736 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8737 pring->sli.sli3.numRiocb +=
8738 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8739 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008740 SLI3_IOCB_CMD_SIZE :
8741 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008742 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008743 SLI3_IOCB_RSP_SIZE :
8744 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008745 pring->iotag_ctr = 0;
8746 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008747 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008748 pring->fast_iotag = pring->iotag_max;
8749 pring->num_mask = 0;
8750 break;
James Smarta4bc3372006-12-02 13:34:16 -05008751 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008752 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008753 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8754 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8755 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008756 SLI3_IOCB_CMD_SIZE :
8757 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008758 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008759 SLI3_IOCB_RSP_SIZE :
8760 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008761 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008762 pring->num_mask = 0;
8763 break;
8764 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8765 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008766 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8767 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8768 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008769 SLI3_IOCB_CMD_SIZE :
8770 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008771 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008772 SLI3_IOCB_RSP_SIZE :
8773 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008774 pring->fast_iotag = 0;
8775 pring->iotag_ctr = 0;
8776 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008777 pring->lpfc_sli_rcv_async_status =
8778 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008779 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008780 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008781 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8782 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008783 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008784 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008785 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008786 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8787 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008788 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008789 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008790 pring->prt[2].profile = 0; /* Mask 2 */
8791 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008792 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008793 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008794 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008795 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008796 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008797 pring->prt[3].profile = 0; /* Mask 3 */
8798 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008799 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008800 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008801 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008802 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008803 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008804 /* abort unsolicited sequence */
8805 pring->prt[4].profile = 0; /* Mask 4 */
8806 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8807 pring->prt[4].type = FC_TYPE_BLS;
8808 pring->prt[4].lpfc_sli_rcv_unsol_event =
8809 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008810 break;
8811 }
James Smart7e56aa22012-08-03 12:35:34 -04008812 totiocbsize += (pring->sli.sli3.numCiocb *
8813 pring->sli.sli3.sizeCiocb) +
8814 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05008815 }
James Smarted957682007-06-17 19:56:37 -05008816 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008817 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008818 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8819 "SLI2 SLIM Data: x%x x%lx\n",
8820 phba->brd_no, totiocbsize,
8821 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008822 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008823 if (phba->cfg_multi_ring_support == 2)
8824 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008825
8826 return 0;
8827}
8828
James Smarte59058c2008-08-24 21:49:00 -04008829/**
James Smart3621a712009-04-06 18:47:14 -04008830 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008831 * @phba: Pointer to HBA context object.
8832 *
8833 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8834 * ring. This function also initializes ring indices of each ring.
8835 * This function is called during the initialization of the SLI
8836 * interface of an HBA.
8837 * This function is called with no lock held and always returns
8838 * 1.
8839 **/
dea31012005-04-17 16:05:31 -05008840int
James Smart2e0fef82007-06-17 19:56:36 -05008841lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008842{
8843 struct lpfc_sli *psli;
8844 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008845 int i;
dea31012005-04-17 16:05:31 -05008846
8847 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008848 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008849 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008850 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008851 /* Initialize list headers for txq and txcmplq as double linked lists */
8852 for (i = 0; i < psli->num_rings; i++) {
8853 pring = &psli->ring[i];
8854 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04008855 pring->sli.sli3.next_cmdidx = 0;
8856 pring->sli.sli3.local_getidx = 0;
8857 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05008858 INIT_LIST_HEAD(&pring->txq);
8859 INIT_LIST_HEAD(&pring->txcmplq);
8860 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008861 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008862 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04008863 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05008864 }
James Smart2e0fef82007-06-17 19:56:36 -05008865 spin_unlock_irq(&phba->hbalock);
8866 return 1;
dea31012005-04-17 16:05:31 -05008867}
8868
James Smarte59058c2008-08-24 21:49:00 -04008869/**
James Smart04c68492009-05-22 14:52:52 -04008870 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8871 * @phba: Pointer to HBA context object.
8872 *
8873 * This routine flushes the mailbox command subsystem. It will unconditionally
8874 * flush all the mailbox commands in the three possible stages in the mailbox
8875 * command sub-system: pending mailbox command queue; the outstanding mailbox
8876 * command; and completed mailbox command queue. It is caller's responsibility
8877 * to make sure that the driver is in the proper state to flush the mailbox
8878 * command sub-system. Namely, the posting of mailbox commands into the
8879 * pending mailbox command queue from the various clients must be stopped;
8880 * either the HBA is in a state that it will never works on the outstanding
8881 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8882 * mailbox command has been completed.
8883 **/
8884static void
8885lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8886{
8887 LIST_HEAD(completions);
8888 struct lpfc_sli *psli = &phba->sli;
8889 LPFC_MBOXQ_t *pmb;
8890 unsigned long iflag;
8891
8892 /* Flush all the mailbox commands in the mbox system */
8893 spin_lock_irqsave(&phba->hbalock, iflag);
8894 /* The pending mailbox command queue */
8895 list_splice_init(&phba->sli.mboxq, &completions);
8896 /* The outstanding active mailbox command */
8897 if (psli->mbox_active) {
8898 list_add_tail(&psli->mbox_active->list, &completions);
8899 psli->mbox_active = NULL;
8900 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8901 }
8902 /* The completed mailbox command queue */
8903 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8904 spin_unlock_irqrestore(&phba->hbalock, iflag);
8905
8906 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8907 while (!list_empty(&completions)) {
8908 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8909 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8910 if (pmb->mbox_cmpl)
8911 pmb->mbox_cmpl(phba, pmb);
8912 }
8913}
8914
8915/**
James Smart3621a712009-04-06 18:47:14 -04008916 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008917 * @vport: Pointer to virtual port object.
8918 *
8919 * lpfc_sli_host_down is called to clean up the resources
8920 * associated with a vport before destroying virtual
8921 * port data structures.
8922 * This function does following operations:
8923 * - Free discovery resources associated with this virtual
8924 * port.
8925 * - Free iocbs associated with this virtual port in
8926 * the txq.
8927 * - Send abort for all iocb commands associated with this
8928 * vport in txcmplq.
8929 *
8930 * This function is called with no lock held and always returns 1.
8931 **/
dea31012005-04-17 16:05:31 -05008932int
James Smart92d7f7b2007-06-17 19:56:38 -05008933lpfc_sli_host_down(struct lpfc_vport *vport)
8934{
James Smart858c9f62007-06-17 19:56:39 -05008935 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008936 struct lpfc_hba *phba = vport->phba;
8937 struct lpfc_sli *psli = &phba->sli;
8938 struct lpfc_sli_ring *pring;
8939 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008940 int i;
8941 unsigned long flags = 0;
8942 uint16_t prev_pring_flag;
8943
8944 lpfc_cleanup_discovery_resources(vport);
8945
8946 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008947 for (i = 0; i < psli->num_rings; i++) {
8948 pring = &psli->ring[i];
8949 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008950 /* Only slow rings */
8951 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008952 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008953 /* Set the lpfc data pending flag */
8954 set_bit(LPFC_DATA_READY, &phba->data_flags);
8955 }
James Smart92d7f7b2007-06-17 19:56:38 -05008956 /*
8957 * Error everything on the txq since these iocbs have not been
8958 * given to the FW yet.
8959 */
James Smart92d7f7b2007-06-17 19:56:38 -05008960 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8961 if (iocb->vport != vport)
8962 continue;
James Smart858c9f62007-06-17 19:56:39 -05008963 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008964 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008965 }
8966
8967 /* Next issue ABTS for everything on the txcmplq */
8968 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8969 list) {
8970 if (iocb->vport != vport)
8971 continue;
8972 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8973 }
8974
8975 pring->flag = prev_pring_flag;
8976 }
8977
8978 spin_unlock_irqrestore(&phba->hbalock, flags);
8979
James Smarta257bf92009-04-06 18:48:10 -04008980 /* Cancel all the IOCBs from the completions list */
8981 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8982 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008983 return 1;
8984}
8985
James Smarte59058c2008-08-24 21:49:00 -04008986/**
James Smart3621a712009-04-06 18:47:14 -04008987 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008988 * @phba: Pointer to HBA context object.
8989 *
8990 * This function cleans up all iocb, buffers, mailbox commands
8991 * while shutting down the HBA. This function is called with no
8992 * lock held and always returns 1.
8993 * This function does the following to cleanup driver resources:
8994 * - Free discovery resources for each virtual port
8995 * - Cleanup any pending fabric iocbs
8996 * - Iterate through the iocb txq and free each entry
8997 * in the list.
8998 * - Free up any buffer posted to the HBA
8999 * - Free mailbox commands in the mailbox queue.
9000 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009001int
James Smart2e0fef82007-06-17 19:56:36 -05009002lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009003{
James Smart2534ba72007-04-25 09:52:20 -04009004 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009005 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009006 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009007 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009008 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009009 int i;
9010
9011 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009012 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009013
dea31012005-04-17 16:05:31 -05009014 lpfc_hba_down_prep(phba);
9015
James Smart92d7f7b2007-06-17 19:56:38 -05009016 lpfc_fabric_abort_hba(phba);
9017
James Smart2e0fef82007-06-17 19:56:36 -05009018 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009019 for (i = 0; i < psli->num_rings; i++) {
9020 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009021 /* Only slow rings */
9022 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009023 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009024 /* Set the lpfc data pending flag */
9025 set_bit(LPFC_DATA_READY, &phba->data_flags);
9026 }
dea31012005-04-17 16:05:31 -05009027
9028 /*
9029 * Error everything on the txq since these iocbs have not been
9030 * given to the FW yet.
9031 */
James Smart2534ba72007-04-25 09:52:20 -04009032 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009033 pring->txq_cnt = 0;
9034
dea31012005-04-17 16:05:31 -05009035 }
James Smart2e0fef82007-06-17 19:56:36 -05009036 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009037
James Smarta257bf92009-04-06 18:48:10 -04009038 /* Cancel all the IOCBs from the completions list */
9039 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9040 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009041
James Smart0ff10d42008-01-11 01:52:36 -05009042 spin_lock_irqsave(&phba->hbalock, flags);
9043 list_splice_init(&phba->elsbuf, &completions);
9044 phba->elsbuf_cnt = 0;
9045 phba->elsbuf_prev_cnt = 0;
9046 spin_unlock_irqrestore(&phba->hbalock, flags);
9047
9048 while (!list_empty(&completions)) {
9049 list_remove_head(&completions, buf_ptr,
9050 struct lpfc_dmabuf, list);
9051 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9052 kfree(buf_ptr);
9053 }
9054
dea31012005-04-17 16:05:31 -05009055 /* Return any active mbox cmds */
9056 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009057
James Smartda0436e2009-05-22 14:51:39 -04009058 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009059 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009060 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009061
James Smartda0436e2009-05-22 14:51:39 -04009062 return 1;
9063}
James Smart92d7f7b2007-06-17 19:56:38 -05009064
James Smartda0436e2009-05-22 14:51:39 -04009065/**
James Smart3621a712009-04-06 18:47:14 -04009066 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009067 * @srcp: Source memory pointer.
9068 * @destp: Destination memory pointer.
9069 * @cnt: Number of words required to be copied.
9070 *
9071 * This function is used for copying data between driver memory
9072 * and the SLI memory. This function also changes the endianness
9073 * of each word if native endianness is different from SLI
9074 * endianness. This function can be called with or without
9075 * lock.
9076 **/
dea31012005-04-17 16:05:31 -05009077void
9078lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9079{
9080 uint32_t *src = srcp;
9081 uint32_t *dest = destp;
9082 uint32_t ldata;
9083 int i;
9084
9085 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9086 ldata = *src;
9087 ldata = le32_to_cpu(ldata);
9088 *dest = ldata;
9089 src++;
9090 dest++;
9091 }
9092}
9093
James Smarte59058c2008-08-24 21:49:00 -04009094
9095/**
James Smarta0c87cb2009-07-19 10:01:10 -04009096 * lpfc_sli_bemem_bcopy - SLI memory copy function
9097 * @srcp: Source memory pointer.
9098 * @destp: Destination memory pointer.
9099 * @cnt: Number of words required to be copied.
9100 *
9101 * This function is used for copying data between a data structure
9102 * with big endian representation to local endianness.
9103 * This function can be called with or without lock.
9104 **/
9105void
9106lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9107{
9108 uint32_t *src = srcp;
9109 uint32_t *dest = destp;
9110 uint32_t ldata;
9111 int i;
9112
9113 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9114 ldata = *src;
9115 ldata = be32_to_cpu(ldata);
9116 *dest = ldata;
9117 src++;
9118 dest++;
9119 }
9120}
9121
9122/**
James Smart3621a712009-04-06 18:47:14 -04009123 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009124 * @phba: Pointer to HBA context object.
9125 * @pring: Pointer to driver SLI ring object.
9126 * @mp: Pointer to driver buffer object.
9127 *
9128 * This function is called with no lock held.
9129 * It always return zero after adding the buffer to the postbufq
9130 * buffer list.
9131 **/
dea31012005-04-17 16:05:31 -05009132int
James Smart2e0fef82007-06-17 19:56:36 -05009133lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9134 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009135{
9136 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9137 later */
James Smart2e0fef82007-06-17 19:56:36 -05009138 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009139 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009140 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009141 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009142 return 0;
9143}
9144
James Smarte59058c2008-08-24 21:49:00 -04009145/**
James Smart3621a712009-04-06 18:47:14 -04009146 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009147 * @phba: Pointer to HBA context object.
9148 *
9149 * When HBQ is enabled, buffers are searched based on tags. This function
9150 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9151 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9152 * does not conflict with tags of buffer posted for unsolicited events.
9153 * The function returns the allocated tag. The function is called with
9154 * no locks held.
9155 **/
James Smart76bb24e2007-10-27 13:38:00 -04009156uint32_t
9157lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9158{
9159 spin_lock_irq(&phba->hbalock);
9160 phba->buffer_tag_count++;
9161 /*
9162 * Always set the QUE_BUFTAG_BIT to distiguish between
9163 * a tag assigned by HBQ.
9164 */
9165 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9166 spin_unlock_irq(&phba->hbalock);
9167 return phba->buffer_tag_count;
9168}
9169
James Smarte59058c2008-08-24 21:49:00 -04009170/**
James Smart3621a712009-04-06 18:47:14 -04009171 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009172 * @phba: Pointer to HBA context object.
9173 * @pring: Pointer to driver SLI ring object.
9174 * @tag: Buffer tag.
9175 *
9176 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9177 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9178 * iocb is posted to the response ring with the tag of the buffer.
9179 * This function searches the pring->postbufq list using the tag
9180 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9181 * iocb. If the buffer is found then lpfc_dmabuf object of the
9182 * buffer is returned to the caller else NULL is returned.
9183 * This function is called with no lock held.
9184 **/
James Smart76bb24e2007-10-27 13:38:00 -04009185struct lpfc_dmabuf *
9186lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9187 uint32_t tag)
9188{
9189 struct lpfc_dmabuf *mp, *next_mp;
9190 struct list_head *slp = &pring->postbufq;
9191
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009192 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009193 spin_lock_irq(&phba->hbalock);
9194 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9195 if (mp->buffer_tag == tag) {
9196 list_del_init(&mp->list);
9197 pring->postbufq_cnt--;
9198 spin_unlock_irq(&phba->hbalock);
9199 return mp;
9200 }
9201 }
9202
9203 spin_unlock_irq(&phba->hbalock);
9204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009205 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009206 "ring %d Data x%lx x%p x%p x%x\n",
9207 pring->ringno, (unsigned long) tag,
9208 slp->next, slp->prev, pring->postbufq_cnt);
9209
9210 return NULL;
9211}
dea31012005-04-17 16:05:31 -05009212
James Smarte59058c2008-08-24 21:49:00 -04009213/**
James Smart3621a712009-04-06 18:47:14 -04009214 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009215 * @phba: Pointer to HBA context object.
9216 * @pring: Pointer to driver SLI ring object.
9217 * @phys: DMA address of the buffer.
9218 *
9219 * This function searches the buffer list using the dma_address
9220 * of unsolicited event to find the driver's lpfc_dmabuf object
9221 * corresponding to the dma_address. The function returns the
9222 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9223 * This function is called by the ct and els unsolicited event
9224 * handlers to get the buffer associated with the unsolicited
9225 * event.
9226 *
9227 * This function is called with no lock held.
9228 **/
dea31012005-04-17 16:05:31 -05009229struct lpfc_dmabuf *
9230lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9231 dma_addr_t phys)
9232{
9233 struct lpfc_dmabuf *mp, *next_mp;
9234 struct list_head *slp = &pring->postbufq;
9235
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009236 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009237 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009238 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9239 if (mp->phys == phys) {
9240 list_del_init(&mp->list);
9241 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009242 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009243 return mp;
9244 }
9245 }
9246
James Smart2e0fef82007-06-17 19:56:36 -05009247 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009249 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009250 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009251 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009252 slp->next, slp->prev, pring->postbufq_cnt);
9253 return NULL;
9254}
9255
James Smarte59058c2008-08-24 21:49:00 -04009256/**
James Smart3621a712009-04-06 18:47:14 -04009257 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009258 * @phba: Pointer to HBA context object.
9259 * @cmdiocb: Pointer to driver command iocb object.
9260 * @rspiocb: Pointer to driver response iocb object.
9261 *
9262 * This function is the completion handler for the abort iocbs for
9263 * ELS commands. This function is called from the ELS ring event
9264 * handler with no lock held. This function frees memory resources
9265 * associated with the abort iocb.
9266 **/
dea31012005-04-17 16:05:31 -05009267static void
James Smart2e0fef82007-06-17 19:56:36 -05009268lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9269 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009270{
James Smart2e0fef82007-06-17 19:56:36 -05009271 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009272 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009273 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009274
9275 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009276
9277 /*
9278 * Assume that the port already completed and returned, or
9279 * will return the iocb. Just Log the message.
9280 */
James Smart2680eea2007-04-25 09:52:55 -04009281 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9282 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9283
James Smart2e0fef82007-06-17 19:56:36 -05009284 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009285 if (phba->sli_rev < LPFC_SLI_REV4) {
9286 if (abort_iotag != 0 &&
9287 abort_iotag <= phba->sli.last_iotag)
9288 abort_iocb =
9289 phba->sli.iocbq_lookup[abort_iotag];
9290 } else
9291 /* For sli4 the abort_tag is the XRI,
9292 * so the abort routine puts the iotag of the iocb
9293 * being aborted in the context field of the abort
9294 * IOCB.
9295 */
9296 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009297
James Smart2a9bf3d2010-06-07 15:24:45 -04009298 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9299 "0327 Cannot abort els iocb %p "
9300 "with tag %x context %x, abort status %x, "
9301 "abort code %x\n",
9302 abort_iocb, abort_iotag, abort_context,
9303 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009304
James Smartff78d8f2011-12-13 13:21:35 -05009305 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009306 }
James Bottomley604a3e32005-10-29 10:28:33 -05009307 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009308 return;
9309}
9310
James Smarte59058c2008-08-24 21:49:00 -04009311/**
James Smart3621a712009-04-06 18:47:14 -04009312 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009313 * @phba: Pointer to HBA context object.
9314 * @cmdiocb: Pointer to driver command iocb object.
9315 * @rspiocb: Pointer to driver response iocb object.
9316 *
9317 * The function is called from SLI ring event handler with no
9318 * lock held. This function is the completion handler for ELS commands
9319 * which are aborted. The function frees memory resources used for
9320 * the aborted ELS commands.
9321 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009322static void
9323lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9324 struct lpfc_iocbq *rspiocb)
9325{
9326 IOCB_t *irsp = &rspiocb->iocb;
9327
9328 /* ELS cmd tag <ulpIoTag> completes */
9329 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009330 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009331 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009332 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009333 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009334 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9335 lpfc_ct_free_iocb(phba, cmdiocb);
9336 else
9337 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009338 return;
9339}
9340
James Smarte59058c2008-08-24 21:49:00 -04009341/**
James Smart5af5eee2010-10-22 11:06:38 -04009342 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009343 * @phba: Pointer to HBA context object.
9344 * @pring: Pointer to driver SLI ring object.
9345 * @cmdiocb: Pointer to driver command iocb object.
9346 *
James Smart5af5eee2010-10-22 11:06:38 -04009347 * This function issues an abort iocb for the provided command iocb down to
9348 * the port. Other than the case the outstanding command iocb is an abort
9349 * request, this function issues abort out unconditionally. This function is
9350 * called with hbalock held. The function returns 0 when it fails due to
9351 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009352 **/
James Smart5af5eee2010-10-22 11:06:38 -04009353static int
9354lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009355 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009356{
James Smart2e0fef82007-06-17 19:56:36 -05009357 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009358 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009359 IOCB_t *icmd = NULL;
9360 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009361 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009362 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009363
James Smart92d7f7b2007-06-17 19:56:38 -05009364 /*
9365 * There are certain command types we don't want to abort. And we
9366 * don't want to abort commands that are already in the process of
9367 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009368 */
9369 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009370 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009371 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9372 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009373 return 0;
9374
dea31012005-04-17 16:05:31 -05009375 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009376 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009377 if (abtsiocbp == NULL)
9378 return 0;
dea31012005-04-17 16:05:31 -05009379
James Smart07951072007-04-25 09:51:38 -04009380 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009381 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009382 */
9383 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9384
dea31012005-04-17 16:05:31 -05009385 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009386 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9387 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009388 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009389 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009390 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9391 }
James Smartda0436e2009-05-22 14:51:39 -04009392 else
9393 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009394 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009395 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009396
James Smart5ffc2662009-11-18 15:39:44 -05009397 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9398 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009399 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9400 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009401
James Smart2e0fef82007-06-17 19:56:36 -05009402 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009403 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9404 else
9405 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9406
9407 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009408
James Smarte8b62012007-08-02 11:10:09 -04009409 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9410 "0339 Abort xri x%x, original iotag x%x, "
9411 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009412 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009413 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009414 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009415
9416 if (phba->sli_rev == LPFC_SLI_REV4) {
9417 /* Note: both hbalock and ring_lock need to be set here */
9418 spin_lock_irqsave(&pring->ring_lock, iflags);
9419 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9420 abtsiocbp, 0);
9421 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9422 } else {
9423 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9424 abtsiocbp, 0);
9425 }
James Smart07951072007-04-25 09:51:38 -04009426
James Smartd7c255b2008-08-24 21:50:00 -04009427 if (retval)
9428 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009429
9430 /*
9431 * Caller to this routine should check for IOCB_ERROR
9432 * and handle it properly. This routine no longer removes
9433 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9434 */
9435 return retval;
9436}
9437
9438/**
9439 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9440 * @phba: Pointer to HBA context object.
9441 * @pring: Pointer to driver SLI ring object.
9442 * @cmdiocb: Pointer to driver command iocb object.
9443 *
9444 * This function issues an abort iocb for the provided command iocb. In case
9445 * of unloading, the abort iocb will not be issued to commands on the ELS
9446 * ring. Instead, the callback function shall be changed to those commands
9447 * so that nothing happens when them finishes. This function is called with
9448 * hbalock held. The function returns 0 when the command iocb is an abort
9449 * request.
9450 **/
9451int
9452lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9453 struct lpfc_iocbq *cmdiocb)
9454{
9455 struct lpfc_vport *vport = cmdiocb->vport;
9456 int retval = IOCB_ERROR;
9457 IOCB_t *icmd = NULL;
9458
9459 /*
9460 * There are certain command types we don't want to abort. And we
9461 * don't want to abort commands that are already in the process of
9462 * being aborted.
9463 */
9464 icmd = &cmdiocb->iocb;
9465 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9466 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9467 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9468 return 0;
9469
9470 /*
9471 * If we're unloading, don't abort iocb on the ELS ring, but change
9472 * the callback so that nothing happens when it finishes.
9473 */
9474 if ((vport->load_flag & FC_UNLOADING) &&
9475 (pring->ringno == LPFC_ELS_RING)) {
9476 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9477 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9478 else
9479 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9480 goto abort_iotag_exit;
9481 }
9482
9483 /* Now, we try to issue the abort to the cmdiocb out */
9484 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9485
James Smart07951072007-04-25 09:51:38 -04009486abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009487 /*
9488 * Caller to this routine should check for IOCB_ERROR
9489 * and handle it properly. This routine no longer removes
9490 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009491 */
James Smart2e0fef82007-06-17 19:56:36 -05009492 return retval;
dea31012005-04-17 16:05:31 -05009493}
9494
James Smarte59058c2008-08-24 21:49:00 -04009495/**
James Smart5af5eee2010-10-22 11:06:38 -04009496 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9497 * @phba: Pointer to HBA context object.
9498 * @pring: Pointer to driver SLI ring object.
9499 *
9500 * This function aborts all iocbs in the given ring and frees all the iocb
9501 * objects in txq. This function issues abort iocbs unconditionally for all
9502 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9503 * to complete before the return of this function. The caller is not required
9504 * to hold any locks.
9505 **/
9506static void
9507lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9508{
9509 LIST_HEAD(completions);
9510 struct lpfc_iocbq *iocb, *next_iocb;
9511
9512 if (pring->ringno == LPFC_ELS_RING)
9513 lpfc_fabric_abort_hba(phba);
9514
9515 spin_lock_irq(&phba->hbalock);
9516
9517 /* Take off all the iocbs on txq for cancelling */
9518 list_splice_init(&pring->txq, &completions);
9519 pring->txq_cnt = 0;
9520
9521 /* Next issue ABTS for everything on the txcmplq */
9522 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9523 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9524
9525 spin_unlock_irq(&phba->hbalock);
9526
9527 /* Cancel all the IOCBs from the completions list */
9528 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9529 IOERR_SLI_ABORTED);
9530}
9531
9532/**
9533 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9534 * @phba: pointer to lpfc HBA data structure.
9535 *
9536 * This routine will abort all pending and outstanding iocbs to an HBA.
9537 **/
9538void
9539lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9540{
9541 struct lpfc_sli *psli = &phba->sli;
9542 struct lpfc_sli_ring *pring;
9543 int i;
9544
9545 for (i = 0; i < psli->num_rings; i++) {
9546 pring = &psli->ring[i];
9547 lpfc_sli_iocb_ring_abort(phba, pring);
9548 }
9549}
9550
9551/**
James Smart3621a712009-04-06 18:47:14 -04009552 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009553 * @iocbq: Pointer to driver iocb object.
9554 * @vport: Pointer to driver virtual port object.
9555 * @tgt_id: SCSI ID of the target.
9556 * @lun_id: LUN ID of the scsi device.
9557 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9558 *
James Smart3621a712009-04-06 18:47:14 -04009559 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009560 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9561 * 0 if the filtering criteria is met for the given iocb and will return
9562 * 1 if the filtering criteria is not met.
9563 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9564 * given iocb is for the SCSI device specified by vport, tgt_id and
9565 * lun_id parameter.
9566 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9567 * given iocb is for the SCSI target specified by vport and tgt_id
9568 * parameters.
9569 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9570 * given iocb is for the SCSI host associated with the given vport.
9571 * This function is called with no locks held.
9572 **/
dea31012005-04-17 16:05:31 -05009573static int
James Smart51ef4c22007-08-02 11:10:31 -04009574lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9575 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009576 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009577{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009578 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009579 int rc = 1;
9580
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009581 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9582 return rc;
9583
James Smart51ef4c22007-08-02 11:10:31 -04009584 if (iocbq->vport != vport)
9585 return rc;
9586
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009587 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009588
James Smart495a7142008-06-14 22:52:59 -04009589 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009590 return rc;
9591
9592 switch (ctx_cmd) {
9593 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009594 if ((lpfc_cmd->rdata->pnode) &&
9595 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9596 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009597 rc = 0;
9598 break;
9599 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009600 if ((lpfc_cmd->rdata->pnode) &&
9601 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009602 rc = 0;
9603 break;
dea31012005-04-17 16:05:31 -05009604 case LPFC_CTX_HOST:
9605 rc = 0;
9606 break;
9607 default:
9608 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009609 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009610 break;
9611 }
9612
9613 return rc;
9614}
9615
James Smarte59058c2008-08-24 21:49:00 -04009616/**
James Smart3621a712009-04-06 18:47:14 -04009617 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009618 * @vport: Pointer to virtual port.
9619 * @tgt_id: SCSI ID of the target.
9620 * @lun_id: LUN ID of the scsi device.
9621 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9622 *
9623 * This function returns number of FCP commands pending for the vport.
9624 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9625 * commands pending on the vport associated with SCSI device specified
9626 * by tgt_id and lun_id parameters.
9627 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9628 * commands pending on the vport associated with SCSI target specified
9629 * by tgt_id parameter.
9630 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9631 * commands pending on the vport.
9632 * This function returns the number of iocbs which satisfy the filter.
9633 * This function is called without any lock held.
9634 **/
dea31012005-04-17 16:05:31 -05009635int
James Smart51ef4c22007-08-02 11:10:31 -04009636lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9637 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009638{
James Smart51ef4c22007-08-02 11:10:31 -04009639 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009640 struct lpfc_iocbq *iocbq;
9641 int sum, i;
dea31012005-04-17 16:05:31 -05009642
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009643 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9644 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009645
James Smart51ef4c22007-08-02 11:10:31 -04009646 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9647 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009648 sum++;
dea31012005-04-17 16:05:31 -05009649 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009650
dea31012005-04-17 16:05:31 -05009651 return sum;
9652}
9653
James Smarte59058c2008-08-24 21:49:00 -04009654/**
James Smart3621a712009-04-06 18:47:14 -04009655 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009656 * @phba: Pointer to HBA context object
9657 * @cmdiocb: Pointer to command iocb object.
9658 * @rspiocb: Pointer to response iocb object.
9659 *
9660 * This function is called when an aborted FCP iocb completes. This
9661 * function is called by the ring event handler with no lock held.
9662 * This function frees the iocb.
9663 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009664void
James Smart2e0fef82007-06-17 19:56:36 -05009665lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9666 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009667{
James Smartcb69f7d2011-12-13 13:21:57 -05009668 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9669 "3096 ABORT_XRI_CN completing on xri x%x "
9670 "original iotag x%x, abort cmd iotag x%x "
9671 "status 0x%x, reason 0x%x\n",
9672 cmdiocb->iocb.un.acxri.abortContextTag,
9673 cmdiocb->iocb.un.acxri.abortIoTag,
9674 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9675 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009676 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009677 return;
9678}
9679
James Smarte59058c2008-08-24 21:49:00 -04009680/**
James Smart3621a712009-04-06 18:47:14 -04009681 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009682 * @vport: Pointer to virtual port.
9683 * @pring: Pointer to driver SLI ring object.
9684 * @tgt_id: SCSI ID of the target.
9685 * @lun_id: LUN ID of the scsi device.
9686 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9687 *
9688 * This function sends an abort command for every SCSI command
9689 * associated with the given virtual port pending on the ring
9690 * filtered by lpfc_sli_validate_fcp_iocb function.
9691 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9692 * FCP iocbs associated with lun specified by tgt_id and lun_id
9693 * parameters
9694 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9695 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9696 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9697 * FCP iocbs associated with virtual port.
9698 * This function returns number of iocbs it failed to abort.
9699 * This function is called with no locks held.
9700 **/
dea31012005-04-17 16:05:31 -05009701int
James Smart51ef4c22007-08-02 11:10:31 -04009702lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9703 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009704{
James Smart51ef4c22007-08-02 11:10:31 -04009705 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009706 struct lpfc_iocbq *iocbq;
9707 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009708 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009709 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009710 int i;
dea31012005-04-17 16:05:31 -05009711
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009712 for (i = 1; i <= phba->sli.last_iotag; i++) {
9713 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009714
James Smart51ef4c22007-08-02 11:10:31 -04009715 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009716 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009717 continue;
9718
9719 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009720 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009721 if (abtsiocb == NULL) {
9722 errcnt++;
9723 continue;
9724 }
dea31012005-04-17 16:05:31 -05009725
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009726 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009727 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9728 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009729 if (phba->sli_rev == LPFC_SLI_REV4)
9730 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9731 else
9732 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009733 abtsiocb->iocb.ulpLe = 1;
9734 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009735 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009736
James Smart5ffc2662009-11-18 15:39:44 -05009737 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9738 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009739 if (iocbq->iocb_flag & LPFC_IO_FCP)
9740 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009741
James Smart2e0fef82007-06-17 19:56:36 -05009742 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009743 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9744 else
9745 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9746
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009747 /* Setup callback routine and issue the command. */
9748 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009749 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9750 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009751 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009752 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009753 errcnt++;
9754 continue;
9755 }
9756 }
9757
9758 return errcnt;
9759}
9760
James Smarte59058c2008-08-24 21:49:00 -04009761/**
James Smart3621a712009-04-06 18:47:14 -04009762 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009763 * @phba: Pointer to HBA context object.
9764 * @cmdiocbq: Pointer to command iocb.
9765 * @rspiocbq: Pointer to response iocb.
9766 *
9767 * This function is the completion handler for iocbs issued using
9768 * lpfc_sli_issue_iocb_wait function. This function is called by the
9769 * ring event handler function without any lock held. This function
9770 * can be called from both worker thread context and interrupt
9771 * context. This function also can be called from other thread which
9772 * cleans up the SLI layer objects.
9773 * This function copy the contents of the response iocb to the
9774 * response iocb memory object provided by the caller of
9775 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9776 * sleeps for the iocb completion.
9777 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009778static void
9779lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9780 struct lpfc_iocbq *cmdiocbq,
9781 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009782{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009783 wait_queue_head_t *pdone_q;
9784 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009785 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009786
James Smart2e0fef82007-06-17 19:56:36 -05009787 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009788 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9789 if (cmdiocbq->context2 && rspiocbq)
9790 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9791 &rspiocbq->iocb, sizeof(IOCB_t));
9792
James Smart0f65ff62010-02-26 14:14:23 -05009793 /* Set the exchange busy flag for task management commands */
9794 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9795 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9796 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9797 cur_iocbq);
9798 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9799 }
9800
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009801 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009802 if (pdone_q)
9803 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009804 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009805 return;
9806}
9807
James Smarte59058c2008-08-24 21:49:00 -04009808/**
James Smartd11e31d2009-06-10 17:23:06 -04009809 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9810 * @phba: Pointer to HBA context object..
9811 * @piocbq: Pointer to command iocb.
9812 * @flag: Flag to test.
9813 *
9814 * This routine grabs the hbalock and then test the iocb_flag to
9815 * see if the passed in flag is set.
9816 * Returns:
9817 * 1 if flag is set.
9818 * 0 if flag is not set.
9819 **/
9820static int
9821lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9822 struct lpfc_iocbq *piocbq, uint32_t flag)
9823{
9824 unsigned long iflags;
9825 int ret;
9826
9827 spin_lock_irqsave(&phba->hbalock, iflags);
9828 ret = piocbq->iocb_flag & flag;
9829 spin_unlock_irqrestore(&phba->hbalock, iflags);
9830 return ret;
9831
9832}
9833
9834/**
James Smart3621a712009-04-06 18:47:14 -04009835 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009836 * @phba: Pointer to HBA context object..
9837 * @pring: Pointer to sli ring.
9838 * @piocb: Pointer to command iocb.
9839 * @prspiocbq: Pointer to response iocb.
9840 * @timeout: Timeout in number of seconds.
9841 *
9842 * This function issues the iocb to firmware and waits for the
9843 * iocb to complete. If the iocb command is not
9844 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9845 * Caller should not free the iocb resources if this function
9846 * returns IOCB_TIMEDOUT.
9847 * The function waits for the iocb completion using an
9848 * non-interruptible wait.
9849 * This function will sleep while waiting for iocb completion.
9850 * So, this function should not be called from any context which
9851 * does not allow sleeping. Due to the same reason, this function
9852 * cannot be called with interrupt disabled.
9853 * This function assumes that the iocb completions occur while
9854 * this function sleep. So, this function cannot be called from
9855 * the thread which process iocb completion for this ring.
9856 * This function clears the iocb_flag of the iocb object before
9857 * issuing the iocb and the iocb completion handler sets this
9858 * flag and wakes this thread when the iocb completes.
9859 * The contents of the response iocb will be copied to prspiocbq
9860 * by the completion handler when the command completes.
9861 * This function returns IOCB_SUCCESS when success.
9862 * This function is called with no lock held.
9863 **/
dea31012005-04-17 16:05:31 -05009864int
James Smart2e0fef82007-06-17 19:56:36 -05009865lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009866 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009867 struct lpfc_iocbq *piocb,
9868 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009869 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009870{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009871 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009872 long timeleft, timeout_req = 0;
9873 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009874 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009875 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009876 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009877 * If the caller has provided a response iocbq buffer, then context2
9878 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009879 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009880 if (prspiocbq) {
9881 if (piocb->context2)
9882 return IOCB_ERROR;
9883 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009884 }
9885
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009886 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9887 piocb->context_un.wait_queue = &done_q;
9888 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009889
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009890 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009891 if (lpfc_readl(phba->HCregaddr, &creg_val))
9892 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009893 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9894 writel(creg_val, phba->HCregaddr);
9895 readl(phba->HCregaddr); /* flush */
9896 }
9897
James Smart2a9bf3d2010-06-07 15:24:45 -04009898 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9899 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009900 if (retval == IOCB_SUCCESS) {
9901 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009902 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009903 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009904 timeout_req);
dea31012005-04-17 16:05:31 -05009905
James Smart7054a602007-04-25 09:52:34 -04009906 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009908 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009909 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009910 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009911 "0338 IOCB wait timeout error - no "
9912 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009913 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009914 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009916 "0330 IOCB wake NOT set, "
9917 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009918 timeout, (timeleft / jiffies));
9919 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009920 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009921 } else if (retval == IOCB_BUSY) {
9922 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9923 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9924 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9925 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009926 } else {
9927 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009928 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009929 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009930 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009931 }
9932
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009933 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009934 if (lpfc_readl(phba->HCregaddr, &creg_val))
9935 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009936 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9937 writel(creg_val, phba->HCregaddr);
9938 readl(phba->HCregaddr); /* flush */
9939 }
9940
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009941 if (prspiocbq)
9942 piocb->context2 = NULL;
9943
9944 piocb->context_un.wait_queue = NULL;
9945 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009946 return retval;
9947}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009948
James Smarte59058c2008-08-24 21:49:00 -04009949/**
James Smart3621a712009-04-06 18:47:14 -04009950 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009951 * @phba: Pointer to HBA context object.
9952 * @pmboxq: Pointer to driver mailbox object.
9953 * @timeout: Timeout in number of seconds.
9954 *
9955 * This function issues the mailbox to firmware and waits for the
9956 * mailbox command to complete. If the mailbox command is not
9957 * completed within timeout seconds, it returns MBX_TIMEOUT.
9958 * The function waits for the mailbox completion using an
9959 * interruptible wait. If the thread is woken up due to a
9960 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9961 * should not free the mailbox resources, if this function returns
9962 * MBX_TIMEOUT.
9963 * This function will sleep while waiting for mailbox completion.
9964 * So, this function should not be called from any context which
9965 * does not allow sleeping. Due to the same reason, this function
9966 * cannot be called with interrupt disabled.
9967 * This function assumes that the mailbox completion occurs while
9968 * this function sleep. So, this function cannot be called from
9969 * the worker thread which processes mailbox completion.
9970 * This function is called in the context of HBA management
9971 * applications.
9972 * This function returns MBX_SUCCESS when successful.
9973 * This function is called with no lock held.
9974 **/
dea31012005-04-17 16:05:31 -05009975int
James Smart2e0fef82007-06-17 19:56:36 -05009976lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009977 uint32_t timeout)
9978{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009979 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009980 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009981 unsigned long flag;
dea31012005-04-17 16:05:31 -05009982
9983 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009984 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009985 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009986
James Smart495a7142008-06-14 22:52:59 -04009987 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009988 /* setup wake call as IOCB callback */
9989 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9990 /* setup context field to pass wait_queue pointer to wake function */
9991 pmboxq->context1 = &done_q;
9992
dea31012005-04-17 16:05:31 -05009993 /* now issue the command */
9994 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009995 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009996 wait_event_interruptible_timeout(done_q,
9997 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9998 timeout * HZ);
9999
James Smart858c9f62007-06-17 19:56:39 -050010000 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010001 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -040010002 /*
10003 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10004 * else do not free the resources.
10005 */
James Smartd7c47992010-06-08 18:31:54 -040010006 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010007 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010008 lpfc_sli4_swap_str(phba, pmboxq);
10009 } else {
James Smart7054a602007-04-25 09:52:34 -040010010 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010011 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10012 }
10013 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010014 }
10015
dea31012005-04-17 16:05:31 -050010016 return retval;
10017}
10018
James Smarte59058c2008-08-24 21:49:00 -040010019/**
James Smart3772a992009-05-22 14:50:54 -040010020 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010021 * @phba: Pointer to HBA context.
10022 *
James Smart3772a992009-05-22 14:50:54 -040010023 * This function is called to shutdown the driver's mailbox sub-system.
10024 * It first marks the mailbox sub-system is in a block state to prevent
10025 * the asynchronous mailbox command from issued off the pending mailbox
10026 * command queue. If the mailbox command sub-system shutdown is due to
10027 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10028 * the mailbox sub-system flush routine to forcefully bring down the
10029 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10030 * as with offline or HBA function reset), this routine will wait for the
10031 * outstanding mailbox command to complete before invoking the mailbox
10032 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010033 **/
James Smart3772a992009-05-22 14:50:54 -040010034void
James Smart618a5232012-06-12 13:54:36 -040010035lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010036{
James Smart3772a992009-05-22 14:50:54 -040010037 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010038 unsigned long timeout;
10039
James Smart618a5232012-06-12 13:54:36 -040010040 if (mbx_action == LPFC_MBX_NO_WAIT) {
10041 /* delay 100ms for port state */
10042 msleep(100);
10043 lpfc_sli_mbox_sys_flush(phba);
10044 return;
10045 }
James Smarta183a152011-10-10 21:32:43 -040010046 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010047
James Smart3772a992009-05-22 14:50:54 -040010048 spin_lock_irq(&phba->hbalock);
10049 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010050
10051 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010052 /* Determine how long we might wait for the active mailbox
10053 * command to be gracefully completed by firmware.
10054 */
James Smarta183a152011-10-10 21:32:43 -040010055 if (phba->sli.mbox_active)
10056 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10057 phba->sli.mbox_active) *
10058 1000) + jiffies;
10059 spin_unlock_irq(&phba->hbalock);
10060
James Smart3772a992009-05-22 14:50:54 -040010061 while (phba->sli.mbox_active) {
10062 /* Check active mailbox complete status every 2ms */
10063 msleep(2);
10064 if (time_after(jiffies, timeout))
10065 /* Timeout, let the mailbox flush routine to
10066 * forcefully release active mailbox command
10067 */
10068 break;
10069 }
James Smartd7069f02012-03-01 22:36:29 -050010070 } else
10071 spin_unlock_irq(&phba->hbalock);
10072
James Smart3772a992009-05-22 14:50:54 -040010073 lpfc_sli_mbox_sys_flush(phba);
10074}
10075
10076/**
10077 * lpfc_sli_eratt_read - read sli-3 error attention events
10078 * @phba: Pointer to HBA context.
10079 *
10080 * This function is called to read the SLI3 device error attention registers
10081 * for possible error attention events. The caller must hold the hostlock
10082 * with spin_lock_irq().
10083 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010084 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010085 * Register and returns 0 otherwise.
10086 **/
10087static int
10088lpfc_sli_eratt_read(struct lpfc_hba *phba)
10089{
James Smarted957682007-06-17 19:56:37 -050010090 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010091
James Smart3772a992009-05-22 14:50:54 -040010092 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010093 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10094 goto unplug_err;
10095
James Smart3772a992009-05-22 14:50:54 -040010096 if (ha_copy & HA_ERATT) {
10097 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010098 if (lpfc_sli_read_hs(phba))
10099 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010100
James Smart3772a992009-05-22 14:50:54 -040010101 /* Check if there is a deferred error condition is active */
10102 if ((HS_FFER1 & phba->work_hs) &&
10103 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010104 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010105 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010106 /* Clear all interrupt enable conditions */
10107 writel(0, phba->HCregaddr);
10108 readl(phba->HCregaddr);
10109 }
10110
10111 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010112 phba->work_ha |= HA_ERATT;
10113 /* Indicate polling handles this ERATT */
10114 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010115 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010116 }
James Smart3772a992009-05-22 14:50:54 -040010117 return 0;
James Smart9940b972011-03-11 16:06:12 -050010118
10119unplug_err:
10120 /* Set the driver HS work bitmap */
10121 phba->work_hs |= UNPLUG_ERR;
10122 /* Set the driver HA work bitmap */
10123 phba->work_ha |= HA_ERATT;
10124 /* Indicate polling handles this ERATT */
10125 phba->hba_flag |= HBA_ERATT_HANDLED;
10126 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010127}
10128
James Smarte59058c2008-08-24 21:49:00 -040010129/**
James Smartda0436e2009-05-22 14:51:39 -040010130 * lpfc_sli4_eratt_read - read sli-4 error attention events
10131 * @phba: Pointer to HBA context.
10132 *
10133 * This function is called to read the SLI4 device error attention registers
10134 * for possible error attention events. The caller must hold the hostlock
10135 * with spin_lock_irq().
10136 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010137 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010138 * Register and returns 0 otherwise.
10139 **/
10140static int
10141lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10142{
10143 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010144 uint32_t if_type, portsmphr;
10145 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010146
James Smart2fcee4b2010-12-15 17:57:46 -050010147 /*
10148 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010149 * registers for error attention. This can be changed later.
10150 */
James Smart2fcee4b2010-12-15 17:57:46 -050010151 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10152 switch (if_type) {
10153 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010154 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10155 &uerr_sta_lo) ||
10156 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10157 &uerr_sta_hi)) {
10158 phba->work_hs |= UNPLUG_ERR;
10159 phba->work_ha |= HA_ERATT;
10160 phba->hba_flag |= HBA_ERATT_HANDLED;
10161 return 1;
10162 }
James Smart2fcee4b2010-12-15 17:57:46 -050010163 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10164 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10166 "1423 HBA Unrecoverable error: "
10167 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10168 "ue_mask_lo_reg=0x%x, "
10169 "ue_mask_hi_reg=0x%x\n",
10170 uerr_sta_lo, uerr_sta_hi,
10171 phba->sli4_hba.ue_mask_lo,
10172 phba->sli4_hba.ue_mask_hi);
10173 phba->work_status[0] = uerr_sta_lo;
10174 phba->work_status[1] = uerr_sta_hi;
10175 phba->work_ha |= HA_ERATT;
10176 phba->hba_flag |= HBA_ERATT_HANDLED;
10177 return 1;
10178 }
10179 break;
10180 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010181 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10182 &portstat_reg.word0) ||
10183 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10184 &portsmphr)){
10185 phba->work_hs |= UNPLUG_ERR;
10186 phba->work_ha |= HA_ERATT;
10187 phba->hba_flag |= HBA_ERATT_HANDLED;
10188 return 1;
10189 }
James Smart2fcee4b2010-12-15 17:57:46 -050010190 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10191 phba->work_status[0] =
10192 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10193 phba->work_status[1] =
10194 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010196 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010197 "port status reg 0x%x, "
10198 "port smphr reg 0x%x, "
10199 "error 1=0x%x, error 2=0x%x\n",
10200 portstat_reg.word0,
10201 portsmphr,
10202 phba->work_status[0],
10203 phba->work_status[1]);
10204 phba->work_ha |= HA_ERATT;
10205 phba->hba_flag |= HBA_ERATT_HANDLED;
10206 return 1;
10207 }
10208 break;
10209 case LPFC_SLI_INTF_IF_TYPE_1:
10210 default:
James Smarta747c9c2009-11-18 15:41:10 -050010211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010212 "2886 HBA Error Attention on unsupported "
10213 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010214 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010215 }
James Smart2fcee4b2010-12-15 17:57:46 -050010216
James Smartda0436e2009-05-22 14:51:39 -040010217 return 0;
10218}
10219
10220/**
James Smart3621a712009-04-06 18:47:14 -040010221 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010222 * @phba: Pointer to HBA context.
10223 *
James Smart3772a992009-05-22 14:50:54 -040010224 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010225 * error attention register bit for error attention events.
10226 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010227 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010228 * Register and returns 0 otherwise.
10229 **/
10230int
10231lpfc_sli_check_eratt(struct lpfc_hba *phba)
10232{
10233 uint32_t ha_copy;
10234
10235 /* If somebody is waiting to handle an eratt, don't process it
10236 * here. The brdkill function will do this.
10237 */
10238 if (phba->link_flag & LS_IGNORE_ERATT)
10239 return 0;
10240
10241 /* Check if interrupt handler handles this ERATT */
10242 spin_lock_irq(&phba->hbalock);
10243 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10244 /* Interrupt handler has handled ERATT */
10245 spin_unlock_irq(&phba->hbalock);
10246 return 0;
10247 }
10248
James Smarta257bf92009-04-06 18:48:10 -040010249 /*
10250 * If there is deferred error attention, do not check for error
10251 * attention
10252 */
10253 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10254 spin_unlock_irq(&phba->hbalock);
10255 return 0;
10256 }
10257
James Smart3772a992009-05-22 14:50:54 -040010258 /* If PCI channel is offline, don't process it */
10259 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010260 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010261 return 0;
10262 }
10263
10264 switch (phba->sli_rev) {
10265 case LPFC_SLI_REV2:
10266 case LPFC_SLI_REV3:
10267 /* Read chip Host Attention (HA) register */
10268 ha_copy = lpfc_sli_eratt_read(phba);
10269 break;
James Smartda0436e2009-05-22 14:51:39 -040010270 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010271 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010272 ha_copy = lpfc_sli4_eratt_read(phba);
10273 break;
James Smart3772a992009-05-22 14:50:54 -040010274 default:
10275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10276 "0299 Invalid SLI revision (%d)\n",
10277 phba->sli_rev);
10278 ha_copy = 0;
10279 break;
James Smart93996272008-08-24 21:50:30 -040010280 }
10281 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010282
10283 return ha_copy;
10284}
10285
10286/**
10287 * lpfc_intr_state_check - Check device state for interrupt handling
10288 * @phba: Pointer to HBA context.
10289 *
10290 * This inline routine checks whether a device or its PCI slot is in a state
10291 * that the interrupt should be handled.
10292 *
10293 * This function returns 0 if the device or the PCI slot is in a state that
10294 * interrupt should be handled, otherwise -EIO.
10295 */
10296static inline int
10297lpfc_intr_state_check(struct lpfc_hba *phba)
10298{
10299 /* If the pci channel is offline, ignore all the interrupts */
10300 if (unlikely(pci_channel_offline(phba->pcidev)))
10301 return -EIO;
10302
10303 /* Update device level interrupt statistics */
10304 phba->sli.slistat.sli_intr++;
10305
10306 /* Ignore all interrupts during initialization. */
10307 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10308 return -EIO;
10309
James Smart93996272008-08-24 21:50:30 -040010310 return 0;
10311}
10312
10313/**
James Smart3772a992009-05-22 14:50:54 -040010314 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010315 * @irq: Interrupt number.
10316 * @dev_id: The device context pointer.
10317 *
James Smart93996272008-08-24 21:50:30 -040010318 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010319 * service routine when device with SLI-3 interface spec is enabled with
10320 * MSI-X multi-message interrupt mode and there are slow-path events in
10321 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10322 * interrupt mode, this function is called as part of the device-level
10323 * interrupt handler. When the PCI slot is in error recovery or the HBA
10324 * is undergoing initialization, the interrupt handler will not process
10325 * the interrupt. The link attention and ELS ring attention events are
10326 * handled by the worker thread. The interrupt handler signals the worker
10327 * thread and returns for these events. This function is called without
10328 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010329 * structures.
10330 *
10331 * This function returns IRQ_HANDLED when interrupt is handled else it
10332 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010333 **/
dea31012005-04-17 16:05:31 -050010334irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010335lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010336{
James Smart2e0fef82007-06-17 19:56:36 -050010337 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010338 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010339 uint32_t work_ha_copy;
10340 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010341 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010342 uint32_t control;
10343
James Smart92d7f7b2007-06-17 19:56:38 -050010344 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010345 struct lpfc_vport *vport;
10346 struct lpfc_nodelist *ndlp;
10347 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010348 LPFC_MBOXQ_t *pmb;
10349 int rc;
10350
dea31012005-04-17 16:05:31 -050010351 /*
10352 * Get the driver's phba structure from the dev_id and
10353 * assume the HBA is not interrupting.
10354 */
James Smart93996272008-08-24 21:50:30 -040010355 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010356
10357 if (unlikely(!phba))
10358 return IRQ_NONE;
10359
dea31012005-04-17 16:05:31 -050010360 /*
James Smart93996272008-08-24 21:50:30 -040010361 * Stuff needs to be attented to when this function is invoked as an
10362 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010363 */
James Smart93996272008-08-24 21:50:30 -040010364 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010365 /* Check device state for handling interrupt */
10366 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010367 return IRQ_NONE;
10368 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010369 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010370 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10371 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010372 /* If somebody is waiting to handle an eratt don't process it
10373 * here. The brdkill function will do this.
10374 */
10375 if (phba->link_flag & LS_IGNORE_ERATT)
10376 ha_copy &= ~HA_ERATT;
10377 /* Check the need for handling ERATT in interrupt handler */
10378 if (ha_copy & HA_ERATT) {
10379 if (phba->hba_flag & HBA_ERATT_HANDLED)
10380 /* ERATT polling has handled ERATT */
10381 ha_copy &= ~HA_ERATT;
10382 else
10383 /* Indicate interrupt handler handles ERATT */
10384 phba->hba_flag |= HBA_ERATT_HANDLED;
10385 }
James Smarta257bf92009-04-06 18:48:10 -040010386
10387 /*
10388 * If there is deferred error attention, do not check for any
10389 * interrupt.
10390 */
10391 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010392 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010393 return IRQ_NONE;
10394 }
10395
James Smart93996272008-08-24 21:50:30 -040010396 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010397 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10398 goto unplug_error;
10399
James Smarta747c9c2009-11-18 15:41:10 -050010400 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10401 HC_LAINT_ENA | HC_ERINT_ENA),
10402 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010403 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10404 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010405 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010406 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010407 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010408 } else
10409 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010410
dea31012005-04-17 16:05:31 -050010411 work_ha_copy = ha_copy & phba->work_ha_mask;
10412
James Smart93996272008-08-24 21:50:30 -040010413 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010414 if (work_ha_copy & HA_LATT) {
10415 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10416 /*
10417 * Turn off Link Attention interrupts
10418 * until CLEAR_LA done
10419 */
James Smart5b75da22008-12-04 22:39:35 -050010420 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010421 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010422 if (lpfc_readl(phba->HCregaddr, &control))
10423 goto unplug_error;
dea31012005-04-17 16:05:31 -050010424 control &= ~HC_LAINT_ENA;
10425 writel(control, phba->HCregaddr);
10426 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010427 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010428 }
10429 else
10430 work_ha_copy &= ~HA_LATT;
10431 }
10432
James Smart93996272008-08-24 21:50:30 -040010433 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010434 /*
10435 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10436 * the only slow ring.
10437 */
10438 status = (work_ha_copy &
10439 (HA_RXMASK << (4*LPFC_ELS_RING)));
10440 status >>= (4*LPFC_ELS_RING);
10441 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010442 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010443 if (lpfc_readl(phba->HCregaddr, &control))
10444 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010445
10446 lpfc_debugfs_slow_ring_trc(phba,
10447 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10448 control, status,
10449 (uint32_t)phba->sli.slistat.sli_intr);
10450
James Smart858c9f62007-06-17 19:56:39 -050010451 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010452 lpfc_debugfs_slow_ring_trc(phba,
10453 "ISR Disable ring:"
10454 "pwork:x%x hawork:x%x wait:x%x",
10455 phba->work_ha, work_ha_copy,
10456 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010457 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010458
James Smart858c9f62007-06-17 19:56:39 -050010459 control &=
10460 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010461 writel(control, phba->HCregaddr);
10462 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010463 }
James Smarta58cbd52007-08-02 11:09:43 -040010464 else {
10465 lpfc_debugfs_slow_ring_trc(phba,
10466 "ISR slow ring: pwork:"
10467 "x%x hawork:x%x wait:x%x",
10468 phba->work_ha, work_ha_copy,
10469 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010470 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010471 }
James Smart5b75da22008-12-04 22:39:35 -050010472 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010473 }
10474 }
James Smart5b75da22008-12-04 22:39:35 -050010475 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010476 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010477 if (lpfc_sli_read_hs(phba))
10478 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010479 /*
10480 * Check if there is a deferred error condition
10481 * is active
10482 */
10483 if ((HS_FFER1 & phba->work_hs) &&
10484 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010485 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10486 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010487 phba->hba_flag |= DEFER_ERATT;
10488 /* Clear all interrupt enable conditions */
10489 writel(0, phba->HCregaddr);
10490 readl(phba->HCregaddr);
10491 }
10492 }
10493
James Smart93996272008-08-24 21:50:30 -040010494 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010495 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010496 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010497 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010498 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010499
10500 /* First check out the status word */
10501 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10502 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010503 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010504 /*
10505 * Stray Mailbox Interrupt, mbxCommand <cmd>
10506 * mbxStatus <status>
10507 */
James Smart09372822008-01-11 01:52:54 -050010508 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010509 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010510 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010511 "Interrupt mbxCommand x%x "
10512 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010513 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010514 pmbox->mbxCommand,
10515 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010516 /* clear mailbox attention bit */
10517 work_ha_copy &= ~HA_MBATT;
10518 } else {
James Smart97eab632008-04-07 10:16:05 -040010519 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010520 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010521 phba->last_completion_time = jiffies;
10522 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010523 if (pmb->mbox_cmpl) {
10524 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10525 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010526 if (pmb->out_ext_byte_len &&
10527 pmb->context2)
10528 lpfc_sli_pcimem_bcopy(
10529 phba->mbox_ext,
10530 pmb->context2,
10531 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010532 }
James Smart09372822008-01-11 01:52:54 -050010533 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10534 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10535
10536 lpfc_debugfs_disc_trc(vport,
10537 LPFC_DISC_TRC_MBOX_VPORT,
10538 "MBOX dflt rpi: : "
10539 "status:x%x rpi:x%x",
10540 (uint32_t)pmbox->mbxStatus,
10541 pmbox->un.varWords[0], 0);
10542
10543 if (!pmbox->mbxStatus) {
10544 mp = (struct lpfc_dmabuf *)
10545 (pmb->context1);
10546 ndlp = (struct lpfc_nodelist *)
10547 pmb->context2;
10548
10549 /* Reg_LOGIN of dflt RPI was
10550 * successful. new lets get
10551 * rid of the RPI using the
10552 * same mbox buffer.
10553 */
10554 lpfc_unreg_login(phba,
10555 vport->vpi,
10556 pmbox->un.varWords[0],
10557 pmb);
10558 pmb->mbox_cmpl =
10559 lpfc_mbx_cmpl_dflt_rpi;
10560 pmb->context1 = mp;
10561 pmb->context2 = ndlp;
10562 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010563 rc = lpfc_sli_issue_mbox(phba,
10564 pmb,
10565 MBX_NOWAIT);
10566 if (rc != MBX_BUSY)
10567 lpfc_printf_log(phba,
10568 KERN_ERR,
10569 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010570 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010571 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010572 if (rc != MBX_NOT_FINISHED)
10573 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010574 }
10575 }
James Smart5b75da22008-12-04 22:39:35 -050010576 spin_lock_irqsave(
10577 &phba->pport->work_port_lock,
10578 iflag);
James Smart09372822008-01-11 01:52:54 -050010579 phba->pport->work_port_events &=
10580 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010581 spin_unlock_irqrestore(
10582 &phba->pport->work_port_lock,
10583 iflag);
James Smart09372822008-01-11 01:52:54 -050010584 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010585 }
James Smart97eab632008-04-07 10:16:05 -040010586 } else
James Smart5b75da22008-12-04 22:39:35 -050010587 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010588
James Smart92d7f7b2007-06-17 19:56:38 -050010589 if ((work_ha_copy & HA_MBATT) &&
10590 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010591send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010592 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010593 do {
10594 rc = lpfc_sli_issue_mbox(phba, NULL,
10595 MBX_NOWAIT);
10596 } while (rc == MBX_NOT_FINISHED);
10597 if (rc != MBX_SUCCESS)
10598 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10599 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010600 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010601 }
10602
James Smart5b75da22008-12-04 22:39:35 -050010603 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010604 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010605 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010606 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010607 }
James Smart93996272008-08-24 21:50:30 -040010608 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010609unplug_error:
10610 spin_unlock_irqrestore(&phba->hbalock, iflag);
10611 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010612
James Smart3772a992009-05-22 14:50:54 -040010613} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010614
10615/**
James Smart3772a992009-05-22 14:50:54 -040010616 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010617 * @irq: Interrupt number.
10618 * @dev_id: The device context pointer.
10619 *
10620 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010621 * service routine when device with SLI-3 interface spec is enabled with
10622 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10623 * ring event in the HBA. However, when the device is enabled with either
10624 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10625 * device-level interrupt handler. When the PCI slot is in error recovery
10626 * or the HBA is undergoing initialization, the interrupt handler will not
10627 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10628 * the intrrupt context. This function is called without any lock held.
10629 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010630 *
10631 * This function returns IRQ_HANDLED when interrupt is handled else it
10632 * returns IRQ_NONE.
10633 **/
10634irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010635lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010636{
10637 struct lpfc_hba *phba;
10638 uint32_t ha_copy;
10639 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010640 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010641
10642 /* Get the driver's phba structure from the dev_id and
10643 * assume the HBA is not interrupting.
10644 */
10645 phba = (struct lpfc_hba *) dev_id;
10646
10647 if (unlikely(!phba))
10648 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010649
10650 /*
James Smart93996272008-08-24 21:50:30 -040010651 * Stuff needs to be attented to when this function is invoked as an
10652 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010653 */
James Smart93996272008-08-24 21:50:30 -040010654 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010655 /* Check device state for handling interrupt */
10656 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010657 return IRQ_NONE;
10658 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010659 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10660 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010661 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010662 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010663 /*
10664 * If there is deferred error attention, do not check for
10665 * any interrupt.
10666 */
10667 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010668 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010669 return IRQ_NONE;
10670 }
James Smart93996272008-08-24 21:50:30 -040010671 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10672 phba->HAregaddr);
10673 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010674 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010675 } else
10676 ha_copy = phba->ha_copy;
10677
10678 /*
10679 * Process all events on FCP ring. Take the optimized path for FCP IO.
10680 */
10681 ha_copy &= ~(phba->work_ha_mask);
10682
10683 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010684 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010685 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010686 lpfc_sli_handle_fast_ring_event(phba,
10687 &phba->sli.ring[LPFC_FCP_RING],
10688 status);
James Smarta4bc3372006-12-02 13:34:16 -050010689
10690 if (phba->cfg_multi_ring_support == 2) {
10691 /*
James Smart93996272008-08-24 21:50:30 -040010692 * Process all events on extra ring. Take the optimized path
10693 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010694 */
James Smart93996272008-08-24 21:50:30 -040010695 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010696 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010697 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010698 lpfc_sli_handle_fast_ring_event(phba,
10699 &phba->sli.ring[LPFC_EXTRA_RING],
10700 status);
10701 }
10702 }
dea31012005-04-17 16:05:31 -050010703 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010704} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010705
James Smart93996272008-08-24 21:50:30 -040010706/**
James Smart3772a992009-05-22 14:50:54 -040010707 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010708 * @irq: Interrupt number.
10709 * @dev_id: The device context pointer.
10710 *
James Smart3772a992009-05-22 14:50:54 -040010711 * This function is the HBA device-level interrupt handler to device with
10712 * SLI-3 interface spec, called from the PCI layer when either MSI or
10713 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10714 * requires driver attention. This function invokes the slow-path interrupt
10715 * attention handling function and fast-path interrupt attention handling
10716 * function in turn to process the relevant HBA attention events. This
10717 * function is called without any lock held. It gets the hbalock to access
10718 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010719 *
10720 * This function returns IRQ_HANDLED when interrupt is handled, else it
10721 * returns IRQ_NONE.
10722 **/
10723irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010724lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010725{
10726 struct lpfc_hba *phba;
10727 irqreturn_t sp_irq_rc, fp_irq_rc;
10728 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010729 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010730
10731 /*
10732 * Get the driver's phba structure from the dev_id and
10733 * assume the HBA is not interrupting.
10734 */
10735 phba = (struct lpfc_hba *) dev_id;
10736
10737 if (unlikely(!phba))
10738 return IRQ_NONE;
10739
James Smart3772a992009-05-22 14:50:54 -040010740 /* Check device state for handling interrupt */
10741 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010742 return IRQ_NONE;
10743
10744 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010745 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10746 spin_unlock(&phba->hbalock);
10747 return IRQ_HANDLED;
10748 }
10749
James Smart93996272008-08-24 21:50:30 -040010750 if (unlikely(!phba->ha_copy)) {
10751 spin_unlock(&phba->hbalock);
10752 return IRQ_NONE;
10753 } else if (phba->ha_copy & HA_ERATT) {
10754 if (phba->hba_flag & HBA_ERATT_HANDLED)
10755 /* ERATT polling has handled ERATT */
10756 phba->ha_copy &= ~HA_ERATT;
10757 else
10758 /* Indicate interrupt handler handles ERATT */
10759 phba->hba_flag |= HBA_ERATT_HANDLED;
10760 }
10761
James Smarta257bf92009-04-06 18:48:10 -040010762 /*
10763 * If there is deferred error attention, do not check for any interrupt.
10764 */
10765 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010766 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010767 return IRQ_NONE;
10768 }
10769
James Smart93996272008-08-24 21:50:30 -040010770 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010771 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10772 spin_unlock(&phba->hbalock);
10773 return IRQ_HANDLED;
10774 }
James Smarta747c9c2009-11-18 15:41:10 -050010775 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10776 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10777 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010778 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010779 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010780 readl(phba->HAregaddr); /* flush */
10781 spin_unlock(&phba->hbalock);
10782
10783 /*
10784 * Invokes slow-path host attention interrupt handling as appropriate.
10785 */
10786
10787 /* status of events with mailbox and link attention */
10788 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10789
10790 /* status of events with ELS ring */
10791 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10792 status2 >>= (4*LPFC_ELS_RING);
10793
10794 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010795 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010796 else
10797 sp_irq_rc = IRQ_NONE;
10798
10799 /*
10800 * Invoke fast-path host attention interrupt handling as appropriate.
10801 */
10802
10803 /* status of events with FCP ring */
10804 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10805 status1 >>= (4*LPFC_FCP_RING);
10806
10807 /* status of events with extra ring */
10808 if (phba->cfg_multi_ring_support == 2) {
10809 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10810 status2 >>= (4*LPFC_EXTRA_RING);
10811 } else
10812 status2 = 0;
10813
10814 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010815 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010816 else
10817 fp_irq_rc = IRQ_NONE;
10818
10819 /* Return device-level interrupt handling status */
10820 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010821} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010822
10823/**
10824 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10825 * @phba: pointer to lpfc hba data structure.
10826 *
10827 * This routine is invoked by the worker thread to process all the pending
10828 * SLI4 FCP abort XRI events.
10829 **/
10830void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10831{
10832 struct lpfc_cq_event *cq_event;
10833
10834 /* First, declare the fcp xri abort event has been handled */
10835 spin_lock_irq(&phba->hbalock);
10836 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10837 spin_unlock_irq(&phba->hbalock);
10838 /* Now, handle all the fcp xri abort events */
10839 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10840 /* Get the first event from the head of the event queue */
10841 spin_lock_irq(&phba->hbalock);
10842 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10843 cq_event, struct lpfc_cq_event, list);
10844 spin_unlock_irq(&phba->hbalock);
10845 /* Notify aborted XRI for FCP work queue */
10846 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10847 /* Free the event processed back to the free pool */
10848 lpfc_sli4_cq_event_release(phba, cq_event);
10849 }
10850}
10851
10852/**
10853 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10854 * @phba: pointer to lpfc hba data structure.
10855 *
10856 * This routine is invoked by the worker thread to process all the pending
10857 * SLI4 els abort xri events.
10858 **/
10859void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10860{
10861 struct lpfc_cq_event *cq_event;
10862
10863 /* First, declare the els xri abort event has been handled */
10864 spin_lock_irq(&phba->hbalock);
10865 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10866 spin_unlock_irq(&phba->hbalock);
10867 /* Now, handle all the els xri abort events */
10868 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10869 /* Get the first event from the head of the event queue */
10870 spin_lock_irq(&phba->hbalock);
10871 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10872 cq_event, struct lpfc_cq_event, list);
10873 spin_unlock_irq(&phba->hbalock);
10874 /* Notify aborted XRI for ELS work queue */
10875 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10876 /* Free the event processed back to the free pool */
10877 lpfc_sli4_cq_event_release(phba, cq_event);
10878 }
10879}
10880
James Smart341af102010-01-26 23:07:37 -050010881/**
10882 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10883 * @phba: pointer to lpfc hba data structure
10884 * @pIocbIn: pointer to the rspiocbq
10885 * @pIocbOut: pointer to the cmdiocbq
10886 * @wcqe: pointer to the complete wcqe
10887 *
10888 * This routine transfers the fields of a command iocbq to a response iocbq
10889 * by copying all the IOCB fields from command iocbq and transferring the
10890 * completion status information from the complete wcqe.
10891 **/
James Smart4f774512009-05-22 14:52:35 -040010892static void
James Smart341af102010-01-26 23:07:37 -050010893lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10894 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010895 struct lpfc_iocbq *pIocbOut,
10896 struct lpfc_wcqe_complete *wcqe)
10897{
James Smart341af102010-01-26 23:07:37 -050010898 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010899 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010900 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10901
10902 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10903 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010904 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010905 status = bf_get(lpfc_wcqe_c_status, wcqe);
10906 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010907 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10908 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10909 pIocbIn->iocb.un.fcpi.fcpi_parm =
10910 pIocbOut->iocb.un.fcpi.fcpi_parm -
10911 wcqe->total_data_placed;
10912 else
10913 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010914 else {
James Smart4f774512009-05-22 14:52:35 -040010915 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010916 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10917 }
James Smart341af102010-01-26 23:07:37 -050010918
James Smartacd68592012-01-18 16:25:09 -050010919 /* Convert BG errors for completion status */
10920 if (status == CQE_STATUS_DI_ERROR) {
10921 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10922
10923 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10924 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10925 else
10926 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10927
10928 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10929 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10930 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10931 BGS_GUARD_ERR_MASK;
10932 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10933 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10934 BGS_APPTAG_ERR_MASK;
10935 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10936 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10937 BGS_REFTAG_ERR_MASK;
10938
10939 /* Check to see if there was any good data before the error */
10940 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10941 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10942 BGS_HI_WATER_MARK_PRESENT_MASK;
10943 pIocbIn->iocb.unsli3.sli3_bg.bghm =
10944 wcqe->total_data_placed;
10945 }
10946
10947 /*
10948 * Set ALL the error bits to indicate we don't know what
10949 * type of error it is.
10950 */
10951 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10952 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10953 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10954 BGS_GUARD_ERR_MASK);
10955 }
10956
James Smart341af102010-01-26 23:07:37 -050010957 /* Pick up HBA exchange busy condition */
10958 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10959 spin_lock_irqsave(&phba->hbalock, iflags);
10960 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10961 spin_unlock_irqrestore(&phba->hbalock, iflags);
10962 }
James Smart4f774512009-05-22 14:52:35 -040010963}
10964
10965/**
James Smart45ed1192009-10-02 15:17:02 -040010966 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10967 * @phba: Pointer to HBA context object.
10968 * @wcqe: Pointer to work-queue completion queue entry.
10969 *
10970 * This routine handles an ELS work-queue completion event and construct
10971 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10972 * discovery engine to handle.
10973 *
10974 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10975 **/
10976static struct lpfc_iocbq *
10977lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10978 struct lpfc_iocbq *irspiocbq)
10979{
10980 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10981 struct lpfc_iocbq *cmdiocbq;
10982 struct lpfc_wcqe_complete *wcqe;
10983 unsigned long iflags;
10984
10985 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040010986 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040010987 pring->stats.iocb_event++;
10988 /* Look up the ELS command IOCB and create pseudo response IOCB */
10989 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10990 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040010991 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040010992
10993 if (unlikely(!cmdiocbq)) {
10994 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10995 "0386 ELS complete with no corresponding "
10996 "cmdiocb: iotag (%d)\n",
10997 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10998 lpfc_sli_release_iocbq(phba, irspiocbq);
10999 return NULL;
11000 }
11001
11002 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011003 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011004
11005 return irspiocbq;
11006}
11007
11008/**
James Smart04c68492009-05-22 14:52:52 -040011009 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11010 * @phba: Pointer to HBA context object.
11011 * @cqe: Pointer to mailbox completion queue entry.
11012 *
11013 * This routine process a mailbox completion queue entry with asynchrous
11014 * event.
11015 *
11016 * Return: true if work posted to worker thread, otherwise false.
11017 **/
11018static bool
11019lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11020{
11021 struct lpfc_cq_event *cq_event;
11022 unsigned long iflags;
11023
11024 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11025 "0392 Async Event: word0:x%x, word1:x%x, "
11026 "word2:x%x, word3:x%x\n", mcqe->word0,
11027 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11028
11029 /* Allocate a new internal CQ_EVENT entry */
11030 cq_event = lpfc_sli4_cq_event_alloc(phba);
11031 if (!cq_event) {
11032 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11033 "0394 Failed to allocate CQ_EVENT entry\n");
11034 return false;
11035 }
11036
11037 /* Move the CQE into an asynchronous event entry */
11038 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11039 spin_lock_irqsave(&phba->hbalock, iflags);
11040 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11041 /* Set the async event flag */
11042 phba->hba_flag |= ASYNC_EVENT;
11043 spin_unlock_irqrestore(&phba->hbalock, iflags);
11044
11045 return true;
11046}
11047
11048/**
11049 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11050 * @phba: Pointer to HBA context object.
11051 * @cqe: Pointer to mailbox completion queue entry.
11052 *
11053 * This routine process a mailbox completion queue entry with mailbox
11054 * completion event.
11055 *
11056 * Return: true if work posted to worker thread, otherwise false.
11057 **/
11058static bool
11059lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11060{
11061 uint32_t mcqe_status;
11062 MAILBOX_t *mbox, *pmbox;
11063 struct lpfc_mqe *mqe;
11064 struct lpfc_vport *vport;
11065 struct lpfc_nodelist *ndlp;
11066 struct lpfc_dmabuf *mp;
11067 unsigned long iflags;
11068 LPFC_MBOXQ_t *pmb;
11069 bool workposted = false;
11070 int rc;
11071
11072 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11073 if (!bf_get(lpfc_trailer_completed, mcqe))
11074 goto out_no_mqe_complete;
11075
11076 /* Get the reference to the active mbox command */
11077 spin_lock_irqsave(&phba->hbalock, iflags);
11078 pmb = phba->sli.mbox_active;
11079 if (unlikely(!pmb)) {
11080 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11081 "1832 No pending MBOX command to handle\n");
11082 spin_unlock_irqrestore(&phba->hbalock, iflags);
11083 goto out_no_mqe_complete;
11084 }
11085 spin_unlock_irqrestore(&phba->hbalock, iflags);
11086 mqe = &pmb->u.mqe;
11087 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11088 mbox = phba->mbox;
11089 vport = pmb->vport;
11090
11091 /* Reset heartbeat timer */
11092 phba->last_completion_time = jiffies;
11093 del_timer(&phba->sli.mbox_tmo);
11094
11095 /* Move mbox data to caller's mailbox region, do endian swapping */
11096 if (pmb->mbox_cmpl && mbox)
11097 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011098
James Smart73d91e52011-10-10 21:32:10 -040011099 /*
11100 * For mcqe errors, conditionally move a modified error code to
11101 * the mbox so that the error will not be missed.
11102 */
11103 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11104 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11105 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11106 bf_set(lpfc_mqe_status, mqe,
11107 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11108 }
James Smart04c68492009-05-22 14:52:52 -040011109 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11110 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11111 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11112 "MBOX dflt rpi: status:x%x rpi:x%x",
11113 mcqe_status,
11114 pmbox->un.varWords[0], 0);
11115 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11116 mp = (struct lpfc_dmabuf *)(pmb->context1);
11117 ndlp = (struct lpfc_nodelist *)pmb->context2;
11118 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11119 * RID of the PPI using the same mbox buffer.
11120 */
11121 lpfc_unreg_login(phba, vport->vpi,
11122 pmbox->un.varWords[0], pmb);
11123 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11124 pmb->context1 = mp;
11125 pmb->context2 = ndlp;
11126 pmb->vport = vport;
11127 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11128 if (rc != MBX_BUSY)
11129 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11130 LOG_SLI, "0385 rc should "
11131 "have been MBX_BUSY\n");
11132 if (rc != MBX_NOT_FINISHED)
11133 goto send_current_mbox;
11134 }
11135 }
11136 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11137 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11138 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11139
11140 /* There is mailbox completion work to do */
11141 spin_lock_irqsave(&phba->hbalock, iflags);
11142 __lpfc_mbox_cmpl_put(phba, pmb);
11143 phba->work_ha |= HA_MBATT;
11144 spin_unlock_irqrestore(&phba->hbalock, iflags);
11145 workposted = true;
11146
11147send_current_mbox:
11148 spin_lock_irqsave(&phba->hbalock, iflags);
11149 /* Release the mailbox command posting token */
11150 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11151 /* Setting active mailbox pointer need to be in sync to flag clear */
11152 phba->sli.mbox_active = NULL;
11153 spin_unlock_irqrestore(&phba->hbalock, iflags);
11154 /* Wake up worker thread to post the next pending mailbox command */
11155 lpfc_worker_wake_up(phba);
11156out_no_mqe_complete:
11157 if (bf_get(lpfc_trailer_consumed, mcqe))
11158 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11159 return workposted;
11160}
11161
11162/**
11163 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11164 * @phba: Pointer to HBA context object.
11165 * @cqe: Pointer to mailbox completion queue entry.
11166 *
11167 * This routine process a mailbox completion queue entry, it invokes the
11168 * proper mailbox complete handling or asynchrous event handling routine
11169 * according to the MCQE's async bit.
11170 *
11171 * Return: true if work posted to worker thread, otherwise false.
11172 **/
11173static bool
11174lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11175{
11176 struct lpfc_mcqe mcqe;
11177 bool workposted;
11178
11179 /* Copy the mailbox MCQE and convert endian order as needed */
11180 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11181
11182 /* Invoke the proper event handling routine */
11183 if (!bf_get(lpfc_trailer_async, &mcqe))
11184 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11185 else
11186 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11187 return workposted;
11188}
11189
11190/**
James Smart4f774512009-05-22 14:52:35 -040011191 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11192 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011193 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011194 * @wcqe: Pointer to work-queue completion queue entry.
11195 *
11196 * This routine handles an ELS work-queue completion event.
11197 *
11198 * Return: true if work posted to worker thread, otherwise false.
11199 **/
11200static bool
James Smart2a76a282012-08-03 12:35:54 -040011201lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011202 struct lpfc_wcqe_complete *wcqe)
11203{
James Smart4f774512009-05-22 14:52:35 -040011204 struct lpfc_iocbq *irspiocbq;
11205 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011206 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011207
James Smart45ed1192009-10-02 15:17:02 -040011208 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011209 irspiocbq = lpfc_sli_get_iocbq(phba);
11210 if (!irspiocbq) {
11211 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011212 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11213 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11214 pring->txq_cnt, phba->iocb_cnt,
11215 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11216 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011217 return false;
James Smart4f774512009-05-22 14:52:35 -040011218 }
James Smart4f774512009-05-22 14:52:35 -040011219
James Smart45ed1192009-10-02 15:17:02 -040011220 /* Save off the slow-path queue event for work thread to process */
11221 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011222 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011223 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011224 &phba->sli4_hba.sp_queue_event);
11225 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011226 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011227
James Smart45ed1192009-10-02 15:17:02 -040011228 return true;
James Smart4f774512009-05-22 14:52:35 -040011229}
11230
11231/**
11232 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11233 * @phba: Pointer to HBA context object.
11234 * @wcqe: Pointer to work-queue completion queue entry.
11235 *
11236 * This routine handles slow-path WQ entry comsumed event by invoking the
11237 * proper WQ release routine to the slow-path WQ.
11238 **/
11239static void
11240lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11241 struct lpfc_wcqe_release *wcqe)
11242{
James Smart2e90f4b2011-12-13 13:22:37 -050011243 /* sanity check on queue memory */
11244 if (unlikely(!phba->sli4_hba.els_wq))
11245 return;
James Smart4f774512009-05-22 14:52:35 -040011246 /* Check for the slow-path ELS work queue */
11247 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11248 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11249 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11250 else
11251 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11252 "2579 Slow-path wqe consume event carries "
11253 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11254 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11255 phba->sli4_hba.els_wq->queue_id);
11256}
11257
11258/**
11259 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11260 * @phba: Pointer to HBA context object.
11261 * @cq: Pointer to a WQ completion queue.
11262 * @wcqe: Pointer to work-queue completion queue entry.
11263 *
11264 * This routine handles an XRI abort event.
11265 *
11266 * Return: true if work posted to worker thread, otherwise false.
11267 **/
11268static bool
11269lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11270 struct lpfc_queue *cq,
11271 struct sli4_wcqe_xri_aborted *wcqe)
11272{
11273 bool workposted = false;
11274 struct lpfc_cq_event *cq_event;
11275 unsigned long iflags;
11276
11277 /* Allocate a new internal CQ_EVENT entry */
11278 cq_event = lpfc_sli4_cq_event_alloc(phba);
11279 if (!cq_event) {
11280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11281 "0602 Failed to allocate CQ_EVENT entry\n");
11282 return false;
11283 }
11284
11285 /* Move the CQE into the proper xri abort event list */
11286 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11287 switch (cq->subtype) {
11288 case LPFC_FCP:
11289 spin_lock_irqsave(&phba->hbalock, iflags);
11290 list_add_tail(&cq_event->list,
11291 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11292 /* Set the fcp xri abort event flag */
11293 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11294 spin_unlock_irqrestore(&phba->hbalock, iflags);
11295 workposted = true;
11296 break;
11297 case LPFC_ELS:
11298 spin_lock_irqsave(&phba->hbalock, iflags);
11299 list_add_tail(&cq_event->list,
11300 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11301 /* Set the els xri abort event flag */
11302 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11303 spin_unlock_irqrestore(&phba->hbalock, iflags);
11304 workposted = true;
11305 break;
11306 default:
11307 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11308 "0603 Invalid work queue CQE subtype (x%x)\n",
11309 cq->subtype);
11310 workposted = false;
11311 break;
11312 }
11313 return workposted;
11314}
11315
11316/**
James Smart4d9ab992009-10-02 15:16:39 -040011317 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011318 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011319 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011320 *
James Smart4d9ab992009-10-02 15:16:39 -040011321 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011322 *
11323 * Return: true if work posted to worker thread, otherwise false.
11324 **/
11325static bool
James Smart4d9ab992009-10-02 15:16:39 -040011326lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11327{
11328 bool workposted = false;
11329 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11330 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11331 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011332 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011333 unsigned long iflags;
11334
James Smart2e90f4b2011-12-13 13:22:37 -050011335 /* sanity check on queue memory */
11336 if (unlikely(!hrq) || unlikely(!drq))
11337 return workposted;
11338
James Smart7851fe22011-07-22 18:36:52 -040011339 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11340 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11341 else
11342 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11343 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011344 goto out;
11345
11346 status = bf_get(lpfc_rcqe_status, rcqe);
11347 switch (status) {
11348 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11349 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11350 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011351 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011352 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011353 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011354 spin_lock_irqsave(&phba->hbalock, iflags);
11355 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11356 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011357 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011358 spin_unlock_irqrestore(&phba->hbalock, iflags);
11359 goto out;
11360 }
James Smartb84daac2012-08-03 12:35:13 -040011361 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011362 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11363 /* save off the frame for the word thread to process */
11364 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011365 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011366 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011367 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011368 spin_unlock_irqrestore(&phba->hbalock, iflags);
11369 workposted = true;
11370 break;
11371 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11372 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011373 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011374 /* Post more buffers if possible */
11375 spin_lock_irqsave(&phba->hbalock, iflags);
11376 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11377 spin_unlock_irqrestore(&phba->hbalock, iflags);
11378 workposted = true;
11379 break;
11380 }
11381out:
11382 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011383}
11384
11385/**
11386 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11387 * @phba: Pointer to HBA context object.
11388 * @cq: Pointer to the completion queue.
11389 * @wcqe: Pointer to a completion queue entry.
11390 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011391 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011392 * entry.
11393 *
11394 * Return: true if work posted to worker thread, otherwise false.
11395 **/
11396static bool
11397lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011398 struct lpfc_cqe *cqe)
11399{
James Smart45ed1192009-10-02 15:17:02 -040011400 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011401 bool workposted = false;
11402
11403 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011404 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011405
11406 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011407 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011408 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011409 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011410 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011411 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011412 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011413 break;
11414 case CQE_CODE_RELEASE_WQE:
11415 /* Process the WQ release event */
11416 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011417 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011418 break;
11419 case CQE_CODE_XRI_ABORTED:
11420 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011421 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011422 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011423 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011424 break;
James Smart4d9ab992009-10-02 15:16:39 -040011425 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011426 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011427 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011428 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011429 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011430 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011431 break;
James Smart4f774512009-05-22 14:52:35 -040011432 default:
11433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11434 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011435 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011436 break;
11437 }
11438 return workposted;
11439}
11440
11441/**
James Smart4f774512009-05-22 14:52:35 -040011442 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11443 * @phba: Pointer to HBA context object.
11444 * @eqe: Pointer to fast-path event queue entry.
11445 *
11446 * This routine process a event queue entry from the slow-path event queue.
11447 * It will check the MajorCode and MinorCode to determine this is for a
11448 * completion event on a completion queue, if not, an error shall be logged
11449 * and just return. Otherwise, it will get to the corresponding completion
11450 * queue and process all the entries on that completion queue, rearm the
11451 * completion queue, and then return.
11452 *
11453 **/
11454static void
11455lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11456{
11457 struct lpfc_queue *cq = NULL, *childq, *speq;
11458 struct lpfc_cqe *cqe;
11459 bool workposted = false;
11460 int ecount = 0;
11461 uint16_t cqid;
11462
James Smartcb5172e2010-03-15 11:25:07 -040011463 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011464 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11465 "0359 Not a valid slow-path completion "
11466 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011467 bf_get_le32(lpfc_eqe_major_code, eqe),
11468 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011469 return;
11470 }
11471
11472 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011473 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011474
11475 /* Search for completion queue pointer matching this cqid */
11476 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011477 /* sanity check on queue memory */
11478 if (unlikely(!speq))
11479 return;
James Smart4f774512009-05-22 14:52:35 -040011480 list_for_each_entry(childq, &speq->child_list, list) {
11481 if (childq->queue_id == cqid) {
11482 cq = childq;
11483 break;
11484 }
11485 }
11486 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011487 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11488 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11489 "0365 Slow-path CQ identifier "
11490 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011491 return;
11492 }
11493
11494 /* Process all the entries to the CQ */
11495 switch (cq->type) {
11496 case LPFC_MCQ:
11497 while ((cqe = lpfc_sli4_cq_get(cq))) {
11498 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011499 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011500 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011501 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011502 }
11503 break;
11504 case LPFC_WCQ:
11505 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011506 if (cq->subtype == LPFC_FCP)
11507 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11508 cqe);
11509 else
11510 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11511 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011512 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011513 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11514 }
James Smartb84daac2012-08-03 12:35:13 -040011515
11516 /* Track the max number of CQEs processed in 1 EQ */
11517 if (ecount > cq->CQ_max_cqe)
11518 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011519 break;
11520 default:
11521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11522 "0370 Invalid completion queue type (%d)\n",
11523 cq->type);
11524 return;
11525 }
11526
11527 /* Catch the no cq entry condition, log an error */
11528 if (unlikely(ecount == 0))
11529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11530 "0371 No entry from the CQ: identifier "
11531 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11532
11533 /* In any case, flash and re-arm the RCQ */
11534 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11535
11536 /* wake up worker thread if there are works to be done */
11537 if (workposted)
11538 lpfc_worker_wake_up(phba);
11539}
11540
11541/**
11542 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011543 * @phba: Pointer to HBA context object.
11544 * @cq: Pointer to associated CQ
11545 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011546 *
11547 * This routine process a fast-path work queue completion entry from fast-path
11548 * event queue for FCP command response completion.
11549 **/
11550static void
James Smart2a76a282012-08-03 12:35:54 -040011551lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011552 struct lpfc_wcqe_complete *wcqe)
11553{
James Smart2a76a282012-08-03 12:35:54 -040011554 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011555 struct lpfc_iocbq *cmdiocbq;
11556 struct lpfc_iocbq irspiocbq;
11557 unsigned long iflags;
11558
James Smart4f774512009-05-22 14:52:35 -040011559 /* Check for response status */
11560 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11561 /* If resource errors reported from HBA, reduce queue
11562 * depth of the SCSI device.
11563 */
11564 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11565 IOSTAT_LOCAL_REJECT) &&
11566 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11567 phba->lpfc_rampdown_queue_depth(phba);
11568 }
11569 /* Log the error status */
11570 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11571 "0373 FCP complete error: status=x%x, "
11572 "hw_status=x%x, total_data_specified=%d, "
11573 "parameter=x%x, word3=x%x\n",
11574 bf_get(lpfc_wcqe_c_status, wcqe),
11575 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11576 wcqe->total_data_placed, wcqe->parameter,
11577 wcqe->word3);
11578 }
11579
11580 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011581 spin_lock_irqsave(&pring->ring_lock, iflags);
11582 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011583 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11584 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011585 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011586 if (unlikely(!cmdiocbq)) {
11587 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11588 "0374 FCP complete with no corresponding "
11589 "cmdiocb: iotag (%d)\n",
11590 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11591 return;
11592 }
11593 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11594 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11595 "0375 FCP cmdiocb not callback function "
11596 "iotag: (%d)\n",
11597 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11598 return;
11599 }
11600
11601 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011602 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011603
James Smart0f65ff62010-02-26 14:14:23 -050011604 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11605 spin_lock_irqsave(&phba->hbalock, iflags);
11606 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11607 spin_unlock_irqrestore(&phba->hbalock, iflags);
11608 }
11609
James Smart4f774512009-05-22 14:52:35 -040011610 /* Pass the cmd_iocb and the rsp state to the upper layer */
11611 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11612}
11613
11614/**
11615 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11616 * @phba: Pointer to HBA context object.
11617 * @cq: Pointer to completion queue.
11618 * @wcqe: Pointer to work-queue completion queue entry.
11619 *
11620 * This routine handles an fast-path WQ entry comsumed event by invoking the
11621 * proper WQ release routine to the slow-path WQ.
11622 **/
11623static void
11624lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11625 struct lpfc_wcqe_release *wcqe)
11626{
11627 struct lpfc_queue *childwq;
11628 bool wqid_matched = false;
11629 uint16_t fcp_wqid;
11630
11631 /* Check for fast-path FCP work queue release */
11632 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11633 list_for_each_entry(childwq, &cq->child_list, list) {
11634 if (childwq->queue_id == fcp_wqid) {
11635 lpfc_sli4_wq_release(childwq,
11636 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11637 wqid_matched = true;
11638 break;
11639 }
11640 }
11641 /* Report warning log message if no match found */
11642 if (wqid_matched != true)
11643 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11644 "2580 Fast-path wqe consume event carries "
11645 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11646}
11647
11648/**
11649 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11650 * @cq: Pointer to the completion queue.
11651 * @eqe: Pointer to fast-path completion queue entry.
11652 *
11653 * This routine process a fast-path work queue completion entry from fast-path
11654 * event queue for FCP command response completion.
11655 **/
11656static int
11657lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11658 struct lpfc_cqe *cqe)
11659{
11660 struct lpfc_wcqe_release wcqe;
11661 bool workposted = false;
11662
11663 /* Copy the work queue CQE and convert endian order if needed */
11664 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11665
11666 /* Check and process for different type of WCQE and dispatch */
11667 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11668 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011669 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011670 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011671 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011672 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040011673 (struct lpfc_wcqe_complete *)&wcqe);
11674 break;
11675 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011676 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011677 /* Process the WQ release event */
11678 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11679 (struct lpfc_wcqe_release *)&wcqe);
11680 break;
11681 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011682 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011683 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011684 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011685 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11686 (struct sli4_wcqe_xri_aborted *)&wcqe);
11687 break;
11688 default:
11689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11690 "0144 Not a valid WCQE code: x%x\n",
11691 bf_get(lpfc_wcqe_c_code, &wcqe));
11692 break;
11693 }
11694 return workposted;
11695}
11696
11697/**
11698 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11699 * @phba: Pointer to HBA context object.
11700 * @eqe: Pointer to fast-path event queue entry.
11701 *
11702 * This routine process a event queue entry from the fast-path event queue.
11703 * It will check the MajorCode and MinorCode to determine this is for a
11704 * completion event on a completion queue, if not, an error shall be logged
11705 * and just return. Otherwise, it will get to the corresponding completion
11706 * queue and process all the entries on the completion queue, rearm the
11707 * completion queue, and then return.
11708 **/
11709static void
11710lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11711 uint32_t fcp_cqidx)
11712{
11713 struct lpfc_queue *cq;
11714 struct lpfc_cqe *cqe;
11715 bool workposted = false;
11716 uint16_t cqid;
11717 int ecount = 0;
11718
James Smartcb5172e2010-03-15 11:25:07 -040011719 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11721 "0366 Not a valid fast-path completion "
11722 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011723 bf_get_le32(lpfc_eqe_major_code, eqe),
11724 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011725 return;
11726 }
11727
James Smart2e90f4b2011-12-13 13:22:37 -050011728 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11729 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11730 "3146 Fast-path completion queues "
11731 "does not exist\n");
11732 return;
11733 }
James Smart4f774512009-05-22 14:52:35 -040011734 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11735 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011736 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11737 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11738 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011739 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011740 return;
11741 }
11742
11743 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011744 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011745 if (unlikely(cqid != cq->queue_id)) {
11746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11747 "0368 Miss-matched fast-path completion "
11748 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11749 cqid, cq->queue_id);
11750 return;
11751 }
11752
11753 /* Process all the entries to the CQ */
11754 while ((cqe = lpfc_sli4_cq_get(cq))) {
11755 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011756 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011757 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11758 }
11759
James Smartb84daac2012-08-03 12:35:13 -040011760 /* Track the max number of CQEs processed in 1 EQ */
11761 if (ecount > cq->CQ_max_cqe)
11762 cq->CQ_max_cqe = ecount;
11763
James Smart4f774512009-05-22 14:52:35 -040011764 /* Catch the no cq entry condition */
11765 if (unlikely(ecount == 0))
11766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11767 "0369 No entry from fast-path completion "
11768 "queue fcpcqid=%d\n", cq->queue_id);
11769
11770 /* In any case, flash and re-arm the CQ */
11771 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11772
11773 /* wake up worker thread if there are works to be done */
11774 if (workposted)
11775 lpfc_worker_wake_up(phba);
11776}
11777
11778static void
11779lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11780{
11781 struct lpfc_eqe *eqe;
11782
11783 /* walk all the EQ entries and drop on the floor */
11784 while ((eqe = lpfc_sli4_eq_get(eq)))
11785 ;
11786
11787 /* Clear and re-arm the EQ */
11788 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11789}
11790
11791/**
11792 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11793 * @irq: Interrupt number.
11794 * @dev_id: The device context pointer.
11795 *
11796 * This function is directly called from the PCI layer as an interrupt
11797 * service routine when device with SLI-4 interface spec is enabled with
11798 * MSI-X multi-message interrupt mode and there are slow-path events in
11799 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11800 * interrupt mode, this function is called as part of the device-level
11801 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11802 * undergoing initialization, the interrupt handler will not process the
11803 * interrupt. The link attention and ELS ring attention events are handled
11804 * by the worker thread. The interrupt handler signals the worker thread
11805 * and returns for these events. This function is called without any lock
11806 * held. It gets the hbalock to access and update SLI data structures.
11807 *
11808 * This function returns IRQ_HANDLED when interrupt is handled else it
11809 * returns IRQ_NONE.
11810 **/
11811irqreturn_t
11812lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11813{
11814 struct lpfc_hba *phba;
11815 struct lpfc_queue *speq;
11816 struct lpfc_eqe *eqe;
11817 unsigned long iflag;
11818 int ecount = 0;
11819
11820 /*
11821 * Get the driver's phba structure from the dev_id
11822 */
11823 phba = (struct lpfc_hba *)dev_id;
11824
11825 if (unlikely(!phba))
11826 return IRQ_NONE;
11827
11828 /* Get to the EQ struct associated with this vector */
11829 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011830 if (unlikely(!speq))
11831 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011832
11833 /* Check device state for handling interrupt */
11834 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011835 speq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011836 /* Check again for link_state with lock held */
11837 spin_lock_irqsave(&phba->hbalock, iflag);
11838 if (phba->link_state < LPFC_LINK_DOWN)
11839 /* Flush, clear interrupt, and rearm the EQ */
11840 lpfc_sli4_eq_flush(phba, speq);
11841 spin_unlock_irqrestore(&phba->hbalock, iflag);
11842 return IRQ_NONE;
11843 }
11844
11845 /*
11846 * Process all the event on FCP slow-path EQ
11847 */
11848 while ((eqe = lpfc_sli4_eq_get(speq))) {
11849 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011850 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011851 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011852 speq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011853 }
11854
James Smartb84daac2012-08-03 12:35:13 -040011855 /* Track the max number of EQEs processed in 1 intr */
11856 if (ecount > speq->EQ_max_eqe)
11857 speq->EQ_max_eqe = ecount;
11858
James Smart4f774512009-05-22 14:52:35 -040011859 /* Always clear and re-arm the slow-path EQ */
11860 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11861
11862 /* Catch the no cq entry condition */
11863 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011864 speq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011865 if (phba->intr_type == MSIX)
11866 /* MSI-X treated interrupt served as no EQ share INT */
11867 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11868 "0357 MSI-X interrupt with no EQE\n");
11869 else
11870 /* Non MSI-X treated on interrupt as EQ share INT */
11871 return IRQ_NONE;
11872 }
11873
11874 return IRQ_HANDLED;
11875} /* lpfc_sli4_sp_intr_handler */
11876
11877/**
11878 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11879 * @irq: Interrupt number.
11880 * @dev_id: The device context pointer.
11881 *
11882 * This function is directly called from the PCI layer as an interrupt
11883 * service routine when device with SLI-4 interface spec is enabled with
11884 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11885 * ring event in the HBA. However, when the device is enabled with either
11886 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11887 * device-level interrupt handler. When the PCI slot is in error recovery
11888 * or the HBA is undergoing initialization, the interrupt handler will not
11889 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11890 * the intrrupt context. This function is called without any lock held.
11891 * It gets the hbalock to access and update SLI data structures. Note that,
11892 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11893 * equal to that of FCP CQ index.
11894 *
11895 * This function returns IRQ_HANDLED when interrupt is handled else it
11896 * returns IRQ_NONE.
11897 **/
11898irqreturn_t
11899lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11900{
11901 struct lpfc_hba *phba;
11902 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11903 struct lpfc_queue *fpeq;
11904 struct lpfc_eqe *eqe;
11905 unsigned long iflag;
11906 int ecount = 0;
11907 uint32_t fcp_eqidx;
11908
11909 /* Get the driver's phba structure from the dev_id */
11910 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11911 phba = fcp_eq_hdl->phba;
11912 fcp_eqidx = fcp_eq_hdl->idx;
11913
11914 if (unlikely(!phba))
11915 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011916 if (unlikely(!phba->sli4_hba.fp_eq))
11917 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011918
11919 /* Get to the EQ struct associated with this vector */
11920 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011921 if (unlikely(!fpeq))
11922 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011923
11924 /* Check device state for handling interrupt */
11925 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011926 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011927 /* Check again for link_state with lock held */
11928 spin_lock_irqsave(&phba->hbalock, iflag);
11929 if (phba->link_state < LPFC_LINK_DOWN)
11930 /* Flush, clear interrupt, and rearm the EQ */
11931 lpfc_sli4_eq_flush(phba, fpeq);
11932 spin_unlock_irqrestore(&phba->hbalock, iflag);
11933 return IRQ_NONE;
11934 }
11935
11936 /*
11937 * Process all the event on FCP fast-path EQ
11938 */
11939 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11940 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011941 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011942 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011943 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011944 }
11945
James Smartb84daac2012-08-03 12:35:13 -040011946 /* Track the max number of EQEs processed in 1 intr */
11947 if (ecount > fpeq->EQ_max_eqe)
11948 fpeq->EQ_max_eqe = ecount;
11949
James Smart4f774512009-05-22 14:52:35 -040011950 /* Always clear and re-arm the fast-path EQ */
11951 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11952
11953 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011954 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011955 if (phba->intr_type == MSIX)
11956 /* MSI-X treated interrupt served as no EQ share INT */
11957 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11958 "0358 MSI-X interrupt with no EQE\n");
11959 else
11960 /* Non MSI-X treated on interrupt as EQ share INT */
11961 return IRQ_NONE;
11962 }
11963
11964 return IRQ_HANDLED;
11965} /* lpfc_sli4_fp_intr_handler */
11966
11967/**
11968 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11969 * @irq: Interrupt number.
11970 * @dev_id: The device context pointer.
11971 *
11972 * This function is the device-level interrupt handler to device with SLI-4
11973 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11974 * interrupt mode is enabled and there is an event in the HBA which requires
11975 * driver attention. This function invokes the slow-path interrupt attention
11976 * handling function and fast-path interrupt attention handling function in
11977 * turn to process the relevant HBA attention events. This function is called
11978 * without any lock held. It gets the hbalock to access and update SLI data
11979 * structures.
11980 *
11981 * This function returns IRQ_HANDLED when interrupt is handled, else it
11982 * returns IRQ_NONE.
11983 **/
11984irqreturn_t
11985lpfc_sli4_intr_handler(int irq, void *dev_id)
11986{
11987 struct lpfc_hba *phba;
11988 irqreturn_t sp_irq_rc, fp_irq_rc;
11989 bool fp_handled = false;
11990 uint32_t fcp_eqidx;
11991
11992 /* Get the driver's phba structure from the dev_id */
11993 phba = (struct lpfc_hba *)dev_id;
11994
11995 if (unlikely(!phba))
11996 return IRQ_NONE;
11997
11998 /*
11999 * Invokes slow-path host attention interrupt handling as appropriate.
12000 */
12001 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
12002
12003 /*
12004 * Invoke fast-path host attention interrupt handling as appropriate.
12005 */
12006 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
12007 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
12008 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12009 if (fp_irq_rc == IRQ_HANDLED)
12010 fp_handled |= true;
12011 }
12012
12013 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
12014} /* lpfc_sli4_intr_handler */
12015
12016/**
12017 * lpfc_sli4_queue_free - free a queue structure and associated memory
12018 * @queue: The queue structure to free.
12019 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012020 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012021 * the host resident queue. This function must be called after destroying the
12022 * queue on the HBA.
12023 **/
12024void
12025lpfc_sli4_queue_free(struct lpfc_queue *queue)
12026{
12027 struct lpfc_dmabuf *dmabuf;
12028
12029 if (!queue)
12030 return;
12031
12032 while (!list_empty(&queue->page_list)) {
12033 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12034 list);
James Smart49198b32010-04-06 15:04:33 -040012035 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012036 dmabuf->virt, dmabuf->phys);
12037 kfree(dmabuf);
12038 }
12039 kfree(queue);
12040 return;
12041}
12042
12043/**
12044 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12045 * @phba: The HBA that this queue is being created on.
12046 * @entry_size: The size of each queue entry for this queue.
12047 * @entry count: The number of entries that this queue will handle.
12048 *
12049 * This function allocates a queue structure and the DMAable memory used for
12050 * the host resident queue. This function must be called before creating the
12051 * queue on the HBA.
12052 **/
12053struct lpfc_queue *
12054lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12055 uint32_t entry_count)
12056{
12057 struct lpfc_queue *queue;
12058 struct lpfc_dmabuf *dmabuf;
12059 int x, total_qe_count;
12060 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012061 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012062
James Smartcb5172e2010-03-15 11:25:07 -040012063 if (!phba->sli4_hba.pc_sli4_params.supported)
12064 hw_page_size = SLI4_PAGE_SIZE;
12065
James Smart4f774512009-05-22 14:52:35 -040012066 queue = kzalloc(sizeof(struct lpfc_queue) +
12067 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12068 if (!queue)
12069 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012070 queue->page_count = (ALIGN(entry_size * entry_count,
12071 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012072 INIT_LIST_HEAD(&queue->list);
12073 INIT_LIST_HEAD(&queue->page_list);
12074 INIT_LIST_HEAD(&queue->child_list);
12075 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12076 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12077 if (!dmabuf)
12078 goto out_fail;
12079 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012080 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012081 GFP_KERNEL);
12082 if (!dmabuf->virt) {
12083 kfree(dmabuf);
12084 goto out_fail;
12085 }
James Smartcb5172e2010-03-15 11:25:07 -040012086 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012087 dmabuf->buffer_tag = x;
12088 list_add_tail(&dmabuf->list, &queue->page_list);
12089 /* initialize queue's entry array */
12090 dma_pointer = dmabuf->virt;
12091 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012092 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012093 total_qe_count++, dma_pointer += entry_size) {
12094 queue->qe[total_qe_count].address = dma_pointer;
12095 }
12096 }
12097 queue->entry_size = entry_size;
12098 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012099
12100 /*
12101 * entry_repost is calculated based on the number of entries in the
12102 * queue. This works out except for RQs. If buffers are NOT initially
12103 * posted for every RQE, entry_repost should be adjusted accordingly.
12104 */
12105 queue->entry_repost = (entry_count >> 3);
12106 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12107 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012108 queue->phba = phba;
12109
12110 return queue;
12111out_fail:
12112 lpfc_sli4_queue_free(queue);
12113 return NULL;
12114}
12115
12116/**
James Smart173edbb2012-06-12 13:54:50 -040012117 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12118 * @phba: HBA structure that indicates port to create a queue on.
12119 * @startq: The starting FCP EQ to modify
12120 *
12121 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12122 *
12123 * The @phba struct is used to send mailbox command to HBA. The @startq
12124 * is used to get the starting FCP EQ to change.
12125 * This function is asynchronous and will wait for the mailbox
12126 * command to finish before continuing.
12127 *
12128 * On success this function will return a zero. If unable to allocate enough
12129 * memory this function will return -ENOMEM. If the queue create mailbox command
12130 * fails this function will return -ENXIO.
12131 **/
12132uint32_t
12133lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12134{
12135 struct lpfc_mbx_modify_eq_delay *eq_delay;
12136 LPFC_MBOXQ_t *mbox;
12137 struct lpfc_queue *eq;
12138 int cnt, rc, length, status = 0;
12139 uint32_t shdr_status, shdr_add_status;
12140 int fcp_eqidx;
12141 union lpfc_sli4_cfg_shdr *shdr;
12142 uint16_t dmult;
12143
12144 if (startq >= phba->cfg_fcp_eq_count)
12145 return 0;
12146
12147 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12148 if (!mbox)
12149 return -ENOMEM;
12150 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12151 sizeof(struct lpfc_sli4_cfg_mhdr));
12152 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12153 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12154 length, LPFC_SLI4_MBX_EMBED);
12155 eq_delay = &mbox->u.mqe.un.eq_delay;
12156
12157 /* Calculate delay multiper from maximum interrupt per second */
12158 dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
12159
12160 cnt = 0;
12161 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
12162 fcp_eqidx++) {
12163 eq = phba->sli4_hba.fp_eq[fcp_eqidx];
12164 if (!eq)
12165 continue;
12166 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12167 eq_delay->u.request.eq[cnt].phase = 0;
12168 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12169 cnt++;
12170 if (cnt >= LPFC_MAX_EQ_DELAY)
12171 break;
12172 }
12173 eq_delay->u.request.num_eq = cnt;
12174
12175 mbox->vport = phba->pport;
12176 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12177 mbox->context1 = NULL;
12178 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12179 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12180 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12181 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12182 if (shdr_status || shdr_add_status || rc) {
12183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12184 "2512 MODIFY_EQ_DELAY mailbox failed with "
12185 "status x%x add_status x%x, mbx status x%x\n",
12186 shdr_status, shdr_add_status, rc);
12187 status = -ENXIO;
12188 }
12189 mempool_free(mbox, phba->mbox_mem_pool);
12190 return status;
12191}
12192
12193/**
James Smart4f774512009-05-22 14:52:35 -040012194 * lpfc_eq_create - Create an Event Queue on the HBA
12195 * @phba: HBA structure that indicates port to create a queue on.
12196 * @eq: The queue structure to use to create the event queue.
12197 * @imax: The maximum interrupt per second limit.
12198 *
12199 * This function creates an event queue, as detailed in @eq, on a port,
12200 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12201 *
12202 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12203 * is used to get the entry count and entry size that are necessary to
12204 * determine the number of pages to allocate and use for this queue. This
12205 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12206 * event queue. This function is asynchronous and will wait for the mailbox
12207 * command to finish before continuing.
12208 *
12209 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012210 * memory this function will return -ENOMEM. If the queue create mailbox command
12211 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012212 **/
12213uint32_t
12214lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12215{
12216 struct lpfc_mbx_eq_create *eq_create;
12217 LPFC_MBOXQ_t *mbox;
12218 int rc, length, status = 0;
12219 struct lpfc_dmabuf *dmabuf;
12220 uint32_t shdr_status, shdr_add_status;
12221 union lpfc_sli4_cfg_shdr *shdr;
12222 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012223 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12224
James Smart2e90f4b2011-12-13 13:22:37 -050012225 /* sanity check on queue memory */
12226 if (!eq)
12227 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012228 if (!phba->sli4_hba.pc_sli4_params.supported)
12229 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012230
12231 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12232 if (!mbox)
12233 return -ENOMEM;
12234 length = (sizeof(struct lpfc_mbx_eq_create) -
12235 sizeof(struct lpfc_sli4_cfg_mhdr));
12236 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12237 LPFC_MBOX_OPCODE_EQ_CREATE,
12238 length, LPFC_SLI4_MBX_EMBED);
12239 eq_create = &mbox->u.mqe.un.eq_create;
12240 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12241 eq->page_count);
12242 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12243 LPFC_EQE_SIZE);
12244 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12245 /* Calculate delay multiper from maximum interrupt per second */
12246 dmult = LPFC_DMULT_CONST/imax - 1;
12247 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12248 dmult);
12249 switch (eq->entry_count) {
12250 default:
12251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12252 "0360 Unsupported EQ count. (%d)\n",
12253 eq->entry_count);
12254 if (eq->entry_count < 256)
12255 return -EINVAL;
12256 /* otherwise default to smallest count (drop through) */
12257 case 256:
12258 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12259 LPFC_EQ_CNT_256);
12260 break;
12261 case 512:
12262 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12263 LPFC_EQ_CNT_512);
12264 break;
12265 case 1024:
12266 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12267 LPFC_EQ_CNT_1024);
12268 break;
12269 case 2048:
12270 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12271 LPFC_EQ_CNT_2048);
12272 break;
12273 case 4096:
12274 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12275 LPFC_EQ_CNT_4096);
12276 break;
12277 }
12278 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012279 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012280 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12281 putPaddrLow(dmabuf->phys);
12282 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12283 putPaddrHigh(dmabuf->phys);
12284 }
12285 mbox->vport = phba->pport;
12286 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12287 mbox->context1 = NULL;
12288 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12289 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12290 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12291 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12292 if (shdr_status || shdr_add_status || rc) {
12293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12294 "2500 EQ_CREATE mailbox failed with "
12295 "status x%x add_status x%x, mbx status x%x\n",
12296 shdr_status, shdr_add_status, rc);
12297 status = -ENXIO;
12298 }
12299 eq->type = LPFC_EQ;
12300 eq->subtype = LPFC_NONE;
12301 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12302 if (eq->queue_id == 0xFFFF)
12303 status = -ENXIO;
12304 eq->host_index = 0;
12305 eq->hba_index = 0;
12306
James Smart8fa38512009-07-19 10:01:03 -040012307 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012308 return status;
12309}
12310
12311/**
12312 * lpfc_cq_create - Create a Completion Queue on the HBA
12313 * @phba: HBA structure that indicates port to create a queue on.
12314 * @cq: The queue structure to use to create the completion queue.
12315 * @eq: The event queue to bind this completion queue to.
12316 *
12317 * This function creates a completion queue, as detailed in @wq, on a port,
12318 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12319 *
12320 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12321 * is used to get the entry count and entry size that are necessary to
12322 * determine the number of pages to allocate and use for this queue. The @eq
12323 * is used to indicate which event queue to bind this completion queue to. This
12324 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12325 * completion queue. This function is asynchronous and will wait for the mailbox
12326 * command to finish before continuing.
12327 *
12328 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012329 * memory this function will return -ENOMEM. If the queue create mailbox command
12330 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012331 **/
12332uint32_t
12333lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12334 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12335{
12336 struct lpfc_mbx_cq_create *cq_create;
12337 struct lpfc_dmabuf *dmabuf;
12338 LPFC_MBOXQ_t *mbox;
12339 int rc, length, status = 0;
12340 uint32_t shdr_status, shdr_add_status;
12341 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012342 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12343
James Smart2e90f4b2011-12-13 13:22:37 -050012344 /* sanity check on queue memory */
12345 if (!cq || !eq)
12346 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012347 if (!phba->sli4_hba.pc_sli4_params.supported)
12348 hw_page_size = SLI4_PAGE_SIZE;
12349
James Smart4f774512009-05-22 14:52:35 -040012350 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12351 if (!mbox)
12352 return -ENOMEM;
12353 length = (sizeof(struct lpfc_mbx_cq_create) -
12354 sizeof(struct lpfc_sli4_cfg_mhdr));
12355 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12356 LPFC_MBOX_OPCODE_CQ_CREATE,
12357 length, LPFC_SLI4_MBX_EMBED);
12358 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012359 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012360 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12361 cq->page_count);
12362 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12363 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012364 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12365 phba->sli4_hba.pc_sli4_params.cqv);
12366 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012367 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12368 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012369 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12370 eq->queue_id);
12371 } else {
12372 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12373 eq->queue_id);
12374 }
James Smart4f774512009-05-22 14:52:35 -040012375 switch (cq->entry_count) {
12376 default:
12377 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12378 "0361 Unsupported CQ count. (%d)\n",
12379 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012380 if (cq->entry_count < 256) {
12381 status = -EINVAL;
12382 goto out;
12383 }
James Smart4f774512009-05-22 14:52:35 -040012384 /* otherwise default to smallest count (drop through) */
12385 case 256:
12386 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12387 LPFC_CQ_CNT_256);
12388 break;
12389 case 512:
12390 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12391 LPFC_CQ_CNT_512);
12392 break;
12393 case 1024:
12394 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12395 LPFC_CQ_CNT_1024);
12396 break;
12397 }
12398 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012399 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012400 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12401 putPaddrLow(dmabuf->phys);
12402 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12403 putPaddrHigh(dmabuf->phys);
12404 }
12405 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12406
12407 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012408 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12409 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12410 if (shdr_status || shdr_add_status || rc) {
12411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12412 "2501 CQ_CREATE mailbox failed with "
12413 "status x%x add_status x%x, mbx status x%x\n",
12414 shdr_status, shdr_add_status, rc);
12415 status = -ENXIO;
12416 goto out;
12417 }
12418 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12419 if (cq->queue_id == 0xFFFF) {
12420 status = -ENXIO;
12421 goto out;
12422 }
12423 /* link the cq onto the parent eq child list */
12424 list_add_tail(&cq->list, &eq->child_list);
12425 /* Set up completion queue's type and subtype */
12426 cq->type = type;
12427 cq->subtype = subtype;
12428 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012429 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012430 cq->host_index = 0;
12431 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012432
James Smart8fa38512009-07-19 10:01:03 -040012433out:
12434 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012435 return status;
12436}
12437
12438/**
James Smartb19a0612010-04-06 14:48:51 -040012439 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012440 * @phba: HBA structure that indicates port to create a queue on.
12441 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012442 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12443 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012444 *
James Smartb19a0612010-04-06 14:48:51 -040012445 * This function provides failback (fb) functionality when the
12446 * mq_create_ext fails on older FW generations. It's purpose is identical
12447 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012448 *
James Smartb19a0612010-04-06 14:48:51 -040012449 * This routine cannot fail as all attributes were previously accessed and
12450 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012451 **/
James Smartb19a0612010-04-06 14:48:51 -040012452static void
12453lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12454 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012455{
12456 struct lpfc_mbx_mq_create *mq_create;
12457 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012458 int length;
James Smart04c68492009-05-22 14:52:52 -040012459
James Smart04c68492009-05-22 14:52:52 -040012460 length = (sizeof(struct lpfc_mbx_mq_create) -
12461 sizeof(struct lpfc_sli4_cfg_mhdr));
12462 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12463 LPFC_MBOX_OPCODE_MQ_CREATE,
12464 length, LPFC_SLI4_MBX_EMBED);
12465 mq_create = &mbox->u.mqe.un.mq_create;
12466 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012467 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012468 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012469 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012470 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12471 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012472 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012473 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12474 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012475 break;
12476 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012477 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12478 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012479 break;
12480 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012481 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12482 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012483 break;
12484 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012485 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12486 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012487 break;
12488 }
12489 list_for_each_entry(dmabuf, &mq->page_list, list) {
12490 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012491 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012492 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012493 putPaddrHigh(dmabuf->phys);
12494 }
12495}
12496
12497/**
12498 * lpfc_mq_create - Create a mailbox Queue on the HBA
12499 * @phba: HBA structure that indicates port to create a queue on.
12500 * @mq: The queue structure to use to create the mailbox queue.
12501 * @cq: The completion queue to associate with this cq.
12502 * @subtype: The queue's subtype.
12503 *
12504 * This function creates a mailbox queue, as detailed in @mq, on a port,
12505 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12506 *
12507 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12508 * is used to get the entry count and entry size that are necessary to
12509 * determine the number of pages to allocate and use for this queue. This
12510 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12511 * mailbox queue. This function is asynchronous and will wait for the mailbox
12512 * command to finish before continuing.
12513 *
12514 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012515 * memory this function will return -ENOMEM. If the queue create mailbox command
12516 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012517 **/
12518int32_t
12519lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12520 struct lpfc_queue *cq, uint32_t subtype)
12521{
12522 struct lpfc_mbx_mq_create *mq_create;
12523 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12524 struct lpfc_dmabuf *dmabuf;
12525 LPFC_MBOXQ_t *mbox;
12526 int rc, length, status = 0;
12527 uint32_t shdr_status, shdr_add_status;
12528 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012529 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012530
James Smart2e90f4b2011-12-13 13:22:37 -050012531 /* sanity check on queue memory */
12532 if (!mq || !cq)
12533 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012534 if (!phba->sli4_hba.pc_sli4_params.supported)
12535 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012536
12537 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12538 if (!mbox)
12539 return -ENOMEM;
12540 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12541 sizeof(struct lpfc_sli4_cfg_mhdr));
12542 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12543 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12544 length, LPFC_SLI4_MBX_EMBED);
12545
12546 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012547 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012548 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12549 &mq_create_ext->u.request, mq->page_count);
12550 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12551 &mq_create_ext->u.request, 1);
12552 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012553 &mq_create_ext->u.request, 1);
12554 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12555 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012556 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12557 &mq_create_ext->u.request, 1);
12558 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12559 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012560 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012561 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12562 phba->sli4_hba.pc_sli4_params.mqv);
12563 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12564 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12565 cq->queue_id);
12566 else
12567 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12568 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012569 switch (mq->entry_count) {
12570 default:
12571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12572 "0362 Unsupported MQ count. (%d)\n",
12573 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012574 if (mq->entry_count < 16) {
12575 status = -EINVAL;
12576 goto out;
12577 }
James Smartb19a0612010-04-06 14:48:51 -040012578 /* otherwise default to smallest count (drop through) */
12579 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012580 bf_set(lpfc_mq_context_ring_size,
12581 &mq_create_ext->u.request.context,
12582 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012583 break;
12584 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012585 bf_set(lpfc_mq_context_ring_size,
12586 &mq_create_ext->u.request.context,
12587 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012588 break;
12589 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012590 bf_set(lpfc_mq_context_ring_size,
12591 &mq_create_ext->u.request.context,
12592 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012593 break;
12594 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012595 bf_set(lpfc_mq_context_ring_size,
12596 &mq_create_ext->u.request.context,
12597 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012598 break;
12599 }
12600 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012601 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012602 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12603 putPaddrLow(dmabuf->phys);
12604 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012605 putPaddrHigh(dmabuf->phys);
12606 }
12607 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012608 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12609 &mq_create_ext->u.response);
12610 if (rc != MBX_SUCCESS) {
12611 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12612 "2795 MQ_CREATE_EXT failed with "
12613 "status x%x. Failback to MQ_CREATE.\n",
12614 rc);
12615 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12616 mq_create = &mbox->u.mqe.un.mq_create;
12617 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12618 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12619 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12620 &mq_create->u.response);
12621 }
12622
James Smart04c68492009-05-22 14:52:52 -040012623 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12626 if (shdr_status || shdr_add_status || rc) {
12627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12628 "2502 MQ_CREATE mailbox failed with "
12629 "status x%x add_status x%x, mbx status x%x\n",
12630 shdr_status, shdr_add_status, rc);
12631 status = -ENXIO;
12632 goto out;
12633 }
James Smart04c68492009-05-22 14:52:52 -040012634 if (mq->queue_id == 0xFFFF) {
12635 status = -ENXIO;
12636 goto out;
12637 }
12638 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012639 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012640 mq->subtype = subtype;
12641 mq->host_index = 0;
12642 mq->hba_index = 0;
12643
12644 /* link the mq onto the parent cq child list */
12645 list_add_tail(&mq->list, &cq->child_list);
12646out:
James Smart8fa38512009-07-19 10:01:03 -040012647 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012648 return status;
12649}
12650
12651/**
James Smart4f774512009-05-22 14:52:35 -040012652 * lpfc_wq_create - Create a Work Queue on the HBA
12653 * @phba: HBA structure that indicates port to create a queue on.
12654 * @wq: The queue structure to use to create the work queue.
12655 * @cq: The completion queue to bind this work queue to.
12656 * @subtype: The subtype of the work queue indicating its functionality.
12657 *
12658 * This function creates a work queue, as detailed in @wq, on a port, described
12659 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12660 *
12661 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12662 * is used to get the entry count and entry size that are necessary to
12663 * determine the number of pages to allocate and use for this queue. The @cq
12664 * is used to indicate which completion queue to bind this work queue to. This
12665 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12666 * work queue. This function is asynchronous and will wait for the mailbox
12667 * command to finish before continuing.
12668 *
12669 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012670 * memory this function will return -ENOMEM. If the queue create mailbox command
12671 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012672 **/
12673uint32_t
12674lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12675 struct lpfc_queue *cq, uint32_t subtype)
12676{
12677 struct lpfc_mbx_wq_create *wq_create;
12678 struct lpfc_dmabuf *dmabuf;
12679 LPFC_MBOXQ_t *mbox;
12680 int rc, length, status = 0;
12681 uint32_t shdr_status, shdr_add_status;
12682 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012683 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012684 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012685
James Smart2e90f4b2011-12-13 13:22:37 -050012686 /* sanity check on queue memory */
12687 if (!wq || !cq)
12688 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012689 if (!phba->sli4_hba.pc_sli4_params.supported)
12690 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012691
12692 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12693 if (!mbox)
12694 return -ENOMEM;
12695 length = (sizeof(struct lpfc_mbx_wq_create) -
12696 sizeof(struct lpfc_sli4_cfg_mhdr));
12697 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12698 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12699 length, LPFC_SLI4_MBX_EMBED);
12700 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012701 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012702 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12703 wq->page_count);
12704 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12705 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012706 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12707 phba->sli4_hba.pc_sli4_params.wqv);
12708 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12709 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12710 wq->entry_count);
12711 switch (wq->entry_size) {
12712 default:
12713 case 64:
12714 bf_set(lpfc_mbx_wq_create_wqe_size,
12715 &wq_create->u.request_1,
12716 LPFC_WQ_WQE_SIZE_64);
12717 break;
12718 case 128:
12719 bf_set(lpfc_mbx_wq_create_wqe_size,
12720 &wq_create->u.request_1,
12721 LPFC_WQ_WQE_SIZE_128);
12722 break;
12723 }
12724 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12725 (PAGE_SIZE/SLI4_PAGE_SIZE));
12726 page = wq_create->u.request_1.page;
12727 } else {
12728 page = wq_create->u.request.page;
12729 }
James Smart4f774512009-05-22 14:52:35 -040012730 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012731 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012732 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12733 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012734 }
12735 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12736 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012737 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12738 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12739 if (shdr_status || shdr_add_status || rc) {
12740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12741 "2503 WQ_CREATE mailbox failed with "
12742 "status x%x add_status x%x, mbx status x%x\n",
12743 shdr_status, shdr_add_status, rc);
12744 status = -ENXIO;
12745 goto out;
12746 }
12747 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12748 if (wq->queue_id == 0xFFFF) {
12749 status = -ENXIO;
12750 goto out;
12751 }
12752 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012753 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012754 wq->subtype = subtype;
12755 wq->host_index = 0;
12756 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012757 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012758
12759 /* link the wq onto the parent cq child list */
12760 list_add_tail(&wq->list, &cq->child_list);
12761out:
James Smart8fa38512009-07-19 10:01:03 -040012762 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012763 return status;
12764}
12765
12766/**
James Smart73d91e52011-10-10 21:32:10 -040012767 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12768 * @phba: HBA structure that indicates port to create a queue on.
12769 * @rq: The queue structure to use for the receive queue.
12770 * @qno: The associated HBQ number
12771 *
12772 *
12773 * For SLI4 we need to adjust the RQ repost value based on
12774 * the number of buffers that are initially posted to the RQ.
12775 */
12776void
12777lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12778{
12779 uint32_t cnt;
12780
James Smart2e90f4b2011-12-13 13:22:37 -050012781 /* sanity check on queue memory */
12782 if (!rq)
12783 return;
James Smart73d91e52011-10-10 21:32:10 -040012784 cnt = lpfc_hbq_defs[qno]->entry_count;
12785
12786 /* Recalc repost for RQs based on buffers initially posted */
12787 cnt = (cnt >> 3);
12788 if (cnt < LPFC_QUEUE_MIN_REPOST)
12789 cnt = LPFC_QUEUE_MIN_REPOST;
12790
12791 rq->entry_repost = cnt;
12792}
12793
12794/**
James Smart4f774512009-05-22 14:52:35 -040012795 * lpfc_rq_create - Create a Receive Queue on the HBA
12796 * @phba: HBA structure that indicates port to create a queue on.
12797 * @hrq: The queue structure to use to create the header receive queue.
12798 * @drq: The queue structure to use to create the data receive queue.
12799 * @cq: The completion queue to bind this work queue to.
12800 *
12801 * This function creates a receive buffer queue pair , as detailed in @hrq and
12802 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12803 * to the HBA.
12804 *
12805 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12806 * struct is used to get the entry count that is necessary to determine the
12807 * number of pages to use for this queue. The @cq is used to indicate which
12808 * completion queue to bind received buffers that are posted to these queues to.
12809 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12810 * receive queue pair. This function is asynchronous and will wait for the
12811 * mailbox command to finish before continuing.
12812 *
12813 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012814 * memory this function will return -ENOMEM. If the queue create mailbox command
12815 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012816 **/
12817uint32_t
12818lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12819 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12820{
12821 struct lpfc_mbx_rq_create *rq_create;
12822 struct lpfc_dmabuf *dmabuf;
12823 LPFC_MBOXQ_t *mbox;
12824 int rc, length, status = 0;
12825 uint32_t shdr_status, shdr_add_status;
12826 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012827 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12828
James Smart2e90f4b2011-12-13 13:22:37 -050012829 /* sanity check on queue memory */
12830 if (!hrq || !drq || !cq)
12831 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012832 if (!phba->sli4_hba.pc_sli4_params.supported)
12833 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012834
12835 if (hrq->entry_count != drq->entry_count)
12836 return -EINVAL;
12837 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12838 if (!mbox)
12839 return -ENOMEM;
12840 length = (sizeof(struct lpfc_mbx_rq_create) -
12841 sizeof(struct lpfc_sli4_cfg_mhdr));
12842 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12843 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12844 length, LPFC_SLI4_MBX_EMBED);
12845 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012846 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12847 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12848 phba->sli4_hba.pc_sli4_params.rqv);
12849 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12850 bf_set(lpfc_rq_context_rqe_count_1,
12851 &rq_create->u.request.context,
12852 hrq->entry_count);
12853 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012854 bf_set(lpfc_rq_context_rqe_size,
12855 &rq_create->u.request.context,
12856 LPFC_RQE_SIZE_8);
12857 bf_set(lpfc_rq_context_page_size,
12858 &rq_create->u.request.context,
12859 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012860 } else {
12861 switch (hrq->entry_count) {
12862 default:
12863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12864 "2535 Unsupported RQ count. (%d)\n",
12865 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012866 if (hrq->entry_count < 512) {
12867 status = -EINVAL;
12868 goto out;
12869 }
James Smart5a6f1332011-03-11 16:05:35 -050012870 /* otherwise default to smallest count (drop through) */
12871 case 512:
12872 bf_set(lpfc_rq_context_rqe_count,
12873 &rq_create->u.request.context,
12874 LPFC_RQ_RING_SIZE_512);
12875 break;
12876 case 1024:
12877 bf_set(lpfc_rq_context_rqe_count,
12878 &rq_create->u.request.context,
12879 LPFC_RQ_RING_SIZE_1024);
12880 break;
12881 case 2048:
12882 bf_set(lpfc_rq_context_rqe_count,
12883 &rq_create->u.request.context,
12884 LPFC_RQ_RING_SIZE_2048);
12885 break;
12886 case 4096:
12887 bf_set(lpfc_rq_context_rqe_count,
12888 &rq_create->u.request.context,
12889 LPFC_RQ_RING_SIZE_4096);
12890 break;
12891 }
12892 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12893 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012894 }
12895 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12896 cq->queue_id);
12897 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12898 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012899 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012900 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012901 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12902 putPaddrLow(dmabuf->phys);
12903 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12904 putPaddrHigh(dmabuf->phys);
12905 }
12906 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12907 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012908 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12909 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12910 if (shdr_status || shdr_add_status || rc) {
12911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12912 "2504 RQ_CREATE mailbox failed with "
12913 "status x%x add_status x%x, mbx status x%x\n",
12914 shdr_status, shdr_add_status, rc);
12915 status = -ENXIO;
12916 goto out;
12917 }
12918 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12919 if (hrq->queue_id == 0xFFFF) {
12920 status = -ENXIO;
12921 goto out;
12922 }
12923 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012924 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012925 hrq->subtype = subtype;
12926 hrq->host_index = 0;
12927 hrq->hba_index = 0;
12928
12929 /* now create the data queue */
12930 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12931 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12932 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012933 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12934 phba->sli4_hba.pc_sli4_params.rqv);
12935 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12936 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012937 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012938 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012939 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12940 LPFC_RQE_SIZE_8);
12941 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12942 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012943 } else {
12944 switch (drq->entry_count) {
12945 default:
12946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12947 "2536 Unsupported RQ count. (%d)\n",
12948 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012949 if (drq->entry_count < 512) {
12950 status = -EINVAL;
12951 goto out;
12952 }
James Smart5a6f1332011-03-11 16:05:35 -050012953 /* otherwise default to smallest count (drop through) */
12954 case 512:
12955 bf_set(lpfc_rq_context_rqe_count,
12956 &rq_create->u.request.context,
12957 LPFC_RQ_RING_SIZE_512);
12958 break;
12959 case 1024:
12960 bf_set(lpfc_rq_context_rqe_count,
12961 &rq_create->u.request.context,
12962 LPFC_RQ_RING_SIZE_1024);
12963 break;
12964 case 2048:
12965 bf_set(lpfc_rq_context_rqe_count,
12966 &rq_create->u.request.context,
12967 LPFC_RQ_RING_SIZE_2048);
12968 break;
12969 case 4096:
12970 bf_set(lpfc_rq_context_rqe_count,
12971 &rq_create->u.request.context,
12972 LPFC_RQ_RING_SIZE_4096);
12973 break;
12974 }
12975 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12976 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012977 }
12978 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12979 cq->queue_id);
12980 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12981 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012982 list_for_each_entry(dmabuf, &drq->page_list, list) {
12983 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12984 putPaddrLow(dmabuf->phys);
12985 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12986 putPaddrHigh(dmabuf->phys);
12987 }
12988 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12989 /* The IOCTL status is embedded in the mailbox subheader. */
12990 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12991 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12992 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12993 if (shdr_status || shdr_add_status || rc) {
12994 status = -ENXIO;
12995 goto out;
12996 }
12997 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12998 if (drq->queue_id == 0xFFFF) {
12999 status = -ENXIO;
13000 goto out;
13001 }
13002 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013003 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013004 drq->subtype = subtype;
13005 drq->host_index = 0;
13006 drq->hba_index = 0;
13007
13008 /* link the header and data RQs onto the parent cq child list */
13009 list_add_tail(&hrq->list, &cq->child_list);
13010 list_add_tail(&drq->list, &cq->child_list);
13011
13012out:
James Smart8fa38512009-07-19 10:01:03 -040013013 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013014 return status;
13015}
13016
13017/**
13018 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13019 * @eq: The queue structure associated with the queue to destroy.
13020 *
13021 * This function destroys a queue, as detailed in @eq by sending an mailbox
13022 * command, specific to the type of queue, to the HBA.
13023 *
13024 * The @eq struct is used to get the queue ID of the queue to destroy.
13025 *
13026 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013027 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013028 **/
13029uint32_t
13030lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13031{
13032 LPFC_MBOXQ_t *mbox;
13033 int rc, length, status = 0;
13034 uint32_t shdr_status, shdr_add_status;
13035 union lpfc_sli4_cfg_shdr *shdr;
13036
James Smart2e90f4b2011-12-13 13:22:37 -050013037 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013038 if (!eq)
13039 return -ENODEV;
13040 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13041 if (!mbox)
13042 return -ENOMEM;
13043 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13044 sizeof(struct lpfc_sli4_cfg_mhdr));
13045 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13046 LPFC_MBOX_OPCODE_EQ_DESTROY,
13047 length, LPFC_SLI4_MBX_EMBED);
13048 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13049 eq->queue_id);
13050 mbox->vport = eq->phba->pport;
13051 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13052
13053 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13054 /* The IOCTL status is embedded in the mailbox subheader. */
13055 shdr = (union lpfc_sli4_cfg_shdr *)
13056 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13057 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13058 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13059 if (shdr_status || shdr_add_status || rc) {
13060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13061 "2505 EQ_DESTROY mailbox failed with "
13062 "status x%x add_status x%x, mbx status x%x\n",
13063 shdr_status, shdr_add_status, rc);
13064 status = -ENXIO;
13065 }
13066
13067 /* Remove eq from any list */
13068 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013069 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013070 return status;
13071}
13072
13073/**
13074 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13075 * @cq: The queue structure associated with the queue to destroy.
13076 *
13077 * This function destroys a queue, as detailed in @cq by sending an mailbox
13078 * command, specific to the type of queue, to the HBA.
13079 *
13080 * The @cq struct is used to get the queue ID of the queue to destroy.
13081 *
13082 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013083 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013084 **/
13085uint32_t
13086lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13087{
13088 LPFC_MBOXQ_t *mbox;
13089 int rc, length, status = 0;
13090 uint32_t shdr_status, shdr_add_status;
13091 union lpfc_sli4_cfg_shdr *shdr;
13092
James Smart2e90f4b2011-12-13 13:22:37 -050013093 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013094 if (!cq)
13095 return -ENODEV;
13096 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13097 if (!mbox)
13098 return -ENOMEM;
13099 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13100 sizeof(struct lpfc_sli4_cfg_mhdr));
13101 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13102 LPFC_MBOX_OPCODE_CQ_DESTROY,
13103 length, LPFC_SLI4_MBX_EMBED);
13104 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13105 cq->queue_id);
13106 mbox->vport = cq->phba->pport;
13107 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13108 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13109 /* The IOCTL status is embedded in the mailbox subheader. */
13110 shdr = (union lpfc_sli4_cfg_shdr *)
13111 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13112 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13113 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13114 if (shdr_status || shdr_add_status || rc) {
13115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13116 "2506 CQ_DESTROY mailbox failed with "
13117 "status x%x add_status x%x, mbx status x%x\n",
13118 shdr_status, shdr_add_status, rc);
13119 status = -ENXIO;
13120 }
13121 /* Remove cq from any list */
13122 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013123 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013124 return status;
13125}
13126
13127/**
James Smart04c68492009-05-22 14:52:52 -040013128 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13129 * @qm: The queue structure associated with the queue to destroy.
13130 *
13131 * This function destroys a queue, as detailed in @mq by sending an mailbox
13132 * command, specific to the type of queue, to the HBA.
13133 *
13134 * The @mq struct is used to get the queue ID of the queue to destroy.
13135 *
13136 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013137 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013138 **/
13139uint32_t
13140lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13141{
13142 LPFC_MBOXQ_t *mbox;
13143 int rc, length, status = 0;
13144 uint32_t shdr_status, shdr_add_status;
13145 union lpfc_sli4_cfg_shdr *shdr;
13146
James Smart2e90f4b2011-12-13 13:22:37 -050013147 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013148 if (!mq)
13149 return -ENODEV;
13150 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13151 if (!mbox)
13152 return -ENOMEM;
13153 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13154 sizeof(struct lpfc_sli4_cfg_mhdr));
13155 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13156 LPFC_MBOX_OPCODE_MQ_DESTROY,
13157 length, LPFC_SLI4_MBX_EMBED);
13158 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13159 mq->queue_id);
13160 mbox->vport = mq->phba->pport;
13161 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13162 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13163 /* The IOCTL status is embedded in the mailbox subheader. */
13164 shdr = (union lpfc_sli4_cfg_shdr *)
13165 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13166 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13167 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13168 if (shdr_status || shdr_add_status || rc) {
13169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13170 "2507 MQ_DESTROY mailbox failed with "
13171 "status x%x add_status x%x, mbx status x%x\n",
13172 shdr_status, shdr_add_status, rc);
13173 status = -ENXIO;
13174 }
13175 /* Remove mq from any list */
13176 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013177 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013178 return status;
13179}
13180
13181/**
James Smart4f774512009-05-22 14:52:35 -040013182 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13183 * @wq: The queue structure associated with the queue to destroy.
13184 *
13185 * This function destroys a queue, as detailed in @wq by sending an mailbox
13186 * command, specific to the type of queue, to the HBA.
13187 *
13188 * The @wq struct is used to get the queue ID of the queue to destroy.
13189 *
13190 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013191 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013192 **/
13193uint32_t
13194lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13195{
13196 LPFC_MBOXQ_t *mbox;
13197 int rc, length, status = 0;
13198 uint32_t shdr_status, shdr_add_status;
13199 union lpfc_sli4_cfg_shdr *shdr;
13200
James Smart2e90f4b2011-12-13 13:22:37 -050013201 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013202 if (!wq)
13203 return -ENODEV;
13204 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13205 if (!mbox)
13206 return -ENOMEM;
13207 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13208 sizeof(struct lpfc_sli4_cfg_mhdr));
13209 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13210 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13211 length, LPFC_SLI4_MBX_EMBED);
13212 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13213 wq->queue_id);
13214 mbox->vport = wq->phba->pport;
13215 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13216 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13217 shdr = (union lpfc_sli4_cfg_shdr *)
13218 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13219 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13220 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13221 if (shdr_status || shdr_add_status || rc) {
13222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13223 "2508 WQ_DESTROY mailbox failed with "
13224 "status x%x add_status x%x, mbx status x%x\n",
13225 shdr_status, shdr_add_status, rc);
13226 status = -ENXIO;
13227 }
13228 /* Remove wq from any list */
13229 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013230 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013231 return status;
13232}
13233
13234/**
13235 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13236 * @rq: The queue structure associated with the queue to destroy.
13237 *
13238 * This function destroys a queue, as detailed in @rq by sending an mailbox
13239 * command, specific to the type of queue, to the HBA.
13240 *
13241 * The @rq struct is used to get the queue ID of the queue to destroy.
13242 *
13243 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013244 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013245 **/
13246uint32_t
13247lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13248 struct lpfc_queue *drq)
13249{
13250 LPFC_MBOXQ_t *mbox;
13251 int rc, length, status = 0;
13252 uint32_t shdr_status, shdr_add_status;
13253 union lpfc_sli4_cfg_shdr *shdr;
13254
James Smart2e90f4b2011-12-13 13:22:37 -050013255 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013256 if (!hrq || !drq)
13257 return -ENODEV;
13258 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13259 if (!mbox)
13260 return -ENOMEM;
13261 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013262 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013263 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13264 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13265 length, LPFC_SLI4_MBX_EMBED);
13266 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13267 hrq->queue_id);
13268 mbox->vport = hrq->phba->pport;
13269 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13270 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13271 /* The IOCTL status is embedded in the mailbox subheader. */
13272 shdr = (union lpfc_sli4_cfg_shdr *)
13273 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13274 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13275 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13276 if (shdr_status || shdr_add_status || rc) {
13277 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13278 "2509 RQ_DESTROY mailbox failed with "
13279 "status x%x add_status x%x, mbx status x%x\n",
13280 shdr_status, shdr_add_status, rc);
13281 if (rc != MBX_TIMEOUT)
13282 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13283 return -ENXIO;
13284 }
13285 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13286 drq->queue_id);
13287 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13288 shdr = (union lpfc_sli4_cfg_shdr *)
13289 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13290 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13291 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13292 if (shdr_status || shdr_add_status || rc) {
13293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13294 "2510 RQ_DESTROY mailbox failed with "
13295 "status x%x add_status x%x, mbx status x%x\n",
13296 shdr_status, shdr_add_status, rc);
13297 status = -ENXIO;
13298 }
13299 list_del_init(&hrq->list);
13300 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013301 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013302 return status;
13303}
13304
13305/**
13306 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13307 * @phba: The virtual port for which this call being executed.
13308 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13309 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13310 * @xritag: the xritag that ties this io to the SGL pages.
13311 *
13312 * This routine will post the sgl pages for the IO that has the xritag
13313 * that is in the iocbq structure. The xritag is assigned during iocbq
13314 * creation and persists for as long as the driver is loaded.
13315 * if the caller has fewer than 256 scatter gather segments to map then
13316 * pdma_phys_addr1 should be 0.
13317 * If the caller needs to map more than 256 scatter gather segment then
13318 * pdma_phys_addr1 should be a valid physical address.
13319 * physical address for SGLs must be 64 byte aligned.
13320 * If you are going to map 2 SGL's then the first one must have 256 entries
13321 * the second sgl can have between 1 and 256 entries.
13322 *
13323 * Return codes:
13324 * 0 - Success
13325 * -ENXIO, -ENOMEM - Failure
13326 **/
13327int
13328lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13329 dma_addr_t pdma_phys_addr0,
13330 dma_addr_t pdma_phys_addr1,
13331 uint16_t xritag)
13332{
13333 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13334 LPFC_MBOXQ_t *mbox;
13335 int rc;
13336 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013337 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013338 union lpfc_sli4_cfg_shdr *shdr;
13339
13340 if (xritag == NO_XRI) {
13341 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13342 "0364 Invalid param:\n");
13343 return -EINVAL;
13344 }
13345
13346 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13347 if (!mbox)
13348 return -ENOMEM;
13349
13350 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13351 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13352 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013353 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013354
13355 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13356 &mbox->u.mqe.un.post_sgl_pages;
13357 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13358 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13359
13360 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13361 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13362 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13363 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13364
13365 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13366 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13367 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13368 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13369 if (!phba->sli4_hba.intr_enable)
13370 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013371 else {
James Smarta183a152011-10-10 21:32:43 -040013372 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013373 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13374 }
James Smart4f774512009-05-22 14:52:35 -040013375 /* The IOCTL status is embedded in the mailbox subheader. */
13376 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13377 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13378 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13379 if (rc != MBX_TIMEOUT)
13380 mempool_free(mbox, phba->mbox_mem_pool);
13381 if (shdr_status || shdr_add_status || rc) {
13382 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13383 "2511 POST_SGL mailbox failed with "
13384 "status x%x add_status x%x, mbx status x%x\n",
13385 shdr_status, shdr_add_status, rc);
13386 rc = -ENXIO;
13387 }
13388 return 0;
13389}
James Smart4f774512009-05-22 14:52:35 -040013390
13391/**
James Smart88a2cfb2011-07-22 18:36:33 -040013392 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013393 * @phba: pointer to lpfc hba data structure.
13394 *
13395 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013396 * HBA consistent with the SLI-4 interface spec. This routine
13397 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13398 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013399 *
James Smart88a2cfb2011-07-22 18:36:33 -040013400 * Returns
13401 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13402 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13403 **/
James Smart6d368e52011-05-24 11:44:12 -040013404uint16_t
13405lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13406{
13407 unsigned long xri;
13408
13409 /*
13410 * Fetch the next logical xri. Because this index is logical,
13411 * the driver starts at 0 each time.
13412 */
13413 spin_lock_irq(&phba->hbalock);
13414 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13415 phba->sli4_hba.max_cfg_param.max_xri, 0);
13416 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13417 spin_unlock_irq(&phba->hbalock);
13418 return NO_XRI;
13419 } else {
13420 set_bit(xri, phba->sli4_hba.xri_bmask);
13421 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013422 }
James Smart6d368e52011-05-24 11:44:12 -040013423 spin_unlock_irq(&phba->hbalock);
13424 return xri;
13425}
13426
13427/**
13428 * lpfc_sli4_free_xri - Release an xri for reuse.
13429 * @phba: pointer to lpfc hba data structure.
13430 *
13431 * This routine is invoked to release an xri to the pool of
13432 * available rpis maintained by the driver.
13433 **/
13434void
13435__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13436{
13437 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013438 phba->sli4_hba.max_cfg_param.xri_used--;
13439 }
13440}
13441
13442/**
13443 * lpfc_sli4_free_xri - Release an xri for reuse.
13444 * @phba: pointer to lpfc hba data structure.
13445 *
13446 * This routine is invoked to release an xri to the pool of
13447 * available rpis maintained by the driver.
13448 **/
13449void
13450lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13451{
13452 spin_lock_irq(&phba->hbalock);
13453 __lpfc_sli4_free_xri(phba, xri);
13454 spin_unlock_irq(&phba->hbalock);
13455}
13456
13457/**
James Smart4f774512009-05-22 14:52:35 -040013458 * lpfc_sli4_next_xritag - Get an xritag for the io
13459 * @phba: Pointer to HBA context object.
13460 *
13461 * This function gets an xritag for the iocb. If there is no unused xritag
13462 * it will return 0xffff.
13463 * The function returns the allocated xritag if successful, else returns zero.
13464 * Zero is not a valid xritag.
13465 * The caller is not required to hold any lock.
13466 **/
13467uint16_t
13468lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13469{
James Smart6d368e52011-05-24 11:44:12 -040013470 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013471
James Smart6d368e52011-05-24 11:44:12 -040013472 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013473 if (xri_index == NO_XRI)
13474 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13475 "2004 Failed to allocate XRI.last XRITAG is %d"
13476 " Max XRI is %d, Used XRI is %d\n",
13477 xri_index,
13478 phba->sli4_hba.max_cfg_param.max_xri,
13479 phba->sli4_hba.max_cfg_param.xri_used);
13480 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013481}
13482
13483/**
James Smart6d368e52011-05-24 11:44:12 -040013484 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013485 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013486 * @post_sgl_list: pointer to els sgl entry list.
13487 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013488 *
13489 * This routine is invoked to post a block of driver's sgl pages to the
13490 * HBA using non-embedded mailbox command. No Lock is held. This routine
13491 * is only called when the driver is loading and after all IO has been
13492 * stopped.
13493 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013494static int
13495lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13496 struct list_head *post_sgl_list,
13497 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013498{
James Smart8a9d2e82012-05-09 21:16:12 -040013499 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013500 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13501 struct sgl_page_pairs *sgl_pg_pairs;
13502 void *viraddr;
13503 LPFC_MBOXQ_t *mbox;
13504 uint32_t reqlen, alloclen, pg_pairs;
13505 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013506 uint16_t xritag_start = 0;
13507 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013508 uint32_t shdr_status, shdr_add_status;
13509 union lpfc_sli4_cfg_shdr *shdr;
13510
James Smart8a9d2e82012-05-09 21:16:12 -040013511 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013512 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013513 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013514 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13515 "2559 Block sgl registration required DMA "
13516 "size (%d) great than a page\n", reqlen);
13517 return -ENOMEM;
13518 }
13519 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013520 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013521 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013522
13523 /* Allocate DMA memory and set up the non-embedded mailbox command */
13524 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13525 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13526 LPFC_SLI4_MBX_NEMBED);
13527
13528 if (alloclen < reqlen) {
13529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13530 "0285 Allocated DMA memory size (%d) is "
13531 "less than the requested DMA memory "
13532 "size (%d)\n", alloclen, reqlen);
13533 lpfc_sli4_mbox_cmd_free(phba, mbox);
13534 return -ENOMEM;
13535 }
James Smart4f774512009-05-22 14:52:35 -040013536 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013537 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013538 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13539 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13540
James Smart8a9d2e82012-05-09 21:16:12 -040013541 pg_pairs = 0;
13542 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013543 /* Set up the sge entry */
13544 sgl_pg_pairs->sgl_pg0_addr_lo =
13545 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13546 sgl_pg_pairs->sgl_pg0_addr_hi =
13547 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13548 sgl_pg_pairs->sgl_pg1_addr_lo =
13549 cpu_to_le32(putPaddrLow(0));
13550 sgl_pg_pairs->sgl_pg1_addr_hi =
13551 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013552
James Smart4f774512009-05-22 14:52:35 -040013553 /* Keep the first xritag on the list */
13554 if (pg_pairs == 0)
13555 xritag_start = sglq_entry->sli4_xritag;
13556 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013557 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013558 }
James Smart6d368e52011-05-24 11:44:12 -040013559
13560 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013561 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013562 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013563 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013564 if (!phba->sli4_hba.intr_enable)
13565 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13566 else {
James Smarta183a152011-10-10 21:32:43 -040013567 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013568 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13569 }
13570 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13571 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13572 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13573 if (rc != MBX_TIMEOUT)
13574 lpfc_sli4_mbox_cmd_free(phba, mbox);
13575 if (shdr_status || shdr_add_status || rc) {
13576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13577 "2513 POST_SGL_BLOCK mailbox command failed "
13578 "status x%x add_status x%x mbx status x%x\n",
13579 shdr_status, shdr_add_status, rc);
13580 rc = -ENXIO;
13581 }
13582 return rc;
13583}
13584
13585/**
13586 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13587 * @phba: pointer to lpfc hba data structure.
13588 * @sblist: pointer to scsi buffer list.
13589 * @count: number of scsi buffers on the list.
13590 *
13591 * This routine is invoked to post a block of @count scsi sgl pages from a
13592 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13593 * No Lock is held.
13594 *
13595 **/
13596int
James Smart8a9d2e82012-05-09 21:16:12 -040013597lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13598 struct list_head *sblist,
13599 int count)
James Smart4f774512009-05-22 14:52:35 -040013600{
13601 struct lpfc_scsi_buf *psb;
13602 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13603 struct sgl_page_pairs *sgl_pg_pairs;
13604 void *viraddr;
13605 LPFC_MBOXQ_t *mbox;
13606 uint32_t reqlen, alloclen, pg_pairs;
13607 uint32_t mbox_tmo;
13608 uint16_t xritag_start = 0;
13609 int rc = 0;
13610 uint32_t shdr_status, shdr_add_status;
13611 dma_addr_t pdma_phys_bpl1;
13612 union lpfc_sli4_cfg_shdr *shdr;
13613
13614 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013615 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013616 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013617 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013618 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13619 "0217 Block sgl registration required DMA "
13620 "size (%d) great than a page\n", reqlen);
13621 return -ENOMEM;
13622 }
13623 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13624 if (!mbox) {
13625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13626 "0283 Failed to allocate mbox cmd memory\n");
13627 return -ENOMEM;
13628 }
13629
13630 /* Allocate DMA memory and set up the non-embedded mailbox command */
13631 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13632 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13633 LPFC_SLI4_MBX_NEMBED);
13634
13635 if (alloclen < reqlen) {
13636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13637 "2561 Allocated DMA memory size (%d) is "
13638 "less than the requested DMA memory "
13639 "size (%d)\n", alloclen, reqlen);
13640 lpfc_sli4_mbox_cmd_free(phba, mbox);
13641 return -ENOMEM;
13642 }
James Smart6d368e52011-05-24 11:44:12 -040013643
James Smart4f774512009-05-22 14:52:35 -040013644 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013645 viraddr = mbox->sge_array->addr[0];
13646
13647 /* Set up the SGL pages in the non-embedded DMA pages */
13648 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13649 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13650
13651 pg_pairs = 0;
13652 list_for_each_entry(psb, sblist, list) {
13653 /* Set up the sge entry */
13654 sgl_pg_pairs->sgl_pg0_addr_lo =
13655 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13656 sgl_pg_pairs->sgl_pg0_addr_hi =
13657 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13658 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13659 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13660 else
13661 pdma_phys_bpl1 = 0;
13662 sgl_pg_pairs->sgl_pg1_addr_lo =
13663 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13664 sgl_pg_pairs->sgl_pg1_addr_hi =
13665 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13666 /* Keep the first xritag on the list */
13667 if (pg_pairs == 0)
13668 xritag_start = psb->cur_iocbq.sli4_xritag;
13669 sgl_pg_pairs++;
13670 pg_pairs++;
13671 }
13672 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13673 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13674 /* Perform endian conversion if necessary */
13675 sgl->word0 = cpu_to_le32(sgl->word0);
13676
13677 if (!phba->sli4_hba.intr_enable)
13678 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13679 else {
James Smarta183a152011-10-10 21:32:43 -040013680 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013681 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13682 }
13683 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13684 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13685 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13686 if (rc != MBX_TIMEOUT)
13687 lpfc_sli4_mbox_cmd_free(phba, mbox);
13688 if (shdr_status || shdr_add_status || rc) {
13689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13690 "2564 POST_SGL_BLOCK mailbox command failed "
13691 "status x%x add_status x%x mbx status x%x\n",
13692 shdr_status, shdr_add_status, rc);
13693 rc = -ENXIO;
13694 }
13695 return rc;
13696}
13697
13698/**
13699 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13700 * @phba: pointer to lpfc_hba struct that the frame was received on
13701 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13702 *
13703 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13704 * valid type of frame that the LPFC driver will handle. This function will
13705 * return a zero if the frame is a valid frame or a non zero value when the
13706 * frame does not pass the check.
13707 **/
13708static int
13709lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13710{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013711 /* make rctl_names static to save stack space */
13712 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013713 char *type_names[] = FC_TYPE_NAMES_INIT;
13714 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013715 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013716
13717 switch (fc_hdr->fh_r_ctl) {
13718 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13719 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13720 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13721 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13722 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13723 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13724 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13725 case FC_RCTL_DD_CMD_STATUS: /* command status */
13726 case FC_RCTL_ELS_REQ: /* extended link services request */
13727 case FC_RCTL_ELS_REP: /* extended link services reply */
13728 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13729 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13730 case FC_RCTL_BA_NOP: /* basic link service NOP */
13731 case FC_RCTL_BA_ABTS: /* basic link service abort */
13732 case FC_RCTL_BA_RMC: /* remove connection */
13733 case FC_RCTL_BA_ACC: /* basic accept */
13734 case FC_RCTL_BA_RJT: /* basic reject */
13735 case FC_RCTL_BA_PRMT:
13736 case FC_RCTL_ACK_1: /* acknowledge_1 */
13737 case FC_RCTL_ACK_0: /* acknowledge_0 */
13738 case FC_RCTL_P_RJT: /* port reject */
13739 case FC_RCTL_F_RJT: /* fabric reject */
13740 case FC_RCTL_P_BSY: /* port busy */
13741 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13742 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13743 case FC_RCTL_LCR: /* link credit reset */
13744 case FC_RCTL_END: /* end */
13745 break;
13746 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13747 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13748 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13749 return lpfc_fc_frame_check(phba, fc_hdr);
13750 default:
13751 goto drop;
13752 }
13753 switch (fc_hdr->fh_type) {
13754 case FC_TYPE_BLS:
13755 case FC_TYPE_ELS:
13756 case FC_TYPE_FCP:
13757 case FC_TYPE_CT:
13758 break;
13759 case FC_TYPE_IP:
13760 case FC_TYPE_ILS:
13761 default:
13762 goto drop;
13763 }
James Smart546fc852011-03-11 16:06:29 -050013764
James Smart4f774512009-05-22 14:52:35 -040013765 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013766 "2538 Received frame rctl:%s type:%s "
13767 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013768 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013769 type_names[fc_hdr->fh_type],
13770 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13771 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13772 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013773 return 0;
13774drop:
13775 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13776 "2539 Dropped frame rctl:%s type:%s\n",
13777 rctl_names[fc_hdr->fh_r_ctl],
13778 type_names[fc_hdr->fh_type]);
13779 return 1;
13780}
13781
13782/**
13783 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13784 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13785 *
13786 * This function processes the FC header to retrieve the VFI from the VF
13787 * header, if one exists. This function will return the VFI if one exists
13788 * or 0 if no VSAN Header exists.
13789 **/
13790static uint32_t
13791lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13792{
13793 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13794
13795 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13796 return 0;
13797 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13798}
13799
13800/**
13801 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13802 * @phba: Pointer to the HBA structure to search for the vport on
13803 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13804 * @fcfi: The FC Fabric ID that the frame came from
13805 *
13806 * This function searches the @phba for a vport that matches the content of the
13807 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13808 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13809 * returns the matching vport pointer or NULL if unable to match frame to a
13810 * vport.
13811 **/
13812static struct lpfc_vport *
13813lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13814 uint16_t fcfi)
13815{
13816 struct lpfc_vport **vports;
13817 struct lpfc_vport *vport = NULL;
13818 int i;
13819 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13820 fc_hdr->fh_d_id[1] << 8 |
13821 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013822
James Smartbf086112011-08-21 21:48:13 -040013823 if (did == Fabric_DID)
13824 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013825 if ((phba->pport->fc_flag & FC_PT2PT) &&
13826 !(phba->link_state == LPFC_HBA_READY))
13827 return phba->pport;
13828
James Smart4f774512009-05-22 14:52:35 -040013829 vports = lpfc_create_vport_work_array(phba);
13830 if (vports != NULL)
13831 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13832 if (phba->fcf.fcfi == fcfi &&
13833 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13834 vports[i]->fc_myDID == did) {
13835 vport = vports[i];
13836 break;
13837 }
13838 }
13839 lpfc_destroy_vport_work_array(phba, vports);
13840 return vport;
13841}
13842
13843/**
James Smart45ed1192009-10-02 15:17:02 -040013844 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13845 * @vport: The vport to work on.
13846 *
13847 * This function updates the receive sequence time stamp for this vport. The
13848 * receive sequence time stamp indicates the time that the last frame of the
13849 * the sequence that has been idle for the longest amount of time was received.
13850 * the driver uses this time stamp to indicate if any received sequences have
13851 * timed out.
13852 **/
13853void
13854lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13855{
13856 struct lpfc_dmabuf *h_buf;
13857 struct hbq_dmabuf *dmabuf = NULL;
13858
13859 /* get the oldest sequence on the rcv list */
13860 h_buf = list_get_first(&vport->rcv_buffer_list,
13861 struct lpfc_dmabuf, list);
13862 if (!h_buf)
13863 return;
13864 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13865 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13866}
13867
13868/**
13869 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13870 * @vport: The vport that the received sequences were sent to.
13871 *
13872 * This function cleans up all outstanding received sequences. This is called
13873 * by the driver when a link event or user action invalidates all the received
13874 * sequences.
13875 **/
13876void
13877lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13878{
13879 struct lpfc_dmabuf *h_buf, *hnext;
13880 struct lpfc_dmabuf *d_buf, *dnext;
13881 struct hbq_dmabuf *dmabuf = NULL;
13882
13883 /* start with the oldest sequence on the rcv list */
13884 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13885 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13886 list_del_init(&dmabuf->hbuf.list);
13887 list_for_each_entry_safe(d_buf, dnext,
13888 &dmabuf->dbuf.list, list) {
13889 list_del_init(&d_buf->list);
13890 lpfc_in_buf_free(vport->phba, d_buf);
13891 }
13892 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13893 }
13894}
13895
13896/**
13897 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13898 * @vport: The vport that the received sequences were sent to.
13899 *
13900 * This function determines whether any received sequences have timed out by
13901 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13902 * indicates that there is at least one timed out sequence this routine will
13903 * go through the received sequences one at a time from most inactive to most
13904 * active to determine which ones need to be cleaned up. Once it has determined
13905 * that a sequence needs to be cleaned up it will simply free up the resources
13906 * without sending an abort.
13907 **/
13908void
13909lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13910{
13911 struct lpfc_dmabuf *h_buf, *hnext;
13912 struct lpfc_dmabuf *d_buf, *dnext;
13913 struct hbq_dmabuf *dmabuf = NULL;
13914 unsigned long timeout;
13915 int abort_count = 0;
13916
13917 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13918 vport->rcv_buffer_time_stamp);
13919 if (list_empty(&vport->rcv_buffer_list) ||
13920 time_before(jiffies, timeout))
13921 return;
13922 /* start with the oldest sequence on the rcv list */
13923 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13924 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13925 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13926 dmabuf->time_stamp);
13927 if (time_before(jiffies, timeout))
13928 break;
13929 abort_count++;
13930 list_del_init(&dmabuf->hbuf.list);
13931 list_for_each_entry_safe(d_buf, dnext,
13932 &dmabuf->dbuf.list, list) {
13933 list_del_init(&d_buf->list);
13934 lpfc_in_buf_free(vport->phba, d_buf);
13935 }
13936 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13937 }
13938 if (abort_count)
13939 lpfc_update_rcv_time_stamp(vport);
13940}
13941
13942/**
James Smart4f774512009-05-22 14:52:35 -040013943 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13944 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13945 *
13946 * This function searches through the existing incomplete sequences that have
13947 * been sent to this @vport. If the frame matches one of the incomplete
13948 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13949 * make up that sequence. If no sequence is found that matches this frame then
13950 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13951 * This function returns a pointer to the first dmabuf in the sequence list that
13952 * the frame was linked to.
13953 **/
13954static struct hbq_dmabuf *
13955lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13956{
13957 struct fc_frame_header *new_hdr;
13958 struct fc_frame_header *temp_hdr;
13959 struct lpfc_dmabuf *d_buf;
13960 struct lpfc_dmabuf *h_buf;
13961 struct hbq_dmabuf *seq_dmabuf = NULL;
13962 struct hbq_dmabuf *temp_dmabuf = NULL;
13963
James Smart4d9ab992009-10-02 15:16:39 -040013964 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013965 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013966 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13967 /* Use the hdr_buf to find the sequence that this frame belongs to */
13968 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13969 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13970 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13971 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13972 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13973 continue;
13974 /* found a pending sequence that matches this frame */
13975 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13976 break;
13977 }
13978 if (!seq_dmabuf) {
13979 /*
13980 * This indicates first frame received for this sequence.
13981 * Queue the buffer on the vport's rcv_buffer_list.
13982 */
13983 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013984 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013985 return dmabuf;
13986 }
13987 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013988 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13989 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013990 list_del_init(&seq_dmabuf->hbuf.list);
13991 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13992 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013993 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013994 return dmabuf;
13995 }
James Smart45ed1192009-10-02 15:17:02 -040013996 /* move this sequence to the tail to indicate a young sequence */
13997 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13998 seq_dmabuf->time_stamp = jiffies;
13999 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014000 if (list_empty(&seq_dmabuf->dbuf.list)) {
14001 temp_hdr = dmabuf->hbuf.virt;
14002 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14003 return seq_dmabuf;
14004 }
James Smart4f774512009-05-22 14:52:35 -040014005 /* find the correct place in the sequence to insert this frame */
14006 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14007 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14008 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14009 /*
14010 * If the frame's sequence count is greater than the frame on
14011 * the list then insert the frame right after this frame
14012 */
James Smarteeead812009-12-21 17:01:23 -050014013 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14014 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014015 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14016 return seq_dmabuf;
14017 }
14018 }
14019 return NULL;
14020}
14021
14022/**
James Smart6669f9b2009-10-02 15:16:45 -040014023 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14024 * @vport: pointer to a vitural port
14025 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14026 *
14027 * This function tries to abort from the partially assembed sequence, described
14028 * by the information from basic abbort @dmabuf. It checks to see whether such
14029 * partially assembled sequence held by the driver. If so, it shall free up all
14030 * the frames from the partially assembled sequence.
14031 *
14032 * Return
14033 * true -- if there is matching partially assembled sequence present and all
14034 * the frames freed with the sequence;
14035 * false -- if there is no matching partially assembled sequence present so
14036 * nothing got aborted in the lower layer driver
14037 **/
14038static bool
14039lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14040 struct hbq_dmabuf *dmabuf)
14041{
14042 struct fc_frame_header *new_hdr;
14043 struct fc_frame_header *temp_hdr;
14044 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14045 struct hbq_dmabuf *seq_dmabuf = NULL;
14046
14047 /* Use the hdr_buf to find the sequence that matches this frame */
14048 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14049 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14050 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14051 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14052 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14053 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14054 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14055 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14056 continue;
14057 /* found a pending sequence that matches this frame */
14058 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14059 break;
14060 }
14061
14062 /* Free up all the frames from the partially assembled sequence */
14063 if (seq_dmabuf) {
14064 list_for_each_entry_safe(d_buf, n_buf,
14065 &seq_dmabuf->dbuf.list, list) {
14066 list_del_init(&d_buf->list);
14067 lpfc_in_buf_free(vport->phba, d_buf);
14068 }
14069 return true;
14070 }
14071 return false;
14072}
14073
14074/**
James Smart546fc852011-03-11 16:06:29 -050014075 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014076 * @phba: Pointer to HBA context object.
14077 * @cmd_iocbq: pointer to the command iocbq structure.
14078 * @rsp_iocbq: pointer to the response iocbq structure.
14079 *
James Smart546fc852011-03-11 16:06:29 -050014080 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014081 * event. It properly releases the memory allocated to the sequence abort
14082 * accept iocb.
14083 **/
14084static void
James Smart546fc852011-03-11 16:06:29 -050014085lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014086 struct lpfc_iocbq *cmd_iocbq,
14087 struct lpfc_iocbq *rsp_iocbq)
14088{
14089 if (cmd_iocbq)
14090 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014091
14092 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14093 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14094 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14095 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14096 rsp_iocbq->iocb.ulpStatus,
14097 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014098}
14099
14100/**
James Smart6d368e52011-05-24 11:44:12 -040014101 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14102 * @phba: Pointer to HBA context object.
14103 * @xri: xri id in transaction.
14104 *
14105 * This function validates the xri maps to the known range of XRIs allocated an
14106 * used by the driver.
14107 **/
James Smart7851fe22011-07-22 18:36:52 -040014108uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014109lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14110 uint16_t xri)
14111{
14112 int i;
14113
14114 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14115 if (xri == phba->sli4_hba.xri_ids[i])
14116 return i;
14117 }
14118 return NO_XRI;
14119}
14120
James Smart6d368e52011-05-24 11:44:12 -040014121/**
James Smart546fc852011-03-11 16:06:29 -050014122 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014123 * @phba: Pointer to HBA context object.
14124 * @fc_hdr: pointer to a FC frame header.
14125 *
James Smart546fc852011-03-11 16:06:29 -050014126 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014127 * event after aborting the sequence handling.
14128 **/
14129static void
James Smart546fc852011-03-11 16:06:29 -050014130lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014131 struct fc_frame_header *fc_hdr)
14132{
14133 struct lpfc_iocbq *ctiocb = NULL;
14134 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014135 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014136 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014137 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014138 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014139
14140 if (!lpfc_is_link_up(phba))
14141 return;
14142
14143 sid = sli4_sid_from_fc_hdr(fc_hdr);
14144 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014145 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014146
14147 ndlp = lpfc_findnode_did(phba->pport, sid);
14148 if (!ndlp) {
14149 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14150 "1268 Find ndlp returned NULL for oxid:x%x "
14151 "SID:x%x\n", oxid, sid);
14152 return;
14153 }
14154
James Smart546fc852011-03-11 16:06:29 -050014155 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014156 ctiocb = lpfc_sli_get_iocbq(phba);
14157 if (!ctiocb)
14158 return;
14159
James Smart5ffc2662009-11-18 15:39:44 -050014160 /* Extract the F_CTL field from FC_HDR */
14161 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14162
James Smart6669f9b2009-10-02 15:16:45 -040014163 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014164 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014165 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014166 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14167 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14168 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14169
14170 /* Fill in the rest of iocb fields */
14171 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14172 icmd->ulpBdeCount = 0;
14173 icmd->ulpLe = 1;
14174 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014175 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014176 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014177
James Smart6669f9b2009-10-02 15:16:45 -040014178 ctiocb->iocb_cmpl = NULL;
14179 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014180 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014181 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014182 ctiocb->sli4_xritag = NO_XRI;
14183
James Smartee0f4fe2012-05-09 21:19:14 -040014184 if (fctl & FC_FC_EX_CTX)
14185 /* Exchange responder sent the abort so we
14186 * own the oxid.
14187 */
14188 xri = oxid;
14189 else
14190 xri = rxid;
14191 lxri = lpfc_sli4_xri_inrange(phba, xri);
14192 if (lxri != NO_XRI)
14193 lpfc_set_rrq_active(phba, ndlp, lxri,
14194 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014195 /* If the oxid maps to the FCP XRI range or if it is out of range,
14196 * send a BLS_RJT. The driver no longer has that exchange.
14197 * Override the IOCB for a BA_RJT.
14198 */
James Smartee0f4fe2012-05-09 21:19:14 -040014199 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014200 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014201 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014202 phba->sli4_hba.max_cfg_param.xri_base)) {
14203 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14204 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14205 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14206 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14207 }
James Smart6669f9b2009-10-02 15:16:45 -040014208
James Smart5ffc2662009-11-18 15:39:44 -050014209 if (fctl & FC_FC_EX_CTX) {
14210 /* ABTS sent by responder to CT exchange, construction
14211 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14212 * field and RX_ID from ABTS for RX_ID field.
14213 */
James Smart546fc852011-03-11 16:06:29 -050014214 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014215 } else {
14216 /* ABTS sent by initiator to CT exchange, construction
14217 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014218 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014219 */
James Smart546fc852011-03-11 16:06:29 -050014220 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014221 }
James Smartf09c3ac2012-03-01 22:33:29 -050014222 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014223 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014224
James Smart546fc852011-03-11 16:06:29 -050014225 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014226 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014227 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14228 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14229
14230 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14231 if (rc == IOCB_ERROR) {
14232 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14233 "2925 Failed to issue CT ABTS RSP x%x on "
14234 "xri x%x, Data x%x\n",
14235 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14236 phba->link_state);
14237 lpfc_sli_release_iocbq(phba, ctiocb);
14238 }
James Smart6669f9b2009-10-02 15:16:45 -040014239}
14240
14241/**
14242 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14243 * @vport: Pointer to the vport on which this sequence was received
14244 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14245 *
14246 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14247 * receive sequence is only partially assembed by the driver, it shall abort
14248 * the partially assembled frames for the sequence. Otherwise, if the
14249 * unsolicited receive sequence has been completely assembled and passed to
14250 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14251 * unsolicited sequence has been aborted. After that, it will issue a basic
14252 * accept to accept the abort.
14253 **/
14254void
14255lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14256 struct hbq_dmabuf *dmabuf)
14257{
14258 struct lpfc_hba *phba = vport->phba;
14259 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014260 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014261 bool abts_par;
14262
James Smart6669f9b2009-10-02 15:16:45 -040014263 /* Make a copy of fc_hdr before the dmabuf being released */
14264 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014265 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014266
James Smart5ffc2662009-11-18 15:39:44 -050014267 if (fctl & FC_FC_EX_CTX) {
14268 /*
14269 * ABTS sent by responder to exchange, just free the buffer
14270 */
James Smart6669f9b2009-10-02 15:16:45 -040014271 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014272 } else {
14273 /*
14274 * ABTS sent by initiator to exchange, need to do cleanup
14275 */
14276 /* Try to abort partially assembled seq */
14277 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14278
14279 /* Send abort to ULP if partially seq abort failed */
14280 if (abts_par == false)
14281 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14282 else
14283 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14284 }
James Smart6669f9b2009-10-02 15:16:45 -040014285 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014286 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014287}
14288
14289/**
James Smart4f774512009-05-22 14:52:35 -040014290 * lpfc_seq_complete - Indicates if a sequence is complete
14291 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14292 *
14293 * This function checks the sequence, starting with the frame described by
14294 * @dmabuf, to see if all the frames associated with this sequence are present.
14295 * the frames associated with this sequence are linked to the @dmabuf using the
14296 * dbuf list. This function looks for two major things. 1) That the first frame
14297 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14298 * set. 3) That there are no holes in the sequence count. The function will
14299 * return 1 when the sequence is complete, otherwise it will return 0.
14300 **/
14301static int
14302lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14303{
14304 struct fc_frame_header *hdr;
14305 struct lpfc_dmabuf *d_buf;
14306 struct hbq_dmabuf *seq_dmabuf;
14307 uint32_t fctl;
14308 int seq_count = 0;
14309
14310 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14311 /* make sure first fame of sequence has a sequence count of zero */
14312 if (hdr->fh_seq_cnt != seq_count)
14313 return 0;
14314 fctl = (hdr->fh_f_ctl[0] << 16 |
14315 hdr->fh_f_ctl[1] << 8 |
14316 hdr->fh_f_ctl[2]);
14317 /* If last frame of sequence we can return success. */
14318 if (fctl & FC_FC_END_SEQ)
14319 return 1;
14320 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14321 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14322 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14323 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014324 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014325 return 0;
14326 fctl = (hdr->fh_f_ctl[0] << 16 |
14327 hdr->fh_f_ctl[1] << 8 |
14328 hdr->fh_f_ctl[2]);
14329 /* If last frame of sequence we can return success. */
14330 if (fctl & FC_FC_END_SEQ)
14331 return 1;
14332 }
14333 return 0;
14334}
14335
14336/**
14337 * lpfc_prep_seq - Prep sequence for ULP processing
14338 * @vport: Pointer to the vport on which this sequence was received
14339 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14340 *
14341 * This function takes a sequence, described by a list of frames, and creates
14342 * a list of iocbq structures to describe the sequence. This iocbq list will be
14343 * used to issue to the generic unsolicited sequence handler. This routine
14344 * returns a pointer to the first iocbq in the list. If the function is unable
14345 * to allocate an iocbq then it throw out the received frames that were not
14346 * able to be described and return a pointer to the first iocbq. If unable to
14347 * allocate any iocbqs (including the first) this function will return NULL.
14348 **/
14349static struct lpfc_iocbq *
14350lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14351{
James Smart7851fe22011-07-22 18:36:52 -040014352 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014353 struct lpfc_dmabuf *d_buf, *n_buf;
14354 struct lpfc_iocbq *first_iocbq, *iocbq;
14355 struct fc_frame_header *fc_hdr;
14356 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014357 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014358 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014359
14360 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14361 /* remove from receive buffer list */
14362 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014363 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014364 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014365 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014366 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014367 /* Get an iocbq struct to fill in. */
14368 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14369 if (first_iocbq) {
14370 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014371 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014372 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014373
14374 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14375 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14376 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14377 first_iocbq->iocb.un.rcvels.parmRo =
14378 sli4_did_from_fc_hdr(fc_hdr);
14379 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14380 } else
14381 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014382 first_iocbq->iocb.ulpContext = NO_XRI;
14383 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14384 be16_to_cpu(fc_hdr->fh_ox_id);
14385 /* iocbq is prepped for internal consumption. Physical vpi. */
14386 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14387 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014388 /* put the first buffer into the first IOCBq */
14389 first_iocbq->context2 = &seq_dmabuf->dbuf;
14390 first_iocbq->context3 = NULL;
14391 first_iocbq->iocb.ulpBdeCount = 1;
14392 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14393 LPFC_DATA_BUF_SIZE;
14394 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014395 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014396 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014397 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014398 }
14399 iocbq = first_iocbq;
14400 /*
14401 * Each IOCBq can have two Buffers assigned, so go through the list
14402 * of buffers for this sequence and save two buffers in each IOCBq
14403 */
14404 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14405 if (!iocbq) {
14406 lpfc_in_buf_free(vport->phba, d_buf);
14407 continue;
14408 }
14409 if (!iocbq->context3) {
14410 iocbq->context3 = d_buf;
14411 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014412 pbde = (struct ulp_bde64 *)
14413 &iocbq->iocb.unsli3.sli3Words[4];
14414 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014415
14416 /* We need to get the size out of the right CQE */
14417 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14418 len = bf_get(lpfc_rcqe_length,
14419 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14420 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14421 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014422 } else {
14423 iocbq = lpfc_sli_get_iocbq(vport->phba);
14424 if (!iocbq) {
14425 if (first_iocbq) {
14426 first_iocbq->iocb.ulpStatus =
14427 IOSTAT_FCP_RSP_ERROR;
14428 first_iocbq->iocb.un.ulpWord[4] =
14429 IOERR_NO_RESOURCES;
14430 }
14431 lpfc_in_buf_free(vport->phba, d_buf);
14432 continue;
14433 }
14434 iocbq->context2 = d_buf;
14435 iocbq->context3 = NULL;
14436 iocbq->iocb.ulpBdeCount = 1;
14437 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14438 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014439
14440 /* We need to get the size out of the right CQE */
14441 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14442 len = bf_get(lpfc_rcqe_length,
14443 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14444 tot_len += len;
14445 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14446
James Smart4f774512009-05-22 14:52:35 -040014447 iocbq->iocb.un.rcvels.remoteID = sid;
14448 list_add_tail(&iocbq->list, &first_iocbq->list);
14449 }
14450 }
14451 return first_iocbq;
14452}
14453
James Smart6669f9b2009-10-02 15:16:45 -040014454static void
14455lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14456 struct hbq_dmabuf *seq_dmabuf)
14457{
14458 struct fc_frame_header *fc_hdr;
14459 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14460 struct lpfc_hba *phba = vport->phba;
14461
14462 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14463 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14464 if (!iocbq) {
14465 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14466 "2707 Ring %d handler: Failed to allocate "
14467 "iocb Rctl x%x Type x%x received\n",
14468 LPFC_ELS_RING,
14469 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14470 return;
14471 }
14472 if (!lpfc_complete_unsol_iocb(phba,
14473 &phba->sli.ring[LPFC_ELS_RING],
14474 iocbq, fc_hdr->fh_r_ctl,
14475 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014476 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014477 "2540 Ring %d handler: unexpected Rctl "
14478 "x%x Type x%x received\n",
14479 LPFC_ELS_RING,
14480 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14481
14482 /* Free iocb created in lpfc_prep_seq */
14483 list_for_each_entry_safe(curr_iocb, next_iocb,
14484 &iocbq->list, list) {
14485 list_del_init(&curr_iocb->list);
14486 lpfc_sli_release_iocbq(phba, curr_iocb);
14487 }
14488 lpfc_sli_release_iocbq(phba, iocbq);
14489}
14490
James Smart4f774512009-05-22 14:52:35 -040014491/**
14492 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14493 * @phba: Pointer to HBA context object.
14494 *
14495 * This function is called with no lock held. This function processes all
14496 * the received buffers and gives it to upper layers when a received buffer
14497 * indicates that it is the final frame in the sequence. The interrupt
14498 * service routine processes received buffers at interrupt contexts and adds
14499 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14500 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14501 * appropriate receive function when the final frame in a sequence is received.
14502 **/
James Smart4d9ab992009-10-02 15:16:39 -040014503void
14504lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14505 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014506{
James Smart4d9ab992009-10-02 15:16:39 -040014507 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014508 struct fc_frame_header *fc_hdr;
14509 struct lpfc_vport *vport;
14510 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014511 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014512
James Smart4f774512009-05-22 14:52:35 -040014513 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014514 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14515 /* check to see if this a valid type of frame */
14516 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14517 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14518 return;
14519 }
James Smart7851fe22011-07-22 18:36:52 -040014520 if ((bf_get(lpfc_cqe_code,
14521 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14522 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14523 &dmabuf->cq_event.cqe.rcqe_cmpl);
14524 else
14525 fcfi = bf_get(lpfc_rcqe_fcf_id,
14526 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014527
James Smart4d9ab992009-10-02 15:16:39 -040014528 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014529 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014530 /* throw out the frame */
14531 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14532 return;
14533 }
James Smart939723a2012-05-09 21:19:03 -040014534
14535 /* d_id this frame is directed to */
14536 did = sli4_did_from_fc_hdr(fc_hdr);
14537
14538 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14539 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14540 (did != Fabric_DID)) {
14541 /*
14542 * Throw out the frame if we are not pt2pt.
14543 * The pt2pt protocol allows for discovery frames
14544 * to be received without a registered VPI.
14545 */
14546 if (!(vport->fc_flag & FC_PT2PT) ||
14547 (phba->link_state == LPFC_HBA_READY)) {
14548 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14549 return;
14550 }
14551 }
14552
James Smart6669f9b2009-10-02 15:16:45 -040014553 /* Handle the basic abort sequence (BA_ABTS) event */
14554 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14555 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14556 return;
14557 }
14558
James Smart4d9ab992009-10-02 15:16:39 -040014559 /* Link this frame */
14560 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14561 if (!seq_dmabuf) {
14562 /* unable to add frame to vport - throw it out */
14563 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14564 return;
14565 }
14566 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014567 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014568 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014569
James Smart6669f9b2009-10-02 15:16:45 -040014570 /* Send the complete sequence to the upper layer protocol */
14571 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014572}
James Smart6fb120a2009-05-22 14:52:59 -040014573
14574/**
14575 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14576 * @phba: pointer to lpfc hba data structure.
14577 *
14578 * This routine is invoked to post rpi header templates to the
14579 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014580 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14581 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014582 *
14583 * This routine does not require any locks. It's usage is expected
14584 * to be driver load or reset recovery when the driver is
14585 * sequential.
14586 *
14587 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014588 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014589 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014590 * When this error occurs, the driver is not guaranteed
14591 * to have any rpi regions posted to the device and
14592 * must either attempt to repost the regions or take a
14593 * fatal error.
14594 **/
14595int
14596lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14597{
14598 struct lpfc_rpi_hdr *rpi_page;
14599 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014600 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014601
James Smart6d368e52011-05-24 11:44:12 -040014602 /* SLI4 ports that support extents do not require RPI headers. */
14603 if (!phba->sli4_hba.rpi_hdrs_in_use)
14604 goto exit;
14605 if (phba->sli4_hba.extents_in_use)
14606 return -EIO;
14607
James Smart6fb120a2009-05-22 14:52:59 -040014608 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014609 /*
14610 * Assign the rpi headers a physical rpi only if the driver
14611 * has not initialized those resources. A port reset only
14612 * needs the headers posted.
14613 */
14614 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14615 LPFC_RPI_RSRC_RDY)
14616 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14617
James Smart6fb120a2009-05-22 14:52:59 -040014618 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14619 if (rc != MBX_SUCCESS) {
14620 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14621 "2008 Error %d posting all rpi "
14622 "headers\n", rc);
14623 rc = -EIO;
14624 break;
14625 }
14626 }
14627
James Smart6d368e52011-05-24 11:44:12 -040014628 exit:
14629 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14630 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014631 return rc;
14632}
14633
14634/**
14635 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14636 * @phba: pointer to lpfc hba data structure.
14637 * @rpi_page: pointer to the rpi memory region.
14638 *
14639 * This routine is invoked to post a single rpi header to the
14640 * HBA consistent with the SLI-4 interface spec. This memory region
14641 * maps up to 64 rpi context regions.
14642 *
14643 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014644 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014645 * -ENOMEM - No available memory
14646 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014647 **/
14648int
14649lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14650{
14651 LPFC_MBOXQ_t *mboxq;
14652 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14653 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014654 uint32_t shdr_status, shdr_add_status;
14655 union lpfc_sli4_cfg_shdr *shdr;
14656
James Smart6d368e52011-05-24 11:44:12 -040014657 /* SLI4 ports that support extents do not require RPI headers. */
14658 if (!phba->sli4_hba.rpi_hdrs_in_use)
14659 return rc;
14660 if (phba->sli4_hba.extents_in_use)
14661 return -EIO;
14662
James Smart6fb120a2009-05-22 14:52:59 -040014663 /* The port is notified of the header region via a mailbox command. */
14664 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14665 if (!mboxq) {
14666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14667 "2001 Unable to allocate memory for issuing "
14668 "SLI_CONFIG_SPECIAL mailbox command\n");
14669 return -ENOMEM;
14670 }
14671
14672 /* Post all rpi memory regions to the port. */
14673 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014674 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14675 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14676 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014677 sizeof(struct lpfc_sli4_cfg_mhdr),
14678 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014679
14680
14681 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014682 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14683 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014684 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14685 hdr_tmpl, rpi_page->page_count);
14686
James Smart6fb120a2009-05-22 14:52:59 -040014687 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14688 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014689 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014690 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14691 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14692 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14693 if (rc != MBX_TIMEOUT)
14694 mempool_free(mboxq, phba->mbox_mem_pool);
14695 if (shdr_status || shdr_add_status || rc) {
14696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14697 "2514 POST_RPI_HDR mailbox failed with "
14698 "status x%x add_status x%x, mbx status x%x\n",
14699 shdr_status, shdr_add_status, rc);
14700 rc = -ENXIO;
14701 }
14702 return rc;
14703}
14704
14705/**
14706 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14707 * @phba: pointer to lpfc hba data structure.
14708 *
14709 * This routine is invoked to post rpi header templates to the
14710 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014711 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14712 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014713 *
14714 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014715 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014716 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14717 **/
14718int
14719lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14720{
James Smart6d368e52011-05-24 11:44:12 -040014721 unsigned long rpi;
14722 uint16_t max_rpi, rpi_limit;
14723 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014724 struct lpfc_rpi_hdr *rpi_hdr;
14725
14726 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014727 rpi_limit = phba->sli4_hba.next_rpi;
14728
14729 /*
James Smart6d368e52011-05-24 11:44:12 -040014730 * Fetch the next logical rpi. Because this index is logical,
14731 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014732 */
14733 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014734 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14735 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014736 rpi = LPFC_RPI_ALLOC_ERROR;
14737 else {
14738 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14739 phba->sli4_hba.max_cfg_param.rpi_used++;
14740 phba->sli4_hba.rpi_count++;
14741 }
14742
14743 /*
14744 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014745 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014746 */
14747 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14748 (phba->sli4_hba.rpi_count >= max_rpi)) {
14749 spin_unlock_irq(&phba->hbalock);
14750 return rpi;
14751 }
14752
14753 /*
James Smart6d368e52011-05-24 11:44:12 -040014754 * RPI header postings are not required for SLI4 ports capable of
14755 * extents.
14756 */
14757 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14758 spin_unlock_irq(&phba->hbalock);
14759 return rpi;
14760 }
14761
14762 /*
James Smart6fb120a2009-05-22 14:52:59 -040014763 * If the driver is running low on rpi resources, allocate another
14764 * page now. Note that the next_rpi value is used because
14765 * it represents how many are actually in use whereas max_rpi notes
14766 * how many are supported max by the device.
14767 */
James Smart6d368e52011-05-24 11:44:12 -040014768 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014769 spin_unlock_irq(&phba->hbalock);
14770 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14771 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14772 if (!rpi_hdr) {
14773 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14774 "2002 Error Could not grow rpi "
14775 "count\n");
14776 } else {
James Smart6d368e52011-05-24 11:44:12 -040014777 lrpi = rpi_hdr->start_rpi;
14778 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014779 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14780 }
14781 }
14782
14783 return rpi;
14784}
14785
14786/**
14787 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14788 * @phba: pointer to lpfc hba data structure.
14789 *
14790 * This routine is invoked to release an rpi to the pool of
14791 * available rpis maintained by the driver.
14792 **/
14793void
James Smartd7c47992010-06-08 18:31:54 -040014794__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14795{
14796 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14797 phba->sli4_hba.rpi_count--;
14798 phba->sli4_hba.max_cfg_param.rpi_used--;
14799 }
14800}
14801
14802/**
14803 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14804 * @phba: pointer to lpfc hba data structure.
14805 *
14806 * This routine is invoked to release an rpi to the pool of
14807 * available rpis maintained by the driver.
14808 **/
14809void
James Smart6fb120a2009-05-22 14:52:59 -040014810lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14811{
14812 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014813 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014814 spin_unlock_irq(&phba->hbalock);
14815}
14816
14817/**
14818 * lpfc_sli4_remove_rpis - 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 **/
14824void
14825lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14826{
14827 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014828 kfree(phba->sli4_hba.rpi_ids);
14829 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014830}
14831
14832/**
14833 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14834 * @phba: pointer to lpfc hba data structure.
14835 *
14836 * This routine is invoked to remove the memory region that
14837 * provided rpi via a bitmask.
14838 **/
14839int
James Smart6b5151f2012-01-18 16:24:06 -050014840lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14841 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014842{
14843 LPFC_MBOXQ_t *mboxq;
14844 struct lpfc_hba *phba = ndlp->phba;
14845 int rc;
14846
14847 /* The port is notified of the header region via a mailbox command. */
14848 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14849 if (!mboxq)
14850 return -ENOMEM;
14851
14852 /* Post all rpi memory regions to the port. */
14853 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014854 if (cmpl) {
14855 mboxq->mbox_cmpl = cmpl;
14856 mboxq->context1 = arg;
14857 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014858 } else
14859 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014860 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014861 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14862 if (rc == MBX_NOT_FINISHED) {
14863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14864 "2010 Resume RPI Mailbox failed "
14865 "status %d, mbxStatus x%x\n", rc,
14866 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14867 mempool_free(mboxq, phba->mbox_mem_pool);
14868 return -EIO;
14869 }
14870 return 0;
14871}
14872
14873/**
14874 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014875 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014876 *
James Smart76a95d72010-11-20 23:11:48 -050014877 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014878 *
14879 * Returns:
14880 * 0 success
14881 * -Evalue otherwise
14882 **/
14883int
James Smart76a95d72010-11-20 23:11:48 -050014884lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014885{
14886 LPFC_MBOXQ_t *mboxq;
14887 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014888 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014889 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014890 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014891 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14892 if (!mboxq)
14893 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014894 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014895 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014896 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014897 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014898 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014899 "2022 INIT VPI Mailbox failed "
14900 "status %d, mbxStatus x%x\n", rc,
14901 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014902 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014903 }
James Smart6a9c52c2009-10-02 15:16:51 -040014904 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014905 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014906
14907 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014908}
14909
14910/**
14911 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14912 * @phba: pointer to lpfc hba data structure.
14913 * @mboxq: Pointer to mailbox object.
14914 *
14915 * This routine is invoked to manually add a single FCF record. The caller
14916 * must pass a completely initialized FCF_Record. This routine takes
14917 * care of the nonembedded mailbox operations.
14918 **/
14919static void
14920lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14921{
14922 void *virt_addr;
14923 union lpfc_sli4_cfg_shdr *shdr;
14924 uint32_t shdr_status, shdr_add_status;
14925
14926 virt_addr = mboxq->sge_array->addr[0];
14927 /* The IOCTL status is embedded in the mailbox subheader. */
14928 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14929 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14930 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14931
14932 if ((shdr_status || shdr_add_status) &&
14933 (shdr_status != STATUS_FCF_IN_USE))
14934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14935 "2558 ADD_FCF_RECORD mailbox failed with "
14936 "status x%x add_status x%x\n",
14937 shdr_status, shdr_add_status);
14938
14939 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14940}
14941
14942/**
14943 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14944 * @phba: pointer to lpfc hba data structure.
14945 * @fcf_record: pointer to the initialized fcf record to add.
14946 *
14947 * This routine is invoked to manually add a single FCF record. The caller
14948 * must pass a completely initialized FCF_Record. This routine takes
14949 * care of the nonembedded mailbox operations.
14950 **/
14951int
14952lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14953{
14954 int rc = 0;
14955 LPFC_MBOXQ_t *mboxq;
14956 uint8_t *bytep;
14957 void *virt_addr;
14958 dma_addr_t phys_addr;
14959 struct lpfc_mbx_sge sge;
14960 uint32_t alloc_len, req_len;
14961 uint32_t fcfindex;
14962
14963 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14964 if (!mboxq) {
14965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14966 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14967 return -ENOMEM;
14968 }
14969
14970 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14971 sizeof(uint32_t);
14972
14973 /* Allocate DMA memory and set up the non-embedded mailbox command */
14974 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14975 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14976 req_len, LPFC_SLI4_MBX_NEMBED);
14977 if (alloc_len < req_len) {
14978 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14979 "2523 Allocated DMA memory size (x%x) is "
14980 "less than the requested DMA memory "
14981 "size (x%x)\n", alloc_len, req_len);
14982 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14983 return -ENOMEM;
14984 }
14985
14986 /*
14987 * Get the first SGE entry from the non-embedded DMA memory. This
14988 * routine only uses a single SGE.
14989 */
14990 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14991 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014992 virt_addr = mboxq->sge_array->addr[0];
14993 /*
14994 * Configure the FCF record for FCFI 0. This is the driver's
14995 * hardcoded default and gets used in nonFIP mode.
14996 */
14997 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14998 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14999 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15000
15001 /*
15002 * Copy the fcf_index and the FCF Record Data. The data starts after
15003 * the FCoE header plus word10. The data copy needs to be endian
15004 * correct.
15005 */
15006 bytep += sizeof(uint32_t);
15007 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15008 mboxq->vport = phba->pport;
15009 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15010 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15011 if (rc == MBX_NOT_FINISHED) {
15012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15013 "2515 ADD_FCF_RECORD mailbox failed with "
15014 "status 0x%x\n", rc);
15015 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15016 rc = -EIO;
15017 } else
15018 rc = 0;
15019
15020 return rc;
15021}
15022
15023/**
15024 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15025 * @phba: pointer to lpfc hba data structure.
15026 * @fcf_record: pointer to the fcf record to write the default data.
15027 * @fcf_index: FCF table entry index.
15028 *
15029 * This routine is invoked to build the driver's default FCF record. The
15030 * values used are hardcoded. This routine handles memory initialization.
15031 *
15032 **/
15033void
15034lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15035 struct fcf_record *fcf_record,
15036 uint16_t fcf_index)
15037{
15038 memset(fcf_record, 0, sizeof(struct fcf_record));
15039 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15040 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15041 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15042 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15043 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15044 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15045 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15046 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15047 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15048 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15049 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15050 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15051 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015052 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015053 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15054 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15055 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15056 /* Set the VLAN bit map */
15057 if (phba->valid_vlan) {
15058 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15059 = 1 << (phba->vlan_id % 8);
15060 }
15061}
15062
15063/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015064 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015065 * @phba: pointer to lpfc hba data structure.
15066 * @fcf_index: FCF table entry offset.
15067 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015068 * This routine is invoked to scan the entire FCF table by reading FCF
15069 * record and processing it one at a time starting from the @fcf_index
15070 * for initial FCF discovery or fast FCF failover rediscovery.
15071 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015072 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015073 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015074 **/
15075int
James Smart0c9ab6f2010-02-26 14:15:57 -050015076lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015077{
15078 int rc = 0, error;
15079 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015080
James Smart32b97932009-07-19 10:01:21 -040015081 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015082 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015083 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15084 if (!mboxq) {
15085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15086 "2000 Failed to allocate mbox for "
15087 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015088 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015089 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015090 }
James Smartecfd03c2010-02-12 14:41:27 -050015091 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015092 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015093 if (rc) {
15094 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015095 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015096 }
James Smartecfd03c2010-02-12 14:41:27 -050015097 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015098 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015099 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015100
15101 spin_lock_irq(&phba->hbalock);
15102 phba->hba_flag |= FCF_TS_INPROG;
15103 spin_unlock_irq(&phba->hbalock);
15104
James Smart6fb120a2009-05-22 14:52:59 -040015105 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015106 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015107 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015108 else {
James Smart38b92ef2010-08-04 16:11:39 -040015109 /* Reset eligible FCF count for new scan */
15110 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015111 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015112 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015113 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015114fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015115 if (error) {
15116 if (mboxq)
15117 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015118 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015119 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015120 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015121 spin_unlock_irq(&phba->hbalock);
15122 }
James Smart6fb120a2009-05-22 14:52:59 -040015123 return error;
15124}
James Smarta0c87cb2009-07-19 10:01:10 -040015125
15126/**
James Smarta93ff372010-10-22 11:06:08 -040015127 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015128 * @phba: pointer to lpfc hba data structure.
15129 * @fcf_index: FCF table entry offset.
15130 *
15131 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015132 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015133 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015134 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015135 * otherwise.
15136 **/
15137int
15138lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15139{
15140 int rc = 0, error;
15141 LPFC_MBOXQ_t *mboxq;
15142
15143 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15144 if (!mboxq) {
15145 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15146 "2763 Failed to allocate mbox for "
15147 "READ_FCF cmd\n");
15148 error = -ENOMEM;
15149 goto fail_fcf_read;
15150 }
15151 /* Construct the read FCF record mailbox command */
15152 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15153 if (rc) {
15154 error = -EINVAL;
15155 goto fail_fcf_read;
15156 }
15157 /* Issue the mailbox command asynchronously */
15158 mboxq->vport = phba->pport;
15159 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15160 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15161 if (rc == MBX_NOT_FINISHED)
15162 error = -EIO;
15163 else
15164 error = 0;
15165
15166fail_fcf_read:
15167 if (error && mboxq)
15168 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15169 return error;
15170}
15171
15172/**
15173 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15174 * @phba: pointer to lpfc hba data structure.
15175 * @fcf_index: FCF table entry offset.
15176 *
15177 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015178 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015179 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015180 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015181 * otherwise.
15182 **/
15183int
15184lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15185{
15186 int rc = 0, error;
15187 LPFC_MBOXQ_t *mboxq;
15188
15189 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15190 if (!mboxq) {
15191 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15192 "2758 Failed to allocate mbox for "
15193 "READ_FCF cmd\n");
15194 error = -ENOMEM;
15195 goto fail_fcf_read;
15196 }
15197 /* Construct the read FCF record mailbox command */
15198 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15199 if (rc) {
15200 error = -EINVAL;
15201 goto fail_fcf_read;
15202 }
15203 /* Issue the mailbox command asynchronously */
15204 mboxq->vport = phba->pport;
15205 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15206 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15207 if (rc == MBX_NOT_FINISHED)
15208 error = -EIO;
15209 else
15210 error = 0;
15211
15212fail_fcf_read:
15213 if (error && mboxq)
15214 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15215 return error;
15216}
15217
15218/**
James Smart7d791df2011-07-22 18:37:52 -040015219 * lpfc_check_next_fcf_pri
15220 * phba pointer to the lpfc_hba struct for this port.
15221 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15222 * routine when the rr_bmask is empty. The FCF indecies are put into the
15223 * rr_bmask based on their priority level. Starting from the highest priority
15224 * to the lowest. The most likely FCF candidate will be in the highest
15225 * priority group. When this routine is called it searches the fcf_pri list for
15226 * next lowest priority group and repopulates the rr_bmask with only those
15227 * fcf_indexes.
15228 * returns:
15229 * 1=success 0=failure
15230 **/
15231int
15232lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15233{
15234 uint16_t next_fcf_pri;
15235 uint16_t last_index;
15236 struct lpfc_fcf_pri *fcf_pri;
15237 int rc;
15238 int ret = 0;
15239
15240 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15241 LPFC_SLI4_FCF_TBL_INDX_MAX);
15242 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15243 "3060 Last IDX %d\n", last_index);
15244 if (list_empty(&phba->fcf.fcf_pri_list)) {
15245 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15246 "3061 Last IDX %d\n", last_index);
15247 return 0; /* Empty rr list */
15248 }
15249 next_fcf_pri = 0;
15250 /*
15251 * Clear the rr_bmask and set all of the bits that are at this
15252 * priority.
15253 */
15254 memset(phba->fcf.fcf_rr_bmask, 0,
15255 sizeof(*phba->fcf.fcf_rr_bmask));
15256 spin_lock_irq(&phba->hbalock);
15257 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15258 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15259 continue;
15260 /*
15261 * the 1st priority that has not FLOGI failed
15262 * will be the highest.
15263 */
15264 if (!next_fcf_pri)
15265 next_fcf_pri = fcf_pri->fcf_rec.priority;
15266 spin_unlock_irq(&phba->hbalock);
15267 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15268 rc = lpfc_sli4_fcf_rr_index_set(phba,
15269 fcf_pri->fcf_rec.fcf_index);
15270 if (rc)
15271 return 0;
15272 }
15273 spin_lock_irq(&phba->hbalock);
15274 }
15275 /*
15276 * if next_fcf_pri was not set above and the list is not empty then
15277 * we have failed flogis on all of them. So reset flogi failed
15278 * and start at the begining.
15279 */
15280 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15281 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15282 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15283 /*
15284 * the 1st priority that has not FLOGI failed
15285 * will be the highest.
15286 */
15287 if (!next_fcf_pri)
15288 next_fcf_pri = fcf_pri->fcf_rec.priority;
15289 spin_unlock_irq(&phba->hbalock);
15290 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15291 rc = lpfc_sli4_fcf_rr_index_set(phba,
15292 fcf_pri->fcf_rec.fcf_index);
15293 if (rc)
15294 return 0;
15295 }
15296 spin_lock_irq(&phba->hbalock);
15297 }
15298 } else
15299 ret = 1;
15300 spin_unlock_irq(&phba->hbalock);
15301
15302 return ret;
15303}
15304/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015305 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15306 * @phba: pointer to lpfc hba data structure.
15307 *
15308 * This routine is to get the next eligible FCF record index in a round
15309 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015310 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015311 * shall be returned, otherwise, the next eligible FCF record's index
15312 * shall be returned.
15313 **/
15314uint16_t
15315lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15316{
15317 uint16_t next_fcf_index;
15318
James Smart3804dc82010-07-14 15:31:37 -040015319 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015320next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015321 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15322 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015323 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15324 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015325 next_fcf_index);
15326
James Smart0c9ab6f2010-02-26 14:15:57 -050015327 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015328 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15329 /*
15330 * If we have wrapped then we need to clear the bits that
15331 * have been tested so that we can detect when we should
15332 * change the priority level.
15333 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015334 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15335 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015336 }
15337
James Smart0c9ab6f2010-02-26 14:15:57 -050015338
James Smart3804dc82010-07-14 15:31:37 -040015339 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015340 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15341 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15342 /*
15343 * If next fcf index is not found check if there are lower
15344 * Priority level fcf's in the fcf_priority list.
15345 * Set up the rr_bmask with all of the avaiable fcf bits
15346 * at that level and continue the selection process.
15347 */
15348 if (lpfc_check_next_fcf_pri_level(phba))
15349 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015350 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15351 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015352 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15353 return LPFC_FCOE_FCF_NEXT_NONE;
15354 else {
15355 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15356 "3063 Only FCF available idx %d, flag %x\n",
15357 next_fcf_index,
15358 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15359 return next_fcf_index;
15360 }
James Smart3804dc82010-07-14 15:31:37 -040015361 }
15362
James Smart7d791df2011-07-22 18:37:52 -040015363 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15364 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15365 LPFC_FCF_FLOGI_FAILED)
15366 goto next_priority;
15367
James Smart3804dc82010-07-14 15:31:37 -040015368 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015369 "2845 Get next roundrobin failover FCF (x%x)\n",
15370 next_fcf_index);
15371
James Smart0c9ab6f2010-02-26 14:15:57 -050015372 return next_fcf_index;
15373}
15374
15375/**
15376 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15377 * @phba: pointer to lpfc hba data structure.
15378 *
15379 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015380 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015381 * does not go beyond the range of the driver allocated bmask dimension
15382 * before setting the bit.
15383 *
15384 * Returns 0 if the index bit successfully set, otherwise, it returns
15385 * -EINVAL.
15386 **/
15387int
15388lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15389{
15390 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15391 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015392 "2610 FCF (x%x) reached driver's book "
15393 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015394 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15395 return -EINVAL;
15396 }
15397 /* Set the eligible FCF record index bmask */
15398 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15399
James Smart3804dc82010-07-14 15:31:37 -040015400 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015401 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015402 "bmask\n", fcf_index);
15403
James Smart0c9ab6f2010-02-26 14:15:57 -050015404 return 0;
15405}
15406
15407/**
James Smart3804dc82010-07-14 15:31:37 -040015408 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015409 * @phba: pointer to lpfc hba data structure.
15410 *
15411 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015412 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015413 * does not go beyond the range of the driver allocated bmask dimension
15414 * before clearing the bit.
15415 **/
15416void
15417lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15418{
James Smart7d791df2011-07-22 18:37:52 -040015419 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015420 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15421 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015422 "2762 FCF (x%x) reached driver's book "
15423 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015424 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15425 return;
15426 }
15427 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015428 spin_lock_irq(&phba->hbalock);
15429 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15430 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15431 list_del_init(&fcf_pri->list);
15432 break;
15433 }
15434 }
15435 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015436 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015437
15438 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015439 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015440 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015441}
15442
15443/**
James Smartecfd03c2010-02-12 14:41:27 -050015444 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15445 * @phba: pointer to lpfc hba data structure.
15446 *
15447 * This routine is the completion routine for the rediscover FCF table mailbox
15448 * command. If the mailbox command returned failure, it will try to stop the
15449 * FCF rediscover wait timer.
15450 **/
15451void
15452lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15453{
15454 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15455 uint32_t shdr_status, shdr_add_status;
15456
15457 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15458
15459 shdr_status = bf_get(lpfc_mbox_hdr_status,
15460 &redisc_fcf->header.cfg_shdr.response);
15461 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15462 &redisc_fcf->header.cfg_shdr.response);
15463 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015464 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015465 "2746 Requesting for FCF rediscovery failed "
15466 "status x%x add_status x%x\n",
15467 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015468 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015469 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015470 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015471 spin_unlock_irq(&phba->hbalock);
15472 /*
15473 * CVL event triggered FCF rediscover request failed,
15474 * last resort to re-try current registered FCF entry.
15475 */
15476 lpfc_retry_pport_discovery(phba);
15477 } else {
15478 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015479 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015480 spin_unlock_irq(&phba->hbalock);
15481 /*
15482 * DEAD FCF event triggered FCF rediscover request
15483 * failed, last resort to fail over as a link down
15484 * to FCF registration.
15485 */
15486 lpfc_sli4_fcf_dead_failthrough(phba);
15487 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015488 } else {
15489 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015490 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015491 /*
15492 * Start FCF rediscovery wait timer for pending FCF
15493 * before rescan FCF record table.
15494 */
15495 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015496 }
James Smartecfd03c2010-02-12 14:41:27 -050015497
15498 mempool_free(mbox, phba->mbox_mem_pool);
15499}
15500
15501/**
James Smart3804dc82010-07-14 15:31:37 -040015502 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015503 * @phba: pointer to lpfc hba data structure.
15504 *
15505 * This routine is invoked to request for rediscovery of the entire FCF table
15506 * by the port.
15507 **/
15508int
15509lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15510{
15511 LPFC_MBOXQ_t *mbox;
15512 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15513 int rc, length;
15514
James Smart0c9ab6f2010-02-26 14:15:57 -050015515 /* Cancel retry delay timers to all vports before FCF rediscover */
15516 lpfc_cancel_all_vport_retry_delay_timer(phba);
15517
James Smartecfd03c2010-02-12 14:41:27 -050015518 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15519 if (!mbox) {
15520 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15521 "2745 Failed to allocate mbox for "
15522 "requesting FCF rediscover.\n");
15523 return -ENOMEM;
15524 }
15525
15526 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15527 sizeof(struct lpfc_sli4_cfg_mhdr));
15528 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15529 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15530 length, LPFC_SLI4_MBX_EMBED);
15531
15532 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15533 /* Set count to 0 for invalidating the entire FCF database */
15534 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15535
15536 /* Issue the mailbox command asynchronously */
15537 mbox->vport = phba->pport;
15538 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15539 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15540
15541 if (rc == MBX_NOT_FINISHED) {
15542 mempool_free(mbox, phba->mbox_mem_pool);
15543 return -EIO;
15544 }
15545 return 0;
15546}
15547
15548/**
James Smartfc2b9892010-02-26 14:15:29 -050015549 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15550 * @phba: pointer to lpfc hba data structure.
15551 *
15552 * This function is the failover routine as a last resort to the FCF DEAD
15553 * event when driver failed to perform fast FCF failover.
15554 **/
15555void
15556lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15557{
15558 uint32_t link_state;
15559
15560 /*
15561 * Last resort as FCF DEAD event failover will treat this as
15562 * a link down, but save the link state because we don't want
15563 * it to be changed to Link Down unless it is already down.
15564 */
15565 link_state = phba->link_state;
15566 lpfc_linkdown(phba);
15567 phba->link_state = link_state;
15568
15569 /* Unregister FCF if no devices connected to it */
15570 lpfc_unregister_unused_fcf(phba);
15571}
15572
15573/**
James Smart026abb82011-12-13 13:20:45 -050015574 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015575 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015576 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015577 *
James Smart026abb82011-12-13 13:20:45 -050015578 * This function gets SLI3 port configure region 23 data through memory dump
15579 * mailbox command. When it successfully retrieves data, the size of the data
15580 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015581 **/
James Smart026abb82011-12-13 13:20:45 -050015582static uint32_t
15583lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015584{
15585 LPFC_MBOXQ_t *pmb = NULL;
15586 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015587 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015588 int rc;
15589
James Smart026abb82011-12-13 13:20:45 -050015590 if (!rgn23_data)
15591 return 0;
15592
James Smarta0c87cb2009-07-19 10:01:10 -040015593 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15594 if (!pmb) {
15595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015596 "2600 failed to allocate mailbox memory\n");
15597 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015598 }
15599 mb = &pmb->u.mb;
15600
James Smarta0c87cb2009-07-19 10:01:10 -040015601 do {
15602 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15603 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15604
15605 if (rc != MBX_SUCCESS) {
15606 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015607 "2601 failed to read config "
15608 "region 23, rc 0x%x Status 0x%x\n",
15609 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015610 mb->un.varDmp.word_cnt = 0;
15611 }
15612 /*
15613 * dump mem may return a zero when finished or we got a
15614 * mailbox error, either way we are done.
15615 */
15616 if (mb->un.varDmp.word_cnt == 0)
15617 break;
15618 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15619 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15620
15621 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015622 rgn23_data + offset,
15623 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015624 offset += mb->un.varDmp.word_cnt;
15625 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15626
James Smart026abb82011-12-13 13:20:45 -050015627 mempool_free(pmb, phba->mbox_mem_pool);
15628 return offset;
15629}
15630
15631/**
15632 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15633 * @phba: pointer to lpfc hba data structure.
15634 * @rgn23_data: pointer to configure region 23 data.
15635 *
15636 * This function gets SLI4 port configure region 23 data through memory dump
15637 * mailbox command. When it successfully retrieves data, the size of the data
15638 * will be returned, otherwise, 0 will be returned.
15639 **/
15640static uint32_t
15641lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15642{
15643 LPFC_MBOXQ_t *mboxq = NULL;
15644 struct lpfc_dmabuf *mp = NULL;
15645 struct lpfc_mqe *mqe;
15646 uint32_t data_length = 0;
15647 int rc;
15648
15649 if (!rgn23_data)
15650 return 0;
15651
15652 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15653 if (!mboxq) {
15654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15655 "3105 failed to allocate mailbox memory\n");
15656 return 0;
15657 }
15658
15659 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15660 goto out;
15661 mqe = &mboxq->u.mqe;
15662 mp = (struct lpfc_dmabuf *) mboxq->context1;
15663 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15664 if (rc)
15665 goto out;
15666 data_length = mqe->un.mb_words[5];
15667 if (data_length == 0)
15668 goto out;
15669 if (data_length > DMP_RGN23_SIZE) {
15670 data_length = 0;
15671 goto out;
15672 }
15673 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15674out:
15675 mempool_free(mboxq, phba->mbox_mem_pool);
15676 if (mp) {
15677 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15678 kfree(mp);
15679 }
15680 return data_length;
15681}
15682
15683/**
15684 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15685 * @phba: pointer to lpfc hba data structure.
15686 *
15687 * This function read region 23 and parse TLV for port status to
15688 * decide if the user disaled the port. If the TLV indicates the
15689 * port is disabled, the hba_flag is set accordingly.
15690 **/
15691void
15692lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15693{
15694 uint8_t *rgn23_data = NULL;
15695 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15696 uint32_t offset = 0;
15697
15698 /* Get adapter Region 23 data */
15699 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15700 if (!rgn23_data)
15701 goto out;
15702
15703 if (phba->sli_rev < LPFC_SLI_REV4)
15704 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15705 else {
15706 if_type = bf_get(lpfc_sli_intf_if_type,
15707 &phba->sli4_hba.sli_intf);
15708 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15709 goto out;
15710 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15711 }
James Smarta0c87cb2009-07-19 10:01:10 -040015712
15713 if (!data_size)
15714 goto out;
15715
15716 /* Check the region signature first */
15717 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15719 "2619 Config region 23 has bad signature\n");
15720 goto out;
15721 }
15722 offset += 4;
15723
15724 /* Check the data structure version */
15725 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15727 "2620 Config region 23 has bad version\n");
15728 goto out;
15729 }
15730 offset += 4;
15731
15732 /* Parse TLV entries in the region */
15733 while (offset < data_size) {
15734 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15735 break;
15736 /*
15737 * If the TLV is not driver specific TLV or driver id is
15738 * not linux driver id, skip the record.
15739 */
15740 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15741 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15742 (rgn23_data[offset + 3] != 0)) {
15743 offset += rgn23_data[offset + 1] * 4 + 4;
15744 continue;
15745 }
15746
15747 /* Driver found a driver specific TLV in the config region */
15748 sub_tlv_len = rgn23_data[offset + 1] * 4;
15749 offset += 4;
15750 tlv_offset = 0;
15751
15752 /*
15753 * Search for configured port state sub-TLV.
15754 */
15755 while ((offset < data_size) &&
15756 (tlv_offset < sub_tlv_len)) {
15757 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15758 offset += 4;
15759 tlv_offset += 4;
15760 break;
15761 }
15762 if (rgn23_data[offset] != PORT_STE_TYPE) {
15763 offset += rgn23_data[offset + 1] * 4 + 4;
15764 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15765 continue;
15766 }
15767
15768 /* This HBA contains PORT_STE configured */
15769 if (!rgn23_data[offset + 2])
15770 phba->hba_flag |= LINK_DISABLED;
15771
15772 goto out;
15773 }
15774 }
James Smart026abb82011-12-13 13:20:45 -050015775
James Smarta0c87cb2009-07-19 10:01:10 -040015776out:
James Smarta0c87cb2009-07-19 10:01:10 -040015777 kfree(rgn23_data);
15778 return;
15779}
James Smart695a8142010-01-26 23:08:03 -050015780
15781/**
James Smart52d52442011-05-24 11:42:45 -040015782 * lpfc_wr_object - write an object to the firmware
15783 * @phba: HBA structure that indicates port to create a queue on.
15784 * @dmabuf_list: list of dmabufs to write to the port.
15785 * @size: the total byte value of the objects to write to the port.
15786 * @offset: the current offset to be used to start the transfer.
15787 *
15788 * This routine will create a wr_object mailbox command to send to the port.
15789 * the mailbox command will be constructed using the dma buffers described in
15790 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15791 * BDEs that the imbedded mailbox can support. The @offset variable will be
15792 * used to indicate the starting offset of the transfer and will also return
15793 * the offset after the write object mailbox has completed. @size is used to
15794 * determine the end of the object and whether the eof bit should be set.
15795 *
15796 * Return 0 is successful and offset will contain the the new offset to use
15797 * for the next write.
15798 * Return negative value for error cases.
15799 **/
15800int
15801lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15802 uint32_t size, uint32_t *offset)
15803{
15804 struct lpfc_mbx_wr_object *wr_object;
15805 LPFC_MBOXQ_t *mbox;
15806 int rc = 0, i = 0;
15807 uint32_t shdr_status, shdr_add_status;
15808 uint32_t mbox_tmo;
15809 union lpfc_sli4_cfg_shdr *shdr;
15810 struct lpfc_dmabuf *dmabuf;
15811 uint32_t written = 0;
15812
15813 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15814 if (!mbox)
15815 return -ENOMEM;
15816
15817 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15818 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15819 sizeof(struct lpfc_mbx_wr_object) -
15820 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15821
15822 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15823 wr_object->u.request.write_offset = *offset;
15824 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15825 wr_object->u.request.object_name[0] =
15826 cpu_to_le32(wr_object->u.request.object_name[0]);
15827 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15828 list_for_each_entry(dmabuf, dmabuf_list, list) {
15829 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15830 break;
15831 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15832 wr_object->u.request.bde[i].addrHigh =
15833 putPaddrHigh(dmabuf->phys);
15834 if (written + SLI4_PAGE_SIZE >= size) {
15835 wr_object->u.request.bde[i].tus.f.bdeSize =
15836 (size - written);
15837 written += (size - written);
15838 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15839 } else {
15840 wr_object->u.request.bde[i].tus.f.bdeSize =
15841 SLI4_PAGE_SIZE;
15842 written += SLI4_PAGE_SIZE;
15843 }
15844 i++;
15845 }
15846 wr_object->u.request.bde_count = i;
15847 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15848 if (!phba->sli4_hba.intr_enable)
15849 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15850 else {
James Smarta183a152011-10-10 21:32:43 -040015851 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015852 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15853 }
15854 /* The IOCTL status is embedded in the mailbox subheader. */
15855 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15856 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15857 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15858 if (rc != MBX_TIMEOUT)
15859 mempool_free(mbox, phba->mbox_mem_pool);
15860 if (shdr_status || shdr_add_status || rc) {
15861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15862 "3025 Write Object mailbox failed with "
15863 "status x%x add_status x%x, mbx status x%x\n",
15864 shdr_status, shdr_add_status, rc);
15865 rc = -ENXIO;
15866 } else
15867 *offset += wr_object->u.response.actual_write_length;
15868 return rc;
15869}
15870
15871/**
James Smart695a8142010-01-26 23:08:03 -050015872 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15873 * @vport: pointer to vport data structure.
15874 *
15875 * This function iterate through the mailboxq and clean up all REG_LOGIN
15876 * and REG_VPI mailbox commands associated with the vport. This function
15877 * is called when driver want to restart discovery of the vport due to
15878 * a Clear Virtual Link event.
15879 **/
15880void
15881lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15882{
15883 struct lpfc_hba *phba = vport->phba;
15884 LPFC_MBOXQ_t *mb, *nextmb;
15885 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015886 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015887 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015888 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015889 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015890 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015891
James Smartd439d282010-09-29 11:18:45 -040015892 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015893 spin_lock_irq(&phba->hbalock);
15894 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15895 if (mb->vport != vport)
15896 continue;
15897
15898 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15899 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15900 continue;
15901
James Smartd439d282010-09-29 11:18:45 -040015902 list_del(&mb->list);
15903 list_add_tail(&mb->list, &mbox_cmd_list);
15904 }
15905 /* Clean up active mailbox command with the vport */
15906 mb = phba->sli.mbox_active;
15907 if (mb && (mb->vport == vport)) {
15908 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15909 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15910 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15911 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15912 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15913 /* Put reference count for delayed processing */
15914 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15915 /* Unregister the RPI when mailbox complete */
15916 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15917 }
15918 }
James Smart63e801c2010-11-20 23:14:19 -050015919 /* Cleanup any mailbox completions which are not yet processed */
15920 do {
15921 restart_loop = 0;
15922 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15923 /*
15924 * If this mailox is already processed or it is
15925 * for another vport ignore it.
15926 */
15927 if ((mb->vport != vport) ||
15928 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15929 continue;
15930
15931 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15932 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15933 continue;
15934
15935 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15936 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15937 ndlp = (struct lpfc_nodelist *)mb->context2;
15938 /* Unregister the RPI when mailbox complete */
15939 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15940 restart_loop = 1;
15941 spin_unlock_irq(&phba->hbalock);
15942 spin_lock(shost->host_lock);
15943 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15944 spin_unlock(shost->host_lock);
15945 spin_lock_irq(&phba->hbalock);
15946 break;
15947 }
15948 }
15949 } while (restart_loop);
15950
James Smartd439d282010-09-29 11:18:45 -040015951 spin_unlock_irq(&phba->hbalock);
15952
15953 /* Release the cleaned-up mailbox commands */
15954 while (!list_empty(&mbox_cmd_list)) {
15955 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015956 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15957 mp = (struct lpfc_dmabuf *) (mb->context1);
15958 if (mp) {
15959 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15960 kfree(mp);
15961 }
James Smart78730cf2010-04-06 15:06:30 -040015962 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015963 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015964 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015965 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015966 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015967 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015968 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015969 }
James Smart695a8142010-01-26 23:08:03 -050015970 }
James Smart695a8142010-01-26 23:08:03 -050015971 mempool_free(mb, phba->mbox_mem_pool);
15972 }
James Smartd439d282010-09-29 11:18:45 -040015973
15974 /* Release the ndlp with the cleaned-up active mailbox command */
15975 if (act_mbx_ndlp) {
15976 spin_lock(shost->host_lock);
15977 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15978 spin_unlock(shost->host_lock);
15979 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015980 }
James Smart695a8142010-01-26 23:08:03 -050015981}
15982
James Smart2a9bf3d2010-06-07 15:24:45 -040015983/**
15984 * lpfc_drain_txq - Drain the txq
15985 * @phba: Pointer to HBA context object.
15986 *
15987 * This function attempt to submit IOCBs on the txq
15988 * to the adapter. For SLI4 adapters, the txq contains
15989 * ELS IOCBs that have been deferred because the there
15990 * are no SGLs. This congestion can occur with large
15991 * vport counts during node discovery.
15992 **/
15993
15994uint32_t
15995lpfc_drain_txq(struct lpfc_hba *phba)
15996{
15997 LIST_HEAD(completions);
15998 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15999 struct lpfc_iocbq *piocbq = 0;
16000 unsigned long iflags = 0;
16001 char *fail_msg = NULL;
16002 struct lpfc_sglq *sglq;
16003 union lpfc_wqe wqe;
16004
16005 spin_lock_irqsave(&phba->hbalock, iflags);
16006 if (pring->txq_cnt > pring->txq_max)
16007 pring->txq_max = pring->txq_cnt;
16008
16009 spin_unlock_irqrestore(&phba->hbalock, iflags);
16010
16011 while (pring->txq_cnt) {
16012 spin_lock_irqsave(&phba->hbalock, iflags);
16013
James Smart19ca7602010-11-20 23:11:55 -050016014 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016015 if (!piocbq) {
16016 spin_unlock_irqrestore(&phba->hbalock, iflags);
16017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16018 "2823 txq empty and txq_cnt is %d\n ",
16019 pring->txq_cnt);
16020 break;
16021 }
James Smart19ca7602010-11-20 23:11:55 -050016022 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016023 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016024 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016025 spin_unlock_irqrestore(&phba->hbalock, iflags);
16026 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016027 }
16028
16029 /* The xri and iocb resources secured,
16030 * attempt to issue request
16031 */
James Smart6d368e52011-05-24 11:44:12 -040016032 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016033 piocbq->sli4_xritag = sglq->sli4_xritag;
16034 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16035 fail_msg = "to convert bpl to sgl";
16036 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16037 fail_msg = "to convert iocb to wqe";
16038 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16039 fail_msg = " - Wq is full";
16040 else
16041 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16042
16043 if (fail_msg) {
16044 /* Failed means we can't issue and need to cancel */
16045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16046 "2822 IOCB failed %s iotag 0x%x "
16047 "xri 0x%x\n",
16048 fail_msg,
16049 piocbq->iotag, piocbq->sli4_xritag);
16050 list_add_tail(&piocbq->list, &completions);
16051 }
16052 spin_unlock_irqrestore(&phba->hbalock, iflags);
16053 }
16054
James Smart2a9bf3d2010-06-07 15:24:45 -040016055 /* Cancel all the IOCBs that cannot be issued */
16056 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16057 IOERR_SLI_ABORTED);
16058
16059 return pring->txq_cnt;
16060}