blob: d7afd0fb457974575d75309a33e92b427b2ef739 [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 **/
7799static 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 Smart8fa38512009-07-19 10:01:03 -04007802 ++phba->fcp_qidx;
7803 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7804 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007805
James Smart8fa38512009-07-19 10:01:03 -04007806 return phba->fcp_qidx;
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 /*
8327 * For FCP command IOCB, get a new WQ index to distribute
8328 * WQE across the WQsr. On the other hand, for abort IOCB,
8329 * it carries the same WQ index to the original command
8330 * IOCB.
8331 */
James Smart341af102010-01-26 23:07:37 -05008332 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008333 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05008334 if (unlikely(!phba->sli4_hba.fcp_wq))
8335 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008336 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8337 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008338 return IOCB_ERROR;
8339 } else {
8340 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8341 return IOCB_ERROR;
8342 }
8343 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8344
8345 return 0;
8346}
8347
8348/**
James Smart3772a992009-05-22 14:50:54 -04008349 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8350 *
8351 * This routine wraps the actual lockless version for issusing IOCB function
8352 * pointer from the lpfc_hba struct.
8353 *
8354 * Return codes:
8355 * IOCB_ERROR - Error
8356 * IOCB_SUCCESS - Success
8357 * IOCB_BUSY - Busy
8358 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008359int
James Smart3772a992009-05-22 14:50:54 -04008360__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8361 struct lpfc_iocbq *piocb, uint32_t flag)
8362{
8363 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8364}
8365
8366/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008367 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008368 * @phba: The hba struct for which this call is being executed.
8369 * @dev_grp: The HBA PCI-Device group number.
8370 *
8371 * This routine sets up the SLI interface API function jump table in @phba
8372 * struct.
8373 * Returns: 0 - success, -ENODEV - failure.
8374 **/
8375int
8376lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8377{
8378
8379 switch (dev_grp) {
8380 case LPFC_PCI_DEV_LP:
8381 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8382 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8383 break;
James Smart4f774512009-05-22 14:52:35 -04008384 case LPFC_PCI_DEV_OC:
8385 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8386 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8387 break;
James Smart3772a992009-05-22 14:50:54 -04008388 default:
8389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8390 "1419 Invalid HBA PCI-device group: 0x%x\n",
8391 dev_grp);
8392 return -ENODEV;
8393 break;
8394 }
8395 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8396 return 0;
8397}
James Smart92d7f7b2007-06-17 19:56:38 -05008398
James Smarte59058c2008-08-24 21:49:00 -04008399/**
James Smart3621a712009-04-06 18:47:14 -04008400 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008401 * @phba: Pointer to HBA context object.
8402 * @pring: Pointer to driver SLI ring object.
8403 * @piocb: Pointer to command iocb.
8404 * @flag: Flag indicating if this command can be put into txq.
8405 *
8406 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8407 * function. This function gets the hbalock and calls
8408 * __lpfc_sli_issue_iocb function and will return the error returned
8409 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8410 * functions which do not hold hbalock.
8411 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008412int
James Smart3772a992009-05-22 14:50:54 -04008413lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008414 struct lpfc_iocbq *piocb, uint32_t flag)
8415{
James Smart7e56aa22012-08-03 12:35:34 -04008416 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart92d7f7b2007-06-17 19:56:38 -05008417 unsigned long iflags;
8418 int rc;
8419
James Smart7e56aa22012-08-03 12:35:34 -04008420 if (phba->sli_rev == LPFC_SLI_REV4) {
8421 pring = &phba->sli.ring[ring_number];
8422 spin_lock_irqsave(&pring->ring_lock, iflags);
8423 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8424 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8425 } else {
8426 /* For now, SLI2/3 will still use hbalock */
8427 spin_lock_irqsave(&phba->hbalock, iflags);
8428 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8429 spin_unlock_irqrestore(&phba->hbalock, iflags);
8430 }
James Smart92d7f7b2007-06-17 19:56:38 -05008431 return rc;
8432}
8433
James Smarte59058c2008-08-24 21:49:00 -04008434/**
James Smart3621a712009-04-06 18:47:14 -04008435 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008436 * @phba: Pointer to HBA context object.
8437 *
8438 * This function is called while driver attaches with the
8439 * HBA to setup the extra ring. The extra ring is used
8440 * only when driver needs to support target mode functionality
8441 * or IP over FC functionalities.
8442 *
8443 * This function is called with no lock held.
8444 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008445static int
8446lpfc_extra_ring_setup( struct lpfc_hba *phba)
8447{
8448 struct lpfc_sli *psli;
8449 struct lpfc_sli_ring *pring;
8450
8451 psli = &phba->sli;
8452
8453 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008454
8455 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008456 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008457 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8458 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8459 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8460 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008461
James Smarta4bc3372006-12-02 13:34:16 -05008462 /* and give them to the extra ring */
8463 pring = &psli->ring[psli->extra_ring];
8464
James Smart7e56aa22012-08-03 12:35:34 -04008465 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8466 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8467 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8468 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008469
8470 /* Setup default profile for this ring */
8471 pring->iotag_max = 4096;
8472 pring->num_mask = 1;
8473 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008474 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8475 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008476 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8477 return 0;
8478}
8479
James Smartcb69f7d2011-12-13 13:21:57 -05008480/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8481 * @vport: pointer to virtual port object.
8482 * @ndlp: nodelist pointer for the impacted rport.
8483 *
8484 * The driver calls this routine in response to a XRI ABORT CQE
8485 * event from the port. In this event, the driver is required to
8486 * recover its login to the rport even though its login may be valid
8487 * from the driver's perspective. The failed ABTS notice from the
8488 * port indicates the rport is not responding.
8489 */
8490static void
8491lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8492 struct lpfc_nodelist *ndlp)
8493{
8494 struct Scsi_Host *shost;
8495 struct lpfc_hba *phba;
8496 unsigned long flags = 0;
8497
8498 shost = lpfc_shost_from_vport(vport);
8499 phba = vport->phba;
8500 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8501 lpfc_printf_log(phba, KERN_INFO,
8502 LOG_SLI, "3093 No rport recovery needed. "
8503 "rport in state 0x%x\n",
8504 ndlp->nlp_state);
8505 return;
8506 }
8507 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8508 "3094 Start rport recovery on shost id 0x%x "
8509 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8510 "flags 0x%x\n",
8511 shost->host_no, ndlp->nlp_DID,
8512 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8513 ndlp->nlp_flag);
8514 /*
8515 * The rport is not responding. Don't attempt ADISC recovery.
8516 * Remove the FCP-2 flag to force a PLOGI.
8517 */
8518 spin_lock_irqsave(shost->host_lock, flags);
8519 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8520 spin_unlock_irqrestore(shost->host_lock, flags);
8521 lpfc_disc_state_machine(vport, ndlp, NULL,
8522 NLP_EVT_DEVICE_RECOVERY);
8523 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8524 spin_lock_irqsave(shost->host_lock, flags);
8525 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8526 spin_unlock_irqrestore(shost->host_lock, flags);
8527 lpfc_disc_start(vport);
8528}
8529
8530/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8531 * @phba: Pointer to HBA context object.
8532 * @iocbq: Pointer to iocb object.
8533 *
8534 * The async_event handler calls this routine when it receives
8535 * an ASYNC_STATUS_CN event from the port. The port generates
8536 * this event when an Abort Sequence request to an rport fails
8537 * twice in succession. The abort could be originated by the
8538 * driver or by the port. The ABTS could have been for an ELS
8539 * or FCP IO. The port only generates this event when an ABTS
8540 * fails to complete after one retry.
8541 */
8542static void
8543lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8544 struct lpfc_iocbq *iocbq)
8545{
8546 struct lpfc_nodelist *ndlp = NULL;
8547 uint16_t rpi = 0, vpi = 0;
8548 struct lpfc_vport *vport = NULL;
8549
8550 /* The rpi in the ulpContext is vport-sensitive. */
8551 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8552 rpi = iocbq->iocb.ulpContext;
8553
8554 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8555 "3092 Port generated ABTS async event "
8556 "on vpi %d rpi %d status 0x%x\n",
8557 vpi, rpi, iocbq->iocb.ulpStatus);
8558
8559 vport = lpfc_find_vport_by_vpid(phba, vpi);
8560 if (!vport)
8561 goto err_exit;
8562 ndlp = lpfc_findnode_rpi(vport, rpi);
8563 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8564 goto err_exit;
8565
8566 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8567 lpfc_sli_abts_recover_port(vport, ndlp);
8568 return;
8569
8570 err_exit:
8571 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8572 "3095 Event Context not found, no "
8573 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8574 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8575 vpi, rpi);
8576}
8577
8578/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8579 * @phba: pointer to HBA context object.
8580 * @ndlp: nodelist pointer for the impacted rport.
8581 * @axri: pointer to the wcqe containing the failed exchange.
8582 *
8583 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8584 * port. The port generates this event when an abort exchange request to an
8585 * rport fails twice in succession with no reply. The abort could be originated
8586 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8587 */
8588void
8589lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8590 struct lpfc_nodelist *ndlp,
8591 struct sli4_wcqe_xri_aborted *axri)
8592{
8593 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008594 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008595
James Smart6b5151f2012-01-18 16:24:06 -05008596 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008597 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8598 "3115 Node Context not found, driver "
8599 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008600 return;
8601 }
8602
James Smartcb69f7d2011-12-13 13:21:57 -05008603 vport = ndlp->vport;
8604 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8605 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008606 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008607 ndlp->vport->vpi, ndlp->nlp_rpi,
8608 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008609 bf_get(lpfc_wcqe_xa_status, axri),
8610 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008611
James Smart5c1db2a2012-03-01 22:34:36 -05008612 /*
8613 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8614 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8615 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8616 */
8617 ext_status = axri->parameter & WCQE_PARAM_MASK;
8618 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8619 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008620 lpfc_sli_abts_recover_port(vport, ndlp);
8621}
8622
James Smarte59058c2008-08-24 21:49:00 -04008623/**
James Smart3621a712009-04-06 18:47:14 -04008624 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008625 * @phba: Pointer to HBA context object.
8626 * @pring: Pointer to driver SLI ring object.
8627 * @iocbq: Pointer to iocb object.
8628 *
8629 * This function is called by the slow ring event handler
8630 * function when there is an ASYNC event iocb in the ring.
8631 * This function is called with no lock held.
8632 * Currently this function handles only temperature related
8633 * ASYNC events. The function decodes the temperature sensor
8634 * event message and posts events for the management applications.
8635 **/
James Smart98c9ea52007-10-27 13:37:33 -04008636static void
James Smart57127f12007-10-27 13:37:05 -04008637lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8638 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8639{
8640 IOCB_t *icmd;
8641 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008642 struct temp_event temp_event_data;
8643 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008644 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008645
8646 icmd = &iocbq->iocb;
8647 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008648
James Smartcb69f7d2011-12-13 13:21:57 -05008649 switch (evt_code) {
8650 case ASYNC_TEMP_WARN:
8651 case ASYNC_TEMP_SAFE:
8652 temp_event_data.data = (uint32_t) icmd->ulpContext;
8653 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8654 if (evt_code == ASYNC_TEMP_WARN) {
8655 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8656 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8657 "0347 Adapter is very hot, please take "
8658 "corrective action. temperature : %d Celsius\n",
8659 (uint32_t) icmd->ulpContext);
8660 } else {
8661 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8662 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8663 "0340 Adapter temperature is OK now. "
8664 "temperature : %d Celsius\n",
8665 (uint32_t) icmd->ulpContext);
8666 }
8667
8668 /* Send temperature change event to applications */
8669 shost = lpfc_shost_from_vport(phba->pport);
8670 fc_host_post_vendor_event(shost, fc_get_event_number(),
8671 sizeof(temp_event_data), (char *) &temp_event_data,
8672 LPFC_NL_VENDOR_ID);
8673 break;
8674 case ASYNC_STATUS_CN:
8675 lpfc_sli_abts_err_handler(phba, iocbq);
8676 break;
8677 default:
James Smarta257bf92009-04-06 18:48:10 -04008678 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008680 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008681 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008682 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8683 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8684 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8685 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008686 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008687 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8688 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8689 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8690 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8691
James Smartcb69f7d2011-12-13 13:21:57 -05008692 break;
James Smart57127f12007-10-27 13:37:05 -04008693 }
James Smart57127f12007-10-27 13:37:05 -04008694}
8695
8696
James Smarte59058c2008-08-24 21:49:00 -04008697/**
James Smart3621a712009-04-06 18:47:14 -04008698 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008699 * @phba: Pointer to HBA context object.
8700 *
8701 * lpfc_sli_setup sets up rings of the SLI interface with
8702 * number of iocbs per ring and iotags. This function is
8703 * called while driver attach to the HBA and before the
8704 * interrupts are enabled. So there is no need for locking.
8705 *
8706 * This function always returns 0.
8707 **/
dea31012005-04-17 16:05:31 -05008708int
8709lpfc_sli_setup(struct lpfc_hba *phba)
8710{
James Smarted957682007-06-17 19:56:37 -05008711 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008712 struct lpfc_sli *psli = &phba->sli;
8713 struct lpfc_sli_ring *pring;
8714
8715 psli->num_rings = MAX_CONFIGURED_RINGS;
8716 psli->sli_flag = 0;
8717 psli->fcp_ring = LPFC_FCP_RING;
8718 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008719 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008720
James Bottomley604a3e32005-10-29 10:28:33 -05008721 psli->iocbq_lookup = NULL;
8722 psli->iocbq_lookup_len = 0;
8723 psli->last_iotag = 0;
8724
dea31012005-04-17 16:05:31 -05008725 for (i = 0; i < psli->num_rings; i++) {
8726 pring = &psli->ring[i];
8727 switch (i) {
8728 case LPFC_FCP_RING: /* ring 0 - FCP */
8729 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008730 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8731 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8732 pring->sli.sli3.numCiocb +=
8733 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8734 pring->sli.sli3.numRiocb +=
8735 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8736 pring->sli.sli3.numCiocb +=
8737 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8738 pring->sli.sli3.numRiocb +=
8739 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8740 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008741 SLI3_IOCB_CMD_SIZE :
8742 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008743 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008744 SLI3_IOCB_RSP_SIZE :
8745 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008746 pring->iotag_ctr = 0;
8747 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008748 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008749 pring->fast_iotag = pring->iotag_max;
8750 pring->num_mask = 0;
8751 break;
James Smarta4bc3372006-12-02 13:34:16 -05008752 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008753 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008754 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8755 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8756 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008757 SLI3_IOCB_CMD_SIZE :
8758 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008759 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008760 SLI3_IOCB_RSP_SIZE :
8761 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008762 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008763 pring->num_mask = 0;
8764 break;
8765 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8766 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008767 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8768 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8769 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008770 SLI3_IOCB_CMD_SIZE :
8771 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008772 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008773 SLI3_IOCB_RSP_SIZE :
8774 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008775 pring->fast_iotag = 0;
8776 pring->iotag_ctr = 0;
8777 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008778 pring->lpfc_sli_rcv_async_status =
8779 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008780 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008781 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008782 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8783 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008784 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008785 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008786 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008787 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8788 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008789 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008790 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008791 pring->prt[2].profile = 0; /* Mask 2 */
8792 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008793 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008794 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008795 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008796 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008797 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008798 pring->prt[3].profile = 0; /* Mask 3 */
8799 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008800 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008801 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008802 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008803 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008804 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008805 /* abort unsolicited sequence */
8806 pring->prt[4].profile = 0; /* Mask 4 */
8807 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8808 pring->prt[4].type = FC_TYPE_BLS;
8809 pring->prt[4].lpfc_sli_rcv_unsol_event =
8810 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008811 break;
8812 }
James Smart7e56aa22012-08-03 12:35:34 -04008813 totiocbsize += (pring->sli.sli3.numCiocb *
8814 pring->sli.sli3.sizeCiocb) +
8815 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05008816 }
James Smarted957682007-06-17 19:56:37 -05008817 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008818 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008819 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8820 "SLI2 SLIM Data: x%x x%lx\n",
8821 phba->brd_no, totiocbsize,
8822 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008823 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008824 if (phba->cfg_multi_ring_support == 2)
8825 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008826
8827 return 0;
8828}
8829
James Smarte59058c2008-08-24 21:49:00 -04008830/**
James Smart3621a712009-04-06 18:47:14 -04008831 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008832 * @phba: Pointer to HBA context object.
8833 *
8834 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8835 * ring. This function also initializes ring indices of each ring.
8836 * This function is called during the initialization of the SLI
8837 * interface of an HBA.
8838 * This function is called with no lock held and always returns
8839 * 1.
8840 **/
dea31012005-04-17 16:05:31 -05008841int
James Smart2e0fef82007-06-17 19:56:36 -05008842lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008843{
8844 struct lpfc_sli *psli;
8845 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008846 int i;
dea31012005-04-17 16:05:31 -05008847
8848 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008849 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008850 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008851 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008852 /* Initialize list headers for txq and txcmplq as double linked lists */
8853 for (i = 0; i < psli->num_rings; i++) {
8854 pring = &psli->ring[i];
8855 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04008856 pring->sli.sli3.next_cmdidx = 0;
8857 pring->sli.sli3.local_getidx = 0;
8858 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05008859 INIT_LIST_HEAD(&pring->txq);
8860 INIT_LIST_HEAD(&pring->txcmplq);
8861 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008862 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008863 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04008864 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05008865 }
James Smart2e0fef82007-06-17 19:56:36 -05008866 spin_unlock_irq(&phba->hbalock);
8867 return 1;
dea31012005-04-17 16:05:31 -05008868}
8869
James Smarte59058c2008-08-24 21:49:00 -04008870/**
James Smart04c68492009-05-22 14:52:52 -04008871 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8872 * @phba: Pointer to HBA context object.
8873 *
8874 * This routine flushes the mailbox command subsystem. It will unconditionally
8875 * flush all the mailbox commands in the three possible stages in the mailbox
8876 * command sub-system: pending mailbox command queue; the outstanding mailbox
8877 * command; and completed mailbox command queue. It is caller's responsibility
8878 * to make sure that the driver is in the proper state to flush the mailbox
8879 * command sub-system. Namely, the posting of mailbox commands into the
8880 * pending mailbox command queue from the various clients must be stopped;
8881 * either the HBA is in a state that it will never works on the outstanding
8882 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8883 * mailbox command has been completed.
8884 **/
8885static void
8886lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8887{
8888 LIST_HEAD(completions);
8889 struct lpfc_sli *psli = &phba->sli;
8890 LPFC_MBOXQ_t *pmb;
8891 unsigned long iflag;
8892
8893 /* Flush all the mailbox commands in the mbox system */
8894 spin_lock_irqsave(&phba->hbalock, iflag);
8895 /* The pending mailbox command queue */
8896 list_splice_init(&phba->sli.mboxq, &completions);
8897 /* The outstanding active mailbox command */
8898 if (psli->mbox_active) {
8899 list_add_tail(&psli->mbox_active->list, &completions);
8900 psli->mbox_active = NULL;
8901 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8902 }
8903 /* The completed mailbox command queue */
8904 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8905 spin_unlock_irqrestore(&phba->hbalock, iflag);
8906
8907 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8908 while (!list_empty(&completions)) {
8909 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8910 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8911 if (pmb->mbox_cmpl)
8912 pmb->mbox_cmpl(phba, pmb);
8913 }
8914}
8915
8916/**
James Smart3621a712009-04-06 18:47:14 -04008917 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008918 * @vport: Pointer to virtual port object.
8919 *
8920 * lpfc_sli_host_down is called to clean up the resources
8921 * associated with a vport before destroying virtual
8922 * port data structures.
8923 * This function does following operations:
8924 * - Free discovery resources associated with this virtual
8925 * port.
8926 * - Free iocbs associated with this virtual port in
8927 * the txq.
8928 * - Send abort for all iocb commands associated with this
8929 * vport in txcmplq.
8930 *
8931 * This function is called with no lock held and always returns 1.
8932 **/
dea31012005-04-17 16:05:31 -05008933int
James Smart92d7f7b2007-06-17 19:56:38 -05008934lpfc_sli_host_down(struct lpfc_vport *vport)
8935{
James Smart858c9f62007-06-17 19:56:39 -05008936 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008937 struct lpfc_hba *phba = vport->phba;
8938 struct lpfc_sli *psli = &phba->sli;
8939 struct lpfc_sli_ring *pring;
8940 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008941 int i;
8942 unsigned long flags = 0;
8943 uint16_t prev_pring_flag;
8944
8945 lpfc_cleanup_discovery_resources(vport);
8946
8947 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008948 for (i = 0; i < psli->num_rings; i++) {
8949 pring = &psli->ring[i];
8950 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008951 /* Only slow rings */
8952 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008953 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008954 /* Set the lpfc data pending flag */
8955 set_bit(LPFC_DATA_READY, &phba->data_flags);
8956 }
James Smart92d7f7b2007-06-17 19:56:38 -05008957 /*
8958 * Error everything on the txq since these iocbs have not been
8959 * given to the FW yet.
8960 */
James Smart92d7f7b2007-06-17 19:56:38 -05008961 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8962 if (iocb->vport != vport)
8963 continue;
James Smart858c9f62007-06-17 19:56:39 -05008964 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008965 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008966 }
8967
8968 /* Next issue ABTS for everything on the txcmplq */
8969 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8970 list) {
8971 if (iocb->vport != vport)
8972 continue;
8973 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8974 }
8975
8976 pring->flag = prev_pring_flag;
8977 }
8978
8979 spin_unlock_irqrestore(&phba->hbalock, flags);
8980
James Smarta257bf92009-04-06 18:48:10 -04008981 /* Cancel all the IOCBs from the completions list */
8982 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8983 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008984 return 1;
8985}
8986
James Smarte59058c2008-08-24 21:49:00 -04008987/**
James Smart3621a712009-04-06 18:47:14 -04008988 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008989 * @phba: Pointer to HBA context object.
8990 *
8991 * This function cleans up all iocb, buffers, mailbox commands
8992 * while shutting down the HBA. This function is called with no
8993 * lock held and always returns 1.
8994 * This function does the following to cleanup driver resources:
8995 * - Free discovery resources for each virtual port
8996 * - Cleanup any pending fabric iocbs
8997 * - Iterate through the iocb txq and free each entry
8998 * in the list.
8999 * - Free up any buffer posted to the HBA
9000 * - Free mailbox commands in the mailbox queue.
9001 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009002int
James Smart2e0fef82007-06-17 19:56:36 -05009003lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009004{
James Smart2534ba72007-04-25 09:52:20 -04009005 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009006 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009007 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009008 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009009 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009010 int i;
9011
9012 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009013 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009014
dea31012005-04-17 16:05:31 -05009015 lpfc_hba_down_prep(phba);
9016
James Smart92d7f7b2007-06-17 19:56:38 -05009017 lpfc_fabric_abort_hba(phba);
9018
James Smart2e0fef82007-06-17 19:56:36 -05009019 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009020 for (i = 0; i < psli->num_rings; i++) {
9021 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009022 /* Only slow rings */
9023 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009024 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009025 /* Set the lpfc data pending flag */
9026 set_bit(LPFC_DATA_READY, &phba->data_flags);
9027 }
dea31012005-04-17 16:05:31 -05009028
9029 /*
9030 * Error everything on the txq since these iocbs have not been
9031 * given to the FW yet.
9032 */
James Smart2534ba72007-04-25 09:52:20 -04009033 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009034 pring->txq_cnt = 0;
9035
dea31012005-04-17 16:05:31 -05009036 }
James Smart2e0fef82007-06-17 19:56:36 -05009037 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009038
James Smarta257bf92009-04-06 18:48:10 -04009039 /* Cancel all the IOCBs from the completions list */
9040 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9041 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009042
James Smart0ff10d42008-01-11 01:52:36 -05009043 spin_lock_irqsave(&phba->hbalock, flags);
9044 list_splice_init(&phba->elsbuf, &completions);
9045 phba->elsbuf_cnt = 0;
9046 phba->elsbuf_prev_cnt = 0;
9047 spin_unlock_irqrestore(&phba->hbalock, flags);
9048
9049 while (!list_empty(&completions)) {
9050 list_remove_head(&completions, buf_ptr,
9051 struct lpfc_dmabuf, list);
9052 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9053 kfree(buf_ptr);
9054 }
9055
dea31012005-04-17 16:05:31 -05009056 /* Return any active mbox cmds */
9057 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009058
James Smartda0436e2009-05-22 14:51:39 -04009059 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009060 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009061 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009062
James Smartda0436e2009-05-22 14:51:39 -04009063 return 1;
9064}
James Smart92d7f7b2007-06-17 19:56:38 -05009065
James Smartda0436e2009-05-22 14:51:39 -04009066/**
James Smart3621a712009-04-06 18:47:14 -04009067 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009068 * @srcp: Source memory pointer.
9069 * @destp: Destination memory pointer.
9070 * @cnt: Number of words required to be copied.
9071 *
9072 * This function is used for copying data between driver memory
9073 * and the SLI memory. This function also changes the endianness
9074 * of each word if native endianness is different from SLI
9075 * endianness. This function can be called with or without
9076 * lock.
9077 **/
dea31012005-04-17 16:05:31 -05009078void
9079lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9080{
9081 uint32_t *src = srcp;
9082 uint32_t *dest = destp;
9083 uint32_t ldata;
9084 int i;
9085
9086 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9087 ldata = *src;
9088 ldata = le32_to_cpu(ldata);
9089 *dest = ldata;
9090 src++;
9091 dest++;
9092 }
9093}
9094
James Smarte59058c2008-08-24 21:49:00 -04009095
9096/**
James Smarta0c87cb2009-07-19 10:01:10 -04009097 * lpfc_sli_bemem_bcopy - SLI memory copy function
9098 * @srcp: Source memory pointer.
9099 * @destp: Destination memory pointer.
9100 * @cnt: Number of words required to be copied.
9101 *
9102 * This function is used for copying data between a data structure
9103 * with big endian representation to local endianness.
9104 * This function can be called with or without lock.
9105 **/
9106void
9107lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9108{
9109 uint32_t *src = srcp;
9110 uint32_t *dest = destp;
9111 uint32_t ldata;
9112 int i;
9113
9114 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9115 ldata = *src;
9116 ldata = be32_to_cpu(ldata);
9117 *dest = ldata;
9118 src++;
9119 dest++;
9120 }
9121}
9122
9123/**
James Smart3621a712009-04-06 18:47:14 -04009124 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009125 * @phba: Pointer to HBA context object.
9126 * @pring: Pointer to driver SLI ring object.
9127 * @mp: Pointer to driver buffer object.
9128 *
9129 * This function is called with no lock held.
9130 * It always return zero after adding the buffer to the postbufq
9131 * buffer list.
9132 **/
dea31012005-04-17 16:05:31 -05009133int
James Smart2e0fef82007-06-17 19:56:36 -05009134lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9135 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009136{
9137 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9138 later */
James Smart2e0fef82007-06-17 19:56:36 -05009139 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009140 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009141 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009142 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009143 return 0;
9144}
9145
James Smarte59058c2008-08-24 21:49:00 -04009146/**
James Smart3621a712009-04-06 18:47:14 -04009147 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009148 * @phba: Pointer to HBA context object.
9149 *
9150 * When HBQ is enabled, buffers are searched based on tags. This function
9151 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9152 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9153 * does not conflict with tags of buffer posted for unsolicited events.
9154 * The function returns the allocated tag. The function is called with
9155 * no locks held.
9156 **/
James Smart76bb24e2007-10-27 13:38:00 -04009157uint32_t
9158lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9159{
9160 spin_lock_irq(&phba->hbalock);
9161 phba->buffer_tag_count++;
9162 /*
9163 * Always set the QUE_BUFTAG_BIT to distiguish between
9164 * a tag assigned by HBQ.
9165 */
9166 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9167 spin_unlock_irq(&phba->hbalock);
9168 return phba->buffer_tag_count;
9169}
9170
James Smarte59058c2008-08-24 21:49:00 -04009171/**
James Smart3621a712009-04-06 18:47:14 -04009172 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009173 * @phba: Pointer to HBA context object.
9174 * @pring: Pointer to driver SLI ring object.
9175 * @tag: Buffer tag.
9176 *
9177 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9178 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9179 * iocb is posted to the response ring with the tag of the buffer.
9180 * This function searches the pring->postbufq list using the tag
9181 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9182 * iocb. If the buffer is found then lpfc_dmabuf object of the
9183 * buffer is returned to the caller else NULL is returned.
9184 * This function is called with no lock held.
9185 **/
James Smart76bb24e2007-10-27 13:38:00 -04009186struct lpfc_dmabuf *
9187lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9188 uint32_t tag)
9189{
9190 struct lpfc_dmabuf *mp, *next_mp;
9191 struct list_head *slp = &pring->postbufq;
9192
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009193 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009194 spin_lock_irq(&phba->hbalock);
9195 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9196 if (mp->buffer_tag == tag) {
9197 list_del_init(&mp->list);
9198 pring->postbufq_cnt--;
9199 spin_unlock_irq(&phba->hbalock);
9200 return mp;
9201 }
9202 }
9203
9204 spin_unlock_irq(&phba->hbalock);
9205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009206 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009207 "ring %d Data x%lx x%p x%p x%x\n",
9208 pring->ringno, (unsigned long) tag,
9209 slp->next, slp->prev, pring->postbufq_cnt);
9210
9211 return NULL;
9212}
dea31012005-04-17 16:05:31 -05009213
James Smarte59058c2008-08-24 21:49:00 -04009214/**
James Smart3621a712009-04-06 18:47:14 -04009215 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009216 * @phba: Pointer to HBA context object.
9217 * @pring: Pointer to driver SLI ring object.
9218 * @phys: DMA address of the buffer.
9219 *
9220 * This function searches the buffer list using the dma_address
9221 * of unsolicited event to find the driver's lpfc_dmabuf object
9222 * corresponding to the dma_address. The function returns the
9223 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9224 * This function is called by the ct and els unsolicited event
9225 * handlers to get the buffer associated with the unsolicited
9226 * event.
9227 *
9228 * This function is called with no lock held.
9229 **/
dea31012005-04-17 16:05:31 -05009230struct lpfc_dmabuf *
9231lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9232 dma_addr_t phys)
9233{
9234 struct lpfc_dmabuf *mp, *next_mp;
9235 struct list_head *slp = &pring->postbufq;
9236
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009237 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009238 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009239 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9240 if (mp->phys == phys) {
9241 list_del_init(&mp->list);
9242 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009243 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009244 return mp;
9245 }
9246 }
9247
James Smart2e0fef82007-06-17 19:56:36 -05009248 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009250 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009251 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009252 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009253 slp->next, slp->prev, pring->postbufq_cnt);
9254 return NULL;
9255}
9256
James Smarte59058c2008-08-24 21:49:00 -04009257/**
James Smart3621a712009-04-06 18:47:14 -04009258 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009259 * @phba: Pointer to HBA context object.
9260 * @cmdiocb: Pointer to driver command iocb object.
9261 * @rspiocb: Pointer to driver response iocb object.
9262 *
9263 * This function is the completion handler for the abort iocbs for
9264 * ELS commands. This function is called from the ELS ring event
9265 * handler with no lock held. This function frees memory resources
9266 * associated with the abort iocb.
9267 **/
dea31012005-04-17 16:05:31 -05009268static void
James Smart2e0fef82007-06-17 19:56:36 -05009269lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9270 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009271{
James Smart2e0fef82007-06-17 19:56:36 -05009272 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009273 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009274 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009275
9276 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009277
9278 /*
9279 * Assume that the port already completed and returned, or
9280 * will return the iocb. Just Log the message.
9281 */
James Smart2680eea2007-04-25 09:52:55 -04009282 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9283 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9284
James Smart2e0fef82007-06-17 19:56:36 -05009285 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009286 if (phba->sli_rev < LPFC_SLI_REV4) {
9287 if (abort_iotag != 0 &&
9288 abort_iotag <= phba->sli.last_iotag)
9289 abort_iocb =
9290 phba->sli.iocbq_lookup[abort_iotag];
9291 } else
9292 /* For sli4 the abort_tag is the XRI,
9293 * so the abort routine puts the iotag of the iocb
9294 * being aborted in the context field of the abort
9295 * IOCB.
9296 */
9297 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009298
James Smart2a9bf3d2010-06-07 15:24:45 -04009299 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9300 "0327 Cannot abort els iocb %p "
9301 "with tag %x context %x, abort status %x, "
9302 "abort code %x\n",
9303 abort_iocb, abort_iotag, abort_context,
9304 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009305
James Smartff78d8f2011-12-13 13:21:35 -05009306 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009307 }
James Bottomley604a3e32005-10-29 10:28:33 -05009308 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009309 return;
9310}
9311
James Smarte59058c2008-08-24 21:49:00 -04009312/**
James Smart3621a712009-04-06 18:47:14 -04009313 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009314 * @phba: Pointer to HBA context object.
9315 * @cmdiocb: Pointer to driver command iocb object.
9316 * @rspiocb: Pointer to driver response iocb object.
9317 *
9318 * The function is called from SLI ring event handler with no
9319 * lock held. This function is the completion handler for ELS commands
9320 * which are aborted. The function frees memory resources used for
9321 * the aborted ELS commands.
9322 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009323static void
9324lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9325 struct lpfc_iocbq *rspiocb)
9326{
9327 IOCB_t *irsp = &rspiocb->iocb;
9328
9329 /* ELS cmd tag <ulpIoTag> completes */
9330 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009331 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009332 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009333 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009334 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009335 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9336 lpfc_ct_free_iocb(phba, cmdiocb);
9337 else
9338 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009339 return;
9340}
9341
James Smarte59058c2008-08-24 21:49:00 -04009342/**
James Smart5af5eee2010-10-22 11:06:38 -04009343 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009344 * @phba: Pointer to HBA context object.
9345 * @pring: Pointer to driver SLI ring object.
9346 * @cmdiocb: Pointer to driver command iocb object.
9347 *
James Smart5af5eee2010-10-22 11:06:38 -04009348 * This function issues an abort iocb for the provided command iocb down to
9349 * the port. Other than the case the outstanding command iocb is an abort
9350 * request, this function issues abort out unconditionally. This function is
9351 * called with hbalock held. The function returns 0 when it fails due to
9352 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009353 **/
James Smart5af5eee2010-10-22 11:06:38 -04009354static int
9355lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009356 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009357{
James Smart2e0fef82007-06-17 19:56:36 -05009358 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009359 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009360 IOCB_t *icmd = NULL;
9361 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009362 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009363 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009364
James Smart92d7f7b2007-06-17 19:56:38 -05009365 /*
9366 * There are certain command types we don't want to abort. And we
9367 * don't want to abort commands that are already in the process of
9368 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009369 */
9370 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009371 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009372 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9373 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009374 return 0;
9375
dea31012005-04-17 16:05:31 -05009376 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009377 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009378 if (abtsiocbp == NULL)
9379 return 0;
dea31012005-04-17 16:05:31 -05009380
James Smart07951072007-04-25 09:51:38 -04009381 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009382 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009383 */
9384 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9385
dea31012005-04-17 16:05:31 -05009386 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009387 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9388 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009389 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009390 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009391 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9392 }
James Smartda0436e2009-05-22 14:51:39 -04009393 else
9394 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009395 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009396 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009397
James Smart5ffc2662009-11-18 15:39:44 -05009398 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9399 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009400 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9401 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009402
James Smart2e0fef82007-06-17 19:56:36 -05009403 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009404 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9405 else
9406 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9407
9408 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009409
James Smarte8b62012007-08-02 11:10:09 -04009410 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9411 "0339 Abort xri x%x, original iotag x%x, "
9412 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009413 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009414 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009415 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009416
9417 if (phba->sli_rev == LPFC_SLI_REV4) {
9418 /* Note: both hbalock and ring_lock need to be set here */
9419 spin_lock_irqsave(&pring->ring_lock, iflags);
9420 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9421 abtsiocbp, 0);
9422 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9423 } else {
9424 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9425 abtsiocbp, 0);
9426 }
James Smart07951072007-04-25 09:51:38 -04009427
James Smartd7c255b2008-08-24 21:50:00 -04009428 if (retval)
9429 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009430
9431 /*
9432 * Caller to this routine should check for IOCB_ERROR
9433 * and handle it properly. This routine no longer removes
9434 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9435 */
9436 return retval;
9437}
9438
9439/**
9440 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9441 * @phba: Pointer to HBA context object.
9442 * @pring: Pointer to driver SLI ring object.
9443 * @cmdiocb: Pointer to driver command iocb object.
9444 *
9445 * This function issues an abort iocb for the provided command iocb. In case
9446 * of unloading, the abort iocb will not be issued to commands on the ELS
9447 * ring. Instead, the callback function shall be changed to those commands
9448 * so that nothing happens when them finishes. This function is called with
9449 * hbalock held. The function returns 0 when the command iocb is an abort
9450 * request.
9451 **/
9452int
9453lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9454 struct lpfc_iocbq *cmdiocb)
9455{
9456 struct lpfc_vport *vport = cmdiocb->vport;
9457 int retval = IOCB_ERROR;
9458 IOCB_t *icmd = NULL;
9459
9460 /*
9461 * There are certain command types we don't want to abort. And we
9462 * don't want to abort commands that are already in the process of
9463 * being aborted.
9464 */
9465 icmd = &cmdiocb->iocb;
9466 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9467 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9468 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9469 return 0;
9470
9471 /*
9472 * If we're unloading, don't abort iocb on the ELS ring, but change
9473 * the callback so that nothing happens when it finishes.
9474 */
9475 if ((vport->load_flag & FC_UNLOADING) &&
9476 (pring->ringno == LPFC_ELS_RING)) {
9477 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9478 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9479 else
9480 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9481 goto abort_iotag_exit;
9482 }
9483
9484 /* Now, we try to issue the abort to the cmdiocb out */
9485 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9486
James Smart07951072007-04-25 09:51:38 -04009487abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009488 /*
9489 * Caller to this routine should check for IOCB_ERROR
9490 * and handle it properly. This routine no longer removes
9491 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009492 */
James Smart2e0fef82007-06-17 19:56:36 -05009493 return retval;
dea31012005-04-17 16:05:31 -05009494}
9495
James Smarte59058c2008-08-24 21:49:00 -04009496/**
James Smart5af5eee2010-10-22 11:06:38 -04009497 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9498 * @phba: Pointer to HBA context object.
9499 * @pring: Pointer to driver SLI ring object.
9500 *
9501 * This function aborts all iocbs in the given ring and frees all the iocb
9502 * objects in txq. This function issues abort iocbs unconditionally for all
9503 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9504 * to complete before the return of this function. The caller is not required
9505 * to hold any locks.
9506 **/
9507static void
9508lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9509{
9510 LIST_HEAD(completions);
9511 struct lpfc_iocbq *iocb, *next_iocb;
9512
9513 if (pring->ringno == LPFC_ELS_RING)
9514 lpfc_fabric_abort_hba(phba);
9515
9516 spin_lock_irq(&phba->hbalock);
9517
9518 /* Take off all the iocbs on txq for cancelling */
9519 list_splice_init(&pring->txq, &completions);
9520 pring->txq_cnt = 0;
9521
9522 /* Next issue ABTS for everything on the txcmplq */
9523 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9524 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9525
9526 spin_unlock_irq(&phba->hbalock);
9527
9528 /* Cancel all the IOCBs from the completions list */
9529 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9530 IOERR_SLI_ABORTED);
9531}
9532
9533/**
9534 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9535 * @phba: pointer to lpfc HBA data structure.
9536 *
9537 * This routine will abort all pending and outstanding iocbs to an HBA.
9538 **/
9539void
9540lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9541{
9542 struct lpfc_sli *psli = &phba->sli;
9543 struct lpfc_sli_ring *pring;
9544 int i;
9545
9546 for (i = 0; i < psli->num_rings; i++) {
9547 pring = &psli->ring[i];
9548 lpfc_sli_iocb_ring_abort(phba, pring);
9549 }
9550}
9551
9552/**
James Smart3621a712009-04-06 18:47:14 -04009553 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009554 * @iocbq: Pointer to driver iocb object.
9555 * @vport: Pointer to driver virtual port object.
9556 * @tgt_id: SCSI ID of the target.
9557 * @lun_id: LUN ID of the scsi device.
9558 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9559 *
James Smart3621a712009-04-06 18:47:14 -04009560 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009561 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9562 * 0 if the filtering criteria is met for the given iocb and will return
9563 * 1 if the filtering criteria is not met.
9564 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9565 * given iocb is for the SCSI device specified by vport, tgt_id and
9566 * lun_id parameter.
9567 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9568 * given iocb is for the SCSI target specified by vport and tgt_id
9569 * parameters.
9570 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9571 * given iocb is for the SCSI host associated with the given vport.
9572 * This function is called with no locks held.
9573 **/
dea31012005-04-17 16:05:31 -05009574static int
James Smart51ef4c22007-08-02 11:10:31 -04009575lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9576 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009577 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009578{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009579 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009580 int rc = 1;
9581
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009582 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9583 return rc;
9584
James Smart51ef4c22007-08-02 11:10:31 -04009585 if (iocbq->vport != vport)
9586 return rc;
9587
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009588 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009589
James Smart495a7142008-06-14 22:52:59 -04009590 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009591 return rc;
9592
9593 switch (ctx_cmd) {
9594 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009595 if ((lpfc_cmd->rdata->pnode) &&
9596 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9597 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009598 rc = 0;
9599 break;
9600 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009601 if ((lpfc_cmd->rdata->pnode) &&
9602 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009603 rc = 0;
9604 break;
dea31012005-04-17 16:05:31 -05009605 case LPFC_CTX_HOST:
9606 rc = 0;
9607 break;
9608 default:
9609 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009610 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009611 break;
9612 }
9613
9614 return rc;
9615}
9616
James Smarte59058c2008-08-24 21:49:00 -04009617/**
James Smart3621a712009-04-06 18:47:14 -04009618 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009619 * @vport: Pointer to virtual port.
9620 * @tgt_id: SCSI ID of the target.
9621 * @lun_id: LUN ID of the scsi device.
9622 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9623 *
9624 * This function returns number of FCP commands pending for the vport.
9625 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9626 * commands pending on the vport associated with SCSI device specified
9627 * by tgt_id and lun_id parameters.
9628 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9629 * commands pending on the vport associated with SCSI target specified
9630 * by tgt_id parameter.
9631 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9632 * commands pending on the vport.
9633 * This function returns the number of iocbs which satisfy the filter.
9634 * This function is called without any lock held.
9635 **/
dea31012005-04-17 16:05:31 -05009636int
James Smart51ef4c22007-08-02 11:10:31 -04009637lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9638 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009639{
James Smart51ef4c22007-08-02 11:10:31 -04009640 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009641 struct lpfc_iocbq *iocbq;
9642 int sum, i;
dea31012005-04-17 16:05:31 -05009643
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009644 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9645 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009646
James Smart51ef4c22007-08-02 11:10:31 -04009647 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9648 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009649 sum++;
dea31012005-04-17 16:05:31 -05009650 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009651
dea31012005-04-17 16:05:31 -05009652 return sum;
9653}
9654
James Smarte59058c2008-08-24 21:49:00 -04009655/**
James Smart3621a712009-04-06 18:47:14 -04009656 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009657 * @phba: Pointer to HBA context object
9658 * @cmdiocb: Pointer to command iocb object.
9659 * @rspiocb: Pointer to response iocb object.
9660 *
9661 * This function is called when an aborted FCP iocb completes. This
9662 * function is called by the ring event handler with no lock held.
9663 * This function frees the iocb.
9664 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009665void
James Smart2e0fef82007-06-17 19:56:36 -05009666lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9667 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009668{
James Smartcb69f7d2011-12-13 13:21:57 -05009669 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9670 "3096 ABORT_XRI_CN completing on xri x%x "
9671 "original iotag x%x, abort cmd iotag x%x "
9672 "status 0x%x, reason 0x%x\n",
9673 cmdiocb->iocb.un.acxri.abortContextTag,
9674 cmdiocb->iocb.un.acxri.abortIoTag,
9675 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9676 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009677 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009678 return;
9679}
9680
James Smarte59058c2008-08-24 21:49:00 -04009681/**
James Smart3621a712009-04-06 18:47:14 -04009682 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009683 * @vport: Pointer to virtual port.
9684 * @pring: Pointer to driver SLI ring object.
9685 * @tgt_id: SCSI ID of the target.
9686 * @lun_id: LUN ID of the scsi device.
9687 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9688 *
9689 * This function sends an abort command for every SCSI command
9690 * associated with the given virtual port pending on the ring
9691 * filtered by lpfc_sli_validate_fcp_iocb function.
9692 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9693 * FCP iocbs associated with lun specified by tgt_id and lun_id
9694 * parameters
9695 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9696 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9697 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9698 * FCP iocbs associated with virtual port.
9699 * This function returns number of iocbs it failed to abort.
9700 * This function is called with no locks held.
9701 **/
dea31012005-04-17 16:05:31 -05009702int
James Smart51ef4c22007-08-02 11:10:31 -04009703lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9704 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009705{
James Smart51ef4c22007-08-02 11:10:31 -04009706 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009707 struct lpfc_iocbq *iocbq;
9708 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009709 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009710 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009711 int i;
dea31012005-04-17 16:05:31 -05009712
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009713 for (i = 1; i <= phba->sli.last_iotag; i++) {
9714 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009715
James Smart51ef4c22007-08-02 11:10:31 -04009716 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009717 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009718 continue;
9719
9720 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009721 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009722 if (abtsiocb == NULL) {
9723 errcnt++;
9724 continue;
9725 }
dea31012005-04-17 16:05:31 -05009726
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009727 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009728 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9729 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009730 if (phba->sli_rev == LPFC_SLI_REV4)
9731 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9732 else
9733 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009734 abtsiocb->iocb.ulpLe = 1;
9735 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009736 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009737
James Smart5ffc2662009-11-18 15:39:44 -05009738 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9739 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009740 if (iocbq->iocb_flag & LPFC_IO_FCP)
9741 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009742
James Smart2e0fef82007-06-17 19:56:36 -05009743 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009744 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9745 else
9746 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9747
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009748 /* Setup callback routine and issue the command. */
9749 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009750 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9751 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009752 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009753 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009754 errcnt++;
9755 continue;
9756 }
9757 }
9758
9759 return errcnt;
9760}
9761
James Smarte59058c2008-08-24 21:49:00 -04009762/**
James Smart3621a712009-04-06 18:47:14 -04009763 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009764 * @phba: Pointer to HBA context object.
9765 * @cmdiocbq: Pointer to command iocb.
9766 * @rspiocbq: Pointer to response iocb.
9767 *
9768 * This function is the completion handler for iocbs issued using
9769 * lpfc_sli_issue_iocb_wait function. This function is called by the
9770 * ring event handler function without any lock held. This function
9771 * can be called from both worker thread context and interrupt
9772 * context. This function also can be called from other thread which
9773 * cleans up the SLI layer objects.
9774 * This function copy the contents of the response iocb to the
9775 * response iocb memory object provided by the caller of
9776 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9777 * sleeps for the iocb completion.
9778 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009779static void
9780lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9781 struct lpfc_iocbq *cmdiocbq,
9782 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009783{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009784 wait_queue_head_t *pdone_q;
9785 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009786 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009787
James Smart2e0fef82007-06-17 19:56:36 -05009788 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009789 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9790 if (cmdiocbq->context2 && rspiocbq)
9791 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9792 &rspiocbq->iocb, sizeof(IOCB_t));
9793
James Smart0f65ff62010-02-26 14:14:23 -05009794 /* Set the exchange busy flag for task management commands */
9795 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9796 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9797 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9798 cur_iocbq);
9799 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9800 }
9801
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009802 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009803 if (pdone_q)
9804 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009805 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009806 return;
9807}
9808
James Smarte59058c2008-08-24 21:49:00 -04009809/**
James Smartd11e31d2009-06-10 17:23:06 -04009810 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9811 * @phba: Pointer to HBA context object..
9812 * @piocbq: Pointer to command iocb.
9813 * @flag: Flag to test.
9814 *
9815 * This routine grabs the hbalock and then test the iocb_flag to
9816 * see if the passed in flag is set.
9817 * Returns:
9818 * 1 if flag is set.
9819 * 0 if flag is not set.
9820 **/
9821static int
9822lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9823 struct lpfc_iocbq *piocbq, uint32_t flag)
9824{
9825 unsigned long iflags;
9826 int ret;
9827
9828 spin_lock_irqsave(&phba->hbalock, iflags);
9829 ret = piocbq->iocb_flag & flag;
9830 spin_unlock_irqrestore(&phba->hbalock, iflags);
9831 return ret;
9832
9833}
9834
9835/**
James Smart3621a712009-04-06 18:47:14 -04009836 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009837 * @phba: Pointer to HBA context object..
9838 * @pring: Pointer to sli ring.
9839 * @piocb: Pointer to command iocb.
9840 * @prspiocbq: Pointer to response iocb.
9841 * @timeout: Timeout in number of seconds.
9842 *
9843 * This function issues the iocb to firmware and waits for the
9844 * iocb to complete. If the iocb command is not
9845 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9846 * Caller should not free the iocb resources if this function
9847 * returns IOCB_TIMEDOUT.
9848 * The function waits for the iocb completion using an
9849 * non-interruptible wait.
9850 * This function will sleep while waiting for iocb completion.
9851 * So, this function should not be called from any context which
9852 * does not allow sleeping. Due to the same reason, this function
9853 * cannot be called with interrupt disabled.
9854 * This function assumes that the iocb completions occur while
9855 * this function sleep. So, this function cannot be called from
9856 * the thread which process iocb completion for this ring.
9857 * This function clears the iocb_flag of the iocb object before
9858 * issuing the iocb and the iocb completion handler sets this
9859 * flag and wakes this thread when the iocb completes.
9860 * The contents of the response iocb will be copied to prspiocbq
9861 * by the completion handler when the command completes.
9862 * This function returns IOCB_SUCCESS when success.
9863 * This function is called with no lock held.
9864 **/
dea31012005-04-17 16:05:31 -05009865int
James Smart2e0fef82007-06-17 19:56:36 -05009866lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009867 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009868 struct lpfc_iocbq *piocb,
9869 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009870 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009871{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009872 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009873 long timeleft, timeout_req = 0;
9874 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009875 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009876 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009877 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009878 * If the caller has provided a response iocbq buffer, then context2
9879 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009880 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009881 if (prspiocbq) {
9882 if (piocb->context2)
9883 return IOCB_ERROR;
9884 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009885 }
9886
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009887 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9888 piocb->context_un.wait_queue = &done_q;
9889 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009890
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009891 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009892 if (lpfc_readl(phba->HCregaddr, &creg_val))
9893 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009894 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9895 writel(creg_val, phba->HCregaddr);
9896 readl(phba->HCregaddr); /* flush */
9897 }
9898
James Smart2a9bf3d2010-06-07 15:24:45 -04009899 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9900 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009901 if (retval == IOCB_SUCCESS) {
9902 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009903 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009904 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009905 timeout_req);
dea31012005-04-17 16:05:31 -05009906
James Smart7054a602007-04-25 09:52:34 -04009907 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9908 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009909 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009910 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009911 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009912 "0338 IOCB wait timeout error - no "
9913 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009914 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009915 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009916 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009917 "0330 IOCB wake NOT set, "
9918 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009919 timeout, (timeleft / jiffies));
9920 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009921 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009922 } else if (retval == IOCB_BUSY) {
9923 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9924 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9925 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9926 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009927 } else {
9928 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009929 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009930 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009931 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009932 }
9933
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009934 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009935 if (lpfc_readl(phba->HCregaddr, &creg_val))
9936 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009937 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9938 writel(creg_val, phba->HCregaddr);
9939 readl(phba->HCregaddr); /* flush */
9940 }
9941
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009942 if (prspiocbq)
9943 piocb->context2 = NULL;
9944
9945 piocb->context_un.wait_queue = NULL;
9946 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009947 return retval;
9948}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009949
James Smarte59058c2008-08-24 21:49:00 -04009950/**
James Smart3621a712009-04-06 18:47:14 -04009951 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009952 * @phba: Pointer to HBA context object.
9953 * @pmboxq: Pointer to driver mailbox object.
9954 * @timeout: Timeout in number of seconds.
9955 *
9956 * This function issues the mailbox to firmware and waits for the
9957 * mailbox command to complete. If the mailbox command is not
9958 * completed within timeout seconds, it returns MBX_TIMEOUT.
9959 * The function waits for the mailbox completion using an
9960 * interruptible wait. If the thread is woken up due to a
9961 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9962 * should not free the mailbox resources, if this function returns
9963 * MBX_TIMEOUT.
9964 * This function will sleep while waiting for mailbox completion.
9965 * So, this function should not be called from any context which
9966 * does not allow sleeping. Due to the same reason, this function
9967 * cannot be called with interrupt disabled.
9968 * This function assumes that the mailbox completion occurs while
9969 * this function sleep. So, this function cannot be called from
9970 * the worker thread which processes mailbox completion.
9971 * This function is called in the context of HBA management
9972 * applications.
9973 * This function returns MBX_SUCCESS when successful.
9974 * This function is called with no lock held.
9975 **/
dea31012005-04-17 16:05:31 -05009976int
James Smart2e0fef82007-06-17 19:56:36 -05009977lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009978 uint32_t timeout)
9979{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009980 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009981 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009982 unsigned long flag;
dea31012005-04-17 16:05:31 -05009983
9984 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009985 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009986 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009987
James Smart495a7142008-06-14 22:52:59 -04009988 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009989 /* setup wake call as IOCB callback */
9990 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9991 /* setup context field to pass wait_queue pointer to wake function */
9992 pmboxq->context1 = &done_q;
9993
dea31012005-04-17 16:05:31 -05009994 /* now issue the command */
9995 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009996 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009997 wait_event_interruptible_timeout(done_q,
9998 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9999 timeout * HZ);
10000
James Smart858c9f62007-06-17 19:56:39 -050010001 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010002 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -040010003 /*
10004 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10005 * else do not free the resources.
10006 */
James Smartd7c47992010-06-08 18:31:54 -040010007 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010008 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010009 lpfc_sli4_swap_str(phba, pmboxq);
10010 } else {
James Smart7054a602007-04-25 09:52:34 -040010011 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010012 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10013 }
10014 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010015 }
10016
dea31012005-04-17 16:05:31 -050010017 return retval;
10018}
10019
James Smarte59058c2008-08-24 21:49:00 -040010020/**
James Smart3772a992009-05-22 14:50:54 -040010021 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010022 * @phba: Pointer to HBA context.
10023 *
James Smart3772a992009-05-22 14:50:54 -040010024 * This function is called to shutdown the driver's mailbox sub-system.
10025 * It first marks the mailbox sub-system is in a block state to prevent
10026 * the asynchronous mailbox command from issued off the pending mailbox
10027 * command queue. If the mailbox command sub-system shutdown is due to
10028 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10029 * the mailbox sub-system flush routine to forcefully bring down the
10030 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10031 * as with offline or HBA function reset), this routine will wait for the
10032 * outstanding mailbox command to complete before invoking the mailbox
10033 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010034 **/
James Smart3772a992009-05-22 14:50:54 -040010035void
James Smart618a5232012-06-12 13:54:36 -040010036lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010037{
James Smart3772a992009-05-22 14:50:54 -040010038 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010039 unsigned long timeout;
10040
James Smart618a5232012-06-12 13:54:36 -040010041 if (mbx_action == LPFC_MBX_NO_WAIT) {
10042 /* delay 100ms for port state */
10043 msleep(100);
10044 lpfc_sli_mbox_sys_flush(phba);
10045 return;
10046 }
James Smarta183a152011-10-10 21:32:43 -040010047 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010048
James Smart3772a992009-05-22 14:50:54 -040010049 spin_lock_irq(&phba->hbalock);
10050 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010051
10052 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010053 /* Determine how long we might wait for the active mailbox
10054 * command to be gracefully completed by firmware.
10055 */
James Smarta183a152011-10-10 21:32:43 -040010056 if (phba->sli.mbox_active)
10057 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10058 phba->sli.mbox_active) *
10059 1000) + jiffies;
10060 spin_unlock_irq(&phba->hbalock);
10061
James Smart3772a992009-05-22 14:50:54 -040010062 while (phba->sli.mbox_active) {
10063 /* Check active mailbox complete status every 2ms */
10064 msleep(2);
10065 if (time_after(jiffies, timeout))
10066 /* Timeout, let the mailbox flush routine to
10067 * forcefully release active mailbox command
10068 */
10069 break;
10070 }
James Smartd7069f02012-03-01 22:36:29 -050010071 } else
10072 spin_unlock_irq(&phba->hbalock);
10073
James Smart3772a992009-05-22 14:50:54 -040010074 lpfc_sli_mbox_sys_flush(phba);
10075}
10076
10077/**
10078 * lpfc_sli_eratt_read - read sli-3 error attention events
10079 * @phba: Pointer to HBA context.
10080 *
10081 * This function is called to read the SLI3 device error attention registers
10082 * for possible error attention events. The caller must hold the hostlock
10083 * with spin_lock_irq().
10084 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010085 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010086 * Register and returns 0 otherwise.
10087 **/
10088static int
10089lpfc_sli_eratt_read(struct lpfc_hba *phba)
10090{
James Smarted957682007-06-17 19:56:37 -050010091 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010092
James Smart3772a992009-05-22 14:50:54 -040010093 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010094 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10095 goto unplug_err;
10096
James Smart3772a992009-05-22 14:50:54 -040010097 if (ha_copy & HA_ERATT) {
10098 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010099 if (lpfc_sli_read_hs(phba))
10100 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010101
James Smart3772a992009-05-22 14:50:54 -040010102 /* Check if there is a deferred error condition is active */
10103 if ((HS_FFER1 & phba->work_hs) &&
10104 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010105 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010106 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010107 /* Clear all interrupt enable conditions */
10108 writel(0, phba->HCregaddr);
10109 readl(phba->HCregaddr);
10110 }
10111
10112 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010113 phba->work_ha |= HA_ERATT;
10114 /* Indicate polling handles this ERATT */
10115 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010116 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010117 }
James Smart3772a992009-05-22 14:50:54 -040010118 return 0;
James Smart9940b972011-03-11 16:06:12 -050010119
10120unplug_err:
10121 /* Set the driver HS work bitmap */
10122 phba->work_hs |= UNPLUG_ERR;
10123 /* Set the driver HA work bitmap */
10124 phba->work_ha |= HA_ERATT;
10125 /* Indicate polling handles this ERATT */
10126 phba->hba_flag |= HBA_ERATT_HANDLED;
10127 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010128}
10129
James Smarte59058c2008-08-24 21:49:00 -040010130/**
James Smartda0436e2009-05-22 14:51:39 -040010131 * lpfc_sli4_eratt_read - read sli-4 error attention events
10132 * @phba: Pointer to HBA context.
10133 *
10134 * This function is called to read the SLI4 device error attention registers
10135 * for possible error attention events. The caller must hold the hostlock
10136 * with spin_lock_irq().
10137 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010138 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010139 * Register and returns 0 otherwise.
10140 **/
10141static int
10142lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10143{
10144 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010145 uint32_t if_type, portsmphr;
10146 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010147
James Smart2fcee4b2010-12-15 17:57:46 -050010148 /*
10149 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010150 * registers for error attention. This can be changed later.
10151 */
James Smart2fcee4b2010-12-15 17:57:46 -050010152 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10153 switch (if_type) {
10154 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010155 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10156 &uerr_sta_lo) ||
10157 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10158 &uerr_sta_hi)) {
10159 phba->work_hs |= UNPLUG_ERR;
10160 phba->work_ha |= HA_ERATT;
10161 phba->hba_flag |= HBA_ERATT_HANDLED;
10162 return 1;
10163 }
James Smart2fcee4b2010-12-15 17:57:46 -050010164 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10165 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10167 "1423 HBA Unrecoverable error: "
10168 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10169 "ue_mask_lo_reg=0x%x, "
10170 "ue_mask_hi_reg=0x%x\n",
10171 uerr_sta_lo, uerr_sta_hi,
10172 phba->sli4_hba.ue_mask_lo,
10173 phba->sli4_hba.ue_mask_hi);
10174 phba->work_status[0] = uerr_sta_lo;
10175 phba->work_status[1] = uerr_sta_hi;
10176 phba->work_ha |= HA_ERATT;
10177 phba->hba_flag |= HBA_ERATT_HANDLED;
10178 return 1;
10179 }
10180 break;
10181 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010182 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10183 &portstat_reg.word0) ||
10184 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10185 &portsmphr)){
10186 phba->work_hs |= UNPLUG_ERR;
10187 phba->work_ha |= HA_ERATT;
10188 phba->hba_flag |= HBA_ERATT_HANDLED;
10189 return 1;
10190 }
James Smart2fcee4b2010-12-15 17:57:46 -050010191 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10192 phba->work_status[0] =
10193 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10194 phba->work_status[1] =
10195 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010197 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010198 "port status reg 0x%x, "
10199 "port smphr reg 0x%x, "
10200 "error 1=0x%x, error 2=0x%x\n",
10201 portstat_reg.word0,
10202 portsmphr,
10203 phba->work_status[0],
10204 phba->work_status[1]);
10205 phba->work_ha |= HA_ERATT;
10206 phba->hba_flag |= HBA_ERATT_HANDLED;
10207 return 1;
10208 }
10209 break;
10210 case LPFC_SLI_INTF_IF_TYPE_1:
10211 default:
James Smarta747c9c2009-11-18 15:41:10 -050010212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010213 "2886 HBA Error Attention on unsupported "
10214 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010215 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010216 }
James Smart2fcee4b2010-12-15 17:57:46 -050010217
James Smartda0436e2009-05-22 14:51:39 -040010218 return 0;
10219}
10220
10221/**
James Smart3621a712009-04-06 18:47:14 -040010222 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010223 * @phba: Pointer to HBA context.
10224 *
James Smart3772a992009-05-22 14:50:54 -040010225 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010226 * error attention register bit for error attention events.
10227 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010228 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010229 * Register and returns 0 otherwise.
10230 **/
10231int
10232lpfc_sli_check_eratt(struct lpfc_hba *phba)
10233{
10234 uint32_t ha_copy;
10235
10236 /* If somebody is waiting to handle an eratt, don't process it
10237 * here. The brdkill function will do this.
10238 */
10239 if (phba->link_flag & LS_IGNORE_ERATT)
10240 return 0;
10241
10242 /* Check if interrupt handler handles this ERATT */
10243 spin_lock_irq(&phba->hbalock);
10244 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10245 /* Interrupt handler has handled ERATT */
10246 spin_unlock_irq(&phba->hbalock);
10247 return 0;
10248 }
10249
James Smarta257bf92009-04-06 18:48:10 -040010250 /*
10251 * If there is deferred error attention, do not check for error
10252 * attention
10253 */
10254 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10255 spin_unlock_irq(&phba->hbalock);
10256 return 0;
10257 }
10258
James Smart3772a992009-05-22 14:50:54 -040010259 /* If PCI channel is offline, don't process it */
10260 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010261 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010262 return 0;
10263 }
10264
10265 switch (phba->sli_rev) {
10266 case LPFC_SLI_REV2:
10267 case LPFC_SLI_REV3:
10268 /* Read chip Host Attention (HA) register */
10269 ha_copy = lpfc_sli_eratt_read(phba);
10270 break;
James Smartda0436e2009-05-22 14:51:39 -040010271 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010272 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010273 ha_copy = lpfc_sli4_eratt_read(phba);
10274 break;
James Smart3772a992009-05-22 14:50:54 -040010275 default:
10276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10277 "0299 Invalid SLI revision (%d)\n",
10278 phba->sli_rev);
10279 ha_copy = 0;
10280 break;
James Smart93996272008-08-24 21:50:30 -040010281 }
10282 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010283
10284 return ha_copy;
10285}
10286
10287/**
10288 * lpfc_intr_state_check - Check device state for interrupt handling
10289 * @phba: Pointer to HBA context.
10290 *
10291 * This inline routine checks whether a device or its PCI slot is in a state
10292 * that the interrupt should be handled.
10293 *
10294 * This function returns 0 if the device or the PCI slot is in a state that
10295 * interrupt should be handled, otherwise -EIO.
10296 */
10297static inline int
10298lpfc_intr_state_check(struct lpfc_hba *phba)
10299{
10300 /* If the pci channel is offline, ignore all the interrupts */
10301 if (unlikely(pci_channel_offline(phba->pcidev)))
10302 return -EIO;
10303
10304 /* Update device level interrupt statistics */
10305 phba->sli.slistat.sli_intr++;
10306
10307 /* Ignore all interrupts during initialization. */
10308 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10309 return -EIO;
10310
James Smart93996272008-08-24 21:50:30 -040010311 return 0;
10312}
10313
10314/**
James Smart3772a992009-05-22 14:50:54 -040010315 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010316 * @irq: Interrupt number.
10317 * @dev_id: The device context pointer.
10318 *
James Smart93996272008-08-24 21:50:30 -040010319 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010320 * service routine when device with SLI-3 interface spec is enabled with
10321 * MSI-X multi-message interrupt mode and there are slow-path events in
10322 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10323 * interrupt mode, this function is called as part of the device-level
10324 * interrupt handler. When the PCI slot is in error recovery or the HBA
10325 * is undergoing initialization, the interrupt handler will not process
10326 * the interrupt. The link attention and ELS ring attention events are
10327 * handled by the worker thread. The interrupt handler signals the worker
10328 * thread and returns for these events. This function is called without
10329 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010330 * structures.
10331 *
10332 * This function returns IRQ_HANDLED when interrupt is handled else it
10333 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010334 **/
dea31012005-04-17 16:05:31 -050010335irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010336lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010337{
James Smart2e0fef82007-06-17 19:56:36 -050010338 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010339 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010340 uint32_t work_ha_copy;
10341 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010342 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010343 uint32_t control;
10344
James Smart92d7f7b2007-06-17 19:56:38 -050010345 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010346 struct lpfc_vport *vport;
10347 struct lpfc_nodelist *ndlp;
10348 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010349 LPFC_MBOXQ_t *pmb;
10350 int rc;
10351
dea31012005-04-17 16:05:31 -050010352 /*
10353 * Get the driver's phba structure from the dev_id and
10354 * assume the HBA is not interrupting.
10355 */
James Smart93996272008-08-24 21:50:30 -040010356 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010357
10358 if (unlikely(!phba))
10359 return IRQ_NONE;
10360
dea31012005-04-17 16:05:31 -050010361 /*
James Smart93996272008-08-24 21:50:30 -040010362 * Stuff needs to be attented to when this function is invoked as an
10363 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010364 */
James Smart93996272008-08-24 21:50:30 -040010365 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010366 /* Check device state for handling interrupt */
10367 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010368 return IRQ_NONE;
10369 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010370 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010371 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10372 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010373 /* If somebody is waiting to handle an eratt don't process it
10374 * here. The brdkill function will do this.
10375 */
10376 if (phba->link_flag & LS_IGNORE_ERATT)
10377 ha_copy &= ~HA_ERATT;
10378 /* Check the need for handling ERATT in interrupt handler */
10379 if (ha_copy & HA_ERATT) {
10380 if (phba->hba_flag & HBA_ERATT_HANDLED)
10381 /* ERATT polling has handled ERATT */
10382 ha_copy &= ~HA_ERATT;
10383 else
10384 /* Indicate interrupt handler handles ERATT */
10385 phba->hba_flag |= HBA_ERATT_HANDLED;
10386 }
James Smarta257bf92009-04-06 18:48:10 -040010387
10388 /*
10389 * If there is deferred error attention, do not check for any
10390 * interrupt.
10391 */
10392 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010393 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010394 return IRQ_NONE;
10395 }
10396
James Smart93996272008-08-24 21:50:30 -040010397 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010398 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10399 goto unplug_error;
10400
James Smarta747c9c2009-11-18 15:41:10 -050010401 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10402 HC_LAINT_ENA | HC_ERINT_ENA),
10403 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010404 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10405 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010406 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010407 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010408 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010409 } else
10410 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010411
dea31012005-04-17 16:05:31 -050010412 work_ha_copy = ha_copy & phba->work_ha_mask;
10413
James Smart93996272008-08-24 21:50:30 -040010414 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010415 if (work_ha_copy & HA_LATT) {
10416 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10417 /*
10418 * Turn off Link Attention interrupts
10419 * until CLEAR_LA done
10420 */
James Smart5b75da22008-12-04 22:39:35 -050010421 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010422 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010423 if (lpfc_readl(phba->HCregaddr, &control))
10424 goto unplug_error;
dea31012005-04-17 16:05:31 -050010425 control &= ~HC_LAINT_ENA;
10426 writel(control, phba->HCregaddr);
10427 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010428 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010429 }
10430 else
10431 work_ha_copy &= ~HA_LATT;
10432 }
10433
James Smart93996272008-08-24 21:50:30 -040010434 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010435 /*
10436 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10437 * the only slow ring.
10438 */
10439 status = (work_ha_copy &
10440 (HA_RXMASK << (4*LPFC_ELS_RING)));
10441 status >>= (4*LPFC_ELS_RING);
10442 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010443 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010444 if (lpfc_readl(phba->HCregaddr, &control))
10445 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010446
10447 lpfc_debugfs_slow_ring_trc(phba,
10448 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10449 control, status,
10450 (uint32_t)phba->sli.slistat.sli_intr);
10451
James Smart858c9f62007-06-17 19:56:39 -050010452 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010453 lpfc_debugfs_slow_ring_trc(phba,
10454 "ISR Disable ring:"
10455 "pwork:x%x hawork:x%x wait:x%x",
10456 phba->work_ha, work_ha_copy,
10457 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010458 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010459
James Smart858c9f62007-06-17 19:56:39 -050010460 control &=
10461 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010462 writel(control, phba->HCregaddr);
10463 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010464 }
James Smarta58cbd52007-08-02 11:09:43 -040010465 else {
10466 lpfc_debugfs_slow_ring_trc(phba,
10467 "ISR slow ring: pwork:"
10468 "x%x hawork:x%x wait:x%x",
10469 phba->work_ha, work_ha_copy,
10470 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010471 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010472 }
James Smart5b75da22008-12-04 22:39:35 -050010473 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010474 }
10475 }
James Smart5b75da22008-12-04 22:39:35 -050010476 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010477 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010478 if (lpfc_sli_read_hs(phba))
10479 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010480 /*
10481 * Check if there is a deferred error condition
10482 * is active
10483 */
10484 if ((HS_FFER1 & phba->work_hs) &&
10485 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010486 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10487 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010488 phba->hba_flag |= DEFER_ERATT;
10489 /* Clear all interrupt enable conditions */
10490 writel(0, phba->HCregaddr);
10491 readl(phba->HCregaddr);
10492 }
10493 }
10494
James Smart93996272008-08-24 21:50:30 -040010495 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010496 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010497 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010498 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010499 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010500
10501 /* First check out the status word */
10502 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10503 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010504 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010505 /*
10506 * Stray Mailbox Interrupt, mbxCommand <cmd>
10507 * mbxStatus <status>
10508 */
James Smart09372822008-01-11 01:52:54 -050010509 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010510 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010511 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010512 "Interrupt mbxCommand x%x "
10513 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010514 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010515 pmbox->mbxCommand,
10516 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010517 /* clear mailbox attention bit */
10518 work_ha_copy &= ~HA_MBATT;
10519 } else {
James Smart97eab632008-04-07 10:16:05 -040010520 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010521 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010522 phba->last_completion_time = jiffies;
10523 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010524 if (pmb->mbox_cmpl) {
10525 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10526 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010527 if (pmb->out_ext_byte_len &&
10528 pmb->context2)
10529 lpfc_sli_pcimem_bcopy(
10530 phba->mbox_ext,
10531 pmb->context2,
10532 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010533 }
James Smart09372822008-01-11 01:52:54 -050010534 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10535 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10536
10537 lpfc_debugfs_disc_trc(vport,
10538 LPFC_DISC_TRC_MBOX_VPORT,
10539 "MBOX dflt rpi: : "
10540 "status:x%x rpi:x%x",
10541 (uint32_t)pmbox->mbxStatus,
10542 pmbox->un.varWords[0], 0);
10543
10544 if (!pmbox->mbxStatus) {
10545 mp = (struct lpfc_dmabuf *)
10546 (pmb->context1);
10547 ndlp = (struct lpfc_nodelist *)
10548 pmb->context2;
10549
10550 /* Reg_LOGIN of dflt RPI was
10551 * successful. new lets get
10552 * rid of the RPI using the
10553 * same mbox buffer.
10554 */
10555 lpfc_unreg_login(phba,
10556 vport->vpi,
10557 pmbox->un.varWords[0],
10558 pmb);
10559 pmb->mbox_cmpl =
10560 lpfc_mbx_cmpl_dflt_rpi;
10561 pmb->context1 = mp;
10562 pmb->context2 = ndlp;
10563 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010564 rc = lpfc_sli_issue_mbox(phba,
10565 pmb,
10566 MBX_NOWAIT);
10567 if (rc != MBX_BUSY)
10568 lpfc_printf_log(phba,
10569 KERN_ERR,
10570 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010571 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010572 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010573 if (rc != MBX_NOT_FINISHED)
10574 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010575 }
10576 }
James Smart5b75da22008-12-04 22:39:35 -050010577 spin_lock_irqsave(
10578 &phba->pport->work_port_lock,
10579 iflag);
James Smart09372822008-01-11 01:52:54 -050010580 phba->pport->work_port_events &=
10581 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010582 spin_unlock_irqrestore(
10583 &phba->pport->work_port_lock,
10584 iflag);
James Smart09372822008-01-11 01:52:54 -050010585 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010586 }
James Smart97eab632008-04-07 10:16:05 -040010587 } else
James Smart5b75da22008-12-04 22:39:35 -050010588 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010589
James Smart92d7f7b2007-06-17 19:56:38 -050010590 if ((work_ha_copy & HA_MBATT) &&
10591 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010592send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010593 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010594 do {
10595 rc = lpfc_sli_issue_mbox(phba, NULL,
10596 MBX_NOWAIT);
10597 } while (rc == MBX_NOT_FINISHED);
10598 if (rc != MBX_SUCCESS)
10599 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10600 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010601 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010602 }
10603
James Smart5b75da22008-12-04 22:39:35 -050010604 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010605 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010606 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010607 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010608 }
James Smart93996272008-08-24 21:50:30 -040010609 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010610unplug_error:
10611 spin_unlock_irqrestore(&phba->hbalock, iflag);
10612 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010613
James Smart3772a992009-05-22 14:50:54 -040010614} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010615
10616/**
James Smart3772a992009-05-22 14:50:54 -040010617 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010618 * @irq: Interrupt number.
10619 * @dev_id: The device context pointer.
10620 *
10621 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010622 * service routine when device with SLI-3 interface spec is enabled with
10623 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10624 * ring event in the HBA. However, when the device is enabled with either
10625 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10626 * device-level interrupt handler. When the PCI slot is in error recovery
10627 * or the HBA is undergoing initialization, the interrupt handler will not
10628 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10629 * the intrrupt context. This function is called without any lock held.
10630 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010631 *
10632 * This function returns IRQ_HANDLED when interrupt is handled else it
10633 * returns IRQ_NONE.
10634 **/
10635irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010636lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010637{
10638 struct lpfc_hba *phba;
10639 uint32_t ha_copy;
10640 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010641 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010642
10643 /* Get the driver's phba structure from the dev_id and
10644 * assume the HBA is not interrupting.
10645 */
10646 phba = (struct lpfc_hba *) dev_id;
10647
10648 if (unlikely(!phba))
10649 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010650
10651 /*
James Smart93996272008-08-24 21:50:30 -040010652 * Stuff needs to be attented to when this function is invoked as an
10653 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010654 */
James Smart93996272008-08-24 21:50:30 -040010655 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010656 /* Check device state for handling interrupt */
10657 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010658 return IRQ_NONE;
10659 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010660 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10661 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010662 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010663 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010664 /*
10665 * If there is deferred error attention, do not check for
10666 * any interrupt.
10667 */
10668 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010669 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010670 return IRQ_NONE;
10671 }
James Smart93996272008-08-24 21:50:30 -040010672 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10673 phba->HAregaddr);
10674 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010675 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010676 } else
10677 ha_copy = phba->ha_copy;
10678
10679 /*
10680 * Process all events on FCP ring. Take the optimized path for FCP IO.
10681 */
10682 ha_copy &= ~(phba->work_ha_mask);
10683
10684 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010685 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010686 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010687 lpfc_sli_handle_fast_ring_event(phba,
10688 &phba->sli.ring[LPFC_FCP_RING],
10689 status);
James Smarta4bc3372006-12-02 13:34:16 -050010690
10691 if (phba->cfg_multi_ring_support == 2) {
10692 /*
James Smart93996272008-08-24 21:50:30 -040010693 * Process all events on extra ring. Take the optimized path
10694 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010695 */
James Smart93996272008-08-24 21:50:30 -040010696 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010697 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010698 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010699 lpfc_sli_handle_fast_ring_event(phba,
10700 &phba->sli.ring[LPFC_EXTRA_RING],
10701 status);
10702 }
10703 }
dea31012005-04-17 16:05:31 -050010704 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010705} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010706
James Smart93996272008-08-24 21:50:30 -040010707/**
James Smart3772a992009-05-22 14:50:54 -040010708 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010709 * @irq: Interrupt number.
10710 * @dev_id: The device context pointer.
10711 *
James Smart3772a992009-05-22 14:50:54 -040010712 * This function is the HBA device-level interrupt handler to device with
10713 * SLI-3 interface spec, called from the PCI layer when either MSI or
10714 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10715 * requires driver attention. This function invokes the slow-path interrupt
10716 * attention handling function and fast-path interrupt attention handling
10717 * function in turn to process the relevant HBA attention events. This
10718 * function is called without any lock held. It gets the hbalock to access
10719 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010720 *
10721 * This function returns IRQ_HANDLED when interrupt is handled, else it
10722 * returns IRQ_NONE.
10723 **/
10724irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010725lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010726{
10727 struct lpfc_hba *phba;
10728 irqreturn_t sp_irq_rc, fp_irq_rc;
10729 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010730 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010731
10732 /*
10733 * Get the driver's phba structure from the dev_id and
10734 * assume the HBA is not interrupting.
10735 */
10736 phba = (struct lpfc_hba *) dev_id;
10737
10738 if (unlikely(!phba))
10739 return IRQ_NONE;
10740
James Smart3772a992009-05-22 14:50:54 -040010741 /* Check device state for handling interrupt */
10742 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010743 return IRQ_NONE;
10744
10745 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010746 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10747 spin_unlock(&phba->hbalock);
10748 return IRQ_HANDLED;
10749 }
10750
James Smart93996272008-08-24 21:50:30 -040010751 if (unlikely(!phba->ha_copy)) {
10752 spin_unlock(&phba->hbalock);
10753 return IRQ_NONE;
10754 } else if (phba->ha_copy & HA_ERATT) {
10755 if (phba->hba_flag & HBA_ERATT_HANDLED)
10756 /* ERATT polling has handled ERATT */
10757 phba->ha_copy &= ~HA_ERATT;
10758 else
10759 /* Indicate interrupt handler handles ERATT */
10760 phba->hba_flag |= HBA_ERATT_HANDLED;
10761 }
10762
James Smarta257bf92009-04-06 18:48:10 -040010763 /*
10764 * If there is deferred error attention, do not check for any interrupt.
10765 */
10766 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010767 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010768 return IRQ_NONE;
10769 }
10770
James Smart93996272008-08-24 21:50:30 -040010771 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010772 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10773 spin_unlock(&phba->hbalock);
10774 return IRQ_HANDLED;
10775 }
James Smarta747c9c2009-11-18 15:41:10 -050010776 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10777 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10778 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010779 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010780 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010781 readl(phba->HAregaddr); /* flush */
10782 spin_unlock(&phba->hbalock);
10783
10784 /*
10785 * Invokes slow-path host attention interrupt handling as appropriate.
10786 */
10787
10788 /* status of events with mailbox and link attention */
10789 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10790
10791 /* status of events with ELS ring */
10792 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10793 status2 >>= (4*LPFC_ELS_RING);
10794
10795 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010796 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010797 else
10798 sp_irq_rc = IRQ_NONE;
10799
10800 /*
10801 * Invoke fast-path host attention interrupt handling as appropriate.
10802 */
10803
10804 /* status of events with FCP ring */
10805 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10806 status1 >>= (4*LPFC_FCP_RING);
10807
10808 /* status of events with extra ring */
10809 if (phba->cfg_multi_ring_support == 2) {
10810 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10811 status2 >>= (4*LPFC_EXTRA_RING);
10812 } else
10813 status2 = 0;
10814
10815 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010816 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010817 else
10818 fp_irq_rc = IRQ_NONE;
10819
10820 /* Return device-level interrupt handling status */
10821 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010822} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010823
10824/**
10825 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10826 * @phba: pointer to lpfc hba data structure.
10827 *
10828 * This routine is invoked by the worker thread to process all the pending
10829 * SLI4 FCP abort XRI events.
10830 **/
10831void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10832{
10833 struct lpfc_cq_event *cq_event;
10834
10835 /* First, declare the fcp xri abort event has been handled */
10836 spin_lock_irq(&phba->hbalock);
10837 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10838 spin_unlock_irq(&phba->hbalock);
10839 /* Now, handle all the fcp xri abort events */
10840 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10841 /* Get the first event from the head of the event queue */
10842 spin_lock_irq(&phba->hbalock);
10843 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10844 cq_event, struct lpfc_cq_event, list);
10845 spin_unlock_irq(&phba->hbalock);
10846 /* Notify aborted XRI for FCP work queue */
10847 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10848 /* Free the event processed back to the free pool */
10849 lpfc_sli4_cq_event_release(phba, cq_event);
10850 }
10851}
10852
10853/**
10854 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10855 * @phba: pointer to lpfc hba data structure.
10856 *
10857 * This routine is invoked by the worker thread to process all the pending
10858 * SLI4 els abort xri events.
10859 **/
10860void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10861{
10862 struct lpfc_cq_event *cq_event;
10863
10864 /* First, declare the els xri abort event has been handled */
10865 spin_lock_irq(&phba->hbalock);
10866 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10867 spin_unlock_irq(&phba->hbalock);
10868 /* Now, handle all the els xri abort events */
10869 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10870 /* Get the first event from the head of the event queue */
10871 spin_lock_irq(&phba->hbalock);
10872 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10873 cq_event, struct lpfc_cq_event, list);
10874 spin_unlock_irq(&phba->hbalock);
10875 /* Notify aborted XRI for ELS work queue */
10876 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10877 /* Free the event processed back to the free pool */
10878 lpfc_sli4_cq_event_release(phba, cq_event);
10879 }
10880}
10881
James Smart341af102010-01-26 23:07:37 -050010882/**
10883 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10884 * @phba: pointer to lpfc hba data structure
10885 * @pIocbIn: pointer to the rspiocbq
10886 * @pIocbOut: pointer to the cmdiocbq
10887 * @wcqe: pointer to the complete wcqe
10888 *
10889 * This routine transfers the fields of a command iocbq to a response iocbq
10890 * by copying all the IOCB fields from command iocbq and transferring the
10891 * completion status information from the complete wcqe.
10892 **/
James Smart4f774512009-05-22 14:52:35 -040010893static void
James Smart341af102010-01-26 23:07:37 -050010894lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10895 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010896 struct lpfc_iocbq *pIocbOut,
10897 struct lpfc_wcqe_complete *wcqe)
10898{
James Smart341af102010-01-26 23:07:37 -050010899 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010900 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010901 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10902
10903 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10904 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010905 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010906 status = bf_get(lpfc_wcqe_c_status, wcqe);
10907 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010908 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10909 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10910 pIocbIn->iocb.un.fcpi.fcpi_parm =
10911 pIocbOut->iocb.un.fcpi.fcpi_parm -
10912 wcqe->total_data_placed;
10913 else
10914 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010915 else {
James Smart4f774512009-05-22 14:52:35 -040010916 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010917 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10918 }
James Smart341af102010-01-26 23:07:37 -050010919
James Smartacd68592012-01-18 16:25:09 -050010920 /* Convert BG errors for completion status */
10921 if (status == CQE_STATUS_DI_ERROR) {
10922 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10923
10924 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10925 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10926 else
10927 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10928
10929 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10930 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10931 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10932 BGS_GUARD_ERR_MASK;
10933 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10934 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10935 BGS_APPTAG_ERR_MASK;
10936 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10937 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10938 BGS_REFTAG_ERR_MASK;
10939
10940 /* Check to see if there was any good data before the error */
10941 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10942 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10943 BGS_HI_WATER_MARK_PRESENT_MASK;
10944 pIocbIn->iocb.unsli3.sli3_bg.bghm =
10945 wcqe->total_data_placed;
10946 }
10947
10948 /*
10949 * Set ALL the error bits to indicate we don't know what
10950 * type of error it is.
10951 */
10952 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10953 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10954 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10955 BGS_GUARD_ERR_MASK);
10956 }
10957
James Smart341af102010-01-26 23:07:37 -050010958 /* Pick up HBA exchange busy condition */
10959 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10960 spin_lock_irqsave(&phba->hbalock, iflags);
10961 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10962 spin_unlock_irqrestore(&phba->hbalock, iflags);
10963 }
James Smart4f774512009-05-22 14:52:35 -040010964}
10965
10966/**
James Smart45ed1192009-10-02 15:17:02 -040010967 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10968 * @phba: Pointer to HBA context object.
10969 * @wcqe: Pointer to work-queue completion queue entry.
10970 *
10971 * This routine handles an ELS work-queue completion event and construct
10972 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10973 * discovery engine to handle.
10974 *
10975 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10976 **/
10977static struct lpfc_iocbq *
10978lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10979 struct lpfc_iocbq *irspiocbq)
10980{
10981 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10982 struct lpfc_iocbq *cmdiocbq;
10983 struct lpfc_wcqe_complete *wcqe;
10984 unsigned long iflags;
10985
10986 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040010987 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040010988 pring->stats.iocb_event++;
10989 /* Look up the ELS command IOCB and create pseudo response IOCB */
10990 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10991 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040010992 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040010993
10994 if (unlikely(!cmdiocbq)) {
10995 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10996 "0386 ELS complete with no corresponding "
10997 "cmdiocb: iotag (%d)\n",
10998 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10999 lpfc_sli_release_iocbq(phba, irspiocbq);
11000 return NULL;
11001 }
11002
11003 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011004 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011005
11006 return irspiocbq;
11007}
11008
11009/**
James Smart04c68492009-05-22 14:52:52 -040011010 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11011 * @phba: Pointer to HBA context object.
11012 * @cqe: Pointer to mailbox completion queue entry.
11013 *
11014 * This routine process a mailbox completion queue entry with asynchrous
11015 * event.
11016 *
11017 * Return: true if work posted to worker thread, otherwise false.
11018 **/
11019static bool
11020lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11021{
11022 struct lpfc_cq_event *cq_event;
11023 unsigned long iflags;
11024
11025 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11026 "0392 Async Event: word0:x%x, word1:x%x, "
11027 "word2:x%x, word3:x%x\n", mcqe->word0,
11028 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11029
11030 /* Allocate a new internal CQ_EVENT entry */
11031 cq_event = lpfc_sli4_cq_event_alloc(phba);
11032 if (!cq_event) {
11033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11034 "0394 Failed to allocate CQ_EVENT entry\n");
11035 return false;
11036 }
11037
11038 /* Move the CQE into an asynchronous event entry */
11039 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11040 spin_lock_irqsave(&phba->hbalock, iflags);
11041 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11042 /* Set the async event flag */
11043 phba->hba_flag |= ASYNC_EVENT;
11044 spin_unlock_irqrestore(&phba->hbalock, iflags);
11045
11046 return true;
11047}
11048
11049/**
11050 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11051 * @phba: Pointer to HBA context object.
11052 * @cqe: Pointer to mailbox completion queue entry.
11053 *
11054 * This routine process a mailbox completion queue entry with mailbox
11055 * completion event.
11056 *
11057 * Return: true if work posted to worker thread, otherwise false.
11058 **/
11059static bool
11060lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11061{
11062 uint32_t mcqe_status;
11063 MAILBOX_t *mbox, *pmbox;
11064 struct lpfc_mqe *mqe;
11065 struct lpfc_vport *vport;
11066 struct lpfc_nodelist *ndlp;
11067 struct lpfc_dmabuf *mp;
11068 unsigned long iflags;
11069 LPFC_MBOXQ_t *pmb;
11070 bool workposted = false;
11071 int rc;
11072
11073 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11074 if (!bf_get(lpfc_trailer_completed, mcqe))
11075 goto out_no_mqe_complete;
11076
11077 /* Get the reference to the active mbox command */
11078 spin_lock_irqsave(&phba->hbalock, iflags);
11079 pmb = phba->sli.mbox_active;
11080 if (unlikely(!pmb)) {
11081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11082 "1832 No pending MBOX command to handle\n");
11083 spin_unlock_irqrestore(&phba->hbalock, iflags);
11084 goto out_no_mqe_complete;
11085 }
11086 spin_unlock_irqrestore(&phba->hbalock, iflags);
11087 mqe = &pmb->u.mqe;
11088 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11089 mbox = phba->mbox;
11090 vport = pmb->vport;
11091
11092 /* Reset heartbeat timer */
11093 phba->last_completion_time = jiffies;
11094 del_timer(&phba->sli.mbox_tmo);
11095
11096 /* Move mbox data to caller's mailbox region, do endian swapping */
11097 if (pmb->mbox_cmpl && mbox)
11098 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011099
James Smart73d91e52011-10-10 21:32:10 -040011100 /*
11101 * For mcqe errors, conditionally move a modified error code to
11102 * the mbox so that the error will not be missed.
11103 */
11104 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11105 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11106 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11107 bf_set(lpfc_mqe_status, mqe,
11108 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11109 }
James Smart04c68492009-05-22 14:52:52 -040011110 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11111 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11112 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11113 "MBOX dflt rpi: status:x%x rpi:x%x",
11114 mcqe_status,
11115 pmbox->un.varWords[0], 0);
11116 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11117 mp = (struct lpfc_dmabuf *)(pmb->context1);
11118 ndlp = (struct lpfc_nodelist *)pmb->context2;
11119 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11120 * RID of the PPI using the same mbox buffer.
11121 */
11122 lpfc_unreg_login(phba, vport->vpi,
11123 pmbox->un.varWords[0], pmb);
11124 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11125 pmb->context1 = mp;
11126 pmb->context2 = ndlp;
11127 pmb->vport = vport;
11128 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11129 if (rc != MBX_BUSY)
11130 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11131 LOG_SLI, "0385 rc should "
11132 "have been MBX_BUSY\n");
11133 if (rc != MBX_NOT_FINISHED)
11134 goto send_current_mbox;
11135 }
11136 }
11137 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11138 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11139 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11140
11141 /* There is mailbox completion work to do */
11142 spin_lock_irqsave(&phba->hbalock, iflags);
11143 __lpfc_mbox_cmpl_put(phba, pmb);
11144 phba->work_ha |= HA_MBATT;
11145 spin_unlock_irqrestore(&phba->hbalock, iflags);
11146 workposted = true;
11147
11148send_current_mbox:
11149 spin_lock_irqsave(&phba->hbalock, iflags);
11150 /* Release the mailbox command posting token */
11151 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11152 /* Setting active mailbox pointer need to be in sync to flag clear */
11153 phba->sli.mbox_active = NULL;
11154 spin_unlock_irqrestore(&phba->hbalock, iflags);
11155 /* Wake up worker thread to post the next pending mailbox command */
11156 lpfc_worker_wake_up(phba);
11157out_no_mqe_complete:
11158 if (bf_get(lpfc_trailer_consumed, mcqe))
11159 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11160 return workposted;
11161}
11162
11163/**
11164 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11165 * @phba: Pointer to HBA context object.
11166 * @cqe: Pointer to mailbox completion queue entry.
11167 *
11168 * This routine process a mailbox completion queue entry, it invokes the
11169 * proper mailbox complete handling or asynchrous event handling routine
11170 * according to the MCQE's async bit.
11171 *
11172 * Return: true if work posted to worker thread, otherwise false.
11173 **/
11174static bool
11175lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11176{
11177 struct lpfc_mcqe mcqe;
11178 bool workposted;
11179
11180 /* Copy the mailbox MCQE and convert endian order as needed */
11181 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11182
11183 /* Invoke the proper event handling routine */
11184 if (!bf_get(lpfc_trailer_async, &mcqe))
11185 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11186 else
11187 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11188 return workposted;
11189}
11190
11191/**
James Smart4f774512009-05-22 14:52:35 -040011192 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11193 * @phba: Pointer to HBA context object.
11194 * @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
11201lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
11202 struct lpfc_wcqe_complete *wcqe)
11203{
James Smart4f774512009-05-22 14:52:35 -040011204 struct lpfc_iocbq *irspiocbq;
11205 unsigned long iflags;
James Smart7e56aa22012-08-03 12:35:34 -040011206 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
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 Smart4f774512009-05-22 14:52:35 -040011411 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
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
11543 * @eqe: Pointer to fast-path completion queue entry.
11544 *
11545 * This routine process a fast-path work queue completion entry from fast-path
11546 * event queue for FCP command response completion.
11547 **/
11548static void
11549lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11550 struct lpfc_wcqe_complete *wcqe)
11551{
11552 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11553 struct lpfc_iocbq *cmdiocbq;
11554 struct lpfc_iocbq irspiocbq;
11555 unsigned long iflags;
11556
James Smart4f774512009-05-22 14:52:35 -040011557 /* Check for response status */
11558 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11559 /* If resource errors reported from HBA, reduce queue
11560 * depth of the SCSI device.
11561 */
11562 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11563 IOSTAT_LOCAL_REJECT) &&
11564 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11565 phba->lpfc_rampdown_queue_depth(phba);
11566 }
11567 /* Log the error status */
11568 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11569 "0373 FCP complete error: status=x%x, "
11570 "hw_status=x%x, total_data_specified=%d, "
11571 "parameter=x%x, word3=x%x\n",
11572 bf_get(lpfc_wcqe_c_status, wcqe),
11573 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11574 wcqe->total_data_placed, wcqe->parameter,
11575 wcqe->word3);
11576 }
11577
11578 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011579 spin_lock_irqsave(&pring->ring_lock, iflags);
11580 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011581 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11582 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011583 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011584 if (unlikely(!cmdiocbq)) {
11585 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11586 "0374 FCP complete with no corresponding "
11587 "cmdiocb: iotag (%d)\n",
11588 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11589 return;
11590 }
11591 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11592 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11593 "0375 FCP cmdiocb not callback function "
11594 "iotag: (%d)\n",
11595 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11596 return;
11597 }
11598
11599 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011600 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011601
James Smart0f65ff62010-02-26 14:14:23 -050011602 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11603 spin_lock_irqsave(&phba->hbalock, iflags);
11604 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11605 spin_unlock_irqrestore(&phba->hbalock, iflags);
11606 }
11607
James Smart4f774512009-05-22 14:52:35 -040011608 /* Pass the cmd_iocb and the rsp state to the upper layer */
11609 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11610}
11611
11612/**
11613 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11614 * @phba: Pointer to HBA context object.
11615 * @cq: Pointer to completion queue.
11616 * @wcqe: Pointer to work-queue completion queue entry.
11617 *
11618 * This routine handles an fast-path WQ entry comsumed event by invoking the
11619 * proper WQ release routine to the slow-path WQ.
11620 **/
11621static void
11622lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11623 struct lpfc_wcqe_release *wcqe)
11624{
11625 struct lpfc_queue *childwq;
11626 bool wqid_matched = false;
11627 uint16_t fcp_wqid;
11628
11629 /* Check for fast-path FCP work queue release */
11630 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11631 list_for_each_entry(childwq, &cq->child_list, list) {
11632 if (childwq->queue_id == fcp_wqid) {
11633 lpfc_sli4_wq_release(childwq,
11634 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11635 wqid_matched = true;
11636 break;
11637 }
11638 }
11639 /* Report warning log message if no match found */
11640 if (wqid_matched != true)
11641 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11642 "2580 Fast-path wqe consume event carries "
11643 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11644}
11645
11646/**
11647 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11648 * @cq: Pointer to the completion queue.
11649 * @eqe: Pointer to fast-path completion queue entry.
11650 *
11651 * This routine process a fast-path work queue completion entry from fast-path
11652 * event queue for FCP command response completion.
11653 **/
11654static int
11655lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11656 struct lpfc_cqe *cqe)
11657{
11658 struct lpfc_wcqe_release wcqe;
11659 bool workposted = false;
11660
11661 /* Copy the work queue CQE and convert endian order if needed */
11662 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11663
11664 /* Check and process for different type of WCQE and dispatch */
11665 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11666 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011667 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011668 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011669 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011670 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11671 (struct lpfc_wcqe_complete *)&wcqe);
11672 break;
11673 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011674 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011675 /* Process the WQ release event */
11676 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11677 (struct lpfc_wcqe_release *)&wcqe);
11678 break;
11679 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011680 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011681 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011682 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011683 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11684 (struct sli4_wcqe_xri_aborted *)&wcqe);
11685 break;
11686 default:
11687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11688 "0144 Not a valid WCQE code: x%x\n",
11689 bf_get(lpfc_wcqe_c_code, &wcqe));
11690 break;
11691 }
11692 return workposted;
11693}
11694
11695/**
11696 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11697 * @phba: Pointer to HBA context object.
11698 * @eqe: Pointer to fast-path event queue entry.
11699 *
11700 * This routine process a event queue entry from the fast-path event queue.
11701 * It will check the MajorCode and MinorCode to determine this is for a
11702 * completion event on a completion queue, if not, an error shall be logged
11703 * and just return. Otherwise, it will get to the corresponding completion
11704 * queue and process all the entries on the completion queue, rearm the
11705 * completion queue, and then return.
11706 **/
11707static void
11708lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11709 uint32_t fcp_cqidx)
11710{
11711 struct lpfc_queue *cq;
11712 struct lpfc_cqe *cqe;
11713 bool workposted = false;
11714 uint16_t cqid;
11715 int ecount = 0;
11716
James Smartcb5172e2010-03-15 11:25:07 -040011717 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011718 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11719 "0366 Not a valid fast-path completion "
11720 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011721 bf_get_le32(lpfc_eqe_major_code, eqe),
11722 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011723 return;
11724 }
11725
James Smart2e90f4b2011-12-13 13:22:37 -050011726 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11727 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11728 "3146 Fast-path completion queues "
11729 "does not exist\n");
11730 return;
11731 }
James Smart4f774512009-05-22 14:52:35 -040011732 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11733 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011734 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11735 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11736 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011737 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011738 return;
11739 }
11740
11741 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011742 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011743 if (unlikely(cqid != cq->queue_id)) {
11744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11745 "0368 Miss-matched fast-path completion "
11746 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11747 cqid, cq->queue_id);
11748 return;
11749 }
11750
11751 /* Process all the entries to the CQ */
11752 while ((cqe = lpfc_sli4_cq_get(cq))) {
11753 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011754 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011755 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11756 }
11757
James Smartb84daac2012-08-03 12:35:13 -040011758 /* Track the max number of CQEs processed in 1 EQ */
11759 if (ecount > cq->CQ_max_cqe)
11760 cq->CQ_max_cqe = ecount;
11761
James Smart4f774512009-05-22 14:52:35 -040011762 /* Catch the no cq entry condition */
11763 if (unlikely(ecount == 0))
11764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11765 "0369 No entry from fast-path completion "
11766 "queue fcpcqid=%d\n", cq->queue_id);
11767
11768 /* In any case, flash and re-arm the CQ */
11769 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11770
11771 /* wake up worker thread if there are works to be done */
11772 if (workposted)
11773 lpfc_worker_wake_up(phba);
11774}
11775
11776static void
11777lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11778{
11779 struct lpfc_eqe *eqe;
11780
11781 /* walk all the EQ entries and drop on the floor */
11782 while ((eqe = lpfc_sli4_eq_get(eq)))
11783 ;
11784
11785 /* Clear and re-arm the EQ */
11786 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11787}
11788
11789/**
11790 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11791 * @irq: Interrupt number.
11792 * @dev_id: The device context pointer.
11793 *
11794 * This function is directly called from the PCI layer as an interrupt
11795 * service routine when device with SLI-4 interface spec is enabled with
11796 * MSI-X multi-message interrupt mode and there are slow-path events in
11797 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11798 * interrupt mode, this function is called as part of the device-level
11799 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11800 * undergoing initialization, the interrupt handler will not process the
11801 * interrupt. The link attention and ELS ring attention events are handled
11802 * by the worker thread. The interrupt handler signals the worker thread
11803 * and returns for these events. This function is called without any lock
11804 * held. It gets the hbalock to access and update SLI data structures.
11805 *
11806 * This function returns IRQ_HANDLED when interrupt is handled else it
11807 * returns IRQ_NONE.
11808 **/
11809irqreturn_t
11810lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11811{
11812 struct lpfc_hba *phba;
11813 struct lpfc_queue *speq;
11814 struct lpfc_eqe *eqe;
11815 unsigned long iflag;
11816 int ecount = 0;
11817
11818 /*
11819 * Get the driver's phba structure from the dev_id
11820 */
11821 phba = (struct lpfc_hba *)dev_id;
11822
11823 if (unlikely(!phba))
11824 return IRQ_NONE;
11825
11826 /* Get to the EQ struct associated with this vector */
11827 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011828 if (unlikely(!speq))
11829 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011830
11831 /* Check device state for handling interrupt */
11832 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011833 speq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011834 /* Check again for link_state with lock held */
11835 spin_lock_irqsave(&phba->hbalock, iflag);
11836 if (phba->link_state < LPFC_LINK_DOWN)
11837 /* Flush, clear interrupt, and rearm the EQ */
11838 lpfc_sli4_eq_flush(phba, speq);
11839 spin_unlock_irqrestore(&phba->hbalock, iflag);
11840 return IRQ_NONE;
11841 }
11842
11843 /*
11844 * Process all the event on FCP slow-path EQ
11845 */
11846 while ((eqe = lpfc_sli4_eq_get(speq))) {
11847 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011848 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011849 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011850 speq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011851 }
11852
James Smartb84daac2012-08-03 12:35:13 -040011853 /* Track the max number of EQEs processed in 1 intr */
11854 if (ecount > speq->EQ_max_eqe)
11855 speq->EQ_max_eqe = ecount;
11856
James Smart4f774512009-05-22 14:52:35 -040011857 /* Always clear and re-arm the slow-path EQ */
11858 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11859
11860 /* Catch the no cq entry condition */
11861 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011862 speq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011863 if (phba->intr_type == MSIX)
11864 /* MSI-X treated interrupt served as no EQ share INT */
11865 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11866 "0357 MSI-X interrupt with no EQE\n");
11867 else
11868 /* Non MSI-X treated on interrupt as EQ share INT */
11869 return IRQ_NONE;
11870 }
11871
11872 return IRQ_HANDLED;
11873} /* lpfc_sli4_sp_intr_handler */
11874
11875/**
11876 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11877 * @irq: Interrupt number.
11878 * @dev_id: The device context pointer.
11879 *
11880 * This function is directly called from the PCI layer as an interrupt
11881 * service routine when device with SLI-4 interface spec is enabled with
11882 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11883 * ring event in the HBA. However, when the device is enabled with either
11884 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11885 * device-level interrupt handler. When the PCI slot is in error recovery
11886 * or the HBA is undergoing initialization, the interrupt handler will not
11887 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11888 * the intrrupt context. This function is called without any lock held.
11889 * It gets the hbalock to access and update SLI data structures. Note that,
11890 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11891 * equal to that of FCP CQ index.
11892 *
11893 * This function returns IRQ_HANDLED when interrupt is handled else it
11894 * returns IRQ_NONE.
11895 **/
11896irqreturn_t
11897lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11898{
11899 struct lpfc_hba *phba;
11900 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11901 struct lpfc_queue *fpeq;
11902 struct lpfc_eqe *eqe;
11903 unsigned long iflag;
11904 int ecount = 0;
11905 uint32_t fcp_eqidx;
11906
11907 /* Get the driver's phba structure from the dev_id */
11908 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11909 phba = fcp_eq_hdl->phba;
11910 fcp_eqidx = fcp_eq_hdl->idx;
11911
11912 if (unlikely(!phba))
11913 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011914 if (unlikely(!phba->sli4_hba.fp_eq))
11915 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011916
11917 /* Get to the EQ struct associated with this vector */
11918 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011919 if (unlikely(!fpeq))
11920 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011921
11922 /* Check device state for handling interrupt */
11923 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011924 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011925 /* Check again for link_state with lock held */
11926 spin_lock_irqsave(&phba->hbalock, iflag);
11927 if (phba->link_state < LPFC_LINK_DOWN)
11928 /* Flush, clear interrupt, and rearm the EQ */
11929 lpfc_sli4_eq_flush(phba, fpeq);
11930 spin_unlock_irqrestore(&phba->hbalock, iflag);
11931 return IRQ_NONE;
11932 }
11933
11934 /*
11935 * Process all the event on FCP fast-path EQ
11936 */
11937 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11938 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011939 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011940 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011941 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011942 }
11943
James Smartb84daac2012-08-03 12:35:13 -040011944 /* Track the max number of EQEs processed in 1 intr */
11945 if (ecount > fpeq->EQ_max_eqe)
11946 fpeq->EQ_max_eqe = ecount;
11947
James Smart4f774512009-05-22 14:52:35 -040011948 /* Always clear and re-arm the fast-path EQ */
11949 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11950
11951 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011952 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011953 if (phba->intr_type == MSIX)
11954 /* MSI-X treated interrupt served as no EQ share INT */
11955 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11956 "0358 MSI-X interrupt with no EQE\n");
11957 else
11958 /* Non MSI-X treated on interrupt as EQ share INT */
11959 return IRQ_NONE;
11960 }
11961
11962 return IRQ_HANDLED;
11963} /* lpfc_sli4_fp_intr_handler */
11964
11965/**
11966 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11967 * @irq: Interrupt number.
11968 * @dev_id: The device context pointer.
11969 *
11970 * This function is the device-level interrupt handler to device with SLI-4
11971 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11972 * interrupt mode is enabled and there is an event in the HBA which requires
11973 * driver attention. This function invokes the slow-path interrupt attention
11974 * handling function and fast-path interrupt attention handling function in
11975 * turn to process the relevant HBA attention events. This function is called
11976 * without any lock held. It gets the hbalock to access and update SLI data
11977 * structures.
11978 *
11979 * This function returns IRQ_HANDLED when interrupt is handled, else it
11980 * returns IRQ_NONE.
11981 **/
11982irqreturn_t
11983lpfc_sli4_intr_handler(int irq, void *dev_id)
11984{
11985 struct lpfc_hba *phba;
11986 irqreturn_t sp_irq_rc, fp_irq_rc;
11987 bool fp_handled = false;
11988 uint32_t fcp_eqidx;
11989
11990 /* Get the driver's phba structure from the dev_id */
11991 phba = (struct lpfc_hba *)dev_id;
11992
11993 if (unlikely(!phba))
11994 return IRQ_NONE;
11995
11996 /*
11997 * Invokes slow-path host attention interrupt handling as appropriate.
11998 */
11999 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
12000
12001 /*
12002 * Invoke fast-path host attention interrupt handling as appropriate.
12003 */
12004 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
12005 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
12006 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12007 if (fp_irq_rc == IRQ_HANDLED)
12008 fp_handled |= true;
12009 }
12010
12011 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
12012} /* lpfc_sli4_intr_handler */
12013
12014/**
12015 * lpfc_sli4_queue_free - free a queue structure and associated memory
12016 * @queue: The queue structure to free.
12017 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012018 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012019 * the host resident queue. This function must be called after destroying the
12020 * queue on the HBA.
12021 **/
12022void
12023lpfc_sli4_queue_free(struct lpfc_queue *queue)
12024{
12025 struct lpfc_dmabuf *dmabuf;
12026
12027 if (!queue)
12028 return;
12029
12030 while (!list_empty(&queue->page_list)) {
12031 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12032 list);
James Smart49198b32010-04-06 15:04:33 -040012033 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012034 dmabuf->virt, dmabuf->phys);
12035 kfree(dmabuf);
12036 }
12037 kfree(queue);
12038 return;
12039}
12040
12041/**
12042 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12043 * @phba: The HBA that this queue is being created on.
12044 * @entry_size: The size of each queue entry for this queue.
12045 * @entry count: The number of entries that this queue will handle.
12046 *
12047 * This function allocates a queue structure and the DMAable memory used for
12048 * the host resident queue. This function must be called before creating the
12049 * queue on the HBA.
12050 **/
12051struct lpfc_queue *
12052lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12053 uint32_t entry_count)
12054{
12055 struct lpfc_queue *queue;
12056 struct lpfc_dmabuf *dmabuf;
12057 int x, total_qe_count;
12058 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012059 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012060
James Smartcb5172e2010-03-15 11:25:07 -040012061 if (!phba->sli4_hba.pc_sli4_params.supported)
12062 hw_page_size = SLI4_PAGE_SIZE;
12063
James Smart4f774512009-05-22 14:52:35 -040012064 queue = kzalloc(sizeof(struct lpfc_queue) +
12065 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12066 if (!queue)
12067 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012068 queue->page_count = (ALIGN(entry_size * entry_count,
12069 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012070 INIT_LIST_HEAD(&queue->list);
12071 INIT_LIST_HEAD(&queue->page_list);
12072 INIT_LIST_HEAD(&queue->child_list);
12073 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12074 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12075 if (!dmabuf)
12076 goto out_fail;
12077 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012078 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012079 GFP_KERNEL);
12080 if (!dmabuf->virt) {
12081 kfree(dmabuf);
12082 goto out_fail;
12083 }
James Smartcb5172e2010-03-15 11:25:07 -040012084 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012085 dmabuf->buffer_tag = x;
12086 list_add_tail(&dmabuf->list, &queue->page_list);
12087 /* initialize queue's entry array */
12088 dma_pointer = dmabuf->virt;
12089 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012090 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012091 total_qe_count++, dma_pointer += entry_size) {
12092 queue->qe[total_qe_count].address = dma_pointer;
12093 }
12094 }
12095 queue->entry_size = entry_size;
12096 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012097
12098 /*
12099 * entry_repost is calculated based on the number of entries in the
12100 * queue. This works out except for RQs. If buffers are NOT initially
12101 * posted for every RQE, entry_repost should be adjusted accordingly.
12102 */
12103 queue->entry_repost = (entry_count >> 3);
12104 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12105 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012106 queue->phba = phba;
12107
12108 return queue;
12109out_fail:
12110 lpfc_sli4_queue_free(queue);
12111 return NULL;
12112}
12113
12114/**
James Smart173edbb2012-06-12 13:54:50 -040012115 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12116 * @phba: HBA structure that indicates port to create a queue on.
12117 * @startq: The starting FCP EQ to modify
12118 *
12119 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12120 *
12121 * The @phba struct is used to send mailbox command to HBA. The @startq
12122 * is used to get the starting FCP EQ to change.
12123 * This function is asynchronous and will wait for the mailbox
12124 * command to finish before continuing.
12125 *
12126 * On success this function will return a zero. If unable to allocate enough
12127 * memory this function will return -ENOMEM. If the queue create mailbox command
12128 * fails this function will return -ENXIO.
12129 **/
12130uint32_t
12131lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12132{
12133 struct lpfc_mbx_modify_eq_delay *eq_delay;
12134 LPFC_MBOXQ_t *mbox;
12135 struct lpfc_queue *eq;
12136 int cnt, rc, length, status = 0;
12137 uint32_t shdr_status, shdr_add_status;
12138 int fcp_eqidx;
12139 union lpfc_sli4_cfg_shdr *shdr;
12140 uint16_t dmult;
12141
12142 if (startq >= phba->cfg_fcp_eq_count)
12143 return 0;
12144
12145 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12146 if (!mbox)
12147 return -ENOMEM;
12148 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12149 sizeof(struct lpfc_sli4_cfg_mhdr));
12150 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12151 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12152 length, LPFC_SLI4_MBX_EMBED);
12153 eq_delay = &mbox->u.mqe.un.eq_delay;
12154
12155 /* Calculate delay multiper from maximum interrupt per second */
12156 dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
12157
12158 cnt = 0;
12159 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
12160 fcp_eqidx++) {
12161 eq = phba->sli4_hba.fp_eq[fcp_eqidx];
12162 if (!eq)
12163 continue;
12164 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12165 eq_delay->u.request.eq[cnt].phase = 0;
12166 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12167 cnt++;
12168 if (cnt >= LPFC_MAX_EQ_DELAY)
12169 break;
12170 }
12171 eq_delay->u.request.num_eq = cnt;
12172
12173 mbox->vport = phba->pport;
12174 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12175 mbox->context1 = NULL;
12176 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12177 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12178 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12179 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12180 if (shdr_status || shdr_add_status || rc) {
12181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12182 "2512 MODIFY_EQ_DELAY mailbox failed with "
12183 "status x%x add_status x%x, mbx status x%x\n",
12184 shdr_status, shdr_add_status, rc);
12185 status = -ENXIO;
12186 }
12187 mempool_free(mbox, phba->mbox_mem_pool);
12188 return status;
12189}
12190
12191/**
James Smart4f774512009-05-22 14:52:35 -040012192 * lpfc_eq_create - Create an Event Queue on the HBA
12193 * @phba: HBA structure that indicates port to create a queue on.
12194 * @eq: The queue structure to use to create the event queue.
12195 * @imax: The maximum interrupt per second limit.
12196 *
12197 * This function creates an event queue, as detailed in @eq, on a port,
12198 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12199 *
12200 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12201 * is used to get the entry count and entry size that are necessary to
12202 * determine the number of pages to allocate and use for this queue. This
12203 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12204 * event queue. This function is asynchronous and will wait for the mailbox
12205 * command to finish before continuing.
12206 *
12207 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012208 * memory this function will return -ENOMEM. If the queue create mailbox command
12209 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012210 **/
12211uint32_t
12212lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12213{
12214 struct lpfc_mbx_eq_create *eq_create;
12215 LPFC_MBOXQ_t *mbox;
12216 int rc, length, status = 0;
12217 struct lpfc_dmabuf *dmabuf;
12218 uint32_t shdr_status, shdr_add_status;
12219 union lpfc_sli4_cfg_shdr *shdr;
12220 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012221 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12222
James Smart2e90f4b2011-12-13 13:22:37 -050012223 /* sanity check on queue memory */
12224 if (!eq)
12225 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012226 if (!phba->sli4_hba.pc_sli4_params.supported)
12227 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012228
12229 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12230 if (!mbox)
12231 return -ENOMEM;
12232 length = (sizeof(struct lpfc_mbx_eq_create) -
12233 sizeof(struct lpfc_sli4_cfg_mhdr));
12234 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12235 LPFC_MBOX_OPCODE_EQ_CREATE,
12236 length, LPFC_SLI4_MBX_EMBED);
12237 eq_create = &mbox->u.mqe.un.eq_create;
12238 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12239 eq->page_count);
12240 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12241 LPFC_EQE_SIZE);
12242 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12243 /* Calculate delay multiper from maximum interrupt per second */
12244 dmult = LPFC_DMULT_CONST/imax - 1;
12245 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12246 dmult);
12247 switch (eq->entry_count) {
12248 default:
12249 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12250 "0360 Unsupported EQ count. (%d)\n",
12251 eq->entry_count);
12252 if (eq->entry_count < 256)
12253 return -EINVAL;
12254 /* otherwise default to smallest count (drop through) */
12255 case 256:
12256 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12257 LPFC_EQ_CNT_256);
12258 break;
12259 case 512:
12260 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12261 LPFC_EQ_CNT_512);
12262 break;
12263 case 1024:
12264 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12265 LPFC_EQ_CNT_1024);
12266 break;
12267 case 2048:
12268 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12269 LPFC_EQ_CNT_2048);
12270 break;
12271 case 4096:
12272 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12273 LPFC_EQ_CNT_4096);
12274 break;
12275 }
12276 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012277 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012278 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12279 putPaddrLow(dmabuf->phys);
12280 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12281 putPaddrHigh(dmabuf->phys);
12282 }
12283 mbox->vport = phba->pport;
12284 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12285 mbox->context1 = NULL;
12286 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12287 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12288 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12289 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12290 if (shdr_status || shdr_add_status || rc) {
12291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12292 "2500 EQ_CREATE mailbox failed with "
12293 "status x%x add_status x%x, mbx status x%x\n",
12294 shdr_status, shdr_add_status, rc);
12295 status = -ENXIO;
12296 }
12297 eq->type = LPFC_EQ;
12298 eq->subtype = LPFC_NONE;
12299 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12300 if (eq->queue_id == 0xFFFF)
12301 status = -ENXIO;
12302 eq->host_index = 0;
12303 eq->hba_index = 0;
12304
James Smart8fa38512009-07-19 10:01:03 -040012305 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012306 return status;
12307}
12308
12309/**
12310 * lpfc_cq_create - Create a Completion Queue on the HBA
12311 * @phba: HBA structure that indicates port to create a queue on.
12312 * @cq: The queue structure to use to create the completion queue.
12313 * @eq: The event queue to bind this completion queue to.
12314 *
12315 * This function creates a completion queue, as detailed in @wq, on a port,
12316 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12317 *
12318 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12319 * is used to get the entry count and entry size that are necessary to
12320 * determine the number of pages to allocate and use for this queue. The @eq
12321 * is used to indicate which event queue to bind this completion queue to. This
12322 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12323 * completion queue. This function is asynchronous and will wait for the mailbox
12324 * command to finish before continuing.
12325 *
12326 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012327 * memory this function will return -ENOMEM. If the queue create mailbox command
12328 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012329 **/
12330uint32_t
12331lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12332 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12333{
12334 struct lpfc_mbx_cq_create *cq_create;
12335 struct lpfc_dmabuf *dmabuf;
12336 LPFC_MBOXQ_t *mbox;
12337 int rc, length, status = 0;
12338 uint32_t shdr_status, shdr_add_status;
12339 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012340 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12341
James Smart2e90f4b2011-12-13 13:22:37 -050012342 /* sanity check on queue memory */
12343 if (!cq || !eq)
12344 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012345 if (!phba->sli4_hba.pc_sli4_params.supported)
12346 hw_page_size = SLI4_PAGE_SIZE;
12347
James Smart4f774512009-05-22 14:52:35 -040012348 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12349 if (!mbox)
12350 return -ENOMEM;
12351 length = (sizeof(struct lpfc_mbx_cq_create) -
12352 sizeof(struct lpfc_sli4_cfg_mhdr));
12353 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12354 LPFC_MBOX_OPCODE_CQ_CREATE,
12355 length, LPFC_SLI4_MBX_EMBED);
12356 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012357 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012358 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12359 cq->page_count);
12360 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12361 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012362 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12363 phba->sli4_hba.pc_sli4_params.cqv);
12364 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012365 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12366 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012367 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12368 eq->queue_id);
12369 } else {
12370 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12371 eq->queue_id);
12372 }
James Smart4f774512009-05-22 14:52:35 -040012373 switch (cq->entry_count) {
12374 default:
12375 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12376 "0361 Unsupported CQ count. (%d)\n",
12377 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012378 if (cq->entry_count < 256) {
12379 status = -EINVAL;
12380 goto out;
12381 }
James Smart4f774512009-05-22 14:52:35 -040012382 /* otherwise default to smallest count (drop through) */
12383 case 256:
12384 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12385 LPFC_CQ_CNT_256);
12386 break;
12387 case 512:
12388 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12389 LPFC_CQ_CNT_512);
12390 break;
12391 case 1024:
12392 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12393 LPFC_CQ_CNT_1024);
12394 break;
12395 }
12396 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012397 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012398 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12399 putPaddrLow(dmabuf->phys);
12400 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12401 putPaddrHigh(dmabuf->phys);
12402 }
12403 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12404
12405 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012406 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12407 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12408 if (shdr_status || shdr_add_status || rc) {
12409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12410 "2501 CQ_CREATE mailbox failed with "
12411 "status x%x add_status x%x, mbx status x%x\n",
12412 shdr_status, shdr_add_status, rc);
12413 status = -ENXIO;
12414 goto out;
12415 }
12416 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12417 if (cq->queue_id == 0xFFFF) {
12418 status = -ENXIO;
12419 goto out;
12420 }
12421 /* link the cq onto the parent eq child list */
12422 list_add_tail(&cq->list, &eq->child_list);
12423 /* Set up completion queue's type and subtype */
12424 cq->type = type;
12425 cq->subtype = subtype;
12426 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012427 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012428 cq->host_index = 0;
12429 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012430
James Smart8fa38512009-07-19 10:01:03 -040012431out:
12432 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012433 return status;
12434}
12435
12436/**
James Smartb19a0612010-04-06 14:48:51 -040012437 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012438 * @phba: HBA structure that indicates port to create a queue on.
12439 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012440 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12441 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012442 *
James Smartb19a0612010-04-06 14:48:51 -040012443 * This function provides failback (fb) functionality when the
12444 * mq_create_ext fails on older FW generations. It's purpose is identical
12445 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012446 *
James Smartb19a0612010-04-06 14:48:51 -040012447 * This routine cannot fail as all attributes were previously accessed and
12448 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012449 **/
James Smartb19a0612010-04-06 14:48:51 -040012450static void
12451lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12452 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012453{
12454 struct lpfc_mbx_mq_create *mq_create;
12455 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012456 int length;
James Smart04c68492009-05-22 14:52:52 -040012457
James Smart04c68492009-05-22 14:52:52 -040012458 length = (sizeof(struct lpfc_mbx_mq_create) -
12459 sizeof(struct lpfc_sli4_cfg_mhdr));
12460 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12461 LPFC_MBOX_OPCODE_MQ_CREATE,
12462 length, LPFC_SLI4_MBX_EMBED);
12463 mq_create = &mbox->u.mqe.un.mq_create;
12464 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012465 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012466 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012467 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012468 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12469 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012470 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012471 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12472 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012473 break;
12474 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012475 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12476 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012477 break;
12478 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012479 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12480 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012481 break;
12482 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012483 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12484 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012485 break;
12486 }
12487 list_for_each_entry(dmabuf, &mq->page_list, list) {
12488 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012489 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012490 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012491 putPaddrHigh(dmabuf->phys);
12492 }
12493}
12494
12495/**
12496 * lpfc_mq_create - Create a mailbox Queue on the HBA
12497 * @phba: HBA structure that indicates port to create a queue on.
12498 * @mq: The queue structure to use to create the mailbox queue.
12499 * @cq: The completion queue to associate with this cq.
12500 * @subtype: The queue's subtype.
12501 *
12502 * This function creates a mailbox queue, as detailed in @mq, on a port,
12503 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12504 *
12505 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12506 * is used to get the entry count and entry size that are necessary to
12507 * determine the number of pages to allocate and use for this queue. This
12508 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12509 * mailbox queue. This function is asynchronous and will wait for the mailbox
12510 * command to finish before continuing.
12511 *
12512 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012513 * memory this function will return -ENOMEM. If the queue create mailbox command
12514 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012515 **/
12516int32_t
12517lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12518 struct lpfc_queue *cq, uint32_t subtype)
12519{
12520 struct lpfc_mbx_mq_create *mq_create;
12521 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12522 struct lpfc_dmabuf *dmabuf;
12523 LPFC_MBOXQ_t *mbox;
12524 int rc, length, status = 0;
12525 uint32_t shdr_status, shdr_add_status;
12526 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012527 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012528
James Smart2e90f4b2011-12-13 13:22:37 -050012529 /* sanity check on queue memory */
12530 if (!mq || !cq)
12531 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012532 if (!phba->sli4_hba.pc_sli4_params.supported)
12533 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012534
12535 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12536 if (!mbox)
12537 return -ENOMEM;
12538 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12539 sizeof(struct lpfc_sli4_cfg_mhdr));
12540 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12541 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12542 length, LPFC_SLI4_MBX_EMBED);
12543
12544 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012545 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012546 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12547 &mq_create_ext->u.request, mq->page_count);
12548 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12549 &mq_create_ext->u.request, 1);
12550 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012551 &mq_create_ext->u.request, 1);
12552 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12553 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012554 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12555 &mq_create_ext->u.request, 1);
12556 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12557 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012558 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012559 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12560 phba->sli4_hba.pc_sli4_params.mqv);
12561 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12562 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12563 cq->queue_id);
12564 else
12565 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12566 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012567 switch (mq->entry_count) {
12568 default:
12569 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12570 "0362 Unsupported MQ count. (%d)\n",
12571 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012572 if (mq->entry_count < 16) {
12573 status = -EINVAL;
12574 goto out;
12575 }
James Smartb19a0612010-04-06 14:48:51 -040012576 /* otherwise default to smallest count (drop through) */
12577 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012578 bf_set(lpfc_mq_context_ring_size,
12579 &mq_create_ext->u.request.context,
12580 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012581 break;
12582 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012583 bf_set(lpfc_mq_context_ring_size,
12584 &mq_create_ext->u.request.context,
12585 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012586 break;
12587 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012588 bf_set(lpfc_mq_context_ring_size,
12589 &mq_create_ext->u.request.context,
12590 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012591 break;
12592 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012593 bf_set(lpfc_mq_context_ring_size,
12594 &mq_create_ext->u.request.context,
12595 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012596 break;
12597 }
12598 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012599 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012600 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12601 putPaddrLow(dmabuf->phys);
12602 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012603 putPaddrHigh(dmabuf->phys);
12604 }
12605 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012606 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12607 &mq_create_ext->u.response);
12608 if (rc != MBX_SUCCESS) {
12609 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12610 "2795 MQ_CREATE_EXT failed with "
12611 "status x%x. Failback to MQ_CREATE.\n",
12612 rc);
12613 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12614 mq_create = &mbox->u.mqe.un.mq_create;
12615 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12616 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12617 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12618 &mq_create->u.response);
12619 }
12620
James Smart04c68492009-05-22 14:52:52 -040012621 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012622 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12623 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12624 if (shdr_status || shdr_add_status || rc) {
12625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12626 "2502 MQ_CREATE mailbox failed with "
12627 "status x%x add_status x%x, mbx status x%x\n",
12628 shdr_status, shdr_add_status, rc);
12629 status = -ENXIO;
12630 goto out;
12631 }
James Smart04c68492009-05-22 14:52:52 -040012632 if (mq->queue_id == 0xFFFF) {
12633 status = -ENXIO;
12634 goto out;
12635 }
12636 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012637 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012638 mq->subtype = subtype;
12639 mq->host_index = 0;
12640 mq->hba_index = 0;
12641
12642 /* link the mq onto the parent cq child list */
12643 list_add_tail(&mq->list, &cq->child_list);
12644out:
James Smart8fa38512009-07-19 10:01:03 -040012645 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012646 return status;
12647}
12648
12649/**
James Smart4f774512009-05-22 14:52:35 -040012650 * lpfc_wq_create - Create a Work Queue on the HBA
12651 * @phba: HBA structure that indicates port to create a queue on.
12652 * @wq: The queue structure to use to create the work queue.
12653 * @cq: The completion queue to bind this work queue to.
12654 * @subtype: The subtype of the work queue indicating its functionality.
12655 *
12656 * This function creates a work queue, as detailed in @wq, on a port, described
12657 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12658 *
12659 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12660 * is used to get the entry count and entry size that are necessary to
12661 * determine the number of pages to allocate and use for this queue. The @cq
12662 * is used to indicate which completion queue to bind this work queue to. This
12663 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12664 * work queue. This function is asynchronous and will wait for the mailbox
12665 * command to finish before continuing.
12666 *
12667 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012668 * memory this function will return -ENOMEM. If the queue create mailbox command
12669 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012670 **/
12671uint32_t
12672lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12673 struct lpfc_queue *cq, uint32_t subtype)
12674{
12675 struct lpfc_mbx_wq_create *wq_create;
12676 struct lpfc_dmabuf *dmabuf;
12677 LPFC_MBOXQ_t *mbox;
12678 int rc, length, status = 0;
12679 uint32_t shdr_status, shdr_add_status;
12680 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012681 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012682 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012683
James Smart2e90f4b2011-12-13 13:22:37 -050012684 /* sanity check on queue memory */
12685 if (!wq || !cq)
12686 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012687 if (!phba->sli4_hba.pc_sli4_params.supported)
12688 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012689
12690 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12691 if (!mbox)
12692 return -ENOMEM;
12693 length = (sizeof(struct lpfc_mbx_wq_create) -
12694 sizeof(struct lpfc_sli4_cfg_mhdr));
12695 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12696 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12697 length, LPFC_SLI4_MBX_EMBED);
12698 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012699 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012700 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12701 wq->page_count);
12702 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12703 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012704 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12705 phba->sli4_hba.pc_sli4_params.wqv);
12706 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12707 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12708 wq->entry_count);
12709 switch (wq->entry_size) {
12710 default:
12711 case 64:
12712 bf_set(lpfc_mbx_wq_create_wqe_size,
12713 &wq_create->u.request_1,
12714 LPFC_WQ_WQE_SIZE_64);
12715 break;
12716 case 128:
12717 bf_set(lpfc_mbx_wq_create_wqe_size,
12718 &wq_create->u.request_1,
12719 LPFC_WQ_WQE_SIZE_128);
12720 break;
12721 }
12722 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12723 (PAGE_SIZE/SLI4_PAGE_SIZE));
12724 page = wq_create->u.request_1.page;
12725 } else {
12726 page = wq_create->u.request.page;
12727 }
James Smart4f774512009-05-22 14:52:35 -040012728 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012729 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012730 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12731 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012732 }
12733 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12734 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012735 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12736 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12737 if (shdr_status || shdr_add_status || rc) {
12738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12739 "2503 WQ_CREATE mailbox failed with "
12740 "status x%x add_status x%x, mbx status x%x\n",
12741 shdr_status, shdr_add_status, rc);
12742 status = -ENXIO;
12743 goto out;
12744 }
12745 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12746 if (wq->queue_id == 0xFFFF) {
12747 status = -ENXIO;
12748 goto out;
12749 }
12750 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012751 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012752 wq->subtype = subtype;
12753 wq->host_index = 0;
12754 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012755 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012756
12757 /* link the wq onto the parent cq child list */
12758 list_add_tail(&wq->list, &cq->child_list);
12759out:
James Smart8fa38512009-07-19 10:01:03 -040012760 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012761 return status;
12762}
12763
12764/**
James Smart73d91e52011-10-10 21:32:10 -040012765 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12766 * @phba: HBA structure that indicates port to create a queue on.
12767 * @rq: The queue structure to use for the receive queue.
12768 * @qno: The associated HBQ number
12769 *
12770 *
12771 * For SLI4 we need to adjust the RQ repost value based on
12772 * the number of buffers that are initially posted to the RQ.
12773 */
12774void
12775lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12776{
12777 uint32_t cnt;
12778
James Smart2e90f4b2011-12-13 13:22:37 -050012779 /* sanity check on queue memory */
12780 if (!rq)
12781 return;
James Smart73d91e52011-10-10 21:32:10 -040012782 cnt = lpfc_hbq_defs[qno]->entry_count;
12783
12784 /* Recalc repost for RQs based on buffers initially posted */
12785 cnt = (cnt >> 3);
12786 if (cnt < LPFC_QUEUE_MIN_REPOST)
12787 cnt = LPFC_QUEUE_MIN_REPOST;
12788
12789 rq->entry_repost = cnt;
12790}
12791
12792/**
James Smart4f774512009-05-22 14:52:35 -040012793 * lpfc_rq_create - Create a Receive Queue on the HBA
12794 * @phba: HBA structure that indicates port to create a queue on.
12795 * @hrq: The queue structure to use to create the header receive queue.
12796 * @drq: The queue structure to use to create the data receive queue.
12797 * @cq: The completion queue to bind this work queue to.
12798 *
12799 * This function creates a receive buffer queue pair , as detailed in @hrq and
12800 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12801 * to the HBA.
12802 *
12803 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12804 * struct is used to get the entry count that is necessary to determine the
12805 * number of pages to use for this queue. The @cq is used to indicate which
12806 * completion queue to bind received buffers that are posted to these queues to.
12807 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12808 * receive queue pair. This function is asynchronous and will wait for the
12809 * mailbox command to finish before continuing.
12810 *
12811 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012812 * memory this function will return -ENOMEM. If the queue create mailbox command
12813 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012814 **/
12815uint32_t
12816lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12817 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12818{
12819 struct lpfc_mbx_rq_create *rq_create;
12820 struct lpfc_dmabuf *dmabuf;
12821 LPFC_MBOXQ_t *mbox;
12822 int rc, length, status = 0;
12823 uint32_t shdr_status, shdr_add_status;
12824 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012825 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12826
James Smart2e90f4b2011-12-13 13:22:37 -050012827 /* sanity check on queue memory */
12828 if (!hrq || !drq || !cq)
12829 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012830 if (!phba->sli4_hba.pc_sli4_params.supported)
12831 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012832
12833 if (hrq->entry_count != drq->entry_count)
12834 return -EINVAL;
12835 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12836 if (!mbox)
12837 return -ENOMEM;
12838 length = (sizeof(struct lpfc_mbx_rq_create) -
12839 sizeof(struct lpfc_sli4_cfg_mhdr));
12840 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12841 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12842 length, LPFC_SLI4_MBX_EMBED);
12843 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012844 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12845 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12846 phba->sli4_hba.pc_sli4_params.rqv);
12847 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12848 bf_set(lpfc_rq_context_rqe_count_1,
12849 &rq_create->u.request.context,
12850 hrq->entry_count);
12851 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012852 bf_set(lpfc_rq_context_rqe_size,
12853 &rq_create->u.request.context,
12854 LPFC_RQE_SIZE_8);
12855 bf_set(lpfc_rq_context_page_size,
12856 &rq_create->u.request.context,
12857 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012858 } else {
12859 switch (hrq->entry_count) {
12860 default:
12861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12862 "2535 Unsupported RQ count. (%d)\n",
12863 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012864 if (hrq->entry_count < 512) {
12865 status = -EINVAL;
12866 goto out;
12867 }
James Smart5a6f1332011-03-11 16:05:35 -050012868 /* otherwise default to smallest count (drop through) */
12869 case 512:
12870 bf_set(lpfc_rq_context_rqe_count,
12871 &rq_create->u.request.context,
12872 LPFC_RQ_RING_SIZE_512);
12873 break;
12874 case 1024:
12875 bf_set(lpfc_rq_context_rqe_count,
12876 &rq_create->u.request.context,
12877 LPFC_RQ_RING_SIZE_1024);
12878 break;
12879 case 2048:
12880 bf_set(lpfc_rq_context_rqe_count,
12881 &rq_create->u.request.context,
12882 LPFC_RQ_RING_SIZE_2048);
12883 break;
12884 case 4096:
12885 bf_set(lpfc_rq_context_rqe_count,
12886 &rq_create->u.request.context,
12887 LPFC_RQ_RING_SIZE_4096);
12888 break;
12889 }
12890 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12891 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012892 }
12893 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12894 cq->queue_id);
12895 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12896 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012897 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012898 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012899 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12900 putPaddrLow(dmabuf->phys);
12901 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12902 putPaddrHigh(dmabuf->phys);
12903 }
12904 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12905 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012906 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12907 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12908 if (shdr_status || shdr_add_status || rc) {
12909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12910 "2504 RQ_CREATE mailbox failed with "
12911 "status x%x add_status x%x, mbx status x%x\n",
12912 shdr_status, shdr_add_status, rc);
12913 status = -ENXIO;
12914 goto out;
12915 }
12916 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12917 if (hrq->queue_id == 0xFFFF) {
12918 status = -ENXIO;
12919 goto out;
12920 }
12921 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012922 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012923 hrq->subtype = subtype;
12924 hrq->host_index = 0;
12925 hrq->hba_index = 0;
12926
12927 /* now create the data queue */
12928 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12929 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12930 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012931 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12932 phba->sli4_hba.pc_sli4_params.rqv);
12933 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12934 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012935 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012936 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012937 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12938 LPFC_RQE_SIZE_8);
12939 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12940 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012941 } else {
12942 switch (drq->entry_count) {
12943 default:
12944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12945 "2536 Unsupported RQ count. (%d)\n",
12946 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012947 if (drq->entry_count < 512) {
12948 status = -EINVAL;
12949 goto out;
12950 }
James Smart5a6f1332011-03-11 16:05:35 -050012951 /* otherwise default to smallest count (drop through) */
12952 case 512:
12953 bf_set(lpfc_rq_context_rqe_count,
12954 &rq_create->u.request.context,
12955 LPFC_RQ_RING_SIZE_512);
12956 break;
12957 case 1024:
12958 bf_set(lpfc_rq_context_rqe_count,
12959 &rq_create->u.request.context,
12960 LPFC_RQ_RING_SIZE_1024);
12961 break;
12962 case 2048:
12963 bf_set(lpfc_rq_context_rqe_count,
12964 &rq_create->u.request.context,
12965 LPFC_RQ_RING_SIZE_2048);
12966 break;
12967 case 4096:
12968 bf_set(lpfc_rq_context_rqe_count,
12969 &rq_create->u.request.context,
12970 LPFC_RQ_RING_SIZE_4096);
12971 break;
12972 }
12973 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12974 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012975 }
12976 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12977 cq->queue_id);
12978 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12979 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012980 list_for_each_entry(dmabuf, &drq->page_list, list) {
12981 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12982 putPaddrLow(dmabuf->phys);
12983 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12984 putPaddrHigh(dmabuf->phys);
12985 }
12986 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12987 /* The IOCTL status is embedded in the mailbox subheader. */
12988 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12989 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12990 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12991 if (shdr_status || shdr_add_status || rc) {
12992 status = -ENXIO;
12993 goto out;
12994 }
12995 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12996 if (drq->queue_id == 0xFFFF) {
12997 status = -ENXIO;
12998 goto out;
12999 }
13000 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013001 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013002 drq->subtype = subtype;
13003 drq->host_index = 0;
13004 drq->hba_index = 0;
13005
13006 /* link the header and data RQs onto the parent cq child list */
13007 list_add_tail(&hrq->list, &cq->child_list);
13008 list_add_tail(&drq->list, &cq->child_list);
13009
13010out:
James Smart8fa38512009-07-19 10:01:03 -040013011 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013012 return status;
13013}
13014
13015/**
13016 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13017 * @eq: The queue structure associated with the queue to destroy.
13018 *
13019 * This function destroys a queue, as detailed in @eq by sending an mailbox
13020 * command, specific to the type of queue, to the HBA.
13021 *
13022 * The @eq struct is used to get the queue ID of the queue to destroy.
13023 *
13024 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013025 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013026 **/
13027uint32_t
13028lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13029{
13030 LPFC_MBOXQ_t *mbox;
13031 int rc, length, status = 0;
13032 uint32_t shdr_status, shdr_add_status;
13033 union lpfc_sli4_cfg_shdr *shdr;
13034
James Smart2e90f4b2011-12-13 13:22:37 -050013035 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013036 if (!eq)
13037 return -ENODEV;
13038 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13039 if (!mbox)
13040 return -ENOMEM;
13041 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13042 sizeof(struct lpfc_sli4_cfg_mhdr));
13043 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13044 LPFC_MBOX_OPCODE_EQ_DESTROY,
13045 length, LPFC_SLI4_MBX_EMBED);
13046 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13047 eq->queue_id);
13048 mbox->vport = eq->phba->pport;
13049 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13050
13051 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13052 /* The IOCTL status is embedded in the mailbox subheader. */
13053 shdr = (union lpfc_sli4_cfg_shdr *)
13054 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13055 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13056 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13057 if (shdr_status || shdr_add_status || rc) {
13058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13059 "2505 EQ_DESTROY mailbox failed with "
13060 "status x%x add_status x%x, mbx status x%x\n",
13061 shdr_status, shdr_add_status, rc);
13062 status = -ENXIO;
13063 }
13064
13065 /* Remove eq from any list */
13066 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013067 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013068 return status;
13069}
13070
13071/**
13072 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13073 * @cq: The queue structure associated with the queue to destroy.
13074 *
13075 * This function destroys a queue, as detailed in @cq by sending an mailbox
13076 * command, specific to the type of queue, to the HBA.
13077 *
13078 * The @cq struct is used to get the queue ID of the queue to destroy.
13079 *
13080 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013081 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013082 **/
13083uint32_t
13084lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13085{
13086 LPFC_MBOXQ_t *mbox;
13087 int rc, length, status = 0;
13088 uint32_t shdr_status, shdr_add_status;
13089 union lpfc_sli4_cfg_shdr *shdr;
13090
James Smart2e90f4b2011-12-13 13:22:37 -050013091 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013092 if (!cq)
13093 return -ENODEV;
13094 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13095 if (!mbox)
13096 return -ENOMEM;
13097 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13098 sizeof(struct lpfc_sli4_cfg_mhdr));
13099 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13100 LPFC_MBOX_OPCODE_CQ_DESTROY,
13101 length, LPFC_SLI4_MBX_EMBED);
13102 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13103 cq->queue_id);
13104 mbox->vport = cq->phba->pport;
13105 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13106 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13107 /* The IOCTL status is embedded in the mailbox subheader. */
13108 shdr = (union lpfc_sli4_cfg_shdr *)
13109 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13110 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13111 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13112 if (shdr_status || shdr_add_status || rc) {
13113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13114 "2506 CQ_DESTROY mailbox failed with "
13115 "status x%x add_status x%x, mbx status x%x\n",
13116 shdr_status, shdr_add_status, rc);
13117 status = -ENXIO;
13118 }
13119 /* Remove cq from any list */
13120 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013121 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013122 return status;
13123}
13124
13125/**
James Smart04c68492009-05-22 14:52:52 -040013126 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13127 * @qm: The queue structure associated with the queue to destroy.
13128 *
13129 * This function destroys a queue, as detailed in @mq by sending an mailbox
13130 * command, specific to the type of queue, to the HBA.
13131 *
13132 * The @mq struct is used to get the queue ID of the queue to destroy.
13133 *
13134 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013135 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013136 **/
13137uint32_t
13138lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13139{
13140 LPFC_MBOXQ_t *mbox;
13141 int rc, length, status = 0;
13142 uint32_t shdr_status, shdr_add_status;
13143 union lpfc_sli4_cfg_shdr *shdr;
13144
James Smart2e90f4b2011-12-13 13:22:37 -050013145 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013146 if (!mq)
13147 return -ENODEV;
13148 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13149 if (!mbox)
13150 return -ENOMEM;
13151 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13152 sizeof(struct lpfc_sli4_cfg_mhdr));
13153 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13154 LPFC_MBOX_OPCODE_MQ_DESTROY,
13155 length, LPFC_SLI4_MBX_EMBED);
13156 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13157 mq->queue_id);
13158 mbox->vport = mq->phba->pport;
13159 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13160 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13161 /* The IOCTL status is embedded in the mailbox subheader. */
13162 shdr = (union lpfc_sli4_cfg_shdr *)
13163 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13164 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13165 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13166 if (shdr_status || shdr_add_status || rc) {
13167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13168 "2507 MQ_DESTROY mailbox failed with "
13169 "status x%x add_status x%x, mbx status x%x\n",
13170 shdr_status, shdr_add_status, rc);
13171 status = -ENXIO;
13172 }
13173 /* Remove mq from any list */
13174 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013175 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013176 return status;
13177}
13178
13179/**
James Smart4f774512009-05-22 14:52:35 -040013180 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13181 * @wq: The queue structure associated with the queue to destroy.
13182 *
13183 * This function destroys a queue, as detailed in @wq by sending an mailbox
13184 * command, specific to the type of queue, to the HBA.
13185 *
13186 * The @wq struct is used to get the queue ID of the queue to destroy.
13187 *
13188 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013189 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013190 **/
13191uint32_t
13192lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13193{
13194 LPFC_MBOXQ_t *mbox;
13195 int rc, length, status = 0;
13196 uint32_t shdr_status, shdr_add_status;
13197 union lpfc_sli4_cfg_shdr *shdr;
13198
James Smart2e90f4b2011-12-13 13:22:37 -050013199 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013200 if (!wq)
13201 return -ENODEV;
13202 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13203 if (!mbox)
13204 return -ENOMEM;
13205 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13206 sizeof(struct lpfc_sli4_cfg_mhdr));
13207 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13208 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13209 length, LPFC_SLI4_MBX_EMBED);
13210 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13211 wq->queue_id);
13212 mbox->vport = wq->phba->pport;
13213 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13214 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13215 shdr = (union lpfc_sli4_cfg_shdr *)
13216 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13217 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13218 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13219 if (shdr_status || shdr_add_status || rc) {
13220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13221 "2508 WQ_DESTROY mailbox failed with "
13222 "status x%x add_status x%x, mbx status x%x\n",
13223 shdr_status, shdr_add_status, rc);
13224 status = -ENXIO;
13225 }
13226 /* Remove wq from any list */
13227 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013228 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013229 return status;
13230}
13231
13232/**
13233 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13234 * @rq: The queue structure associated with the queue to destroy.
13235 *
13236 * This function destroys a queue, as detailed in @rq by sending an mailbox
13237 * command, specific to the type of queue, to the HBA.
13238 *
13239 * The @rq struct is used to get the queue ID of the queue to destroy.
13240 *
13241 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013242 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013243 **/
13244uint32_t
13245lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13246 struct lpfc_queue *drq)
13247{
13248 LPFC_MBOXQ_t *mbox;
13249 int rc, length, status = 0;
13250 uint32_t shdr_status, shdr_add_status;
13251 union lpfc_sli4_cfg_shdr *shdr;
13252
James Smart2e90f4b2011-12-13 13:22:37 -050013253 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013254 if (!hrq || !drq)
13255 return -ENODEV;
13256 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13257 if (!mbox)
13258 return -ENOMEM;
13259 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013260 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013261 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13262 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13263 length, LPFC_SLI4_MBX_EMBED);
13264 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13265 hrq->queue_id);
13266 mbox->vport = hrq->phba->pport;
13267 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13268 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13269 /* The IOCTL status is embedded in the mailbox subheader. */
13270 shdr = (union lpfc_sli4_cfg_shdr *)
13271 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13272 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13273 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13274 if (shdr_status || shdr_add_status || rc) {
13275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13276 "2509 RQ_DESTROY mailbox failed with "
13277 "status x%x add_status x%x, mbx status x%x\n",
13278 shdr_status, shdr_add_status, rc);
13279 if (rc != MBX_TIMEOUT)
13280 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13281 return -ENXIO;
13282 }
13283 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13284 drq->queue_id);
13285 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13286 shdr = (union lpfc_sli4_cfg_shdr *)
13287 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13288 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13289 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13290 if (shdr_status || shdr_add_status || rc) {
13291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13292 "2510 RQ_DESTROY mailbox failed with "
13293 "status x%x add_status x%x, mbx status x%x\n",
13294 shdr_status, shdr_add_status, rc);
13295 status = -ENXIO;
13296 }
13297 list_del_init(&hrq->list);
13298 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013299 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013300 return status;
13301}
13302
13303/**
13304 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13305 * @phba: The virtual port for which this call being executed.
13306 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13307 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13308 * @xritag: the xritag that ties this io to the SGL pages.
13309 *
13310 * This routine will post the sgl pages for the IO that has the xritag
13311 * that is in the iocbq structure. The xritag is assigned during iocbq
13312 * creation and persists for as long as the driver is loaded.
13313 * if the caller has fewer than 256 scatter gather segments to map then
13314 * pdma_phys_addr1 should be 0.
13315 * If the caller needs to map more than 256 scatter gather segment then
13316 * pdma_phys_addr1 should be a valid physical address.
13317 * physical address for SGLs must be 64 byte aligned.
13318 * If you are going to map 2 SGL's then the first one must have 256 entries
13319 * the second sgl can have between 1 and 256 entries.
13320 *
13321 * Return codes:
13322 * 0 - Success
13323 * -ENXIO, -ENOMEM - Failure
13324 **/
13325int
13326lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13327 dma_addr_t pdma_phys_addr0,
13328 dma_addr_t pdma_phys_addr1,
13329 uint16_t xritag)
13330{
13331 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13332 LPFC_MBOXQ_t *mbox;
13333 int rc;
13334 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013335 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013336 union lpfc_sli4_cfg_shdr *shdr;
13337
13338 if (xritag == NO_XRI) {
13339 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13340 "0364 Invalid param:\n");
13341 return -EINVAL;
13342 }
13343
13344 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13345 if (!mbox)
13346 return -ENOMEM;
13347
13348 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13349 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13350 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013351 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013352
13353 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13354 &mbox->u.mqe.un.post_sgl_pages;
13355 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13356 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13357
13358 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13359 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13360 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13361 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13362
13363 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13364 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13365 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13366 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13367 if (!phba->sli4_hba.intr_enable)
13368 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013369 else {
James Smarta183a152011-10-10 21:32:43 -040013370 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013371 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13372 }
James Smart4f774512009-05-22 14:52:35 -040013373 /* The IOCTL status is embedded in the mailbox subheader. */
13374 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13377 if (rc != MBX_TIMEOUT)
13378 mempool_free(mbox, phba->mbox_mem_pool);
13379 if (shdr_status || shdr_add_status || rc) {
13380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13381 "2511 POST_SGL mailbox failed with "
13382 "status x%x add_status x%x, mbx status x%x\n",
13383 shdr_status, shdr_add_status, rc);
13384 rc = -ENXIO;
13385 }
13386 return 0;
13387}
James Smart4f774512009-05-22 14:52:35 -040013388
13389/**
James Smart88a2cfb2011-07-22 18:36:33 -040013390 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013391 * @phba: pointer to lpfc hba data structure.
13392 *
13393 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013394 * HBA consistent with the SLI-4 interface spec. This routine
13395 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13396 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013397 *
James Smart88a2cfb2011-07-22 18:36:33 -040013398 * Returns
13399 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13400 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13401 **/
James Smart6d368e52011-05-24 11:44:12 -040013402uint16_t
13403lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13404{
13405 unsigned long xri;
13406
13407 /*
13408 * Fetch the next logical xri. Because this index is logical,
13409 * the driver starts at 0 each time.
13410 */
13411 spin_lock_irq(&phba->hbalock);
13412 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13413 phba->sli4_hba.max_cfg_param.max_xri, 0);
13414 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13415 spin_unlock_irq(&phba->hbalock);
13416 return NO_XRI;
13417 } else {
13418 set_bit(xri, phba->sli4_hba.xri_bmask);
13419 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013420 }
James Smart6d368e52011-05-24 11:44:12 -040013421 spin_unlock_irq(&phba->hbalock);
13422 return xri;
13423}
13424
13425/**
13426 * lpfc_sli4_free_xri - Release an xri for reuse.
13427 * @phba: pointer to lpfc hba data structure.
13428 *
13429 * This routine is invoked to release an xri to the pool of
13430 * available rpis maintained by the driver.
13431 **/
13432void
13433__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13434{
13435 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013436 phba->sli4_hba.max_cfg_param.xri_used--;
13437 }
13438}
13439
13440/**
13441 * lpfc_sli4_free_xri - Release an xri for reuse.
13442 * @phba: pointer to lpfc hba data structure.
13443 *
13444 * This routine is invoked to release an xri to the pool of
13445 * available rpis maintained by the driver.
13446 **/
13447void
13448lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13449{
13450 spin_lock_irq(&phba->hbalock);
13451 __lpfc_sli4_free_xri(phba, xri);
13452 spin_unlock_irq(&phba->hbalock);
13453}
13454
13455/**
James Smart4f774512009-05-22 14:52:35 -040013456 * lpfc_sli4_next_xritag - Get an xritag for the io
13457 * @phba: Pointer to HBA context object.
13458 *
13459 * This function gets an xritag for the iocb. If there is no unused xritag
13460 * it will return 0xffff.
13461 * The function returns the allocated xritag if successful, else returns zero.
13462 * Zero is not a valid xritag.
13463 * The caller is not required to hold any lock.
13464 **/
13465uint16_t
13466lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13467{
James Smart6d368e52011-05-24 11:44:12 -040013468 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013469
James Smart6d368e52011-05-24 11:44:12 -040013470 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013471 if (xri_index == NO_XRI)
13472 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13473 "2004 Failed to allocate XRI.last XRITAG is %d"
13474 " Max XRI is %d, Used XRI is %d\n",
13475 xri_index,
13476 phba->sli4_hba.max_cfg_param.max_xri,
13477 phba->sli4_hba.max_cfg_param.xri_used);
13478 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013479}
13480
13481/**
James Smart6d368e52011-05-24 11:44:12 -040013482 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013483 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013484 * @post_sgl_list: pointer to els sgl entry list.
13485 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013486 *
13487 * This routine is invoked to post a block of driver's sgl pages to the
13488 * HBA using non-embedded mailbox command. No Lock is held. This routine
13489 * is only called when the driver is loading and after all IO has been
13490 * stopped.
13491 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013492static int
13493lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13494 struct list_head *post_sgl_list,
13495 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013496{
James Smart8a9d2e82012-05-09 21:16:12 -040013497 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013498 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13499 struct sgl_page_pairs *sgl_pg_pairs;
13500 void *viraddr;
13501 LPFC_MBOXQ_t *mbox;
13502 uint32_t reqlen, alloclen, pg_pairs;
13503 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013504 uint16_t xritag_start = 0;
13505 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013506 uint32_t shdr_status, shdr_add_status;
13507 union lpfc_sli4_cfg_shdr *shdr;
13508
James Smart8a9d2e82012-05-09 21:16:12 -040013509 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013510 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013511 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013512 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13513 "2559 Block sgl registration required DMA "
13514 "size (%d) great than a page\n", reqlen);
13515 return -ENOMEM;
13516 }
13517 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013518 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013519 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013520
13521 /* Allocate DMA memory and set up the non-embedded mailbox command */
13522 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13523 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13524 LPFC_SLI4_MBX_NEMBED);
13525
13526 if (alloclen < reqlen) {
13527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13528 "0285 Allocated DMA memory size (%d) is "
13529 "less than the requested DMA memory "
13530 "size (%d)\n", alloclen, reqlen);
13531 lpfc_sli4_mbox_cmd_free(phba, mbox);
13532 return -ENOMEM;
13533 }
James Smart4f774512009-05-22 14:52:35 -040013534 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013535 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013536 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13537 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13538
James Smart8a9d2e82012-05-09 21:16:12 -040013539 pg_pairs = 0;
13540 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013541 /* Set up the sge entry */
13542 sgl_pg_pairs->sgl_pg0_addr_lo =
13543 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13544 sgl_pg_pairs->sgl_pg0_addr_hi =
13545 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13546 sgl_pg_pairs->sgl_pg1_addr_lo =
13547 cpu_to_le32(putPaddrLow(0));
13548 sgl_pg_pairs->sgl_pg1_addr_hi =
13549 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013550
James Smart4f774512009-05-22 14:52:35 -040013551 /* Keep the first xritag on the list */
13552 if (pg_pairs == 0)
13553 xritag_start = sglq_entry->sli4_xritag;
13554 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013555 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013556 }
James Smart6d368e52011-05-24 11:44:12 -040013557
13558 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013559 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013560 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013561 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013562 if (!phba->sli4_hba.intr_enable)
13563 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13564 else {
James Smarta183a152011-10-10 21:32:43 -040013565 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013566 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13567 }
13568 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13569 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13570 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13571 if (rc != MBX_TIMEOUT)
13572 lpfc_sli4_mbox_cmd_free(phba, mbox);
13573 if (shdr_status || shdr_add_status || rc) {
13574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13575 "2513 POST_SGL_BLOCK mailbox command failed "
13576 "status x%x add_status x%x mbx status x%x\n",
13577 shdr_status, shdr_add_status, rc);
13578 rc = -ENXIO;
13579 }
13580 return rc;
13581}
13582
13583/**
13584 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13585 * @phba: pointer to lpfc hba data structure.
13586 * @sblist: pointer to scsi buffer list.
13587 * @count: number of scsi buffers on the list.
13588 *
13589 * This routine is invoked to post a block of @count scsi sgl pages from a
13590 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13591 * No Lock is held.
13592 *
13593 **/
13594int
James Smart8a9d2e82012-05-09 21:16:12 -040013595lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13596 struct list_head *sblist,
13597 int count)
James Smart4f774512009-05-22 14:52:35 -040013598{
13599 struct lpfc_scsi_buf *psb;
13600 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13601 struct sgl_page_pairs *sgl_pg_pairs;
13602 void *viraddr;
13603 LPFC_MBOXQ_t *mbox;
13604 uint32_t reqlen, alloclen, pg_pairs;
13605 uint32_t mbox_tmo;
13606 uint16_t xritag_start = 0;
13607 int rc = 0;
13608 uint32_t shdr_status, shdr_add_status;
13609 dma_addr_t pdma_phys_bpl1;
13610 union lpfc_sli4_cfg_shdr *shdr;
13611
13612 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013613 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013614 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013615 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013616 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13617 "0217 Block sgl registration required DMA "
13618 "size (%d) great than a page\n", reqlen);
13619 return -ENOMEM;
13620 }
13621 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13622 if (!mbox) {
13623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13624 "0283 Failed to allocate mbox cmd memory\n");
13625 return -ENOMEM;
13626 }
13627
13628 /* Allocate DMA memory and set up the non-embedded mailbox command */
13629 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13630 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13631 LPFC_SLI4_MBX_NEMBED);
13632
13633 if (alloclen < reqlen) {
13634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13635 "2561 Allocated DMA memory size (%d) is "
13636 "less than the requested DMA memory "
13637 "size (%d)\n", alloclen, reqlen);
13638 lpfc_sli4_mbox_cmd_free(phba, mbox);
13639 return -ENOMEM;
13640 }
James Smart6d368e52011-05-24 11:44:12 -040013641
James Smart4f774512009-05-22 14:52:35 -040013642 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013643 viraddr = mbox->sge_array->addr[0];
13644
13645 /* Set up the SGL pages in the non-embedded DMA pages */
13646 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13647 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13648
13649 pg_pairs = 0;
13650 list_for_each_entry(psb, sblist, list) {
13651 /* Set up the sge entry */
13652 sgl_pg_pairs->sgl_pg0_addr_lo =
13653 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13654 sgl_pg_pairs->sgl_pg0_addr_hi =
13655 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13656 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13657 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13658 else
13659 pdma_phys_bpl1 = 0;
13660 sgl_pg_pairs->sgl_pg1_addr_lo =
13661 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13662 sgl_pg_pairs->sgl_pg1_addr_hi =
13663 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13664 /* Keep the first xritag on the list */
13665 if (pg_pairs == 0)
13666 xritag_start = psb->cur_iocbq.sli4_xritag;
13667 sgl_pg_pairs++;
13668 pg_pairs++;
13669 }
13670 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13671 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13672 /* Perform endian conversion if necessary */
13673 sgl->word0 = cpu_to_le32(sgl->word0);
13674
13675 if (!phba->sli4_hba.intr_enable)
13676 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13677 else {
James Smarta183a152011-10-10 21:32:43 -040013678 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013679 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13680 }
13681 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13682 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13683 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13684 if (rc != MBX_TIMEOUT)
13685 lpfc_sli4_mbox_cmd_free(phba, mbox);
13686 if (shdr_status || shdr_add_status || rc) {
13687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13688 "2564 POST_SGL_BLOCK mailbox command failed "
13689 "status x%x add_status x%x mbx status x%x\n",
13690 shdr_status, shdr_add_status, rc);
13691 rc = -ENXIO;
13692 }
13693 return rc;
13694}
13695
13696/**
13697 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13698 * @phba: pointer to lpfc_hba struct that the frame was received on
13699 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13700 *
13701 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13702 * valid type of frame that the LPFC driver will handle. This function will
13703 * return a zero if the frame is a valid frame or a non zero value when the
13704 * frame does not pass the check.
13705 **/
13706static int
13707lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13708{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013709 /* make rctl_names static to save stack space */
13710 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013711 char *type_names[] = FC_TYPE_NAMES_INIT;
13712 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013713 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013714
13715 switch (fc_hdr->fh_r_ctl) {
13716 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13717 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13718 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13719 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13720 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13721 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13722 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13723 case FC_RCTL_DD_CMD_STATUS: /* command status */
13724 case FC_RCTL_ELS_REQ: /* extended link services request */
13725 case FC_RCTL_ELS_REP: /* extended link services reply */
13726 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13727 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13728 case FC_RCTL_BA_NOP: /* basic link service NOP */
13729 case FC_RCTL_BA_ABTS: /* basic link service abort */
13730 case FC_RCTL_BA_RMC: /* remove connection */
13731 case FC_RCTL_BA_ACC: /* basic accept */
13732 case FC_RCTL_BA_RJT: /* basic reject */
13733 case FC_RCTL_BA_PRMT:
13734 case FC_RCTL_ACK_1: /* acknowledge_1 */
13735 case FC_RCTL_ACK_0: /* acknowledge_0 */
13736 case FC_RCTL_P_RJT: /* port reject */
13737 case FC_RCTL_F_RJT: /* fabric reject */
13738 case FC_RCTL_P_BSY: /* port busy */
13739 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13740 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13741 case FC_RCTL_LCR: /* link credit reset */
13742 case FC_RCTL_END: /* end */
13743 break;
13744 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13745 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13746 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13747 return lpfc_fc_frame_check(phba, fc_hdr);
13748 default:
13749 goto drop;
13750 }
13751 switch (fc_hdr->fh_type) {
13752 case FC_TYPE_BLS:
13753 case FC_TYPE_ELS:
13754 case FC_TYPE_FCP:
13755 case FC_TYPE_CT:
13756 break;
13757 case FC_TYPE_IP:
13758 case FC_TYPE_ILS:
13759 default:
13760 goto drop;
13761 }
James Smart546fc852011-03-11 16:06:29 -050013762
James Smart4f774512009-05-22 14:52:35 -040013763 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013764 "2538 Received frame rctl:%s type:%s "
13765 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013766 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013767 type_names[fc_hdr->fh_type],
13768 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13769 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13770 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013771 return 0;
13772drop:
13773 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13774 "2539 Dropped frame rctl:%s type:%s\n",
13775 rctl_names[fc_hdr->fh_r_ctl],
13776 type_names[fc_hdr->fh_type]);
13777 return 1;
13778}
13779
13780/**
13781 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13782 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13783 *
13784 * This function processes the FC header to retrieve the VFI from the VF
13785 * header, if one exists. This function will return the VFI if one exists
13786 * or 0 if no VSAN Header exists.
13787 **/
13788static uint32_t
13789lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13790{
13791 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13792
13793 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13794 return 0;
13795 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13796}
13797
13798/**
13799 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13800 * @phba: Pointer to the HBA structure to search for the vport on
13801 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13802 * @fcfi: The FC Fabric ID that the frame came from
13803 *
13804 * This function searches the @phba for a vport that matches the content of the
13805 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13806 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13807 * returns the matching vport pointer or NULL if unable to match frame to a
13808 * vport.
13809 **/
13810static struct lpfc_vport *
13811lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13812 uint16_t fcfi)
13813{
13814 struct lpfc_vport **vports;
13815 struct lpfc_vport *vport = NULL;
13816 int i;
13817 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13818 fc_hdr->fh_d_id[1] << 8 |
13819 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013820
James Smartbf086112011-08-21 21:48:13 -040013821 if (did == Fabric_DID)
13822 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013823 if ((phba->pport->fc_flag & FC_PT2PT) &&
13824 !(phba->link_state == LPFC_HBA_READY))
13825 return phba->pport;
13826
James Smart4f774512009-05-22 14:52:35 -040013827 vports = lpfc_create_vport_work_array(phba);
13828 if (vports != NULL)
13829 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13830 if (phba->fcf.fcfi == fcfi &&
13831 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13832 vports[i]->fc_myDID == did) {
13833 vport = vports[i];
13834 break;
13835 }
13836 }
13837 lpfc_destroy_vport_work_array(phba, vports);
13838 return vport;
13839}
13840
13841/**
James Smart45ed1192009-10-02 15:17:02 -040013842 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13843 * @vport: The vport to work on.
13844 *
13845 * This function updates the receive sequence time stamp for this vport. The
13846 * receive sequence time stamp indicates the time that the last frame of the
13847 * the sequence that has been idle for the longest amount of time was received.
13848 * the driver uses this time stamp to indicate if any received sequences have
13849 * timed out.
13850 **/
13851void
13852lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13853{
13854 struct lpfc_dmabuf *h_buf;
13855 struct hbq_dmabuf *dmabuf = NULL;
13856
13857 /* get the oldest sequence on the rcv list */
13858 h_buf = list_get_first(&vport->rcv_buffer_list,
13859 struct lpfc_dmabuf, list);
13860 if (!h_buf)
13861 return;
13862 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13863 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13864}
13865
13866/**
13867 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13868 * @vport: The vport that the received sequences were sent to.
13869 *
13870 * This function cleans up all outstanding received sequences. This is called
13871 * by the driver when a link event or user action invalidates all the received
13872 * sequences.
13873 **/
13874void
13875lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13876{
13877 struct lpfc_dmabuf *h_buf, *hnext;
13878 struct lpfc_dmabuf *d_buf, *dnext;
13879 struct hbq_dmabuf *dmabuf = NULL;
13880
13881 /* start with the oldest sequence on the rcv list */
13882 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13883 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13884 list_del_init(&dmabuf->hbuf.list);
13885 list_for_each_entry_safe(d_buf, dnext,
13886 &dmabuf->dbuf.list, list) {
13887 list_del_init(&d_buf->list);
13888 lpfc_in_buf_free(vport->phba, d_buf);
13889 }
13890 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13891 }
13892}
13893
13894/**
13895 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13896 * @vport: The vport that the received sequences were sent to.
13897 *
13898 * This function determines whether any received sequences have timed out by
13899 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13900 * indicates that there is at least one timed out sequence this routine will
13901 * go through the received sequences one at a time from most inactive to most
13902 * active to determine which ones need to be cleaned up. Once it has determined
13903 * that a sequence needs to be cleaned up it will simply free up the resources
13904 * without sending an abort.
13905 **/
13906void
13907lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13908{
13909 struct lpfc_dmabuf *h_buf, *hnext;
13910 struct lpfc_dmabuf *d_buf, *dnext;
13911 struct hbq_dmabuf *dmabuf = NULL;
13912 unsigned long timeout;
13913 int abort_count = 0;
13914
13915 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13916 vport->rcv_buffer_time_stamp);
13917 if (list_empty(&vport->rcv_buffer_list) ||
13918 time_before(jiffies, timeout))
13919 return;
13920 /* start with the oldest sequence on the rcv list */
13921 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13922 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13923 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13924 dmabuf->time_stamp);
13925 if (time_before(jiffies, timeout))
13926 break;
13927 abort_count++;
13928 list_del_init(&dmabuf->hbuf.list);
13929 list_for_each_entry_safe(d_buf, dnext,
13930 &dmabuf->dbuf.list, list) {
13931 list_del_init(&d_buf->list);
13932 lpfc_in_buf_free(vport->phba, d_buf);
13933 }
13934 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13935 }
13936 if (abort_count)
13937 lpfc_update_rcv_time_stamp(vport);
13938}
13939
13940/**
James Smart4f774512009-05-22 14:52:35 -040013941 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13942 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13943 *
13944 * This function searches through the existing incomplete sequences that have
13945 * been sent to this @vport. If the frame matches one of the incomplete
13946 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13947 * make up that sequence. If no sequence is found that matches this frame then
13948 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13949 * This function returns a pointer to the first dmabuf in the sequence list that
13950 * the frame was linked to.
13951 **/
13952static struct hbq_dmabuf *
13953lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13954{
13955 struct fc_frame_header *new_hdr;
13956 struct fc_frame_header *temp_hdr;
13957 struct lpfc_dmabuf *d_buf;
13958 struct lpfc_dmabuf *h_buf;
13959 struct hbq_dmabuf *seq_dmabuf = NULL;
13960 struct hbq_dmabuf *temp_dmabuf = NULL;
13961
James Smart4d9ab992009-10-02 15:16:39 -040013962 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013963 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013964 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13965 /* Use the hdr_buf to find the sequence that this frame belongs to */
13966 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13967 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13968 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13969 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13970 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13971 continue;
13972 /* found a pending sequence that matches this frame */
13973 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13974 break;
13975 }
13976 if (!seq_dmabuf) {
13977 /*
13978 * This indicates first frame received for this sequence.
13979 * Queue the buffer on the vport's rcv_buffer_list.
13980 */
13981 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013982 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013983 return dmabuf;
13984 }
13985 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013986 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13987 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013988 list_del_init(&seq_dmabuf->hbuf.list);
13989 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13990 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013991 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013992 return dmabuf;
13993 }
James Smart45ed1192009-10-02 15:17:02 -040013994 /* move this sequence to the tail to indicate a young sequence */
13995 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13996 seq_dmabuf->time_stamp = jiffies;
13997 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013998 if (list_empty(&seq_dmabuf->dbuf.list)) {
13999 temp_hdr = dmabuf->hbuf.virt;
14000 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14001 return seq_dmabuf;
14002 }
James Smart4f774512009-05-22 14:52:35 -040014003 /* find the correct place in the sequence to insert this frame */
14004 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14005 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14006 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14007 /*
14008 * If the frame's sequence count is greater than the frame on
14009 * the list then insert the frame right after this frame
14010 */
James Smarteeead812009-12-21 17:01:23 -050014011 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14012 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014013 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14014 return seq_dmabuf;
14015 }
14016 }
14017 return NULL;
14018}
14019
14020/**
James Smart6669f9b2009-10-02 15:16:45 -040014021 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14022 * @vport: pointer to a vitural port
14023 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14024 *
14025 * This function tries to abort from the partially assembed sequence, described
14026 * by the information from basic abbort @dmabuf. It checks to see whether such
14027 * partially assembled sequence held by the driver. If so, it shall free up all
14028 * the frames from the partially assembled sequence.
14029 *
14030 * Return
14031 * true -- if there is matching partially assembled sequence present and all
14032 * the frames freed with the sequence;
14033 * false -- if there is no matching partially assembled sequence present so
14034 * nothing got aborted in the lower layer driver
14035 **/
14036static bool
14037lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14038 struct hbq_dmabuf *dmabuf)
14039{
14040 struct fc_frame_header *new_hdr;
14041 struct fc_frame_header *temp_hdr;
14042 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14043 struct hbq_dmabuf *seq_dmabuf = NULL;
14044
14045 /* Use the hdr_buf to find the sequence that matches this frame */
14046 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14047 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14048 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14049 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14050 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14051 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14052 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14053 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14054 continue;
14055 /* found a pending sequence that matches this frame */
14056 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14057 break;
14058 }
14059
14060 /* Free up all the frames from the partially assembled sequence */
14061 if (seq_dmabuf) {
14062 list_for_each_entry_safe(d_buf, n_buf,
14063 &seq_dmabuf->dbuf.list, list) {
14064 list_del_init(&d_buf->list);
14065 lpfc_in_buf_free(vport->phba, d_buf);
14066 }
14067 return true;
14068 }
14069 return false;
14070}
14071
14072/**
James Smart546fc852011-03-11 16:06:29 -050014073 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014074 * @phba: Pointer to HBA context object.
14075 * @cmd_iocbq: pointer to the command iocbq structure.
14076 * @rsp_iocbq: pointer to the response iocbq structure.
14077 *
James Smart546fc852011-03-11 16:06:29 -050014078 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014079 * event. It properly releases the memory allocated to the sequence abort
14080 * accept iocb.
14081 **/
14082static void
James Smart546fc852011-03-11 16:06:29 -050014083lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014084 struct lpfc_iocbq *cmd_iocbq,
14085 struct lpfc_iocbq *rsp_iocbq)
14086{
14087 if (cmd_iocbq)
14088 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014089
14090 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14091 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14093 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14094 rsp_iocbq->iocb.ulpStatus,
14095 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014096}
14097
14098/**
James Smart6d368e52011-05-24 11:44:12 -040014099 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14100 * @phba: Pointer to HBA context object.
14101 * @xri: xri id in transaction.
14102 *
14103 * This function validates the xri maps to the known range of XRIs allocated an
14104 * used by the driver.
14105 **/
James Smart7851fe22011-07-22 18:36:52 -040014106uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014107lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14108 uint16_t xri)
14109{
14110 int i;
14111
14112 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14113 if (xri == phba->sli4_hba.xri_ids[i])
14114 return i;
14115 }
14116 return NO_XRI;
14117}
14118
James Smart6d368e52011-05-24 11:44:12 -040014119/**
James Smart546fc852011-03-11 16:06:29 -050014120 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014121 * @phba: Pointer to HBA context object.
14122 * @fc_hdr: pointer to a FC frame header.
14123 *
James Smart546fc852011-03-11 16:06:29 -050014124 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014125 * event after aborting the sequence handling.
14126 **/
14127static void
James Smart546fc852011-03-11 16:06:29 -050014128lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014129 struct fc_frame_header *fc_hdr)
14130{
14131 struct lpfc_iocbq *ctiocb = NULL;
14132 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014133 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014134 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014135 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014136 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014137
14138 if (!lpfc_is_link_up(phba))
14139 return;
14140
14141 sid = sli4_sid_from_fc_hdr(fc_hdr);
14142 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014143 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014144
14145 ndlp = lpfc_findnode_did(phba->pport, sid);
14146 if (!ndlp) {
14147 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14148 "1268 Find ndlp returned NULL for oxid:x%x "
14149 "SID:x%x\n", oxid, sid);
14150 return;
14151 }
14152
James Smart546fc852011-03-11 16:06:29 -050014153 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014154 ctiocb = lpfc_sli_get_iocbq(phba);
14155 if (!ctiocb)
14156 return;
14157
James Smart5ffc2662009-11-18 15:39:44 -050014158 /* Extract the F_CTL field from FC_HDR */
14159 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14160
James Smart6669f9b2009-10-02 15:16:45 -040014161 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014162 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014163 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014164 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14165 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14166 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14167
14168 /* Fill in the rest of iocb fields */
14169 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14170 icmd->ulpBdeCount = 0;
14171 icmd->ulpLe = 1;
14172 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014173 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014174 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014175
James Smart6669f9b2009-10-02 15:16:45 -040014176 ctiocb->iocb_cmpl = NULL;
14177 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014178 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014179 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014180 ctiocb->sli4_xritag = NO_XRI;
14181
James Smartee0f4fe2012-05-09 21:19:14 -040014182 if (fctl & FC_FC_EX_CTX)
14183 /* Exchange responder sent the abort so we
14184 * own the oxid.
14185 */
14186 xri = oxid;
14187 else
14188 xri = rxid;
14189 lxri = lpfc_sli4_xri_inrange(phba, xri);
14190 if (lxri != NO_XRI)
14191 lpfc_set_rrq_active(phba, ndlp, lxri,
14192 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014193 /* If the oxid maps to the FCP XRI range or if it is out of range,
14194 * send a BLS_RJT. The driver no longer has that exchange.
14195 * Override the IOCB for a BA_RJT.
14196 */
James Smartee0f4fe2012-05-09 21:19:14 -040014197 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014198 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014199 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014200 phba->sli4_hba.max_cfg_param.xri_base)) {
14201 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14202 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14203 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14204 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14205 }
James Smart6669f9b2009-10-02 15:16:45 -040014206
James Smart5ffc2662009-11-18 15:39:44 -050014207 if (fctl & FC_FC_EX_CTX) {
14208 /* ABTS sent by responder to CT exchange, construction
14209 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14210 * field and RX_ID from ABTS for RX_ID field.
14211 */
James Smart546fc852011-03-11 16:06:29 -050014212 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014213 } else {
14214 /* ABTS sent by initiator to CT exchange, construction
14215 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014216 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014217 */
James Smart546fc852011-03-11 16:06:29 -050014218 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014219 }
James Smartf09c3ac2012-03-01 22:33:29 -050014220 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014221 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014222
James Smart546fc852011-03-11 16:06:29 -050014223 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014224 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014225 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14226 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14227
14228 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14229 if (rc == IOCB_ERROR) {
14230 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14231 "2925 Failed to issue CT ABTS RSP x%x on "
14232 "xri x%x, Data x%x\n",
14233 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14234 phba->link_state);
14235 lpfc_sli_release_iocbq(phba, ctiocb);
14236 }
James Smart6669f9b2009-10-02 15:16:45 -040014237}
14238
14239/**
14240 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14241 * @vport: Pointer to the vport on which this sequence was received
14242 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14243 *
14244 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14245 * receive sequence is only partially assembed by the driver, it shall abort
14246 * the partially assembled frames for the sequence. Otherwise, if the
14247 * unsolicited receive sequence has been completely assembled and passed to
14248 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14249 * unsolicited sequence has been aborted. After that, it will issue a basic
14250 * accept to accept the abort.
14251 **/
14252void
14253lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14254 struct hbq_dmabuf *dmabuf)
14255{
14256 struct lpfc_hba *phba = vport->phba;
14257 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014258 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014259 bool abts_par;
14260
James Smart6669f9b2009-10-02 15:16:45 -040014261 /* Make a copy of fc_hdr before the dmabuf being released */
14262 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014263 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014264
James Smart5ffc2662009-11-18 15:39:44 -050014265 if (fctl & FC_FC_EX_CTX) {
14266 /*
14267 * ABTS sent by responder to exchange, just free the buffer
14268 */
James Smart6669f9b2009-10-02 15:16:45 -040014269 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014270 } else {
14271 /*
14272 * ABTS sent by initiator to exchange, need to do cleanup
14273 */
14274 /* Try to abort partially assembled seq */
14275 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14276
14277 /* Send abort to ULP if partially seq abort failed */
14278 if (abts_par == false)
14279 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14280 else
14281 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14282 }
James Smart6669f9b2009-10-02 15:16:45 -040014283 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014284 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014285}
14286
14287/**
James Smart4f774512009-05-22 14:52:35 -040014288 * lpfc_seq_complete - Indicates if a sequence is complete
14289 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14290 *
14291 * This function checks the sequence, starting with the frame described by
14292 * @dmabuf, to see if all the frames associated with this sequence are present.
14293 * the frames associated with this sequence are linked to the @dmabuf using the
14294 * dbuf list. This function looks for two major things. 1) That the first frame
14295 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14296 * set. 3) That there are no holes in the sequence count. The function will
14297 * return 1 when the sequence is complete, otherwise it will return 0.
14298 **/
14299static int
14300lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14301{
14302 struct fc_frame_header *hdr;
14303 struct lpfc_dmabuf *d_buf;
14304 struct hbq_dmabuf *seq_dmabuf;
14305 uint32_t fctl;
14306 int seq_count = 0;
14307
14308 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14309 /* make sure first fame of sequence has a sequence count of zero */
14310 if (hdr->fh_seq_cnt != seq_count)
14311 return 0;
14312 fctl = (hdr->fh_f_ctl[0] << 16 |
14313 hdr->fh_f_ctl[1] << 8 |
14314 hdr->fh_f_ctl[2]);
14315 /* If last frame of sequence we can return success. */
14316 if (fctl & FC_FC_END_SEQ)
14317 return 1;
14318 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14319 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14320 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14321 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014322 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014323 return 0;
14324 fctl = (hdr->fh_f_ctl[0] << 16 |
14325 hdr->fh_f_ctl[1] << 8 |
14326 hdr->fh_f_ctl[2]);
14327 /* If last frame of sequence we can return success. */
14328 if (fctl & FC_FC_END_SEQ)
14329 return 1;
14330 }
14331 return 0;
14332}
14333
14334/**
14335 * lpfc_prep_seq - Prep sequence for ULP processing
14336 * @vport: Pointer to the vport on which this sequence was received
14337 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14338 *
14339 * This function takes a sequence, described by a list of frames, and creates
14340 * a list of iocbq structures to describe the sequence. This iocbq list will be
14341 * used to issue to the generic unsolicited sequence handler. This routine
14342 * returns a pointer to the first iocbq in the list. If the function is unable
14343 * to allocate an iocbq then it throw out the received frames that were not
14344 * able to be described and return a pointer to the first iocbq. If unable to
14345 * allocate any iocbqs (including the first) this function will return NULL.
14346 **/
14347static struct lpfc_iocbq *
14348lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14349{
James Smart7851fe22011-07-22 18:36:52 -040014350 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014351 struct lpfc_dmabuf *d_buf, *n_buf;
14352 struct lpfc_iocbq *first_iocbq, *iocbq;
14353 struct fc_frame_header *fc_hdr;
14354 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014355 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014356 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014357
14358 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14359 /* remove from receive buffer list */
14360 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014361 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014362 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014363 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014364 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014365 /* Get an iocbq struct to fill in. */
14366 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14367 if (first_iocbq) {
14368 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014369 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014370 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014371
14372 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14373 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14374 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14375 first_iocbq->iocb.un.rcvels.parmRo =
14376 sli4_did_from_fc_hdr(fc_hdr);
14377 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14378 } else
14379 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014380 first_iocbq->iocb.ulpContext = NO_XRI;
14381 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14382 be16_to_cpu(fc_hdr->fh_ox_id);
14383 /* iocbq is prepped for internal consumption. Physical vpi. */
14384 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14385 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014386 /* put the first buffer into the first IOCBq */
14387 first_iocbq->context2 = &seq_dmabuf->dbuf;
14388 first_iocbq->context3 = NULL;
14389 first_iocbq->iocb.ulpBdeCount = 1;
14390 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14391 LPFC_DATA_BUF_SIZE;
14392 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014393 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014394 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014395 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014396 }
14397 iocbq = first_iocbq;
14398 /*
14399 * Each IOCBq can have two Buffers assigned, so go through the list
14400 * of buffers for this sequence and save two buffers in each IOCBq
14401 */
14402 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14403 if (!iocbq) {
14404 lpfc_in_buf_free(vport->phba, d_buf);
14405 continue;
14406 }
14407 if (!iocbq->context3) {
14408 iocbq->context3 = d_buf;
14409 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014410 pbde = (struct ulp_bde64 *)
14411 &iocbq->iocb.unsli3.sli3Words[4];
14412 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014413
14414 /* We need to get the size out of the right CQE */
14415 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14416 len = bf_get(lpfc_rcqe_length,
14417 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14418 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14419 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014420 } else {
14421 iocbq = lpfc_sli_get_iocbq(vport->phba);
14422 if (!iocbq) {
14423 if (first_iocbq) {
14424 first_iocbq->iocb.ulpStatus =
14425 IOSTAT_FCP_RSP_ERROR;
14426 first_iocbq->iocb.un.ulpWord[4] =
14427 IOERR_NO_RESOURCES;
14428 }
14429 lpfc_in_buf_free(vport->phba, d_buf);
14430 continue;
14431 }
14432 iocbq->context2 = d_buf;
14433 iocbq->context3 = NULL;
14434 iocbq->iocb.ulpBdeCount = 1;
14435 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14436 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014437
14438 /* We need to get the size out of the right CQE */
14439 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14440 len = bf_get(lpfc_rcqe_length,
14441 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14442 tot_len += len;
14443 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14444
James Smart4f774512009-05-22 14:52:35 -040014445 iocbq->iocb.un.rcvels.remoteID = sid;
14446 list_add_tail(&iocbq->list, &first_iocbq->list);
14447 }
14448 }
14449 return first_iocbq;
14450}
14451
James Smart6669f9b2009-10-02 15:16:45 -040014452static void
14453lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14454 struct hbq_dmabuf *seq_dmabuf)
14455{
14456 struct fc_frame_header *fc_hdr;
14457 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14458 struct lpfc_hba *phba = vport->phba;
14459
14460 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14461 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14462 if (!iocbq) {
14463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14464 "2707 Ring %d handler: Failed to allocate "
14465 "iocb Rctl x%x Type x%x received\n",
14466 LPFC_ELS_RING,
14467 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14468 return;
14469 }
14470 if (!lpfc_complete_unsol_iocb(phba,
14471 &phba->sli.ring[LPFC_ELS_RING],
14472 iocbq, fc_hdr->fh_r_ctl,
14473 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014474 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014475 "2540 Ring %d handler: unexpected Rctl "
14476 "x%x Type x%x received\n",
14477 LPFC_ELS_RING,
14478 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14479
14480 /* Free iocb created in lpfc_prep_seq */
14481 list_for_each_entry_safe(curr_iocb, next_iocb,
14482 &iocbq->list, list) {
14483 list_del_init(&curr_iocb->list);
14484 lpfc_sli_release_iocbq(phba, curr_iocb);
14485 }
14486 lpfc_sli_release_iocbq(phba, iocbq);
14487}
14488
James Smart4f774512009-05-22 14:52:35 -040014489/**
14490 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14491 * @phba: Pointer to HBA context object.
14492 *
14493 * This function is called with no lock held. This function processes all
14494 * the received buffers and gives it to upper layers when a received buffer
14495 * indicates that it is the final frame in the sequence. The interrupt
14496 * service routine processes received buffers at interrupt contexts and adds
14497 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14498 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14499 * appropriate receive function when the final frame in a sequence is received.
14500 **/
James Smart4d9ab992009-10-02 15:16:39 -040014501void
14502lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14503 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014504{
James Smart4d9ab992009-10-02 15:16:39 -040014505 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014506 struct fc_frame_header *fc_hdr;
14507 struct lpfc_vport *vport;
14508 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014509 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014510
James Smart4f774512009-05-22 14:52:35 -040014511 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014512 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14513 /* check to see if this a valid type of frame */
14514 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14515 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14516 return;
14517 }
James Smart7851fe22011-07-22 18:36:52 -040014518 if ((bf_get(lpfc_cqe_code,
14519 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14520 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14521 &dmabuf->cq_event.cqe.rcqe_cmpl);
14522 else
14523 fcfi = bf_get(lpfc_rcqe_fcf_id,
14524 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014525
James Smart4d9ab992009-10-02 15:16:39 -040014526 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014527 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014528 /* throw out the frame */
14529 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14530 return;
14531 }
James Smart939723a2012-05-09 21:19:03 -040014532
14533 /* d_id this frame is directed to */
14534 did = sli4_did_from_fc_hdr(fc_hdr);
14535
14536 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14537 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14538 (did != Fabric_DID)) {
14539 /*
14540 * Throw out the frame if we are not pt2pt.
14541 * The pt2pt protocol allows for discovery frames
14542 * to be received without a registered VPI.
14543 */
14544 if (!(vport->fc_flag & FC_PT2PT) ||
14545 (phba->link_state == LPFC_HBA_READY)) {
14546 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14547 return;
14548 }
14549 }
14550
James Smart6669f9b2009-10-02 15:16:45 -040014551 /* Handle the basic abort sequence (BA_ABTS) event */
14552 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14553 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14554 return;
14555 }
14556
James Smart4d9ab992009-10-02 15:16:39 -040014557 /* Link this frame */
14558 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14559 if (!seq_dmabuf) {
14560 /* unable to add frame to vport - throw it out */
14561 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14562 return;
14563 }
14564 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014565 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014566 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014567
James Smart6669f9b2009-10-02 15:16:45 -040014568 /* Send the complete sequence to the upper layer protocol */
14569 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014570}
James Smart6fb120a2009-05-22 14:52:59 -040014571
14572/**
14573 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14574 * @phba: pointer to lpfc hba data structure.
14575 *
14576 * This routine is invoked to post rpi header templates to the
14577 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014578 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14579 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014580 *
14581 * This routine does not require any locks. It's usage is expected
14582 * to be driver load or reset recovery when the driver is
14583 * sequential.
14584 *
14585 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014586 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014587 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014588 * When this error occurs, the driver is not guaranteed
14589 * to have any rpi regions posted to the device and
14590 * must either attempt to repost the regions or take a
14591 * fatal error.
14592 **/
14593int
14594lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14595{
14596 struct lpfc_rpi_hdr *rpi_page;
14597 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014598 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014599
James Smart6d368e52011-05-24 11:44:12 -040014600 /* SLI4 ports that support extents do not require RPI headers. */
14601 if (!phba->sli4_hba.rpi_hdrs_in_use)
14602 goto exit;
14603 if (phba->sli4_hba.extents_in_use)
14604 return -EIO;
14605
James Smart6fb120a2009-05-22 14:52:59 -040014606 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014607 /*
14608 * Assign the rpi headers a physical rpi only if the driver
14609 * has not initialized those resources. A port reset only
14610 * needs the headers posted.
14611 */
14612 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14613 LPFC_RPI_RSRC_RDY)
14614 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14615
James Smart6fb120a2009-05-22 14:52:59 -040014616 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14617 if (rc != MBX_SUCCESS) {
14618 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14619 "2008 Error %d posting all rpi "
14620 "headers\n", rc);
14621 rc = -EIO;
14622 break;
14623 }
14624 }
14625
James Smart6d368e52011-05-24 11:44:12 -040014626 exit:
14627 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14628 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014629 return rc;
14630}
14631
14632/**
14633 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14634 * @phba: pointer to lpfc hba data structure.
14635 * @rpi_page: pointer to the rpi memory region.
14636 *
14637 * This routine is invoked to post a single rpi header to the
14638 * HBA consistent with the SLI-4 interface spec. This memory region
14639 * maps up to 64 rpi context regions.
14640 *
14641 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014642 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014643 * -ENOMEM - No available memory
14644 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014645 **/
14646int
14647lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14648{
14649 LPFC_MBOXQ_t *mboxq;
14650 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14651 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014652 uint32_t shdr_status, shdr_add_status;
14653 union lpfc_sli4_cfg_shdr *shdr;
14654
James Smart6d368e52011-05-24 11:44:12 -040014655 /* SLI4 ports that support extents do not require RPI headers. */
14656 if (!phba->sli4_hba.rpi_hdrs_in_use)
14657 return rc;
14658 if (phba->sli4_hba.extents_in_use)
14659 return -EIO;
14660
James Smart6fb120a2009-05-22 14:52:59 -040014661 /* The port is notified of the header region via a mailbox command. */
14662 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14663 if (!mboxq) {
14664 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14665 "2001 Unable to allocate memory for issuing "
14666 "SLI_CONFIG_SPECIAL mailbox command\n");
14667 return -ENOMEM;
14668 }
14669
14670 /* Post all rpi memory regions to the port. */
14671 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014672 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14673 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14674 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014675 sizeof(struct lpfc_sli4_cfg_mhdr),
14676 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014677
14678
14679 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014680 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14681 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014682 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14683 hdr_tmpl, rpi_page->page_count);
14684
James Smart6fb120a2009-05-22 14:52:59 -040014685 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14686 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014687 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014688 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14689 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14691 if (rc != MBX_TIMEOUT)
14692 mempool_free(mboxq, phba->mbox_mem_pool);
14693 if (shdr_status || shdr_add_status || rc) {
14694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14695 "2514 POST_RPI_HDR mailbox failed with "
14696 "status x%x add_status x%x, mbx status x%x\n",
14697 shdr_status, shdr_add_status, rc);
14698 rc = -ENXIO;
14699 }
14700 return rc;
14701}
14702
14703/**
14704 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14705 * @phba: pointer to lpfc hba data structure.
14706 *
14707 * This routine is invoked to post rpi header templates to the
14708 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014709 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14710 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014711 *
14712 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014713 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014714 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14715 **/
14716int
14717lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14718{
James Smart6d368e52011-05-24 11:44:12 -040014719 unsigned long rpi;
14720 uint16_t max_rpi, rpi_limit;
14721 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014722 struct lpfc_rpi_hdr *rpi_hdr;
14723
14724 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014725 rpi_limit = phba->sli4_hba.next_rpi;
14726
14727 /*
James Smart6d368e52011-05-24 11:44:12 -040014728 * Fetch the next logical rpi. Because this index is logical,
14729 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014730 */
14731 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014732 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14733 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014734 rpi = LPFC_RPI_ALLOC_ERROR;
14735 else {
14736 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14737 phba->sli4_hba.max_cfg_param.rpi_used++;
14738 phba->sli4_hba.rpi_count++;
14739 }
14740
14741 /*
14742 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014743 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014744 */
14745 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14746 (phba->sli4_hba.rpi_count >= max_rpi)) {
14747 spin_unlock_irq(&phba->hbalock);
14748 return rpi;
14749 }
14750
14751 /*
James Smart6d368e52011-05-24 11:44:12 -040014752 * RPI header postings are not required for SLI4 ports capable of
14753 * extents.
14754 */
14755 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14756 spin_unlock_irq(&phba->hbalock);
14757 return rpi;
14758 }
14759
14760 /*
James Smart6fb120a2009-05-22 14:52:59 -040014761 * If the driver is running low on rpi resources, allocate another
14762 * page now. Note that the next_rpi value is used because
14763 * it represents how many are actually in use whereas max_rpi notes
14764 * how many are supported max by the device.
14765 */
James Smart6d368e52011-05-24 11:44:12 -040014766 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014767 spin_unlock_irq(&phba->hbalock);
14768 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14769 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14770 if (!rpi_hdr) {
14771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14772 "2002 Error Could not grow rpi "
14773 "count\n");
14774 } else {
James Smart6d368e52011-05-24 11:44:12 -040014775 lrpi = rpi_hdr->start_rpi;
14776 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014777 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14778 }
14779 }
14780
14781 return rpi;
14782}
14783
14784/**
14785 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14786 * @phba: pointer to lpfc hba data structure.
14787 *
14788 * This routine is invoked to release an rpi to the pool of
14789 * available rpis maintained by the driver.
14790 **/
14791void
James Smartd7c47992010-06-08 18:31:54 -040014792__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14793{
14794 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14795 phba->sli4_hba.rpi_count--;
14796 phba->sli4_hba.max_cfg_param.rpi_used--;
14797 }
14798}
14799
14800/**
14801 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14802 * @phba: pointer to lpfc hba data structure.
14803 *
14804 * This routine is invoked to release an rpi to the pool of
14805 * available rpis maintained by the driver.
14806 **/
14807void
James Smart6fb120a2009-05-22 14:52:59 -040014808lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14809{
14810 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014811 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014812 spin_unlock_irq(&phba->hbalock);
14813}
14814
14815/**
14816 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14817 * @phba: pointer to lpfc hba data structure.
14818 *
14819 * This routine is invoked to remove the memory region that
14820 * provided rpi via a bitmask.
14821 **/
14822void
14823lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14824{
14825 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014826 kfree(phba->sli4_hba.rpi_ids);
14827 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014828}
14829
14830/**
14831 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14832 * @phba: pointer to lpfc hba data structure.
14833 *
14834 * This routine is invoked to remove the memory region that
14835 * provided rpi via a bitmask.
14836 **/
14837int
James Smart6b5151f2012-01-18 16:24:06 -050014838lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14839 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014840{
14841 LPFC_MBOXQ_t *mboxq;
14842 struct lpfc_hba *phba = ndlp->phba;
14843 int rc;
14844
14845 /* The port is notified of the header region via a mailbox command. */
14846 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14847 if (!mboxq)
14848 return -ENOMEM;
14849
14850 /* Post all rpi memory regions to the port. */
14851 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014852 if (cmpl) {
14853 mboxq->mbox_cmpl = cmpl;
14854 mboxq->context1 = arg;
14855 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014856 } else
14857 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014858 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014859 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14860 if (rc == MBX_NOT_FINISHED) {
14861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14862 "2010 Resume RPI Mailbox failed "
14863 "status %d, mbxStatus x%x\n", rc,
14864 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14865 mempool_free(mboxq, phba->mbox_mem_pool);
14866 return -EIO;
14867 }
14868 return 0;
14869}
14870
14871/**
14872 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014873 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014874 *
James Smart76a95d72010-11-20 23:11:48 -050014875 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014876 *
14877 * Returns:
14878 * 0 success
14879 * -Evalue otherwise
14880 **/
14881int
James Smart76a95d72010-11-20 23:11:48 -050014882lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014883{
14884 LPFC_MBOXQ_t *mboxq;
14885 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014886 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014887 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014888 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014889 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14890 if (!mboxq)
14891 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014892 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014893 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014894 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014895 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014896 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014897 "2022 INIT VPI Mailbox failed "
14898 "status %d, mbxStatus x%x\n", rc,
14899 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014900 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014901 }
James Smart6a9c52c2009-10-02 15:16:51 -040014902 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014903 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014904
14905 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014906}
14907
14908/**
14909 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14910 * @phba: pointer to lpfc hba data structure.
14911 * @mboxq: Pointer to mailbox object.
14912 *
14913 * This routine is invoked to manually add a single FCF record. The caller
14914 * must pass a completely initialized FCF_Record. This routine takes
14915 * care of the nonembedded mailbox operations.
14916 **/
14917static void
14918lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14919{
14920 void *virt_addr;
14921 union lpfc_sli4_cfg_shdr *shdr;
14922 uint32_t shdr_status, shdr_add_status;
14923
14924 virt_addr = mboxq->sge_array->addr[0];
14925 /* The IOCTL status is embedded in the mailbox subheader. */
14926 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14927 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14928 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14929
14930 if ((shdr_status || shdr_add_status) &&
14931 (shdr_status != STATUS_FCF_IN_USE))
14932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14933 "2558 ADD_FCF_RECORD mailbox failed with "
14934 "status x%x add_status x%x\n",
14935 shdr_status, shdr_add_status);
14936
14937 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14938}
14939
14940/**
14941 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14942 * @phba: pointer to lpfc hba data structure.
14943 * @fcf_record: pointer to the initialized fcf record to add.
14944 *
14945 * This routine is invoked to manually add a single FCF record. The caller
14946 * must pass a completely initialized FCF_Record. This routine takes
14947 * care of the nonembedded mailbox operations.
14948 **/
14949int
14950lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14951{
14952 int rc = 0;
14953 LPFC_MBOXQ_t *mboxq;
14954 uint8_t *bytep;
14955 void *virt_addr;
14956 dma_addr_t phys_addr;
14957 struct lpfc_mbx_sge sge;
14958 uint32_t alloc_len, req_len;
14959 uint32_t fcfindex;
14960
14961 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14962 if (!mboxq) {
14963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14964 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14965 return -ENOMEM;
14966 }
14967
14968 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14969 sizeof(uint32_t);
14970
14971 /* Allocate DMA memory and set up the non-embedded mailbox command */
14972 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14973 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14974 req_len, LPFC_SLI4_MBX_NEMBED);
14975 if (alloc_len < req_len) {
14976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14977 "2523 Allocated DMA memory size (x%x) is "
14978 "less than the requested DMA memory "
14979 "size (x%x)\n", alloc_len, req_len);
14980 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14981 return -ENOMEM;
14982 }
14983
14984 /*
14985 * Get the first SGE entry from the non-embedded DMA memory. This
14986 * routine only uses a single SGE.
14987 */
14988 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14989 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014990 virt_addr = mboxq->sge_array->addr[0];
14991 /*
14992 * Configure the FCF record for FCFI 0. This is the driver's
14993 * hardcoded default and gets used in nonFIP mode.
14994 */
14995 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14996 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14997 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14998
14999 /*
15000 * Copy the fcf_index and the FCF Record Data. The data starts after
15001 * the FCoE header plus word10. The data copy needs to be endian
15002 * correct.
15003 */
15004 bytep += sizeof(uint32_t);
15005 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15006 mboxq->vport = phba->pport;
15007 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15008 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15009 if (rc == MBX_NOT_FINISHED) {
15010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15011 "2515 ADD_FCF_RECORD mailbox failed with "
15012 "status 0x%x\n", rc);
15013 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15014 rc = -EIO;
15015 } else
15016 rc = 0;
15017
15018 return rc;
15019}
15020
15021/**
15022 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15023 * @phba: pointer to lpfc hba data structure.
15024 * @fcf_record: pointer to the fcf record to write the default data.
15025 * @fcf_index: FCF table entry index.
15026 *
15027 * This routine is invoked to build the driver's default FCF record. The
15028 * values used are hardcoded. This routine handles memory initialization.
15029 *
15030 **/
15031void
15032lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15033 struct fcf_record *fcf_record,
15034 uint16_t fcf_index)
15035{
15036 memset(fcf_record, 0, sizeof(struct fcf_record));
15037 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15038 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15039 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15040 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15041 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15042 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15043 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15044 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15045 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15046 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15047 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15048 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15049 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015050 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015051 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15052 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15053 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15054 /* Set the VLAN bit map */
15055 if (phba->valid_vlan) {
15056 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15057 = 1 << (phba->vlan_id % 8);
15058 }
15059}
15060
15061/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015062 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015063 * @phba: pointer to lpfc hba data structure.
15064 * @fcf_index: FCF table entry offset.
15065 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015066 * This routine is invoked to scan the entire FCF table by reading FCF
15067 * record and processing it one at a time starting from the @fcf_index
15068 * for initial FCF discovery or fast FCF failover rediscovery.
15069 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015070 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015071 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015072 **/
15073int
James Smart0c9ab6f2010-02-26 14:15:57 -050015074lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015075{
15076 int rc = 0, error;
15077 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015078
James Smart32b97932009-07-19 10:01:21 -040015079 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015080 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015081 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15082 if (!mboxq) {
15083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15084 "2000 Failed to allocate mbox for "
15085 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015086 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015087 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015088 }
James Smartecfd03c2010-02-12 14:41:27 -050015089 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015090 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015091 if (rc) {
15092 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015093 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015094 }
James Smartecfd03c2010-02-12 14:41:27 -050015095 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015096 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015097 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015098
15099 spin_lock_irq(&phba->hbalock);
15100 phba->hba_flag |= FCF_TS_INPROG;
15101 spin_unlock_irq(&phba->hbalock);
15102
James Smart6fb120a2009-05-22 14:52:59 -040015103 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015104 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015105 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015106 else {
James Smart38b92ef2010-08-04 16:11:39 -040015107 /* Reset eligible FCF count for new scan */
15108 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015109 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015110 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015111 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015112fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015113 if (error) {
15114 if (mboxq)
15115 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015116 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015117 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015118 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015119 spin_unlock_irq(&phba->hbalock);
15120 }
James Smart6fb120a2009-05-22 14:52:59 -040015121 return error;
15122}
James Smarta0c87cb2009-07-19 10:01:10 -040015123
15124/**
James Smarta93ff372010-10-22 11:06:08 -040015125 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015126 * @phba: pointer to lpfc hba data structure.
15127 * @fcf_index: FCF table entry offset.
15128 *
15129 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015130 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015131 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015132 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015133 * otherwise.
15134 **/
15135int
15136lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15137{
15138 int rc = 0, error;
15139 LPFC_MBOXQ_t *mboxq;
15140
15141 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15142 if (!mboxq) {
15143 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15144 "2763 Failed to allocate mbox for "
15145 "READ_FCF cmd\n");
15146 error = -ENOMEM;
15147 goto fail_fcf_read;
15148 }
15149 /* Construct the read FCF record mailbox command */
15150 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15151 if (rc) {
15152 error = -EINVAL;
15153 goto fail_fcf_read;
15154 }
15155 /* Issue the mailbox command asynchronously */
15156 mboxq->vport = phba->pport;
15157 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15158 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15159 if (rc == MBX_NOT_FINISHED)
15160 error = -EIO;
15161 else
15162 error = 0;
15163
15164fail_fcf_read:
15165 if (error && mboxq)
15166 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15167 return error;
15168}
15169
15170/**
15171 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15172 * @phba: pointer to lpfc hba data structure.
15173 * @fcf_index: FCF table entry offset.
15174 *
15175 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015176 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015177 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015178 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015179 * otherwise.
15180 **/
15181int
15182lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15183{
15184 int rc = 0, error;
15185 LPFC_MBOXQ_t *mboxq;
15186
15187 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15188 if (!mboxq) {
15189 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15190 "2758 Failed to allocate mbox for "
15191 "READ_FCF cmd\n");
15192 error = -ENOMEM;
15193 goto fail_fcf_read;
15194 }
15195 /* Construct the read FCF record mailbox command */
15196 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15197 if (rc) {
15198 error = -EINVAL;
15199 goto fail_fcf_read;
15200 }
15201 /* Issue the mailbox command asynchronously */
15202 mboxq->vport = phba->pport;
15203 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15204 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15205 if (rc == MBX_NOT_FINISHED)
15206 error = -EIO;
15207 else
15208 error = 0;
15209
15210fail_fcf_read:
15211 if (error && mboxq)
15212 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15213 return error;
15214}
15215
15216/**
James Smart7d791df2011-07-22 18:37:52 -040015217 * lpfc_check_next_fcf_pri
15218 * phba pointer to the lpfc_hba struct for this port.
15219 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15220 * routine when the rr_bmask is empty. The FCF indecies are put into the
15221 * rr_bmask based on their priority level. Starting from the highest priority
15222 * to the lowest. The most likely FCF candidate will be in the highest
15223 * priority group. When this routine is called it searches the fcf_pri list for
15224 * next lowest priority group and repopulates the rr_bmask with only those
15225 * fcf_indexes.
15226 * returns:
15227 * 1=success 0=failure
15228 **/
15229int
15230lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15231{
15232 uint16_t next_fcf_pri;
15233 uint16_t last_index;
15234 struct lpfc_fcf_pri *fcf_pri;
15235 int rc;
15236 int ret = 0;
15237
15238 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15239 LPFC_SLI4_FCF_TBL_INDX_MAX);
15240 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15241 "3060 Last IDX %d\n", last_index);
15242 if (list_empty(&phba->fcf.fcf_pri_list)) {
15243 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15244 "3061 Last IDX %d\n", last_index);
15245 return 0; /* Empty rr list */
15246 }
15247 next_fcf_pri = 0;
15248 /*
15249 * Clear the rr_bmask and set all of the bits that are at this
15250 * priority.
15251 */
15252 memset(phba->fcf.fcf_rr_bmask, 0,
15253 sizeof(*phba->fcf.fcf_rr_bmask));
15254 spin_lock_irq(&phba->hbalock);
15255 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15256 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15257 continue;
15258 /*
15259 * the 1st priority that has not FLOGI failed
15260 * will be the highest.
15261 */
15262 if (!next_fcf_pri)
15263 next_fcf_pri = fcf_pri->fcf_rec.priority;
15264 spin_unlock_irq(&phba->hbalock);
15265 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15266 rc = lpfc_sli4_fcf_rr_index_set(phba,
15267 fcf_pri->fcf_rec.fcf_index);
15268 if (rc)
15269 return 0;
15270 }
15271 spin_lock_irq(&phba->hbalock);
15272 }
15273 /*
15274 * if next_fcf_pri was not set above and the list is not empty then
15275 * we have failed flogis on all of them. So reset flogi failed
15276 * and start at the begining.
15277 */
15278 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15279 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15280 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15281 /*
15282 * the 1st priority that has not FLOGI failed
15283 * will be the highest.
15284 */
15285 if (!next_fcf_pri)
15286 next_fcf_pri = fcf_pri->fcf_rec.priority;
15287 spin_unlock_irq(&phba->hbalock);
15288 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15289 rc = lpfc_sli4_fcf_rr_index_set(phba,
15290 fcf_pri->fcf_rec.fcf_index);
15291 if (rc)
15292 return 0;
15293 }
15294 spin_lock_irq(&phba->hbalock);
15295 }
15296 } else
15297 ret = 1;
15298 spin_unlock_irq(&phba->hbalock);
15299
15300 return ret;
15301}
15302/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015303 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15304 * @phba: pointer to lpfc hba data structure.
15305 *
15306 * This routine is to get the next eligible FCF record index in a round
15307 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015308 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015309 * shall be returned, otherwise, the next eligible FCF record's index
15310 * shall be returned.
15311 **/
15312uint16_t
15313lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15314{
15315 uint16_t next_fcf_index;
15316
James Smart3804dc82010-07-14 15:31:37 -040015317 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015318next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015319 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15320 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015321 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15322 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015323 next_fcf_index);
15324
James Smart0c9ab6f2010-02-26 14:15:57 -050015325 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015326 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15327 /*
15328 * If we have wrapped then we need to clear the bits that
15329 * have been tested so that we can detect when we should
15330 * change the priority level.
15331 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015332 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15333 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015334 }
15335
James Smart0c9ab6f2010-02-26 14:15:57 -050015336
James Smart3804dc82010-07-14 15:31:37 -040015337 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015338 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15339 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15340 /*
15341 * If next fcf index is not found check if there are lower
15342 * Priority level fcf's in the fcf_priority list.
15343 * Set up the rr_bmask with all of the avaiable fcf bits
15344 * at that level and continue the selection process.
15345 */
15346 if (lpfc_check_next_fcf_pri_level(phba))
15347 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015348 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15349 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015350 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15351 return LPFC_FCOE_FCF_NEXT_NONE;
15352 else {
15353 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15354 "3063 Only FCF available idx %d, flag %x\n",
15355 next_fcf_index,
15356 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15357 return next_fcf_index;
15358 }
James Smart3804dc82010-07-14 15:31:37 -040015359 }
15360
James Smart7d791df2011-07-22 18:37:52 -040015361 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15362 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15363 LPFC_FCF_FLOGI_FAILED)
15364 goto next_priority;
15365
James Smart3804dc82010-07-14 15:31:37 -040015366 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015367 "2845 Get next roundrobin failover FCF (x%x)\n",
15368 next_fcf_index);
15369
James Smart0c9ab6f2010-02-26 14:15:57 -050015370 return next_fcf_index;
15371}
15372
15373/**
15374 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15375 * @phba: pointer to lpfc hba data structure.
15376 *
15377 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015378 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015379 * does not go beyond the range of the driver allocated bmask dimension
15380 * before setting the bit.
15381 *
15382 * Returns 0 if the index bit successfully set, otherwise, it returns
15383 * -EINVAL.
15384 **/
15385int
15386lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15387{
15388 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15389 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015390 "2610 FCF (x%x) reached driver's book "
15391 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015392 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15393 return -EINVAL;
15394 }
15395 /* Set the eligible FCF record index bmask */
15396 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15397
James Smart3804dc82010-07-14 15:31:37 -040015398 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015399 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015400 "bmask\n", fcf_index);
15401
James Smart0c9ab6f2010-02-26 14:15:57 -050015402 return 0;
15403}
15404
15405/**
James Smart3804dc82010-07-14 15:31:37 -040015406 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015407 * @phba: pointer to lpfc hba data structure.
15408 *
15409 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015410 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015411 * does not go beyond the range of the driver allocated bmask dimension
15412 * before clearing the bit.
15413 **/
15414void
15415lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15416{
James Smart7d791df2011-07-22 18:37:52 -040015417 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015418 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15419 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015420 "2762 FCF (x%x) reached driver's book "
15421 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015422 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15423 return;
15424 }
15425 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015426 spin_lock_irq(&phba->hbalock);
15427 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15428 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15429 list_del_init(&fcf_pri->list);
15430 break;
15431 }
15432 }
15433 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015434 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015435
15436 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015437 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015438 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015439}
15440
15441/**
James Smartecfd03c2010-02-12 14:41:27 -050015442 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15443 * @phba: pointer to lpfc hba data structure.
15444 *
15445 * This routine is the completion routine for the rediscover FCF table mailbox
15446 * command. If the mailbox command returned failure, it will try to stop the
15447 * FCF rediscover wait timer.
15448 **/
15449void
15450lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15451{
15452 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15453 uint32_t shdr_status, shdr_add_status;
15454
15455 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15456
15457 shdr_status = bf_get(lpfc_mbox_hdr_status,
15458 &redisc_fcf->header.cfg_shdr.response);
15459 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15460 &redisc_fcf->header.cfg_shdr.response);
15461 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015462 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015463 "2746 Requesting for FCF rediscovery failed "
15464 "status x%x add_status x%x\n",
15465 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015466 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015467 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015468 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015469 spin_unlock_irq(&phba->hbalock);
15470 /*
15471 * CVL event triggered FCF rediscover request failed,
15472 * last resort to re-try current registered FCF entry.
15473 */
15474 lpfc_retry_pport_discovery(phba);
15475 } else {
15476 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015477 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015478 spin_unlock_irq(&phba->hbalock);
15479 /*
15480 * DEAD FCF event triggered FCF rediscover request
15481 * failed, last resort to fail over as a link down
15482 * to FCF registration.
15483 */
15484 lpfc_sli4_fcf_dead_failthrough(phba);
15485 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015486 } else {
15487 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015488 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015489 /*
15490 * Start FCF rediscovery wait timer for pending FCF
15491 * before rescan FCF record table.
15492 */
15493 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015494 }
James Smartecfd03c2010-02-12 14:41:27 -050015495
15496 mempool_free(mbox, phba->mbox_mem_pool);
15497}
15498
15499/**
James Smart3804dc82010-07-14 15:31:37 -040015500 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015501 * @phba: pointer to lpfc hba data structure.
15502 *
15503 * This routine is invoked to request for rediscovery of the entire FCF table
15504 * by the port.
15505 **/
15506int
15507lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15508{
15509 LPFC_MBOXQ_t *mbox;
15510 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15511 int rc, length;
15512
James Smart0c9ab6f2010-02-26 14:15:57 -050015513 /* Cancel retry delay timers to all vports before FCF rediscover */
15514 lpfc_cancel_all_vport_retry_delay_timer(phba);
15515
James Smartecfd03c2010-02-12 14:41:27 -050015516 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15517 if (!mbox) {
15518 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15519 "2745 Failed to allocate mbox for "
15520 "requesting FCF rediscover.\n");
15521 return -ENOMEM;
15522 }
15523
15524 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15525 sizeof(struct lpfc_sli4_cfg_mhdr));
15526 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15527 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15528 length, LPFC_SLI4_MBX_EMBED);
15529
15530 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15531 /* Set count to 0 for invalidating the entire FCF database */
15532 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15533
15534 /* Issue the mailbox command asynchronously */
15535 mbox->vport = phba->pport;
15536 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15537 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15538
15539 if (rc == MBX_NOT_FINISHED) {
15540 mempool_free(mbox, phba->mbox_mem_pool);
15541 return -EIO;
15542 }
15543 return 0;
15544}
15545
15546/**
James Smartfc2b9892010-02-26 14:15:29 -050015547 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15548 * @phba: pointer to lpfc hba data structure.
15549 *
15550 * This function is the failover routine as a last resort to the FCF DEAD
15551 * event when driver failed to perform fast FCF failover.
15552 **/
15553void
15554lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15555{
15556 uint32_t link_state;
15557
15558 /*
15559 * Last resort as FCF DEAD event failover will treat this as
15560 * a link down, but save the link state because we don't want
15561 * it to be changed to Link Down unless it is already down.
15562 */
15563 link_state = phba->link_state;
15564 lpfc_linkdown(phba);
15565 phba->link_state = link_state;
15566
15567 /* Unregister FCF if no devices connected to it */
15568 lpfc_unregister_unused_fcf(phba);
15569}
15570
15571/**
James Smart026abb82011-12-13 13:20:45 -050015572 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015573 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015574 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015575 *
James Smart026abb82011-12-13 13:20:45 -050015576 * This function gets SLI3 port configure region 23 data through memory dump
15577 * mailbox command. When it successfully retrieves data, the size of the data
15578 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015579 **/
James Smart026abb82011-12-13 13:20:45 -050015580static uint32_t
15581lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015582{
15583 LPFC_MBOXQ_t *pmb = NULL;
15584 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015585 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015586 int rc;
15587
James Smart026abb82011-12-13 13:20:45 -050015588 if (!rgn23_data)
15589 return 0;
15590
James Smarta0c87cb2009-07-19 10:01:10 -040015591 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15592 if (!pmb) {
15593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015594 "2600 failed to allocate mailbox memory\n");
15595 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015596 }
15597 mb = &pmb->u.mb;
15598
James Smarta0c87cb2009-07-19 10:01:10 -040015599 do {
15600 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15601 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15602
15603 if (rc != MBX_SUCCESS) {
15604 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015605 "2601 failed to read config "
15606 "region 23, rc 0x%x Status 0x%x\n",
15607 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015608 mb->un.varDmp.word_cnt = 0;
15609 }
15610 /*
15611 * dump mem may return a zero when finished or we got a
15612 * mailbox error, either way we are done.
15613 */
15614 if (mb->un.varDmp.word_cnt == 0)
15615 break;
15616 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15617 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15618
15619 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015620 rgn23_data + offset,
15621 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015622 offset += mb->un.varDmp.word_cnt;
15623 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15624
James Smart026abb82011-12-13 13:20:45 -050015625 mempool_free(pmb, phba->mbox_mem_pool);
15626 return offset;
15627}
15628
15629/**
15630 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15631 * @phba: pointer to lpfc hba data structure.
15632 * @rgn23_data: pointer to configure region 23 data.
15633 *
15634 * This function gets SLI4 port configure region 23 data through memory dump
15635 * mailbox command. When it successfully retrieves data, the size of the data
15636 * will be returned, otherwise, 0 will be returned.
15637 **/
15638static uint32_t
15639lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15640{
15641 LPFC_MBOXQ_t *mboxq = NULL;
15642 struct lpfc_dmabuf *mp = NULL;
15643 struct lpfc_mqe *mqe;
15644 uint32_t data_length = 0;
15645 int rc;
15646
15647 if (!rgn23_data)
15648 return 0;
15649
15650 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15651 if (!mboxq) {
15652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15653 "3105 failed to allocate mailbox memory\n");
15654 return 0;
15655 }
15656
15657 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15658 goto out;
15659 mqe = &mboxq->u.mqe;
15660 mp = (struct lpfc_dmabuf *) mboxq->context1;
15661 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15662 if (rc)
15663 goto out;
15664 data_length = mqe->un.mb_words[5];
15665 if (data_length == 0)
15666 goto out;
15667 if (data_length > DMP_RGN23_SIZE) {
15668 data_length = 0;
15669 goto out;
15670 }
15671 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15672out:
15673 mempool_free(mboxq, phba->mbox_mem_pool);
15674 if (mp) {
15675 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15676 kfree(mp);
15677 }
15678 return data_length;
15679}
15680
15681/**
15682 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15683 * @phba: pointer to lpfc hba data structure.
15684 *
15685 * This function read region 23 and parse TLV for port status to
15686 * decide if the user disaled the port. If the TLV indicates the
15687 * port is disabled, the hba_flag is set accordingly.
15688 **/
15689void
15690lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15691{
15692 uint8_t *rgn23_data = NULL;
15693 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15694 uint32_t offset = 0;
15695
15696 /* Get adapter Region 23 data */
15697 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15698 if (!rgn23_data)
15699 goto out;
15700
15701 if (phba->sli_rev < LPFC_SLI_REV4)
15702 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15703 else {
15704 if_type = bf_get(lpfc_sli_intf_if_type,
15705 &phba->sli4_hba.sli_intf);
15706 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15707 goto out;
15708 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15709 }
James Smarta0c87cb2009-07-19 10:01:10 -040015710
15711 if (!data_size)
15712 goto out;
15713
15714 /* Check the region signature first */
15715 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15717 "2619 Config region 23 has bad signature\n");
15718 goto out;
15719 }
15720 offset += 4;
15721
15722 /* Check the data structure version */
15723 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15724 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15725 "2620 Config region 23 has bad version\n");
15726 goto out;
15727 }
15728 offset += 4;
15729
15730 /* Parse TLV entries in the region */
15731 while (offset < data_size) {
15732 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15733 break;
15734 /*
15735 * If the TLV is not driver specific TLV or driver id is
15736 * not linux driver id, skip the record.
15737 */
15738 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15739 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15740 (rgn23_data[offset + 3] != 0)) {
15741 offset += rgn23_data[offset + 1] * 4 + 4;
15742 continue;
15743 }
15744
15745 /* Driver found a driver specific TLV in the config region */
15746 sub_tlv_len = rgn23_data[offset + 1] * 4;
15747 offset += 4;
15748 tlv_offset = 0;
15749
15750 /*
15751 * Search for configured port state sub-TLV.
15752 */
15753 while ((offset < data_size) &&
15754 (tlv_offset < sub_tlv_len)) {
15755 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15756 offset += 4;
15757 tlv_offset += 4;
15758 break;
15759 }
15760 if (rgn23_data[offset] != PORT_STE_TYPE) {
15761 offset += rgn23_data[offset + 1] * 4 + 4;
15762 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15763 continue;
15764 }
15765
15766 /* This HBA contains PORT_STE configured */
15767 if (!rgn23_data[offset + 2])
15768 phba->hba_flag |= LINK_DISABLED;
15769
15770 goto out;
15771 }
15772 }
James Smart026abb82011-12-13 13:20:45 -050015773
James Smarta0c87cb2009-07-19 10:01:10 -040015774out:
James Smarta0c87cb2009-07-19 10:01:10 -040015775 kfree(rgn23_data);
15776 return;
15777}
James Smart695a8142010-01-26 23:08:03 -050015778
15779/**
James Smart52d52442011-05-24 11:42:45 -040015780 * lpfc_wr_object - write an object to the firmware
15781 * @phba: HBA structure that indicates port to create a queue on.
15782 * @dmabuf_list: list of dmabufs to write to the port.
15783 * @size: the total byte value of the objects to write to the port.
15784 * @offset: the current offset to be used to start the transfer.
15785 *
15786 * This routine will create a wr_object mailbox command to send to the port.
15787 * the mailbox command will be constructed using the dma buffers described in
15788 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15789 * BDEs that the imbedded mailbox can support. The @offset variable will be
15790 * used to indicate the starting offset of the transfer and will also return
15791 * the offset after the write object mailbox has completed. @size is used to
15792 * determine the end of the object and whether the eof bit should be set.
15793 *
15794 * Return 0 is successful and offset will contain the the new offset to use
15795 * for the next write.
15796 * Return negative value for error cases.
15797 **/
15798int
15799lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15800 uint32_t size, uint32_t *offset)
15801{
15802 struct lpfc_mbx_wr_object *wr_object;
15803 LPFC_MBOXQ_t *mbox;
15804 int rc = 0, i = 0;
15805 uint32_t shdr_status, shdr_add_status;
15806 uint32_t mbox_tmo;
15807 union lpfc_sli4_cfg_shdr *shdr;
15808 struct lpfc_dmabuf *dmabuf;
15809 uint32_t written = 0;
15810
15811 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15812 if (!mbox)
15813 return -ENOMEM;
15814
15815 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15816 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15817 sizeof(struct lpfc_mbx_wr_object) -
15818 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15819
15820 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15821 wr_object->u.request.write_offset = *offset;
15822 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15823 wr_object->u.request.object_name[0] =
15824 cpu_to_le32(wr_object->u.request.object_name[0]);
15825 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15826 list_for_each_entry(dmabuf, dmabuf_list, list) {
15827 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15828 break;
15829 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15830 wr_object->u.request.bde[i].addrHigh =
15831 putPaddrHigh(dmabuf->phys);
15832 if (written + SLI4_PAGE_SIZE >= size) {
15833 wr_object->u.request.bde[i].tus.f.bdeSize =
15834 (size - written);
15835 written += (size - written);
15836 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15837 } else {
15838 wr_object->u.request.bde[i].tus.f.bdeSize =
15839 SLI4_PAGE_SIZE;
15840 written += SLI4_PAGE_SIZE;
15841 }
15842 i++;
15843 }
15844 wr_object->u.request.bde_count = i;
15845 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15846 if (!phba->sli4_hba.intr_enable)
15847 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15848 else {
James Smarta183a152011-10-10 21:32:43 -040015849 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015850 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15851 }
15852 /* The IOCTL status is embedded in the mailbox subheader. */
15853 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15854 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15855 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15856 if (rc != MBX_TIMEOUT)
15857 mempool_free(mbox, phba->mbox_mem_pool);
15858 if (shdr_status || shdr_add_status || rc) {
15859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15860 "3025 Write Object mailbox failed with "
15861 "status x%x add_status x%x, mbx status x%x\n",
15862 shdr_status, shdr_add_status, rc);
15863 rc = -ENXIO;
15864 } else
15865 *offset += wr_object->u.response.actual_write_length;
15866 return rc;
15867}
15868
15869/**
James Smart695a8142010-01-26 23:08:03 -050015870 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15871 * @vport: pointer to vport data structure.
15872 *
15873 * This function iterate through the mailboxq and clean up all REG_LOGIN
15874 * and REG_VPI mailbox commands associated with the vport. This function
15875 * is called when driver want to restart discovery of the vport due to
15876 * a Clear Virtual Link event.
15877 **/
15878void
15879lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15880{
15881 struct lpfc_hba *phba = vport->phba;
15882 LPFC_MBOXQ_t *mb, *nextmb;
15883 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015884 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015885 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015886 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015887 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015888 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015889
James Smartd439d282010-09-29 11:18:45 -040015890 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015891 spin_lock_irq(&phba->hbalock);
15892 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15893 if (mb->vport != vport)
15894 continue;
15895
15896 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15897 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15898 continue;
15899
James Smartd439d282010-09-29 11:18:45 -040015900 list_del(&mb->list);
15901 list_add_tail(&mb->list, &mbox_cmd_list);
15902 }
15903 /* Clean up active mailbox command with the vport */
15904 mb = phba->sli.mbox_active;
15905 if (mb && (mb->vport == vport)) {
15906 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15907 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15908 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15909 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15910 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15911 /* Put reference count for delayed processing */
15912 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15913 /* Unregister the RPI when mailbox complete */
15914 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15915 }
15916 }
James Smart63e801c2010-11-20 23:14:19 -050015917 /* Cleanup any mailbox completions which are not yet processed */
15918 do {
15919 restart_loop = 0;
15920 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15921 /*
15922 * If this mailox is already processed or it is
15923 * for another vport ignore it.
15924 */
15925 if ((mb->vport != vport) ||
15926 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15927 continue;
15928
15929 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15930 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15931 continue;
15932
15933 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15934 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15935 ndlp = (struct lpfc_nodelist *)mb->context2;
15936 /* Unregister the RPI when mailbox complete */
15937 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15938 restart_loop = 1;
15939 spin_unlock_irq(&phba->hbalock);
15940 spin_lock(shost->host_lock);
15941 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15942 spin_unlock(shost->host_lock);
15943 spin_lock_irq(&phba->hbalock);
15944 break;
15945 }
15946 }
15947 } while (restart_loop);
15948
James Smartd439d282010-09-29 11:18:45 -040015949 spin_unlock_irq(&phba->hbalock);
15950
15951 /* Release the cleaned-up mailbox commands */
15952 while (!list_empty(&mbox_cmd_list)) {
15953 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015954 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15955 mp = (struct lpfc_dmabuf *) (mb->context1);
15956 if (mp) {
15957 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15958 kfree(mp);
15959 }
James Smart78730cf2010-04-06 15:06:30 -040015960 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015961 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015962 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015963 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015964 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015965 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015966 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015967 }
James Smart695a8142010-01-26 23:08:03 -050015968 }
James Smart695a8142010-01-26 23:08:03 -050015969 mempool_free(mb, phba->mbox_mem_pool);
15970 }
James Smartd439d282010-09-29 11:18:45 -040015971
15972 /* Release the ndlp with the cleaned-up active mailbox command */
15973 if (act_mbx_ndlp) {
15974 spin_lock(shost->host_lock);
15975 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15976 spin_unlock(shost->host_lock);
15977 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015978 }
James Smart695a8142010-01-26 23:08:03 -050015979}
15980
James Smart2a9bf3d2010-06-07 15:24:45 -040015981/**
15982 * lpfc_drain_txq - Drain the txq
15983 * @phba: Pointer to HBA context object.
15984 *
15985 * This function attempt to submit IOCBs on the txq
15986 * to the adapter. For SLI4 adapters, the txq contains
15987 * ELS IOCBs that have been deferred because the there
15988 * are no SGLs. This congestion can occur with large
15989 * vport counts during node discovery.
15990 **/
15991
15992uint32_t
15993lpfc_drain_txq(struct lpfc_hba *phba)
15994{
15995 LIST_HEAD(completions);
15996 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15997 struct lpfc_iocbq *piocbq = 0;
15998 unsigned long iflags = 0;
15999 char *fail_msg = NULL;
16000 struct lpfc_sglq *sglq;
16001 union lpfc_wqe wqe;
16002
16003 spin_lock_irqsave(&phba->hbalock, iflags);
16004 if (pring->txq_cnt > pring->txq_max)
16005 pring->txq_max = pring->txq_cnt;
16006
16007 spin_unlock_irqrestore(&phba->hbalock, iflags);
16008
16009 while (pring->txq_cnt) {
16010 spin_lock_irqsave(&phba->hbalock, iflags);
16011
James Smart19ca7602010-11-20 23:11:55 -050016012 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016013 if (!piocbq) {
16014 spin_unlock_irqrestore(&phba->hbalock, iflags);
16015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16016 "2823 txq empty and txq_cnt is %d\n ",
16017 pring->txq_cnt);
16018 break;
16019 }
James Smart19ca7602010-11-20 23:11:55 -050016020 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016021 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016022 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016023 spin_unlock_irqrestore(&phba->hbalock, iflags);
16024 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016025 }
16026
16027 /* The xri and iocb resources secured,
16028 * attempt to issue request
16029 */
James Smart6d368e52011-05-24 11:44:12 -040016030 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016031 piocbq->sli4_xritag = sglq->sli4_xritag;
16032 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16033 fail_msg = "to convert bpl to sgl";
16034 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16035 fail_msg = "to convert iocb to wqe";
16036 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16037 fail_msg = " - Wq is full";
16038 else
16039 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16040
16041 if (fail_msg) {
16042 /* Failed means we can't issue and need to cancel */
16043 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16044 "2822 IOCB failed %s iotag 0x%x "
16045 "xri 0x%x\n",
16046 fail_msg,
16047 piocbq->iotag, piocbq->sli4_xritag);
16048 list_add_tail(&piocbq->list, &completions);
16049 }
16050 spin_unlock_irqrestore(&phba->hbalock, iflags);
16051 }
16052
James Smart2a9bf3d2010-06-07 15:24:45 -040016053 /* Cancel all the IOCBs that cannot be issued */
16054 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16055 IOERR_SLI_ABORTED);
16056
16057 return pring->txq_cnt;
16058}