blob: 84ed28bd4b13eba88098905ffd96e7fdf1d819cd [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 Smartba20c852012-08-03 12:36:52 -040072static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
James Smart05580562011-05-24 11:40:48 -040074
James Smart4f774512009-05-22 14:52:35 -040075static IOCB_t *
76lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
77{
78 return &iocbq->iocb;
79}
80
81/**
82 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
83 * @q: The Work Queue to operate on.
84 * @wqe: The work Queue Entry to put on the Work queue.
85 *
86 * This routine will copy the contents of @wqe to the next available entry on
87 * the @q. This function will then ring the Work Queue Doorbell to signal the
88 * HBA to start processing the Work Queue Entry. This function returns 0 if
89 * successful. If no entries are available on @q then this function will return
90 * -ENOMEM.
91 * The caller is expected to hold the hbalock when calling this routine.
92 **/
93static uint32_t
94lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
95{
James Smart2e90f4b2011-12-13 13:22:37 -050096 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040097 struct lpfc_register doorbell;
98 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -040099 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400100
James Smart2e90f4b2011-12-13 13:22:37 -0500101 /* sanity check on queue memory */
102 if (unlikely(!q))
103 return -ENOMEM;
104 temp_wqe = q->qe[q->host_index].wqe;
105
James Smart4f774512009-05-22 14:52:35 -0400106 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400107 idx = ((q->host_index + 1) % q->entry_count);
108 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400109 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400110 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400111 }
112 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400113 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500114 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400115 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500116 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
117 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400118 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
119
120 /* Update the host index before invoking device */
121 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400122
123 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400124
125 /* Ring Doorbell */
126 doorbell.word0 = 0;
127 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
128 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
129 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
130 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400131
132 return 0;
133}
134
135/**
136 * lpfc_sli4_wq_release - Updates internal hba index for WQ
137 * @q: The Work Queue to operate on.
138 * @index: The index to advance the hba index to.
139 *
140 * This routine will update the HBA index of a queue to reflect consumption of
141 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
142 * an entry the host calls this function to update the queue's internal
143 * pointers. This routine returns the number of entries that were consumed by
144 * the HBA.
145 **/
146static uint32_t
147lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
148{
149 uint32_t released = 0;
150
James Smart2e90f4b2011-12-13 13:22:37 -0500151 /* sanity check on queue memory */
152 if (unlikely(!q))
153 return 0;
154
James Smart4f774512009-05-22 14:52:35 -0400155 if (q->hba_index == index)
156 return 0;
157 do {
158 q->hba_index = ((q->hba_index + 1) % q->entry_count);
159 released++;
160 } while (q->hba_index != index);
161 return released;
162}
163
164/**
165 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
166 * @q: The Mailbox Queue to operate on.
167 * @wqe: The Mailbox Queue Entry to put on the Work queue.
168 *
169 * This routine will copy the contents of @mqe to the next available entry on
170 * the @q. This function will then ring the Work Queue Doorbell to signal the
171 * HBA to start processing the Work Queue Entry. This function returns 0 if
172 * successful. If no entries are available on @q then this function will return
173 * -ENOMEM.
174 * The caller is expected to hold the hbalock when calling this routine.
175 **/
176static uint32_t
177lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
178{
James Smart2e90f4b2011-12-13 13:22:37 -0500179 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400180 struct lpfc_register doorbell;
181 uint32_t host_index;
182
James Smart2e90f4b2011-12-13 13:22:37 -0500183 /* sanity check on queue memory */
184 if (unlikely(!q))
185 return -ENOMEM;
186 temp_mqe = q->qe[q->host_index].mqe;
187
James Smart4f774512009-05-22 14:52:35 -0400188 /* If the host has not yet processed the next entry then we are done */
189 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
190 return -ENOMEM;
191 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
192 /* Save off the mailbox pointer for completion */
193 q->phba->mbox = (MAILBOX_t *)temp_mqe;
194
195 /* Update the host index before invoking device */
196 host_index = q->host_index;
197 q->host_index = ((q->host_index + 1) % q->entry_count);
198
199 /* Ring Doorbell */
200 doorbell.word0 = 0;
201 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
202 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
203 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400204 return 0;
205}
206
207/**
208 * lpfc_sli4_mq_release - Updates internal hba index for MQ
209 * @q: The Mailbox Queue to operate on.
210 *
211 * This routine will update the HBA index of a queue to reflect consumption of
212 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
213 * an entry the host calls this function to update the queue's internal
214 * pointers. This routine returns the number of entries that were consumed by
215 * the HBA.
216 **/
217static uint32_t
218lpfc_sli4_mq_release(struct lpfc_queue *q)
219{
James Smart2e90f4b2011-12-13 13:22:37 -0500220 /* sanity check on queue memory */
221 if (unlikely(!q))
222 return 0;
223
James Smart4f774512009-05-22 14:52:35 -0400224 /* Clear the mailbox pointer for completion */
225 q->phba->mbox = NULL;
226 q->hba_index = ((q->hba_index + 1) % q->entry_count);
227 return 1;
228}
229
230/**
231 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
232 * @q: The Event Queue to get the first valid EQE from
233 *
234 * This routine will get the first valid Event Queue Entry from @q, update
235 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
236 * the Queue (no more work to do), or the Queue is full of EQEs that have been
237 * processed, but not popped back to the HBA then this routine will return NULL.
238 **/
239static struct lpfc_eqe *
240lpfc_sli4_eq_get(struct lpfc_queue *q)
241{
James Smart2e90f4b2011-12-13 13:22:37 -0500242 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400243 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500244
245 /* sanity check on queue memory */
246 if (unlikely(!q))
247 return NULL;
248 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400249
250 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400251 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400252 return NULL;
253 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400254 idx = ((q->hba_index + 1) % q->entry_count);
255 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400256 return NULL;
257
James Smart027140e2012-08-03 12:35:44 -0400258 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400259 return eqe;
260}
261
262/**
James Smartba20c852012-08-03 12:36:52 -0400263 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
264 * @q: The Event Queue to disable interrupts
265 *
266 **/
267static inline void
268lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
269{
270 struct lpfc_register doorbell;
271
272 doorbell.word0 = 0;
273 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
274 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
275 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
276 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
277 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
278 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
279}
280
281/**
James Smart4f774512009-05-22 14:52:35 -0400282 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
283 * @q: The Event Queue that the host has completed processing for.
284 * @arm: Indicates whether the host wants to arms this CQ.
285 *
286 * This routine will mark all Event Queue Entries on @q, from the last
287 * known completed entry to the last entry that was processed, as completed
288 * by clearing the valid bit for each completion queue entry. Then it will
289 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
290 * The internal host index in the @q will be updated by this routine to indicate
291 * that the host has finished processing the entries. The @arm parameter
292 * indicates that the queue should be rearmed when ringing the doorbell.
293 *
294 * This function will return the number of EQEs that were popped.
295 **/
296uint32_t
297lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
298{
299 uint32_t released = 0;
300 struct lpfc_eqe *temp_eqe;
301 struct lpfc_register doorbell;
302
James Smart2e90f4b2011-12-13 13:22:37 -0500303 /* sanity check on queue memory */
304 if (unlikely(!q))
305 return 0;
306
James Smart4f774512009-05-22 14:52:35 -0400307 /* while there are valid entries */
308 while (q->hba_index != q->host_index) {
309 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400310 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400311 released++;
312 q->host_index = ((q->host_index + 1) % q->entry_count);
313 }
314 if (unlikely(released == 0 && !arm))
315 return 0;
316
317 /* ring doorbell for number popped */
318 doorbell.word0 = 0;
319 if (arm) {
320 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
321 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
322 }
323 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
324 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500325 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
326 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
327 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400328 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500329 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
330 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
331 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400332 return released;
333}
334
335/**
336 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
337 * @q: The Completion Queue to get the first valid CQE from
338 *
339 * This routine will get the first valid Completion Queue Entry from @q, update
340 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
341 * the Queue (no more work to do), or the Queue is full of CQEs that have been
342 * processed, but not popped back to the HBA then this routine will return NULL.
343 **/
344static struct lpfc_cqe *
345lpfc_sli4_cq_get(struct lpfc_queue *q)
346{
347 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400348 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400349
James Smart2e90f4b2011-12-13 13:22:37 -0500350 /* sanity check on queue memory */
351 if (unlikely(!q))
352 return NULL;
353
James Smart4f774512009-05-22 14:52:35 -0400354 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400355 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400356 return NULL;
357 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400358 idx = ((q->hba_index + 1) % q->entry_count);
359 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400360 return NULL;
361
362 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400363 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400364 return cqe;
365}
366
367/**
368 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
369 * @q: The Completion Queue that the host has completed processing for.
370 * @arm: Indicates whether the host wants to arms this CQ.
371 *
372 * This routine will mark all Completion queue entries on @q, from the last
373 * known completed entry to the last entry that was processed, as completed
374 * by clearing the valid bit for each completion queue entry. Then it will
375 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
376 * The internal host index in the @q will be updated by this routine to indicate
377 * that the host has finished processing the entries. The @arm parameter
378 * indicates that the queue should be rearmed when ringing the doorbell.
379 *
380 * This function will return the number of CQEs that were released.
381 **/
382uint32_t
383lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
384{
385 uint32_t released = 0;
386 struct lpfc_cqe *temp_qe;
387 struct lpfc_register doorbell;
388
James Smart2e90f4b2011-12-13 13:22:37 -0500389 /* sanity check on queue memory */
390 if (unlikely(!q))
391 return 0;
James Smart4f774512009-05-22 14:52:35 -0400392 /* while there are valid entries */
393 while (q->hba_index != q->host_index) {
394 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400395 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400396 released++;
397 q->host_index = ((q->host_index + 1) % q->entry_count);
398 }
399 if (unlikely(released == 0 && !arm))
400 return 0;
401
402 /* ring doorbell for number popped */
403 doorbell.word0 = 0;
404 if (arm)
405 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
406 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
407 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500408 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
409 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
410 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400411 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
412 return released;
413}
414
415/**
416 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
417 * @q: The Header Receive Queue to operate on.
418 * @wqe: The Receive Queue Entry to put on the Receive queue.
419 *
420 * This routine will copy the contents of @wqe to the next available entry on
421 * the @q. This function will then ring the Receive Queue Doorbell to signal the
422 * HBA to start processing the Receive Queue Entry. This function returns the
423 * index that the rqe was copied to if successful. If no entries are available
424 * on @q then this function will return -ENOMEM.
425 * The caller is expected to hold the hbalock when calling this routine.
426 **/
427static int
428lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
429 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
430{
James Smart2e90f4b2011-12-13 13:22:37 -0500431 struct lpfc_rqe *temp_hrqe;
432 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400433 struct lpfc_register doorbell;
434 int put_index = hq->host_index;
435
James Smart2e90f4b2011-12-13 13:22:37 -0500436 /* sanity check on queue memory */
437 if (unlikely(!hq) || unlikely(!dq))
438 return -ENOMEM;
439 temp_hrqe = hq->qe[hq->host_index].rqe;
440 temp_drqe = dq->qe[dq->host_index].rqe;
441
James Smart4f774512009-05-22 14:52:35 -0400442 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
443 return -EINVAL;
444 if (hq->host_index != dq->host_index)
445 return -EINVAL;
446 /* If the host has not yet processed the next entry then we are done */
447 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
448 return -EBUSY;
449 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
450 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
451
452 /* Update the host index to point to the next slot */
453 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
454 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
455
456 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400457 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400458 doorbell.word0 = 0;
459 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400460 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400461 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
462 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
463 }
464 return put_index;
465}
466
467/**
468 * lpfc_sli4_rq_release - Updates internal hba index for RQ
469 * @q: The Header Receive Queue to operate on.
470 *
471 * This routine will update the HBA index of a queue to reflect consumption of
472 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
473 * consumed an entry the host calls this function to update the queue's
474 * internal pointers. This routine returns the number of entries that were
475 * consumed by the HBA.
476 **/
477static uint32_t
478lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
479{
James Smart2e90f4b2011-12-13 13:22:37 -0500480 /* sanity check on queue memory */
481 if (unlikely(!hq) || unlikely(!dq))
482 return 0;
483
James Smart4f774512009-05-22 14:52:35 -0400484 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
485 return 0;
486 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
487 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
488 return 1;
489}
490
James Smarte59058c2008-08-24 21:49:00 -0400491/**
James Smart3621a712009-04-06 18:47:14 -0400492 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400493 * @phba: Pointer to HBA context object.
494 * @pring: Pointer to driver SLI ring object.
495 *
496 * This function returns pointer to next command iocb entry
497 * in the command ring. The caller must hold hbalock to prevent
498 * other threads consume the next command iocb.
499 * SLI-2/SLI-3 provide different sized iocbs.
500 **/
James Smarted957682007-06-17 19:56:37 -0500501static inline IOCB_t *
502lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
503{
James Smart7e56aa22012-08-03 12:35:34 -0400504 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
505 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500506}
507
James Smarte59058c2008-08-24 21:49:00 -0400508/**
James Smart3621a712009-04-06 18:47:14 -0400509 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400510 * @phba: Pointer to HBA context object.
511 * @pring: Pointer to driver SLI ring object.
512 *
513 * This function returns pointer to next response iocb entry
514 * in the response ring. The caller must hold hbalock to make sure
515 * that no other thread consume the next response iocb.
516 * SLI-2/SLI-3 provide different sized iocbs.
517 **/
James Smarted957682007-06-17 19:56:37 -0500518static inline IOCB_t *
519lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
520{
James Smart7e56aa22012-08-03 12:35:34 -0400521 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
522 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500523}
524
James Smarte59058c2008-08-24 21:49:00 -0400525/**
James Smart3621a712009-04-06 18:47:14 -0400526 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400527 * @phba: Pointer to HBA context object.
528 *
529 * This function is called with hbalock held. This function
530 * allocates a new driver iocb object from the iocb pool. If the
531 * allocation is successful, it returns pointer to the newly
532 * allocated iocb object else it returns NULL.
533 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400534struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500535__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400536{
537 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
538 struct lpfc_iocbq * iocbq = NULL;
539
540 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400541 if (iocbq)
542 phba->iocb_cnt++;
543 if (phba->iocb_cnt > phba->iocb_max)
544 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400545 return iocbq;
546}
547
James Smarte59058c2008-08-24 21:49:00 -0400548/**
James Smartda0436e2009-05-22 14:51:39 -0400549 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
550 * @phba: Pointer to HBA context object.
551 * @xritag: XRI value.
552 *
553 * This function clears the sglq pointer from the array of acive
554 * sglq's. The xritag that is passed in is used to index into the
555 * array. Before the xritag can be used it needs to be adjusted
556 * by subtracting the xribase.
557 *
558 * Returns sglq ponter = success, NULL = Failure.
559 **/
560static struct lpfc_sglq *
561__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
562{
James Smartda0436e2009-05-22 14:51:39 -0400563 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400564
565 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
566 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400567 return sglq;
568}
569
570/**
571 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
572 * @phba: Pointer to HBA context object.
573 * @xritag: XRI value.
574 *
575 * This function returns the sglq pointer from the array of acive
576 * sglq's. The xritag that is passed in is used to index into the
577 * array. Before the xritag can be used it needs to be adjusted
578 * by subtracting the xribase.
579 *
580 * Returns sglq ponter = success, NULL = Failure.
581 **/
James Smart0f65ff62010-02-26 14:14:23 -0500582struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400583__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
584{
James Smartda0436e2009-05-22 14:51:39 -0400585 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400586
587 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400588 return sglq;
589}
590
591/**
James Smart1151e3e2011-02-16 12:39:35 -0500592 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500593 * @phba: Pointer to HBA context object.
594 * @xritag: xri used in this exchange.
595 * @rrq: The RRQ to be cleared.
596 *
James Smart19ca7602010-11-20 23:11:55 -0500597 **/
James Smart1151e3e2011-02-16 12:39:35 -0500598void
599lpfc_clr_rrq_active(struct lpfc_hba *phba,
600 uint16_t xritag,
601 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500602{
James Smart1151e3e2011-02-16 12:39:35 -0500603 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500604
James Smart1151e3e2011-02-16 12:39:35 -0500605 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
606 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500607
608 /* The target DID could have been swapped (cable swap)
609 * we should use the ndlp from the findnode if it is
610 * available.
611 */
James Smart1151e3e2011-02-16 12:39:35 -0500612 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500613 ndlp = rrq->ndlp;
614
James Smart1151e3e2011-02-16 12:39:35 -0500615 if (!ndlp)
616 goto out;
617
James Smart6d368e52011-05-24 11:44:12 -0400618 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500619 rrq->send_rrq = 0;
620 rrq->xritag = 0;
621 rrq->rrq_stop_time = 0;
622 }
James Smart1151e3e2011-02-16 12:39:35 -0500623out:
James Smart19ca7602010-11-20 23:11:55 -0500624 mempool_free(rrq, phba->rrq_pool);
625}
626
627/**
628 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
629 * @phba: Pointer to HBA context object.
630 *
631 * This function is called with hbalock held. This function
632 * Checks if stop_time (ratov from setting rrq active) has
633 * been reached, if it has and the send_rrq flag is set then
634 * it will call lpfc_send_rrq. If the send_rrq flag is not set
635 * then it will just call the routine to clear the rrq and
636 * free the rrq resource.
637 * The timer is set to the next rrq that is going to expire before
638 * leaving the routine.
639 *
640 **/
641void
642lpfc_handle_rrq_active(struct lpfc_hba *phba)
643{
644 struct lpfc_node_rrq *rrq;
645 struct lpfc_node_rrq *nextrrq;
646 unsigned long next_time;
647 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500648 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500649
650 spin_lock_irqsave(&phba->hbalock, iflags);
651 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
652 next_time = jiffies + HZ * (phba->fc_ratov + 1);
653 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500654 &phba->active_rrq_list, list) {
655 if (time_after(jiffies, rrq->rrq_stop_time))
656 list_move(&rrq->list, &send_rrq);
657 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500658 next_time = rrq->rrq_stop_time;
659 }
660 spin_unlock_irqrestore(&phba->hbalock, iflags);
661 if (!list_empty(&phba->active_rrq_list))
662 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500663 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
664 list_del(&rrq->list);
665 if (!rrq->send_rrq)
666 /* this call will free the rrq */
667 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
668 else if (lpfc_send_rrq(phba, rrq)) {
669 /* if we send the rrq then the completion handler
670 * will clear the bit in the xribitmap.
671 */
672 lpfc_clr_rrq_active(phba, rrq->xritag,
673 rrq);
674 }
675 }
James Smart19ca7602010-11-20 23:11:55 -0500676}
677
678/**
679 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
680 * @vport: Pointer to vport context object.
681 * @xri: The xri used in the exchange.
682 * @did: The targets DID for this exchange.
683 *
684 * returns NULL = rrq not found in the phba->active_rrq_list.
685 * rrq = rrq for this xri and target.
686 **/
687struct lpfc_node_rrq *
688lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
689{
690 struct lpfc_hba *phba = vport->phba;
691 struct lpfc_node_rrq *rrq;
692 struct lpfc_node_rrq *nextrrq;
693 unsigned long iflags;
694
695 if (phba->sli_rev != LPFC_SLI_REV4)
696 return NULL;
697 spin_lock_irqsave(&phba->hbalock, iflags);
698 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
699 if (rrq->vport == vport && rrq->xritag == xri &&
700 rrq->nlp_DID == did){
701 list_del(&rrq->list);
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return rrq;
704 }
705 }
706 spin_unlock_irqrestore(&phba->hbalock, iflags);
707 return NULL;
708}
709
710/**
711 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
712 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500713 * @ndlp: Pointer to the lpfc_node_list structure.
714 * If ndlp is NULL Remove all active RRQs for this vport from the
715 * phba->active_rrq_list and clear the rrq.
716 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500717 **/
718void
James Smart1151e3e2011-02-16 12:39:35 -0500719lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500720
721{
722 struct lpfc_hba *phba = vport->phba;
723 struct lpfc_node_rrq *rrq;
724 struct lpfc_node_rrq *nextrrq;
725 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500726 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500727
728 if (phba->sli_rev != LPFC_SLI_REV4)
729 return;
James Smart1151e3e2011-02-16 12:39:35 -0500730 if (!ndlp) {
731 lpfc_sli4_vport_delete_els_xri_aborted(vport);
732 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500733 }
James Smart1151e3e2011-02-16 12:39:35 -0500734 spin_lock_irqsave(&phba->hbalock, iflags);
735 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
736 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
737 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500738 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500739
740 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
741 list_del(&rrq->list);
742 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
743 }
James Smart19ca7602010-11-20 23:11:55 -0500744}
745
746/**
747 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
748 * @phba: Pointer to HBA context object.
749 *
750 * Remove all rrqs from the phba->active_rrq_list and free them by
751 * calling __lpfc_clr_active_rrq
752 *
753 **/
754void
755lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
756{
757 struct lpfc_node_rrq *rrq;
758 struct lpfc_node_rrq *nextrrq;
759 unsigned long next_time;
760 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500761 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500762
763 if (phba->sli_rev != LPFC_SLI_REV4)
764 return;
765 spin_lock_irqsave(&phba->hbalock, iflags);
766 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
767 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500768 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500769 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500770
771 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
772 list_del(&rrq->list);
773 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
774 }
James Smart19ca7602010-11-20 23:11:55 -0500775 if (!list_empty(&phba->active_rrq_list))
776 mod_timer(&phba->rrq_tmr, next_time);
777}
778
779
780/**
James Smart1151e3e2011-02-16 12:39:35 -0500781 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500782 * @phba: Pointer to HBA context object.
783 * @ndlp: Targets nodelist pointer for this exchange.
784 * @xritag the xri in the bitmap to test.
785 *
786 * This function is called with hbalock held. This function
787 * returns 0 = rrq not active for this xri
788 * 1 = rrq is valid for this xri.
789 **/
James Smart1151e3e2011-02-16 12:39:35 -0500790int
791lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500792 uint16_t xritag)
793{
James Smart19ca7602010-11-20 23:11:55 -0500794 if (!ndlp)
795 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400796 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500797 return 1;
798 else
799 return 0;
800}
801
802/**
803 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
804 * @phba: Pointer to HBA context object.
805 * @ndlp: nodelist pointer for this target.
806 * @xritag: xri used in this exchange.
807 * @rxid: Remote Exchange ID.
808 * @send_rrq: Flag used to determine if we should send rrq els cmd.
809 *
810 * This function takes the hbalock.
811 * The active bit is always set in the active rrq xri_bitmap even
812 * if there is no slot avaiable for the other rrq information.
813 *
814 * returns 0 rrq actived for this xri
815 * < 0 No memory or invalid ndlp.
816 **/
817int
818lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500819 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500820{
James Smart19ca7602010-11-20 23:11:55 -0500821 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500822 struct lpfc_node_rrq *rrq;
823 int empty;
824
825 if (!ndlp)
826 return -EINVAL;
827
828 if (!phba->cfg_enable_rrq)
829 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500830
831 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500832 if (phba->pport->load_flag & FC_UNLOADING) {
833 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
834 goto out;
835 }
836
837 /*
838 * set the active bit even if there is no mem available.
839 */
840 if (NLP_CHK_FREE_REQ(ndlp))
841 goto out;
842
843 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
844 goto out;
845
846 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
847 goto out;
848
James Smart19ca7602010-11-20 23:11:55 -0500849 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500850 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
851 if (!rrq) {
852 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
853 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
854 " DID:0x%x Send:%d\n",
855 xritag, rxid, ndlp->nlp_DID, send_rrq);
856 return -EINVAL;
857 }
858 rrq->send_rrq = send_rrq;
859 rrq->xritag = xritag;
860 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
861 rrq->ndlp = ndlp;
862 rrq->nlp_DID = ndlp->nlp_DID;
863 rrq->vport = ndlp->vport;
864 rrq->rxid = rxid;
865 rrq->send_rrq = send_rrq;
866 spin_lock_irqsave(&phba->hbalock, iflags);
867 empty = list_empty(&phba->active_rrq_list);
868 list_add_tail(&rrq->list, &phba->active_rrq_list);
869 phba->hba_flag |= HBA_RRQ_ACTIVE;
870 if (empty)
871 lpfc_worker_wake_up(phba);
872 spin_unlock_irqrestore(&phba->hbalock, iflags);
873 return 0;
874out:
875 spin_unlock_irqrestore(&phba->hbalock, iflags);
876 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
877 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
878 " DID:0x%x Send:%d\n",
879 xritag, rxid, ndlp->nlp_DID, send_rrq);
880 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500881}
882
883/**
James Smartda0436e2009-05-22 14:51:39 -0400884 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
885 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500886 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400887 *
888 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400889 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400890 * list is not empty then it is successful, it returns pointer to the newly
891 * allocated sglq object else it returns NULL.
892 **/
893static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500894__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400895{
896 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
897 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500898 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500899 struct lpfc_scsi_buf *lpfc_cmd;
900 struct lpfc_nodelist *ndlp;
901 int found = 0;
902
903 if (piocbq->iocb_flag & LPFC_IO_FCP) {
904 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
905 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500906 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
907 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500908 ndlp = piocbq->context_un.ndlp;
James Smart93d1379e2012-05-09 21:19:34 -0400909 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
910 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
911 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500912 else
913 ndlp = piocbq->context1;
914
James Smartda0436e2009-05-22 14:51:39 -0400915 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500916 start_sglq = sglq;
917 while (!found) {
918 if (!sglq)
919 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400920 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500921 /* This xri has an rrq outstanding for this DID.
922 * put it back in the list and get another xri.
923 */
924 list_add_tail(&sglq->list, lpfc_sgl_list);
925 sglq = NULL;
926 list_remove_head(lpfc_sgl_list, sglq,
927 struct lpfc_sglq, list);
928 if (sglq == start_sglq) {
929 sglq = NULL;
930 break;
931 } else
932 continue;
933 }
934 sglq->ndlp = ndlp;
935 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400936 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500937 sglq->state = SGL_ALLOCATED;
938 }
James Smartda0436e2009-05-22 14:51:39 -0400939 return sglq;
940}
941
942/**
James Smart3621a712009-04-06 18:47:14 -0400943 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400944 * @phba: Pointer to HBA context object.
945 *
946 * This function is called with no lock held. This function
947 * allocates a new driver iocb object from the iocb pool. If the
948 * allocation is successful, it returns pointer to the newly
949 * allocated iocb object else it returns NULL.
950 **/
James Smart2e0fef82007-06-17 19:56:36 -0500951struct lpfc_iocbq *
952lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500953{
James Smart2e0fef82007-06-17 19:56:36 -0500954 struct lpfc_iocbq * iocbq = NULL;
955 unsigned long iflags;
956
957 spin_lock_irqsave(&phba->hbalock, iflags);
958 iocbq = __lpfc_sli_get_iocbq(phba);
959 spin_unlock_irqrestore(&phba->hbalock, iflags);
960 return iocbq;
961}
962
James Smarte59058c2008-08-24 21:49:00 -0400963/**
James Smart4f774512009-05-22 14:52:35 -0400964 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
965 * @phba: Pointer to HBA context object.
966 * @iocbq: Pointer to driver iocb object.
967 *
968 * This function is called with hbalock held to release driver
969 * iocb object to the iocb pool. The iotag in the iocb object
970 * does not change for each use of the iocb object. This function
971 * clears all other fields of the iocb object when it is freed.
972 * The sqlq structure that holds the xritag and phys and virtual
973 * mappings for the scatter gather list is retrieved from the
974 * active array of sglq. The get of the sglq pointer also clears
975 * the entry in the array. If the status of the IO indiactes that
976 * this IO was aborted then the sglq entry it put on the
977 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
978 * IO has good status or fails for any other reason then the sglq
979 * entry is added to the free list (lpfc_sgl_list).
980 **/
981static void
982__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
983{
984 struct lpfc_sglq *sglq;
985 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400986 unsigned long iflag = 0;
987 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400988
989 if (iocbq->sli4_xritag == NO_XRI)
990 sglq = NULL;
991 else
James Smart6d368e52011-05-24 11:44:12 -0400992 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
993
James Smart4f774512009-05-22 14:52:35 -0400994 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500995 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
996 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400997 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
998 iflag);
999 list_add(&sglq->list,
1000 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1001 spin_unlock_irqrestore(
1002 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001003 } else {
1004 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001005 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001006 list_add_tail(&sglq->list,
1007 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -04001008
1009 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -04001010 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -04001011 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001012 }
James Smart4f774512009-05-22 14:52:35 -04001013 }
1014
1015
1016 /*
1017 * Clean all volatile data fields, preserve iotag and node struct.
1018 */
1019 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001020 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001021 iocbq->sli4_xritag = NO_XRI;
1022 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1023}
1024
James Smart2a9bf3d2010-06-07 15:24:45 -04001025
James Smart4f774512009-05-22 14:52:35 -04001026/**
James Smart3772a992009-05-22 14:50:54 -04001027 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1028 * @phba: Pointer to HBA context object.
1029 * @iocbq: Pointer to driver iocb object.
1030 *
1031 * This function is called with hbalock held to release driver
1032 * iocb object to the iocb pool. The iotag in the iocb object
1033 * does not change for each use of the iocb object. This function
1034 * clears all other fields of the iocb object when it is freed.
1035 **/
1036static void
1037__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1038{
1039 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1040
1041 /*
1042 * Clean all volatile data fields, preserve iotag and node struct.
1043 */
1044 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1045 iocbq->sli4_xritag = NO_XRI;
1046 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1047}
1048
1049/**
James Smart3621a712009-04-06 18:47:14 -04001050 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001051 * @phba: Pointer to HBA context object.
1052 * @iocbq: Pointer to driver iocb object.
1053 *
1054 * This function is called with hbalock held to release driver
1055 * iocb object to the iocb pool. The iotag in the iocb object
1056 * does not change for each use of the iocb object. This function
1057 * clears all other fields of the iocb object when it is freed.
1058 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001059static void
James Smart2e0fef82007-06-17 19:56:36 -05001060__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1061{
James Smart3772a992009-05-22 14:50:54 -04001062 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001063 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001064}
1065
James Smarte59058c2008-08-24 21:49:00 -04001066/**
James Smart3621a712009-04-06 18:47:14 -04001067 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001068 * @phba: Pointer to HBA context object.
1069 * @iocbq: Pointer to driver iocb object.
1070 *
1071 * This function is called with no lock held to release the iocb to
1072 * iocb pool.
1073 **/
James Smart2e0fef82007-06-17 19:56:36 -05001074void
1075lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1076{
1077 unsigned long iflags;
1078
1079 /*
1080 * Clean all volatile data fields, preserve iotag and node struct.
1081 */
1082 spin_lock_irqsave(&phba->hbalock, iflags);
1083 __lpfc_sli_release_iocbq(phba, iocbq);
1084 spin_unlock_irqrestore(&phba->hbalock, iflags);
1085}
1086
James Smarte59058c2008-08-24 21:49:00 -04001087/**
James Smarta257bf92009-04-06 18:48:10 -04001088 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1089 * @phba: Pointer to HBA context object.
1090 * @iocblist: List of IOCBs.
1091 * @ulpstatus: ULP status in IOCB command field.
1092 * @ulpWord4: ULP word-4 in IOCB command field.
1093 *
1094 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1095 * on the list by invoking the complete callback function associated with the
1096 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1097 * fields.
1098 **/
1099void
1100lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1101 uint32_t ulpstatus, uint32_t ulpWord4)
1102{
1103 struct lpfc_iocbq *piocb;
1104
1105 while (!list_empty(iocblist)) {
1106 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1107
1108 if (!piocb->iocb_cmpl)
1109 lpfc_sli_release_iocbq(phba, piocb);
1110 else {
1111 piocb->iocb.ulpStatus = ulpstatus;
1112 piocb->iocb.un.ulpWord[4] = ulpWord4;
1113 (piocb->iocb_cmpl) (phba, piocb, piocb);
1114 }
1115 }
1116 return;
1117}
1118
1119/**
James Smart3621a712009-04-06 18:47:14 -04001120 * lpfc_sli_iocb_cmd_type - Get the iocb type
1121 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001122 *
1123 * This function is called by ring event handler function to get the iocb type.
1124 * This function translates the iocb command to an iocb command type used to
1125 * decide the final disposition of each completed IOCB.
1126 * The function returns
1127 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1128 * LPFC_SOL_IOCB if it is a solicited iocb completion
1129 * LPFC_ABORT_IOCB if it is an abort iocb
1130 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1131 *
1132 * The caller is not required to hold any lock.
1133 **/
dea31012005-04-17 16:05:31 -05001134static lpfc_iocb_type
1135lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1136{
1137 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1138
1139 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1140 return 0;
1141
1142 switch (iocb_cmnd) {
1143 case CMD_XMIT_SEQUENCE_CR:
1144 case CMD_XMIT_SEQUENCE_CX:
1145 case CMD_XMIT_BCAST_CN:
1146 case CMD_XMIT_BCAST_CX:
1147 case CMD_ELS_REQUEST_CR:
1148 case CMD_ELS_REQUEST_CX:
1149 case CMD_CREATE_XRI_CR:
1150 case CMD_CREATE_XRI_CX:
1151 case CMD_GET_RPI_CN:
1152 case CMD_XMIT_ELS_RSP_CX:
1153 case CMD_GET_RPI_CR:
1154 case CMD_FCP_IWRITE_CR:
1155 case CMD_FCP_IWRITE_CX:
1156 case CMD_FCP_IREAD_CR:
1157 case CMD_FCP_IREAD_CX:
1158 case CMD_FCP_ICMND_CR:
1159 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001160 case CMD_FCP_TSEND_CX:
1161 case CMD_FCP_TRSP_CX:
1162 case CMD_FCP_TRECEIVE_CX:
1163 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001164 case CMD_ADAPTER_MSG:
1165 case CMD_ADAPTER_DUMP:
1166 case CMD_XMIT_SEQUENCE64_CR:
1167 case CMD_XMIT_SEQUENCE64_CX:
1168 case CMD_XMIT_BCAST64_CN:
1169 case CMD_XMIT_BCAST64_CX:
1170 case CMD_ELS_REQUEST64_CR:
1171 case CMD_ELS_REQUEST64_CX:
1172 case CMD_FCP_IWRITE64_CR:
1173 case CMD_FCP_IWRITE64_CX:
1174 case CMD_FCP_IREAD64_CR:
1175 case CMD_FCP_IREAD64_CX:
1176 case CMD_FCP_ICMND64_CR:
1177 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001178 case CMD_FCP_TSEND64_CX:
1179 case CMD_FCP_TRSP64_CX:
1180 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001181 case CMD_GEN_REQUEST64_CR:
1182 case CMD_GEN_REQUEST64_CX:
1183 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001184 case DSSCMD_IWRITE64_CR:
1185 case DSSCMD_IWRITE64_CX:
1186 case DSSCMD_IREAD64_CR:
1187 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001188 type = LPFC_SOL_IOCB;
1189 break;
1190 case CMD_ABORT_XRI_CN:
1191 case CMD_ABORT_XRI_CX:
1192 case CMD_CLOSE_XRI_CN:
1193 case CMD_CLOSE_XRI_CX:
1194 case CMD_XRI_ABORTED_CX:
1195 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001196 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001197 type = LPFC_ABORT_IOCB;
1198 break;
1199 case CMD_RCV_SEQUENCE_CX:
1200 case CMD_RCV_ELS_REQ_CX:
1201 case CMD_RCV_SEQUENCE64_CX:
1202 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001203 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001204 case CMD_IOCB_RCV_SEQ64_CX:
1205 case CMD_IOCB_RCV_ELS64_CX:
1206 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001207 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001208 type = LPFC_UNSOL_IOCB;
1209 break;
James Smart3163f722008-02-08 18:50:25 -05001210 case CMD_IOCB_XMIT_MSEQ64_CR:
1211 case CMD_IOCB_XMIT_MSEQ64_CX:
1212 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1213 case CMD_IOCB_RCV_ELS_LIST64_CX:
1214 case CMD_IOCB_CLOSE_EXTENDED_CN:
1215 case CMD_IOCB_ABORT_EXTENDED_CN:
1216 case CMD_IOCB_RET_HBQE64_CN:
1217 case CMD_IOCB_FCP_IBIDIR64_CR:
1218 case CMD_IOCB_FCP_IBIDIR64_CX:
1219 case CMD_IOCB_FCP_ITASKMGT64_CX:
1220 case CMD_IOCB_LOGENTRY_CN:
1221 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1222 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001223 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001224 type = LPFC_UNKNOWN_IOCB;
1225 break;
dea31012005-04-17 16:05:31 -05001226 default:
1227 type = LPFC_UNKNOWN_IOCB;
1228 break;
1229 }
1230
1231 return type;
1232}
1233
James Smarte59058c2008-08-24 21:49:00 -04001234/**
James Smart3621a712009-04-06 18:47:14 -04001235 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001236 * @phba: Pointer to HBA context object.
1237 *
1238 * This function is called from SLI initialization code
1239 * to configure every ring of the HBA's SLI interface. The
1240 * caller is not required to hold any lock. This function issues
1241 * a config_ring mailbox command for each ring.
1242 * This function returns zero if successful else returns a negative
1243 * error code.
1244 **/
dea31012005-04-17 16:05:31 -05001245static int
James Smarted957682007-06-17 19:56:37 -05001246lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001247{
1248 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001249 LPFC_MBOXQ_t *pmb;
1250 MAILBOX_t *pmbox;
1251 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001252
James Smarted957682007-06-17 19:56:37 -05001253 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1254 if (!pmb)
1255 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001256 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001257 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001258 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001259 lpfc_config_ring(phba, i, pmb);
1260 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1261 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001263 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001264 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1265 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001266 rc, pmbox->mbxCommand,
1267 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001268 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001269 ret = -ENXIO;
1270 break;
dea31012005-04-17 16:05:31 -05001271 }
1272 }
James Smarted957682007-06-17 19:56:37 -05001273 mempool_free(pmb, phba->mbox_mem_pool);
1274 return ret;
dea31012005-04-17 16:05:31 -05001275}
1276
James Smarte59058c2008-08-24 21:49:00 -04001277/**
James Smart3621a712009-04-06 18:47:14 -04001278 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001279 * @phba: Pointer to HBA context object.
1280 * @pring: Pointer to driver SLI ring object.
1281 * @piocb: Pointer to the driver iocb object.
1282 *
1283 * This function is called with hbalock held. The function adds the
1284 * new iocb to txcmplq of the given ring. This function always returns
1285 * 0. If this function is called for ELS ring, this function checks if
1286 * there is a vport associated with the ELS command. This function also
1287 * starts els_tmofunc timer if this is an ELS command.
1288 **/
dea31012005-04-17 16:05:31 -05001289static int
James Smart2e0fef82007-06-17 19:56:36 -05001290lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1291 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001292{
dea31012005-04-17 16:05:31 -05001293 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001294 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
dea31012005-04-17 16:05:31 -05001295 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001296 if (pring->txcmplq_cnt > pring->txcmplq_max)
1297 pring->txcmplq_max = pring->txcmplq_cnt;
1298
James Smart92d7f7b2007-06-17 19:56:38 -05001299 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1300 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1301 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1302 if (!piocb->vport)
1303 BUG();
1304 else
1305 mod_timer(&piocb->vport->els_tmofunc,
1306 jiffies + HZ * (phba->fc_ratov << 1));
1307 }
1308
dea31012005-04-17 16:05:31 -05001309
James Smart2e0fef82007-06-17 19:56:36 -05001310 return 0;
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_ringtx_get - Get first element of the txq
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 to get next
1319 * iocb in txq of the given ring. If there is any iocb in
1320 * the txq, the function returns first iocb in the list after
1321 * removing the iocb from the list, else it returns NULL.
1322 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001323struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001324lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001325{
dea31012005-04-17 16:05:31 -05001326 struct lpfc_iocbq *cmd_iocb;
1327
James Smart858c9f62007-06-17 19:56:39 -05001328 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1329 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001330 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001331 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001332}
1333
James Smarte59058c2008-08-24 21:49:00 -04001334/**
James Smart3621a712009-04-06 18:47:14 -04001335 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001336 * @phba: Pointer to HBA context object.
1337 * @pring: Pointer to driver SLI ring object.
1338 *
1339 * This function is called with hbalock held and the caller must post the
1340 * iocb without releasing the lock. If the caller releases the lock,
1341 * iocb slot returned by the function is not guaranteed to be available.
1342 * The function returns pointer to the next available iocb slot if there
1343 * is available slot in the ring, else it returns NULL.
1344 * If the get index of the ring is ahead of the put index, the function
1345 * will post an error attention event to the worker thread to take the
1346 * HBA to offline state.
1347 **/
dea31012005-04-17 16:05:31 -05001348static IOCB_t *
1349lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1350{
James Smart34b02dc2008-08-24 21:49:55 -04001351 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001352 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1353 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1354 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1355 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001356
James Smart7e56aa22012-08-03 12:35:34 -04001357 if (unlikely(pring->sli.sli3.local_getidx ==
1358 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001359
James Smart7e56aa22012-08-03 12:35:34 -04001360 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001361
James Smart7e56aa22012-08-03 12:35:34 -04001362 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001363 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001364 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001365 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001366 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001367 pring->sli.sli3.local_getidx,
1368 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001369
James Smart2e0fef82007-06-17 19:56:36 -05001370 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001371 /*
1372 * All error attention handlers are posted to
1373 * worker thread
1374 */
1375 phba->work_ha |= HA_ERATT;
1376 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001377
James Smart5e9d9b82008-06-14 22:52:53 -04001378 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001379
1380 return NULL;
1381 }
1382
James Smart7e56aa22012-08-03 12:35:34 -04001383 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001384 return NULL;
1385 }
1386
James Smarted957682007-06-17 19:56:37 -05001387 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001388}
1389
James Smarte59058c2008-08-24 21:49:00 -04001390/**
James Smart3621a712009-04-06 18:47:14 -04001391 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001392 * @phba: Pointer to HBA context object.
1393 * @iocbq: Pointer to driver iocb object.
1394 *
1395 * This function gets an iotag for the iocb. If there is no unused iotag and
1396 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1397 * array and assigns a new iotag.
1398 * The function returns the allocated iotag if successful, else returns zero.
1399 * Zero is not a valid iotag.
1400 * The caller is not required to hold any lock.
1401 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001402uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001403lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001404{
James Smart2e0fef82007-06-17 19:56:36 -05001405 struct lpfc_iocbq **new_arr;
1406 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001407 size_t new_len;
1408 struct lpfc_sli *psli = &phba->sli;
1409 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001410
James Smart2e0fef82007-06-17 19:56:36 -05001411 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001412 iotag = psli->last_iotag;
1413 if(++iotag < psli->iocbq_lookup_len) {
1414 psli->last_iotag = iotag;
1415 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001416 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001417 iocbq->iotag = iotag;
1418 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001419 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001420 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1421 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001422 spin_unlock_irq(&phba->hbalock);
1423 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001424 GFP_KERNEL);
1425 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001426 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001427 old_arr = psli->iocbq_lookup;
1428 if (new_len <= psli->iocbq_lookup_len) {
1429 /* highly unprobable case */
1430 kfree(new_arr);
1431 iotag = psli->last_iotag;
1432 if(++iotag < psli->iocbq_lookup_len) {
1433 psli->last_iotag = iotag;
1434 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001435 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001436 iocbq->iotag = iotag;
1437 return iotag;
1438 }
James Smart2e0fef82007-06-17 19:56:36 -05001439 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001440 return 0;
1441 }
1442 if (psli->iocbq_lookup)
1443 memcpy(new_arr, old_arr,
1444 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001445 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001446 psli->iocbq_lookup = new_arr;
1447 psli->iocbq_lookup_len = new_len;
1448 psli->last_iotag = iotag;
1449 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001450 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001451 iocbq->iotag = iotag;
1452 kfree(old_arr);
1453 return iotag;
1454 }
James Smart8f6d98d2006-08-01 07:34:00 -04001455 } else
James Smart2e0fef82007-06-17 19:56:36 -05001456 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001457
James Smartbc739052010-08-04 16:11:18 -04001458 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001459 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1460 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001461
James Bottomley604a3e32005-10-29 10:28:33 -05001462 return 0;
dea31012005-04-17 16:05:31 -05001463}
1464
James Smarte59058c2008-08-24 21:49:00 -04001465/**
James Smart3621a712009-04-06 18:47:14 -04001466 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001467 * @phba: Pointer to HBA context object.
1468 * @pring: Pointer to driver SLI ring object.
1469 * @iocb: Pointer to iocb slot in the ring.
1470 * @nextiocb: Pointer to driver iocb object which need to be
1471 * posted to firmware.
1472 *
1473 * This function is called with hbalock held to post a new iocb to
1474 * the firmware. This function copies the new iocb to ring iocb slot and
1475 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1476 * a completion call back for this iocb else the function will free the
1477 * iocb object.
1478 **/
dea31012005-04-17 16:05:31 -05001479static void
1480lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1481 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1482{
1483 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001484 * Set up an iotag
dea31012005-04-17 16:05:31 -05001485 */
James Bottomley604a3e32005-10-29 10:28:33 -05001486 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001487
James Smarte2a0a9d2008-12-04 22:40:02 -05001488
James Smarta58cbd52007-08-02 11:09:43 -04001489 if (pring->ringno == LPFC_ELS_RING) {
1490 lpfc_debugfs_slow_ring_trc(phba,
1491 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1492 *(((uint32_t *) &nextiocb->iocb) + 4),
1493 *(((uint32_t *) &nextiocb->iocb) + 6),
1494 *(((uint32_t *) &nextiocb->iocb) + 7));
1495 }
1496
dea31012005-04-17 16:05:31 -05001497 /*
1498 * Issue iocb command to adapter
1499 */
James Smart92d7f7b2007-06-17 19:56:38 -05001500 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001501 wmb();
1502 pring->stats.iocb_cmd++;
1503
1504 /*
1505 * If there is no completion routine to call, we can release the
1506 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1507 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1508 */
1509 if (nextiocb->iocb_cmpl)
1510 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001511 else
James Smart2e0fef82007-06-17 19:56:36 -05001512 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001513
1514 /*
1515 * Let the HBA know what IOCB slot will be the next one the
1516 * driver will put a command into.
1517 */
James Smart7e56aa22012-08-03 12:35:34 -04001518 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1519 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001520}
1521
James Smarte59058c2008-08-24 21:49:00 -04001522/**
James Smart3621a712009-04-06 18:47:14 -04001523 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001524 * @phba: Pointer to HBA context object.
1525 * @pring: Pointer to driver SLI ring object.
1526 *
1527 * The caller is not required to hold any lock for calling this function.
1528 * This function updates the chip attention bits for the ring to inform firmware
1529 * that there are pending work to be done for this ring and requests an
1530 * interrupt when there is space available in the ring. This function is
1531 * called when the driver is unable to post more iocbs to the ring due
1532 * to unavailability of space in the ring.
1533 **/
dea31012005-04-17 16:05:31 -05001534static void
James Smart2e0fef82007-06-17 19:56:36 -05001535lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001536{
1537 int ringno = pring->ringno;
1538
1539 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1540
1541 wmb();
1542
1543 /*
1544 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1545 * The HBA will tell us when an IOCB entry is available.
1546 */
1547 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1548 readl(phba->CAregaddr); /* flush */
1549
1550 pring->stats.iocb_cmd_full++;
1551}
1552
James Smarte59058c2008-08-24 21:49:00 -04001553/**
James Smart3621a712009-04-06 18:47:14 -04001554 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001555 * @phba: Pointer to HBA context object.
1556 * @pring: Pointer to driver SLI ring object.
1557 *
1558 * This function updates the chip attention register bit for the
1559 * given ring to inform HBA that there is more work to be done
1560 * in this ring. The caller is not required to hold any lock.
1561 **/
dea31012005-04-17 16:05:31 -05001562static void
James Smart2e0fef82007-06-17 19:56:36 -05001563lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001564{
1565 int ringno = pring->ringno;
1566
1567 /*
1568 * Tell the HBA that there is work to do in this ring.
1569 */
James Smart34b02dc2008-08-24 21:49:55 -04001570 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1571 wmb();
1572 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1573 readl(phba->CAregaddr); /* flush */
1574 }
dea31012005-04-17 16:05:31 -05001575}
1576
James Smarte59058c2008-08-24 21:49:00 -04001577/**
James Smart3621a712009-04-06 18:47:14 -04001578 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001579 * @phba: Pointer to HBA context object.
1580 * @pring: Pointer to driver SLI ring object.
1581 *
1582 * This function is called with hbalock held to post pending iocbs
1583 * in the txq to the firmware. This function is called when driver
1584 * detects space available in the ring.
1585 **/
dea31012005-04-17 16:05:31 -05001586static void
James Smart2e0fef82007-06-17 19:56:36 -05001587lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001588{
1589 IOCB_t *iocb;
1590 struct lpfc_iocbq *nextiocb;
1591
1592 /*
1593 * Check to see if:
1594 * (a) there is anything on the txq to send
1595 * (b) link is up
1596 * (c) link attention events can be processed (fcp ring only)
1597 * (d) IOCB processing is not blocked by the outstanding mbox command.
1598 */
1599 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001600 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001601 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001602 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001603
1604 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1605 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1606 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1607
1608 if (iocb)
1609 lpfc_sli_update_ring(phba, pring);
1610 else
1611 lpfc_sli_update_full_ring(phba, pring);
1612 }
1613
1614 return;
1615}
1616
James Smarte59058c2008-08-24 21:49:00 -04001617/**
James Smart3621a712009-04-06 18:47:14 -04001618 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001619 * @phba: Pointer to HBA context object.
1620 * @hbqno: HBQ number.
1621 *
1622 * This function is called with hbalock held to get the next
1623 * available slot for the given HBQ. If there is free slot
1624 * available for the HBQ it will return pointer to the next available
1625 * HBQ entry else it will return NULL.
1626 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001627static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001628lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1629{
1630 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1631
1632 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1633 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1634 hbqp->next_hbqPutIdx = 0;
1635
1636 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001637 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001638 uint32_t getidx = le32_to_cpu(raw_index);
1639
1640 hbqp->local_hbqGetIdx = getidx;
1641
1642 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1643 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001644 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001645 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001646 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001647 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001648 hbqp->entry_count);
1649
1650 phba->link_state = LPFC_HBA_ERROR;
1651 return NULL;
1652 }
1653
1654 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1655 return NULL;
1656 }
1657
James Smart51ef4c22007-08-02 11:10:31 -04001658 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1659 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001660}
1661
James Smarte59058c2008-08-24 21:49:00 -04001662/**
James Smart3621a712009-04-06 18:47:14 -04001663 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001664 * @phba: Pointer to HBA context object.
1665 *
1666 * This function is called with no lock held to free all the
1667 * hbq buffers while uninitializing the SLI interface. It also
1668 * frees the HBQ buffers returned by the firmware but not yet
1669 * processed by the upper layers.
1670 **/
James Smarted957682007-06-17 19:56:37 -05001671void
1672lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1673{
James Smart92d7f7b2007-06-17 19:56:38 -05001674 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1675 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001676 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001677 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001678 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001679
James Smart51ef4c22007-08-02 11:10:31 -04001680 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001681 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001682 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001683 for (i = 0; i < hbq_count; ++i) {
1684 list_for_each_entry_safe(dmabuf, next_dmabuf,
1685 &phba->hbqs[i].hbq_buffer_list, list) {
1686 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1687 list_del(&hbq_buf->dbuf.list);
1688 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1689 }
James Smarta8adb832007-10-27 13:37:53 -04001690 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001691 }
James Smart3163f722008-02-08 18:50:25 -05001692 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001693 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1694 list) {
James Smart3163f722008-02-08 18:50:25 -05001695 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1696 list_del(&hbq_buf->dbuf.list);
1697 if (hbq_buf->tag == -1) {
1698 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1699 (phba, hbq_buf);
1700 } else {
1701 hbqno = hbq_buf->tag >> 16;
1702 if (hbqno >= LPFC_MAX_HBQS)
1703 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1704 (phba, hbq_buf);
1705 else
1706 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1707 hbq_buf);
1708 }
1709 }
1710
1711 /* Mark the HBQs not in use */
1712 phba->hbq_in_use = 0;
1713 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001714}
1715
James Smarte59058c2008-08-24 21:49:00 -04001716/**
James Smart3621a712009-04-06 18:47:14 -04001717 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001718 * @phba: Pointer to HBA context object.
1719 * @hbqno: HBQ number.
1720 * @hbq_buf: Pointer to HBQ buffer.
1721 *
1722 * This function is called with the hbalock held to post a
1723 * hbq buffer to the firmware. If the function finds an empty
1724 * slot in the HBQ, it will post the buffer. The function will return
1725 * pointer to the hbq entry if it successfully post the buffer
1726 * else it will return NULL.
1727 **/
James Smart3772a992009-05-22 14:50:54 -04001728static int
James Smarted957682007-06-17 19:56:37 -05001729lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001730 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001731{
James Smart3772a992009-05-22 14:50:54 -04001732 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1733}
1734
1735/**
1736 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1737 * @phba: Pointer to HBA context object.
1738 * @hbqno: HBQ number.
1739 * @hbq_buf: Pointer to HBQ buffer.
1740 *
1741 * This function is called with the hbalock held to post a hbq buffer to the
1742 * firmware. If the function finds an empty slot in the HBQ, it will post the
1743 * buffer and place it on the hbq_buffer_list. The function will return zero if
1744 * it successfully post the buffer else it will return an error.
1745 **/
1746static int
1747lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1748 struct hbq_dmabuf *hbq_buf)
1749{
James Smarted957682007-06-17 19:56:37 -05001750 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001751 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001752
1753 /* Get next HBQ entry slot to use */
1754 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1755 if (hbqe) {
1756 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1757
James Smart92d7f7b2007-06-17 19:56:38 -05001758 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1759 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001760 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001761 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001762 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1763 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1764 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001765 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1766 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001767 /* flush */
James Smarted957682007-06-17 19:56:37 -05001768 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001769 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001770 return 0;
1771 } else
1772 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001773}
1774
James Smart4f774512009-05-22 14:52:35 -04001775/**
1776 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1777 * @phba: Pointer to HBA context object.
1778 * @hbqno: HBQ number.
1779 * @hbq_buf: Pointer to HBQ buffer.
1780 *
1781 * This function is called with the hbalock held to post an RQE to the SLI4
1782 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1783 * the hbq_buffer_list and return zero, otherwise it will return an error.
1784 **/
1785static int
1786lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1787 struct hbq_dmabuf *hbq_buf)
1788{
1789 int rc;
1790 struct lpfc_rqe hrqe;
1791 struct lpfc_rqe drqe;
1792
1793 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1794 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1795 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1796 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1797 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1798 &hrqe, &drqe);
1799 if (rc < 0)
1800 return rc;
1801 hbq_buf->tag = rc;
1802 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1803 return 0;
1804}
1805
James Smarte59058c2008-08-24 21:49:00 -04001806/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001807static struct lpfc_hbq_init lpfc_els_hbq = {
1808 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001809 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001810 .mask_count = 0,
1811 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001812 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001813 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001814 .init_count = 40,
1815 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001816};
James Smarted957682007-06-17 19:56:37 -05001817
James Smarte59058c2008-08-24 21:49:00 -04001818/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001819static struct lpfc_hbq_init lpfc_extra_hbq = {
1820 .rn = 1,
1821 .entry_count = 200,
1822 .mask_count = 0,
1823 .profile = 0,
1824 .ring_mask = (1 << LPFC_EXTRA_RING),
1825 .buffer_count = 0,
1826 .init_count = 0,
1827 .add_count = 5,
1828};
1829
James Smarte59058c2008-08-24 21:49:00 -04001830/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001831struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001832 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001833 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001834};
1835
James Smarte59058c2008-08-24 21:49:00 -04001836/**
James Smart3621a712009-04-06 18:47:14 -04001837 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001838 * @phba: Pointer to HBA context object.
1839 * @hbqno: HBQ number.
1840 * @count: Number of HBQ buffers to be posted.
1841 *
James Smartd7c255b2008-08-24 21:50:00 -04001842 * This function is called with no lock held to post more hbq buffers to the
1843 * given HBQ. The function returns the number of HBQ buffers successfully
1844 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001845 **/
James Smart311464e2007-08-02 11:10:37 -04001846static int
James Smart92d7f7b2007-06-17 19:56:38 -05001847lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1848{
James Smartd7c255b2008-08-24 21:50:00 -04001849 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001850 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001851 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001852 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001853 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001854 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001855
James Smartd7c255b2008-08-24 21:50:00 -04001856 if ((phba->hbqs[hbqno].buffer_count + count) >
1857 lpfc_hbq_defs[hbqno]->entry_count)
1858 count = lpfc_hbq_defs[hbqno]->entry_count -
1859 phba->hbqs[hbqno].buffer_count;
1860 if (!count)
1861 return 0;
1862 /* Allocate HBQ entries */
1863 for (i = 0; i < count; i++) {
1864 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1865 if (!hbq_buffer)
1866 break;
1867 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1868 }
James Smart3163f722008-02-08 18:50:25 -05001869 /* Check whether HBQ is still in use */
1870 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001871 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001872 goto err;
1873 while (!list_empty(&hbq_buf_list)) {
1874 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1875 dbuf.list);
1876 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1877 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001878 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001879 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001880 posted++;
1881 } else
James Smart51ef4c22007-08-02 11:10:31 -04001882 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001883 }
James Smart3163f722008-02-08 18:50:25 -05001884 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001885 return posted;
1886err:
1887 spin_unlock_irqrestore(&phba->hbalock, flags);
1888 while (!list_empty(&hbq_buf_list)) {
1889 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1890 dbuf.list);
1891 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1892 }
James Smart92d7f7b2007-06-17 19:56:38 -05001893 return 0;
James Smarted957682007-06-17 19:56:37 -05001894}
1895
James Smarte59058c2008-08-24 21:49:00 -04001896/**
James Smart3621a712009-04-06 18:47:14 -04001897 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001898 * @phba: Pointer to HBA context object.
1899 * @qno: HBQ number.
1900 *
1901 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001902 * is called with no lock held. The function returns the number of HBQ entries
1903 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001904 **/
James Smarted957682007-06-17 19:56:37 -05001905int
James Smart92d7f7b2007-06-17 19:56:38 -05001906lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001907{
James Smartdef9c7a2009-12-21 17:02:28 -05001908 if (phba->sli_rev == LPFC_SLI_REV4)
1909 return 0;
1910 else
1911 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1912 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001913}
1914
James Smarte59058c2008-08-24 21:49:00 -04001915/**
James Smart3621a712009-04-06 18:47:14 -04001916 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001917 * @phba: Pointer to HBA context object.
1918 * @qno: HBQ queue number.
1919 *
1920 * This function is called from SLI initialization code path with
1921 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001922 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001923 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001924static int
James Smart92d7f7b2007-06-17 19:56:38 -05001925lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001926{
James Smartdef9c7a2009-12-21 17:02:28 -05001927 if (phba->sli_rev == LPFC_SLI_REV4)
1928 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001929 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001930 else
1931 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1932 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001933}
1934
James Smarte59058c2008-08-24 21:49:00 -04001935/**
James Smart3772a992009-05-22 14:50:54 -04001936 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1937 * @phba: Pointer to HBA context object.
1938 * @hbqno: HBQ number.
1939 *
1940 * This function removes the first hbq buffer on an hbq list and returns a
1941 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1942 **/
1943static struct hbq_dmabuf *
1944lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1945{
1946 struct lpfc_dmabuf *d_buf;
1947
1948 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1949 if (!d_buf)
1950 return NULL;
1951 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1952}
1953
1954/**
James Smart3621a712009-04-06 18:47:14 -04001955 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001956 * @phba: Pointer to HBA context object.
1957 * @tag: Tag of the hbq buffer.
1958 *
1959 * This function is called with hbalock held. This function searches
1960 * for the hbq buffer associated with the given tag in the hbq buffer
1961 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1962 * it returns NULL.
1963 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001964static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001965lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1966{
James Smart92d7f7b2007-06-17 19:56:38 -05001967 struct lpfc_dmabuf *d_buf;
1968 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001969 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001970
James Smart51ef4c22007-08-02 11:10:31 -04001971 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001972 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001973 return NULL;
1974
James Smart3772a992009-05-22 14:50:54 -04001975 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001976 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001977 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001978 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001979 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001980 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001981 }
1982 }
James Smart3772a992009-05-22 14:50:54 -04001983 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001985 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001986 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001987 return NULL;
James Smarted957682007-06-17 19:56:37 -05001988}
1989
James Smarte59058c2008-08-24 21:49:00 -04001990/**
James Smart3621a712009-04-06 18:47:14 -04001991 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001992 * @phba: Pointer to HBA context object.
1993 * @hbq_buffer: Pointer to HBQ buffer.
1994 *
1995 * This function is called with hbalock. This function gives back
1996 * the hbq buffer to firmware. If the HBQ does not have space to
1997 * post the buffer, it will free the buffer.
1998 **/
James Smarted957682007-06-17 19:56:37 -05001999void
James Smart51ef4c22007-08-02 11:10:31 -04002000lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002001{
2002 uint32_t hbqno;
2003
James Smart51ef4c22007-08-02 11:10:31 -04002004 if (hbq_buffer) {
2005 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002006 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002007 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002008 }
2009}
2010
James Smarte59058c2008-08-24 21:49:00 -04002011/**
James Smart3621a712009-04-06 18:47:14 -04002012 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002013 * @mbxCommand: mailbox command code.
2014 *
2015 * This function is called by the mailbox event handler function to verify
2016 * that the completed mailbox command is a legitimate mailbox command. If the
2017 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2018 * and the mailbox event handler will take the HBA offline.
2019 **/
dea31012005-04-17 16:05:31 -05002020static int
2021lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2022{
2023 uint8_t ret;
2024
2025 switch (mbxCommand) {
2026 case MBX_LOAD_SM:
2027 case MBX_READ_NV:
2028 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002029 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002030 case MBX_RUN_BIU_DIAG:
2031 case MBX_INIT_LINK:
2032 case MBX_DOWN_LINK:
2033 case MBX_CONFIG_LINK:
2034 case MBX_CONFIG_RING:
2035 case MBX_RESET_RING:
2036 case MBX_READ_CONFIG:
2037 case MBX_READ_RCONFIG:
2038 case MBX_READ_SPARM:
2039 case MBX_READ_STATUS:
2040 case MBX_READ_RPI:
2041 case MBX_READ_XRI:
2042 case MBX_READ_REV:
2043 case MBX_READ_LNK_STAT:
2044 case MBX_REG_LOGIN:
2045 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002046 case MBX_CLEAR_LA:
2047 case MBX_DUMP_MEMORY:
2048 case MBX_DUMP_CONTEXT:
2049 case MBX_RUN_DIAGS:
2050 case MBX_RESTART:
2051 case MBX_UPDATE_CFG:
2052 case MBX_DOWN_LOAD:
2053 case MBX_DEL_LD_ENTRY:
2054 case MBX_RUN_PROGRAM:
2055 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002056 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002057 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002058 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002059 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002060 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002061 case MBX_LOAD_AREA:
2062 case MBX_RUN_BIU_DIAG64:
2063 case MBX_CONFIG_PORT:
2064 case MBX_READ_SPARM64:
2065 case MBX_READ_RPI64:
2066 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002067 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002068 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002069 case MBX_SET_DEBUG:
2070 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002071 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002072 case MBX_REG_VPI:
2073 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002074 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002075 case MBX_PORT_CAPABILITIES:
2076 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002077 case MBX_SLI4_CONFIG:
2078 case MBX_SLI4_REQ_FTRS:
2079 case MBX_REG_FCFI:
2080 case MBX_UNREG_FCFI:
2081 case MBX_REG_VFI:
2082 case MBX_UNREG_VFI:
2083 case MBX_INIT_VPI:
2084 case MBX_INIT_VFI:
2085 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002086 case MBX_READ_EVENT_LOG_STATUS:
2087 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002088 case MBX_SECURITY_MGMT:
2089 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002090 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002091 ret = mbxCommand;
2092 break;
2093 default:
2094 ret = MBX_SHUTDOWN;
2095 break;
2096 }
James Smart2e0fef82007-06-17 19:56:36 -05002097 return ret;
dea31012005-04-17 16:05:31 -05002098}
James Smarte59058c2008-08-24 21:49:00 -04002099
2100/**
James Smart3621a712009-04-06 18:47:14 -04002101 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002102 * @phba: Pointer to HBA context object.
2103 * @pmboxq: Pointer to mailbox command.
2104 *
2105 * This is completion handler function for mailbox commands issued from
2106 * lpfc_sli_issue_mbox_wait function. This function is called by the
2107 * mailbox event handler function with no lock held. This function
2108 * will wake up thread waiting on the wait queue pointed by context1
2109 * of the mailbox.
2110 **/
James Smart04c68492009-05-22 14:52:52 -04002111void
James Smart2e0fef82007-06-17 19:56:36 -05002112lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002113{
2114 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002115 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002116
2117 /*
2118 * If pdone_q is empty, the driver thread gave up waiting and
2119 * continued running.
2120 */
James Smart7054a602007-04-25 09:52:34 -04002121 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002122 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002123 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2124 if (pdone_q)
2125 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002126 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002127 return;
2128}
2129
James Smarte59058c2008-08-24 21:49:00 -04002130
2131/**
James Smart3621a712009-04-06 18:47:14 -04002132 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002133 * @phba: Pointer to HBA context object.
2134 * @pmb: Pointer to mailbox object.
2135 *
2136 * This function is the default mailbox completion handler. It
2137 * frees the memory resources associated with the completed mailbox
2138 * command. If the completed command is a REG_LOGIN mailbox command,
2139 * this function will issue a UREG_LOGIN to re-claim the RPI.
2140 **/
dea31012005-04-17 16:05:31 -05002141void
James Smart2e0fef82007-06-17 19:56:36 -05002142lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002143{
James Smartd439d282010-09-29 11:18:45 -04002144 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002145 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002146 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002147 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002148 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002149 int rc;
2150
dea31012005-04-17 16:05:31 -05002151 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002152
dea31012005-04-17 16:05:31 -05002153 if (mp) {
2154 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2155 kfree(mp);
2156 }
James Smart7054a602007-04-25 09:52:34 -04002157
2158 /*
2159 * If a REG_LOGIN succeeded after node is destroyed or node
2160 * is in re-discovery driver need to cleanup the RPI.
2161 */
James Smart2e0fef82007-06-17 19:56:36 -05002162 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002163 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2164 !pmb->u.mb.mbxStatus) {
2165 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002166 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002167 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002168 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002169 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2170 if (rc != MBX_NOT_FINISHED)
2171 return;
2172 }
2173
James Smart695a8142010-01-26 23:08:03 -05002174 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2175 !(phba->pport->load_flag & FC_UNLOADING) &&
2176 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002177 shost = lpfc_shost_from_vport(vport);
2178 spin_lock_irq(shost->host_lock);
2179 vport->vpi_state |= LPFC_VPI_REGISTERED;
2180 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2181 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002182 }
2183
James Smartd439d282010-09-29 11:18:45 -04002184 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2185 ndlp = (struct lpfc_nodelist *)pmb->context2;
2186 lpfc_nlp_put(ndlp);
2187 pmb->context2 = NULL;
2188 }
2189
James Smartdcf2a4e2010-09-29 11:18:53 -04002190 /* Check security permission status on INIT_LINK mailbox command */
2191 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2192 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2193 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2194 "2860 SLI authentication is required "
2195 "for INIT_LINK but has not done yet\n");
2196
James Smart04c68492009-05-22 14:52:52 -04002197 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2198 lpfc_sli4_mbox_cmd_free(phba, pmb);
2199 else
2200 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002201}
2202
James Smarte59058c2008-08-24 21:49:00 -04002203/**
James Smart3621a712009-04-06 18:47:14 -04002204 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002205 * @phba: Pointer to HBA context object.
2206 *
2207 * This function is called with no lock held. This function processes all
2208 * the completed mailbox commands and gives it to upper layers. The interrupt
2209 * service routine processes mailbox completion interrupt and adds completed
2210 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2211 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2212 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2213 * function returns the mailbox commands to the upper layer by calling the
2214 * completion handler function of each mailbox.
2215 **/
dea31012005-04-17 16:05:31 -05002216int
James Smart2e0fef82007-06-17 19:56:36 -05002217lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002218{
James Smart92d7f7b2007-06-17 19:56:38 -05002219 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002220 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002221 int rc;
2222 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002223
2224 phba->sli.slistat.mbox_event++;
2225
James Smart92d7f7b2007-06-17 19:56:38 -05002226 /* Get all completed mailboxe buffers into the cmplq */
2227 spin_lock_irq(&phba->hbalock);
2228 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2229 spin_unlock_irq(&phba->hbalock);
2230
dea31012005-04-17 16:05:31 -05002231 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002232 do {
2233 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2234 if (pmb == NULL)
2235 break;
2236
James Smart04c68492009-05-22 14:52:52 -04002237 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002238
James Smart858c9f62007-06-17 19:56:39 -05002239 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2240 if (pmb->vport) {
2241 lpfc_debugfs_disc_trc(pmb->vport,
2242 LPFC_DISC_TRC_MBOX_VPORT,
2243 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2244 (uint32_t)pmbox->mbxCommand,
2245 pmbox->un.varWords[0],
2246 pmbox->un.varWords[1]);
2247 }
2248 else {
2249 lpfc_debugfs_disc_trc(phba->pport,
2250 LPFC_DISC_TRC_MBOX,
2251 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2252 (uint32_t)pmbox->mbxCommand,
2253 pmbox->un.varWords[0],
2254 pmbox->un.varWords[1]);
2255 }
2256 }
2257
dea31012005-04-17 16:05:31 -05002258 /*
2259 * It is a fatal error if unknown mbox command completion.
2260 */
2261 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2262 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002263 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002264 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002265 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002266 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002267 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002268 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002269 lpfc_sli_config_mbox_subsys_get(phba,
2270 pmb),
2271 lpfc_sli_config_mbox_opcode_get(phba,
2272 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002273 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002274 phba->work_hs = HS_FFER3;
2275 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002276 continue;
dea31012005-04-17 16:05:31 -05002277 }
2278
dea31012005-04-17 16:05:31 -05002279 if (pmbox->mbxStatus) {
2280 phba->sli.slistat.mbox_stat_err++;
2281 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2282 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002283 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002284 LOG_MBOX | LOG_SLI,
2285 "(%d):0305 Mbox cmd cmpl "
2286 "error - RETRYing Data: x%x "
2287 "(x%x/x%x) x%x x%x x%x\n",
2288 pmb->vport ? pmb->vport->vpi : 0,
2289 pmbox->mbxCommand,
2290 lpfc_sli_config_mbox_subsys_get(phba,
2291 pmb),
2292 lpfc_sli_config_mbox_opcode_get(phba,
2293 pmb),
2294 pmbox->mbxStatus,
2295 pmbox->un.varWords[0],
2296 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002297 pmbox->mbxStatus = 0;
2298 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002299 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002300 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002301 continue;
dea31012005-04-17 16:05:31 -05002302 }
2303 }
2304
2305 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002306 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002307 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002308 "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 -05002309 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002310 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002311 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2312 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002313 pmb->mbox_cmpl,
2314 *((uint32_t *) pmbox),
2315 pmbox->un.varWords[0],
2316 pmbox->un.varWords[1],
2317 pmbox->un.varWords[2],
2318 pmbox->un.varWords[3],
2319 pmbox->un.varWords[4],
2320 pmbox->un.varWords[5],
2321 pmbox->un.varWords[6],
2322 pmbox->un.varWords[7]);
2323
James Smart92d7f7b2007-06-17 19:56:38 -05002324 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002325 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002326 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002327 return 0;
dea31012005-04-17 16:05:31 -05002328}
James Smart92d7f7b2007-06-17 19:56:38 -05002329
James Smarte59058c2008-08-24 21:49:00 -04002330/**
James Smart3621a712009-04-06 18:47:14 -04002331 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002332 * @phba: Pointer to HBA context object.
2333 * @pring: Pointer to driver SLI ring object.
2334 * @tag: buffer tag.
2335 *
2336 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2337 * is set in the tag the buffer is posted for a particular exchange,
2338 * the function will return the buffer without replacing the buffer.
2339 * If the buffer is for unsolicited ELS or CT traffic, this function
2340 * returns the buffer and also posts another buffer to the firmware.
2341 **/
James Smart76bb24e2007-10-27 13:38:00 -04002342static struct lpfc_dmabuf *
2343lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002344 struct lpfc_sli_ring *pring,
2345 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002346{
James Smart9f1e1b52008-12-04 22:39:40 -05002347 struct hbq_dmabuf *hbq_entry;
2348
James Smart76bb24e2007-10-27 13:38:00 -04002349 if (tag & QUE_BUFTAG_BIT)
2350 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002351 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2352 if (!hbq_entry)
2353 return NULL;
2354 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002355}
James Smart57127f12007-10-27 13:37:05 -04002356
James Smart3772a992009-05-22 14:50:54 -04002357/**
2358 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2359 * @phba: Pointer to HBA context object.
2360 * @pring: Pointer to driver SLI ring object.
2361 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2362 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2363 * @fch_type: the type for the first frame of the sequence.
2364 *
2365 * This function is called with no lock held. This function uses the r_ctl and
2366 * type of the received sequence to find the correct callback function to call
2367 * to process the sequence.
2368 **/
2369static int
2370lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2371 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2372 uint32_t fch_type)
2373{
2374 int i;
2375
2376 /* unSolicited Responses */
2377 if (pring->prt[0].profile) {
2378 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2379 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2380 saveq);
2381 return 1;
2382 }
2383 /* We must search, based on rctl / type
2384 for the right routine */
2385 for (i = 0; i < pring->num_mask; i++) {
2386 if ((pring->prt[i].rctl == fch_r_ctl) &&
2387 (pring->prt[i].type == fch_type)) {
2388 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2389 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2390 (phba, pring, saveq);
2391 return 1;
2392 }
2393 }
2394 return 0;
2395}
James Smarte59058c2008-08-24 21:49:00 -04002396
2397/**
James Smart3621a712009-04-06 18:47:14 -04002398 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002399 * @phba: Pointer to HBA context object.
2400 * @pring: Pointer to driver SLI ring object.
2401 * @saveq: Pointer to the unsolicited iocb.
2402 *
2403 * This function is called with no lock held by the ring event handler
2404 * when there is an unsolicited iocb posted to the response ring by the
2405 * firmware. This function gets the buffer associated with the iocbs
2406 * and calls the event handler for the ring. This function handles both
2407 * qring buffers and hbq buffers.
2408 * When the function returns 1 the caller can free the iocb object otherwise
2409 * upper layer functions will free the iocb objects.
2410 **/
dea31012005-04-17 16:05:31 -05002411static int
2412lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2413 struct lpfc_iocbq *saveq)
2414{
2415 IOCB_t * irsp;
2416 WORD5 * w5p;
2417 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002418 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002419 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002420 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002421
2422 match = 0;
2423 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002424
2425 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2426 if (pring->lpfc_sli_rcv_async_status)
2427 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2428 else
2429 lpfc_printf_log(phba,
2430 KERN_WARNING,
2431 LOG_SLI,
2432 "0316 Ring %d handler: unexpected "
2433 "ASYNC_STATUS iocb received evt_code "
2434 "0x%x\n",
2435 pring->ringno,
2436 irsp->un.asyncstat.evt_code);
2437 return 1;
2438 }
2439
James Smart3163f722008-02-08 18:50:25 -05002440 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2441 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2442 if (irsp->ulpBdeCount > 0) {
2443 dmzbuf = lpfc_sli_get_buff(phba, pring,
2444 irsp->un.ulpWord[3]);
2445 lpfc_in_buf_free(phba, dmzbuf);
2446 }
2447
2448 if (irsp->ulpBdeCount > 1) {
2449 dmzbuf = lpfc_sli_get_buff(phba, pring,
2450 irsp->unsli3.sli3Words[3]);
2451 lpfc_in_buf_free(phba, dmzbuf);
2452 }
2453
2454 if (irsp->ulpBdeCount > 2) {
2455 dmzbuf = lpfc_sli_get_buff(phba, pring,
2456 irsp->unsli3.sli3Words[7]);
2457 lpfc_in_buf_free(phba, dmzbuf);
2458 }
2459
2460 return 1;
2461 }
2462
James Smart92d7f7b2007-06-17 19:56:38 -05002463 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002464 if (irsp->ulpBdeCount != 0) {
2465 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002466 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002467 if (!saveq->context2)
2468 lpfc_printf_log(phba,
2469 KERN_ERR,
2470 LOG_SLI,
2471 "0341 Ring %d Cannot find buffer for "
2472 "an unsolicited iocb. tag 0x%x\n",
2473 pring->ringno,
2474 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002475 }
2476 if (irsp->ulpBdeCount == 2) {
2477 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002478 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002479 if (!saveq->context3)
2480 lpfc_printf_log(phba,
2481 KERN_ERR,
2482 LOG_SLI,
2483 "0342 Ring %d Cannot find buffer for an"
2484 " unsolicited iocb. tag 0x%x\n",
2485 pring->ringno,
2486 irsp->unsli3.sli3Words[7]);
2487 }
2488 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002489 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002490 if (irsp->ulpBdeCount != 0) {
2491 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2492 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002493 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002494 lpfc_printf_log(phba,
2495 KERN_ERR,
2496 LOG_SLI,
2497 "0343 Ring %d Cannot find "
2498 "buffer for an unsolicited iocb"
2499 ". tag 0x%x\n", pring->ringno,
2500 irsp->un.ulpWord[3]);
2501 }
2502 if (irsp->ulpBdeCount == 2) {
2503 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2504 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002505 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002506 lpfc_printf_log(phba,
2507 KERN_ERR,
2508 LOG_SLI,
2509 "0344 Ring %d Cannot find "
2510 "buffer for an unsolicited "
2511 "iocb. tag 0x%x\n",
2512 pring->ringno,
2513 irsp->unsli3.sli3Words[7]);
2514 }
2515 }
James Smart92d7f7b2007-06-17 19:56:38 -05002516 }
James Smart9c2face2008-01-11 01:53:18 -05002517 if (irsp->ulpBdeCount != 0 &&
2518 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2519 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2520 int found = 0;
2521
2522 /* search continue save q for same XRI */
2523 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002524 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2525 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002526 list_add_tail(&saveq->list, &iocbq->list);
2527 found = 1;
2528 break;
2529 }
2530 }
2531 if (!found)
2532 list_add_tail(&saveq->clist,
2533 &pring->iocb_continue_saveq);
2534 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2535 list_del_init(&iocbq->clist);
2536 saveq = iocbq;
2537 irsp = &(saveq->iocb);
2538 } else
2539 return 0;
2540 }
2541 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2542 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2543 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002544 Rctl = FC_RCTL_ELS_REQ;
2545 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002546 } else {
2547 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2548 Rctl = w5p->hcsw.Rctl;
2549 Type = w5p->hcsw.Type;
2550
2551 /* Firmware Workaround */
2552 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2553 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2554 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002555 Rctl = FC_RCTL_ELS_REQ;
2556 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002557 w5p->hcsw.Rctl = Rctl;
2558 w5p->hcsw.Type = Type;
2559 }
2560 }
James Smart92d7f7b2007-06-17 19:56:38 -05002561
James Smart3772a992009-05-22 14:50:54 -04002562 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002563 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002564 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002565 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002566 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002567
James Smart92d7f7b2007-06-17 19:56:38 -05002568 return 1;
dea31012005-04-17 16:05:31 -05002569}
2570
James Smarte59058c2008-08-24 21:49:00 -04002571/**
James Smart3621a712009-04-06 18:47:14 -04002572 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002573 * @phba: Pointer to HBA context object.
2574 * @pring: Pointer to driver SLI ring object.
2575 * @prspiocb: Pointer to response iocb object.
2576 *
2577 * This function looks up the iocb_lookup table to get the command iocb
2578 * corresponding to the given response iocb using the iotag of the
2579 * response iocb. This function is called with the hbalock held.
2580 * This function returns the command iocb object if it finds the command
2581 * iocb else returns NULL.
2582 **/
dea31012005-04-17 16:05:31 -05002583static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002584lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2585 struct lpfc_sli_ring *pring,
2586 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002587{
dea31012005-04-17 16:05:31 -05002588 struct lpfc_iocbq *cmd_iocb = NULL;
2589 uint16_t iotag;
2590
James Bottomley604a3e32005-10-29 10:28:33 -05002591 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002592
James Bottomley604a3e32005-10-29 10:28:33 -05002593 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2594 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002595 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002596 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart2a9bf3d2010-06-07 15:24:45 -04002597 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002598 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002599 }
James Bottomley604a3e32005-10-29 10:28:33 -05002600 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002601 }
2602
dea31012005-04-17 16:05:31 -05002603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002604 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002605 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002606 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002607 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002608 return NULL;
2609}
2610
James Smarte59058c2008-08-24 21:49:00 -04002611/**
James Smart3772a992009-05-22 14:50:54 -04002612 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2613 * @phba: Pointer to HBA context object.
2614 * @pring: Pointer to driver SLI ring object.
2615 * @iotag: IOCB tag.
2616 *
2617 * This function looks up the iocb_lookup table to get the command iocb
2618 * corresponding to the given iotag. This function is called with the
2619 * hbalock held.
2620 * This function returns the command iocb object if it finds the command
2621 * iocb else returns NULL.
2622 **/
2623static struct lpfc_iocbq *
2624lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2625 struct lpfc_sli_ring *pring, uint16_t iotag)
2626{
2627 struct lpfc_iocbq *cmd_iocb;
2628
2629 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2630 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002631 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2632 /* remove from txcmpl queue list */
2633 list_del_init(&cmd_iocb->list);
2634 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002635 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002636 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002637 }
James Smart3772a992009-05-22 14:50:54 -04002638 }
James Smart3772a992009-05-22 14:50:54 -04002639 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2640 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2641 iotag, phba->sli.last_iotag);
2642 return NULL;
2643}
2644
2645/**
James Smart3621a712009-04-06 18:47:14 -04002646 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002647 * @phba: Pointer to HBA context object.
2648 * @pring: Pointer to driver SLI ring object.
2649 * @saveq: Pointer to the response iocb to be processed.
2650 *
2651 * This function is called by the ring event handler for non-fcp
2652 * rings when there is a new response iocb in the response ring.
2653 * The caller is not required to hold any locks. This function
2654 * gets the command iocb associated with the response iocb and
2655 * calls the completion handler for the command iocb. If there
2656 * is no completion handler, the function will free the resources
2657 * associated with command iocb. If the response iocb is for
2658 * an already aborted command iocb, the status of the completion
2659 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2660 * This function always returns 1.
2661 **/
dea31012005-04-17 16:05:31 -05002662static int
James Smart2e0fef82007-06-17 19:56:36 -05002663lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002664 struct lpfc_iocbq *saveq)
2665{
James Smart2e0fef82007-06-17 19:56:36 -05002666 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002667 int rc = 1;
2668 unsigned long iflag;
2669
2670 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002671 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002672 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002673 spin_unlock_irqrestore(&phba->hbalock, iflag);
2674
dea31012005-04-17 16:05:31 -05002675 if (cmdiocbp) {
2676 if (cmdiocbp->iocb_cmpl) {
2677 /*
James Smartea2151b2008-09-07 11:52:10 -04002678 * If an ELS command failed send an event to mgmt
2679 * application.
2680 */
2681 if (saveq->iocb.ulpStatus &&
2682 (pring->ringno == LPFC_ELS_RING) &&
2683 (cmdiocbp->iocb.ulpCommand ==
2684 CMD_ELS_REQUEST64_CR))
2685 lpfc_send_els_failure_event(phba,
2686 cmdiocbp, saveq);
2687
2688 /*
dea31012005-04-17 16:05:31 -05002689 * Post all ELS completions to the worker thread.
2690 * All other are passed to the completion callback.
2691 */
2692 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002693 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2694 (cmdiocbp->iocb_flag &
2695 LPFC_DRIVER_ABORTED)) {
2696 spin_lock_irqsave(&phba->hbalock,
2697 iflag);
James Smart07951072007-04-25 09:51:38 -04002698 cmdiocbp->iocb_flag &=
2699 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002700 spin_unlock_irqrestore(&phba->hbalock,
2701 iflag);
James Smart07951072007-04-25 09:51:38 -04002702 saveq->iocb.ulpStatus =
2703 IOSTAT_LOCAL_REJECT;
2704 saveq->iocb.un.ulpWord[4] =
2705 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002706
2707 /* Firmware could still be in progress
2708 * of DMAing payload, so don't free data
2709 * buffer till after a hbeat.
2710 */
James Smart341af102010-01-26 23:07:37 -05002711 spin_lock_irqsave(&phba->hbalock,
2712 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002713 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002714 spin_unlock_irqrestore(&phba->hbalock,
2715 iflag);
2716 }
James Smart0f65ff62010-02-26 14:14:23 -05002717 if (phba->sli_rev == LPFC_SLI_REV4) {
2718 if (saveq->iocb_flag &
2719 LPFC_EXCHANGE_BUSY) {
2720 /* Set cmdiocb flag for the
2721 * exchange busy so sgl (xri)
2722 * will not be released until
2723 * the abort xri is received
2724 * from hba.
2725 */
2726 spin_lock_irqsave(
2727 &phba->hbalock, iflag);
2728 cmdiocbp->iocb_flag |=
2729 LPFC_EXCHANGE_BUSY;
2730 spin_unlock_irqrestore(
2731 &phba->hbalock, iflag);
2732 }
2733 if (cmdiocbp->iocb_flag &
2734 LPFC_DRIVER_ABORTED) {
2735 /*
2736 * Clear LPFC_DRIVER_ABORTED
2737 * bit in case it was driver
2738 * initiated abort.
2739 */
2740 spin_lock_irqsave(
2741 &phba->hbalock, iflag);
2742 cmdiocbp->iocb_flag &=
2743 ~LPFC_DRIVER_ABORTED;
2744 spin_unlock_irqrestore(
2745 &phba->hbalock, iflag);
2746 cmdiocbp->iocb.ulpStatus =
2747 IOSTAT_LOCAL_REJECT;
2748 cmdiocbp->iocb.un.ulpWord[4] =
2749 IOERR_ABORT_REQUESTED;
2750 /*
2751 * For SLI4, irsiocb contains
2752 * NO_XRI in sli_xritag, it
2753 * shall not affect releasing
2754 * sgl (xri) process.
2755 */
2756 saveq->iocb.ulpStatus =
2757 IOSTAT_LOCAL_REJECT;
2758 saveq->iocb.un.ulpWord[4] =
2759 IOERR_SLI_ABORTED;
2760 spin_lock_irqsave(
2761 &phba->hbalock, iflag);
2762 saveq->iocb_flag |=
2763 LPFC_DELAY_MEM_FREE;
2764 spin_unlock_irqrestore(
2765 &phba->hbalock, iflag);
2766 }
James Smart07951072007-04-25 09:51:38 -04002767 }
dea31012005-04-17 16:05:31 -05002768 }
James Smart2e0fef82007-06-17 19:56:36 -05002769 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002770 } else
2771 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002772 } else {
2773 /*
2774 * Unknown initiating command based on the response iotag.
2775 * This could be the case on the ELS ring because of
2776 * lpfc_els_abort().
2777 */
2778 if (pring->ringno != LPFC_ELS_RING) {
2779 /*
2780 * Ring <ringno> handler: unexpected completion IoTag
2781 * <IoTag>
2782 */
James Smarta257bf92009-04-06 18:48:10 -04002783 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002784 "0322 Ring %d handler: "
2785 "unexpected completion IoTag x%x "
2786 "Data: x%x x%x x%x x%x\n",
2787 pring->ringno,
2788 saveq->iocb.ulpIoTag,
2789 saveq->iocb.ulpStatus,
2790 saveq->iocb.un.ulpWord[4],
2791 saveq->iocb.ulpCommand,
2792 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002793 }
2794 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002795
dea31012005-04-17 16:05:31 -05002796 return rc;
2797}
2798
James Smarte59058c2008-08-24 21:49:00 -04002799/**
James Smart3621a712009-04-06 18:47:14 -04002800 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002801 * @phba: Pointer to HBA context object.
2802 * @pring: Pointer to driver SLI ring object.
2803 *
2804 * This function is called from the iocb ring event handlers when
2805 * put pointer is ahead of the get pointer for a ring. This function signal
2806 * an error attention condition to the worker thread and the worker
2807 * thread will transition the HBA to offline state.
2808 **/
James Smart2e0fef82007-06-17 19:56:36 -05002809static void
2810lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002811{
James Smart34b02dc2008-08-24 21:49:55 -04002812 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002813 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002814 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002815 * rsp ring <portRspMax>
2816 */
2817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002818 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002819 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002820 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002821 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002822
James Smart2e0fef82007-06-17 19:56:36 -05002823 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002824
2825 /*
2826 * All error attention handlers are posted to
2827 * worker thread
2828 */
2829 phba->work_ha |= HA_ERATT;
2830 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002831
James Smart5e9d9b82008-06-14 22:52:53 -04002832 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002833
2834 return;
2835}
2836
James Smarte59058c2008-08-24 21:49:00 -04002837/**
James Smart3621a712009-04-06 18:47:14 -04002838 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002839 * @ptr: Pointer to address of HBA context object.
2840 *
2841 * This function is invoked by the Error Attention polling timer when the
2842 * timer times out. It will check the SLI Error Attention register for
2843 * possible attention events. If so, it will post an Error Attention event
2844 * and wake up worker thread to process it. Otherwise, it will set up the
2845 * Error Attention polling timer for the next poll.
2846 **/
2847void lpfc_poll_eratt(unsigned long ptr)
2848{
2849 struct lpfc_hba *phba;
James Smartaa6fbb72012-08-03 12:36:03 -04002850 uint32_t eratt = 0, rem;
2851 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002852
2853 phba = (struct lpfc_hba *)ptr;
2854
James Smartaa6fbb72012-08-03 12:36:03 -04002855 /* Here we will also keep track of interrupts per sec of the hba */
2856 sli_intr = phba->sli.slistat.sli_intr;
2857
2858 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2859 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2860 sli_intr);
2861 else
2862 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2863
2864 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2865 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2866 phba->sli.slistat.sli_ips = cnt;
2867
2868 phba->sli.slistat.sli_prev_intr = sli_intr;
2869
James Smart93996272008-08-24 21:50:30 -04002870 /* Check chip HA register for error event */
2871 eratt = lpfc_sli_check_eratt(phba);
2872
2873 if (eratt)
2874 /* Tell the worker thread there is work to do */
2875 lpfc_worker_wake_up(phba);
2876 else
2877 /* Restart the timer for next eratt poll */
2878 mod_timer(&phba->eratt_poll, jiffies +
2879 HZ * LPFC_ERATT_POLL_INTERVAL);
2880 return;
2881}
2882
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002883
James Smarte59058c2008-08-24 21:49:00 -04002884/**
James Smart3621a712009-04-06 18:47:14 -04002885 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002886 * @phba: Pointer to HBA context object.
2887 * @pring: Pointer to driver SLI ring object.
2888 * @mask: Host attention register mask for this ring.
2889 *
2890 * This function is called from the interrupt context when there is a ring
2891 * event for the fcp ring. The caller does not hold any lock.
2892 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002893 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002894 * LE bit set. The function will call the completion handler of the command iocb
2895 * if the response iocb indicates a completion for a command iocb or it is
2896 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2897 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002898 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002899 * to check it explicitly.
2900 */
2901int
James Smart2e0fef82007-06-17 19:56:36 -05002902lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2903 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002904{
James Smart34b02dc2008-08-24 21:49:55 -04002905 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002906 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002907 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002908 struct lpfc_iocbq *cmdiocbq = NULL;
2909 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002910 uint32_t status;
2911 uint32_t portRspPut, portRspMax;
2912 int rc = 1;
2913 lpfc_iocb_type type;
2914 unsigned long iflag;
2915 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002916
James Smart2e0fef82007-06-17 19:56:36 -05002917 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002918 pring->stats.iocb_event++;
2919
dea31012005-04-17 16:05:31 -05002920 /*
2921 * The next available response entry should never exceed the maximum
2922 * entries. If it does, treat it as an adapter hardware error.
2923 */
James Smart7e56aa22012-08-03 12:35:34 -04002924 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002925 portRspPut = le32_to_cpu(pgp->rspPutInx);
2926 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002927 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002928 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002929 return 1;
2930 }
James Smart45ed1192009-10-02 15:17:02 -04002931 if (phba->fcp_ring_in_use) {
2932 spin_unlock_irqrestore(&phba->hbalock, iflag);
2933 return 1;
2934 } else
2935 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002936
2937 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002938 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002939 /*
2940 * Fetch an entry off the ring and copy it into a local data
2941 * structure. The copy involves a byte-swap since the
2942 * network byte order and pci byte orders are different.
2943 */
James Smarted957682007-06-17 19:56:37 -05002944 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002945 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002946
James Smart7e56aa22012-08-03 12:35:34 -04002947 if (++pring->sli.sli3.rspidx >= portRspMax)
2948 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002949
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002950 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2951 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002952 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002953 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002954 irsp = &rspiocbq.iocb;
2955
dea31012005-04-17 16:05:31 -05002956 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2957 pring->stats.iocb_rsp++;
2958 rsp_cmpl++;
2959
2960 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002961 /*
2962 * If resource errors reported from HBA, reduce
2963 * queuedepths of the SCSI device.
2964 */
2965 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2966 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2967 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002968 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002969 spin_lock_irqsave(&phba->hbalock, iflag);
2970 }
2971
dea31012005-04-17 16:05:31 -05002972 /* Rsp ring <ringno> error: IOCB */
2973 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002974 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002975 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002976 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002977 irsp->un.ulpWord[0],
2978 irsp->un.ulpWord[1],
2979 irsp->un.ulpWord[2],
2980 irsp->un.ulpWord[3],
2981 irsp->un.ulpWord[4],
2982 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002983 *(uint32_t *)&irsp->un1,
2984 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002985 }
2986
2987 switch (type) {
2988 case LPFC_ABORT_IOCB:
2989 case LPFC_SOL_IOCB:
2990 /*
2991 * Idle exchange closed via ABTS from port. No iocb
2992 * resources need to be recovered.
2993 */
2994 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002995 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002996 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002997 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002998 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002999 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003000 break;
3001 }
3002
James Bottomley604a3e32005-10-29 10:28:33 -05003003 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3004 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003005 if (unlikely(!cmdiocbq))
3006 break;
3007 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3008 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3009 if (cmdiocbq->iocb_cmpl) {
3010 spin_unlock_irqrestore(&phba->hbalock, iflag);
3011 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3012 &rspiocbq);
3013 spin_lock_irqsave(&phba->hbalock, iflag);
3014 }
dea31012005-04-17 16:05:31 -05003015 break;
James Smarta4bc3372006-12-02 13:34:16 -05003016 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003017 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003018 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003019 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003020 break;
dea31012005-04-17 16:05:31 -05003021 default:
3022 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3023 char adaptermsg[LPFC_MAX_ADPTMSG];
3024 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3025 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3026 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003027 dev_warn(&((phba->pcidev)->dev),
3028 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003029 phba->brd_no, adaptermsg);
3030 } else {
3031 /* Unknown IOCB command */
3032 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003033 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003034 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003035 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003036 irsp->ulpStatus,
3037 irsp->ulpIoTag,
3038 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003039 }
3040 break;
3041 }
3042
3043 /*
3044 * The response IOCB has been processed. Update the ring
3045 * pointer in SLIM. If the port response put pointer has not
3046 * been updated, sync the pgp->rspPutInx and fetch the new port
3047 * response put pointer.
3048 */
James Smart7e56aa22012-08-03 12:35:34 -04003049 writel(pring->sli.sli3.rspidx,
3050 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003051
James Smart7e56aa22012-08-03 12:35:34 -04003052 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003053 portRspPut = le32_to_cpu(pgp->rspPutInx);
3054 }
3055
3056 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3057 pring->stats.iocb_rsp_full++;
3058 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3059 writel(status, phba->CAregaddr);
3060 readl(phba->CAregaddr);
3061 }
3062 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3063 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3064 pring->stats.iocb_cmd_empty++;
3065
3066 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003067 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003068 lpfc_sli_resume_iocb(phba, pring);
3069
3070 if ((pring->lpfc_sli_cmd_available))
3071 (pring->lpfc_sli_cmd_available) (phba, pring);
3072
3073 }
3074
James Smart45ed1192009-10-02 15:17:02 -04003075 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003076 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003077 return rc;
3078}
3079
James Smarte59058c2008-08-24 21:49:00 -04003080/**
James Smart3772a992009-05-22 14:50:54 -04003081 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3082 * @phba: Pointer to HBA context object.
3083 * @pring: Pointer to driver SLI ring object.
3084 * @rspiocbp: Pointer to driver response IOCB object.
3085 *
3086 * This function is called from the worker thread when there is a slow-path
3087 * response IOCB to process. This function chains all the response iocbs until
3088 * seeing the iocb with the LE bit set. The function will call
3089 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3090 * completion of a command iocb. The function will call the
3091 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3092 * The function frees the resources or calls the completion handler if this
3093 * iocb is an abort completion. The function returns NULL when the response
3094 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3095 * this function shall chain the iocb on to the iocb_continueq and return the
3096 * response iocb passed in.
3097 **/
3098static struct lpfc_iocbq *
3099lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3100 struct lpfc_iocbq *rspiocbp)
3101{
3102 struct lpfc_iocbq *saveq;
3103 struct lpfc_iocbq *cmdiocbp;
3104 struct lpfc_iocbq *next_iocb;
3105 IOCB_t *irsp = NULL;
3106 uint32_t free_saveq;
3107 uint8_t iocb_cmd_type;
3108 lpfc_iocb_type type;
3109 unsigned long iflag;
3110 int rc;
3111
3112 spin_lock_irqsave(&phba->hbalock, iflag);
3113 /* First add the response iocb to the countinueq list */
3114 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3115 pring->iocb_continueq_cnt++;
3116
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003117 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003118 irsp = &rspiocbp->iocb;
3119 if (irsp->ulpLe) {
3120 /*
3121 * By default, the driver expects to free all resources
3122 * associated with this iocb completion.
3123 */
3124 free_saveq = 1;
3125 saveq = list_get_first(&pring->iocb_continueq,
3126 struct lpfc_iocbq, list);
3127 irsp = &(saveq->iocb);
3128 list_del_init(&pring->iocb_continueq);
3129 pring->iocb_continueq_cnt = 0;
3130
3131 pring->stats.iocb_rsp++;
3132
3133 /*
3134 * If resource errors reported from HBA, reduce
3135 * queuedepths of the SCSI device.
3136 */
3137 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3138 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3139 spin_unlock_irqrestore(&phba->hbalock, iflag);
3140 phba->lpfc_rampdown_queue_depth(phba);
3141 spin_lock_irqsave(&phba->hbalock, iflag);
3142 }
3143
3144 if (irsp->ulpStatus) {
3145 /* Rsp ring <ringno> error: IOCB */
3146 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3147 "0328 Rsp Ring %d error: "
3148 "IOCB Data: "
3149 "x%x x%x x%x x%x "
3150 "x%x x%x x%x x%x "
3151 "x%x x%x x%x x%x "
3152 "x%x x%x x%x x%x\n",
3153 pring->ringno,
3154 irsp->un.ulpWord[0],
3155 irsp->un.ulpWord[1],
3156 irsp->un.ulpWord[2],
3157 irsp->un.ulpWord[3],
3158 irsp->un.ulpWord[4],
3159 irsp->un.ulpWord[5],
3160 *(((uint32_t *) irsp) + 6),
3161 *(((uint32_t *) irsp) + 7),
3162 *(((uint32_t *) irsp) + 8),
3163 *(((uint32_t *) irsp) + 9),
3164 *(((uint32_t *) irsp) + 10),
3165 *(((uint32_t *) irsp) + 11),
3166 *(((uint32_t *) irsp) + 12),
3167 *(((uint32_t *) irsp) + 13),
3168 *(((uint32_t *) irsp) + 14),
3169 *(((uint32_t *) irsp) + 15));
3170 }
3171
3172 /*
3173 * Fetch the IOCB command type and call the correct completion
3174 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3175 * get freed back to the lpfc_iocb_list by the discovery
3176 * kernel thread.
3177 */
3178 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3179 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3180 switch (type) {
3181 case LPFC_SOL_IOCB:
3182 spin_unlock_irqrestore(&phba->hbalock, iflag);
3183 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3184 spin_lock_irqsave(&phba->hbalock, iflag);
3185 break;
3186
3187 case LPFC_UNSOL_IOCB:
3188 spin_unlock_irqrestore(&phba->hbalock, iflag);
3189 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3190 spin_lock_irqsave(&phba->hbalock, iflag);
3191 if (!rc)
3192 free_saveq = 0;
3193 break;
3194
3195 case LPFC_ABORT_IOCB:
3196 cmdiocbp = NULL;
3197 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3198 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3199 saveq);
3200 if (cmdiocbp) {
3201 /* Call the specified completion routine */
3202 if (cmdiocbp->iocb_cmpl) {
3203 spin_unlock_irqrestore(&phba->hbalock,
3204 iflag);
3205 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3206 saveq);
3207 spin_lock_irqsave(&phba->hbalock,
3208 iflag);
3209 } else
3210 __lpfc_sli_release_iocbq(phba,
3211 cmdiocbp);
3212 }
3213 break;
3214
3215 case LPFC_UNKNOWN_IOCB:
3216 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3217 char adaptermsg[LPFC_MAX_ADPTMSG];
3218 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3219 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3220 MAX_MSG_DATA);
3221 dev_warn(&((phba->pcidev)->dev),
3222 "lpfc%d: %s\n",
3223 phba->brd_no, adaptermsg);
3224 } else {
3225 /* Unknown IOCB command */
3226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3227 "0335 Unknown IOCB "
3228 "command Data: x%x "
3229 "x%x x%x x%x\n",
3230 irsp->ulpCommand,
3231 irsp->ulpStatus,
3232 irsp->ulpIoTag,
3233 irsp->ulpContext);
3234 }
3235 break;
3236 }
3237
3238 if (free_saveq) {
3239 list_for_each_entry_safe(rspiocbp, next_iocb,
3240 &saveq->list, list) {
3241 list_del(&rspiocbp->list);
3242 __lpfc_sli_release_iocbq(phba, rspiocbp);
3243 }
3244 __lpfc_sli_release_iocbq(phba, saveq);
3245 }
3246 rspiocbp = NULL;
3247 }
3248 spin_unlock_irqrestore(&phba->hbalock, iflag);
3249 return rspiocbp;
3250}
3251
3252/**
3253 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003254 * @phba: Pointer to HBA context object.
3255 * @pring: Pointer to driver SLI ring object.
3256 * @mask: Host attention register mask for this ring.
3257 *
James Smart3772a992009-05-22 14:50:54 -04003258 * This routine wraps the actual slow_ring event process routine from the
3259 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003260 **/
James Smart3772a992009-05-22 14:50:54 -04003261void
James Smart2e0fef82007-06-17 19:56:36 -05003262lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3263 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003264{
James Smart3772a992009-05-22 14:50:54 -04003265 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3266}
3267
3268/**
3269 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3270 * @phba: Pointer to HBA context object.
3271 * @pring: Pointer to driver SLI ring object.
3272 * @mask: Host attention register mask for this ring.
3273 *
3274 * This function is called from the worker thread when there is a ring event
3275 * for non-fcp rings. The caller does not hold any lock. The function will
3276 * remove each response iocb in the response ring and calls the handle
3277 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3278 **/
3279static void
3280lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3281 struct lpfc_sli_ring *pring, uint32_t mask)
3282{
James Smart34b02dc2008-08-24 21:49:55 -04003283 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003284 IOCB_t *entry;
3285 IOCB_t *irsp = NULL;
3286 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003287 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003288 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003289 uint32_t status;
dea31012005-04-17 16:05:31 -05003290
James Smart34b02dc2008-08-24 21:49:55 -04003291 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003292 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003293 pring->stats.iocb_event++;
3294
dea31012005-04-17 16:05:31 -05003295 /*
3296 * The next available response entry should never exceed the maximum
3297 * entries. If it does, treat it as an adapter hardware error.
3298 */
James Smart7e56aa22012-08-03 12:35:34 -04003299 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003300 portRspPut = le32_to_cpu(pgp->rspPutInx);
3301 if (portRspPut >= portRspMax) {
3302 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003303 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003304 * rsp ring <portRspMax>
3305 */
James Smarted957682007-06-17 19:56:37 -05003306 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003307 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003308 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003309 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003310
James Smart2e0fef82007-06-17 19:56:36 -05003311 phba->link_state = LPFC_HBA_ERROR;
3312 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003313
3314 phba->work_hs = HS_FFER3;
3315 lpfc_handle_eratt(phba);
3316
James Smart3772a992009-05-22 14:50:54 -04003317 return;
dea31012005-04-17 16:05:31 -05003318 }
3319
3320 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003321 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003322 /*
3323 * Build a completion list and call the appropriate handler.
3324 * The process is to get the next available response iocb, get
3325 * a free iocb from the list, copy the response data into the
3326 * free iocb, insert to the continuation list, and update the
3327 * next response index to slim. This process makes response
3328 * iocb's in the ring available to DMA as fast as possible but
3329 * pays a penalty for a copy operation. Since the iocb is
3330 * only 32 bytes, this penalty is considered small relative to
3331 * the PCI reads for register values and a slim write. When
3332 * the ulpLe field is set, the entire Command has been
3333 * received.
3334 */
James Smarted957682007-06-17 19:56:37 -05003335 entry = lpfc_resp_iocb(phba, pring);
3336
James Smart858c9f62007-06-17 19:56:39 -05003337 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003338 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003339 if (rspiocbp == NULL) {
3340 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003341 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003342 break;
3343 }
3344
James Smarted957682007-06-17 19:56:37 -05003345 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3346 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003347 irsp = &rspiocbp->iocb;
3348
James Smart7e56aa22012-08-03 12:35:34 -04003349 if (++pring->sli.sli3.rspidx >= portRspMax)
3350 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003351
James Smarta58cbd52007-08-02 11:09:43 -04003352 if (pring->ringno == LPFC_ELS_RING) {
3353 lpfc_debugfs_slow_ring_trc(phba,
3354 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3355 *(((uint32_t *) irsp) + 4),
3356 *(((uint32_t *) irsp) + 6),
3357 *(((uint32_t *) irsp) + 7));
3358 }
3359
James Smart7e56aa22012-08-03 12:35:34 -04003360 writel(pring->sli.sli3.rspidx,
3361 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003362
James Smart3772a992009-05-22 14:50:54 -04003363 spin_unlock_irqrestore(&phba->hbalock, iflag);
3364 /* Handle the response IOCB */
3365 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3366 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003367
3368 /*
3369 * If the port response put pointer has not been updated, sync
3370 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3371 * response put pointer.
3372 */
James Smart7e56aa22012-08-03 12:35:34 -04003373 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003374 portRspPut = le32_to_cpu(pgp->rspPutInx);
3375 }
James Smart7e56aa22012-08-03 12:35:34 -04003376 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003377
James Smart92d7f7b2007-06-17 19:56:38 -05003378 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003379 /* At least one response entry has been freed */
3380 pring->stats.iocb_rsp_full++;
3381 /* SET RxRE_RSP in Chip Att register */
3382 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3383 writel(status, phba->CAregaddr);
3384 readl(phba->CAregaddr); /* flush */
3385 }
3386 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3387 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3388 pring->stats.iocb_cmd_empty++;
3389
3390 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003391 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003392 lpfc_sli_resume_iocb(phba, pring);
3393
3394 if ((pring->lpfc_sli_cmd_available))
3395 (pring->lpfc_sli_cmd_available) (phba, pring);
3396
3397 }
3398
James Smart2e0fef82007-06-17 19:56:36 -05003399 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003400 return;
dea31012005-04-17 16:05:31 -05003401}
3402
James Smarte59058c2008-08-24 21:49:00 -04003403/**
James Smart4f774512009-05-22 14:52:35 -04003404 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3405 * @phba: Pointer to HBA context object.
3406 * @pring: Pointer to driver SLI ring object.
3407 * @mask: Host attention register mask for this ring.
3408 *
3409 * This function is called from the worker thread when there is a pending
3410 * ELS response iocb on the driver internal slow-path response iocb worker
3411 * queue. The caller does not hold any lock. The function will remove each
3412 * response iocb from the response worker queue and calls the handle
3413 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3414 **/
3415static void
3416lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3417 struct lpfc_sli_ring *pring, uint32_t mask)
3418{
3419 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003420 struct hbq_dmabuf *dmabuf;
3421 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003422 unsigned long iflag;
3423
James Smart45ed1192009-10-02 15:17:02 -04003424 spin_lock_irqsave(&phba->hbalock, iflag);
3425 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3426 spin_unlock_irqrestore(&phba->hbalock, iflag);
3427 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003428 /* Get the response iocb from the head of work queue */
3429 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003430 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003431 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003432 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003433
3434 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3435 case CQE_CODE_COMPL_WQE:
3436 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3437 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003438 /* Translate ELS WCQE to response IOCBQ */
3439 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3440 irspiocbq);
3441 if (irspiocbq)
3442 lpfc_sli_sp_handle_rspiocb(phba, pring,
3443 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003444 break;
3445 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003446 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003447 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3448 cq_event);
3449 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3450 break;
3451 default:
3452 break;
3453 }
James Smart4f774512009-05-22 14:52:35 -04003454 }
3455}
3456
3457/**
James Smart3621a712009-04-06 18:47:14 -04003458 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003459 * @phba: Pointer to HBA context object.
3460 * @pring: Pointer to driver SLI ring object.
3461 *
3462 * This function aborts all iocbs in the given ring and frees all the iocb
3463 * objects in txq. This function issues an abort iocb for all the iocb commands
3464 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3465 * the return of this function. The caller is not required to hold any locks.
3466 **/
James Smart2e0fef82007-06-17 19:56:36 -05003467void
dea31012005-04-17 16:05:31 -05003468lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3469{
James Smart2534ba72007-04-25 09:52:20 -04003470 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003471 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003472
James Smart92d7f7b2007-06-17 19:56:38 -05003473 if (pring->ringno == LPFC_ELS_RING) {
3474 lpfc_fabric_abort_hba(phba);
3475 }
3476
dea31012005-04-17 16:05:31 -05003477 /* Error everything on txq and txcmplq
3478 * First do the txq.
3479 */
James Smart2e0fef82007-06-17 19:56:36 -05003480 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003481 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003482 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003483
3484 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003485 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3486 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3487
James Smart2e0fef82007-06-17 19:56:36 -05003488 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003489
James Smarta257bf92009-04-06 18:48:10 -04003490 /* Cancel all the IOCBs from the completions list */
3491 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3492 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003493}
3494
James Smarte59058c2008-08-24 21:49:00 -04003495/**
James Smart3621a712009-04-06 18:47:14 -04003496 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003497 * @phba: Pointer to HBA context object.
3498 *
3499 * This function flushes all iocbs in the fcp ring and frees all the iocb
3500 * objects in txq and txcmplq. This function will not issue abort iocbs
3501 * for all the iocb commands in txcmplq, they will just be returned with
3502 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3503 * slot has been permanently disabled.
3504 **/
3505void
3506lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3507{
3508 LIST_HEAD(txq);
3509 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003510 struct lpfc_sli *psli = &phba->sli;
3511 struct lpfc_sli_ring *pring;
3512
3513 /* Currently, only one fcp ring */
3514 pring = &psli->ring[psli->fcp_ring];
3515
3516 spin_lock_irq(&phba->hbalock);
3517 /* Retrieve everything on txq */
3518 list_splice_init(&pring->txq, &txq);
3519 pring->txq_cnt = 0;
3520
3521 /* Retrieve everything on the txcmplq */
3522 list_splice_init(&pring->txcmplq, &txcmplq);
3523 pring->txcmplq_cnt = 0;
James Smart4f2e66c2012-05-09 21:17:07 -04003524
3525 /* Indicate the I/O queues are flushed */
3526 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003527 spin_unlock_irq(&phba->hbalock);
3528
3529 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003530 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3531 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003532
3533 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003534 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3535 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003536}
3537
3538/**
James Smart3772a992009-05-22 14:50:54 -04003539 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003540 * @phba: Pointer to HBA context object.
3541 * @mask: Bit mask to be checked.
3542 *
3543 * This function reads the host status register and compares
3544 * with the provided bit mask to check if HBA completed
3545 * the restart. This function will wait in a loop for the
3546 * HBA to complete restart. If the HBA does not restart within
3547 * 15 iterations, the function will reset the HBA again. The
3548 * function returns 1 when HBA fail to restart otherwise returns
3549 * zero.
3550 **/
James Smart3772a992009-05-22 14:50:54 -04003551static int
3552lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003553{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003554 uint32_t status;
3555 int i = 0;
3556 int retval = 0;
dea31012005-04-17 16:05:31 -05003557
Jamie Wellnitz41415862006-02-28 19:25:27 -05003558 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003559 if (lpfc_readl(phba->HSregaddr, &status))
3560 return 1;
dea31012005-04-17 16:05:31 -05003561
Jamie Wellnitz41415862006-02-28 19:25:27 -05003562 /*
3563 * Check status register every 100ms for 5 retries, then every
3564 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3565 * every 2.5 sec for 4.
3566 * Break our of the loop if errors occurred during init.
3567 */
3568 while (((status & mask) != mask) &&
3569 !(status & HS_FFERM) &&
3570 i++ < 20) {
dea31012005-04-17 16:05:31 -05003571
Jamie Wellnitz41415862006-02-28 19:25:27 -05003572 if (i <= 5)
3573 msleep(10);
3574 else if (i <= 10)
3575 msleep(500);
3576 else
3577 msleep(2500);
dea31012005-04-17 16:05:31 -05003578
Jamie Wellnitz41415862006-02-28 19:25:27 -05003579 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003580 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003581 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003582 lpfc_sli_brdrestart(phba);
3583 }
3584 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003585 if (lpfc_readl(phba->HSregaddr, &status)) {
3586 retval = 1;
3587 break;
3588 }
dea31012005-04-17 16:05:31 -05003589 }
dea31012005-04-17 16:05:31 -05003590
Jamie Wellnitz41415862006-02-28 19:25:27 -05003591 /* Check to see if any errors occurred during init */
3592 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3594 "2751 Adapter failed to restart, "
3595 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3596 status,
3597 readl(phba->MBslimaddr + 0xa8),
3598 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003599 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003600 retval = 1;
3601 }
dea31012005-04-17 16:05:31 -05003602
Jamie Wellnitz41415862006-02-28 19:25:27 -05003603 return retval;
dea31012005-04-17 16:05:31 -05003604}
3605
James Smartda0436e2009-05-22 14:51:39 -04003606/**
3607 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3608 * @phba: Pointer to HBA context object.
3609 * @mask: Bit mask to be checked.
3610 *
3611 * This function checks the host status register to check if HBA is
3612 * ready. This function will wait in a loop for the HBA to be ready
3613 * If the HBA is not ready , the function will will reset the HBA PCI
3614 * function again. The function returns 1 when HBA fail to be ready
3615 * otherwise returns zero.
3616 **/
3617static int
3618lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3619{
3620 uint32_t status;
3621 int retval = 0;
3622
3623 /* Read the HBA Host Status Register */
3624 status = lpfc_sli4_post_status_check(phba);
3625
3626 if (status) {
3627 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3628 lpfc_sli_brdrestart(phba);
3629 status = lpfc_sli4_post_status_check(phba);
3630 }
3631
3632 /* Check to see if any errors occurred during init */
3633 if (status) {
3634 phba->link_state = LPFC_HBA_ERROR;
3635 retval = 1;
3636 } else
3637 phba->sli4_hba.intr_enable = 0;
3638
3639 return retval;
3640}
3641
3642/**
3643 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3644 * @phba: Pointer to HBA context object.
3645 * @mask: Bit mask to be checked.
3646 *
3647 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3648 * from the API jump table function pointer from the lpfc_hba struct.
3649 **/
3650int
3651lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3652{
3653 return phba->lpfc_sli_brdready(phba, mask);
3654}
3655
James Smart92908312006-03-07 15:04:13 -05003656#define BARRIER_TEST_PATTERN (0xdeadbeef)
3657
James Smarte59058c2008-08-24 21:49:00 -04003658/**
James Smart3621a712009-04-06 18:47:14 -04003659 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003660 * @phba: Pointer to HBA context object.
3661 *
James Smart1b511972011-12-13 13:23:09 -05003662 * This function is called before resetting an HBA. This function is called
3663 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003664 **/
James Smart2e0fef82007-06-17 19:56:36 -05003665void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003666{
James Smart65a29c12006-07-06 15:50:50 -04003667 uint32_t __iomem *resp_buf;
3668 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003669 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003670 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003671 int i;
3672 uint8_t hdrtype;
3673
3674 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3675 if (hdrtype != 0x80 ||
3676 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3677 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3678 return;
3679
3680 /*
3681 * Tell the other part of the chip to suspend temporarily all
3682 * its DMA activity.
3683 */
James Smart65a29c12006-07-06 15:50:50 -04003684 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003685
3686 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003687 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3688 return;
James Smart92908312006-03-07 15:04:13 -05003689 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3690 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003691 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003692
James Smart9940b972011-03-11 16:06:12 -05003693 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3694 return;
3695 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003696 /* Clear Chip error bit */
3697 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003698 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003699 }
3700
3701 mbox = 0;
3702 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3703 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3704
3705 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003706 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003707 writel(mbox, mbox_buf);
3708
James Smart9940b972011-03-11 16:06:12 -05003709 for (i = 0; i < 50; i++) {
3710 if (lpfc_readl((resp_buf + 1), &resp_data))
3711 return;
3712 if (resp_data != ~(BARRIER_TEST_PATTERN))
3713 mdelay(1);
3714 else
3715 break;
3716 }
3717 resp_data = 0;
3718 if (lpfc_readl((resp_buf + 1), &resp_data))
3719 return;
3720 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003721 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003722 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003723 goto restore_hc;
3724 else
3725 goto clear_errat;
3726 }
3727
3728 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003729 resp_data = 0;
3730 for (i = 0; i < 500; i++) {
3731 if (lpfc_readl(resp_buf, &resp_data))
3732 return;
3733 if (resp_data != mbox)
3734 mdelay(1);
3735 else
3736 break;
3737 }
James Smart92908312006-03-07 15:04:13 -05003738
3739clear_errat:
3740
James Smart9940b972011-03-11 16:06:12 -05003741 while (++i < 500) {
3742 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3743 return;
3744 if (!(ha_copy & HA_ERATT))
3745 mdelay(1);
3746 else
3747 break;
3748 }
James Smart92908312006-03-07 15:04:13 -05003749
3750 if (readl(phba->HAregaddr) & HA_ERATT) {
3751 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003752 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003753 }
3754
3755restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003756 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003757 writel(hc_copy, phba->HCregaddr);
3758 readl(phba->HCregaddr); /* flush */
3759}
3760
James Smarte59058c2008-08-24 21:49:00 -04003761/**
James Smart3621a712009-04-06 18:47:14 -04003762 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003763 * @phba: Pointer to HBA context object.
3764 *
3765 * This function issues a kill_board mailbox command and waits for
3766 * the error attention interrupt. This function is called for stopping
3767 * the firmware processing. The caller is not required to hold any
3768 * locks. This function calls lpfc_hba_down_post function to free
3769 * any pending commands after the kill. The function will return 1 when it
3770 * fails to kill the board else will return 0.
3771 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003772int
James Smart2e0fef82007-06-17 19:56:36 -05003773lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003774{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003775 struct lpfc_sli *psli;
3776 LPFC_MBOXQ_t *pmb;
3777 uint32_t status;
3778 uint32_t ha_copy;
3779 int retval;
3780 int i = 0;
3781
3782 psli = &phba->sli;
3783
3784 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003785 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003786 "0329 Kill HBA Data: x%x x%x\n",
3787 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003788
James Smart98c9ea52007-10-27 13:37:33 -04003789 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3790 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003791 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003792
3793 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003794 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003795 if (lpfc_readl(phba->HCregaddr, &status)) {
3796 spin_unlock_irq(&phba->hbalock);
3797 mempool_free(pmb, phba->mbox_mem_pool);
3798 return 1;
3799 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003800 status &= ~HC_ERINT_ENA;
3801 writel(status, phba->HCregaddr);
3802 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003803 phba->link_flag |= LS_IGNORE_ERATT;
3804 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003805
3806 lpfc_kill_board(phba, pmb);
3807 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3808 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3809
3810 if (retval != MBX_SUCCESS) {
3811 if (retval != MBX_BUSY)
3812 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003813 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3814 "2752 KILL_BOARD command failed retval %d\n",
3815 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003816 spin_lock_irq(&phba->hbalock);
3817 phba->link_flag &= ~LS_IGNORE_ERATT;
3818 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003819 return 1;
3820 }
3821
James Smartf4b4c682009-05-22 14:53:12 -04003822 spin_lock_irq(&phba->hbalock);
3823 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3824 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003825
Jamie Wellnitz41415862006-02-28 19:25:27 -05003826 mempool_free(pmb, phba->mbox_mem_pool);
3827
3828 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3829 * attention every 100ms for 3 seconds. If we don't get ERATT after
3830 * 3 seconds we still set HBA_ERROR state because the status of the
3831 * board is now undefined.
3832 */
James Smart9940b972011-03-11 16:06:12 -05003833 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3834 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003835 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3836 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003837 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3838 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003839 }
3840
3841 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003842 if (ha_copy & HA_ERATT) {
3843 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003844 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003845 }
James Smart2e0fef82007-06-17 19:56:36 -05003846 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003847 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003848 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003849 phba->link_flag &= ~LS_IGNORE_ERATT;
3850 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003851
Jamie Wellnitz41415862006-02-28 19:25:27 -05003852 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003853 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003854
James Smart2e0fef82007-06-17 19:56:36 -05003855 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003856}
3857
James Smarte59058c2008-08-24 21:49:00 -04003858/**
James Smart3772a992009-05-22 14:50:54 -04003859 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003860 * @phba: Pointer to HBA context object.
3861 *
3862 * This function resets the HBA by writing HC_INITFF to the control
3863 * register. After the HBA resets, this function resets all the iocb ring
3864 * indices. This function disables PCI layer parity checking during
3865 * the reset.
3866 * This function returns 0 always.
3867 * The caller is not required to hold any locks.
3868 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003869int
James Smart2e0fef82007-06-17 19:56:36 -05003870lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003871{
3872 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003873 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003874 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003875 int i;
dea31012005-04-17 16:05:31 -05003876
Jamie Wellnitz41415862006-02-28 19:25:27 -05003877 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003878
Jamie Wellnitz41415862006-02-28 19:25:27 -05003879 /* Reset HBA */
3880 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003881 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003882 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003883
3884 /* perform board reset */
3885 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003886 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003887 phba->pport->fc_myDID = 0;
3888 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003889
Jamie Wellnitz41415862006-02-28 19:25:27 -05003890 /* Turn off parity checking and serr during the physical reset */
3891 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3892 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3893 (cfg_value &
3894 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3895
James Smart3772a992009-05-22 14:50:54 -04003896 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3897
Jamie Wellnitz41415862006-02-28 19:25:27 -05003898 /* Now toggle INITFF bit in the Host Control Register */
3899 writel(HC_INITFF, phba->HCregaddr);
3900 mdelay(1);
3901 readl(phba->HCregaddr); /* flush */
3902 writel(0, phba->HCregaddr);
3903 readl(phba->HCregaddr); /* flush */
3904
3905 /* Restore PCI cmd register */
3906 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003907
3908 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003909 for (i = 0; i < psli->num_rings; i++) {
3910 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003911 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003912 pring->sli.sli3.rspidx = 0;
3913 pring->sli.sli3.next_cmdidx = 0;
3914 pring->sli.sli3.local_getidx = 0;
3915 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003916 pring->missbufcnt = 0;
3917 }
dea31012005-04-17 16:05:31 -05003918
James Smart2e0fef82007-06-17 19:56:36 -05003919 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003920 return 0;
3921}
3922
James Smarte59058c2008-08-24 21:49:00 -04003923/**
James Smartda0436e2009-05-22 14:51:39 -04003924 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3925 * @phba: Pointer to HBA context object.
3926 *
3927 * This function resets a SLI4 HBA. This function disables PCI layer parity
3928 * checking during resets the device. The caller is not required to hold
3929 * any locks.
3930 *
3931 * This function returns 0 always.
3932 **/
3933int
3934lpfc_sli4_brdreset(struct lpfc_hba *phba)
3935{
3936 struct lpfc_sli *psli = &phba->sli;
3937 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003938 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003939
3940 /* Reset HBA */
3941 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3942 "0295 Reset HBA Data: x%x x%x\n",
3943 phba->pport->port_state, psli->sli_flag);
3944
3945 /* perform board reset */
3946 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003947 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003948 phba->pport->fc_myDID = 0;
3949 phba->pport->fc_prevDID = 0;
3950
James Smartda0436e2009-05-22 14:51:39 -04003951 spin_lock_irq(&phba->hbalock);
3952 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3953 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003954 spin_unlock_irq(&phba->hbalock);
3955
3956 /* Now physically reset the device */
3957 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3958 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003959
3960 /* Turn off parity checking and serr during the physical reset */
3961 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3962 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3963 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3964
James Smartda0436e2009-05-22 14:51:39 -04003965 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003966 lpfc_sli4_queue_destroy(phba);
James Smart27b01b82012-05-09 21:19:44 -04003967 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04003968
James Smartbe858b62010-12-15 17:57:20 -05003969 /* Restore PCI cmd register */
3970 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3971
James Smart27b01b82012-05-09 21:19:44 -04003972 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003973}
3974
3975/**
3976 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003977 * @phba: Pointer to HBA context object.
3978 *
3979 * This function is called in the SLI initialization code path to
3980 * restart the HBA. The caller is not required to hold any lock.
3981 * This function writes MBX_RESTART mailbox command to the SLIM and
3982 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3983 * function to free any pending commands. The function enables
3984 * POST only during the first initialization. The function returns zero.
3985 * The function does not guarantee completion of MBX_RESTART mailbox
3986 * command before the return of this function.
3987 **/
James Smartda0436e2009-05-22 14:51:39 -04003988static int
3989lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003990{
3991 MAILBOX_t *mb;
3992 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003993 volatile uint32_t word0;
3994 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003995 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003996
James Smart2e0fef82007-06-17 19:56:36 -05003997 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003998
James Smart0d878412009-10-02 15:16:56 -04003999 /* Take PCIe device Advanced Error Reporting (AER) state */
4000 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4001
Jamie Wellnitz41415862006-02-28 19:25:27 -05004002 psli = &phba->sli;
4003
4004 /* Restart HBA */
4005 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004006 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004007 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004008
4009 word0 = 0;
4010 mb = (MAILBOX_t *) &word0;
4011 mb->mbxCommand = MBX_RESTART;
4012 mb->mbxHc = 1;
4013
James Smart92908312006-03-07 15:04:13 -05004014 lpfc_reset_barrier(phba);
4015
Jamie Wellnitz41415862006-02-28 19:25:27 -05004016 to_slim = phba->MBslimaddr;
4017 writel(*(uint32_t *) mb, to_slim);
4018 readl(to_slim); /* flush */
4019
4020 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004021 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004022 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004023 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004024 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004025 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004026 writel(*(uint32_t *) mb, to_slim);
4027 readl(to_slim); /* flush */
4028
4029 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004030 phba->pport->stopped = 0;
4031 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004032 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004033 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004034
James Smart64ba8812006-08-02 15:24:34 -04004035 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4036 psli->stats_start = get_seconds();
4037
James Smarteaf15d52008-12-04 22:39:29 -05004038 /* Give the INITFF and Post time to settle. */
4039 mdelay(100);
dea31012005-04-17 16:05:31 -05004040
James Smart0d878412009-10-02 15:16:56 -04004041 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4042 if (hba_aer_enabled)
4043 pci_disable_pcie_error_reporting(phba->pcidev);
4044
Jamie Wellnitz41415862006-02-28 19:25:27 -05004045 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004046
4047 return 0;
4048}
4049
James Smarte59058c2008-08-24 21:49:00 -04004050/**
James Smartda0436e2009-05-22 14:51:39 -04004051 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4052 * @phba: Pointer to HBA context object.
4053 *
4054 * This function is called in the SLI initialization code path to restart
4055 * a SLI4 HBA. The caller is not required to hold any lock.
4056 * At the end of the function, it calls lpfc_hba_down_post function to
4057 * free any pending commands.
4058 **/
4059static int
4060lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4061{
4062 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004063 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004064 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004065
4066 /* Restart HBA */
4067 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4068 "0296 Restart HBA Data: x%x x%x\n",
4069 phba->pport->port_state, psli->sli_flag);
4070
James Smart75baf692010-06-08 18:31:21 -04004071 /* Take PCIe device Advanced Error Reporting (AER) state */
4072 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4073
James Smart27b01b82012-05-09 21:19:44 -04004074 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004075
4076 spin_lock_irq(&phba->hbalock);
4077 phba->pport->stopped = 0;
4078 phba->link_state = LPFC_INIT_START;
4079 phba->hba_flag = 0;
4080 spin_unlock_irq(&phba->hbalock);
4081
4082 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4083 psli->stats_start = get_seconds();
4084
James Smart75baf692010-06-08 18:31:21 -04004085 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4086 if (hba_aer_enabled)
4087 pci_disable_pcie_error_reporting(phba->pcidev);
4088
James Smartda0436e2009-05-22 14:51:39 -04004089 lpfc_hba_down_post(phba);
4090
James Smart27b01b82012-05-09 21:19:44 -04004091 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004092}
4093
4094/**
4095 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4096 * @phba: Pointer to HBA context object.
4097 *
4098 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4099 * API jump table function pointer from the lpfc_hba struct.
4100**/
4101int
4102lpfc_sli_brdrestart(struct lpfc_hba *phba)
4103{
4104 return phba->lpfc_sli_brdrestart(phba);
4105}
4106
4107/**
James Smart3621a712009-04-06 18:47:14 -04004108 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004109 * @phba: Pointer to HBA context object.
4110 *
4111 * This function is called after a HBA restart to wait for successful
4112 * restart of the HBA. Successful restart of the HBA is indicated by
4113 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4114 * iteration, the function will restart the HBA again. The function returns
4115 * zero if HBA successfully restarted else returns negative error code.
4116 **/
dea31012005-04-17 16:05:31 -05004117static int
4118lpfc_sli_chipset_init(struct lpfc_hba *phba)
4119{
4120 uint32_t status, i = 0;
4121
4122 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004123 if (lpfc_readl(phba->HSregaddr, &status))
4124 return -EIO;
dea31012005-04-17 16:05:31 -05004125
4126 /* Check status register to see what current state is */
4127 i = 0;
4128 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4129
James Smartdcf2a4e2010-09-29 11:18:53 -04004130 /* Check every 10ms for 10 retries, then every 100ms for 90
4131 * retries, then every 1 sec for 50 retires for a total of
4132 * ~60 seconds before reset the board again and check every
4133 * 1 sec for 50 retries. The up to 60 seconds before the
4134 * board ready is required by the Falcon FIPS zeroization
4135 * complete, and any reset the board in between shall cause
4136 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004137 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004138 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004139 /* Adapter failed to init, timeout, status reg
4140 <status> */
James Smarted957682007-06-17 19:56:37 -05004141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004142 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004143 "timeout, status reg x%x, "
4144 "FW Data: A8 x%x AC x%x\n", status,
4145 readl(phba->MBslimaddr + 0xa8),
4146 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004147 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004148 return -ETIMEDOUT;
4149 }
4150
4151 /* Check to see if any errors occurred during init */
4152 if (status & HS_FFERM) {
4153 /* ERROR: During chipset initialization */
4154 /* Adapter failed to init, chipset, status reg
4155 <status> */
James Smarted957682007-06-17 19:56:37 -05004156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004157 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004158 "chipset, status reg x%x, "
4159 "FW Data: A8 x%x AC x%x\n", status,
4160 readl(phba->MBslimaddr + 0xa8),
4161 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004162 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004163 return -EIO;
4164 }
4165
James Smartdcf2a4e2010-09-29 11:18:53 -04004166 if (i <= 10)
dea31012005-04-17 16:05:31 -05004167 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004168 else if (i <= 100)
4169 msleep(100);
4170 else
4171 msleep(1000);
dea31012005-04-17 16:05:31 -05004172
James Smartdcf2a4e2010-09-29 11:18:53 -04004173 if (i == 150) {
4174 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004175 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004176 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004177 }
4178 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004179 if (lpfc_readl(phba->HSregaddr, &status))
4180 return -EIO;
dea31012005-04-17 16:05:31 -05004181 }
4182
4183 /* Check to see if any errors occurred during init */
4184 if (status & HS_FFERM) {
4185 /* ERROR: During chipset initialization */
4186 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004188 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004189 "status reg x%x, "
4190 "FW Data: A8 x%x AC x%x\n", status,
4191 readl(phba->MBslimaddr + 0xa8),
4192 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004193 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004194 return -EIO;
4195 }
4196
4197 /* Clear all interrupt enable conditions */
4198 writel(0, phba->HCregaddr);
4199 readl(phba->HCregaddr); /* flush */
4200
4201 /* setup host attn register */
4202 writel(0xffffffff, phba->HAregaddr);
4203 readl(phba->HAregaddr); /* flush */
4204 return 0;
4205}
4206
James Smarte59058c2008-08-24 21:49:00 -04004207/**
James Smart3621a712009-04-06 18:47:14 -04004208 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004209 *
4210 * This function calculates and returns the number of HBQs required to be
4211 * configured.
4212 **/
James Smart78b2d852007-08-02 11:10:21 -04004213int
James Smarted957682007-06-17 19:56:37 -05004214lpfc_sli_hbq_count(void)
4215{
James Smart92d7f7b2007-06-17 19:56:38 -05004216 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004217}
4218
James Smarte59058c2008-08-24 21:49:00 -04004219/**
James Smart3621a712009-04-06 18:47:14 -04004220 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004221 *
4222 * This function adds the number of hbq entries in every HBQ to get
4223 * the total number of hbq entries required for the HBA and returns
4224 * the total count.
4225 **/
James Smarted957682007-06-17 19:56:37 -05004226static int
4227lpfc_sli_hbq_entry_count(void)
4228{
4229 int hbq_count = lpfc_sli_hbq_count();
4230 int count = 0;
4231 int i;
4232
4233 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004234 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004235 return count;
4236}
4237
James Smarte59058c2008-08-24 21:49:00 -04004238/**
James Smart3621a712009-04-06 18:47:14 -04004239 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004240 *
4241 * This function calculates amount of memory required for all hbq entries
4242 * to be configured and returns the total memory required.
4243 **/
dea31012005-04-17 16:05:31 -05004244int
James Smarted957682007-06-17 19:56:37 -05004245lpfc_sli_hbq_size(void)
4246{
4247 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4248}
4249
James Smarte59058c2008-08-24 21:49:00 -04004250/**
James Smart3621a712009-04-06 18:47:14 -04004251 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004252 * @phba: Pointer to HBA context object.
4253 *
4254 * This function is called during the SLI initialization to configure
4255 * all the HBQs and post buffers to the HBQ. The caller is not
4256 * required to hold any locks. This function will return zero if successful
4257 * else it will return negative error code.
4258 **/
James Smarted957682007-06-17 19:56:37 -05004259static int
4260lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4261{
4262 int hbq_count = lpfc_sli_hbq_count();
4263 LPFC_MBOXQ_t *pmb;
4264 MAILBOX_t *pmbox;
4265 uint32_t hbqno;
4266 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004267
James Smart92d7f7b2007-06-17 19:56:38 -05004268 /* Get a Mailbox buffer to setup mailbox
4269 * commands for HBA initialization
4270 */
James Smarted957682007-06-17 19:56:37 -05004271 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4272
4273 if (!pmb)
4274 return -ENOMEM;
4275
James Smart04c68492009-05-22 14:52:52 -04004276 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004277
4278 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4279 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004280 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004281
4282 hbq_entry_index = 0;
4283 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4284 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4285 phba->hbqs[hbqno].hbqPutIdx = 0;
4286 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4287 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004288 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004289 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4290 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004291 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4292
4293 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4294 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4295 mbxStatus <status>, ring <num> */
4296
4297 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004298 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004299 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004300 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004301 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004302 pmbox->mbxStatus, hbqno);
4303
4304 phba->link_state = LPFC_HBA_ERROR;
4305 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004306 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004307 }
4308 }
4309 phba->hbq_count = hbq_count;
4310
James Smarted957682007-06-17 19:56:37 -05004311 mempool_free(pmb, phba->mbox_mem_pool);
4312
James Smart92d7f7b2007-06-17 19:56:38 -05004313 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004314 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4315 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004316 return 0;
4317}
4318
James Smarte59058c2008-08-24 21:49:00 -04004319/**
James Smart4f774512009-05-22 14:52:35 -04004320 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4321 * @phba: Pointer to HBA context object.
4322 *
4323 * This function is called during the SLI initialization to configure
4324 * all the HBQs and post buffers to the HBQ. The caller is not
4325 * required to hold any locks. This function will return zero if successful
4326 * else it will return negative error code.
4327 **/
4328static int
4329lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4330{
4331 phba->hbq_in_use = 1;
4332 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4333 phba->hbq_count = 1;
4334 /* Initially populate or replenish the HBQs */
4335 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4336 return 0;
4337}
4338
4339/**
James Smart3621a712009-04-06 18:47:14 -04004340 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004341 * @phba: Pointer to HBA context object.
4342 * @sli_mode: sli mode - 2/3
4343 *
4344 * This function is called by the sli intialization code path
4345 * to issue config_port mailbox command. This function restarts the
4346 * HBA firmware and issues a config_port mailbox command to configure
4347 * the SLI interface in the sli mode specified by sli_mode
4348 * variable. The caller is not required to hold any locks.
4349 * The function returns 0 if successful, else returns negative error
4350 * code.
4351 **/
James Smart93996272008-08-24 21:50:30 -04004352int
4353lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004354{
4355 LPFC_MBOXQ_t *pmb;
4356 uint32_t resetcount = 0, rc = 0, done = 0;
4357
4358 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4359 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004360 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004361 return -ENOMEM;
4362 }
4363
James Smarted957682007-06-17 19:56:37 -05004364 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004365 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004366 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004367 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004368 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004369 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004370 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004371 rc = lpfc_sli_chipset_init(phba);
4372 if (rc)
4373 break;
4374
James Smart2e0fef82007-06-17 19:56:36 -05004375 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004376 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004377 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004378 resetcount++;
4379
James Smarted957682007-06-17 19:56:37 -05004380 /* Call pre CONFIG_PORT mailbox command initialization. A
4381 * value of 0 means the call was successful. Any other
4382 * nonzero value is a failure, but if ERESTART is returned,
4383 * the driver may reset the HBA and try again.
4384 */
dea31012005-04-17 16:05:31 -05004385 rc = lpfc_config_port_prep(phba);
4386 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004387 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004388 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004389 } else if (rc)
dea31012005-04-17 16:05:31 -05004390 break;
James Smart6d368e52011-05-24 11:44:12 -04004391
James Smart2e0fef82007-06-17 19:56:36 -05004392 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004393 lpfc_config_port(phba, pmb);
4394 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004395 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4396 LPFC_SLI3_HBQ_ENABLED |
4397 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004398 LPFC_SLI3_BG_ENABLED |
4399 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004400 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004402 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004403 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004404 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004405 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004406 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004407 spin_unlock_irq(&phba->hbalock);
4408 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004409 } else {
4410 /* Allow asynchronous mailbox command to go through */
4411 spin_lock_irq(&phba->hbalock);
4412 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4413 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004414 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004415
4416 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4417 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4418 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4419 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004420 }
dea31012005-04-17 16:05:31 -05004421 }
James Smarted957682007-06-17 19:56:37 -05004422 if (!done) {
4423 rc = -EINVAL;
4424 goto do_prep_failed;
4425 }
James Smart04c68492009-05-22 14:52:52 -04004426 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4427 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004428 rc = -ENXIO;
4429 goto do_prep_failed;
4430 }
James Smart04c68492009-05-22 14:52:52 -04004431 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004432 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004433 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4434 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4435 phba->max_vpi : phba->max_vports;
4436
James Smart34b02dc2008-08-24 21:49:55 -04004437 } else
4438 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004439 phba->fips_level = 0;
4440 phba->fips_spec_rev = 0;
4441 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004442 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004443 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4444 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4445 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4446 "2850 Security Crypto Active. FIPS x%d "
4447 "(Spec Rev: x%d)",
4448 phba->fips_level, phba->fips_spec_rev);
4449 }
4450 if (pmb->u.mb.un.varCfgPort.sec_err) {
4451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4452 "2856 Config Port Security Crypto "
4453 "Error: x%x ",
4454 pmb->u.mb.un.varCfgPort.sec_err);
4455 }
James Smart04c68492009-05-22 14:52:52 -04004456 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004457 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004458 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004459 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004460
4461 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4462 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004463
4464 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004465 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004466 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4467 else
4468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4469 "0443 Adapter did not grant "
4470 "BlockGuard\n");
4471 }
James Smart34b02dc2008-08-24 21:49:55 -04004472 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004473 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004474 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004475 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004476 }
James Smart92d7f7b2007-06-17 19:56:38 -05004477do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004478 mempool_free(pmb, phba->mbox_mem_pool);
4479 return rc;
4480}
4481
James Smarte59058c2008-08-24 21:49:00 -04004482
4483/**
James Smart3621a712009-04-06 18:47:14 -04004484 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004485 * @phba: Pointer to HBA context object.
4486 *
4487 * This function is the main SLI intialization function. This function
4488 * is called by the HBA intialization code, HBA reset code and HBA
4489 * error attention handler code. Caller is not required to hold any
4490 * locks. This function issues config_port mailbox command to configure
4491 * the SLI, setup iocb rings and HBQ rings. In the end the function
4492 * calls the config_port_post function to issue init_link mailbox
4493 * command and to start the discovery. The function will return zero
4494 * if successful, else it will return negative error code.
4495 **/
James Smarted957682007-06-17 19:56:37 -05004496int
4497lpfc_sli_hba_setup(struct lpfc_hba *phba)
4498{
4499 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004500 int mode = 3, i;
4501 int longs;
James Smarted957682007-06-17 19:56:37 -05004502
4503 switch (lpfc_sli_mode) {
4504 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004505 if (phba->cfg_enable_npiv) {
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 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004508 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004509 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004510 break;
4511 }
James Smarted957682007-06-17 19:56:37 -05004512 mode = 2;
4513 break;
4514 case 0:
4515 case 3:
4516 break;
4517 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004519 "1819 Unrecognized lpfc_sli_mode "
4520 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004521
4522 break;
4523 }
4524
James Smart93996272008-08-24 21:50:30 -04004525 rc = lpfc_sli_config_port(phba, mode);
4526
James Smarted957682007-06-17 19:56:37 -05004527 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004529 "1820 Unable to select SLI-3. "
4530 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004531 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004532 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004533 if (rc)
dea31012005-04-17 16:05:31 -05004534 goto lpfc_sli_hba_setup_error;
4535
James Smart0d878412009-10-02 15:16:56 -04004536 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4537 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4538 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4539 if (!rc) {
4540 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4541 "2709 This device supports "
4542 "Advanced Error Reporting (AER)\n");
4543 spin_lock_irq(&phba->hbalock);
4544 phba->hba_flag |= HBA_AER_ENABLED;
4545 spin_unlock_irq(&phba->hbalock);
4546 } else {
4547 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4548 "2708 This device does not support "
4549 "Advanced Error Reporting (AER)\n");
4550 phba->cfg_aer_support = 0;
4551 }
4552 }
4553
James Smarted957682007-06-17 19:56:37 -05004554 if (phba->sli_rev == 3) {
4555 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4556 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004557 } else {
4558 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4559 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004560 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004561 }
4562
4563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004564 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4565 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004566 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004567
4568 if (rc)
4569 goto lpfc_sli_hba_setup_error;
4570
James Smart6d368e52011-05-24 11:44:12 -04004571 /* Initialize VPIs. */
4572 if (phba->sli_rev == LPFC_SLI_REV3) {
4573 /*
4574 * The VPI bitmask and physical ID array are allocated
4575 * and initialized once only - at driver load. A port
4576 * reset doesn't need to reinitialize this memory.
4577 */
4578 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4579 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4580 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4581 GFP_KERNEL);
4582 if (!phba->vpi_bmask) {
4583 rc = -ENOMEM;
4584 goto lpfc_sli_hba_setup_error;
4585 }
4586
4587 phba->vpi_ids = kzalloc(
4588 (phba->max_vpi+1) * sizeof(uint16_t),
4589 GFP_KERNEL);
4590 if (!phba->vpi_ids) {
4591 kfree(phba->vpi_bmask);
4592 rc = -ENOMEM;
4593 goto lpfc_sli_hba_setup_error;
4594 }
4595 for (i = 0; i < phba->max_vpi; i++)
4596 phba->vpi_ids[i] = i;
4597 }
4598 }
4599
James Smart93996272008-08-24 21:50:30 -04004600 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004601 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4602 rc = lpfc_sli_hbq_setup(phba);
4603 if (rc)
4604 goto lpfc_sli_hba_setup_error;
4605 }
James Smart04c68492009-05-22 14:52:52 -04004606 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004607 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004608 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004609
4610 rc = lpfc_config_port_post(phba);
4611 if (rc)
4612 goto lpfc_sli_hba_setup_error;
4613
James Smarted957682007-06-17 19:56:37 -05004614 return rc;
4615
James Smart92d7f7b2007-06-17 19:56:38 -05004616lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004617 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004619 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004620 return rc;
4621}
4622
James Smartda0436e2009-05-22 14:51:39 -04004623/**
4624 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4625 * @phba: Pointer to HBA context object.
4626 * @mboxq: mailbox pointer.
4627 * This function issue a dump mailbox command to read config region
4628 * 23 and parse the records in the region and populate driver
4629 * data structure.
4630 **/
4631static int
James Smartff78d8f2011-12-13 13:21:35 -05004632lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004633{
James Smartff78d8f2011-12-13 13:21:35 -05004634 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004635 struct lpfc_dmabuf *mp;
4636 struct lpfc_mqe *mqe;
4637 uint32_t data_length;
4638 int rc;
4639
4640 /* Program the default value of vlan_id and fc_map */
4641 phba->valid_vlan = 0;
4642 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4643 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4644 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4645
James Smartff78d8f2011-12-13 13:21:35 -05004646 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4647 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004648 return -ENOMEM;
4649
James Smartff78d8f2011-12-13 13:21:35 -05004650 mqe = &mboxq->u.mqe;
4651 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4652 rc = -ENOMEM;
4653 goto out_free_mboxq;
4654 }
4655
James Smartda0436e2009-05-22 14:51:39 -04004656 mp = (struct lpfc_dmabuf *) mboxq->context1;
4657 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4658
4659 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4660 "(%d):2571 Mailbox cmd x%x Status x%x "
4661 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4662 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4663 "CQ: x%x x%x x%x x%x\n",
4664 mboxq->vport ? mboxq->vport->vpi : 0,
4665 bf_get(lpfc_mqe_command, mqe),
4666 bf_get(lpfc_mqe_status, mqe),
4667 mqe->un.mb_words[0], mqe->un.mb_words[1],
4668 mqe->un.mb_words[2], mqe->un.mb_words[3],
4669 mqe->un.mb_words[4], mqe->un.mb_words[5],
4670 mqe->un.mb_words[6], mqe->un.mb_words[7],
4671 mqe->un.mb_words[8], mqe->un.mb_words[9],
4672 mqe->un.mb_words[10], mqe->un.mb_words[11],
4673 mqe->un.mb_words[12], mqe->un.mb_words[13],
4674 mqe->un.mb_words[14], mqe->un.mb_words[15],
4675 mqe->un.mb_words[16], mqe->un.mb_words[50],
4676 mboxq->mcqe.word0,
4677 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4678 mboxq->mcqe.trailer);
4679
4680 if (rc) {
4681 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4682 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004683 rc = -EIO;
4684 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004685 }
4686 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004687 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004688 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4689 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004690 rc = -EIO;
4691 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004692 }
James Smartda0436e2009-05-22 14:51:39 -04004693
4694 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4695 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4696 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004697 rc = 0;
4698
4699out_free_mboxq:
4700 mempool_free(mboxq, phba->mbox_mem_pool);
4701 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004702}
4703
4704/**
4705 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4706 * @phba: pointer to lpfc hba data structure.
4707 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4708 * @vpd: pointer to the memory to hold resulting port vpd data.
4709 * @vpd_size: On input, the number of bytes allocated to @vpd.
4710 * On output, the number of data bytes in @vpd.
4711 *
4712 * This routine executes a READ_REV SLI4 mailbox command. In
4713 * addition, this routine gets the port vpd data.
4714 *
4715 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004716 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004717 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004718 **/
4719static int
4720lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4721 uint8_t *vpd, uint32_t *vpd_size)
4722{
4723 int rc = 0;
4724 uint32_t dma_size;
4725 struct lpfc_dmabuf *dmabuf;
4726 struct lpfc_mqe *mqe;
4727
4728 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4729 if (!dmabuf)
4730 return -ENOMEM;
4731
4732 /*
4733 * Get a DMA buffer for the vpd data resulting from the READ_REV
4734 * mailbox command.
4735 */
4736 dma_size = *vpd_size;
4737 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4738 dma_size,
4739 &dmabuf->phys,
4740 GFP_KERNEL);
4741 if (!dmabuf->virt) {
4742 kfree(dmabuf);
4743 return -ENOMEM;
4744 }
4745 memset(dmabuf->virt, 0, dma_size);
4746
4747 /*
4748 * The SLI4 implementation of READ_REV conflicts at word1,
4749 * bits 31:16 and SLI4 adds vpd functionality not present
4750 * in SLI3. This code corrects the conflicts.
4751 */
4752 lpfc_read_rev(phba, mboxq);
4753 mqe = &mboxq->u.mqe;
4754 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4755 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4756 mqe->un.read_rev.word1 &= 0x0000FFFF;
4757 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4758 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4759
4760 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4761 if (rc) {
4762 dma_free_coherent(&phba->pcidev->dev, dma_size,
4763 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004764 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004765 return -EIO;
4766 }
4767
James Smartda0436e2009-05-22 14:51:39 -04004768 /*
4769 * The available vpd length cannot be bigger than the
4770 * DMA buffer passed to the port. Catch the less than
4771 * case and update the caller's size.
4772 */
4773 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4774 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4775
James Smartd7c47992010-06-08 18:31:54 -04004776 memcpy(vpd, dmabuf->virt, *vpd_size);
4777
James Smartda0436e2009-05-22 14:51:39 -04004778 dma_free_coherent(&phba->pcidev->dev, dma_size,
4779 dmabuf->virt, dmabuf->phys);
4780 kfree(dmabuf);
4781 return 0;
4782}
4783
4784/**
James Smartcd1c8302011-10-10 21:33:25 -04004785 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4786 * @phba: pointer to lpfc hba data structure.
4787 *
4788 * This routine retrieves SLI4 device physical port name this PCI function
4789 * is attached to.
4790 *
4791 * Return codes
4792 * 0 - sucessful
4793 * otherwise - failed to retrieve physical port name
4794 **/
4795static int
4796lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4797{
4798 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004799 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4800 struct lpfc_controller_attribute *cntl_attr;
4801 struct lpfc_mbx_get_port_name *get_port_name;
4802 void *virtaddr = NULL;
4803 uint32_t alloclen, reqlen;
4804 uint32_t shdr_status, shdr_add_status;
4805 union lpfc_sli4_cfg_shdr *shdr;
4806 char cport_name = 0;
4807 int rc;
4808
4809 /* We assume nothing at this point */
4810 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4811 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4812
4813 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4814 if (!mboxq)
4815 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004816 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004817 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4818 lpfc_sli4_read_config(phba);
4819 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4820 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004821
4822 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4823 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4824 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4825 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4826 LPFC_SLI4_MBX_NEMBED);
4827 if (alloclen < reqlen) {
4828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4829 "3084 Allocated DMA memory size (%d) is "
4830 "less than the requested DMA memory size "
4831 "(%d)\n", alloclen, reqlen);
4832 rc = -ENOMEM;
4833 goto out_free_mboxq;
4834 }
4835 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4836 virtaddr = mboxq->sge_array->addr[0];
4837 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4838 shdr = &mbx_cntl_attr->cfg_shdr;
4839 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4840 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4841 if (shdr_status || shdr_add_status || rc) {
4842 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4843 "3085 Mailbox x%x (x%x/x%x) failed, "
4844 "rc:x%x, status:x%x, add_status:x%x\n",
4845 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4846 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4847 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4848 rc, shdr_status, shdr_add_status);
4849 rc = -ENXIO;
4850 goto out_free_mboxq;
4851 }
4852 cntl_attr = &mbx_cntl_attr->cntl_attr;
4853 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4854 phba->sli4_hba.lnk_info.lnk_tp =
4855 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4856 phba->sli4_hba.lnk_info.lnk_no =
4857 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4858 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4859 "3086 lnk_type:%d, lnk_numb:%d\n",
4860 phba->sli4_hba.lnk_info.lnk_tp,
4861 phba->sli4_hba.lnk_info.lnk_no);
4862
4863retrieve_ppname:
4864 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4865 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4866 sizeof(struct lpfc_mbx_get_port_name) -
4867 sizeof(struct lpfc_sli4_cfg_mhdr),
4868 LPFC_SLI4_MBX_EMBED);
4869 get_port_name = &mboxq->u.mqe.un.get_port_name;
4870 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4871 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4872 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4873 phba->sli4_hba.lnk_info.lnk_tp);
4874 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4875 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4876 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4877 if (shdr_status || shdr_add_status || rc) {
4878 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4879 "3087 Mailbox x%x (x%x/x%x) failed: "
4880 "rc:x%x, status:x%x, add_status:x%x\n",
4881 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4882 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4883 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4884 rc, shdr_status, shdr_add_status);
4885 rc = -ENXIO;
4886 goto out_free_mboxq;
4887 }
4888 switch (phba->sli4_hba.lnk_info.lnk_no) {
4889 case LPFC_LINK_NUMBER_0:
4890 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4891 &get_port_name->u.response);
4892 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4893 break;
4894 case LPFC_LINK_NUMBER_1:
4895 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4896 &get_port_name->u.response);
4897 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4898 break;
4899 case LPFC_LINK_NUMBER_2:
4900 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4901 &get_port_name->u.response);
4902 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4903 break;
4904 case LPFC_LINK_NUMBER_3:
4905 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4906 &get_port_name->u.response);
4907 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4908 break;
4909 default:
4910 break;
4911 }
4912
4913 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4914 phba->Port[0] = cport_name;
4915 phba->Port[1] = '\0';
4916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4917 "3091 SLI get port name: %s\n", phba->Port);
4918 }
4919
4920out_free_mboxq:
4921 if (rc != MBX_TIMEOUT) {
4922 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4923 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4924 else
4925 mempool_free(mboxq, phba->mbox_mem_pool);
4926 }
4927 return rc;
4928}
4929
4930/**
James Smartda0436e2009-05-22 14:51:39 -04004931 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4932 * @phba: pointer to lpfc hba data structure.
4933 *
4934 * This routine is called to explicitly arm the SLI4 device's completion and
4935 * event queues
4936 **/
4937static void
4938lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4939{
4940 uint8_t fcp_eqidx;
4941
4942 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4943 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004944 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004945 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04004946 do {
James Smart2e90f4b2011-12-13 13:22:37 -05004947 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4948 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04004949 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05004950 }
James Smart67d12732012-08-03 12:36:13 -04004951 if (phba->sli4_hba.hba_eq) {
4952 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05004953 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04004954 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05004955 LPFC_QUEUE_REARM);
4956 }
James Smartda0436e2009-05-22 14:51:39 -04004957}
4958
4959/**
James Smart6d368e52011-05-24 11:44:12 -04004960 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4961 * @phba: Pointer to HBA context object.
4962 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004963 * @extnt_count: buffer to hold port available extent count.
4964 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004965 *
James Smartb76f2dc2011-07-22 18:37:42 -04004966 * This function calls the port and retrievs the number of available
4967 * extents and their size for a particular extent type.
4968 *
4969 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004970 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004971int
James Smart6d368e52011-05-24 11:44:12 -04004972lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4973 uint16_t *extnt_count, uint16_t *extnt_size)
4974{
4975 int rc = 0;
4976 uint32_t length;
4977 uint32_t mbox_tmo;
4978 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4979 LPFC_MBOXQ_t *mbox;
4980
4981 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4982 if (!mbox)
4983 return -ENOMEM;
4984
4985 /* Find out how many extents are available for this resource type */
4986 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4987 sizeof(struct lpfc_sli4_cfg_mhdr));
4988 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4989 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4990 length, LPFC_SLI4_MBX_EMBED);
4991
4992 /* Send an extents count of 0 - the GET doesn't use it. */
4993 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4994 LPFC_SLI4_MBX_EMBED);
4995 if (unlikely(rc)) {
4996 rc = -EIO;
4997 goto err_exit;
4998 }
4999
5000 if (!phba->sli4_hba.intr_enable)
5001 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5002 else {
James Smarta183a152011-10-10 21:32:43 -04005003 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005004 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5005 }
5006 if (unlikely(rc)) {
5007 rc = -EIO;
5008 goto err_exit;
5009 }
5010
5011 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5012 if (bf_get(lpfc_mbox_hdr_status,
5013 &rsrc_info->header.cfg_shdr.response)) {
5014 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5015 "2930 Failed to get resource extents "
5016 "Status 0x%x Add'l Status 0x%x\n",
5017 bf_get(lpfc_mbox_hdr_status,
5018 &rsrc_info->header.cfg_shdr.response),
5019 bf_get(lpfc_mbox_hdr_add_status,
5020 &rsrc_info->header.cfg_shdr.response));
5021 rc = -EIO;
5022 goto err_exit;
5023 }
5024
5025 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5026 &rsrc_info->u.rsp);
5027 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5028 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005029
5030 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5031 "3162 Retrieved extents type-%d from port: count:%d, "
5032 "size:%d\n", type, *extnt_count, *extnt_size);
5033
5034err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005035 mempool_free(mbox, phba->mbox_mem_pool);
5036 return rc;
5037}
5038
5039/**
5040 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5041 * @phba: Pointer to HBA context object.
5042 * @type: The extent type to check.
5043 *
5044 * This function reads the current available extents from the port and checks
5045 * if the extent count or extent size has changed since the last access.
5046 * Callers use this routine post port reset to understand if there is a
5047 * extent reprovisioning requirement.
5048 *
5049 * Returns:
5050 * -Error: error indicates problem.
5051 * 1: Extent count or size has changed.
5052 * 0: No changes.
5053 **/
5054static int
5055lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5056{
5057 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5058 uint16_t size_diff, rsrc_ext_size;
5059 int rc = 0;
5060 struct lpfc_rsrc_blks *rsrc_entry;
5061 struct list_head *rsrc_blk_list = NULL;
5062
5063 size_diff = 0;
5064 curr_ext_cnt = 0;
5065 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5066 &rsrc_ext_cnt,
5067 &rsrc_ext_size);
5068 if (unlikely(rc))
5069 return -EIO;
5070
5071 switch (type) {
5072 case LPFC_RSC_TYPE_FCOE_RPI:
5073 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5074 break;
5075 case LPFC_RSC_TYPE_FCOE_VPI:
5076 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5077 break;
5078 case LPFC_RSC_TYPE_FCOE_XRI:
5079 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5080 break;
5081 case LPFC_RSC_TYPE_FCOE_VFI:
5082 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5083 break;
5084 default:
5085 break;
5086 }
5087
5088 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5089 curr_ext_cnt++;
5090 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5091 size_diff++;
5092 }
5093
5094 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5095 rc = 1;
5096
5097 return rc;
5098}
5099
5100/**
5101 * lpfc_sli4_cfg_post_extnts -
5102 * @phba: Pointer to HBA context object.
5103 * @extnt_cnt - number of available extents.
5104 * @type - the extent type (rpi, xri, vfi, vpi).
5105 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5106 * @mbox - pointer to the caller's allocated mailbox structure.
5107 *
5108 * This function executes the extents allocation request. It also
5109 * takes care of the amount of memory needed to allocate or get the
5110 * allocated extents. It is the caller's responsibility to evaluate
5111 * the response.
5112 *
5113 * Returns:
5114 * -Error: Error value describes the condition found.
5115 * 0: if successful
5116 **/
5117static int
James Smart8a9d2e82012-05-09 21:16:12 -04005118lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005119 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5120{
5121 int rc = 0;
5122 uint32_t req_len;
5123 uint32_t emb_len;
5124 uint32_t alloc_len, mbox_tmo;
5125
5126 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005127 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005128
5129 /*
5130 * Calculate the size of an embedded mailbox. The uint32_t
5131 * accounts for extents-specific word.
5132 */
5133 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5134 sizeof(uint32_t);
5135
5136 /*
5137 * Presume the allocation and response will fit into an embedded
5138 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5139 */
5140 *emb = LPFC_SLI4_MBX_EMBED;
5141 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005142 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005143 sizeof(union lpfc_sli4_cfg_shdr) +
5144 sizeof(uint32_t);
5145 *emb = LPFC_SLI4_MBX_NEMBED;
5146 }
5147
5148 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5149 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5150 req_len, *emb);
5151 if (alloc_len < req_len) {
5152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005153 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005154 "less than the requested DMA memory "
5155 "size (x%x)\n", alloc_len, req_len);
5156 return -ENOMEM;
5157 }
James Smart8a9d2e82012-05-09 21:16:12 -04005158 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005159 if (unlikely(rc))
5160 return -EIO;
5161
5162 if (!phba->sli4_hba.intr_enable)
5163 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5164 else {
James Smarta183a152011-10-10 21:32:43 -04005165 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005166 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5167 }
5168
5169 if (unlikely(rc))
5170 rc = -EIO;
5171 return rc;
5172}
5173
5174/**
5175 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5176 * @phba: Pointer to HBA context object.
5177 * @type: The resource extent type to allocate.
5178 *
5179 * This function allocates the number of elements for the specified
5180 * resource type.
5181 **/
5182static int
5183lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5184{
5185 bool emb = false;
5186 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5187 uint16_t rsrc_id, rsrc_start, j, k;
5188 uint16_t *ids;
5189 int i, rc;
5190 unsigned long longs;
5191 unsigned long *bmask;
5192 struct lpfc_rsrc_blks *rsrc_blks;
5193 LPFC_MBOXQ_t *mbox;
5194 uint32_t length;
5195 struct lpfc_id_range *id_array = NULL;
5196 void *virtaddr = NULL;
5197 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5198 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5199 struct list_head *ext_blk_list;
5200
5201 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5202 &rsrc_cnt,
5203 &rsrc_size);
5204 if (unlikely(rc))
5205 return -EIO;
5206
5207 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5208 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5209 "3009 No available Resource Extents "
5210 "for resource type 0x%x: Count: 0x%x, "
5211 "Size 0x%x\n", type, rsrc_cnt,
5212 rsrc_size);
5213 return -ENOMEM;
5214 }
5215
James Smart8a9d2e82012-05-09 21:16:12 -04005216 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5217 "2903 Post resource extents type-0x%x: "
5218 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005219
5220 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5221 if (!mbox)
5222 return -ENOMEM;
5223
James Smart8a9d2e82012-05-09 21:16:12 -04005224 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005225 if (unlikely(rc)) {
5226 rc = -EIO;
5227 goto err_exit;
5228 }
5229
5230 /*
5231 * Figure out where the response is located. Then get local pointers
5232 * to the response data. The port does not guarantee to respond to
5233 * all extents counts request so update the local variable with the
5234 * allocated count from the port.
5235 */
5236 if (emb == LPFC_SLI4_MBX_EMBED) {
5237 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5238 id_array = &rsrc_ext->u.rsp.id[0];
5239 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5240 } else {
5241 virtaddr = mbox->sge_array->addr[0];
5242 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5243 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5244 id_array = &n_rsrc->id;
5245 }
5246
5247 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5248 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5249
5250 /*
5251 * Based on the resource size and count, correct the base and max
5252 * resource values.
5253 */
5254 length = sizeof(struct lpfc_rsrc_blks);
5255 switch (type) {
5256 case LPFC_RSC_TYPE_FCOE_RPI:
5257 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5258 sizeof(unsigned long),
5259 GFP_KERNEL);
5260 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5261 rc = -ENOMEM;
5262 goto err_exit;
5263 }
5264 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5265 sizeof(uint16_t),
5266 GFP_KERNEL);
5267 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5268 kfree(phba->sli4_hba.rpi_bmask);
5269 rc = -ENOMEM;
5270 goto err_exit;
5271 }
5272
5273 /*
5274 * The next_rpi was initialized with the maximum available
5275 * count but the port may allocate a smaller number. Catch
5276 * that case and update the next_rpi.
5277 */
5278 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5279
5280 /* Initialize local ptrs for common extent processing later. */
5281 bmask = phba->sli4_hba.rpi_bmask;
5282 ids = phba->sli4_hba.rpi_ids;
5283 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5284 break;
5285 case LPFC_RSC_TYPE_FCOE_VPI:
5286 phba->vpi_bmask = kzalloc(longs *
5287 sizeof(unsigned long),
5288 GFP_KERNEL);
5289 if (unlikely(!phba->vpi_bmask)) {
5290 rc = -ENOMEM;
5291 goto err_exit;
5292 }
5293 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5294 sizeof(uint16_t),
5295 GFP_KERNEL);
5296 if (unlikely(!phba->vpi_ids)) {
5297 kfree(phba->vpi_bmask);
5298 rc = -ENOMEM;
5299 goto err_exit;
5300 }
5301
5302 /* Initialize local ptrs for common extent processing later. */
5303 bmask = phba->vpi_bmask;
5304 ids = phba->vpi_ids;
5305 ext_blk_list = &phba->lpfc_vpi_blk_list;
5306 break;
5307 case LPFC_RSC_TYPE_FCOE_XRI:
5308 phba->sli4_hba.xri_bmask = kzalloc(longs *
5309 sizeof(unsigned long),
5310 GFP_KERNEL);
5311 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5312 rc = -ENOMEM;
5313 goto err_exit;
5314 }
James Smart8a9d2e82012-05-09 21:16:12 -04005315 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005316 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5317 sizeof(uint16_t),
5318 GFP_KERNEL);
5319 if (unlikely(!phba->sli4_hba.xri_ids)) {
5320 kfree(phba->sli4_hba.xri_bmask);
5321 rc = -ENOMEM;
5322 goto err_exit;
5323 }
5324
5325 /* Initialize local ptrs for common extent processing later. */
5326 bmask = phba->sli4_hba.xri_bmask;
5327 ids = phba->sli4_hba.xri_ids;
5328 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5329 break;
5330 case LPFC_RSC_TYPE_FCOE_VFI:
5331 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5332 sizeof(unsigned long),
5333 GFP_KERNEL);
5334 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5335 rc = -ENOMEM;
5336 goto err_exit;
5337 }
5338 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5339 sizeof(uint16_t),
5340 GFP_KERNEL);
5341 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5342 kfree(phba->sli4_hba.vfi_bmask);
5343 rc = -ENOMEM;
5344 goto err_exit;
5345 }
5346
5347 /* Initialize local ptrs for common extent processing later. */
5348 bmask = phba->sli4_hba.vfi_bmask;
5349 ids = phba->sli4_hba.vfi_ids;
5350 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5351 break;
5352 default:
5353 /* Unsupported Opcode. Fail call. */
5354 id_array = NULL;
5355 bmask = NULL;
5356 ids = NULL;
5357 ext_blk_list = NULL;
5358 goto err_exit;
5359 }
5360
5361 /*
5362 * Complete initializing the extent configuration with the
5363 * allocated ids assigned to this function. The bitmask serves
5364 * as an index into the array and manages the available ids. The
5365 * array just stores the ids communicated to the port via the wqes.
5366 */
5367 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5368 if ((i % 2) == 0)
5369 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5370 &id_array[k]);
5371 else
5372 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5373 &id_array[k]);
5374
5375 rsrc_blks = kzalloc(length, GFP_KERNEL);
5376 if (unlikely(!rsrc_blks)) {
5377 rc = -ENOMEM;
5378 kfree(bmask);
5379 kfree(ids);
5380 goto err_exit;
5381 }
5382 rsrc_blks->rsrc_start = rsrc_id;
5383 rsrc_blks->rsrc_size = rsrc_size;
5384 list_add_tail(&rsrc_blks->list, ext_blk_list);
5385 rsrc_start = rsrc_id;
5386 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5387 phba->sli4_hba.scsi_xri_start = rsrc_start +
5388 lpfc_sli4_get_els_iocb_cnt(phba);
5389
5390 while (rsrc_id < (rsrc_start + rsrc_size)) {
5391 ids[j] = rsrc_id;
5392 rsrc_id++;
5393 j++;
5394 }
5395 /* Entire word processed. Get next word.*/
5396 if ((i % 2) == 1)
5397 k++;
5398 }
5399 err_exit:
5400 lpfc_sli4_mbox_cmd_free(phba, mbox);
5401 return rc;
5402}
5403
5404/**
5405 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5406 * @phba: Pointer to HBA context object.
5407 * @type: the extent's type.
5408 *
5409 * This function deallocates all extents of a particular resource type.
5410 * SLI4 does not allow for deallocating a particular extent range. It
5411 * is the caller's responsibility to release all kernel memory resources.
5412 **/
5413static int
5414lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5415{
5416 int rc;
5417 uint32_t length, mbox_tmo = 0;
5418 LPFC_MBOXQ_t *mbox;
5419 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5420 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5421
5422 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5423 if (!mbox)
5424 return -ENOMEM;
5425
5426 /*
5427 * This function sends an embedded mailbox because it only sends the
5428 * the resource type. All extents of this type are released by the
5429 * port.
5430 */
5431 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5432 sizeof(struct lpfc_sli4_cfg_mhdr));
5433 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5434 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5435 length, LPFC_SLI4_MBX_EMBED);
5436
5437 /* Send an extents count of 0 - the dealloc doesn't use it. */
5438 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5439 LPFC_SLI4_MBX_EMBED);
5440 if (unlikely(rc)) {
5441 rc = -EIO;
5442 goto out_free_mbox;
5443 }
5444 if (!phba->sli4_hba.intr_enable)
5445 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5446 else {
James Smarta183a152011-10-10 21:32:43 -04005447 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005448 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5449 }
5450 if (unlikely(rc)) {
5451 rc = -EIO;
5452 goto out_free_mbox;
5453 }
5454
5455 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5456 if (bf_get(lpfc_mbox_hdr_status,
5457 &dealloc_rsrc->header.cfg_shdr.response)) {
5458 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5459 "2919 Failed to release resource extents "
5460 "for type %d - Status 0x%x Add'l Status 0x%x. "
5461 "Resource memory not released.\n",
5462 type,
5463 bf_get(lpfc_mbox_hdr_status,
5464 &dealloc_rsrc->header.cfg_shdr.response),
5465 bf_get(lpfc_mbox_hdr_add_status,
5466 &dealloc_rsrc->header.cfg_shdr.response));
5467 rc = -EIO;
5468 goto out_free_mbox;
5469 }
5470
5471 /* Release kernel memory resources for the specific type. */
5472 switch (type) {
5473 case LPFC_RSC_TYPE_FCOE_VPI:
5474 kfree(phba->vpi_bmask);
5475 kfree(phba->vpi_ids);
5476 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5477 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5478 &phba->lpfc_vpi_blk_list, list) {
5479 list_del_init(&rsrc_blk->list);
5480 kfree(rsrc_blk);
5481 }
5482 break;
5483 case LPFC_RSC_TYPE_FCOE_XRI:
5484 kfree(phba->sli4_hba.xri_bmask);
5485 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005486 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5487 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5488 list_del_init(&rsrc_blk->list);
5489 kfree(rsrc_blk);
5490 }
5491 break;
5492 case LPFC_RSC_TYPE_FCOE_VFI:
5493 kfree(phba->sli4_hba.vfi_bmask);
5494 kfree(phba->sli4_hba.vfi_ids);
5495 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5496 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5497 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5498 list_del_init(&rsrc_blk->list);
5499 kfree(rsrc_blk);
5500 }
5501 break;
5502 case LPFC_RSC_TYPE_FCOE_RPI:
5503 /* RPI bitmask and physical id array are cleaned up earlier. */
5504 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5505 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5506 list_del_init(&rsrc_blk->list);
5507 kfree(rsrc_blk);
5508 }
5509 break;
5510 default:
5511 break;
5512 }
5513
5514 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5515
5516 out_free_mbox:
5517 mempool_free(mbox, phba->mbox_mem_pool);
5518 return rc;
5519}
5520
5521/**
5522 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5523 * @phba: Pointer to HBA context object.
5524 *
5525 * This function allocates all SLI4 resource identifiers.
5526 **/
5527int
5528lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5529{
5530 int i, rc, error = 0;
5531 uint16_t count, base;
5532 unsigned long longs;
5533
James Smartff78d8f2011-12-13 13:21:35 -05005534 if (!phba->sli4_hba.rpi_hdrs_in_use)
5535 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005536 if (phba->sli4_hba.extents_in_use) {
5537 /*
5538 * The port supports resource extents. The XRI, VPI, VFI, RPI
5539 * resource extent count must be read and allocated before
5540 * provisioning the resource id arrays.
5541 */
5542 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5543 LPFC_IDX_RSRC_RDY) {
5544 /*
5545 * Extent-based resources are set - the driver could
5546 * be in a port reset. Figure out if any corrective
5547 * actions need to be taken.
5548 */
5549 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5550 LPFC_RSC_TYPE_FCOE_VFI);
5551 if (rc != 0)
5552 error++;
5553 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5554 LPFC_RSC_TYPE_FCOE_VPI);
5555 if (rc != 0)
5556 error++;
5557 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5558 LPFC_RSC_TYPE_FCOE_XRI);
5559 if (rc != 0)
5560 error++;
5561 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5562 LPFC_RSC_TYPE_FCOE_RPI);
5563 if (rc != 0)
5564 error++;
5565
5566 /*
5567 * It's possible that the number of resources
5568 * provided to this port instance changed between
5569 * resets. Detect this condition and reallocate
5570 * resources. Otherwise, there is no action.
5571 */
5572 if (error) {
5573 lpfc_printf_log(phba, KERN_INFO,
5574 LOG_MBOX | LOG_INIT,
5575 "2931 Detected extent resource "
5576 "change. Reallocating all "
5577 "extents.\n");
5578 rc = lpfc_sli4_dealloc_extent(phba,
5579 LPFC_RSC_TYPE_FCOE_VFI);
5580 rc = lpfc_sli4_dealloc_extent(phba,
5581 LPFC_RSC_TYPE_FCOE_VPI);
5582 rc = lpfc_sli4_dealloc_extent(phba,
5583 LPFC_RSC_TYPE_FCOE_XRI);
5584 rc = lpfc_sli4_dealloc_extent(phba,
5585 LPFC_RSC_TYPE_FCOE_RPI);
5586 } else
5587 return 0;
5588 }
5589
5590 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5591 if (unlikely(rc))
5592 goto err_exit;
5593
5594 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5595 if (unlikely(rc))
5596 goto err_exit;
5597
5598 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5599 if (unlikely(rc))
5600 goto err_exit;
5601
5602 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5603 if (unlikely(rc))
5604 goto err_exit;
5605 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5606 LPFC_IDX_RSRC_RDY);
5607 return rc;
5608 } else {
5609 /*
5610 * The port does not support resource extents. The XRI, VPI,
5611 * VFI, RPI resource ids were determined from READ_CONFIG.
5612 * Just allocate the bitmasks and provision the resource id
5613 * arrays. If a port reset is active, the resources don't
5614 * need any action - just exit.
5615 */
5616 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005617 LPFC_IDX_RSRC_RDY) {
5618 lpfc_sli4_dealloc_resource_identifiers(phba);
5619 lpfc_sli4_remove_rpis(phba);
5620 }
James Smart6d368e52011-05-24 11:44:12 -04005621 /* RPIs. */
5622 count = phba->sli4_hba.max_cfg_param.max_rpi;
5623 base = phba->sli4_hba.max_cfg_param.rpi_base;
5624 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5625 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5626 sizeof(unsigned long),
5627 GFP_KERNEL);
5628 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5629 rc = -ENOMEM;
5630 goto err_exit;
5631 }
5632 phba->sli4_hba.rpi_ids = kzalloc(count *
5633 sizeof(uint16_t),
5634 GFP_KERNEL);
5635 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5636 rc = -ENOMEM;
5637 goto free_rpi_bmask;
5638 }
5639
5640 for (i = 0; i < count; i++)
5641 phba->sli4_hba.rpi_ids[i] = base + i;
5642
5643 /* VPIs. */
5644 count = phba->sli4_hba.max_cfg_param.max_vpi;
5645 base = phba->sli4_hba.max_cfg_param.vpi_base;
5646 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5647 phba->vpi_bmask = kzalloc(longs *
5648 sizeof(unsigned long),
5649 GFP_KERNEL);
5650 if (unlikely(!phba->vpi_bmask)) {
5651 rc = -ENOMEM;
5652 goto free_rpi_ids;
5653 }
5654 phba->vpi_ids = kzalloc(count *
5655 sizeof(uint16_t),
5656 GFP_KERNEL);
5657 if (unlikely(!phba->vpi_ids)) {
5658 rc = -ENOMEM;
5659 goto free_vpi_bmask;
5660 }
5661
5662 for (i = 0; i < count; i++)
5663 phba->vpi_ids[i] = base + i;
5664
5665 /* XRIs. */
5666 count = phba->sli4_hba.max_cfg_param.max_xri;
5667 base = phba->sli4_hba.max_cfg_param.xri_base;
5668 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5669 phba->sli4_hba.xri_bmask = kzalloc(longs *
5670 sizeof(unsigned long),
5671 GFP_KERNEL);
5672 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5673 rc = -ENOMEM;
5674 goto free_vpi_ids;
5675 }
James Smart41899be2012-03-01 22:34:19 -05005676 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005677 phba->sli4_hba.xri_ids = kzalloc(count *
5678 sizeof(uint16_t),
5679 GFP_KERNEL);
5680 if (unlikely(!phba->sli4_hba.xri_ids)) {
5681 rc = -ENOMEM;
5682 goto free_xri_bmask;
5683 }
5684
5685 for (i = 0; i < count; i++)
5686 phba->sli4_hba.xri_ids[i] = base + i;
5687
5688 /* VFIs. */
5689 count = phba->sli4_hba.max_cfg_param.max_vfi;
5690 base = phba->sli4_hba.max_cfg_param.vfi_base;
5691 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5692 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5693 sizeof(unsigned long),
5694 GFP_KERNEL);
5695 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5696 rc = -ENOMEM;
5697 goto free_xri_ids;
5698 }
5699 phba->sli4_hba.vfi_ids = kzalloc(count *
5700 sizeof(uint16_t),
5701 GFP_KERNEL);
5702 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5703 rc = -ENOMEM;
5704 goto free_vfi_bmask;
5705 }
5706
5707 for (i = 0; i < count; i++)
5708 phba->sli4_hba.vfi_ids[i] = base + i;
5709
5710 /*
5711 * Mark all resources ready. An HBA reset doesn't need
5712 * to reset the initialization.
5713 */
5714 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5715 LPFC_IDX_RSRC_RDY);
5716 return 0;
5717 }
5718
5719 free_vfi_bmask:
5720 kfree(phba->sli4_hba.vfi_bmask);
5721 free_xri_ids:
5722 kfree(phba->sli4_hba.xri_ids);
5723 free_xri_bmask:
5724 kfree(phba->sli4_hba.xri_bmask);
5725 free_vpi_ids:
5726 kfree(phba->vpi_ids);
5727 free_vpi_bmask:
5728 kfree(phba->vpi_bmask);
5729 free_rpi_ids:
5730 kfree(phba->sli4_hba.rpi_ids);
5731 free_rpi_bmask:
5732 kfree(phba->sli4_hba.rpi_bmask);
5733 err_exit:
5734 return rc;
5735}
5736
5737/**
5738 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5739 * @phba: Pointer to HBA context object.
5740 *
5741 * This function allocates the number of elements for the specified
5742 * resource type.
5743 **/
5744int
5745lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5746{
5747 if (phba->sli4_hba.extents_in_use) {
5748 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5749 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5750 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5751 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5752 } else {
5753 kfree(phba->vpi_bmask);
5754 kfree(phba->vpi_ids);
5755 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5756 kfree(phba->sli4_hba.xri_bmask);
5757 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005758 kfree(phba->sli4_hba.vfi_bmask);
5759 kfree(phba->sli4_hba.vfi_ids);
5760 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5761 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5762 }
5763
5764 return 0;
5765}
5766
5767/**
James Smartb76f2dc2011-07-22 18:37:42 -04005768 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5769 * @phba: Pointer to HBA context object.
5770 * @type: The resource extent type.
5771 * @extnt_count: buffer to hold port extent count response
5772 * @extnt_size: buffer to hold port extent size response.
5773 *
5774 * This function calls the port to read the host allocated extents
5775 * for a particular type.
5776 **/
5777int
5778lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5779 uint16_t *extnt_cnt, uint16_t *extnt_size)
5780{
5781 bool emb;
5782 int rc = 0;
5783 uint16_t curr_blks = 0;
5784 uint32_t req_len, emb_len;
5785 uint32_t alloc_len, mbox_tmo;
5786 struct list_head *blk_list_head;
5787 struct lpfc_rsrc_blks *rsrc_blk;
5788 LPFC_MBOXQ_t *mbox;
5789 void *virtaddr = NULL;
5790 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5791 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5792 union lpfc_sli4_cfg_shdr *shdr;
5793
5794 switch (type) {
5795 case LPFC_RSC_TYPE_FCOE_VPI:
5796 blk_list_head = &phba->lpfc_vpi_blk_list;
5797 break;
5798 case LPFC_RSC_TYPE_FCOE_XRI:
5799 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5800 break;
5801 case LPFC_RSC_TYPE_FCOE_VFI:
5802 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5803 break;
5804 case LPFC_RSC_TYPE_FCOE_RPI:
5805 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5806 break;
5807 default:
5808 return -EIO;
5809 }
5810
5811 /* Count the number of extents currently allocatd for this type. */
5812 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5813 if (curr_blks == 0) {
5814 /*
5815 * The GET_ALLOCATED mailbox does not return the size,
5816 * just the count. The size should be just the size
5817 * stored in the current allocated block and all sizes
5818 * for an extent type are the same so set the return
5819 * value now.
5820 */
5821 *extnt_size = rsrc_blk->rsrc_size;
5822 }
5823 curr_blks++;
5824 }
5825
5826 /* Calculate the total requested length of the dma memory. */
5827 req_len = curr_blks * sizeof(uint16_t);
5828
5829 /*
5830 * Calculate the size of an embedded mailbox. The uint32_t
5831 * accounts for extents-specific word.
5832 */
5833 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5834 sizeof(uint32_t);
5835
5836 /*
5837 * Presume the allocation and response will fit into an embedded
5838 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5839 */
5840 emb = LPFC_SLI4_MBX_EMBED;
5841 req_len = emb_len;
5842 if (req_len > emb_len) {
5843 req_len = curr_blks * sizeof(uint16_t) +
5844 sizeof(union lpfc_sli4_cfg_shdr) +
5845 sizeof(uint32_t);
5846 emb = LPFC_SLI4_MBX_NEMBED;
5847 }
5848
5849 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5850 if (!mbox)
5851 return -ENOMEM;
5852 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5853
5854 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5855 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5856 req_len, emb);
5857 if (alloc_len < req_len) {
5858 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5859 "2983 Allocated DMA memory size (x%x) is "
5860 "less than the requested DMA memory "
5861 "size (x%x)\n", alloc_len, req_len);
5862 rc = -ENOMEM;
5863 goto err_exit;
5864 }
5865 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5866 if (unlikely(rc)) {
5867 rc = -EIO;
5868 goto err_exit;
5869 }
5870
5871 if (!phba->sli4_hba.intr_enable)
5872 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5873 else {
James Smarta183a152011-10-10 21:32:43 -04005874 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005875 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5876 }
5877
5878 if (unlikely(rc)) {
5879 rc = -EIO;
5880 goto err_exit;
5881 }
5882
5883 /*
5884 * Figure out where the response is located. Then get local pointers
5885 * to the response data. The port does not guarantee to respond to
5886 * all extents counts request so update the local variable with the
5887 * allocated count from the port.
5888 */
5889 if (emb == LPFC_SLI4_MBX_EMBED) {
5890 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5891 shdr = &rsrc_ext->header.cfg_shdr;
5892 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5893 } else {
5894 virtaddr = mbox->sge_array->addr[0];
5895 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5896 shdr = &n_rsrc->cfg_shdr;
5897 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5898 }
5899
5900 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5901 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5902 "2984 Failed to read allocated resources "
5903 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5904 type,
5905 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5906 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5907 rc = -EIO;
5908 goto err_exit;
5909 }
5910 err_exit:
5911 lpfc_sli4_mbox_cmd_free(phba, mbox);
5912 return rc;
5913}
5914
5915/**
James Smart8a9d2e82012-05-09 21:16:12 -04005916 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5917 * @phba: pointer to lpfc hba data structure.
5918 *
5919 * This routine walks the list of els buffers that have been allocated and
5920 * repost them to the port by using SGL block post. This is needed after a
5921 * pci_function_reset/warm_start or start. It attempts to construct blocks
5922 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5923 * SGL block post mailbox commands to post them to the port. For single els
5924 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5925 * mailbox command for posting.
5926 *
5927 * Returns: 0 = success, non-zero failure.
5928 **/
5929static int
5930lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5931{
5932 struct lpfc_sglq *sglq_entry = NULL;
5933 struct lpfc_sglq *sglq_entry_next = NULL;
5934 struct lpfc_sglq *sglq_entry_first = NULL;
5935 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5936 int last_xritag = NO_XRI;
5937 LIST_HEAD(prep_sgl_list);
5938 LIST_HEAD(blck_sgl_list);
5939 LIST_HEAD(allc_sgl_list);
5940 LIST_HEAD(post_sgl_list);
5941 LIST_HEAD(free_sgl_list);
5942
5943 spin_lock(&phba->hbalock);
5944 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5945 spin_unlock(&phba->hbalock);
5946
5947 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5948 &allc_sgl_list, list) {
5949 list_del_init(&sglq_entry->list);
5950 block_cnt++;
5951 if ((last_xritag != NO_XRI) &&
5952 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5953 /* a hole in xri block, form a sgl posting block */
5954 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5955 post_cnt = block_cnt - 1;
5956 /* prepare list for next posting block */
5957 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5958 block_cnt = 1;
5959 } else {
5960 /* prepare list for next posting block */
5961 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5962 /* enough sgls for non-embed sgl mbox command */
5963 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5964 list_splice_init(&prep_sgl_list,
5965 &blck_sgl_list);
5966 post_cnt = block_cnt;
5967 block_cnt = 0;
5968 }
5969 }
5970 num_posted++;
5971
5972 /* keep track of last sgl's xritag */
5973 last_xritag = sglq_entry->sli4_xritag;
5974
5975 /* end of repost sgl list condition for els buffers */
5976 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5977 if (post_cnt == 0) {
5978 list_splice_init(&prep_sgl_list,
5979 &blck_sgl_list);
5980 post_cnt = block_cnt;
5981 } else if (block_cnt == 1) {
5982 status = lpfc_sli4_post_sgl(phba,
5983 sglq_entry->phys, 0,
5984 sglq_entry->sli4_xritag);
5985 if (!status) {
5986 /* successful, put sgl to posted list */
5987 list_add_tail(&sglq_entry->list,
5988 &post_sgl_list);
5989 } else {
5990 /* Failure, put sgl to free list */
5991 lpfc_printf_log(phba, KERN_WARNING,
5992 LOG_SLI,
5993 "3159 Failed to post els "
5994 "sgl, xritag:x%x\n",
5995 sglq_entry->sli4_xritag);
5996 list_add_tail(&sglq_entry->list,
5997 &free_sgl_list);
5998 spin_lock_irq(&phba->hbalock);
5999 phba->sli4_hba.els_xri_cnt--;
6000 spin_unlock_irq(&phba->hbalock);
6001 }
6002 }
6003 }
6004
6005 /* continue until a nembed page worth of sgls */
6006 if (post_cnt == 0)
6007 continue;
6008
6009 /* post the els buffer list sgls as a block */
6010 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6011 post_cnt);
6012
6013 if (!status) {
6014 /* success, put sgl list to posted sgl list */
6015 list_splice_init(&blck_sgl_list, &post_sgl_list);
6016 } else {
6017 /* Failure, put sgl list to free sgl list */
6018 sglq_entry_first = list_first_entry(&blck_sgl_list,
6019 struct lpfc_sglq,
6020 list);
6021 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6022 "3160 Failed to post els sgl-list, "
6023 "xritag:x%x-x%x\n",
6024 sglq_entry_first->sli4_xritag,
6025 (sglq_entry_first->sli4_xritag +
6026 post_cnt - 1));
6027 list_splice_init(&blck_sgl_list, &free_sgl_list);
6028 spin_lock_irq(&phba->hbalock);
6029 phba->sli4_hba.els_xri_cnt -= post_cnt;
6030 spin_unlock_irq(&phba->hbalock);
6031 }
6032
6033 /* don't reset xirtag due to hole in xri block */
6034 if (block_cnt == 0)
6035 last_xritag = NO_XRI;
6036
6037 /* reset els sgl post count for next round of posting */
6038 post_cnt = 0;
6039 }
6040
6041 /* free the els sgls failed to post */
6042 lpfc_free_sgl_list(phba, &free_sgl_list);
6043
6044 /* push els sgls posted to the availble list */
6045 if (!list_empty(&post_sgl_list)) {
6046 spin_lock(&phba->hbalock);
6047 list_splice_init(&post_sgl_list,
6048 &phba->sli4_hba.lpfc_sgl_list);
6049 spin_unlock(&phba->hbalock);
6050 } else {
6051 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6052 "3161 Failure to post els sgl to port.\n");
6053 return -EIO;
6054 }
6055 return 0;
6056}
6057
6058/**
James Smartda0436e2009-05-22 14:51:39 -04006059 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6060 * @phba: Pointer to HBA context object.
6061 *
6062 * This function is the main SLI4 device intialization PCI function. This
6063 * function is called by the HBA intialization code, HBA reset code and
6064 * HBA error attention handler code. Caller is not required to hold any
6065 * locks.
6066 **/
6067int
6068lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6069{
6070 int rc;
6071 LPFC_MBOXQ_t *mboxq;
6072 struct lpfc_mqe *mqe;
6073 uint8_t *vpd;
6074 uint32_t vpd_size;
6075 uint32_t ftr_rsp = 0;
6076 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6077 struct lpfc_vport *vport = phba->pport;
6078 struct lpfc_dmabuf *mp;
6079
6080 /* Perform a PCI function reset to start from clean */
6081 rc = lpfc_pci_function_reset(phba);
6082 if (unlikely(rc))
6083 return -ENODEV;
6084
6085 /* Check the HBA Host Status Register for readyness */
6086 rc = lpfc_sli4_post_status_check(phba);
6087 if (unlikely(rc))
6088 return -ENODEV;
6089 else {
6090 spin_lock_irq(&phba->hbalock);
6091 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6092 spin_unlock_irq(&phba->hbalock);
6093 }
6094
6095 /*
6096 * Allocate a single mailbox container for initializing the
6097 * port.
6098 */
6099 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6100 if (!mboxq)
6101 return -ENOMEM;
6102
James Smartda0436e2009-05-22 14:51:39 -04006103 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006104 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006105 vpd = kzalloc(vpd_size, GFP_KERNEL);
6106 if (!vpd) {
6107 rc = -ENOMEM;
6108 goto out_free_mbox;
6109 }
6110
6111 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006112 if (unlikely(rc)) {
6113 kfree(vpd);
6114 goto out_free_mbox;
6115 }
James Smartda0436e2009-05-22 14:51:39 -04006116 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006117 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6118 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006119 phba->hba_flag |= HBA_FCOE_MODE;
6120 else
6121 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006122
6123 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6124 LPFC_DCBX_CEE_MODE)
6125 phba->hba_flag |= HBA_FIP_SUPPORT;
6126 else
6127 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6128
James Smart4f2e66c2012-05-09 21:17:07 -04006129 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6130
James Smartc31098c2011-04-16 11:03:33 -04006131 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6133 "0376 READ_REV Error. SLI Level %d "
6134 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006135 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006136 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006137 kfree(vpd);
6138 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006139 }
James Smartcd1c8302011-10-10 21:33:25 -04006140
6141 /*
James Smartff78d8f2011-12-13 13:21:35 -05006142 * Continue initialization with default values even if driver failed
6143 * to read FCoE param config regions, only read parameters if the
6144 * board is FCoE
6145 */
6146 if (phba->hba_flag & HBA_FCOE_MODE &&
6147 lpfc_sli4_read_fcoe_params(phba))
6148 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6149 "2570 Failed to read FCoE parameters\n");
6150
6151 /*
James Smartcd1c8302011-10-10 21:33:25 -04006152 * Retrieve sli4 device physical port name, failure of doing it
6153 * is considered as non-fatal.
6154 */
6155 rc = lpfc_sli4_retrieve_pport_name(phba);
6156 if (!rc)
6157 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6158 "3080 Successful retrieving SLI4 device "
6159 "physical port name: %s.\n", phba->Port);
6160
James Smartda0436e2009-05-22 14:51:39 -04006161 /*
6162 * Evaluate the read rev and vpd data. Populate the driver
6163 * state with the results. If this routine fails, the failure
6164 * is not fatal as the driver will use generic values.
6165 */
6166 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6167 if (unlikely(!rc)) {
6168 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6169 "0377 Error %d parsing vpd. "
6170 "Using defaults.\n", rc);
6171 rc = 0;
6172 }
James Smart76a95d72010-11-20 23:11:48 -05006173 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006174
James Smartf1126682009-06-10 17:22:44 -04006175 /* Save information as VPD data */
6176 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6177 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6178 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6179 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6180 &mqe->un.read_rev);
6181 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6182 &mqe->un.read_rev);
6183 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6184 &mqe->un.read_rev);
6185 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6186 &mqe->un.read_rev);
6187 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6188 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6189 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6190 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6191 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6192 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6193 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6194 "(%d):0380 READ_REV Status x%x "
6195 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6196 mboxq->vport ? mboxq->vport->vpi : 0,
6197 bf_get(lpfc_mqe_status, mqe),
6198 phba->vpd.rev.opFwName,
6199 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6200 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006201
6202 /*
6203 * Discover the port's supported feature set and match it against the
6204 * hosts requests.
6205 */
6206 lpfc_request_features(phba, mboxq);
6207 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6208 if (unlikely(rc)) {
6209 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006210 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006211 }
6212
6213 /*
6214 * The port must support FCP initiator mode as this is the
6215 * only mode running in the host.
6216 */
6217 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6218 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6219 "0378 No support for fcpi mode.\n");
6220 ftr_rsp++;
6221 }
James Smartfedd3b72011-02-16 12:39:24 -05006222 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6223 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6224 else
6225 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006226 /*
6227 * If the port cannot support the host's requested features
6228 * then turn off the global config parameters to disable the
6229 * feature in the driver. This is not a fatal error.
6230 */
James Smartbf086112011-08-21 21:48:13 -04006231 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6232 if (phba->cfg_enable_bg) {
6233 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6234 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6235 else
6236 ftr_rsp++;
6237 }
James Smartda0436e2009-05-22 14:51:39 -04006238
6239 if (phba->max_vpi && phba->cfg_enable_npiv &&
6240 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6241 ftr_rsp++;
6242
6243 if (ftr_rsp) {
6244 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6245 "0379 Feature Mismatch Data: x%08x %08x "
6246 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6247 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6248 phba->cfg_enable_npiv, phba->max_vpi);
6249 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6250 phba->cfg_enable_bg = 0;
6251 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6252 phba->cfg_enable_npiv = 0;
6253 }
6254
6255 /* These SLI3 features are assumed in SLI4 */
6256 spin_lock_irq(&phba->hbalock);
6257 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6258 spin_unlock_irq(&phba->hbalock);
6259
James Smart6d368e52011-05-24 11:44:12 -04006260 /*
6261 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6262 * calls depends on these resources to complete port setup.
6263 */
6264 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6265 if (rc) {
6266 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6267 "2920 Failed to alloc Resource IDs "
6268 "rc = x%x\n", rc);
6269 goto out_free_mbox;
6270 }
6271
James Smartda0436e2009-05-22 14:51:39 -04006272 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006273 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6274 if (rc) {
6275 phba->link_state = LPFC_HBA_ERROR;
6276 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006277 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006278 }
6279
James Smartda0436e2009-05-22 14:51:39 -04006280 mboxq->vport = vport;
6281 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6282 mp = (struct lpfc_dmabuf *) mboxq->context1;
6283 if (rc == MBX_SUCCESS) {
6284 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6285 rc = 0;
6286 }
6287
6288 /*
6289 * This memory was allocated by the lpfc_read_sparam routine. Release
6290 * it to the mbuf pool.
6291 */
6292 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6293 kfree(mp);
6294 mboxq->context1 = NULL;
6295 if (unlikely(rc)) {
6296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6297 "0382 READ_SPARAM command failed "
6298 "status %d, mbxStatus x%x\n",
6299 rc, bf_get(lpfc_mqe_status, mqe));
6300 phba->link_state = LPFC_HBA_ERROR;
6301 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006302 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006303 }
6304
James Smart05580562011-05-24 11:40:48 -04006305 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006306
6307 /* Update the fc_host data structures with new wwn. */
6308 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6309 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6310
James Smart8a9d2e82012-05-09 21:16:12 -04006311 /* update host els and scsi xri-sgl sizes and mappings */
6312 rc = lpfc_sli4_xri_sgl_update(phba);
6313 if (unlikely(rc)) {
6314 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6315 "1400 Failed to update xri-sgl size and "
6316 "mapping: %d\n", rc);
6317 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006318 }
6319
James Smart8a9d2e82012-05-09 21:16:12 -04006320 /* register the els sgl pool to the port */
6321 rc = lpfc_sli4_repost_els_sgl_list(phba);
6322 if (unlikely(rc)) {
6323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6324 "0582 Error %d during els sgl post "
6325 "operation\n", rc);
6326 rc = -ENODEV;
6327 goto out_free_mbox;
6328 }
6329
6330 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006331 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6332 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006333 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006334 "0383 Error %d during scsi sgl post "
6335 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006336 /* Some Scsi buffers were moved to the abort scsi list */
6337 /* A pci function reset will repost them */
6338 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006339 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006340 }
6341
6342 /* Post the rpi header region to the device. */
6343 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6344 if (unlikely(rc)) {
6345 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6346 "0393 Error %d during rpi post operation\n",
6347 rc);
6348 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006349 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006350 }
James Smart97f2ecf2012-03-01 22:35:23 -05006351 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006352
James Smart5350d872011-10-10 21:33:49 -04006353 /* Create all the SLI4 queues */
6354 rc = lpfc_sli4_queue_create(phba);
6355 if (rc) {
6356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6357 "3089 Failed to allocate queues\n");
6358 rc = -ENODEV;
6359 goto out_stop_timers;
6360 }
James Smartda0436e2009-05-22 14:51:39 -04006361 /* Set up all the queues to the device */
6362 rc = lpfc_sli4_queue_setup(phba);
6363 if (unlikely(rc)) {
6364 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6365 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006366 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006367 }
6368
6369 /* Arm the CQs and then EQs on device */
6370 lpfc_sli4_arm_cqeq_intr(phba);
6371
6372 /* Indicate device interrupt mode */
6373 phba->sli4_hba.intr_enable = 1;
6374
6375 /* Allow asynchronous mailbox command to go through */
6376 spin_lock_irq(&phba->hbalock);
6377 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6378 spin_unlock_irq(&phba->hbalock);
6379
6380 /* Post receive buffers to the device */
6381 lpfc_sli4_rb_setup(phba);
6382
James Smartfc2b9892010-02-26 14:15:29 -05006383 /* Reset HBA FCF states after HBA reset */
6384 phba->fcf.fcf_flag = 0;
6385 phba->fcf.current_rec.flag = 0;
6386
James Smartda0436e2009-05-22 14:51:39 -04006387 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006388 mod_timer(&vport->els_tmofunc,
6389 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006390
6391 /* Start heart beat timer */
6392 mod_timer(&phba->hb_tmofunc,
6393 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6394 phba->hb_outstanding = 0;
6395 phba->last_completion_time = jiffies;
6396
6397 /* Start error attention (ERATT) polling timer */
6398 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6399
James Smart75baf692010-06-08 18:31:21 -04006400 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6401 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6402 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6403 if (!rc) {
6404 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6405 "2829 This device supports "
6406 "Advanced Error Reporting (AER)\n");
6407 spin_lock_irq(&phba->hbalock);
6408 phba->hba_flag |= HBA_AER_ENABLED;
6409 spin_unlock_irq(&phba->hbalock);
6410 } else {
6411 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6412 "2830 This device does not support "
6413 "Advanced Error Reporting (AER)\n");
6414 phba->cfg_aer_support = 0;
6415 }
James Smart0a96e972011-07-22 18:37:28 -04006416 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006417 }
6418
James Smart76a95d72010-11-20 23:11:48 -05006419 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6420 /*
6421 * The FC Port needs to register FCFI (index 0)
6422 */
6423 lpfc_reg_fcfi(phba, mboxq);
6424 mboxq->vport = phba->pport;
6425 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006426 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006427 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006428 rc = 0;
6429 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6430 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006431
6432 /* Check if the port is configured to be disabled */
6433 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006434 }
James Smart026abb82011-12-13 13:20:45 -05006435
James Smartda0436e2009-05-22 14:51:39 -04006436 /*
6437 * The port is ready, set the host's link state to LINK_DOWN
6438 * in preparation for link interrupts.
6439 */
James Smartda0436e2009-05-22 14:51:39 -04006440 spin_lock_irq(&phba->hbalock);
6441 phba->link_state = LPFC_LINK_DOWN;
6442 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006443 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6444 (phba->hba_flag & LINK_DISABLED)) {
6445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6446 "3103 Adapter Link is disabled.\n");
6447 lpfc_down_link(phba, mboxq);
6448 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6449 if (rc != MBX_SUCCESS) {
6450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6451 "3104 Adapter failed to issue "
6452 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6453 goto out_unset_queue;
6454 }
6455 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006456 /* don't perform init_link on SLI4 FC port loopback test */
6457 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6458 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6459 if (rc)
6460 goto out_unset_queue;
6461 }
James Smart5350d872011-10-10 21:33:49 -04006462 }
6463 mempool_free(mboxq, phba->mbox_mem_pool);
6464 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006465out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006466 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006467 lpfc_sli4_queue_unset(phba);
6468out_destroy_queue:
6469 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006470out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006471 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006472out_free_mbox:
6473 mempool_free(mboxq, phba->mbox_mem_pool);
6474 return rc;
6475}
James Smarte59058c2008-08-24 21:49:00 -04006476
6477/**
James Smart3621a712009-04-06 18:47:14 -04006478 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006479 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006480 *
James Smarte59058c2008-08-24 21:49:00 -04006481 * This is the callback function for mailbox timer. The mailbox
6482 * timer is armed when a new mailbox command is issued and the timer
6483 * is deleted when the mailbox complete. The function is called by
6484 * the kernel timer code when a mailbox does not complete within
6485 * expected time. This function wakes up the worker thread to
6486 * process the mailbox timeout and returns. All the processing is
6487 * done by the worker thread function lpfc_mbox_timeout_handler.
6488 **/
dea31012005-04-17 16:05:31 -05006489void
6490lpfc_mbox_timeout(unsigned long ptr)
6491{
James Smart92d7f7b2007-06-17 19:56:38 -05006492 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006493 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006494 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006495
James Smart2e0fef82007-06-17 19:56:36 -05006496 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006497 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006498 if (!tmo_posted)
6499 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6500 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6501
James Smart5e9d9b82008-06-14 22:52:53 -04006502 if (!tmo_posted)
6503 lpfc_worker_wake_up(phba);
6504 return;
dea31012005-04-17 16:05:31 -05006505}
6506
James Smarte59058c2008-08-24 21:49:00 -04006507
6508/**
James Smart3621a712009-04-06 18:47:14 -04006509 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006510 * @phba: Pointer to HBA context object.
6511 *
6512 * This function is called from worker thread when a mailbox command times out.
6513 * The caller is not required to hold any locks. This function will reset the
6514 * HBA and recover all the pending commands.
6515 **/
dea31012005-04-17 16:05:31 -05006516void
6517lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6518{
James Smart2e0fef82007-06-17 19:56:36 -05006519 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006520 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006521 struct lpfc_sli *psli = &phba->sli;
6522 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006523
James Smarta257bf92009-04-06 18:48:10 -04006524 /* Check the pmbox pointer first. There is a race condition
6525 * between the mbox timeout handler getting executed in the
6526 * worklist and the mailbox actually completing. When this
6527 * race condition occurs, the mbox_active will be NULL.
6528 */
6529 spin_lock_irq(&phba->hbalock);
6530 if (pmbox == NULL) {
6531 lpfc_printf_log(phba, KERN_WARNING,
6532 LOG_MBOX | LOG_SLI,
6533 "0353 Active Mailbox cleared - mailbox timeout "
6534 "exiting\n");
6535 spin_unlock_irq(&phba->hbalock);
6536 return;
6537 }
6538
dea31012005-04-17 16:05:31 -05006539 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006540 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006541 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006542 mb->mbxCommand,
6543 phba->pport->port_state,
6544 phba->sli.sli_flag,
6545 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006546 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006547
James Smart1dcb58e2007-04-25 09:51:30 -04006548 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6549 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006550 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006551 */
James Smart2e0fef82007-06-17 19:56:36 -05006552 spin_lock_irq(&phba->pport->work_port_lock);
6553 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6554 spin_unlock_irq(&phba->pport->work_port_lock);
6555 spin_lock_irq(&phba->hbalock);
6556 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006557 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006558 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006559
6560 pring = &psli->ring[psli->fcp_ring];
6561 lpfc_sli_abort_iocb_ring(phba, pring);
6562
6563 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006564 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006565
6566 /* Reset the HBA device */
6567 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006568}
6569
James Smarte59058c2008-08-24 21:49:00 -04006570/**
James Smart3772a992009-05-22 14:50:54 -04006571 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006572 * @phba: Pointer to HBA context object.
6573 * @pmbox: Pointer to mailbox object.
6574 * @flag: Flag indicating how the mailbox need to be processed.
6575 *
6576 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006577 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6578 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006579 * The mailbox command can be submitted in polling mode, in which case
6580 * this function will wait in a polling loop for the completion of the
6581 * mailbox.
6582 * If the mailbox is submitted in no_wait mode (not polling) the
6583 * function will submit the command and returns immediately without waiting
6584 * for the mailbox completion. The no_wait is supported only when HBA
6585 * is in SLI2/SLI3 mode - interrupts are enabled.
6586 * The SLI interface allows only one mailbox pending at a time. If the
6587 * mailbox is issued in polling mode and there is already a mailbox
6588 * pending, then the function will return an error. If the mailbox is issued
6589 * in NO_WAIT mode and there is a mailbox pending already, the function
6590 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6591 * The sli layer owns the mailbox object until the completion of mailbox
6592 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6593 * return codes the caller owns the mailbox command after the return of
6594 * the function.
6595 **/
James Smart3772a992009-05-22 14:50:54 -04006596static int
6597lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6598 uint32_t flag)
dea31012005-04-17 16:05:31 -05006599{
dea31012005-04-17 16:05:31 -05006600 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006601 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006602 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006603 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006604 int i;
James Smart09372822008-01-11 01:52:54 -05006605 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006606 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006607 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006608 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006609 int processing_queue = 0;
6610
6611 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6612 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006613 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006614 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006615 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6616 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6617 return MBX_SUCCESS;
6618 }
James Smart58da1ff2008-04-07 10:15:56 -04006619 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006620 pmbox = lpfc_mbox_get(phba);
6621 if (!pmbox) {
6622 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6623 return MBX_SUCCESS;
6624 }
6625 }
dea31012005-04-17 16:05:31 -05006626
James Smarted957682007-06-17 19:56:37 -05006627 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006628 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006629 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006630 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006631 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006632 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006633 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006634 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006635 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006636 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006637 }
6638 }
6639
Linas Vepstas8d63f372007-02-14 14:28:36 -06006640 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006641 if (unlikely(pci_channel_offline(phba->pcidev))) {
6642 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6643 goto out_not_finished;
6644 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006645
James Smarta257bf92009-04-06 18:48:10 -04006646 /* If HBA has a deferred error attention, fail the iocb. */
6647 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6648 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6649 goto out_not_finished;
6650 }
6651
dea31012005-04-17 16:05:31 -05006652 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006653
James Smart3772a992009-05-22 14:50:54 -04006654 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006655 status = MBX_SUCCESS;
6656
James Smart2e0fef82007-06-17 19:56:36 -05006657 if (phba->link_state == LPFC_HBA_ERROR) {
6658 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006659
6660 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006661 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6662 "(%d):0311 Mailbox command x%x cannot "
6663 "issue Data: x%x x%x\n",
6664 pmbox->vport ? pmbox->vport->vpi : 0,
6665 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006666 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006667 }
6668
James Smart9940b972011-03-11 16:06:12 -05006669 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6670 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6671 !(hc_copy & HC_MBINT_ENA)) {
6672 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6673 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006674 "(%d):2528 Mailbox command x%x cannot "
6675 "issue Data: x%x x%x\n",
6676 pmbox->vport ? pmbox->vport->vpi : 0,
6677 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006678 goto out_not_finished;
6679 }
James Smart92908312006-03-07 15:04:13 -05006680 }
6681
dea31012005-04-17 16:05:31 -05006682 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6683 /* Polling for a mbox command when another one is already active
6684 * is not allowed in SLI. Also, the driver must have established
6685 * SLI2 mode to queue and process multiple mbox commands.
6686 */
6687
6688 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006689 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006690
6691 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006692 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6693 "(%d):2529 Mailbox command x%x "
6694 "cannot issue Data: x%x x%x\n",
6695 pmbox->vport ? pmbox->vport->vpi : 0,
6696 pmbox->u.mb.mbxCommand,
6697 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006698 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006699 }
6700
James Smart3772a992009-05-22 14:50:54 -04006701 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006702 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006703 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006704 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6705 "(%d):2530 Mailbox command x%x "
6706 "cannot issue Data: x%x x%x\n",
6707 pmbox->vport ? pmbox->vport->vpi : 0,
6708 pmbox->u.mb.mbxCommand,
6709 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006710 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006711 }
6712
dea31012005-04-17 16:05:31 -05006713 /* Another mailbox command is still being processed, queue this
6714 * command to be processed later.
6715 */
6716 lpfc_mbox_put(phba, pmbox);
6717
6718 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006719 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006720 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006721 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006722 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6723 mb->mbxCommand, phba->pport->port_state,
6724 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006725
6726 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006727 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006728
James Smart858c9f62007-06-17 19:56:39 -05006729 if (pmbox->vport) {
6730 lpfc_debugfs_disc_trc(pmbox->vport,
6731 LPFC_DISC_TRC_MBOX_VPORT,
6732 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6733 (uint32_t)mb->mbxCommand,
6734 mb->un.varWords[0], mb->un.varWords[1]);
6735 }
6736 else {
6737 lpfc_debugfs_disc_trc(phba->pport,
6738 LPFC_DISC_TRC_MBOX,
6739 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6740 (uint32_t)mb->mbxCommand,
6741 mb->un.varWords[0], mb->un.varWords[1]);
6742 }
6743
James Smart2e0fef82007-06-17 19:56:36 -05006744 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006745 }
6746
dea31012005-04-17 16:05:31 -05006747 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6748
6749 /* If we are not polling, we MUST be in SLI2 mode */
6750 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006751 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006752 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006753 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006754 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006755 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006756 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6757 "(%d):2531 Mailbox command x%x "
6758 "cannot issue Data: x%x x%x\n",
6759 pmbox->vport ? pmbox->vport->vpi : 0,
6760 pmbox->u.mb.mbxCommand,
6761 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006762 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006763 }
6764 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006765 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006766 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006767 }
6768
6769 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006770 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006771 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006772 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006773 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006774 mb->mbxCommand, phba->pport->port_state,
6775 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006776
James Smart858c9f62007-06-17 19:56:39 -05006777 if (mb->mbxCommand != MBX_HEARTBEAT) {
6778 if (pmbox->vport) {
6779 lpfc_debugfs_disc_trc(pmbox->vport,
6780 LPFC_DISC_TRC_MBOX_VPORT,
6781 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6782 (uint32_t)mb->mbxCommand,
6783 mb->un.varWords[0], mb->un.varWords[1]);
6784 }
6785 else {
6786 lpfc_debugfs_disc_trc(phba->pport,
6787 LPFC_DISC_TRC_MBOX,
6788 "MBOX Send: cmd:x%x mb:x%x x%x",
6789 (uint32_t)mb->mbxCommand,
6790 mb->un.varWords[0], mb->un.varWords[1]);
6791 }
6792 }
6793
dea31012005-04-17 16:05:31 -05006794 psli->slistat.mbox_cmd++;
6795 evtctr = psli->slistat.mbox_event;
6796
6797 /* next set own bit for the adapter and copy over command word */
6798 mb->mbxOwner = OWN_CHIP;
6799
James Smart3772a992009-05-22 14:50:54 -04006800 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006801 /* Populate mbox extension offset word. */
6802 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6803 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6804 = (uint8_t *)phba->mbox_ext
6805 - (uint8_t *)phba->mbox;
6806 }
6807
6808 /* Copy the mailbox extension data */
6809 if (pmbox->in_ext_byte_len && pmbox->context2) {
6810 lpfc_sli_pcimem_bcopy(pmbox->context2,
6811 (uint8_t *)phba->mbox_ext,
6812 pmbox->in_ext_byte_len);
6813 }
6814 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006815 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006816 } else {
James Smart7a470272010-03-15 11:25:20 -04006817 /* Populate mbox extension offset word. */
6818 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6819 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6820 = MAILBOX_HBA_EXT_OFFSET;
6821
6822 /* Copy the mailbox extension data */
6823 if (pmbox->in_ext_byte_len && pmbox->context2) {
6824 lpfc_memcpy_to_slim(phba->MBslimaddr +
6825 MAILBOX_HBA_EXT_OFFSET,
6826 pmbox->context2, pmbox->in_ext_byte_len);
6827
6828 }
James Smart92908312006-03-07 15:04:13 -05006829 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006830 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006831 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006832 }
6833
6834 /* First copy mbox command data to HBA SLIM, skip past first
6835 word */
6836 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6837 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6838 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6839
6840 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006841 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006842 to_slim = phba->MBslimaddr;
6843 writel(ldata, to_slim);
6844 readl(to_slim); /* flush */
6845
6846 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6847 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006848 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006849 }
6850 }
6851
6852 wmb();
dea31012005-04-17 16:05:31 -05006853
6854 switch (flag) {
6855 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006856 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006857 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006858 /* Interrupt board to do it */
6859 writel(CA_MBATT, phba->CAregaddr);
6860 readl(phba->CAregaddr); /* flush */
6861 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006862 break;
6863
6864 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006865 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006866 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006867 /* Interrupt board to do it */
6868 writel(CA_MBATT, phba->CAregaddr);
6869 readl(phba->CAregaddr); /* flush */
6870
James Smart3772a992009-05-22 14:50:54 -04006871 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006872 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006873 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006874 word0 = le32_to_cpu(word0);
6875 } else {
6876 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006877 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6878 spin_unlock_irqrestore(&phba->hbalock,
6879 drvr_flag);
6880 goto out_not_finished;
6881 }
dea31012005-04-17 16:05:31 -05006882 }
6883
6884 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006885 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6886 spin_unlock_irqrestore(&phba->hbalock,
6887 drvr_flag);
6888 goto out_not_finished;
6889 }
James Smarta183a152011-10-10 21:32:43 -04006890 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6891 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006892 i = 0;
dea31012005-04-17 16:05:31 -05006893 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006894 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6895 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006896 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006897 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006898 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006899 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006900 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006901 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006902 }
6903
6904 /* Check if we took a mbox interrupt while we were
6905 polling */
6906 if (((word0 & OWN_CHIP) != OWN_CHIP)
6907 && (evtctr != psli->slistat.mbox_event))
6908 break;
6909
James Smart09372822008-01-11 01:52:54 -05006910 if (i++ > 10) {
6911 spin_unlock_irqrestore(&phba->hbalock,
6912 drvr_flag);
6913 msleep(1);
6914 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6915 }
dea31012005-04-17 16:05:31 -05006916
James Smart3772a992009-05-22 14:50:54 -04006917 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006918 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006919 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006920 word0 = le32_to_cpu(word0);
6921 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6922 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006923 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006924 /* Check real SLIM for any errors */
6925 slimword0 = readl(phba->MBslimaddr);
6926 slimmb = (MAILBOX_t *) & slimword0;
6927 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6928 && slimmb->mbxStatus) {
6929 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006930 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006931 word0 = slimword0;
6932 }
6933 }
6934 } else {
6935 /* First copy command data */
6936 word0 = readl(phba->MBslimaddr);
6937 }
6938 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006939 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6940 spin_unlock_irqrestore(&phba->hbalock,
6941 drvr_flag);
6942 goto out_not_finished;
6943 }
dea31012005-04-17 16:05:31 -05006944 }
6945
James Smart3772a992009-05-22 14:50:54 -04006946 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006947 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006948 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006949 /* Copy the mailbox extension data */
6950 if (pmbox->out_ext_byte_len && pmbox->context2) {
6951 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6952 pmbox->context2,
6953 pmbox->out_ext_byte_len);
6954 }
dea31012005-04-17 16:05:31 -05006955 } else {
6956 /* First copy command data */
6957 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6958 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006959 /* Copy the mailbox extension data */
6960 if (pmbox->out_ext_byte_len && pmbox->context2) {
6961 lpfc_memcpy_from_slim(pmbox->context2,
6962 phba->MBslimaddr +
6963 MAILBOX_HBA_EXT_OFFSET,
6964 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006965 }
6966 }
6967
6968 writel(HA_MBATT, phba->HAregaddr);
6969 readl(phba->HAregaddr); /* flush */
6970
6971 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6972 status = mb->mbxStatus;
6973 }
6974
James Smart2e0fef82007-06-17 19:56:36 -05006975 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6976 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006977
6978out_not_finished:
6979 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006980 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006981 lpfc_mbox_cmpl_put(phba, pmbox);
6982 }
6983 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006984}
6985
James Smarte59058c2008-08-24 21:49:00 -04006986/**
James Smartf1126682009-06-10 17:22:44 -04006987 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6988 * @phba: Pointer to HBA context object.
6989 *
6990 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6991 * the driver internal pending mailbox queue. It will then try to wait out the
6992 * possible outstanding mailbox command before return.
6993 *
6994 * Returns:
6995 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6996 * the outstanding mailbox command timed out.
6997 **/
6998static int
6999lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7000{
7001 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007002 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007003 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007004
7005 /* Mark the asynchronous mailbox command posting as blocked */
7006 spin_lock_irq(&phba->hbalock);
7007 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007008 /* Determine how long we might wait for the active mailbox
7009 * command to be gracefully completed by firmware.
7010 */
James Smarta183a152011-10-10 21:32:43 -04007011 if (phba->sli.mbox_active)
7012 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7013 phba->sli.mbox_active) *
7014 1000) + jiffies;
7015 spin_unlock_irq(&phba->hbalock);
7016
James Smartf1126682009-06-10 17:22:44 -04007017 /* Wait for the outstnading mailbox command to complete */
7018 while (phba->sli.mbox_active) {
7019 /* Check active mailbox complete status every 2ms */
7020 msleep(2);
7021 if (time_after(jiffies, timeout)) {
7022 /* Timeout, marked the outstanding cmd not complete */
7023 rc = 1;
7024 break;
7025 }
7026 }
7027
7028 /* Can not cleanly block async mailbox command, fails it */
7029 if (rc) {
7030 spin_lock_irq(&phba->hbalock);
7031 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7032 spin_unlock_irq(&phba->hbalock);
7033 }
7034 return rc;
7035}
7036
7037/**
7038 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7039 * @phba: Pointer to HBA context object.
7040 *
7041 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7042 * commands from the driver internal pending mailbox queue. It makes sure
7043 * that there is no outstanding mailbox command before resuming posting
7044 * asynchronous mailbox commands. If, for any reason, there is outstanding
7045 * mailbox command, it will try to wait it out before resuming asynchronous
7046 * mailbox command posting.
7047 **/
7048static void
7049lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7050{
7051 struct lpfc_sli *psli = &phba->sli;
7052
7053 spin_lock_irq(&phba->hbalock);
7054 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7055 /* Asynchronous mailbox posting is not blocked, do nothing */
7056 spin_unlock_irq(&phba->hbalock);
7057 return;
7058 }
7059
7060 /* Outstanding synchronous mailbox command is guaranteed to be done,
7061 * successful or timeout, after timing-out the outstanding mailbox
7062 * command shall always be removed, so just unblock posting async
7063 * mailbox command and resume
7064 */
7065 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7066 spin_unlock_irq(&phba->hbalock);
7067
7068 /* wake up worker thread to post asynchronlous mailbox command */
7069 lpfc_worker_wake_up(phba);
7070}
7071
7072/**
James Smartda0436e2009-05-22 14:51:39 -04007073 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7074 * @phba: Pointer to HBA context object.
7075 * @mboxq: Pointer to mailbox object.
7076 *
7077 * The function posts a mailbox to the port. The mailbox is expected
7078 * to be comletely filled in and ready for the port to operate on it.
7079 * This routine executes a synchronous completion operation on the
7080 * mailbox by polling for its completion.
7081 *
7082 * The caller must not be holding any locks when calling this routine.
7083 *
7084 * Returns:
7085 * MBX_SUCCESS - mailbox posted successfully
7086 * Any of the MBX error values.
7087 **/
7088static int
7089lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7090{
7091 int rc = MBX_SUCCESS;
7092 unsigned long iflag;
7093 uint32_t db_ready;
7094 uint32_t mcqe_status;
7095 uint32_t mbx_cmnd;
7096 unsigned long timeout;
7097 struct lpfc_sli *psli = &phba->sli;
7098 struct lpfc_mqe *mb = &mboxq->u.mqe;
7099 struct lpfc_bmbx_create *mbox_rgn;
7100 struct dma_address *dma_address;
7101 struct lpfc_register bmbx_reg;
7102
7103 /*
7104 * Only one mailbox can be active to the bootstrap mailbox region
7105 * at a time and there is no queueing provided.
7106 */
7107 spin_lock_irqsave(&phba->hbalock, iflag);
7108 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7109 spin_unlock_irqrestore(&phba->hbalock, iflag);
7110 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007111 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007112 "cannot issue Data: x%x x%x\n",
7113 mboxq->vport ? mboxq->vport->vpi : 0,
7114 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007115 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7116 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007117 psli->sli_flag, MBX_POLL);
7118 return MBXERR_ERROR;
7119 }
7120 /* The server grabs the token and owns it until release */
7121 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7122 phba->sli.mbox_active = mboxq;
7123 spin_unlock_irqrestore(&phba->hbalock, iflag);
7124
7125 /*
7126 * Initialize the bootstrap memory region to avoid stale data areas
7127 * in the mailbox post. Then copy the caller's mailbox contents to
7128 * the bmbx mailbox region.
7129 */
7130 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7131 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7132 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7133 sizeof(struct lpfc_mqe));
7134
7135 /* Post the high mailbox dma address to the port and wait for ready. */
7136 dma_address = &phba->sli4_hba.bmbx.dma_address;
7137 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7138
James Smarta183a152011-10-10 21:32:43 -04007139 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007140 * 1000) + jiffies;
7141 do {
7142 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7143 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7144 if (!db_ready)
7145 msleep(2);
7146
7147 if (time_after(jiffies, timeout)) {
7148 rc = MBXERR_ERROR;
7149 goto exit;
7150 }
7151 } while (!db_ready);
7152
7153 /* Post the low mailbox dma address to the port. */
7154 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04007155 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007156 * 1000) + jiffies;
7157 do {
7158 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7159 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7160 if (!db_ready)
7161 msleep(2);
7162
7163 if (time_after(jiffies, timeout)) {
7164 rc = MBXERR_ERROR;
7165 goto exit;
7166 }
7167 } while (!db_ready);
7168
7169 /*
7170 * Read the CQ to ensure the mailbox has completed.
7171 * If so, update the mailbox status so that the upper layers
7172 * can complete the request normally.
7173 */
7174 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7175 sizeof(struct lpfc_mqe));
7176 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7177 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7178 sizeof(struct lpfc_mcqe));
7179 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007180 /*
7181 * When the CQE status indicates a failure and the mailbox status
7182 * indicates success then copy the CQE status into the mailbox status
7183 * (and prefix it with x4000).
7184 */
James Smartda0436e2009-05-22 14:51:39 -04007185 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007186 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7187 bf_set(lpfc_mqe_status, mb,
7188 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007189 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007190 } else
7191 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007192
7193 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007194 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007195 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7196 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007197 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7198 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7199 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007200 bf_get(lpfc_mqe_status, mb),
7201 mb->un.mb_words[0], mb->un.mb_words[1],
7202 mb->un.mb_words[2], mb->un.mb_words[3],
7203 mb->un.mb_words[4], mb->un.mb_words[5],
7204 mb->un.mb_words[6], mb->un.mb_words[7],
7205 mb->un.mb_words[8], mb->un.mb_words[9],
7206 mb->un.mb_words[10], mb->un.mb_words[11],
7207 mb->un.mb_words[12], mboxq->mcqe.word0,
7208 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7209 mboxq->mcqe.trailer);
7210exit:
7211 /* We are holding the token, no needed for lock when release */
7212 spin_lock_irqsave(&phba->hbalock, iflag);
7213 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7214 phba->sli.mbox_active = NULL;
7215 spin_unlock_irqrestore(&phba->hbalock, iflag);
7216 return rc;
7217}
7218
7219/**
7220 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7221 * @phba: Pointer to HBA context object.
7222 * @pmbox: Pointer to mailbox object.
7223 * @flag: Flag indicating how the mailbox need to be processed.
7224 *
7225 * This function is called by discovery code and HBA management code to submit
7226 * a mailbox command to firmware with SLI-4 interface spec.
7227 *
7228 * Return codes the caller owns the mailbox command after the return of the
7229 * function.
7230 **/
7231static int
7232lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7233 uint32_t flag)
7234{
7235 struct lpfc_sli *psli = &phba->sli;
7236 unsigned long iflags;
7237 int rc;
7238
James Smartb76f2dc2011-07-22 18:37:42 -04007239 /* dump from issue mailbox command if setup */
7240 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7241
James Smart8fa38512009-07-19 10:01:03 -04007242 rc = lpfc_mbox_dev_check(phba);
7243 if (unlikely(rc)) {
7244 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007245 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007246 "cannot issue Data: x%x x%x\n",
7247 mboxq->vport ? mboxq->vport->vpi : 0,
7248 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007249 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7250 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007251 psli->sli_flag, flag);
7252 goto out_not_finished;
7253 }
7254
James Smartda0436e2009-05-22 14:51:39 -04007255 /* Detect polling mode and jump to a handler */
7256 if (!phba->sli4_hba.intr_enable) {
7257 if (flag == MBX_POLL)
7258 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7259 else
7260 rc = -EIO;
7261 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007262 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007263 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007264 "(x%x/x%x) failure: "
7265 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7266 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007267 mboxq->vport ? mboxq->vport->vpi : 0,
7268 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007269 lpfc_sli_config_mbox_subsys_get(phba,
7270 mboxq),
7271 lpfc_sli_config_mbox_opcode_get(phba,
7272 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007273 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7274 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7275 bf_get(lpfc_mcqe_ext_status,
7276 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007277 psli->sli_flag, flag);
7278 return rc;
7279 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007280 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7281 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007282 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007283 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007284 mboxq->vport ? mboxq->vport->vpi : 0,
7285 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007286 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7287 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007288 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007289 /* Try to block the asynchronous mailbox posting */
7290 rc = lpfc_sli4_async_mbox_block(phba);
7291 if (!rc) {
7292 /* Successfully blocked, now issue sync mbox cmd */
7293 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7294 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007295 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007296 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007297 "(%d):2597 Sync Mailbox command "
7298 "x%x (x%x/x%x) failure: "
7299 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7300 "Data: x%x x%x\n,",
7301 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007302 mboxq->u.mb.mbxCommand,
7303 lpfc_sli_config_mbox_subsys_get(phba,
7304 mboxq),
7305 lpfc_sli_config_mbox_opcode_get(phba,
7306 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007307 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7308 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7309 bf_get(lpfc_mcqe_ext_status,
7310 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007311 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007312 /* Unblock the async mailbox posting afterward */
7313 lpfc_sli4_async_mbox_unblock(phba);
7314 }
7315 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007316 }
7317
7318 /* Now, interrupt mode asynchrous mailbox command */
7319 rc = lpfc_mbox_cmd_check(phba, mboxq);
7320 if (rc) {
7321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007322 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007323 "cannot issue Data: x%x x%x\n",
7324 mboxq->vport ? mboxq->vport->vpi : 0,
7325 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007326 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7327 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007328 psli->sli_flag, flag);
7329 goto out_not_finished;
7330 }
James Smartda0436e2009-05-22 14:51:39 -04007331
7332 /* Put the mailbox command to the driver internal FIFO */
7333 psli->slistat.mbox_busy++;
7334 spin_lock_irqsave(&phba->hbalock, iflags);
7335 lpfc_mbox_put(phba, mboxq);
7336 spin_unlock_irqrestore(&phba->hbalock, iflags);
7337 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7338 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007339 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007340 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7341 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007342 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7343 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007344 phba->pport->port_state,
7345 psli->sli_flag, MBX_NOWAIT);
7346 /* Wake up worker thread to transport mailbox command from head */
7347 lpfc_worker_wake_up(phba);
7348
7349 return MBX_BUSY;
7350
7351out_not_finished:
7352 return MBX_NOT_FINISHED;
7353}
7354
7355/**
7356 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7357 * @phba: Pointer to HBA context object.
7358 *
7359 * This function is called by worker thread to send a mailbox command to
7360 * SLI4 HBA firmware.
7361 *
7362 **/
7363int
7364lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7365{
7366 struct lpfc_sli *psli = &phba->sli;
7367 LPFC_MBOXQ_t *mboxq;
7368 int rc = MBX_SUCCESS;
7369 unsigned long iflags;
7370 struct lpfc_mqe *mqe;
7371 uint32_t mbx_cmnd;
7372
7373 /* Check interrupt mode before post async mailbox command */
7374 if (unlikely(!phba->sli4_hba.intr_enable))
7375 return MBX_NOT_FINISHED;
7376
7377 /* Check for mailbox command service token */
7378 spin_lock_irqsave(&phba->hbalock, iflags);
7379 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7380 spin_unlock_irqrestore(&phba->hbalock, iflags);
7381 return MBX_NOT_FINISHED;
7382 }
7383 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7384 spin_unlock_irqrestore(&phba->hbalock, iflags);
7385 return MBX_NOT_FINISHED;
7386 }
7387 if (unlikely(phba->sli.mbox_active)) {
7388 spin_unlock_irqrestore(&phba->hbalock, iflags);
7389 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7390 "0384 There is pending active mailbox cmd\n");
7391 return MBX_NOT_FINISHED;
7392 }
7393 /* Take the mailbox command service token */
7394 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7395
7396 /* Get the next mailbox command from head of queue */
7397 mboxq = lpfc_mbox_get(phba);
7398
7399 /* If no more mailbox command waiting for post, we're done */
7400 if (!mboxq) {
7401 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7402 spin_unlock_irqrestore(&phba->hbalock, iflags);
7403 return MBX_SUCCESS;
7404 }
7405 phba->sli.mbox_active = mboxq;
7406 spin_unlock_irqrestore(&phba->hbalock, iflags);
7407
7408 /* Check device readiness for posting mailbox command */
7409 rc = lpfc_mbox_dev_check(phba);
7410 if (unlikely(rc))
7411 /* Driver clean routine will clean up pending mailbox */
7412 goto out_not_finished;
7413
7414 /* Prepare the mbox command to be posted */
7415 mqe = &mboxq->u.mqe;
7416 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7417
7418 /* Start timer for the mbox_tmo and log some mailbox post messages */
7419 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007420 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007421
7422 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007423 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007424 "x%x x%x\n",
7425 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007426 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7427 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007428 phba->pport->port_state, psli->sli_flag);
7429
7430 if (mbx_cmnd != MBX_HEARTBEAT) {
7431 if (mboxq->vport) {
7432 lpfc_debugfs_disc_trc(mboxq->vport,
7433 LPFC_DISC_TRC_MBOX_VPORT,
7434 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7435 mbx_cmnd, mqe->un.mb_words[0],
7436 mqe->un.mb_words[1]);
7437 } else {
7438 lpfc_debugfs_disc_trc(phba->pport,
7439 LPFC_DISC_TRC_MBOX,
7440 "MBOX Send: cmd:x%x mb:x%x x%x",
7441 mbx_cmnd, mqe->un.mb_words[0],
7442 mqe->un.mb_words[1]);
7443 }
7444 }
7445 psli->slistat.mbox_cmd++;
7446
7447 /* Post the mailbox command to the port */
7448 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7449 if (rc != MBX_SUCCESS) {
7450 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007451 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007452 "cannot issue Data: x%x x%x\n",
7453 mboxq->vport ? mboxq->vport->vpi : 0,
7454 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007455 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7456 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007457 psli->sli_flag, MBX_NOWAIT);
7458 goto out_not_finished;
7459 }
7460
7461 return rc;
7462
7463out_not_finished:
7464 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007465 if (phba->sli.mbox_active) {
7466 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7467 __lpfc_mbox_cmpl_put(phba, mboxq);
7468 /* Release the token */
7469 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7470 phba->sli.mbox_active = NULL;
7471 }
James Smartda0436e2009-05-22 14:51:39 -04007472 spin_unlock_irqrestore(&phba->hbalock, iflags);
7473
7474 return MBX_NOT_FINISHED;
7475}
7476
7477/**
7478 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7479 * @phba: Pointer to HBA context object.
7480 * @pmbox: Pointer to mailbox object.
7481 * @flag: Flag indicating how the mailbox need to be processed.
7482 *
7483 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7484 * the API jump table function pointer from the lpfc_hba struct.
7485 *
7486 * Return codes the caller owns the mailbox command after the return of the
7487 * function.
7488 **/
7489int
7490lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7491{
7492 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7493}
7494
7495/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007496 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007497 * @phba: The hba struct for which this call is being executed.
7498 * @dev_grp: The HBA PCI-Device group number.
7499 *
7500 * This routine sets up the mbox interface API function jump table in @phba
7501 * struct.
7502 * Returns: 0 - success, -ENODEV - failure.
7503 **/
7504int
7505lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7506{
7507
7508 switch (dev_grp) {
7509 case LPFC_PCI_DEV_LP:
7510 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7511 phba->lpfc_sli_handle_slow_ring_event =
7512 lpfc_sli_handle_slow_ring_event_s3;
7513 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7514 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7515 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7516 break;
7517 case LPFC_PCI_DEV_OC:
7518 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7519 phba->lpfc_sli_handle_slow_ring_event =
7520 lpfc_sli_handle_slow_ring_event_s4;
7521 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7522 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7523 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7524 break;
7525 default:
7526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7527 "1420 Invalid HBA PCI-device group: 0x%x\n",
7528 dev_grp);
7529 return -ENODEV;
7530 break;
7531 }
7532 return 0;
7533}
7534
7535/**
James Smart3621a712009-04-06 18:47:14 -04007536 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007537 * @phba: Pointer to HBA context object.
7538 * @pring: Pointer to driver SLI ring object.
7539 * @piocb: Pointer to address of newly added command iocb.
7540 *
7541 * This function is called with hbalock held to add a command
7542 * iocb to the txq when SLI layer cannot submit the command iocb
7543 * to the ring.
7544 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007545void
James Smart92d7f7b2007-06-17 19:56:38 -05007546__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007547 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007548{
7549 /* Insert the caller's iocb in the txq tail for later processing. */
7550 list_add_tail(&piocb->list, &pring->txq);
7551 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007552}
7553
James Smarte59058c2008-08-24 21:49:00 -04007554/**
James Smart3621a712009-04-06 18:47:14 -04007555 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007556 * @phba: Pointer to HBA context object.
7557 * @pring: Pointer to driver SLI ring object.
7558 * @piocb: Pointer to address of newly added command iocb.
7559 *
7560 * This function is called with hbalock held before a new
7561 * iocb is submitted to the firmware. This function checks
7562 * txq to flush the iocbs in txq to Firmware before
7563 * submitting new iocbs to the Firmware.
7564 * If there are iocbs in the txq which need to be submitted
7565 * to firmware, lpfc_sli_next_iocb returns the first element
7566 * of the txq after dequeuing it from txq.
7567 * If there is no iocb in the txq then the function will return
7568 * *piocb and *piocb is set to NULL. Caller needs to check
7569 * *piocb to find if there are more commands in the txq.
7570 **/
dea31012005-04-17 16:05:31 -05007571static struct lpfc_iocbq *
7572lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007573 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007574{
7575 struct lpfc_iocbq * nextiocb;
7576
7577 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7578 if (!nextiocb) {
7579 nextiocb = *piocb;
7580 *piocb = NULL;
7581 }
7582
7583 return nextiocb;
7584}
7585
James Smarte59058c2008-08-24 21:49:00 -04007586/**
James Smart3772a992009-05-22 14:50:54 -04007587 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007588 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007589 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007590 * @piocb: Pointer to command iocb.
7591 * @flag: Flag indicating if this command can be put into txq.
7592 *
James Smart3772a992009-05-22 14:50:54 -04007593 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7594 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7595 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7596 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7597 * this function allows only iocbs for posting buffers. This function finds
7598 * next available slot in the command ring and posts the command to the
7599 * available slot and writes the port attention register to request HBA start
7600 * processing new iocb. If there is no slot available in the ring and
7601 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7602 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007603 *
James Smart3772a992009-05-22 14:50:54 -04007604 * This function is called with hbalock held. The function will return success
7605 * after it successfully submit the iocb to firmware or after adding to the
7606 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007607 **/
James Smart98c9ea52007-10-27 13:37:33 -04007608static int
James Smart3772a992009-05-22 14:50:54 -04007609__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007610 struct lpfc_iocbq *piocb, uint32_t flag)
7611{
7612 struct lpfc_iocbq *nextiocb;
7613 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007614 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007615
James Smart92d7f7b2007-06-17 19:56:38 -05007616 if (piocb->iocb_cmpl && (!piocb->vport) &&
7617 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7618 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7619 lpfc_printf_log(phba, KERN_ERR,
7620 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007621 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007622 piocb->iocb.ulpCommand);
7623 dump_stack();
7624 return IOCB_ERROR;
7625 }
7626
7627
Linas Vepstas8d63f372007-02-14 14:28:36 -06007628 /* If the PCI channel is in offline state, do not post iocbs. */
7629 if (unlikely(pci_channel_offline(phba->pcidev)))
7630 return IOCB_ERROR;
7631
James Smarta257bf92009-04-06 18:48:10 -04007632 /* If HBA has a deferred error attention, fail the iocb. */
7633 if (unlikely(phba->hba_flag & DEFER_ERATT))
7634 return IOCB_ERROR;
7635
dea31012005-04-17 16:05:31 -05007636 /*
7637 * We should never get an IOCB if we are in a < LINK_DOWN state
7638 */
James Smart2e0fef82007-06-17 19:56:36 -05007639 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007640 return IOCB_ERROR;
7641
7642 /*
7643 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007644 * outstanding event.
dea31012005-04-17 16:05:31 -05007645 */
James Smart0b727fe2007-10-27 13:37:25 -04007646 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007647 goto iocb_busy;
7648
James Smart2e0fef82007-06-17 19:56:36 -05007649 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007650 /*
James Smart2680eea2007-04-25 09:52:55 -04007651 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007652 * can be issued if the link is not up.
7653 */
7654 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007655 case CMD_GEN_REQUEST64_CR:
7656 case CMD_GEN_REQUEST64_CX:
7657 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7658 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007659 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007660 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7661 MENLO_TRANSPORT_TYPE))
7662
7663 goto iocb_busy;
7664 break;
dea31012005-04-17 16:05:31 -05007665 case CMD_QUE_RING_BUF_CN:
7666 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007667 /*
7668 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7669 * completion, iocb_cmpl MUST be 0.
7670 */
7671 if (piocb->iocb_cmpl)
7672 piocb->iocb_cmpl = NULL;
7673 /*FALLTHROUGH*/
7674 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007675 case CMD_CLOSE_XRI_CN:
7676 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007677 break;
7678 default:
7679 goto iocb_busy;
7680 }
7681
7682 /*
7683 * For FCP commands, we must be in a state where we can process link
7684 * attention events.
7685 */
7686 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007687 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007688 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007689 }
dea31012005-04-17 16:05:31 -05007690
dea31012005-04-17 16:05:31 -05007691 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7692 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7693 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7694
7695 if (iocb)
7696 lpfc_sli_update_ring(phba, pring);
7697 else
7698 lpfc_sli_update_full_ring(phba, pring);
7699
7700 if (!piocb)
7701 return IOCB_SUCCESS;
7702
7703 goto out_busy;
7704
7705 iocb_busy:
7706 pring->stats.iocb_cmd_delay++;
7707
7708 out_busy:
7709
7710 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007711 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007712 return IOCB_SUCCESS;
7713 }
7714
7715 return IOCB_BUSY;
7716}
7717
James Smart3772a992009-05-22 14:50:54 -04007718/**
James Smart4f774512009-05-22 14:52:35 -04007719 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7720 * @phba: Pointer to HBA context object.
7721 * @piocb: Pointer to command iocb.
7722 * @sglq: Pointer to the scatter gather queue object.
7723 *
7724 * This routine converts the bpl or bde that is in the IOCB
7725 * to a sgl list for the sli4 hardware. The physical address
7726 * of the bpl/bde is converted back to a virtual address.
7727 * If the IOCB contains a BPL then the list of BDE's is
7728 * converted to sli4_sge's. If the IOCB contains a single
7729 * BDE then it is converted to a single sli_sge.
7730 * The IOCB is still in cpu endianess so the contents of
7731 * the bpl can be used without byte swapping.
7732 *
7733 * Returns valid XRI = Success, NO_XRI = Failure.
7734**/
7735static uint16_t
7736lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7737 struct lpfc_sglq *sglq)
7738{
7739 uint16_t xritag = NO_XRI;
7740 struct ulp_bde64 *bpl = NULL;
7741 struct ulp_bde64 bde;
7742 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007743 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007744 IOCB_t *icmd;
7745 int numBdes = 0;
7746 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007747 uint32_t offset = 0; /* accumulated offset in the sg request list */
7748 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007749
7750 if (!piocbq || !sglq)
7751 return xritag;
7752
7753 sgl = (struct sli4_sge *)sglq->sgl;
7754 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007755 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7756 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007757 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7758 numBdes = icmd->un.genreq64.bdl.bdeSize /
7759 sizeof(struct ulp_bde64);
7760 /* The addrHigh and addrLow fields within the IOCB
7761 * have not been byteswapped yet so there is no
7762 * need to swap them back.
7763 */
James Smart1b511972011-12-13 13:23:09 -05007764 if (piocbq->context3)
7765 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7766 else
7767 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007768
James Smart1b511972011-12-13 13:23:09 -05007769 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007770 if (!bpl)
7771 return xritag;
7772
7773 for (i = 0; i < numBdes; i++) {
7774 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007775 sgl->addr_hi = bpl->addrHigh;
7776 sgl->addr_lo = bpl->addrLow;
7777
James Smart05580562011-05-24 11:40:48 -04007778 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007779 if ((i+1) == numBdes)
7780 bf_set(lpfc_sli4_sge_last, sgl, 1);
7781 else
7782 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007783 /* swap the size field back to the cpu so we
7784 * can assign it to the sgl.
7785 */
7786 bde.tus.w = le32_to_cpu(bpl->tus.w);
7787 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007788 /* The offsets in the sgl need to be accumulated
7789 * separately for the request and reply lists.
7790 * The request is always first, the reply follows.
7791 */
7792 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7793 /* add up the reply sg entries */
7794 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7795 inbound++;
7796 /* first inbound? reset the offset */
7797 if (inbound == 1)
7798 offset = 0;
7799 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007800 bf_set(lpfc_sli4_sge_type, sgl,
7801 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007802 offset += bde.tus.f.bdeSize;
7803 }
James Smart546fc852011-03-11 16:06:29 -05007804 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007805 bpl++;
7806 sgl++;
7807 }
7808 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7809 /* The addrHigh and addrLow fields of the BDE have not
7810 * been byteswapped yet so they need to be swapped
7811 * before putting them in the sgl.
7812 */
7813 sgl->addr_hi =
7814 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7815 sgl->addr_lo =
7816 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007817 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007818 bf_set(lpfc_sli4_sge_last, sgl, 1);
7819 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007820 sgl->sge_len =
7821 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007822 }
7823 return sglq->sli4_xritag;
7824}
7825
7826/**
7827 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7828 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007829 *
James Smarta93ff372010-10-22 11:06:08 -04007830 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007831 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7832 * held.
James Smart4f774512009-05-22 14:52:35 -04007833 *
7834 * Return: index into SLI4 fast-path FCP queue index.
7835 **/
James Smart2a76a282012-08-03 12:35:54 -04007836static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007837lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007838{
James Smart2a76a282012-08-03 12:35:54 -04007839 int i;
James Smart4f774512009-05-22 14:52:35 -04007840
James Smart49aa1432012-08-03 12:36:42 -04007841 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
7842 i = smp_processor_id();
7843 else
7844 i = atomic_add_return(1, &phba->fcp_qidx);
7845
James Smart67d12732012-08-03 12:36:13 -04007846 i = (i % phba->cfg_fcp_io_channel);
James Smart2a76a282012-08-03 12:35:54 -04007847 return i;
James Smart4f774512009-05-22 14:52:35 -04007848}
7849
7850/**
7851 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7852 * @phba: Pointer to HBA context object.
7853 * @piocb: Pointer to command iocb.
7854 * @wqe: Pointer to the work queue entry.
7855 *
7856 * This routine converts the iocb command to its Work Queue Entry
7857 * equivalent. The wqe pointer should not have any fields set when
7858 * this routine is called because it will memcpy over them.
7859 * This routine does not set the CQ_ID or the WQEC bits in the
7860 * wqe.
7861 *
7862 * Returns: 0 = Success, IOCB_ERROR = Failure.
7863 **/
7864static int
7865lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7866 union lpfc_wqe *wqe)
7867{
James Smart5ffc2662009-11-18 15:39:44 -05007868 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007869 uint8_t ct = 0;
7870 uint32_t fip;
7871 uint32_t abort_tag;
7872 uint8_t command_type = ELS_COMMAND_NON_FIP;
7873 uint8_t cmnd;
7874 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007875 uint16_t abrt_iotag;
7876 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007877 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007878 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007879 int numBdes, i;
7880 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007881 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007882 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007883 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007884
James Smart45ed1192009-10-02 15:17:02 -04007885 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007886 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007887 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007888 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007889 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007890 command_type = ELS_COMMAND_FIP;
7891 else
7892 command_type = ELS_COMMAND_NON_FIP;
7893
James Smart4f774512009-05-22 14:52:35 -04007894 /* Some of the fields are in the right position already */
7895 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7896 abort_tag = (uint32_t) iocbq->iotag;
7897 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007898 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007899 /* words0-2 bpl convert bde */
7900 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007901 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7902 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007903 bpl = (struct ulp_bde64 *)
7904 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7905 if (!bpl)
7906 return IOCB_ERROR;
7907
7908 /* Should already be byte swapped. */
7909 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7910 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7911 /* swap the size field back to the cpu so we
7912 * can assign it to the sgl.
7913 */
7914 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007915 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7916 total_len = 0;
7917 for (i = 0; i < numBdes; i++) {
7918 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7919 total_len += bde.tus.f.bdeSize;
7920 }
James Smart4f774512009-05-22 14:52:35 -04007921 } else
James Smart5ffc2662009-11-18 15:39:44 -05007922 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007923
7924 iocbq->iocb.ulpIoTag = iocbq->iotag;
7925 cmnd = iocbq->iocb.ulpCommand;
7926
7927 switch (iocbq->iocb.ulpCommand) {
7928 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04007929 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7930 ndlp = iocbq->context_un.ndlp;
7931 else
7932 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007933 if (!iocbq->iocb.ulpLe) {
7934 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7935 "2007 Only Limited Edition cmd Format"
7936 " supported 0x%x\n",
7937 iocbq->iocb.ulpCommand);
7938 return IOCB_ERROR;
7939 }
James Smartff78d8f2011-12-13 13:21:35 -05007940
James Smart5ffc2662009-11-18 15:39:44 -05007941 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007942 /* Els_reguest64 has a TMO */
7943 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7944 iocbq->iocb.ulpTimeout);
7945 /* Need a VF for word 4 set the vf bit*/
7946 bf_set(els_req64_vf, &wqe->els_req, 0);
7947 /* And a VFID for word 12 */
7948 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007949 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007950 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7951 iocbq->iocb.ulpContext);
7952 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7953 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007954 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007955 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007956 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7957 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007958 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7959 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007960 if_type = bf_get(lpfc_sli_intf_if_type,
7961 &phba->sli4_hba.sli_intf);
7962 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007963 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007964 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007965 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05007966 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05007967 *pcmd == ELS_CMD_PLOGI)) {
7968 bf_set(els_req64_sp, &wqe->els_req, 1);
7969 bf_set(els_req64_sid, &wqe->els_req,
7970 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04007971 if ((*pcmd == ELS_CMD_FLOGI) &&
7972 !(phba->fc_topology ==
7973 LPFC_TOPOLOGY_LOOP))
7974 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05007975 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7976 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04007977 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007978 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007979 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7980 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7981 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7982 }
James Smartc8685952009-11-18 15:39:16 -05007983 }
James Smart6d368e52011-05-24 11:44:12 -04007984 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7985 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007986 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7987 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7988 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7989 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7990 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7991 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007992 break;
James Smart5ffc2662009-11-18 15:39:44 -05007993 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007994 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7995 iocbq->iocb.un.ulpWord[3]);
7996 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007997 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007998 /* The entire sequence is transmitted for this IOCB */
7999 xmit_len = total_len;
8000 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008001 if (phba->link_flag & LS_LOOPBACK_MODE)
8002 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008003 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008004 /* word3 iocb=io_tag32 wqe=reserved */
8005 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008006 /* word4 relative_offset memcpy */
8007 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008008 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8009 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8010 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8011 LPFC_WQE_IOD_WRITE);
8012 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8013 LPFC_WQE_LENLOC_WORD12);
8014 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008015 wqe->xmit_sequence.xmit_len = xmit_len;
8016 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008017 break;
James Smart4f774512009-05-22 14:52:35 -04008018 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008019 /* word3 iocb=iotag32 wqe=seq_payload_len */
8020 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008021 /* word4 iocb=rsvd wqe=rsvd */
8022 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8023 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008024 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008025 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008026 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8027 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8028 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8029 LPFC_WQE_LENLOC_WORD3);
8030 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008031 break;
James Smart4f774512009-05-22 14:52:35 -04008032 case CMD_FCP_IWRITE64_CR:
8033 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008034 /* word3 iocb=iotag wqe=payload_offset_len */
8035 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8036 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05008037 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04008038 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8039 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8040 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8041 iocbq->iocb.ulpFCP2Rcvy);
8042 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8043 /* Always open the exchange */
8044 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008045 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8046 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8047 LPFC_WQE_LENLOC_WORD4);
8048 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8049 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008050 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8051 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8052 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8053 }
8054 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008055 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008056 case CMD_FCP_IREAD64_CR:
8057 /* word3 iocb=iotag wqe=payload_offset_len */
8058 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8059 wqe->fcp_iread.payload_offset_len =
8060 xmit_len + sizeof(struct fcp_rsp);
8061 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8062 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8063 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8064 iocbq->iocb.ulpFCP2Rcvy);
8065 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008066 /* Always open the exchange */
8067 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008068 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8069 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8070 LPFC_WQE_LENLOC_WORD4);
8071 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8072 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008073 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8074 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8075 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8076 }
8077 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008078 break;
James Smartf1126682009-06-10 17:22:44 -04008079 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008080 /* word3 iocb=IO_TAG wqe=reserved */
8081 wqe->fcp_icmd.rsrvd3 = 0;
8082 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008083 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008084 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8085 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8086 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8087 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8088 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8089 LPFC_WQE_LENLOC_NONE);
8090 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008091 break;
James Smart4f774512009-05-22 14:52:35 -04008092 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008093 /* For this command calculate the xmit length of the
8094 * request bde.
8095 */
8096 xmit_len = 0;
8097 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8098 sizeof(struct ulp_bde64);
8099 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008100 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008101 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8102 break;
James Smart63e801c2010-11-20 23:14:19 -05008103 xmit_len += bde.tus.f.bdeSize;
8104 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008105 /* word3 iocb=IO_TAG wqe=request_payload_len */
8106 wqe->gen_req.request_payload_len = xmit_len;
8107 /* word4 iocb=parameter wqe=relative_offset memcpy */
8108 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008109 /* word6 context tag copied in memcpy */
8110 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8111 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8113 "2015 Invalid CT %x command 0x%x\n",
8114 ct, iocbq->iocb.ulpCommand);
8115 return IOCB_ERROR;
8116 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008117 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8118 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8119 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8120 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8121 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8122 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8123 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8124 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008125 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008126 break;
James Smart4f774512009-05-22 14:52:35 -04008127 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008128 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008129 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008130 /* word3 iocb=iotag32 wqe=response_payload_len */
8131 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008132 /* word4 */
8133 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008134 /* word5 iocb=rsvd wge=did */
8135 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008136 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8137
8138 if_type = bf_get(lpfc_sli_intf_if_type,
8139 &phba->sli4_hba.sli_intf);
8140 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8141 if (iocbq->vport->fc_flag & FC_PT2PT) {
8142 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8143 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8144 iocbq->vport->fc_myDID);
8145 if (iocbq->vport->fc_myDID == Fabric_DID) {
8146 bf_set(wqe_els_did,
8147 &wqe->xmit_els_rsp.wqe_dest, 0);
8148 }
8149 }
8150 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008151 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8152 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8153 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8154 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008155 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008156 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008157 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008158 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008159 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8160 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8161 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8162 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8163 LPFC_WQE_LENLOC_WORD3);
8164 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008165 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8166 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008167 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8168 iocbq->context2)->virt);
8169 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008170 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8171 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008172 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008173 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8174 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008175 phba->vpi_ids[phba->pport->vpi]);
8176 }
James Smart4f774512009-05-22 14:52:35 -04008177 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008178 break;
James Smart4f774512009-05-22 14:52:35 -04008179 case CMD_CLOSE_XRI_CN:
8180 case CMD_ABORT_XRI_CN:
8181 case CMD_ABORT_XRI_CX:
8182 /* words 0-2 memcpy should be 0 rserved */
8183 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008184 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8185 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8186 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8187 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8188 } else
8189 fip = 0;
8190
8191 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008192 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008193 * The link is down, or the command was ELS_FIP
8194 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008195 * on the wire.
8196 */
8197 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8198 else
8199 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8200 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008201 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8202 wqe->abort_cmd.rsrvd5 = 0;
8203 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008204 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8205 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008206 /*
8207 * The abort handler will send us CMD_ABORT_XRI_CN or
8208 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8209 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008210 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8211 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8212 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8213 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008214 cmnd = CMD_ABORT_XRI_CX;
8215 command_type = OTHER_COMMAND;
8216 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008217 break;
James Smart6669f9b2009-10-02 15:16:45 -04008218 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008219 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008220 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008221 * we re-construct this WQE here based on information in
8222 * iocbq from scratch.
8223 */
8224 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008225 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008226 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008227 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8228 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008229 LPFC_ABTS_UNSOL_INT) {
8230 /* ABTS sent by initiator to CT exchange, the
8231 * RX_ID field will be filled with the newly
8232 * allocated responder XRI.
8233 */
8234 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8235 iocbq->sli4_xritag);
8236 } else {
8237 /* ABTS sent by responder to CT exchange, the
8238 * RX_ID field will be filled with the responder
8239 * RX_ID from ABTS.
8240 */
8241 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008242 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008243 }
James Smart6669f9b2009-10-02 15:16:45 -04008244 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8245 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008246
8247 /* Use CT=VPI */
8248 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8249 ndlp->nlp_DID);
8250 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8251 iocbq->iocb.ulpContext);
8252 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008253 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008254 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008255 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8256 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8257 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008258 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8259 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008260 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8261 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8262 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8263 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8264 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8265 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8266 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8267 }
8268
James Smart7851fe22011-07-22 18:36:52 -04008269 break;
James Smart4f774512009-05-22 14:52:35 -04008270 case CMD_XRI_ABORTED_CX:
8271 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008272 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8273 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8274 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8275 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8276 default:
8277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8278 "2014 Invalid command 0x%x\n",
8279 iocbq->iocb.ulpCommand);
8280 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008281 break;
James Smart4f774512009-05-22 14:52:35 -04008282 }
James Smart6d368e52011-05-24 11:44:12 -04008283
James Smartf0d9bcc2010-10-22 11:07:09 -04008284 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8285 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8286 wqe->generic.wqe_com.abort_tag = abort_tag;
8287 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8288 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8289 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8290 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008291 return 0;
8292}
8293
8294/**
8295 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8296 * @phba: Pointer to HBA context object.
8297 * @ring_number: SLI ring number to issue iocb on.
8298 * @piocb: Pointer to command iocb.
8299 * @flag: Flag indicating if this command can be put into txq.
8300 *
8301 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8302 * an iocb command to an HBA with SLI-4 interface spec.
8303 *
8304 * This function is called with hbalock held. The function will return success
8305 * after it successfully submit the iocb to firmware or after adding to the
8306 * txq.
8307 **/
8308static int
8309__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8310 struct lpfc_iocbq *piocb, uint32_t flag)
8311{
8312 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008313 union lpfc_wqe wqe;
8314 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008315
8316 if (piocb->sli4_xritag == NO_XRI) {
8317 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008318 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008319 sglq = NULL;
8320 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008321 if (pring->txq_cnt) {
8322 if (!(flag & SLI_IOCB_RET_IOCB)) {
8323 __lpfc_sli_ringtx_put(phba,
8324 pring, piocb);
8325 return IOCB_SUCCESS;
8326 } else {
8327 return IOCB_BUSY;
8328 }
8329 } else {
James Smart6d368e52011-05-24 11:44:12 -04008330 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008331 if (!sglq) {
8332 if (!(flag & SLI_IOCB_RET_IOCB)) {
8333 __lpfc_sli_ringtx_put(phba,
8334 pring,
8335 piocb);
8336 return IOCB_SUCCESS;
8337 } else
8338 return IOCB_BUSY;
8339 }
8340 }
James Smart4f774512009-05-22 14:52:35 -04008341 }
8342 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008343 /* These IO's already have an XRI and a mapped sgl. */
8344 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008345 } else {
James Smart6d368e52011-05-24 11:44:12 -04008346 /*
8347 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008348 * sglq is on the active list
8349 */
8350 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8351 if (!sglq)
8352 return IOCB_ERROR;
8353 }
8354
8355 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008356 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008357 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008358 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008359 return IOCB_ERROR;
8360 }
8361
8362 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8363 return IOCB_ERROR;
8364
James Smart341af102010-01-26 23:07:37 -05008365 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8366 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008367 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8368 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008369 return IOCB_ERROR;
8370 } else {
8371 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8372 return IOCB_ERROR;
8373 }
8374 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8375
8376 return 0;
8377}
8378
8379/**
James Smart3772a992009-05-22 14:50:54 -04008380 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8381 *
8382 * This routine wraps the actual lockless version for issusing IOCB function
8383 * pointer from the lpfc_hba struct.
8384 *
8385 * Return codes:
8386 * IOCB_ERROR - Error
8387 * IOCB_SUCCESS - Success
8388 * IOCB_BUSY - Busy
8389 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008390int
James Smart3772a992009-05-22 14:50:54 -04008391__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8392 struct lpfc_iocbq *piocb, uint32_t flag)
8393{
8394 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8395}
8396
8397/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008398 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008399 * @phba: The hba struct for which this call is being executed.
8400 * @dev_grp: The HBA PCI-Device group number.
8401 *
8402 * This routine sets up the SLI interface API function jump table in @phba
8403 * struct.
8404 * Returns: 0 - success, -ENODEV - failure.
8405 **/
8406int
8407lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8408{
8409
8410 switch (dev_grp) {
8411 case LPFC_PCI_DEV_LP:
8412 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8413 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8414 break;
James Smart4f774512009-05-22 14:52:35 -04008415 case LPFC_PCI_DEV_OC:
8416 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8417 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8418 break;
James Smart3772a992009-05-22 14:50:54 -04008419 default:
8420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8421 "1419 Invalid HBA PCI-device group: 0x%x\n",
8422 dev_grp);
8423 return -ENODEV;
8424 break;
8425 }
8426 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8427 return 0;
8428}
James Smart92d7f7b2007-06-17 19:56:38 -05008429
James Smarte59058c2008-08-24 21:49:00 -04008430/**
James Smart3621a712009-04-06 18:47:14 -04008431 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008432 * @phba: Pointer to HBA context object.
8433 * @pring: Pointer to driver SLI ring object.
8434 * @piocb: Pointer to command iocb.
8435 * @flag: Flag indicating if this command can be put into txq.
8436 *
8437 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8438 * function. This function gets the hbalock and calls
8439 * __lpfc_sli_issue_iocb function and will return the error returned
8440 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8441 * functions which do not hold hbalock.
8442 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008443int
James Smart3772a992009-05-22 14:50:54 -04008444lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008445 struct lpfc_iocbq *piocb, uint32_t flag)
8446{
James Smartba20c852012-08-03 12:36:52 -04008447 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008448 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008449 struct lpfc_queue *fpeq;
8450 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008451 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008452 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008453
James Smart7e56aa22012-08-03 12:35:34 -04008454 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008455 if (piocb->iocb_flag & LPFC_IO_FCP) {
8456 if (unlikely(!phba->sli4_hba.fcp_wq))
8457 return IOCB_ERROR;
8458 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8459 piocb->fcp_wqidx = idx;
8460 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
James Smartba20c852012-08-03 12:36:52 -04008461
8462 pring = &phba->sli.ring[ring_number];
8463 spin_lock_irqsave(&pring->ring_lock, iflags);
8464 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8465 flag);
8466 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8467
8468 if (lpfc_fcp_look_ahead) {
8469 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8470
8471 if (atomic_dec_and_test(&fcp_eq_hdl->
8472 fcp_eq_in_use)) {
8473
8474 /* Get associated EQ with this index */
8475 fpeq = phba->sli4_hba.hba_eq[idx];
8476
8477 /* Turn off interrupts from this EQ */
8478 lpfc_sli4_eq_clr_intr(fpeq);
8479
8480 /*
8481 * Process all the events on FCP EQ
8482 */
8483 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8484 lpfc_sli4_hba_handle_eqe(phba,
8485 eqe, idx);
8486 fpeq->EQ_processed++;
8487 }
8488
8489 /* Always clear and re-arm the EQ */
8490 lpfc_sli4_eq_release(fpeq,
8491 LPFC_QUEUE_REARM);
8492 }
8493 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8494 }
8495 } else {
8496 pring = &phba->sli.ring[ring_number];
8497 spin_lock_irqsave(&pring->ring_lock, iflags);
8498 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8499 flag);
8500 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8501
James Smart2a76a282012-08-03 12:35:54 -04008502 }
James Smart7e56aa22012-08-03 12:35:34 -04008503 } else {
8504 /* For now, SLI2/3 will still use hbalock */
8505 spin_lock_irqsave(&phba->hbalock, iflags);
8506 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8507 spin_unlock_irqrestore(&phba->hbalock, iflags);
8508 }
James Smart92d7f7b2007-06-17 19:56:38 -05008509 return rc;
8510}
8511
James Smarte59058c2008-08-24 21:49:00 -04008512/**
James Smart3621a712009-04-06 18:47:14 -04008513 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008514 * @phba: Pointer to HBA context object.
8515 *
8516 * This function is called while driver attaches with the
8517 * HBA to setup the extra ring. The extra ring is used
8518 * only when driver needs to support target mode functionality
8519 * or IP over FC functionalities.
8520 *
8521 * This function is called with no lock held.
8522 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008523static int
8524lpfc_extra_ring_setup( struct lpfc_hba *phba)
8525{
8526 struct lpfc_sli *psli;
8527 struct lpfc_sli_ring *pring;
8528
8529 psli = &phba->sli;
8530
8531 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008532
8533 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008534 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008535 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8536 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8537 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8538 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008539
James Smarta4bc3372006-12-02 13:34:16 -05008540 /* and give them to the extra ring */
8541 pring = &psli->ring[psli->extra_ring];
8542
James Smart7e56aa22012-08-03 12:35:34 -04008543 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8544 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8545 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8546 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008547
8548 /* Setup default profile for this ring */
8549 pring->iotag_max = 4096;
8550 pring->num_mask = 1;
8551 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008552 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8553 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008554 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8555 return 0;
8556}
8557
James Smartcb69f7d2011-12-13 13:21:57 -05008558/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8559 * @vport: pointer to virtual port object.
8560 * @ndlp: nodelist pointer for the impacted rport.
8561 *
8562 * The driver calls this routine in response to a XRI ABORT CQE
8563 * event from the port. In this event, the driver is required to
8564 * recover its login to the rport even though its login may be valid
8565 * from the driver's perspective. The failed ABTS notice from the
8566 * port indicates the rport is not responding.
8567 */
8568static void
8569lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8570 struct lpfc_nodelist *ndlp)
8571{
8572 struct Scsi_Host *shost;
8573 struct lpfc_hba *phba;
8574 unsigned long flags = 0;
8575
8576 shost = lpfc_shost_from_vport(vport);
8577 phba = vport->phba;
8578 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8579 lpfc_printf_log(phba, KERN_INFO,
8580 LOG_SLI, "3093 No rport recovery needed. "
8581 "rport in state 0x%x\n",
8582 ndlp->nlp_state);
8583 return;
8584 }
8585 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8586 "3094 Start rport recovery on shost id 0x%x "
8587 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8588 "flags 0x%x\n",
8589 shost->host_no, ndlp->nlp_DID,
8590 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8591 ndlp->nlp_flag);
8592 /*
8593 * The rport is not responding. Don't attempt ADISC recovery.
8594 * Remove the FCP-2 flag to force a PLOGI.
8595 */
8596 spin_lock_irqsave(shost->host_lock, flags);
8597 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8598 spin_unlock_irqrestore(shost->host_lock, flags);
8599 lpfc_disc_state_machine(vport, ndlp, NULL,
8600 NLP_EVT_DEVICE_RECOVERY);
8601 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8602 spin_lock_irqsave(shost->host_lock, flags);
8603 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8604 spin_unlock_irqrestore(shost->host_lock, flags);
8605 lpfc_disc_start(vport);
8606}
8607
8608/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8609 * @phba: Pointer to HBA context object.
8610 * @iocbq: Pointer to iocb object.
8611 *
8612 * The async_event handler calls this routine when it receives
8613 * an ASYNC_STATUS_CN event from the port. The port generates
8614 * this event when an Abort Sequence request to an rport fails
8615 * twice in succession. The abort could be originated by the
8616 * driver or by the port. The ABTS could have been for an ELS
8617 * or FCP IO. The port only generates this event when an ABTS
8618 * fails to complete after one retry.
8619 */
8620static void
8621lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8622 struct lpfc_iocbq *iocbq)
8623{
8624 struct lpfc_nodelist *ndlp = NULL;
8625 uint16_t rpi = 0, vpi = 0;
8626 struct lpfc_vport *vport = NULL;
8627
8628 /* The rpi in the ulpContext is vport-sensitive. */
8629 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8630 rpi = iocbq->iocb.ulpContext;
8631
8632 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8633 "3092 Port generated ABTS async event "
8634 "on vpi %d rpi %d status 0x%x\n",
8635 vpi, rpi, iocbq->iocb.ulpStatus);
8636
8637 vport = lpfc_find_vport_by_vpid(phba, vpi);
8638 if (!vport)
8639 goto err_exit;
8640 ndlp = lpfc_findnode_rpi(vport, rpi);
8641 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8642 goto err_exit;
8643
8644 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8645 lpfc_sli_abts_recover_port(vport, ndlp);
8646 return;
8647
8648 err_exit:
8649 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8650 "3095 Event Context not found, no "
8651 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8652 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8653 vpi, rpi);
8654}
8655
8656/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8657 * @phba: pointer to HBA context object.
8658 * @ndlp: nodelist pointer for the impacted rport.
8659 * @axri: pointer to the wcqe containing the failed exchange.
8660 *
8661 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8662 * port. The port generates this event when an abort exchange request to an
8663 * rport fails twice in succession with no reply. The abort could be originated
8664 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8665 */
8666void
8667lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8668 struct lpfc_nodelist *ndlp,
8669 struct sli4_wcqe_xri_aborted *axri)
8670{
8671 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008672 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008673
James Smart6b5151f2012-01-18 16:24:06 -05008674 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008675 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8676 "3115 Node Context not found, driver "
8677 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008678 return;
8679 }
8680
James Smartcb69f7d2011-12-13 13:21:57 -05008681 vport = ndlp->vport;
8682 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8683 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008684 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008685 ndlp->vport->vpi, ndlp->nlp_rpi,
8686 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008687 bf_get(lpfc_wcqe_xa_status, axri),
8688 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008689
James Smart5c1db2a2012-03-01 22:34:36 -05008690 /*
8691 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8692 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8693 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8694 */
8695 ext_status = axri->parameter & WCQE_PARAM_MASK;
8696 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8697 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008698 lpfc_sli_abts_recover_port(vport, ndlp);
8699}
8700
James Smarte59058c2008-08-24 21:49:00 -04008701/**
James Smart3621a712009-04-06 18:47:14 -04008702 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008703 * @phba: Pointer to HBA context object.
8704 * @pring: Pointer to driver SLI ring object.
8705 * @iocbq: Pointer to iocb object.
8706 *
8707 * This function is called by the slow ring event handler
8708 * function when there is an ASYNC event iocb in the ring.
8709 * This function is called with no lock held.
8710 * Currently this function handles only temperature related
8711 * ASYNC events. The function decodes the temperature sensor
8712 * event message and posts events for the management applications.
8713 **/
James Smart98c9ea52007-10-27 13:37:33 -04008714static void
James Smart57127f12007-10-27 13:37:05 -04008715lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8716 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8717{
8718 IOCB_t *icmd;
8719 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008720 struct temp_event temp_event_data;
8721 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008722 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008723
8724 icmd = &iocbq->iocb;
8725 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008726
James Smartcb69f7d2011-12-13 13:21:57 -05008727 switch (evt_code) {
8728 case ASYNC_TEMP_WARN:
8729 case ASYNC_TEMP_SAFE:
8730 temp_event_data.data = (uint32_t) icmd->ulpContext;
8731 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8732 if (evt_code == ASYNC_TEMP_WARN) {
8733 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8734 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8735 "0347 Adapter is very hot, please take "
8736 "corrective action. temperature : %d Celsius\n",
8737 (uint32_t) icmd->ulpContext);
8738 } else {
8739 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8740 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8741 "0340 Adapter temperature is OK now. "
8742 "temperature : %d Celsius\n",
8743 (uint32_t) icmd->ulpContext);
8744 }
8745
8746 /* Send temperature change event to applications */
8747 shost = lpfc_shost_from_vport(phba->pport);
8748 fc_host_post_vendor_event(shost, fc_get_event_number(),
8749 sizeof(temp_event_data), (char *) &temp_event_data,
8750 LPFC_NL_VENDOR_ID);
8751 break;
8752 case ASYNC_STATUS_CN:
8753 lpfc_sli_abts_err_handler(phba, iocbq);
8754 break;
8755 default:
James Smarta257bf92009-04-06 18:48:10 -04008756 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008758 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008759 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008760 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8761 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8762 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8763 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008764 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008765 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8766 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8767 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8768 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8769
James Smartcb69f7d2011-12-13 13:21:57 -05008770 break;
James Smart57127f12007-10-27 13:37:05 -04008771 }
James Smart57127f12007-10-27 13:37:05 -04008772}
8773
8774
James Smarte59058c2008-08-24 21:49:00 -04008775/**
James Smart3621a712009-04-06 18:47:14 -04008776 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008777 * @phba: Pointer to HBA context object.
8778 *
8779 * lpfc_sli_setup sets up rings of the SLI interface with
8780 * number of iocbs per ring and iotags. This function is
8781 * called while driver attach to the HBA and before the
8782 * interrupts are enabled. So there is no need for locking.
8783 *
8784 * This function always returns 0.
8785 **/
dea31012005-04-17 16:05:31 -05008786int
8787lpfc_sli_setup(struct lpfc_hba *phba)
8788{
James Smarted957682007-06-17 19:56:37 -05008789 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008790 struct lpfc_sli *psli = &phba->sli;
8791 struct lpfc_sli_ring *pring;
8792
James Smart2a76a282012-08-03 12:35:54 -04008793 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8794 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04008795 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05008796 psli->sli_flag = 0;
8797 psli->fcp_ring = LPFC_FCP_RING;
8798 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008799 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008800
James Bottomley604a3e32005-10-29 10:28:33 -05008801 psli->iocbq_lookup = NULL;
8802 psli->iocbq_lookup_len = 0;
8803 psli->last_iotag = 0;
8804
dea31012005-04-17 16:05:31 -05008805 for (i = 0; i < psli->num_rings; i++) {
8806 pring = &psli->ring[i];
8807 switch (i) {
8808 case LPFC_FCP_RING: /* ring 0 - FCP */
8809 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008810 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8811 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8812 pring->sli.sli3.numCiocb +=
8813 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8814 pring->sli.sli3.numRiocb +=
8815 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8816 pring->sli.sli3.numCiocb +=
8817 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8818 pring->sli.sli3.numRiocb +=
8819 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8820 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008821 SLI3_IOCB_CMD_SIZE :
8822 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008823 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008824 SLI3_IOCB_RSP_SIZE :
8825 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008826 pring->iotag_ctr = 0;
8827 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008828 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008829 pring->fast_iotag = pring->iotag_max;
8830 pring->num_mask = 0;
8831 break;
James Smarta4bc3372006-12-02 13:34:16 -05008832 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008833 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008834 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8835 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8836 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008837 SLI3_IOCB_CMD_SIZE :
8838 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008839 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008840 SLI3_IOCB_RSP_SIZE :
8841 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008842 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008843 pring->num_mask = 0;
8844 break;
8845 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8846 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008847 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8848 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8849 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008850 SLI3_IOCB_CMD_SIZE :
8851 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008852 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008853 SLI3_IOCB_RSP_SIZE :
8854 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008855 pring->fast_iotag = 0;
8856 pring->iotag_ctr = 0;
8857 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008858 pring->lpfc_sli_rcv_async_status =
8859 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008860 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008861 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008862 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8863 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008864 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008865 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008866 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008867 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8868 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008869 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008870 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008871 pring->prt[2].profile = 0; /* Mask 2 */
8872 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008873 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008874 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008875 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008876 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008877 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008878 pring->prt[3].profile = 0; /* Mask 3 */
8879 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008880 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008881 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008882 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008883 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008884 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008885 /* abort unsolicited sequence */
8886 pring->prt[4].profile = 0; /* Mask 4 */
8887 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8888 pring->prt[4].type = FC_TYPE_BLS;
8889 pring->prt[4].lpfc_sli_rcv_unsol_event =
8890 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008891 break;
8892 }
James Smart7e56aa22012-08-03 12:35:34 -04008893 totiocbsize += (pring->sli.sli3.numCiocb *
8894 pring->sli.sli3.sizeCiocb) +
8895 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05008896 }
James Smarted957682007-06-17 19:56:37 -05008897 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008898 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008899 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8900 "SLI2 SLIM Data: x%x x%lx\n",
8901 phba->brd_no, totiocbsize,
8902 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008903 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008904 if (phba->cfg_multi_ring_support == 2)
8905 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008906
8907 return 0;
8908}
8909
James Smarte59058c2008-08-24 21:49:00 -04008910/**
James Smart3621a712009-04-06 18:47:14 -04008911 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008912 * @phba: Pointer to HBA context object.
8913 *
8914 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8915 * ring. This function also initializes ring indices of each ring.
8916 * This function is called during the initialization of the SLI
8917 * interface of an HBA.
8918 * This function is called with no lock held and always returns
8919 * 1.
8920 **/
dea31012005-04-17 16:05:31 -05008921int
James Smart2e0fef82007-06-17 19:56:36 -05008922lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008923{
8924 struct lpfc_sli *psli;
8925 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008926 int i;
dea31012005-04-17 16:05:31 -05008927
8928 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008929 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008930 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008931 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008932 /* Initialize list headers for txq and txcmplq as double linked lists */
8933 for (i = 0; i < psli->num_rings; i++) {
8934 pring = &psli->ring[i];
8935 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04008936 pring->sli.sli3.next_cmdidx = 0;
8937 pring->sli.sli3.local_getidx = 0;
8938 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05008939 INIT_LIST_HEAD(&pring->txq);
8940 INIT_LIST_HEAD(&pring->txcmplq);
8941 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008942 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008943 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04008944 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05008945 }
James Smart2e0fef82007-06-17 19:56:36 -05008946 spin_unlock_irq(&phba->hbalock);
8947 return 1;
dea31012005-04-17 16:05:31 -05008948}
8949
James Smarte59058c2008-08-24 21:49:00 -04008950/**
James Smart04c68492009-05-22 14:52:52 -04008951 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8952 * @phba: Pointer to HBA context object.
8953 *
8954 * This routine flushes the mailbox command subsystem. It will unconditionally
8955 * flush all the mailbox commands in the three possible stages in the mailbox
8956 * command sub-system: pending mailbox command queue; the outstanding mailbox
8957 * command; and completed mailbox command queue. It is caller's responsibility
8958 * to make sure that the driver is in the proper state to flush the mailbox
8959 * command sub-system. Namely, the posting of mailbox commands into the
8960 * pending mailbox command queue from the various clients must be stopped;
8961 * either the HBA is in a state that it will never works on the outstanding
8962 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8963 * mailbox command has been completed.
8964 **/
8965static void
8966lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8967{
8968 LIST_HEAD(completions);
8969 struct lpfc_sli *psli = &phba->sli;
8970 LPFC_MBOXQ_t *pmb;
8971 unsigned long iflag;
8972
8973 /* Flush all the mailbox commands in the mbox system */
8974 spin_lock_irqsave(&phba->hbalock, iflag);
8975 /* The pending mailbox command queue */
8976 list_splice_init(&phba->sli.mboxq, &completions);
8977 /* The outstanding active mailbox command */
8978 if (psli->mbox_active) {
8979 list_add_tail(&psli->mbox_active->list, &completions);
8980 psli->mbox_active = NULL;
8981 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8982 }
8983 /* The completed mailbox command queue */
8984 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8985 spin_unlock_irqrestore(&phba->hbalock, iflag);
8986
8987 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8988 while (!list_empty(&completions)) {
8989 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8990 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8991 if (pmb->mbox_cmpl)
8992 pmb->mbox_cmpl(phba, pmb);
8993 }
8994}
8995
8996/**
James Smart3621a712009-04-06 18:47:14 -04008997 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008998 * @vport: Pointer to virtual port object.
8999 *
9000 * lpfc_sli_host_down is called to clean up the resources
9001 * associated with a vport before destroying virtual
9002 * port data structures.
9003 * This function does following operations:
9004 * - Free discovery resources associated with this virtual
9005 * port.
9006 * - Free iocbs associated with this virtual port in
9007 * the txq.
9008 * - Send abort for all iocb commands associated with this
9009 * vport in txcmplq.
9010 *
9011 * This function is called with no lock held and always returns 1.
9012 **/
dea31012005-04-17 16:05:31 -05009013int
James Smart92d7f7b2007-06-17 19:56:38 -05009014lpfc_sli_host_down(struct lpfc_vport *vport)
9015{
James Smart858c9f62007-06-17 19:56:39 -05009016 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009017 struct lpfc_hba *phba = vport->phba;
9018 struct lpfc_sli *psli = &phba->sli;
9019 struct lpfc_sli_ring *pring;
9020 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009021 int i;
9022 unsigned long flags = 0;
9023 uint16_t prev_pring_flag;
9024
9025 lpfc_cleanup_discovery_resources(vport);
9026
9027 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009028 for (i = 0; i < psli->num_rings; i++) {
9029 pring = &psli->ring[i];
9030 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009031 /* Only slow rings */
9032 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009033 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009034 /* Set the lpfc data pending flag */
9035 set_bit(LPFC_DATA_READY, &phba->data_flags);
9036 }
James Smart92d7f7b2007-06-17 19:56:38 -05009037 /*
9038 * Error everything on the txq since these iocbs have not been
9039 * given to the FW yet.
9040 */
James Smart92d7f7b2007-06-17 19:56:38 -05009041 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9042 if (iocb->vport != vport)
9043 continue;
James Smart858c9f62007-06-17 19:56:39 -05009044 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009045 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05009046 }
9047
9048 /* Next issue ABTS for everything on the txcmplq */
9049 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9050 list) {
9051 if (iocb->vport != vport)
9052 continue;
9053 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9054 }
9055
9056 pring->flag = prev_pring_flag;
9057 }
9058
9059 spin_unlock_irqrestore(&phba->hbalock, flags);
9060
James Smarta257bf92009-04-06 18:48:10 -04009061 /* Cancel all the IOCBs from the completions list */
9062 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9063 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009064 return 1;
9065}
9066
James Smarte59058c2008-08-24 21:49:00 -04009067/**
James Smart3621a712009-04-06 18:47:14 -04009068 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009069 * @phba: Pointer to HBA context object.
9070 *
9071 * This function cleans up all iocb, buffers, mailbox commands
9072 * while shutting down the HBA. This function is called with no
9073 * lock held and always returns 1.
9074 * This function does the following to cleanup driver resources:
9075 * - Free discovery resources for each virtual port
9076 * - Cleanup any pending fabric iocbs
9077 * - Iterate through the iocb txq and free each entry
9078 * in the list.
9079 * - Free up any buffer posted to the HBA
9080 * - Free mailbox commands in the mailbox queue.
9081 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009082int
James Smart2e0fef82007-06-17 19:56:36 -05009083lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009084{
James Smart2534ba72007-04-25 09:52:20 -04009085 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009086 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009087 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009088 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009089 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009090 int i;
9091
9092 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009093 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009094
dea31012005-04-17 16:05:31 -05009095 lpfc_hba_down_prep(phba);
9096
James Smart92d7f7b2007-06-17 19:56:38 -05009097 lpfc_fabric_abort_hba(phba);
9098
James Smart2e0fef82007-06-17 19:56:36 -05009099 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009100 for (i = 0; i < psli->num_rings; i++) {
9101 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009102 /* Only slow rings */
9103 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009104 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009105 /* Set the lpfc data pending flag */
9106 set_bit(LPFC_DATA_READY, &phba->data_flags);
9107 }
dea31012005-04-17 16:05:31 -05009108
9109 /*
9110 * Error everything on the txq since these iocbs have not been
9111 * given to the FW yet.
9112 */
James Smart2534ba72007-04-25 09:52:20 -04009113 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009114 pring->txq_cnt = 0;
9115
dea31012005-04-17 16:05:31 -05009116 }
James Smart2e0fef82007-06-17 19:56:36 -05009117 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009118
James Smarta257bf92009-04-06 18:48:10 -04009119 /* Cancel all the IOCBs from the completions list */
9120 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9121 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009122
James Smart0ff10d42008-01-11 01:52:36 -05009123 spin_lock_irqsave(&phba->hbalock, flags);
9124 list_splice_init(&phba->elsbuf, &completions);
9125 phba->elsbuf_cnt = 0;
9126 phba->elsbuf_prev_cnt = 0;
9127 spin_unlock_irqrestore(&phba->hbalock, flags);
9128
9129 while (!list_empty(&completions)) {
9130 list_remove_head(&completions, buf_ptr,
9131 struct lpfc_dmabuf, list);
9132 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9133 kfree(buf_ptr);
9134 }
9135
dea31012005-04-17 16:05:31 -05009136 /* Return any active mbox cmds */
9137 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009138
James Smartda0436e2009-05-22 14:51:39 -04009139 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009140 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009141 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009142
James Smartda0436e2009-05-22 14:51:39 -04009143 return 1;
9144}
James Smart92d7f7b2007-06-17 19:56:38 -05009145
James Smartda0436e2009-05-22 14:51:39 -04009146/**
James Smart3621a712009-04-06 18:47:14 -04009147 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009148 * @srcp: Source memory pointer.
9149 * @destp: Destination memory pointer.
9150 * @cnt: Number of words required to be copied.
9151 *
9152 * This function is used for copying data between driver memory
9153 * and the SLI memory. This function also changes the endianness
9154 * of each word if native endianness is different from SLI
9155 * endianness. This function can be called with or without
9156 * lock.
9157 **/
dea31012005-04-17 16:05:31 -05009158void
9159lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9160{
9161 uint32_t *src = srcp;
9162 uint32_t *dest = destp;
9163 uint32_t ldata;
9164 int i;
9165
9166 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9167 ldata = *src;
9168 ldata = le32_to_cpu(ldata);
9169 *dest = ldata;
9170 src++;
9171 dest++;
9172 }
9173}
9174
James Smarte59058c2008-08-24 21:49:00 -04009175
9176/**
James Smarta0c87cb2009-07-19 10:01:10 -04009177 * lpfc_sli_bemem_bcopy - SLI memory copy function
9178 * @srcp: Source memory pointer.
9179 * @destp: Destination memory pointer.
9180 * @cnt: Number of words required to be copied.
9181 *
9182 * This function is used for copying data between a data structure
9183 * with big endian representation to local endianness.
9184 * This function can be called with or without lock.
9185 **/
9186void
9187lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9188{
9189 uint32_t *src = srcp;
9190 uint32_t *dest = destp;
9191 uint32_t ldata;
9192 int i;
9193
9194 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9195 ldata = *src;
9196 ldata = be32_to_cpu(ldata);
9197 *dest = ldata;
9198 src++;
9199 dest++;
9200 }
9201}
9202
9203/**
James Smart3621a712009-04-06 18:47:14 -04009204 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009205 * @phba: Pointer to HBA context object.
9206 * @pring: Pointer to driver SLI ring object.
9207 * @mp: Pointer to driver buffer object.
9208 *
9209 * This function is called with no lock held.
9210 * It always return zero after adding the buffer to the postbufq
9211 * buffer list.
9212 **/
dea31012005-04-17 16:05:31 -05009213int
James Smart2e0fef82007-06-17 19:56:36 -05009214lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9215 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009216{
9217 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9218 later */
James Smart2e0fef82007-06-17 19:56:36 -05009219 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009220 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009221 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009222 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009223 return 0;
9224}
9225
James Smarte59058c2008-08-24 21:49:00 -04009226/**
James Smart3621a712009-04-06 18:47:14 -04009227 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009228 * @phba: Pointer to HBA context object.
9229 *
9230 * When HBQ is enabled, buffers are searched based on tags. This function
9231 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9232 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9233 * does not conflict with tags of buffer posted for unsolicited events.
9234 * The function returns the allocated tag. The function is called with
9235 * no locks held.
9236 **/
James Smart76bb24e2007-10-27 13:38:00 -04009237uint32_t
9238lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9239{
9240 spin_lock_irq(&phba->hbalock);
9241 phba->buffer_tag_count++;
9242 /*
9243 * Always set the QUE_BUFTAG_BIT to distiguish between
9244 * a tag assigned by HBQ.
9245 */
9246 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9247 spin_unlock_irq(&phba->hbalock);
9248 return phba->buffer_tag_count;
9249}
9250
James Smarte59058c2008-08-24 21:49:00 -04009251/**
James Smart3621a712009-04-06 18:47:14 -04009252 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009253 * @phba: Pointer to HBA context object.
9254 * @pring: Pointer to driver SLI ring object.
9255 * @tag: Buffer tag.
9256 *
9257 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9258 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9259 * iocb is posted to the response ring with the tag of the buffer.
9260 * This function searches the pring->postbufq list using the tag
9261 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9262 * iocb. If the buffer is found then lpfc_dmabuf object of the
9263 * buffer is returned to the caller else NULL is returned.
9264 * This function is called with no lock held.
9265 **/
James Smart76bb24e2007-10-27 13:38:00 -04009266struct lpfc_dmabuf *
9267lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9268 uint32_t tag)
9269{
9270 struct lpfc_dmabuf *mp, *next_mp;
9271 struct list_head *slp = &pring->postbufq;
9272
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009273 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009274 spin_lock_irq(&phba->hbalock);
9275 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9276 if (mp->buffer_tag == tag) {
9277 list_del_init(&mp->list);
9278 pring->postbufq_cnt--;
9279 spin_unlock_irq(&phba->hbalock);
9280 return mp;
9281 }
9282 }
9283
9284 spin_unlock_irq(&phba->hbalock);
9285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009286 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009287 "ring %d Data x%lx x%p x%p x%x\n",
9288 pring->ringno, (unsigned long) tag,
9289 slp->next, slp->prev, pring->postbufq_cnt);
9290
9291 return NULL;
9292}
dea31012005-04-17 16:05:31 -05009293
James Smarte59058c2008-08-24 21:49:00 -04009294/**
James Smart3621a712009-04-06 18:47:14 -04009295 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009296 * @phba: Pointer to HBA context object.
9297 * @pring: Pointer to driver SLI ring object.
9298 * @phys: DMA address of the buffer.
9299 *
9300 * This function searches the buffer list using the dma_address
9301 * of unsolicited event to find the driver's lpfc_dmabuf object
9302 * corresponding to the dma_address. The function returns the
9303 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9304 * This function is called by the ct and els unsolicited event
9305 * handlers to get the buffer associated with the unsolicited
9306 * event.
9307 *
9308 * This function is called with no lock held.
9309 **/
dea31012005-04-17 16:05:31 -05009310struct lpfc_dmabuf *
9311lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9312 dma_addr_t phys)
9313{
9314 struct lpfc_dmabuf *mp, *next_mp;
9315 struct list_head *slp = &pring->postbufq;
9316
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009317 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009318 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009319 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9320 if (mp->phys == phys) {
9321 list_del_init(&mp->list);
9322 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009323 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009324 return mp;
9325 }
9326 }
9327
James Smart2e0fef82007-06-17 19:56:36 -05009328 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009330 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009331 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009332 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009333 slp->next, slp->prev, pring->postbufq_cnt);
9334 return NULL;
9335}
9336
James Smarte59058c2008-08-24 21:49:00 -04009337/**
James Smart3621a712009-04-06 18:47:14 -04009338 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009339 * @phba: Pointer to HBA context object.
9340 * @cmdiocb: Pointer to driver command iocb object.
9341 * @rspiocb: Pointer to driver response iocb object.
9342 *
9343 * This function is the completion handler for the abort iocbs for
9344 * ELS commands. This function is called from the ELS ring event
9345 * handler with no lock held. This function frees memory resources
9346 * associated with the abort iocb.
9347 **/
dea31012005-04-17 16:05:31 -05009348static void
James Smart2e0fef82007-06-17 19:56:36 -05009349lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9350 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009351{
James Smart2e0fef82007-06-17 19:56:36 -05009352 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009353 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009354 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009355
9356 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009357
9358 /*
9359 * Assume that the port already completed and returned, or
9360 * will return the iocb. Just Log the message.
9361 */
James Smart2680eea2007-04-25 09:52:55 -04009362 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9363 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9364
James Smart2e0fef82007-06-17 19:56:36 -05009365 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009366 if (phba->sli_rev < LPFC_SLI_REV4) {
9367 if (abort_iotag != 0 &&
9368 abort_iotag <= phba->sli.last_iotag)
9369 abort_iocb =
9370 phba->sli.iocbq_lookup[abort_iotag];
9371 } else
9372 /* For sli4 the abort_tag is the XRI,
9373 * so the abort routine puts the iotag of the iocb
9374 * being aborted in the context field of the abort
9375 * IOCB.
9376 */
9377 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009378
James Smart2a9bf3d2010-06-07 15:24:45 -04009379 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9380 "0327 Cannot abort els iocb %p "
9381 "with tag %x context %x, abort status %x, "
9382 "abort code %x\n",
9383 abort_iocb, abort_iotag, abort_context,
9384 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009385
James Smartff78d8f2011-12-13 13:21:35 -05009386 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009387 }
James Bottomley604a3e32005-10-29 10:28:33 -05009388 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009389 return;
9390}
9391
James Smarte59058c2008-08-24 21:49:00 -04009392/**
James Smart3621a712009-04-06 18:47:14 -04009393 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009394 * @phba: Pointer to HBA context object.
9395 * @cmdiocb: Pointer to driver command iocb object.
9396 * @rspiocb: Pointer to driver response iocb object.
9397 *
9398 * The function is called from SLI ring event handler with no
9399 * lock held. This function is the completion handler for ELS commands
9400 * which are aborted. The function frees memory resources used for
9401 * the aborted ELS commands.
9402 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009403static void
9404lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9405 struct lpfc_iocbq *rspiocb)
9406{
9407 IOCB_t *irsp = &rspiocb->iocb;
9408
9409 /* ELS cmd tag <ulpIoTag> completes */
9410 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009411 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009412 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009413 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009414 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009415 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9416 lpfc_ct_free_iocb(phba, cmdiocb);
9417 else
9418 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009419 return;
9420}
9421
James Smarte59058c2008-08-24 21:49:00 -04009422/**
James Smart5af5eee2010-10-22 11:06:38 -04009423 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009424 * @phba: Pointer to HBA context object.
9425 * @pring: Pointer to driver SLI ring object.
9426 * @cmdiocb: Pointer to driver command iocb object.
9427 *
James Smart5af5eee2010-10-22 11:06:38 -04009428 * This function issues an abort iocb for the provided command iocb down to
9429 * the port. Other than the case the outstanding command iocb is an abort
9430 * request, this function issues abort out unconditionally. This function is
9431 * called with hbalock held. The function returns 0 when it fails due to
9432 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009433 **/
James Smart5af5eee2010-10-22 11:06:38 -04009434static int
9435lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009436 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009437{
James Smart2e0fef82007-06-17 19:56:36 -05009438 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009439 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009440 IOCB_t *icmd = NULL;
9441 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009442 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009443 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009444
James Smart92d7f7b2007-06-17 19:56:38 -05009445 /*
9446 * There are certain command types we don't want to abort. And we
9447 * don't want to abort commands that are already in the process of
9448 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009449 */
9450 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009451 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009452 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9453 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009454 return 0;
9455
dea31012005-04-17 16:05:31 -05009456 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009457 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009458 if (abtsiocbp == NULL)
9459 return 0;
dea31012005-04-17 16:05:31 -05009460
James Smart07951072007-04-25 09:51:38 -04009461 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009462 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009463 */
9464 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9465
dea31012005-04-17 16:05:31 -05009466 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009467 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9468 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009469 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009470 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009471 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9472 }
James Smartda0436e2009-05-22 14:51:39 -04009473 else
9474 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009475 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009476 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009477
James Smart5ffc2662009-11-18 15:39:44 -05009478 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9479 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009480 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9481 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009482
James Smart2e0fef82007-06-17 19:56:36 -05009483 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009484 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9485 else
9486 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9487
9488 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009489
James Smarte8b62012007-08-02 11:10:09 -04009490 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9491 "0339 Abort xri x%x, original iotag x%x, "
9492 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009493 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009494 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009495 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009496
9497 if (phba->sli_rev == LPFC_SLI_REV4) {
9498 /* Note: both hbalock and ring_lock need to be set here */
9499 spin_lock_irqsave(&pring->ring_lock, iflags);
9500 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9501 abtsiocbp, 0);
9502 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9503 } else {
9504 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9505 abtsiocbp, 0);
9506 }
James Smart07951072007-04-25 09:51:38 -04009507
James Smartd7c255b2008-08-24 21:50:00 -04009508 if (retval)
9509 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009510
9511 /*
9512 * Caller to this routine should check for IOCB_ERROR
9513 * and handle it properly. This routine no longer removes
9514 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9515 */
9516 return retval;
9517}
9518
9519/**
9520 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9521 * @phba: Pointer to HBA context object.
9522 * @pring: Pointer to driver SLI ring object.
9523 * @cmdiocb: Pointer to driver command iocb object.
9524 *
9525 * This function issues an abort iocb for the provided command iocb. In case
9526 * of unloading, the abort iocb will not be issued to commands on the ELS
9527 * ring. Instead, the callback function shall be changed to those commands
9528 * so that nothing happens when them finishes. This function is called with
9529 * hbalock held. The function returns 0 when the command iocb is an abort
9530 * request.
9531 **/
9532int
9533lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9534 struct lpfc_iocbq *cmdiocb)
9535{
9536 struct lpfc_vport *vport = cmdiocb->vport;
9537 int retval = IOCB_ERROR;
9538 IOCB_t *icmd = NULL;
9539
9540 /*
9541 * There are certain command types we don't want to abort. And we
9542 * don't want to abort commands that are already in the process of
9543 * being aborted.
9544 */
9545 icmd = &cmdiocb->iocb;
9546 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9547 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9548 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9549 return 0;
9550
9551 /*
9552 * If we're unloading, don't abort iocb on the ELS ring, but change
9553 * the callback so that nothing happens when it finishes.
9554 */
9555 if ((vport->load_flag & FC_UNLOADING) &&
9556 (pring->ringno == LPFC_ELS_RING)) {
9557 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9558 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9559 else
9560 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9561 goto abort_iotag_exit;
9562 }
9563
9564 /* Now, we try to issue the abort to the cmdiocb out */
9565 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9566
James Smart07951072007-04-25 09:51:38 -04009567abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009568 /*
9569 * Caller to this routine should check for IOCB_ERROR
9570 * and handle it properly. This routine no longer removes
9571 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009572 */
James Smart2e0fef82007-06-17 19:56:36 -05009573 return retval;
dea31012005-04-17 16:05:31 -05009574}
9575
James Smarte59058c2008-08-24 21:49:00 -04009576/**
James Smart5af5eee2010-10-22 11:06:38 -04009577 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9578 * @phba: Pointer to HBA context object.
9579 * @pring: Pointer to driver SLI ring object.
9580 *
9581 * This function aborts all iocbs in the given ring and frees all the iocb
9582 * objects in txq. This function issues abort iocbs unconditionally for all
9583 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9584 * to complete before the return of this function. The caller is not required
9585 * to hold any locks.
9586 **/
9587static void
9588lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9589{
9590 LIST_HEAD(completions);
9591 struct lpfc_iocbq *iocb, *next_iocb;
9592
9593 if (pring->ringno == LPFC_ELS_RING)
9594 lpfc_fabric_abort_hba(phba);
9595
9596 spin_lock_irq(&phba->hbalock);
9597
9598 /* Take off all the iocbs on txq for cancelling */
9599 list_splice_init(&pring->txq, &completions);
9600 pring->txq_cnt = 0;
9601
9602 /* Next issue ABTS for everything on the txcmplq */
9603 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9604 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9605
9606 spin_unlock_irq(&phba->hbalock);
9607
9608 /* Cancel all the IOCBs from the completions list */
9609 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9610 IOERR_SLI_ABORTED);
9611}
9612
9613/**
9614 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9615 * @phba: pointer to lpfc HBA data structure.
9616 *
9617 * This routine will abort all pending and outstanding iocbs to an HBA.
9618 **/
9619void
9620lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9621{
9622 struct lpfc_sli *psli = &phba->sli;
9623 struct lpfc_sli_ring *pring;
9624 int i;
9625
9626 for (i = 0; i < psli->num_rings; i++) {
9627 pring = &psli->ring[i];
9628 lpfc_sli_iocb_ring_abort(phba, pring);
9629 }
9630}
9631
9632/**
James Smart3621a712009-04-06 18:47:14 -04009633 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009634 * @iocbq: Pointer to driver iocb object.
9635 * @vport: Pointer to driver virtual port object.
9636 * @tgt_id: SCSI ID of the target.
9637 * @lun_id: LUN ID of the scsi device.
9638 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9639 *
James Smart3621a712009-04-06 18:47:14 -04009640 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009641 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9642 * 0 if the filtering criteria is met for the given iocb and will return
9643 * 1 if the filtering criteria is not met.
9644 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9645 * given iocb is for the SCSI device specified by vport, tgt_id and
9646 * lun_id parameter.
9647 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9648 * given iocb is for the SCSI target specified by vport and tgt_id
9649 * parameters.
9650 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9651 * given iocb is for the SCSI host associated with the given vport.
9652 * This function is called with no locks held.
9653 **/
dea31012005-04-17 16:05:31 -05009654static int
James Smart51ef4c22007-08-02 11:10:31 -04009655lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9656 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009657 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009658{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009659 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009660 int rc = 1;
9661
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009662 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9663 return rc;
9664
James Smart51ef4c22007-08-02 11:10:31 -04009665 if (iocbq->vport != vport)
9666 return rc;
9667
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009668 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009669
James Smart495a7142008-06-14 22:52:59 -04009670 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009671 return rc;
9672
9673 switch (ctx_cmd) {
9674 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009675 if ((lpfc_cmd->rdata->pnode) &&
9676 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9677 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009678 rc = 0;
9679 break;
9680 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009681 if ((lpfc_cmd->rdata->pnode) &&
9682 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009683 rc = 0;
9684 break;
dea31012005-04-17 16:05:31 -05009685 case LPFC_CTX_HOST:
9686 rc = 0;
9687 break;
9688 default:
9689 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009690 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009691 break;
9692 }
9693
9694 return rc;
9695}
9696
James Smarte59058c2008-08-24 21:49:00 -04009697/**
James Smart3621a712009-04-06 18:47:14 -04009698 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009699 * @vport: Pointer to virtual port.
9700 * @tgt_id: SCSI ID of the target.
9701 * @lun_id: LUN ID of the scsi device.
9702 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9703 *
9704 * This function returns number of FCP commands pending for the vport.
9705 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9706 * commands pending on the vport associated with SCSI device specified
9707 * by tgt_id and lun_id parameters.
9708 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9709 * commands pending on the vport associated with SCSI target specified
9710 * by tgt_id parameter.
9711 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9712 * commands pending on the vport.
9713 * This function returns the number of iocbs which satisfy the filter.
9714 * This function is called without any lock held.
9715 **/
dea31012005-04-17 16:05:31 -05009716int
James Smart51ef4c22007-08-02 11:10:31 -04009717lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9718 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009719{
James Smart51ef4c22007-08-02 11:10:31 -04009720 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009721 struct lpfc_iocbq *iocbq;
9722 int sum, i;
dea31012005-04-17 16:05:31 -05009723
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009724 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9725 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009726
James Smart51ef4c22007-08-02 11:10:31 -04009727 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9728 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009729 sum++;
dea31012005-04-17 16:05:31 -05009730 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009731
dea31012005-04-17 16:05:31 -05009732 return sum;
9733}
9734
James Smarte59058c2008-08-24 21:49:00 -04009735/**
James Smart3621a712009-04-06 18:47:14 -04009736 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009737 * @phba: Pointer to HBA context object
9738 * @cmdiocb: Pointer to command iocb object.
9739 * @rspiocb: Pointer to response iocb object.
9740 *
9741 * This function is called when an aborted FCP iocb completes. This
9742 * function is called by the ring event handler with no lock held.
9743 * This function frees the iocb.
9744 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009745void
James Smart2e0fef82007-06-17 19:56:36 -05009746lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9747 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009748{
James Smartcb69f7d2011-12-13 13:21:57 -05009749 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9750 "3096 ABORT_XRI_CN completing on xri x%x "
9751 "original iotag x%x, abort cmd iotag x%x "
9752 "status 0x%x, reason 0x%x\n",
9753 cmdiocb->iocb.un.acxri.abortContextTag,
9754 cmdiocb->iocb.un.acxri.abortIoTag,
9755 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9756 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009757 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009758 return;
9759}
9760
James Smarte59058c2008-08-24 21:49:00 -04009761/**
James Smart3621a712009-04-06 18:47:14 -04009762 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009763 * @vport: Pointer to virtual port.
9764 * @pring: Pointer to driver SLI ring object.
9765 * @tgt_id: SCSI ID of the target.
9766 * @lun_id: LUN ID of the scsi device.
9767 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9768 *
9769 * This function sends an abort command for every SCSI command
9770 * associated with the given virtual port pending on the ring
9771 * filtered by lpfc_sli_validate_fcp_iocb function.
9772 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9773 * FCP iocbs associated with lun specified by tgt_id and lun_id
9774 * parameters
9775 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9776 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9777 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9778 * FCP iocbs associated with virtual port.
9779 * This function returns number of iocbs it failed to abort.
9780 * This function is called with no locks held.
9781 **/
dea31012005-04-17 16:05:31 -05009782int
James Smart51ef4c22007-08-02 11:10:31 -04009783lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9784 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009785{
James Smart51ef4c22007-08-02 11:10:31 -04009786 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009787 struct lpfc_iocbq *iocbq;
9788 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009789 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009790 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009791 int i;
dea31012005-04-17 16:05:31 -05009792
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009793 for (i = 1; i <= phba->sli.last_iotag; i++) {
9794 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009795
James Smart51ef4c22007-08-02 11:10:31 -04009796 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009797 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009798 continue;
9799
9800 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009801 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009802 if (abtsiocb == NULL) {
9803 errcnt++;
9804 continue;
9805 }
dea31012005-04-17 16:05:31 -05009806
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009807 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009808 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9809 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009810 if (phba->sli_rev == LPFC_SLI_REV4)
9811 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9812 else
9813 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009814 abtsiocb->iocb.ulpLe = 1;
9815 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009816 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009817
James Smart5ffc2662009-11-18 15:39:44 -05009818 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9819 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009820 if (iocbq->iocb_flag & LPFC_IO_FCP)
9821 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009822
James Smart2e0fef82007-06-17 19:56:36 -05009823 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009824 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9825 else
9826 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9827
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009828 /* Setup callback routine and issue the command. */
9829 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009830 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9831 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009832 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009833 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009834 errcnt++;
9835 continue;
9836 }
9837 }
9838
9839 return errcnt;
9840}
9841
James Smarte59058c2008-08-24 21:49:00 -04009842/**
James Smart3621a712009-04-06 18:47:14 -04009843 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009844 * @phba: Pointer to HBA context object.
9845 * @cmdiocbq: Pointer to command iocb.
9846 * @rspiocbq: Pointer to response iocb.
9847 *
9848 * This function is the completion handler for iocbs issued using
9849 * lpfc_sli_issue_iocb_wait function. This function is called by the
9850 * ring event handler function without any lock held. This function
9851 * can be called from both worker thread context and interrupt
9852 * context. This function also can be called from other thread which
9853 * cleans up the SLI layer objects.
9854 * This function copy the contents of the response iocb to the
9855 * response iocb memory object provided by the caller of
9856 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9857 * sleeps for the iocb completion.
9858 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009859static void
9860lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9861 struct lpfc_iocbq *cmdiocbq,
9862 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009863{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009864 wait_queue_head_t *pdone_q;
9865 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009866 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009867
James Smart2e0fef82007-06-17 19:56:36 -05009868 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009869 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9870 if (cmdiocbq->context2 && rspiocbq)
9871 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9872 &rspiocbq->iocb, sizeof(IOCB_t));
9873
James Smart0f65ff62010-02-26 14:14:23 -05009874 /* Set the exchange busy flag for task management commands */
9875 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9876 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9877 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9878 cur_iocbq);
9879 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9880 }
9881
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009882 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009883 if (pdone_q)
9884 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009885 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009886 return;
9887}
9888
James Smarte59058c2008-08-24 21:49:00 -04009889/**
James Smartd11e31d2009-06-10 17:23:06 -04009890 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9891 * @phba: Pointer to HBA context object..
9892 * @piocbq: Pointer to command iocb.
9893 * @flag: Flag to test.
9894 *
9895 * This routine grabs the hbalock and then test the iocb_flag to
9896 * see if the passed in flag is set.
9897 * Returns:
9898 * 1 if flag is set.
9899 * 0 if flag is not set.
9900 **/
9901static int
9902lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9903 struct lpfc_iocbq *piocbq, uint32_t flag)
9904{
9905 unsigned long iflags;
9906 int ret;
9907
9908 spin_lock_irqsave(&phba->hbalock, iflags);
9909 ret = piocbq->iocb_flag & flag;
9910 spin_unlock_irqrestore(&phba->hbalock, iflags);
9911 return ret;
9912
9913}
9914
9915/**
James Smart3621a712009-04-06 18:47:14 -04009916 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009917 * @phba: Pointer to HBA context object..
9918 * @pring: Pointer to sli ring.
9919 * @piocb: Pointer to command iocb.
9920 * @prspiocbq: Pointer to response iocb.
9921 * @timeout: Timeout in number of seconds.
9922 *
9923 * This function issues the iocb to firmware and waits for the
9924 * iocb to complete. If the iocb command is not
9925 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9926 * Caller should not free the iocb resources if this function
9927 * returns IOCB_TIMEDOUT.
9928 * The function waits for the iocb completion using an
9929 * non-interruptible wait.
9930 * This function will sleep while waiting for iocb completion.
9931 * So, this function should not be called from any context which
9932 * does not allow sleeping. Due to the same reason, this function
9933 * cannot be called with interrupt disabled.
9934 * This function assumes that the iocb completions occur while
9935 * this function sleep. So, this function cannot be called from
9936 * the thread which process iocb completion for this ring.
9937 * This function clears the iocb_flag of the iocb object before
9938 * issuing the iocb and the iocb completion handler sets this
9939 * flag and wakes this thread when the iocb completes.
9940 * The contents of the response iocb will be copied to prspiocbq
9941 * by the completion handler when the command completes.
9942 * This function returns IOCB_SUCCESS when success.
9943 * This function is called with no lock held.
9944 **/
dea31012005-04-17 16:05:31 -05009945int
James Smart2e0fef82007-06-17 19:56:36 -05009946lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009947 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009948 struct lpfc_iocbq *piocb,
9949 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009950 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009951{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009952 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009953 long timeleft, timeout_req = 0;
9954 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009955 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009956 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009957 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009958 * If the caller has provided a response iocbq buffer, then context2
9959 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009960 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009961 if (prspiocbq) {
9962 if (piocb->context2)
9963 return IOCB_ERROR;
9964 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009965 }
9966
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009967 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9968 piocb->context_un.wait_queue = &done_q;
9969 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009970
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009971 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009972 if (lpfc_readl(phba->HCregaddr, &creg_val))
9973 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009974 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9975 writel(creg_val, phba->HCregaddr);
9976 readl(phba->HCregaddr); /* flush */
9977 }
9978
James Smart2a9bf3d2010-06-07 15:24:45 -04009979 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9980 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009981 if (retval == IOCB_SUCCESS) {
9982 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009983 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009984 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009985 timeout_req);
dea31012005-04-17 16:05:31 -05009986
James Smart7054a602007-04-25 09:52:34 -04009987 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9988 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009989 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009990 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009992 "0338 IOCB wait timeout error - no "
9993 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009994 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009995 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009997 "0330 IOCB wake NOT set, "
9998 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009999 timeout, (timeleft / jiffies));
10000 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010001 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010002 } else if (retval == IOCB_BUSY) {
10003 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10004 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10005 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
10006 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010007 } else {
10008 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010009 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010010 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010011 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010012 }
10013
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010014 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010015 if (lpfc_readl(phba->HCregaddr, &creg_val))
10016 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010017 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10018 writel(creg_val, phba->HCregaddr);
10019 readl(phba->HCregaddr); /* flush */
10020 }
10021
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010022 if (prspiocbq)
10023 piocb->context2 = NULL;
10024
10025 piocb->context_un.wait_queue = NULL;
10026 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010027 return retval;
10028}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010029
James Smarte59058c2008-08-24 21:49:00 -040010030/**
James Smart3621a712009-04-06 18:47:14 -040010031 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010032 * @phba: Pointer to HBA context object.
10033 * @pmboxq: Pointer to driver mailbox object.
10034 * @timeout: Timeout in number of seconds.
10035 *
10036 * This function issues the mailbox to firmware and waits for the
10037 * mailbox command to complete. If the mailbox command is not
10038 * completed within timeout seconds, it returns MBX_TIMEOUT.
10039 * The function waits for the mailbox completion using an
10040 * interruptible wait. If the thread is woken up due to a
10041 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10042 * should not free the mailbox resources, if this function returns
10043 * MBX_TIMEOUT.
10044 * This function will sleep while waiting for mailbox completion.
10045 * So, this function should not be called from any context which
10046 * does not allow sleeping. Due to the same reason, this function
10047 * cannot be called with interrupt disabled.
10048 * This function assumes that the mailbox completion occurs while
10049 * this function sleep. So, this function cannot be called from
10050 * the worker thread which processes mailbox completion.
10051 * This function is called in the context of HBA management
10052 * applications.
10053 * This function returns MBX_SUCCESS when successful.
10054 * This function is called with no lock held.
10055 **/
dea31012005-04-17 16:05:31 -050010056int
James Smart2e0fef82007-06-17 19:56:36 -050010057lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010058 uint32_t timeout)
10059{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010060 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -050010061 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010062 unsigned long flag;
dea31012005-04-17 16:05:31 -050010063
10064 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -040010065 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -050010066 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -050010067
James Smart495a7142008-06-14 22:52:59 -040010068 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010069 /* setup wake call as IOCB callback */
10070 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10071 /* setup context field to pass wait_queue pointer to wake function */
10072 pmboxq->context1 = &done_q;
10073
dea31012005-04-17 16:05:31 -050010074 /* now issue the command */
10075 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010076 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010077 wait_event_interruptible_timeout(done_q,
10078 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10079 timeout * HZ);
10080
James Smart858c9f62007-06-17 19:56:39 -050010081 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010082 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -040010083 /*
10084 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10085 * else do not free the resources.
10086 */
James Smartd7c47992010-06-08 18:31:54 -040010087 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010088 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010089 lpfc_sli4_swap_str(phba, pmboxq);
10090 } else {
James Smart7054a602007-04-25 09:52:34 -040010091 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010092 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10093 }
10094 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010095 }
10096
dea31012005-04-17 16:05:31 -050010097 return retval;
10098}
10099
James Smarte59058c2008-08-24 21:49:00 -040010100/**
James Smart3772a992009-05-22 14:50:54 -040010101 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010102 * @phba: Pointer to HBA context.
10103 *
James Smart3772a992009-05-22 14:50:54 -040010104 * This function is called to shutdown the driver's mailbox sub-system.
10105 * It first marks the mailbox sub-system is in a block state to prevent
10106 * the asynchronous mailbox command from issued off the pending mailbox
10107 * command queue. If the mailbox command sub-system shutdown is due to
10108 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10109 * the mailbox sub-system flush routine to forcefully bring down the
10110 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10111 * as with offline or HBA function reset), this routine will wait for the
10112 * outstanding mailbox command to complete before invoking the mailbox
10113 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010114 **/
James Smart3772a992009-05-22 14:50:54 -040010115void
James Smart618a5232012-06-12 13:54:36 -040010116lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010117{
James Smart3772a992009-05-22 14:50:54 -040010118 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010119 unsigned long timeout;
10120
James Smart618a5232012-06-12 13:54:36 -040010121 if (mbx_action == LPFC_MBX_NO_WAIT) {
10122 /* delay 100ms for port state */
10123 msleep(100);
10124 lpfc_sli_mbox_sys_flush(phba);
10125 return;
10126 }
James Smarta183a152011-10-10 21:32:43 -040010127 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010128
James Smart3772a992009-05-22 14:50:54 -040010129 spin_lock_irq(&phba->hbalock);
10130 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010131
10132 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010133 /* Determine how long we might wait for the active mailbox
10134 * command to be gracefully completed by firmware.
10135 */
James Smarta183a152011-10-10 21:32:43 -040010136 if (phba->sli.mbox_active)
10137 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10138 phba->sli.mbox_active) *
10139 1000) + jiffies;
10140 spin_unlock_irq(&phba->hbalock);
10141
James Smart3772a992009-05-22 14:50:54 -040010142 while (phba->sli.mbox_active) {
10143 /* Check active mailbox complete status every 2ms */
10144 msleep(2);
10145 if (time_after(jiffies, timeout))
10146 /* Timeout, let the mailbox flush routine to
10147 * forcefully release active mailbox command
10148 */
10149 break;
10150 }
James Smartd7069f02012-03-01 22:36:29 -050010151 } else
10152 spin_unlock_irq(&phba->hbalock);
10153
James Smart3772a992009-05-22 14:50:54 -040010154 lpfc_sli_mbox_sys_flush(phba);
10155}
10156
10157/**
10158 * lpfc_sli_eratt_read - read sli-3 error attention events
10159 * @phba: Pointer to HBA context.
10160 *
10161 * This function is called to read the SLI3 device error attention registers
10162 * for possible error attention events. The caller must hold the hostlock
10163 * with spin_lock_irq().
10164 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010165 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010166 * Register and returns 0 otherwise.
10167 **/
10168static int
10169lpfc_sli_eratt_read(struct lpfc_hba *phba)
10170{
James Smarted957682007-06-17 19:56:37 -050010171 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010172
James Smart3772a992009-05-22 14:50:54 -040010173 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010174 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10175 goto unplug_err;
10176
James Smart3772a992009-05-22 14:50:54 -040010177 if (ha_copy & HA_ERATT) {
10178 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010179 if (lpfc_sli_read_hs(phba))
10180 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010181
James Smart3772a992009-05-22 14:50:54 -040010182 /* Check if there is a deferred error condition is active */
10183 if ((HS_FFER1 & phba->work_hs) &&
10184 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010185 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010186 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010187 /* Clear all interrupt enable conditions */
10188 writel(0, phba->HCregaddr);
10189 readl(phba->HCregaddr);
10190 }
10191
10192 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010193 phba->work_ha |= HA_ERATT;
10194 /* Indicate polling handles this ERATT */
10195 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010196 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010197 }
James Smart3772a992009-05-22 14:50:54 -040010198 return 0;
James Smart9940b972011-03-11 16:06:12 -050010199
10200unplug_err:
10201 /* Set the driver HS work bitmap */
10202 phba->work_hs |= UNPLUG_ERR;
10203 /* Set the driver HA work bitmap */
10204 phba->work_ha |= HA_ERATT;
10205 /* Indicate polling handles this ERATT */
10206 phba->hba_flag |= HBA_ERATT_HANDLED;
10207 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010208}
10209
James Smarte59058c2008-08-24 21:49:00 -040010210/**
James Smartda0436e2009-05-22 14:51:39 -040010211 * lpfc_sli4_eratt_read - read sli-4 error attention events
10212 * @phba: Pointer to HBA context.
10213 *
10214 * This function is called to read the SLI4 device error attention registers
10215 * for possible error attention events. The caller must hold the hostlock
10216 * with spin_lock_irq().
10217 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010218 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010219 * Register and returns 0 otherwise.
10220 **/
10221static int
10222lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10223{
10224 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010225 uint32_t if_type, portsmphr;
10226 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010227
James Smart2fcee4b2010-12-15 17:57:46 -050010228 /*
10229 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010230 * registers for error attention. This can be changed later.
10231 */
James Smart2fcee4b2010-12-15 17:57:46 -050010232 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10233 switch (if_type) {
10234 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010235 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10236 &uerr_sta_lo) ||
10237 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10238 &uerr_sta_hi)) {
10239 phba->work_hs |= UNPLUG_ERR;
10240 phba->work_ha |= HA_ERATT;
10241 phba->hba_flag |= HBA_ERATT_HANDLED;
10242 return 1;
10243 }
James Smart2fcee4b2010-12-15 17:57:46 -050010244 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10245 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10247 "1423 HBA Unrecoverable error: "
10248 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10249 "ue_mask_lo_reg=0x%x, "
10250 "ue_mask_hi_reg=0x%x\n",
10251 uerr_sta_lo, uerr_sta_hi,
10252 phba->sli4_hba.ue_mask_lo,
10253 phba->sli4_hba.ue_mask_hi);
10254 phba->work_status[0] = uerr_sta_lo;
10255 phba->work_status[1] = uerr_sta_hi;
10256 phba->work_ha |= HA_ERATT;
10257 phba->hba_flag |= HBA_ERATT_HANDLED;
10258 return 1;
10259 }
10260 break;
10261 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010262 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10263 &portstat_reg.word0) ||
10264 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10265 &portsmphr)){
10266 phba->work_hs |= UNPLUG_ERR;
10267 phba->work_ha |= HA_ERATT;
10268 phba->hba_flag |= HBA_ERATT_HANDLED;
10269 return 1;
10270 }
James Smart2fcee4b2010-12-15 17:57:46 -050010271 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10272 phba->work_status[0] =
10273 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10274 phba->work_status[1] =
10275 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010277 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010278 "port status reg 0x%x, "
10279 "port smphr reg 0x%x, "
10280 "error 1=0x%x, error 2=0x%x\n",
10281 portstat_reg.word0,
10282 portsmphr,
10283 phba->work_status[0],
10284 phba->work_status[1]);
10285 phba->work_ha |= HA_ERATT;
10286 phba->hba_flag |= HBA_ERATT_HANDLED;
10287 return 1;
10288 }
10289 break;
10290 case LPFC_SLI_INTF_IF_TYPE_1:
10291 default:
James Smarta747c9c2009-11-18 15:41:10 -050010292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010293 "2886 HBA Error Attention on unsupported "
10294 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010295 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010296 }
James Smart2fcee4b2010-12-15 17:57:46 -050010297
James Smartda0436e2009-05-22 14:51:39 -040010298 return 0;
10299}
10300
10301/**
James Smart3621a712009-04-06 18:47:14 -040010302 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010303 * @phba: Pointer to HBA context.
10304 *
James Smart3772a992009-05-22 14:50:54 -040010305 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010306 * error attention register bit for error attention events.
10307 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010308 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010309 * Register and returns 0 otherwise.
10310 **/
10311int
10312lpfc_sli_check_eratt(struct lpfc_hba *phba)
10313{
10314 uint32_t ha_copy;
10315
10316 /* If somebody is waiting to handle an eratt, don't process it
10317 * here. The brdkill function will do this.
10318 */
10319 if (phba->link_flag & LS_IGNORE_ERATT)
10320 return 0;
10321
10322 /* Check if interrupt handler handles this ERATT */
10323 spin_lock_irq(&phba->hbalock);
10324 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10325 /* Interrupt handler has handled ERATT */
10326 spin_unlock_irq(&phba->hbalock);
10327 return 0;
10328 }
10329
James Smarta257bf92009-04-06 18:48:10 -040010330 /*
10331 * If there is deferred error attention, do not check for error
10332 * attention
10333 */
10334 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10335 spin_unlock_irq(&phba->hbalock);
10336 return 0;
10337 }
10338
James Smart3772a992009-05-22 14:50:54 -040010339 /* If PCI channel is offline, don't process it */
10340 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010341 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010342 return 0;
10343 }
10344
10345 switch (phba->sli_rev) {
10346 case LPFC_SLI_REV2:
10347 case LPFC_SLI_REV3:
10348 /* Read chip Host Attention (HA) register */
10349 ha_copy = lpfc_sli_eratt_read(phba);
10350 break;
James Smartda0436e2009-05-22 14:51:39 -040010351 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010352 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010353 ha_copy = lpfc_sli4_eratt_read(phba);
10354 break;
James Smart3772a992009-05-22 14:50:54 -040010355 default:
10356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10357 "0299 Invalid SLI revision (%d)\n",
10358 phba->sli_rev);
10359 ha_copy = 0;
10360 break;
James Smart93996272008-08-24 21:50:30 -040010361 }
10362 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010363
10364 return ha_copy;
10365}
10366
10367/**
10368 * lpfc_intr_state_check - Check device state for interrupt handling
10369 * @phba: Pointer to HBA context.
10370 *
10371 * This inline routine checks whether a device or its PCI slot is in a state
10372 * that the interrupt should be handled.
10373 *
10374 * This function returns 0 if the device or the PCI slot is in a state that
10375 * interrupt should be handled, otherwise -EIO.
10376 */
10377static inline int
10378lpfc_intr_state_check(struct lpfc_hba *phba)
10379{
10380 /* If the pci channel is offline, ignore all the interrupts */
10381 if (unlikely(pci_channel_offline(phba->pcidev)))
10382 return -EIO;
10383
10384 /* Update device level interrupt statistics */
10385 phba->sli.slistat.sli_intr++;
10386
10387 /* Ignore all interrupts during initialization. */
10388 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10389 return -EIO;
10390
James Smart93996272008-08-24 21:50:30 -040010391 return 0;
10392}
10393
10394/**
James Smart3772a992009-05-22 14:50:54 -040010395 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010396 * @irq: Interrupt number.
10397 * @dev_id: The device context pointer.
10398 *
James Smart93996272008-08-24 21:50:30 -040010399 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010400 * service routine when device with SLI-3 interface spec is enabled with
10401 * MSI-X multi-message interrupt mode and there are slow-path events in
10402 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10403 * interrupt mode, this function is called as part of the device-level
10404 * interrupt handler. When the PCI slot is in error recovery or the HBA
10405 * is undergoing initialization, the interrupt handler will not process
10406 * the interrupt. The link attention and ELS ring attention events are
10407 * handled by the worker thread. The interrupt handler signals the worker
10408 * thread and returns for these events. This function is called without
10409 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010410 * structures.
10411 *
10412 * This function returns IRQ_HANDLED when interrupt is handled else it
10413 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010414 **/
dea31012005-04-17 16:05:31 -050010415irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010416lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010417{
James Smart2e0fef82007-06-17 19:56:36 -050010418 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010419 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010420 uint32_t work_ha_copy;
10421 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010422 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010423 uint32_t control;
10424
James Smart92d7f7b2007-06-17 19:56:38 -050010425 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010426 struct lpfc_vport *vport;
10427 struct lpfc_nodelist *ndlp;
10428 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010429 LPFC_MBOXQ_t *pmb;
10430 int rc;
10431
dea31012005-04-17 16:05:31 -050010432 /*
10433 * Get the driver's phba structure from the dev_id and
10434 * assume the HBA is not interrupting.
10435 */
James Smart93996272008-08-24 21:50:30 -040010436 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010437
10438 if (unlikely(!phba))
10439 return IRQ_NONE;
10440
dea31012005-04-17 16:05:31 -050010441 /*
James Smart93996272008-08-24 21:50:30 -040010442 * Stuff needs to be attented to when this function is invoked as an
10443 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010444 */
James Smart93996272008-08-24 21:50:30 -040010445 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010446 /* Check device state for handling interrupt */
10447 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010448 return IRQ_NONE;
10449 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010450 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010451 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10452 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010453 /* If somebody is waiting to handle an eratt don't process it
10454 * here. The brdkill function will do this.
10455 */
10456 if (phba->link_flag & LS_IGNORE_ERATT)
10457 ha_copy &= ~HA_ERATT;
10458 /* Check the need for handling ERATT in interrupt handler */
10459 if (ha_copy & HA_ERATT) {
10460 if (phba->hba_flag & HBA_ERATT_HANDLED)
10461 /* ERATT polling has handled ERATT */
10462 ha_copy &= ~HA_ERATT;
10463 else
10464 /* Indicate interrupt handler handles ERATT */
10465 phba->hba_flag |= HBA_ERATT_HANDLED;
10466 }
James Smarta257bf92009-04-06 18:48:10 -040010467
10468 /*
10469 * If there is deferred error attention, do not check for any
10470 * interrupt.
10471 */
10472 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010473 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010474 return IRQ_NONE;
10475 }
10476
James Smart93996272008-08-24 21:50:30 -040010477 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010478 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10479 goto unplug_error;
10480
James Smarta747c9c2009-11-18 15:41:10 -050010481 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10482 HC_LAINT_ENA | HC_ERINT_ENA),
10483 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010484 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10485 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010486 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010487 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010488 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010489 } else
10490 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010491
dea31012005-04-17 16:05:31 -050010492 work_ha_copy = ha_copy & phba->work_ha_mask;
10493
James Smart93996272008-08-24 21:50:30 -040010494 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010495 if (work_ha_copy & HA_LATT) {
10496 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10497 /*
10498 * Turn off Link Attention interrupts
10499 * until CLEAR_LA done
10500 */
James Smart5b75da22008-12-04 22:39:35 -050010501 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010502 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010503 if (lpfc_readl(phba->HCregaddr, &control))
10504 goto unplug_error;
dea31012005-04-17 16:05:31 -050010505 control &= ~HC_LAINT_ENA;
10506 writel(control, phba->HCregaddr);
10507 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010508 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010509 }
10510 else
10511 work_ha_copy &= ~HA_LATT;
10512 }
10513
James Smart93996272008-08-24 21:50:30 -040010514 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010515 /*
10516 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10517 * the only slow ring.
10518 */
10519 status = (work_ha_copy &
10520 (HA_RXMASK << (4*LPFC_ELS_RING)));
10521 status >>= (4*LPFC_ELS_RING);
10522 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010523 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010524 if (lpfc_readl(phba->HCregaddr, &control))
10525 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010526
10527 lpfc_debugfs_slow_ring_trc(phba,
10528 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10529 control, status,
10530 (uint32_t)phba->sli.slistat.sli_intr);
10531
James Smart858c9f62007-06-17 19:56:39 -050010532 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010533 lpfc_debugfs_slow_ring_trc(phba,
10534 "ISR Disable ring:"
10535 "pwork:x%x hawork:x%x wait:x%x",
10536 phba->work_ha, work_ha_copy,
10537 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010538 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010539
James Smart858c9f62007-06-17 19:56:39 -050010540 control &=
10541 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010542 writel(control, phba->HCregaddr);
10543 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010544 }
James Smarta58cbd52007-08-02 11:09:43 -040010545 else {
10546 lpfc_debugfs_slow_ring_trc(phba,
10547 "ISR slow ring: pwork:"
10548 "x%x hawork:x%x wait:x%x",
10549 phba->work_ha, work_ha_copy,
10550 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010551 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010552 }
James Smart5b75da22008-12-04 22:39:35 -050010553 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010554 }
10555 }
James Smart5b75da22008-12-04 22:39:35 -050010556 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010557 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010558 if (lpfc_sli_read_hs(phba))
10559 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010560 /*
10561 * Check if there is a deferred error condition
10562 * is active
10563 */
10564 if ((HS_FFER1 & phba->work_hs) &&
10565 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010566 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10567 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010568 phba->hba_flag |= DEFER_ERATT;
10569 /* Clear all interrupt enable conditions */
10570 writel(0, phba->HCregaddr);
10571 readl(phba->HCregaddr);
10572 }
10573 }
10574
James Smart93996272008-08-24 21:50:30 -040010575 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010576 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010577 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010578 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010579 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010580
10581 /* First check out the status word */
10582 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10583 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010584 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010585 /*
10586 * Stray Mailbox Interrupt, mbxCommand <cmd>
10587 * mbxStatus <status>
10588 */
James Smart09372822008-01-11 01:52:54 -050010589 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010590 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010591 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010592 "Interrupt mbxCommand x%x "
10593 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010594 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010595 pmbox->mbxCommand,
10596 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010597 /* clear mailbox attention bit */
10598 work_ha_copy &= ~HA_MBATT;
10599 } else {
James Smart97eab632008-04-07 10:16:05 -040010600 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010601 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010602 phba->last_completion_time = jiffies;
10603 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010604 if (pmb->mbox_cmpl) {
10605 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10606 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010607 if (pmb->out_ext_byte_len &&
10608 pmb->context2)
10609 lpfc_sli_pcimem_bcopy(
10610 phba->mbox_ext,
10611 pmb->context2,
10612 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010613 }
James Smart09372822008-01-11 01:52:54 -050010614 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10615 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10616
10617 lpfc_debugfs_disc_trc(vport,
10618 LPFC_DISC_TRC_MBOX_VPORT,
10619 "MBOX dflt rpi: : "
10620 "status:x%x rpi:x%x",
10621 (uint32_t)pmbox->mbxStatus,
10622 pmbox->un.varWords[0], 0);
10623
10624 if (!pmbox->mbxStatus) {
10625 mp = (struct lpfc_dmabuf *)
10626 (pmb->context1);
10627 ndlp = (struct lpfc_nodelist *)
10628 pmb->context2;
10629
10630 /* Reg_LOGIN of dflt RPI was
10631 * successful. new lets get
10632 * rid of the RPI using the
10633 * same mbox buffer.
10634 */
10635 lpfc_unreg_login(phba,
10636 vport->vpi,
10637 pmbox->un.varWords[0],
10638 pmb);
10639 pmb->mbox_cmpl =
10640 lpfc_mbx_cmpl_dflt_rpi;
10641 pmb->context1 = mp;
10642 pmb->context2 = ndlp;
10643 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010644 rc = lpfc_sli_issue_mbox(phba,
10645 pmb,
10646 MBX_NOWAIT);
10647 if (rc != MBX_BUSY)
10648 lpfc_printf_log(phba,
10649 KERN_ERR,
10650 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010651 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010652 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010653 if (rc != MBX_NOT_FINISHED)
10654 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010655 }
10656 }
James Smart5b75da22008-12-04 22:39:35 -050010657 spin_lock_irqsave(
10658 &phba->pport->work_port_lock,
10659 iflag);
James Smart09372822008-01-11 01:52:54 -050010660 phba->pport->work_port_events &=
10661 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010662 spin_unlock_irqrestore(
10663 &phba->pport->work_port_lock,
10664 iflag);
James Smart09372822008-01-11 01:52:54 -050010665 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010666 }
James Smart97eab632008-04-07 10:16:05 -040010667 } else
James Smart5b75da22008-12-04 22:39:35 -050010668 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010669
James Smart92d7f7b2007-06-17 19:56:38 -050010670 if ((work_ha_copy & HA_MBATT) &&
10671 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010672send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010673 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010674 do {
10675 rc = lpfc_sli_issue_mbox(phba, NULL,
10676 MBX_NOWAIT);
10677 } while (rc == MBX_NOT_FINISHED);
10678 if (rc != MBX_SUCCESS)
10679 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10680 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010681 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010682 }
10683
James Smart5b75da22008-12-04 22:39:35 -050010684 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010685 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010686 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010687 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010688 }
James Smart93996272008-08-24 21:50:30 -040010689 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010690unplug_error:
10691 spin_unlock_irqrestore(&phba->hbalock, iflag);
10692 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010693
James Smart3772a992009-05-22 14:50:54 -040010694} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010695
10696/**
James Smart3772a992009-05-22 14:50:54 -040010697 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010698 * @irq: Interrupt number.
10699 * @dev_id: The device context pointer.
10700 *
10701 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010702 * service routine when device with SLI-3 interface spec is enabled with
10703 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10704 * ring event in the HBA. However, when the device is enabled with either
10705 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10706 * device-level interrupt handler. When the PCI slot is in error recovery
10707 * or the HBA is undergoing initialization, the interrupt handler will not
10708 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10709 * the intrrupt context. This function is called without any lock held.
10710 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010711 *
10712 * This function returns IRQ_HANDLED when interrupt is handled else it
10713 * returns IRQ_NONE.
10714 **/
10715irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010716lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010717{
10718 struct lpfc_hba *phba;
10719 uint32_t ha_copy;
10720 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010721 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010722
10723 /* Get the driver's phba structure from the dev_id and
10724 * assume the HBA is not interrupting.
10725 */
10726 phba = (struct lpfc_hba *) dev_id;
10727
10728 if (unlikely(!phba))
10729 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010730
10731 /*
James Smart93996272008-08-24 21:50:30 -040010732 * Stuff needs to be attented to when this function is invoked as an
10733 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010734 */
James Smart93996272008-08-24 21:50:30 -040010735 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010736 /* Check device state for handling interrupt */
10737 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010738 return IRQ_NONE;
10739 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010740 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10741 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010742 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010743 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010744 /*
10745 * If there is deferred error attention, do not check for
10746 * any interrupt.
10747 */
10748 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010749 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010750 return IRQ_NONE;
10751 }
James Smart93996272008-08-24 21:50:30 -040010752 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10753 phba->HAregaddr);
10754 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010755 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010756 } else
10757 ha_copy = phba->ha_copy;
10758
10759 /*
10760 * Process all events on FCP ring. Take the optimized path for FCP IO.
10761 */
10762 ha_copy &= ~(phba->work_ha_mask);
10763
10764 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010765 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010766 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010767 lpfc_sli_handle_fast_ring_event(phba,
10768 &phba->sli.ring[LPFC_FCP_RING],
10769 status);
James Smarta4bc3372006-12-02 13:34:16 -050010770
10771 if (phba->cfg_multi_ring_support == 2) {
10772 /*
James Smart93996272008-08-24 21:50:30 -040010773 * Process all events on extra ring. Take the optimized path
10774 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010775 */
James Smart93996272008-08-24 21:50:30 -040010776 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010777 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010778 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010779 lpfc_sli_handle_fast_ring_event(phba,
10780 &phba->sli.ring[LPFC_EXTRA_RING],
10781 status);
10782 }
10783 }
dea31012005-04-17 16:05:31 -050010784 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010785} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010786
James Smart93996272008-08-24 21:50:30 -040010787/**
James Smart3772a992009-05-22 14:50:54 -040010788 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010789 * @irq: Interrupt number.
10790 * @dev_id: The device context pointer.
10791 *
James Smart3772a992009-05-22 14:50:54 -040010792 * This function is the HBA device-level interrupt handler to device with
10793 * SLI-3 interface spec, called from the PCI layer when either MSI or
10794 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10795 * requires driver attention. This function invokes the slow-path interrupt
10796 * attention handling function and fast-path interrupt attention handling
10797 * function in turn to process the relevant HBA attention events. This
10798 * function is called without any lock held. It gets the hbalock to access
10799 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010800 *
10801 * This function returns IRQ_HANDLED when interrupt is handled, else it
10802 * returns IRQ_NONE.
10803 **/
10804irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010805lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010806{
10807 struct lpfc_hba *phba;
10808 irqreturn_t sp_irq_rc, fp_irq_rc;
10809 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010810 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010811
10812 /*
10813 * Get the driver's phba structure from the dev_id and
10814 * assume the HBA is not interrupting.
10815 */
10816 phba = (struct lpfc_hba *) dev_id;
10817
10818 if (unlikely(!phba))
10819 return IRQ_NONE;
10820
James Smart3772a992009-05-22 14:50:54 -040010821 /* Check device state for handling interrupt */
10822 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010823 return IRQ_NONE;
10824
10825 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010826 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10827 spin_unlock(&phba->hbalock);
10828 return IRQ_HANDLED;
10829 }
10830
James Smart93996272008-08-24 21:50:30 -040010831 if (unlikely(!phba->ha_copy)) {
10832 spin_unlock(&phba->hbalock);
10833 return IRQ_NONE;
10834 } else if (phba->ha_copy & HA_ERATT) {
10835 if (phba->hba_flag & HBA_ERATT_HANDLED)
10836 /* ERATT polling has handled ERATT */
10837 phba->ha_copy &= ~HA_ERATT;
10838 else
10839 /* Indicate interrupt handler handles ERATT */
10840 phba->hba_flag |= HBA_ERATT_HANDLED;
10841 }
10842
James Smarta257bf92009-04-06 18:48:10 -040010843 /*
10844 * If there is deferred error attention, do not check for any interrupt.
10845 */
10846 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010847 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010848 return IRQ_NONE;
10849 }
10850
James Smart93996272008-08-24 21:50:30 -040010851 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010852 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10853 spin_unlock(&phba->hbalock);
10854 return IRQ_HANDLED;
10855 }
James Smarta747c9c2009-11-18 15:41:10 -050010856 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10857 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10858 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010859 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010860 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010861 readl(phba->HAregaddr); /* flush */
10862 spin_unlock(&phba->hbalock);
10863
10864 /*
10865 * Invokes slow-path host attention interrupt handling as appropriate.
10866 */
10867
10868 /* status of events with mailbox and link attention */
10869 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10870
10871 /* status of events with ELS ring */
10872 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10873 status2 >>= (4*LPFC_ELS_RING);
10874
10875 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010876 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010877 else
10878 sp_irq_rc = IRQ_NONE;
10879
10880 /*
10881 * Invoke fast-path host attention interrupt handling as appropriate.
10882 */
10883
10884 /* status of events with FCP ring */
10885 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10886 status1 >>= (4*LPFC_FCP_RING);
10887
10888 /* status of events with extra ring */
10889 if (phba->cfg_multi_ring_support == 2) {
10890 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10891 status2 >>= (4*LPFC_EXTRA_RING);
10892 } else
10893 status2 = 0;
10894
10895 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010896 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010897 else
10898 fp_irq_rc = IRQ_NONE;
10899
10900 /* Return device-level interrupt handling status */
10901 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010902} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010903
10904/**
10905 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10906 * @phba: pointer to lpfc hba data structure.
10907 *
10908 * This routine is invoked by the worker thread to process all the pending
10909 * SLI4 FCP abort XRI events.
10910 **/
10911void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10912{
10913 struct lpfc_cq_event *cq_event;
10914
10915 /* First, declare the fcp xri abort event has been handled */
10916 spin_lock_irq(&phba->hbalock);
10917 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10918 spin_unlock_irq(&phba->hbalock);
10919 /* Now, handle all the fcp xri abort events */
10920 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10921 /* Get the first event from the head of the event queue */
10922 spin_lock_irq(&phba->hbalock);
10923 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10924 cq_event, struct lpfc_cq_event, list);
10925 spin_unlock_irq(&phba->hbalock);
10926 /* Notify aborted XRI for FCP work queue */
10927 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10928 /* Free the event processed back to the free pool */
10929 lpfc_sli4_cq_event_release(phba, cq_event);
10930 }
10931}
10932
10933/**
10934 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10935 * @phba: pointer to lpfc hba data structure.
10936 *
10937 * This routine is invoked by the worker thread to process all the pending
10938 * SLI4 els abort xri events.
10939 **/
10940void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10941{
10942 struct lpfc_cq_event *cq_event;
10943
10944 /* First, declare the els xri abort event has been handled */
10945 spin_lock_irq(&phba->hbalock);
10946 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10947 spin_unlock_irq(&phba->hbalock);
10948 /* Now, handle all the els xri abort events */
10949 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10950 /* Get the first event from the head of the event queue */
10951 spin_lock_irq(&phba->hbalock);
10952 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10953 cq_event, struct lpfc_cq_event, list);
10954 spin_unlock_irq(&phba->hbalock);
10955 /* Notify aborted XRI for ELS work queue */
10956 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10957 /* Free the event processed back to the free pool */
10958 lpfc_sli4_cq_event_release(phba, cq_event);
10959 }
10960}
10961
James Smart341af102010-01-26 23:07:37 -050010962/**
10963 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10964 * @phba: pointer to lpfc hba data structure
10965 * @pIocbIn: pointer to the rspiocbq
10966 * @pIocbOut: pointer to the cmdiocbq
10967 * @wcqe: pointer to the complete wcqe
10968 *
10969 * This routine transfers the fields of a command iocbq to a response iocbq
10970 * by copying all the IOCB fields from command iocbq and transferring the
10971 * completion status information from the complete wcqe.
10972 **/
James Smart4f774512009-05-22 14:52:35 -040010973static void
James Smart341af102010-01-26 23:07:37 -050010974lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10975 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010976 struct lpfc_iocbq *pIocbOut,
10977 struct lpfc_wcqe_complete *wcqe)
10978{
James Smart341af102010-01-26 23:07:37 -050010979 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010980 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010981 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10982
10983 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10984 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010985 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010986 status = bf_get(lpfc_wcqe_c_status, wcqe);
10987 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010988 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10989 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10990 pIocbIn->iocb.un.fcpi.fcpi_parm =
10991 pIocbOut->iocb.un.fcpi.fcpi_parm -
10992 wcqe->total_data_placed;
10993 else
10994 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010995 else {
James Smart4f774512009-05-22 14:52:35 -040010996 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010997 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10998 }
James Smart341af102010-01-26 23:07:37 -050010999
James Smartacd68592012-01-18 16:25:09 -050011000 /* Convert BG errors for completion status */
11001 if (status == CQE_STATUS_DI_ERROR) {
11002 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11003
11004 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11005 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11006 else
11007 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11008
11009 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11010 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11011 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11012 BGS_GUARD_ERR_MASK;
11013 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11014 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11015 BGS_APPTAG_ERR_MASK;
11016 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11017 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11018 BGS_REFTAG_ERR_MASK;
11019
11020 /* Check to see if there was any good data before the error */
11021 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11022 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11023 BGS_HI_WATER_MARK_PRESENT_MASK;
11024 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11025 wcqe->total_data_placed;
11026 }
11027
11028 /*
11029 * Set ALL the error bits to indicate we don't know what
11030 * type of error it is.
11031 */
11032 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11033 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11034 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11035 BGS_GUARD_ERR_MASK);
11036 }
11037
James Smart341af102010-01-26 23:07:37 -050011038 /* Pick up HBA exchange busy condition */
11039 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11040 spin_lock_irqsave(&phba->hbalock, iflags);
11041 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11042 spin_unlock_irqrestore(&phba->hbalock, iflags);
11043 }
James Smart4f774512009-05-22 14:52:35 -040011044}
11045
11046/**
James Smart45ed1192009-10-02 15:17:02 -040011047 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11048 * @phba: Pointer to HBA context object.
11049 * @wcqe: Pointer to work-queue completion queue entry.
11050 *
11051 * This routine handles an ELS work-queue completion event and construct
11052 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11053 * discovery engine to handle.
11054 *
11055 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11056 **/
11057static struct lpfc_iocbq *
11058lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11059 struct lpfc_iocbq *irspiocbq)
11060{
11061 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11062 struct lpfc_iocbq *cmdiocbq;
11063 struct lpfc_wcqe_complete *wcqe;
11064 unsigned long iflags;
11065
11066 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011067 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011068 pring->stats.iocb_event++;
11069 /* Look up the ELS command IOCB and create pseudo response IOCB */
11070 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11071 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011072 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011073
11074 if (unlikely(!cmdiocbq)) {
11075 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11076 "0386 ELS complete with no corresponding "
11077 "cmdiocb: iotag (%d)\n",
11078 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11079 lpfc_sli_release_iocbq(phba, irspiocbq);
11080 return NULL;
11081 }
11082
11083 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011084 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011085
11086 return irspiocbq;
11087}
11088
11089/**
James Smart04c68492009-05-22 14:52:52 -040011090 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11091 * @phba: Pointer to HBA context object.
11092 * @cqe: Pointer to mailbox completion queue entry.
11093 *
11094 * This routine process a mailbox completion queue entry with asynchrous
11095 * event.
11096 *
11097 * Return: true if work posted to worker thread, otherwise false.
11098 **/
11099static bool
11100lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11101{
11102 struct lpfc_cq_event *cq_event;
11103 unsigned long iflags;
11104
11105 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11106 "0392 Async Event: word0:x%x, word1:x%x, "
11107 "word2:x%x, word3:x%x\n", mcqe->word0,
11108 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11109
11110 /* Allocate a new internal CQ_EVENT entry */
11111 cq_event = lpfc_sli4_cq_event_alloc(phba);
11112 if (!cq_event) {
11113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11114 "0394 Failed to allocate CQ_EVENT entry\n");
11115 return false;
11116 }
11117
11118 /* Move the CQE into an asynchronous event entry */
11119 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11120 spin_lock_irqsave(&phba->hbalock, iflags);
11121 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11122 /* Set the async event flag */
11123 phba->hba_flag |= ASYNC_EVENT;
11124 spin_unlock_irqrestore(&phba->hbalock, iflags);
11125
11126 return true;
11127}
11128
11129/**
11130 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11131 * @phba: Pointer to HBA context object.
11132 * @cqe: Pointer to mailbox completion queue entry.
11133 *
11134 * This routine process a mailbox completion queue entry with mailbox
11135 * completion event.
11136 *
11137 * Return: true if work posted to worker thread, otherwise false.
11138 **/
11139static bool
11140lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11141{
11142 uint32_t mcqe_status;
11143 MAILBOX_t *mbox, *pmbox;
11144 struct lpfc_mqe *mqe;
11145 struct lpfc_vport *vport;
11146 struct lpfc_nodelist *ndlp;
11147 struct lpfc_dmabuf *mp;
11148 unsigned long iflags;
11149 LPFC_MBOXQ_t *pmb;
11150 bool workposted = false;
11151 int rc;
11152
11153 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11154 if (!bf_get(lpfc_trailer_completed, mcqe))
11155 goto out_no_mqe_complete;
11156
11157 /* Get the reference to the active mbox command */
11158 spin_lock_irqsave(&phba->hbalock, iflags);
11159 pmb = phba->sli.mbox_active;
11160 if (unlikely(!pmb)) {
11161 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11162 "1832 No pending MBOX command to handle\n");
11163 spin_unlock_irqrestore(&phba->hbalock, iflags);
11164 goto out_no_mqe_complete;
11165 }
11166 spin_unlock_irqrestore(&phba->hbalock, iflags);
11167 mqe = &pmb->u.mqe;
11168 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11169 mbox = phba->mbox;
11170 vport = pmb->vport;
11171
11172 /* Reset heartbeat timer */
11173 phba->last_completion_time = jiffies;
11174 del_timer(&phba->sli.mbox_tmo);
11175
11176 /* Move mbox data to caller's mailbox region, do endian swapping */
11177 if (pmb->mbox_cmpl && mbox)
11178 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011179
James Smart73d91e52011-10-10 21:32:10 -040011180 /*
11181 * For mcqe errors, conditionally move a modified error code to
11182 * the mbox so that the error will not be missed.
11183 */
11184 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11185 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11186 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11187 bf_set(lpfc_mqe_status, mqe,
11188 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11189 }
James Smart04c68492009-05-22 14:52:52 -040011190 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11191 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11192 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11193 "MBOX dflt rpi: status:x%x rpi:x%x",
11194 mcqe_status,
11195 pmbox->un.varWords[0], 0);
11196 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11197 mp = (struct lpfc_dmabuf *)(pmb->context1);
11198 ndlp = (struct lpfc_nodelist *)pmb->context2;
11199 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11200 * RID of the PPI using the same mbox buffer.
11201 */
11202 lpfc_unreg_login(phba, vport->vpi,
11203 pmbox->un.varWords[0], pmb);
11204 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11205 pmb->context1 = mp;
11206 pmb->context2 = ndlp;
11207 pmb->vport = vport;
11208 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11209 if (rc != MBX_BUSY)
11210 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11211 LOG_SLI, "0385 rc should "
11212 "have been MBX_BUSY\n");
11213 if (rc != MBX_NOT_FINISHED)
11214 goto send_current_mbox;
11215 }
11216 }
11217 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11218 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11219 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11220
11221 /* There is mailbox completion work to do */
11222 spin_lock_irqsave(&phba->hbalock, iflags);
11223 __lpfc_mbox_cmpl_put(phba, pmb);
11224 phba->work_ha |= HA_MBATT;
11225 spin_unlock_irqrestore(&phba->hbalock, iflags);
11226 workposted = true;
11227
11228send_current_mbox:
11229 spin_lock_irqsave(&phba->hbalock, iflags);
11230 /* Release the mailbox command posting token */
11231 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11232 /* Setting active mailbox pointer need to be in sync to flag clear */
11233 phba->sli.mbox_active = NULL;
11234 spin_unlock_irqrestore(&phba->hbalock, iflags);
11235 /* Wake up worker thread to post the next pending mailbox command */
11236 lpfc_worker_wake_up(phba);
11237out_no_mqe_complete:
11238 if (bf_get(lpfc_trailer_consumed, mcqe))
11239 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11240 return workposted;
11241}
11242
11243/**
11244 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11245 * @phba: Pointer to HBA context object.
11246 * @cqe: Pointer to mailbox completion queue entry.
11247 *
11248 * This routine process a mailbox completion queue entry, it invokes the
11249 * proper mailbox complete handling or asynchrous event handling routine
11250 * according to the MCQE's async bit.
11251 *
11252 * Return: true if work posted to worker thread, otherwise false.
11253 **/
11254static bool
11255lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11256{
11257 struct lpfc_mcqe mcqe;
11258 bool workposted;
11259
11260 /* Copy the mailbox MCQE and convert endian order as needed */
11261 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11262
11263 /* Invoke the proper event handling routine */
11264 if (!bf_get(lpfc_trailer_async, &mcqe))
11265 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11266 else
11267 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11268 return workposted;
11269}
11270
11271/**
James Smart4f774512009-05-22 14:52:35 -040011272 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11273 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011274 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011275 * @wcqe: Pointer to work-queue completion queue entry.
11276 *
11277 * This routine handles an ELS work-queue completion event.
11278 *
11279 * Return: true if work posted to worker thread, otherwise false.
11280 **/
11281static bool
James Smart2a76a282012-08-03 12:35:54 -040011282lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011283 struct lpfc_wcqe_complete *wcqe)
11284{
James Smart4f774512009-05-22 14:52:35 -040011285 struct lpfc_iocbq *irspiocbq;
11286 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011287 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011288
James Smart45ed1192009-10-02 15:17:02 -040011289 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011290 irspiocbq = lpfc_sli_get_iocbq(phba);
11291 if (!irspiocbq) {
11292 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011293 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11294 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11295 pring->txq_cnt, phba->iocb_cnt,
11296 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11297 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011298 return false;
James Smart4f774512009-05-22 14:52:35 -040011299 }
James Smart4f774512009-05-22 14:52:35 -040011300
James Smart45ed1192009-10-02 15:17:02 -040011301 /* Save off the slow-path queue event for work thread to process */
11302 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011303 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011304 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011305 &phba->sli4_hba.sp_queue_event);
11306 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011307 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011308
James Smart45ed1192009-10-02 15:17:02 -040011309 return true;
James Smart4f774512009-05-22 14:52:35 -040011310}
11311
11312/**
11313 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11314 * @phba: Pointer to HBA context object.
11315 * @wcqe: Pointer to work-queue completion queue entry.
11316 *
11317 * This routine handles slow-path WQ entry comsumed event by invoking the
11318 * proper WQ release routine to the slow-path WQ.
11319 **/
11320static void
11321lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11322 struct lpfc_wcqe_release *wcqe)
11323{
James Smart2e90f4b2011-12-13 13:22:37 -050011324 /* sanity check on queue memory */
11325 if (unlikely(!phba->sli4_hba.els_wq))
11326 return;
James Smart4f774512009-05-22 14:52:35 -040011327 /* Check for the slow-path ELS work queue */
11328 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11329 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11330 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11331 else
11332 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11333 "2579 Slow-path wqe consume event carries "
11334 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11335 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11336 phba->sli4_hba.els_wq->queue_id);
11337}
11338
11339/**
11340 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11341 * @phba: Pointer to HBA context object.
11342 * @cq: Pointer to a WQ completion queue.
11343 * @wcqe: Pointer to work-queue completion queue entry.
11344 *
11345 * This routine handles an XRI abort event.
11346 *
11347 * Return: true if work posted to worker thread, otherwise false.
11348 **/
11349static bool
11350lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11351 struct lpfc_queue *cq,
11352 struct sli4_wcqe_xri_aborted *wcqe)
11353{
11354 bool workposted = false;
11355 struct lpfc_cq_event *cq_event;
11356 unsigned long iflags;
11357
11358 /* Allocate a new internal CQ_EVENT entry */
11359 cq_event = lpfc_sli4_cq_event_alloc(phba);
11360 if (!cq_event) {
11361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11362 "0602 Failed to allocate CQ_EVENT entry\n");
11363 return false;
11364 }
11365
11366 /* Move the CQE into the proper xri abort event list */
11367 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11368 switch (cq->subtype) {
11369 case LPFC_FCP:
11370 spin_lock_irqsave(&phba->hbalock, iflags);
11371 list_add_tail(&cq_event->list,
11372 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11373 /* Set the fcp xri abort event flag */
11374 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11375 spin_unlock_irqrestore(&phba->hbalock, iflags);
11376 workposted = true;
11377 break;
11378 case LPFC_ELS:
11379 spin_lock_irqsave(&phba->hbalock, iflags);
11380 list_add_tail(&cq_event->list,
11381 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11382 /* Set the els xri abort event flag */
11383 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11384 spin_unlock_irqrestore(&phba->hbalock, iflags);
11385 workposted = true;
11386 break;
11387 default:
11388 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11389 "0603 Invalid work queue CQE subtype (x%x)\n",
11390 cq->subtype);
11391 workposted = false;
11392 break;
11393 }
11394 return workposted;
11395}
11396
11397/**
James Smart4d9ab992009-10-02 15:16:39 -040011398 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011399 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011400 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011401 *
James Smart4d9ab992009-10-02 15:16:39 -040011402 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011403 *
11404 * Return: true if work posted to worker thread, otherwise false.
11405 **/
11406static bool
James Smart4d9ab992009-10-02 15:16:39 -040011407lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11408{
11409 bool workposted = false;
11410 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11411 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11412 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011413 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011414 unsigned long iflags;
11415
James Smart2e90f4b2011-12-13 13:22:37 -050011416 /* sanity check on queue memory */
11417 if (unlikely(!hrq) || unlikely(!drq))
11418 return workposted;
11419
James Smart7851fe22011-07-22 18:36:52 -040011420 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11421 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11422 else
11423 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11424 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011425 goto out;
11426
11427 status = bf_get(lpfc_rcqe_status, rcqe);
11428 switch (status) {
11429 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11431 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011432 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011433 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011434 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011435 spin_lock_irqsave(&phba->hbalock, iflags);
11436 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11437 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011438 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011439 spin_unlock_irqrestore(&phba->hbalock, iflags);
11440 goto out;
11441 }
James Smartb84daac2012-08-03 12:35:13 -040011442 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011443 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11444 /* save off the frame for the word thread to process */
11445 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011446 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011447 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011448 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011449 spin_unlock_irqrestore(&phba->hbalock, iflags);
11450 workposted = true;
11451 break;
11452 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11453 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011454 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011455 /* Post more buffers if possible */
11456 spin_lock_irqsave(&phba->hbalock, iflags);
11457 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11458 spin_unlock_irqrestore(&phba->hbalock, iflags);
11459 workposted = true;
11460 break;
11461 }
11462out:
11463 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011464}
11465
11466/**
11467 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11468 * @phba: Pointer to HBA context object.
11469 * @cq: Pointer to the completion queue.
11470 * @wcqe: Pointer to a completion queue entry.
11471 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011472 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011473 * entry.
11474 *
11475 * Return: true if work posted to worker thread, otherwise false.
11476 **/
11477static bool
11478lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011479 struct lpfc_cqe *cqe)
11480{
James Smart45ed1192009-10-02 15:17:02 -040011481 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011482 bool workposted = false;
11483
11484 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011485 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011486
11487 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011488 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011489 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011490 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011491 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011492 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011493 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011494 break;
11495 case CQE_CODE_RELEASE_WQE:
11496 /* Process the WQ release event */
11497 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011498 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011499 break;
11500 case CQE_CODE_XRI_ABORTED:
11501 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011502 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011503 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011504 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011505 break;
James Smart4d9ab992009-10-02 15:16:39 -040011506 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011507 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011508 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011509 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011510 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011511 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011512 break;
James Smart4f774512009-05-22 14:52:35 -040011513 default:
11514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11515 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011516 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011517 break;
11518 }
11519 return workposted;
11520}
11521
11522/**
James Smart4f774512009-05-22 14:52:35 -040011523 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11524 * @phba: Pointer to HBA context object.
11525 * @eqe: Pointer to fast-path event queue entry.
11526 *
11527 * This routine process a event queue entry from the slow-path event queue.
11528 * It will check the MajorCode and MinorCode to determine this is for a
11529 * completion event on a completion queue, if not, an error shall be logged
11530 * and just return. Otherwise, it will get to the corresponding completion
11531 * queue and process all the entries on that completion queue, rearm the
11532 * completion queue, and then return.
11533 *
11534 **/
11535static void
James Smart67d12732012-08-03 12:36:13 -040011536lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11537 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040011538{
James Smart67d12732012-08-03 12:36:13 -040011539 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040011540 struct lpfc_cqe *cqe;
11541 bool workposted = false;
11542 int ecount = 0;
11543 uint16_t cqid;
11544
James Smart4f774512009-05-22 14:52:35 -040011545 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011546 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011547
James Smart4f774512009-05-22 14:52:35 -040011548 list_for_each_entry(childq, &speq->child_list, list) {
11549 if (childq->queue_id == cqid) {
11550 cq = childq;
11551 break;
11552 }
11553 }
11554 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011555 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11556 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11557 "0365 Slow-path CQ identifier "
11558 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011559 return;
11560 }
11561
11562 /* Process all the entries to the CQ */
11563 switch (cq->type) {
11564 case LPFC_MCQ:
11565 while ((cqe = lpfc_sli4_cq_get(cq))) {
11566 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011567 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011568 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011569 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011570 }
11571 break;
11572 case LPFC_WCQ:
11573 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011574 if (cq->subtype == LPFC_FCP)
11575 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11576 cqe);
11577 else
11578 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11579 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011580 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011581 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11582 }
James Smartb84daac2012-08-03 12:35:13 -040011583
11584 /* Track the max number of CQEs processed in 1 EQ */
11585 if (ecount > cq->CQ_max_cqe)
11586 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011587 break;
11588 default:
11589 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11590 "0370 Invalid completion queue type (%d)\n",
11591 cq->type);
11592 return;
11593 }
11594
11595 /* Catch the no cq entry condition, log an error */
11596 if (unlikely(ecount == 0))
11597 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11598 "0371 No entry from the CQ: identifier "
11599 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11600
11601 /* In any case, flash and re-arm the RCQ */
11602 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11603
11604 /* wake up worker thread if there are works to be done */
11605 if (workposted)
11606 lpfc_worker_wake_up(phba);
11607}
11608
11609/**
11610 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011611 * @phba: Pointer to HBA context object.
11612 * @cq: Pointer to associated CQ
11613 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011614 *
11615 * This routine process a fast-path work queue completion entry from fast-path
11616 * event queue for FCP command response completion.
11617 **/
11618static void
James Smart2a76a282012-08-03 12:35:54 -040011619lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011620 struct lpfc_wcqe_complete *wcqe)
11621{
James Smart2a76a282012-08-03 12:35:54 -040011622 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011623 struct lpfc_iocbq *cmdiocbq;
11624 struct lpfc_iocbq irspiocbq;
11625 unsigned long iflags;
11626
James Smart4f774512009-05-22 14:52:35 -040011627 /* Check for response status */
11628 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11629 /* If resource errors reported from HBA, reduce queue
11630 * depth of the SCSI device.
11631 */
11632 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11633 IOSTAT_LOCAL_REJECT) &&
11634 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11635 phba->lpfc_rampdown_queue_depth(phba);
11636 }
11637 /* Log the error status */
11638 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11639 "0373 FCP complete error: status=x%x, "
11640 "hw_status=x%x, total_data_specified=%d, "
11641 "parameter=x%x, word3=x%x\n",
11642 bf_get(lpfc_wcqe_c_status, wcqe),
11643 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11644 wcqe->total_data_placed, wcqe->parameter,
11645 wcqe->word3);
11646 }
11647
11648 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011649 spin_lock_irqsave(&pring->ring_lock, iflags);
11650 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011651 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11652 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011653 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011654 if (unlikely(!cmdiocbq)) {
11655 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11656 "0374 FCP complete with no corresponding "
11657 "cmdiocb: iotag (%d)\n",
11658 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11659 return;
11660 }
11661 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11662 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11663 "0375 FCP cmdiocb not callback function "
11664 "iotag: (%d)\n",
11665 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11666 return;
11667 }
11668
11669 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011670 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011671
James Smart0f65ff62010-02-26 14:14:23 -050011672 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11673 spin_lock_irqsave(&phba->hbalock, iflags);
11674 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11675 spin_unlock_irqrestore(&phba->hbalock, iflags);
11676 }
11677
James Smart4f774512009-05-22 14:52:35 -040011678 /* Pass the cmd_iocb and the rsp state to the upper layer */
11679 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11680}
11681
11682/**
11683 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11684 * @phba: Pointer to HBA context object.
11685 * @cq: Pointer to completion queue.
11686 * @wcqe: Pointer to work-queue completion queue entry.
11687 *
11688 * This routine handles an fast-path WQ entry comsumed event by invoking the
11689 * proper WQ release routine to the slow-path WQ.
11690 **/
11691static void
11692lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11693 struct lpfc_wcqe_release *wcqe)
11694{
11695 struct lpfc_queue *childwq;
11696 bool wqid_matched = false;
11697 uint16_t fcp_wqid;
11698
11699 /* Check for fast-path FCP work queue release */
11700 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11701 list_for_each_entry(childwq, &cq->child_list, list) {
11702 if (childwq->queue_id == fcp_wqid) {
11703 lpfc_sli4_wq_release(childwq,
11704 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11705 wqid_matched = true;
11706 break;
11707 }
11708 }
11709 /* Report warning log message if no match found */
11710 if (wqid_matched != true)
11711 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11712 "2580 Fast-path wqe consume event carries "
11713 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11714}
11715
11716/**
11717 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11718 * @cq: Pointer to the completion queue.
11719 * @eqe: Pointer to fast-path completion queue entry.
11720 *
11721 * This routine process a fast-path work queue completion entry from fast-path
11722 * event queue for FCP command response completion.
11723 **/
11724static int
11725lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11726 struct lpfc_cqe *cqe)
11727{
11728 struct lpfc_wcqe_release wcqe;
11729 bool workposted = false;
11730
11731 /* Copy the work queue CQE and convert endian order if needed */
11732 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11733
11734 /* Check and process for different type of WCQE and dispatch */
11735 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11736 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011737 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011738 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011739 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011740 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040011741 (struct lpfc_wcqe_complete *)&wcqe);
11742 break;
11743 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011744 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011745 /* Process the WQ release event */
11746 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11747 (struct lpfc_wcqe_release *)&wcqe);
11748 break;
11749 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011750 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011751 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011752 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011753 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11754 (struct sli4_wcqe_xri_aborted *)&wcqe);
11755 break;
11756 default:
11757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11758 "0144 Not a valid WCQE code: x%x\n",
11759 bf_get(lpfc_wcqe_c_code, &wcqe));
11760 break;
11761 }
11762 return workposted;
11763}
11764
11765/**
James Smart67d12732012-08-03 12:36:13 -040011766 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040011767 * @phba: Pointer to HBA context object.
11768 * @eqe: Pointer to fast-path event queue entry.
11769 *
11770 * This routine process a event queue entry from the fast-path event queue.
11771 * It will check the MajorCode and MinorCode to determine this is for a
11772 * completion event on a completion queue, if not, an error shall be logged
11773 * and just return. Otherwise, it will get to the corresponding completion
11774 * queue and process all the entries on the completion queue, rearm the
11775 * completion queue, and then return.
11776 **/
11777static void
James Smart67d12732012-08-03 12:36:13 -040011778lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11779 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040011780{
11781 struct lpfc_queue *cq;
11782 struct lpfc_cqe *cqe;
11783 bool workposted = false;
11784 uint16_t cqid;
11785 int ecount = 0;
11786
James Smartcb5172e2010-03-15 11:25:07 -040011787 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011788 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040011789 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040011790 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011791 bf_get_le32(lpfc_eqe_major_code, eqe),
11792 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011793 return;
11794 }
11795
James Smart67d12732012-08-03 12:36:13 -040011796 /* Get the reference to the corresponding CQ */
11797 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11798
11799 /* Check if this is a Slow path event */
11800 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
11801 lpfc_sli4_sp_handle_eqe(phba, eqe,
11802 phba->sli4_hba.hba_eq[qidx]);
11803 return;
11804 }
11805
James Smart2e90f4b2011-12-13 13:22:37 -050011806 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11807 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11808 "3146 Fast-path completion queues "
11809 "does not exist\n");
11810 return;
11811 }
James Smart67d12732012-08-03 12:36:13 -040011812 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040011813 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011814 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11815 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11816 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040011817 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040011818 return;
11819 }
11820
James Smart4f774512009-05-22 14:52:35 -040011821 if (unlikely(cqid != cq->queue_id)) {
11822 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11823 "0368 Miss-matched fast-path completion "
11824 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11825 cqid, cq->queue_id);
11826 return;
11827 }
11828
11829 /* Process all the entries to the CQ */
11830 while ((cqe = lpfc_sli4_cq_get(cq))) {
11831 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011832 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011833 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11834 }
11835
James Smartb84daac2012-08-03 12:35:13 -040011836 /* Track the max number of CQEs processed in 1 EQ */
11837 if (ecount > cq->CQ_max_cqe)
11838 cq->CQ_max_cqe = ecount;
11839
James Smart4f774512009-05-22 14:52:35 -040011840 /* Catch the no cq entry condition */
11841 if (unlikely(ecount == 0))
11842 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11843 "0369 No entry from fast-path completion "
11844 "queue fcpcqid=%d\n", cq->queue_id);
11845
11846 /* In any case, flash and re-arm the CQ */
11847 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11848
11849 /* wake up worker thread if there are works to be done */
11850 if (workposted)
11851 lpfc_worker_wake_up(phba);
11852}
11853
11854static void
11855lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11856{
11857 struct lpfc_eqe *eqe;
11858
11859 /* walk all the EQ entries and drop on the floor */
11860 while ((eqe = lpfc_sli4_eq_get(eq)))
11861 ;
11862
11863 /* Clear and re-arm the EQ */
11864 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11865}
11866
11867/**
James Smart67d12732012-08-03 12:36:13 -040011868 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040011869 * @irq: Interrupt number.
11870 * @dev_id: The device context pointer.
11871 *
11872 * This function is directly called from the PCI layer as an interrupt
11873 * service routine when device with SLI-4 interface spec is enabled with
11874 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11875 * ring event in the HBA. However, when the device is enabled with either
11876 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11877 * device-level interrupt handler. When the PCI slot is in error recovery
11878 * or the HBA is undergoing initialization, the interrupt handler will not
11879 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11880 * the intrrupt context. This function is called without any lock held.
11881 * It gets the hbalock to access and update SLI data structures. Note that,
11882 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11883 * equal to that of FCP CQ index.
11884 *
James Smart67d12732012-08-03 12:36:13 -040011885 * The link attention and ELS ring attention events are handled
11886 * by the worker thread. The interrupt handler signals the worker thread
11887 * and returns for these events. This function is called without any lock
11888 * held. It gets the hbalock to access and update SLI data structures.
11889 *
James Smart4f774512009-05-22 14:52:35 -040011890 * This function returns IRQ_HANDLED when interrupt is handled else it
11891 * returns IRQ_NONE.
11892 **/
11893irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040011894lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040011895{
11896 struct lpfc_hba *phba;
11897 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11898 struct lpfc_queue *fpeq;
11899 struct lpfc_eqe *eqe;
11900 unsigned long iflag;
11901 int ecount = 0;
11902 uint32_t fcp_eqidx;
11903
11904 /* Get the driver's phba structure from the dev_id */
11905 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11906 phba = fcp_eq_hdl->phba;
11907 fcp_eqidx = fcp_eq_hdl->idx;
11908
11909 if (unlikely(!phba))
11910 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040011911 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040011912 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011913
11914 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040011915 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011916 if (unlikely(!fpeq))
11917 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011918
James Smartba20c852012-08-03 12:36:52 -040011919 if (lpfc_fcp_look_ahead) {
11920 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
11921 lpfc_sli4_eq_clr_intr(fpeq);
11922 else {
11923 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11924 return IRQ_NONE;
11925 }
11926 }
11927
James Smart4f774512009-05-22 14:52:35 -040011928 /* Check device state for handling interrupt */
11929 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011930 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011931 /* Check again for link_state with lock held */
11932 spin_lock_irqsave(&phba->hbalock, iflag);
11933 if (phba->link_state < LPFC_LINK_DOWN)
11934 /* Flush, clear interrupt, and rearm the EQ */
11935 lpfc_sli4_eq_flush(phba, fpeq);
11936 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040011937 if (lpfc_fcp_look_ahead)
11938 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040011939 return IRQ_NONE;
11940 }
11941
11942 /*
11943 * Process all the event on FCP fast-path EQ
11944 */
11945 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smart67d12732012-08-03 12:36:13 -040011946 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011947 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011948 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011949 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011950 }
11951
James Smartb84daac2012-08-03 12:35:13 -040011952 /* Track the max number of EQEs processed in 1 intr */
11953 if (ecount > fpeq->EQ_max_eqe)
11954 fpeq->EQ_max_eqe = ecount;
11955
James Smart4f774512009-05-22 14:52:35 -040011956 /* Always clear and re-arm the fast-path EQ */
11957 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11958
11959 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011960 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040011961
11962 if (lpfc_fcp_look_ahead) {
11963 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11964 return IRQ_NONE;
11965 }
11966
James Smart4f774512009-05-22 14:52:35 -040011967 if (phba->intr_type == MSIX)
11968 /* MSI-X treated interrupt served as no EQ share INT */
11969 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11970 "0358 MSI-X interrupt with no EQE\n");
11971 else
11972 /* Non MSI-X treated on interrupt as EQ share INT */
11973 return IRQ_NONE;
11974 }
11975
James Smartba20c852012-08-03 12:36:52 -040011976 if (lpfc_fcp_look_ahead)
11977 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040011978 return IRQ_HANDLED;
11979} /* lpfc_sli4_fp_intr_handler */
11980
11981/**
11982 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11983 * @irq: Interrupt number.
11984 * @dev_id: The device context pointer.
11985 *
11986 * This function is the device-level interrupt handler to device with SLI-4
11987 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11988 * interrupt mode is enabled and there is an event in the HBA which requires
11989 * driver attention. This function invokes the slow-path interrupt attention
11990 * handling function and fast-path interrupt attention handling function in
11991 * turn to process the relevant HBA attention events. This function is called
11992 * without any lock held. It gets the hbalock to access and update SLI data
11993 * structures.
11994 *
11995 * This function returns IRQ_HANDLED when interrupt is handled, else it
11996 * returns IRQ_NONE.
11997 **/
11998irqreturn_t
11999lpfc_sli4_intr_handler(int irq, void *dev_id)
12000{
12001 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012002 irqreturn_t hba_irq_rc;
12003 bool hba_handled = false;
James Smart4f774512009-05-22 14:52:35 -040012004 uint32_t fcp_eqidx;
12005
12006 /* Get the driver's phba structure from the dev_id */
12007 phba = (struct lpfc_hba *)dev_id;
12008
12009 if (unlikely(!phba))
12010 return IRQ_NONE;
12011
12012 /*
James Smart4f774512009-05-22 14:52:35 -040012013 * Invoke fast-path host attention interrupt handling as appropriate.
12014 */
James Smart67d12732012-08-03 12:36:13 -040012015 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12016 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012017 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012018 if (hba_irq_rc == IRQ_HANDLED)
12019 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012020 }
12021
James Smart67d12732012-08-03 12:36:13 -040012022 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012023} /* lpfc_sli4_intr_handler */
12024
12025/**
12026 * lpfc_sli4_queue_free - free a queue structure and associated memory
12027 * @queue: The queue structure to free.
12028 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012029 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012030 * the host resident queue. This function must be called after destroying the
12031 * queue on the HBA.
12032 **/
12033void
12034lpfc_sli4_queue_free(struct lpfc_queue *queue)
12035{
12036 struct lpfc_dmabuf *dmabuf;
12037
12038 if (!queue)
12039 return;
12040
12041 while (!list_empty(&queue->page_list)) {
12042 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12043 list);
James Smart49198b32010-04-06 15:04:33 -040012044 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012045 dmabuf->virt, dmabuf->phys);
12046 kfree(dmabuf);
12047 }
12048 kfree(queue);
12049 return;
12050}
12051
12052/**
12053 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12054 * @phba: The HBA that this queue is being created on.
12055 * @entry_size: The size of each queue entry for this queue.
12056 * @entry count: The number of entries that this queue will handle.
12057 *
12058 * This function allocates a queue structure and the DMAable memory used for
12059 * the host resident queue. This function must be called before creating the
12060 * queue on the HBA.
12061 **/
12062struct lpfc_queue *
12063lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12064 uint32_t entry_count)
12065{
12066 struct lpfc_queue *queue;
12067 struct lpfc_dmabuf *dmabuf;
12068 int x, total_qe_count;
12069 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012070 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012071
James Smartcb5172e2010-03-15 11:25:07 -040012072 if (!phba->sli4_hba.pc_sli4_params.supported)
12073 hw_page_size = SLI4_PAGE_SIZE;
12074
James Smart4f774512009-05-22 14:52:35 -040012075 queue = kzalloc(sizeof(struct lpfc_queue) +
12076 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12077 if (!queue)
12078 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012079 queue->page_count = (ALIGN(entry_size * entry_count,
12080 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012081 INIT_LIST_HEAD(&queue->list);
12082 INIT_LIST_HEAD(&queue->page_list);
12083 INIT_LIST_HEAD(&queue->child_list);
12084 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12085 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12086 if (!dmabuf)
12087 goto out_fail;
12088 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012089 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012090 GFP_KERNEL);
12091 if (!dmabuf->virt) {
12092 kfree(dmabuf);
12093 goto out_fail;
12094 }
James Smartcb5172e2010-03-15 11:25:07 -040012095 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012096 dmabuf->buffer_tag = x;
12097 list_add_tail(&dmabuf->list, &queue->page_list);
12098 /* initialize queue's entry array */
12099 dma_pointer = dmabuf->virt;
12100 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012101 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012102 total_qe_count++, dma_pointer += entry_size) {
12103 queue->qe[total_qe_count].address = dma_pointer;
12104 }
12105 }
12106 queue->entry_size = entry_size;
12107 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012108
12109 /*
12110 * entry_repost is calculated based on the number of entries in the
12111 * queue. This works out except for RQs. If buffers are NOT initially
12112 * posted for every RQE, entry_repost should be adjusted accordingly.
12113 */
12114 queue->entry_repost = (entry_count >> 3);
12115 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12116 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012117 queue->phba = phba;
12118
12119 return queue;
12120out_fail:
12121 lpfc_sli4_queue_free(queue);
12122 return NULL;
12123}
12124
12125/**
James Smart173edbb2012-06-12 13:54:50 -040012126 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12127 * @phba: HBA structure that indicates port to create a queue on.
12128 * @startq: The starting FCP EQ to modify
12129 *
12130 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12131 *
12132 * The @phba struct is used to send mailbox command to HBA. The @startq
12133 * is used to get the starting FCP EQ to change.
12134 * This function is asynchronous and will wait for the mailbox
12135 * command to finish before continuing.
12136 *
12137 * On success this function will return a zero. If unable to allocate enough
12138 * memory this function will return -ENOMEM. If the queue create mailbox command
12139 * fails this function will return -ENXIO.
12140 **/
12141uint32_t
12142lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12143{
12144 struct lpfc_mbx_modify_eq_delay *eq_delay;
12145 LPFC_MBOXQ_t *mbox;
12146 struct lpfc_queue *eq;
12147 int cnt, rc, length, status = 0;
12148 uint32_t shdr_status, shdr_add_status;
12149 int fcp_eqidx;
12150 union lpfc_sli4_cfg_shdr *shdr;
12151 uint16_t dmult;
12152
James Smart67d12732012-08-03 12:36:13 -040012153 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040012154 return 0;
12155
12156 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12157 if (!mbox)
12158 return -ENOMEM;
12159 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12160 sizeof(struct lpfc_sli4_cfg_mhdr));
12161 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12162 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12163 length, LPFC_SLI4_MBX_EMBED);
12164 eq_delay = &mbox->u.mqe.un.eq_delay;
12165
12166 /* Calculate delay multiper from maximum interrupt per second */
James Smartbf8dae82012-08-03 12:36:24 -040012167 dmult = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12168 dmult = LPFC_DMULT_CONST/dmult - 1;
James Smart173edbb2012-06-12 13:54:50 -040012169
12170 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040012171 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040012172 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040012173 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040012174 if (!eq)
12175 continue;
12176 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12177 eq_delay->u.request.eq[cnt].phase = 0;
12178 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12179 cnt++;
12180 if (cnt >= LPFC_MAX_EQ_DELAY)
12181 break;
12182 }
12183 eq_delay->u.request.num_eq = cnt;
12184
12185 mbox->vport = phba->pport;
12186 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12187 mbox->context1 = NULL;
12188 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12189 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12190 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12191 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12192 if (shdr_status || shdr_add_status || rc) {
12193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12194 "2512 MODIFY_EQ_DELAY mailbox failed with "
12195 "status x%x add_status x%x, mbx status x%x\n",
12196 shdr_status, shdr_add_status, rc);
12197 status = -ENXIO;
12198 }
12199 mempool_free(mbox, phba->mbox_mem_pool);
12200 return status;
12201}
12202
12203/**
James Smart4f774512009-05-22 14:52:35 -040012204 * lpfc_eq_create - Create an Event Queue on the HBA
12205 * @phba: HBA structure that indicates port to create a queue on.
12206 * @eq: The queue structure to use to create the event queue.
12207 * @imax: The maximum interrupt per second limit.
12208 *
12209 * This function creates an event queue, as detailed in @eq, on a port,
12210 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12211 *
12212 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12213 * is used to get the entry count and entry size that are necessary to
12214 * determine the number of pages to allocate and use for this queue. This
12215 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12216 * event queue. This function is asynchronous and will wait for the mailbox
12217 * command to finish before continuing.
12218 *
12219 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012220 * memory this function will return -ENOMEM. If the queue create mailbox command
12221 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012222 **/
12223uint32_t
12224lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12225{
12226 struct lpfc_mbx_eq_create *eq_create;
12227 LPFC_MBOXQ_t *mbox;
12228 int rc, length, status = 0;
12229 struct lpfc_dmabuf *dmabuf;
12230 uint32_t shdr_status, shdr_add_status;
12231 union lpfc_sli4_cfg_shdr *shdr;
12232 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012233 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12234
James Smart2e90f4b2011-12-13 13:22:37 -050012235 /* sanity check on queue memory */
12236 if (!eq)
12237 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012238 if (!phba->sli4_hba.pc_sli4_params.supported)
12239 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012240
12241 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12242 if (!mbox)
12243 return -ENOMEM;
12244 length = (sizeof(struct lpfc_mbx_eq_create) -
12245 sizeof(struct lpfc_sli4_cfg_mhdr));
12246 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12247 LPFC_MBOX_OPCODE_EQ_CREATE,
12248 length, LPFC_SLI4_MBX_EMBED);
12249 eq_create = &mbox->u.mqe.un.eq_create;
12250 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12251 eq->page_count);
12252 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12253 LPFC_EQE_SIZE);
12254 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12255 /* Calculate delay multiper from maximum interrupt per second */
12256 dmult = LPFC_DMULT_CONST/imax - 1;
12257 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12258 dmult);
12259 switch (eq->entry_count) {
12260 default:
12261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12262 "0360 Unsupported EQ count. (%d)\n",
12263 eq->entry_count);
12264 if (eq->entry_count < 256)
12265 return -EINVAL;
12266 /* otherwise default to smallest count (drop through) */
12267 case 256:
12268 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12269 LPFC_EQ_CNT_256);
12270 break;
12271 case 512:
12272 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12273 LPFC_EQ_CNT_512);
12274 break;
12275 case 1024:
12276 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12277 LPFC_EQ_CNT_1024);
12278 break;
12279 case 2048:
12280 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12281 LPFC_EQ_CNT_2048);
12282 break;
12283 case 4096:
12284 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12285 LPFC_EQ_CNT_4096);
12286 break;
12287 }
12288 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012289 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012290 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12291 putPaddrLow(dmabuf->phys);
12292 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12293 putPaddrHigh(dmabuf->phys);
12294 }
12295 mbox->vport = phba->pport;
12296 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12297 mbox->context1 = NULL;
12298 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12299 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12300 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12301 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12302 if (shdr_status || shdr_add_status || rc) {
12303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12304 "2500 EQ_CREATE mailbox failed with "
12305 "status x%x add_status x%x, mbx status x%x\n",
12306 shdr_status, shdr_add_status, rc);
12307 status = -ENXIO;
12308 }
12309 eq->type = LPFC_EQ;
12310 eq->subtype = LPFC_NONE;
12311 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12312 if (eq->queue_id == 0xFFFF)
12313 status = -ENXIO;
12314 eq->host_index = 0;
12315 eq->hba_index = 0;
12316
James Smart8fa38512009-07-19 10:01:03 -040012317 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012318 return status;
12319}
12320
12321/**
12322 * lpfc_cq_create - Create a Completion Queue on the HBA
12323 * @phba: HBA structure that indicates port to create a queue on.
12324 * @cq: The queue structure to use to create the completion queue.
12325 * @eq: The event queue to bind this completion queue to.
12326 *
12327 * This function creates a completion queue, as detailed in @wq, on a port,
12328 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12329 *
12330 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12331 * is used to get the entry count and entry size that are necessary to
12332 * determine the number of pages to allocate and use for this queue. The @eq
12333 * is used to indicate which event queue to bind this completion queue to. This
12334 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12335 * completion queue. This function is asynchronous and will wait for the mailbox
12336 * command to finish before continuing.
12337 *
12338 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012339 * memory this function will return -ENOMEM. If the queue create mailbox command
12340 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012341 **/
12342uint32_t
12343lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12344 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12345{
12346 struct lpfc_mbx_cq_create *cq_create;
12347 struct lpfc_dmabuf *dmabuf;
12348 LPFC_MBOXQ_t *mbox;
12349 int rc, length, status = 0;
12350 uint32_t shdr_status, shdr_add_status;
12351 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012352 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12353
James Smart2e90f4b2011-12-13 13:22:37 -050012354 /* sanity check on queue memory */
12355 if (!cq || !eq)
12356 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012357 if (!phba->sli4_hba.pc_sli4_params.supported)
12358 hw_page_size = SLI4_PAGE_SIZE;
12359
James Smart4f774512009-05-22 14:52:35 -040012360 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12361 if (!mbox)
12362 return -ENOMEM;
12363 length = (sizeof(struct lpfc_mbx_cq_create) -
12364 sizeof(struct lpfc_sli4_cfg_mhdr));
12365 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12366 LPFC_MBOX_OPCODE_CQ_CREATE,
12367 length, LPFC_SLI4_MBX_EMBED);
12368 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012369 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012370 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12371 cq->page_count);
12372 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12373 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012374 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12375 phba->sli4_hba.pc_sli4_params.cqv);
12376 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012377 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12378 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012379 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12380 eq->queue_id);
12381 } else {
12382 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12383 eq->queue_id);
12384 }
James Smart4f774512009-05-22 14:52:35 -040012385 switch (cq->entry_count) {
12386 default:
12387 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12388 "0361 Unsupported CQ count. (%d)\n",
12389 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012390 if (cq->entry_count < 256) {
12391 status = -EINVAL;
12392 goto out;
12393 }
James Smart4f774512009-05-22 14:52:35 -040012394 /* otherwise default to smallest count (drop through) */
12395 case 256:
12396 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12397 LPFC_CQ_CNT_256);
12398 break;
12399 case 512:
12400 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12401 LPFC_CQ_CNT_512);
12402 break;
12403 case 1024:
12404 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12405 LPFC_CQ_CNT_1024);
12406 break;
12407 }
12408 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012409 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012410 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12411 putPaddrLow(dmabuf->phys);
12412 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12413 putPaddrHigh(dmabuf->phys);
12414 }
12415 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12416
12417 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012418 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12419 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12420 if (shdr_status || shdr_add_status || rc) {
12421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12422 "2501 CQ_CREATE mailbox failed with "
12423 "status x%x add_status x%x, mbx status x%x\n",
12424 shdr_status, shdr_add_status, rc);
12425 status = -ENXIO;
12426 goto out;
12427 }
12428 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12429 if (cq->queue_id == 0xFFFF) {
12430 status = -ENXIO;
12431 goto out;
12432 }
12433 /* link the cq onto the parent eq child list */
12434 list_add_tail(&cq->list, &eq->child_list);
12435 /* Set up completion queue's type and subtype */
12436 cq->type = type;
12437 cq->subtype = subtype;
12438 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012439 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012440 cq->host_index = 0;
12441 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012442
James Smart8fa38512009-07-19 10:01:03 -040012443out:
12444 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012445 return status;
12446}
12447
12448/**
James Smartb19a0612010-04-06 14:48:51 -040012449 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012450 * @phba: HBA structure that indicates port to create a queue on.
12451 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012452 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12453 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012454 *
James Smartb19a0612010-04-06 14:48:51 -040012455 * This function provides failback (fb) functionality when the
12456 * mq_create_ext fails on older FW generations. It's purpose is identical
12457 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012458 *
James Smartb19a0612010-04-06 14:48:51 -040012459 * This routine cannot fail as all attributes were previously accessed and
12460 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012461 **/
James Smartb19a0612010-04-06 14:48:51 -040012462static void
12463lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12464 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012465{
12466 struct lpfc_mbx_mq_create *mq_create;
12467 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012468 int length;
James Smart04c68492009-05-22 14:52:52 -040012469
James Smart04c68492009-05-22 14:52:52 -040012470 length = (sizeof(struct lpfc_mbx_mq_create) -
12471 sizeof(struct lpfc_sli4_cfg_mhdr));
12472 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12473 LPFC_MBOX_OPCODE_MQ_CREATE,
12474 length, LPFC_SLI4_MBX_EMBED);
12475 mq_create = &mbox->u.mqe.un.mq_create;
12476 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012477 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012478 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012479 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012480 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12481 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012482 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012483 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12484 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012485 break;
12486 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012487 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12488 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012489 break;
12490 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012491 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12492 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012493 break;
12494 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012495 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12496 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012497 break;
12498 }
12499 list_for_each_entry(dmabuf, &mq->page_list, list) {
12500 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012501 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012502 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012503 putPaddrHigh(dmabuf->phys);
12504 }
12505}
12506
12507/**
12508 * lpfc_mq_create - Create a mailbox Queue on the HBA
12509 * @phba: HBA structure that indicates port to create a queue on.
12510 * @mq: The queue structure to use to create the mailbox queue.
12511 * @cq: The completion queue to associate with this cq.
12512 * @subtype: The queue's subtype.
12513 *
12514 * This function creates a mailbox queue, as detailed in @mq, on a port,
12515 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12516 *
12517 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12518 * is used to get the entry count and entry size that are necessary to
12519 * determine the number of pages to allocate and use for this queue. This
12520 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12521 * mailbox queue. This function is asynchronous and will wait for the mailbox
12522 * command to finish before continuing.
12523 *
12524 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012525 * memory this function will return -ENOMEM. If the queue create mailbox command
12526 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012527 **/
12528int32_t
12529lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12530 struct lpfc_queue *cq, uint32_t subtype)
12531{
12532 struct lpfc_mbx_mq_create *mq_create;
12533 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12534 struct lpfc_dmabuf *dmabuf;
12535 LPFC_MBOXQ_t *mbox;
12536 int rc, length, status = 0;
12537 uint32_t shdr_status, shdr_add_status;
12538 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012539 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012540
James Smart2e90f4b2011-12-13 13:22:37 -050012541 /* sanity check on queue memory */
12542 if (!mq || !cq)
12543 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012544 if (!phba->sli4_hba.pc_sli4_params.supported)
12545 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012546
12547 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12548 if (!mbox)
12549 return -ENOMEM;
12550 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12551 sizeof(struct lpfc_sli4_cfg_mhdr));
12552 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12553 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12554 length, LPFC_SLI4_MBX_EMBED);
12555
12556 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012557 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012558 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12559 &mq_create_ext->u.request, mq->page_count);
12560 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12561 &mq_create_ext->u.request, 1);
12562 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012563 &mq_create_ext->u.request, 1);
12564 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12565 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012566 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12567 &mq_create_ext->u.request, 1);
12568 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12569 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012570 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012571 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12572 phba->sli4_hba.pc_sli4_params.mqv);
12573 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12574 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12575 cq->queue_id);
12576 else
12577 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12578 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012579 switch (mq->entry_count) {
12580 default:
12581 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12582 "0362 Unsupported MQ count. (%d)\n",
12583 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012584 if (mq->entry_count < 16) {
12585 status = -EINVAL;
12586 goto out;
12587 }
James Smartb19a0612010-04-06 14:48:51 -040012588 /* otherwise default to smallest count (drop through) */
12589 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012590 bf_set(lpfc_mq_context_ring_size,
12591 &mq_create_ext->u.request.context,
12592 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012593 break;
12594 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012595 bf_set(lpfc_mq_context_ring_size,
12596 &mq_create_ext->u.request.context,
12597 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012598 break;
12599 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012600 bf_set(lpfc_mq_context_ring_size,
12601 &mq_create_ext->u.request.context,
12602 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012603 break;
12604 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012605 bf_set(lpfc_mq_context_ring_size,
12606 &mq_create_ext->u.request.context,
12607 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012608 break;
12609 }
12610 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012611 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012612 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12613 putPaddrLow(dmabuf->phys);
12614 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012615 putPaddrHigh(dmabuf->phys);
12616 }
12617 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012618 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12619 &mq_create_ext->u.response);
12620 if (rc != MBX_SUCCESS) {
12621 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12622 "2795 MQ_CREATE_EXT failed with "
12623 "status x%x. Failback to MQ_CREATE.\n",
12624 rc);
12625 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12626 mq_create = &mbox->u.mqe.un.mq_create;
12627 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12628 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12629 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12630 &mq_create->u.response);
12631 }
12632
James Smart04c68492009-05-22 14:52:52 -040012633 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012634 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12635 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12636 if (shdr_status || shdr_add_status || rc) {
12637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12638 "2502 MQ_CREATE mailbox failed with "
12639 "status x%x add_status x%x, mbx status x%x\n",
12640 shdr_status, shdr_add_status, rc);
12641 status = -ENXIO;
12642 goto out;
12643 }
James Smart04c68492009-05-22 14:52:52 -040012644 if (mq->queue_id == 0xFFFF) {
12645 status = -ENXIO;
12646 goto out;
12647 }
12648 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012649 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012650 mq->subtype = subtype;
12651 mq->host_index = 0;
12652 mq->hba_index = 0;
12653
12654 /* link the mq onto the parent cq child list */
12655 list_add_tail(&mq->list, &cq->child_list);
12656out:
James Smart8fa38512009-07-19 10:01:03 -040012657 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012658 return status;
12659}
12660
12661/**
James Smart4f774512009-05-22 14:52:35 -040012662 * lpfc_wq_create - Create a Work Queue on the HBA
12663 * @phba: HBA structure that indicates port to create a queue on.
12664 * @wq: The queue structure to use to create the work queue.
12665 * @cq: The completion queue to bind this work queue to.
12666 * @subtype: The subtype of the work queue indicating its functionality.
12667 *
12668 * This function creates a work queue, as detailed in @wq, on a port, described
12669 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12670 *
12671 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12672 * is used to get the entry count and entry size that are necessary to
12673 * determine the number of pages to allocate and use for this queue. The @cq
12674 * is used to indicate which completion queue to bind this work queue to. This
12675 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12676 * work queue. This function is asynchronous and will wait for the mailbox
12677 * command to finish before continuing.
12678 *
12679 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012680 * memory this function will return -ENOMEM. If the queue create mailbox command
12681 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012682 **/
12683uint32_t
12684lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12685 struct lpfc_queue *cq, uint32_t subtype)
12686{
12687 struct lpfc_mbx_wq_create *wq_create;
12688 struct lpfc_dmabuf *dmabuf;
12689 LPFC_MBOXQ_t *mbox;
12690 int rc, length, status = 0;
12691 uint32_t shdr_status, shdr_add_status;
12692 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012693 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012694 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012695
James Smart2e90f4b2011-12-13 13:22:37 -050012696 /* sanity check on queue memory */
12697 if (!wq || !cq)
12698 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012699 if (!phba->sli4_hba.pc_sli4_params.supported)
12700 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012701
12702 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12703 if (!mbox)
12704 return -ENOMEM;
12705 length = (sizeof(struct lpfc_mbx_wq_create) -
12706 sizeof(struct lpfc_sli4_cfg_mhdr));
12707 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12708 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12709 length, LPFC_SLI4_MBX_EMBED);
12710 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012711 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012712 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12713 wq->page_count);
12714 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12715 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012716 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12717 phba->sli4_hba.pc_sli4_params.wqv);
12718 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12719 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12720 wq->entry_count);
12721 switch (wq->entry_size) {
12722 default:
12723 case 64:
12724 bf_set(lpfc_mbx_wq_create_wqe_size,
12725 &wq_create->u.request_1,
12726 LPFC_WQ_WQE_SIZE_64);
12727 break;
12728 case 128:
12729 bf_set(lpfc_mbx_wq_create_wqe_size,
12730 &wq_create->u.request_1,
12731 LPFC_WQ_WQE_SIZE_128);
12732 break;
12733 }
12734 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12735 (PAGE_SIZE/SLI4_PAGE_SIZE));
12736 page = wq_create->u.request_1.page;
12737 } else {
12738 page = wq_create->u.request.page;
12739 }
James Smart4f774512009-05-22 14:52:35 -040012740 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012741 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012742 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12743 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012744 }
12745 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12746 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012747 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12748 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12749 if (shdr_status || shdr_add_status || rc) {
12750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12751 "2503 WQ_CREATE mailbox failed with "
12752 "status x%x add_status x%x, mbx status x%x\n",
12753 shdr_status, shdr_add_status, rc);
12754 status = -ENXIO;
12755 goto out;
12756 }
12757 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12758 if (wq->queue_id == 0xFFFF) {
12759 status = -ENXIO;
12760 goto out;
12761 }
12762 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012763 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012764 wq->subtype = subtype;
12765 wq->host_index = 0;
12766 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012767 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012768
12769 /* link the wq onto the parent cq child list */
12770 list_add_tail(&wq->list, &cq->child_list);
12771out:
James Smart8fa38512009-07-19 10:01:03 -040012772 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012773 return status;
12774}
12775
12776/**
James Smart73d91e52011-10-10 21:32:10 -040012777 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12778 * @phba: HBA structure that indicates port to create a queue on.
12779 * @rq: The queue structure to use for the receive queue.
12780 * @qno: The associated HBQ number
12781 *
12782 *
12783 * For SLI4 we need to adjust the RQ repost value based on
12784 * the number of buffers that are initially posted to the RQ.
12785 */
12786void
12787lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12788{
12789 uint32_t cnt;
12790
James Smart2e90f4b2011-12-13 13:22:37 -050012791 /* sanity check on queue memory */
12792 if (!rq)
12793 return;
James Smart73d91e52011-10-10 21:32:10 -040012794 cnt = lpfc_hbq_defs[qno]->entry_count;
12795
12796 /* Recalc repost for RQs based on buffers initially posted */
12797 cnt = (cnt >> 3);
12798 if (cnt < LPFC_QUEUE_MIN_REPOST)
12799 cnt = LPFC_QUEUE_MIN_REPOST;
12800
12801 rq->entry_repost = cnt;
12802}
12803
12804/**
James Smart4f774512009-05-22 14:52:35 -040012805 * lpfc_rq_create - Create a Receive Queue on the HBA
12806 * @phba: HBA structure that indicates port to create a queue on.
12807 * @hrq: The queue structure to use to create the header receive queue.
12808 * @drq: The queue structure to use to create the data receive queue.
12809 * @cq: The completion queue to bind this work queue to.
12810 *
12811 * This function creates a receive buffer queue pair , as detailed in @hrq and
12812 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12813 * to the HBA.
12814 *
12815 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12816 * struct is used to get the entry count that is necessary to determine the
12817 * number of pages to use for this queue. The @cq is used to indicate which
12818 * completion queue to bind received buffers that are posted to these queues to.
12819 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12820 * receive queue pair. This function is asynchronous and will wait for the
12821 * mailbox command to finish before continuing.
12822 *
12823 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012824 * memory this function will return -ENOMEM. If the queue create mailbox command
12825 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012826 **/
12827uint32_t
12828lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12829 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12830{
12831 struct lpfc_mbx_rq_create *rq_create;
12832 struct lpfc_dmabuf *dmabuf;
12833 LPFC_MBOXQ_t *mbox;
12834 int rc, length, status = 0;
12835 uint32_t shdr_status, shdr_add_status;
12836 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012837 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12838
James Smart2e90f4b2011-12-13 13:22:37 -050012839 /* sanity check on queue memory */
12840 if (!hrq || !drq || !cq)
12841 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012842 if (!phba->sli4_hba.pc_sli4_params.supported)
12843 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012844
12845 if (hrq->entry_count != drq->entry_count)
12846 return -EINVAL;
12847 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12848 if (!mbox)
12849 return -ENOMEM;
12850 length = (sizeof(struct lpfc_mbx_rq_create) -
12851 sizeof(struct lpfc_sli4_cfg_mhdr));
12852 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12853 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12854 length, LPFC_SLI4_MBX_EMBED);
12855 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012856 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12857 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12858 phba->sli4_hba.pc_sli4_params.rqv);
12859 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12860 bf_set(lpfc_rq_context_rqe_count_1,
12861 &rq_create->u.request.context,
12862 hrq->entry_count);
12863 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012864 bf_set(lpfc_rq_context_rqe_size,
12865 &rq_create->u.request.context,
12866 LPFC_RQE_SIZE_8);
12867 bf_set(lpfc_rq_context_page_size,
12868 &rq_create->u.request.context,
12869 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012870 } else {
12871 switch (hrq->entry_count) {
12872 default:
12873 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12874 "2535 Unsupported RQ count. (%d)\n",
12875 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012876 if (hrq->entry_count < 512) {
12877 status = -EINVAL;
12878 goto out;
12879 }
James Smart5a6f1332011-03-11 16:05:35 -050012880 /* otherwise default to smallest count (drop through) */
12881 case 512:
12882 bf_set(lpfc_rq_context_rqe_count,
12883 &rq_create->u.request.context,
12884 LPFC_RQ_RING_SIZE_512);
12885 break;
12886 case 1024:
12887 bf_set(lpfc_rq_context_rqe_count,
12888 &rq_create->u.request.context,
12889 LPFC_RQ_RING_SIZE_1024);
12890 break;
12891 case 2048:
12892 bf_set(lpfc_rq_context_rqe_count,
12893 &rq_create->u.request.context,
12894 LPFC_RQ_RING_SIZE_2048);
12895 break;
12896 case 4096:
12897 bf_set(lpfc_rq_context_rqe_count,
12898 &rq_create->u.request.context,
12899 LPFC_RQ_RING_SIZE_4096);
12900 break;
12901 }
12902 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12903 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012904 }
12905 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12906 cq->queue_id);
12907 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12908 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012909 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012910 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012911 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12912 putPaddrLow(dmabuf->phys);
12913 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12914 putPaddrHigh(dmabuf->phys);
12915 }
12916 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12917 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012918 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12919 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12920 if (shdr_status || shdr_add_status || rc) {
12921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12922 "2504 RQ_CREATE mailbox failed with "
12923 "status x%x add_status x%x, mbx status x%x\n",
12924 shdr_status, shdr_add_status, rc);
12925 status = -ENXIO;
12926 goto out;
12927 }
12928 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12929 if (hrq->queue_id == 0xFFFF) {
12930 status = -ENXIO;
12931 goto out;
12932 }
12933 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012934 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012935 hrq->subtype = subtype;
12936 hrq->host_index = 0;
12937 hrq->hba_index = 0;
12938
12939 /* now create the data queue */
12940 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12941 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12942 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012943 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12944 phba->sli4_hba.pc_sli4_params.rqv);
12945 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12946 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012947 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012948 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012949 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12950 LPFC_RQE_SIZE_8);
12951 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12952 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012953 } else {
12954 switch (drq->entry_count) {
12955 default:
12956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12957 "2536 Unsupported RQ count. (%d)\n",
12958 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012959 if (drq->entry_count < 512) {
12960 status = -EINVAL;
12961 goto out;
12962 }
James Smart5a6f1332011-03-11 16:05:35 -050012963 /* otherwise default to smallest count (drop through) */
12964 case 512:
12965 bf_set(lpfc_rq_context_rqe_count,
12966 &rq_create->u.request.context,
12967 LPFC_RQ_RING_SIZE_512);
12968 break;
12969 case 1024:
12970 bf_set(lpfc_rq_context_rqe_count,
12971 &rq_create->u.request.context,
12972 LPFC_RQ_RING_SIZE_1024);
12973 break;
12974 case 2048:
12975 bf_set(lpfc_rq_context_rqe_count,
12976 &rq_create->u.request.context,
12977 LPFC_RQ_RING_SIZE_2048);
12978 break;
12979 case 4096:
12980 bf_set(lpfc_rq_context_rqe_count,
12981 &rq_create->u.request.context,
12982 LPFC_RQ_RING_SIZE_4096);
12983 break;
12984 }
12985 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12986 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012987 }
12988 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12989 cq->queue_id);
12990 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12991 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012992 list_for_each_entry(dmabuf, &drq->page_list, list) {
12993 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12994 putPaddrLow(dmabuf->phys);
12995 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12996 putPaddrHigh(dmabuf->phys);
12997 }
12998 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12999 /* The IOCTL status is embedded in the mailbox subheader. */
13000 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13001 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13002 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13003 if (shdr_status || shdr_add_status || rc) {
13004 status = -ENXIO;
13005 goto out;
13006 }
13007 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13008 if (drq->queue_id == 0xFFFF) {
13009 status = -ENXIO;
13010 goto out;
13011 }
13012 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013013 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013014 drq->subtype = subtype;
13015 drq->host_index = 0;
13016 drq->hba_index = 0;
13017
13018 /* link the header and data RQs onto the parent cq child list */
13019 list_add_tail(&hrq->list, &cq->child_list);
13020 list_add_tail(&drq->list, &cq->child_list);
13021
13022out:
James Smart8fa38512009-07-19 10:01:03 -040013023 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013024 return status;
13025}
13026
13027/**
13028 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13029 * @eq: The queue structure associated with the queue to destroy.
13030 *
13031 * This function destroys a queue, as detailed in @eq by sending an mailbox
13032 * command, specific to the type of queue, to the HBA.
13033 *
13034 * The @eq struct is used to get the queue ID of the queue to destroy.
13035 *
13036 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013037 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013038 **/
13039uint32_t
13040lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13041{
13042 LPFC_MBOXQ_t *mbox;
13043 int rc, length, status = 0;
13044 uint32_t shdr_status, shdr_add_status;
13045 union lpfc_sli4_cfg_shdr *shdr;
13046
James Smart2e90f4b2011-12-13 13:22:37 -050013047 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013048 if (!eq)
13049 return -ENODEV;
13050 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13051 if (!mbox)
13052 return -ENOMEM;
13053 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13054 sizeof(struct lpfc_sli4_cfg_mhdr));
13055 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13056 LPFC_MBOX_OPCODE_EQ_DESTROY,
13057 length, LPFC_SLI4_MBX_EMBED);
13058 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13059 eq->queue_id);
13060 mbox->vport = eq->phba->pport;
13061 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13062
13063 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13064 /* The IOCTL status is embedded in the mailbox subheader. */
13065 shdr = (union lpfc_sli4_cfg_shdr *)
13066 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13067 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13068 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13069 if (shdr_status || shdr_add_status || rc) {
13070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13071 "2505 EQ_DESTROY mailbox failed with "
13072 "status x%x add_status x%x, mbx status x%x\n",
13073 shdr_status, shdr_add_status, rc);
13074 status = -ENXIO;
13075 }
13076
13077 /* Remove eq from any list */
13078 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013079 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013080 return status;
13081}
13082
13083/**
13084 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13085 * @cq: The queue structure associated with the queue to destroy.
13086 *
13087 * This function destroys a queue, as detailed in @cq by sending an mailbox
13088 * command, specific to the type of queue, to the HBA.
13089 *
13090 * The @cq struct is used to get the queue ID of the queue to destroy.
13091 *
13092 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013093 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013094 **/
13095uint32_t
13096lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13097{
13098 LPFC_MBOXQ_t *mbox;
13099 int rc, length, status = 0;
13100 uint32_t shdr_status, shdr_add_status;
13101 union lpfc_sli4_cfg_shdr *shdr;
13102
James Smart2e90f4b2011-12-13 13:22:37 -050013103 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013104 if (!cq)
13105 return -ENODEV;
13106 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13107 if (!mbox)
13108 return -ENOMEM;
13109 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13110 sizeof(struct lpfc_sli4_cfg_mhdr));
13111 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13112 LPFC_MBOX_OPCODE_CQ_DESTROY,
13113 length, LPFC_SLI4_MBX_EMBED);
13114 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13115 cq->queue_id);
13116 mbox->vport = cq->phba->pport;
13117 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13118 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13119 /* The IOCTL status is embedded in the mailbox subheader. */
13120 shdr = (union lpfc_sli4_cfg_shdr *)
13121 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13122 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13123 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13124 if (shdr_status || shdr_add_status || rc) {
13125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13126 "2506 CQ_DESTROY mailbox failed with "
13127 "status x%x add_status x%x, mbx status x%x\n",
13128 shdr_status, shdr_add_status, rc);
13129 status = -ENXIO;
13130 }
13131 /* Remove cq from any list */
13132 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013133 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013134 return status;
13135}
13136
13137/**
James Smart04c68492009-05-22 14:52:52 -040013138 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13139 * @qm: The queue structure associated with the queue to destroy.
13140 *
13141 * This function destroys a queue, as detailed in @mq by sending an mailbox
13142 * command, specific to the type of queue, to the HBA.
13143 *
13144 * The @mq struct is used to get the queue ID of the queue to destroy.
13145 *
13146 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013147 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013148 **/
13149uint32_t
13150lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13151{
13152 LPFC_MBOXQ_t *mbox;
13153 int rc, length, status = 0;
13154 uint32_t shdr_status, shdr_add_status;
13155 union lpfc_sli4_cfg_shdr *shdr;
13156
James Smart2e90f4b2011-12-13 13:22:37 -050013157 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013158 if (!mq)
13159 return -ENODEV;
13160 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13161 if (!mbox)
13162 return -ENOMEM;
13163 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13164 sizeof(struct lpfc_sli4_cfg_mhdr));
13165 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13166 LPFC_MBOX_OPCODE_MQ_DESTROY,
13167 length, LPFC_SLI4_MBX_EMBED);
13168 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13169 mq->queue_id);
13170 mbox->vport = mq->phba->pport;
13171 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13172 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13173 /* The IOCTL status is embedded in the mailbox subheader. */
13174 shdr = (union lpfc_sli4_cfg_shdr *)
13175 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13176 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13177 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13178 if (shdr_status || shdr_add_status || rc) {
13179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13180 "2507 MQ_DESTROY mailbox failed with "
13181 "status x%x add_status x%x, mbx status x%x\n",
13182 shdr_status, shdr_add_status, rc);
13183 status = -ENXIO;
13184 }
13185 /* Remove mq from any list */
13186 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013187 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013188 return status;
13189}
13190
13191/**
James Smart4f774512009-05-22 14:52:35 -040013192 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13193 * @wq: The queue structure associated with the queue to destroy.
13194 *
13195 * This function destroys a queue, as detailed in @wq by sending an mailbox
13196 * command, specific to the type of queue, to the HBA.
13197 *
13198 * The @wq struct is used to get the queue ID of the queue to destroy.
13199 *
13200 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013201 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013202 **/
13203uint32_t
13204lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13205{
13206 LPFC_MBOXQ_t *mbox;
13207 int rc, length, status = 0;
13208 uint32_t shdr_status, shdr_add_status;
13209 union lpfc_sli4_cfg_shdr *shdr;
13210
James Smart2e90f4b2011-12-13 13:22:37 -050013211 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013212 if (!wq)
13213 return -ENODEV;
13214 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13215 if (!mbox)
13216 return -ENOMEM;
13217 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13218 sizeof(struct lpfc_sli4_cfg_mhdr));
13219 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13220 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13221 length, LPFC_SLI4_MBX_EMBED);
13222 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13223 wq->queue_id);
13224 mbox->vport = wq->phba->pport;
13225 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13226 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13227 shdr = (union lpfc_sli4_cfg_shdr *)
13228 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13229 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13230 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13231 if (shdr_status || shdr_add_status || rc) {
13232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13233 "2508 WQ_DESTROY mailbox failed with "
13234 "status x%x add_status x%x, mbx status x%x\n",
13235 shdr_status, shdr_add_status, rc);
13236 status = -ENXIO;
13237 }
13238 /* Remove wq from any list */
13239 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013240 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013241 return status;
13242}
13243
13244/**
13245 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13246 * @rq: The queue structure associated with the queue to destroy.
13247 *
13248 * This function destroys a queue, as detailed in @rq by sending an mailbox
13249 * command, specific to the type of queue, to the HBA.
13250 *
13251 * The @rq struct is used to get the queue ID of the queue to destroy.
13252 *
13253 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013254 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013255 **/
13256uint32_t
13257lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13258 struct lpfc_queue *drq)
13259{
13260 LPFC_MBOXQ_t *mbox;
13261 int rc, length, status = 0;
13262 uint32_t shdr_status, shdr_add_status;
13263 union lpfc_sli4_cfg_shdr *shdr;
13264
James Smart2e90f4b2011-12-13 13:22:37 -050013265 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013266 if (!hrq || !drq)
13267 return -ENODEV;
13268 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13269 if (!mbox)
13270 return -ENOMEM;
13271 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013272 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013273 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13274 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13275 length, LPFC_SLI4_MBX_EMBED);
13276 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13277 hrq->queue_id);
13278 mbox->vport = hrq->phba->pport;
13279 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13280 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13281 /* The IOCTL status is embedded in the mailbox subheader. */
13282 shdr = (union lpfc_sli4_cfg_shdr *)
13283 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13284 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13285 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13286 if (shdr_status || shdr_add_status || rc) {
13287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13288 "2509 RQ_DESTROY mailbox failed with "
13289 "status x%x add_status x%x, mbx status x%x\n",
13290 shdr_status, shdr_add_status, rc);
13291 if (rc != MBX_TIMEOUT)
13292 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13293 return -ENXIO;
13294 }
13295 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13296 drq->queue_id);
13297 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13298 shdr = (union lpfc_sli4_cfg_shdr *)
13299 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13300 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13301 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13302 if (shdr_status || shdr_add_status || rc) {
13303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13304 "2510 RQ_DESTROY mailbox failed with "
13305 "status x%x add_status x%x, mbx status x%x\n",
13306 shdr_status, shdr_add_status, rc);
13307 status = -ENXIO;
13308 }
13309 list_del_init(&hrq->list);
13310 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013311 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013312 return status;
13313}
13314
13315/**
13316 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13317 * @phba: The virtual port for which this call being executed.
13318 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13319 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13320 * @xritag: the xritag that ties this io to the SGL pages.
13321 *
13322 * This routine will post the sgl pages for the IO that has the xritag
13323 * that is in the iocbq structure. The xritag is assigned during iocbq
13324 * creation and persists for as long as the driver is loaded.
13325 * if the caller has fewer than 256 scatter gather segments to map then
13326 * pdma_phys_addr1 should be 0.
13327 * If the caller needs to map more than 256 scatter gather segment then
13328 * pdma_phys_addr1 should be a valid physical address.
13329 * physical address for SGLs must be 64 byte aligned.
13330 * If you are going to map 2 SGL's then the first one must have 256 entries
13331 * the second sgl can have between 1 and 256 entries.
13332 *
13333 * Return codes:
13334 * 0 - Success
13335 * -ENXIO, -ENOMEM - Failure
13336 **/
13337int
13338lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13339 dma_addr_t pdma_phys_addr0,
13340 dma_addr_t pdma_phys_addr1,
13341 uint16_t xritag)
13342{
13343 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13344 LPFC_MBOXQ_t *mbox;
13345 int rc;
13346 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013347 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013348 union lpfc_sli4_cfg_shdr *shdr;
13349
13350 if (xritag == NO_XRI) {
13351 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13352 "0364 Invalid param:\n");
13353 return -EINVAL;
13354 }
13355
13356 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13357 if (!mbox)
13358 return -ENOMEM;
13359
13360 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13361 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13362 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013363 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013364
13365 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13366 &mbox->u.mqe.un.post_sgl_pages;
13367 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13368 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13369
13370 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13371 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13372 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13373 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13374
13375 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13376 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13377 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13378 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13379 if (!phba->sli4_hba.intr_enable)
13380 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013381 else {
James Smarta183a152011-10-10 21:32:43 -040013382 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013383 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13384 }
James Smart4f774512009-05-22 14:52:35 -040013385 /* The IOCTL status is embedded in the mailbox subheader. */
13386 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13387 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13388 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13389 if (rc != MBX_TIMEOUT)
13390 mempool_free(mbox, phba->mbox_mem_pool);
13391 if (shdr_status || shdr_add_status || rc) {
13392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13393 "2511 POST_SGL mailbox failed with "
13394 "status x%x add_status x%x, mbx status x%x\n",
13395 shdr_status, shdr_add_status, rc);
13396 rc = -ENXIO;
13397 }
13398 return 0;
13399}
James Smart4f774512009-05-22 14:52:35 -040013400
13401/**
James Smart88a2cfb2011-07-22 18:36:33 -040013402 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013403 * @phba: pointer to lpfc hba data structure.
13404 *
13405 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013406 * HBA consistent with the SLI-4 interface spec. This routine
13407 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13408 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013409 *
James Smart88a2cfb2011-07-22 18:36:33 -040013410 * Returns
13411 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13412 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13413 **/
James Smart6d368e52011-05-24 11:44:12 -040013414uint16_t
13415lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13416{
13417 unsigned long xri;
13418
13419 /*
13420 * Fetch the next logical xri. Because this index is logical,
13421 * the driver starts at 0 each time.
13422 */
13423 spin_lock_irq(&phba->hbalock);
13424 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13425 phba->sli4_hba.max_cfg_param.max_xri, 0);
13426 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13427 spin_unlock_irq(&phba->hbalock);
13428 return NO_XRI;
13429 } else {
13430 set_bit(xri, phba->sli4_hba.xri_bmask);
13431 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013432 }
James Smart6d368e52011-05-24 11:44:12 -040013433 spin_unlock_irq(&phba->hbalock);
13434 return xri;
13435}
13436
13437/**
13438 * lpfc_sli4_free_xri - Release an xri for reuse.
13439 * @phba: pointer to lpfc hba data structure.
13440 *
13441 * This routine is invoked to release an xri to the pool of
13442 * available rpis maintained by the driver.
13443 **/
13444void
13445__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13446{
13447 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013448 phba->sli4_hba.max_cfg_param.xri_used--;
13449 }
13450}
13451
13452/**
13453 * lpfc_sli4_free_xri - Release an xri for reuse.
13454 * @phba: pointer to lpfc hba data structure.
13455 *
13456 * This routine is invoked to release an xri to the pool of
13457 * available rpis maintained by the driver.
13458 **/
13459void
13460lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13461{
13462 spin_lock_irq(&phba->hbalock);
13463 __lpfc_sli4_free_xri(phba, xri);
13464 spin_unlock_irq(&phba->hbalock);
13465}
13466
13467/**
James Smart4f774512009-05-22 14:52:35 -040013468 * lpfc_sli4_next_xritag - Get an xritag for the io
13469 * @phba: Pointer to HBA context object.
13470 *
13471 * This function gets an xritag for the iocb. If there is no unused xritag
13472 * it will return 0xffff.
13473 * The function returns the allocated xritag if successful, else returns zero.
13474 * Zero is not a valid xritag.
13475 * The caller is not required to hold any lock.
13476 **/
13477uint16_t
13478lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13479{
James Smart6d368e52011-05-24 11:44:12 -040013480 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013481
James Smart6d368e52011-05-24 11:44:12 -040013482 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013483 if (xri_index == NO_XRI)
13484 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13485 "2004 Failed to allocate XRI.last XRITAG is %d"
13486 " Max XRI is %d, Used XRI is %d\n",
13487 xri_index,
13488 phba->sli4_hba.max_cfg_param.max_xri,
13489 phba->sli4_hba.max_cfg_param.xri_used);
13490 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013491}
13492
13493/**
James Smart6d368e52011-05-24 11:44:12 -040013494 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013495 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013496 * @post_sgl_list: pointer to els sgl entry list.
13497 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013498 *
13499 * This routine is invoked to post a block of driver's sgl pages to the
13500 * HBA using non-embedded mailbox command. No Lock is held. This routine
13501 * is only called when the driver is loading and after all IO has been
13502 * stopped.
13503 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013504static int
13505lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13506 struct list_head *post_sgl_list,
13507 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013508{
James Smart8a9d2e82012-05-09 21:16:12 -040013509 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013510 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13511 struct sgl_page_pairs *sgl_pg_pairs;
13512 void *viraddr;
13513 LPFC_MBOXQ_t *mbox;
13514 uint32_t reqlen, alloclen, pg_pairs;
13515 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013516 uint16_t xritag_start = 0;
13517 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013518 uint32_t shdr_status, shdr_add_status;
13519 union lpfc_sli4_cfg_shdr *shdr;
13520
James Smart8a9d2e82012-05-09 21:16:12 -040013521 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013522 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013523 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013524 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13525 "2559 Block sgl registration required DMA "
13526 "size (%d) great than a page\n", reqlen);
13527 return -ENOMEM;
13528 }
13529 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013530 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013531 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013532
13533 /* Allocate DMA memory and set up the non-embedded mailbox command */
13534 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13535 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13536 LPFC_SLI4_MBX_NEMBED);
13537
13538 if (alloclen < reqlen) {
13539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13540 "0285 Allocated DMA memory size (%d) is "
13541 "less than the requested DMA memory "
13542 "size (%d)\n", alloclen, reqlen);
13543 lpfc_sli4_mbox_cmd_free(phba, mbox);
13544 return -ENOMEM;
13545 }
James Smart4f774512009-05-22 14:52:35 -040013546 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013547 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013548 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13549 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13550
James Smart8a9d2e82012-05-09 21:16:12 -040013551 pg_pairs = 0;
13552 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013553 /* Set up the sge entry */
13554 sgl_pg_pairs->sgl_pg0_addr_lo =
13555 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13556 sgl_pg_pairs->sgl_pg0_addr_hi =
13557 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13558 sgl_pg_pairs->sgl_pg1_addr_lo =
13559 cpu_to_le32(putPaddrLow(0));
13560 sgl_pg_pairs->sgl_pg1_addr_hi =
13561 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013562
James Smart4f774512009-05-22 14:52:35 -040013563 /* Keep the first xritag on the list */
13564 if (pg_pairs == 0)
13565 xritag_start = sglq_entry->sli4_xritag;
13566 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013567 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013568 }
James Smart6d368e52011-05-24 11:44:12 -040013569
13570 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013571 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013572 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013573 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013574 if (!phba->sli4_hba.intr_enable)
13575 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13576 else {
James Smarta183a152011-10-10 21:32:43 -040013577 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013578 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13579 }
13580 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13581 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13582 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13583 if (rc != MBX_TIMEOUT)
13584 lpfc_sli4_mbox_cmd_free(phba, mbox);
13585 if (shdr_status || shdr_add_status || rc) {
13586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13587 "2513 POST_SGL_BLOCK mailbox command failed "
13588 "status x%x add_status x%x mbx status x%x\n",
13589 shdr_status, shdr_add_status, rc);
13590 rc = -ENXIO;
13591 }
13592 return rc;
13593}
13594
13595/**
13596 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13597 * @phba: pointer to lpfc hba data structure.
13598 * @sblist: pointer to scsi buffer list.
13599 * @count: number of scsi buffers on the list.
13600 *
13601 * This routine is invoked to post a block of @count scsi sgl pages from a
13602 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13603 * No Lock is held.
13604 *
13605 **/
13606int
James Smart8a9d2e82012-05-09 21:16:12 -040013607lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13608 struct list_head *sblist,
13609 int count)
James Smart4f774512009-05-22 14:52:35 -040013610{
13611 struct lpfc_scsi_buf *psb;
13612 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13613 struct sgl_page_pairs *sgl_pg_pairs;
13614 void *viraddr;
13615 LPFC_MBOXQ_t *mbox;
13616 uint32_t reqlen, alloclen, pg_pairs;
13617 uint32_t mbox_tmo;
13618 uint16_t xritag_start = 0;
13619 int rc = 0;
13620 uint32_t shdr_status, shdr_add_status;
13621 dma_addr_t pdma_phys_bpl1;
13622 union lpfc_sli4_cfg_shdr *shdr;
13623
13624 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013625 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013626 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013627 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013628 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13629 "0217 Block sgl registration required DMA "
13630 "size (%d) great than a page\n", reqlen);
13631 return -ENOMEM;
13632 }
13633 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13634 if (!mbox) {
13635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13636 "0283 Failed to allocate mbox cmd memory\n");
13637 return -ENOMEM;
13638 }
13639
13640 /* Allocate DMA memory and set up the non-embedded mailbox command */
13641 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13642 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13643 LPFC_SLI4_MBX_NEMBED);
13644
13645 if (alloclen < reqlen) {
13646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13647 "2561 Allocated DMA memory size (%d) is "
13648 "less than the requested DMA memory "
13649 "size (%d)\n", alloclen, reqlen);
13650 lpfc_sli4_mbox_cmd_free(phba, mbox);
13651 return -ENOMEM;
13652 }
James Smart6d368e52011-05-24 11:44:12 -040013653
James Smart4f774512009-05-22 14:52:35 -040013654 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013655 viraddr = mbox->sge_array->addr[0];
13656
13657 /* Set up the SGL pages in the non-embedded DMA pages */
13658 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13659 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13660
13661 pg_pairs = 0;
13662 list_for_each_entry(psb, sblist, list) {
13663 /* Set up the sge entry */
13664 sgl_pg_pairs->sgl_pg0_addr_lo =
13665 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13666 sgl_pg_pairs->sgl_pg0_addr_hi =
13667 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13668 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13669 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13670 else
13671 pdma_phys_bpl1 = 0;
13672 sgl_pg_pairs->sgl_pg1_addr_lo =
13673 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13674 sgl_pg_pairs->sgl_pg1_addr_hi =
13675 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13676 /* Keep the first xritag on the list */
13677 if (pg_pairs == 0)
13678 xritag_start = psb->cur_iocbq.sli4_xritag;
13679 sgl_pg_pairs++;
13680 pg_pairs++;
13681 }
13682 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13683 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13684 /* Perform endian conversion if necessary */
13685 sgl->word0 = cpu_to_le32(sgl->word0);
13686
13687 if (!phba->sli4_hba.intr_enable)
13688 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13689 else {
James Smarta183a152011-10-10 21:32:43 -040013690 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013691 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13692 }
13693 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13694 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13695 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13696 if (rc != MBX_TIMEOUT)
13697 lpfc_sli4_mbox_cmd_free(phba, mbox);
13698 if (shdr_status || shdr_add_status || rc) {
13699 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13700 "2564 POST_SGL_BLOCK mailbox command failed "
13701 "status x%x add_status x%x mbx status x%x\n",
13702 shdr_status, shdr_add_status, rc);
13703 rc = -ENXIO;
13704 }
13705 return rc;
13706}
13707
13708/**
13709 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13710 * @phba: pointer to lpfc_hba struct that the frame was received on
13711 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13712 *
13713 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13714 * valid type of frame that the LPFC driver will handle. This function will
13715 * return a zero if the frame is a valid frame or a non zero value when the
13716 * frame does not pass the check.
13717 **/
13718static int
13719lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13720{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013721 /* make rctl_names static to save stack space */
13722 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013723 char *type_names[] = FC_TYPE_NAMES_INIT;
13724 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013725 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013726
13727 switch (fc_hdr->fh_r_ctl) {
13728 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13729 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13730 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13731 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13732 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13733 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13734 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13735 case FC_RCTL_DD_CMD_STATUS: /* command status */
13736 case FC_RCTL_ELS_REQ: /* extended link services request */
13737 case FC_RCTL_ELS_REP: /* extended link services reply */
13738 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13739 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13740 case FC_RCTL_BA_NOP: /* basic link service NOP */
13741 case FC_RCTL_BA_ABTS: /* basic link service abort */
13742 case FC_RCTL_BA_RMC: /* remove connection */
13743 case FC_RCTL_BA_ACC: /* basic accept */
13744 case FC_RCTL_BA_RJT: /* basic reject */
13745 case FC_RCTL_BA_PRMT:
13746 case FC_RCTL_ACK_1: /* acknowledge_1 */
13747 case FC_RCTL_ACK_0: /* acknowledge_0 */
13748 case FC_RCTL_P_RJT: /* port reject */
13749 case FC_RCTL_F_RJT: /* fabric reject */
13750 case FC_RCTL_P_BSY: /* port busy */
13751 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13752 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13753 case FC_RCTL_LCR: /* link credit reset */
13754 case FC_RCTL_END: /* end */
13755 break;
13756 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13757 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13758 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13759 return lpfc_fc_frame_check(phba, fc_hdr);
13760 default:
13761 goto drop;
13762 }
13763 switch (fc_hdr->fh_type) {
13764 case FC_TYPE_BLS:
13765 case FC_TYPE_ELS:
13766 case FC_TYPE_FCP:
13767 case FC_TYPE_CT:
13768 break;
13769 case FC_TYPE_IP:
13770 case FC_TYPE_ILS:
13771 default:
13772 goto drop;
13773 }
James Smart546fc852011-03-11 16:06:29 -050013774
James Smart4f774512009-05-22 14:52:35 -040013775 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013776 "2538 Received frame rctl:%s type:%s "
13777 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013778 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013779 type_names[fc_hdr->fh_type],
13780 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13781 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13782 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013783 return 0;
13784drop:
13785 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13786 "2539 Dropped frame rctl:%s type:%s\n",
13787 rctl_names[fc_hdr->fh_r_ctl],
13788 type_names[fc_hdr->fh_type]);
13789 return 1;
13790}
13791
13792/**
13793 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13794 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13795 *
13796 * This function processes the FC header to retrieve the VFI from the VF
13797 * header, if one exists. This function will return the VFI if one exists
13798 * or 0 if no VSAN Header exists.
13799 **/
13800static uint32_t
13801lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13802{
13803 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13804
13805 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13806 return 0;
13807 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13808}
13809
13810/**
13811 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13812 * @phba: Pointer to the HBA structure to search for the vport on
13813 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13814 * @fcfi: The FC Fabric ID that the frame came from
13815 *
13816 * This function searches the @phba for a vport that matches the content of the
13817 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13818 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13819 * returns the matching vport pointer or NULL if unable to match frame to a
13820 * vport.
13821 **/
13822static struct lpfc_vport *
13823lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13824 uint16_t fcfi)
13825{
13826 struct lpfc_vport **vports;
13827 struct lpfc_vport *vport = NULL;
13828 int i;
13829 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13830 fc_hdr->fh_d_id[1] << 8 |
13831 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013832
James Smartbf086112011-08-21 21:48:13 -040013833 if (did == Fabric_DID)
13834 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013835 if ((phba->pport->fc_flag & FC_PT2PT) &&
13836 !(phba->link_state == LPFC_HBA_READY))
13837 return phba->pport;
13838
James Smart4f774512009-05-22 14:52:35 -040013839 vports = lpfc_create_vport_work_array(phba);
13840 if (vports != NULL)
13841 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13842 if (phba->fcf.fcfi == fcfi &&
13843 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13844 vports[i]->fc_myDID == did) {
13845 vport = vports[i];
13846 break;
13847 }
13848 }
13849 lpfc_destroy_vport_work_array(phba, vports);
13850 return vport;
13851}
13852
13853/**
James Smart45ed1192009-10-02 15:17:02 -040013854 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13855 * @vport: The vport to work on.
13856 *
13857 * This function updates the receive sequence time stamp for this vport. The
13858 * receive sequence time stamp indicates the time that the last frame of the
13859 * the sequence that has been idle for the longest amount of time was received.
13860 * the driver uses this time stamp to indicate if any received sequences have
13861 * timed out.
13862 **/
13863void
13864lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13865{
13866 struct lpfc_dmabuf *h_buf;
13867 struct hbq_dmabuf *dmabuf = NULL;
13868
13869 /* get the oldest sequence on the rcv list */
13870 h_buf = list_get_first(&vport->rcv_buffer_list,
13871 struct lpfc_dmabuf, list);
13872 if (!h_buf)
13873 return;
13874 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13875 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13876}
13877
13878/**
13879 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13880 * @vport: The vport that the received sequences were sent to.
13881 *
13882 * This function cleans up all outstanding received sequences. This is called
13883 * by the driver when a link event or user action invalidates all the received
13884 * sequences.
13885 **/
13886void
13887lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13888{
13889 struct lpfc_dmabuf *h_buf, *hnext;
13890 struct lpfc_dmabuf *d_buf, *dnext;
13891 struct hbq_dmabuf *dmabuf = NULL;
13892
13893 /* start with the oldest sequence on the rcv list */
13894 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13895 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13896 list_del_init(&dmabuf->hbuf.list);
13897 list_for_each_entry_safe(d_buf, dnext,
13898 &dmabuf->dbuf.list, list) {
13899 list_del_init(&d_buf->list);
13900 lpfc_in_buf_free(vport->phba, d_buf);
13901 }
13902 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13903 }
13904}
13905
13906/**
13907 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13908 * @vport: The vport that the received sequences were sent to.
13909 *
13910 * This function determines whether any received sequences have timed out by
13911 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13912 * indicates that there is at least one timed out sequence this routine will
13913 * go through the received sequences one at a time from most inactive to most
13914 * active to determine which ones need to be cleaned up. Once it has determined
13915 * that a sequence needs to be cleaned up it will simply free up the resources
13916 * without sending an abort.
13917 **/
13918void
13919lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13920{
13921 struct lpfc_dmabuf *h_buf, *hnext;
13922 struct lpfc_dmabuf *d_buf, *dnext;
13923 struct hbq_dmabuf *dmabuf = NULL;
13924 unsigned long timeout;
13925 int abort_count = 0;
13926
13927 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13928 vport->rcv_buffer_time_stamp);
13929 if (list_empty(&vport->rcv_buffer_list) ||
13930 time_before(jiffies, timeout))
13931 return;
13932 /* start with the oldest sequence on the rcv list */
13933 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13934 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13935 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13936 dmabuf->time_stamp);
13937 if (time_before(jiffies, timeout))
13938 break;
13939 abort_count++;
13940 list_del_init(&dmabuf->hbuf.list);
13941 list_for_each_entry_safe(d_buf, dnext,
13942 &dmabuf->dbuf.list, list) {
13943 list_del_init(&d_buf->list);
13944 lpfc_in_buf_free(vport->phba, d_buf);
13945 }
13946 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13947 }
13948 if (abort_count)
13949 lpfc_update_rcv_time_stamp(vport);
13950}
13951
13952/**
James Smart4f774512009-05-22 14:52:35 -040013953 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13954 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13955 *
13956 * This function searches through the existing incomplete sequences that have
13957 * been sent to this @vport. If the frame matches one of the incomplete
13958 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13959 * make up that sequence. If no sequence is found that matches this frame then
13960 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13961 * This function returns a pointer to the first dmabuf in the sequence list that
13962 * the frame was linked to.
13963 **/
13964static struct hbq_dmabuf *
13965lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13966{
13967 struct fc_frame_header *new_hdr;
13968 struct fc_frame_header *temp_hdr;
13969 struct lpfc_dmabuf *d_buf;
13970 struct lpfc_dmabuf *h_buf;
13971 struct hbq_dmabuf *seq_dmabuf = NULL;
13972 struct hbq_dmabuf *temp_dmabuf = NULL;
13973
James Smart4d9ab992009-10-02 15:16:39 -040013974 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013975 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013976 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13977 /* Use the hdr_buf to find the sequence that this frame belongs to */
13978 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13979 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13980 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13981 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13982 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13983 continue;
13984 /* found a pending sequence that matches this frame */
13985 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13986 break;
13987 }
13988 if (!seq_dmabuf) {
13989 /*
13990 * This indicates first frame received for this sequence.
13991 * Queue the buffer on the vport's rcv_buffer_list.
13992 */
13993 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013994 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013995 return dmabuf;
13996 }
13997 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013998 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13999 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040014000 list_del_init(&seq_dmabuf->hbuf.list);
14001 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14002 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014003 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014004 return dmabuf;
14005 }
James Smart45ed1192009-10-02 15:17:02 -040014006 /* move this sequence to the tail to indicate a young sequence */
14007 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14008 seq_dmabuf->time_stamp = jiffies;
14009 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014010 if (list_empty(&seq_dmabuf->dbuf.list)) {
14011 temp_hdr = dmabuf->hbuf.virt;
14012 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14013 return seq_dmabuf;
14014 }
James Smart4f774512009-05-22 14:52:35 -040014015 /* find the correct place in the sequence to insert this frame */
14016 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14017 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14018 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14019 /*
14020 * If the frame's sequence count is greater than the frame on
14021 * the list then insert the frame right after this frame
14022 */
James Smarteeead812009-12-21 17:01:23 -050014023 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14024 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014025 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14026 return seq_dmabuf;
14027 }
14028 }
14029 return NULL;
14030}
14031
14032/**
James Smart6669f9b2009-10-02 15:16:45 -040014033 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14034 * @vport: pointer to a vitural port
14035 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14036 *
14037 * This function tries to abort from the partially assembed sequence, described
14038 * by the information from basic abbort @dmabuf. It checks to see whether such
14039 * partially assembled sequence held by the driver. If so, it shall free up all
14040 * the frames from the partially assembled sequence.
14041 *
14042 * Return
14043 * true -- if there is matching partially assembled sequence present and all
14044 * the frames freed with the sequence;
14045 * false -- if there is no matching partially assembled sequence present so
14046 * nothing got aborted in the lower layer driver
14047 **/
14048static bool
14049lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14050 struct hbq_dmabuf *dmabuf)
14051{
14052 struct fc_frame_header *new_hdr;
14053 struct fc_frame_header *temp_hdr;
14054 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14055 struct hbq_dmabuf *seq_dmabuf = NULL;
14056
14057 /* Use the hdr_buf to find the sequence that matches this frame */
14058 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14059 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14060 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14061 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14062 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14063 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14064 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14065 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14066 continue;
14067 /* found a pending sequence that matches this frame */
14068 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14069 break;
14070 }
14071
14072 /* Free up all the frames from the partially assembled sequence */
14073 if (seq_dmabuf) {
14074 list_for_each_entry_safe(d_buf, n_buf,
14075 &seq_dmabuf->dbuf.list, list) {
14076 list_del_init(&d_buf->list);
14077 lpfc_in_buf_free(vport->phba, d_buf);
14078 }
14079 return true;
14080 }
14081 return false;
14082}
14083
14084/**
James Smart546fc852011-03-11 16:06:29 -050014085 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014086 * @phba: Pointer to HBA context object.
14087 * @cmd_iocbq: pointer to the command iocbq structure.
14088 * @rsp_iocbq: pointer to the response iocbq structure.
14089 *
James Smart546fc852011-03-11 16:06:29 -050014090 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014091 * event. It properly releases the memory allocated to the sequence abort
14092 * accept iocb.
14093 **/
14094static void
James Smart546fc852011-03-11 16:06:29 -050014095lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014096 struct lpfc_iocbq *cmd_iocbq,
14097 struct lpfc_iocbq *rsp_iocbq)
14098{
14099 if (cmd_iocbq)
14100 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014101
14102 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14103 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14105 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14106 rsp_iocbq->iocb.ulpStatus,
14107 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014108}
14109
14110/**
James Smart6d368e52011-05-24 11:44:12 -040014111 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14112 * @phba: Pointer to HBA context object.
14113 * @xri: xri id in transaction.
14114 *
14115 * This function validates the xri maps to the known range of XRIs allocated an
14116 * used by the driver.
14117 **/
James Smart7851fe22011-07-22 18:36:52 -040014118uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014119lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14120 uint16_t xri)
14121{
14122 int i;
14123
14124 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14125 if (xri == phba->sli4_hba.xri_ids[i])
14126 return i;
14127 }
14128 return NO_XRI;
14129}
14130
James Smart6d368e52011-05-24 11:44:12 -040014131/**
James Smart546fc852011-03-11 16:06:29 -050014132 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014133 * @phba: Pointer to HBA context object.
14134 * @fc_hdr: pointer to a FC frame header.
14135 *
James Smart546fc852011-03-11 16:06:29 -050014136 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014137 * event after aborting the sequence handling.
14138 **/
14139static void
James Smart546fc852011-03-11 16:06:29 -050014140lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014141 struct fc_frame_header *fc_hdr)
14142{
14143 struct lpfc_iocbq *ctiocb = NULL;
14144 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014145 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014146 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014147 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014148 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014149
14150 if (!lpfc_is_link_up(phba))
14151 return;
14152
14153 sid = sli4_sid_from_fc_hdr(fc_hdr);
14154 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014155 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014156
14157 ndlp = lpfc_findnode_did(phba->pport, sid);
14158 if (!ndlp) {
14159 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14160 "1268 Find ndlp returned NULL for oxid:x%x "
14161 "SID:x%x\n", oxid, sid);
14162 return;
14163 }
14164
James Smart546fc852011-03-11 16:06:29 -050014165 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014166 ctiocb = lpfc_sli_get_iocbq(phba);
14167 if (!ctiocb)
14168 return;
14169
James Smart5ffc2662009-11-18 15:39:44 -050014170 /* Extract the F_CTL field from FC_HDR */
14171 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14172
James Smart6669f9b2009-10-02 15:16:45 -040014173 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014174 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014175 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014176 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14177 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14178 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14179
14180 /* Fill in the rest of iocb fields */
14181 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14182 icmd->ulpBdeCount = 0;
14183 icmd->ulpLe = 1;
14184 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014185 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014186 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014187
James Smart6669f9b2009-10-02 15:16:45 -040014188 ctiocb->iocb_cmpl = NULL;
14189 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014190 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014191 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014192 ctiocb->sli4_xritag = NO_XRI;
14193
James Smartee0f4fe2012-05-09 21:19:14 -040014194 if (fctl & FC_FC_EX_CTX)
14195 /* Exchange responder sent the abort so we
14196 * own the oxid.
14197 */
14198 xri = oxid;
14199 else
14200 xri = rxid;
14201 lxri = lpfc_sli4_xri_inrange(phba, xri);
14202 if (lxri != NO_XRI)
14203 lpfc_set_rrq_active(phba, ndlp, lxri,
14204 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014205 /* If the oxid maps to the FCP XRI range or if it is out of range,
14206 * send a BLS_RJT. The driver no longer has that exchange.
14207 * Override the IOCB for a BA_RJT.
14208 */
James Smartee0f4fe2012-05-09 21:19:14 -040014209 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014210 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014211 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014212 phba->sli4_hba.max_cfg_param.xri_base)) {
14213 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14214 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14215 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14216 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14217 }
James Smart6669f9b2009-10-02 15:16:45 -040014218
James Smart5ffc2662009-11-18 15:39:44 -050014219 if (fctl & FC_FC_EX_CTX) {
14220 /* ABTS sent by responder to CT exchange, construction
14221 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14222 * field and RX_ID from ABTS for RX_ID field.
14223 */
James Smart546fc852011-03-11 16:06:29 -050014224 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014225 } else {
14226 /* ABTS sent by initiator to CT exchange, construction
14227 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014228 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014229 */
James Smart546fc852011-03-11 16:06:29 -050014230 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014231 }
James Smartf09c3ac2012-03-01 22:33:29 -050014232 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014233 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014234
James Smart546fc852011-03-11 16:06:29 -050014235 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014236 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014237 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14238 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14239
14240 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14241 if (rc == IOCB_ERROR) {
14242 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14243 "2925 Failed to issue CT ABTS RSP x%x on "
14244 "xri x%x, Data x%x\n",
14245 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14246 phba->link_state);
14247 lpfc_sli_release_iocbq(phba, ctiocb);
14248 }
James Smart6669f9b2009-10-02 15:16:45 -040014249}
14250
14251/**
14252 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14253 * @vport: Pointer to the vport on which this sequence was received
14254 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14255 *
14256 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14257 * receive sequence is only partially assembed by the driver, it shall abort
14258 * the partially assembled frames for the sequence. Otherwise, if the
14259 * unsolicited receive sequence has been completely assembled and passed to
14260 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14261 * unsolicited sequence has been aborted. After that, it will issue a basic
14262 * accept to accept the abort.
14263 **/
14264void
14265lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14266 struct hbq_dmabuf *dmabuf)
14267{
14268 struct lpfc_hba *phba = vport->phba;
14269 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014270 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014271 bool abts_par;
14272
James Smart6669f9b2009-10-02 15:16:45 -040014273 /* Make a copy of fc_hdr before the dmabuf being released */
14274 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014275 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014276
James Smart5ffc2662009-11-18 15:39:44 -050014277 if (fctl & FC_FC_EX_CTX) {
14278 /*
14279 * ABTS sent by responder to exchange, just free the buffer
14280 */
James Smart6669f9b2009-10-02 15:16:45 -040014281 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014282 } else {
14283 /*
14284 * ABTS sent by initiator to exchange, need to do cleanup
14285 */
14286 /* Try to abort partially assembled seq */
14287 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14288
14289 /* Send abort to ULP if partially seq abort failed */
14290 if (abts_par == false)
14291 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14292 else
14293 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14294 }
James Smart6669f9b2009-10-02 15:16:45 -040014295 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014296 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014297}
14298
14299/**
James Smart4f774512009-05-22 14:52:35 -040014300 * lpfc_seq_complete - Indicates if a sequence is complete
14301 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14302 *
14303 * This function checks the sequence, starting with the frame described by
14304 * @dmabuf, to see if all the frames associated with this sequence are present.
14305 * the frames associated with this sequence are linked to the @dmabuf using the
14306 * dbuf list. This function looks for two major things. 1) That the first frame
14307 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14308 * set. 3) That there are no holes in the sequence count. The function will
14309 * return 1 when the sequence is complete, otherwise it will return 0.
14310 **/
14311static int
14312lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14313{
14314 struct fc_frame_header *hdr;
14315 struct lpfc_dmabuf *d_buf;
14316 struct hbq_dmabuf *seq_dmabuf;
14317 uint32_t fctl;
14318 int seq_count = 0;
14319
14320 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14321 /* make sure first fame of sequence has a sequence count of zero */
14322 if (hdr->fh_seq_cnt != seq_count)
14323 return 0;
14324 fctl = (hdr->fh_f_ctl[0] << 16 |
14325 hdr->fh_f_ctl[1] << 8 |
14326 hdr->fh_f_ctl[2]);
14327 /* If last frame of sequence we can return success. */
14328 if (fctl & FC_FC_END_SEQ)
14329 return 1;
14330 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14331 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14332 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14333 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014334 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014335 return 0;
14336 fctl = (hdr->fh_f_ctl[0] << 16 |
14337 hdr->fh_f_ctl[1] << 8 |
14338 hdr->fh_f_ctl[2]);
14339 /* If last frame of sequence we can return success. */
14340 if (fctl & FC_FC_END_SEQ)
14341 return 1;
14342 }
14343 return 0;
14344}
14345
14346/**
14347 * lpfc_prep_seq - Prep sequence for ULP processing
14348 * @vport: Pointer to the vport on which this sequence was received
14349 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14350 *
14351 * This function takes a sequence, described by a list of frames, and creates
14352 * a list of iocbq structures to describe the sequence. This iocbq list will be
14353 * used to issue to the generic unsolicited sequence handler. This routine
14354 * returns a pointer to the first iocbq in the list. If the function is unable
14355 * to allocate an iocbq then it throw out the received frames that were not
14356 * able to be described and return a pointer to the first iocbq. If unable to
14357 * allocate any iocbqs (including the first) this function will return NULL.
14358 **/
14359static struct lpfc_iocbq *
14360lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14361{
James Smart7851fe22011-07-22 18:36:52 -040014362 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014363 struct lpfc_dmabuf *d_buf, *n_buf;
14364 struct lpfc_iocbq *first_iocbq, *iocbq;
14365 struct fc_frame_header *fc_hdr;
14366 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014367 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014368 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014369
14370 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14371 /* remove from receive buffer list */
14372 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014373 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014374 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014375 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014376 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014377 /* Get an iocbq struct to fill in. */
14378 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14379 if (first_iocbq) {
14380 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014381 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014382 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014383
14384 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14385 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14386 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14387 first_iocbq->iocb.un.rcvels.parmRo =
14388 sli4_did_from_fc_hdr(fc_hdr);
14389 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14390 } else
14391 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014392 first_iocbq->iocb.ulpContext = NO_XRI;
14393 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14394 be16_to_cpu(fc_hdr->fh_ox_id);
14395 /* iocbq is prepped for internal consumption. Physical vpi. */
14396 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14397 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014398 /* put the first buffer into the first IOCBq */
14399 first_iocbq->context2 = &seq_dmabuf->dbuf;
14400 first_iocbq->context3 = NULL;
14401 first_iocbq->iocb.ulpBdeCount = 1;
14402 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14403 LPFC_DATA_BUF_SIZE;
14404 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014405 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014406 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014407 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014408 }
14409 iocbq = first_iocbq;
14410 /*
14411 * Each IOCBq can have two Buffers assigned, so go through the list
14412 * of buffers for this sequence and save two buffers in each IOCBq
14413 */
14414 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14415 if (!iocbq) {
14416 lpfc_in_buf_free(vport->phba, d_buf);
14417 continue;
14418 }
14419 if (!iocbq->context3) {
14420 iocbq->context3 = d_buf;
14421 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014422 pbde = (struct ulp_bde64 *)
14423 &iocbq->iocb.unsli3.sli3Words[4];
14424 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014425
14426 /* We need to get the size out of the right CQE */
14427 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14428 len = bf_get(lpfc_rcqe_length,
14429 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14430 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14431 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014432 } else {
14433 iocbq = lpfc_sli_get_iocbq(vport->phba);
14434 if (!iocbq) {
14435 if (first_iocbq) {
14436 first_iocbq->iocb.ulpStatus =
14437 IOSTAT_FCP_RSP_ERROR;
14438 first_iocbq->iocb.un.ulpWord[4] =
14439 IOERR_NO_RESOURCES;
14440 }
14441 lpfc_in_buf_free(vport->phba, d_buf);
14442 continue;
14443 }
14444 iocbq->context2 = d_buf;
14445 iocbq->context3 = NULL;
14446 iocbq->iocb.ulpBdeCount = 1;
14447 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14448 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014449
14450 /* We need to get the size out of the right CQE */
14451 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14452 len = bf_get(lpfc_rcqe_length,
14453 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14454 tot_len += len;
14455 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14456
James Smart4f774512009-05-22 14:52:35 -040014457 iocbq->iocb.un.rcvels.remoteID = sid;
14458 list_add_tail(&iocbq->list, &first_iocbq->list);
14459 }
14460 }
14461 return first_iocbq;
14462}
14463
James Smart6669f9b2009-10-02 15:16:45 -040014464static void
14465lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14466 struct hbq_dmabuf *seq_dmabuf)
14467{
14468 struct fc_frame_header *fc_hdr;
14469 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14470 struct lpfc_hba *phba = vport->phba;
14471
14472 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14473 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14474 if (!iocbq) {
14475 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14476 "2707 Ring %d handler: Failed to allocate "
14477 "iocb Rctl x%x Type x%x received\n",
14478 LPFC_ELS_RING,
14479 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14480 return;
14481 }
14482 if (!lpfc_complete_unsol_iocb(phba,
14483 &phba->sli.ring[LPFC_ELS_RING],
14484 iocbq, fc_hdr->fh_r_ctl,
14485 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014486 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014487 "2540 Ring %d handler: unexpected Rctl "
14488 "x%x Type x%x received\n",
14489 LPFC_ELS_RING,
14490 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14491
14492 /* Free iocb created in lpfc_prep_seq */
14493 list_for_each_entry_safe(curr_iocb, next_iocb,
14494 &iocbq->list, list) {
14495 list_del_init(&curr_iocb->list);
14496 lpfc_sli_release_iocbq(phba, curr_iocb);
14497 }
14498 lpfc_sli_release_iocbq(phba, iocbq);
14499}
14500
James Smart4f774512009-05-22 14:52:35 -040014501/**
14502 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14503 * @phba: Pointer to HBA context object.
14504 *
14505 * This function is called with no lock held. This function processes all
14506 * the received buffers and gives it to upper layers when a received buffer
14507 * indicates that it is the final frame in the sequence. The interrupt
14508 * service routine processes received buffers at interrupt contexts and adds
14509 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14510 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14511 * appropriate receive function when the final frame in a sequence is received.
14512 **/
James Smart4d9ab992009-10-02 15:16:39 -040014513void
14514lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14515 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014516{
James Smart4d9ab992009-10-02 15:16:39 -040014517 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014518 struct fc_frame_header *fc_hdr;
14519 struct lpfc_vport *vport;
14520 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014521 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014522
James Smart4f774512009-05-22 14:52:35 -040014523 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014524 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14525 /* check to see if this a valid type of frame */
14526 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14527 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14528 return;
14529 }
James Smart7851fe22011-07-22 18:36:52 -040014530 if ((bf_get(lpfc_cqe_code,
14531 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14532 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14533 &dmabuf->cq_event.cqe.rcqe_cmpl);
14534 else
14535 fcfi = bf_get(lpfc_rcqe_fcf_id,
14536 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014537
James Smart4d9ab992009-10-02 15:16:39 -040014538 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014539 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014540 /* throw out the frame */
14541 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14542 return;
14543 }
James Smart939723a2012-05-09 21:19:03 -040014544
14545 /* d_id this frame is directed to */
14546 did = sli4_did_from_fc_hdr(fc_hdr);
14547
14548 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14549 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14550 (did != Fabric_DID)) {
14551 /*
14552 * Throw out the frame if we are not pt2pt.
14553 * The pt2pt protocol allows for discovery frames
14554 * to be received without a registered VPI.
14555 */
14556 if (!(vport->fc_flag & FC_PT2PT) ||
14557 (phba->link_state == LPFC_HBA_READY)) {
14558 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14559 return;
14560 }
14561 }
14562
James Smart6669f9b2009-10-02 15:16:45 -040014563 /* Handle the basic abort sequence (BA_ABTS) event */
14564 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14565 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14566 return;
14567 }
14568
James Smart4d9ab992009-10-02 15:16:39 -040014569 /* Link this frame */
14570 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14571 if (!seq_dmabuf) {
14572 /* unable to add frame to vport - throw it out */
14573 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14574 return;
14575 }
14576 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014577 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014578 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014579
James Smart6669f9b2009-10-02 15:16:45 -040014580 /* Send the complete sequence to the upper layer protocol */
14581 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014582}
James Smart6fb120a2009-05-22 14:52:59 -040014583
14584/**
14585 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14586 * @phba: pointer to lpfc hba data structure.
14587 *
14588 * This routine is invoked to post rpi header templates to the
14589 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014590 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14591 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014592 *
14593 * This routine does not require any locks. It's usage is expected
14594 * to be driver load or reset recovery when the driver is
14595 * sequential.
14596 *
14597 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014598 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014599 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014600 * When this error occurs, the driver is not guaranteed
14601 * to have any rpi regions posted to the device and
14602 * must either attempt to repost the regions or take a
14603 * fatal error.
14604 **/
14605int
14606lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14607{
14608 struct lpfc_rpi_hdr *rpi_page;
14609 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014610 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014611
James Smart6d368e52011-05-24 11:44:12 -040014612 /* SLI4 ports that support extents do not require RPI headers. */
14613 if (!phba->sli4_hba.rpi_hdrs_in_use)
14614 goto exit;
14615 if (phba->sli4_hba.extents_in_use)
14616 return -EIO;
14617
James Smart6fb120a2009-05-22 14:52:59 -040014618 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014619 /*
14620 * Assign the rpi headers a physical rpi only if the driver
14621 * has not initialized those resources. A port reset only
14622 * needs the headers posted.
14623 */
14624 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14625 LPFC_RPI_RSRC_RDY)
14626 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14627
James Smart6fb120a2009-05-22 14:52:59 -040014628 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14629 if (rc != MBX_SUCCESS) {
14630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14631 "2008 Error %d posting all rpi "
14632 "headers\n", rc);
14633 rc = -EIO;
14634 break;
14635 }
14636 }
14637
James Smart6d368e52011-05-24 11:44:12 -040014638 exit:
14639 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14640 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014641 return rc;
14642}
14643
14644/**
14645 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14646 * @phba: pointer to lpfc hba data structure.
14647 * @rpi_page: pointer to the rpi memory region.
14648 *
14649 * This routine is invoked to post a single rpi header to the
14650 * HBA consistent with the SLI-4 interface spec. This memory region
14651 * maps up to 64 rpi context regions.
14652 *
14653 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014654 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014655 * -ENOMEM - No available memory
14656 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014657 **/
14658int
14659lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14660{
14661 LPFC_MBOXQ_t *mboxq;
14662 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14663 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014664 uint32_t shdr_status, shdr_add_status;
14665 union lpfc_sli4_cfg_shdr *shdr;
14666
James Smart6d368e52011-05-24 11:44:12 -040014667 /* SLI4 ports that support extents do not require RPI headers. */
14668 if (!phba->sli4_hba.rpi_hdrs_in_use)
14669 return rc;
14670 if (phba->sli4_hba.extents_in_use)
14671 return -EIO;
14672
James Smart6fb120a2009-05-22 14:52:59 -040014673 /* The port is notified of the header region via a mailbox command. */
14674 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14675 if (!mboxq) {
14676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14677 "2001 Unable to allocate memory for issuing "
14678 "SLI_CONFIG_SPECIAL mailbox command\n");
14679 return -ENOMEM;
14680 }
14681
14682 /* Post all rpi memory regions to the port. */
14683 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014684 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14685 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14686 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014687 sizeof(struct lpfc_sli4_cfg_mhdr),
14688 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014689
14690
14691 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014692 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14693 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014694 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14695 hdr_tmpl, rpi_page->page_count);
14696
James Smart6fb120a2009-05-22 14:52:59 -040014697 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14698 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014699 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014700 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14701 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14702 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14703 if (rc != MBX_TIMEOUT)
14704 mempool_free(mboxq, phba->mbox_mem_pool);
14705 if (shdr_status || shdr_add_status || rc) {
14706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14707 "2514 POST_RPI_HDR mailbox failed with "
14708 "status x%x add_status x%x, mbx status x%x\n",
14709 shdr_status, shdr_add_status, rc);
14710 rc = -ENXIO;
14711 }
14712 return rc;
14713}
14714
14715/**
14716 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14717 * @phba: pointer to lpfc hba data structure.
14718 *
14719 * This routine is invoked to post rpi header templates to the
14720 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014721 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14722 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014723 *
14724 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014725 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014726 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14727 **/
14728int
14729lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14730{
James Smart6d368e52011-05-24 11:44:12 -040014731 unsigned long rpi;
14732 uint16_t max_rpi, rpi_limit;
14733 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014734 struct lpfc_rpi_hdr *rpi_hdr;
14735
14736 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014737 rpi_limit = phba->sli4_hba.next_rpi;
14738
14739 /*
James Smart6d368e52011-05-24 11:44:12 -040014740 * Fetch the next logical rpi. Because this index is logical,
14741 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014742 */
14743 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014744 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14745 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014746 rpi = LPFC_RPI_ALLOC_ERROR;
14747 else {
14748 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14749 phba->sli4_hba.max_cfg_param.rpi_used++;
14750 phba->sli4_hba.rpi_count++;
14751 }
14752
14753 /*
14754 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014755 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014756 */
14757 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14758 (phba->sli4_hba.rpi_count >= max_rpi)) {
14759 spin_unlock_irq(&phba->hbalock);
14760 return rpi;
14761 }
14762
14763 /*
James Smart6d368e52011-05-24 11:44:12 -040014764 * RPI header postings are not required for SLI4 ports capable of
14765 * extents.
14766 */
14767 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14768 spin_unlock_irq(&phba->hbalock);
14769 return rpi;
14770 }
14771
14772 /*
James Smart6fb120a2009-05-22 14:52:59 -040014773 * If the driver is running low on rpi resources, allocate another
14774 * page now. Note that the next_rpi value is used because
14775 * it represents how many are actually in use whereas max_rpi notes
14776 * how many are supported max by the device.
14777 */
James Smart6d368e52011-05-24 11:44:12 -040014778 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014779 spin_unlock_irq(&phba->hbalock);
14780 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14781 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14782 if (!rpi_hdr) {
14783 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14784 "2002 Error Could not grow rpi "
14785 "count\n");
14786 } else {
James Smart6d368e52011-05-24 11:44:12 -040014787 lrpi = rpi_hdr->start_rpi;
14788 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014789 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14790 }
14791 }
14792
14793 return rpi;
14794}
14795
14796/**
14797 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14798 * @phba: pointer to lpfc hba data structure.
14799 *
14800 * This routine is invoked to release an rpi to the pool of
14801 * available rpis maintained by the driver.
14802 **/
14803void
James Smartd7c47992010-06-08 18:31:54 -040014804__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14805{
14806 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14807 phba->sli4_hba.rpi_count--;
14808 phba->sli4_hba.max_cfg_param.rpi_used--;
14809 }
14810}
14811
14812/**
14813 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14814 * @phba: pointer to lpfc hba data structure.
14815 *
14816 * This routine is invoked to release an rpi to the pool of
14817 * available rpis maintained by the driver.
14818 **/
14819void
James Smart6fb120a2009-05-22 14:52:59 -040014820lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14821{
14822 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014823 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014824 spin_unlock_irq(&phba->hbalock);
14825}
14826
14827/**
14828 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14829 * @phba: pointer to lpfc hba data structure.
14830 *
14831 * This routine is invoked to remove the memory region that
14832 * provided rpi via a bitmask.
14833 **/
14834void
14835lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14836{
14837 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014838 kfree(phba->sli4_hba.rpi_ids);
14839 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014840}
14841
14842/**
14843 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14844 * @phba: pointer to lpfc hba data structure.
14845 *
14846 * This routine is invoked to remove the memory region that
14847 * provided rpi via a bitmask.
14848 **/
14849int
James Smart6b5151f2012-01-18 16:24:06 -050014850lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14851 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014852{
14853 LPFC_MBOXQ_t *mboxq;
14854 struct lpfc_hba *phba = ndlp->phba;
14855 int rc;
14856
14857 /* The port is notified of the header region via a mailbox command. */
14858 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14859 if (!mboxq)
14860 return -ENOMEM;
14861
14862 /* Post all rpi memory regions to the port. */
14863 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014864 if (cmpl) {
14865 mboxq->mbox_cmpl = cmpl;
14866 mboxq->context1 = arg;
14867 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014868 } else
14869 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014870 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014871 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14872 if (rc == MBX_NOT_FINISHED) {
14873 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14874 "2010 Resume RPI Mailbox failed "
14875 "status %d, mbxStatus x%x\n", rc,
14876 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14877 mempool_free(mboxq, phba->mbox_mem_pool);
14878 return -EIO;
14879 }
14880 return 0;
14881}
14882
14883/**
14884 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014885 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014886 *
James Smart76a95d72010-11-20 23:11:48 -050014887 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014888 *
14889 * Returns:
14890 * 0 success
14891 * -Evalue otherwise
14892 **/
14893int
James Smart76a95d72010-11-20 23:11:48 -050014894lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014895{
14896 LPFC_MBOXQ_t *mboxq;
14897 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014898 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014899 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014900 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014901 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14902 if (!mboxq)
14903 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014904 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014905 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014906 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014907 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014908 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014909 "2022 INIT VPI Mailbox failed "
14910 "status %d, mbxStatus x%x\n", rc,
14911 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014912 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014913 }
James Smart6a9c52c2009-10-02 15:16:51 -040014914 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014915 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014916
14917 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014918}
14919
14920/**
14921 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14922 * @phba: pointer to lpfc hba data structure.
14923 * @mboxq: Pointer to mailbox object.
14924 *
14925 * This routine is invoked to manually add a single FCF record. The caller
14926 * must pass a completely initialized FCF_Record. This routine takes
14927 * care of the nonembedded mailbox operations.
14928 **/
14929static void
14930lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14931{
14932 void *virt_addr;
14933 union lpfc_sli4_cfg_shdr *shdr;
14934 uint32_t shdr_status, shdr_add_status;
14935
14936 virt_addr = mboxq->sge_array->addr[0];
14937 /* The IOCTL status is embedded in the mailbox subheader. */
14938 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14939 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14940 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14941
14942 if ((shdr_status || shdr_add_status) &&
14943 (shdr_status != STATUS_FCF_IN_USE))
14944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14945 "2558 ADD_FCF_RECORD mailbox failed with "
14946 "status x%x add_status x%x\n",
14947 shdr_status, shdr_add_status);
14948
14949 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14950}
14951
14952/**
14953 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14954 * @phba: pointer to lpfc hba data structure.
14955 * @fcf_record: pointer to the initialized fcf record to add.
14956 *
14957 * This routine is invoked to manually add a single FCF record. The caller
14958 * must pass a completely initialized FCF_Record. This routine takes
14959 * care of the nonembedded mailbox operations.
14960 **/
14961int
14962lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14963{
14964 int rc = 0;
14965 LPFC_MBOXQ_t *mboxq;
14966 uint8_t *bytep;
14967 void *virt_addr;
14968 dma_addr_t phys_addr;
14969 struct lpfc_mbx_sge sge;
14970 uint32_t alloc_len, req_len;
14971 uint32_t fcfindex;
14972
14973 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14974 if (!mboxq) {
14975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14976 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14977 return -ENOMEM;
14978 }
14979
14980 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14981 sizeof(uint32_t);
14982
14983 /* Allocate DMA memory and set up the non-embedded mailbox command */
14984 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14985 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14986 req_len, LPFC_SLI4_MBX_NEMBED);
14987 if (alloc_len < req_len) {
14988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14989 "2523 Allocated DMA memory size (x%x) is "
14990 "less than the requested DMA memory "
14991 "size (x%x)\n", alloc_len, req_len);
14992 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14993 return -ENOMEM;
14994 }
14995
14996 /*
14997 * Get the first SGE entry from the non-embedded DMA memory. This
14998 * routine only uses a single SGE.
14999 */
15000 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15001 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040015002 virt_addr = mboxq->sge_array->addr[0];
15003 /*
15004 * Configure the FCF record for FCFI 0. This is the driver's
15005 * hardcoded default and gets used in nonFIP mode.
15006 */
15007 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15008 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15009 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15010
15011 /*
15012 * Copy the fcf_index and the FCF Record Data. The data starts after
15013 * the FCoE header plus word10. The data copy needs to be endian
15014 * correct.
15015 */
15016 bytep += sizeof(uint32_t);
15017 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15018 mboxq->vport = phba->pport;
15019 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15020 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15021 if (rc == MBX_NOT_FINISHED) {
15022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15023 "2515 ADD_FCF_RECORD mailbox failed with "
15024 "status 0x%x\n", rc);
15025 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15026 rc = -EIO;
15027 } else
15028 rc = 0;
15029
15030 return rc;
15031}
15032
15033/**
15034 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15035 * @phba: pointer to lpfc hba data structure.
15036 * @fcf_record: pointer to the fcf record to write the default data.
15037 * @fcf_index: FCF table entry index.
15038 *
15039 * This routine is invoked to build the driver's default FCF record. The
15040 * values used are hardcoded. This routine handles memory initialization.
15041 *
15042 **/
15043void
15044lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15045 struct fcf_record *fcf_record,
15046 uint16_t fcf_index)
15047{
15048 memset(fcf_record, 0, sizeof(struct fcf_record));
15049 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15050 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15051 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15052 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15053 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15054 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15055 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15056 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15057 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15058 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15059 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15060 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15061 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015062 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015063 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15064 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15065 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15066 /* Set the VLAN bit map */
15067 if (phba->valid_vlan) {
15068 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15069 = 1 << (phba->vlan_id % 8);
15070 }
15071}
15072
15073/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015074 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015075 * @phba: pointer to lpfc hba data structure.
15076 * @fcf_index: FCF table entry offset.
15077 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015078 * This routine is invoked to scan the entire FCF table by reading FCF
15079 * record and processing it one at a time starting from the @fcf_index
15080 * for initial FCF discovery or fast FCF failover rediscovery.
15081 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015082 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015083 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015084 **/
15085int
James Smart0c9ab6f2010-02-26 14:15:57 -050015086lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015087{
15088 int rc = 0, error;
15089 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015090
James Smart32b97932009-07-19 10:01:21 -040015091 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015092 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015093 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15094 if (!mboxq) {
15095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15096 "2000 Failed to allocate mbox for "
15097 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015098 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015099 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015100 }
James Smartecfd03c2010-02-12 14:41:27 -050015101 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015102 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015103 if (rc) {
15104 error = -EINVAL;
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 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015108 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015109 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015110
15111 spin_lock_irq(&phba->hbalock);
15112 phba->hba_flag |= FCF_TS_INPROG;
15113 spin_unlock_irq(&phba->hbalock);
15114
James Smart6fb120a2009-05-22 14:52:59 -040015115 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015116 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015117 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015118 else {
James Smart38b92ef2010-08-04 16:11:39 -040015119 /* Reset eligible FCF count for new scan */
15120 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015121 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015122 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015123 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015124fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015125 if (error) {
15126 if (mboxq)
15127 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015128 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015129 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015130 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015131 spin_unlock_irq(&phba->hbalock);
15132 }
James Smart6fb120a2009-05-22 14:52:59 -040015133 return error;
15134}
James Smarta0c87cb2009-07-19 10:01:10 -040015135
15136/**
James Smarta93ff372010-10-22 11:06:08 -040015137 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015138 * @phba: pointer to lpfc hba data structure.
15139 * @fcf_index: FCF table entry offset.
15140 *
15141 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015142 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015143 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015144 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015145 * otherwise.
15146 **/
15147int
15148lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15149{
15150 int rc = 0, error;
15151 LPFC_MBOXQ_t *mboxq;
15152
15153 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15154 if (!mboxq) {
15155 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15156 "2763 Failed to allocate mbox for "
15157 "READ_FCF cmd\n");
15158 error = -ENOMEM;
15159 goto fail_fcf_read;
15160 }
15161 /* Construct the read FCF record mailbox command */
15162 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15163 if (rc) {
15164 error = -EINVAL;
15165 goto fail_fcf_read;
15166 }
15167 /* Issue the mailbox command asynchronously */
15168 mboxq->vport = phba->pport;
15169 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15170 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15171 if (rc == MBX_NOT_FINISHED)
15172 error = -EIO;
15173 else
15174 error = 0;
15175
15176fail_fcf_read:
15177 if (error && mboxq)
15178 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15179 return error;
15180}
15181
15182/**
15183 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15184 * @phba: pointer to lpfc hba data structure.
15185 * @fcf_index: FCF table entry offset.
15186 *
15187 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015188 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015189 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015190 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015191 * otherwise.
15192 **/
15193int
15194lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15195{
15196 int rc = 0, error;
15197 LPFC_MBOXQ_t *mboxq;
15198
15199 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15200 if (!mboxq) {
15201 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15202 "2758 Failed to allocate mbox for "
15203 "READ_FCF cmd\n");
15204 error = -ENOMEM;
15205 goto fail_fcf_read;
15206 }
15207 /* Construct the read FCF record mailbox command */
15208 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15209 if (rc) {
15210 error = -EINVAL;
15211 goto fail_fcf_read;
15212 }
15213 /* Issue the mailbox command asynchronously */
15214 mboxq->vport = phba->pport;
15215 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15216 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15217 if (rc == MBX_NOT_FINISHED)
15218 error = -EIO;
15219 else
15220 error = 0;
15221
15222fail_fcf_read:
15223 if (error && mboxq)
15224 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15225 return error;
15226}
15227
15228/**
James Smart7d791df2011-07-22 18:37:52 -040015229 * lpfc_check_next_fcf_pri
15230 * phba pointer to the lpfc_hba struct for this port.
15231 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15232 * routine when the rr_bmask is empty. The FCF indecies are put into the
15233 * rr_bmask based on their priority level. Starting from the highest priority
15234 * to the lowest. The most likely FCF candidate will be in the highest
15235 * priority group. When this routine is called it searches the fcf_pri list for
15236 * next lowest priority group and repopulates the rr_bmask with only those
15237 * fcf_indexes.
15238 * returns:
15239 * 1=success 0=failure
15240 **/
15241int
15242lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15243{
15244 uint16_t next_fcf_pri;
15245 uint16_t last_index;
15246 struct lpfc_fcf_pri *fcf_pri;
15247 int rc;
15248 int ret = 0;
15249
15250 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15251 LPFC_SLI4_FCF_TBL_INDX_MAX);
15252 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15253 "3060 Last IDX %d\n", last_index);
15254 if (list_empty(&phba->fcf.fcf_pri_list)) {
15255 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15256 "3061 Last IDX %d\n", last_index);
15257 return 0; /* Empty rr list */
15258 }
15259 next_fcf_pri = 0;
15260 /*
15261 * Clear the rr_bmask and set all of the bits that are at this
15262 * priority.
15263 */
15264 memset(phba->fcf.fcf_rr_bmask, 0,
15265 sizeof(*phba->fcf.fcf_rr_bmask));
15266 spin_lock_irq(&phba->hbalock);
15267 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15268 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15269 continue;
15270 /*
15271 * the 1st priority that has not FLOGI failed
15272 * will be the highest.
15273 */
15274 if (!next_fcf_pri)
15275 next_fcf_pri = fcf_pri->fcf_rec.priority;
15276 spin_unlock_irq(&phba->hbalock);
15277 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15278 rc = lpfc_sli4_fcf_rr_index_set(phba,
15279 fcf_pri->fcf_rec.fcf_index);
15280 if (rc)
15281 return 0;
15282 }
15283 spin_lock_irq(&phba->hbalock);
15284 }
15285 /*
15286 * if next_fcf_pri was not set above and the list is not empty then
15287 * we have failed flogis on all of them. So reset flogi failed
15288 * and start at the begining.
15289 */
15290 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15291 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15292 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15293 /*
15294 * the 1st priority that has not FLOGI failed
15295 * will be the highest.
15296 */
15297 if (!next_fcf_pri)
15298 next_fcf_pri = fcf_pri->fcf_rec.priority;
15299 spin_unlock_irq(&phba->hbalock);
15300 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15301 rc = lpfc_sli4_fcf_rr_index_set(phba,
15302 fcf_pri->fcf_rec.fcf_index);
15303 if (rc)
15304 return 0;
15305 }
15306 spin_lock_irq(&phba->hbalock);
15307 }
15308 } else
15309 ret = 1;
15310 spin_unlock_irq(&phba->hbalock);
15311
15312 return ret;
15313}
15314/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015315 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15316 * @phba: pointer to lpfc hba data structure.
15317 *
15318 * This routine is to get the next eligible FCF record index in a round
15319 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015320 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015321 * shall be returned, otherwise, the next eligible FCF record's index
15322 * shall be returned.
15323 **/
15324uint16_t
15325lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15326{
15327 uint16_t next_fcf_index;
15328
James Smart3804dc82010-07-14 15:31:37 -040015329 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015330next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015331 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15332 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015333 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15334 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015335 next_fcf_index);
15336
James Smart0c9ab6f2010-02-26 14:15:57 -050015337 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015338 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15339 /*
15340 * If we have wrapped then we need to clear the bits that
15341 * have been tested so that we can detect when we should
15342 * change the priority level.
15343 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015344 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15345 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015346 }
15347
James Smart0c9ab6f2010-02-26 14:15:57 -050015348
James Smart3804dc82010-07-14 15:31:37 -040015349 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015350 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15351 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15352 /*
15353 * If next fcf index is not found check if there are lower
15354 * Priority level fcf's in the fcf_priority list.
15355 * Set up the rr_bmask with all of the avaiable fcf bits
15356 * at that level and continue the selection process.
15357 */
15358 if (lpfc_check_next_fcf_pri_level(phba))
15359 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015360 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15361 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015362 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15363 return LPFC_FCOE_FCF_NEXT_NONE;
15364 else {
15365 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15366 "3063 Only FCF available idx %d, flag %x\n",
15367 next_fcf_index,
15368 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15369 return next_fcf_index;
15370 }
James Smart3804dc82010-07-14 15:31:37 -040015371 }
15372
James Smart7d791df2011-07-22 18:37:52 -040015373 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15374 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15375 LPFC_FCF_FLOGI_FAILED)
15376 goto next_priority;
15377
James Smart3804dc82010-07-14 15:31:37 -040015378 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015379 "2845 Get next roundrobin failover FCF (x%x)\n",
15380 next_fcf_index);
15381
James Smart0c9ab6f2010-02-26 14:15:57 -050015382 return next_fcf_index;
15383}
15384
15385/**
15386 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15387 * @phba: pointer to lpfc hba data structure.
15388 *
15389 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015390 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015391 * does not go beyond the range of the driver allocated bmask dimension
15392 * before setting the bit.
15393 *
15394 * Returns 0 if the index bit successfully set, otherwise, it returns
15395 * -EINVAL.
15396 **/
15397int
15398lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15399{
15400 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15401 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015402 "2610 FCF (x%x) reached driver's book "
15403 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015404 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15405 return -EINVAL;
15406 }
15407 /* Set the eligible FCF record index bmask */
15408 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15409
James Smart3804dc82010-07-14 15:31:37 -040015410 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015411 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015412 "bmask\n", fcf_index);
15413
James Smart0c9ab6f2010-02-26 14:15:57 -050015414 return 0;
15415}
15416
15417/**
James Smart3804dc82010-07-14 15:31:37 -040015418 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015419 * @phba: pointer to lpfc hba data structure.
15420 *
15421 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015422 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015423 * does not go beyond the range of the driver allocated bmask dimension
15424 * before clearing the bit.
15425 **/
15426void
15427lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15428{
James Smart7d791df2011-07-22 18:37:52 -040015429 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015430 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15431 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015432 "2762 FCF (x%x) reached driver's book "
15433 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015434 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15435 return;
15436 }
15437 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015438 spin_lock_irq(&phba->hbalock);
15439 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15440 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15441 list_del_init(&fcf_pri->list);
15442 break;
15443 }
15444 }
15445 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015446 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015447
15448 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015449 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015450 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015451}
15452
15453/**
James Smartecfd03c2010-02-12 14:41:27 -050015454 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15455 * @phba: pointer to lpfc hba data structure.
15456 *
15457 * This routine is the completion routine for the rediscover FCF table mailbox
15458 * command. If the mailbox command returned failure, it will try to stop the
15459 * FCF rediscover wait timer.
15460 **/
15461void
15462lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15463{
15464 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15465 uint32_t shdr_status, shdr_add_status;
15466
15467 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15468
15469 shdr_status = bf_get(lpfc_mbox_hdr_status,
15470 &redisc_fcf->header.cfg_shdr.response);
15471 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15472 &redisc_fcf->header.cfg_shdr.response);
15473 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015474 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015475 "2746 Requesting for FCF rediscovery failed "
15476 "status x%x add_status x%x\n",
15477 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015478 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015479 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015480 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015481 spin_unlock_irq(&phba->hbalock);
15482 /*
15483 * CVL event triggered FCF rediscover request failed,
15484 * last resort to re-try current registered FCF entry.
15485 */
15486 lpfc_retry_pport_discovery(phba);
15487 } else {
15488 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015489 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015490 spin_unlock_irq(&phba->hbalock);
15491 /*
15492 * DEAD FCF event triggered FCF rediscover request
15493 * failed, last resort to fail over as a link down
15494 * to FCF registration.
15495 */
15496 lpfc_sli4_fcf_dead_failthrough(phba);
15497 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015498 } else {
15499 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015500 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015501 /*
15502 * Start FCF rediscovery wait timer for pending FCF
15503 * before rescan FCF record table.
15504 */
15505 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015506 }
James Smartecfd03c2010-02-12 14:41:27 -050015507
15508 mempool_free(mbox, phba->mbox_mem_pool);
15509}
15510
15511/**
James Smart3804dc82010-07-14 15:31:37 -040015512 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015513 * @phba: pointer to lpfc hba data structure.
15514 *
15515 * This routine is invoked to request for rediscovery of the entire FCF table
15516 * by the port.
15517 **/
15518int
15519lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15520{
15521 LPFC_MBOXQ_t *mbox;
15522 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15523 int rc, length;
15524
James Smart0c9ab6f2010-02-26 14:15:57 -050015525 /* Cancel retry delay timers to all vports before FCF rediscover */
15526 lpfc_cancel_all_vport_retry_delay_timer(phba);
15527
James Smartecfd03c2010-02-12 14:41:27 -050015528 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15529 if (!mbox) {
15530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15531 "2745 Failed to allocate mbox for "
15532 "requesting FCF rediscover.\n");
15533 return -ENOMEM;
15534 }
15535
15536 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15537 sizeof(struct lpfc_sli4_cfg_mhdr));
15538 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15539 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15540 length, LPFC_SLI4_MBX_EMBED);
15541
15542 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15543 /* Set count to 0 for invalidating the entire FCF database */
15544 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15545
15546 /* Issue the mailbox command asynchronously */
15547 mbox->vport = phba->pport;
15548 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15549 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15550
15551 if (rc == MBX_NOT_FINISHED) {
15552 mempool_free(mbox, phba->mbox_mem_pool);
15553 return -EIO;
15554 }
15555 return 0;
15556}
15557
15558/**
James Smartfc2b9892010-02-26 14:15:29 -050015559 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15560 * @phba: pointer to lpfc hba data structure.
15561 *
15562 * This function is the failover routine as a last resort to the FCF DEAD
15563 * event when driver failed to perform fast FCF failover.
15564 **/
15565void
15566lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15567{
15568 uint32_t link_state;
15569
15570 /*
15571 * Last resort as FCF DEAD event failover will treat this as
15572 * a link down, but save the link state because we don't want
15573 * it to be changed to Link Down unless it is already down.
15574 */
15575 link_state = phba->link_state;
15576 lpfc_linkdown(phba);
15577 phba->link_state = link_state;
15578
15579 /* Unregister FCF if no devices connected to it */
15580 lpfc_unregister_unused_fcf(phba);
15581}
15582
15583/**
James Smart026abb82011-12-13 13:20:45 -050015584 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015585 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015586 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015587 *
James Smart026abb82011-12-13 13:20:45 -050015588 * This function gets SLI3 port configure region 23 data through memory dump
15589 * mailbox command. When it successfully retrieves data, the size of the data
15590 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015591 **/
James Smart026abb82011-12-13 13:20:45 -050015592static uint32_t
15593lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015594{
15595 LPFC_MBOXQ_t *pmb = NULL;
15596 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015597 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015598 int rc;
15599
James Smart026abb82011-12-13 13:20:45 -050015600 if (!rgn23_data)
15601 return 0;
15602
James Smarta0c87cb2009-07-19 10:01:10 -040015603 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15604 if (!pmb) {
15605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015606 "2600 failed to allocate mailbox memory\n");
15607 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015608 }
15609 mb = &pmb->u.mb;
15610
James Smarta0c87cb2009-07-19 10:01:10 -040015611 do {
15612 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15613 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15614
15615 if (rc != MBX_SUCCESS) {
15616 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015617 "2601 failed to read config "
15618 "region 23, rc 0x%x Status 0x%x\n",
15619 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015620 mb->un.varDmp.word_cnt = 0;
15621 }
15622 /*
15623 * dump mem may return a zero when finished or we got a
15624 * mailbox error, either way we are done.
15625 */
15626 if (mb->un.varDmp.word_cnt == 0)
15627 break;
15628 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15629 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15630
15631 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015632 rgn23_data + offset,
15633 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015634 offset += mb->un.varDmp.word_cnt;
15635 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15636
James Smart026abb82011-12-13 13:20:45 -050015637 mempool_free(pmb, phba->mbox_mem_pool);
15638 return offset;
15639}
15640
15641/**
15642 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15643 * @phba: pointer to lpfc hba data structure.
15644 * @rgn23_data: pointer to configure region 23 data.
15645 *
15646 * This function gets SLI4 port configure region 23 data through memory dump
15647 * mailbox command. When it successfully retrieves data, the size of the data
15648 * will be returned, otherwise, 0 will be returned.
15649 **/
15650static uint32_t
15651lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15652{
15653 LPFC_MBOXQ_t *mboxq = NULL;
15654 struct lpfc_dmabuf *mp = NULL;
15655 struct lpfc_mqe *mqe;
15656 uint32_t data_length = 0;
15657 int rc;
15658
15659 if (!rgn23_data)
15660 return 0;
15661
15662 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15663 if (!mboxq) {
15664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15665 "3105 failed to allocate mailbox memory\n");
15666 return 0;
15667 }
15668
15669 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15670 goto out;
15671 mqe = &mboxq->u.mqe;
15672 mp = (struct lpfc_dmabuf *) mboxq->context1;
15673 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15674 if (rc)
15675 goto out;
15676 data_length = mqe->un.mb_words[5];
15677 if (data_length == 0)
15678 goto out;
15679 if (data_length > DMP_RGN23_SIZE) {
15680 data_length = 0;
15681 goto out;
15682 }
15683 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15684out:
15685 mempool_free(mboxq, phba->mbox_mem_pool);
15686 if (mp) {
15687 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15688 kfree(mp);
15689 }
15690 return data_length;
15691}
15692
15693/**
15694 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15695 * @phba: pointer to lpfc hba data structure.
15696 *
15697 * This function read region 23 and parse TLV for port status to
15698 * decide if the user disaled the port. If the TLV indicates the
15699 * port is disabled, the hba_flag is set accordingly.
15700 **/
15701void
15702lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15703{
15704 uint8_t *rgn23_data = NULL;
15705 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15706 uint32_t offset = 0;
15707
15708 /* Get adapter Region 23 data */
15709 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15710 if (!rgn23_data)
15711 goto out;
15712
15713 if (phba->sli_rev < LPFC_SLI_REV4)
15714 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15715 else {
15716 if_type = bf_get(lpfc_sli_intf_if_type,
15717 &phba->sli4_hba.sli_intf);
15718 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15719 goto out;
15720 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15721 }
James Smarta0c87cb2009-07-19 10:01:10 -040015722
15723 if (!data_size)
15724 goto out;
15725
15726 /* Check the region signature first */
15727 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15729 "2619 Config region 23 has bad signature\n");
15730 goto out;
15731 }
15732 offset += 4;
15733
15734 /* Check the data structure version */
15735 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15737 "2620 Config region 23 has bad version\n");
15738 goto out;
15739 }
15740 offset += 4;
15741
15742 /* Parse TLV entries in the region */
15743 while (offset < data_size) {
15744 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15745 break;
15746 /*
15747 * If the TLV is not driver specific TLV or driver id is
15748 * not linux driver id, skip the record.
15749 */
15750 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15751 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15752 (rgn23_data[offset + 3] != 0)) {
15753 offset += rgn23_data[offset + 1] * 4 + 4;
15754 continue;
15755 }
15756
15757 /* Driver found a driver specific TLV in the config region */
15758 sub_tlv_len = rgn23_data[offset + 1] * 4;
15759 offset += 4;
15760 tlv_offset = 0;
15761
15762 /*
15763 * Search for configured port state sub-TLV.
15764 */
15765 while ((offset < data_size) &&
15766 (tlv_offset < sub_tlv_len)) {
15767 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15768 offset += 4;
15769 tlv_offset += 4;
15770 break;
15771 }
15772 if (rgn23_data[offset] != PORT_STE_TYPE) {
15773 offset += rgn23_data[offset + 1] * 4 + 4;
15774 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15775 continue;
15776 }
15777
15778 /* This HBA contains PORT_STE configured */
15779 if (!rgn23_data[offset + 2])
15780 phba->hba_flag |= LINK_DISABLED;
15781
15782 goto out;
15783 }
15784 }
James Smart026abb82011-12-13 13:20:45 -050015785
James Smarta0c87cb2009-07-19 10:01:10 -040015786out:
James Smarta0c87cb2009-07-19 10:01:10 -040015787 kfree(rgn23_data);
15788 return;
15789}
James Smart695a8142010-01-26 23:08:03 -050015790
15791/**
James Smart52d52442011-05-24 11:42:45 -040015792 * lpfc_wr_object - write an object to the firmware
15793 * @phba: HBA structure that indicates port to create a queue on.
15794 * @dmabuf_list: list of dmabufs to write to the port.
15795 * @size: the total byte value of the objects to write to the port.
15796 * @offset: the current offset to be used to start the transfer.
15797 *
15798 * This routine will create a wr_object mailbox command to send to the port.
15799 * the mailbox command will be constructed using the dma buffers described in
15800 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15801 * BDEs that the imbedded mailbox can support. The @offset variable will be
15802 * used to indicate the starting offset of the transfer and will also return
15803 * the offset after the write object mailbox has completed. @size is used to
15804 * determine the end of the object and whether the eof bit should be set.
15805 *
15806 * Return 0 is successful and offset will contain the the new offset to use
15807 * for the next write.
15808 * Return negative value for error cases.
15809 **/
15810int
15811lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15812 uint32_t size, uint32_t *offset)
15813{
15814 struct lpfc_mbx_wr_object *wr_object;
15815 LPFC_MBOXQ_t *mbox;
15816 int rc = 0, i = 0;
15817 uint32_t shdr_status, shdr_add_status;
15818 uint32_t mbox_tmo;
15819 union lpfc_sli4_cfg_shdr *shdr;
15820 struct lpfc_dmabuf *dmabuf;
15821 uint32_t written = 0;
15822
15823 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15824 if (!mbox)
15825 return -ENOMEM;
15826
15827 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15828 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15829 sizeof(struct lpfc_mbx_wr_object) -
15830 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15831
15832 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15833 wr_object->u.request.write_offset = *offset;
15834 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15835 wr_object->u.request.object_name[0] =
15836 cpu_to_le32(wr_object->u.request.object_name[0]);
15837 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15838 list_for_each_entry(dmabuf, dmabuf_list, list) {
15839 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15840 break;
15841 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15842 wr_object->u.request.bde[i].addrHigh =
15843 putPaddrHigh(dmabuf->phys);
15844 if (written + SLI4_PAGE_SIZE >= size) {
15845 wr_object->u.request.bde[i].tus.f.bdeSize =
15846 (size - written);
15847 written += (size - written);
15848 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15849 } else {
15850 wr_object->u.request.bde[i].tus.f.bdeSize =
15851 SLI4_PAGE_SIZE;
15852 written += SLI4_PAGE_SIZE;
15853 }
15854 i++;
15855 }
15856 wr_object->u.request.bde_count = i;
15857 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15858 if (!phba->sli4_hba.intr_enable)
15859 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15860 else {
James Smarta183a152011-10-10 21:32:43 -040015861 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015862 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15863 }
15864 /* The IOCTL status is embedded in the mailbox subheader. */
15865 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15866 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15867 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15868 if (rc != MBX_TIMEOUT)
15869 mempool_free(mbox, phba->mbox_mem_pool);
15870 if (shdr_status || shdr_add_status || rc) {
15871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15872 "3025 Write Object mailbox failed with "
15873 "status x%x add_status x%x, mbx status x%x\n",
15874 shdr_status, shdr_add_status, rc);
15875 rc = -ENXIO;
15876 } else
15877 *offset += wr_object->u.response.actual_write_length;
15878 return rc;
15879}
15880
15881/**
James Smart695a8142010-01-26 23:08:03 -050015882 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15883 * @vport: pointer to vport data structure.
15884 *
15885 * This function iterate through the mailboxq and clean up all REG_LOGIN
15886 * and REG_VPI mailbox commands associated with the vport. This function
15887 * is called when driver want to restart discovery of the vport due to
15888 * a Clear Virtual Link event.
15889 **/
15890void
15891lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15892{
15893 struct lpfc_hba *phba = vport->phba;
15894 LPFC_MBOXQ_t *mb, *nextmb;
15895 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015896 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015897 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015898 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015899 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015900 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015901
James Smartd439d282010-09-29 11:18:45 -040015902 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015903 spin_lock_irq(&phba->hbalock);
15904 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15905 if (mb->vport != vport)
15906 continue;
15907
15908 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15909 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15910 continue;
15911
James Smartd439d282010-09-29 11:18:45 -040015912 list_del(&mb->list);
15913 list_add_tail(&mb->list, &mbox_cmd_list);
15914 }
15915 /* Clean up active mailbox command with the vport */
15916 mb = phba->sli.mbox_active;
15917 if (mb && (mb->vport == vport)) {
15918 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15919 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15920 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15921 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15922 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15923 /* Put reference count for delayed processing */
15924 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15925 /* Unregister the RPI when mailbox complete */
15926 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15927 }
15928 }
James Smart63e801c2010-11-20 23:14:19 -050015929 /* Cleanup any mailbox completions which are not yet processed */
15930 do {
15931 restart_loop = 0;
15932 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15933 /*
15934 * If this mailox is already processed or it is
15935 * for another vport ignore it.
15936 */
15937 if ((mb->vport != vport) ||
15938 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15939 continue;
15940
15941 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15942 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15943 continue;
15944
15945 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15946 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15947 ndlp = (struct lpfc_nodelist *)mb->context2;
15948 /* Unregister the RPI when mailbox complete */
15949 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15950 restart_loop = 1;
15951 spin_unlock_irq(&phba->hbalock);
15952 spin_lock(shost->host_lock);
15953 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15954 spin_unlock(shost->host_lock);
15955 spin_lock_irq(&phba->hbalock);
15956 break;
15957 }
15958 }
15959 } while (restart_loop);
15960
James Smartd439d282010-09-29 11:18:45 -040015961 spin_unlock_irq(&phba->hbalock);
15962
15963 /* Release the cleaned-up mailbox commands */
15964 while (!list_empty(&mbox_cmd_list)) {
15965 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015966 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15967 mp = (struct lpfc_dmabuf *) (mb->context1);
15968 if (mp) {
15969 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15970 kfree(mp);
15971 }
James Smart78730cf2010-04-06 15:06:30 -040015972 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015973 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015974 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015975 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015976 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015977 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015978 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015979 }
James Smart695a8142010-01-26 23:08:03 -050015980 }
James Smart695a8142010-01-26 23:08:03 -050015981 mempool_free(mb, phba->mbox_mem_pool);
15982 }
James Smartd439d282010-09-29 11:18:45 -040015983
15984 /* Release the ndlp with the cleaned-up active mailbox command */
15985 if (act_mbx_ndlp) {
15986 spin_lock(shost->host_lock);
15987 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15988 spin_unlock(shost->host_lock);
15989 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015990 }
James Smart695a8142010-01-26 23:08:03 -050015991}
15992
James Smart2a9bf3d2010-06-07 15:24:45 -040015993/**
15994 * lpfc_drain_txq - Drain the txq
15995 * @phba: Pointer to HBA context object.
15996 *
15997 * This function attempt to submit IOCBs on the txq
15998 * to the adapter. For SLI4 adapters, the txq contains
15999 * ELS IOCBs that have been deferred because the there
16000 * are no SGLs. This congestion can occur with large
16001 * vport counts during node discovery.
16002 **/
16003
16004uint32_t
16005lpfc_drain_txq(struct lpfc_hba *phba)
16006{
16007 LIST_HEAD(completions);
16008 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16009 struct lpfc_iocbq *piocbq = 0;
16010 unsigned long iflags = 0;
16011 char *fail_msg = NULL;
16012 struct lpfc_sglq *sglq;
16013 union lpfc_wqe wqe;
16014
16015 spin_lock_irqsave(&phba->hbalock, iflags);
16016 if (pring->txq_cnt > pring->txq_max)
16017 pring->txq_max = pring->txq_cnt;
16018
16019 spin_unlock_irqrestore(&phba->hbalock, iflags);
16020
16021 while (pring->txq_cnt) {
16022 spin_lock_irqsave(&phba->hbalock, iflags);
16023
James Smart19ca7602010-11-20 23:11:55 -050016024 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016025 if (!piocbq) {
16026 spin_unlock_irqrestore(&phba->hbalock, iflags);
16027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16028 "2823 txq empty and txq_cnt is %d\n ",
16029 pring->txq_cnt);
16030 break;
16031 }
James Smart19ca7602010-11-20 23:11:55 -050016032 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016033 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016034 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016035 spin_unlock_irqrestore(&phba->hbalock, iflags);
16036 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016037 }
16038
16039 /* The xri and iocb resources secured,
16040 * attempt to issue request
16041 */
James Smart6d368e52011-05-24 11:44:12 -040016042 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016043 piocbq->sli4_xritag = sglq->sli4_xritag;
16044 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16045 fail_msg = "to convert bpl to sgl";
16046 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16047 fail_msg = "to convert iocb to wqe";
16048 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16049 fail_msg = " - Wq is full";
16050 else
16051 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16052
16053 if (fail_msg) {
16054 /* Failed means we can't issue and need to cancel */
16055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16056 "2822 IOCB failed %s iotag 0x%x "
16057 "xri 0x%x\n",
16058 fail_msg,
16059 piocbq->iotag, piocbq->sli4_xritag);
16060 list_add_tail(&piocbq->list, &completions);
16061 }
16062 spin_unlock_irqrestore(&phba->hbalock, iflags);
16063 }
16064
James Smart2a9bf3d2010-06-07 15:24:45 -040016065 /* Cancel all the IOCBs that cannot be issued */
16066 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16067 IOERR_SLI_ABORTED);
16068
16069 return pring->txq_cnt;
16070}