blob: 83a1c9e4902d198769a7b9c9245dfd1713d38e12 [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;
James Smartaa6fbb72012-08-03 12:36:03 -04002828 uint32_t eratt = 0, rem;
2829 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002830
2831 phba = (struct lpfc_hba *)ptr;
2832
James Smartaa6fbb72012-08-03 12:36:03 -04002833 /* Here we will also keep track of interrupts per sec of the hba */
2834 sli_intr = phba->sli.slistat.sli_intr;
2835
2836 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2837 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2838 sli_intr);
2839 else
2840 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2841
2842 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2843 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2844 phba->sli.slistat.sli_ips = cnt;
2845
2846 phba->sli.slistat.sli_prev_intr = sli_intr;
2847
James Smart93996272008-08-24 21:50:30 -04002848 /* Check chip HA register for error event */
2849 eratt = lpfc_sli_check_eratt(phba);
2850
2851 if (eratt)
2852 /* Tell the worker thread there is work to do */
2853 lpfc_worker_wake_up(phba);
2854 else
2855 /* Restart the timer for next eratt poll */
2856 mod_timer(&phba->eratt_poll, jiffies +
2857 HZ * LPFC_ERATT_POLL_INTERVAL);
2858 return;
2859}
2860
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002861
James Smarte59058c2008-08-24 21:49:00 -04002862/**
James Smart3621a712009-04-06 18:47:14 -04002863 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002864 * @phba: Pointer to HBA context object.
2865 * @pring: Pointer to driver SLI ring object.
2866 * @mask: Host attention register mask for this ring.
2867 *
2868 * This function is called from the interrupt context when there is a ring
2869 * event for the fcp ring. The caller does not hold any lock.
2870 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002871 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002872 * LE bit set. The function will call the completion handler of the command iocb
2873 * if the response iocb indicates a completion for a command iocb or it is
2874 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2875 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002876 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002877 * to check it explicitly.
2878 */
2879int
James Smart2e0fef82007-06-17 19:56:36 -05002880lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2881 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002882{
James Smart34b02dc2008-08-24 21:49:55 -04002883 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002884 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002885 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002886 struct lpfc_iocbq *cmdiocbq = NULL;
2887 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002888 uint32_t status;
2889 uint32_t portRspPut, portRspMax;
2890 int rc = 1;
2891 lpfc_iocb_type type;
2892 unsigned long iflag;
2893 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002894
James Smart2e0fef82007-06-17 19:56:36 -05002895 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002896 pring->stats.iocb_event++;
2897
dea31012005-04-17 16:05:31 -05002898 /*
2899 * The next available response entry should never exceed the maximum
2900 * entries. If it does, treat it as an adapter hardware error.
2901 */
James Smart7e56aa22012-08-03 12:35:34 -04002902 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002903 portRspPut = le32_to_cpu(pgp->rspPutInx);
2904 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002905 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002906 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002907 return 1;
2908 }
James Smart45ed1192009-10-02 15:17:02 -04002909 if (phba->fcp_ring_in_use) {
2910 spin_unlock_irqrestore(&phba->hbalock, iflag);
2911 return 1;
2912 } else
2913 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002914
2915 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002916 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002917 /*
2918 * Fetch an entry off the ring and copy it into a local data
2919 * structure. The copy involves a byte-swap since the
2920 * network byte order and pci byte orders are different.
2921 */
James Smarted957682007-06-17 19:56:37 -05002922 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002923 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002924
James Smart7e56aa22012-08-03 12:35:34 -04002925 if (++pring->sli.sli3.rspidx >= portRspMax)
2926 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002927
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002928 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2929 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002930 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002931 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002932 irsp = &rspiocbq.iocb;
2933
dea31012005-04-17 16:05:31 -05002934 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2935 pring->stats.iocb_rsp++;
2936 rsp_cmpl++;
2937
2938 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002939 /*
2940 * If resource errors reported from HBA, reduce
2941 * queuedepths of the SCSI device.
2942 */
2943 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2944 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2945 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002946 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002947 spin_lock_irqsave(&phba->hbalock, iflag);
2948 }
2949
dea31012005-04-17 16:05:31 -05002950 /* Rsp ring <ringno> error: IOCB */
2951 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002952 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002953 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002954 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002955 irsp->un.ulpWord[0],
2956 irsp->un.ulpWord[1],
2957 irsp->un.ulpWord[2],
2958 irsp->un.ulpWord[3],
2959 irsp->un.ulpWord[4],
2960 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002961 *(uint32_t *)&irsp->un1,
2962 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002963 }
2964
2965 switch (type) {
2966 case LPFC_ABORT_IOCB:
2967 case LPFC_SOL_IOCB:
2968 /*
2969 * Idle exchange closed via ABTS from port. No iocb
2970 * resources need to be recovered.
2971 */
2972 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002973 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002974 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002975 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002976 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002977 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002978 break;
2979 }
2980
James Bottomley604a3e32005-10-29 10:28:33 -05002981 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2982 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002983 if (unlikely(!cmdiocbq))
2984 break;
2985 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2986 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2987 if (cmdiocbq->iocb_cmpl) {
2988 spin_unlock_irqrestore(&phba->hbalock, iflag);
2989 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2990 &rspiocbq);
2991 spin_lock_irqsave(&phba->hbalock, iflag);
2992 }
dea31012005-04-17 16:05:31 -05002993 break;
James Smarta4bc3372006-12-02 13:34:16 -05002994 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002995 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002996 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002997 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002998 break;
dea31012005-04-17 16:05:31 -05002999 default:
3000 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3001 char adaptermsg[LPFC_MAX_ADPTMSG];
3002 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3003 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3004 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003005 dev_warn(&((phba->pcidev)->dev),
3006 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003007 phba->brd_no, adaptermsg);
3008 } else {
3009 /* Unknown IOCB command */
3010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003011 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003012 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003013 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003014 irsp->ulpStatus,
3015 irsp->ulpIoTag,
3016 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003017 }
3018 break;
3019 }
3020
3021 /*
3022 * The response IOCB has been processed. Update the ring
3023 * pointer in SLIM. If the port response put pointer has not
3024 * been updated, sync the pgp->rspPutInx and fetch the new port
3025 * response put pointer.
3026 */
James Smart7e56aa22012-08-03 12:35:34 -04003027 writel(pring->sli.sli3.rspidx,
3028 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003029
James Smart7e56aa22012-08-03 12:35:34 -04003030 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003031 portRspPut = le32_to_cpu(pgp->rspPutInx);
3032 }
3033
3034 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3035 pring->stats.iocb_rsp_full++;
3036 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3037 writel(status, phba->CAregaddr);
3038 readl(phba->CAregaddr);
3039 }
3040 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3041 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3042 pring->stats.iocb_cmd_empty++;
3043
3044 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003045 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003046 lpfc_sli_resume_iocb(phba, pring);
3047
3048 if ((pring->lpfc_sli_cmd_available))
3049 (pring->lpfc_sli_cmd_available) (phba, pring);
3050
3051 }
3052
James Smart45ed1192009-10-02 15:17:02 -04003053 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003054 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003055 return rc;
3056}
3057
James Smarte59058c2008-08-24 21:49:00 -04003058/**
James Smart3772a992009-05-22 14:50:54 -04003059 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3060 * @phba: Pointer to HBA context object.
3061 * @pring: Pointer to driver SLI ring object.
3062 * @rspiocbp: Pointer to driver response IOCB object.
3063 *
3064 * This function is called from the worker thread when there is a slow-path
3065 * response IOCB to process. This function chains all the response iocbs until
3066 * seeing the iocb with the LE bit set. The function will call
3067 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3068 * completion of a command iocb. The function will call the
3069 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3070 * The function frees the resources or calls the completion handler if this
3071 * iocb is an abort completion. The function returns NULL when the response
3072 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3073 * this function shall chain the iocb on to the iocb_continueq and return the
3074 * response iocb passed in.
3075 **/
3076static struct lpfc_iocbq *
3077lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3078 struct lpfc_iocbq *rspiocbp)
3079{
3080 struct lpfc_iocbq *saveq;
3081 struct lpfc_iocbq *cmdiocbp;
3082 struct lpfc_iocbq *next_iocb;
3083 IOCB_t *irsp = NULL;
3084 uint32_t free_saveq;
3085 uint8_t iocb_cmd_type;
3086 lpfc_iocb_type type;
3087 unsigned long iflag;
3088 int rc;
3089
3090 spin_lock_irqsave(&phba->hbalock, iflag);
3091 /* First add the response iocb to the countinueq list */
3092 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3093 pring->iocb_continueq_cnt++;
3094
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003095 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003096 irsp = &rspiocbp->iocb;
3097 if (irsp->ulpLe) {
3098 /*
3099 * By default, the driver expects to free all resources
3100 * associated with this iocb completion.
3101 */
3102 free_saveq = 1;
3103 saveq = list_get_first(&pring->iocb_continueq,
3104 struct lpfc_iocbq, list);
3105 irsp = &(saveq->iocb);
3106 list_del_init(&pring->iocb_continueq);
3107 pring->iocb_continueq_cnt = 0;
3108
3109 pring->stats.iocb_rsp++;
3110
3111 /*
3112 * If resource errors reported from HBA, reduce
3113 * queuedepths of the SCSI device.
3114 */
3115 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3116 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3117 spin_unlock_irqrestore(&phba->hbalock, iflag);
3118 phba->lpfc_rampdown_queue_depth(phba);
3119 spin_lock_irqsave(&phba->hbalock, iflag);
3120 }
3121
3122 if (irsp->ulpStatus) {
3123 /* Rsp ring <ringno> error: IOCB */
3124 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3125 "0328 Rsp Ring %d error: "
3126 "IOCB Data: "
3127 "x%x x%x x%x x%x "
3128 "x%x x%x x%x x%x "
3129 "x%x x%x x%x x%x "
3130 "x%x x%x x%x x%x\n",
3131 pring->ringno,
3132 irsp->un.ulpWord[0],
3133 irsp->un.ulpWord[1],
3134 irsp->un.ulpWord[2],
3135 irsp->un.ulpWord[3],
3136 irsp->un.ulpWord[4],
3137 irsp->un.ulpWord[5],
3138 *(((uint32_t *) irsp) + 6),
3139 *(((uint32_t *) irsp) + 7),
3140 *(((uint32_t *) irsp) + 8),
3141 *(((uint32_t *) irsp) + 9),
3142 *(((uint32_t *) irsp) + 10),
3143 *(((uint32_t *) irsp) + 11),
3144 *(((uint32_t *) irsp) + 12),
3145 *(((uint32_t *) irsp) + 13),
3146 *(((uint32_t *) irsp) + 14),
3147 *(((uint32_t *) irsp) + 15));
3148 }
3149
3150 /*
3151 * Fetch the IOCB command type and call the correct completion
3152 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3153 * get freed back to the lpfc_iocb_list by the discovery
3154 * kernel thread.
3155 */
3156 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3157 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3158 switch (type) {
3159 case LPFC_SOL_IOCB:
3160 spin_unlock_irqrestore(&phba->hbalock, iflag);
3161 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3162 spin_lock_irqsave(&phba->hbalock, iflag);
3163 break;
3164
3165 case LPFC_UNSOL_IOCB:
3166 spin_unlock_irqrestore(&phba->hbalock, iflag);
3167 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3168 spin_lock_irqsave(&phba->hbalock, iflag);
3169 if (!rc)
3170 free_saveq = 0;
3171 break;
3172
3173 case LPFC_ABORT_IOCB:
3174 cmdiocbp = NULL;
3175 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3176 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3177 saveq);
3178 if (cmdiocbp) {
3179 /* Call the specified completion routine */
3180 if (cmdiocbp->iocb_cmpl) {
3181 spin_unlock_irqrestore(&phba->hbalock,
3182 iflag);
3183 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3184 saveq);
3185 spin_lock_irqsave(&phba->hbalock,
3186 iflag);
3187 } else
3188 __lpfc_sli_release_iocbq(phba,
3189 cmdiocbp);
3190 }
3191 break;
3192
3193 case LPFC_UNKNOWN_IOCB:
3194 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3195 char adaptermsg[LPFC_MAX_ADPTMSG];
3196 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3197 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3198 MAX_MSG_DATA);
3199 dev_warn(&((phba->pcidev)->dev),
3200 "lpfc%d: %s\n",
3201 phba->brd_no, adaptermsg);
3202 } else {
3203 /* Unknown IOCB command */
3204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3205 "0335 Unknown IOCB "
3206 "command Data: x%x "
3207 "x%x x%x x%x\n",
3208 irsp->ulpCommand,
3209 irsp->ulpStatus,
3210 irsp->ulpIoTag,
3211 irsp->ulpContext);
3212 }
3213 break;
3214 }
3215
3216 if (free_saveq) {
3217 list_for_each_entry_safe(rspiocbp, next_iocb,
3218 &saveq->list, list) {
3219 list_del(&rspiocbp->list);
3220 __lpfc_sli_release_iocbq(phba, rspiocbp);
3221 }
3222 __lpfc_sli_release_iocbq(phba, saveq);
3223 }
3224 rspiocbp = NULL;
3225 }
3226 spin_unlock_irqrestore(&phba->hbalock, iflag);
3227 return rspiocbp;
3228}
3229
3230/**
3231 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003232 * @phba: Pointer to HBA context object.
3233 * @pring: Pointer to driver SLI ring object.
3234 * @mask: Host attention register mask for this ring.
3235 *
James Smart3772a992009-05-22 14:50:54 -04003236 * This routine wraps the actual slow_ring event process routine from the
3237 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003238 **/
James Smart3772a992009-05-22 14:50:54 -04003239void
James Smart2e0fef82007-06-17 19:56:36 -05003240lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3241 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003242{
James Smart3772a992009-05-22 14:50:54 -04003243 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3244}
3245
3246/**
3247 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3248 * @phba: Pointer to HBA context object.
3249 * @pring: Pointer to driver SLI ring object.
3250 * @mask: Host attention register mask for this ring.
3251 *
3252 * This function is called from the worker thread when there is a ring event
3253 * for non-fcp rings. The caller does not hold any lock. The function will
3254 * remove each response iocb in the response ring and calls the handle
3255 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3256 **/
3257static void
3258lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3259 struct lpfc_sli_ring *pring, uint32_t mask)
3260{
James Smart34b02dc2008-08-24 21:49:55 -04003261 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003262 IOCB_t *entry;
3263 IOCB_t *irsp = NULL;
3264 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003265 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003266 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003267 uint32_t status;
dea31012005-04-17 16:05:31 -05003268
James Smart34b02dc2008-08-24 21:49:55 -04003269 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003270 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003271 pring->stats.iocb_event++;
3272
dea31012005-04-17 16:05:31 -05003273 /*
3274 * The next available response entry should never exceed the maximum
3275 * entries. If it does, treat it as an adapter hardware error.
3276 */
James Smart7e56aa22012-08-03 12:35:34 -04003277 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003278 portRspPut = le32_to_cpu(pgp->rspPutInx);
3279 if (portRspPut >= portRspMax) {
3280 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003281 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003282 * rsp ring <portRspMax>
3283 */
James Smarted957682007-06-17 19:56:37 -05003284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003285 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003286 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003287 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003288
James Smart2e0fef82007-06-17 19:56:36 -05003289 phba->link_state = LPFC_HBA_ERROR;
3290 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003291
3292 phba->work_hs = HS_FFER3;
3293 lpfc_handle_eratt(phba);
3294
James Smart3772a992009-05-22 14:50:54 -04003295 return;
dea31012005-04-17 16:05:31 -05003296 }
3297
3298 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003299 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003300 /*
3301 * Build a completion list and call the appropriate handler.
3302 * The process is to get the next available response iocb, get
3303 * a free iocb from the list, copy the response data into the
3304 * free iocb, insert to the continuation list, and update the
3305 * next response index to slim. This process makes response
3306 * iocb's in the ring available to DMA as fast as possible but
3307 * pays a penalty for a copy operation. Since the iocb is
3308 * only 32 bytes, this penalty is considered small relative to
3309 * the PCI reads for register values and a slim write. When
3310 * the ulpLe field is set, the entire Command has been
3311 * received.
3312 */
James Smarted957682007-06-17 19:56:37 -05003313 entry = lpfc_resp_iocb(phba, pring);
3314
James Smart858c9f62007-06-17 19:56:39 -05003315 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003316 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003317 if (rspiocbp == NULL) {
3318 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003319 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003320 break;
3321 }
3322
James Smarted957682007-06-17 19:56:37 -05003323 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3324 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003325 irsp = &rspiocbp->iocb;
3326
James Smart7e56aa22012-08-03 12:35:34 -04003327 if (++pring->sli.sli3.rspidx >= portRspMax)
3328 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003329
James Smarta58cbd52007-08-02 11:09:43 -04003330 if (pring->ringno == LPFC_ELS_RING) {
3331 lpfc_debugfs_slow_ring_trc(phba,
3332 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3333 *(((uint32_t *) irsp) + 4),
3334 *(((uint32_t *) irsp) + 6),
3335 *(((uint32_t *) irsp) + 7));
3336 }
3337
James Smart7e56aa22012-08-03 12:35:34 -04003338 writel(pring->sli.sli3.rspidx,
3339 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003340
James Smart3772a992009-05-22 14:50:54 -04003341 spin_unlock_irqrestore(&phba->hbalock, iflag);
3342 /* Handle the response IOCB */
3343 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3344 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003345
3346 /*
3347 * If the port response put pointer has not been updated, sync
3348 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3349 * response put pointer.
3350 */
James Smart7e56aa22012-08-03 12:35:34 -04003351 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003352 portRspPut = le32_to_cpu(pgp->rspPutInx);
3353 }
James Smart7e56aa22012-08-03 12:35:34 -04003354 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003355
James Smart92d7f7b2007-06-17 19:56:38 -05003356 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003357 /* At least one response entry has been freed */
3358 pring->stats.iocb_rsp_full++;
3359 /* SET RxRE_RSP in Chip Att register */
3360 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3361 writel(status, phba->CAregaddr);
3362 readl(phba->CAregaddr); /* flush */
3363 }
3364 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3365 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3366 pring->stats.iocb_cmd_empty++;
3367
3368 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003369 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003370 lpfc_sli_resume_iocb(phba, pring);
3371
3372 if ((pring->lpfc_sli_cmd_available))
3373 (pring->lpfc_sli_cmd_available) (phba, pring);
3374
3375 }
3376
James Smart2e0fef82007-06-17 19:56:36 -05003377 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003378 return;
dea31012005-04-17 16:05:31 -05003379}
3380
James Smarte59058c2008-08-24 21:49:00 -04003381/**
James Smart4f774512009-05-22 14:52:35 -04003382 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3383 * @phba: Pointer to HBA context object.
3384 * @pring: Pointer to driver SLI ring object.
3385 * @mask: Host attention register mask for this ring.
3386 *
3387 * This function is called from the worker thread when there is a pending
3388 * ELS response iocb on the driver internal slow-path response iocb worker
3389 * queue. The caller does not hold any lock. The function will remove each
3390 * response iocb from the response worker queue and calls the handle
3391 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3392 **/
3393static void
3394lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3395 struct lpfc_sli_ring *pring, uint32_t mask)
3396{
3397 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003398 struct hbq_dmabuf *dmabuf;
3399 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003400 unsigned long iflag;
3401
James Smart45ed1192009-10-02 15:17:02 -04003402 spin_lock_irqsave(&phba->hbalock, iflag);
3403 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3404 spin_unlock_irqrestore(&phba->hbalock, iflag);
3405 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003406 /* Get the response iocb from the head of work queue */
3407 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003408 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003409 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003410 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003411
3412 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3413 case CQE_CODE_COMPL_WQE:
3414 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3415 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003416 /* Translate ELS WCQE to response IOCBQ */
3417 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3418 irspiocbq);
3419 if (irspiocbq)
3420 lpfc_sli_sp_handle_rspiocb(phba, pring,
3421 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003422 break;
3423 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003424 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003425 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3426 cq_event);
3427 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3428 break;
3429 default:
3430 break;
3431 }
James Smart4f774512009-05-22 14:52:35 -04003432 }
3433}
3434
3435/**
James Smart3621a712009-04-06 18:47:14 -04003436 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003437 * @phba: Pointer to HBA context object.
3438 * @pring: Pointer to driver SLI ring object.
3439 *
3440 * This function aborts all iocbs in the given ring and frees all the iocb
3441 * objects in txq. This function issues an abort iocb for all the iocb commands
3442 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3443 * the return of this function. The caller is not required to hold any locks.
3444 **/
James Smart2e0fef82007-06-17 19:56:36 -05003445void
dea31012005-04-17 16:05:31 -05003446lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3447{
James Smart2534ba72007-04-25 09:52:20 -04003448 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003449 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003450
James Smart92d7f7b2007-06-17 19:56:38 -05003451 if (pring->ringno == LPFC_ELS_RING) {
3452 lpfc_fabric_abort_hba(phba);
3453 }
3454
dea31012005-04-17 16:05:31 -05003455 /* Error everything on txq and txcmplq
3456 * First do the txq.
3457 */
James Smart2e0fef82007-06-17 19:56:36 -05003458 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003459 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003460 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003461
3462 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003463 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3464 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3465
James Smart2e0fef82007-06-17 19:56:36 -05003466 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003467
James Smarta257bf92009-04-06 18:48:10 -04003468 /* Cancel all the IOCBs from the completions list */
3469 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3470 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003471}
3472
James Smarte59058c2008-08-24 21:49:00 -04003473/**
James Smart3621a712009-04-06 18:47:14 -04003474 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003475 * @phba: Pointer to HBA context object.
3476 *
3477 * This function flushes all iocbs in the fcp ring and frees all the iocb
3478 * objects in txq and txcmplq. This function will not issue abort iocbs
3479 * for all the iocb commands in txcmplq, they will just be returned with
3480 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3481 * slot has been permanently disabled.
3482 **/
3483void
3484lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3485{
3486 LIST_HEAD(txq);
3487 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003488 struct lpfc_sli *psli = &phba->sli;
3489 struct lpfc_sli_ring *pring;
3490
3491 /* Currently, only one fcp ring */
3492 pring = &psli->ring[psli->fcp_ring];
3493
3494 spin_lock_irq(&phba->hbalock);
3495 /* Retrieve everything on txq */
3496 list_splice_init(&pring->txq, &txq);
3497 pring->txq_cnt = 0;
3498
3499 /* Retrieve everything on the txcmplq */
3500 list_splice_init(&pring->txcmplq, &txcmplq);
3501 pring->txcmplq_cnt = 0;
James Smart4f2e66c2012-05-09 21:17:07 -04003502
3503 /* Indicate the I/O queues are flushed */
3504 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003505 spin_unlock_irq(&phba->hbalock);
3506
3507 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003508 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3509 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003510
3511 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003512 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3513 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003514}
3515
3516/**
James Smart3772a992009-05-22 14:50:54 -04003517 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003518 * @phba: Pointer to HBA context object.
3519 * @mask: Bit mask to be checked.
3520 *
3521 * This function reads the host status register and compares
3522 * with the provided bit mask to check if HBA completed
3523 * the restart. This function will wait in a loop for the
3524 * HBA to complete restart. If the HBA does not restart within
3525 * 15 iterations, the function will reset the HBA again. The
3526 * function returns 1 when HBA fail to restart otherwise returns
3527 * zero.
3528 **/
James Smart3772a992009-05-22 14:50:54 -04003529static int
3530lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003531{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003532 uint32_t status;
3533 int i = 0;
3534 int retval = 0;
dea31012005-04-17 16:05:31 -05003535
Jamie Wellnitz41415862006-02-28 19:25:27 -05003536 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003537 if (lpfc_readl(phba->HSregaddr, &status))
3538 return 1;
dea31012005-04-17 16:05:31 -05003539
Jamie Wellnitz41415862006-02-28 19:25:27 -05003540 /*
3541 * Check status register every 100ms for 5 retries, then every
3542 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3543 * every 2.5 sec for 4.
3544 * Break our of the loop if errors occurred during init.
3545 */
3546 while (((status & mask) != mask) &&
3547 !(status & HS_FFERM) &&
3548 i++ < 20) {
dea31012005-04-17 16:05:31 -05003549
Jamie Wellnitz41415862006-02-28 19:25:27 -05003550 if (i <= 5)
3551 msleep(10);
3552 else if (i <= 10)
3553 msleep(500);
3554 else
3555 msleep(2500);
dea31012005-04-17 16:05:31 -05003556
Jamie Wellnitz41415862006-02-28 19:25:27 -05003557 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003558 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003559 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003560 lpfc_sli_brdrestart(phba);
3561 }
3562 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003563 if (lpfc_readl(phba->HSregaddr, &status)) {
3564 retval = 1;
3565 break;
3566 }
dea31012005-04-17 16:05:31 -05003567 }
dea31012005-04-17 16:05:31 -05003568
Jamie Wellnitz41415862006-02-28 19:25:27 -05003569 /* Check to see if any errors occurred during init */
3570 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3572 "2751 Adapter failed to restart, "
3573 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3574 status,
3575 readl(phba->MBslimaddr + 0xa8),
3576 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003577 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003578 retval = 1;
3579 }
dea31012005-04-17 16:05:31 -05003580
Jamie Wellnitz41415862006-02-28 19:25:27 -05003581 return retval;
dea31012005-04-17 16:05:31 -05003582}
3583
James Smartda0436e2009-05-22 14:51:39 -04003584/**
3585 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3586 * @phba: Pointer to HBA context object.
3587 * @mask: Bit mask to be checked.
3588 *
3589 * This function checks the host status register to check if HBA is
3590 * ready. This function will wait in a loop for the HBA to be ready
3591 * If the HBA is not ready , the function will will reset the HBA PCI
3592 * function again. The function returns 1 when HBA fail to be ready
3593 * otherwise returns zero.
3594 **/
3595static int
3596lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3597{
3598 uint32_t status;
3599 int retval = 0;
3600
3601 /* Read the HBA Host Status Register */
3602 status = lpfc_sli4_post_status_check(phba);
3603
3604 if (status) {
3605 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3606 lpfc_sli_brdrestart(phba);
3607 status = lpfc_sli4_post_status_check(phba);
3608 }
3609
3610 /* Check to see if any errors occurred during init */
3611 if (status) {
3612 phba->link_state = LPFC_HBA_ERROR;
3613 retval = 1;
3614 } else
3615 phba->sli4_hba.intr_enable = 0;
3616
3617 return retval;
3618}
3619
3620/**
3621 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3622 * @phba: Pointer to HBA context object.
3623 * @mask: Bit mask to be checked.
3624 *
3625 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3626 * from the API jump table function pointer from the lpfc_hba struct.
3627 **/
3628int
3629lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3630{
3631 return phba->lpfc_sli_brdready(phba, mask);
3632}
3633
James Smart92908312006-03-07 15:04:13 -05003634#define BARRIER_TEST_PATTERN (0xdeadbeef)
3635
James Smarte59058c2008-08-24 21:49:00 -04003636/**
James Smart3621a712009-04-06 18:47:14 -04003637 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003638 * @phba: Pointer to HBA context object.
3639 *
James Smart1b511972011-12-13 13:23:09 -05003640 * This function is called before resetting an HBA. This function is called
3641 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003642 **/
James Smart2e0fef82007-06-17 19:56:36 -05003643void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003644{
James Smart65a29c12006-07-06 15:50:50 -04003645 uint32_t __iomem *resp_buf;
3646 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003647 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003648 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003649 int i;
3650 uint8_t hdrtype;
3651
3652 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3653 if (hdrtype != 0x80 ||
3654 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3655 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3656 return;
3657
3658 /*
3659 * Tell the other part of the chip to suspend temporarily all
3660 * its DMA activity.
3661 */
James Smart65a29c12006-07-06 15:50:50 -04003662 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003663
3664 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003665 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3666 return;
James Smart92908312006-03-07 15:04:13 -05003667 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3668 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003669 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003670
James Smart9940b972011-03-11 16:06:12 -05003671 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3672 return;
3673 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003674 /* Clear Chip error bit */
3675 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003676 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003677 }
3678
3679 mbox = 0;
3680 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3681 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3682
3683 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003684 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003685 writel(mbox, mbox_buf);
3686
James Smart9940b972011-03-11 16:06:12 -05003687 for (i = 0; i < 50; i++) {
3688 if (lpfc_readl((resp_buf + 1), &resp_data))
3689 return;
3690 if (resp_data != ~(BARRIER_TEST_PATTERN))
3691 mdelay(1);
3692 else
3693 break;
3694 }
3695 resp_data = 0;
3696 if (lpfc_readl((resp_buf + 1), &resp_data))
3697 return;
3698 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003699 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003700 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003701 goto restore_hc;
3702 else
3703 goto clear_errat;
3704 }
3705
3706 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003707 resp_data = 0;
3708 for (i = 0; i < 500; i++) {
3709 if (lpfc_readl(resp_buf, &resp_data))
3710 return;
3711 if (resp_data != mbox)
3712 mdelay(1);
3713 else
3714 break;
3715 }
James Smart92908312006-03-07 15:04:13 -05003716
3717clear_errat:
3718
James Smart9940b972011-03-11 16:06:12 -05003719 while (++i < 500) {
3720 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3721 return;
3722 if (!(ha_copy & HA_ERATT))
3723 mdelay(1);
3724 else
3725 break;
3726 }
James Smart92908312006-03-07 15:04:13 -05003727
3728 if (readl(phba->HAregaddr) & HA_ERATT) {
3729 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003730 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003731 }
3732
3733restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003734 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003735 writel(hc_copy, phba->HCregaddr);
3736 readl(phba->HCregaddr); /* flush */
3737}
3738
James Smarte59058c2008-08-24 21:49:00 -04003739/**
James Smart3621a712009-04-06 18:47:14 -04003740 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003741 * @phba: Pointer to HBA context object.
3742 *
3743 * This function issues a kill_board mailbox command and waits for
3744 * the error attention interrupt. This function is called for stopping
3745 * the firmware processing. The caller is not required to hold any
3746 * locks. This function calls lpfc_hba_down_post function to free
3747 * any pending commands after the kill. The function will return 1 when it
3748 * fails to kill the board else will return 0.
3749 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003750int
James Smart2e0fef82007-06-17 19:56:36 -05003751lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003752{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003753 struct lpfc_sli *psli;
3754 LPFC_MBOXQ_t *pmb;
3755 uint32_t status;
3756 uint32_t ha_copy;
3757 int retval;
3758 int i = 0;
3759
3760 psli = &phba->sli;
3761
3762 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003763 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003764 "0329 Kill HBA Data: x%x x%x\n",
3765 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003766
James Smart98c9ea52007-10-27 13:37:33 -04003767 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3768 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003769 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003770
3771 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003772 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003773 if (lpfc_readl(phba->HCregaddr, &status)) {
3774 spin_unlock_irq(&phba->hbalock);
3775 mempool_free(pmb, phba->mbox_mem_pool);
3776 return 1;
3777 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003778 status &= ~HC_ERINT_ENA;
3779 writel(status, phba->HCregaddr);
3780 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003781 phba->link_flag |= LS_IGNORE_ERATT;
3782 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003783
3784 lpfc_kill_board(phba, pmb);
3785 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3786 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3787
3788 if (retval != MBX_SUCCESS) {
3789 if (retval != MBX_BUSY)
3790 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003791 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3792 "2752 KILL_BOARD command failed retval %d\n",
3793 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003794 spin_lock_irq(&phba->hbalock);
3795 phba->link_flag &= ~LS_IGNORE_ERATT;
3796 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003797 return 1;
3798 }
3799
James Smartf4b4c682009-05-22 14:53:12 -04003800 spin_lock_irq(&phba->hbalock);
3801 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3802 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003803
Jamie Wellnitz41415862006-02-28 19:25:27 -05003804 mempool_free(pmb, phba->mbox_mem_pool);
3805
3806 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3807 * attention every 100ms for 3 seconds. If we don't get ERATT after
3808 * 3 seconds we still set HBA_ERROR state because the status of the
3809 * board is now undefined.
3810 */
James Smart9940b972011-03-11 16:06:12 -05003811 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3812 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003813 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3814 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003815 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3816 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003817 }
3818
3819 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003820 if (ha_copy & HA_ERATT) {
3821 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003822 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003823 }
James Smart2e0fef82007-06-17 19:56:36 -05003824 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003825 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003826 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003827 phba->link_flag &= ~LS_IGNORE_ERATT;
3828 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003829
Jamie Wellnitz41415862006-02-28 19:25:27 -05003830 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003831 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003832
James Smart2e0fef82007-06-17 19:56:36 -05003833 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003834}
3835
James Smarte59058c2008-08-24 21:49:00 -04003836/**
James Smart3772a992009-05-22 14:50:54 -04003837 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003838 * @phba: Pointer to HBA context object.
3839 *
3840 * This function resets the HBA by writing HC_INITFF to the control
3841 * register. After the HBA resets, this function resets all the iocb ring
3842 * indices. This function disables PCI layer parity checking during
3843 * the reset.
3844 * This function returns 0 always.
3845 * The caller is not required to hold any locks.
3846 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003847int
James Smart2e0fef82007-06-17 19:56:36 -05003848lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003849{
3850 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003851 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003852 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003853 int i;
dea31012005-04-17 16:05:31 -05003854
Jamie Wellnitz41415862006-02-28 19:25:27 -05003855 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003856
Jamie Wellnitz41415862006-02-28 19:25:27 -05003857 /* Reset HBA */
3858 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003859 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003860 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003861
3862 /* perform board reset */
3863 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003864 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003865 phba->pport->fc_myDID = 0;
3866 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003867
Jamie Wellnitz41415862006-02-28 19:25:27 -05003868 /* Turn off parity checking and serr during the physical reset */
3869 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3870 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3871 (cfg_value &
3872 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3873
James Smart3772a992009-05-22 14:50:54 -04003874 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3875
Jamie Wellnitz41415862006-02-28 19:25:27 -05003876 /* Now toggle INITFF bit in the Host Control Register */
3877 writel(HC_INITFF, phba->HCregaddr);
3878 mdelay(1);
3879 readl(phba->HCregaddr); /* flush */
3880 writel(0, phba->HCregaddr);
3881 readl(phba->HCregaddr); /* flush */
3882
3883 /* Restore PCI cmd register */
3884 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003885
3886 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003887 for (i = 0; i < psli->num_rings; i++) {
3888 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003889 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003890 pring->sli.sli3.rspidx = 0;
3891 pring->sli.sli3.next_cmdidx = 0;
3892 pring->sli.sli3.local_getidx = 0;
3893 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003894 pring->missbufcnt = 0;
3895 }
dea31012005-04-17 16:05:31 -05003896
James Smart2e0fef82007-06-17 19:56:36 -05003897 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003898 return 0;
3899}
3900
James Smarte59058c2008-08-24 21:49:00 -04003901/**
James Smartda0436e2009-05-22 14:51:39 -04003902 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3903 * @phba: Pointer to HBA context object.
3904 *
3905 * This function resets a SLI4 HBA. This function disables PCI layer parity
3906 * checking during resets the device. The caller is not required to hold
3907 * any locks.
3908 *
3909 * This function returns 0 always.
3910 **/
3911int
3912lpfc_sli4_brdreset(struct lpfc_hba *phba)
3913{
3914 struct lpfc_sli *psli = &phba->sli;
3915 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003916 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003917
3918 /* Reset HBA */
3919 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3920 "0295 Reset HBA Data: x%x x%x\n",
3921 phba->pport->port_state, psli->sli_flag);
3922
3923 /* perform board reset */
3924 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003925 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003926 phba->pport->fc_myDID = 0;
3927 phba->pport->fc_prevDID = 0;
3928
James Smartda0436e2009-05-22 14:51:39 -04003929 spin_lock_irq(&phba->hbalock);
3930 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3931 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003932 spin_unlock_irq(&phba->hbalock);
3933
3934 /* Now physically reset the device */
3935 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3936 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003937
3938 /* Turn off parity checking and serr during the physical reset */
3939 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3940 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3941 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3942
James Smartda0436e2009-05-22 14:51:39 -04003943 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003944 lpfc_sli4_queue_destroy(phba);
James Smart27b01b82012-05-09 21:19:44 -04003945 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04003946
James Smartbe858b62010-12-15 17:57:20 -05003947 /* Restore PCI cmd register */
3948 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3949
James Smart27b01b82012-05-09 21:19:44 -04003950 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003951}
3952
3953/**
3954 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003955 * @phba: Pointer to HBA context object.
3956 *
3957 * This function is called in the SLI initialization code path to
3958 * restart the HBA. The caller is not required to hold any lock.
3959 * This function writes MBX_RESTART mailbox command to the SLIM and
3960 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3961 * function to free any pending commands. The function enables
3962 * POST only during the first initialization. The function returns zero.
3963 * The function does not guarantee completion of MBX_RESTART mailbox
3964 * command before the return of this function.
3965 **/
James Smartda0436e2009-05-22 14:51:39 -04003966static int
3967lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003968{
3969 MAILBOX_t *mb;
3970 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003971 volatile uint32_t word0;
3972 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003973 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003974
James Smart2e0fef82007-06-17 19:56:36 -05003975 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003976
James Smart0d878412009-10-02 15:16:56 -04003977 /* Take PCIe device Advanced Error Reporting (AER) state */
3978 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3979
Jamie Wellnitz41415862006-02-28 19:25:27 -05003980 psli = &phba->sli;
3981
3982 /* Restart HBA */
3983 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003984 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003985 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003986
3987 word0 = 0;
3988 mb = (MAILBOX_t *) &word0;
3989 mb->mbxCommand = MBX_RESTART;
3990 mb->mbxHc = 1;
3991
James Smart92908312006-03-07 15:04:13 -05003992 lpfc_reset_barrier(phba);
3993
Jamie Wellnitz41415862006-02-28 19:25:27 -05003994 to_slim = phba->MBslimaddr;
3995 writel(*(uint32_t *) mb, to_slim);
3996 readl(to_slim); /* flush */
3997
3998 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003999 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004000 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004001 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004002 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004003 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004004 writel(*(uint32_t *) mb, to_slim);
4005 readl(to_slim); /* flush */
4006
4007 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004008 phba->pport->stopped = 0;
4009 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004010 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004011 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004012
James Smart64ba8812006-08-02 15:24:34 -04004013 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4014 psli->stats_start = get_seconds();
4015
James Smarteaf15d52008-12-04 22:39:29 -05004016 /* Give the INITFF and Post time to settle. */
4017 mdelay(100);
dea31012005-04-17 16:05:31 -05004018
James Smart0d878412009-10-02 15:16:56 -04004019 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4020 if (hba_aer_enabled)
4021 pci_disable_pcie_error_reporting(phba->pcidev);
4022
Jamie Wellnitz41415862006-02-28 19:25:27 -05004023 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004024
4025 return 0;
4026}
4027
James Smarte59058c2008-08-24 21:49:00 -04004028/**
James Smartda0436e2009-05-22 14:51:39 -04004029 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4030 * @phba: Pointer to HBA context object.
4031 *
4032 * This function is called in the SLI initialization code path to restart
4033 * a SLI4 HBA. The caller is not required to hold any lock.
4034 * At the end of the function, it calls lpfc_hba_down_post function to
4035 * free any pending commands.
4036 **/
4037static int
4038lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4039{
4040 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004041 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004042 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004043
4044 /* Restart HBA */
4045 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4046 "0296 Restart HBA Data: x%x x%x\n",
4047 phba->pport->port_state, psli->sli_flag);
4048
James Smart75baf692010-06-08 18:31:21 -04004049 /* Take PCIe device Advanced Error Reporting (AER) state */
4050 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4051
James Smart27b01b82012-05-09 21:19:44 -04004052 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004053
4054 spin_lock_irq(&phba->hbalock);
4055 phba->pport->stopped = 0;
4056 phba->link_state = LPFC_INIT_START;
4057 phba->hba_flag = 0;
4058 spin_unlock_irq(&phba->hbalock);
4059
4060 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4061 psli->stats_start = get_seconds();
4062
James Smart75baf692010-06-08 18:31:21 -04004063 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4064 if (hba_aer_enabled)
4065 pci_disable_pcie_error_reporting(phba->pcidev);
4066
James Smartda0436e2009-05-22 14:51:39 -04004067 lpfc_hba_down_post(phba);
4068
James Smart27b01b82012-05-09 21:19:44 -04004069 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004070}
4071
4072/**
4073 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4074 * @phba: Pointer to HBA context object.
4075 *
4076 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4077 * API jump table function pointer from the lpfc_hba struct.
4078**/
4079int
4080lpfc_sli_brdrestart(struct lpfc_hba *phba)
4081{
4082 return phba->lpfc_sli_brdrestart(phba);
4083}
4084
4085/**
James Smart3621a712009-04-06 18:47:14 -04004086 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004087 * @phba: Pointer to HBA context object.
4088 *
4089 * This function is called after a HBA restart to wait for successful
4090 * restart of the HBA. Successful restart of the HBA is indicated by
4091 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4092 * iteration, the function will restart the HBA again. The function returns
4093 * zero if HBA successfully restarted else returns negative error code.
4094 **/
dea31012005-04-17 16:05:31 -05004095static int
4096lpfc_sli_chipset_init(struct lpfc_hba *phba)
4097{
4098 uint32_t status, i = 0;
4099
4100 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004101 if (lpfc_readl(phba->HSregaddr, &status))
4102 return -EIO;
dea31012005-04-17 16:05:31 -05004103
4104 /* Check status register to see what current state is */
4105 i = 0;
4106 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4107
James Smartdcf2a4e2010-09-29 11:18:53 -04004108 /* Check every 10ms for 10 retries, then every 100ms for 90
4109 * retries, then every 1 sec for 50 retires for a total of
4110 * ~60 seconds before reset the board again and check every
4111 * 1 sec for 50 retries. The up to 60 seconds before the
4112 * board ready is required by the Falcon FIPS zeroization
4113 * complete, and any reset the board in between shall cause
4114 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004115 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004116 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004117 /* Adapter failed to init, timeout, status reg
4118 <status> */
James Smarted957682007-06-17 19:56:37 -05004119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004120 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004121 "timeout, status reg x%x, "
4122 "FW Data: A8 x%x AC x%x\n", status,
4123 readl(phba->MBslimaddr + 0xa8),
4124 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004125 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004126 return -ETIMEDOUT;
4127 }
4128
4129 /* Check to see if any errors occurred during init */
4130 if (status & HS_FFERM) {
4131 /* ERROR: During chipset initialization */
4132 /* Adapter failed to init, chipset, status reg
4133 <status> */
James Smarted957682007-06-17 19:56:37 -05004134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004135 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004136 "chipset, status reg x%x, "
4137 "FW Data: A8 x%x AC x%x\n", status,
4138 readl(phba->MBslimaddr + 0xa8),
4139 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004140 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004141 return -EIO;
4142 }
4143
James Smartdcf2a4e2010-09-29 11:18:53 -04004144 if (i <= 10)
dea31012005-04-17 16:05:31 -05004145 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004146 else if (i <= 100)
4147 msleep(100);
4148 else
4149 msleep(1000);
dea31012005-04-17 16:05:31 -05004150
James Smartdcf2a4e2010-09-29 11:18:53 -04004151 if (i == 150) {
4152 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004153 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004154 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004155 }
4156 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004157 if (lpfc_readl(phba->HSregaddr, &status))
4158 return -EIO;
dea31012005-04-17 16:05:31 -05004159 }
4160
4161 /* Check to see if any errors occurred during init */
4162 if (status & HS_FFERM) {
4163 /* ERROR: During chipset initialization */
4164 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004166 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004167 "status reg x%x, "
4168 "FW Data: A8 x%x AC x%x\n", status,
4169 readl(phba->MBslimaddr + 0xa8),
4170 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004171 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004172 return -EIO;
4173 }
4174
4175 /* Clear all interrupt enable conditions */
4176 writel(0, phba->HCregaddr);
4177 readl(phba->HCregaddr); /* flush */
4178
4179 /* setup host attn register */
4180 writel(0xffffffff, phba->HAregaddr);
4181 readl(phba->HAregaddr); /* flush */
4182 return 0;
4183}
4184
James Smarte59058c2008-08-24 21:49:00 -04004185/**
James Smart3621a712009-04-06 18:47:14 -04004186 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004187 *
4188 * This function calculates and returns the number of HBQs required to be
4189 * configured.
4190 **/
James Smart78b2d852007-08-02 11:10:21 -04004191int
James Smarted957682007-06-17 19:56:37 -05004192lpfc_sli_hbq_count(void)
4193{
James Smart92d7f7b2007-06-17 19:56:38 -05004194 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004195}
4196
James Smarte59058c2008-08-24 21:49:00 -04004197/**
James Smart3621a712009-04-06 18:47:14 -04004198 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004199 *
4200 * This function adds the number of hbq entries in every HBQ to get
4201 * the total number of hbq entries required for the HBA and returns
4202 * the total count.
4203 **/
James Smarted957682007-06-17 19:56:37 -05004204static int
4205lpfc_sli_hbq_entry_count(void)
4206{
4207 int hbq_count = lpfc_sli_hbq_count();
4208 int count = 0;
4209 int i;
4210
4211 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004212 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004213 return count;
4214}
4215
James Smarte59058c2008-08-24 21:49:00 -04004216/**
James Smart3621a712009-04-06 18:47:14 -04004217 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004218 *
4219 * This function calculates amount of memory required for all hbq entries
4220 * to be configured and returns the total memory required.
4221 **/
dea31012005-04-17 16:05:31 -05004222int
James Smarted957682007-06-17 19:56:37 -05004223lpfc_sli_hbq_size(void)
4224{
4225 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4226}
4227
James Smarte59058c2008-08-24 21:49:00 -04004228/**
James Smart3621a712009-04-06 18:47:14 -04004229 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004230 * @phba: Pointer to HBA context object.
4231 *
4232 * This function is called during the SLI initialization to configure
4233 * all the HBQs and post buffers to the HBQ. The caller is not
4234 * required to hold any locks. This function will return zero if successful
4235 * else it will return negative error code.
4236 **/
James Smarted957682007-06-17 19:56:37 -05004237static int
4238lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4239{
4240 int hbq_count = lpfc_sli_hbq_count();
4241 LPFC_MBOXQ_t *pmb;
4242 MAILBOX_t *pmbox;
4243 uint32_t hbqno;
4244 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004245
James Smart92d7f7b2007-06-17 19:56:38 -05004246 /* Get a Mailbox buffer to setup mailbox
4247 * commands for HBA initialization
4248 */
James Smarted957682007-06-17 19:56:37 -05004249 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4250
4251 if (!pmb)
4252 return -ENOMEM;
4253
James Smart04c68492009-05-22 14:52:52 -04004254 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004255
4256 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4257 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004258 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004259
4260 hbq_entry_index = 0;
4261 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4262 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4263 phba->hbqs[hbqno].hbqPutIdx = 0;
4264 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4265 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004266 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004267 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4268 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004269 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4270
4271 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4272 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4273 mbxStatus <status>, ring <num> */
4274
4275 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004276 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004277 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004278 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004279 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004280 pmbox->mbxStatus, hbqno);
4281
4282 phba->link_state = LPFC_HBA_ERROR;
4283 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004284 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004285 }
4286 }
4287 phba->hbq_count = hbq_count;
4288
James Smarted957682007-06-17 19:56:37 -05004289 mempool_free(pmb, phba->mbox_mem_pool);
4290
James Smart92d7f7b2007-06-17 19:56:38 -05004291 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004292 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4293 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004294 return 0;
4295}
4296
James Smarte59058c2008-08-24 21:49:00 -04004297/**
James Smart4f774512009-05-22 14:52:35 -04004298 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4299 * @phba: Pointer to HBA context object.
4300 *
4301 * This function is called during the SLI initialization to configure
4302 * all the HBQs and post buffers to the HBQ. The caller is not
4303 * required to hold any locks. This function will return zero if successful
4304 * else it will return negative error code.
4305 **/
4306static int
4307lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4308{
4309 phba->hbq_in_use = 1;
4310 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4311 phba->hbq_count = 1;
4312 /* Initially populate or replenish the HBQs */
4313 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4314 return 0;
4315}
4316
4317/**
James Smart3621a712009-04-06 18:47:14 -04004318 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004319 * @phba: Pointer to HBA context object.
4320 * @sli_mode: sli mode - 2/3
4321 *
4322 * This function is called by the sli intialization code path
4323 * to issue config_port mailbox command. This function restarts the
4324 * HBA firmware and issues a config_port mailbox command to configure
4325 * the SLI interface in the sli mode specified by sli_mode
4326 * variable. The caller is not required to hold any locks.
4327 * The function returns 0 if successful, else returns negative error
4328 * code.
4329 **/
James Smart93996272008-08-24 21:50:30 -04004330int
4331lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004332{
4333 LPFC_MBOXQ_t *pmb;
4334 uint32_t resetcount = 0, rc = 0, done = 0;
4335
4336 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4337 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004338 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004339 return -ENOMEM;
4340 }
4341
James Smarted957682007-06-17 19:56:37 -05004342 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004343 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004344 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004345 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004346 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004347 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004348 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004349 rc = lpfc_sli_chipset_init(phba);
4350 if (rc)
4351 break;
4352
James Smart2e0fef82007-06-17 19:56:36 -05004353 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004354 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004355 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004356 resetcount++;
4357
James Smarted957682007-06-17 19:56:37 -05004358 /* Call pre CONFIG_PORT mailbox command initialization. A
4359 * value of 0 means the call was successful. Any other
4360 * nonzero value is a failure, but if ERESTART is returned,
4361 * the driver may reset the HBA and try again.
4362 */
dea31012005-04-17 16:05:31 -05004363 rc = lpfc_config_port_prep(phba);
4364 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004365 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004366 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004367 } else if (rc)
dea31012005-04-17 16:05:31 -05004368 break;
James Smart6d368e52011-05-24 11:44:12 -04004369
James Smart2e0fef82007-06-17 19:56:36 -05004370 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004371 lpfc_config_port(phba, pmb);
4372 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004373 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4374 LPFC_SLI3_HBQ_ENABLED |
4375 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004376 LPFC_SLI3_BG_ENABLED |
4377 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004378 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004380 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004381 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004382 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004383 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004384 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004385 spin_unlock_irq(&phba->hbalock);
4386 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004387 } else {
4388 /* Allow asynchronous mailbox command to go through */
4389 spin_lock_irq(&phba->hbalock);
4390 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4391 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004392 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004393
4394 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4395 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4396 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4397 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004398 }
dea31012005-04-17 16:05:31 -05004399 }
James Smarted957682007-06-17 19:56:37 -05004400 if (!done) {
4401 rc = -EINVAL;
4402 goto do_prep_failed;
4403 }
James Smart04c68492009-05-22 14:52:52 -04004404 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4405 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004406 rc = -ENXIO;
4407 goto do_prep_failed;
4408 }
James Smart04c68492009-05-22 14:52:52 -04004409 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004410 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004411 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4412 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4413 phba->max_vpi : phba->max_vports;
4414
James Smart34b02dc2008-08-24 21:49:55 -04004415 } else
4416 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004417 phba->fips_level = 0;
4418 phba->fips_spec_rev = 0;
4419 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004420 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004421 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4422 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4423 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4424 "2850 Security Crypto Active. FIPS x%d "
4425 "(Spec Rev: x%d)",
4426 phba->fips_level, phba->fips_spec_rev);
4427 }
4428 if (pmb->u.mb.un.varCfgPort.sec_err) {
4429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4430 "2856 Config Port Security Crypto "
4431 "Error: x%x ",
4432 pmb->u.mb.un.varCfgPort.sec_err);
4433 }
James Smart04c68492009-05-22 14:52:52 -04004434 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004435 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004436 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004437 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004438
4439 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4440 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004441
4442 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004443 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004444 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4445 else
4446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4447 "0443 Adapter did not grant "
4448 "BlockGuard\n");
4449 }
James Smart34b02dc2008-08-24 21:49:55 -04004450 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004451 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004452 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004453 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004454 }
James Smart92d7f7b2007-06-17 19:56:38 -05004455do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004456 mempool_free(pmb, phba->mbox_mem_pool);
4457 return rc;
4458}
4459
James Smarte59058c2008-08-24 21:49:00 -04004460
4461/**
James Smart3621a712009-04-06 18:47:14 -04004462 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004463 * @phba: Pointer to HBA context object.
4464 *
4465 * This function is the main SLI intialization function. This function
4466 * is called by the HBA intialization code, HBA reset code and HBA
4467 * error attention handler code. Caller is not required to hold any
4468 * locks. This function issues config_port mailbox command to configure
4469 * the SLI, setup iocb rings and HBQ rings. In the end the function
4470 * calls the config_port_post function to issue init_link mailbox
4471 * command and to start the discovery. The function will return zero
4472 * if successful, else it will return negative error code.
4473 **/
James Smarted957682007-06-17 19:56:37 -05004474int
4475lpfc_sli_hba_setup(struct lpfc_hba *phba)
4476{
4477 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004478 int mode = 3, i;
4479 int longs;
James Smarted957682007-06-17 19:56:37 -05004480
4481 switch (lpfc_sli_mode) {
4482 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004483 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004485 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004486 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004487 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004488 break;
4489 }
James Smarted957682007-06-17 19:56:37 -05004490 mode = 2;
4491 break;
4492 case 0:
4493 case 3:
4494 break;
4495 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004497 "1819 Unrecognized lpfc_sli_mode "
4498 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004499
4500 break;
4501 }
4502
James Smart93996272008-08-24 21:50:30 -04004503 rc = lpfc_sli_config_port(phba, mode);
4504
James Smarted957682007-06-17 19:56:37 -05004505 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004507 "1820 Unable to select SLI-3. "
4508 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004509 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004510 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004511 if (rc)
dea31012005-04-17 16:05:31 -05004512 goto lpfc_sli_hba_setup_error;
4513
James Smart0d878412009-10-02 15:16:56 -04004514 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4515 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4516 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4517 if (!rc) {
4518 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4519 "2709 This device supports "
4520 "Advanced Error Reporting (AER)\n");
4521 spin_lock_irq(&phba->hbalock);
4522 phba->hba_flag |= HBA_AER_ENABLED;
4523 spin_unlock_irq(&phba->hbalock);
4524 } else {
4525 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4526 "2708 This device does not support "
4527 "Advanced Error Reporting (AER)\n");
4528 phba->cfg_aer_support = 0;
4529 }
4530 }
4531
James Smarted957682007-06-17 19:56:37 -05004532 if (phba->sli_rev == 3) {
4533 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4534 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004535 } else {
4536 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4537 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004538 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004539 }
4540
4541 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004542 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4543 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004544 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004545
4546 if (rc)
4547 goto lpfc_sli_hba_setup_error;
4548
James Smart6d368e52011-05-24 11:44:12 -04004549 /* Initialize VPIs. */
4550 if (phba->sli_rev == LPFC_SLI_REV3) {
4551 /*
4552 * The VPI bitmask and physical ID array are allocated
4553 * and initialized once only - at driver load. A port
4554 * reset doesn't need to reinitialize this memory.
4555 */
4556 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4557 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4558 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4559 GFP_KERNEL);
4560 if (!phba->vpi_bmask) {
4561 rc = -ENOMEM;
4562 goto lpfc_sli_hba_setup_error;
4563 }
4564
4565 phba->vpi_ids = kzalloc(
4566 (phba->max_vpi+1) * sizeof(uint16_t),
4567 GFP_KERNEL);
4568 if (!phba->vpi_ids) {
4569 kfree(phba->vpi_bmask);
4570 rc = -ENOMEM;
4571 goto lpfc_sli_hba_setup_error;
4572 }
4573 for (i = 0; i < phba->max_vpi; i++)
4574 phba->vpi_ids[i] = i;
4575 }
4576 }
4577
James Smart93996272008-08-24 21:50:30 -04004578 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004579 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4580 rc = lpfc_sli_hbq_setup(phba);
4581 if (rc)
4582 goto lpfc_sli_hba_setup_error;
4583 }
James Smart04c68492009-05-22 14:52:52 -04004584 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004585 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004586 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004587
4588 rc = lpfc_config_port_post(phba);
4589 if (rc)
4590 goto lpfc_sli_hba_setup_error;
4591
James Smarted957682007-06-17 19:56:37 -05004592 return rc;
4593
James Smart92d7f7b2007-06-17 19:56:38 -05004594lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004595 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004597 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004598 return rc;
4599}
4600
James Smartda0436e2009-05-22 14:51:39 -04004601/**
4602 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4603 * @phba: Pointer to HBA context object.
4604 * @mboxq: mailbox pointer.
4605 * This function issue a dump mailbox command to read config region
4606 * 23 and parse the records in the region and populate driver
4607 * data structure.
4608 **/
4609static int
James Smartff78d8f2011-12-13 13:21:35 -05004610lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004611{
James Smartff78d8f2011-12-13 13:21:35 -05004612 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004613 struct lpfc_dmabuf *mp;
4614 struct lpfc_mqe *mqe;
4615 uint32_t data_length;
4616 int rc;
4617
4618 /* Program the default value of vlan_id and fc_map */
4619 phba->valid_vlan = 0;
4620 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4621 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4622 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4623
James Smartff78d8f2011-12-13 13:21:35 -05004624 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4625 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004626 return -ENOMEM;
4627
James Smartff78d8f2011-12-13 13:21:35 -05004628 mqe = &mboxq->u.mqe;
4629 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4630 rc = -ENOMEM;
4631 goto out_free_mboxq;
4632 }
4633
James Smartda0436e2009-05-22 14:51:39 -04004634 mp = (struct lpfc_dmabuf *) mboxq->context1;
4635 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4636
4637 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4638 "(%d):2571 Mailbox cmd x%x Status x%x "
4639 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4640 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4641 "CQ: x%x x%x x%x x%x\n",
4642 mboxq->vport ? mboxq->vport->vpi : 0,
4643 bf_get(lpfc_mqe_command, mqe),
4644 bf_get(lpfc_mqe_status, mqe),
4645 mqe->un.mb_words[0], mqe->un.mb_words[1],
4646 mqe->un.mb_words[2], mqe->un.mb_words[3],
4647 mqe->un.mb_words[4], mqe->un.mb_words[5],
4648 mqe->un.mb_words[6], mqe->un.mb_words[7],
4649 mqe->un.mb_words[8], mqe->un.mb_words[9],
4650 mqe->un.mb_words[10], mqe->un.mb_words[11],
4651 mqe->un.mb_words[12], mqe->un.mb_words[13],
4652 mqe->un.mb_words[14], mqe->un.mb_words[15],
4653 mqe->un.mb_words[16], mqe->un.mb_words[50],
4654 mboxq->mcqe.word0,
4655 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4656 mboxq->mcqe.trailer);
4657
4658 if (rc) {
4659 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4660 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004661 rc = -EIO;
4662 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004663 }
4664 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004665 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004666 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4667 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004668 rc = -EIO;
4669 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004670 }
James Smartda0436e2009-05-22 14:51:39 -04004671
4672 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4673 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4674 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004675 rc = 0;
4676
4677out_free_mboxq:
4678 mempool_free(mboxq, phba->mbox_mem_pool);
4679 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004680}
4681
4682/**
4683 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4684 * @phba: pointer to lpfc hba data structure.
4685 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4686 * @vpd: pointer to the memory to hold resulting port vpd data.
4687 * @vpd_size: On input, the number of bytes allocated to @vpd.
4688 * On output, the number of data bytes in @vpd.
4689 *
4690 * This routine executes a READ_REV SLI4 mailbox command. In
4691 * addition, this routine gets the port vpd data.
4692 *
4693 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004694 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004695 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004696 **/
4697static int
4698lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4699 uint8_t *vpd, uint32_t *vpd_size)
4700{
4701 int rc = 0;
4702 uint32_t dma_size;
4703 struct lpfc_dmabuf *dmabuf;
4704 struct lpfc_mqe *mqe;
4705
4706 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4707 if (!dmabuf)
4708 return -ENOMEM;
4709
4710 /*
4711 * Get a DMA buffer for the vpd data resulting from the READ_REV
4712 * mailbox command.
4713 */
4714 dma_size = *vpd_size;
4715 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4716 dma_size,
4717 &dmabuf->phys,
4718 GFP_KERNEL);
4719 if (!dmabuf->virt) {
4720 kfree(dmabuf);
4721 return -ENOMEM;
4722 }
4723 memset(dmabuf->virt, 0, dma_size);
4724
4725 /*
4726 * The SLI4 implementation of READ_REV conflicts at word1,
4727 * bits 31:16 and SLI4 adds vpd functionality not present
4728 * in SLI3. This code corrects the conflicts.
4729 */
4730 lpfc_read_rev(phba, mboxq);
4731 mqe = &mboxq->u.mqe;
4732 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4733 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4734 mqe->un.read_rev.word1 &= 0x0000FFFF;
4735 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4736 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4737
4738 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4739 if (rc) {
4740 dma_free_coherent(&phba->pcidev->dev, dma_size,
4741 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004742 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004743 return -EIO;
4744 }
4745
James Smartda0436e2009-05-22 14:51:39 -04004746 /*
4747 * The available vpd length cannot be bigger than the
4748 * DMA buffer passed to the port. Catch the less than
4749 * case and update the caller's size.
4750 */
4751 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4752 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4753
James Smartd7c47992010-06-08 18:31:54 -04004754 memcpy(vpd, dmabuf->virt, *vpd_size);
4755
James Smartda0436e2009-05-22 14:51:39 -04004756 dma_free_coherent(&phba->pcidev->dev, dma_size,
4757 dmabuf->virt, dmabuf->phys);
4758 kfree(dmabuf);
4759 return 0;
4760}
4761
4762/**
James Smartcd1c8302011-10-10 21:33:25 -04004763 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4764 * @phba: pointer to lpfc hba data structure.
4765 *
4766 * This routine retrieves SLI4 device physical port name this PCI function
4767 * is attached to.
4768 *
4769 * Return codes
4770 * 0 - sucessful
4771 * otherwise - failed to retrieve physical port name
4772 **/
4773static int
4774lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4775{
4776 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004777 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4778 struct lpfc_controller_attribute *cntl_attr;
4779 struct lpfc_mbx_get_port_name *get_port_name;
4780 void *virtaddr = NULL;
4781 uint32_t alloclen, reqlen;
4782 uint32_t shdr_status, shdr_add_status;
4783 union lpfc_sli4_cfg_shdr *shdr;
4784 char cport_name = 0;
4785 int rc;
4786
4787 /* We assume nothing at this point */
4788 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4789 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4790
4791 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4792 if (!mboxq)
4793 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004794 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004795 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4796 lpfc_sli4_read_config(phba);
4797 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4798 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004799
4800 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4801 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4802 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4803 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4804 LPFC_SLI4_MBX_NEMBED);
4805 if (alloclen < reqlen) {
4806 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4807 "3084 Allocated DMA memory size (%d) is "
4808 "less than the requested DMA memory size "
4809 "(%d)\n", alloclen, reqlen);
4810 rc = -ENOMEM;
4811 goto out_free_mboxq;
4812 }
4813 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4814 virtaddr = mboxq->sge_array->addr[0];
4815 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4816 shdr = &mbx_cntl_attr->cfg_shdr;
4817 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4818 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4819 if (shdr_status || shdr_add_status || rc) {
4820 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4821 "3085 Mailbox x%x (x%x/x%x) failed, "
4822 "rc:x%x, status:x%x, add_status:x%x\n",
4823 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4824 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4825 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4826 rc, shdr_status, shdr_add_status);
4827 rc = -ENXIO;
4828 goto out_free_mboxq;
4829 }
4830 cntl_attr = &mbx_cntl_attr->cntl_attr;
4831 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4832 phba->sli4_hba.lnk_info.lnk_tp =
4833 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4834 phba->sli4_hba.lnk_info.lnk_no =
4835 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4836 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4837 "3086 lnk_type:%d, lnk_numb:%d\n",
4838 phba->sli4_hba.lnk_info.lnk_tp,
4839 phba->sli4_hba.lnk_info.lnk_no);
4840
4841retrieve_ppname:
4842 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4843 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4844 sizeof(struct lpfc_mbx_get_port_name) -
4845 sizeof(struct lpfc_sli4_cfg_mhdr),
4846 LPFC_SLI4_MBX_EMBED);
4847 get_port_name = &mboxq->u.mqe.un.get_port_name;
4848 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4849 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4850 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4851 phba->sli4_hba.lnk_info.lnk_tp);
4852 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4853 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4854 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4855 if (shdr_status || shdr_add_status || rc) {
4856 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4857 "3087 Mailbox x%x (x%x/x%x) failed: "
4858 "rc:x%x, status:x%x, add_status:x%x\n",
4859 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4860 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4861 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4862 rc, shdr_status, shdr_add_status);
4863 rc = -ENXIO;
4864 goto out_free_mboxq;
4865 }
4866 switch (phba->sli4_hba.lnk_info.lnk_no) {
4867 case LPFC_LINK_NUMBER_0:
4868 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4869 &get_port_name->u.response);
4870 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4871 break;
4872 case LPFC_LINK_NUMBER_1:
4873 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4874 &get_port_name->u.response);
4875 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4876 break;
4877 case LPFC_LINK_NUMBER_2:
4878 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4879 &get_port_name->u.response);
4880 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4881 break;
4882 case LPFC_LINK_NUMBER_3:
4883 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4884 &get_port_name->u.response);
4885 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4886 break;
4887 default:
4888 break;
4889 }
4890
4891 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4892 phba->Port[0] = cport_name;
4893 phba->Port[1] = '\0';
4894 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4895 "3091 SLI get port name: %s\n", phba->Port);
4896 }
4897
4898out_free_mboxq:
4899 if (rc != MBX_TIMEOUT) {
4900 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4901 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4902 else
4903 mempool_free(mboxq, phba->mbox_mem_pool);
4904 }
4905 return rc;
4906}
4907
4908/**
James Smartda0436e2009-05-22 14:51:39 -04004909 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4910 * @phba: pointer to lpfc hba data structure.
4911 *
4912 * This routine is called to explicitly arm the SLI4 device's completion and
4913 * event queues
4914 **/
4915static void
4916lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4917{
4918 uint8_t fcp_eqidx;
4919
4920 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4921 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004922 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004923 if (phba->sli4_hba.fcp_cq) {
4924 do
4925 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4926 LPFC_QUEUE_REARM);
4927 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4928 }
James Smartda0436e2009-05-22 14:51:39 -04004929 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
James Smart2e90f4b2011-12-13 13:22:37 -05004930 if (phba->sli4_hba.fp_eq) {
4931 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
4932 fcp_eqidx++)
4933 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4934 LPFC_QUEUE_REARM);
4935 }
James Smartda0436e2009-05-22 14:51:39 -04004936}
4937
4938/**
James Smart6d368e52011-05-24 11:44:12 -04004939 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4940 * @phba: Pointer to HBA context object.
4941 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004942 * @extnt_count: buffer to hold port available extent count.
4943 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004944 *
James Smartb76f2dc2011-07-22 18:37:42 -04004945 * This function calls the port and retrievs the number of available
4946 * extents and their size for a particular extent type.
4947 *
4948 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004949 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004950int
James Smart6d368e52011-05-24 11:44:12 -04004951lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4952 uint16_t *extnt_count, uint16_t *extnt_size)
4953{
4954 int rc = 0;
4955 uint32_t length;
4956 uint32_t mbox_tmo;
4957 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4958 LPFC_MBOXQ_t *mbox;
4959
4960 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4961 if (!mbox)
4962 return -ENOMEM;
4963
4964 /* Find out how many extents are available for this resource type */
4965 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4966 sizeof(struct lpfc_sli4_cfg_mhdr));
4967 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4968 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4969 length, LPFC_SLI4_MBX_EMBED);
4970
4971 /* Send an extents count of 0 - the GET doesn't use it. */
4972 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4973 LPFC_SLI4_MBX_EMBED);
4974 if (unlikely(rc)) {
4975 rc = -EIO;
4976 goto err_exit;
4977 }
4978
4979 if (!phba->sli4_hba.intr_enable)
4980 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4981 else {
James Smarta183a152011-10-10 21:32:43 -04004982 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004983 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4984 }
4985 if (unlikely(rc)) {
4986 rc = -EIO;
4987 goto err_exit;
4988 }
4989
4990 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4991 if (bf_get(lpfc_mbox_hdr_status,
4992 &rsrc_info->header.cfg_shdr.response)) {
4993 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4994 "2930 Failed to get resource extents "
4995 "Status 0x%x Add'l Status 0x%x\n",
4996 bf_get(lpfc_mbox_hdr_status,
4997 &rsrc_info->header.cfg_shdr.response),
4998 bf_get(lpfc_mbox_hdr_add_status,
4999 &rsrc_info->header.cfg_shdr.response));
5000 rc = -EIO;
5001 goto err_exit;
5002 }
5003
5004 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5005 &rsrc_info->u.rsp);
5006 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5007 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005008
5009 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5010 "3162 Retrieved extents type-%d from port: count:%d, "
5011 "size:%d\n", type, *extnt_count, *extnt_size);
5012
5013err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005014 mempool_free(mbox, phba->mbox_mem_pool);
5015 return rc;
5016}
5017
5018/**
5019 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5020 * @phba: Pointer to HBA context object.
5021 * @type: The extent type to check.
5022 *
5023 * This function reads the current available extents from the port and checks
5024 * if the extent count or extent size has changed since the last access.
5025 * Callers use this routine post port reset to understand if there is a
5026 * extent reprovisioning requirement.
5027 *
5028 * Returns:
5029 * -Error: error indicates problem.
5030 * 1: Extent count or size has changed.
5031 * 0: No changes.
5032 **/
5033static int
5034lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5035{
5036 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5037 uint16_t size_diff, rsrc_ext_size;
5038 int rc = 0;
5039 struct lpfc_rsrc_blks *rsrc_entry;
5040 struct list_head *rsrc_blk_list = NULL;
5041
5042 size_diff = 0;
5043 curr_ext_cnt = 0;
5044 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5045 &rsrc_ext_cnt,
5046 &rsrc_ext_size);
5047 if (unlikely(rc))
5048 return -EIO;
5049
5050 switch (type) {
5051 case LPFC_RSC_TYPE_FCOE_RPI:
5052 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5053 break;
5054 case LPFC_RSC_TYPE_FCOE_VPI:
5055 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5056 break;
5057 case LPFC_RSC_TYPE_FCOE_XRI:
5058 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5059 break;
5060 case LPFC_RSC_TYPE_FCOE_VFI:
5061 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5062 break;
5063 default:
5064 break;
5065 }
5066
5067 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5068 curr_ext_cnt++;
5069 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5070 size_diff++;
5071 }
5072
5073 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5074 rc = 1;
5075
5076 return rc;
5077}
5078
5079/**
5080 * lpfc_sli4_cfg_post_extnts -
5081 * @phba: Pointer to HBA context object.
5082 * @extnt_cnt - number of available extents.
5083 * @type - the extent type (rpi, xri, vfi, vpi).
5084 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5085 * @mbox - pointer to the caller's allocated mailbox structure.
5086 *
5087 * This function executes the extents allocation request. It also
5088 * takes care of the amount of memory needed to allocate or get the
5089 * allocated extents. It is the caller's responsibility to evaluate
5090 * the response.
5091 *
5092 * Returns:
5093 * -Error: Error value describes the condition found.
5094 * 0: if successful
5095 **/
5096static int
James Smart8a9d2e82012-05-09 21:16:12 -04005097lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005098 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5099{
5100 int rc = 0;
5101 uint32_t req_len;
5102 uint32_t emb_len;
5103 uint32_t alloc_len, mbox_tmo;
5104
5105 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005106 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005107
5108 /*
5109 * Calculate the size of an embedded mailbox. The uint32_t
5110 * accounts for extents-specific word.
5111 */
5112 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5113 sizeof(uint32_t);
5114
5115 /*
5116 * Presume the allocation and response will fit into an embedded
5117 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5118 */
5119 *emb = LPFC_SLI4_MBX_EMBED;
5120 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005121 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005122 sizeof(union lpfc_sli4_cfg_shdr) +
5123 sizeof(uint32_t);
5124 *emb = LPFC_SLI4_MBX_NEMBED;
5125 }
5126
5127 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5128 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5129 req_len, *emb);
5130 if (alloc_len < req_len) {
5131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005132 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005133 "less than the requested DMA memory "
5134 "size (x%x)\n", alloc_len, req_len);
5135 return -ENOMEM;
5136 }
James Smart8a9d2e82012-05-09 21:16:12 -04005137 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005138 if (unlikely(rc))
5139 return -EIO;
5140
5141 if (!phba->sli4_hba.intr_enable)
5142 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5143 else {
James Smarta183a152011-10-10 21:32:43 -04005144 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005145 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5146 }
5147
5148 if (unlikely(rc))
5149 rc = -EIO;
5150 return rc;
5151}
5152
5153/**
5154 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5155 * @phba: Pointer to HBA context object.
5156 * @type: The resource extent type to allocate.
5157 *
5158 * This function allocates the number of elements for the specified
5159 * resource type.
5160 **/
5161static int
5162lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5163{
5164 bool emb = false;
5165 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5166 uint16_t rsrc_id, rsrc_start, j, k;
5167 uint16_t *ids;
5168 int i, rc;
5169 unsigned long longs;
5170 unsigned long *bmask;
5171 struct lpfc_rsrc_blks *rsrc_blks;
5172 LPFC_MBOXQ_t *mbox;
5173 uint32_t length;
5174 struct lpfc_id_range *id_array = NULL;
5175 void *virtaddr = NULL;
5176 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5177 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5178 struct list_head *ext_blk_list;
5179
5180 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5181 &rsrc_cnt,
5182 &rsrc_size);
5183 if (unlikely(rc))
5184 return -EIO;
5185
5186 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5187 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5188 "3009 No available Resource Extents "
5189 "for resource type 0x%x: Count: 0x%x, "
5190 "Size 0x%x\n", type, rsrc_cnt,
5191 rsrc_size);
5192 return -ENOMEM;
5193 }
5194
James Smart8a9d2e82012-05-09 21:16:12 -04005195 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5196 "2903 Post resource extents type-0x%x: "
5197 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005198
5199 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5200 if (!mbox)
5201 return -ENOMEM;
5202
James Smart8a9d2e82012-05-09 21:16:12 -04005203 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005204 if (unlikely(rc)) {
5205 rc = -EIO;
5206 goto err_exit;
5207 }
5208
5209 /*
5210 * Figure out where the response is located. Then get local pointers
5211 * to the response data. The port does not guarantee to respond to
5212 * all extents counts request so update the local variable with the
5213 * allocated count from the port.
5214 */
5215 if (emb == LPFC_SLI4_MBX_EMBED) {
5216 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5217 id_array = &rsrc_ext->u.rsp.id[0];
5218 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5219 } else {
5220 virtaddr = mbox->sge_array->addr[0];
5221 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5222 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5223 id_array = &n_rsrc->id;
5224 }
5225
5226 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5227 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5228
5229 /*
5230 * Based on the resource size and count, correct the base and max
5231 * resource values.
5232 */
5233 length = sizeof(struct lpfc_rsrc_blks);
5234 switch (type) {
5235 case LPFC_RSC_TYPE_FCOE_RPI:
5236 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5237 sizeof(unsigned long),
5238 GFP_KERNEL);
5239 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5240 rc = -ENOMEM;
5241 goto err_exit;
5242 }
5243 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5244 sizeof(uint16_t),
5245 GFP_KERNEL);
5246 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5247 kfree(phba->sli4_hba.rpi_bmask);
5248 rc = -ENOMEM;
5249 goto err_exit;
5250 }
5251
5252 /*
5253 * The next_rpi was initialized with the maximum available
5254 * count but the port may allocate a smaller number. Catch
5255 * that case and update the next_rpi.
5256 */
5257 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5258
5259 /* Initialize local ptrs for common extent processing later. */
5260 bmask = phba->sli4_hba.rpi_bmask;
5261 ids = phba->sli4_hba.rpi_ids;
5262 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5263 break;
5264 case LPFC_RSC_TYPE_FCOE_VPI:
5265 phba->vpi_bmask = kzalloc(longs *
5266 sizeof(unsigned long),
5267 GFP_KERNEL);
5268 if (unlikely(!phba->vpi_bmask)) {
5269 rc = -ENOMEM;
5270 goto err_exit;
5271 }
5272 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5273 sizeof(uint16_t),
5274 GFP_KERNEL);
5275 if (unlikely(!phba->vpi_ids)) {
5276 kfree(phba->vpi_bmask);
5277 rc = -ENOMEM;
5278 goto err_exit;
5279 }
5280
5281 /* Initialize local ptrs for common extent processing later. */
5282 bmask = phba->vpi_bmask;
5283 ids = phba->vpi_ids;
5284 ext_blk_list = &phba->lpfc_vpi_blk_list;
5285 break;
5286 case LPFC_RSC_TYPE_FCOE_XRI:
5287 phba->sli4_hba.xri_bmask = kzalloc(longs *
5288 sizeof(unsigned long),
5289 GFP_KERNEL);
5290 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5291 rc = -ENOMEM;
5292 goto err_exit;
5293 }
James Smart8a9d2e82012-05-09 21:16:12 -04005294 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005295 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5296 sizeof(uint16_t),
5297 GFP_KERNEL);
5298 if (unlikely(!phba->sli4_hba.xri_ids)) {
5299 kfree(phba->sli4_hba.xri_bmask);
5300 rc = -ENOMEM;
5301 goto err_exit;
5302 }
5303
5304 /* Initialize local ptrs for common extent processing later. */
5305 bmask = phba->sli4_hba.xri_bmask;
5306 ids = phba->sli4_hba.xri_ids;
5307 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5308 break;
5309 case LPFC_RSC_TYPE_FCOE_VFI:
5310 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5311 sizeof(unsigned long),
5312 GFP_KERNEL);
5313 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5314 rc = -ENOMEM;
5315 goto err_exit;
5316 }
5317 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5318 sizeof(uint16_t),
5319 GFP_KERNEL);
5320 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5321 kfree(phba->sli4_hba.vfi_bmask);
5322 rc = -ENOMEM;
5323 goto err_exit;
5324 }
5325
5326 /* Initialize local ptrs for common extent processing later. */
5327 bmask = phba->sli4_hba.vfi_bmask;
5328 ids = phba->sli4_hba.vfi_ids;
5329 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5330 break;
5331 default:
5332 /* Unsupported Opcode. Fail call. */
5333 id_array = NULL;
5334 bmask = NULL;
5335 ids = NULL;
5336 ext_blk_list = NULL;
5337 goto err_exit;
5338 }
5339
5340 /*
5341 * Complete initializing the extent configuration with the
5342 * allocated ids assigned to this function. The bitmask serves
5343 * as an index into the array and manages the available ids. The
5344 * array just stores the ids communicated to the port via the wqes.
5345 */
5346 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5347 if ((i % 2) == 0)
5348 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5349 &id_array[k]);
5350 else
5351 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5352 &id_array[k]);
5353
5354 rsrc_blks = kzalloc(length, GFP_KERNEL);
5355 if (unlikely(!rsrc_blks)) {
5356 rc = -ENOMEM;
5357 kfree(bmask);
5358 kfree(ids);
5359 goto err_exit;
5360 }
5361 rsrc_blks->rsrc_start = rsrc_id;
5362 rsrc_blks->rsrc_size = rsrc_size;
5363 list_add_tail(&rsrc_blks->list, ext_blk_list);
5364 rsrc_start = rsrc_id;
5365 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5366 phba->sli4_hba.scsi_xri_start = rsrc_start +
5367 lpfc_sli4_get_els_iocb_cnt(phba);
5368
5369 while (rsrc_id < (rsrc_start + rsrc_size)) {
5370 ids[j] = rsrc_id;
5371 rsrc_id++;
5372 j++;
5373 }
5374 /* Entire word processed. Get next word.*/
5375 if ((i % 2) == 1)
5376 k++;
5377 }
5378 err_exit:
5379 lpfc_sli4_mbox_cmd_free(phba, mbox);
5380 return rc;
5381}
5382
5383/**
5384 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5385 * @phba: Pointer to HBA context object.
5386 * @type: the extent's type.
5387 *
5388 * This function deallocates all extents of a particular resource type.
5389 * SLI4 does not allow for deallocating a particular extent range. It
5390 * is the caller's responsibility to release all kernel memory resources.
5391 **/
5392static int
5393lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5394{
5395 int rc;
5396 uint32_t length, mbox_tmo = 0;
5397 LPFC_MBOXQ_t *mbox;
5398 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5399 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5400
5401 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5402 if (!mbox)
5403 return -ENOMEM;
5404
5405 /*
5406 * This function sends an embedded mailbox because it only sends the
5407 * the resource type. All extents of this type are released by the
5408 * port.
5409 */
5410 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5411 sizeof(struct lpfc_sli4_cfg_mhdr));
5412 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5413 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5414 length, LPFC_SLI4_MBX_EMBED);
5415
5416 /* Send an extents count of 0 - the dealloc doesn't use it. */
5417 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5418 LPFC_SLI4_MBX_EMBED);
5419 if (unlikely(rc)) {
5420 rc = -EIO;
5421 goto out_free_mbox;
5422 }
5423 if (!phba->sli4_hba.intr_enable)
5424 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5425 else {
James Smarta183a152011-10-10 21:32:43 -04005426 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005427 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5428 }
5429 if (unlikely(rc)) {
5430 rc = -EIO;
5431 goto out_free_mbox;
5432 }
5433
5434 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5435 if (bf_get(lpfc_mbox_hdr_status,
5436 &dealloc_rsrc->header.cfg_shdr.response)) {
5437 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5438 "2919 Failed to release resource extents "
5439 "for type %d - Status 0x%x Add'l Status 0x%x. "
5440 "Resource memory not released.\n",
5441 type,
5442 bf_get(lpfc_mbox_hdr_status,
5443 &dealloc_rsrc->header.cfg_shdr.response),
5444 bf_get(lpfc_mbox_hdr_add_status,
5445 &dealloc_rsrc->header.cfg_shdr.response));
5446 rc = -EIO;
5447 goto out_free_mbox;
5448 }
5449
5450 /* Release kernel memory resources for the specific type. */
5451 switch (type) {
5452 case LPFC_RSC_TYPE_FCOE_VPI:
5453 kfree(phba->vpi_bmask);
5454 kfree(phba->vpi_ids);
5455 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5456 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5457 &phba->lpfc_vpi_blk_list, list) {
5458 list_del_init(&rsrc_blk->list);
5459 kfree(rsrc_blk);
5460 }
5461 break;
5462 case LPFC_RSC_TYPE_FCOE_XRI:
5463 kfree(phba->sli4_hba.xri_bmask);
5464 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005465 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5466 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5467 list_del_init(&rsrc_blk->list);
5468 kfree(rsrc_blk);
5469 }
5470 break;
5471 case LPFC_RSC_TYPE_FCOE_VFI:
5472 kfree(phba->sli4_hba.vfi_bmask);
5473 kfree(phba->sli4_hba.vfi_ids);
5474 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5475 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5476 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5477 list_del_init(&rsrc_blk->list);
5478 kfree(rsrc_blk);
5479 }
5480 break;
5481 case LPFC_RSC_TYPE_FCOE_RPI:
5482 /* RPI bitmask and physical id array are cleaned up earlier. */
5483 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5484 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5485 list_del_init(&rsrc_blk->list);
5486 kfree(rsrc_blk);
5487 }
5488 break;
5489 default:
5490 break;
5491 }
5492
5493 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5494
5495 out_free_mbox:
5496 mempool_free(mbox, phba->mbox_mem_pool);
5497 return rc;
5498}
5499
5500/**
5501 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5502 * @phba: Pointer to HBA context object.
5503 *
5504 * This function allocates all SLI4 resource identifiers.
5505 **/
5506int
5507lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5508{
5509 int i, rc, error = 0;
5510 uint16_t count, base;
5511 unsigned long longs;
5512
James Smartff78d8f2011-12-13 13:21:35 -05005513 if (!phba->sli4_hba.rpi_hdrs_in_use)
5514 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005515 if (phba->sli4_hba.extents_in_use) {
5516 /*
5517 * The port supports resource extents. The XRI, VPI, VFI, RPI
5518 * resource extent count must be read and allocated before
5519 * provisioning the resource id arrays.
5520 */
5521 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5522 LPFC_IDX_RSRC_RDY) {
5523 /*
5524 * Extent-based resources are set - the driver could
5525 * be in a port reset. Figure out if any corrective
5526 * actions need to be taken.
5527 */
5528 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5529 LPFC_RSC_TYPE_FCOE_VFI);
5530 if (rc != 0)
5531 error++;
5532 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5533 LPFC_RSC_TYPE_FCOE_VPI);
5534 if (rc != 0)
5535 error++;
5536 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5537 LPFC_RSC_TYPE_FCOE_XRI);
5538 if (rc != 0)
5539 error++;
5540 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5541 LPFC_RSC_TYPE_FCOE_RPI);
5542 if (rc != 0)
5543 error++;
5544
5545 /*
5546 * It's possible that the number of resources
5547 * provided to this port instance changed between
5548 * resets. Detect this condition and reallocate
5549 * resources. Otherwise, there is no action.
5550 */
5551 if (error) {
5552 lpfc_printf_log(phba, KERN_INFO,
5553 LOG_MBOX | LOG_INIT,
5554 "2931 Detected extent resource "
5555 "change. Reallocating all "
5556 "extents.\n");
5557 rc = lpfc_sli4_dealloc_extent(phba,
5558 LPFC_RSC_TYPE_FCOE_VFI);
5559 rc = lpfc_sli4_dealloc_extent(phba,
5560 LPFC_RSC_TYPE_FCOE_VPI);
5561 rc = lpfc_sli4_dealloc_extent(phba,
5562 LPFC_RSC_TYPE_FCOE_XRI);
5563 rc = lpfc_sli4_dealloc_extent(phba,
5564 LPFC_RSC_TYPE_FCOE_RPI);
5565 } else
5566 return 0;
5567 }
5568
5569 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5570 if (unlikely(rc))
5571 goto err_exit;
5572
5573 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5574 if (unlikely(rc))
5575 goto err_exit;
5576
5577 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5578 if (unlikely(rc))
5579 goto err_exit;
5580
5581 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5582 if (unlikely(rc))
5583 goto err_exit;
5584 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5585 LPFC_IDX_RSRC_RDY);
5586 return rc;
5587 } else {
5588 /*
5589 * The port does not support resource extents. The XRI, VPI,
5590 * VFI, RPI resource ids were determined from READ_CONFIG.
5591 * Just allocate the bitmasks and provision the resource id
5592 * arrays. If a port reset is active, the resources don't
5593 * need any action - just exit.
5594 */
5595 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005596 LPFC_IDX_RSRC_RDY) {
5597 lpfc_sli4_dealloc_resource_identifiers(phba);
5598 lpfc_sli4_remove_rpis(phba);
5599 }
James Smart6d368e52011-05-24 11:44:12 -04005600 /* RPIs. */
5601 count = phba->sli4_hba.max_cfg_param.max_rpi;
5602 base = phba->sli4_hba.max_cfg_param.rpi_base;
5603 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5604 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5605 sizeof(unsigned long),
5606 GFP_KERNEL);
5607 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5608 rc = -ENOMEM;
5609 goto err_exit;
5610 }
5611 phba->sli4_hba.rpi_ids = kzalloc(count *
5612 sizeof(uint16_t),
5613 GFP_KERNEL);
5614 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5615 rc = -ENOMEM;
5616 goto free_rpi_bmask;
5617 }
5618
5619 for (i = 0; i < count; i++)
5620 phba->sli4_hba.rpi_ids[i] = base + i;
5621
5622 /* VPIs. */
5623 count = phba->sli4_hba.max_cfg_param.max_vpi;
5624 base = phba->sli4_hba.max_cfg_param.vpi_base;
5625 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5626 phba->vpi_bmask = kzalloc(longs *
5627 sizeof(unsigned long),
5628 GFP_KERNEL);
5629 if (unlikely(!phba->vpi_bmask)) {
5630 rc = -ENOMEM;
5631 goto free_rpi_ids;
5632 }
5633 phba->vpi_ids = kzalloc(count *
5634 sizeof(uint16_t),
5635 GFP_KERNEL);
5636 if (unlikely(!phba->vpi_ids)) {
5637 rc = -ENOMEM;
5638 goto free_vpi_bmask;
5639 }
5640
5641 for (i = 0; i < count; i++)
5642 phba->vpi_ids[i] = base + i;
5643
5644 /* XRIs. */
5645 count = phba->sli4_hba.max_cfg_param.max_xri;
5646 base = phba->sli4_hba.max_cfg_param.xri_base;
5647 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5648 phba->sli4_hba.xri_bmask = kzalloc(longs *
5649 sizeof(unsigned long),
5650 GFP_KERNEL);
5651 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5652 rc = -ENOMEM;
5653 goto free_vpi_ids;
5654 }
James Smart41899be2012-03-01 22:34:19 -05005655 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005656 phba->sli4_hba.xri_ids = kzalloc(count *
5657 sizeof(uint16_t),
5658 GFP_KERNEL);
5659 if (unlikely(!phba->sli4_hba.xri_ids)) {
5660 rc = -ENOMEM;
5661 goto free_xri_bmask;
5662 }
5663
5664 for (i = 0; i < count; i++)
5665 phba->sli4_hba.xri_ids[i] = base + i;
5666
5667 /* VFIs. */
5668 count = phba->sli4_hba.max_cfg_param.max_vfi;
5669 base = phba->sli4_hba.max_cfg_param.vfi_base;
5670 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5671 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5672 sizeof(unsigned long),
5673 GFP_KERNEL);
5674 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5675 rc = -ENOMEM;
5676 goto free_xri_ids;
5677 }
5678 phba->sli4_hba.vfi_ids = kzalloc(count *
5679 sizeof(uint16_t),
5680 GFP_KERNEL);
5681 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5682 rc = -ENOMEM;
5683 goto free_vfi_bmask;
5684 }
5685
5686 for (i = 0; i < count; i++)
5687 phba->sli4_hba.vfi_ids[i] = base + i;
5688
5689 /*
5690 * Mark all resources ready. An HBA reset doesn't need
5691 * to reset the initialization.
5692 */
5693 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5694 LPFC_IDX_RSRC_RDY);
5695 return 0;
5696 }
5697
5698 free_vfi_bmask:
5699 kfree(phba->sli4_hba.vfi_bmask);
5700 free_xri_ids:
5701 kfree(phba->sli4_hba.xri_ids);
5702 free_xri_bmask:
5703 kfree(phba->sli4_hba.xri_bmask);
5704 free_vpi_ids:
5705 kfree(phba->vpi_ids);
5706 free_vpi_bmask:
5707 kfree(phba->vpi_bmask);
5708 free_rpi_ids:
5709 kfree(phba->sli4_hba.rpi_ids);
5710 free_rpi_bmask:
5711 kfree(phba->sli4_hba.rpi_bmask);
5712 err_exit:
5713 return rc;
5714}
5715
5716/**
5717 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5718 * @phba: Pointer to HBA context object.
5719 *
5720 * This function allocates the number of elements for the specified
5721 * resource type.
5722 **/
5723int
5724lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5725{
5726 if (phba->sli4_hba.extents_in_use) {
5727 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5728 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5729 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5730 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5731 } else {
5732 kfree(phba->vpi_bmask);
5733 kfree(phba->vpi_ids);
5734 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5735 kfree(phba->sli4_hba.xri_bmask);
5736 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005737 kfree(phba->sli4_hba.vfi_bmask);
5738 kfree(phba->sli4_hba.vfi_ids);
5739 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5740 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5741 }
5742
5743 return 0;
5744}
5745
5746/**
James Smartb76f2dc2011-07-22 18:37:42 -04005747 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5748 * @phba: Pointer to HBA context object.
5749 * @type: The resource extent type.
5750 * @extnt_count: buffer to hold port extent count response
5751 * @extnt_size: buffer to hold port extent size response.
5752 *
5753 * This function calls the port to read the host allocated extents
5754 * for a particular type.
5755 **/
5756int
5757lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5758 uint16_t *extnt_cnt, uint16_t *extnt_size)
5759{
5760 bool emb;
5761 int rc = 0;
5762 uint16_t curr_blks = 0;
5763 uint32_t req_len, emb_len;
5764 uint32_t alloc_len, mbox_tmo;
5765 struct list_head *blk_list_head;
5766 struct lpfc_rsrc_blks *rsrc_blk;
5767 LPFC_MBOXQ_t *mbox;
5768 void *virtaddr = NULL;
5769 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5770 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5771 union lpfc_sli4_cfg_shdr *shdr;
5772
5773 switch (type) {
5774 case LPFC_RSC_TYPE_FCOE_VPI:
5775 blk_list_head = &phba->lpfc_vpi_blk_list;
5776 break;
5777 case LPFC_RSC_TYPE_FCOE_XRI:
5778 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5779 break;
5780 case LPFC_RSC_TYPE_FCOE_VFI:
5781 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5782 break;
5783 case LPFC_RSC_TYPE_FCOE_RPI:
5784 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5785 break;
5786 default:
5787 return -EIO;
5788 }
5789
5790 /* Count the number of extents currently allocatd for this type. */
5791 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5792 if (curr_blks == 0) {
5793 /*
5794 * The GET_ALLOCATED mailbox does not return the size,
5795 * just the count. The size should be just the size
5796 * stored in the current allocated block and all sizes
5797 * for an extent type are the same so set the return
5798 * value now.
5799 */
5800 *extnt_size = rsrc_blk->rsrc_size;
5801 }
5802 curr_blks++;
5803 }
5804
5805 /* Calculate the total requested length of the dma memory. */
5806 req_len = curr_blks * sizeof(uint16_t);
5807
5808 /*
5809 * Calculate the size of an embedded mailbox. The uint32_t
5810 * accounts for extents-specific word.
5811 */
5812 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5813 sizeof(uint32_t);
5814
5815 /*
5816 * Presume the allocation and response will fit into an embedded
5817 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5818 */
5819 emb = LPFC_SLI4_MBX_EMBED;
5820 req_len = emb_len;
5821 if (req_len > emb_len) {
5822 req_len = curr_blks * sizeof(uint16_t) +
5823 sizeof(union lpfc_sli4_cfg_shdr) +
5824 sizeof(uint32_t);
5825 emb = LPFC_SLI4_MBX_NEMBED;
5826 }
5827
5828 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5829 if (!mbox)
5830 return -ENOMEM;
5831 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5832
5833 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5834 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5835 req_len, emb);
5836 if (alloc_len < req_len) {
5837 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5838 "2983 Allocated DMA memory size (x%x) is "
5839 "less than the requested DMA memory "
5840 "size (x%x)\n", alloc_len, req_len);
5841 rc = -ENOMEM;
5842 goto err_exit;
5843 }
5844 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5845 if (unlikely(rc)) {
5846 rc = -EIO;
5847 goto err_exit;
5848 }
5849
5850 if (!phba->sli4_hba.intr_enable)
5851 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5852 else {
James Smarta183a152011-10-10 21:32:43 -04005853 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005854 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5855 }
5856
5857 if (unlikely(rc)) {
5858 rc = -EIO;
5859 goto err_exit;
5860 }
5861
5862 /*
5863 * Figure out where the response is located. Then get local pointers
5864 * to the response data. The port does not guarantee to respond to
5865 * all extents counts request so update the local variable with the
5866 * allocated count from the port.
5867 */
5868 if (emb == LPFC_SLI4_MBX_EMBED) {
5869 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5870 shdr = &rsrc_ext->header.cfg_shdr;
5871 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5872 } else {
5873 virtaddr = mbox->sge_array->addr[0];
5874 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5875 shdr = &n_rsrc->cfg_shdr;
5876 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5877 }
5878
5879 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5880 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5881 "2984 Failed to read allocated resources "
5882 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5883 type,
5884 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5885 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5886 rc = -EIO;
5887 goto err_exit;
5888 }
5889 err_exit:
5890 lpfc_sli4_mbox_cmd_free(phba, mbox);
5891 return rc;
5892}
5893
5894/**
James Smart8a9d2e82012-05-09 21:16:12 -04005895 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5896 * @phba: pointer to lpfc hba data structure.
5897 *
5898 * This routine walks the list of els buffers that have been allocated and
5899 * repost them to the port by using SGL block post. This is needed after a
5900 * pci_function_reset/warm_start or start. It attempts to construct blocks
5901 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5902 * SGL block post mailbox commands to post them to the port. For single els
5903 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5904 * mailbox command for posting.
5905 *
5906 * Returns: 0 = success, non-zero failure.
5907 **/
5908static int
5909lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5910{
5911 struct lpfc_sglq *sglq_entry = NULL;
5912 struct lpfc_sglq *sglq_entry_next = NULL;
5913 struct lpfc_sglq *sglq_entry_first = NULL;
5914 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5915 int last_xritag = NO_XRI;
5916 LIST_HEAD(prep_sgl_list);
5917 LIST_HEAD(blck_sgl_list);
5918 LIST_HEAD(allc_sgl_list);
5919 LIST_HEAD(post_sgl_list);
5920 LIST_HEAD(free_sgl_list);
5921
5922 spin_lock(&phba->hbalock);
5923 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5924 spin_unlock(&phba->hbalock);
5925
5926 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5927 &allc_sgl_list, list) {
5928 list_del_init(&sglq_entry->list);
5929 block_cnt++;
5930 if ((last_xritag != NO_XRI) &&
5931 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5932 /* a hole in xri block, form a sgl posting block */
5933 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5934 post_cnt = block_cnt - 1;
5935 /* prepare list for next posting block */
5936 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5937 block_cnt = 1;
5938 } else {
5939 /* prepare list for next posting block */
5940 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5941 /* enough sgls for non-embed sgl mbox command */
5942 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5943 list_splice_init(&prep_sgl_list,
5944 &blck_sgl_list);
5945 post_cnt = block_cnt;
5946 block_cnt = 0;
5947 }
5948 }
5949 num_posted++;
5950
5951 /* keep track of last sgl's xritag */
5952 last_xritag = sglq_entry->sli4_xritag;
5953
5954 /* end of repost sgl list condition for els buffers */
5955 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5956 if (post_cnt == 0) {
5957 list_splice_init(&prep_sgl_list,
5958 &blck_sgl_list);
5959 post_cnt = block_cnt;
5960 } else if (block_cnt == 1) {
5961 status = lpfc_sli4_post_sgl(phba,
5962 sglq_entry->phys, 0,
5963 sglq_entry->sli4_xritag);
5964 if (!status) {
5965 /* successful, put sgl to posted list */
5966 list_add_tail(&sglq_entry->list,
5967 &post_sgl_list);
5968 } else {
5969 /* Failure, put sgl to free list */
5970 lpfc_printf_log(phba, KERN_WARNING,
5971 LOG_SLI,
5972 "3159 Failed to post els "
5973 "sgl, xritag:x%x\n",
5974 sglq_entry->sli4_xritag);
5975 list_add_tail(&sglq_entry->list,
5976 &free_sgl_list);
5977 spin_lock_irq(&phba->hbalock);
5978 phba->sli4_hba.els_xri_cnt--;
5979 spin_unlock_irq(&phba->hbalock);
5980 }
5981 }
5982 }
5983
5984 /* continue until a nembed page worth of sgls */
5985 if (post_cnt == 0)
5986 continue;
5987
5988 /* post the els buffer list sgls as a block */
5989 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
5990 post_cnt);
5991
5992 if (!status) {
5993 /* success, put sgl list to posted sgl list */
5994 list_splice_init(&blck_sgl_list, &post_sgl_list);
5995 } else {
5996 /* Failure, put sgl list to free sgl list */
5997 sglq_entry_first = list_first_entry(&blck_sgl_list,
5998 struct lpfc_sglq,
5999 list);
6000 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6001 "3160 Failed to post els sgl-list, "
6002 "xritag:x%x-x%x\n",
6003 sglq_entry_first->sli4_xritag,
6004 (sglq_entry_first->sli4_xritag +
6005 post_cnt - 1));
6006 list_splice_init(&blck_sgl_list, &free_sgl_list);
6007 spin_lock_irq(&phba->hbalock);
6008 phba->sli4_hba.els_xri_cnt -= post_cnt;
6009 spin_unlock_irq(&phba->hbalock);
6010 }
6011
6012 /* don't reset xirtag due to hole in xri block */
6013 if (block_cnt == 0)
6014 last_xritag = NO_XRI;
6015
6016 /* reset els sgl post count for next round of posting */
6017 post_cnt = 0;
6018 }
6019
6020 /* free the els sgls failed to post */
6021 lpfc_free_sgl_list(phba, &free_sgl_list);
6022
6023 /* push els sgls posted to the availble list */
6024 if (!list_empty(&post_sgl_list)) {
6025 spin_lock(&phba->hbalock);
6026 list_splice_init(&post_sgl_list,
6027 &phba->sli4_hba.lpfc_sgl_list);
6028 spin_unlock(&phba->hbalock);
6029 } else {
6030 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6031 "3161 Failure to post els sgl to port.\n");
6032 return -EIO;
6033 }
6034 return 0;
6035}
6036
6037/**
James Smartda0436e2009-05-22 14:51:39 -04006038 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6039 * @phba: Pointer to HBA context object.
6040 *
6041 * This function is the main SLI4 device intialization PCI function. This
6042 * function is called by the HBA intialization code, HBA reset code and
6043 * HBA error attention handler code. Caller is not required to hold any
6044 * locks.
6045 **/
6046int
6047lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6048{
6049 int rc;
6050 LPFC_MBOXQ_t *mboxq;
6051 struct lpfc_mqe *mqe;
6052 uint8_t *vpd;
6053 uint32_t vpd_size;
6054 uint32_t ftr_rsp = 0;
6055 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6056 struct lpfc_vport *vport = phba->pport;
6057 struct lpfc_dmabuf *mp;
6058
6059 /* Perform a PCI function reset to start from clean */
6060 rc = lpfc_pci_function_reset(phba);
6061 if (unlikely(rc))
6062 return -ENODEV;
6063
6064 /* Check the HBA Host Status Register for readyness */
6065 rc = lpfc_sli4_post_status_check(phba);
6066 if (unlikely(rc))
6067 return -ENODEV;
6068 else {
6069 spin_lock_irq(&phba->hbalock);
6070 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6071 spin_unlock_irq(&phba->hbalock);
6072 }
6073
6074 /*
6075 * Allocate a single mailbox container for initializing the
6076 * port.
6077 */
6078 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6079 if (!mboxq)
6080 return -ENOMEM;
6081
James Smartda0436e2009-05-22 14:51:39 -04006082 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006083 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006084 vpd = kzalloc(vpd_size, GFP_KERNEL);
6085 if (!vpd) {
6086 rc = -ENOMEM;
6087 goto out_free_mbox;
6088 }
6089
6090 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006091 if (unlikely(rc)) {
6092 kfree(vpd);
6093 goto out_free_mbox;
6094 }
James Smartda0436e2009-05-22 14:51:39 -04006095 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006096 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6097 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006098 phba->hba_flag |= HBA_FCOE_MODE;
6099 else
6100 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006101
6102 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6103 LPFC_DCBX_CEE_MODE)
6104 phba->hba_flag |= HBA_FIP_SUPPORT;
6105 else
6106 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6107
James Smart4f2e66c2012-05-09 21:17:07 -04006108 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6109
James Smartc31098c2011-04-16 11:03:33 -04006110 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006111 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6112 "0376 READ_REV Error. SLI Level %d "
6113 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006114 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006115 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006116 kfree(vpd);
6117 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006118 }
James Smartcd1c8302011-10-10 21:33:25 -04006119
6120 /*
James Smartff78d8f2011-12-13 13:21:35 -05006121 * Continue initialization with default values even if driver failed
6122 * to read FCoE param config regions, only read parameters if the
6123 * board is FCoE
6124 */
6125 if (phba->hba_flag & HBA_FCOE_MODE &&
6126 lpfc_sli4_read_fcoe_params(phba))
6127 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6128 "2570 Failed to read FCoE parameters\n");
6129
6130 /*
James Smartcd1c8302011-10-10 21:33:25 -04006131 * Retrieve sli4 device physical port name, failure of doing it
6132 * is considered as non-fatal.
6133 */
6134 rc = lpfc_sli4_retrieve_pport_name(phba);
6135 if (!rc)
6136 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6137 "3080 Successful retrieving SLI4 device "
6138 "physical port name: %s.\n", phba->Port);
6139
James Smartda0436e2009-05-22 14:51:39 -04006140 /*
6141 * Evaluate the read rev and vpd data. Populate the driver
6142 * state with the results. If this routine fails, the failure
6143 * is not fatal as the driver will use generic values.
6144 */
6145 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6146 if (unlikely(!rc)) {
6147 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6148 "0377 Error %d parsing vpd. "
6149 "Using defaults.\n", rc);
6150 rc = 0;
6151 }
James Smart76a95d72010-11-20 23:11:48 -05006152 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006153
James Smartf1126682009-06-10 17:22:44 -04006154 /* Save information as VPD data */
6155 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6156 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6157 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6158 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6159 &mqe->un.read_rev);
6160 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6161 &mqe->un.read_rev);
6162 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6163 &mqe->un.read_rev);
6164 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6165 &mqe->un.read_rev);
6166 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6167 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6168 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6169 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6170 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6171 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6172 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6173 "(%d):0380 READ_REV Status x%x "
6174 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6175 mboxq->vport ? mboxq->vport->vpi : 0,
6176 bf_get(lpfc_mqe_status, mqe),
6177 phba->vpd.rev.opFwName,
6178 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6179 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006180
6181 /*
6182 * Discover the port's supported feature set and match it against the
6183 * hosts requests.
6184 */
6185 lpfc_request_features(phba, mboxq);
6186 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6187 if (unlikely(rc)) {
6188 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006189 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006190 }
6191
6192 /*
6193 * The port must support FCP initiator mode as this is the
6194 * only mode running in the host.
6195 */
6196 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6197 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6198 "0378 No support for fcpi mode.\n");
6199 ftr_rsp++;
6200 }
James Smartfedd3b72011-02-16 12:39:24 -05006201 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6202 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6203 else
6204 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006205 /*
6206 * If the port cannot support the host's requested features
6207 * then turn off the global config parameters to disable the
6208 * feature in the driver. This is not a fatal error.
6209 */
James Smartbf086112011-08-21 21:48:13 -04006210 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6211 if (phba->cfg_enable_bg) {
6212 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6213 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6214 else
6215 ftr_rsp++;
6216 }
James Smartda0436e2009-05-22 14:51:39 -04006217
6218 if (phba->max_vpi && phba->cfg_enable_npiv &&
6219 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6220 ftr_rsp++;
6221
6222 if (ftr_rsp) {
6223 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6224 "0379 Feature Mismatch Data: x%08x %08x "
6225 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6226 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6227 phba->cfg_enable_npiv, phba->max_vpi);
6228 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6229 phba->cfg_enable_bg = 0;
6230 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6231 phba->cfg_enable_npiv = 0;
6232 }
6233
6234 /* These SLI3 features are assumed in SLI4 */
6235 spin_lock_irq(&phba->hbalock);
6236 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6237 spin_unlock_irq(&phba->hbalock);
6238
James Smart6d368e52011-05-24 11:44:12 -04006239 /*
6240 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6241 * calls depends on these resources to complete port setup.
6242 */
6243 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6244 if (rc) {
6245 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6246 "2920 Failed to alloc Resource IDs "
6247 "rc = x%x\n", rc);
6248 goto out_free_mbox;
6249 }
6250
James Smartda0436e2009-05-22 14:51:39 -04006251 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006252 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6253 if (rc) {
6254 phba->link_state = LPFC_HBA_ERROR;
6255 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006256 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006257 }
6258
James Smartda0436e2009-05-22 14:51:39 -04006259 mboxq->vport = vport;
6260 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6261 mp = (struct lpfc_dmabuf *) mboxq->context1;
6262 if (rc == MBX_SUCCESS) {
6263 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6264 rc = 0;
6265 }
6266
6267 /*
6268 * This memory was allocated by the lpfc_read_sparam routine. Release
6269 * it to the mbuf pool.
6270 */
6271 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6272 kfree(mp);
6273 mboxq->context1 = NULL;
6274 if (unlikely(rc)) {
6275 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6276 "0382 READ_SPARAM command failed "
6277 "status %d, mbxStatus x%x\n",
6278 rc, bf_get(lpfc_mqe_status, mqe));
6279 phba->link_state = LPFC_HBA_ERROR;
6280 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006281 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006282 }
6283
James Smart05580562011-05-24 11:40:48 -04006284 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006285
6286 /* Update the fc_host data structures with new wwn. */
6287 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6288 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6289
James Smart8a9d2e82012-05-09 21:16:12 -04006290 /* update host els and scsi xri-sgl sizes and mappings */
6291 rc = lpfc_sli4_xri_sgl_update(phba);
6292 if (unlikely(rc)) {
6293 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6294 "1400 Failed to update xri-sgl size and "
6295 "mapping: %d\n", rc);
6296 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006297 }
6298
James Smart8a9d2e82012-05-09 21:16:12 -04006299 /* register the els sgl pool to the port */
6300 rc = lpfc_sli4_repost_els_sgl_list(phba);
6301 if (unlikely(rc)) {
6302 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6303 "0582 Error %d during els sgl post "
6304 "operation\n", rc);
6305 rc = -ENODEV;
6306 goto out_free_mbox;
6307 }
6308
6309 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006310 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6311 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006312 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006313 "0383 Error %d during scsi sgl post "
6314 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006315 /* Some Scsi buffers were moved to the abort scsi list */
6316 /* A pci function reset will repost them */
6317 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006318 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006319 }
6320
6321 /* Post the rpi header region to the device. */
6322 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6323 if (unlikely(rc)) {
6324 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6325 "0393 Error %d during rpi post operation\n",
6326 rc);
6327 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006328 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006329 }
James Smart97f2ecf2012-03-01 22:35:23 -05006330 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006331
James Smart5350d872011-10-10 21:33:49 -04006332 /* Create all the SLI4 queues */
6333 rc = lpfc_sli4_queue_create(phba);
6334 if (rc) {
6335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6336 "3089 Failed to allocate queues\n");
6337 rc = -ENODEV;
6338 goto out_stop_timers;
6339 }
James Smartda0436e2009-05-22 14:51:39 -04006340 /* Set up all the queues to the device */
6341 rc = lpfc_sli4_queue_setup(phba);
6342 if (unlikely(rc)) {
6343 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6344 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006345 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006346 }
6347
6348 /* Arm the CQs and then EQs on device */
6349 lpfc_sli4_arm_cqeq_intr(phba);
6350
6351 /* Indicate device interrupt mode */
6352 phba->sli4_hba.intr_enable = 1;
6353
6354 /* Allow asynchronous mailbox command to go through */
6355 spin_lock_irq(&phba->hbalock);
6356 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6357 spin_unlock_irq(&phba->hbalock);
6358
6359 /* Post receive buffers to the device */
6360 lpfc_sli4_rb_setup(phba);
6361
James Smartfc2b9892010-02-26 14:15:29 -05006362 /* Reset HBA FCF states after HBA reset */
6363 phba->fcf.fcf_flag = 0;
6364 phba->fcf.current_rec.flag = 0;
6365
James Smartda0436e2009-05-22 14:51:39 -04006366 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006367 mod_timer(&vport->els_tmofunc,
6368 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006369
6370 /* Start heart beat timer */
6371 mod_timer(&phba->hb_tmofunc,
6372 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6373 phba->hb_outstanding = 0;
6374 phba->last_completion_time = jiffies;
6375
6376 /* Start error attention (ERATT) polling timer */
6377 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6378
James Smart75baf692010-06-08 18:31:21 -04006379 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6380 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6381 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6382 if (!rc) {
6383 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6384 "2829 This device supports "
6385 "Advanced Error Reporting (AER)\n");
6386 spin_lock_irq(&phba->hbalock);
6387 phba->hba_flag |= HBA_AER_ENABLED;
6388 spin_unlock_irq(&phba->hbalock);
6389 } else {
6390 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6391 "2830 This device does not support "
6392 "Advanced Error Reporting (AER)\n");
6393 phba->cfg_aer_support = 0;
6394 }
James Smart0a96e972011-07-22 18:37:28 -04006395 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006396 }
6397
James Smart76a95d72010-11-20 23:11:48 -05006398 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6399 /*
6400 * The FC Port needs to register FCFI (index 0)
6401 */
6402 lpfc_reg_fcfi(phba, mboxq);
6403 mboxq->vport = phba->pport;
6404 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006405 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006406 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006407 rc = 0;
6408 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6409 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006410
6411 /* Check if the port is configured to be disabled */
6412 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006413 }
James Smart026abb82011-12-13 13:20:45 -05006414
James Smartda0436e2009-05-22 14:51:39 -04006415 /*
6416 * The port is ready, set the host's link state to LINK_DOWN
6417 * in preparation for link interrupts.
6418 */
James Smartda0436e2009-05-22 14:51:39 -04006419 spin_lock_irq(&phba->hbalock);
6420 phba->link_state = LPFC_LINK_DOWN;
6421 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006422 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6423 (phba->hba_flag & LINK_DISABLED)) {
6424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6425 "3103 Adapter Link is disabled.\n");
6426 lpfc_down_link(phba, mboxq);
6427 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6428 if (rc != MBX_SUCCESS) {
6429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6430 "3104 Adapter failed to issue "
6431 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6432 goto out_unset_queue;
6433 }
6434 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006435 /* don't perform init_link on SLI4 FC port loopback test */
6436 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6437 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6438 if (rc)
6439 goto out_unset_queue;
6440 }
James Smart5350d872011-10-10 21:33:49 -04006441 }
6442 mempool_free(mboxq, phba->mbox_mem_pool);
6443 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006444out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006445 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006446 lpfc_sli4_queue_unset(phba);
6447out_destroy_queue:
6448 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006449out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006450 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006451out_free_mbox:
6452 mempool_free(mboxq, phba->mbox_mem_pool);
6453 return rc;
6454}
James Smarte59058c2008-08-24 21:49:00 -04006455
6456/**
James Smart3621a712009-04-06 18:47:14 -04006457 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006458 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006459 *
James Smarte59058c2008-08-24 21:49:00 -04006460 * This is the callback function for mailbox timer. The mailbox
6461 * timer is armed when a new mailbox command is issued and the timer
6462 * is deleted when the mailbox complete. The function is called by
6463 * the kernel timer code when a mailbox does not complete within
6464 * expected time. This function wakes up the worker thread to
6465 * process the mailbox timeout and returns. All the processing is
6466 * done by the worker thread function lpfc_mbox_timeout_handler.
6467 **/
dea31012005-04-17 16:05:31 -05006468void
6469lpfc_mbox_timeout(unsigned long ptr)
6470{
James Smart92d7f7b2007-06-17 19:56:38 -05006471 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006472 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006473 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006474
James Smart2e0fef82007-06-17 19:56:36 -05006475 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006476 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006477 if (!tmo_posted)
6478 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6479 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6480
James Smart5e9d9b82008-06-14 22:52:53 -04006481 if (!tmo_posted)
6482 lpfc_worker_wake_up(phba);
6483 return;
dea31012005-04-17 16:05:31 -05006484}
6485
James Smarte59058c2008-08-24 21:49:00 -04006486
6487/**
James Smart3621a712009-04-06 18:47:14 -04006488 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006489 * @phba: Pointer to HBA context object.
6490 *
6491 * This function is called from worker thread when a mailbox command times out.
6492 * The caller is not required to hold any locks. This function will reset the
6493 * HBA and recover all the pending commands.
6494 **/
dea31012005-04-17 16:05:31 -05006495void
6496lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6497{
James Smart2e0fef82007-06-17 19:56:36 -05006498 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006499 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006500 struct lpfc_sli *psli = &phba->sli;
6501 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006502
James Smarta257bf92009-04-06 18:48:10 -04006503 /* Check the pmbox pointer first. There is a race condition
6504 * between the mbox timeout handler getting executed in the
6505 * worklist and the mailbox actually completing. When this
6506 * race condition occurs, the mbox_active will be NULL.
6507 */
6508 spin_lock_irq(&phba->hbalock);
6509 if (pmbox == NULL) {
6510 lpfc_printf_log(phba, KERN_WARNING,
6511 LOG_MBOX | LOG_SLI,
6512 "0353 Active Mailbox cleared - mailbox timeout "
6513 "exiting\n");
6514 spin_unlock_irq(&phba->hbalock);
6515 return;
6516 }
6517
dea31012005-04-17 16:05:31 -05006518 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006519 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006520 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006521 mb->mbxCommand,
6522 phba->pport->port_state,
6523 phba->sli.sli_flag,
6524 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006525 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006526
James Smart1dcb58e2007-04-25 09:51:30 -04006527 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6528 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006529 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006530 */
James Smart2e0fef82007-06-17 19:56:36 -05006531 spin_lock_irq(&phba->pport->work_port_lock);
6532 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6533 spin_unlock_irq(&phba->pport->work_port_lock);
6534 spin_lock_irq(&phba->hbalock);
6535 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006536 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006537 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006538
6539 pring = &psli->ring[psli->fcp_ring];
6540 lpfc_sli_abort_iocb_ring(phba, pring);
6541
6542 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006543 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006544
6545 /* Reset the HBA device */
6546 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006547}
6548
James Smarte59058c2008-08-24 21:49:00 -04006549/**
James Smart3772a992009-05-22 14:50:54 -04006550 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006551 * @phba: Pointer to HBA context object.
6552 * @pmbox: Pointer to mailbox object.
6553 * @flag: Flag indicating how the mailbox need to be processed.
6554 *
6555 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006556 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6557 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006558 * The mailbox command can be submitted in polling mode, in which case
6559 * this function will wait in a polling loop for the completion of the
6560 * mailbox.
6561 * If the mailbox is submitted in no_wait mode (not polling) the
6562 * function will submit the command and returns immediately without waiting
6563 * for the mailbox completion. The no_wait is supported only when HBA
6564 * is in SLI2/SLI3 mode - interrupts are enabled.
6565 * The SLI interface allows only one mailbox pending at a time. If the
6566 * mailbox is issued in polling mode and there is already a mailbox
6567 * pending, then the function will return an error. If the mailbox is issued
6568 * in NO_WAIT mode and there is a mailbox pending already, the function
6569 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6570 * The sli layer owns the mailbox object until the completion of mailbox
6571 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6572 * return codes the caller owns the mailbox command after the return of
6573 * the function.
6574 **/
James Smart3772a992009-05-22 14:50:54 -04006575static int
6576lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6577 uint32_t flag)
dea31012005-04-17 16:05:31 -05006578{
dea31012005-04-17 16:05:31 -05006579 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006580 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006581 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006582 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006583 int i;
James Smart09372822008-01-11 01:52:54 -05006584 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006585 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006586 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006587 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006588 int processing_queue = 0;
6589
6590 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6591 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006592 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006593 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006594 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6595 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6596 return MBX_SUCCESS;
6597 }
James Smart58da1ff2008-04-07 10:15:56 -04006598 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006599 pmbox = lpfc_mbox_get(phba);
6600 if (!pmbox) {
6601 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6602 return MBX_SUCCESS;
6603 }
6604 }
dea31012005-04-17 16:05:31 -05006605
James Smarted957682007-06-17 19:56:37 -05006606 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006607 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006608 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006609 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006610 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006611 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006612 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006613 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006614 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006615 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006616 }
6617 }
6618
Linas Vepstas8d63f372007-02-14 14:28:36 -06006619 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006620 if (unlikely(pci_channel_offline(phba->pcidev))) {
6621 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6622 goto out_not_finished;
6623 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006624
James Smarta257bf92009-04-06 18:48:10 -04006625 /* If HBA has a deferred error attention, fail the iocb. */
6626 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6627 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6628 goto out_not_finished;
6629 }
6630
dea31012005-04-17 16:05:31 -05006631 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006632
James Smart3772a992009-05-22 14:50:54 -04006633 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006634 status = MBX_SUCCESS;
6635
James Smart2e0fef82007-06-17 19:56:36 -05006636 if (phba->link_state == LPFC_HBA_ERROR) {
6637 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006638
6639 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006640 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6641 "(%d):0311 Mailbox command x%x cannot "
6642 "issue Data: x%x x%x\n",
6643 pmbox->vport ? pmbox->vport->vpi : 0,
6644 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006645 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006646 }
6647
James Smart9940b972011-03-11 16:06:12 -05006648 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6649 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6650 !(hc_copy & HC_MBINT_ENA)) {
6651 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006653 "(%d):2528 Mailbox command x%x cannot "
6654 "issue Data: x%x x%x\n",
6655 pmbox->vport ? pmbox->vport->vpi : 0,
6656 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006657 goto out_not_finished;
6658 }
James Smart92908312006-03-07 15:04:13 -05006659 }
6660
dea31012005-04-17 16:05:31 -05006661 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6662 /* Polling for a mbox command when another one is already active
6663 * is not allowed in SLI. Also, the driver must have established
6664 * SLI2 mode to queue and process multiple mbox commands.
6665 */
6666
6667 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006668 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006669
6670 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006671 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6672 "(%d):2529 Mailbox command x%x "
6673 "cannot issue Data: x%x x%x\n",
6674 pmbox->vport ? pmbox->vport->vpi : 0,
6675 pmbox->u.mb.mbxCommand,
6676 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006677 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006678 }
6679
James Smart3772a992009-05-22 14:50:54 -04006680 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006681 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006682 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006683 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6684 "(%d):2530 Mailbox command x%x "
6685 "cannot issue Data: x%x x%x\n",
6686 pmbox->vport ? pmbox->vport->vpi : 0,
6687 pmbox->u.mb.mbxCommand,
6688 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006689 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006690 }
6691
dea31012005-04-17 16:05:31 -05006692 /* Another mailbox command is still being processed, queue this
6693 * command to be processed later.
6694 */
6695 lpfc_mbox_put(phba, pmbox);
6696
6697 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006698 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006699 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006700 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006701 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6702 mb->mbxCommand, phba->pport->port_state,
6703 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006704
6705 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006706 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006707
James Smart858c9f62007-06-17 19:56:39 -05006708 if (pmbox->vport) {
6709 lpfc_debugfs_disc_trc(pmbox->vport,
6710 LPFC_DISC_TRC_MBOX_VPORT,
6711 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6712 (uint32_t)mb->mbxCommand,
6713 mb->un.varWords[0], mb->un.varWords[1]);
6714 }
6715 else {
6716 lpfc_debugfs_disc_trc(phba->pport,
6717 LPFC_DISC_TRC_MBOX,
6718 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6719 (uint32_t)mb->mbxCommand,
6720 mb->un.varWords[0], mb->un.varWords[1]);
6721 }
6722
James Smart2e0fef82007-06-17 19:56:36 -05006723 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006724 }
6725
dea31012005-04-17 16:05:31 -05006726 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6727
6728 /* If we are not polling, we MUST be in SLI2 mode */
6729 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006730 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006731 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006732 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006733 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006734 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006735 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6736 "(%d):2531 Mailbox command x%x "
6737 "cannot issue Data: x%x x%x\n",
6738 pmbox->vport ? pmbox->vport->vpi : 0,
6739 pmbox->u.mb.mbxCommand,
6740 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006741 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006742 }
6743 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006744 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006745 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006746 }
6747
6748 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006749 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006750 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006751 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006752 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006753 mb->mbxCommand, phba->pport->port_state,
6754 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006755
James Smart858c9f62007-06-17 19:56:39 -05006756 if (mb->mbxCommand != MBX_HEARTBEAT) {
6757 if (pmbox->vport) {
6758 lpfc_debugfs_disc_trc(pmbox->vport,
6759 LPFC_DISC_TRC_MBOX_VPORT,
6760 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6761 (uint32_t)mb->mbxCommand,
6762 mb->un.varWords[0], mb->un.varWords[1]);
6763 }
6764 else {
6765 lpfc_debugfs_disc_trc(phba->pport,
6766 LPFC_DISC_TRC_MBOX,
6767 "MBOX Send: cmd:x%x mb:x%x x%x",
6768 (uint32_t)mb->mbxCommand,
6769 mb->un.varWords[0], mb->un.varWords[1]);
6770 }
6771 }
6772
dea31012005-04-17 16:05:31 -05006773 psli->slistat.mbox_cmd++;
6774 evtctr = psli->slistat.mbox_event;
6775
6776 /* next set own bit for the adapter and copy over command word */
6777 mb->mbxOwner = OWN_CHIP;
6778
James Smart3772a992009-05-22 14:50:54 -04006779 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
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 = (uint8_t *)phba->mbox_ext
6784 - (uint8_t *)phba->mbox;
6785 }
6786
6787 /* Copy the mailbox extension data */
6788 if (pmbox->in_ext_byte_len && pmbox->context2) {
6789 lpfc_sli_pcimem_bcopy(pmbox->context2,
6790 (uint8_t *)phba->mbox_ext,
6791 pmbox->in_ext_byte_len);
6792 }
6793 /* Copy command data to host SLIM area */
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 } else {
James Smart7a470272010-03-15 11:25:20 -04006796 /* Populate mbox extension offset word. */
6797 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6798 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6799 = MAILBOX_HBA_EXT_OFFSET;
6800
6801 /* Copy the mailbox extension data */
6802 if (pmbox->in_ext_byte_len && pmbox->context2) {
6803 lpfc_memcpy_to_slim(phba->MBslimaddr +
6804 MAILBOX_HBA_EXT_OFFSET,
6805 pmbox->context2, pmbox->in_ext_byte_len);
6806
6807 }
James Smart92908312006-03-07 15:04:13 -05006808 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006809 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006810 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006811 }
6812
6813 /* First copy mbox command data to HBA SLIM, skip past first
6814 word */
6815 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6816 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6817 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6818
6819 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006820 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006821 to_slim = phba->MBslimaddr;
6822 writel(ldata, to_slim);
6823 readl(to_slim); /* flush */
6824
6825 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6826 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006827 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006828 }
6829 }
6830
6831 wmb();
dea31012005-04-17 16:05:31 -05006832
6833 switch (flag) {
6834 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006835 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006836 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006837 /* Interrupt board to do it */
6838 writel(CA_MBATT, phba->CAregaddr);
6839 readl(phba->CAregaddr); /* flush */
6840 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006841 break;
6842
6843 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006844 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006845 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006846 /* Interrupt board to do it */
6847 writel(CA_MBATT, phba->CAregaddr);
6848 readl(phba->CAregaddr); /* flush */
6849
James Smart3772a992009-05-22 14:50:54 -04006850 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006851 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006852 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006853 word0 = le32_to_cpu(word0);
6854 } else {
6855 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006856 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6857 spin_unlock_irqrestore(&phba->hbalock,
6858 drvr_flag);
6859 goto out_not_finished;
6860 }
dea31012005-04-17 16:05:31 -05006861 }
6862
6863 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006864 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6865 spin_unlock_irqrestore(&phba->hbalock,
6866 drvr_flag);
6867 goto out_not_finished;
6868 }
James Smarta183a152011-10-10 21:32:43 -04006869 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6870 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006871 i = 0;
dea31012005-04-17 16:05:31 -05006872 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006873 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6874 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006875 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006876 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006877 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006878 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006879 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006880 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006881 }
6882
6883 /* Check if we took a mbox interrupt while we were
6884 polling */
6885 if (((word0 & OWN_CHIP) != OWN_CHIP)
6886 && (evtctr != psli->slistat.mbox_event))
6887 break;
6888
James Smart09372822008-01-11 01:52:54 -05006889 if (i++ > 10) {
6890 spin_unlock_irqrestore(&phba->hbalock,
6891 drvr_flag);
6892 msleep(1);
6893 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6894 }
dea31012005-04-17 16:05:31 -05006895
James Smart3772a992009-05-22 14:50:54 -04006896 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006897 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006898 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006899 word0 = le32_to_cpu(word0);
6900 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6901 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006902 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006903 /* Check real SLIM for any errors */
6904 slimword0 = readl(phba->MBslimaddr);
6905 slimmb = (MAILBOX_t *) & slimword0;
6906 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6907 && slimmb->mbxStatus) {
6908 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006909 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006910 word0 = slimword0;
6911 }
6912 }
6913 } else {
6914 /* First copy command data */
6915 word0 = readl(phba->MBslimaddr);
6916 }
6917 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006918 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6919 spin_unlock_irqrestore(&phba->hbalock,
6920 drvr_flag);
6921 goto out_not_finished;
6922 }
dea31012005-04-17 16:05:31 -05006923 }
6924
James Smart3772a992009-05-22 14:50:54 -04006925 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006926 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006927 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006928 /* Copy the mailbox extension data */
6929 if (pmbox->out_ext_byte_len && pmbox->context2) {
6930 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6931 pmbox->context2,
6932 pmbox->out_ext_byte_len);
6933 }
dea31012005-04-17 16:05:31 -05006934 } else {
6935 /* First copy command data */
6936 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6937 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006938 /* Copy the mailbox extension data */
6939 if (pmbox->out_ext_byte_len && pmbox->context2) {
6940 lpfc_memcpy_from_slim(pmbox->context2,
6941 phba->MBslimaddr +
6942 MAILBOX_HBA_EXT_OFFSET,
6943 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006944 }
6945 }
6946
6947 writel(HA_MBATT, phba->HAregaddr);
6948 readl(phba->HAregaddr); /* flush */
6949
6950 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6951 status = mb->mbxStatus;
6952 }
6953
James Smart2e0fef82007-06-17 19:56:36 -05006954 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6955 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006956
6957out_not_finished:
6958 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006959 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006960 lpfc_mbox_cmpl_put(phba, pmbox);
6961 }
6962 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006963}
6964
James Smarte59058c2008-08-24 21:49:00 -04006965/**
James Smartf1126682009-06-10 17:22:44 -04006966 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6967 * @phba: Pointer to HBA context object.
6968 *
6969 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6970 * the driver internal pending mailbox queue. It will then try to wait out the
6971 * possible outstanding mailbox command before return.
6972 *
6973 * Returns:
6974 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6975 * the outstanding mailbox command timed out.
6976 **/
6977static int
6978lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6979{
6980 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006981 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006982 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006983
6984 /* Mark the asynchronous mailbox command posting as blocked */
6985 spin_lock_irq(&phba->hbalock);
6986 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006987 /* Determine how long we might wait for the active mailbox
6988 * command to be gracefully completed by firmware.
6989 */
James Smarta183a152011-10-10 21:32:43 -04006990 if (phba->sli.mbox_active)
6991 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6992 phba->sli.mbox_active) *
6993 1000) + jiffies;
6994 spin_unlock_irq(&phba->hbalock);
6995
James Smartf1126682009-06-10 17:22:44 -04006996 /* Wait for the outstnading mailbox command to complete */
6997 while (phba->sli.mbox_active) {
6998 /* Check active mailbox complete status every 2ms */
6999 msleep(2);
7000 if (time_after(jiffies, timeout)) {
7001 /* Timeout, marked the outstanding cmd not complete */
7002 rc = 1;
7003 break;
7004 }
7005 }
7006
7007 /* Can not cleanly block async mailbox command, fails it */
7008 if (rc) {
7009 spin_lock_irq(&phba->hbalock);
7010 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7011 spin_unlock_irq(&phba->hbalock);
7012 }
7013 return rc;
7014}
7015
7016/**
7017 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7018 * @phba: Pointer to HBA context object.
7019 *
7020 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7021 * commands from the driver internal pending mailbox queue. It makes sure
7022 * that there is no outstanding mailbox command before resuming posting
7023 * asynchronous mailbox commands. If, for any reason, there is outstanding
7024 * mailbox command, it will try to wait it out before resuming asynchronous
7025 * mailbox command posting.
7026 **/
7027static void
7028lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7029{
7030 struct lpfc_sli *psli = &phba->sli;
7031
7032 spin_lock_irq(&phba->hbalock);
7033 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7034 /* Asynchronous mailbox posting is not blocked, do nothing */
7035 spin_unlock_irq(&phba->hbalock);
7036 return;
7037 }
7038
7039 /* Outstanding synchronous mailbox command is guaranteed to be done,
7040 * successful or timeout, after timing-out the outstanding mailbox
7041 * command shall always be removed, so just unblock posting async
7042 * mailbox command and resume
7043 */
7044 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7045 spin_unlock_irq(&phba->hbalock);
7046
7047 /* wake up worker thread to post asynchronlous mailbox command */
7048 lpfc_worker_wake_up(phba);
7049}
7050
7051/**
James Smartda0436e2009-05-22 14:51:39 -04007052 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7053 * @phba: Pointer to HBA context object.
7054 * @mboxq: Pointer to mailbox object.
7055 *
7056 * The function posts a mailbox to the port. The mailbox is expected
7057 * to be comletely filled in and ready for the port to operate on it.
7058 * This routine executes a synchronous completion operation on the
7059 * mailbox by polling for its completion.
7060 *
7061 * The caller must not be holding any locks when calling this routine.
7062 *
7063 * Returns:
7064 * MBX_SUCCESS - mailbox posted successfully
7065 * Any of the MBX error values.
7066 **/
7067static int
7068lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7069{
7070 int rc = MBX_SUCCESS;
7071 unsigned long iflag;
7072 uint32_t db_ready;
7073 uint32_t mcqe_status;
7074 uint32_t mbx_cmnd;
7075 unsigned long timeout;
7076 struct lpfc_sli *psli = &phba->sli;
7077 struct lpfc_mqe *mb = &mboxq->u.mqe;
7078 struct lpfc_bmbx_create *mbox_rgn;
7079 struct dma_address *dma_address;
7080 struct lpfc_register bmbx_reg;
7081
7082 /*
7083 * Only one mailbox can be active to the bootstrap mailbox region
7084 * at a time and there is no queueing provided.
7085 */
7086 spin_lock_irqsave(&phba->hbalock, iflag);
7087 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7088 spin_unlock_irqrestore(&phba->hbalock, iflag);
7089 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007090 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007091 "cannot issue Data: x%x x%x\n",
7092 mboxq->vport ? mboxq->vport->vpi : 0,
7093 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007094 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7095 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007096 psli->sli_flag, MBX_POLL);
7097 return MBXERR_ERROR;
7098 }
7099 /* The server grabs the token and owns it until release */
7100 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7101 phba->sli.mbox_active = mboxq;
7102 spin_unlock_irqrestore(&phba->hbalock, iflag);
7103
7104 /*
7105 * Initialize the bootstrap memory region to avoid stale data areas
7106 * in the mailbox post. Then copy the caller's mailbox contents to
7107 * the bmbx mailbox region.
7108 */
7109 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7110 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7111 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7112 sizeof(struct lpfc_mqe));
7113
7114 /* Post the high mailbox dma address to the port and wait for ready. */
7115 dma_address = &phba->sli4_hba.bmbx.dma_address;
7116 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7117
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 /* Post the low mailbox dma address to the port. */
7133 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04007134 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007135 * 1000) + jiffies;
7136 do {
7137 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7138 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7139 if (!db_ready)
7140 msleep(2);
7141
7142 if (time_after(jiffies, timeout)) {
7143 rc = MBXERR_ERROR;
7144 goto exit;
7145 }
7146 } while (!db_ready);
7147
7148 /*
7149 * Read the CQ to ensure the mailbox has completed.
7150 * If so, update the mailbox status so that the upper layers
7151 * can complete the request normally.
7152 */
7153 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7154 sizeof(struct lpfc_mqe));
7155 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7156 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7157 sizeof(struct lpfc_mcqe));
7158 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007159 /*
7160 * When the CQE status indicates a failure and the mailbox status
7161 * indicates success then copy the CQE status into the mailbox status
7162 * (and prefix it with x4000).
7163 */
James Smartda0436e2009-05-22 14:51:39 -04007164 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007165 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7166 bf_set(lpfc_mqe_status, mb,
7167 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007168 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007169 } else
7170 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007171
7172 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007173 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007174 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7175 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007176 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7177 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7178 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007179 bf_get(lpfc_mqe_status, mb),
7180 mb->un.mb_words[0], mb->un.mb_words[1],
7181 mb->un.mb_words[2], mb->un.mb_words[3],
7182 mb->un.mb_words[4], mb->un.mb_words[5],
7183 mb->un.mb_words[6], mb->un.mb_words[7],
7184 mb->un.mb_words[8], mb->un.mb_words[9],
7185 mb->un.mb_words[10], mb->un.mb_words[11],
7186 mb->un.mb_words[12], mboxq->mcqe.word0,
7187 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7188 mboxq->mcqe.trailer);
7189exit:
7190 /* We are holding the token, no needed for lock when release */
7191 spin_lock_irqsave(&phba->hbalock, iflag);
7192 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7193 phba->sli.mbox_active = NULL;
7194 spin_unlock_irqrestore(&phba->hbalock, iflag);
7195 return rc;
7196}
7197
7198/**
7199 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7200 * @phba: Pointer to HBA context object.
7201 * @pmbox: Pointer to mailbox object.
7202 * @flag: Flag indicating how the mailbox need to be processed.
7203 *
7204 * This function is called by discovery code and HBA management code to submit
7205 * a mailbox command to firmware with SLI-4 interface spec.
7206 *
7207 * Return codes the caller owns the mailbox command after the return of the
7208 * function.
7209 **/
7210static int
7211lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7212 uint32_t flag)
7213{
7214 struct lpfc_sli *psli = &phba->sli;
7215 unsigned long iflags;
7216 int rc;
7217
James Smartb76f2dc2011-07-22 18:37:42 -04007218 /* dump from issue mailbox command if setup */
7219 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7220
James Smart8fa38512009-07-19 10:01:03 -04007221 rc = lpfc_mbox_dev_check(phba);
7222 if (unlikely(rc)) {
7223 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007224 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007225 "cannot issue Data: x%x x%x\n",
7226 mboxq->vport ? mboxq->vport->vpi : 0,
7227 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007228 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7229 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007230 psli->sli_flag, flag);
7231 goto out_not_finished;
7232 }
7233
James Smartda0436e2009-05-22 14:51:39 -04007234 /* Detect polling mode and jump to a handler */
7235 if (!phba->sli4_hba.intr_enable) {
7236 if (flag == MBX_POLL)
7237 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7238 else
7239 rc = -EIO;
7240 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007241 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007242 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007243 "(x%x/x%x) failure: "
7244 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7245 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007246 mboxq->vport ? mboxq->vport->vpi : 0,
7247 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007248 lpfc_sli_config_mbox_subsys_get(phba,
7249 mboxq),
7250 lpfc_sli_config_mbox_opcode_get(phba,
7251 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007252 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7253 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7254 bf_get(lpfc_mcqe_ext_status,
7255 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007256 psli->sli_flag, flag);
7257 return rc;
7258 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007259 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7260 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007261 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007262 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007263 mboxq->vport ? mboxq->vport->vpi : 0,
7264 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007265 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7266 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007267 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007268 /* Try to block the asynchronous mailbox posting */
7269 rc = lpfc_sli4_async_mbox_block(phba);
7270 if (!rc) {
7271 /* Successfully blocked, now issue sync mbox cmd */
7272 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7273 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007274 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007275 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007276 "(%d):2597 Sync Mailbox command "
7277 "x%x (x%x/x%x) failure: "
7278 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7279 "Data: x%x x%x\n,",
7280 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007281 mboxq->u.mb.mbxCommand,
7282 lpfc_sli_config_mbox_subsys_get(phba,
7283 mboxq),
7284 lpfc_sli_config_mbox_opcode_get(phba,
7285 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007286 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7287 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7288 bf_get(lpfc_mcqe_ext_status,
7289 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007290 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007291 /* Unblock the async mailbox posting afterward */
7292 lpfc_sli4_async_mbox_unblock(phba);
7293 }
7294 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007295 }
7296
7297 /* Now, interrupt mode asynchrous mailbox command */
7298 rc = lpfc_mbox_cmd_check(phba, mboxq);
7299 if (rc) {
7300 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007301 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007302 "cannot issue Data: x%x x%x\n",
7303 mboxq->vport ? mboxq->vport->vpi : 0,
7304 mboxq->u.mb.mbxCommand,
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 psli->sli_flag, flag);
7308 goto out_not_finished;
7309 }
James Smartda0436e2009-05-22 14:51:39 -04007310
7311 /* Put the mailbox command to the driver internal FIFO */
7312 psli->slistat.mbox_busy++;
7313 spin_lock_irqsave(&phba->hbalock, iflags);
7314 lpfc_mbox_put(phba, mboxq);
7315 spin_unlock_irqrestore(&phba->hbalock, iflags);
7316 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7317 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007318 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007319 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7320 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007321 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7322 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007323 phba->pport->port_state,
7324 psli->sli_flag, MBX_NOWAIT);
7325 /* Wake up worker thread to transport mailbox command from head */
7326 lpfc_worker_wake_up(phba);
7327
7328 return MBX_BUSY;
7329
7330out_not_finished:
7331 return MBX_NOT_FINISHED;
7332}
7333
7334/**
7335 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7336 * @phba: Pointer to HBA context object.
7337 *
7338 * This function is called by worker thread to send a mailbox command to
7339 * SLI4 HBA firmware.
7340 *
7341 **/
7342int
7343lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7344{
7345 struct lpfc_sli *psli = &phba->sli;
7346 LPFC_MBOXQ_t *mboxq;
7347 int rc = MBX_SUCCESS;
7348 unsigned long iflags;
7349 struct lpfc_mqe *mqe;
7350 uint32_t mbx_cmnd;
7351
7352 /* Check interrupt mode before post async mailbox command */
7353 if (unlikely(!phba->sli4_hba.intr_enable))
7354 return MBX_NOT_FINISHED;
7355
7356 /* Check for mailbox command service token */
7357 spin_lock_irqsave(&phba->hbalock, iflags);
7358 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7359 spin_unlock_irqrestore(&phba->hbalock, iflags);
7360 return MBX_NOT_FINISHED;
7361 }
7362 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7363 spin_unlock_irqrestore(&phba->hbalock, iflags);
7364 return MBX_NOT_FINISHED;
7365 }
7366 if (unlikely(phba->sli.mbox_active)) {
7367 spin_unlock_irqrestore(&phba->hbalock, iflags);
7368 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7369 "0384 There is pending active mailbox cmd\n");
7370 return MBX_NOT_FINISHED;
7371 }
7372 /* Take the mailbox command service token */
7373 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7374
7375 /* Get the next mailbox command from head of queue */
7376 mboxq = lpfc_mbox_get(phba);
7377
7378 /* If no more mailbox command waiting for post, we're done */
7379 if (!mboxq) {
7380 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7381 spin_unlock_irqrestore(&phba->hbalock, iflags);
7382 return MBX_SUCCESS;
7383 }
7384 phba->sli.mbox_active = mboxq;
7385 spin_unlock_irqrestore(&phba->hbalock, iflags);
7386
7387 /* Check device readiness for posting mailbox command */
7388 rc = lpfc_mbox_dev_check(phba);
7389 if (unlikely(rc))
7390 /* Driver clean routine will clean up pending mailbox */
7391 goto out_not_finished;
7392
7393 /* Prepare the mbox command to be posted */
7394 mqe = &mboxq->u.mqe;
7395 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7396
7397 /* Start timer for the mbox_tmo and log some mailbox post messages */
7398 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007399 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007400
7401 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007402 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007403 "x%x x%x\n",
7404 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007405 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7406 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007407 phba->pport->port_state, psli->sli_flag);
7408
7409 if (mbx_cmnd != MBX_HEARTBEAT) {
7410 if (mboxq->vport) {
7411 lpfc_debugfs_disc_trc(mboxq->vport,
7412 LPFC_DISC_TRC_MBOX_VPORT,
7413 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7414 mbx_cmnd, mqe->un.mb_words[0],
7415 mqe->un.mb_words[1]);
7416 } else {
7417 lpfc_debugfs_disc_trc(phba->pport,
7418 LPFC_DISC_TRC_MBOX,
7419 "MBOX Send: cmd:x%x mb:x%x x%x",
7420 mbx_cmnd, mqe->un.mb_words[0],
7421 mqe->un.mb_words[1]);
7422 }
7423 }
7424 psli->slistat.mbox_cmd++;
7425
7426 /* Post the mailbox command to the port */
7427 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7428 if (rc != MBX_SUCCESS) {
7429 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007430 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007431 "cannot issue Data: x%x x%x\n",
7432 mboxq->vport ? mboxq->vport->vpi : 0,
7433 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007434 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7435 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007436 psli->sli_flag, MBX_NOWAIT);
7437 goto out_not_finished;
7438 }
7439
7440 return rc;
7441
7442out_not_finished:
7443 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007444 if (phba->sli.mbox_active) {
7445 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7446 __lpfc_mbox_cmpl_put(phba, mboxq);
7447 /* Release the token */
7448 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7449 phba->sli.mbox_active = NULL;
7450 }
James Smartda0436e2009-05-22 14:51:39 -04007451 spin_unlock_irqrestore(&phba->hbalock, iflags);
7452
7453 return MBX_NOT_FINISHED;
7454}
7455
7456/**
7457 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7458 * @phba: Pointer to HBA context object.
7459 * @pmbox: Pointer to mailbox object.
7460 * @flag: Flag indicating how the mailbox need to be processed.
7461 *
7462 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7463 * the API jump table function pointer from the lpfc_hba struct.
7464 *
7465 * Return codes the caller owns the mailbox command after the return of the
7466 * function.
7467 **/
7468int
7469lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7470{
7471 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7472}
7473
7474/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007475 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007476 * @phba: The hba struct for which this call is being executed.
7477 * @dev_grp: The HBA PCI-Device group number.
7478 *
7479 * This routine sets up the mbox interface API function jump table in @phba
7480 * struct.
7481 * Returns: 0 - success, -ENODEV - failure.
7482 **/
7483int
7484lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7485{
7486
7487 switch (dev_grp) {
7488 case LPFC_PCI_DEV_LP:
7489 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7490 phba->lpfc_sli_handle_slow_ring_event =
7491 lpfc_sli_handle_slow_ring_event_s3;
7492 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7493 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7494 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7495 break;
7496 case LPFC_PCI_DEV_OC:
7497 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7498 phba->lpfc_sli_handle_slow_ring_event =
7499 lpfc_sli_handle_slow_ring_event_s4;
7500 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7501 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7502 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7503 break;
7504 default:
7505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7506 "1420 Invalid HBA PCI-device group: 0x%x\n",
7507 dev_grp);
7508 return -ENODEV;
7509 break;
7510 }
7511 return 0;
7512}
7513
7514/**
James Smart3621a712009-04-06 18:47:14 -04007515 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007516 * @phba: Pointer to HBA context object.
7517 * @pring: Pointer to driver SLI ring object.
7518 * @piocb: Pointer to address of newly added command iocb.
7519 *
7520 * This function is called with hbalock held to add a command
7521 * iocb to the txq when SLI layer cannot submit the command iocb
7522 * to the ring.
7523 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007524void
James Smart92d7f7b2007-06-17 19:56:38 -05007525__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007526 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007527{
7528 /* Insert the caller's iocb in the txq tail for later processing. */
7529 list_add_tail(&piocb->list, &pring->txq);
7530 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007531}
7532
James Smarte59058c2008-08-24 21:49:00 -04007533/**
James Smart3621a712009-04-06 18:47:14 -04007534 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007535 * @phba: Pointer to HBA context object.
7536 * @pring: Pointer to driver SLI ring object.
7537 * @piocb: Pointer to address of newly added command iocb.
7538 *
7539 * This function is called with hbalock held before a new
7540 * iocb is submitted to the firmware. This function checks
7541 * txq to flush the iocbs in txq to Firmware before
7542 * submitting new iocbs to the Firmware.
7543 * If there are iocbs in the txq which need to be submitted
7544 * to firmware, lpfc_sli_next_iocb returns the first element
7545 * of the txq after dequeuing it from txq.
7546 * If there is no iocb in the txq then the function will return
7547 * *piocb and *piocb is set to NULL. Caller needs to check
7548 * *piocb to find if there are more commands in the txq.
7549 **/
dea31012005-04-17 16:05:31 -05007550static struct lpfc_iocbq *
7551lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007552 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007553{
7554 struct lpfc_iocbq * nextiocb;
7555
7556 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7557 if (!nextiocb) {
7558 nextiocb = *piocb;
7559 *piocb = NULL;
7560 }
7561
7562 return nextiocb;
7563}
7564
James Smarte59058c2008-08-24 21:49:00 -04007565/**
James Smart3772a992009-05-22 14:50:54 -04007566 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007567 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007568 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007569 * @piocb: Pointer to command iocb.
7570 * @flag: Flag indicating if this command can be put into txq.
7571 *
James Smart3772a992009-05-22 14:50:54 -04007572 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7573 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7574 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7575 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7576 * this function allows only iocbs for posting buffers. This function finds
7577 * next available slot in the command ring and posts the command to the
7578 * available slot and writes the port attention register to request HBA start
7579 * processing new iocb. If there is no slot available in the ring and
7580 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7581 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007582 *
James Smart3772a992009-05-22 14:50:54 -04007583 * This function is called with hbalock held. The function will return success
7584 * after it successfully submit the iocb to firmware or after adding to the
7585 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007586 **/
James Smart98c9ea52007-10-27 13:37:33 -04007587static int
James Smart3772a992009-05-22 14:50:54 -04007588__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007589 struct lpfc_iocbq *piocb, uint32_t flag)
7590{
7591 struct lpfc_iocbq *nextiocb;
7592 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007593 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007594
James Smart92d7f7b2007-06-17 19:56:38 -05007595 if (piocb->iocb_cmpl && (!piocb->vport) &&
7596 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7597 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7598 lpfc_printf_log(phba, KERN_ERR,
7599 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007600 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007601 piocb->iocb.ulpCommand);
7602 dump_stack();
7603 return IOCB_ERROR;
7604 }
7605
7606
Linas Vepstas8d63f372007-02-14 14:28:36 -06007607 /* If the PCI channel is in offline state, do not post iocbs. */
7608 if (unlikely(pci_channel_offline(phba->pcidev)))
7609 return IOCB_ERROR;
7610
James Smarta257bf92009-04-06 18:48:10 -04007611 /* If HBA has a deferred error attention, fail the iocb. */
7612 if (unlikely(phba->hba_flag & DEFER_ERATT))
7613 return IOCB_ERROR;
7614
dea31012005-04-17 16:05:31 -05007615 /*
7616 * We should never get an IOCB if we are in a < LINK_DOWN state
7617 */
James Smart2e0fef82007-06-17 19:56:36 -05007618 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007619 return IOCB_ERROR;
7620
7621 /*
7622 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007623 * outstanding event.
dea31012005-04-17 16:05:31 -05007624 */
James Smart0b727fe2007-10-27 13:37:25 -04007625 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007626 goto iocb_busy;
7627
James Smart2e0fef82007-06-17 19:56:36 -05007628 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007629 /*
James Smart2680eea2007-04-25 09:52:55 -04007630 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007631 * can be issued if the link is not up.
7632 */
7633 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007634 case CMD_GEN_REQUEST64_CR:
7635 case CMD_GEN_REQUEST64_CX:
7636 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7637 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007638 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007639 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7640 MENLO_TRANSPORT_TYPE))
7641
7642 goto iocb_busy;
7643 break;
dea31012005-04-17 16:05:31 -05007644 case CMD_QUE_RING_BUF_CN:
7645 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007646 /*
7647 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7648 * completion, iocb_cmpl MUST be 0.
7649 */
7650 if (piocb->iocb_cmpl)
7651 piocb->iocb_cmpl = NULL;
7652 /*FALLTHROUGH*/
7653 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007654 case CMD_CLOSE_XRI_CN:
7655 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007656 break;
7657 default:
7658 goto iocb_busy;
7659 }
7660
7661 /*
7662 * For FCP commands, we must be in a state where we can process link
7663 * attention events.
7664 */
7665 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007666 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007667 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007668 }
dea31012005-04-17 16:05:31 -05007669
dea31012005-04-17 16:05:31 -05007670 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7671 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7672 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7673
7674 if (iocb)
7675 lpfc_sli_update_ring(phba, pring);
7676 else
7677 lpfc_sli_update_full_ring(phba, pring);
7678
7679 if (!piocb)
7680 return IOCB_SUCCESS;
7681
7682 goto out_busy;
7683
7684 iocb_busy:
7685 pring->stats.iocb_cmd_delay++;
7686
7687 out_busy:
7688
7689 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007690 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007691 return IOCB_SUCCESS;
7692 }
7693
7694 return IOCB_BUSY;
7695}
7696
James Smart3772a992009-05-22 14:50:54 -04007697/**
James Smart4f774512009-05-22 14:52:35 -04007698 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7699 * @phba: Pointer to HBA context object.
7700 * @piocb: Pointer to command iocb.
7701 * @sglq: Pointer to the scatter gather queue object.
7702 *
7703 * This routine converts the bpl or bde that is in the IOCB
7704 * to a sgl list for the sli4 hardware. The physical address
7705 * of the bpl/bde is converted back to a virtual address.
7706 * If the IOCB contains a BPL then the list of BDE's is
7707 * converted to sli4_sge's. If the IOCB contains a single
7708 * BDE then it is converted to a single sli_sge.
7709 * The IOCB is still in cpu endianess so the contents of
7710 * the bpl can be used without byte swapping.
7711 *
7712 * Returns valid XRI = Success, NO_XRI = Failure.
7713**/
7714static uint16_t
7715lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7716 struct lpfc_sglq *sglq)
7717{
7718 uint16_t xritag = NO_XRI;
7719 struct ulp_bde64 *bpl = NULL;
7720 struct ulp_bde64 bde;
7721 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007722 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007723 IOCB_t *icmd;
7724 int numBdes = 0;
7725 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007726 uint32_t offset = 0; /* accumulated offset in the sg request list */
7727 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007728
7729 if (!piocbq || !sglq)
7730 return xritag;
7731
7732 sgl = (struct sli4_sge *)sglq->sgl;
7733 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007734 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7735 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007736 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7737 numBdes = icmd->un.genreq64.bdl.bdeSize /
7738 sizeof(struct ulp_bde64);
7739 /* The addrHigh and addrLow fields within the IOCB
7740 * have not been byteswapped yet so there is no
7741 * need to swap them back.
7742 */
James Smart1b511972011-12-13 13:23:09 -05007743 if (piocbq->context3)
7744 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7745 else
7746 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007747
James Smart1b511972011-12-13 13:23:09 -05007748 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007749 if (!bpl)
7750 return xritag;
7751
7752 for (i = 0; i < numBdes; i++) {
7753 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007754 sgl->addr_hi = bpl->addrHigh;
7755 sgl->addr_lo = bpl->addrLow;
7756
James Smart05580562011-05-24 11:40:48 -04007757 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007758 if ((i+1) == numBdes)
7759 bf_set(lpfc_sli4_sge_last, sgl, 1);
7760 else
7761 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007762 /* swap the size field back to the cpu so we
7763 * can assign it to the sgl.
7764 */
7765 bde.tus.w = le32_to_cpu(bpl->tus.w);
7766 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007767 /* The offsets in the sgl need to be accumulated
7768 * separately for the request and reply lists.
7769 * The request is always first, the reply follows.
7770 */
7771 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7772 /* add up the reply sg entries */
7773 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7774 inbound++;
7775 /* first inbound? reset the offset */
7776 if (inbound == 1)
7777 offset = 0;
7778 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007779 bf_set(lpfc_sli4_sge_type, sgl,
7780 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007781 offset += bde.tus.f.bdeSize;
7782 }
James Smart546fc852011-03-11 16:06:29 -05007783 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007784 bpl++;
7785 sgl++;
7786 }
7787 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7788 /* The addrHigh and addrLow fields of the BDE have not
7789 * been byteswapped yet so they need to be swapped
7790 * before putting them in the sgl.
7791 */
7792 sgl->addr_hi =
7793 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7794 sgl->addr_lo =
7795 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007796 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007797 bf_set(lpfc_sli4_sge_last, sgl, 1);
7798 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007799 sgl->sge_len =
7800 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007801 }
7802 return sglq->sli4_xritag;
7803}
7804
7805/**
7806 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7807 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007808 *
James Smarta93ff372010-10-22 11:06:08 -04007809 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007810 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7811 * held.
James Smart4f774512009-05-22 14:52:35 -04007812 *
7813 * Return: index into SLI4 fast-path FCP queue index.
7814 **/
James Smart2a76a282012-08-03 12:35:54 -04007815static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007816lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007817{
James Smart2a76a282012-08-03 12:35:54 -04007818 int i;
James Smart4f774512009-05-22 14:52:35 -04007819
James Smart2a76a282012-08-03 12:35:54 -04007820 i = atomic_add_return(1, &phba->fcp_qidx);
7821 i = (i % phba->cfg_fcp_wq_count);
7822 return i;
James Smart4f774512009-05-22 14:52:35 -04007823}
7824
7825/**
7826 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7827 * @phba: Pointer to HBA context object.
7828 * @piocb: Pointer to command iocb.
7829 * @wqe: Pointer to the work queue entry.
7830 *
7831 * This routine converts the iocb command to its Work Queue Entry
7832 * equivalent. The wqe pointer should not have any fields set when
7833 * this routine is called because it will memcpy over them.
7834 * This routine does not set the CQ_ID or the WQEC bits in the
7835 * wqe.
7836 *
7837 * Returns: 0 = Success, IOCB_ERROR = Failure.
7838 **/
7839static int
7840lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7841 union lpfc_wqe *wqe)
7842{
James Smart5ffc2662009-11-18 15:39:44 -05007843 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007844 uint8_t ct = 0;
7845 uint32_t fip;
7846 uint32_t abort_tag;
7847 uint8_t command_type = ELS_COMMAND_NON_FIP;
7848 uint8_t cmnd;
7849 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007850 uint16_t abrt_iotag;
7851 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007852 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007853 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007854 int numBdes, i;
7855 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007856 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007857 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007858 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007859
James Smart45ed1192009-10-02 15:17:02 -04007860 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007861 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007862 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007863 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007864 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007865 command_type = ELS_COMMAND_FIP;
7866 else
7867 command_type = ELS_COMMAND_NON_FIP;
7868
James Smart4f774512009-05-22 14:52:35 -04007869 /* Some of the fields are in the right position already */
7870 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7871 abort_tag = (uint32_t) iocbq->iotag;
7872 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007873 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007874 /* words0-2 bpl convert bde */
7875 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007876 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7877 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007878 bpl = (struct ulp_bde64 *)
7879 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7880 if (!bpl)
7881 return IOCB_ERROR;
7882
7883 /* Should already be byte swapped. */
7884 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7885 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7886 /* swap the size field back to the cpu so we
7887 * can assign it to the sgl.
7888 */
7889 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007890 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7891 total_len = 0;
7892 for (i = 0; i < numBdes; i++) {
7893 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7894 total_len += bde.tus.f.bdeSize;
7895 }
James Smart4f774512009-05-22 14:52:35 -04007896 } else
James Smart5ffc2662009-11-18 15:39:44 -05007897 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007898
7899 iocbq->iocb.ulpIoTag = iocbq->iotag;
7900 cmnd = iocbq->iocb.ulpCommand;
7901
7902 switch (iocbq->iocb.ulpCommand) {
7903 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04007904 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7905 ndlp = iocbq->context_un.ndlp;
7906 else
7907 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007908 if (!iocbq->iocb.ulpLe) {
7909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7910 "2007 Only Limited Edition cmd Format"
7911 " supported 0x%x\n",
7912 iocbq->iocb.ulpCommand);
7913 return IOCB_ERROR;
7914 }
James Smartff78d8f2011-12-13 13:21:35 -05007915
James Smart5ffc2662009-11-18 15:39:44 -05007916 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007917 /* Els_reguest64 has a TMO */
7918 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7919 iocbq->iocb.ulpTimeout);
7920 /* Need a VF for word 4 set the vf bit*/
7921 bf_set(els_req64_vf, &wqe->els_req, 0);
7922 /* And a VFID for word 12 */
7923 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007924 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007925 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7926 iocbq->iocb.ulpContext);
7927 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7928 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007929 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007930 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007931 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7932 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007933 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7934 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007935 if_type = bf_get(lpfc_sli_intf_if_type,
7936 &phba->sli4_hba.sli_intf);
7937 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007938 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007939 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007940 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05007941 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05007942 *pcmd == ELS_CMD_PLOGI)) {
7943 bf_set(els_req64_sp, &wqe->els_req, 1);
7944 bf_set(els_req64_sid, &wqe->els_req,
7945 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04007946 if ((*pcmd == ELS_CMD_FLOGI) &&
7947 !(phba->fc_topology ==
7948 LPFC_TOPOLOGY_LOOP))
7949 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05007950 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7951 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04007952 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007953 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007954 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7955 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7956 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7957 }
James Smartc8685952009-11-18 15:39:16 -05007958 }
James Smart6d368e52011-05-24 11:44:12 -04007959 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7960 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007961 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7962 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7963 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7964 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7965 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7966 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007967 break;
James Smart5ffc2662009-11-18 15:39:44 -05007968 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007969 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7970 iocbq->iocb.un.ulpWord[3]);
7971 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007972 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007973 /* The entire sequence is transmitted for this IOCB */
7974 xmit_len = total_len;
7975 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05007976 if (phba->link_flag & LS_LOOPBACK_MODE)
7977 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04007978 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007979 /* word3 iocb=io_tag32 wqe=reserved */
7980 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007981 /* word4 relative_offset memcpy */
7982 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007983 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7984 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7985 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7986 LPFC_WQE_IOD_WRITE);
7987 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7988 LPFC_WQE_LENLOC_WORD12);
7989 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007990 wqe->xmit_sequence.xmit_len = xmit_len;
7991 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007992 break;
James Smart4f774512009-05-22 14:52:35 -04007993 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007994 /* word3 iocb=iotag32 wqe=seq_payload_len */
7995 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007996 /* word4 iocb=rsvd wqe=rsvd */
7997 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7998 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007999 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008000 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008001 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8002 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8003 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8004 LPFC_WQE_LENLOC_WORD3);
8005 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008006 break;
James Smart4f774512009-05-22 14:52:35 -04008007 case CMD_FCP_IWRITE64_CR:
8008 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008009 /* word3 iocb=iotag wqe=payload_offset_len */
8010 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8011 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05008012 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04008013 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8014 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8015 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8016 iocbq->iocb.ulpFCP2Rcvy);
8017 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8018 /* Always open the exchange */
8019 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008020 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8021 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8022 LPFC_WQE_LENLOC_WORD4);
8023 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8024 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008025 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8026 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8027 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8028 }
8029 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008030 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008031 case CMD_FCP_IREAD64_CR:
8032 /* word3 iocb=iotag wqe=payload_offset_len */
8033 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8034 wqe->fcp_iread.payload_offset_len =
8035 xmit_len + sizeof(struct fcp_rsp);
8036 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8037 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8038 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8039 iocbq->iocb.ulpFCP2Rcvy);
8040 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008041 /* Always open the exchange */
8042 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008043 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8044 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8045 LPFC_WQE_LENLOC_WORD4);
8046 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8047 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008048 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8049 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8050 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8051 }
8052 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008053 break;
James Smartf1126682009-06-10 17:22:44 -04008054 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008055 /* word3 iocb=IO_TAG wqe=reserved */
8056 wqe->fcp_icmd.rsrvd3 = 0;
8057 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008058 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008059 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8060 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8061 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8062 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8063 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8064 LPFC_WQE_LENLOC_NONE);
8065 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008066 break;
James Smart4f774512009-05-22 14:52:35 -04008067 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008068 /* For this command calculate the xmit length of the
8069 * request bde.
8070 */
8071 xmit_len = 0;
8072 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8073 sizeof(struct ulp_bde64);
8074 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008075 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008076 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8077 break;
James Smart63e801c2010-11-20 23:14:19 -05008078 xmit_len += bde.tus.f.bdeSize;
8079 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008080 /* word3 iocb=IO_TAG wqe=request_payload_len */
8081 wqe->gen_req.request_payload_len = xmit_len;
8082 /* word4 iocb=parameter wqe=relative_offset memcpy */
8083 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008084 /* word6 context tag copied in memcpy */
8085 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8086 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8088 "2015 Invalid CT %x command 0x%x\n",
8089 ct, iocbq->iocb.ulpCommand);
8090 return IOCB_ERROR;
8091 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008092 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8093 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8094 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8095 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8096 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8097 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8098 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8099 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008100 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008101 break;
James Smart4f774512009-05-22 14:52:35 -04008102 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008103 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008104 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008105 /* word3 iocb=iotag32 wqe=response_payload_len */
8106 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008107 /* word4 */
8108 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008109 /* word5 iocb=rsvd wge=did */
8110 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008111 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8112
8113 if_type = bf_get(lpfc_sli_intf_if_type,
8114 &phba->sli4_hba.sli_intf);
8115 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8116 if (iocbq->vport->fc_flag & FC_PT2PT) {
8117 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8118 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8119 iocbq->vport->fc_myDID);
8120 if (iocbq->vport->fc_myDID == Fabric_DID) {
8121 bf_set(wqe_els_did,
8122 &wqe->xmit_els_rsp.wqe_dest, 0);
8123 }
8124 }
8125 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008126 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8127 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8128 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8129 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008130 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008131 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008132 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008133 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008134 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8135 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8136 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8137 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8138 LPFC_WQE_LENLOC_WORD3);
8139 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008140 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8141 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008142 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8143 iocbq->context2)->virt);
8144 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008145 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8146 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008147 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008148 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8149 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008150 phba->vpi_ids[phba->pport->vpi]);
8151 }
James Smart4f774512009-05-22 14:52:35 -04008152 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008153 break;
James Smart4f774512009-05-22 14:52:35 -04008154 case CMD_CLOSE_XRI_CN:
8155 case CMD_ABORT_XRI_CN:
8156 case CMD_ABORT_XRI_CX:
8157 /* words 0-2 memcpy should be 0 rserved */
8158 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008159 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8160 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8161 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8162 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8163 } else
8164 fip = 0;
8165
8166 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008167 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008168 * The link is down, or the command was ELS_FIP
8169 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008170 * on the wire.
8171 */
8172 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8173 else
8174 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8175 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008176 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8177 wqe->abort_cmd.rsrvd5 = 0;
8178 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008179 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8180 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008181 /*
8182 * The abort handler will send us CMD_ABORT_XRI_CN or
8183 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8184 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008185 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8186 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8187 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8188 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008189 cmnd = CMD_ABORT_XRI_CX;
8190 command_type = OTHER_COMMAND;
8191 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008192 break;
James Smart6669f9b2009-10-02 15:16:45 -04008193 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008194 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008195 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008196 * we re-construct this WQE here based on information in
8197 * iocbq from scratch.
8198 */
8199 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008200 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008201 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008202 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8203 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008204 LPFC_ABTS_UNSOL_INT) {
8205 /* ABTS sent by initiator to CT exchange, the
8206 * RX_ID field will be filled with the newly
8207 * allocated responder XRI.
8208 */
8209 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8210 iocbq->sli4_xritag);
8211 } else {
8212 /* ABTS sent by responder to CT exchange, the
8213 * RX_ID field will be filled with the responder
8214 * RX_ID from ABTS.
8215 */
8216 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008217 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008218 }
James Smart6669f9b2009-10-02 15:16:45 -04008219 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8220 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008221
8222 /* Use CT=VPI */
8223 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8224 ndlp->nlp_DID);
8225 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8226 iocbq->iocb.ulpContext);
8227 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008228 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008229 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008230 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8231 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8232 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008233 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8234 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008235 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8236 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8237 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8238 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8239 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8240 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8241 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8242 }
8243
James Smart7851fe22011-07-22 18:36:52 -04008244 break;
James Smart4f774512009-05-22 14:52:35 -04008245 case CMD_XRI_ABORTED_CX:
8246 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008247 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8248 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8249 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8250 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8251 default:
8252 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8253 "2014 Invalid command 0x%x\n",
8254 iocbq->iocb.ulpCommand);
8255 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008256 break;
James Smart4f774512009-05-22 14:52:35 -04008257 }
James Smart6d368e52011-05-24 11:44:12 -04008258
James Smartf0d9bcc2010-10-22 11:07:09 -04008259 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8260 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8261 wqe->generic.wqe_com.abort_tag = abort_tag;
8262 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8263 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8264 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8265 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008266 return 0;
8267}
8268
8269/**
8270 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8271 * @phba: Pointer to HBA context object.
8272 * @ring_number: SLI ring number to issue iocb on.
8273 * @piocb: Pointer to command iocb.
8274 * @flag: Flag indicating if this command can be put into txq.
8275 *
8276 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8277 * an iocb command to an HBA with SLI-4 interface spec.
8278 *
8279 * This function is called with hbalock held. The function will return success
8280 * after it successfully submit the iocb to firmware or after adding to the
8281 * txq.
8282 **/
8283static int
8284__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8285 struct lpfc_iocbq *piocb, uint32_t flag)
8286{
8287 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008288 union lpfc_wqe wqe;
8289 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008290
8291 if (piocb->sli4_xritag == NO_XRI) {
8292 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008293 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008294 sglq = NULL;
8295 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008296 if (pring->txq_cnt) {
8297 if (!(flag & SLI_IOCB_RET_IOCB)) {
8298 __lpfc_sli_ringtx_put(phba,
8299 pring, piocb);
8300 return IOCB_SUCCESS;
8301 } else {
8302 return IOCB_BUSY;
8303 }
8304 } else {
James Smart6d368e52011-05-24 11:44:12 -04008305 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008306 if (!sglq) {
8307 if (!(flag & SLI_IOCB_RET_IOCB)) {
8308 __lpfc_sli_ringtx_put(phba,
8309 pring,
8310 piocb);
8311 return IOCB_SUCCESS;
8312 } else
8313 return IOCB_BUSY;
8314 }
8315 }
James Smart4f774512009-05-22 14:52:35 -04008316 }
8317 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008318 /* These IO's already have an XRI and a mapped sgl. */
8319 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008320 } else {
James Smart6d368e52011-05-24 11:44:12 -04008321 /*
8322 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008323 * sglq is on the active list
8324 */
8325 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8326 if (!sglq)
8327 return IOCB_ERROR;
8328 }
8329
8330 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008331 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008332 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008333 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008334 return IOCB_ERROR;
8335 }
8336
8337 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8338 return IOCB_ERROR;
8339
James Smart341af102010-01-26 23:07:37 -05008340 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8341 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008342 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8343 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008344 return IOCB_ERROR;
8345 } else {
8346 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8347 return IOCB_ERROR;
8348 }
8349 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8350
8351 return 0;
8352}
8353
8354/**
James Smart3772a992009-05-22 14:50:54 -04008355 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8356 *
8357 * This routine wraps the actual lockless version for issusing IOCB function
8358 * pointer from the lpfc_hba struct.
8359 *
8360 * Return codes:
8361 * IOCB_ERROR - Error
8362 * IOCB_SUCCESS - Success
8363 * IOCB_BUSY - Busy
8364 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008365int
James Smart3772a992009-05-22 14:50:54 -04008366__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8367 struct lpfc_iocbq *piocb, uint32_t flag)
8368{
8369 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8370}
8371
8372/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008373 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008374 * @phba: The hba struct for which this call is being executed.
8375 * @dev_grp: The HBA PCI-Device group number.
8376 *
8377 * This routine sets up the SLI interface API function jump table in @phba
8378 * struct.
8379 * Returns: 0 - success, -ENODEV - failure.
8380 **/
8381int
8382lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8383{
8384
8385 switch (dev_grp) {
8386 case LPFC_PCI_DEV_LP:
8387 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8388 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8389 break;
James Smart4f774512009-05-22 14:52:35 -04008390 case LPFC_PCI_DEV_OC:
8391 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8392 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8393 break;
James Smart3772a992009-05-22 14:50:54 -04008394 default:
8395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8396 "1419 Invalid HBA PCI-device group: 0x%x\n",
8397 dev_grp);
8398 return -ENODEV;
8399 break;
8400 }
8401 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8402 return 0;
8403}
James Smart92d7f7b2007-06-17 19:56:38 -05008404
James Smarte59058c2008-08-24 21:49:00 -04008405/**
James Smart3621a712009-04-06 18:47:14 -04008406 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008407 * @phba: Pointer to HBA context object.
8408 * @pring: Pointer to driver SLI ring object.
8409 * @piocb: Pointer to command iocb.
8410 * @flag: Flag indicating if this command can be put into txq.
8411 *
8412 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8413 * function. This function gets the hbalock and calls
8414 * __lpfc_sli_issue_iocb function and will return the error returned
8415 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8416 * functions which do not hold hbalock.
8417 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008418int
James Smart3772a992009-05-22 14:50:54 -04008419lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008420 struct lpfc_iocbq *piocb, uint32_t flag)
8421{
James Smart2a76a282012-08-03 12:35:54 -04008422 struct lpfc_sli_ring *pring;
James Smart92d7f7b2007-06-17 19:56:38 -05008423 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008424 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008425
James Smart7e56aa22012-08-03 12:35:34 -04008426 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008427 if (piocb->iocb_flag & LPFC_IO_FCP) {
8428 if (unlikely(!phba->sli4_hba.fcp_wq))
8429 return IOCB_ERROR;
8430 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8431 piocb->fcp_wqidx = idx;
8432 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
8433 }
James Smart7e56aa22012-08-03 12:35:34 -04008434 pring = &phba->sli.ring[ring_number];
8435 spin_lock_irqsave(&pring->ring_lock, iflags);
8436 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8437 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8438 } else {
8439 /* For now, SLI2/3 will still use hbalock */
8440 spin_lock_irqsave(&phba->hbalock, iflags);
8441 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8442 spin_unlock_irqrestore(&phba->hbalock, iflags);
8443 }
James Smart92d7f7b2007-06-17 19:56:38 -05008444 return rc;
8445}
8446
James Smarte59058c2008-08-24 21:49:00 -04008447/**
James Smart3621a712009-04-06 18:47:14 -04008448 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008449 * @phba: Pointer to HBA context object.
8450 *
8451 * This function is called while driver attaches with the
8452 * HBA to setup the extra ring. The extra ring is used
8453 * only when driver needs to support target mode functionality
8454 * or IP over FC functionalities.
8455 *
8456 * This function is called with no lock held.
8457 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008458static int
8459lpfc_extra_ring_setup( struct lpfc_hba *phba)
8460{
8461 struct lpfc_sli *psli;
8462 struct lpfc_sli_ring *pring;
8463
8464 psli = &phba->sli;
8465
8466 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008467
8468 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008469 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008470 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8471 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8472 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8473 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008474
James Smarta4bc3372006-12-02 13:34:16 -05008475 /* and give them to the extra ring */
8476 pring = &psli->ring[psli->extra_ring];
8477
James Smart7e56aa22012-08-03 12:35:34 -04008478 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8479 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8480 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8481 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008482
8483 /* Setup default profile for this ring */
8484 pring->iotag_max = 4096;
8485 pring->num_mask = 1;
8486 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008487 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8488 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008489 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8490 return 0;
8491}
8492
James Smartcb69f7d2011-12-13 13:21:57 -05008493/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8494 * @vport: pointer to virtual port object.
8495 * @ndlp: nodelist pointer for the impacted rport.
8496 *
8497 * The driver calls this routine in response to a XRI ABORT CQE
8498 * event from the port. In this event, the driver is required to
8499 * recover its login to the rport even though its login may be valid
8500 * from the driver's perspective. The failed ABTS notice from the
8501 * port indicates the rport is not responding.
8502 */
8503static void
8504lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8505 struct lpfc_nodelist *ndlp)
8506{
8507 struct Scsi_Host *shost;
8508 struct lpfc_hba *phba;
8509 unsigned long flags = 0;
8510
8511 shost = lpfc_shost_from_vport(vport);
8512 phba = vport->phba;
8513 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8514 lpfc_printf_log(phba, KERN_INFO,
8515 LOG_SLI, "3093 No rport recovery needed. "
8516 "rport in state 0x%x\n",
8517 ndlp->nlp_state);
8518 return;
8519 }
8520 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8521 "3094 Start rport recovery on shost id 0x%x "
8522 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8523 "flags 0x%x\n",
8524 shost->host_no, ndlp->nlp_DID,
8525 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8526 ndlp->nlp_flag);
8527 /*
8528 * The rport is not responding. Don't attempt ADISC recovery.
8529 * Remove the FCP-2 flag to force a PLOGI.
8530 */
8531 spin_lock_irqsave(shost->host_lock, flags);
8532 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8533 spin_unlock_irqrestore(shost->host_lock, flags);
8534 lpfc_disc_state_machine(vport, ndlp, NULL,
8535 NLP_EVT_DEVICE_RECOVERY);
8536 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8537 spin_lock_irqsave(shost->host_lock, flags);
8538 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8539 spin_unlock_irqrestore(shost->host_lock, flags);
8540 lpfc_disc_start(vport);
8541}
8542
8543/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8544 * @phba: Pointer to HBA context object.
8545 * @iocbq: Pointer to iocb object.
8546 *
8547 * The async_event handler calls this routine when it receives
8548 * an ASYNC_STATUS_CN event from the port. The port generates
8549 * this event when an Abort Sequence request to an rport fails
8550 * twice in succession. The abort could be originated by the
8551 * driver or by the port. The ABTS could have been for an ELS
8552 * or FCP IO. The port only generates this event when an ABTS
8553 * fails to complete after one retry.
8554 */
8555static void
8556lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8557 struct lpfc_iocbq *iocbq)
8558{
8559 struct lpfc_nodelist *ndlp = NULL;
8560 uint16_t rpi = 0, vpi = 0;
8561 struct lpfc_vport *vport = NULL;
8562
8563 /* The rpi in the ulpContext is vport-sensitive. */
8564 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8565 rpi = iocbq->iocb.ulpContext;
8566
8567 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8568 "3092 Port generated ABTS async event "
8569 "on vpi %d rpi %d status 0x%x\n",
8570 vpi, rpi, iocbq->iocb.ulpStatus);
8571
8572 vport = lpfc_find_vport_by_vpid(phba, vpi);
8573 if (!vport)
8574 goto err_exit;
8575 ndlp = lpfc_findnode_rpi(vport, rpi);
8576 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8577 goto err_exit;
8578
8579 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8580 lpfc_sli_abts_recover_port(vport, ndlp);
8581 return;
8582
8583 err_exit:
8584 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8585 "3095 Event Context not found, no "
8586 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8587 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8588 vpi, rpi);
8589}
8590
8591/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8592 * @phba: pointer to HBA context object.
8593 * @ndlp: nodelist pointer for the impacted rport.
8594 * @axri: pointer to the wcqe containing the failed exchange.
8595 *
8596 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8597 * port. The port generates this event when an abort exchange request to an
8598 * rport fails twice in succession with no reply. The abort could be originated
8599 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8600 */
8601void
8602lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8603 struct lpfc_nodelist *ndlp,
8604 struct sli4_wcqe_xri_aborted *axri)
8605{
8606 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008607 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008608
James Smart6b5151f2012-01-18 16:24:06 -05008609 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8611 "3115 Node Context not found, driver "
8612 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008613 return;
8614 }
8615
James Smartcb69f7d2011-12-13 13:21:57 -05008616 vport = ndlp->vport;
8617 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8618 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008619 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008620 ndlp->vport->vpi, ndlp->nlp_rpi,
8621 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008622 bf_get(lpfc_wcqe_xa_status, axri),
8623 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008624
James Smart5c1db2a2012-03-01 22:34:36 -05008625 /*
8626 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8627 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8628 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8629 */
8630 ext_status = axri->parameter & WCQE_PARAM_MASK;
8631 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8632 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008633 lpfc_sli_abts_recover_port(vport, ndlp);
8634}
8635
James Smarte59058c2008-08-24 21:49:00 -04008636/**
James Smart3621a712009-04-06 18:47:14 -04008637 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008638 * @phba: Pointer to HBA context object.
8639 * @pring: Pointer to driver SLI ring object.
8640 * @iocbq: Pointer to iocb object.
8641 *
8642 * This function is called by the slow ring event handler
8643 * function when there is an ASYNC event iocb in the ring.
8644 * This function is called with no lock held.
8645 * Currently this function handles only temperature related
8646 * ASYNC events. The function decodes the temperature sensor
8647 * event message and posts events for the management applications.
8648 **/
James Smart98c9ea52007-10-27 13:37:33 -04008649static void
James Smart57127f12007-10-27 13:37:05 -04008650lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8651 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8652{
8653 IOCB_t *icmd;
8654 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008655 struct temp_event temp_event_data;
8656 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008657 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008658
8659 icmd = &iocbq->iocb;
8660 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008661
James Smartcb69f7d2011-12-13 13:21:57 -05008662 switch (evt_code) {
8663 case ASYNC_TEMP_WARN:
8664 case ASYNC_TEMP_SAFE:
8665 temp_event_data.data = (uint32_t) icmd->ulpContext;
8666 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8667 if (evt_code == ASYNC_TEMP_WARN) {
8668 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8669 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8670 "0347 Adapter is very hot, please take "
8671 "corrective action. temperature : %d Celsius\n",
8672 (uint32_t) icmd->ulpContext);
8673 } else {
8674 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8675 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8676 "0340 Adapter temperature is OK now. "
8677 "temperature : %d Celsius\n",
8678 (uint32_t) icmd->ulpContext);
8679 }
8680
8681 /* Send temperature change event to applications */
8682 shost = lpfc_shost_from_vport(phba->pport);
8683 fc_host_post_vendor_event(shost, fc_get_event_number(),
8684 sizeof(temp_event_data), (char *) &temp_event_data,
8685 LPFC_NL_VENDOR_ID);
8686 break;
8687 case ASYNC_STATUS_CN:
8688 lpfc_sli_abts_err_handler(phba, iocbq);
8689 break;
8690 default:
James Smarta257bf92009-04-06 18:48:10 -04008691 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008693 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008694 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008695 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8696 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8697 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8698 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008699 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008700 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8701 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8702 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8703 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8704
James Smartcb69f7d2011-12-13 13:21:57 -05008705 break;
James Smart57127f12007-10-27 13:37:05 -04008706 }
James Smart57127f12007-10-27 13:37:05 -04008707}
8708
8709
James Smarte59058c2008-08-24 21:49:00 -04008710/**
James Smart3621a712009-04-06 18:47:14 -04008711 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008712 * @phba: Pointer to HBA context object.
8713 *
8714 * lpfc_sli_setup sets up rings of the SLI interface with
8715 * number of iocbs per ring and iotags. This function is
8716 * called while driver attach to the HBA and before the
8717 * interrupts are enabled. So there is no need for locking.
8718 *
8719 * This function always returns 0.
8720 **/
dea31012005-04-17 16:05:31 -05008721int
8722lpfc_sli_setup(struct lpfc_hba *phba)
8723{
James Smarted957682007-06-17 19:56:37 -05008724 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008725 struct lpfc_sli *psli = &phba->sli;
8726 struct lpfc_sli_ring *pring;
8727
James Smart2a76a282012-08-03 12:35:54 -04008728 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8729 if (phba->sli_rev == LPFC_SLI_REV4)
8730 psli->num_rings += phba->cfg_fcp_eq_count;
dea31012005-04-17 16:05:31 -05008731 psli->sli_flag = 0;
8732 psli->fcp_ring = LPFC_FCP_RING;
8733 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008734 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008735
James Bottomley604a3e32005-10-29 10:28:33 -05008736 psli->iocbq_lookup = NULL;
8737 psli->iocbq_lookup_len = 0;
8738 psli->last_iotag = 0;
8739
dea31012005-04-17 16:05:31 -05008740 for (i = 0; i < psli->num_rings; i++) {
8741 pring = &psli->ring[i];
8742 switch (i) {
8743 case LPFC_FCP_RING: /* ring 0 - FCP */
8744 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008745 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8746 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8747 pring->sli.sli3.numCiocb +=
8748 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8749 pring->sli.sli3.numRiocb +=
8750 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8751 pring->sli.sli3.numCiocb +=
8752 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8753 pring->sli.sli3.numRiocb +=
8754 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8755 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008756 SLI3_IOCB_CMD_SIZE :
8757 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008758 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008759 SLI3_IOCB_RSP_SIZE :
8760 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008761 pring->iotag_ctr = 0;
8762 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008763 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008764 pring->fast_iotag = pring->iotag_max;
8765 pring->num_mask = 0;
8766 break;
James Smarta4bc3372006-12-02 13:34:16 -05008767 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008768 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008769 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8770 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8771 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008772 SLI3_IOCB_CMD_SIZE :
8773 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008774 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008775 SLI3_IOCB_RSP_SIZE :
8776 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008777 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008778 pring->num_mask = 0;
8779 break;
8780 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8781 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008782 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8783 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8784 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008785 SLI3_IOCB_CMD_SIZE :
8786 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008787 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008788 SLI3_IOCB_RSP_SIZE :
8789 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008790 pring->fast_iotag = 0;
8791 pring->iotag_ctr = 0;
8792 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008793 pring->lpfc_sli_rcv_async_status =
8794 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008795 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008796 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008797 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8798 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008799 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008800 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008801 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008802 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8803 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008804 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008805 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008806 pring->prt[2].profile = 0; /* Mask 2 */
8807 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008808 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008809 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008810 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008811 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008812 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008813 pring->prt[3].profile = 0; /* Mask 3 */
8814 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008815 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008816 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008817 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008818 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008819 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008820 /* abort unsolicited sequence */
8821 pring->prt[4].profile = 0; /* Mask 4 */
8822 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8823 pring->prt[4].type = FC_TYPE_BLS;
8824 pring->prt[4].lpfc_sli_rcv_unsol_event =
8825 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008826 break;
8827 }
James Smart7e56aa22012-08-03 12:35:34 -04008828 totiocbsize += (pring->sli.sli3.numCiocb *
8829 pring->sli.sli3.sizeCiocb) +
8830 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05008831 }
James Smarted957682007-06-17 19:56:37 -05008832 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008833 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008834 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8835 "SLI2 SLIM Data: x%x x%lx\n",
8836 phba->brd_no, totiocbsize,
8837 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008838 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008839 if (phba->cfg_multi_ring_support == 2)
8840 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008841
8842 return 0;
8843}
8844
James Smarte59058c2008-08-24 21:49:00 -04008845/**
James Smart3621a712009-04-06 18:47:14 -04008846 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008847 * @phba: Pointer to HBA context object.
8848 *
8849 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8850 * ring. This function also initializes ring indices of each ring.
8851 * This function is called during the initialization of the SLI
8852 * interface of an HBA.
8853 * This function is called with no lock held and always returns
8854 * 1.
8855 **/
dea31012005-04-17 16:05:31 -05008856int
James Smart2e0fef82007-06-17 19:56:36 -05008857lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008858{
8859 struct lpfc_sli *psli;
8860 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008861 int i;
dea31012005-04-17 16:05:31 -05008862
8863 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008864 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008865 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008866 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008867 /* Initialize list headers for txq and txcmplq as double linked lists */
8868 for (i = 0; i < psli->num_rings; i++) {
8869 pring = &psli->ring[i];
8870 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04008871 pring->sli.sli3.next_cmdidx = 0;
8872 pring->sli.sli3.local_getidx = 0;
8873 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05008874 INIT_LIST_HEAD(&pring->txq);
8875 INIT_LIST_HEAD(&pring->txcmplq);
8876 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008877 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008878 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04008879 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05008880 }
James Smart2e0fef82007-06-17 19:56:36 -05008881 spin_unlock_irq(&phba->hbalock);
8882 return 1;
dea31012005-04-17 16:05:31 -05008883}
8884
James Smarte59058c2008-08-24 21:49:00 -04008885/**
James Smart04c68492009-05-22 14:52:52 -04008886 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8887 * @phba: Pointer to HBA context object.
8888 *
8889 * This routine flushes the mailbox command subsystem. It will unconditionally
8890 * flush all the mailbox commands in the three possible stages in the mailbox
8891 * command sub-system: pending mailbox command queue; the outstanding mailbox
8892 * command; and completed mailbox command queue. It is caller's responsibility
8893 * to make sure that the driver is in the proper state to flush the mailbox
8894 * command sub-system. Namely, the posting of mailbox commands into the
8895 * pending mailbox command queue from the various clients must be stopped;
8896 * either the HBA is in a state that it will never works on the outstanding
8897 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8898 * mailbox command has been completed.
8899 **/
8900static void
8901lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8902{
8903 LIST_HEAD(completions);
8904 struct lpfc_sli *psli = &phba->sli;
8905 LPFC_MBOXQ_t *pmb;
8906 unsigned long iflag;
8907
8908 /* Flush all the mailbox commands in the mbox system */
8909 spin_lock_irqsave(&phba->hbalock, iflag);
8910 /* The pending mailbox command queue */
8911 list_splice_init(&phba->sli.mboxq, &completions);
8912 /* The outstanding active mailbox command */
8913 if (psli->mbox_active) {
8914 list_add_tail(&psli->mbox_active->list, &completions);
8915 psli->mbox_active = NULL;
8916 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8917 }
8918 /* The completed mailbox command queue */
8919 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8920 spin_unlock_irqrestore(&phba->hbalock, iflag);
8921
8922 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8923 while (!list_empty(&completions)) {
8924 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8925 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8926 if (pmb->mbox_cmpl)
8927 pmb->mbox_cmpl(phba, pmb);
8928 }
8929}
8930
8931/**
James Smart3621a712009-04-06 18:47:14 -04008932 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008933 * @vport: Pointer to virtual port object.
8934 *
8935 * lpfc_sli_host_down is called to clean up the resources
8936 * associated with a vport before destroying virtual
8937 * port data structures.
8938 * This function does following operations:
8939 * - Free discovery resources associated with this virtual
8940 * port.
8941 * - Free iocbs associated with this virtual port in
8942 * the txq.
8943 * - Send abort for all iocb commands associated with this
8944 * vport in txcmplq.
8945 *
8946 * This function is called with no lock held and always returns 1.
8947 **/
dea31012005-04-17 16:05:31 -05008948int
James Smart92d7f7b2007-06-17 19:56:38 -05008949lpfc_sli_host_down(struct lpfc_vport *vport)
8950{
James Smart858c9f62007-06-17 19:56:39 -05008951 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008952 struct lpfc_hba *phba = vport->phba;
8953 struct lpfc_sli *psli = &phba->sli;
8954 struct lpfc_sli_ring *pring;
8955 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008956 int i;
8957 unsigned long flags = 0;
8958 uint16_t prev_pring_flag;
8959
8960 lpfc_cleanup_discovery_resources(vport);
8961
8962 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008963 for (i = 0; i < psli->num_rings; i++) {
8964 pring = &psli->ring[i];
8965 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008966 /* Only slow rings */
8967 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008968 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008969 /* Set the lpfc data pending flag */
8970 set_bit(LPFC_DATA_READY, &phba->data_flags);
8971 }
James Smart92d7f7b2007-06-17 19:56:38 -05008972 /*
8973 * Error everything on the txq since these iocbs have not been
8974 * given to the FW yet.
8975 */
James Smart92d7f7b2007-06-17 19:56:38 -05008976 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8977 if (iocb->vport != vport)
8978 continue;
James Smart858c9f62007-06-17 19:56:39 -05008979 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008980 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008981 }
8982
8983 /* Next issue ABTS for everything on the txcmplq */
8984 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8985 list) {
8986 if (iocb->vport != vport)
8987 continue;
8988 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8989 }
8990
8991 pring->flag = prev_pring_flag;
8992 }
8993
8994 spin_unlock_irqrestore(&phba->hbalock, flags);
8995
James Smarta257bf92009-04-06 18:48:10 -04008996 /* Cancel all the IOCBs from the completions list */
8997 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8998 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008999 return 1;
9000}
9001
James Smarte59058c2008-08-24 21:49:00 -04009002/**
James Smart3621a712009-04-06 18:47:14 -04009003 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009004 * @phba: Pointer to HBA context object.
9005 *
9006 * This function cleans up all iocb, buffers, mailbox commands
9007 * while shutting down the HBA. This function is called with no
9008 * lock held and always returns 1.
9009 * This function does the following to cleanup driver resources:
9010 * - Free discovery resources for each virtual port
9011 * - Cleanup any pending fabric iocbs
9012 * - Iterate through the iocb txq and free each entry
9013 * in the list.
9014 * - Free up any buffer posted to the HBA
9015 * - Free mailbox commands in the mailbox queue.
9016 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009017int
James Smart2e0fef82007-06-17 19:56:36 -05009018lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009019{
James Smart2534ba72007-04-25 09:52:20 -04009020 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009021 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009022 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009023 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009024 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009025 int i;
9026
9027 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009028 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009029
dea31012005-04-17 16:05:31 -05009030 lpfc_hba_down_prep(phba);
9031
James Smart92d7f7b2007-06-17 19:56:38 -05009032 lpfc_fabric_abort_hba(phba);
9033
James Smart2e0fef82007-06-17 19:56:36 -05009034 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009035 for (i = 0; i < psli->num_rings; i++) {
9036 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009037 /* Only slow rings */
9038 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009039 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009040 /* Set the lpfc data pending flag */
9041 set_bit(LPFC_DATA_READY, &phba->data_flags);
9042 }
dea31012005-04-17 16:05:31 -05009043
9044 /*
9045 * Error everything on the txq since these iocbs have not been
9046 * given to the FW yet.
9047 */
James Smart2534ba72007-04-25 09:52:20 -04009048 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009049 pring->txq_cnt = 0;
9050
dea31012005-04-17 16:05:31 -05009051 }
James Smart2e0fef82007-06-17 19:56:36 -05009052 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009053
James Smarta257bf92009-04-06 18:48:10 -04009054 /* Cancel all the IOCBs from the completions list */
9055 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9056 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009057
James Smart0ff10d42008-01-11 01:52:36 -05009058 spin_lock_irqsave(&phba->hbalock, flags);
9059 list_splice_init(&phba->elsbuf, &completions);
9060 phba->elsbuf_cnt = 0;
9061 phba->elsbuf_prev_cnt = 0;
9062 spin_unlock_irqrestore(&phba->hbalock, flags);
9063
9064 while (!list_empty(&completions)) {
9065 list_remove_head(&completions, buf_ptr,
9066 struct lpfc_dmabuf, list);
9067 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9068 kfree(buf_ptr);
9069 }
9070
dea31012005-04-17 16:05:31 -05009071 /* Return any active mbox cmds */
9072 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009073
James Smartda0436e2009-05-22 14:51:39 -04009074 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009075 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009076 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009077
James Smartda0436e2009-05-22 14:51:39 -04009078 return 1;
9079}
James Smart92d7f7b2007-06-17 19:56:38 -05009080
James Smartda0436e2009-05-22 14:51:39 -04009081/**
James Smart3621a712009-04-06 18:47:14 -04009082 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009083 * @srcp: Source memory pointer.
9084 * @destp: Destination memory pointer.
9085 * @cnt: Number of words required to be copied.
9086 *
9087 * This function is used for copying data between driver memory
9088 * and the SLI memory. This function also changes the endianness
9089 * of each word if native endianness is different from SLI
9090 * endianness. This function can be called with or without
9091 * lock.
9092 **/
dea31012005-04-17 16:05:31 -05009093void
9094lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9095{
9096 uint32_t *src = srcp;
9097 uint32_t *dest = destp;
9098 uint32_t ldata;
9099 int i;
9100
9101 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9102 ldata = *src;
9103 ldata = le32_to_cpu(ldata);
9104 *dest = ldata;
9105 src++;
9106 dest++;
9107 }
9108}
9109
James Smarte59058c2008-08-24 21:49:00 -04009110
9111/**
James Smarta0c87cb2009-07-19 10:01:10 -04009112 * lpfc_sli_bemem_bcopy - SLI memory copy function
9113 * @srcp: Source memory pointer.
9114 * @destp: Destination memory pointer.
9115 * @cnt: Number of words required to be copied.
9116 *
9117 * This function is used for copying data between a data structure
9118 * with big endian representation to local endianness.
9119 * This function can be called with or without lock.
9120 **/
9121void
9122lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9123{
9124 uint32_t *src = srcp;
9125 uint32_t *dest = destp;
9126 uint32_t ldata;
9127 int i;
9128
9129 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9130 ldata = *src;
9131 ldata = be32_to_cpu(ldata);
9132 *dest = ldata;
9133 src++;
9134 dest++;
9135 }
9136}
9137
9138/**
James Smart3621a712009-04-06 18:47:14 -04009139 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009140 * @phba: Pointer to HBA context object.
9141 * @pring: Pointer to driver SLI ring object.
9142 * @mp: Pointer to driver buffer object.
9143 *
9144 * This function is called with no lock held.
9145 * It always return zero after adding the buffer to the postbufq
9146 * buffer list.
9147 **/
dea31012005-04-17 16:05:31 -05009148int
James Smart2e0fef82007-06-17 19:56:36 -05009149lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9150 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009151{
9152 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9153 later */
James Smart2e0fef82007-06-17 19:56:36 -05009154 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009155 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009156 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009157 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009158 return 0;
9159}
9160
James Smarte59058c2008-08-24 21:49:00 -04009161/**
James Smart3621a712009-04-06 18:47:14 -04009162 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009163 * @phba: Pointer to HBA context object.
9164 *
9165 * When HBQ is enabled, buffers are searched based on tags. This function
9166 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9167 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9168 * does not conflict with tags of buffer posted for unsolicited events.
9169 * The function returns the allocated tag. The function is called with
9170 * no locks held.
9171 **/
James Smart76bb24e2007-10-27 13:38:00 -04009172uint32_t
9173lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9174{
9175 spin_lock_irq(&phba->hbalock);
9176 phba->buffer_tag_count++;
9177 /*
9178 * Always set the QUE_BUFTAG_BIT to distiguish between
9179 * a tag assigned by HBQ.
9180 */
9181 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9182 spin_unlock_irq(&phba->hbalock);
9183 return phba->buffer_tag_count;
9184}
9185
James Smarte59058c2008-08-24 21:49:00 -04009186/**
James Smart3621a712009-04-06 18:47:14 -04009187 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009188 * @phba: Pointer to HBA context object.
9189 * @pring: Pointer to driver SLI ring object.
9190 * @tag: Buffer tag.
9191 *
9192 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9193 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9194 * iocb is posted to the response ring with the tag of the buffer.
9195 * This function searches the pring->postbufq list using the tag
9196 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9197 * iocb. If the buffer is found then lpfc_dmabuf object of the
9198 * buffer is returned to the caller else NULL is returned.
9199 * This function is called with no lock held.
9200 **/
James Smart76bb24e2007-10-27 13:38:00 -04009201struct lpfc_dmabuf *
9202lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9203 uint32_t tag)
9204{
9205 struct lpfc_dmabuf *mp, *next_mp;
9206 struct list_head *slp = &pring->postbufq;
9207
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009208 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009209 spin_lock_irq(&phba->hbalock);
9210 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9211 if (mp->buffer_tag == tag) {
9212 list_del_init(&mp->list);
9213 pring->postbufq_cnt--;
9214 spin_unlock_irq(&phba->hbalock);
9215 return mp;
9216 }
9217 }
9218
9219 spin_unlock_irq(&phba->hbalock);
9220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009221 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009222 "ring %d Data x%lx x%p x%p x%x\n",
9223 pring->ringno, (unsigned long) tag,
9224 slp->next, slp->prev, pring->postbufq_cnt);
9225
9226 return NULL;
9227}
dea31012005-04-17 16:05:31 -05009228
James Smarte59058c2008-08-24 21:49:00 -04009229/**
James Smart3621a712009-04-06 18:47:14 -04009230 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009231 * @phba: Pointer to HBA context object.
9232 * @pring: Pointer to driver SLI ring object.
9233 * @phys: DMA address of the buffer.
9234 *
9235 * This function searches the buffer list using the dma_address
9236 * of unsolicited event to find the driver's lpfc_dmabuf object
9237 * corresponding to the dma_address. The function returns the
9238 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9239 * This function is called by the ct and els unsolicited event
9240 * handlers to get the buffer associated with the unsolicited
9241 * event.
9242 *
9243 * This function is called with no lock held.
9244 **/
dea31012005-04-17 16:05:31 -05009245struct lpfc_dmabuf *
9246lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9247 dma_addr_t phys)
9248{
9249 struct lpfc_dmabuf *mp, *next_mp;
9250 struct list_head *slp = &pring->postbufq;
9251
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009252 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009253 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009254 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9255 if (mp->phys == phys) {
9256 list_del_init(&mp->list);
9257 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009258 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009259 return mp;
9260 }
9261 }
9262
James Smart2e0fef82007-06-17 19:56:36 -05009263 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009265 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009266 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009267 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009268 slp->next, slp->prev, pring->postbufq_cnt);
9269 return NULL;
9270}
9271
James Smarte59058c2008-08-24 21:49:00 -04009272/**
James Smart3621a712009-04-06 18:47:14 -04009273 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009274 * @phba: Pointer to HBA context object.
9275 * @cmdiocb: Pointer to driver command iocb object.
9276 * @rspiocb: Pointer to driver response iocb object.
9277 *
9278 * This function is the completion handler for the abort iocbs for
9279 * ELS commands. This function is called from the ELS ring event
9280 * handler with no lock held. This function frees memory resources
9281 * associated with the abort iocb.
9282 **/
dea31012005-04-17 16:05:31 -05009283static void
James Smart2e0fef82007-06-17 19:56:36 -05009284lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9285 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009286{
James Smart2e0fef82007-06-17 19:56:36 -05009287 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009288 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009289 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009290
9291 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009292
9293 /*
9294 * Assume that the port already completed and returned, or
9295 * will return the iocb. Just Log the message.
9296 */
James Smart2680eea2007-04-25 09:52:55 -04009297 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9298 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9299
James Smart2e0fef82007-06-17 19:56:36 -05009300 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009301 if (phba->sli_rev < LPFC_SLI_REV4) {
9302 if (abort_iotag != 0 &&
9303 abort_iotag <= phba->sli.last_iotag)
9304 abort_iocb =
9305 phba->sli.iocbq_lookup[abort_iotag];
9306 } else
9307 /* For sli4 the abort_tag is the XRI,
9308 * so the abort routine puts the iotag of the iocb
9309 * being aborted in the context field of the abort
9310 * IOCB.
9311 */
9312 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009313
James Smart2a9bf3d2010-06-07 15:24:45 -04009314 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9315 "0327 Cannot abort els iocb %p "
9316 "with tag %x context %x, abort status %x, "
9317 "abort code %x\n",
9318 abort_iocb, abort_iotag, abort_context,
9319 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009320
James Smartff78d8f2011-12-13 13:21:35 -05009321 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009322 }
James Bottomley604a3e32005-10-29 10:28:33 -05009323 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009324 return;
9325}
9326
James Smarte59058c2008-08-24 21:49:00 -04009327/**
James Smart3621a712009-04-06 18:47:14 -04009328 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009329 * @phba: Pointer to HBA context object.
9330 * @cmdiocb: Pointer to driver command iocb object.
9331 * @rspiocb: Pointer to driver response iocb object.
9332 *
9333 * The function is called from SLI ring event handler with no
9334 * lock held. This function is the completion handler for ELS commands
9335 * which are aborted. The function frees memory resources used for
9336 * the aborted ELS commands.
9337 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009338static void
9339lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9340 struct lpfc_iocbq *rspiocb)
9341{
9342 IOCB_t *irsp = &rspiocb->iocb;
9343
9344 /* ELS cmd tag <ulpIoTag> completes */
9345 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009346 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009347 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009348 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009349 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009350 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9351 lpfc_ct_free_iocb(phba, cmdiocb);
9352 else
9353 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009354 return;
9355}
9356
James Smarte59058c2008-08-24 21:49:00 -04009357/**
James Smart5af5eee2010-10-22 11:06:38 -04009358 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009359 * @phba: Pointer to HBA context object.
9360 * @pring: Pointer to driver SLI ring object.
9361 * @cmdiocb: Pointer to driver command iocb object.
9362 *
James Smart5af5eee2010-10-22 11:06:38 -04009363 * This function issues an abort iocb for the provided command iocb down to
9364 * the port. Other than the case the outstanding command iocb is an abort
9365 * request, this function issues abort out unconditionally. This function is
9366 * called with hbalock held. The function returns 0 when it fails due to
9367 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009368 **/
James Smart5af5eee2010-10-22 11:06:38 -04009369static int
9370lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009371 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009372{
James Smart2e0fef82007-06-17 19:56:36 -05009373 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009374 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009375 IOCB_t *icmd = NULL;
9376 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009377 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009378 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009379
James Smart92d7f7b2007-06-17 19:56:38 -05009380 /*
9381 * There are certain command types we don't want to abort. And we
9382 * don't want to abort commands that are already in the process of
9383 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009384 */
9385 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009386 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009387 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9388 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009389 return 0;
9390
dea31012005-04-17 16:05:31 -05009391 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009392 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009393 if (abtsiocbp == NULL)
9394 return 0;
dea31012005-04-17 16:05:31 -05009395
James Smart07951072007-04-25 09:51:38 -04009396 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009397 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009398 */
9399 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9400
dea31012005-04-17 16:05:31 -05009401 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009402 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9403 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009404 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009405 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009406 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9407 }
James Smartda0436e2009-05-22 14:51:39 -04009408 else
9409 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009410 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009411 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009412
James Smart5ffc2662009-11-18 15:39:44 -05009413 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9414 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009415 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9416 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009417
James Smart2e0fef82007-06-17 19:56:36 -05009418 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009419 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9420 else
9421 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9422
9423 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009424
James Smarte8b62012007-08-02 11:10:09 -04009425 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9426 "0339 Abort xri x%x, original iotag x%x, "
9427 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009428 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009429 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009430 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009431
9432 if (phba->sli_rev == LPFC_SLI_REV4) {
9433 /* Note: both hbalock and ring_lock need to be set here */
9434 spin_lock_irqsave(&pring->ring_lock, iflags);
9435 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9436 abtsiocbp, 0);
9437 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9438 } else {
9439 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9440 abtsiocbp, 0);
9441 }
James Smart07951072007-04-25 09:51:38 -04009442
James Smartd7c255b2008-08-24 21:50:00 -04009443 if (retval)
9444 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009445
9446 /*
9447 * Caller to this routine should check for IOCB_ERROR
9448 * and handle it properly. This routine no longer removes
9449 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9450 */
9451 return retval;
9452}
9453
9454/**
9455 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9456 * @phba: Pointer to HBA context object.
9457 * @pring: Pointer to driver SLI ring object.
9458 * @cmdiocb: Pointer to driver command iocb object.
9459 *
9460 * This function issues an abort iocb for the provided command iocb. In case
9461 * of unloading, the abort iocb will not be issued to commands on the ELS
9462 * ring. Instead, the callback function shall be changed to those commands
9463 * so that nothing happens when them finishes. This function is called with
9464 * hbalock held. The function returns 0 when the command iocb is an abort
9465 * request.
9466 **/
9467int
9468lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9469 struct lpfc_iocbq *cmdiocb)
9470{
9471 struct lpfc_vport *vport = cmdiocb->vport;
9472 int retval = IOCB_ERROR;
9473 IOCB_t *icmd = NULL;
9474
9475 /*
9476 * There are certain command types we don't want to abort. And we
9477 * don't want to abort commands that are already in the process of
9478 * being aborted.
9479 */
9480 icmd = &cmdiocb->iocb;
9481 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9482 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9483 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9484 return 0;
9485
9486 /*
9487 * If we're unloading, don't abort iocb on the ELS ring, but change
9488 * the callback so that nothing happens when it finishes.
9489 */
9490 if ((vport->load_flag & FC_UNLOADING) &&
9491 (pring->ringno == LPFC_ELS_RING)) {
9492 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9493 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9494 else
9495 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9496 goto abort_iotag_exit;
9497 }
9498
9499 /* Now, we try to issue the abort to the cmdiocb out */
9500 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9501
James Smart07951072007-04-25 09:51:38 -04009502abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009503 /*
9504 * Caller to this routine should check for IOCB_ERROR
9505 * and handle it properly. This routine no longer removes
9506 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009507 */
James Smart2e0fef82007-06-17 19:56:36 -05009508 return retval;
dea31012005-04-17 16:05:31 -05009509}
9510
James Smarte59058c2008-08-24 21:49:00 -04009511/**
James Smart5af5eee2010-10-22 11:06:38 -04009512 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9513 * @phba: Pointer to HBA context object.
9514 * @pring: Pointer to driver SLI ring object.
9515 *
9516 * This function aborts all iocbs in the given ring and frees all the iocb
9517 * objects in txq. This function issues abort iocbs unconditionally for all
9518 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9519 * to complete before the return of this function. The caller is not required
9520 * to hold any locks.
9521 **/
9522static void
9523lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9524{
9525 LIST_HEAD(completions);
9526 struct lpfc_iocbq *iocb, *next_iocb;
9527
9528 if (pring->ringno == LPFC_ELS_RING)
9529 lpfc_fabric_abort_hba(phba);
9530
9531 spin_lock_irq(&phba->hbalock);
9532
9533 /* Take off all the iocbs on txq for cancelling */
9534 list_splice_init(&pring->txq, &completions);
9535 pring->txq_cnt = 0;
9536
9537 /* Next issue ABTS for everything on the txcmplq */
9538 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9539 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9540
9541 spin_unlock_irq(&phba->hbalock);
9542
9543 /* Cancel all the IOCBs from the completions list */
9544 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9545 IOERR_SLI_ABORTED);
9546}
9547
9548/**
9549 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9550 * @phba: pointer to lpfc HBA data structure.
9551 *
9552 * This routine will abort all pending and outstanding iocbs to an HBA.
9553 **/
9554void
9555lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9556{
9557 struct lpfc_sli *psli = &phba->sli;
9558 struct lpfc_sli_ring *pring;
9559 int i;
9560
9561 for (i = 0; i < psli->num_rings; i++) {
9562 pring = &psli->ring[i];
9563 lpfc_sli_iocb_ring_abort(phba, pring);
9564 }
9565}
9566
9567/**
James Smart3621a712009-04-06 18:47:14 -04009568 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009569 * @iocbq: Pointer to driver iocb object.
9570 * @vport: Pointer to driver virtual port object.
9571 * @tgt_id: SCSI ID of the target.
9572 * @lun_id: LUN ID of the scsi device.
9573 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9574 *
James Smart3621a712009-04-06 18:47:14 -04009575 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009576 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9577 * 0 if the filtering criteria is met for the given iocb and will return
9578 * 1 if the filtering criteria is not met.
9579 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9580 * given iocb is for the SCSI device specified by vport, tgt_id and
9581 * lun_id parameter.
9582 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9583 * given iocb is for the SCSI target specified by vport and tgt_id
9584 * parameters.
9585 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9586 * given iocb is for the SCSI host associated with the given vport.
9587 * This function is called with no locks held.
9588 **/
dea31012005-04-17 16:05:31 -05009589static int
James Smart51ef4c22007-08-02 11:10:31 -04009590lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9591 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009592 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009593{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009594 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009595 int rc = 1;
9596
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009597 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9598 return rc;
9599
James Smart51ef4c22007-08-02 11:10:31 -04009600 if (iocbq->vport != vport)
9601 return rc;
9602
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009603 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009604
James Smart495a7142008-06-14 22:52:59 -04009605 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009606 return rc;
9607
9608 switch (ctx_cmd) {
9609 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009610 if ((lpfc_cmd->rdata->pnode) &&
9611 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9612 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009613 rc = 0;
9614 break;
9615 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009616 if ((lpfc_cmd->rdata->pnode) &&
9617 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009618 rc = 0;
9619 break;
dea31012005-04-17 16:05:31 -05009620 case LPFC_CTX_HOST:
9621 rc = 0;
9622 break;
9623 default:
9624 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009625 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009626 break;
9627 }
9628
9629 return rc;
9630}
9631
James Smarte59058c2008-08-24 21:49:00 -04009632/**
James Smart3621a712009-04-06 18:47:14 -04009633 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009634 * @vport: Pointer to virtual port.
9635 * @tgt_id: SCSI ID of the target.
9636 * @lun_id: LUN ID of the scsi device.
9637 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9638 *
9639 * This function returns number of FCP commands pending for the vport.
9640 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9641 * commands pending on the vport associated with SCSI device specified
9642 * by tgt_id and lun_id parameters.
9643 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9644 * commands pending on the vport associated with SCSI target specified
9645 * by tgt_id parameter.
9646 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9647 * commands pending on the vport.
9648 * This function returns the number of iocbs which satisfy the filter.
9649 * This function is called without any lock held.
9650 **/
dea31012005-04-17 16:05:31 -05009651int
James Smart51ef4c22007-08-02 11:10:31 -04009652lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9653 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009654{
James Smart51ef4c22007-08-02 11:10:31 -04009655 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009656 struct lpfc_iocbq *iocbq;
9657 int sum, i;
dea31012005-04-17 16:05:31 -05009658
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009659 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9660 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009661
James Smart51ef4c22007-08-02 11:10:31 -04009662 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9663 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009664 sum++;
dea31012005-04-17 16:05:31 -05009665 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009666
dea31012005-04-17 16:05:31 -05009667 return sum;
9668}
9669
James Smarte59058c2008-08-24 21:49:00 -04009670/**
James Smart3621a712009-04-06 18:47:14 -04009671 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009672 * @phba: Pointer to HBA context object
9673 * @cmdiocb: Pointer to command iocb object.
9674 * @rspiocb: Pointer to response iocb object.
9675 *
9676 * This function is called when an aborted FCP iocb completes. This
9677 * function is called by the ring event handler with no lock held.
9678 * This function frees the iocb.
9679 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009680void
James Smart2e0fef82007-06-17 19:56:36 -05009681lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9682 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009683{
James Smartcb69f7d2011-12-13 13:21:57 -05009684 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9685 "3096 ABORT_XRI_CN completing on xri x%x "
9686 "original iotag x%x, abort cmd iotag x%x "
9687 "status 0x%x, reason 0x%x\n",
9688 cmdiocb->iocb.un.acxri.abortContextTag,
9689 cmdiocb->iocb.un.acxri.abortIoTag,
9690 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9691 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009692 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009693 return;
9694}
9695
James Smarte59058c2008-08-24 21:49:00 -04009696/**
James Smart3621a712009-04-06 18:47:14 -04009697 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009698 * @vport: Pointer to virtual port.
9699 * @pring: Pointer to driver SLI ring object.
9700 * @tgt_id: SCSI ID of the target.
9701 * @lun_id: LUN ID of the scsi device.
9702 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9703 *
9704 * This function sends an abort command for every SCSI command
9705 * associated with the given virtual port pending on the ring
9706 * filtered by lpfc_sli_validate_fcp_iocb function.
9707 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9708 * FCP iocbs associated with lun specified by tgt_id and lun_id
9709 * parameters
9710 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9711 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9712 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9713 * FCP iocbs associated with virtual port.
9714 * This function returns number of iocbs it failed to abort.
9715 * This function is called with no locks held.
9716 **/
dea31012005-04-17 16:05:31 -05009717int
James Smart51ef4c22007-08-02 11:10:31 -04009718lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9719 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009720{
James Smart51ef4c22007-08-02 11:10:31 -04009721 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009722 struct lpfc_iocbq *iocbq;
9723 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009724 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009725 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009726 int i;
dea31012005-04-17 16:05:31 -05009727
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009728 for (i = 1; i <= phba->sli.last_iotag; i++) {
9729 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009730
James Smart51ef4c22007-08-02 11:10:31 -04009731 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009732 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009733 continue;
9734
9735 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009736 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009737 if (abtsiocb == NULL) {
9738 errcnt++;
9739 continue;
9740 }
dea31012005-04-17 16:05:31 -05009741
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009742 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009743 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9744 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009745 if (phba->sli_rev == LPFC_SLI_REV4)
9746 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9747 else
9748 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009749 abtsiocb->iocb.ulpLe = 1;
9750 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009751 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009752
James Smart5ffc2662009-11-18 15:39:44 -05009753 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9754 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009755 if (iocbq->iocb_flag & LPFC_IO_FCP)
9756 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009757
James Smart2e0fef82007-06-17 19:56:36 -05009758 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009759 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9760 else
9761 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9762
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009763 /* Setup callback routine and issue the command. */
9764 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009765 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9766 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009767 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009768 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009769 errcnt++;
9770 continue;
9771 }
9772 }
9773
9774 return errcnt;
9775}
9776
James Smarte59058c2008-08-24 21:49:00 -04009777/**
James Smart3621a712009-04-06 18:47:14 -04009778 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009779 * @phba: Pointer to HBA context object.
9780 * @cmdiocbq: Pointer to command iocb.
9781 * @rspiocbq: Pointer to response iocb.
9782 *
9783 * This function is the completion handler for iocbs issued using
9784 * lpfc_sli_issue_iocb_wait function. This function is called by the
9785 * ring event handler function without any lock held. This function
9786 * can be called from both worker thread context and interrupt
9787 * context. This function also can be called from other thread which
9788 * cleans up the SLI layer objects.
9789 * This function copy the contents of the response iocb to the
9790 * response iocb memory object provided by the caller of
9791 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9792 * sleeps for the iocb completion.
9793 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009794static void
9795lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9796 struct lpfc_iocbq *cmdiocbq,
9797 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009798{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009799 wait_queue_head_t *pdone_q;
9800 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009801 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009802
James Smart2e0fef82007-06-17 19:56:36 -05009803 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009804 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9805 if (cmdiocbq->context2 && rspiocbq)
9806 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9807 &rspiocbq->iocb, sizeof(IOCB_t));
9808
James Smart0f65ff62010-02-26 14:14:23 -05009809 /* Set the exchange busy flag for task management commands */
9810 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9811 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9812 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9813 cur_iocbq);
9814 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9815 }
9816
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009817 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009818 if (pdone_q)
9819 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009820 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009821 return;
9822}
9823
James Smarte59058c2008-08-24 21:49:00 -04009824/**
James Smartd11e31d2009-06-10 17:23:06 -04009825 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9826 * @phba: Pointer to HBA context object..
9827 * @piocbq: Pointer to command iocb.
9828 * @flag: Flag to test.
9829 *
9830 * This routine grabs the hbalock and then test the iocb_flag to
9831 * see if the passed in flag is set.
9832 * Returns:
9833 * 1 if flag is set.
9834 * 0 if flag is not set.
9835 **/
9836static int
9837lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9838 struct lpfc_iocbq *piocbq, uint32_t flag)
9839{
9840 unsigned long iflags;
9841 int ret;
9842
9843 spin_lock_irqsave(&phba->hbalock, iflags);
9844 ret = piocbq->iocb_flag & flag;
9845 spin_unlock_irqrestore(&phba->hbalock, iflags);
9846 return ret;
9847
9848}
9849
9850/**
James Smart3621a712009-04-06 18:47:14 -04009851 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009852 * @phba: Pointer to HBA context object..
9853 * @pring: Pointer to sli ring.
9854 * @piocb: Pointer to command iocb.
9855 * @prspiocbq: Pointer to response iocb.
9856 * @timeout: Timeout in number of seconds.
9857 *
9858 * This function issues the iocb to firmware and waits for the
9859 * iocb to complete. If the iocb command is not
9860 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9861 * Caller should not free the iocb resources if this function
9862 * returns IOCB_TIMEDOUT.
9863 * The function waits for the iocb completion using an
9864 * non-interruptible wait.
9865 * This function will sleep while waiting for iocb completion.
9866 * So, this function should not be called from any context which
9867 * does not allow sleeping. Due to the same reason, this function
9868 * cannot be called with interrupt disabled.
9869 * This function assumes that the iocb completions occur while
9870 * this function sleep. So, this function cannot be called from
9871 * the thread which process iocb completion for this ring.
9872 * This function clears the iocb_flag of the iocb object before
9873 * issuing the iocb and the iocb completion handler sets this
9874 * flag and wakes this thread when the iocb completes.
9875 * The contents of the response iocb will be copied to prspiocbq
9876 * by the completion handler when the command completes.
9877 * This function returns IOCB_SUCCESS when success.
9878 * This function is called with no lock held.
9879 **/
dea31012005-04-17 16:05:31 -05009880int
James Smart2e0fef82007-06-17 19:56:36 -05009881lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009882 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009883 struct lpfc_iocbq *piocb,
9884 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009885 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009886{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009887 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009888 long timeleft, timeout_req = 0;
9889 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009890 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009891 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009892 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009893 * If the caller has provided a response iocbq buffer, then context2
9894 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009895 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009896 if (prspiocbq) {
9897 if (piocb->context2)
9898 return IOCB_ERROR;
9899 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009900 }
9901
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009902 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9903 piocb->context_un.wait_queue = &done_q;
9904 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009905
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009906 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009907 if (lpfc_readl(phba->HCregaddr, &creg_val))
9908 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009909 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9910 writel(creg_val, phba->HCregaddr);
9911 readl(phba->HCregaddr); /* flush */
9912 }
9913
James Smart2a9bf3d2010-06-07 15:24:45 -04009914 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9915 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009916 if (retval == IOCB_SUCCESS) {
9917 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009918 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009919 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009920 timeout_req);
dea31012005-04-17 16:05:31 -05009921
James Smart7054a602007-04-25 09:52:34 -04009922 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9923 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009924 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009925 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009927 "0338 IOCB wait timeout error - no "
9928 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009929 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009930 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009932 "0330 IOCB wake NOT set, "
9933 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009934 timeout, (timeleft / jiffies));
9935 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009936 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009937 } else if (retval == IOCB_BUSY) {
9938 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9939 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9940 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9941 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009942 } else {
9943 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009944 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009945 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009946 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009947 }
9948
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009949 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009950 if (lpfc_readl(phba->HCregaddr, &creg_val))
9951 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009952 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9953 writel(creg_val, phba->HCregaddr);
9954 readl(phba->HCregaddr); /* flush */
9955 }
9956
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009957 if (prspiocbq)
9958 piocb->context2 = NULL;
9959
9960 piocb->context_un.wait_queue = NULL;
9961 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009962 return retval;
9963}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009964
James Smarte59058c2008-08-24 21:49:00 -04009965/**
James Smart3621a712009-04-06 18:47:14 -04009966 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009967 * @phba: Pointer to HBA context object.
9968 * @pmboxq: Pointer to driver mailbox object.
9969 * @timeout: Timeout in number of seconds.
9970 *
9971 * This function issues the mailbox to firmware and waits for the
9972 * mailbox command to complete. If the mailbox command is not
9973 * completed within timeout seconds, it returns MBX_TIMEOUT.
9974 * The function waits for the mailbox completion using an
9975 * interruptible wait. If the thread is woken up due to a
9976 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9977 * should not free the mailbox resources, if this function returns
9978 * MBX_TIMEOUT.
9979 * This function will sleep while waiting for mailbox completion.
9980 * So, this function should not be called from any context which
9981 * does not allow sleeping. Due to the same reason, this function
9982 * cannot be called with interrupt disabled.
9983 * This function assumes that the mailbox completion occurs while
9984 * this function sleep. So, this function cannot be called from
9985 * the worker thread which processes mailbox completion.
9986 * This function is called in the context of HBA management
9987 * applications.
9988 * This function returns MBX_SUCCESS when successful.
9989 * This function is called with no lock held.
9990 **/
dea31012005-04-17 16:05:31 -05009991int
James Smart2e0fef82007-06-17 19:56:36 -05009992lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009993 uint32_t timeout)
9994{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009995 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009996 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009997 unsigned long flag;
dea31012005-04-17 16:05:31 -05009998
9999 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -040010000 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -050010001 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -050010002
James Smart495a7142008-06-14 22:52:59 -040010003 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010004 /* setup wake call as IOCB callback */
10005 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10006 /* setup context field to pass wait_queue pointer to wake function */
10007 pmboxq->context1 = &done_q;
10008
dea31012005-04-17 16:05:31 -050010009 /* now issue the command */
10010 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010011 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010012 wait_event_interruptible_timeout(done_q,
10013 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10014 timeout * HZ);
10015
James Smart858c9f62007-06-17 19:56:39 -050010016 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010017 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -040010018 /*
10019 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10020 * else do not free the resources.
10021 */
James Smartd7c47992010-06-08 18:31:54 -040010022 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010023 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010024 lpfc_sli4_swap_str(phba, pmboxq);
10025 } else {
James Smart7054a602007-04-25 09:52:34 -040010026 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010027 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10028 }
10029 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010030 }
10031
dea31012005-04-17 16:05:31 -050010032 return retval;
10033}
10034
James Smarte59058c2008-08-24 21:49:00 -040010035/**
James Smart3772a992009-05-22 14:50:54 -040010036 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010037 * @phba: Pointer to HBA context.
10038 *
James Smart3772a992009-05-22 14:50:54 -040010039 * This function is called to shutdown the driver's mailbox sub-system.
10040 * It first marks the mailbox sub-system is in a block state to prevent
10041 * the asynchronous mailbox command from issued off the pending mailbox
10042 * command queue. If the mailbox command sub-system shutdown is due to
10043 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10044 * the mailbox sub-system flush routine to forcefully bring down the
10045 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10046 * as with offline or HBA function reset), this routine will wait for the
10047 * outstanding mailbox command to complete before invoking the mailbox
10048 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010049 **/
James Smart3772a992009-05-22 14:50:54 -040010050void
James Smart618a5232012-06-12 13:54:36 -040010051lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010052{
James Smart3772a992009-05-22 14:50:54 -040010053 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010054 unsigned long timeout;
10055
James Smart618a5232012-06-12 13:54:36 -040010056 if (mbx_action == LPFC_MBX_NO_WAIT) {
10057 /* delay 100ms for port state */
10058 msleep(100);
10059 lpfc_sli_mbox_sys_flush(phba);
10060 return;
10061 }
James Smarta183a152011-10-10 21:32:43 -040010062 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010063
James Smart3772a992009-05-22 14:50:54 -040010064 spin_lock_irq(&phba->hbalock);
10065 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010066
10067 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010068 /* Determine how long we might wait for the active mailbox
10069 * command to be gracefully completed by firmware.
10070 */
James Smarta183a152011-10-10 21:32:43 -040010071 if (phba->sli.mbox_active)
10072 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10073 phba->sli.mbox_active) *
10074 1000) + jiffies;
10075 spin_unlock_irq(&phba->hbalock);
10076
James Smart3772a992009-05-22 14:50:54 -040010077 while (phba->sli.mbox_active) {
10078 /* Check active mailbox complete status every 2ms */
10079 msleep(2);
10080 if (time_after(jiffies, timeout))
10081 /* Timeout, let the mailbox flush routine to
10082 * forcefully release active mailbox command
10083 */
10084 break;
10085 }
James Smartd7069f02012-03-01 22:36:29 -050010086 } else
10087 spin_unlock_irq(&phba->hbalock);
10088
James Smart3772a992009-05-22 14:50:54 -040010089 lpfc_sli_mbox_sys_flush(phba);
10090}
10091
10092/**
10093 * lpfc_sli_eratt_read - read sli-3 error attention events
10094 * @phba: Pointer to HBA context.
10095 *
10096 * This function is called to read the SLI3 device error attention registers
10097 * for possible error attention events. The caller must hold the hostlock
10098 * with spin_lock_irq().
10099 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010100 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010101 * Register and returns 0 otherwise.
10102 **/
10103static int
10104lpfc_sli_eratt_read(struct lpfc_hba *phba)
10105{
James Smarted957682007-06-17 19:56:37 -050010106 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010107
James Smart3772a992009-05-22 14:50:54 -040010108 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010109 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10110 goto unplug_err;
10111
James Smart3772a992009-05-22 14:50:54 -040010112 if (ha_copy & HA_ERATT) {
10113 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010114 if (lpfc_sli_read_hs(phba))
10115 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010116
James Smart3772a992009-05-22 14:50:54 -040010117 /* Check if there is a deferred error condition is active */
10118 if ((HS_FFER1 & phba->work_hs) &&
10119 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010120 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010121 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010122 /* Clear all interrupt enable conditions */
10123 writel(0, phba->HCregaddr);
10124 readl(phba->HCregaddr);
10125 }
10126
10127 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010128 phba->work_ha |= HA_ERATT;
10129 /* Indicate polling handles this ERATT */
10130 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010131 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010132 }
James Smart3772a992009-05-22 14:50:54 -040010133 return 0;
James Smart9940b972011-03-11 16:06:12 -050010134
10135unplug_err:
10136 /* Set the driver HS work bitmap */
10137 phba->work_hs |= UNPLUG_ERR;
10138 /* Set the driver HA work bitmap */
10139 phba->work_ha |= HA_ERATT;
10140 /* Indicate polling handles this ERATT */
10141 phba->hba_flag |= HBA_ERATT_HANDLED;
10142 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010143}
10144
James Smarte59058c2008-08-24 21:49:00 -040010145/**
James Smartda0436e2009-05-22 14:51:39 -040010146 * lpfc_sli4_eratt_read - read sli-4 error attention events
10147 * @phba: Pointer to HBA context.
10148 *
10149 * This function is called to read the SLI4 device error attention registers
10150 * for possible error attention events. The caller must hold the hostlock
10151 * with spin_lock_irq().
10152 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010153 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010154 * Register and returns 0 otherwise.
10155 **/
10156static int
10157lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10158{
10159 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010160 uint32_t if_type, portsmphr;
10161 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010162
James Smart2fcee4b2010-12-15 17:57:46 -050010163 /*
10164 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010165 * registers for error attention. This can be changed later.
10166 */
James Smart2fcee4b2010-12-15 17:57:46 -050010167 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10168 switch (if_type) {
10169 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010170 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10171 &uerr_sta_lo) ||
10172 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10173 &uerr_sta_hi)) {
10174 phba->work_hs |= UNPLUG_ERR;
10175 phba->work_ha |= HA_ERATT;
10176 phba->hba_flag |= HBA_ERATT_HANDLED;
10177 return 1;
10178 }
James Smart2fcee4b2010-12-15 17:57:46 -050010179 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10180 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10182 "1423 HBA Unrecoverable error: "
10183 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10184 "ue_mask_lo_reg=0x%x, "
10185 "ue_mask_hi_reg=0x%x\n",
10186 uerr_sta_lo, uerr_sta_hi,
10187 phba->sli4_hba.ue_mask_lo,
10188 phba->sli4_hba.ue_mask_hi);
10189 phba->work_status[0] = uerr_sta_lo;
10190 phba->work_status[1] = uerr_sta_hi;
10191 phba->work_ha |= HA_ERATT;
10192 phba->hba_flag |= HBA_ERATT_HANDLED;
10193 return 1;
10194 }
10195 break;
10196 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010197 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10198 &portstat_reg.word0) ||
10199 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10200 &portsmphr)){
10201 phba->work_hs |= UNPLUG_ERR;
10202 phba->work_ha |= HA_ERATT;
10203 phba->hba_flag |= HBA_ERATT_HANDLED;
10204 return 1;
10205 }
James Smart2fcee4b2010-12-15 17:57:46 -050010206 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10207 phba->work_status[0] =
10208 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10209 phba->work_status[1] =
10210 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010212 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010213 "port status reg 0x%x, "
10214 "port smphr reg 0x%x, "
10215 "error 1=0x%x, error 2=0x%x\n",
10216 portstat_reg.word0,
10217 portsmphr,
10218 phba->work_status[0],
10219 phba->work_status[1]);
10220 phba->work_ha |= HA_ERATT;
10221 phba->hba_flag |= HBA_ERATT_HANDLED;
10222 return 1;
10223 }
10224 break;
10225 case LPFC_SLI_INTF_IF_TYPE_1:
10226 default:
James Smarta747c9c2009-11-18 15:41:10 -050010227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010228 "2886 HBA Error Attention on unsupported "
10229 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010230 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010231 }
James Smart2fcee4b2010-12-15 17:57:46 -050010232
James Smartda0436e2009-05-22 14:51:39 -040010233 return 0;
10234}
10235
10236/**
James Smart3621a712009-04-06 18:47:14 -040010237 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010238 * @phba: Pointer to HBA context.
10239 *
James Smart3772a992009-05-22 14:50:54 -040010240 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010241 * error attention register bit for error attention events.
10242 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010243 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010244 * Register and returns 0 otherwise.
10245 **/
10246int
10247lpfc_sli_check_eratt(struct lpfc_hba *phba)
10248{
10249 uint32_t ha_copy;
10250
10251 /* If somebody is waiting to handle an eratt, don't process it
10252 * here. The brdkill function will do this.
10253 */
10254 if (phba->link_flag & LS_IGNORE_ERATT)
10255 return 0;
10256
10257 /* Check if interrupt handler handles this ERATT */
10258 spin_lock_irq(&phba->hbalock);
10259 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10260 /* Interrupt handler has handled ERATT */
10261 spin_unlock_irq(&phba->hbalock);
10262 return 0;
10263 }
10264
James Smarta257bf92009-04-06 18:48:10 -040010265 /*
10266 * If there is deferred error attention, do not check for error
10267 * attention
10268 */
10269 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10270 spin_unlock_irq(&phba->hbalock);
10271 return 0;
10272 }
10273
James Smart3772a992009-05-22 14:50:54 -040010274 /* If PCI channel is offline, don't process it */
10275 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010276 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010277 return 0;
10278 }
10279
10280 switch (phba->sli_rev) {
10281 case LPFC_SLI_REV2:
10282 case LPFC_SLI_REV3:
10283 /* Read chip Host Attention (HA) register */
10284 ha_copy = lpfc_sli_eratt_read(phba);
10285 break;
James Smartda0436e2009-05-22 14:51:39 -040010286 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010287 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010288 ha_copy = lpfc_sli4_eratt_read(phba);
10289 break;
James Smart3772a992009-05-22 14:50:54 -040010290 default:
10291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10292 "0299 Invalid SLI revision (%d)\n",
10293 phba->sli_rev);
10294 ha_copy = 0;
10295 break;
James Smart93996272008-08-24 21:50:30 -040010296 }
10297 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010298
10299 return ha_copy;
10300}
10301
10302/**
10303 * lpfc_intr_state_check - Check device state for interrupt handling
10304 * @phba: Pointer to HBA context.
10305 *
10306 * This inline routine checks whether a device or its PCI slot is in a state
10307 * that the interrupt should be handled.
10308 *
10309 * This function returns 0 if the device or the PCI slot is in a state that
10310 * interrupt should be handled, otherwise -EIO.
10311 */
10312static inline int
10313lpfc_intr_state_check(struct lpfc_hba *phba)
10314{
10315 /* If the pci channel is offline, ignore all the interrupts */
10316 if (unlikely(pci_channel_offline(phba->pcidev)))
10317 return -EIO;
10318
10319 /* Update device level interrupt statistics */
10320 phba->sli.slistat.sli_intr++;
10321
10322 /* Ignore all interrupts during initialization. */
10323 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10324 return -EIO;
10325
James Smart93996272008-08-24 21:50:30 -040010326 return 0;
10327}
10328
10329/**
James Smart3772a992009-05-22 14:50:54 -040010330 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010331 * @irq: Interrupt number.
10332 * @dev_id: The device context pointer.
10333 *
James Smart93996272008-08-24 21:50:30 -040010334 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010335 * service routine when device with SLI-3 interface spec is enabled with
10336 * MSI-X multi-message interrupt mode and there are slow-path events in
10337 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10338 * interrupt mode, this function is called as part of the device-level
10339 * interrupt handler. When the PCI slot is in error recovery or the HBA
10340 * is undergoing initialization, the interrupt handler will not process
10341 * the interrupt. The link attention and ELS ring attention events are
10342 * handled by the worker thread. The interrupt handler signals the worker
10343 * thread and returns for these events. This function is called without
10344 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010345 * structures.
10346 *
10347 * This function returns IRQ_HANDLED when interrupt is handled else it
10348 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010349 **/
dea31012005-04-17 16:05:31 -050010350irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010351lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010352{
James Smart2e0fef82007-06-17 19:56:36 -050010353 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010354 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010355 uint32_t work_ha_copy;
10356 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010357 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010358 uint32_t control;
10359
James Smart92d7f7b2007-06-17 19:56:38 -050010360 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010361 struct lpfc_vport *vport;
10362 struct lpfc_nodelist *ndlp;
10363 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010364 LPFC_MBOXQ_t *pmb;
10365 int rc;
10366
dea31012005-04-17 16:05:31 -050010367 /*
10368 * Get the driver's phba structure from the dev_id and
10369 * assume the HBA is not interrupting.
10370 */
James Smart93996272008-08-24 21:50:30 -040010371 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010372
10373 if (unlikely(!phba))
10374 return IRQ_NONE;
10375
dea31012005-04-17 16:05:31 -050010376 /*
James Smart93996272008-08-24 21:50:30 -040010377 * Stuff needs to be attented to when this function is invoked as an
10378 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010379 */
James Smart93996272008-08-24 21:50:30 -040010380 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010381 /* Check device state for handling interrupt */
10382 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010383 return IRQ_NONE;
10384 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010385 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010386 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10387 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010388 /* If somebody is waiting to handle an eratt don't process it
10389 * here. The brdkill function will do this.
10390 */
10391 if (phba->link_flag & LS_IGNORE_ERATT)
10392 ha_copy &= ~HA_ERATT;
10393 /* Check the need for handling ERATT in interrupt handler */
10394 if (ha_copy & HA_ERATT) {
10395 if (phba->hba_flag & HBA_ERATT_HANDLED)
10396 /* ERATT polling has handled ERATT */
10397 ha_copy &= ~HA_ERATT;
10398 else
10399 /* Indicate interrupt handler handles ERATT */
10400 phba->hba_flag |= HBA_ERATT_HANDLED;
10401 }
James Smarta257bf92009-04-06 18:48:10 -040010402
10403 /*
10404 * If there is deferred error attention, do not check for any
10405 * interrupt.
10406 */
10407 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010408 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010409 return IRQ_NONE;
10410 }
10411
James Smart93996272008-08-24 21:50:30 -040010412 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010413 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10414 goto unplug_error;
10415
James Smarta747c9c2009-11-18 15:41:10 -050010416 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10417 HC_LAINT_ENA | HC_ERINT_ENA),
10418 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010419 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10420 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010421 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010422 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010423 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010424 } else
10425 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010426
dea31012005-04-17 16:05:31 -050010427 work_ha_copy = ha_copy & phba->work_ha_mask;
10428
James Smart93996272008-08-24 21:50:30 -040010429 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010430 if (work_ha_copy & HA_LATT) {
10431 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10432 /*
10433 * Turn off Link Attention interrupts
10434 * until CLEAR_LA done
10435 */
James Smart5b75da22008-12-04 22:39:35 -050010436 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010437 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010438 if (lpfc_readl(phba->HCregaddr, &control))
10439 goto unplug_error;
dea31012005-04-17 16:05:31 -050010440 control &= ~HC_LAINT_ENA;
10441 writel(control, phba->HCregaddr);
10442 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010443 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010444 }
10445 else
10446 work_ha_copy &= ~HA_LATT;
10447 }
10448
James Smart93996272008-08-24 21:50:30 -040010449 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010450 /*
10451 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10452 * the only slow ring.
10453 */
10454 status = (work_ha_copy &
10455 (HA_RXMASK << (4*LPFC_ELS_RING)));
10456 status >>= (4*LPFC_ELS_RING);
10457 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010458 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010459 if (lpfc_readl(phba->HCregaddr, &control))
10460 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010461
10462 lpfc_debugfs_slow_ring_trc(phba,
10463 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10464 control, status,
10465 (uint32_t)phba->sli.slistat.sli_intr);
10466
James Smart858c9f62007-06-17 19:56:39 -050010467 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010468 lpfc_debugfs_slow_ring_trc(phba,
10469 "ISR Disable ring:"
10470 "pwork:x%x hawork:x%x wait:x%x",
10471 phba->work_ha, work_ha_copy,
10472 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010473 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010474
James Smart858c9f62007-06-17 19:56:39 -050010475 control &=
10476 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010477 writel(control, phba->HCregaddr);
10478 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010479 }
James Smarta58cbd52007-08-02 11:09:43 -040010480 else {
10481 lpfc_debugfs_slow_ring_trc(phba,
10482 "ISR slow ring: pwork:"
10483 "x%x hawork:x%x wait:x%x",
10484 phba->work_ha, work_ha_copy,
10485 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010486 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010487 }
James Smart5b75da22008-12-04 22:39:35 -050010488 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010489 }
10490 }
James Smart5b75da22008-12-04 22:39:35 -050010491 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010492 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010493 if (lpfc_sli_read_hs(phba))
10494 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010495 /*
10496 * Check if there is a deferred error condition
10497 * is active
10498 */
10499 if ((HS_FFER1 & phba->work_hs) &&
10500 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010501 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10502 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010503 phba->hba_flag |= DEFER_ERATT;
10504 /* Clear all interrupt enable conditions */
10505 writel(0, phba->HCregaddr);
10506 readl(phba->HCregaddr);
10507 }
10508 }
10509
James Smart93996272008-08-24 21:50:30 -040010510 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010511 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010512 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010513 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010514 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010515
10516 /* First check out the status word */
10517 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10518 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010519 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010520 /*
10521 * Stray Mailbox Interrupt, mbxCommand <cmd>
10522 * mbxStatus <status>
10523 */
James Smart09372822008-01-11 01:52:54 -050010524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010525 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010526 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010527 "Interrupt mbxCommand x%x "
10528 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010529 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010530 pmbox->mbxCommand,
10531 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010532 /* clear mailbox attention bit */
10533 work_ha_copy &= ~HA_MBATT;
10534 } else {
James Smart97eab632008-04-07 10:16:05 -040010535 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010536 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010537 phba->last_completion_time = jiffies;
10538 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010539 if (pmb->mbox_cmpl) {
10540 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10541 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010542 if (pmb->out_ext_byte_len &&
10543 pmb->context2)
10544 lpfc_sli_pcimem_bcopy(
10545 phba->mbox_ext,
10546 pmb->context2,
10547 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010548 }
James Smart09372822008-01-11 01:52:54 -050010549 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10550 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10551
10552 lpfc_debugfs_disc_trc(vport,
10553 LPFC_DISC_TRC_MBOX_VPORT,
10554 "MBOX dflt rpi: : "
10555 "status:x%x rpi:x%x",
10556 (uint32_t)pmbox->mbxStatus,
10557 pmbox->un.varWords[0], 0);
10558
10559 if (!pmbox->mbxStatus) {
10560 mp = (struct lpfc_dmabuf *)
10561 (pmb->context1);
10562 ndlp = (struct lpfc_nodelist *)
10563 pmb->context2;
10564
10565 /* Reg_LOGIN of dflt RPI was
10566 * successful. new lets get
10567 * rid of the RPI using the
10568 * same mbox buffer.
10569 */
10570 lpfc_unreg_login(phba,
10571 vport->vpi,
10572 pmbox->un.varWords[0],
10573 pmb);
10574 pmb->mbox_cmpl =
10575 lpfc_mbx_cmpl_dflt_rpi;
10576 pmb->context1 = mp;
10577 pmb->context2 = ndlp;
10578 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010579 rc = lpfc_sli_issue_mbox(phba,
10580 pmb,
10581 MBX_NOWAIT);
10582 if (rc != MBX_BUSY)
10583 lpfc_printf_log(phba,
10584 KERN_ERR,
10585 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010586 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010587 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010588 if (rc != MBX_NOT_FINISHED)
10589 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010590 }
10591 }
James Smart5b75da22008-12-04 22:39:35 -050010592 spin_lock_irqsave(
10593 &phba->pport->work_port_lock,
10594 iflag);
James Smart09372822008-01-11 01:52:54 -050010595 phba->pport->work_port_events &=
10596 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010597 spin_unlock_irqrestore(
10598 &phba->pport->work_port_lock,
10599 iflag);
James Smart09372822008-01-11 01:52:54 -050010600 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010601 }
James Smart97eab632008-04-07 10:16:05 -040010602 } else
James Smart5b75da22008-12-04 22:39:35 -050010603 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010604
James Smart92d7f7b2007-06-17 19:56:38 -050010605 if ((work_ha_copy & HA_MBATT) &&
10606 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010607send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010608 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010609 do {
10610 rc = lpfc_sli_issue_mbox(phba, NULL,
10611 MBX_NOWAIT);
10612 } while (rc == MBX_NOT_FINISHED);
10613 if (rc != MBX_SUCCESS)
10614 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10615 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010616 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010617 }
10618
James Smart5b75da22008-12-04 22:39:35 -050010619 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010620 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010621 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010622 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010623 }
James Smart93996272008-08-24 21:50:30 -040010624 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010625unplug_error:
10626 spin_unlock_irqrestore(&phba->hbalock, iflag);
10627 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010628
James Smart3772a992009-05-22 14:50:54 -040010629} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010630
10631/**
James Smart3772a992009-05-22 14:50:54 -040010632 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010633 * @irq: Interrupt number.
10634 * @dev_id: The device context pointer.
10635 *
10636 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010637 * service routine when device with SLI-3 interface spec is enabled with
10638 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10639 * ring event in the HBA. However, when the device is enabled with either
10640 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10641 * device-level interrupt handler. When the PCI slot is in error recovery
10642 * or the HBA is undergoing initialization, the interrupt handler will not
10643 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10644 * the intrrupt context. This function is called without any lock held.
10645 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010646 *
10647 * This function returns IRQ_HANDLED when interrupt is handled else it
10648 * returns IRQ_NONE.
10649 **/
10650irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010651lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010652{
10653 struct lpfc_hba *phba;
10654 uint32_t ha_copy;
10655 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010656 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010657
10658 /* Get the driver's phba structure from the dev_id and
10659 * assume the HBA is not interrupting.
10660 */
10661 phba = (struct lpfc_hba *) dev_id;
10662
10663 if (unlikely(!phba))
10664 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010665
10666 /*
James Smart93996272008-08-24 21:50:30 -040010667 * Stuff needs to be attented to when this function is invoked as an
10668 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010669 */
James Smart93996272008-08-24 21:50:30 -040010670 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010671 /* Check device state for handling interrupt */
10672 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010673 return IRQ_NONE;
10674 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010675 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10676 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010677 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010678 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010679 /*
10680 * If there is deferred error attention, do not check for
10681 * any interrupt.
10682 */
10683 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010684 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010685 return IRQ_NONE;
10686 }
James Smart93996272008-08-24 21:50:30 -040010687 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10688 phba->HAregaddr);
10689 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010690 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010691 } else
10692 ha_copy = phba->ha_copy;
10693
10694 /*
10695 * Process all events on FCP ring. Take the optimized path for FCP IO.
10696 */
10697 ha_copy &= ~(phba->work_ha_mask);
10698
10699 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010700 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010701 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010702 lpfc_sli_handle_fast_ring_event(phba,
10703 &phba->sli.ring[LPFC_FCP_RING],
10704 status);
James Smarta4bc3372006-12-02 13:34:16 -050010705
10706 if (phba->cfg_multi_ring_support == 2) {
10707 /*
James Smart93996272008-08-24 21:50:30 -040010708 * Process all events on extra ring. Take the optimized path
10709 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010710 */
James Smart93996272008-08-24 21:50:30 -040010711 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010712 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010713 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010714 lpfc_sli_handle_fast_ring_event(phba,
10715 &phba->sli.ring[LPFC_EXTRA_RING],
10716 status);
10717 }
10718 }
dea31012005-04-17 16:05:31 -050010719 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010720} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010721
James Smart93996272008-08-24 21:50:30 -040010722/**
James Smart3772a992009-05-22 14:50:54 -040010723 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010724 * @irq: Interrupt number.
10725 * @dev_id: The device context pointer.
10726 *
James Smart3772a992009-05-22 14:50:54 -040010727 * This function is the HBA device-level interrupt handler to device with
10728 * SLI-3 interface spec, called from the PCI layer when either MSI or
10729 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10730 * requires driver attention. This function invokes the slow-path interrupt
10731 * attention handling function and fast-path interrupt attention handling
10732 * function in turn to process the relevant HBA attention events. This
10733 * function is called without any lock held. It gets the hbalock to access
10734 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010735 *
10736 * This function returns IRQ_HANDLED when interrupt is handled, else it
10737 * returns IRQ_NONE.
10738 **/
10739irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010740lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010741{
10742 struct lpfc_hba *phba;
10743 irqreturn_t sp_irq_rc, fp_irq_rc;
10744 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010745 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010746
10747 /*
10748 * Get the driver's phba structure from the dev_id and
10749 * assume the HBA is not interrupting.
10750 */
10751 phba = (struct lpfc_hba *) dev_id;
10752
10753 if (unlikely(!phba))
10754 return IRQ_NONE;
10755
James Smart3772a992009-05-22 14:50:54 -040010756 /* Check device state for handling interrupt */
10757 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010758 return IRQ_NONE;
10759
10760 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010761 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10762 spin_unlock(&phba->hbalock);
10763 return IRQ_HANDLED;
10764 }
10765
James Smart93996272008-08-24 21:50:30 -040010766 if (unlikely(!phba->ha_copy)) {
10767 spin_unlock(&phba->hbalock);
10768 return IRQ_NONE;
10769 } else if (phba->ha_copy & HA_ERATT) {
10770 if (phba->hba_flag & HBA_ERATT_HANDLED)
10771 /* ERATT polling has handled ERATT */
10772 phba->ha_copy &= ~HA_ERATT;
10773 else
10774 /* Indicate interrupt handler handles ERATT */
10775 phba->hba_flag |= HBA_ERATT_HANDLED;
10776 }
10777
James Smarta257bf92009-04-06 18:48:10 -040010778 /*
10779 * If there is deferred error attention, do not check for any interrupt.
10780 */
10781 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010782 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010783 return IRQ_NONE;
10784 }
10785
James Smart93996272008-08-24 21:50:30 -040010786 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010787 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10788 spin_unlock(&phba->hbalock);
10789 return IRQ_HANDLED;
10790 }
James Smarta747c9c2009-11-18 15:41:10 -050010791 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10792 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10793 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010794 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010795 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010796 readl(phba->HAregaddr); /* flush */
10797 spin_unlock(&phba->hbalock);
10798
10799 /*
10800 * Invokes slow-path host attention interrupt handling as appropriate.
10801 */
10802
10803 /* status of events with mailbox and link attention */
10804 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10805
10806 /* status of events with ELS ring */
10807 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10808 status2 >>= (4*LPFC_ELS_RING);
10809
10810 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010811 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010812 else
10813 sp_irq_rc = IRQ_NONE;
10814
10815 /*
10816 * Invoke fast-path host attention interrupt handling as appropriate.
10817 */
10818
10819 /* status of events with FCP ring */
10820 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10821 status1 >>= (4*LPFC_FCP_RING);
10822
10823 /* status of events with extra ring */
10824 if (phba->cfg_multi_ring_support == 2) {
10825 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10826 status2 >>= (4*LPFC_EXTRA_RING);
10827 } else
10828 status2 = 0;
10829
10830 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010831 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010832 else
10833 fp_irq_rc = IRQ_NONE;
10834
10835 /* Return device-level interrupt handling status */
10836 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010837} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010838
10839/**
10840 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10841 * @phba: pointer to lpfc hba data structure.
10842 *
10843 * This routine is invoked by the worker thread to process all the pending
10844 * SLI4 FCP abort XRI events.
10845 **/
10846void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10847{
10848 struct lpfc_cq_event *cq_event;
10849
10850 /* First, declare the fcp xri abort event has been handled */
10851 spin_lock_irq(&phba->hbalock);
10852 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10853 spin_unlock_irq(&phba->hbalock);
10854 /* Now, handle all the fcp xri abort events */
10855 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10856 /* Get the first event from the head of the event queue */
10857 spin_lock_irq(&phba->hbalock);
10858 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10859 cq_event, struct lpfc_cq_event, list);
10860 spin_unlock_irq(&phba->hbalock);
10861 /* Notify aborted XRI for FCP work queue */
10862 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10863 /* Free the event processed back to the free pool */
10864 lpfc_sli4_cq_event_release(phba, cq_event);
10865 }
10866}
10867
10868/**
10869 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10870 * @phba: pointer to lpfc hba data structure.
10871 *
10872 * This routine is invoked by the worker thread to process all the pending
10873 * SLI4 els abort xri events.
10874 **/
10875void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10876{
10877 struct lpfc_cq_event *cq_event;
10878
10879 /* First, declare the els xri abort event has been handled */
10880 spin_lock_irq(&phba->hbalock);
10881 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10882 spin_unlock_irq(&phba->hbalock);
10883 /* Now, handle all the els xri abort events */
10884 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10885 /* Get the first event from the head of the event queue */
10886 spin_lock_irq(&phba->hbalock);
10887 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10888 cq_event, struct lpfc_cq_event, list);
10889 spin_unlock_irq(&phba->hbalock);
10890 /* Notify aborted XRI for ELS work queue */
10891 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10892 /* Free the event processed back to the free pool */
10893 lpfc_sli4_cq_event_release(phba, cq_event);
10894 }
10895}
10896
James Smart341af102010-01-26 23:07:37 -050010897/**
10898 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10899 * @phba: pointer to lpfc hba data structure
10900 * @pIocbIn: pointer to the rspiocbq
10901 * @pIocbOut: pointer to the cmdiocbq
10902 * @wcqe: pointer to the complete wcqe
10903 *
10904 * This routine transfers the fields of a command iocbq to a response iocbq
10905 * by copying all the IOCB fields from command iocbq and transferring the
10906 * completion status information from the complete wcqe.
10907 **/
James Smart4f774512009-05-22 14:52:35 -040010908static void
James Smart341af102010-01-26 23:07:37 -050010909lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10910 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010911 struct lpfc_iocbq *pIocbOut,
10912 struct lpfc_wcqe_complete *wcqe)
10913{
James Smart341af102010-01-26 23:07:37 -050010914 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010915 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010916 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10917
10918 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10919 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010920 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010921 status = bf_get(lpfc_wcqe_c_status, wcqe);
10922 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010923 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10924 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10925 pIocbIn->iocb.un.fcpi.fcpi_parm =
10926 pIocbOut->iocb.un.fcpi.fcpi_parm -
10927 wcqe->total_data_placed;
10928 else
10929 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010930 else {
James Smart4f774512009-05-22 14:52:35 -040010931 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010932 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10933 }
James Smart341af102010-01-26 23:07:37 -050010934
James Smartacd68592012-01-18 16:25:09 -050010935 /* Convert BG errors for completion status */
10936 if (status == CQE_STATUS_DI_ERROR) {
10937 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10938
10939 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10940 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10941 else
10942 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10943
10944 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10945 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10946 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10947 BGS_GUARD_ERR_MASK;
10948 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10949 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10950 BGS_APPTAG_ERR_MASK;
10951 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10952 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10953 BGS_REFTAG_ERR_MASK;
10954
10955 /* Check to see if there was any good data before the error */
10956 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10957 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10958 BGS_HI_WATER_MARK_PRESENT_MASK;
10959 pIocbIn->iocb.unsli3.sli3_bg.bghm =
10960 wcqe->total_data_placed;
10961 }
10962
10963 /*
10964 * Set ALL the error bits to indicate we don't know what
10965 * type of error it is.
10966 */
10967 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10968 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10969 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10970 BGS_GUARD_ERR_MASK);
10971 }
10972
James Smart341af102010-01-26 23:07:37 -050010973 /* Pick up HBA exchange busy condition */
10974 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10975 spin_lock_irqsave(&phba->hbalock, iflags);
10976 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10977 spin_unlock_irqrestore(&phba->hbalock, iflags);
10978 }
James Smart4f774512009-05-22 14:52:35 -040010979}
10980
10981/**
James Smart45ed1192009-10-02 15:17:02 -040010982 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10983 * @phba: Pointer to HBA context object.
10984 * @wcqe: Pointer to work-queue completion queue entry.
10985 *
10986 * This routine handles an ELS work-queue completion event and construct
10987 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10988 * discovery engine to handle.
10989 *
10990 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10991 **/
10992static struct lpfc_iocbq *
10993lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10994 struct lpfc_iocbq *irspiocbq)
10995{
10996 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10997 struct lpfc_iocbq *cmdiocbq;
10998 struct lpfc_wcqe_complete *wcqe;
10999 unsigned long iflags;
11000
11001 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011002 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011003 pring->stats.iocb_event++;
11004 /* Look up the ELS command IOCB and create pseudo response IOCB */
11005 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11006 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011007 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011008
11009 if (unlikely(!cmdiocbq)) {
11010 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11011 "0386 ELS complete with no corresponding "
11012 "cmdiocb: iotag (%d)\n",
11013 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11014 lpfc_sli_release_iocbq(phba, irspiocbq);
11015 return NULL;
11016 }
11017
11018 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011019 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011020
11021 return irspiocbq;
11022}
11023
11024/**
James Smart04c68492009-05-22 14:52:52 -040011025 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11026 * @phba: Pointer to HBA context object.
11027 * @cqe: Pointer to mailbox completion queue entry.
11028 *
11029 * This routine process a mailbox completion queue entry with asynchrous
11030 * event.
11031 *
11032 * Return: true if work posted to worker thread, otherwise false.
11033 **/
11034static bool
11035lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11036{
11037 struct lpfc_cq_event *cq_event;
11038 unsigned long iflags;
11039
11040 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11041 "0392 Async Event: word0:x%x, word1:x%x, "
11042 "word2:x%x, word3:x%x\n", mcqe->word0,
11043 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11044
11045 /* Allocate a new internal CQ_EVENT entry */
11046 cq_event = lpfc_sli4_cq_event_alloc(phba);
11047 if (!cq_event) {
11048 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11049 "0394 Failed to allocate CQ_EVENT entry\n");
11050 return false;
11051 }
11052
11053 /* Move the CQE into an asynchronous event entry */
11054 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11055 spin_lock_irqsave(&phba->hbalock, iflags);
11056 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11057 /* Set the async event flag */
11058 phba->hba_flag |= ASYNC_EVENT;
11059 spin_unlock_irqrestore(&phba->hbalock, iflags);
11060
11061 return true;
11062}
11063
11064/**
11065 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11066 * @phba: Pointer to HBA context object.
11067 * @cqe: Pointer to mailbox completion queue entry.
11068 *
11069 * This routine process a mailbox completion queue entry with mailbox
11070 * completion event.
11071 *
11072 * Return: true if work posted to worker thread, otherwise false.
11073 **/
11074static bool
11075lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11076{
11077 uint32_t mcqe_status;
11078 MAILBOX_t *mbox, *pmbox;
11079 struct lpfc_mqe *mqe;
11080 struct lpfc_vport *vport;
11081 struct lpfc_nodelist *ndlp;
11082 struct lpfc_dmabuf *mp;
11083 unsigned long iflags;
11084 LPFC_MBOXQ_t *pmb;
11085 bool workposted = false;
11086 int rc;
11087
11088 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11089 if (!bf_get(lpfc_trailer_completed, mcqe))
11090 goto out_no_mqe_complete;
11091
11092 /* Get the reference to the active mbox command */
11093 spin_lock_irqsave(&phba->hbalock, iflags);
11094 pmb = phba->sli.mbox_active;
11095 if (unlikely(!pmb)) {
11096 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11097 "1832 No pending MBOX command to handle\n");
11098 spin_unlock_irqrestore(&phba->hbalock, iflags);
11099 goto out_no_mqe_complete;
11100 }
11101 spin_unlock_irqrestore(&phba->hbalock, iflags);
11102 mqe = &pmb->u.mqe;
11103 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11104 mbox = phba->mbox;
11105 vport = pmb->vport;
11106
11107 /* Reset heartbeat timer */
11108 phba->last_completion_time = jiffies;
11109 del_timer(&phba->sli.mbox_tmo);
11110
11111 /* Move mbox data to caller's mailbox region, do endian swapping */
11112 if (pmb->mbox_cmpl && mbox)
11113 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011114
James Smart73d91e52011-10-10 21:32:10 -040011115 /*
11116 * For mcqe errors, conditionally move a modified error code to
11117 * the mbox so that the error will not be missed.
11118 */
11119 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11120 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11121 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11122 bf_set(lpfc_mqe_status, mqe,
11123 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11124 }
James Smart04c68492009-05-22 14:52:52 -040011125 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11126 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11127 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11128 "MBOX dflt rpi: status:x%x rpi:x%x",
11129 mcqe_status,
11130 pmbox->un.varWords[0], 0);
11131 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11132 mp = (struct lpfc_dmabuf *)(pmb->context1);
11133 ndlp = (struct lpfc_nodelist *)pmb->context2;
11134 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11135 * RID of the PPI using the same mbox buffer.
11136 */
11137 lpfc_unreg_login(phba, vport->vpi,
11138 pmbox->un.varWords[0], pmb);
11139 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11140 pmb->context1 = mp;
11141 pmb->context2 = ndlp;
11142 pmb->vport = vport;
11143 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11144 if (rc != MBX_BUSY)
11145 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11146 LOG_SLI, "0385 rc should "
11147 "have been MBX_BUSY\n");
11148 if (rc != MBX_NOT_FINISHED)
11149 goto send_current_mbox;
11150 }
11151 }
11152 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11153 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11154 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11155
11156 /* There is mailbox completion work to do */
11157 spin_lock_irqsave(&phba->hbalock, iflags);
11158 __lpfc_mbox_cmpl_put(phba, pmb);
11159 phba->work_ha |= HA_MBATT;
11160 spin_unlock_irqrestore(&phba->hbalock, iflags);
11161 workposted = true;
11162
11163send_current_mbox:
11164 spin_lock_irqsave(&phba->hbalock, iflags);
11165 /* Release the mailbox command posting token */
11166 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11167 /* Setting active mailbox pointer need to be in sync to flag clear */
11168 phba->sli.mbox_active = NULL;
11169 spin_unlock_irqrestore(&phba->hbalock, iflags);
11170 /* Wake up worker thread to post the next pending mailbox command */
11171 lpfc_worker_wake_up(phba);
11172out_no_mqe_complete:
11173 if (bf_get(lpfc_trailer_consumed, mcqe))
11174 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11175 return workposted;
11176}
11177
11178/**
11179 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11180 * @phba: Pointer to HBA context object.
11181 * @cqe: Pointer to mailbox completion queue entry.
11182 *
11183 * This routine process a mailbox completion queue entry, it invokes the
11184 * proper mailbox complete handling or asynchrous event handling routine
11185 * according to the MCQE's async bit.
11186 *
11187 * Return: true if work posted to worker thread, otherwise false.
11188 **/
11189static bool
11190lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11191{
11192 struct lpfc_mcqe mcqe;
11193 bool workposted;
11194
11195 /* Copy the mailbox MCQE and convert endian order as needed */
11196 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11197
11198 /* Invoke the proper event handling routine */
11199 if (!bf_get(lpfc_trailer_async, &mcqe))
11200 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11201 else
11202 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11203 return workposted;
11204}
11205
11206/**
James Smart4f774512009-05-22 14:52:35 -040011207 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11208 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011209 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011210 * @wcqe: Pointer to work-queue completion queue entry.
11211 *
11212 * This routine handles an ELS work-queue completion event.
11213 *
11214 * Return: true if work posted to worker thread, otherwise false.
11215 **/
11216static bool
James Smart2a76a282012-08-03 12:35:54 -040011217lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011218 struct lpfc_wcqe_complete *wcqe)
11219{
James Smart4f774512009-05-22 14:52:35 -040011220 struct lpfc_iocbq *irspiocbq;
11221 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011222 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011223
James Smart45ed1192009-10-02 15:17:02 -040011224 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011225 irspiocbq = lpfc_sli_get_iocbq(phba);
11226 if (!irspiocbq) {
11227 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011228 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11229 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11230 pring->txq_cnt, phba->iocb_cnt,
11231 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11232 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011233 return false;
James Smart4f774512009-05-22 14:52:35 -040011234 }
James Smart4f774512009-05-22 14:52:35 -040011235
James Smart45ed1192009-10-02 15:17:02 -040011236 /* Save off the slow-path queue event for work thread to process */
11237 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011238 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011239 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011240 &phba->sli4_hba.sp_queue_event);
11241 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011242 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011243
James Smart45ed1192009-10-02 15:17:02 -040011244 return true;
James Smart4f774512009-05-22 14:52:35 -040011245}
11246
11247/**
11248 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11249 * @phba: Pointer to HBA context object.
11250 * @wcqe: Pointer to work-queue completion queue entry.
11251 *
11252 * This routine handles slow-path WQ entry comsumed event by invoking the
11253 * proper WQ release routine to the slow-path WQ.
11254 **/
11255static void
11256lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11257 struct lpfc_wcqe_release *wcqe)
11258{
James Smart2e90f4b2011-12-13 13:22:37 -050011259 /* sanity check on queue memory */
11260 if (unlikely(!phba->sli4_hba.els_wq))
11261 return;
James Smart4f774512009-05-22 14:52:35 -040011262 /* Check for the slow-path ELS work queue */
11263 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11264 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11265 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11266 else
11267 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11268 "2579 Slow-path wqe consume event carries "
11269 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11270 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11271 phba->sli4_hba.els_wq->queue_id);
11272}
11273
11274/**
11275 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11276 * @phba: Pointer to HBA context object.
11277 * @cq: Pointer to a WQ completion queue.
11278 * @wcqe: Pointer to work-queue completion queue entry.
11279 *
11280 * This routine handles an XRI abort event.
11281 *
11282 * Return: true if work posted to worker thread, otherwise false.
11283 **/
11284static bool
11285lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11286 struct lpfc_queue *cq,
11287 struct sli4_wcqe_xri_aborted *wcqe)
11288{
11289 bool workposted = false;
11290 struct lpfc_cq_event *cq_event;
11291 unsigned long iflags;
11292
11293 /* Allocate a new internal CQ_EVENT entry */
11294 cq_event = lpfc_sli4_cq_event_alloc(phba);
11295 if (!cq_event) {
11296 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11297 "0602 Failed to allocate CQ_EVENT entry\n");
11298 return false;
11299 }
11300
11301 /* Move the CQE into the proper xri abort event list */
11302 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11303 switch (cq->subtype) {
11304 case LPFC_FCP:
11305 spin_lock_irqsave(&phba->hbalock, iflags);
11306 list_add_tail(&cq_event->list,
11307 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11308 /* Set the fcp xri abort event flag */
11309 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11310 spin_unlock_irqrestore(&phba->hbalock, iflags);
11311 workposted = true;
11312 break;
11313 case LPFC_ELS:
11314 spin_lock_irqsave(&phba->hbalock, iflags);
11315 list_add_tail(&cq_event->list,
11316 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11317 /* Set the els xri abort event flag */
11318 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11319 spin_unlock_irqrestore(&phba->hbalock, iflags);
11320 workposted = true;
11321 break;
11322 default:
11323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11324 "0603 Invalid work queue CQE subtype (x%x)\n",
11325 cq->subtype);
11326 workposted = false;
11327 break;
11328 }
11329 return workposted;
11330}
11331
11332/**
James Smart4d9ab992009-10-02 15:16:39 -040011333 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011334 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011335 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011336 *
James Smart4d9ab992009-10-02 15:16:39 -040011337 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011338 *
11339 * Return: true if work posted to worker thread, otherwise false.
11340 **/
11341static bool
James Smart4d9ab992009-10-02 15:16:39 -040011342lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11343{
11344 bool workposted = false;
11345 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11346 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11347 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011348 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011349 unsigned long iflags;
11350
James Smart2e90f4b2011-12-13 13:22:37 -050011351 /* sanity check on queue memory */
11352 if (unlikely(!hrq) || unlikely(!drq))
11353 return workposted;
11354
James Smart7851fe22011-07-22 18:36:52 -040011355 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11356 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11357 else
11358 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11359 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011360 goto out;
11361
11362 status = bf_get(lpfc_rcqe_status, rcqe);
11363 switch (status) {
11364 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11366 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011367 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011368 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011369 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011370 spin_lock_irqsave(&phba->hbalock, iflags);
11371 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11372 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011373 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011374 spin_unlock_irqrestore(&phba->hbalock, iflags);
11375 goto out;
11376 }
James Smartb84daac2012-08-03 12:35:13 -040011377 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011378 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11379 /* save off the frame for the word thread to process */
11380 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011381 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011382 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011383 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011384 spin_unlock_irqrestore(&phba->hbalock, iflags);
11385 workposted = true;
11386 break;
11387 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11388 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011389 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011390 /* Post more buffers if possible */
11391 spin_lock_irqsave(&phba->hbalock, iflags);
11392 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11393 spin_unlock_irqrestore(&phba->hbalock, iflags);
11394 workposted = true;
11395 break;
11396 }
11397out:
11398 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011399}
11400
11401/**
11402 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11403 * @phba: Pointer to HBA context object.
11404 * @cq: Pointer to the completion queue.
11405 * @wcqe: Pointer to a completion queue entry.
11406 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011407 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011408 * entry.
11409 *
11410 * Return: true if work posted to worker thread, otherwise false.
11411 **/
11412static bool
11413lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011414 struct lpfc_cqe *cqe)
11415{
James Smart45ed1192009-10-02 15:17:02 -040011416 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011417 bool workposted = false;
11418
11419 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011420 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011421
11422 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011423 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011424 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011425 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011426 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011427 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011428 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011429 break;
11430 case CQE_CODE_RELEASE_WQE:
11431 /* Process the WQ release event */
11432 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011433 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011434 break;
11435 case CQE_CODE_XRI_ABORTED:
11436 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011437 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011438 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011439 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011440 break;
James Smart4d9ab992009-10-02 15:16:39 -040011441 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011442 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011443 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011444 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011445 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011446 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011447 break;
James Smart4f774512009-05-22 14:52:35 -040011448 default:
11449 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11450 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011451 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011452 break;
11453 }
11454 return workposted;
11455}
11456
11457/**
James Smart4f774512009-05-22 14:52:35 -040011458 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11459 * @phba: Pointer to HBA context object.
11460 * @eqe: Pointer to fast-path event queue entry.
11461 *
11462 * This routine process a event queue entry from the slow-path event queue.
11463 * It will check the MajorCode and MinorCode to determine this is for a
11464 * completion event on a completion queue, if not, an error shall be logged
11465 * and just return. Otherwise, it will get to the corresponding completion
11466 * queue and process all the entries on that completion queue, rearm the
11467 * completion queue, and then return.
11468 *
11469 **/
11470static void
11471lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11472{
11473 struct lpfc_queue *cq = NULL, *childq, *speq;
11474 struct lpfc_cqe *cqe;
11475 bool workposted = false;
11476 int ecount = 0;
11477 uint16_t cqid;
11478
James Smartcb5172e2010-03-15 11:25:07 -040011479 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11481 "0359 Not a valid slow-path completion "
11482 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011483 bf_get_le32(lpfc_eqe_major_code, eqe),
11484 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011485 return;
11486 }
11487
11488 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011489 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011490
11491 /* Search for completion queue pointer matching this cqid */
11492 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011493 /* sanity check on queue memory */
11494 if (unlikely(!speq))
11495 return;
James Smart4f774512009-05-22 14:52:35 -040011496 list_for_each_entry(childq, &speq->child_list, list) {
11497 if (childq->queue_id == cqid) {
11498 cq = childq;
11499 break;
11500 }
11501 }
11502 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011503 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11504 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11505 "0365 Slow-path CQ identifier "
11506 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011507 return;
11508 }
11509
11510 /* Process all the entries to the CQ */
11511 switch (cq->type) {
11512 case LPFC_MCQ:
11513 while ((cqe = lpfc_sli4_cq_get(cq))) {
11514 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011515 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011516 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011517 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011518 }
11519 break;
11520 case LPFC_WCQ:
11521 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011522 if (cq->subtype == LPFC_FCP)
11523 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11524 cqe);
11525 else
11526 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11527 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011528 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011529 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11530 }
James Smartb84daac2012-08-03 12:35:13 -040011531
11532 /* Track the max number of CQEs processed in 1 EQ */
11533 if (ecount > cq->CQ_max_cqe)
11534 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011535 break;
11536 default:
11537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11538 "0370 Invalid completion queue type (%d)\n",
11539 cq->type);
11540 return;
11541 }
11542
11543 /* Catch the no cq entry condition, log an error */
11544 if (unlikely(ecount == 0))
11545 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11546 "0371 No entry from the CQ: identifier "
11547 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11548
11549 /* In any case, flash and re-arm the RCQ */
11550 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11551
11552 /* wake up worker thread if there are works to be done */
11553 if (workposted)
11554 lpfc_worker_wake_up(phba);
11555}
11556
11557/**
11558 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011559 * @phba: Pointer to HBA context object.
11560 * @cq: Pointer to associated CQ
11561 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011562 *
11563 * This routine process a fast-path work queue completion entry from fast-path
11564 * event queue for FCP command response completion.
11565 **/
11566static void
James Smart2a76a282012-08-03 12:35:54 -040011567lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011568 struct lpfc_wcqe_complete *wcqe)
11569{
James Smart2a76a282012-08-03 12:35:54 -040011570 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011571 struct lpfc_iocbq *cmdiocbq;
11572 struct lpfc_iocbq irspiocbq;
11573 unsigned long iflags;
11574
James Smart4f774512009-05-22 14:52:35 -040011575 /* Check for response status */
11576 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11577 /* If resource errors reported from HBA, reduce queue
11578 * depth of the SCSI device.
11579 */
11580 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11581 IOSTAT_LOCAL_REJECT) &&
11582 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11583 phba->lpfc_rampdown_queue_depth(phba);
11584 }
11585 /* Log the error status */
11586 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11587 "0373 FCP complete error: status=x%x, "
11588 "hw_status=x%x, total_data_specified=%d, "
11589 "parameter=x%x, word3=x%x\n",
11590 bf_get(lpfc_wcqe_c_status, wcqe),
11591 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11592 wcqe->total_data_placed, wcqe->parameter,
11593 wcqe->word3);
11594 }
11595
11596 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011597 spin_lock_irqsave(&pring->ring_lock, iflags);
11598 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011599 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11600 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011601 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011602 if (unlikely(!cmdiocbq)) {
11603 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11604 "0374 FCP complete with no corresponding "
11605 "cmdiocb: iotag (%d)\n",
11606 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11607 return;
11608 }
11609 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11610 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11611 "0375 FCP cmdiocb not callback function "
11612 "iotag: (%d)\n",
11613 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11614 return;
11615 }
11616
11617 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011618 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011619
James Smart0f65ff62010-02-26 14:14:23 -050011620 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11621 spin_lock_irqsave(&phba->hbalock, iflags);
11622 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11623 spin_unlock_irqrestore(&phba->hbalock, iflags);
11624 }
11625
James Smart4f774512009-05-22 14:52:35 -040011626 /* Pass the cmd_iocb and the rsp state to the upper layer */
11627 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11628}
11629
11630/**
11631 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11632 * @phba: Pointer to HBA context object.
11633 * @cq: Pointer to completion queue.
11634 * @wcqe: Pointer to work-queue completion queue entry.
11635 *
11636 * This routine handles an fast-path WQ entry comsumed event by invoking the
11637 * proper WQ release routine to the slow-path WQ.
11638 **/
11639static void
11640lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11641 struct lpfc_wcqe_release *wcqe)
11642{
11643 struct lpfc_queue *childwq;
11644 bool wqid_matched = false;
11645 uint16_t fcp_wqid;
11646
11647 /* Check for fast-path FCP work queue release */
11648 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11649 list_for_each_entry(childwq, &cq->child_list, list) {
11650 if (childwq->queue_id == fcp_wqid) {
11651 lpfc_sli4_wq_release(childwq,
11652 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11653 wqid_matched = true;
11654 break;
11655 }
11656 }
11657 /* Report warning log message if no match found */
11658 if (wqid_matched != true)
11659 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11660 "2580 Fast-path wqe consume event carries "
11661 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11662}
11663
11664/**
11665 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11666 * @cq: Pointer to the completion queue.
11667 * @eqe: Pointer to fast-path completion queue entry.
11668 *
11669 * This routine process a fast-path work queue completion entry from fast-path
11670 * event queue for FCP command response completion.
11671 **/
11672static int
11673lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11674 struct lpfc_cqe *cqe)
11675{
11676 struct lpfc_wcqe_release wcqe;
11677 bool workposted = false;
11678
11679 /* Copy the work queue CQE and convert endian order if needed */
11680 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11681
11682 /* Check and process for different type of WCQE and dispatch */
11683 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11684 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011685 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011686 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011687 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011688 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040011689 (struct lpfc_wcqe_complete *)&wcqe);
11690 break;
11691 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011692 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011693 /* Process the WQ release event */
11694 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11695 (struct lpfc_wcqe_release *)&wcqe);
11696 break;
11697 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011698 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011699 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011700 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011701 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11702 (struct sli4_wcqe_xri_aborted *)&wcqe);
11703 break;
11704 default:
11705 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11706 "0144 Not a valid WCQE code: x%x\n",
11707 bf_get(lpfc_wcqe_c_code, &wcqe));
11708 break;
11709 }
11710 return workposted;
11711}
11712
11713/**
11714 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11715 * @phba: Pointer to HBA context object.
11716 * @eqe: Pointer to fast-path event queue entry.
11717 *
11718 * This routine process a event queue entry from the fast-path event queue.
11719 * It will check the MajorCode and MinorCode to determine this is for a
11720 * completion event on a completion queue, if not, an error shall be logged
11721 * and just return. Otherwise, it will get to the corresponding completion
11722 * queue and process all the entries on the completion queue, rearm the
11723 * completion queue, and then return.
11724 **/
11725static void
11726lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11727 uint32_t fcp_cqidx)
11728{
11729 struct lpfc_queue *cq;
11730 struct lpfc_cqe *cqe;
11731 bool workposted = false;
11732 uint16_t cqid;
11733 int ecount = 0;
11734
James Smartcb5172e2010-03-15 11:25:07 -040011735 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11737 "0366 Not a valid fast-path completion "
11738 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011739 bf_get_le32(lpfc_eqe_major_code, eqe),
11740 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011741 return;
11742 }
11743
James Smart2e90f4b2011-12-13 13:22:37 -050011744 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11745 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11746 "3146 Fast-path completion queues "
11747 "does not exist\n");
11748 return;
11749 }
James Smart4f774512009-05-22 14:52:35 -040011750 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11751 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011752 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11753 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11754 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011755 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011756 return;
11757 }
11758
11759 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011760 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011761 if (unlikely(cqid != cq->queue_id)) {
11762 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11763 "0368 Miss-matched fast-path completion "
11764 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11765 cqid, cq->queue_id);
11766 return;
11767 }
11768
11769 /* Process all the entries to the CQ */
11770 while ((cqe = lpfc_sli4_cq_get(cq))) {
11771 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011772 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011773 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11774 }
11775
James Smartb84daac2012-08-03 12:35:13 -040011776 /* Track the max number of CQEs processed in 1 EQ */
11777 if (ecount > cq->CQ_max_cqe)
11778 cq->CQ_max_cqe = ecount;
11779
James Smart4f774512009-05-22 14:52:35 -040011780 /* Catch the no cq entry condition */
11781 if (unlikely(ecount == 0))
11782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11783 "0369 No entry from fast-path completion "
11784 "queue fcpcqid=%d\n", cq->queue_id);
11785
11786 /* In any case, flash and re-arm the CQ */
11787 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11788
11789 /* wake up worker thread if there are works to be done */
11790 if (workposted)
11791 lpfc_worker_wake_up(phba);
11792}
11793
11794static void
11795lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11796{
11797 struct lpfc_eqe *eqe;
11798
11799 /* walk all the EQ entries and drop on the floor */
11800 while ((eqe = lpfc_sli4_eq_get(eq)))
11801 ;
11802
11803 /* Clear and re-arm the EQ */
11804 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11805}
11806
11807/**
11808 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11809 * @irq: Interrupt number.
11810 * @dev_id: The device context pointer.
11811 *
11812 * This function is directly called from the PCI layer as an interrupt
11813 * service routine when device with SLI-4 interface spec is enabled with
11814 * MSI-X multi-message interrupt mode and there are slow-path events in
11815 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11816 * interrupt mode, this function is called as part of the device-level
11817 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11818 * undergoing initialization, the interrupt handler will not process the
11819 * interrupt. The link attention and ELS ring attention events are handled
11820 * by the worker thread. The interrupt handler signals the worker thread
11821 * and returns for these events. This function is called without any lock
11822 * held. It gets the hbalock to access and update SLI data structures.
11823 *
11824 * This function returns IRQ_HANDLED when interrupt is handled else it
11825 * returns IRQ_NONE.
11826 **/
11827irqreturn_t
11828lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11829{
11830 struct lpfc_hba *phba;
11831 struct lpfc_queue *speq;
11832 struct lpfc_eqe *eqe;
11833 unsigned long iflag;
11834 int ecount = 0;
11835
11836 /*
11837 * Get the driver's phba structure from the dev_id
11838 */
11839 phba = (struct lpfc_hba *)dev_id;
11840
11841 if (unlikely(!phba))
11842 return IRQ_NONE;
11843
11844 /* Get to the EQ struct associated with this vector */
11845 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011846 if (unlikely(!speq))
11847 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011848
11849 /* Check device state for handling interrupt */
11850 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011851 speq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011852 /* Check again for link_state with lock held */
11853 spin_lock_irqsave(&phba->hbalock, iflag);
11854 if (phba->link_state < LPFC_LINK_DOWN)
11855 /* Flush, clear interrupt, and rearm the EQ */
11856 lpfc_sli4_eq_flush(phba, speq);
11857 spin_unlock_irqrestore(&phba->hbalock, iflag);
11858 return IRQ_NONE;
11859 }
11860
11861 /*
11862 * Process all the event on FCP slow-path EQ
11863 */
11864 while ((eqe = lpfc_sli4_eq_get(speq))) {
11865 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011866 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011867 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011868 speq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011869 }
11870
James Smartb84daac2012-08-03 12:35:13 -040011871 /* Track the max number of EQEs processed in 1 intr */
11872 if (ecount > speq->EQ_max_eqe)
11873 speq->EQ_max_eqe = ecount;
11874
James Smart4f774512009-05-22 14:52:35 -040011875 /* Always clear and re-arm the slow-path EQ */
11876 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11877
11878 /* Catch the no cq entry condition */
11879 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011880 speq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011881 if (phba->intr_type == MSIX)
11882 /* MSI-X treated interrupt served as no EQ share INT */
11883 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11884 "0357 MSI-X interrupt with no EQE\n");
11885 else
11886 /* Non MSI-X treated on interrupt as EQ share INT */
11887 return IRQ_NONE;
11888 }
11889
11890 return IRQ_HANDLED;
11891} /* lpfc_sli4_sp_intr_handler */
11892
11893/**
11894 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11895 * @irq: Interrupt number.
11896 * @dev_id: The device context pointer.
11897 *
11898 * This function is directly called from the PCI layer as an interrupt
11899 * service routine when device with SLI-4 interface spec is enabled with
11900 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11901 * ring event in the HBA. However, when the device is enabled with either
11902 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11903 * device-level interrupt handler. When the PCI slot is in error recovery
11904 * or the HBA is undergoing initialization, the interrupt handler will not
11905 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11906 * the intrrupt context. This function is called without any lock held.
11907 * It gets the hbalock to access and update SLI data structures. Note that,
11908 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11909 * equal to that of FCP CQ index.
11910 *
11911 * This function returns IRQ_HANDLED when interrupt is handled else it
11912 * returns IRQ_NONE.
11913 **/
11914irqreturn_t
11915lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11916{
11917 struct lpfc_hba *phba;
11918 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11919 struct lpfc_queue *fpeq;
11920 struct lpfc_eqe *eqe;
11921 unsigned long iflag;
11922 int ecount = 0;
11923 uint32_t fcp_eqidx;
11924
11925 /* Get the driver's phba structure from the dev_id */
11926 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11927 phba = fcp_eq_hdl->phba;
11928 fcp_eqidx = fcp_eq_hdl->idx;
11929
11930 if (unlikely(!phba))
11931 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011932 if (unlikely(!phba->sli4_hba.fp_eq))
11933 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011934
11935 /* Get to the EQ struct associated with this vector */
11936 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011937 if (unlikely(!fpeq))
11938 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011939
11940 /* Check device state for handling interrupt */
11941 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011942 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011943 /* Check again for link_state with lock held */
11944 spin_lock_irqsave(&phba->hbalock, iflag);
11945 if (phba->link_state < LPFC_LINK_DOWN)
11946 /* Flush, clear interrupt, and rearm the EQ */
11947 lpfc_sli4_eq_flush(phba, fpeq);
11948 spin_unlock_irqrestore(&phba->hbalock, iflag);
11949 return IRQ_NONE;
11950 }
11951
11952 /*
11953 * Process all the event on FCP fast-path EQ
11954 */
11955 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11956 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011957 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011958 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011959 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011960 }
11961
James Smartb84daac2012-08-03 12:35:13 -040011962 /* Track the max number of EQEs processed in 1 intr */
11963 if (ecount > fpeq->EQ_max_eqe)
11964 fpeq->EQ_max_eqe = ecount;
11965
James Smart4f774512009-05-22 14:52:35 -040011966 /* Always clear and re-arm the fast-path EQ */
11967 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11968
11969 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011970 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011971 if (phba->intr_type == MSIX)
11972 /* MSI-X treated interrupt served as no EQ share INT */
11973 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11974 "0358 MSI-X interrupt with no EQE\n");
11975 else
11976 /* Non MSI-X treated on interrupt as EQ share INT */
11977 return IRQ_NONE;
11978 }
11979
11980 return IRQ_HANDLED;
11981} /* lpfc_sli4_fp_intr_handler */
11982
11983/**
11984 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11985 * @irq: Interrupt number.
11986 * @dev_id: The device context pointer.
11987 *
11988 * This function is the device-level interrupt handler to device with SLI-4
11989 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11990 * interrupt mode is enabled and there is an event in the HBA which requires
11991 * driver attention. This function invokes the slow-path interrupt attention
11992 * handling function and fast-path interrupt attention handling function in
11993 * turn to process the relevant HBA attention events. This function is called
11994 * without any lock held. It gets the hbalock to access and update SLI data
11995 * structures.
11996 *
11997 * This function returns IRQ_HANDLED when interrupt is handled, else it
11998 * returns IRQ_NONE.
11999 **/
12000irqreturn_t
12001lpfc_sli4_intr_handler(int irq, void *dev_id)
12002{
12003 struct lpfc_hba *phba;
12004 irqreturn_t sp_irq_rc, fp_irq_rc;
12005 bool fp_handled = false;
12006 uint32_t fcp_eqidx;
12007
12008 /* Get the driver's phba structure from the dev_id */
12009 phba = (struct lpfc_hba *)dev_id;
12010
12011 if (unlikely(!phba))
12012 return IRQ_NONE;
12013
12014 /*
12015 * Invokes slow-path host attention interrupt handling as appropriate.
12016 */
12017 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
12018
12019 /*
12020 * Invoke fast-path host attention interrupt handling as appropriate.
12021 */
12022 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
12023 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
12024 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12025 if (fp_irq_rc == IRQ_HANDLED)
12026 fp_handled |= true;
12027 }
12028
12029 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
12030} /* lpfc_sli4_intr_handler */
12031
12032/**
12033 * lpfc_sli4_queue_free - free a queue structure and associated memory
12034 * @queue: The queue structure to free.
12035 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012036 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012037 * the host resident queue. This function must be called after destroying the
12038 * queue on the HBA.
12039 **/
12040void
12041lpfc_sli4_queue_free(struct lpfc_queue *queue)
12042{
12043 struct lpfc_dmabuf *dmabuf;
12044
12045 if (!queue)
12046 return;
12047
12048 while (!list_empty(&queue->page_list)) {
12049 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12050 list);
James Smart49198b32010-04-06 15:04:33 -040012051 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012052 dmabuf->virt, dmabuf->phys);
12053 kfree(dmabuf);
12054 }
12055 kfree(queue);
12056 return;
12057}
12058
12059/**
12060 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12061 * @phba: The HBA that this queue is being created on.
12062 * @entry_size: The size of each queue entry for this queue.
12063 * @entry count: The number of entries that this queue will handle.
12064 *
12065 * This function allocates a queue structure and the DMAable memory used for
12066 * the host resident queue. This function must be called before creating the
12067 * queue on the HBA.
12068 **/
12069struct lpfc_queue *
12070lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12071 uint32_t entry_count)
12072{
12073 struct lpfc_queue *queue;
12074 struct lpfc_dmabuf *dmabuf;
12075 int x, total_qe_count;
12076 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012077 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012078
James Smartcb5172e2010-03-15 11:25:07 -040012079 if (!phba->sli4_hba.pc_sli4_params.supported)
12080 hw_page_size = SLI4_PAGE_SIZE;
12081
James Smart4f774512009-05-22 14:52:35 -040012082 queue = kzalloc(sizeof(struct lpfc_queue) +
12083 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12084 if (!queue)
12085 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012086 queue->page_count = (ALIGN(entry_size * entry_count,
12087 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012088 INIT_LIST_HEAD(&queue->list);
12089 INIT_LIST_HEAD(&queue->page_list);
12090 INIT_LIST_HEAD(&queue->child_list);
12091 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12092 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12093 if (!dmabuf)
12094 goto out_fail;
12095 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012096 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012097 GFP_KERNEL);
12098 if (!dmabuf->virt) {
12099 kfree(dmabuf);
12100 goto out_fail;
12101 }
James Smartcb5172e2010-03-15 11:25:07 -040012102 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012103 dmabuf->buffer_tag = x;
12104 list_add_tail(&dmabuf->list, &queue->page_list);
12105 /* initialize queue's entry array */
12106 dma_pointer = dmabuf->virt;
12107 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012108 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012109 total_qe_count++, dma_pointer += entry_size) {
12110 queue->qe[total_qe_count].address = dma_pointer;
12111 }
12112 }
12113 queue->entry_size = entry_size;
12114 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012115
12116 /*
12117 * entry_repost is calculated based on the number of entries in the
12118 * queue. This works out except for RQs. If buffers are NOT initially
12119 * posted for every RQE, entry_repost should be adjusted accordingly.
12120 */
12121 queue->entry_repost = (entry_count >> 3);
12122 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12123 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012124 queue->phba = phba;
12125
12126 return queue;
12127out_fail:
12128 lpfc_sli4_queue_free(queue);
12129 return NULL;
12130}
12131
12132/**
James Smart173edbb2012-06-12 13:54:50 -040012133 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12134 * @phba: HBA structure that indicates port to create a queue on.
12135 * @startq: The starting FCP EQ to modify
12136 *
12137 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12138 *
12139 * The @phba struct is used to send mailbox command to HBA. The @startq
12140 * is used to get the starting FCP EQ to change.
12141 * This function is asynchronous and will wait for the mailbox
12142 * command to finish before continuing.
12143 *
12144 * On success this function will return a zero. If unable to allocate enough
12145 * memory this function will return -ENOMEM. If the queue create mailbox command
12146 * fails this function will return -ENXIO.
12147 **/
12148uint32_t
12149lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12150{
12151 struct lpfc_mbx_modify_eq_delay *eq_delay;
12152 LPFC_MBOXQ_t *mbox;
12153 struct lpfc_queue *eq;
12154 int cnt, rc, length, status = 0;
12155 uint32_t shdr_status, shdr_add_status;
12156 int fcp_eqidx;
12157 union lpfc_sli4_cfg_shdr *shdr;
12158 uint16_t dmult;
12159
12160 if (startq >= phba->cfg_fcp_eq_count)
12161 return 0;
12162
12163 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12164 if (!mbox)
12165 return -ENOMEM;
12166 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12167 sizeof(struct lpfc_sli4_cfg_mhdr));
12168 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12169 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12170 length, LPFC_SLI4_MBX_EMBED);
12171 eq_delay = &mbox->u.mqe.un.eq_delay;
12172
12173 /* Calculate delay multiper from maximum interrupt per second */
12174 dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
12175
12176 cnt = 0;
12177 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
12178 fcp_eqidx++) {
12179 eq = phba->sli4_hba.fp_eq[fcp_eqidx];
12180 if (!eq)
12181 continue;
12182 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12183 eq_delay->u.request.eq[cnt].phase = 0;
12184 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12185 cnt++;
12186 if (cnt >= LPFC_MAX_EQ_DELAY)
12187 break;
12188 }
12189 eq_delay->u.request.num_eq = cnt;
12190
12191 mbox->vport = phba->pport;
12192 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12193 mbox->context1 = NULL;
12194 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12195 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12196 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12197 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12198 if (shdr_status || shdr_add_status || rc) {
12199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12200 "2512 MODIFY_EQ_DELAY mailbox failed with "
12201 "status x%x add_status x%x, mbx status x%x\n",
12202 shdr_status, shdr_add_status, rc);
12203 status = -ENXIO;
12204 }
12205 mempool_free(mbox, phba->mbox_mem_pool);
12206 return status;
12207}
12208
12209/**
James Smart4f774512009-05-22 14:52:35 -040012210 * lpfc_eq_create - Create an Event Queue on the HBA
12211 * @phba: HBA structure that indicates port to create a queue on.
12212 * @eq: The queue structure to use to create the event queue.
12213 * @imax: The maximum interrupt per second limit.
12214 *
12215 * This function creates an event queue, as detailed in @eq, on a port,
12216 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12217 *
12218 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12219 * is used to get the entry count and entry size that are necessary to
12220 * determine the number of pages to allocate and use for this queue. This
12221 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12222 * event queue. This function is asynchronous and will wait for the mailbox
12223 * command to finish before continuing.
12224 *
12225 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012226 * memory this function will return -ENOMEM. If the queue create mailbox command
12227 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012228 **/
12229uint32_t
12230lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12231{
12232 struct lpfc_mbx_eq_create *eq_create;
12233 LPFC_MBOXQ_t *mbox;
12234 int rc, length, status = 0;
12235 struct lpfc_dmabuf *dmabuf;
12236 uint32_t shdr_status, shdr_add_status;
12237 union lpfc_sli4_cfg_shdr *shdr;
12238 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012239 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12240
James Smart2e90f4b2011-12-13 13:22:37 -050012241 /* sanity check on queue memory */
12242 if (!eq)
12243 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012244 if (!phba->sli4_hba.pc_sli4_params.supported)
12245 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012246
12247 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12248 if (!mbox)
12249 return -ENOMEM;
12250 length = (sizeof(struct lpfc_mbx_eq_create) -
12251 sizeof(struct lpfc_sli4_cfg_mhdr));
12252 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12253 LPFC_MBOX_OPCODE_EQ_CREATE,
12254 length, LPFC_SLI4_MBX_EMBED);
12255 eq_create = &mbox->u.mqe.un.eq_create;
12256 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12257 eq->page_count);
12258 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12259 LPFC_EQE_SIZE);
12260 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12261 /* Calculate delay multiper from maximum interrupt per second */
12262 dmult = LPFC_DMULT_CONST/imax - 1;
12263 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12264 dmult);
12265 switch (eq->entry_count) {
12266 default:
12267 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12268 "0360 Unsupported EQ count. (%d)\n",
12269 eq->entry_count);
12270 if (eq->entry_count < 256)
12271 return -EINVAL;
12272 /* otherwise default to smallest count (drop through) */
12273 case 256:
12274 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12275 LPFC_EQ_CNT_256);
12276 break;
12277 case 512:
12278 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12279 LPFC_EQ_CNT_512);
12280 break;
12281 case 1024:
12282 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12283 LPFC_EQ_CNT_1024);
12284 break;
12285 case 2048:
12286 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12287 LPFC_EQ_CNT_2048);
12288 break;
12289 case 4096:
12290 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12291 LPFC_EQ_CNT_4096);
12292 break;
12293 }
12294 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012295 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012296 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12297 putPaddrLow(dmabuf->phys);
12298 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12299 putPaddrHigh(dmabuf->phys);
12300 }
12301 mbox->vport = phba->pport;
12302 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12303 mbox->context1 = NULL;
12304 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12305 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12306 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12307 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12308 if (shdr_status || shdr_add_status || rc) {
12309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12310 "2500 EQ_CREATE mailbox failed with "
12311 "status x%x add_status x%x, mbx status x%x\n",
12312 shdr_status, shdr_add_status, rc);
12313 status = -ENXIO;
12314 }
12315 eq->type = LPFC_EQ;
12316 eq->subtype = LPFC_NONE;
12317 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12318 if (eq->queue_id == 0xFFFF)
12319 status = -ENXIO;
12320 eq->host_index = 0;
12321 eq->hba_index = 0;
12322
James Smart8fa38512009-07-19 10:01:03 -040012323 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012324 return status;
12325}
12326
12327/**
12328 * lpfc_cq_create - Create a Completion Queue on the HBA
12329 * @phba: HBA structure that indicates port to create a queue on.
12330 * @cq: The queue structure to use to create the completion queue.
12331 * @eq: The event queue to bind this completion queue to.
12332 *
12333 * This function creates a completion queue, as detailed in @wq, on a port,
12334 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12335 *
12336 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12337 * is used to get the entry count and entry size that are necessary to
12338 * determine the number of pages to allocate and use for this queue. The @eq
12339 * is used to indicate which event queue to bind this completion queue to. This
12340 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12341 * completion queue. This function is asynchronous and will wait for the mailbox
12342 * command to finish before continuing.
12343 *
12344 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012345 * memory this function will return -ENOMEM. If the queue create mailbox command
12346 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012347 **/
12348uint32_t
12349lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12350 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12351{
12352 struct lpfc_mbx_cq_create *cq_create;
12353 struct lpfc_dmabuf *dmabuf;
12354 LPFC_MBOXQ_t *mbox;
12355 int rc, length, status = 0;
12356 uint32_t shdr_status, shdr_add_status;
12357 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012358 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12359
James Smart2e90f4b2011-12-13 13:22:37 -050012360 /* sanity check on queue memory */
12361 if (!cq || !eq)
12362 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012363 if (!phba->sli4_hba.pc_sli4_params.supported)
12364 hw_page_size = SLI4_PAGE_SIZE;
12365
James Smart4f774512009-05-22 14:52:35 -040012366 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12367 if (!mbox)
12368 return -ENOMEM;
12369 length = (sizeof(struct lpfc_mbx_cq_create) -
12370 sizeof(struct lpfc_sli4_cfg_mhdr));
12371 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12372 LPFC_MBOX_OPCODE_CQ_CREATE,
12373 length, LPFC_SLI4_MBX_EMBED);
12374 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012375 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012376 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12377 cq->page_count);
12378 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12379 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012380 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12381 phba->sli4_hba.pc_sli4_params.cqv);
12382 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012383 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12384 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012385 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12386 eq->queue_id);
12387 } else {
12388 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12389 eq->queue_id);
12390 }
James Smart4f774512009-05-22 14:52:35 -040012391 switch (cq->entry_count) {
12392 default:
12393 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12394 "0361 Unsupported CQ count. (%d)\n",
12395 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012396 if (cq->entry_count < 256) {
12397 status = -EINVAL;
12398 goto out;
12399 }
James Smart4f774512009-05-22 14:52:35 -040012400 /* otherwise default to smallest count (drop through) */
12401 case 256:
12402 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12403 LPFC_CQ_CNT_256);
12404 break;
12405 case 512:
12406 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12407 LPFC_CQ_CNT_512);
12408 break;
12409 case 1024:
12410 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12411 LPFC_CQ_CNT_1024);
12412 break;
12413 }
12414 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012415 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012416 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12417 putPaddrLow(dmabuf->phys);
12418 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12419 putPaddrHigh(dmabuf->phys);
12420 }
12421 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12422
12423 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012424 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12425 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12426 if (shdr_status || shdr_add_status || rc) {
12427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12428 "2501 CQ_CREATE mailbox failed with "
12429 "status x%x add_status x%x, mbx status x%x\n",
12430 shdr_status, shdr_add_status, rc);
12431 status = -ENXIO;
12432 goto out;
12433 }
12434 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12435 if (cq->queue_id == 0xFFFF) {
12436 status = -ENXIO;
12437 goto out;
12438 }
12439 /* link the cq onto the parent eq child list */
12440 list_add_tail(&cq->list, &eq->child_list);
12441 /* Set up completion queue's type and subtype */
12442 cq->type = type;
12443 cq->subtype = subtype;
12444 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012445 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012446 cq->host_index = 0;
12447 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012448
James Smart8fa38512009-07-19 10:01:03 -040012449out:
12450 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012451 return status;
12452}
12453
12454/**
James Smartb19a0612010-04-06 14:48:51 -040012455 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012456 * @phba: HBA structure that indicates port to create a queue on.
12457 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012458 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12459 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012460 *
James Smartb19a0612010-04-06 14:48:51 -040012461 * This function provides failback (fb) functionality when the
12462 * mq_create_ext fails on older FW generations. It's purpose is identical
12463 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012464 *
James Smartb19a0612010-04-06 14:48:51 -040012465 * This routine cannot fail as all attributes were previously accessed and
12466 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012467 **/
James Smartb19a0612010-04-06 14:48:51 -040012468static void
12469lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12470 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012471{
12472 struct lpfc_mbx_mq_create *mq_create;
12473 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012474 int length;
James Smart04c68492009-05-22 14:52:52 -040012475
James Smart04c68492009-05-22 14:52:52 -040012476 length = (sizeof(struct lpfc_mbx_mq_create) -
12477 sizeof(struct lpfc_sli4_cfg_mhdr));
12478 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12479 LPFC_MBOX_OPCODE_MQ_CREATE,
12480 length, LPFC_SLI4_MBX_EMBED);
12481 mq_create = &mbox->u.mqe.un.mq_create;
12482 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012483 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012484 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012485 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012486 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12487 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012488 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012489 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12490 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012491 break;
12492 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012493 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12494 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012495 break;
12496 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012497 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12498 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012499 break;
12500 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012501 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12502 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012503 break;
12504 }
12505 list_for_each_entry(dmabuf, &mq->page_list, list) {
12506 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012507 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012508 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012509 putPaddrHigh(dmabuf->phys);
12510 }
12511}
12512
12513/**
12514 * lpfc_mq_create - Create a mailbox Queue on the HBA
12515 * @phba: HBA structure that indicates port to create a queue on.
12516 * @mq: The queue structure to use to create the mailbox queue.
12517 * @cq: The completion queue to associate with this cq.
12518 * @subtype: The queue's subtype.
12519 *
12520 * This function creates a mailbox queue, as detailed in @mq, on a port,
12521 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12522 *
12523 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12524 * is used to get the entry count and entry size that are necessary to
12525 * determine the number of pages to allocate and use for this queue. This
12526 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12527 * mailbox queue. This function is asynchronous and will wait for the mailbox
12528 * command to finish before continuing.
12529 *
12530 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012531 * memory this function will return -ENOMEM. If the queue create mailbox command
12532 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012533 **/
12534int32_t
12535lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12536 struct lpfc_queue *cq, uint32_t subtype)
12537{
12538 struct lpfc_mbx_mq_create *mq_create;
12539 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12540 struct lpfc_dmabuf *dmabuf;
12541 LPFC_MBOXQ_t *mbox;
12542 int rc, length, status = 0;
12543 uint32_t shdr_status, shdr_add_status;
12544 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012545 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012546
James Smart2e90f4b2011-12-13 13:22:37 -050012547 /* sanity check on queue memory */
12548 if (!mq || !cq)
12549 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012550 if (!phba->sli4_hba.pc_sli4_params.supported)
12551 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012552
12553 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12554 if (!mbox)
12555 return -ENOMEM;
12556 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12557 sizeof(struct lpfc_sli4_cfg_mhdr));
12558 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12559 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12560 length, LPFC_SLI4_MBX_EMBED);
12561
12562 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012563 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012564 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12565 &mq_create_ext->u.request, mq->page_count);
12566 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12567 &mq_create_ext->u.request, 1);
12568 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012569 &mq_create_ext->u.request, 1);
12570 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12571 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012572 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12573 &mq_create_ext->u.request, 1);
12574 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12575 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012576 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012577 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12578 phba->sli4_hba.pc_sli4_params.mqv);
12579 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12580 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12581 cq->queue_id);
12582 else
12583 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12584 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012585 switch (mq->entry_count) {
12586 default:
12587 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12588 "0362 Unsupported MQ count. (%d)\n",
12589 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012590 if (mq->entry_count < 16) {
12591 status = -EINVAL;
12592 goto out;
12593 }
James Smartb19a0612010-04-06 14:48:51 -040012594 /* otherwise default to smallest count (drop through) */
12595 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012596 bf_set(lpfc_mq_context_ring_size,
12597 &mq_create_ext->u.request.context,
12598 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012599 break;
12600 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012601 bf_set(lpfc_mq_context_ring_size,
12602 &mq_create_ext->u.request.context,
12603 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012604 break;
12605 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012606 bf_set(lpfc_mq_context_ring_size,
12607 &mq_create_ext->u.request.context,
12608 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012609 break;
12610 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012611 bf_set(lpfc_mq_context_ring_size,
12612 &mq_create_ext->u.request.context,
12613 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012614 break;
12615 }
12616 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012617 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012618 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12619 putPaddrLow(dmabuf->phys);
12620 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012621 putPaddrHigh(dmabuf->phys);
12622 }
12623 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012624 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12625 &mq_create_ext->u.response);
12626 if (rc != MBX_SUCCESS) {
12627 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12628 "2795 MQ_CREATE_EXT failed with "
12629 "status x%x. Failback to MQ_CREATE.\n",
12630 rc);
12631 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12632 mq_create = &mbox->u.mqe.un.mq_create;
12633 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12634 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12635 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12636 &mq_create->u.response);
12637 }
12638
James Smart04c68492009-05-22 14:52:52 -040012639 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012640 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12641 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12642 if (shdr_status || shdr_add_status || rc) {
12643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12644 "2502 MQ_CREATE mailbox failed with "
12645 "status x%x add_status x%x, mbx status x%x\n",
12646 shdr_status, shdr_add_status, rc);
12647 status = -ENXIO;
12648 goto out;
12649 }
James Smart04c68492009-05-22 14:52:52 -040012650 if (mq->queue_id == 0xFFFF) {
12651 status = -ENXIO;
12652 goto out;
12653 }
12654 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012655 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012656 mq->subtype = subtype;
12657 mq->host_index = 0;
12658 mq->hba_index = 0;
12659
12660 /* link the mq onto the parent cq child list */
12661 list_add_tail(&mq->list, &cq->child_list);
12662out:
James Smart8fa38512009-07-19 10:01:03 -040012663 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012664 return status;
12665}
12666
12667/**
James Smart4f774512009-05-22 14:52:35 -040012668 * lpfc_wq_create - Create a Work Queue on the HBA
12669 * @phba: HBA structure that indicates port to create a queue on.
12670 * @wq: The queue structure to use to create the work queue.
12671 * @cq: The completion queue to bind this work queue to.
12672 * @subtype: The subtype of the work queue indicating its functionality.
12673 *
12674 * This function creates a work queue, as detailed in @wq, on a port, described
12675 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12676 *
12677 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12678 * is used to get the entry count and entry size that are necessary to
12679 * determine the number of pages to allocate and use for this queue. The @cq
12680 * is used to indicate which completion queue to bind this work queue to. This
12681 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12682 * work queue. This function is asynchronous and will wait for the mailbox
12683 * command to finish before continuing.
12684 *
12685 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012686 * memory this function will return -ENOMEM. If the queue create mailbox command
12687 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012688 **/
12689uint32_t
12690lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12691 struct lpfc_queue *cq, uint32_t subtype)
12692{
12693 struct lpfc_mbx_wq_create *wq_create;
12694 struct lpfc_dmabuf *dmabuf;
12695 LPFC_MBOXQ_t *mbox;
12696 int rc, length, status = 0;
12697 uint32_t shdr_status, shdr_add_status;
12698 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012699 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012700 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012701
James Smart2e90f4b2011-12-13 13:22:37 -050012702 /* sanity check on queue memory */
12703 if (!wq || !cq)
12704 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012705 if (!phba->sli4_hba.pc_sli4_params.supported)
12706 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012707
12708 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12709 if (!mbox)
12710 return -ENOMEM;
12711 length = (sizeof(struct lpfc_mbx_wq_create) -
12712 sizeof(struct lpfc_sli4_cfg_mhdr));
12713 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12714 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12715 length, LPFC_SLI4_MBX_EMBED);
12716 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012717 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012718 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12719 wq->page_count);
12720 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12721 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012722 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12723 phba->sli4_hba.pc_sli4_params.wqv);
12724 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12725 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12726 wq->entry_count);
12727 switch (wq->entry_size) {
12728 default:
12729 case 64:
12730 bf_set(lpfc_mbx_wq_create_wqe_size,
12731 &wq_create->u.request_1,
12732 LPFC_WQ_WQE_SIZE_64);
12733 break;
12734 case 128:
12735 bf_set(lpfc_mbx_wq_create_wqe_size,
12736 &wq_create->u.request_1,
12737 LPFC_WQ_WQE_SIZE_128);
12738 break;
12739 }
12740 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12741 (PAGE_SIZE/SLI4_PAGE_SIZE));
12742 page = wq_create->u.request_1.page;
12743 } else {
12744 page = wq_create->u.request.page;
12745 }
James Smart4f774512009-05-22 14:52:35 -040012746 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012747 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012748 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12749 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012750 }
12751 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12752 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012753 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12754 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12755 if (shdr_status || shdr_add_status || rc) {
12756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12757 "2503 WQ_CREATE mailbox failed with "
12758 "status x%x add_status x%x, mbx status x%x\n",
12759 shdr_status, shdr_add_status, rc);
12760 status = -ENXIO;
12761 goto out;
12762 }
12763 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12764 if (wq->queue_id == 0xFFFF) {
12765 status = -ENXIO;
12766 goto out;
12767 }
12768 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012769 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012770 wq->subtype = subtype;
12771 wq->host_index = 0;
12772 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012773 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012774
12775 /* link the wq onto the parent cq child list */
12776 list_add_tail(&wq->list, &cq->child_list);
12777out:
James Smart8fa38512009-07-19 10:01:03 -040012778 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012779 return status;
12780}
12781
12782/**
James Smart73d91e52011-10-10 21:32:10 -040012783 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12784 * @phba: HBA structure that indicates port to create a queue on.
12785 * @rq: The queue structure to use for the receive queue.
12786 * @qno: The associated HBQ number
12787 *
12788 *
12789 * For SLI4 we need to adjust the RQ repost value based on
12790 * the number of buffers that are initially posted to the RQ.
12791 */
12792void
12793lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12794{
12795 uint32_t cnt;
12796
James Smart2e90f4b2011-12-13 13:22:37 -050012797 /* sanity check on queue memory */
12798 if (!rq)
12799 return;
James Smart73d91e52011-10-10 21:32:10 -040012800 cnt = lpfc_hbq_defs[qno]->entry_count;
12801
12802 /* Recalc repost for RQs based on buffers initially posted */
12803 cnt = (cnt >> 3);
12804 if (cnt < LPFC_QUEUE_MIN_REPOST)
12805 cnt = LPFC_QUEUE_MIN_REPOST;
12806
12807 rq->entry_repost = cnt;
12808}
12809
12810/**
James Smart4f774512009-05-22 14:52:35 -040012811 * lpfc_rq_create - Create a Receive Queue on the HBA
12812 * @phba: HBA structure that indicates port to create a queue on.
12813 * @hrq: The queue structure to use to create the header receive queue.
12814 * @drq: The queue structure to use to create the data receive queue.
12815 * @cq: The completion queue to bind this work queue to.
12816 *
12817 * This function creates a receive buffer queue pair , as detailed in @hrq and
12818 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12819 * to the HBA.
12820 *
12821 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12822 * struct is used to get the entry count that is necessary to determine the
12823 * number of pages to use for this queue. The @cq is used to indicate which
12824 * completion queue to bind received buffers that are posted to these queues to.
12825 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12826 * receive queue pair. This function is asynchronous and will wait for the
12827 * mailbox command to finish before continuing.
12828 *
12829 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012830 * memory this function will return -ENOMEM. If the queue create mailbox command
12831 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012832 **/
12833uint32_t
12834lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12835 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12836{
12837 struct lpfc_mbx_rq_create *rq_create;
12838 struct lpfc_dmabuf *dmabuf;
12839 LPFC_MBOXQ_t *mbox;
12840 int rc, length, status = 0;
12841 uint32_t shdr_status, shdr_add_status;
12842 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012843 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12844
James Smart2e90f4b2011-12-13 13:22:37 -050012845 /* sanity check on queue memory */
12846 if (!hrq || !drq || !cq)
12847 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012848 if (!phba->sli4_hba.pc_sli4_params.supported)
12849 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012850
12851 if (hrq->entry_count != drq->entry_count)
12852 return -EINVAL;
12853 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12854 if (!mbox)
12855 return -ENOMEM;
12856 length = (sizeof(struct lpfc_mbx_rq_create) -
12857 sizeof(struct lpfc_sli4_cfg_mhdr));
12858 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12859 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12860 length, LPFC_SLI4_MBX_EMBED);
12861 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012862 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12863 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12864 phba->sli4_hba.pc_sli4_params.rqv);
12865 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12866 bf_set(lpfc_rq_context_rqe_count_1,
12867 &rq_create->u.request.context,
12868 hrq->entry_count);
12869 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012870 bf_set(lpfc_rq_context_rqe_size,
12871 &rq_create->u.request.context,
12872 LPFC_RQE_SIZE_8);
12873 bf_set(lpfc_rq_context_page_size,
12874 &rq_create->u.request.context,
12875 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012876 } else {
12877 switch (hrq->entry_count) {
12878 default:
12879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12880 "2535 Unsupported RQ count. (%d)\n",
12881 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012882 if (hrq->entry_count < 512) {
12883 status = -EINVAL;
12884 goto out;
12885 }
James Smart5a6f1332011-03-11 16:05:35 -050012886 /* otherwise default to smallest count (drop through) */
12887 case 512:
12888 bf_set(lpfc_rq_context_rqe_count,
12889 &rq_create->u.request.context,
12890 LPFC_RQ_RING_SIZE_512);
12891 break;
12892 case 1024:
12893 bf_set(lpfc_rq_context_rqe_count,
12894 &rq_create->u.request.context,
12895 LPFC_RQ_RING_SIZE_1024);
12896 break;
12897 case 2048:
12898 bf_set(lpfc_rq_context_rqe_count,
12899 &rq_create->u.request.context,
12900 LPFC_RQ_RING_SIZE_2048);
12901 break;
12902 case 4096:
12903 bf_set(lpfc_rq_context_rqe_count,
12904 &rq_create->u.request.context,
12905 LPFC_RQ_RING_SIZE_4096);
12906 break;
12907 }
12908 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12909 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012910 }
12911 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12912 cq->queue_id);
12913 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12914 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012915 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012916 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012917 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12918 putPaddrLow(dmabuf->phys);
12919 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12920 putPaddrHigh(dmabuf->phys);
12921 }
12922 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12923 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012924 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12925 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12926 if (shdr_status || shdr_add_status || rc) {
12927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12928 "2504 RQ_CREATE mailbox failed with "
12929 "status x%x add_status x%x, mbx status x%x\n",
12930 shdr_status, shdr_add_status, rc);
12931 status = -ENXIO;
12932 goto out;
12933 }
12934 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12935 if (hrq->queue_id == 0xFFFF) {
12936 status = -ENXIO;
12937 goto out;
12938 }
12939 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012940 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012941 hrq->subtype = subtype;
12942 hrq->host_index = 0;
12943 hrq->hba_index = 0;
12944
12945 /* now create the data queue */
12946 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12947 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12948 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012949 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12950 phba->sli4_hba.pc_sli4_params.rqv);
12951 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12952 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012953 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012954 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012955 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12956 LPFC_RQE_SIZE_8);
12957 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12958 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012959 } else {
12960 switch (drq->entry_count) {
12961 default:
12962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12963 "2536 Unsupported RQ count. (%d)\n",
12964 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012965 if (drq->entry_count < 512) {
12966 status = -EINVAL;
12967 goto out;
12968 }
James Smart5a6f1332011-03-11 16:05:35 -050012969 /* otherwise default to smallest count (drop through) */
12970 case 512:
12971 bf_set(lpfc_rq_context_rqe_count,
12972 &rq_create->u.request.context,
12973 LPFC_RQ_RING_SIZE_512);
12974 break;
12975 case 1024:
12976 bf_set(lpfc_rq_context_rqe_count,
12977 &rq_create->u.request.context,
12978 LPFC_RQ_RING_SIZE_1024);
12979 break;
12980 case 2048:
12981 bf_set(lpfc_rq_context_rqe_count,
12982 &rq_create->u.request.context,
12983 LPFC_RQ_RING_SIZE_2048);
12984 break;
12985 case 4096:
12986 bf_set(lpfc_rq_context_rqe_count,
12987 &rq_create->u.request.context,
12988 LPFC_RQ_RING_SIZE_4096);
12989 break;
12990 }
12991 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12992 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012993 }
12994 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12995 cq->queue_id);
12996 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12997 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012998 list_for_each_entry(dmabuf, &drq->page_list, list) {
12999 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13000 putPaddrLow(dmabuf->phys);
13001 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13002 putPaddrHigh(dmabuf->phys);
13003 }
13004 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13005 /* The IOCTL status is embedded in the mailbox subheader. */
13006 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13007 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13008 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13009 if (shdr_status || shdr_add_status || rc) {
13010 status = -ENXIO;
13011 goto out;
13012 }
13013 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13014 if (drq->queue_id == 0xFFFF) {
13015 status = -ENXIO;
13016 goto out;
13017 }
13018 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013019 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013020 drq->subtype = subtype;
13021 drq->host_index = 0;
13022 drq->hba_index = 0;
13023
13024 /* link the header and data RQs onto the parent cq child list */
13025 list_add_tail(&hrq->list, &cq->child_list);
13026 list_add_tail(&drq->list, &cq->child_list);
13027
13028out:
James Smart8fa38512009-07-19 10:01:03 -040013029 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013030 return status;
13031}
13032
13033/**
13034 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13035 * @eq: The queue structure associated with the queue to destroy.
13036 *
13037 * This function destroys a queue, as detailed in @eq by sending an mailbox
13038 * command, specific to the type of queue, to the HBA.
13039 *
13040 * The @eq struct is used to get the queue ID of the queue to destroy.
13041 *
13042 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013043 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013044 **/
13045uint32_t
13046lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13047{
13048 LPFC_MBOXQ_t *mbox;
13049 int rc, length, status = 0;
13050 uint32_t shdr_status, shdr_add_status;
13051 union lpfc_sli4_cfg_shdr *shdr;
13052
James Smart2e90f4b2011-12-13 13:22:37 -050013053 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013054 if (!eq)
13055 return -ENODEV;
13056 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13057 if (!mbox)
13058 return -ENOMEM;
13059 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13060 sizeof(struct lpfc_sli4_cfg_mhdr));
13061 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13062 LPFC_MBOX_OPCODE_EQ_DESTROY,
13063 length, LPFC_SLI4_MBX_EMBED);
13064 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13065 eq->queue_id);
13066 mbox->vport = eq->phba->pport;
13067 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13068
13069 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13070 /* The IOCTL status is embedded in the mailbox subheader. */
13071 shdr = (union lpfc_sli4_cfg_shdr *)
13072 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13073 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13074 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13075 if (shdr_status || shdr_add_status || rc) {
13076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13077 "2505 EQ_DESTROY mailbox failed with "
13078 "status x%x add_status x%x, mbx status x%x\n",
13079 shdr_status, shdr_add_status, rc);
13080 status = -ENXIO;
13081 }
13082
13083 /* Remove eq from any list */
13084 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013085 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013086 return status;
13087}
13088
13089/**
13090 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13091 * @cq: The queue structure associated with the queue to destroy.
13092 *
13093 * This function destroys a queue, as detailed in @cq by sending an mailbox
13094 * command, specific to the type of queue, to the HBA.
13095 *
13096 * The @cq struct is used to get the queue ID of the queue to destroy.
13097 *
13098 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013099 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013100 **/
13101uint32_t
13102lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13103{
13104 LPFC_MBOXQ_t *mbox;
13105 int rc, length, status = 0;
13106 uint32_t shdr_status, shdr_add_status;
13107 union lpfc_sli4_cfg_shdr *shdr;
13108
James Smart2e90f4b2011-12-13 13:22:37 -050013109 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013110 if (!cq)
13111 return -ENODEV;
13112 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13113 if (!mbox)
13114 return -ENOMEM;
13115 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13116 sizeof(struct lpfc_sli4_cfg_mhdr));
13117 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13118 LPFC_MBOX_OPCODE_CQ_DESTROY,
13119 length, LPFC_SLI4_MBX_EMBED);
13120 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13121 cq->queue_id);
13122 mbox->vport = cq->phba->pport;
13123 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13124 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13125 /* The IOCTL status is embedded in the mailbox subheader. */
13126 shdr = (union lpfc_sli4_cfg_shdr *)
13127 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13128 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13129 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13130 if (shdr_status || shdr_add_status || rc) {
13131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13132 "2506 CQ_DESTROY mailbox failed with "
13133 "status x%x add_status x%x, mbx status x%x\n",
13134 shdr_status, shdr_add_status, rc);
13135 status = -ENXIO;
13136 }
13137 /* Remove cq from any list */
13138 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013139 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013140 return status;
13141}
13142
13143/**
James Smart04c68492009-05-22 14:52:52 -040013144 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13145 * @qm: The queue structure associated with the queue to destroy.
13146 *
13147 * This function destroys a queue, as detailed in @mq by sending an mailbox
13148 * command, specific to the type of queue, to the HBA.
13149 *
13150 * The @mq struct is used to get the queue ID of the queue to destroy.
13151 *
13152 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013153 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013154 **/
13155uint32_t
13156lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13157{
13158 LPFC_MBOXQ_t *mbox;
13159 int rc, length, status = 0;
13160 uint32_t shdr_status, shdr_add_status;
13161 union lpfc_sli4_cfg_shdr *shdr;
13162
James Smart2e90f4b2011-12-13 13:22:37 -050013163 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013164 if (!mq)
13165 return -ENODEV;
13166 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13167 if (!mbox)
13168 return -ENOMEM;
13169 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13170 sizeof(struct lpfc_sli4_cfg_mhdr));
13171 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13172 LPFC_MBOX_OPCODE_MQ_DESTROY,
13173 length, LPFC_SLI4_MBX_EMBED);
13174 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13175 mq->queue_id);
13176 mbox->vport = mq->phba->pport;
13177 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13178 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13179 /* The IOCTL status is embedded in the mailbox subheader. */
13180 shdr = (union lpfc_sli4_cfg_shdr *)
13181 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13182 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13183 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13184 if (shdr_status || shdr_add_status || rc) {
13185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13186 "2507 MQ_DESTROY mailbox failed with "
13187 "status x%x add_status x%x, mbx status x%x\n",
13188 shdr_status, shdr_add_status, rc);
13189 status = -ENXIO;
13190 }
13191 /* Remove mq from any list */
13192 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013193 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013194 return status;
13195}
13196
13197/**
James Smart4f774512009-05-22 14:52:35 -040013198 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13199 * @wq: The queue structure associated with the queue to destroy.
13200 *
13201 * This function destroys a queue, as detailed in @wq by sending an mailbox
13202 * command, specific to the type of queue, to the HBA.
13203 *
13204 * The @wq struct is used to get the queue ID of the queue to destroy.
13205 *
13206 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013207 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013208 **/
13209uint32_t
13210lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13211{
13212 LPFC_MBOXQ_t *mbox;
13213 int rc, length, status = 0;
13214 uint32_t shdr_status, shdr_add_status;
13215 union lpfc_sli4_cfg_shdr *shdr;
13216
James Smart2e90f4b2011-12-13 13:22:37 -050013217 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013218 if (!wq)
13219 return -ENODEV;
13220 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13221 if (!mbox)
13222 return -ENOMEM;
13223 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13224 sizeof(struct lpfc_sli4_cfg_mhdr));
13225 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13226 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13227 length, LPFC_SLI4_MBX_EMBED);
13228 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13229 wq->queue_id);
13230 mbox->vport = wq->phba->pport;
13231 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13232 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13233 shdr = (union lpfc_sli4_cfg_shdr *)
13234 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13235 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13236 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13237 if (shdr_status || shdr_add_status || rc) {
13238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13239 "2508 WQ_DESTROY mailbox failed with "
13240 "status x%x add_status x%x, mbx status x%x\n",
13241 shdr_status, shdr_add_status, rc);
13242 status = -ENXIO;
13243 }
13244 /* Remove wq from any list */
13245 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013246 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013247 return status;
13248}
13249
13250/**
13251 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13252 * @rq: The queue structure associated with the queue to destroy.
13253 *
13254 * This function destroys a queue, as detailed in @rq by sending an mailbox
13255 * command, specific to the type of queue, to the HBA.
13256 *
13257 * The @rq struct is used to get the queue ID of the queue to destroy.
13258 *
13259 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013260 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013261 **/
13262uint32_t
13263lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13264 struct lpfc_queue *drq)
13265{
13266 LPFC_MBOXQ_t *mbox;
13267 int rc, length, status = 0;
13268 uint32_t shdr_status, shdr_add_status;
13269 union lpfc_sli4_cfg_shdr *shdr;
13270
James Smart2e90f4b2011-12-13 13:22:37 -050013271 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013272 if (!hrq || !drq)
13273 return -ENODEV;
13274 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13275 if (!mbox)
13276 return -ENOMEM;
13277 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013278 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013279 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13280 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13281 length, LPFC_SLI4_MBX_EMBED);
13282 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13283 hrq->queue_id);
13284 mbox->vport = hrq->phba->pport;
13285 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13286 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13287 /* The IOCTL status is embedded in the mailbox subheader. */
13288 shdr = (union lpfc_sli4_cfg_shdr *)
13289 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13290 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13291 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13292 if (shdr_status || shdr_add_status || rc) {
13293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13294 "2509 RQ_DESTROY mailbox failed with "
13295 "status x%x add_status x%x, mbx status x%x\n",
13296 shdr_status, shdr_add_status, rc);
13297 if (rc != MBX_TIMEOUT)
13298 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13299 return -ENXIO;
13300 }
13301 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13302 drq->queue_id);
13303 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13304 shdr = (union lpfc_sli4_cfg_shdr *)
13305 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13306 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13307 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13308 if (shdr_status || shdr_add_status || rc) {
13309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13310 "2510 RQ_DESTROY mailbox failed with "
13311 "status x%x add_status x%x, mbx status x%x\n",
13312 shdr_status, shdr_add_status, rc);
13313 status = -ENXIO;
13314 }
13315 list_del_init(&hrq->list);
13316 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013317 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013318 return status;
13319}
13320
13321/**
13322 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13323 * @phba: The virtual port for which this call being executed.
13324 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13325 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13326 * @xritag: the xritag that ties this io to the SGL pages.
13327 *
13328 * This routine will post the sgl pages for the IO that has the xritag
13329 * that is in the iocbq structure. The xritag is assigned during iocbq
13330 * creation and persists for as long as the driver is loaded.
13331 * if the caller has fewer than 256 scatter gather segments to map then
13332 * pdma_phys_addr1 should be 0.
13333 * If the caller needs to map more than 256 scatter gather segment then
13334 * pdma_phys_addr1 should be a valid physical address.
13335 * physical address for SGLs must be 64 byte aligned.
13336 * If you are going to map 2 SGL's then the first one must have 256 entries
13337 * the second sgl can have between 1 and 256 entries.
13338 *
13339 * Return codes:
13340 * 0 - Success
13341 * -ENXIO, -ENOMEM - Failure
13342 **/
13343int
13344lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13345 dma_addr_t pdma_phys_addr0,
13346 dma_addr_t pdma_phys_addr1,
13347 uint16_t xritag)
13348{
13349 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13350 LPFC_MBOXQ_t *mbox;
13351 int rc;
13352 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013353 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013354 union lpfc_sli4_cfg_shdr *shdr;
13355
13356 if (xritag == NO_XRI) {
13357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13358 "0364 Invalid param:\n");
13359 return -EINVAL;
13360 }
13361
13362 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13363 if (!mbox)
13364 return -ENOMEM;
13365
13366 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13367 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13368 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013369 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013370
13371 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13372 &mbox->u.mqe.un.post_sgl_pages;
13373 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13374 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13375
13376 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13377 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13378 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13379 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13380
13381 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13382 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13383 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13384 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13385 if (!phba->sli4_hba.intr_enable)
13386 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013387 else {
James Smarta183a152011-10-10 21:32:43 -040013388 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013389 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13390 }
James Smart4f774512009-05-22 14:52:35 -040013391 /* The IOCTL status is embedded in the mailbox subheader. */
13392 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13393 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13394 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13395 if (rc != MBX_TIMEOUT)
13396 mempool_free(mbox, phba->mbox_mem_pool);
13397 if (shdr_status || shdr_add_status || rc) {
13398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13399 "2511 POST_SGL mailbox failed with "
13400 "status x%x add_status x%x, mbx status x%x\n",
13401 shdr_status, shdr_add_status, rc);
13402 rc = -ENXIO;
13403 }
13404 return 0;
13405}
James Smart4f774512009-05-22 14:52:35 -040013406
13407/**
James Smart88a2cfb2011-07-22 18:36:33 -040013408 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013409 * @phba: pointer to lpfc hba data structure.
13410 *
13411 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013412 * HBA consistent with the SLI-4 interface spec. This routine
13413 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13414 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013415 *
James Smart88a2cfb2011-07-22 18:36:33 -040013416 * Returns
13417 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13418 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13419 **/
James Smart6d368e52011-05-24 11:44:12 -040013420uint16_t
13421lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13422{
13423 unsigned long xri;
13424
13425 /*
13426 * Fetch the next logical xri. Because this index is logical,
13427 * the driver starts at 0 each time.
13428 */
13429 spin_lock_irq(&phba->hbalock);
13430 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13431 phba->sli4_hba.max_cfg_param.max_xri, 0);
13432 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13433 spin_unlock_irq(&phba->hbalock);
13434 return NO_XRI;
13435 } else {
13436 set_bit(xri, phba->sli4_hba.xri_bmask);
13437 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013438 }
James Smart6d368e52011-05-24 11:44:12 -040013439 spin_unlock_irq(&phba->hbalock);
13440 return xri;
13441}
13442
13443/**
13444 * lpfc_sli4_free_xri - Release an xri for reuse.
13445 * @phba: pointer to lpfc hba data structure.
13446 *
13447 * This routine is invoked to release an xri to the pool of
13448 * available rpis maintained by the driver.
13449 **/
13450void
13451__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13452{
13453 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013454 phba->sli4_hba.max_cfg_param.xri_used--;
13455 }
13456}
13457
13458/**
13459 * lpfc_sli4_free_xri - Release an xri for reuse.
13460 * @phba: pointer to lpfc hba data structure.
13461 *
13462 * This routine is invoked to release an xri to the pool of
13463 * available rpis maintained by the driver.
13464 **/
13465void
13466lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13467{
13468 spin_lock_irq(&phba->hbalock);
13469 __lpfc_sli4_free_xri(phba, xri);
13470 spin_unlock_irq(&phba->hbalock);
13471}
13472
13473/**
James Smart4f774512009-05-22 14:52:35 -040013474 * lpfc_sli4_next_xritag - Get an xritag for the io
13475 * @phba: Pointer to HBA context object.
13476 *
13477 * This function gets an xritag for the iocb. If there is no unused xritag
13478 * it will return 0xffff.
13479 * The function returns the allocated xritag if successful, else returns zero.
13480 * Zero is not a valid xritag.
13481 * The caller is not required to hold any lock.
13482 **/
13483uint16_t
13484lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13485{
James Smart6d368e52011-05-24 11:44:12 -040013486 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013487
James Smart6d368e52011-05-24 11:44:12 -040013488 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013489 if (xri_index == NO_XRI)
13490 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13491 "2004 Failed to allocate XRI.last XRITAG is %d"
13492 " Max XRI is %d, Used XRI is %d\n",
13493 xri_index,
13494 phba->sli4_hba.max_cfg_param.max_xri,
13495 phba->sli4_hba.max_cfg_param.xri_used);
13496 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013497}
13498
13499/**
James Smart6d368e52011-05-24 11:44:12 -040013500 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013501 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013502 * @post_sgl_list: pointer to els sgl entry list.
13503 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013504 *
13505 * This routine is invoked to post a block of driver's sgl pages to the
13506 * HBA using non-embedded mailbox command. No Lock is held. This routine
13507 * is only called when the driver is loading and after all IO has been
13508 * stopped.
13509 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013510static int
13511lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13512 struct list_head *post_sgl_list,
13513 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013514{
James Smart8a9d2e82012-05-09 21:16:12 -040013515 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013516 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13517 struct sgl_page_pairs *sgl_pg_pairs;
13518 void *viraddr;
13519 LPFC_MBOXQ_t *mbox;
13520 uint32_t reqlen, alloclen, pg_pairs;
13521 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013522 uint16_t xritag_start = 0;
13523 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013524 uint32_t shdr_status, shdr_add_status;
13525 union lpfc_sli4_cfg_shdr *shdr;
13526
James Smart8a9d2e82012-05-09 21:16:12 -040013527 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013528 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013529 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013530 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13531 "2559 Block sgl registration required DMA "
13532 "size (%d) great than a page\n", reqlen);
13533 return -ENOMEM;
13534 }
13535 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013536 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013537 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013538
13539 /* Allocate DMA memory and set up the non-embedded mailbox command */
13540 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13541 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13542 LPFC_SLI4_MBX_NEMBED);
13543
13544 if (alloclen < reqlen) {
13545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13546 "0285 Allocated DMA memory size (%d) is "
13547 "less than the requested DMA memory "
13548 "size (%d)\n", alloclen, reqlen);
13549 lpfc_sli4_mbox_cmd_free(phba, mbox);
13550 return -ENOMEM;
13551 }
James Smart4f774512009-05-22 14:52:35 -040013552 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013553 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013554 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13555 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13556
James Smart8a9d2e82012-05-09 21:16:12 -040013557 pg_pairs = 0;
13558 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013559 /* Set up the sge entry */
13560 sgl_pg_pairs->sgl_pg0_addr_lo =
13561 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13562 sgl_pg_pairs->sgl_pg0_addr_hi =
13563 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13564 sgl_pg_pairs->sgl_pg1_addr_lo =
13565 cpu_to_le32(putPaddrLow(0));
13566 sgl_pg_pairs->sgl_pg1_addr_hi =
13567 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013568
James Smart4f774512009-05-22 14:52:35 -040013569 /* Keep the first xritag on the list */
13570 if (pg_pairs == 0)
13571 xritag_start = sglq_entry->sli4_xritag;
13572 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013573 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013574 }
James Smart6d368e52011-05-24 11:44:12 -040013575
13576 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013577 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013578 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013579 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013580 if (!phba->sli4_hba.intr_enable)
13581 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13582 else {
James Smarta183a152011-10-10 21:32:43 -040013583 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013584 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13585 }
13586 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13587 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13588 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13589 if (rc != MBX_TIMEOUT)
13590 lpfc_sli4_mbox_cmd_free(phba, mbox);
13591 if (shdr_status || shdr_add_status || rc) {
13592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13593 "2513 POST_SGL_BLOCK mailbox command failed "
13594 "status x%x add_status x%x mbx status x%x\n",
13595 shdr_status, shdr_add_status, rc);
13596 rc = -ENXIO;
13597 }
13598 return rc;
13599}
13600
13601/**
13602 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13603 * @phba: pointer to lpfc hba data structure.
13604 * @sblist: pointer to scsi buffer list.
13605 * @count: number of scsi buffers on the list.
13606 *
13607 * This routine is invoked to post a block of @count scsi sgl pages from a
13608 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13609 * No Lock is held.
13610 *
13611 **/
13612int
James Smart8a9d2e82012-05-09 21:16:12 -040013613lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13614 struct list_head *sblist,
13615 int count)
James Smart4f774512009-05-22 14:52:35 -040013616{
13617 struct lpfc_scsi_buf *psb;
13618 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13619 struct sgl_page_pairs *sgl_pg_pairs;
13620 void *viraddr;
13621 LPFC_MBOXQ_t *mbox;
13622 uint32_t reqlen, alloclen, pg_pairs;
13623 uint32_t mbox_tmo;
13624 uint16_t xritag_start = 0;
13625 int rc = 0;
13626 uint32_t shdr_status, shdr_add_status;
13627 dma_addr_t pdma_phys_bpl1;
13628 union lpfc_sli4_cfg_shdr *shdr;
13629
13630 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013631 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013632 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013633 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013634 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13635 "0217 Block sgl registration required DMA "
13636 "size (%d) great than a page\n", reqlen);
13637 return -ENOMEM;
13638 }
13639 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13640 if (!mbox) {
13641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13642 "0283 Failed to allocate mbox cmd memory\n");
13643 return -ENOMEM;
13644 }
13645
13646 /* Allocate DMA memory and set up the non-embedded mailbox command */
13647 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13648 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13649 LPFC_SLI4_MBX_NEMBED);
13650
13651 if (alloclen < reqlen) {
13652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13653 "2561 Allocated DMA memory size (%d) is "
13654 "less than the requested DMA memory "
13655 "size (%d)\n", alloclen, reqlen);
13656 lpfc_sli4_mbox_cmd_free(phba, mbox);
13657 return -ENOMEM;
13658 }
James Smart6d368e52011-05-24 11:44:12 -040013659
James Smart4f774512009-05-22 14:52:35 -040013660 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013661 viraddr = mbox->sge_array->addr[0];
13662
13663 /* Set up the SGL pages in the non-embedded DMA pages */
13664 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13665 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13666
13667 pg_pairs = 0;
13668 list_for_each_entry(psb, sblist, list) {
13669 /* Set up the sge entry */
13670 sgl_pg_pairs->sgl_pg0_addr_lo =
13671 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13672 sgl_pg_pairs->sgl_pg0_addr_hi =
13673 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13674 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13675 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13676 else
13677 pdma_phys_bpl1 = 0;
13678 sgl_pg_pairs->sgl_pg1_addr_lo =
13679 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13680 sgl_pg_pairs->sgl_pg1_addr_hi =
13681 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13682 /* Keep the first xritag on the list */
13683 if (pg_pairs == 0)
13684 xritag_start = psb->cur_iocbq.sli4_xritag;
13685 sgl_pg_pairs++;
13686 pg_pairs++;
13687 }
13688 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13689 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13690 /* Perform endian conversion if necessary */
13691 sgl->word0 = cpu_to_le32(sgl->word0);
13692
13693 if (!phba->sli4_hba.intr_enable)
13694 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13695 else {
James Smarta183a152011-10-10 21:32:43 -040013696 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013697 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13698 }
13699 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13700 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13702 if (rc != MBX_TIMEOUT)
13703 lpfc_sli4_mbox_cmd_free(phba, mbox);
13704 if (shdr_status || shdr_add_status || rc) {
13705 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13706 "2564 POST_SGL_BLOCK mailbox command failed "
13707 "status x%x add_status x%x mbx status x%x\n",
13708 shdr_status, shdr_add_status, rc);
13709 rc = -ENXIO;
13710 }
13711 return rc;
13712}
13713
13714/**
13715 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13716 * @phba: pointer to lpfc_hba struct that the frame was received on
13717 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13718 *
13719 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13720 * valid type of frame that the LPFC driver will handle. This function will
13721 * return a zero if the frame is a valid frame or a non zero value when the
13722 * frame does not pass the check.
13723 **/
13724static int
13725lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13726{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013727 /* make rctl_names static to save stack space */
13728 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013729 char *type_names[] = FC_TYPE_NAMES_INIT;
13730 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013731 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013732
13733 switch (fc_hdr->fh_r_ctl) {
13734 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13735 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13736 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13737 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13738 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13739 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13740 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13741 case FC_RCTL_DD_CMD_STATUS: /* command status */
13742 case FC_RCTL_ELS_REQ: /* extended link services request */
13743 case FC_RCTL_ELS_REP: /* extended link services reply */
13744 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13745 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13746 case FC_RCTL_BA_NOP: /* basic link service NOP */
13747 case FC_RCTL_BA_ABTS: /* basic link service abort */
13748 case FC_RCTL_BA_RMC: /* remove connection */
13749 case FC_RCTL_BA_ACC: /* basic accept */
13750 case FC_RCTL_BA_RJT: /* basic reject */
13751 case FC_RCTL_BA_PRMT:
13752 case FC_RCTL_ACK_1: /* acknowledge_1 */
13753 case FC_RCTL_ACK_0: /* acknowledge_0 */
13754 case FC_RCTL_P_RJT: /* port reject */
13755 case FC_RCTL_F_RJT: /* fabric reject */
13756 case FC_RCTL_P_BSY: /* port busy */
13757 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13758 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13759 case FC_RCTL_LCR: /* link credit reset */
13760 case FC_RCTL_END: /* end */
13761 break;
13762 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13763 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13764 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13765 return lpfc_fc_frame_check(phba, fc_hdr);
13766 default:
13767 goto drop;
13768 }
13769 switch (fc_hdr->fh_type) {
13770 case FC_TYPE_BLS:
13771 case FC_TYPE_ELS:
13772 case FC_TYPE_FCP:
13773 case FC_TYPE_CT:
13774 break;
13775 case FC_TYPE_IP:
13776 case FC_TYPE_ILS:
13777 default:
13778 goto drop;
13779 }
James Smart546fc852011-03-11 16:06:29 -050013780
James Smart4f774512009-05-22 14:52:35 -040013781 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013782 "2538 Received frame rctl:%s type:%s "
13783 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013784 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013785 type_names[fc_hdr->fh_type],
13786 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13787 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13788 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013789 return 0;
13790drop:
13791 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13792 "2539 Dropped frame rctl:%s type:%s\n",
13793 rctl_names[fc_hdr->fh_r_ctl],
13794 type_names[fc_hdr->fh_type]);
13795 return 1;
13796}
13797
13798/**
13799 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13800 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13801 *
13802 * This function processes the FC header to retrieve the VFI from the VF
13803 * header, if one exists. This function will return the VFI if one exists
13804 * or 0 if no VSAN Header exists.
13805 **/
13806static uint32_t
13807lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13808{
13809 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13810
13811 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13812 return 0;
13813 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13814}
13815
13816/**
13817 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13818 * @phba: Pointer to the HBA structure to search for the vport on
13819 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13820 * @fcfi: The FC Fabric ID that the frame came from
13821 *
13822 * This function searches the @phba for a vport that matches the content of the
13823 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13824 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13825 * returns the matching vport pointer or NULL if unable to match frame to a
13826 * vport.
13827 **/
13828static struct lpfc_vport *
13829lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13830 uint16_t fcfi)
13831{
13832 struct lpfc_vport **vports;
13833 struct lpfc_vport *vport = NULL;
13834 int i;
13835 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13836 fc_hdr->fh_d_id[1] << 8 |
13837 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013838
James Smartbf086112011-08-21 21:48:13 -040013839 if (did == Fabric_DID)
13840 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013841 if ((phba->pport->fc_flag & FC_PT2PT) &&
13842 !(phba->link_state == LPFC_HBA_READY))
13843 return phba->pport;
13844
James Smart4f774512009-05-22 14:52:35 -040013845 vports = lpfc_create_vport_work_array(phba);
13846 if (vports != NULL)
13847 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13848 if (phba->fcf.fcfi == fcfi &&
13849 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13850 vports[i]->fc_myDID == did) {
13851 vport = vports[i];
13852 break;
13853 }
13854 }
13855 lpfc_destroy_vport_work_array(phba, vports);
13856 return vport;
13857}
13858
13859/**
James Smart45ed1192009-10-02 15:17:02 -040013860 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13861 * @vport: The vport to work on.
13862 *
13863 * This function updates the receive sequence time stamp for this vport. The
13864 * receive sequence time stamp indicates the time that the last frame of the
13865 * the sequence that has been idle for the longest amount of time was received.
13866 * the driver uses this time stamp to indicate if any received sequences have
13867 * timed out.
13868 **/
13869void
13870lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13871{
13872 struct lpfc_dmabuf *h_buf;
13873 struct hbq_dmabuf *dmabuf = NULL;
13874
13875 /* get the oldest sequence on the rcv list */
13876 h_buf = list_get_first(&vport->rcv_buffer_list,
13877 struct lpfc_dmabuf, list);
13878 if (!h_buf)
13879 return;
13880 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13881 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13882}
13883
13884/**
13885 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13886 * @vport: The vport that the received sequences were sent to.
13887 *
13888 * This function cleans up all outstanding received sequences. This is called
13889 * by the driver when a link event or user action invalidates all the received
13890 * sequences.
13891 **/
13892void
13893lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13894{
13895 struct lpfc_dmabuf *h_buf, *hnext;
13896 struct lpfc_dmabuf *d_buf, *dnext;
13897 struct hbq_dmabuf *dmabuf = NULL;
13898
13899 /* start with the oldest sequence on the rcv list */
13900 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13901 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13902 list_del_init(&dmabuf->hbuf.list);
13903 list_for_each_entry_safe(d_buf, dnext,
13904 &dmabuf->dbuf.list, list) {
13905 list_del_init(&d_buf->list);
13906 lpfc_in_buf_free(vport->phba, d_buf);
13907 }
13908 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13909 }
13910}
13911
13912/**
13913 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13914 * @vport: The vport that the received sequences were sent to.
13915 *
13916 * This function determines whether any received sequences have timed out by
13917 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13918 * indicates that there is at least one timed out sequence this routine will
13919 * go through the received sequences one at a time from most inactive to most
13920 * active to determine which ones need to be cleaned up. Once it has determined
13921 * that a sequence needs to be cleaned up it will simply free up the resources
13922 * without sending an abort.
13923 **/
13924void
13925lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13926{
13927 struct lpfc_dmabuf *h_buf, *hnext;
13928 struct lpfc_dmabuf *d_buf, *dnext;
13929 struct hbq_dmabuf *dmabuf = NULL;
13930 unsigned long timeout;
13931 int abort_count = 0;
13932
13933 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13934 vport->rcv_buffer_time_stamp);
13935 if (list_empty(&vport->rcv_buffer_list) ||
13936 time_before(jiffies, timeout))
13937 return;
13938 /* start with the oldest sequence on the rcv list */
13939 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13940 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13941 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13942 dmabuf->time_stamp);
13943 if (time_before(jiffies, timeout))
13944 break;
13945 abort_count++;
13946 list_del_init(&dmabuf->hbuf.list);
13947 list_for_each_entry_safe(d_buf, dnext,
13948 &dmabuf->dbuf.list, list) {
13949 list_del_init(&d_buf->list);
13950 lpfc_in_buf_free(vport->phba, d_buf);
13951 }
13952 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13953 }
13954 if (abort_count)
13955 lpfc_update_rcv_time_stamp(vport);
13956}
13957
13958/**
James Smart4f774512009-05-22 14:52:35 -040013959 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13960 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13961 *
13962 * This function searches through the existing incomplete sequences that have
13963 * been sent to this @vport. If the frame matches one of the incomplete
13964 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13965 * make up that sequence. If no sequence is found that matches this frame then
13966 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13967 * This function returns a pointer to the first dmabuf in the sequence list that
13968 * the frame was linked to.
13969 **/
13970static struct hbq_dmabuf *
13971lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13972{
13973 struct fc_frame_header *new_hdr;
13974 struct fc_frame_header *temp_hdr;
13975 struct lpfc_dmabuf *d_buf;
13976 struct lpfc_dmabuf *h_buf;
13977 struct hbq_dmabuf *seq_dmabuf = NULL;
13978 struct hbq_dmabuf *temp_dmabuf = NULL;
13979
James Smart4d9ab992009-10-02 15:16:39 -040013980 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013981 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013982 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13983 /* Use the hdr_buf to find the sequence that this frame belongs to */
13984 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13985 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13986 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13987 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13988 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13989 continue;
13990 /* found a pending sequence that matches this frame */
13991 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13992 break;
13993 }
13994 if (!seq_dmabuf) {
13995 /*
13996 * This indicates first frame received for this sequence.
13997 * Queue the buffer on the vport's rcv_buffer_list.
13998 */
13999 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040014000 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014001 return dmabuf;
14002 }
14003 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050014004 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14005 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040014006 list_del_init(&seq_dmabuf->hbuf.list);
14007 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14008 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014009 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014010 return dmabuf;
14011 }
James Smart45ed1192009-10-02 15:17:02 -040014012 /* move this sequence to the tail to indicate a young sequence */
14013 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14014 seq_dmabuf->time_stamp = jiffies;
14015 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014016 if (list_empty(&seq_dmabuf->dbuf.list)) {
14017 temp_hdr = dmabuf->hbuf.virt;
14018 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14019 return seq_dmabuf;
14020 }
James Smart4f774512009-05-22 14:52:35 -040014021 /* find the correct place in the sequence to insert this frame */
14022 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14023 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14024 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14025 /*
14026 * If the frame's sequence count is greater than the frame on
14027 * the list then insert the frame right after this frame
14028 */
James Smarteeead812009-12-21 17:01:23 -050014029 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14030 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014031 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14032 return seq_dmabuf;
14033 }
14034 }
14035 return NULL;
14036}
14037
14038/**
James Smart6669f9b2009-10-02 15:16:45 -040014039 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14040 * @vport: pointer to a vitural port
14041 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14042 *
14043 * This function tries to abort from the partially assembed sequence, described
14044 * by the information from basic abbort @dmabuf. It checks to see whether such
14045 * partially assembled sequence held by the driver. If so, it shall free up all
14046 * the frames from the partially assembled sequence.
14047 *
14048 * Return
14049 * true -- if there is matching partially assembled sequence present and all
14050 * the frames freed with the sequence;
14051 * false -- if there is no matching partially assembled sequence present so
14052 * nothing got aborted in the lower layer driver
14053 **/
14054static bool
14055lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14056 struct hbq_dmabuf *dmabuf)
14057{
14058 struct fc_frame_header *new_hdr;
14059 struct fc_frame_header *temp_hdr;
14060 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14061 struct hbq_dmabuf *seq_dmabuf = NULL;
14062
14063 /* Use the hdr_buf to find the sequence that matches this frame */
14064 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14065 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14066 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14067 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14068 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14069 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14070 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14071 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14072 continue;
14073 /* found a pending sequence that matches this frame */
14074 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14075 break;
14076 }
14077
14078 /* Free up all the frames from the partially assembled sequence */
14079 if (seq_dmabuf) {
14080 list_for_each_entry_safe(d_buf, n_buf,
14081 &seq_dmabuf->dbuf.list, list) {
14082 list_del_init(&d_buf->list);
14083 lpfc_in_buf_free(vport->phba, d_buf);
14084 }
14085 return true;
14086 }
14087 return false;
14088}
14089
14090/**
James Smart546fc852011-03-11 16:06:29 -050014091 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014092 * @phba: Pointer to HBA context object.
14093 * @cmd_iocbq: pointer to the command iocbq structure.
14094 * @rsp_iocbq: pointer to the response iocbq structure.
14095 *
James Smart546fc852011-03-11 16:06:29 -050014096 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014097 * event. It properly releases the memory allocated to the sequence abort
14098 * accept iocb.
14099 **/
14100static void
James Smart546fc852011-03-11 16:06:29 -050014101lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014102 struct lpfc_iocbq *cmd_iocbq,
14103 struct lpfc_iocbq *rsp_iocbq)
14104{
14105 if (cmd_iocbq)
14106 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014107
14108 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14109 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14110 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14111 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14112 rsp_iocbq->iocb.ulpStatus,
14113 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014114}
14115
14116/**
James Smart6d368e52011-05-24 11:44:12 -040014117 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14118 * @phba: Pointer to HBA context object.
14119 * @xri: xri id in transaction.
14120 *
14121 * This function validates the xri maps to the known range of XRIs allocated an
14122 * used by the driver.
14123 **/
James Smart7851fe22011-07-22 18:36:52 -040014124uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014125lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14126 uint16_t xri)
14127{
14128 int i;
14129
14130 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14131 if (xri == phba->sli4_hba.xri_ids[i])
14132 return i;
14133 }
14134 return NO_XRI;
14135}
14136
James Smart6d368e52011-05-24 11:44:12 -040014137/**
James Smart546fc852011-03-11 16:06:29 -050014138 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014139 * @phba: Pointer to HBA context object.
14140 * @fc_hdr: pointer to a FC frame header.
14141 *
James Smart546fc852011-03-11 16:06:29 -050014142 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014143 * event after aborting the sequence handling.
14144 **/
14145static void
James Smart546fc852011-03-11 16:06:29 -050014146lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014147 struct fc_frame_header *fc_hdr)
14148{
14149 struct lpfc_iocbq *ctiocb = NULL;
14150 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014151 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014152 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014153 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014154 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014155
14156 if (!lpfc_is_link_up(phba))
14157 return;
14158
14159 sid = sli4_sid_from_fc_hdr(fc_hdr);
14160 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014161 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014162
14163 ndlp = lpfc_findnode_did(phba->pport, sid);
14164 if (!ndlp) {
14165 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14166 "1268 Find ndlp returned NULL for oxid:x%x "
14167 "SID:x%x\n", oxid, sid);
14168 return;
14169 }
14170
James Smart546fc852011-03-11 16:06:29 -050014171 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014172 ctiocb = lpfc_sli_get_iocbq(phba);
14173 if (!ctiocb)
14174 return;
14175
James Smart5ffc2662009-11-18 15:39:44 -050014176 /* Extract the F_CTL field from FC_HDR */
14177 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14178
James Smart6669f9b2009-10-02 15:16:45 -040014179 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014180 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014181 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014182 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14183 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14184 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14185
14186 /* Fill in the rest of iocb fields */
14187 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14188 icmd->ulpBdeCount = 0;
14189 icmd->ulpLe = 1;
14190 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014191 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014192 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014193
James Smart6669f9b2009-10-02 15:16:45 -040014194 ctiocb->iocb_cmpl = NULL;
14195 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014196 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014197 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014198 ctiocb->sli4_xritag = NO_XRI;
14199
James Smartee0f4fe2012-05-09 21:19:14 -040014200 if (fctl & FC_FC_EX_CTX)
14201 /* Exchange responder sent the abort so we
14202 * own the oxid.
14203 */
14204 xri = oxid;
14205 else
14206 xri = rxid;
14207 lxri = lpfc_sli4_xri_inrange(phba, xri);
14208 if (lxri != NO_XRI)
14209 lpfc_set_rrq_active(phba, ndlp, lxri,
14210 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014211 /* If the oxid maps to the FCP XRI range or if it is out of range,
14212 * send a BLS_RJT. The driver no longer has that exchange.
14213 * Override the IOCB for a BA_RJT.
14214 */
James Smartee0f4fe2012-05-09 21:19:14 -040014215 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014216 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014217 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014218 phba->sli4_hba.max_cfg_param.xri_base)) {
14219 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14220 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14221 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14222 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14223 }
James Smart6669f9b2009-10-02 15:16:45 -040014224
James Smart5ffc2662009-11-18 15:39:44 -050014225 if (fctl & FC_FC_EX_CTX) {
14226 /* ABTS sent by responder to CT exchange, construction
14227 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14228 * field and RX_ID from ABTS for RX_ID field.
14229 */
James Smart546fc852011-03-11 16:06:29 -050014230 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014231 } else {
14232 /* ABTS sent by initiator to CT exchange, construction
14233 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014234 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014235 */
James Smart546fc852011-03-11 16:06:29 -050014236 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014237 }
James Smartf09c3ac2012-03-01 22:33:29 -050014238 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014239 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014240
James Smart546fc852011-03-11 16:06:29 -050014241 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014242 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014243 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14244 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14245
14246 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14247 if (rc == IOCB_ERROR) {
14248 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14249 "2925 Failed to issue CT ABTS RSP x%x on "
14250 "xri x%x, Data x%x\n",
14251 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14252 phba->link_state);
14253 lpfc_sli_release_iocbq(phba, ctiocb);
14254 }
James Smart6669f9b2009-10-02 15:16:45 -040014255}
14256
14257/**
14258 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14259 * @vport: Pointer to the vport on which this sequence was received
14260 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14261 *
14262 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14263 * receive sequence is only partially assembed by the driver, it shall abort
14264 * the partially assembled frames for the sequence. Otherwise, if the
14265 * unsolicited receive sequence has been completely assembled and passed to
14266 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14267 * unsolicited sequence has been aborted. After that, it will issue a basic
14268 * accept to accept the abort.
14269 **/
14270void
14271lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14272 struct hbq_dmabuf *dmabuf)
14273{
14274 struct lpfc_hba *phba = vport->phba;
14275 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014276 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014277 bool abts_par;
14278
James Smart6669f9b2009-10-02 15:16:45 -040014279 /* Make a copy of fc_hdr before the dmabuf being released */
14280 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014281 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014282
James Smart5ffc2662009-11-18 15:39:44 -050014283 if (fctl & FC_FC_EX_CTX) {
14284 /*
14285 * ABTS sent by responder to exchange, just free the buffer
14286 */
James Smart6669f9b2009-10-02 15:16:45 -040014287 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014288 } else {
14289 /*
14290 * ABTS sent by initiator to exchange, need to do cleanup
14291 */
14292 /* Try to abort partially assembled seq */
14293 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14294
14295 /* Send abort to ULP if partially seq abort failed */
14296 if (abts_par == false)
14297 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14298 else
14299 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14300 }
James Smart6669f9b2009-10-02 15:16:45 -040014301 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014302 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014303}
14304
14305/**
James Smart4f774512009-05-22 14:52:35 -040014306 * lpfc_seq_complete - Indicates if a sequence is complete
14307 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14308 *
14309 * This function checks the sequence, starting with the frame described by
14310 * @dmabuf, to see if all the frames associated with this sequence are present.
14311 * the frames associated with this sequence are linked to the @dmabuf using the
14312 * dbuf list. This function looks for two major things. 1) That the first frame
14313 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14314 * set. 3) That there are no holes in the sequence count. The function will
14315 * return 1 when the sequence is complete, otherwise it will return 0.
14316 **/
14317static int
14318lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14319{
14320 struct fc_frame_header *hdr;
14321 struct lpfc_dmabuf *d_buf;
14322 struct hbq_dmabuf *seq_dmabuf;
14323 uint32_t fctl;
14324 int seq_count = 0;
14325
14326 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14327 /* make sure first fame of sequence has a sequence count of zero */
14328 if (hdr->fh_seq_cnt != seq_count)
14329 return 0;
14330 fctl = (hdr->fh_f_ctl[0] << 16 |
14331 hdr->fh_f_ctl[1] << 8 |
14332 hdr->fh_f_ctl[2]);
14333 /* If last frame of sequence we can return success. */
14334 if (fctl & FC_FC_END_SEQ)
14335 return 1;
14336 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14337 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14338 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14339 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014340 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014341 return 0;
14342 fctl = (hdr->fh_f_ctl[0] << 16 |
14343 hdr->fh_f_ctl[1] << 8 |
14344 hdr->fh_f_ctl[2]);
14345 /* If last frame of sequence we can return success. */
14346 if (fctl & FC_FC_END_SEQ)
14347 return 1;
14348 }
14349 return 0;
14350}
14351
14352/**
14353 * lpfc_prep_seq - Prep sequence for ULP processing
14354 * @vport: Pointer to the vport on which this sequence was received
14355 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14356 *
14357 * This function takes a sequence, described by a list of frames, and creates
14358 * a list of iocbq structures to describe the sequence. This iocbq list will be
14359 * used to issue to the generic unsolicited sequence handler. This routine
14360 * returns a pointer to the first iocbq in the list. If the function is unable
14361 * to allocate an iocbq then it throw out the received frames that were not
14362 * able to be described and return a pointer to the first iocbq. If unable to
14363 * allocate any iocbqs (including the first) this function will return NULL.
14364 **/
14365static struct lpfc_iocbq *
14366lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14367{
James Smart7851fe22011-07-22 18:36:52 -040014368 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014369 struct lpfc_dmabuf *d_buf, *n_buf;
14370 struct lpfc_iocbq *first_iocbq, *iocbq;
14371 struct fc_frame_header *fc_hdr;
14372 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014373 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014374 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014375
14376 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14377 /* remove from receive buffer list */
14378 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014379 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014380 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014381 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014382 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014383 /* Get an iocbq struct to fill in. */
14384 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14385 if (first_iocbq) {
14386 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014387 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014388 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014389
14390 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14391 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14392 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14393 first_iocbq->iocb.un.rcvels.parmRo =
14394 sli4_did_from_fc_hdr(fc_hdr);
14395 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14396 } else
14397 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014398 first_iocbq->iocb.ulpContext = NO_XRI;
14399 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14400 be16_to_cpu(fc_hdr->fh_ox_id);
14401 /* iocbq is prepped for internal consumption. Physical vpi. */
14402 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14403 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014404 /* put the first buffer into the first IOCBq */
14405 first_iocbq->context2 = &seq_dmabuf->dbuf;
14406 first_iocbq->context3 = NULL;
14407 first_iocbq->iocb.ulpBdeCount = 1;
14408 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14409 LPFC_DATA_BUF_SIZE;
14410 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014411 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014412 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014413 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014414 }
14415 iocbq = first_iocbq;
14416 /*
14417 * Each IOCBq can have two Buffers assigned, so go through the list
14418 * of buffers for this sequence and save two buffers in each IOCBq
14419 */
14420 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14421 if (!iocbq) {
14422 lpfc_in_buf_free(vport->phba, d_buf);
14423 continue;
14424 }
14425 if (!iocbq->context3) {
14426 iocbq->context3 = d_buf;
14427 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014428 pbde = (struct ulp_bde64 *)
14429 &iocbq->iocb.unsli3.sli3Words[4];
14430 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014431
14432 /* We need to get the size out of the right CQE */
14433 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14434 len = bf_get(lpfc_rcqe_length,
14435 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14436 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14437 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014438 } else {
14439 iocbq = lpfc_sli_get_iocbq(vport->phba);
14440 if (!iocbq) {
14441 if (first_iocbq) {
14442 first_iocbq->iocb.ulpStatus =
14443 IOSTAT_FCP_RSP_ERROR;
14444 first_iocbq->iocb.un.ulpWord[4] =
14445 IOERR_NO_RESOURCES;
14446 }
14447 lpfc_in_buf_free(vport->phba, d_buf);
14448 continue;
14449 }
14450 iocbq->context2 = d_buf;
14451 iocbq->context3 = NULL;
14452 iocbq->iocb.ulpBdeCount = 1;
14453 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14454 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014455
14456 /* We need to get the size out of the right CQE */
14457 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14458 len = bf_get(lpfc_rcqe_length,
14459 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14460 tot_len += len;
14461 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14462
James Smart4f774512009-05-22 14:52:35 -040014463 iocbq->iocb.un.rcvels.remoteID = sid;
14464 list_add_tail(&iocbq->list, &first_iocbq->list);
14465 }
14466 }
14467 return first_iocbq;
14468}
14469
James Smart6669f9b2009-10-02 15:16:45 -040014470static void
14471lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14472 struct hbq_dmabuf *seq_dmabuf)
14473{
14474 struct fc_frame_header *fc_hdr;
14475 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14476 struct lpfc_hba *phba = vport->phba;
14477
14478 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14479 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14480 if (!iocbq) {
14481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14482 "2707 Ring %d handler: Failed to allocate "
14483 "iocb Rctl x%x Type x%x received\n",
14484 LPFC_ELS_RING,
14485 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14486 return;
14487 }
14488 if (!lpfc_complete_unsol_iocb(phba,
14489 &phba->sli.ring[LPFC_ELS_RING],
14490 iocbq, fc_hdr->fh_r_ctl,
14491 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014492 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014493 "2540 Ring %d handler: unexpected Rctl "
14494 "x%x Type x%x received\n",
14495 LPFC_ELS_RING,
14496 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14497
14498 /* Free iocb created in lpfc_prep_seq */
14499 list_for_each_entry_safe(curr_iocb, next_iocb,
14500 &iocbq->list, list) {
14501 list_del_init(&curr_iocb->list);
14502 lpfc_sli_release_iocbq(phba, curr_iocb);
14503 }
14504 lpfc_sli_release_iocbq(phba, iocbq);
14505}
14506
James Smart4f774512009-05-22 14:52:35 -040014507/**
14508 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14509 * @phba: Pointer to HBA context object.
14510 *
14511 * This function is called with no lock held. This function processes all
14512 * the received buffers and gives it to upper layers when a received buffer
14513 * indicates that it is the final frame in the sequence. The interrupt
14514 * service routine processes received buffers at interrupt contexts and adds
14515 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14516 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14517 * appropriate receive function when the final frame in a sequence is received.
14518 **/
James Smart4d9ab992009-10-02 15:16:39 -040014519void
14520lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14521 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014522{
James Smart4d9ab992009-10-02 15:16:39 -040014523 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014524 struct fc_frame_header *fc_hdr;
14525 struct lpfc_vport *vport;
14526 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014527 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014528
James Smart4f774512009-05-22 14:52:35 -040014529 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014530 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14531 /* check to see if this a valid type of frame */
14532 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14533 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14534 return;
14535 }
James Smart7851fe22011-07-22 18:36:52 -040014536 if ((bf_get(lpfc_cqe_code,
14537 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14538 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14539 &dmabuf->cq_event.cqe.rcqe_cmpl);
14540 else
14541 fcfi = bf_get(lpfc_rcqe_fcf_id,
14542 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014543
James Smart4d9ab992009-10-02 15:16:39 -040014544 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014545 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014546 /* throw out the frame */
14547 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14548 return;
14549 }
James Smart939723a2012-05-09 21:19:03 -040014550
14551 /* d_id this frame is directed to */
14552 did = sli4_did_from_fc_hdr(fc_hdr);
14553
14554 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14555 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14556 (did != Fabric_DID)) {
14557 /*
14558 * Throw out the frame if we are not pt2pt.
14559 * The pt2pt protocol allows for discovery frames
14560 * to be received without a registered VPI.
14561 */
14562 if (!(vport->fc_flag & FC_PT2PT) ||
14563 (phba->link_state == LPFC_HBA_READY)) {
14564 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14565 return;
14566 }
14567 }
14568
James Smart6669f9b2009-10-02 15:16:45 -040014569 /* Handle the basic abort sequence (BA_ABTS) event */
14570 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14571 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14572 return;
14573 }
14574
James Smart4d9ab992009-10-02 15:16:39 -040014575 /* Link this frame */
14576 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14577 if (!seq_dmabuf) {
14578 /* unable to add frame to vport - throw it out */
14579 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14580 return;
14581 }
14582 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014583 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014584 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014585
James Smart6669f9b2009-10-02 15:16:45 -040014586 /* Send the complete sequence to the upper layer protocol */
14587 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014588}
James Smart6fb120a2009-05-22 14:52:59 -040014589
14590/**
14591 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14592 * @phba: pointer to lpfc hba data structure.
14593 *
14594 * This routine is invoked to post rpi header templates to the
14595 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014596 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14597 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014598 *
14599 * This routine does not require any locks. It's usage is expected
14600 * to be driver load or reset recovery when the driver is
14601 * sequential.
14602 *
14603 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014604 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014605 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014606 * When this error occurs, the driver is not guaranteed
14607 * to have any rpi regions posted to the device and
14608 * must either attempt to repost the regions or take a
14609 * fatal error.
14610 **/
14611int
14612lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14613{
14614 struct lpfc_rpi_hdr *rpi_page;
14615 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014616 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014617
James Smart6d368e52011-05-24 11:44:12 -040014618 /* SLI4 ports that support extents do not require RPI headers. */
14619 if (!phba->sli4_hba.rpi_hdrs_in_use)
14620 goto exit;
14621 if (phba->sli4_hba.extents_in_use)
14622 return -EIO;
14623
James Smart6fb120a2009-05-22 14:52:59 -040014624 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014625 /*
14626 * Assign the rpi headers a physical rpi only if the driver
14627 * has not initialized those resources. A port reset only
14628 * needs the headers posted.
14629 */
14630 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14631 LPFC_RPI_RSRC_RDY)
14632 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14633
James Smart6fb120a2009-05-22 14:52:59 -040014634 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14635 if (rc != MBX_SUCCESS) {
14636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14637 "2008 Error %d posting all rpi "
14638 "headers\n", rc);
14639 rc = -EIO;
14640 break;
14641 }
14642 }
14643
James Smart6d368e52011-05-24 11:44:12 -040014644 exit:
14645 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14646 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014647 return rc;
14648}
14649
14650/**
14651 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14652 * @phba: pointer to lpfc hba data structure.
14653 * @rpi_page: pointer to the rpi memory region.
14654 *
14655 * This routine is invoked to post a single rpi header to the
14656 * HBA consistent with the SLI-4 interface spec. This memory region
14657 * maps up to 64 rpi context regions.
14658 *
14659 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014660 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014661 * -ENOMEM - No available memory
14662 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014663 **/
14664int
14665lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14666{
14667 LPFC_MBOXQ_t *mboxq;
14668 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14669 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014670 uint32_t shdr_status, shdr_add_status;
14671 union lpfc_sli4_cfg_shdr *shdr;
14672
James Smart6d368e52011-05-24 11:44:12 -040014673 /* SLI4 ports that support extents do not require RPI headers. */
14674 if (!phba->sli4_hba.rpi_hdrs_in_use)
14675 return rc;
14676 if (phba->sli4_hba.extents_in_use)
14677 return -EIO;
14678
James Smart6fb120a2009-05-22 14:52:59 -040014679 /* The port is notified of the header region via a mailbox command. */
14680 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14681 if (!mboxq) {
14682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14683 "2001 Unable to allocate memory for issuing "
14684 "SLI_CONFIG_SPECIAL mailbox command\n");
14685 return -ENOMEM;
14686 }
14687
14688 /* Post all rpi memory regions to the port. */
14689 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014690 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14691 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14692 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014693 sizeof(struct lpfc_sli4_cfg_mhdr),
14694 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014695
14696
14697 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014698 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14699 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014700 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14701 hdr_tmpl, rpi_page->page_count);
14702
James Smart6fb120a2009-05-22 14:52:59 -040014703 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14704 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014705 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014706 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14707 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14708 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14709 if (rc != MBX_TIMEOUT)
14710 mempool_free(mboxq, phba->mbox_mem_pool);
14711 if (shdr_status || shdr_add_status || rc) {
14712 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14713 "2514 POST_RPI_HDR mailbox failed with "
14714 "status x%x add_status x%x, mbx status x%x\n",
14715 shdr_status, shdr_add_status, rc);
14716 rc = -ENXIO;
14717 }
14718 return rc;
14719}
14720
14721/**
14722 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14723 * @phba: pointer to lpfc hba data structure.
14724 *
14725 * This routine is invoked to post rpi header templates to the
14726 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014727 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14728 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014729 *
14730 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014731 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014732 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14733 **/
14734int
14735lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14736{
James Smart6d368e52011-05-24 11:44:12 -040014737 unsigned long rpi;
14738 uint16_t max_rpi, rpi_limit;
14739 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014740 struct lpfc_rpi_hdr *rpi_hdr;
14741
14742 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014743 rpi_limit = phba->sli4_hba.next_rpi;
14744
14745 /*
James Smart6d368e52011-05-24 11:44:12 -040014746 * Fetch the next logical rpi. Because this index is logical,
14747 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014748 */
14749 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014750 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14751 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014752 rpi = LPFC_RPI_ALLOC_ERROR;
14753 else {
14754 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14755 phba->sli4_hba.max_cfg_param.rpi_used++;
14756 phba->sli4_hba.rpi_count++;
14757 }
14758
14759 /*
14760 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014761 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014762 */
14763 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14764 (phba->sli4_hba.rpi_count >= max_rpi)) {
14765 spin_unlock_irq(&phba->hbalock);
14766 return rpi;
14767 }
14768
14769 /*
James Smart6d368e52011-05-24 11:44:12 -040014770 * RPI header postings are not required for SLI4 ports capable of
14771 * extents.
14772 */
14773 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14774 spin_unlock_irq(&phba->hbalock);
14775 return rpi;
14776 }
14777
14778 /*
James Smart6fb120a2009-05-22 14:52:59 -040014779 * If the driver is running low on rpi resources, allocate another
14780 * page now. Note that the next_rpi value is used because
14781 * it represents how many are actually in use whereas max_rpi notes
14782 * how many are supported max by the device.
14783 */
James Smart6d368e52011-05-24 11:44:12 -040014784 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014785 spin_unlock_irq(&phba->hbalock);
14786 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14787 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14788 if (!rpi_hdr) {
14789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14790 "2002 Error Could not grow rpi "
14791 "count\n");
14792 } else {
James Smart6d368e52011-05-24 11:44:12 -040014793 lrpi = rpi_hdr->start_rpi;
14794 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014795 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14796 }
14797 }
14798
14799 return rpi;
14800}
14801
14802/**
14803 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14804 * @phba: pointer to lpfc hba data structure.
14805 *
14806 * This routine is invoked to release an rpi to the pool of
14807 * available rpis maintained by the driver.
14808 **/
14809void
James Smartd7c47992010-06-08 18:31:54 -040014810__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14811{
14812 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14813 phba->sli4_hba.rpi_count--;
14814 phba->sli4_hba.max_cfg_param.rpi_used--;
14815 }
14816}
14817
14818/**
14819 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14820 * @phba: pointer to lpfc hba data structure.
14821 *
14822 * This routine is invoked to release an rpi to the pool of
14823 * available rpis maintained by the driver.
14824 **/
14825void
James Smart6fb120a2009-05-22 14:52:59 -040014826lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14827{
14828 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014829 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014830 spin_unlock_irq(&phba->hbalock);
14831}
14832
14833/**
14834 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14835 * @phba: pointer to lpfc hba data structure.
14836 *
14837 * This routine is invoked to remove the memory region that
14838 * provided rpi via a bitmask.
14839 **/
14840void
14841lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14842{
14843 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014844 kfree(phba->sli4_hba.rpi_ids);
14845 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014846}
14847
14848/**
14849 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14850 * @phba: pointer to lpfc hba data structure.
14851 *
14852 * This routine is invoked to remove the memory region that
14853 * provided rpi via a bitmask.
14854 **/
14855int
James Smart6b5151f2012-01-18 16:24:06 -050014856lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14857 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014858{
14859 LPFC_MBOXQ_t *mboxq;
14860 struct lpfc_hba *phba = ndlp->phba;
14861 int rc;
14862
14863 /* The port is notified of the header region via a mailbox command. */
14864 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14865 if (!mboxq)
14866 return -ENOMEM;
14867
14868 /* Post all rpi memory regions to the port. */
14869 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014870 if (cmpl) {
14871 mboxq->mbox_cmpl = cmpl;
14872 mboxq->context1 = arg;
14873 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014874 } else
14875 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014876 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014877 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14878 if (rc == MBX_NOT_FINISHED) {
14879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14880 "2010 Resume RPI Mailbox failed "
14881 "status %d, mbxStatus x%x\n", rc,
14882 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14883 mempool_free(mboxq, phba->mbox_mem_pool);
14884 return -EIO;
14885 }
14886 return 0;
14887}
14888
14889/**
14890 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014891 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014892 *
James Smart76a95d72010-11-20 23:11:48 -050014893 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014894 *
14895 * Returns:
14896 * 0 success
14897 * -Evalue otherwise
14898 **/
14899int
James Smart76a95d72010-11-20 23:11:48 -050014900lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014901{
14902 LPFC_MBOXQ_t *mboxq;
14903 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014904 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014905 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014906 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014907 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14908 if (!mboxq)
14909 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014910 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014911 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014912 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014913 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014914 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014915 "2022 INIT VPI Mailbox failed "
14916 "status %d, mbxStatus x%x\n", rc,
14917 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014918 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014919 }
James Smart6a9c52c2009-10-02 15:16:51 -040014920 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014921 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014922
14923 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014924}
14925
14926/**
14927 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14928 * @phba: pointer to lpfc hba data structure.
14929 * @mboxq: Pointer to mailbox object.
14930 *
14931 * This routine is invoked to manually add a single FCF record. The caller
14932 * must pass a completely initialized FCF_Record. This routine takes
14933 * care of the nonembedded mailbox operations.
14934 **/
14935static void
14936lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14937{
14938 void *virt_addr;
14939 union lpfc_sli4_cfg_shdr *shdr;
14940 uint32_t shdr_status, shdr_add_status;
14941
14942 virt_addr = mboxq->sge_array->addr[0];
14943 /* The IOCTL status is embedded in the mailbox subheader. */
14944 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14945 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14946 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14947
14948 if ((shdr_status || shdr_add_status) &&
14949 (shdr_status != STATUS_FCF_IN_USE))
14950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14951 "2558 ADD_FCF_RECORD mailbox failed with "
14952 "status x%x add_status x%x\n",
14953 shdr_status, shdr_add_status);
14954
14955 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14956}
14957
14958/**
14959 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14960 * @phba: pointer to lpfc hba data structure.
14961 * @fcf_record: pointer to the initialized fcf record to add.
14962 *
14963 * This routine is invoked to manually add a single FCF record. The caller
14964 * must pass a completely initialized FCF_Record. This routine takes
14965 * care of the nonembedded mailbox operations.
14966 **/
14967int
14968lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14969{
14970 int rc = 0;
14971 LPFC_MBOXQ_t *mboxq;
14972 uint8_t *bytep;
14973 void *virt_addr;
14974 dma_addr_t phys_addr;
14975 struct lpfc_mbx_sge sge;
14976 uint32_t alloc_len, req_len;
14977 uint32_t fcfindex;
14978
14979 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14980 if (!mboxq) {
14981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14982 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14983 return -ENOMEM;
14984 }
14985
14986 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14987 sizeof(uint32_t);
14988
14989 /* Allocate DMA memory and set up the non-embedded mailbox command */
14990 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14991 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14992 req_len, LPFC_SLI4_MBX_NEMBED);
14993 if (alloc_len < req_len) {
14994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14995 "2523 Allocated DMA memory size (x%x) is "
14996 "less than the requested DMA memory "
14997 "size (x%x)\n", alloc_len, req_len);
14998 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14999 return -ENOMEM;
15000 }
15001
15002 /*
15003 * Get the first SGE entry from the non-embedded DMA memory. This
15004 * routine only uses a single SGE.
15005 */
15006 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15007 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040015008 virt_addr = mboxq->sge_array->addr[0];
15009 /*
15010 * Configure the FCF record for FCFI 0. This is the driver's
15011 * hardcoded default and gets used in nonFIP mode.
15012 */
15013 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15014 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15015 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15016
15017 /*
15018 * Copy the fcf_index and the FCF Record Data. The data starts after
15019 * the FCoE header plus word10. The data copy needs to be endian
15020 * correct.
15021 */
15022 bytep += sizeof(uint32_t);
15023 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15024 mboxq->vport = phba->pport;
15025 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15026 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15027 if (rc == MBX_NOT_FINISHED) {
15028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15029 "2515 ADD_FCF_RECORD mailbox failed with "
15030 "status 0x%x\n", rc);
15031 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15032 rc = -EIO;
15033 } else
15034 rc = 0;
15035
15036 return rc;
15037}
15038
15039/**
15040 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15041 * @phba: pointer to lpfc hba data structure.
15042 * @fcf_record: pointer to the fcf record to write the default data.
15043 * @fcf_index: FCF table entry index.
15044 *
15045 * This routine is invoked to build the driver's default FCF record. The
15046 * values used are hardcoded. This routine handles memory initialization.
15047 *
15048 **/
15049void
15050lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15051 struct fcf_record *fcf_record,
15052 uint16_t fcf_index)
15053{
15054 memset(fcf_record, 0, sizeof(struct fcf_record));
15055 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15056 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15057 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15058 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15059 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15060 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15061 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15062 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15063 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15064 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15065 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15066 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15067 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015068 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015069 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15070 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15071 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15072 /* Set the VLAN bit map */
15073 if (phba->valid_vlan) {
15074 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15075 = 1 << (phba->vlan_id % 8);
15076 }
15077}
15078
15079/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015080 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015081 * @phba: pointer to lpfc hba data structure.
15082 * @fcf_index: FCF table entry offset.
15083 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015084 * This routine is invoked to scan the entire FCF table by reading FCF
15085 * record and processing it one at a time starting from the @fcf_index
15086 * for initial FCF discovery or fast FCF failover rediscovery.
15087 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015088 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015089 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015090 **/
15091int
James Smart0c9ab6f2010-02-26 14:15:57 -050015092lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015093{
15094 int rc = 0, error;
15095 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015096
James Smart32b97932009-07-19 10:01:21 -040015097 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015098 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015099 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15100 if (!mboxq) {
15101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15102 "2000 Failed to allocate mbox for "
15103 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015104 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015105 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015106 }
James Smartecfd03c2010-02-12 14:41:27 -050015107 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015108 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015109 if (rc) {
15110 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015111 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015112 }
James Smartecfd03c2010-02-12 14:41:27 -050015113 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015114 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015115 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015116
15117 spin_lock_irq(&phba->hbalock);
15118 phba->hba_flag |= FCF_TS_INPROG;
15119 spin_unlock_irq(&phba->hbalock);
15120
James Smart6fb120a2009-05-22 14:52:59 -040015121 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015122 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015123 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015124 else {
James Smart38b92ef2010-08-04 16:11:39 -040015125 /* Reset eligible FCF count for new scan */
15126 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015127 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015128 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015129 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015130fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015131 if (error) {
15132 if (mboxq)
15133 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015134 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015135 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015136 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015137 spin_unlock_irq(&phba->hbalock);
15138 }
James Smart6fb120a2009-05-22 14:52:59 -040015139 return error;
15140}
James Smarta0c87cb2009-07-19 10:01:10 -040015141
15142/**
James Smarta93ff372010-10-22 11:06:08 -040015143 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015144 * @phba: pointer to lpfc hba data structure.
15145 * @fcf_index: FCF table entry offset.
15146 *
15147 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015148 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015149 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015150 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015151 * otherwise.
15152 **/
15153int
15154lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15155{
15156 int rc = 0, error;
15157 LPFC_MBOXQ_t *mboxq;
15158
15159 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15160 if (!mboxq) {
15161 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15162 "2763 Failed to allocate mbox for "
15163 "READ_FCF cmd\n");
15164 error = -ENOMEM;
15165 goto fail_fcf_read;
15166 }
15167 /* Construct the read FCF record mailbox command */
15168 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15169 if (rc) {
15170 error = -EINVAL;
15171 goto fail_fcf_read;
15172 }
15173 /* Issue the mailbox command asynchronously */
15174 mboxq->vport = phba->pport;
15175 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15176 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15177 if (rc == MBX_NOT_FINISHED)
15178 error = -EIO;
15179 else
15180 error = 0;
15181
15182fail_fcf_read:
15183 if (error && mboxq)
15184 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15185 return error;
15186}
15187
15188/**
15189 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15190 * @phba: pointer to lpfc hba data structure.
15191 * @fcf_index: FCF table entry offset.
15192 *
15193 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015194 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015195 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015196 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015197 * otherwise.
15198 **/
15199int
15200lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15201{
15202 int rc = 0, error;
15203 LPFC_MBOXQ_t *mboxq;
15204
15205 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15206 if (!mboxq) {
15207 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15208 "2758 Failed to allocate mbox for "
15209 "READ_FCF cmd\n");
15210 error = -ENOMEM;
15211 goto fail_fcf_read;
15212 }
15213 /* Construct the read FCF record mailbox command */
15214 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15215 if (rc) {
15216 error = -EINVAL;
15217 goto fail_fcf_read;
15218 }
15219 /* Issue the mailbox command asynchronously */
15220 mboxq->vport = phba->pport;
15221 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15222 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15223 if (rc == MBX_NOT_FINISHED)
15224 error = -EIO;
15225 else
15226 error = 0;
15227
15228fail_fcf_read:
15229 if (error && mboxq)
15230 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15231 return error;
15232}
15233
15234/**
James Smart7d791df2011-07-22 18:37:52 -040015235 * lpfc_check_next_fcf_pri
15236 * phba pointer to the lpfc_hba struct for this port.
15237 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15238 * routine when the rr_bmask is empty. The FCF indecies are put into the
15239 * rr_bmask based on their priority level. Starting from the highest priority
15240 * to the lowest. The most likely FCF candidate will be in the highest
15241 * priority group. When this routine is called it searches the fcf_pri list for
15242 * next lowest priority group and repopulates the rr_bmask with only those
15243 * fcf_indexes.
15244 * returns:
15245 * 1=success 0=failure
15246 **/
15247int
15248lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15249{
15250 uint16_t next_fcf_pri;
15251 uint16_t last_index;
15252 struct lpfc_fcf_pri *fcf_pri;
15253 int rc;
15254 int ret = 0;
15255
15256 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15257 LPFC_SLI4_FCF_TBL_INDX_MAX);
15258 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15259 "3060 Last IDX %d\n", last_index);
15260 if (list_empty(&phba->fcf.fcf_pri_list)) {
15261 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15262 "3061 Last IDX %d\n", last_index);
15263 return 0; /* Empty rr list */
15264 }
15265 next_fcf_pri = 0;
15266 /*
15267 * Clear the rr_bmask and set all of the bits that are at this
15268 * priority.
15269 */
15270 memset(phba->fcf.fcf_rr_bmask, 0,
15271 sizeof(*phba->fcf.fcf_rr_bmask));
15272 spin_lock_irq(&phba->hbalock);
15273 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15274 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15275 continue;
15276 /*
15277 * the 1st priority that has not FLOGI failed
15278 * will be the highest.
15279 */
15280 if (!next_fcf_pri)
15281 next_fcf_pri = fcf_pri->fcf_rec.priority;
15282 spin_unlock_irq(&phba->hbalock);
15283 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15284 rc = lpfc_sli4_fcf_rr_index_set(phba,
15285 fcf_pri->fcf_rec.fcf_index);
15286 if (rc)
15287 return 0;
15288 }
15289 spin_lock_irq(&phba->hbalock);
15290 }
15291 /*
15292 * if next_fcf_pri was not set above and the list is not empty then
15293 * we have failed flogis on all of them. So reset flogi failed
15294 * and start at the begining.
15295 */
15296 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15297 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15298 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15299 /*
15300 * the 1st priority that has not FLOGI failed
15301 * will be the highest.
15302 */
15303 if (!next_fcf_pri)
15304 next_fcf_pri = fcf_pri->fcf_rec.priority;
15305 spin_unlock_irq(&phba->hbalock);
15306 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15307 rc = lpfc_sli4_fcf_rr_index_set(phba,
15308 fcf_pri->fcf_rec.fcf_index);
15309 if (rc)
15310 return 0;
15311 }
15312 spin_lock_irq(&phba->hbalock);
15313 }
15314 } else
15315 ret = 1;
15316 spin_unlock_irq(&phba->hbalock);
15317
15318 return ret;
15319}
15320/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015321 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15322 * @phba: pointer to lpfc hba data structure.
15323 *
15324 * This routine is to get the next eligible FCF record index in a round
15325 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015326 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015327 * shall be returned, otherwise, the next eligible FCF record's index
15328 * shall be returned.
15329 **/
15330uint16_t
15331lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15332{
15333 uint16_t next_fcf_index;
15334
James Smart3804dc82010-07-14 15:31:37 -040015335 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015336next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015337 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15338 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015339 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15340 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015341 next_fcf_index);
15342
James Smart0c9ab6f2010-02-26 14:15:57 -050015343 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015344 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15345 /*
15346 * If we have wrapped then we need to clear the bits that
15347 * have been tested so that we can detect when we should
15348 * change the priority level.
15349 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015350 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15351 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015352 }
15353
James Smart0c9ab6f2010-02-26 14:15:57 -050015354
James Smart3804dc82010-07-14 15:31:37 -040015355 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015356 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15357 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15358 /*
15359 * If next fcf index is not found check if there are lower
15360 * Priority level fcf's in the fcf_priority list.
15361 * Set up the rr_bmask with all of the avaiable fcf bits
15362 * at that level and continue the selection process.
15363 */
15364 if (lpfc_check_next_fcf_pri_level(phba))
15365 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015366 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15367 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015368 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15369 return LPFC_FCOE_FCF_NEXT_NONE;
15370 else {
15371 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15372 "3063 Only FCF available idx %d, flag %x\n",
15373 next_fcf_index,
15374 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15375 return next_fcf_index;
15376 }
James Smart3804dc82010-07-14 15:31:37 -040015377 }
15378
James Smart7d791df2011-07-22 18:37:52 -040015379 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15380 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15381 LPFC_FCF_FLOGI_FAILED)
15382 goto next_priority;
15383
James Smart3804dc82010-07-14 15:31:37 -040015384 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015385 "2845 Get next roundrobin failover FCF (x%x)\n",
15386 next_fcf_index);
15387
James Smart0c9ab6f2010-02-26 14:15:57 -050015388 return next_fcf_index;
15389}
15390
15391/**
15392 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15393 * @phba: pointer to lpfc hba data structure.
15394 *
15395 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015396 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015397 * does not go beyond the range of the driver allocated bmask dimension
15398 * before setting the bit.
15399 *
15400 * Returns 0 if the index bit successfully set, otherwise, it returns
15401 * -EINVAL.
15402 **/
15403int
15404lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15405{
15406 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15407 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015408 "2610 FCF (x%x) reached driver's book "
15409 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015410 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15411 return -EINVAL;
15412 }
15413 /* Set the eligible FCF record index bmask */
15414 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15415
James Smart3804dc82010-07-14 15:31:37 -040015416 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015417 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015418 "bmask\n", fcf_index);
15419
James Smart0c9ab6f2010-02-26 14:15:57 -050015420 return 0;
15421}
15422
15423/**
James Smart3804dc82010-07-14 15:31:37 -040015424 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015425 * @phba: pointer to lpfc hba data structure.
15426 *
15427 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015428 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015429 * does not go beyond the range of the driver allocated bmask dimension
15430 * before clearing the bit.
15431 **/
15432void
15433lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15434{
James Smart7d791df2011-07-22 18:37:52 -040015435 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015436 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15437 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015438 "2762 FCF (x%x) reached driver's book "
15439 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015440 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15441 return;
15442 }
15443 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015444 spin_lock_irq(&phba->hbalock);
15445 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15446 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15447 list_del_init(&fcf_pri->list);
15448 break;
15449 }
15450 }
15451 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015452 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015453
15454 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015455 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015456 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015457}
15458
15459/**
James Smartecfd03c2010-02-12 14:41:27 -050015460 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15461 * @phba: pointer to lpfc hba data structure.
15462 *
15463 * This routine is the completion routine for the rediscover FCF table mailbox
15464 * command. If the mailbox command returned failure, it will try to stop the
15465 * FCF rediscover wait timer.
15466 **/
15467void
15468lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15469{
15470 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15471 uint32_t shdr_status, shdr_add_status;
15472
15473 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15474
15475 shdr_status = bf_get(lpfc_mbox_hdr_status,
15476 &redisc_fcf->header.cfg_shdr.response);
15477 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15478 &redisc_fcf->header.cfg_shdr.response);
15479 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015480 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015481 "2746 Requesting for FCF rediscovery failed "
15482 "status x%x add_status x%x\n",
15483 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015484 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015485 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015486 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015487 spin_unlock_irq(&phba->hbalock);
15488 /*
15489 * CVL event triggered FCF rediscover request failed,
15490 * last resort to re-try current registered FCF entry.
15491 */
15492 lpfc_retry_pport_discovery(phba);
15493 } else {
15494 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015495 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015496 spin_unlock_irq(&phba->hbalock);
15497 /*
15498 * DEAD FCF event triggered FCF rediscover request
15499 * failed, last resort to fail over as a link down
15500 * to FCF registration.
15501 */
15502 lpfc_sli4_fcf_dead_failthrough(phba);
15503 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015504 } else {
15505 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015506 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015507 /*
15508 * Start FCF rediscovery wait timer for pending FCF
15509 * before rescan FCF record table.
15510 */
15511 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015512 }
James Smartecfd03c2010-02-12 14:41:27 -050015513
15514 mempool_free(mbox, phba->mbox_mem_pool);
15515}
15516
15517/**
James Smart3804dc82010-07-14 15:31:37 -040015518 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015519 * @phba: pointer to lpfc hba data structure.
15520 *
15521 * This routine is invoked to request for rediscovery of the entire FCF table
15522 * by the port.
15523 **/
15524int
15525lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15526{
15527 LPFC_MBOXQ_t *mbox;
15528 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15529 int rc, length;
15530
James Smart0c9ab6f2010-02-26 14:15:57 -050015531 /* Cancel retry delay timers to all vports before FCF rediscover */
15532 lpfc_cancel_all_vport_retry_delay_timer(phba);
15533
James Smartecfd03c2010-02-12 14:41:27 -050015534 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15535 if (!mbox) {
15536 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15537 "2745 Failed to allocate mbox for "
15538 "requesting FCF rediscover.\n");
15539 return -ENOMEM;
15540 }
15541
15542 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15543 sizeof(struct lpfc_sli4_cfg_mhdr));
15544 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15545 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15546 length, LPFC_SLI4_MBX_EMBED);
15547
15548 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15549 /* Set count to 0 for invalidating the entire FCF database */
15550 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15551
15552 /* Issue the mailbox command asynchronously */
15553 mbox->vport = phba->pport;
15554 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15555 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15556
15557 if (rc == MBX_NOT_FINISHED) {
15558 mempool_free(mbox, phba->mbox_mem_pool);
15559 return -EIO;
15560 }
15561 return 0;
15562}
15563
15564/**
James Smartfc2b9892010-02-26 14:15:29 -050015565 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15566 * @phba: pointer to lpfc hba data structure.
15567 *
15568 * This function is the failover routine as a last resort to the FCF DEAD
15569 * event when driver failed to perform fast FCF failover.
15570 **/
15571void
15572lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15573{
15574 uint32_t link_state;
15575
15576 /*
15577 * Last resort as FCF DEAD event failover will treat this as
15578 * a link down, but save the link state because we don't want
15579 * it to be changed to Link Down unless it is already down.
15580 */
15581 link_state = phba->link_state;
15582 lpfc_linkdown(phba);
15583 phba->link_state = link_state;
15584
15585 /* Unregister FCF if no devices connected to it */
15586 lpfc_unregister_unused_fcf(phba);
15587}
15588
15589/**
James Smart026abb82011-12-13 13:20:45 -050015590 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015591 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015592 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015593 *
James Smart026abb82011-12-13 13:20:45 -050015594 * This function gets SLI3 port configure region 23 data through memory dump
15595 * mailbox command. When it successfully retrieves data, the size of the data
15596 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015597 **/
James Smart026abb82011-12-13 13:20:45 -050015598static uint32_t
15599lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015600{
15601 LPFC_MBOXQ_t *pmb = NULL;
15602 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015603 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015604 int rc;
15605
James Smart026abb82011-12-13 13:20:45 -050015606 if (!rgn23_data)
15607 return 0;
15608
James Smarta0c87cb2009-07-19 10:01:10 -040015609 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15610 if (!pmb) {
15611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015612 "2600 failed to allocate mailbox memory\n");
15613 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015614 }
15615 mb = &pmb->u.mb;
15616
James Smarta0c87cb2009-07-19 10:01:10 -040015617 do {
15618 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15619 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15620
15621 if (rc != MBX_SUCCESS) {
15622 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015623 "2601 failed to read config "
15624 "region 23, rc 0x%x Status 0x%x\n",
15625 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015626 mb->un.varDmp.word_cnt = 0;
15627 }
15628 /*
15629 * dump mem may return a zero when finished or we got a
15630 * mailbox error, either way we are done.
15631 */
15632 if (mb->un.varDmp.word_cnt == 0)
15633 break;
15634 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15635 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15636
15637 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015638 rgn23_data + offset,
15639 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015640 offset += mb->un.varDmp.word_cnt;
15641 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15642
James Smart026abb82011-12-13 13:20:45 -050015643 mempool_free(pmb, phba->mbox_mem_pool);
15644 return offset;
15645}
15646
15647/**
15648 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15649 * @phba: pointer to lpfc hba data structure.
15650 * @rgn23_data: pointer to configure region 23 data.
15651 *
15652 * This function gets SLI4 port configure region 23 data through memory dump
15653 * mailbox command. When it successfully retrieves data, the size of the data
15654 * will be returned, otherwise, 0 will be returned.
15655 **/
15656static uint32_t
15657lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15658{
15659 LPFC_MBOXQ_t *mboxq = NULL;
15660 struct lpfc_dmabuf *mp = NULL;
15661 struct lpfc_mqe *mqe;
15662 uint32_t data_length = 0;
15663 int rc;
15664
15665 if (!rgn23_data)
15666 return 0;
15667
15668 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15669 if (!mboxq) {
15670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15671 "3105 failed to allocate mailbox memory\n");
15672 return 0;
15673 }
15674
15675 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15676 goto out;
15677 mqe = &mboxq->u.mqe;
15678 mp = (struct lpfc_dmabuf *) mboxq->context1;
15679 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15680 if (rc)
15681 goto out;
15682 data_length = mqe->un.mb_words[5];
15683 if (data_length == 0)
15684 goto out;
15685 if (data_length > DMP_RGN23_SIZE) {
15686 data_length = 0;
15687 goto out;
15688 }
15689 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15690out:
15691 mempool_free(mboxq, phba->mbox_mem_pool);
15692 if (mp) {
15693 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15694 kfree(mp);
15695 }
15696 return data_length;
15697}
15698
15699/**
15700 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15701 * @phba: pointer to lpfc hba data structure.
15702 *
15703 * This function read region 23 and parse TLV for port status to
15704 * decide if the user disaled the port. If the TLV indicates the
15705 * port is disabled, the hba_flag is set accordingly.
15706 **/
15707void
15708lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15709{
15710 uint8_t *rgn23_data = NULL;
15711 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15712 uint32_t offset = 0;
15713
15714 /* Get adapter Region 23 data */
15715 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15716 if (!rgn23_data)
15717 goto out;
15718
15719 if (phba->sli_rev < LPFC_SLI_REV4)
15720 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15721 else {
15722 if_type = bf_get(lpfc_sli_intf_if_type,
15723 &phba->sli4_hba.sli_intf);
15724 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15725 goto out;
15726 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15727 }
James Smarta0c87cb2009-07-19 10:01:10 -040015728
15729 if (!data_size)
15730 goto out;
15731
15732 /* Check the region signature first */
15733 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15735 "2619 Config region 23 has bad signature\n");
15736 goto out;
15737 }
15738 offset += 4;
15739
15740 /* Check the data structure version */
15741 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15743 "2620 Config region 23 has bad version\n");
15744 goto out;
15745 }
15746 offset += 4;
15747
15748 /* Parse TLV entries in the region */
15749 while (offset < data_size) {
15750 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15751 break;
15752 /*
15753 * If the TLV is not driver specific TLV or driver id is
15754 * not linux driver id, skip the record.
15755 */
15756 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15757 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15758 (rgn23_data[offset + 3] != 0)) {
15759 offset += rgn23_data[offset + 1] * 4 + 4;
15760 continue;
15761 }
15762
15763 /* Driver found a driver specific TLV in the config region */
15764 sub_tlv_len = rgn23_data[offset + 1] * 4;
15765 offset += 4;
15766 tlv_offset = 0;
15767
15768 /*
15769 * Search for configured port state sub-TLV.
15770 */
15771 while ((offset < data_size) &&
15772 (tlv_offset < sub_tlv_len)) {
15773 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15774 offset += 4;
15775 tlv_offset += 4;
15776 break;
15777 }
15778 if (rgn23_data[offset] != PORT_STE_TYPE) {
15779 offset += rgn23_data[offset + 1] * 4 + 4;
15780 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15781 continue;
15782 }
15783
15784 /* This HBA contains PORT_STE configured */
15785 if (!rgn23_data[offset + 2])
15786 phba->hba_flag |= LINK_DISABLED;
15787
15788 goto out;
15789 }
15790 }
James Smart026abb82011-12-13 13:20:45 -050015791
James Smarta0c87cb2009-07-19 10:01:10 -040015792out:
James Smarta0c87cb2009-07-19 10:01:10 -040015793 kfree(rgn23_data);
15794 return;
15795}
James Smart695a8142010-01-26 23:08:03 -050015796
15797/**
James Smart52d52442011-05-24 11:42:45 -040015798 * lpfc_wr_object - write an object to the firmware
15799 * @phba: HBA structure that indicates port to create a queue on.
15800 * @dmabuf_list: list of dmabufs to write to the port.
15801 * @size: the total byte value of the objects to write to the port.
15802 * @offset: the current offset to be used to start the transfer.
15803 *
15804 * This routine will create a wr_object mailbox command to send to the port.
15805 * the mailbox command will be constructed using the dma buffers described in
15806 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15807 * BDEs that the imbedded mailbox can support. The @offset variable will be
15808 * used to indicate the starting offset of the transfer and will also return
15809 * the offset after the write object mailbox has completed. @size is used to
15810 * determine the end of the object and whether the eof bit should be set.
15811 *
15812 * Return 0 is successful and offset will contain the the new offset to use
15813 * for the next write.
15814 * Return negative value for error cases.
15815 **/
15816int
15817lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15818 uint32_t size, uint32_t *offset)
15819{
15820 struct lpfc_mbx_wr_object *wr_object;
15821 LPFC_MBOXQ_t *mbox;
15822 int rc = 0, i = 0;
15823 uint32_t shdr_status, shdr_add_status;
15824 uint32_t mbox_tmo;
15825 union lpfc_sli4_cfg_shdr *shdr;
15826 struct lpfc_dmabuf *dmabuf;
15827 uint32_t written = 0;
15828
15829 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15830 if (!mbox)
15831 return -ENOMEM;
15832
15833 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15834 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15835 sizeof(struct lpfc_mbx_wr_object) -
15836 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15837
15838 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15839 wr_object->u.request.write_offset = *offset;
15840 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15841 wr_object->u.request.object_name[0] =
15842 cpu_to_le32(wr_object->u.request.object_name[0]);
15843 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15844 list_for_each_entry(dmabuf, dmabuf_list, list) {
15845 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15846 break;
15847 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15848 wr_object->u.request.bde[i].addrHigh =
15849 putPaddrHigh(dmabuf->phys);
15850 if (written + SLI4_PAGE_SIZE >= size) {
15851 wr_object->u.request.bde[i].tus.f.bdeSize =
15852 (size - written);
15853 written += (size - written);
15854 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15855 } else {
15856 wr_object->u.request.bde[i].tus.f.bdeSize =
15857 SLI4_PAGE_SIZE;
15858 written += SLI4_PAGE_SIZE;
15859 }
15860 i++;
15861 }
15862 wr_object->u.request.bde_count = i;
15863 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15864 if (!phba->sli4_hba.intr_enable)
15865 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15866 else {
James Smarta183a152011-10-10 21:32:43 -040015867 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015868 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15869 }
15870 /* The IOCTL status is embedded in the mailbox subheader. */
15871 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15872 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15873 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15874 if (rc != MBX_TIMEOUT)
15875 mempool_free(mbox, phba->mbox_mem_pool);
15876 if (shdr_status || shdr_add_status || rc) {
15877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15878 "3025 Write Object mailbox failed with "
15879 "status x%x add_status x%x, mbx status x%x\n",
15880 shdr_status, shdr_add_status, rc);
15881 rc = -ENXIO;
15882 } else
15883 *offset += wr_object->u.response.actual_write_length;
15884 return rc;
15885}
15886
15887/**
James Smart695a8142010-01-26 23:08:03 -050015888 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15889 * @vport: pointer to vport data structure.
15890 *
15891 * This function iterate through the mailboxq and clean up all REG_LOGIN
15892 * and REG_VPI mailbox commands associated with the vport. This function
15893 * is called when driver want to restart discovery of the vport due to
15894 * a Clear Virtual Link event.
15895 **/
15896void
15897lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15898{
15899 struct lpfc_hba *phba = vport->phba;
15900 LPFC_MBOXQ_t *mb, *nextmb;
15901 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015902 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015903 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015904 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015905 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015906 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015907
James Smartd439d282010-09-29 11:18:45 -040015908 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015909 spin_lock_irq(&phba->hbalock);
15910 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15911 if (mb->vport != vport)
15912 continue;
15913
15914 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15915 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15916 continue;
15917
James Smartd439d282010-09-29 11:18:45 -040015918 list_del(&mb->list);
15919 list_add_tail(&mb->list, &mbox_cmd_list);
15920 }
15921 /* Clean up active mailbox command with the vport */
15922 mb = phba->sli.mbox_active;
15923 if (mb && (mb->vport == vport)) {
15924 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15925 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15926 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15927 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15928 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15929 /* Put reference count for delayed processing */
15930 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15931 /* Unregister the RPI when mailbox complete */
15932 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15933 }
15934 }
James Smart63e801c2010-11-20 23:14:19 -050015935 /* Cleanup any mailbox completions which are not yet processed */
15936 do {
15937 restart_loop = 0;
15938 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15939 /*
15940 * If this mailox is already processed or it is
15941 * for another vport ignore it.
15942 */
15943 if ((mb->vport != vport) ||
15944 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15945 continue;
15946
15947 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15948 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15949 continue;
15950
15951 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15952 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15953 ndlp = (struct lpfc_nodelist *)mb->context2;
15954 /* Unregister the RPI when mailbox complete */
15955 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15956 restart_loop = 1;
15957 spin_unlock_irq(&phba->hbalock);
15958 spin_lock(shost->host_lock);
15959 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15960 spin_unlock(shost->host_lock);
15961 spin_lock_irq(&phba->hbalock);
15962 break;
15963 }
15964 }
15965 } while (restart_loop);
15966
James Smartd439d282010-09-29 11:18:45 -040015967 spin_unlock_irq(&phba->hbalock);
15968
15969 /* Release the cleaned-up mailbox commands */
15970 while (!list_empty(&mbox_cmd_list)) {
15971 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015972 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15973 mp = (struct lpfc_dmabuf *) (mb->context1);
15974 if (mp) {
15975 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15976 kfree(mp);
15977 }
James Smart78730cf2010-04-06 15:06:30 -040015978 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015979 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015980 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015981 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015982 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015983 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015984 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015985 }
James Smart695a8142010-01-26 23:08:03 -050015986 }
James Smart695a8142010-01-26 23:08:03 -050015987 mempool_free(mb, phba->mbox_mem_pool);
15988 }
James Smartd439d282010-09-29 11:18:45 -040015989
15990 /* Release the ndlp with the cleaned-up active mailbox command */
15991 if (act_mbx_ndlp) {
15992 spin_lock(shost->host_lock);
15993 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15994 spin_unlock(shost->host_lock);
15995 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015996 }
James Smart695a8142010-01-26 23:08:03 -050015997}
15998
James Smart2a9bf3d2010-06-07 15:24:45 -040015999/**
16000 * lpfc_drain_txq - Drain the txq
16001 * @phba: Pointer to HBA context object.
16002 *
16003 * This function attempt to submit IOCBs on the txq
16004 * to the adapter. For SLI4 adapters, the txq contains
16005 * ELS IOCBs that have been deferred because the there
16006 * are no SGLs. This congestion can occur with large
16007 * vport counts during node discovery.
16008 **/
16009
16010uint32_t
16011lpfc_drain_txq(struct lpfc_hba *phba)
16012{
16013 LIST_HEAD(completions);
16014 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16015 struct lpfc_iocbq *piocbq = 0;
16016 unsigned long iflags = 0;
16017 char *fail_msg = NULL;
16018 struct lpfc_sglq *sglq;
16019 union lpfc_wqe wqe;
16020
16021 spin_lock_irqsave(&phba->hbalock, iflags);
16022 if (pring->txq_cnt > pring->txq_max)
16023 pring->txq_max = pring->txq_cnt;
16024
16025 spin_unlock_irqrestore(&phba->hbalock, iflags);
16026
16027 while (pring->txq_cnt) {
16028 spin_lock_irqsave(&phba->hbalock, iflags);
16029
James Smart19ca7602010-11-20 23:11:55 -050016030 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016031 if (!piocbq) {
16032 spin_unlock_irqrestore(&phba->hbalock, iflags);
16033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16034 "2823 txq empty and txq_cnt is %d\n ",
16035 pring->txq_cnt);
16036 break;
16037 }
James Smart19ca7602010-11-20 23:11:55 -050016038 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016039 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016040 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016041 spin_unlock_irqrestore(&phba->hbalock, iflags);
16042 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016043 }
16044
16045 /* The xri and iocb resources secured,
16046 * attempt to issue request
16047 */
James Smart6d368e52011-05-24 11:44:12 -040016048 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016049 piocbq->sli4_xritag = sglq->sli4_xritag;
16050 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16051 fail_msg = "to convert bpl to sgl";
16052 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16053 fail_msg = "to convert iocb to wqe";
16054 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16055 fail_msg = " - Wq is full";
16056 else
16057 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16058
16059 if (fail_msg) {
16060 /* Failed means we can't issue and need to cancel */
16061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16062 "2822 IOCB failed %s iotag 0x%x "
16063 "xri 0x%x\n",
16064 fail_msg,
16065 piocbq->iotag, piocbq->sli4_xritag);
16066 list_add_tail(&piocbq->list, &completions);
16067 }
16068 spin_unlock_irqrestore(&phba->hbalock, iflags);
16069 }
16070
James Smart2a9bf3d2010-06-07 15:24:45 -040016071 /* Cancel all the IOCBs that cannot be issued */
16072 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16073 IOERR_SLI_ABORTED);
16074
16075 return pring->txq_cnt;
16076}