blob: 38e56d91aef4b3945c35f091fbbe9178fb7a7809 [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 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 Smarte8d3c3b2013-10-10 12:21:30 -040074static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
75static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart05580562011-05-24 11:40:48 -040076
James Smart4f774512009-05-22 14:52:35 -040077static IOCB_t *
78lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
79{
80 return &iocbq->iocb;
81}
82
83/**
84 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
85 * @q: The Work Queue to operate on.
86 * @wqe: The work Queue Entry to put on the Work queue.
87 *
88 * This routine will copy the contents of @wqe to the next available entry on
89 * the @q. This function will then ring the Work Queue Doorbell to signal the
90 * HBA to start processing the Work Queue Entry. This function returns 0 if
91 * successful. If no entries are available on @q then this function will return
92 * -ENOMEM.
93 * The caller is expected to hold the hbalock when calling this routine.
94 **/
95static uint32_t
96lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
97{
James Smart2e90f4b2011-12-13 13:22:37 -050098 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040099 struct lpfc_register doorbell;
100 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400101 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400102
James Smart2e90f4b2011-12-13 13:22:37 -0500103 /* sanity check on queue memory */
104 if (unlikely(!q))
105 return -ENOMEM;
106 temp_wqe = q->qe[q->host_index].wqe;
107
James Smart4f774512009-05-22 14:52:35 -0400108 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400109 idx = ((q->host_index + 1) % q->entry_count);
110 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400111 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400112 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400113 }
114 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400115 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500116 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400117 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500118 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
119 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400120 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
121
122 /* Update the host index before invoking device */
123 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400124
125 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400126
127 /* Ring Doorbell */
128 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500129 if (q->db_format == LPFC_DB_LIST_FORMAT) {
130 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
131 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
132 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
133 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
134 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
135 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
136 } else {
137 return -EINVAL;
138 }
139 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400140
141 return 0;
142}
143
144/**
145 * lpfc_sli4_wq_release - Updates internal hba index for WQ
146 * @q: The Work Queue to operate on.
147 * @index: The index to advance the hba index to.
148 *
149 * This routine will update the HBA index of a queue to reflect consumption of
150 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
151 * an entry the host calls this function to update the queue's internal
152 * pointers. This routine returns the number of entries that were consumed by
153 * the HBA.
154 **/
155static uint32_t
156lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
157{
158 uint32_t released = 0;
159
James Smart2e90f4b2011-12-13 13:22:37 -0500160 /* sanity check on queue memory */
161 if (unlikely(!q))
162 return 0;
163
James Smart4f774512009-05-22 14:52:35 -0400164 if (q->hba_index == index)
165 return 0;
166 do {
167 q->hba_index = ((q->hba_index + 1) % q->entry_count);
168 released++;
169 } while (q->hba_index != index);
170 return released;
171}
172
173/**
174 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
175 * @q: The Mailbox Queue to operate on.
176 * @wqe: The Mailbox Queue Entry to put on the Work queue.
177 *
178 * This routine will copy the contents of @mqe to the next available entry on
179 * the @q. This function will then ring the Work Queue Doorbell to signal the
180 * HBA to start processing the Work Queue Entry. This function returns 0 if
181 * successful. If no entries are available on @q then this function will return
182 * -ENOMEM.
183 * The caller is expected to hold the hbalock when calling this routine.
184 **/
185static uint32_t
186lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
187{
James Smart2e90f4b2011-12-13 13:22:37 -0500188 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400189 struct lpfc_register doorbell;
190 uint32_t host_index;
191
James Smart2e90f4b2011-12-13 13:22:37 -0500192 /* sanity check on queue memory */
193 if (unlikely(!q))
194 return -ENOMEM;
195 temp_mqe = q->qe[q->host_index].mqe;
196
James Smart4f774512009-05-22 14:52:35 -0400197 /* If the host has not yet processed the next entry then we are done */
198 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
199 return -ENOMEM;
200 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
201 /* Save off the mailbox pointer for completion */
202 q->phba->mbox = (MAILBOX_t *)temp_mqe;
203
204 /* Update the host index before invoking device */
205 host_index = q->host_index;
206 q->host_index = ((q->host_index + 1) % q->entry_count);
207
208 /* Ring Doorbell */
209 doorbell.word0 = 0;
210 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
211 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
212 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400213 return 0;
214}
215
216/**
217 * lpfc_sli4_mq_release - Updates internal hba index for MQ
218 * @q: The Mailbox Queue to operate on.
219 *
220 * This routine will update the HBA index of a queue to reflect consumption of
221 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
222 * an entry the host calls this function to update the queue's internal
223 * pointers. This routine returns the number of entries that were consumed by
224 * the HBA.
225 **/
226static uint32_t
227lpfc_sli4_mq_release(struct lpfc_queue *q)
228{
James Smart2e90f4b2011-12-13 13:22:37 -0500229 /* sanity check on queue memory */
230 if (unlikely(!q))
231 return 0;
232
James Smart4f774512009-05-22 14:52:35 -0400233 /* Clear the mailbox pointer for completion */
234 q->phba->mbox = NULL;
235 q->hba_index = ((q->hba_index + 1) % q->entry_count);
236 return 1;
237}
238
239/**
240 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
241 * @q: The Event Queue to get the first valid EQE from
242 *
243 * This routine will get the first valid Event Queue Entry from @q, update
244 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
245 * the Queue (no more work to do), or the Queue is full of EQEs that have been
246 * processed, but not popped back to the HBA then this routine will return NULL.
247 **/
248static struct lpfc_eqe *
249lpfc_sli4_eq_get(struct lpfc_queue *q)
250{
James Smart2e90f4b2011-12-13 13:22:37 -0500251 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400252 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500253
254 /* sanity check on queue memory */
255 if (unlikely(!q))
256 return NULL;
257 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400258
259 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400260 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400261 return NULL;
262 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400263 idx = ((q->hba_index + 1) % q->entry_count);
264 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400265 return NULL;
266
James Smart027140e2012-08-03 12:35:44 -0400267 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400268 return eqe;
269}
270
271/**
James Smartba20c852012-08-03 12:36:52 -0400272 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
273 * @q: The Event Queue to disable interrupts
274 *
275 **/
276static inline void
277lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
278{
279 struct lpfc_register doorbell;
280
281 doorbell.word0 = 0;
282 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
283 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
284 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
285 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
286 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
287 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
288}
289
290/**
James Smart4f774512009-05-22 14:52:35 -0400291 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
292 * @q: The Event Queue that the host has completed processing for.
293 * @arm: Indicates whether the host wants to arms this CQ.
294 *
295 * This routine will mark all Event Queue Entries on @q, from the last
296 * known completed entry to the last entry that was processed, as completed
297 * by clearing the valid bit for each completion queue entry. Then it will
298 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
299 * The internal host index in the @q will be updated by this routine to indicate
300 * that the host has finished processing the entries. The @arm parameter
301 * indicates that the queue should be rearmed when ringing the doorbell.
302 *
303 * This function will return the number of EQEs that were popped.
304 **/
305uint32_t
306lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
307{
308 uint32_t released = 0;
309 struct lpfc_eqe *temp_eqe;
310 struct lpfc_register doorbell;
311
James Smart2e90f4b2011-12-13 13:22:37 -0500312 /* sanity check on queue memory */
313 if (unlikely(!q))
314 return 0;
315
James Smart4f774512009-05-22 14:52:35 -0400316 /* while there are valid entries */
317 while (q->hba_index != q->host_index) {
318 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400319 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400320 released++;
321 q->host_index = ((q->host_index + 1) % q->entry_count);
322 }
323 if (unlikely(released == 0 && !arm))
324 return 0;
325
326 /* ring doorbell for number popped */
327 doorbell.word0 = 0;
328 if (arm) {
329 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
330 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
331 }
332 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
333 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500334 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
335 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
336 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400337 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500338 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
339 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
340 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400341 return released;
342}
343
344/**
345 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
346 * @q: The Completion Queue to get the first valid CQE from
347 *
348 * This routine will get the first valid Completion Queue Entry from @q, update
349 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
350 * the Queue (no more work to do), or the Queue is full of CQEs that have been
351 * processed, but not popped back to the HBA then this routine will return NULL.
352 **/
353static struct lpfc_cqe *
354lpfc_sli4_cq_get(struct lpfc_queue *q)
355{
356 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400357 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400358
James Smart2e90f4b2011-12-13 13:22:37 -0500359 /* sanity check on queue memory */
360 if (unlikely(!q))
361 return NULL;
362
James Smart4f774512009-05-22 14:52:35 -0400363 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400364 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400365 return NULL;
366 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400367 idx = ((q->hba_index + 1) % q->entry_count);
368 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400369 return NULL;
370
371 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400372 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400373 return cqe;
374}
375
376/**
377 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
378 * @q: The Completion Queue that the host has completed processing for.
379 * @arm: Indicates whether the host wants to arms this CQ.
380 *
381 * This routine will mark all Completion queue entries on @q, from the last
382 * known completed entry to the last entry that was processed, as completed
383 * by clearing the valid bit for each completion queue entry. Then it will
384 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
385 * The internal host index in the @q will be updated by this routine to indicate
386 * that the host has finished processing the entries. The @arm parameter
387 * indicates that the queue should be rearmed when ringing the doorbell.
388 *
389 * This function will return the number of CQEs that were released.
390 **/
391uint32_t
392lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
393{
394 uint32_t released = 0;
395 struct lpfc_cqe *temp_qe;
396 struct lpfc_register doorbell;
397
James Smart2e90f4b2011-12-13 13:22:37 -0500398 /* sanity check on queue memory */
399 if (unlikely(!q))
400 return 0;
James Smart4f774512009-05-22 14:52:35 -0400401 /* while there are valid entries */
402 while (q->hba_index != q->host_index) {
403 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400404 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400405 released++;
406 q->host_index = ((q->host_index + 1) % q->entry_count);
407 }
408 if (unlikely(released == 0 && !arm))
409 return 0;
410
411 /* ring doorbell for number popped */
412 doorbell.word0 = 0;
413 if (arm)
414 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
415 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
416 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500417 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
418 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
419 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400420 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
421 return released;
422}
423
424/**
425 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
426 * @q: The Header Receive Queue to operate on.
427 * @wqe: The Receive Queue Entry to put on the Receive queue.
428 *
429 * This routine will copy the contents of @wqe to the next available entry on
430 * the @q. This function will then ring the Receive Queue Doorbell to signal the
431 * HBA to start processing the Receive Queue Entry. This function returns the
432 * index that the rqe was copied to if successful. If no entries are available
433 * on @q then this function will return -ENOMEM.
434 * The caller is expected to hold the hbalock when calling this routine.
435 **/
436static int
437lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
438 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
439{
James Smart2e90f4b2011-12-13 13:22:37 -0500440 struct lpfc_rqe *temp_hrqe;
441 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400442 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500443 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400444
James Smart2e90f4b2011-12-13 13:22:37 -0500445 /* sanity check on queue memory */
446 if (unlikely(!hq) || unlikely(!dq))
447 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500448 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500449 temp_hrqe = hq->qe[hq->host_index].rqe;
450 temp_drqe = dq->qe[dq->host_index].rqe;
451
James Smart4f774512009-05-22 14:52:35 -0400452 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
453 return -EINVAL;
454 if (hq->host_index != dq->host_index)
455 return -EINVAL;
456 /* If the host has not yet processed the next entry then we are done */
457 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
458 return -EBUSY;
459 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
460 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
461
462 /* Update the host index to point to the next slot */
463 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
464 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
465
466 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400467 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400468 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500469 if (hq->db_format == LPFC_DB_RING_FORMAT) {
470 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
471 hq->entry_repost);
472 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
473 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
474 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
475 hq->entry_repost);
476 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
477 hq->host_index);
478 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
479 } else {
480 return -EINVAL;
481 }
482 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400483 }
484 return put_index;
485}
486
487/**
488 * lpfc_sli4_rq_release - Updates internal hba index for RQ
489 * @q: The Header Receive Queue to operate on.
490 *
491 * This routine will update the HBA index of a queue to reflect consumption of
492 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
493 * consumed an entry the host calls this function to update the queue's
494 * internal pointers. This routine returns the number of entries that were
495 * consumed by the HBA.
496 **/
497static uint32_t
498lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
499{
James Smart2e90f4b2011-12-13 13:22:37 -0500500 /* sanity check on queue memory */
501 if (unlikely(!hq) || unlikely(!dq))
502 return 0;
503
James Smart4f774512009-05-22 14:52:35 -0400504 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
505 return 0;
506 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
507 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
508 return 1;
509}
510
James Smarte59058c2008-08-24 21:49:00 -0400511/**
James Smart3621a712009-04-06 18:47:14 -0400512 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400513 * @phba: Pointer to HBA context object.
514 * @pring: Pointer to driver SLI ring object.
515 *
516 * This function returns pointer to next command iocb entry
517 * in the command ring. The caller must hold hbalock to prevent
518 * other threads consume the next command iocb.
519 * SLI-2/SLI-3 provide different sized iocbs.
520 **/
James Smarted957682007-06-17 19:56:37 -0500521static inline IOCB_t *
522lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
523{
James Smart7e56aa22012-08-03 12:35:34 -0400524 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
525 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500526}
527
James Smarte59058c2008-08-24 21:49:00 -0400528/**
James Smart3621a712009-04-06 18:47:14 -0400529 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400530 * @phba: Pointer to HBA context object.
531 * @pring: Pointer to driver SLI ring object.
532 *
533 * This function returns pointer to next response iocb entry
534 * in the response ring. The caller must hold hbalock to make sure
535 * that no other thread consume the next response iocb.
536 * SLI-2/SLI-3 provide different sized iocbs.
537 **/
James Smarted957682007-06-17 19:56:37 -0500538static inline IOCB_t *
539lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
540{
James Smart7e56aa22012-08-03 12:35:34 -0400541 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
542 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500543}
544
James Smarte59058c2008-08-24 21:49:00 -0400545/**
James Smart3621a712009-04-06 18:47:14 -0400546 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400547 * @phba: Pointer to HBA context object.
548 *
549 * This function is called with hbalock held. This function
550 * allocates a new driver iocb object from the iocb pool. If the
551 * allocation is successful, it returns pointer to the newly
552 * allocated iocb object else it returns NULL.
553 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400554struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500555__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400556{
557 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
558 struct lpfc_iocbq * iocbq = NULL;
559
560 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400561 if (iocbq)
562 phba->iocb_cnt++;
563 if (phba->iocb_cnt > phba->iocb_max)
564 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400565 return iocbq;
566}
567
James Smarte59058c2008-08-24 21:49:00 -0400568/**
James Smartda0436e2009-05-22 14:51:39 -0400569 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
570 * @phba: Pointer to HBA context object.
571 * @xritag: XRI value.
572 *
573 * This function clears the sglq pointer from the array of acive
574 * sglq's. The xritag that is passed in is used to index into the
575 * array. Before the xritag can be used it needs to be adjusted
576 * by subtracting the xribase.
577 *
578 * Returns sglq ponter = success, NULL = Failure.
579 **/
580static struct lpfc_sglq *
581__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
582{
James Smartda0436e2009-05-22 14:51:39 -0400583 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400584
585 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
586 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400587 return sglq;
588}
589
590/**
591 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
592 * @phba: Pointer to HBA context object.
593 * @xritag: XRI value.
594 *
595 * This function returns the sglq pointer from the array of acive
596 * sglq's. The xritag that is passed in is used to index into the
597 * array. Before the xritag can be used it needs to be adjusted
598 * by subtracting the xribase.
599 *
600 * Returns sglq ponter = success, NULL = Failure.
601 **/
James Smart0f65ff62010-02-26 14:14:23 -0500602struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400603__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
604{
James Smartda0436e2009-05-22 14:51:39 -0400605 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400606
607 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400608 return sglq;
609}
610
611/**
James Smart1151e3e2011-02-16 12:39:35 -0500612 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500613 * @phba: Pointer to HBA context object.
614 * @xritag: xri used in this exchange.
615 * @rrq: The RRQ to be cleared.
616 *
James Smart19ca7602010-11-20 23:11:55 -0500617 **/
James Smart1151e3e2011-02-16 12:39:35 -0500618void
619lpfc_clr_rrq_active(struct lpfc_hba *phba,
620 uint16_t xritag,
621 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500622{
James Smart1151e3e2011-02-16 12:39:35 -0500623 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500624
James Smart1151e3e2011-02-16 12:39:35 -0500625 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
626 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500627
628 /* The target DID could have been swapped (cable swap)
629 * we should use the ndlp from the findnode if it is
630 * available.
631 */
James Smart1151e3e2011-02-16 12:39:35 -0500632 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500633 ndlp = rrq->ndlp;
634
James Smart1151e3e2011-02-16 12:39:35 -0500635 if (!ndlp)
636 goto out;
637
James Smartcff261f2013-12-17 20:29:47 -0500638 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500639 rrq->send_rrq = 0;
640 rrq->xritag = 0;
641 rrq->rrq_stop_time = 0;
642 }
James Smart1151e3e2011-02-16 12:39:35 -0500643out:
James Smart19ca7602010-11-20 23:11:55 -0500644 mempool_free(rrq, phba->rrq_pool);
645}
646
647/**
648 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
649 * @phba: Pointer to HBA context object.
650 *
651 * This function is called with hbalock held. This function
652 * Checks if stop_time (ratov from setting rrq active) has
653 * been reached, if it has and the send_rrq flag is set then
654 * it will call lpfc_send_rrq. If the send_rrq flag is not set
655 * then it will just call the routine to clear the rrq and
656 * free the rrq resource.
657 * The timer is set to the next rrq that is going to expire before
658 * leaving the routine.
659 *
660 **/
661void
662lpfc_handle_rrq_active(struct lpfc_hba *phba)
663{
664 struct lpfc_node_rrq *rrq;
665 struct lpfc_node_rrq *nextrrq;
666 unsigned long next_time;
667 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500668 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500669
670 spin_lock_irqsave(&phba->hbalock, iflags);
671 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400672 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500673 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500674 &phba->active_rrq_list, list) {
675 if (time_after(jiffies, rrq->rrq_stop_time))
676 list_move(&rrq->list, &send_rrq);
677 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500678 next_time = rrq->rrq_stop_time;
679 }
680 spin_unlock_irqrestore(&phba->hbalock, iflags);
681 if (!list_empty(&phba->active_rrq_list))
682 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500683 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
684 list_del(&rrq->list);
685 if (!rrq->send_rrq)
686 /* this call will free the rrq */
687 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
688 else if (lpfc_send_rrq(phba, rrq)) {
689 /* if we send the rrq then the completion handler
690 * will clear the bit in the xribitmap.
691 */
692 lpfc_clr_rrq_active(phba, rrq->xritag,
693 rrq);
694 }
695 }
James Smart19ca7602010-11-20 23:11:55 -0500696}
697
698/**
699 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
700 * @vport: Pointer to vport context object.
701 * @xri: The xri used in the exchange.
702 * @did: The targets DID for this exchange.
703 *
704 * returns NULL = rrq not found in the phba->active_rrq_list.
705 * rrq = rrq for this xri and target.
706 **/
707struct lpfc_node_rrq *
708lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
709{
710 struct lpfc_hba *phba = vport->phba;
711 struct lpfc_node_rrq *rrq;
712 struct lpfc_node_rrq *nextrrq;
713 unsigned long iflags;
714
715 if (phba->sli_rev != LPFC_SLI_REV4)
716 return NULL;
717 spin_lock_irqsave(&phba->hbalock, iflags);
718 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
719 if (rrq->vport == vport && rrq->xritag == xri &&
720 rrq->nlp_DID == did){
721 list_del(&rrq->list);
722 spin_unlock_irqrestore(&phba->hbalock, iflags);
723 return rrq;
724 }
725 }
726 spin_unlock_irqrestore(&phba->hbalock, iflags);
727 return NULL;
728}
729
730/**
731 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
732 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500733 * @ndlp: Pointer to the lpfc_node_list structure.
734 * If ndlp is NULL Remove all active RRQs for this vport from the
735 * phba->active_rrq_list and clear the rrq.
736 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500737 **/
738void
James Smart1151e3e2011-02-16 12:39:35 -0500739lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500740
741{
742 struct lpfc_hba *phba = vport->phba;
743 struct lpfc_node_rrq *rrq;
744 struct lpfc_node_rrq *nextrrq;
745 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500746 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500747
748 if (phba->sli_rev != LPFC_SLI_REV4)
749 return;
James Smart1151e3e2011-02-16 12:39:35 -0500750 if (!ndlp) {
751 lpfc_sli4_vport_delete_els_xri_aborted(vport);
752 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500753 }
James Smart1151e3e2011-02-16 12:39:35 -0500754 spin_lock_irqsave(&phba->hbalock, iflags);
755 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
756 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
757 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500758 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500759
760 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
761 list_del(&rrq->list);
762 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
763 }
James Smart19ca7602010-11-20 23:11:55 -0500764}
765
766/**
767 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
768 * @phba: Pointer to HBA context object.
769 *
770 * Remove all rrqs from the phba->active_rrq_list and free them by
771 * calling __lpfc_clr_active_rrq
772 *
773 **/
774void
775lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
776{
777 struct lpfc_node_rrq *rrq;
778 struct lpfc_node_rrq *nextrrq;
779 unsigned long next_time;
780 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500781 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500782
783 if (phba->sli_rev != LPFC_SLI_REV4)
784 return;
785 spin_lock_irqsave(&phba->hbalock, iflags);
786 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400787 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2));
James Smart1151e3e2011-02-16 12:39:35 -0500788 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500789 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500790
791 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
792 list_del(&rrq->list);
793 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
794 }
James Smart19ca7602010-11-20 23:11:55 -0500795 if (!list_empty(&phba->active_rrq_list))
796 mod_timer(&phba->rrq_tmr, next_time);
797}
798
799
800/**
James Smart1151e3e2011-02-16 12:39:35 -0500801 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500802 * @phba: Pointer to HBA context object.
803 * @ndlp: Targets nodelist pointer for this exchange.
804 * @xritag the xri in the bitmap to test.
805 *
806 * This function is called with hbalock held. This function
807 * returns 0 = rrq not active for this xri
808 * 1 = rrq is valid for this xri.
809 **/
James Smart1151e3e2011-02-16 12:39:35 -0500810int
811lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500812 uint16_t xritag)
813{
James Smart19ca7602010-11-20 23:11:55 -0500814 if (!ndlp)
815 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500816 if (!ndlp->active_rrqs_xri_bitmap)
817 return 0;
818 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500819 return 1;
820 else
821 return 0;
822}
823
824/**
825 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
826 * @phba: Pointer to HBA context object.
827 * @ndlp: nodelist pointer for this target.
828 * @xritag: xri used in this exchange.
829 * @rxid: Remote Exchange ID.
830 * @send_rrq: Flag used to determine if we should send rrq els cmd.
831 *
832 * This function takes the hbalock.
833 * The active bit is always set in the active rrq xri_bitmap even
834 * if there is no slot avaiable for the other rrq information.
835 *
836 * returns 0 rrq actived for this xri
837 * < 0 No memory or invalid ndlp.
838 **/
839int
840lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500841 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500842{
James Smart19ca7602010-11-20 23:11:55 -0500843 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500844 struct lpfc_node_rrq *rrq;
845 int empty;
846
847 if (!ndlp)
848 return -EINVAL;
849
850 if (!phba->cfg_enable_rrq)
851 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500852
853 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500854 if (phba->pport->load_flag & FC_UNLOADING) {
855 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
856 goto out;
857 }
858
859 /*
860 * set the active bit even if there is no mem available.
861 */
862 if (NLP_CHK_FREE_REQ(ndlp))
863 goto out;
864
865 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
866 goto out;
867
James Smartcff261f2013-12-17 20:29:47 -0500868 if (!ndlp->active_rrqs_xri_bitmap)
869 goto out;
870
871 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500872 goto out;
873
James Smart19ca7602010-11-20 23:11:55 -0500874 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500875 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
876 if (!rrq) {
877 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
878 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
879 " DID:0x%x Send:%d\n",
880 xritag, rxid, ndlp->nlp_DID, send_rrq);
881 return -EINVAL;
882 }
James Smarte5771b42013-03-01 16:37:14 -0500883 if (phba->cfg_enable_rrq == 1)
884 rrq->send_rrq = send_rrq;
885 else
886 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500887 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400888 rrq->rrq_stop_time = jiffies +
889 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500890 rrq->ndlp = ndlp;
891 rrq->nlp_DID = ndlp->nlp_DID;
892 rrq->vport = ndlp->vport;
893 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500894 spin_lock_irqsave(&phba->hbalock, iflags);
895 empty = list_empty(&phba->active_rrq_list);
896 list_add_tail(&rrq->list, &phba->active_rrq_list);
897 phba->hba_flag |= HBA_RRQ_ACTIVE;
898 if (empty)
899 lpfc_worker_wake_up(phba);
900 spin_unlock_irqrestore(&phba->hbalock, iflags);
901 return 0;
902out:
903 spin_unlock_irqrestore(&phba->hbalock, iflags);
904 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
905 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
906 " DID:0x%x Send:%d\n",
907 xritag, rxid, ndlp->nlp_DID, send_rrq);
908 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500909}
910
911/**
James Smartda0436e2009-05-22 14:51:39 -0400912 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
913 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500914 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400915 *
916 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400917 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400918 * list is not empty then it is successful, it returns pointer to the newly
919 * allocated sglq object else it returns NULL.
920 **/
921static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500922__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400923{
924 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
925 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500926 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500927 struct lpfc_scsi_buf *lpfc_cmd;
928 struct lpfc_nodelist *ndlp;
929 int found = 0;
930
931 if (piocbq->iocb_flag & LPFC_IO_FCP) {
932 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
933 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500934 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
935 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500936 ndlp = piocbq->context_un.ndlp;
James Smartd5ce53b2013-04-17 20:17:26 -0400937 else if (piocbq->iocb_flag & LPFC_IO_LIBDFC)
James Smart93d1379e2012-05-09 21:19:34 -0400938 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500939 else
940 ndlp = piocbq->context1;
941
James Smartda0436e2009-05-22 14:51:39 -0400942 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500943 start_sglq = sglq;
944 while (!found) {
945 if (!sglq)
946 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400947 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500948 /* This xri has an rrq outstanding for this DID.
949 * put it back in the list and get another xri.
950 */
951 list_add_tail(&sglq->list, lpfc_sgl_list);
952 sglq = NULL;
953 list_remove_head(lpfc_sgl_list, sglq,
954 struct lpfc_sglq, list);
955 if (sglq == start_sglq) {
956 sglq = NULL;
957 break;
958 } else
959 continue;
960 }
961 sglq->ndlp = ndlp;
962 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400963 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500964 sglq->state = SGL_ALLOCATED;
965 }
James Smartda0436e2009-05-22 14:51:39 -0400966 return sglq;
967}
968
969/**
James Smart3621a712009-04-06 18:47:14 -0400970 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400971 * @phba: Pointer to HBA context object.
972 *
973 * This function is called with no lock held. This function
974 * allocates a new driver iocb object from the iocb pool. If the
975 * allocation is successful, it returns pointer to the newly
976 * allocated iocb object else it returns NULL.
977 **/
James Smart2e0fef82007-06-17 19:56:36 -0500978struct lpfc_iocbq *
979lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500980{
James Smart2e0fef82007-06-17 19:56:36 -0500981 struct lpfc_iocbq * iocbq = NULL;
982 unsigned long iflags;
983
984 spin_lock_irqsave(&phba->hbalock, iflags);
985 iocbq = __lpfc_sli_get_iocbq(phba);
986 spin_unlock_irqrestore(&phba->hbalock, iflags);
987 return iocbq;
988}
989
James Smarte59058c2008-08-24 21:49:00 -0400990/**
James Smart4f774512009-05-22 14:52:35 -0400991 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
992 * @phba: Pointer to HBA context object.
993 * @iocbq: Pointer to driver iocb object.
994 *
995 * This function is called with hbalock held to release driver
996 * iocb object to the iocb pool. The iotag in the iocb object
997 * does not change for each use of the iocb object. This function
998 * clears all other fields of the iocb object when it is freed.
999 * The sqlq structure that holds the xritag and phys and virtual
1000 * mappings for the scatter gather list is retrieved from the
1001 * active array of sglq. The get of the sglq pointer also clears
1002 * the entry in the array. If the status of the IO indiactes that
1003 * this IO was aborted then the sglq entry it put on the
1004 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1005 * IO has good status or fails for any other reason then the sglq
1006 * entry is added to the free list (lpfc_sgl_list).
1007 **/
1008static void
1009__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1010{
1011 struct lpfc_sglq *sglq;
1012 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001013 unsigned long iflag = 0;
1014 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001015
1016 if (iocbq->sli4_xritag == NO_XRI)
1017 sglq = NULL;
1018 else
James Smart6d368e52011-05-24 11:44:12 -04001019 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1020
James Smart0e9bb8d2013-03-01 16:35:12 -05001021
James Smart4f774512009-05-22 14:52:35 -04001022 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001023 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1024 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001025 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1026 iflag);
1027 list_add(&sglq->list,
1028 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1029 spin_unlock_irqrestore(
1030 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001031 } else {
1032 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001033 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001034 list_add_tail(&sglq->list,
1035 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -04001036
1037 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001038 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001039 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001040 }
James Smart4f774512009-05-22 14:52:35 -04001041 }
1042
1043
1044 /*
1045 * Clean all volatile data fields, preserve iotag and node struct.
1046 */
1047 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001048 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001049 iocbq->sli4_xritag = NO_XRI;
1050 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1051}
1052
James Smart2a9bf3d2010-06-07 15:24:45 -04001053
James Smart4f774512009-05-22 14:52:35 -04001054/**
James Smart3772a992009-05-22 14:50:54 -04001055 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1056 * @phba: Pointer to HBA context object.
1057 * @iocbq: Pointer to driver iocb object.
1058 *
1059 * This function is called with hbalock held to release driver
1060 * iocb object to the iocb pool. The iotag in the iocb object
1061 * does not change for each use of the iocb object. This function
1062 * clears all other fields of the iocb object when it is freed.
1063 **/
1064static void
1065__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1066{
1067 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1068
James Smart0e9bb8d2013-03-01 16:35:12 -05001069
1070 /*
James Smart3772a992009-05-22 14:50:54 -04001071 * Clean all volatile data fields, preserve iotag and node struct.
1072 */
1073 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1074 iocbq->sli4_xritag = NO_XRI;
1075 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1076}
1077
1078/**
James Smart3621a712009-04-06 18:47:14 -04001079 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001080 * @phba: Pointer to HBA context object.
1081 * @iocbq: Pointer to driver iocb object.
1082 *
1083 * This function is called with hbalock held to release driver
1084 * iocb object to the iocb pool. The iotag in the iocb object
1085 * does not change for each use of the iocb object. This function
1086 * clears all other fields of the iocb object when it is freed.
1087 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001088static void
James Smart2e0fef82007-06-17 19:56:36 -05001089__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1090{
James Smart3772a992009-05-22 14:50:54 -04001091 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001092 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001093}
1094
James Smarte59058c2008-08-24 21:49:00 -04001095/**
James Smart3621a712009-04-06 18:47:14 -04001096 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001097 * @phba: Pointer to HBA context object.
1098 * @iocbq: Pointer to driver iocb object.
1099 *
1100 * This function is called with no lock held to release the iocb to
1101 * iocb pool.
1102 **/
James Smart2e0fef82007-06-17 19:56:36 -05001103void
1104lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1105{
1106 unsigned long iflags;
1107
1108 /*
1109 * Clean all volatile data fields, preserve iotag and node struct.
1110 */
1111 spin_lock_irqsave(&phba->hbalock, iflags);
1112 __lpfc_sli_release_iocbq(phba, iocbq);
1113 spin_unlock_irqrestore(&phba->hbalock, iflags);
1114}
1115
James Smarte59058c2008-08-24 21:49:00 -04001116/**
James Smarta257bf92009-04-06 18:48:10 -04001117 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1118 * @phba: Pointer to HBA context object.
1119 * @iocblist: List of IOCBs.
1120 * @ulpstatus: ULP status in IOCB command field.
1121 * @ulpWord4: ULP word-4 in IOCB command field.
1122 *
1123 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1124 * on the list by invoking the complete callback function associated with the
1125 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1126 * fields.
1127 **/
1128void
1129lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1130 uint32_t ulpstatus, uint32_t ulpWord4)
1131{
1132 struct lpfc_iocbq *piocb;
1133
1134 while (!list_empty(iocblist)) {
1135 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001136 if (!piocb->iocb_cmpl)
1137 lpfc_sli_release_iocbq(phba, piocb);
1138 else {
1139 piocb->iocb.ulpStatus = ulpstatus;
1140 piocb->iocb.un.ulpWord[4] = ulpWord4;
1141 (piocb->iocb_cmpl) (phba, piocb, piocb);
1142 }
1143 }
1144 return;
1145}
1146
1147/**
James Smart3621a712009-04-06 18:47:14 -04001148 * lpfc_sli_iocb_cmd_type - Get the iocb type
1149 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001150 *
1151 * This function is called by ring event handler function to get the iocb type.
1152 * This function translates the iocb command to an iocb command type used to
1153 * decide the final disposition of each completed IOCB.
1154 * The function returns
1155 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1156 * LPFC_SOL_IOCB if it is a solicited iocb completion
1157 * LPFC_ABORT_IOCB if it is an abort iocb
1158 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1159 *
1160 * The caller is not required to hold any lock.
1161 **/
dea31012005-04-17 16:05:31 -05001162static lpfc_iocb_type
1163lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1164{
1165 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1166
1167 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1168 return 0;
1169
1170 switch (iocb_cmnd) {
1171 case CMD_XMIT_SEQUENCE_CR:
1172 case CMD_XMIT_SEQUENCE_CX:
1173 case CMD_XMIT_BCAST_CN:
1174 case CMD_XMIT_BCAST_CX:
1175 case CMD_ELS_REQUEST_CR:
1176 case CMD_ELS_REQUEST_CX:
1177 case CMD_CREATE_XRI_CR:
1178 case CMD_CREATE_XRI_CX:
1179 case CMD_GET_RPI_CN:
1180 case CMD_XMIT_ELS_RSP_CX:
1181 case CMD_GET_RPI_CR:
1182 case CMD_FCP_IWRITE_CR:
1183 case CMD_FCP_IWRITE_CX:
1184 case CMD_FCP_IREAD_CR:
1185 case CMD_FCP_IREAD_CX:
1186 case CMD_FCP_ICMND_CR:
1187 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001188 case CMD_FCP_TSEND_CX:
1189 case CMD_FCP_TRSP_CX:
1190 case CMD_FCP_TRECEIVE_CX:
1191 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001192 case CMD_ADAPTER_MSG:
1193 case CMD_ADAPTER_DUMP:
1194 case CMD_XMIT_SEQUENCE64_CR:
1195 case CMD_XMIT_SEQUENCE64_CX:
1196 case CMD_XMIT_BCAST64_CN:
1197 case CMD_XMIT_BCAST64_CX:
1198 case CMD_ELS_REQUEST64_CR:
1199 case CMD_ELS_REQUEST64_CX:
1200 case CMD_FCP_IWRITE64_CR:
1201 case CMD_FCP_IWRITE64_CX:
1202 case CMD_FCP_IREAD64_CR:
1203 case CMD_FCP_IREAD64_CX:
1204 case CMD_FCP_ICMND64_CR:
1205 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001206 case CMD_FCP_TSEND64_CX:
1207 case CMD_FCP_TRSP64_CX:
1208 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001209 case CMD_GEN_REQUEST64_CR:
1210 case CMD_GEN_REQUEST64_CX:
1211 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001212 case DSSCMD_IWRITE64_CR:
1213 case DSSCMD_IWRITE64_CX:
1214 case DSSCMD_IREAD64_CR:
1215 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001216 type = LPFC_SOL_IOCB;
1217 break;
1218 case CMD_ABORT_XRI_CN:
1219 case CMD_ABORT_XRI_CX:
1220 case CMD_CLOSE_XRI_CN:
1221 case CMD_CLOSE_XRI_CX:
1222 case CMD_XRI_ABORTED_CX:
1223 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001224 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001225 type = LPFC_ABORT_IOCB;
1226 break;
1227 case CMD_RCV_SEQUENCE_CX:
1228 case CMD_RCV_ELS_REQ_CX:
1229 case CMD_RCV_SEQUENCE64_CX:
1230 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001231 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001232 case CMD_IOCB_RCV_SEQ64_CX:
1233 case CMD_IOCB_RCV_ELS64_CX:
1234 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001235 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001236 type = LPFC_UNSOL_IOCB;
1237 break;
James Smart3163f722008-02-08 18:50:25 -05001238 case CMD_IOCB_XMIT_MSEQ64_CR:
1239 case CMD_IOCB_XMIT_MSEQ64_CX:
1240 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1241 case CMD_IOCB_RCV_ELS_LIST64_CX:
1242 case CMD_IOCB_CLOSE_EXTENDED_CN:
1243 case CMD_IOCB_ABORT_EXTENDED_CN:
1244 case CMD_IOCB_RET_HBQE64_CN:
1245 case CMD_IOCB_FCP_IBIDIR64_CR:
1246 case CMD_IOCB_FCP_IBIDIR64_CX:
1247 case CMD_IOCB_FCP_ITASKMGT64_CX:
1248 case CMD_IOCB_LOGENTRY_CN:
1249 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1250 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001251 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001252 type = LPFC_UNKNOWN_IOCB;
1253 break;
dea31012005-04-17 16:05:31 -05001254 default:
1255 type = LPFC_UNKNOWN_IOCB;
1256 break;
1257 }
1258
1259 return type;
1260}
1261
James Smarte59058c2008-08-24 21:49:00 -04001262/**
James Smart3621a712009-04-06 18:47:14 -04001263 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001264 * @phba: Pointer to HBA context object.
1265 *
1266 * This function is called from SLI initialization code
1267 * to configure every ring of the HBA's SLI interface. The
1268 * caller is not required to hold any lock. This function issues
1269 * a config_ring mailbox command for each ring.
1270 * This function returns zero if successful else returns a negative
1271 * error code.
1272 **/
dea31012005-04-17 16:05:31 -05001273static int
James Smarted957682007-06-17 19:56:37 -05001274lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001275{
1276 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001277 LPFC_MBOXQ_t *pmb;
1278 MAILBOX_t *pmbox;
1279 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001280
James Smarted957682007-06-17 19:56:37 -05001281 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1282 if (!pmb)
1283 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001284 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001285 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001286 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001287 lpfc_config_ring(phba, i, pmb);
1288 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1289 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001291 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001292 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1293 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001294 rc, pmbox->mbxCommand,
1295 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001296 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001297 ret = -ENXIO;
1298 break;
dea31012005-04-17 16:05:31 -05001299 }
1300 }
James Smarted957682007-06-17 19:56:37 -05001301 mempool_free(pmb, phba->mbox_mem_pool);
1302 return ret;
dea31012005-04-17 16:05:31 -05001303}
1304
James Smarte59058c2008-08-24 21:49:00 -04001305/**
James Smart3621a712009-04-06 18:47:14 -04001306 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001307 * @phba: Pointer to HBA context object.
1308 * @pring: Pointer to driver SLI ring object.
1309 * @piocb: Pointer to the driver iocb object.
1310 *
1311 * This function is called with hbalock held. The function adds the
1312 * new iocb to txcmplq of the given ring. This function always returns
1313 * 0. If this function is called for ELS ring, this function checks if
1314 * there is a vport associated with the ELS command. This function also
1315 * starts els_tmofunc timer if this is an ELS command.
1316 **/
dea31012005-04-17 16:05:31 -05001317static int
James Smart2e0fef82007-06-17 19:56:36 -05001318lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1319 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001320{
dea31012005-04-17 16:05:31 -05001321 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001322 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001323
James Smart92d7f7b2007-06-17 19:56:38 -05001324 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1325 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1326 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1327 if (!piocb->vport)
1328 BUG();
1329 else
1330 mod_timer(&piocb->vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001331 jiffies +
1332 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
James Smart92d7f7b2007-06-17 19:56:38 -05001333 }
1334
dea31012005-04-17 16:05:31 -05001335
James Smart2e0fef82007-06-17 19:56:36 -05001336 return 0;
dea31012005-04-17 16:05:31 -05001337}
1338
James Smarte59058c2008-08-24 21:49:00 -04001339/**
James Smart3621a712009-04-06 18:47:14 -04001340 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001341 * @phba: Pointer to HBA context object.
1342 * @pring: Pointer to driver SLI ring object.
1343 *
1344 * This function is called with hbalock held to get next
1345 * iocb in txq of the given ring. If there is any iocb in
1346 * the txq, the function returns first iocb in the list after
1347 * removing the iocb from the list, else it returns NULL.
1348 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001349struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001350lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001351{
dea31012005-04-17 16:05:31 -05001352 struct lpfc_iocbq *cmd_iocb;
1353
James Smart858c9f62007-06-17 19:56:39 -05001354 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001355 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001356}
1357
James Smarte59058c2008-08-24 21:49:00 -04001358/**
James Smart3621a712009-04-06 18:47:14 -04001359 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001360 * @phba: Pointer to HBA context object.
1361 * @pring: Pointer to driver SLI ring object.
1362 *
1363 * This function is called with hbalock held and the caller must post the
1364 * iocb without releasing the lock. If the caller releases the lock,
1365 * iocb slot returned by the function is not guaranteed to be available.
1366 * The function returns pointer to the next available iocb slot if there
1367 * is available slot in the ring, else it returns NULL.
1368 * If the get index of the ring is ahead of the put index, the function
1369 * will post an error attention event to the worker thread to take the
1370 * HBA to offline state.
1371 **/
dea31012005-04-17 16:05:31 -05001372static IOCB_t *
1373lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1374{
James Smart34b02dc2008-08-24 21:49:55 -04001375 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001376 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1377 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1378 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1379 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001380
James Smart7e56aa22012-08-03 12:35:34 -04001381 if (unlikely(pring->sli.sli3.local_getidx ==
1382 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001383
James Smart7e56aa22012-08-03 12:35:34 -04001384 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001385
James Smart7e56aa22012-08-03 12:35:34 -04001386 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001387 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001388 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001389 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001390 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001391 pring->sli.sli3.local_getidx,
1392 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001393
James Smart2e0fef82007-06-17 19:56:36 -05001394 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001395 /*
1396 * All error attention handlers are posted to
1397 * worker thread
1398 */
1399 phba->work_ha |= HA_ERATT;
1400 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001401
James Smart5e9d9b82008-06-14 22:52:53 -04001402 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001403
1404 return NULL;
1405 }
1406
James Smart7e56aa22012-08-03 12:35:34 -04001407 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001408 return NULL;
1409 }
1410
James Smarted957682007-06-17 19:56:37 -05001411 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001412}
1413
James Smarte59058c2008-08-24 21:49:00 -04001414/**
James Smart3621a712009-04-06 18:47:14 -04001415 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001416 * @phba: Pointer to HBA context object.
1417 * @iocbq: Pointer to driver iocb object.
1418 *
1419 * This function gets an iotag for the iocb. If there is no unused iotag and
1420 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1421 * array and assigns a new iotag.
1422 * The function returns the allocated iotag if successful, else returns zero.
1423 * Zero is not a valid iotag.
1424 * The caller is not required to hold any lock.
1425 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001426uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001427lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001428{
James Smart2e0fef82007-06-17 19:56:36 -05001429 struct lpfc_iocbq **new_arr;
1430 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001431 size_t new_len;
1432 struct lpfc_sli *psli = &phba->sli;
1433 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001434
James Smart2e0fef82007-06-17 19:56:36 -05001435 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001436 iotag = psli->last_iotag;
1437 if(++iotag < psli->iocbq_lookup_len) {
1438 psli->last_iotag = iotag;
1439 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001440 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001441 iocbq->iotag = iotag;
1442 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001443 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001444 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1445 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001446 spin_unlock_irq(&phba->hbalock);
1447 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001448 GFP_KERNEL);
1449 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001450 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001451 old_arr = psli->iocbq_lookup;
1452 if (new_len <= psli->iocbq_lookup_len) {
1453 /* highly unprobable case */
1454 kfree(new_arr);
1455 iotag = psli->last_iotag;
1456 if(++iotag < psli->iocbq_lookup_len) {
1457 psli->last_iotag = iotag;
1458 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001459 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001460 iocbq->iotag = iotag;
1461 return iotag;
1462 }
James Smart2e0fef82007-06-17 19:56:36 -05001463 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001464 return 0;
1465 }
1466 if (psli->iocbq_lookup)
1467 memcpy(new_arr, old_arr,
1468 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001469 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001470 psli->iocbq_lookup = new_arr;
1471 psli->iocbq_lookup_len = new_len;
1472 psli->last_iotag = iotag;
1473 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001474 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001475 iocbq->iotag = iotag;
1476 kfree(old_arr);
1477 return iotag;
1478 }
James Smart8f6d98d2006-08-01 07:34:00 -04001479 } else
James Smart2e0fef82007-06-17 19:56:36 -05001480 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001481
James Smartbc739052010-08-04 16:11:18 -04001482 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001483 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1484 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001485
James Bottomley604a3e32005-10-29 10:28:33 -05001486 return 0;
dea31012005-04-17 16:05:31 -05001487}
1488
James Smarte59058c2008-08-24 21:49:00 -04001489/**
James Smart3621a712009-04-06 18:47:14 -04001490 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001491 * @phba: Pointer to HBA context object.
1492 * @pring: Pointer to driver SLI ring object.
1493 * @iocb: Pointer to iocb slot in the ring.
1494 * @nextiocb: Pointer to driver iocb object which need to be
1495 * posted to firmware.
1496 *
1497 * This function is called with hbalock held to post a new iocb to
1498 * the firmware. This function copies the new iocb to ring iocb slot and
1499 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1500 * a completion call back for this iocb else the function will free the
1501 * iocb object.
1502 **/
dea31012005-04-17 16:05:31 -05001503static void
1504lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1505 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1506{
1507 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001508 * Set up an iotag
dea31012005-04-17 16:05:31 -05001509 */
James Bottomley604a3e32005-10-29 10:28:33 -05001510 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001511
James Smarte2a0a9d2008-12-04 22:40:02 -05001512
James Smarta58cbd52007-08-02 11:09:43 -04001513 if (pring->ringno == LPFC_ELS_RING) {
1514 lpfc_debugfs_slow_ring_trc(phba,
1515 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1516 *(((uint32_t *) &nextiocb->iocb) + 4),
1517 *(((uint32_t *) &nextiocb->iocb) + 6),
1518 *(((uint32_t *) &nextiocb->iocb) + 7));
1519 }
1520
dea31012005-04-17 16:05:31 -05001521 /*
1522 * Issue iocb command to adapter
1523 */
James Smart92d7f7b2007-06-17 19:56:38 -05001524 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001525 wmb();
1526 pring->stats.iocb_cmd++;
1527
1528 /*
1529 * If there is no completion routine to call, we can release the
1530 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1531 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1532 */
1533 if (nextiocb->iocb_cmpl)
1534 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001535 else
James Smart2e0fef82007-06-17 19:56:36 -05001536 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001537
1538 /*
1539 * Let the HBA know what IOCB slot will be the next one the
1540 * driver will put a command into.
1541 */
James Smart7e56aa22012-08-03 12:35:34 -04001542 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1543 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001544}
1545
James Smarte59058c2008-08-24 21:49:00 -04001546/**
James Smart3621a712009-04-06 18:47:14 -04001547 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001548 * @phba: Pointer to HBA context object.
1549 * @pring: Pointer to driver SLI ring object.
1550 *
1551 * The caller is not required to hold any lock for calling this function.
1552 * This function updates the chip attention bits for the ring to inform firmware
1553 * that there are pending work to be done for this ring and requests an
1554 * interrupt when there is space available in the ring. This function is
1555 * called when the driver is unable to post more iocbs to the ring due
1556 * to unavailability of space in the ring.
1557 **/
dea31012005-04-17 16:05:31 -05001558static void
James Smart2e0fef82007-06-17 19:56:36 -05001559lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001560{
1561 int ringno = pring->ringno;
1562
1563 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1564
1565 wmb();
1566
1567 /*
1568 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1569 * The HBA will tell us when an IOCB entry is available.
1570 */
1571 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1572 readl(phba->CAregaddr); /* flush */
1573
1574 pring->stats.iocb_cmd_full++;
1575}
1576
James Smarte59058c2008-08-24 21:49:00 -04001577/**
James Smart3621a712009-04-06 18:47:14 -04001578 * lpfc_sli_update_ring - Update chip attention register
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 updates the chip attention register bit for the
1583 * given ring to inform HBA that there is more work to be done
1584 * in this ring. The caller is not required to hold any lock.
1585 **/
dea31012005-04-17 16:05:31 -05001586static void
James Smart2e0fef82007-06-17 19:56:36 -05001587lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001588{
1589 int ringno = pring->ringno;
1590
1591 /*
1592 * Tell the HBA that there is work to do in this ring.
1593 */
James Smart34b02dc2008-08-24 21:49:55 -04001594 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1595 wmb();
1596 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1597 readl(phba->CAregaddr); /* flush */
1598 }
dea31012005-04-17 16:05:31 -05001599}
1600
James Smarte59058c2008-08-24 21:49:00 -04001601/**
James Smart3621a712009-04-06 18:47:14 -04001602 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001603 * @phba: Pointer to HBA context object.
1604 * @pring: Pointer to driver SLI ring object.
1605 *
1606 * This function is called with hbalock held to post pending iocbs
1607 * in the txq to the firmware. This function is called when driver
1608 * detects space available in the ring.
1609 **/
dea31012005-04-17 16:05:31 -05001610static void
James Smart2e0fef82007-06-17 19:56:36 -05001611lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001612{
1613 IOCB_t *iocb;
1614 struct lpfc_iocbq *nextiocb;
1615
1616 /*
1617 * Check to see if:
1618 * (a) there is anything on the txq to send
1619 * (b) link is up
1620 * (c) link attention events can be processed (fcp ring only)
1621 * (d) IOCB processing is not blocked by the outstanding mbox command.
1622 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001623
1624 if (lpfc_is_link_up(phba) &&
1625 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001626 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001627 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001628
1629 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1630 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1631 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1632
1633 if (iocb)
1634 lpfc_sli_update_ring(phba, pring);
1635 else
1636 lpfc_sli_update_full_ring(phba, pring);
1637 }
1638
1639 return;
1640}
1641
James Smarte59058c2008-08-24 21:49:00 -04001642/**
James Smart3621a712009-04-06 18:47:14 -04001643 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001644 * @phba: Pointer to HBA context object.
1645 * @hbqno: HBQ number.
1646 *
1647 * This function is called with hbalock held to get the next
1648 * available slot for the given HBQ. If there is free slot
1649 * available for the HBQ it will return pointer to the next available
1650 * HBQ entry else it will return NULL.
1651 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001652static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001653lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1654{
1655 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1656
1657 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1658 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1659 hbqp->next_hbqPutIdx = 0;
1660
1661 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001662 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001663 uint32_t getidx = le32_to_cpu(raw_index);
1664
1665 hbqp->local_hbqGetIdx = getidx;
1666
1667 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1668 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001669 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001670 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001671 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001672 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001673 hbqp->entry_count);
1674
1675 phba->link_state = LPFC_HBA_ERROR;
1676 return NULL;
1677 }
1678
1679 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1680 return NULL;
1681 }
1682
James Smart51ef4c22007-08-02 11:10:31 -04001683 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1684 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001685}
1686
James Smarte59058c2008-08-24 21:49:00 -04001687/**
James Smart3621a712009-04-06 18:47:14 -04001688 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001689 * @phba: Pointer to HBA context object.
1690 *
1691 * This function is called with no lock held to free all the
1692 * hbq buffers while uninitializing the SLI interface. It also
1693 * frees the HBQ buffers returned by the firmware but not yet
1694 * processed by the upper layers.
1695 **/
James Smarted957682007-06-17 19:56:37 -05001696void
1697lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1698{
James Smart92d7f7b2007-06-17 19:56:38 -05001699 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1700 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001701 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001702 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001703 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001704
James Smart51ef4c22007-08-02 11:10:31 -04001705 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001706 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001707 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001708 for (i = 0; i < hbq_count; ++i) {
1709 list_for_each_entry_safe(dmabuf, next_dmabuf,
1710 &phba->hbqs[i].hbq_buffer_list, list) {
1711 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1712 list_del(&hbq_buf->dbuf.list);
1713 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1714 }
James Smarta8adb832007-10-27 13:37:53 -04001715 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001716 }
James Smart3163f722008-02-08 18:50:25 -05001717 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001718 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1719 list) {
James Smart3163f722008-02-08 18:50:25 -05001720 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1721 list_del(&hbq_buf->dbuf.list);
1722 if (hbq_buf->tag == -1) {
1723 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1724 (phba, hbq_buf);
1725 } else {
1726 hbqno = hbq_buf->tag >> 16;
1727 if (hbqno >= LPFC_MAX_HBQS)
1728 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1729 (phba, hbq_buf);
1730 else
1731 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1732 hbq_buf);
1733 }
1734 }
1735
1736 /* Mark the HBQs not in use */
1737 phba->hbq_in_use = 0;
1738 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001739}
1740
James Smarte59058c2008-08-24 21:49:00 -04001741/**
James Smart3621a712009-04-06 18:47:14 -04001742 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001743 * @phba: Pointer to HBA context object.
1744 * @hbqno: HBQ number.
1745 * @hbq_buf: Pointer to HBQ buffer.
1746 *
1747 * This function is called with the hbalock held to post a
1748 * hbq buffer to the firmware. If the function finds an empty
1749 * slot in the HBQ, it will post the buffer. The function will return
1750 * pointer to the hbq entry if it successfully post the buffer
1751 * else it will return NULL.
1752 **/
James Smart3772a992009-05-22 14:50:54 -04001753static int
James Smarted957682007-06-17 19:56:37 -05001754lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001755 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001756{
James Smart3772a992009-05-22 14:50:54 -04001757 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1758}
1759
1760/**
1761 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1762 * @phba: Pointer to HBA context object.
1763 * @hbqno: HBQ number.
1764 * @hbq_buf: Pointer to HBQ buffer.
1765 *
1766 * This function is called with the hbalock held to post a hbq buffer to the
1767 * firmware. If the function finds an empty slot in the HBQ, it will post the
1768 * buffer and place it on the hbq_buffer_list. The function will return zero if
1769 * it successfully post the buffer else it will return an error.
1770 **/
1771static int
1772lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1773 struct hbq_dmabuf *hbq_buf)
1774{
James Smarted957682007-06-17 19:56:37 -05001775 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001776 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001777
1778 /* Get next HBQ entry slot to use */
1779 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1780 if (hbqe) {
1781 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1782
James Smart92d7f7b2007-06-17 19:56:38 -05001783 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1784 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001785 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001786 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001787 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1788 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1789 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001790 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1791 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001792 /* flush */
James Smarted957682007-06-17 19:56:37 -05001793 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001794 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001795 return 0;
1796 } else
1797 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001798}
1799
James Smart4f774512009-05-22 14:52:35 -04001800/**
1801 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1802 * @phba: Pointer to HBA context object.
1803 * @hbqno: HBQ number.
1804 * @hbq_buf: Pointer to HBQ buffer.
1805 *
1806 * This function is called with the hbalock held to post an RQE to the SLI4
1807 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1808 * the hbq_buffer_list and return zero, otherwise it will return an error.
1809 **/
1810static int
1811lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1812 struct hbq_dmabuf *hbq_buf)
1813{
1814 int rc;
1815 struct lpfc_rqe hrqe;
1816 struct lpfc_rqe drqe;
1817
1818 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1819 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1820 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1821 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1822 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1823 &hrqe, &drqe);
1824 if (rc < 0)
1825 return rc;
1826 hbq_buf->tag = rc;
1827 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1828 return 0;
1829}
1830
James Smarte59058c2008-08-24 21:49:00 -04001831/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001832static struct lpfc_hbq_init lpfc_els_hbq = {
1833 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001834 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001835 .mask_count = 0,
1836 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001837 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001838 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001839 .init_count = 40,
1840 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001841};
James Smarted957682007-06-17 19:56:37 -05001842
James Smarte59058c2008-08-24 21:49:00 -04001843/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001844static struct lpfc_hbq_init lpfc_extra_hbq = {
1845 .rn = 1,
1846 .entry_count = 200,
1847 .mask_count = 0,
1848 .profile = 0,
1849 .ring_mask = (1 << LPFC_EXTRA_RING),
1850 .buffer_count = 0,
1851 .init_count = 0,
1852 .add_count = 5,
1853};
1854
James Smarte59058c2008-08-24 21:49:00 -04001855/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001856struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001857 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001858 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001859};
1860
James Smarte59058c2008-08-24 21:49:00 -04001861/**
James Smart3621a712009-04-06 18:47:14 -04001862 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001863 * @phba: Pointer to HBA context object.
1864 * @hbqno: HBQ number.
1865 * @count: Number of HBQ buffers to be posted.
1866 *
James Smartd7c255b2008-08-24 21:50:00 -04001867 * This function is called with no lock held to post more hbq buffers to the
1868 * given HBQ. The function returns the number of HBQ buffers successfully
1869 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001870 **/
James Smart311464e2007-08-02 11:10:37 -04001871static int
James Smart92d7f7b2007-06-17 19:56:38 -05001872lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1873{
James Smartd7c255b2008-08-24 21:50:00 -04001874 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001875 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001876 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001877 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001878 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001879 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001880
James Smartd7c255b2008-08-24 21:50:00 -04001881 if ((phba->hbqs[hbqno].buffer_count + count) >
1882 lpfc_hbq_defs[hbqno]->entry_count)
1883 count = lpfc_hbq_defs[hbqno]->entry_count -
1884 phba->hbqs[hbqno].buffer_count;
1885 if (!count)
1886 return 0;
1887 /* Allocate HBQ entries */
1888 for (i = 0; i < count; i++) {
1889 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1890 if (!hbq_buffer)
1891 break;
1892 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1893 }
James Smart3163f722008-02-08 18:50:25 -05001894 /* Check whether HBQ is still in use */
1895 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001896 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001897 goto err;
1898 while (!list_empty(&hbq_buf_list)) {
1899 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1900 dbuf.list);
1901 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1902 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001903 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001904 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001905 posted++;
1906 } else
James Smart51ef4c22007-08-02 11:10:31 -04001907 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001908 }
James Smart3163f722008-02-08 18:50:25 -05001909 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001910 return posted;
1911err:
1912 spin_unlock_irqrestore(&phba->hbalock, flags);
1913 while (!list_empty(&hbq_buf_list)) {
1914 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1915 dbuf.list);
1916 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1917 }
James Smart92d7f7b2007-06-17 19:56:38 -05001918 return 0;
James Smarted957682007-06-17 19:56:37 -05001919}
1920
James Smarte59058c2008-08-24 21:49:00 -04001921/**
James Smart3621a712009-04-06 18:47:14 -04001922 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001923 * @phba: Pointer to HBA context object.
1924 * @qno: HBQ number.
1925 *
1926 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001927 * is called with no lock held. The function returns the number of HBQ entries
1928 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001929 **/
James Smarted957682007-06-17 19:56:37 -05001930int
James Smart92d7f7b2007-06-17 19:56:38 -05001931lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001932{
James Smartdef9c7a2009-12-21 17:02:28 -05001933 if (phba->sli_rev == LPFC_SLI_REV4)
1934 return 0;
1935 else
1936 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1937 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001938}
1939
James Smarte59058c2008-08-24 21:49:00 -04001940/**
James Smart3621a712009-04-06 18:47:14 -04001941 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001942 * @phba: Pointer to HBA context object.
1943 * @qno: HBQ queue number.
1944 *
1945 * This function is called from SLI initialization code path with
1946 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001947 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001948 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001949static int
James Smart92d7f7b2007-06-17 19:56:38 -05001950lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001951{
James Smartdef9c7a2009-12-21 17:02:28 -05001952 if (phba->sli_rev == LPFC_SLI_REV4)
1953 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001954 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001955 else
1956 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1957 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001958}
1959
James Smarte59058c2008-08-24 21:49:00 -04001960/**
James Smart3772a992009-05-22 14:50:54 -04001961 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1962 * @phba: Pointer to HBA context object.
1963 * @hbqno: HBQ number.
1964 *
1965 * This function removes the first hbq buffer on an hbq list and returns a
1966 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1967 **/
1968static struct hbq_dmabuf *
1969lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1970{
1971 struct lpfc_dmabuf *d_buf;
1972
1973 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1974 if (!d_buf)
1975 return NULL;
1976 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1977}
1978
1979/**
James Smart3621a712009-04-06 18:47:14 -04001980 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001981 * @phba: Pointer to HBA context object.
1982 * @tag: Tag of the hbq buffer.
1983 *
1984 * This function is called with hbalock held. This function searches
1985 * for the hbq buffer associated with the given tag in the hbq buffer
1986 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1987 * it returns NULL.
1988 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001989static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001990lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1991{
James Smart92d7f7b2007-06-17 19:56:38 -05001992 struct lpfc_dmabuf *d_buf;
1993 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001994 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001995
James Smart51ef4c22007-08-02 11:10:31 -04001996 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001997 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001998 return NULL;
1999
James Smart3772a992009-05-22 14:50:54 -04002000 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002001 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002002 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002003 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002004 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002005 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002006 }
2007 }
James Smart3772a992009-05-22 14:50:54 -04002008 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002010 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002011 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002012 return NULL;
James Smarted957682007-06-17 19:56:37 -05002013}
2014
James Smarte59058c2008-08-24 21:49:00 -04002015/**
James Smart3621a712009-04-06 18:47:14 -04002016 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002017 * @phba: Pointer to HBA context object.
2018 * @hbq_buffer: Pointer to HBQ buffer.
2019 *
2020 * This function is called with hbalock. This function gives back
2021 * the hbq buffer to firmware. If the HBQ does not have space to
2022 * post the buffer, it will free the buffer.
2023 **/
James Smarted957682007-06-17 19:56:37 -05002024void
James Smart51ef4c22007-08-02 11:10:31 -04002025lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002026{
2027 uint32_t hbqno;
2028
James Smart51ef4c22007-08-02 11:10:31 -04002029 if (hbq_buffer) {
2030 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002031 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002032 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002033 }
2034}
2035
James Smarte59058c2008-08-24 21:49:00 -04002036/**
James Smart3621a712009-04-06 18:47:14 -04002037 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002038 * @mbxCommand: mailbox command code.
2039 *
2040 * This function is called by the mailbox event handler function to verify
2041 * that the completed mailbox command is a legitimate mailbox command. If the
2042 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2043 * and the mailbox event handler will take the HBA offline.
2044 **/
dea31012005-04-17 16:05:31 -05002045static int
2046lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2047{
2048 uint8_t ret;
2049
2050 switch (mbxCommand) {
2051 case MBX_LOAD_SM:
2052 case MBX_READ_NV:
2053 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002054 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002055 case MBX_RUN_BIU_DIAG:
2056 case MBX_INIT_LINK:
2057 case MBX_DOWN_LINK:
2058 case MBX_CONFIG_LINK:
2059 case MBX_CONFIG_RING:
2060 case MBX_RESET_RING:
2061 case MBX_READ_CONFIG:
2062 case MBX_READ_RCONFIG:
2063 case MBX_READ_SPARM:
2064 case MBX_READ_STATUS:
2065 case MBX_READ_RPI:
2066 case MBX_READ_XRI:
2067 case MBX_READ_REV:
2068 case MBX_READ_LNK_STAT:
2069 case MBX_REG_LOGIN:
2070 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002071 case MBX_CLEAR_LA:
2072 case MBX_DUMP_MEMORY:
2073 case MBX_DUMP_CONTEXT:
2074 case MBX_RUN_DIAGS:
2075 case MBX_RESTART:
2076 case MBX_UPDATE_CFG:
2077 case MBX_DOWN_LOAD:
2078 case MBX_DEL_LD_ENTRY:
2079 case MBX_RUN_PROGRAM:
2080 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002081 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002082 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002083 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002084 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002085 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002086 case MBX_LOAD_AREA:
2087 case MBX_RUN_BIU_DIAG64:
2088 case MBX_CONFIG_PORT:
2089 case MBX_READ_SPARM64:
2090 case MBX_READ_RPI64:
2091 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002092 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002093 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002094 case MBX_SET_DEBUG:
2095 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002096 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002097 case MBX_REG_VPI:
2098 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002099 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002100 case MBX_PORT_CAPABILITIES:
2101 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002102 case MBX_SLI4_CONFIG:
2103 case MBX_SLI4_REQ_FTRS:
2104 case MBX_REG_FCFI:
2105 case MBX_UNREG_FCFI:
2106 case MBX_REG_VFI:
2107 case MBX_UNREG_VFI:
2108 case MBX_INIT_VPI:
2109 case MBX_INIT_VFI:
2110 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002111 case MBX_READ_EVENT_LOG_STATUS:
2112 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002113 case MBX_SECURITY_MGMT:
2114 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002115 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002116 ret = mbxCommand;
2117 break;
2118 default:
2119 ret = MBX_SHUTDOWN;
2120 break;
2121 }
James Smart2e0fef82007-06-17 19:56:36 -05002122 return ret;
dea31012005-04-17 16:05:31 -05002123}
James Smarte59058c2008-08-24 21:49:00 -04002124
2125/**
James Smart3621a712009-04-06 18:47:14 -04002126 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002127 * @phba: Pointer to HBA context object.
2128 * @pmboxq: Pointer to mailbox command.
2129 *
2130 * This is completion handler function for mailbox commands issued from
2131 * lpfc_sli_issue_mbox_wait function. This function is called by the
2132 * mailbox event handler function with no lock held. This function
2133 * will wake up thread waiting on the wait queue pointed by context1
2134 * of the mailbox.
2135 **/
James Smart04c68492009-05-22 14:52:52 -04002136void
James Smart2e0fef82007-06-17 19:56:36 -05002137lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002138{
2139 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002140 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002141
2142 /*
2143 * If pdone_q is empty, the driver thread gave up waiting and
2144 * continued running.
2145 */
James Smart7054a602007-04-25 09:52:34 -04002146 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002147 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002148 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2149 if (pdone_q)
2150 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002151 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002152 return;
2153}
2154
James Smarte59058c2008-08-24 21:49:00 -04002155
2156/**
James Smart3621a712009-04-06 18:47:14 -04002157 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002158 * @phba: Pointer to HBA context object.
2159 * @pmb: Pointer to mailbox object.
2160 *
2161 * This function is the default mailbox completion handler. It
2162 * frees the memory resources associated with the completed mailbox
2163 * command. If the completed command is a REG_LOGIN mailbox command,
2164 * this function will issue a UREG_LOGIN to re-claim the RPI.
2165 **/
dea31012005-04-17 16:05:31 -05002166void
James Smart2e0fef82007-06-17 19:56:36 -05002167lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002168{
James Smartd439d282010-09-29 11:18:45 -04002169 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002170 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002171 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002172 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002173 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002174 int rc;
2175
dea31012005-04-17 16:05:31 -05002176 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002177
dea31012005-04-17 16:05:31 -05002178 if (mp) {
2179 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2180 kfree(mp);
2181 }
James Smart7054a602007-04-25 09:52:34 -04002182
2183 /*
2184 * If a REG_LOGIN succeeded after node is destroyed or node
2185 * is in re-discovery driver need to cleanup the RPI.
2186 */
James Smart2e0fef82007-06-17 19:56:36 -05002187 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002188 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2189 !pmb->u.mb.mbxStatus) {
2190 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002191 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002192 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002193 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002194 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2195 if (rc != MBX_NOT_FINISHED)
2196 return;
2197 }
2198
James Smart695a8142010-01-26 23:08:03 -05002199 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2200 !(phba->pport->load_flag & FC_UNLOADING) &&
2201 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002202 shost = lpfc_shost_from_vport(vport);
2203 spin_lock_irq(shost->host_lock);
2204 vport->vpi_state |= LPFC_VPI_REGISTERED;
2205 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2206 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002207 }
2208
James Smartd439d282010-09-29 11:18:45 -04002209 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2210 ndlp = (struct lpfc_nodelist *)pmb->context2;
2211 lpfc_nlp_put(ndlp);
2212 pmb->context2 = NULL;
2213 }
2214
James Smartdcf2a4e2010-09-29 11:18:53 -04002215 /* Check security permission status on INIT_LINK mailbox command */
2216 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2217 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2218 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2219 "2860 SLI authentication is required "
2220 "for INIT_LINK but has not done yet\n");
2221
James Smart04c68492009-05-22 14:52:52 -04002222 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2223 lpfc_sli4_mbox_cmd_free(phba, pmb);
2224 else
2225 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002226}
2227
James Smarte59058c2008-08-24 21:49:00 -04002228/**
James Smart3621a712009-04-06 18:47:14 -04002229 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002230 * @phba: Pointer to HBA context object.
2231 *
2232 * This function is called with no lock held. This function processes all
2233 * the completed mailbox commands and gives it to upper layers. The interrupt
2234 * service routine processes mailbox completion interrupt and adds completed
2235 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2236 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2237 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2238 * function returns the mailbox commands to the upper layer by calling the
2239 * completion handler function of each mailbox.
2240 **/
dea31012005-04-17 16:05:31 -05002241int
James Smart2e0fef82007-06-17 19:56:36 -05002242lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002243{
James Smart92d7f7b2007-06-17 19:56:38 -05002244 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002245 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002246 int rc;
2247 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002248
2249 phba->sli.slistat.mbox_event++;
2250
James Smart92d7f7b2007-06-17 19:56:38 -05002251 /* Get all completed mailboxe buffers into the cmplq */
2252 spin_lock_irq(&phba->hbalock);
2253 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2254 spin_unlock_irq(&phba->hbalock);
2255
dea31012005-04-17 16:05:31 -05002256 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002257 do {
2258 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2259 if (pmb == NULL)
2260 break;
2261
James Smart04c68492009-05-22 14:52:52 -04002262 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002263
James Smart858c9f62007-06-17 19:56:39 -05002264 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2265 if (pmb->vport) {
2266 lpfc_debugfs_disc_trc(pmb->vport,
2267 LPFC_DISC_TRC_MBOX_VPORT,
2268 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2269 (uint32_t)pmbox->mbxCommand,
2270 pmbox->un.varWords[0],
2271 pmbox->un.varWords[1]);
2272 }
2273 else {
2274 lpfc_debugfs_disc_trc(phba->pport,
2275 LPFC_DISC_TRC_MBOX,
2276 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2277 (uint32_t)pmbox->mbxCommand,
2278 pmbox->un.varWords[0],
2279 pmbox->un.varWords[1]);
2280 }
2281 }
2282
dea31012005-04-17 16:05:31 -05002283 /*
2284 * It is a fatal error if unknown mbox command completion.
2285 */
2286 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2287 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002288 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002289 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002290 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002291 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002292 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002293 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002294 lpfc_sli_config_mbox_subsys_get(phba,
2295 pmb),
2296 lpfc_sli_config_mbox_opcode_get(phba,
2297 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002298 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002299 phba->work_hs = HS_FFER3;
2300 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002301 continue;
dea31012005-04-17 16:05:31 -05002302 }
2303
dea31012005-04-17 16:05:31 -05002304 if (pmbox->mbxStatus) {
2305 phba->sli.slistat.mbox_stat_err++;
2306 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2307 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002308 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002309 LOG_MBOX | LOG_SLI,
2310 "(%d):0305 Mbox cmd cmpl "
2311 "error - RETRYing Data: x%x "
2312 "(x%x/x%x) x%x x%x x%x\n",
2313 pmb->vport ? pmb->vport->vpi : 0,
2314 pmbox->mbxCommand,
2315 lpfc_sli_config_mbox_subsys_get(phba,
2316 pmb),
2317 lpfc_sli_config_mbox_opcode_get(phba,
2318 pmb),
2319 pmbox->mbxStatus,
2320 pmbox->un.varWords[0],
2321 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002322 pmbox->mbxStatus = 0;
2323 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002324 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002325 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002326 continue;
dea31012005-04-17 16:05:31 -05002327 }
2328 }
2329
2330 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002331 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002332 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002333 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2334 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002335 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002336 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002337 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2338 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002339 pmb->mbox_cmpl,
2340 *((uint32_t *) pmbox),
2341 pmbox->un.varWords[0],
2342 pmbox->un.varWords[1],
2343 pmbox->un.varWords[2],
2344 pmbox->un.varWords[3],
2345 pmbox->un.varWords[4],
2346 pmbox->un.varWords[5],
2347 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002348 pmbox->un.varWords[7],
2349 pmbox->un.varWords[8],
2350 pmbox->un.varWords[9],
2351 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002352
James Smart92d7f7b2007-06-17 19:56:38 -05002353 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002354 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002355 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002356 return 0;
dea31012005-04-17 16:05:31 -05002357}
James Smart92d7f7b2007-06-17 19:56:38 -05002358
James Smarte59058c2008-08-24 21:49:00 -04002359/**
James Smart3621a712009-04-06 18:47:14 -04002360 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002361 * @phba: Pointer to HBA context object.
2362 * @pring: Pointer to driver SLI ring object.
2363 * @tag: buffer tag.
2364 *
2365 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2366 * is set in the tag the buffer is posted for a particular exchange,
2367 * the function will return the buffer without replacing the buffer.
2368 * If the buffer is for unsolicited ELS or CT traffic, this function
2369 * returns the buffer and also posts another buffer to the firmware.
2370 **/
James Smart76bb24e2007-10-27 13:38:00 -04002371static struct lpfc_dmabuf *
2372lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002373 struct lpfc_sli_ring *pring,
2374 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002375{
James Smart9f1e1b52008-12-04 22:39:40 -05002376 struct hbq_dmabuf *hbq_entry;
2377
James Smart76bb24e2007-10-27 13:38:00 -04002378 if (tag & QUE_BUFTAG_BIT)
2379 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002380 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2381 if (!hbq_entry)
2382 return NULL;
2383 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002384}
James Smart57127f12007-10-27 13:37:05 -04002385
James Smart3772a992009-05-22 14:50:54 -04002386/**
2387 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2388 * @phba: Pointer to HBA context object.
2389 * @pring: Pointer to driver SLI ring object.
2390 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2391 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2392 * @fch_type: the type for the first frame of the sequence.
2393 *
2394 * This function is called with no lock held. This function uses the r_ctl and
2395 * type of the received sequence to find the correct callback function to call
2396 * to process the sequence.
2397 **/
2398static int
2399lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2400 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2401 uint32_t fch_type)
2402{
2403 int i;
2404
2405 /* unSolicited Responses */
2406 if (pring->prt[0].profile) {
2407 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2408 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2409 saveq);
2410 return 1;
2411 }
2412 /* We must search, based on rctl / type
2413 for the right routine */
2414 for (i = 0; i < pring->num_mask; i++) {
2415 if ((pring->prt[i].rctl == fch_r_ctl) &&
2416 (pring->prt[i].type == fch_type)) {
2417 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2418 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2419 (phba, pring, saveq);
2420 return 1;
2421 }
2422 }
2423 return 0;
2424}
James Smarte59058c2008-08-24 21:49:00 -04002425
2426/**
James Smart3621a712009-04-06 18:47:14 -04002427 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002428 * @phba: Pointer to HBA context object.
2429 * @pring: Pointer to driver SLI ring object.
2430 * @saveq: Pointer to the unsolicited iocb.
2431 *
2432 * This function is called with no lock held by the ring event handler
2433 * when there is an unsolicited iocb posted to the response ring by the
2434 * firmware. This function gets the buffer associated with the iocbs
2435 * and calls the event handler for the ring. This function handles both
2436 * qring buffers and hbq buffers.
2437 * When the function returns 1 the caller can free the iocb object otherwise
2438 * upper layer functions will free the iocb objects.
2439 **/
dea31012005-04-17 16:05:31 -05002440static int
2441lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2442 struct lpfc_iocbq *saveq)
2443{
2444 IOCB_t * irsp;
2445 WORD5 * w5p;
2446 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002447 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002448 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002449 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002450
2451 match = 0;
2452 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002453
2454 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2455 if (pring->lpfc_sli_rcv_async_status)
2456 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2457 else
2458 lpfc_printf_log(phba,
2459 KERN_WARNING,
2460 LOG_SLI,
2461 "0316 Ring %d handler: unexpected "
2462 "ASYNC_STATUS iocb received evt_code "
2463 "0x%x\n",
2464 pring->ringno,
2465 irsp->un.asyncstat.evt_code);
2466 return 1;
2467 }
2468
James Smart3163f722008-02-08 18:50:25 -05002469 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2470 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2471 if (irsp->ulpBdeCount > 0) {
2472 dmzbuf = lpfc_sli_get_buff(phba, pring,
2473 irsp->un.ulpWord[3]);
2474 lpfc_in_buf_free(phba, dmzbuf);
2475 }
2476
2477 if (irsp->ulpBdeCount > 1) {
2478 dmzbuf = lpfc_sli_get_buff(phba, pring,
2479 irsp->unsli3.sli3Words[3]);
2480 lpfc_in_buf_free(phba, dmzbuf);
2481 }
2482
2483 if (irsp->ulpBdeCount > 2) {
2484 dmzbuf = lpfc_sli_get_buff(phba, pring,
2485 irsp->unsli3.sli3Words[7]);
2486 lpfc_in_buf_free(phba, dmzbuf);
2487 }
2488
2489 return 1;
2490 }
2491
James Smart92d7f7b2007-06-17 19:56:38 -05002492 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002493 if (irsp->ulpBdeCount != 0) {
2494 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002495 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002496 if (!saveq->context2)
2497 lpfc_printf_log(phba,
2498 KERN_ERR,
2499 LOG_SLI,
2500 "0341 Ring %d Cannot find buffer for "
2501 "an unsolicited iocb. tag 0x%x\n",
2502 pring->ringno,
2503 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002504 }
2505 if (irsp->ulpBdeCount == 2) {
2506 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002507 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002508 if (!saveq->context3)
2509 lpfc_printf_log(phba,
2510 KERN_ERR,
2511 LOG_SLI,
2512 "0342 Ring %d Cannot find buffer for an"
2513 " unsolicited iocb. tag 0x%x\n",
2514 pring->ringno,
2515 irsp->unsli3.sli3Words[7]);
2516 }
2517 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002518 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002519 if (irsp->ulpBdeCount != 0) {
2520 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2521 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002522 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002523 lpfc_printf_log(phba,
2524 KERN_ERR,
2525 LOG_SLI,
2526 "0343 Ring %d Cannot find "
2527 "buffer for an unsolicited iocb"
2528 ". tag 0x%x\n", pring->ringno,
2529 irsp->un.ulpWord[3]);
2530 }
2531 if (irsp->ulpBdeCount == 2) {
2532 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2533 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002534 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002535 lpfc_printf_log(phba,
2536 KERN_ERR,
2537 LOG_SLI,
2538 "0344 Ring %d Cannot find "
2539 "buffer for an unsolicited "
2540 "iocb. tag 0x%x\n",
2541 pring->ringno,
2542 irsp->unsli3.sli3Words[7]);
2543 }
2544 }
James Smart92d7f7b2007-06-17 19:56:38 -05002545 }
James Smart9c2face2008-01-11 01:53:18 -05002546 if (irsp->ulpBdeCount != 0 &&
2547 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2548 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2549 int found = 0;
2550
2551 /* search continue save q for same XRI */
2552 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002553 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2554 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002555 list_add_tail(&saveq->list, &iocbq->list);
2556 found = 1;
2557 break;
2558 }
2559 }
2560 if (!found)
2561 list_add_tail(&saveq->clist,
2562 &pring->iocb_continue_saveq);
2563 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2564 list_del_init(&iocbq->clist);
2565 saveq = iocbq;
2566 irsp = &(saveq->iocb);
2567 } else
2568 return 0;
2569 }
2570 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2571 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2572 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002573 Rctl = FC_RCTL_ELS_REQ;
2574 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002575 } else {
2576 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2577 Rctl = w5p->hcsw.Rctl;
2578 Type = w5p->hcsw.Type;
2579
2580 /* Firmware Workaround */
2581 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2582 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2583 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002584 Rctl = FC_RCTL_ELS_REQ;
2585 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002586 w5p->hcsw.Rctl = Rctl;
2587 w5p->hcsw.Type = Type;
2588 }
2589 }
James Smart92d7f7b2007-06-17 19:56:38 -05002590
James Smart3772a992009-05-22 14:50:54 -04002591 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002592 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002593 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002594 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002595 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002596
James Smart92d7f7b2007-06-17 19:56:38 -05002597 return 1;
dea31012005-04-17 16:05:31 -05002598}
2599
James Smarte59058c2008-08-24 21:49:00 -04002600/**
James Smart3621a712009-04-06 18:47:14 -04002601 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002602 * @phba: Pointer to HBA context object.
2603 * @pring: Pointer to driver SLI ring object.
2604 * @prspiocb: Pointer to response iocb object.
2605 *
2606 * This function looks up the iocb_lookup table to get the command iocb
2607 * corresponding to the given response iocb using the iotag of the
2608 * response iocb. This function is called with the hbalock held.
2609 * This function returns the command iocb object if it finds the command
2610 * iocb else returns NULL.
2611 **/
dea31012005-04-17 16:05:31 -05002612static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002613lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2614 struct lpfc_sli_ring *pring,
2615 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002616{
dea31012005-04-17 16:05:31 -05002617 struct lpfc_iocbq *cmd_iocb = NULL;
2618 uint16_t iotag;
2619
James Bottomley604a3e32005-10-29 10:28:33 -05002620 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002621
James Bottomley604a3e32005-10-29 10:28:33 -05002622 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2623 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002624 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002625 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002626 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002627 }
James Bottomley604a3e32005-10-29 10:28:33 -05002628 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002629 }
2630
dea31012005-04-17 16:05:31 -05002631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002632 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002633 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002634 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002635 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002636 return NULL;
2637}
2638
James Smarte59058c2008-08-24 21:49:00 -04002639/**
James Smart3772a992009-05-22 14:50:54 -04002640 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2641 * @phba: Pointer to HBA context object.
2642 * @pring: Pointer to driver SLI ring object.
2643 * @iotag: IOCB tag.
2644 *
2645 * This function looks up the iocb_lookup table to get the command iocb
2646 * corresponding to the given iotag. This function is called with the
2647 * hbalock held.
2648 * This function returns the command iocb object if it finds the command
2649 * iocb else returns NULL.
2650 **/
2651static struct lpfc_iocbq *
2652lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2653 struct lpfc_sli_ring *pring, uint16_t iotag)
2654{
2655 struct lpfc_iocbq *cmd_iocb;
2656
2657 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2658 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002659 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2660 /* remove from txcmpl queue list */
2661 list_del_init(&cmd_iocb->list);
2662 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002663 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002664 }
James Smart3772a992009-05-22 14:50:54 -04002665 }
James Smart3772a992009-05-22 14:50:54 -04002666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2667 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2668 iotag, phba->sli.last_iotag);
2669 return NULL;
2670}
2671
2672/**
James Smart3621a712009-04-06 18:47:14 -04002673 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002674 * @phba: Pointer to HBA context object.
2675 * @pring: Pointer to driver SLI ring object.
2676 * @saveq: Pointer to the response iocb to be processed.
2677 *
2678 * This function is called by the ring event handler for non-fcp
2679 * rings when there is a new response iocb in the response ring.
2680 * The caller is not required to hold any locks. This function
2681 * gets the command iocb associated with the response iocb and
2682 * calls the completion handler for the command iocb. If there
2683 * is no completion handler, the function will free the resources
2684 * associated with command iocb. If the response iocb is for
2685 * an already aborted command iocb, the status of the completion
2686 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2687 * This function always returns 1.
2688 **/
dea31012005-04-17 16:05:31 -05002689static int
James Smart2e0fef82007-06-17 19:56:36 -05002690lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002691 struct lpfc_iocbq *saveq)
2692{
James Smart2e0fef82007-06-17 19:56:36 -05002693 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002694 int rc = 1;
2695 unsigned long iflag;
2696
2697 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002698 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002699 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002700 spin_unlock_irqrestore(&phba->hbalock, iflag);
2701
dea31012005-04-17 16:05:31 -05002702 if (cmdiocbp) {
2703 if (cmdiocbp->iocb_cmpl) {
2704 /*
James Smartea2151b2008-09-07 11:52:10 -04002705 * If an ELS command failed send an event to mgmt
2706 * application.
2707 */
2708 if (saveq->iocb.ulpStatus &&
2709 (pring->ringno == LPFC_ELS_RING) &&
2710 (cmdiocbp->iocb.ulpCommand ==
2711 CMD_ELS_REQUEST64_CR))
2712 lpfc_send_els_failure_event(phba,
2713 cmdiocbp, saveq);
2714
2715 /*
dea31012005-04-17 16:05:31 -05002716 * Post all ELS completions to the worker thread.
2717 * All other are passed to the completion callback.
2718 */
2719 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002720 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2721 (cmdiocbp->iocb_flag &
2722 LPFC_DRIVER_ABORTED)) {
2723 spin_lock_irqsave(&phba->hbalock,
2724 iflag);
James Smart07951072007-04-25 09:51:38 -04002725 cmdiocbp->iocb_flag &=
2726 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002727 spin_unlock_irqrestore(&phba->hbalock,
2728 iflag);
James Smart07951072007-04-25 09:51:38 -04002729 saveq->iocb.ulpStatus =
2730 IOSTAT_LOCAL_REJECT;
2731 saveq->iocb.un.ulpWord[4] =
2732 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002733
2734 /* Firmware could still be in progress
2735 * of DMAing payload, so don't free data
2736 * buffer till after a hbeat.
2737 */
James Smart341af102010-01-26 23:07:37 -05002738 spin_lock_irqsave(&phba->hbalock,
2739 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002740 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002741 spin_unlock_irqrestore(&phba->hbalock,
2742 iflag);
2743 }
James Smart0f65ff62010-02-26 14:14:23 -05002744 if (phba->sli_rev == LPFC_SLI_REV4) {
2745 if (saveq->iocb_flag &
2746 LPFC_EXCHANGE_BUSY) {
2747 /* Set cmdiocb flag for the
2748 * exchange busy so sgl (xri)
2749 * will not be released until
2750 * the abort xri is received
2751 * from hba.
2752 */
2753 spin_lock_irqsave(
2754 &phba->hbalock, iflag);
2755 cmdiocbp->iocb_flag |=
2756 LPFC_EXCHANGE_BUSY;
2757 spin_unlock_irqrestore(
2758 &phba->hbalock, iflag);
2759 }
2760 if (cmdiocbp->iocb_flag &
2761 LPFC_DRIVER_ABORTED) {
2762 /*
2763 * Clear LPFC_DRIVER_ABORTED
2764 * bit in case it was driver
2765 * initiated abort.
2766 */
2767 spin_lock_irqsave(
2768 &phba->hbalock, iflag);
2769 cmdiocbp->iocb_flag &=
2770 ~LPFC_DRIVER_ABORTED;
2771 spin_unlock_irqrestore(
2772 &phba->hbalock, iflag);
2773 cmdiocbp->iocb.ulpStatus =
2774 IOSTAT_LOCAL_REJECT;
2775 cmdiocbp->iocb.un.ulpWord[4] =
2776 IOERR_ABORT_REQUESTED;
2777 /*
2778 * For SLI4, irsiocb contains
2779 * NO_XRI in sli_xritag, it
2780 * shall not affect releasing
2781 * sgl (xri) process.
2782 */
2783 saveq->iocb.ulpStatus =
2784 IOSTAT_LOCAL_REJECT;
2785 saveq->iocb.un.ulpWord[4] =
2786 IOERR_SLI_ABORTED;
2787 spin_lock_irqsave(
2788 &phba->hbalock, iflag);
2789 saveq->iocb_flag |=
2790 LPFC_DELAY_MEM_FREE;
2791 spin_unlock_irqrestore(
2792 &phba->hbalock, iflag);
2793 }
James Smart07951072007-04-25 09:51:38 -04002794 }
dea31012005-04-17 16:05:31 -05002795 }
James Smart2e0fef82007-06-17 19:56:36 -05002796 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002797 } else
2798 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002799 } else {
2800 /*
2801 * Unknown initiating command based on the response iotag.
2802 * This could be the case on the ELS ring because of
2803 * lpfc_els_abort().
2804 */
2805 if (pring->ringno != LPFC_ELS_RING) {
2806 /*
2807 * Ring <ringno> handler: unexpected completion IoTag
2808 * <IoTag>
2809 */
James Smarta257bf92009-04-06 18:48:10 -04002810 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002811 "0322 Ring %d handler: "
2812 "unexpected completion IoTag x%x "
2813 "Data: x%x x%x x%x x%x\n",
2814 pring->ringno,
2815 saveq->iocb.ulpIoTag,
2816 saveq->iocb.ulpStatus,
2817 saveq->iocb.un.ulpWord[4],
2818 saveq->iocb.ulpCommand,
2819 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002820 }
2821 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002822
dea31012005-04-17 16:05:31 -05002823 return rc;
2824}
2825
James Smarte59058c2008-08-24 21:49:00 -04002826/**
James Smart3621a712009-04-06 18:47:14 -04002827 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002828 * @phba: Pointer to HBA context object.
2829 * @pring: Pointer to driver SLI ring object.
2830 *
2831 * This function is called from the iocb ring event handlers when
2832 * put pointer is ahead of the get pointer for a ring. This function signal
2833 * an error attention condition to the worker thread and the worker
2834 * thread will transition the HBA to offline state.
2835 **/
James Smart2e0fef82007-06-17 19:56:36 -05002836static void
2837lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002838{
James Smart34b02dc2008-08-24 21:49:55 -04002839 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002840 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002841 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002842 * rsp ring <portRspMax>
2843 */
2844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002845 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002846 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002847 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002848 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002849
James Smart2e0fef82007-06-17 19:56:36 -05002850 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002851
2852 /*
2853 * All error attention handlers are posted to
2854 * worker thread
2855 */
2856 phba->work_ha |= HA_ERATT;
2857 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002858
James Smart5e9d9b82008-06-14 22:52:53 -04002859 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002860
2861 return;
2862}
2863
James Smarte59058c2008-08-24 21:49:00 -04002864/**
James Smart3621a712009-04-06 18:47:14 -04002865 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002866 * @ptr: Pointer to address of HBA context object.
2867 *
2868 * This function is invoked by the Error Attention polling timer when the
2869 * timer times out. It will check the SLI Error Attention register for
2870 * possible attention events. If so, it will post an Error Attention event
2871 * and wake up worker thread to process it. Otherwise, it will set up the
2872 * Error Attention polling timer for the next poll.
2873 **/
2874void lpfc_poll_eratt(unsigned long ptr)
2875{
2876 struct lpfc_hba *phba;
James Smartaa6fbb72012-08-03 12:36:03 -04002877 uint32_t eratt = 0, rem;
2878 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002879
2880 phba = (struct lpfc_hba *)ptr;
2881
James Smartaa6fbb72012-08-03 12:36:03 -04002882 /* Here we will also keep track of interrupts per sec of the hba */
2883 sli_intr = phba->sli.slistat.sli_intr;
2884
2885 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2886 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2887 sli_intr);
2888 else
2889 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2890
2891 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2892 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2893 phba->sli.slistat.sli_ips = cnt;
2894
2895 phba->sli.slistat.sli_prev_intr = sli_intr;
2896
James Smart93996272008-08-24 21:50:30 -04002897 /* Check chip HA register for error event */
2898 eratt = lpfc_sli_check_eratt(phba);
2899
2900 if (eratt)
2901 /* Tell the worker thread there is work to do */
2902 lpfc_worker_wake_up(phba);
2903 else
2904 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002905 mod_timer(&phba->eratt_poll,
2906 jiffies +
2907 msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smart93996272008-08-24 21:50:30 -04002908 return;
2909}
2910
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002911
James Smarte59058c2008-08-24 21:49:00 -04002912/**
James Smart3621a712009-04-06 18:47:14 -04002913 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002914 * @phba: Pointer to HBA context object.
2915 * @pring: Pointer to driver SLI ring object.
2916 * @mask: Host attention register mask for this ring.
2917 *
2918 * This function is called from the interrupt context when there is a ring
2919 * event for the fcp ring. The caller does not hold any lock.
2920 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002921 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002922 * LE bit set. The function will call the completion handler of the command iocb
2923 * if the response iocb indicates a completion for a command iocb or it is
2924 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2925 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002926 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002927 * to check it explicitly.
2928 */
2929int
James Smart2e0fef82007-06-17 19:56:36 -05002930lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2931 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002932{
James Smart34b02dc2008-08-24 21:49:55 -04002933 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002934 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002935 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002936 struct lpfc_iocbq *cmdiocbq = NULL;
2937 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002938 uint32_t status;
2939 uint32_t portRspPut, portRspMax;
2940 int rc = 1;
2941 lpfc_iocb_type type;
2942 unsigned long iflag;
2943 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002944
James Smart2e0fef82007-06-17 19:56:36 -05002945 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002946 pring->stats.iocb_event++;
2947
dea31012005-04-17 16:05:31 -05002948 /*
2949 * The next available response entry should never exceed the maximum
2950 * entries. If it does, treat it as an adapter hardware error.
2951 */
James Smart7e56aa22012-08-03 12:35:34 -04002952 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002953 portRspPut = le32_to_cpu(pgp->rspPutInx);
2954 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002955 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002956 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002957 return 1;
2958 }
James Smart45ed1192009-10-02 15:17:02 -04002959 if (phba->fcp_ring_in_use) {
2960 spin_unlock_irqrestore(&phba->hbalock, iflag);
2961 return 1;
2962 } else
2963 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002964
2965 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002966 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002967 /*
2968 * Fetch an entry off the ring and copy it into a local data
2969 * structure. The copy involves a byte-swap since the
2970 * network byte order and pci byte orders are different.
2971 */
James Smarted957682007-06-17 19:56:37 -05002972 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002973 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002974
James Smart7e56aa22012-08-03 12:35:34 -04002975 if (++pring->sli.sli3.rspidx >= portRspMax)
2976 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002977
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002978 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2979 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002980 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002981 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002982 irsp = &rspiocbq.iocb;
2983
dea31012005-04-17 16:05:31 -05002984 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2985 pring->stats.iocb_rsp++;
2986 rsp_cmpl++;
2987
2988 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002989 /*
2990 * If resource errors reported from HBA, reduce
2991 * queuedepths of the SCSI device.
2992 */
2993 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04002994 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
2995 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002996 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002997 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002998 spin_lock_irqsave(&phba->hbalock, iflag);
2999 }
3000
dea31012005-04-17 16:05:31 -05003001 /* Rsp ring <ringno> error: IOCB */
3002 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003003 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003004 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003005 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003006 irsp->un.ulpWord[0],
3007 irsp->un.ulpWord[1],
3008 irsp->un.ulpWord[2],
3009 irsp->un.ulpWord[3],
3010 irsp->un.ulpWord[4],
3011 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003012 *(uint32_t *)&irsp->un1,
3013 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003014 }
3015
3016 switch (type) {
3017 case LPFC_ABORT_IOCB:
3018 case LPFC_SOL_IOCB:
3019 /*
3020 * Idle exchange closed via ABTS from port. No iocb
3021 * resources need to be recovered.
3022 */
3023 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003024 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003025 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003026 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003027 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003028 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003029 break;
3030 }
3031
James Bottomley604a3e32005-10-29 10:28:33 -05003032 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3033 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003034 if (unlikely(!cmdiocbq))
3035 break;
3036 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3037 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3038 if (cmdiocbq->iocb_cmpl) {
3039 spin_unlock_irqrestore(&phba->hbalock, iflag);
3040 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3041 &rspiocbq);
3042 spin_lock_irqsave(&phba->hbalock, iflag);
3043 }
dea31012005-04-17 16:05:31 -05003044 break;
James Smarta4bc3372006-12-02 13:34:16 -05003045 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003046 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003047 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003048 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003049 break;
dea31012005-04-17 16:05:31 -05003050 default:
3051 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3052 char adaptermsg[LPFC_MAX_ADPTMSG];
3053 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3054 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3055 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003056 dev_warn(&((phba->pcidev)->dev),
3057 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003058 phba->brd_no, adaptermsg);
3059 } else {
3060 /* Unknown IOCB command */
3061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003062 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003063 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003064 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003065 irsp->ulpStatus,
3066 irsp->ulpIoTag,
3067 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003068 }
3069 break;
3070 }
3071
3072 /*
3073 * The response IOCB has been processed. Update the ring
3074 * pointer in SLIM. If the port response put pointer has not
3075 * been updated, sync the pgp->rspPutInx and fetch the new port
3076 * response put pointer.
3077 */
James Smart7e56aa22012-08-03 12:35:34 -04003078 writel(pring->sli.sli3.rspidx,
3079 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003080
James Smart7e56aa22012-08-03 12:35:34 -04003081 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003082 portRspPut = le32_to_cpu(pgp->rspPutInx);
3083 }
3084
3085 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3086 pring->stats.iocb_rsp_full++;
3087 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3088 writel(status, phba->CAregaddr);
3089 readl(phba->CAregaddr);
3090 }
3091 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3092 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3093 pring->stats.iocb_cmd_empty++;
3094
3095 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003096 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003097 lpfc_sli_resume_iocb(phba, pring);
3098
3099 if ((pring->lpfc_sli_cmd_available))
3100 (pring->lpfc_sli_cmd_available) (phba, pring);
3101
3102 }
3103
James Smart45ed1192009-10-02 15:17:02 -04003104 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003105 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003106 return rc;
3107}
3108
James Smarte59058c2008-08-24 21:49:00 -04003109/**
James Smart3772a992009-05-22 14:50:54 -04003110 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3111 * @phba: Pointer to HBA context object.
3112 * @pring: Pointer to driver SLI ring object.
3113 * @rspiocbp: Pointer to driver response IOCB object.
3114 *
3115 * This function is called from the worker thread when there is a slow-path
3116 * response IOCB to process. This function chains all the response iocbs until
3117 * seeing the iocb with the LE bit set. The function will call
3118 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3119 * completion of a command iocb. The function will call the
3120 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3121 * The function frees the resources or calls the completion handler if this
3122 * iocb is an abort completion. The function returns NULL when the response
3123 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3124 * this function shall chain the iocb on to the iocb_continueq and return the
3125 * response iocb passed in.
3126 **/
3127static struct lpfc_iocbq *
3128lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3129 struct lpfc_iocbq *rspiocbp)
3130{
3131 struct lpfc_iocbq *saveq;
3132 struct lpfc_iocbq *cmdiocbp;
3133 struct lpfc_iocbq *next_iocb;
3134 IOCB_t *irsp = NULL;
3135 uint32_t free_saveq;
3136 uint8_t iocb_cmd_type;
3137 lpfc_iocb_type type;
3138 unsigned long iflag;
3139 int rc;
3140
3141 spin_lock_irqsave(&phba->hbalock, iflag);
3142 /* First add the response iocb to the countinueq list */
3143 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3144 pring->iocb_continueq_cnt++;
3145
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003146 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003147 irsp = &rspiocbp->iocb;
3148 if (irsp->ulpLe) {
3149 /*
3150 * By default, the driver expects to free all resources
3151 * associated with this iocb completion.
3152 */
3153 free_saveq = 1;
3154 saveq = list_get_first(&pring->iocb_continueq,
3155 struct lpfc_iocbq, list);
3156 irsp = &(saveq->iocb);
3157 list_del_init(&pring->iocb_continueq);
3158 pring->iocb_continueq_cnt = 0;
3159
3160 pring->stats.iocb_rsp++;
3161
3162 /*
3163 * If resource errors reported from HBA, reduce
3164 * queuedepths of the SCSI device.
3165 */
3166 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003167 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3168 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003169 spin_unlock_irqrestore(&phba->hbalock, iflag);
3170 phba->lpfc_rampdown_queue_depth(phba);
3171 spin_lock_irqsave(&phba->hbalock, iflag);
3172 }
3173
3174 if (irsp->ulpStatus) {
3175 /* Rsp ring <ringno> error: IOCB */
3176 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3177 "0328 Rsp Ring %d error: "
3178 "IOCB Data: "
3179 "x%x x%x x%x x%x "
3180 "x%x x%x x%x x%x "
3181 "x%x x%x x%x x%x "
3182 "x%x x%x x%x x%x\n",
3183 pring->ringno,
3184 irsp->un.ulpWord[0],
3185 irsp->un.ulpWord[1],
3186 irsp->un.ulpWord[2],
3187 irsp->un.ulpWord[3],
3188 irsp->un.ulpWord[4],
3189 irsp->un.ulpWord[5],
3190 *(((uint32_t *) irsp) + 6),
3191 *(((uint32_t *) irsp) + 7),
3192 *(((uint32_t *) irsp) + 8),
3193 *(((uint32_t *) irsp) + 9),
3194 *(((uint32_t *) irsp) + 10),
3195 *(((uint32_t *) irsp) + 11),
3196 *(((uint32_t *) irsp) + 12),
3197 *(((uint32_t *) irsp) + 13),
3198 *(((uint32_t *) irsp) + 14),
3199 *(((uint32_t *) irsp) + 15));
3200 }
3201
3202 /*
3203 * Fetch the IOCB command type and call the correct completion
3204 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3205 * get freed back to the lpfc_iocb_list by the discovery
3206 * kernel thread.
3207 */
3208 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3209 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3210 switch (type) {
3211 case LPFC_SOL_IOCB:
3212 spin_unlock_irqrestore(&phba->hbalock, iflag);
3213 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3214 spin_lock_irqsave(&phba->hbalock, iflag);
3215 break;
3216
3217 case LPFC_UNSOL_IOCB:
3218 spin_unlock_irqrestore(&phba->hbalock, iflag);
3219 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3220 spin_lock_irqsave(&phba->hbalock, iflag);
3221 if (!rc)
3222 free_saveq = 0;
3223 break;
3224
3225 case LPFC_ABORT_IOCB:
3226 cmdiocbp = NULL;
3227 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3228 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3229 saveq);
3230 if (cmdiocbp) {
3231 /* Call the specified completion routine */
3232 if (cmdiocbp->iocb_cmpl) {
3233 spin_unlock_irqrestore(&phba->hbalock,
3234 iflag);
3235 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3236 saveq);
3237 spin_lock_irqsave(&phba->hbalock,
3238 iflag);
3239 } else
3240 __lpfc_sli_release_iocbq(phba,
3241 cmdiocbp);
3242 }
3243 break;
3244
3245 case LPFC_UNKNOWN_IOCB:
3246 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3247 char adaptermsg[LPFC_MAX_ADPTMSG];
3248 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3249 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3250 MAX_MSG_DATA);
3251 dev_warn(&((phba->pcidev)->dev),
3252 "lpfc%d: %s\n",
3253 phba->brd_no, adaptermsg);
3254 } else {
3255 /* Unknown IOCB command */
3256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3257 "0335 Unknown IOCB "
3258 "command Data: x%x "
3259 "x%x x%x x%x\n",
3260 irsp->ulpCommand,
3261 irsp->ulpStatus,
3262 irsp->ulpIoTag,
3263 irsp->ulpContext);
3264 }
3265 break;
3266 }
3267
3268 if (free_saveq) {
3269 list_for_each_entry_safe(rspiocbp, next_iocb,
3270 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003271 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003272 __lpfc_sli_release_iocbq(phba, rspiocbp);
3273 }
3274 __lpfc_sli_release_iocbq(phba, saveq);
3275 }
3276 rspiocbp = NULL;
3277 }
3278 spin_unlock_irqrestore(&phba->hbalock, iflag);
3279 return rspiocbp;
3280}
3281
3282/**
3283 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003284 * @phba: Pointer to HBA context object.
3285 * @pring: Pointer to driver SLI ring object.
3286 * @mask: Host attention register mask for this ring.
3287 *
James Smart3772a992009-05-22 14:50:54 -04003288 * This routine wraps the actual slow_ring event process routine from the
3289 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003290 **/
James Smart3772a992009-05-22 14:50:54 -04003291void
James Smart2e0fef82007-06-17 19:56:36 -05003292lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3293 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003294{
James Smart3772a992009-05-22 14:50:54 -04003295 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3296}
3297
3298/**
3299 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3300 * @phba: Pointer to HBA context object.
3301 * @pring: Pointer to driver SLI ring object.
3302 * @mask: Host attention register mask for this ring.
3303 *
3304 * This function is called from the worker thread when there is a ring event
3305 * for non-fcp rings. The caller does not hold any lock. The function will
3306 * remove each response iocb in the response ring and calls the handle
3307 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3308 **/
3309static void
3310lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3311 struct lpfc_sli_ring *pring, uint32_t mask)
3312{
James Smart34b02dc2008-08-24 21:49:55 -04003313 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003314 IOCB_t *entry;
3315 IOCB_t *irsp = NULL;
3316 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003317 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003318 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003319 uint32_t status;
dea31012005-04-17 16:05:31 -05003320
James Smart34b02dc2008-08-24 21:49:55 -04003321 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003322 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003323 pring->stats.iocb_event++;
3324
dea31012005-04-17 16:05:31 -05003325 /*
3326 * The next available response entry should never exceed the maximum
3327 * entries. If it does, treat it as an adapter hardware error.
3328 */
James Smart7e56aa22012-08-03 12:35:34 -04003329 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003330 portRspPut = le32_to_cpu(pgp->rspPutInx);
3331 if (portRspPut >= portRspMax) {
3332 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003333 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003334 * rsp ring <portRspMax>
3335 */
James Smarted957682007-06-17 19:56:37 -05003336 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003337 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003338 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003339 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003340
James Smart2e0fef82007-06-17 19:56:36 -05003341 phba->link_state = LPFC_HBA_ERROR;
3342 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003343
3344 phba->work_hs = HS_FFER3;
3345 lpfc_handle_eratt(phba);
3346
James Smart3772a992009-05-22 14:50:54 -04003347 return;
dea31012005-04-17 16:05:31 -05003348 }
3349
3350 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003351 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003352 /*
3353 * Build a completion list and call the appropriate handler.
3354 * The process is to get the next available response iocb, get
3355 * a free iocb from the list, copy the response data into the
3356 * free iocb, insert to the continuation list, and update the
3357 * next response index to slim. This process makes response
3358 * iocb's in the ring available to DMA as fast as possible but
3359 * pays a penalty for a copy operation. Since the iocb is
3360 * only 32 bytes, this penalty is considered small relative to
3361 * the PCI reads for register values and a slim write. When
3362 * the ulpLe field is set, the entire Command has been
3363 * received.
3364 */
James Smarted957682007-06-17 19:56:37 -05003365 entry = lpfc_resp_iocb(phba, pring);
3366
James Smart858c9f62007-06-17 19:56:39 -05003367 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003368 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003369 if (rspiocbp == NULL) {
3370 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003371 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003372 break;
3373 }
3374
James Smarted957682007-06-17 19:56:37 -05003375 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3376 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003377 irsp = &rspiocbp->iocb;
3378
James Smart7e56aa22012-08-03 12:35:34 -04003379 if (++pring->sli.sli3.rspidx >= portRspMax)
3380 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003381
James Smarta58cbd52007-08-02 11:09:43 -04003382 if (pring->ringno == LPFC_ELS_RING) {
3383 lpfc_debugfs_slow_ring_trc(phba,
3384 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3385 *(((uint32_t *) irsp) + 4),
3386 *(((uint32_t *) irsp) + 6),
3387 *(((uint32_t *) irsp) + 7));
3388 }
3389
James Smart7e56aa22012-08-03 12:35:34 -04003390 writel(pring->sli.sli3.rspidx,
3391 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003392
James Smart3772a992009-05-22 14:50:54 -04003393 spin_unlock_irqrestore(&phba->hbalock, iflag);
3394 /* Handle the response IOCB */
3395 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3396 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003397
3398 /*
3399 * If the port response put pointer has not been updated, sync
3400 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3401 * response put pointer.
3402 */
James Smart7e56aa22012-08-03 12:35:34 -04003403 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003404 portRspPut = le32_to_cpu(pgp->rspPutInx);
3405 }
James Smart7e56aa22012-08-03 12:35:34 -04003406 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003407
James Smart92d7f7b2007-06-17 19:56:38 -05003408 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003409 /* At least one response entry has been freed */
3410 pring->stats.iocb_rsp_full++;
3411 /* SET RxRE_RSP in Chip Att register */
3412 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3413 writel(status, phba->CAregaddr);
3414 readl(phba->CAregaddr); /* flush */
3415 }
3416 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3417 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3418 pring->stats.iocb_cmd_empty++;
3419
3420 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003421 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003422 lpfc_sli_resume_iocb(phba, pring);
3423
3424 if ((pring->lpfc_sli_cmd_available))
3425 (pring->lpfc_sli_cmd_available) (phba, pring);
3426
3427 }
3428
James Smart2e0fef82007-06-17 19:56:36 -05003429 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003430 return;
dea31012005-04-17 16:05:31 -05003431}
3432
James Smarte59058c2008-08-24 21:49:00 -04003433/**
James Smart4f774512009-05-22 14:52:35 -04003434 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3435 * @phba: Pointer to HBA context object.
3436 * @pring: Pointer to driver SLI ring object.
3437 * @mask: Host attention register mask for this ring.
3438 *
3439 * This function is called from the worker thread when there is a pending
3440 * ELS response iocb on the driver internal slow-path response iocb worker
3441 * queue. The caller does not hold any lock. The function will remove each
3442 * response iocb from the response worker queue and calls the handle
3443 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3444 **/
3445static void
3446lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3447 struct lpfc_sli_ring *pring, uint32_t mask)
3448{
3449 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003450 struct hbq_dmabuf *dmabuf;
3451 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003452 unsigned long iflag;
3453
James Smart45ed1192009-10-02 15:17:02 -04003454 spin_lock_irqsave(&phba->hbalock, iflag);
3455 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3456 spin_unlock_irqrestore(&phba->hbalock, iflag);
3457 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003458 /* Get the response iocb from the head of work queue */
3459 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003460 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003461 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003462 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003463
3464 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3465 case CQE_CODE_COMPL_WQE:
3466 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3467 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003468 /* Translate ELS WCQE to response IOCBQ */
3469 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3470 irspiocbq);
3471 if (irspiocbq)
3472 lpfc_sli_sp_handle_rspiocb(phba, pring,
3473 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003474 break;
3475 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003476 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003477 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3478 cq_event);
3479 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3480 break;
3481 default:
3482 break;
3483 }
James Smart4f774512009-05-22 14:52:35 -04003484 }
3485}
3486
3487/**
James Smart3621a712009-04-06 18:47:14 -04003488 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003489 * @phba: Pointer to HBA context object.
3490 * @pring: Pointer to driver SLI ring object.
3491 *
3492 * This function aborts all iocbs in the given ring and frees all the iocb
3493 * objects in txq. This function issues an abort iocb for all the iocb commands
3494 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3495 * the return of this function. The caller is not required to hold any locks.
3496 **/
James Smart2e0fef82007-06-17 19:56:36 -05003497void
dea31012005-04-17 16:05:31 -05003498lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3499{
James Smart2534ba72007-04-25 09:52:20 -04003500 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003501 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003502
James Smart92d7f7b2007-06-17 19:56:38 -05003503 if (pring->ringno == LPFC_ELS_RING) {
3504 lpfc_fabric_abort_hba(phba);
3505 }
3506
dea31012005-04-17 16:05:31 -05003507 /* Error everything on txq and txcmplq
3508 * First do the txq.
3509 */
James Smart2e0fef82007-06-17 19:56:36 -05003510 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003511 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003512
3513 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003514 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3515 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3516
James Smart2e0fef82007-06-17 19:56:36 -05003517 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003518
James Smarta257bf92009-04-06 18:48:10 -04003519 /* Cancel all the IOCBs from the completions list */
3520 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3521 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003522}
3523
James Smarte59058c2008-08-24 21:49:00 -04003524/**
James Smart3621a712009-04-06 18:47:14 -04003525 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003526 * @phba: Pointer to HBA context object.
3527 *
3528 * This function flushes all iocbs in the fcp ring and frees all the iocb
3529 * objects in txq and txcmplq. This function will not issue abort iocbs
3530 * for all the iocb commands in txcmplq, they will just be returned with
3531 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3532 * slot has been permanently disabled.
3533 **/
3534void
3535lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3536{
3537 LIST_HEAD(txq);
3538 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003539 struct lpfc_sli *psli = &phba->sli;
3540 struct lpfc_sli_ring *pring;
3541
3542 /* Currently, only one fcp ring */
3543 pring = &psli->ring[psli->fcp_ring];
3544
3545 spin_lock_irq(&phba->hbalock);
3546 /* Retrieve everything on txq */
3547 list_splice_init(&pring->txq, &txq);
James Smarta8e497d2008-08-24 21:50:11 -04003548
3549 /* Retrieve everything on the txcmplq */
3550 list_splice_init(&pring->txcmplq, &txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04003551
3552 /* Indicate the I/O queues are flushed */
3553 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003554 spin_unlock_irq(&phba->hbalock);
3555
3556 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003557 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3558 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003559
3560 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003561 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3562 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003563}
3564
3565/**
James Smart3772a992009-05-22 14:50:54 -04003566 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003567 * @phba: Pointer to HBA context object.
3568 * @mask: Bit mask to be checked.
3569 *
3570 * This function reads the host status register and compares
3571 * with the provided bit mask to check if HBA completed
3572 * the restart. This function will wait in a loop for the
3573 * HBA to complete restart. If the HBA does not restart within
3574 * 15 iterations, the function will reset the HBA again. The
3575 * function returns 1 when HBA fail to restart otherwise returns
3576 * zero.
3577 **/
James Smart3772a992009-05-22 14:50:54 -04003578static int
3579lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003580{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003581 uint32_t status;
3582 int i = 0;
3583 int retval = 0;
dea31012005-04-17 16:05:31 -05003584
Jamie Wellnitz41415862006-02-28 19:25:27 -05003585 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003586 if (lpfc_readl(phba->HSregaddr, &status))
3587 return 1;
dea31012005-04-17 16:05:31 -05003588
Jamie Wellnitz41415862006-02-28 19:25:27 -05003589 /*
3590 * Check status register every 100ms for 5 retries, then every
3591 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3592 * every 2.5 sec for 4.
3593 * Break our of the loop if errors occurred during init.
3594 */
3595 while (((status & mask) != mask) &&
3596 !(status & HS_FFERM) &&
3597 i++ < 20) {
dea31012005-04-17 16:05:31 -05003598
Jamie Wellnitz41415862006-02-28 19:25:27 -05003599 if (i <= 5)
3600 msleep(10);
3601 else if (i <= 10)
3602 msleep(500);
3603 else
3604 msleep(2500);
dea31012005-04-17 16:05:31 -05003605
Jamie Wellnitz41415862006-02-28 19:25:27 -05003606 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003607 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003608 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003609 lpfc_sli_brdrestart(phba);
3610 }
3611 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003612 if (lpfc_readl(phba->HSregaddr, &status)) {
3613 retval = 1;
3614 break;
3615 }
dea31012005-04-17 16:05:31 -05003616 }
dea31012005-04-17 16:05:31 -05003617
Jamie Wellnitz41415862006-02-28 19:25:27 -05003618 /* Check to see if any errors occurred during init */
3619 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3621 "2751 Adapter failed to restart, "
3622 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3623 status,
3624 readl(phba->MBslimaddr + 0xa8),
3625 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003626 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003627 retval = 1;
3628 }
dea31012005-04-17 16:05:31 -05003629
Jamie Wellnitz41415862006-02-28 19:25:27 -05003630 return retval;
dea31012005-04-17 16:05:31 -05003631}
3632
James Smartda0436e2009-05-22 14:51:39 -04003633/**
3634 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3635 * @phba: Pointer to HBA context object.
3636 * @mask: Bit mask to be checked.
3637 *
3638 * This function checks the host status register to check if HBA is
3639 * ready. This function will wait in a loop for the HBA to be ready
3640 * If the HBA is not ready , the function will will reset the HBA PCI
3641 * function again. The function returns 1 when HBA fail to be ready
3642 * otherwise returns zero.
3643 **/
3644static int
3645lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3646{
3647 uint32_t status;
3648 int retval = 0;
3649
3650 /* Read the HBA Host Status Register */
3651 status = lpfc_sli4_post_status_check(phba);
3652
3653 if (status) {
3654 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3655 lpfc_sli_brdrestart(phba);
3656 status = lpfc_sli4_post_status_check(phba);
3657 }
3658
3659 /* Check to see if any errors occurred during init */
3660 if (status) {
3661 phba->link_state = LPFC_HBA_ERROR;
3662 retval = 1;
3663 } else
3664 phba->sli4_hba.intr_enable = 0;
3665
3666 return retval;
3667}
3668
3669/**
3670 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3671 * @phba: Pointer to HBA context object.
3672 * @mask: Bit mask to be checked.
3673 *
3674 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3675 * from the API jump table function pointer from the lpfc_hba struct.
3676 **/
3677int
3678lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3679{
3680 return phba->lpfc_sli_brdready(phba, mask);
3681}
3682
James Smart92908312006-03-07 15:04:13 -05003683#define BARRIER_TEST_PATTERN (0xdeadbeef)
3684
James Smarte59058c2008-08-24 21:49:00 -04003685/**
James Smart3621a712009-04-06 18:47:14 -04003686 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003687 * @phba: Pointer to HBA context object.
3688 *
James Smart1b511972011-12-13 13:23:09 -05003689 * This function is called before resetting an HBA. This function is called
3690 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003691 **/
James Smart2e0fef82007-06-17 19:56:36 -05003692void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003693{
James Smart65a29c12006-07-06 15:50:50 -04003694 uint32_t __iomem *resp_buf;
3695 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003696 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003697 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003698 int i;
3699 uint8_t hdrtype;
3700
3701 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3702 if (hdrtype != 0x80 ||
3703 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3704 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3705 return;
3706
3707 /*
3708 * Tell the other part of the chip to suspend temporarily all
3709 * its DMA activity.
3710 */
James Smart65a29c12006-07-06 15:50:50 -04003711 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003712
3713 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003714 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3715 return;
James Smart92908312006-03-07 15:04:13 -05003716 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3717 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003718 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003719
James Smart9940b972011-03-11 16:06:12 -05003720 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3721 return;
3722 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003723 /* Clear Chip error bit */
3724 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003725 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003726 }
3727
3728 mbox = 0;
3729 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3730 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3731
3732 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003733 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003734 writel(mbox, mbox_buf);
3735
James Smart9940b972011-03-11 16:06:12 -05003736 for (i = 0; i < 50; i++) {
3737 if (lpfc_readl((resp_buf + 1), &resp_data))
3738 return;
3739 if (resp_data != ~(BARRIER_TEST_PATTERN))
3740 mdelay(1);
3741 else
3742 break;
3743 }
3744 resp_data = 0;
3745 if (lpfc_readl((resp_buf + 1), &resp_data))
3746 return;
3747 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003748 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003749 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003750 goto restore_hc;
3751 else
3752 goto clear_errat;
3753 }
3754
3755 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003756 resp_data = 0;
3757 for (i = 0; i < 500; i++) {
3758 if (lpfc_readl(resp_buf, &resp_data))
3759 return;
3760 if (resp_data != mbox)
3761 mdelay(1);
3762 else
3763 break;
3764 }
James Smart92908312006-03-07 15:04:13 -05003765
3766clear_errat:
3767
James Smart9940b972011-03-11 16:06:12 -05003768 while (++i < 500) {
3769 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3770 return;
3771 if (!(ha_copy & HA_ERATT))
3772 mdelay(1);
3773 else
3774 break;
3775 }
James Smart92908312006-03-07 15:04:13 -05003776
3777 if (readl(phba->HAregaddr) & HA_ERATT) {
3778 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003779 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003780 }
3781
3782restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003783 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003784 writel(hc_copy, phba->HCregaddr);
3785 readl(phba->HCregaddr); /* flush */
3786}
3787
James Smarte59058c2008-08-24 21:49:00 -04003788/**
James Smart3621a712009-04-06 18:47:14 -04003789 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003790 * @phba: Pointer to HBA context object.
3791 *
3792 * This function issues a kill_board mailbox command and waits for
3793 * the error attention interrupt. This function is called for stopping
3794 * the firmware processing. The caller is not required to hold any
3795 * locks. This function calls lpfc_hba_down_post function to free
3796 * any pending commands after the kill. The function will return 1 when it
3797 * fails to kill the board else will return 0.
3798 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003799int
James Smart2e0fef82007-06-17 19:56:36 -05003800lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003801{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003802 struct lpfc_sli *psli;
3803 LPFC_MBOXQ_t *pmb;
3804 uint32_t status;
3805 uint32_t ha_copy;
3806 int retval;
3807 int i = 0;
3808
3809 psli = &phba->sli;
3810
3811 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003812 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003813 "0329 Kill HBA Data: x%x x%x\n",
3814 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003815
James Smart98c9ea52007-10-27 13:37:33 -04003816 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3817 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003818 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003819
3820 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003821 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003822 if (lpfc_readl(phba->HCregaddr, &status)) {
3823 spin_unlock_irq(&phba->hbalock);
3824 mempool_free(pmb, phba->mbox_mem_pool);
3825 return 1;
3826 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003827 status &= ~HC_ERINT_ENA;
3828 writel(status, phba->HCregaddr);
3829 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003830 phba->link_flag |= LS_IGNORE_ERATT;
3831 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003832
3833 lpfc_kill_board(phba, pmb);
3834 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3835 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3836
3837 if (retval != MBX_SUCCESS) {
3838 if (retval != MBX_BUSY)
3839 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003840 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3841 "2752 KILL_BOARD command failed retval %d\n",
3842 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003843 spin_lock_irq(&phba->hbalock);
3844 phba->link_flag &= ~LS_IGNORE_ERATT;
3845 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003846 return 1;
3847 }
3848
James Smartf4b4c682009-05-22 14:53:12 -04003849 spin_lock_irq(&phba->hbalock);
3850 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3851 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003852
Jamie Wellnitz41415862006-02-28 19:25:27 -05003853 mempool_free(pmb, phba->mbox_mem_pool);
3854
3855 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3856 * attention every 100ms for 3 seconds. If we don't get ERATT after
3857 * 3 seconds we still set HBA_ERROR state because the status of the
3858 * board is now undefined.
3859 */
James Smart9940b972011-03-11 16:06:12 -05003860 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3861 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003862 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3863 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003864 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3865 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003866 }
3867
3868 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003869 if (ha_copy & HA_ERATT) {
3870 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003871 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003872 }
James Smart2e0fef82007-06-17 19:56:36 -05003873 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003874 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003875 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003876 phba->link_flag &= ~LS_IGNORE_ERATT;
3877 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003878
Jamie Wellnitz41415862006-02-28 19:25:27 -05003879 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003880 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003881
James Smart2e0fef82007-06-17 19:56:36 -05003882 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003883}
3884
James Smarte59058c2008-08-24 21:49:00 -04003885/**
James Smart3772a992009-05-22 14:50:54 -04003886 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003887 * @phba: Pointer to HBA context object.
3888 *
3889 * This function resets the HBA by writing HC_INITFF to the control
3890 * register. After the HBA resets, this function resets all the iocb ring
3891 * indices. This function disables PCI layer parity checking during
3892 * the reset.
3893 * This function returns 0 always.
3894 * The caller is not required to hold any locks.
3895 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003896int
James Smart2e0fef82007-06-17 19:56:36 -05003897lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003898{
3899 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003900 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003901 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003902 int i;
dea31012005-04-17 16:05:31 -05003903
Jamie Wellnitz41415862006-02-28 19:25:27 -05003904 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003905
Jamie Wellnitz41415862006-02-28 19:25:27 -05003906 /* Reset HBA */
3907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003908 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003909 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003910
3911 /* perform board reset */
3912 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003913 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003914 phba->pport->fc_myDID = 0;
3915 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003916
Jamie Wellnitz41415862006-02-28 19:25:27 -05003917 /* Turn off parity checking and serr during the physical reset */
3918 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3919 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3920 (cfg_value &
3921 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3922
James Smart3772a992009-05-22 14:50:54 -04003923 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3924
Jamie Wellnitz41415862006-02-28 19:25:27 -05003925 /* Now toggle INITFF bit in the Host Control Register */
3926 writel(HC_INITFF, phba->HCregaddr);
3927 mdelay(1);
3928 readl(phba->HCregaddr); /* flush */
3929 writel(0, phba->HCregaddr);
3930 readl(phba->HCregaddr); /* flush */
3931
3932 /* Restore PCI cmd register */
3933 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003934
3935 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936 for (i = 0; i < psli->num_rings; i++) {
3937 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003938 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003939 pring->sli.sli3.rspidx = 0;
3940 pring->sli.sli3.next_cmdidx = 0;
3941 pring->sli.sli3.local_getidx = 0;
3942 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003943 pring->missbufcnt = 0;
3944 }
dea31012005-04-17 16:05:31 -05003945
James Smart2e0fef82007-06-17 19:56:36 -05003946 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003947 return 0;
3948}
3949
James Smarte59058c2008-08-24 21:49:00 -04003950/**
James Smartda0436e2009-05-22 14:51:39 -04003951 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3952 * @phba: Pointer to HBA context object.
3953 *
3954 * This function resets a SLI4 HBA. This function disables PCI layer parity
3955 * checking during resets the device. The caller is not required to hold
3956 * any locks.
3957 *
3958 * This function returns 0 always.
3959 **/
3960int
3961lpfc_sli4_brdreset(struct lpfc_hba *phba)
3962{
3963 struct lpfc_sli *psli = &phba->sli;
3964 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003965 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003966
3967 /* Reset HBA */
3968 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3969 "0295 Reset HBA Data: x%x x%x\n",
3970 phba->pport->port_state, psli->sli_flag);
3971
3972 /* perform board reset */
3973 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003974 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003975 phba->pport->fc_myDID = 0;
3976 phba->pport->fc_prevDID = 0;
3977
James Smartda0436e2009-05-22 14:51:39 -04003978 spin_lock_irq(&phba->hbalock);
3979 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3980 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003981 spin_unlock_irq(&phba->hbalock);
3982
3983 /* Now physically reset the device */
3984 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3985 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003986
3987 /* Turn off parity checking and serr during the physical reset */
3988 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3989 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3990 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3991
James Smart88318812012-09-29 11:29:29 -04003992 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04003993 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04003994 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04003995
James Smartbe858b62010-12-15 17:57:20 -05003996 /* Restore PCI cmd register */
3997 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3998
James Smart27b01b82012-05-09 21:19:44 -04003999 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004000}
4001
4002/**
4003 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004004 * @phba: Pointer to HBA context object.
4005 *
4006 * This function is called in the SLI initialization code path to
4007 * restart the HBA. The caller is not required to hold any lock.
4008 * This function writes MBX_RESTART mailbox command to the SLIM and
4009 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4010 * function to free any pending commands. The function enables
4011 * POST only during the first initialization. The function returns zero.
4012 * The function does not guarantee completion of MBX_RESTART mailbox
4013 * command before the return of this function.
4014 **/
James Smartda0436e2009-05-22 14:51:39 -04004015static int
4016lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004017{
4018 MAILBOX_t *mb;
4019 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004020 volatile uint32_t word0;
4021 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004022 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004023
James Smart2e0fef82007-06-17 19:56:36 -05004024 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004025
James Smart0d878412009-10-02 15:16:56 -04004026 /* Take PCIe device Advanced Error Reporting (AER) state */
4027 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4028
Jamie Wellnitz41415862006-02-28 19:25:27 -05004029 psli = &phba->sli;
4030
4031 /* Restart HBA */
4032 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004033 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004034 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004035
4036 word0 = 0;
4037 mb = (MAILBOX_t *) &word0;
4038 mb->mbxCommand = MBX_RESTART;
4039 mb->mbxHc = 1;
4040
James Smart92908312006-03-07 15:04:13 -05004041 lpfc_reset_barrier(phba);
4042
Jamie Wellnitz41415862006-02-28 19:25:27 -05004043 to_slim = phba->MBslimaddr;
4044 writel(*(uint32_t *) mb, to_slim);
4045 readl(to_slim); /* flush */
4046
4047 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004048 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004049 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004050 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004051 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004052 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004053 writel(*(uint32_t *) mb, to_slim);
4054 readl(to_slim); /* flush */
4055
4056 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004057 phba->pport->stopped = 0;
4058 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004059 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004060 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004061
James Smart64ba8812006-08-02 15:24:34 -04004062 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4063 psli->stats_start = get_seconds();
4064
James Smarteaf15d52008-12-04 22:39:29 -05004065 /* Give the INITFF and Post time to settle. */
4066 mdelay(100);
dea31012005-04-17 16:05:31 -05004067
James Smart0d878412009-10-02 15:16:56 -04004068 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4069 if (hba_aer_enabled)
4070 pci_disable_pcie_error_reporting(phba->pcidev);
4071
Jamie Wellnitz41415862006-02-28 19:25:27 -05004072 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004073
4074 return 0;
4075}
4076
James Smarte59058c2008-08-24 21:49:00 -04004077/**
James Smartda0436e2009-05-22 14:51:39 -04004078 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4079 * @phba: Pointer to HBA context object.
4080 *
4081 * This function is called in the SLI initialization code path to restart
4082 * a SLI4 HBA. The caller is not required to hold any lock.
4083 * At the end of the function, it calls lpfc_hba_down_post function to
4084 * free any pending commands.
4085 **/
4086static int
4087lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4088{
4089 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004090 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004091 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004092
4093 /* Restart HBA */
4094 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4095 "0296 Restart HBA Data: x%x x%x\n",
4096 phba->pport->port_state, psli->sli_flag);
4097
James Smart75baf692010-06-08 18:31:21 -04004098 /* Take PCIe device Advanced Error Reporting (AER) state */
4099 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4100
James Smart27b01b82012-05-09 21:19:44 -04004101 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004102
4103 spin_lock_irq(&phba->hbalock);
4104 phba->pport->stopped = 0;
4105 phba->link_state = LPFC_INIT_START;
4106 phba->hba_flag = 0;
4107 spin_unlock_irq(&phba->hbalock);
4108
4109 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4110 psli->stats_start = get_seconds();
4111
James Smart75baf692010-06-08 18:31:21 -04004112 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4113 if (hba_aer_enabled)
4114 pci_disable_pcie_error_reporting(phba->pcidev);
4115
James Smartda0436e2009-05-22 14:51:39 -04004116 lpfc_hba_down_post(phba);
4117
James Smart27b01b82012-05-09 21:19:44 -04004118 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004119}
4120
4121/**
4122 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4123 * @phba: Pointer to HBA context object.
4124 *
4125 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4126 * API jump table function pointer from the lpfc_hba struct.
4127**/
4128int
4129lpfc_sli_brdrestart(struct lpfc_hba *phba)
4130{
4131 return phba->lpfc_sli_brdrestart(phba);
4132}
4133
4134/**
James Smart3621a712009-04-06 18:47:14 -04004135 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004136 * @phba: Pointer to HBA context object.
4137 *
4138 * This function is called after a HBA restart to wait for successful
4139 * restart of the HBA. Successful restart of the HBA is indicated by
4140 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4141 * iteration, the function will restart the HBA again. The function returns
4142 * zero if HBA successfully restarted else returns negative error code.
4143 **/
dea31012005-04-17 16:05:31 -05004144static int
4145lpfc_sli_chipset_init(struct lpfc_hba *phba)
4146{
4147 uint32_t status, i = 0;
4148
4149 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004150 if (lpfc_readl(phba->HSregaddr, &status))
4151 return -EIO;
dea31012005-04-17 16:05:31 -05004152
4153 /* Check status register to see what current state is */
4154 i = 0;
4155 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4156
James Smartdcf2a4e2010-09-29 11:18:53 -04004157 /* Check every 10ms for 10 retries, then every 100ms for 90
4158 * retries, then every 1 sec for 50 retires for a total of
4159 * ~60 seconds before reset the board again and check every
4160 * 1 sec for 50 retries. The up to 60 seconds before the
4161 * board ready is required by the Falcon FIPS zeroization
4162 * complete, and any reset the board in between shall cause
4163 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004164 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004165 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004166 /* Adapter failed to init, timeout, status reg
4167 <status> */
James Smarted957682007-06-17 19:56:37 -05004168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004169 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004170 "timeout, status reg x%x, "
4171 "FW Data: A8 x%x AC x%x\n", status,
4172 readl(phba->MBslimaddr + 0xa8),
4173 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004174 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004175 return -ETIMEDOUT;
4176 }
4177
4178 /* Check to see if any errors occurred during init */
4179 if (status & HS_FFERM) {
4180 /* ERROR: During chipset initialization */
4181 /* Adapter failed to init, chipset, status reg
4182 <status> */
James Smarted957682007-06-17 19:56:37 -05004183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004184 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004185 "chipset, status reg x%x, "
4186 "FW Data: A8 x%x AC x%x\n", status,
4187 readl(phba->MBslimaddr + 0xa8),
4188 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004189 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004190 return -EIO;
4191 }
4192
James Smartdcf2a4e2010-09-29 11:18:53 -04004193 if (i <= 10)
dea31012005-04-17 16:05:31 -05004194 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004195 else if (i <= 100)
4196 msleep(100);
4197 else
4198 msleep(1000);
dea31012005-04-17 16:05:31 -05004199
James Smartdcf2a4e2010-09-29 11:18:53 -04004200 if (i == 150) {
4201 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004202 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004203 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004204 }
4205 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004206 if (lpfc_readl(phba->HSregaddr, &status))
4207 return -EIO;
dea31012005-04-17 16:05:31 -05004208 }
4209
4210 /* Check to see if any errors occurred during init */
4211 if (status & HS_FFERM) {
4212 /* ERROR: During chipset initialization */
4213 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004215 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004216 "status reg x%x, "
4217 "FW Data: A8 x%x AC x%x\n", status,
4218 readl(phba->MBslimaddr + 0xa8),
4219 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004220 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004221 return -EIO;
4222 }
4223
4224 /* Clear all interrupt enable conditions */
4225 writel(0, phba->HCregaddr);
4226 readl(phba->HCregaddr); /* flush */
4227
4228 /* setup host attn register */
4229 writel(0xffffffff, phba->HAregaddr);
4230 readl(phba->HAregaddr); /* flush */
4231 return 0;
4232}
4233
James Smarte59058c2008-08-24 21:49:00 -04004234/**
James Smart3621a712009-04-06 18:47:14 -04004235 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004236 *
4237 * This function calculates and returns the number of HBQs required to be
4238 * configured.
4239 **/
James Smart78b2d852007-08-02 11:10:21 -04004240int
James Smarted957682007-06-17 19:56:37 -05004241lpfc_sli_hbq_count(void)
4242{
James Smart92d7f7b2007-06-17 19:56:38 -05004243 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004244}
4245
James Smarte59058c2008-08-24 21:49:00 -04004246/**
James Smart3621a712009-04-06 18:47:14 -04004247 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004248 *
4249 * This function adds the number of hbq entries in every HBQ to get
4250 * the total number of hbq entries required for the HBA and returns
4251 * the total count.
4252 **/
James Smarted957682007-06-17 19:56:37 -05004253static int
4254lpfc_sli_hbq_entry_count(void)
4255{
4256 int hbq_count = lpfc_sli_hbq_count();
4257 int count = 0;
4258 int i;
4259
4260 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004261 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004262 return count;
4263}
4264
James Smarte59058c2008-08-24 21:49:00 -04004265/**
James Smart3621a712009-04-06 18:47:14 -04004266 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004267 *
4268 * This function calculates amount of memory required for all hbq entries
4269 * to be configured and returns the total memory required.
4270 **/
dea31012005-04-17 16:05:31 -05004271int
James Smarted957682007-06-17 19:56:37 -05004272lpfc_sli_hbq_size(void)
4273{
4274 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4275}
4276
James Smarte59058c2008-08-24 21:49:00 -04004277/**
James Smart3621a712009-04-06 18:47:14 -04004278 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004279 * @phba: Pointer to HBA context object.
4280 *
4281 * This function is called during the SLI initialization to configure
4282 * all the HBQs and post buffers to the HBQ. The caller is not
4283 * required to hold any locks. This function will return zero if successful
4284 * else it will return negative error code.
4285 **/
James Smarted957682007-06-17 19:56:37 -05004286static int
4287lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4288{
4289 int hbq_count = lpfc_sli_hbq_count();
4290 LPFC_MBOXQ_t *pmb;
4291 MAILBOX_t *pmbox;
4292 uint32_t hbqno;
4293 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004294
James Smart92d7f7b2007-06-17 19:56:38 -05004295 /* Get a Mailbox buffer to setup mailbox
4296 * commands for HBA initialization
4297 */
James Smarted957682007-06-17 19:56:37 -05004298 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4299
4300 if (!pmb)
4301 return -ENOMEM;
4302
James Smart04c68492009-05-22 14:52:52 -04004303 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004304
4305 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4306 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004307 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004308
4309 hbq_entry_index = 0;
4310 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4311 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4312 phba->hbqs[hbqno].hbqPutIdx = 0;
4313 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4314 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004315 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004316 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4317 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004318 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4319
4320 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4321 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4322 mbxStatus <status>, ring <num> */
4323
4324 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004325 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004326 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004327 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004328 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004329 pmbox->mbxStatus, hbqno);
4330
4331 phba->link_state = LPFC_HBA_ERROR;
4332 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004333 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004334 }
4335 }
4336 phba->hbq_count = hbq_count;
4337
James Smarted957682007-06-17 19:56:37 -05004338 mempool_free(pmb, phba->mbox_mem_pool);
4339
James Smart92d7f7b2007-06-17 19:56:38 -05004340 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004341 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4342 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004343 return 0;
4344}
4345
James Smarte59058c2008-08-24 21:49:00 -04004346/**
James Smart4f774512009-05-22 14:52:35 -04004347 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4348 * @phba: Pointer to HBA context object.
4349 *
4350 * This function is called during the SLI initialization to configure
4351 * all the HBQs and post buffers to the HBQ. The caller is not
4352 * required to hold any locks. This function will return zero if successful
4353 * else it will return negative error code.
4354 **/
4355static int
4356lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4357{
4358 phba->hbq_in_use = 1;
4359 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4360 phba->hbq_count = 1;
4361 /* Initially populate or replenish the HBQs */
4362 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4363 return 0;
4364}
4365
4366/**
James Smart3621a712009-04-06 18:47:14 -04004367 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004368 * @phba: Pointer to HBA context object.
4369 * @sli_mode: sli mode - 2/3
4370 *
4371 * This function is called by the sli intialization code path
4372 * to issue config_port mailbox command. This function restarts the
4373 * HBA firmware and issues a config_port mailbox command to configure
4374 * the SLI interface in the sli mode specified by sli_mode
4375 * variable. The caller is not required to hold any locks.
4376 * The function returns 0 if successful, else returns negative error
4377 * code.
4378 **/
James Smart93996272008-08-24 21:50:30 -04004379int
4380lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004381{
4382 LPFC_MBOXQ_t *pmb;
4383 uint32_t resetcount = 0, rc = 0, done = 0;
4384
4385 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4386 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004387 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004388 return -ENOMEM;
4389 }
4390
James Smarted957682007-06-17 19:56:37 -05004391 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004392 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004393 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004394 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004395 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004396 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004397 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004398 rc = lpfc_sli_chipset_init(phba);
4399 if (rc)
4400 break;
4401
James Smart2e0fef82007-06-17 19:56:36 -05004402 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004403 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004404 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004405 resetcount++;
4406
James Smarted957682007-06-17 19:56:37 -05004407 /* Call pre CONFIG_PORT mailbox command initialization. A
4408 * value of 0 means the call was successful. Any other
4409 * nonzero value is a failure, but if ERESTART is returned,
4410 * the driver may reset the HBA and try again.
4411 */
dea31012005-04-17 16:05:31 -05004412 rc = lpfc_config_port_prep(phba);
4413 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004414 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004415 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004416 } else if (rc)
dea31012005-04-17 16:05:31 -05004417 break;
James Smart6d368e52011-05-24 11:44:12 -04004418
James Smart2e0fef82007-06-17 19:56:36 -05004419 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004420 lpfc_config_port(phba, pmb);
4421 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004422 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4423 LPFC_SLI3_HBQ_ENABLED |
4424 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004425 LPFC_SLI3_BG_ENABLED |
4426 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004427 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004429 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004430 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004431 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004432 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004433 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004434 spin_unlock_irq(&phba->hbalock);
4435 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004436 } else {
4437 /* Allow asynchronous mailbox command to go through */
4438 spin_lock_irq(&phba->hbalock);
4439 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4440 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004441 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004442
4443 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4444 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4445 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4446 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004447 }
dea31012005-04-17 16:05:31 -05004448 }
James Smarted957682007-06-17 19:56:37 -05004449 if (!done) {
4450 rc = -EINVAL;
4451 goto do_prep_failed;
4452 }
James Smart04c68492009-05-22 14:52:52 -04004453 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4454 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004455 rc = -ENXIO;
4456 goto do_prep_failed;
4457 }
James Smart04c68492009-05-22 14:52:52 -04004458 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004459 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004460 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4461 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4462 phba->max_vpi : phba->max_vports;
4463
James Smart34b02dc2008-08-24 21:49:55 -04004464 } else
4465 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004466 phba->fips_level = 0;
4467 phba->fips_spec_rev = 0;
4468 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004469 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004470 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4471 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4472 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4473 "2850 Security Crypto Active. FIPS x%d "
4474 "(Spec Rev: x%d)",
4475 phba->fips_level, phba->fips_spec_rev);
4476 }
4477 if (pmb->u.mb.un.varCfgPort.sec_err) {
4478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4479 "2856 Config Port Security Crypto "
4480 "Error: x%x ",
4481 pmb->u.mb.un.varCfgPort.sec_err);
4482 }
James Smart04c68492009-05-22 14:52:52 -04004483 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004484 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004485 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004486 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004487
4488 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4489 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004490
4491 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004492 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004493 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4494 else
4495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4496 "0443 Adapter did not grant "
4497 "BlockGuard\n");
4498 }
James Smart34b02dc2008-08-24 21:49:55 -04004499 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004500 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004501 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004502 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004503 }
James Smart92d7f7b2007-06-17 19:56:38 -05004504do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004505 mempool_free(pmb, phba->mbox_mem_pool);
4506 return rc;
4507}
4508
James Smarte59058c2008-08-24 21:49:00 -04004509
4510/**
James Smart3621a712009-04-06 18:47:14 -04004511 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004512 * @phba: Pointer to HBA context object.
4513 *
4514 * This function is the main SLI intialization function. This function
4515 * is called by the HBA intialization code, HBA reset code and HBA
4516 * error attention handler code. Caller is not required to hold any
4517 * locks. This function issues config_port mailbox command to configure
4518 * the SLI, setup iocb rings and HBQ rings. In the end the function
4519 * calls the config_port_post function to issue init_link mailbox
4520 * command and to start the discovery. The function will return zero
4521 * if successful, else it will return negative error code.
4522 **/
James Smarted957682007-06-17 19:56:37 -05004523int
4524lpfc_sli_hba_setup(struct lpfc_hba *phba)
4525{
4526 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004527 int mode = 3, i;
4528 int longs;
James Smarted957682007-06-17 19:56:37 -05004529
4530 switch (lpfc_sli_mode) {
4531 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004532 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004534 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004535 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004536 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004537 break;
4538 }
James Smarted957682007-06-17 19:56:37 -05004539 mode = 2;
4540 break;
4541 case 0:
4542 case 3:
4543 break;
4544 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004546 "1819 Unrecognized lpfc_sli_mode "
4547 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004548
4549 break;
4550 }
4551
James Smart93996272008-08-24 21:50:30 -04004552 rc = lpfc_sli_config_port(phba, mode);
4553
James Smarted957682007-06-17 19:56:37 -05004554 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004556 "1820 Unable to select SLI-3. "
4557 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004558 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004559 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004560 if (rc)
dea31012005-04-17 16:05:31 -05004561 goto lpfc_sli_hba_setup_error;
4562
James Smart0d878412009-10-02 15:16:56 -04004563 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4564 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4565 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4566 if (!rc) {
4567 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4568 "2709 This device supports "
4569 "Advanced Error Reporting (AER)\n");
4570 spin_lock_irq(&phba->hbalock);
4571 phba->hba_flag |= HBA_AER_ENABLED;
4572 spin_unlock_irq(&phba->hbalock);
4573 } else {
4574 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4575 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004576 "Advanced Error Reporting (AER): %d\n",
4577 rc);
James Smart0d878412009-10-02 15:16:56 -04004578 phba->cfg_aer_support = 0;
4579 }
4580 }
4581
James Smarted957682007-06-17 19:56:37 -05004582 if (phba->sli_rev == 3) {
4583 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4584 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004585 } else {
4586 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4587 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004588 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004589 }
4590
4591 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004592 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4593 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004594 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004595
4596 if (rc)
4597 goto lpfc_sli_hba_setup_error;
4598
James Smart6d368e52011-05-24 11:44:12 -04004599 /* Initialize VPIs. */
4600 if (phba->sli_rev == LPFC_SLI_REV3) {
4601 /*
4602 * The VPI bitmask and physical ID array are allocated
4603 * and initialized once only - at driver load. A port
4604 * reset doesn't need to reinitialize this memory.
4605 */
4606 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4607 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4608 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4609 GFP_KERNEL);
4610 if (!phba->vpi_bmask) {
4611 rc = -ENOMEM;
4612 goto lpfc_sli_hba_setup_error;
4613 }
4614
4615 phba->vpi_ids = kzalloc(
4616 (phba->max_vpi+1) * sizeof(uint16_t),
4617 GFP_KERNEL);
4618 if (!phba->vpi_ids) {
4619 kfree(phba->vpi_bmask);
4620 rc = -ENOMEM;
4621 goto lpfc_sli_hba_setup_error;
4622 }
4623 for (i = 0; i < phba->max_vpi; i++)
4624 phba->vpi_ids[i] = i;
4625 }
4626 }
4627
James Smart93996272008-08-24 21:50:30 -04004628 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004629 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4630 rc = lpfc_sli_hbq_setup(phba);
4631 if (rc)
4632 goto lpfc_sli_hba_setup_error;
4633 }
James Smart04c68492009-05-22 14:52:52 -04004634 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004635 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004636 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004637
4638 rc = lpfc_config_port_post(phba);
4639 if (rc)
4640 goto lpfc_sli_hba_setup_error;
4641
James Smarted957682007-06-17 19:56:37 -05004642 return rc;
4643
James Smart92d7f7b2007-06-17 19:56:38 -05004644lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004645 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004647 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004648 return rc;
4649}
4650
James Smartda0436e2009-05-22 14:51:39 -04004651/**
4652 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4653 * @phba: Pointer to HBA context object.
4654 * @mboxq: mailbox pointer.
4655 * This function issue a dump mailbox command to read config region
4656 * 23 and parse the records in the region and populate driver
4657 * data structure.
4658 **/
4659static int
James Smartff78d8f2011-12-13 13:21:35 -05004660lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004661{
James Smartff78d8f2011-12-13 13:21:35 -05004662 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004663 struct lpfc_dmabuf *mp;
4664 struct lpfc_mqe *mqe;
4665 uint32_t data_length;
4666 int rc;
4667
4668 /* Program the default value of vlan_id and fc_map */
4669 phba->valid_vlan = 0;
4670 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4671 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4672 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4673
James Smartff78d8f2011-12-13 13:21:35 -05004674 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4675 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004676 return -ENOMEM;
4677
James Smartff78d8f2011-12-13 13:21:35 -05004678 mqe = &mboxq->u.mqe;
4679 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4680 rc = -ENOMEM;
4681 goto out_free_mboxq;
4682 }
4683
James Smartda0436e2009-05-22 14:51:39 -04004684 mp = (struct lpfc_dmabuf *) mboxq->context1;
4685 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4686
4687 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4688 "(%d):2571 Mailbox cmd x%x Status x%x "
4689 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4690 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4691 "CQ: x%x x%x x%x x%x\n",
4692 mboxq->vport ? mboxq->vport->vpi : 0,
4693 bf_get(lpfc_mqe_command, mqe),
4694 bf_get(lpfc_mqe_status, mqe),
4695 mqe->un.mb_words[0], mqe->un.mb_words[1],
4696 mqe->un.mb_words[2], mqe->un.mb_words[3],
4697 mqe->un.mb_words[4], mqe->un.mb_words[5],
4698 mqe->un.mb_words[6], mqe->un.mb_words[7],
4699 mqe->un.mb_words[8], mqe->un.mb_words[9],
4700 mqe->un.mb_words[10], mqe->un.mb_words[11],
4701 mqe->un.mb_words[12], mqe->un.mb_words[13],
4702 mqe->un.mb_words[14], mqe->un.mb_words[15],
4703 mqe->un.mb_words[16], mqe->un.mb_words[50],
4704 mboxq->mcqe.word0,
4705 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4706 mboxq->mcqe.trailer);
4707
4708 if (rc) {
4709 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4710 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004711 rc = -EIO;
4712 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004713 }
4714 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004715 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004716 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4717 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004718 rc = -EIO;
4719 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004720 }
James Smartda0436e2009-05-22 14:51:39 -04004721
4722 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4724 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004725 rc = 0;
4726
4727out_free_mboxq:
4728 mempool_free(mboxq, phba->mbox_mem_pool);
4729 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004730}
4731
4732/**
4733 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4734 * @phba: pointer to lpfc hba data structure.
4735 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4736 * @vpd: pointer to the memory to hold resulting port vpd data.
4737 * @vpd_size: On input, the number of bytes allocated to @vpd.
4738 * On output, the number of data bytes in @vpd.
4739 *
4740 * This routine executes a READ_REV SLI4 mailbox command. In
4741 * addition, this routine gets the port vpd data.
4742 *
4743 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004744 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004745 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004746 **/
4747static int
4748lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4749 uint8_t *vpd, uint32_t *vpd_size)
4750{
4751 int rc = 0;
4752 uint32_t dma_size;
4753 struct lpfc_dmabuf *dmabuf;
4754 struct lpfc_mqe *mqe;
4755
4756 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4757 if (!dmabuf)
4758 return -ENOMEM;
4759
4760 /*
4761 * Get a DMA buffer for the vpd data resulting from the READ_REV
4762 * mailbox command.
4763 */
4764 dma_size = *vpd_size;
4765 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4766 dma_size,
4767 &dmabuf->phys,
4768 GFP_KERNEL);
4769 if (!dmabuf->virt) {
4770 kfree(dmabuf);
4771 return -ENOMEM;
4772 }
4773 memset(dmabuf->virt, 0, dma_size);
4774
4775 /*
4776 * The SLI4 implementation of READ_REV conflicts at word1,
4777 * bits 31:16 and SLI4 adds vpd functionality not present
4778 * in SLI3. This code corrects the conflicts.
4779 */
4780 lpfc_read_rev(phba, mboxq);
4781 mqe = &mboxq->u.mqe;
4782 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4783 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4784 mqe->un.read_rev.word1 &= 0x0000FFFF;
4785 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4786 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4787
4788 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4789 if (rc) {
4790 dma_free_coherent(&phba->pcidev->dev, dma_size,
4791 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004792 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004793 return -EIO;
4794 }
4795
James Smartda0436e2009-05-22 14:51:39 -04004796 /*
4797 * The available vpd length cannot be bigger than the
4798 * DMA buffer passed to the port. Catch the less than
4799 * case and update the caller's size.
4800 */
4801 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4802 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4803
James Smartd7c47992010-06-08 18:31:54 -04004804 memcpy(vpd, dmabuf->virt, *vpd_size);
4805
James Smartda0436e2009-05-22 14:51:39 -04004806 dma_free_coherent(&phba->pcidev->dev, dma_size,
4807 dmabuf->virt, dmabuf->phys);
4808 kfree(dmabuf);
4809 return 0;
4810}
4811
4812/**
James Smartcd1c8302011-10-10 21:33:25 -04004813 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4814 * @phba: pointer to lpfc hba data structure.
4815 *
4816 * This routine retrieves SLI4 device physical port name this PCI function
4817 * is attached to.
4818 *
4819 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004820 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004821 * otherwise - failed to retrieve physical port name
4822 **/
4823static int
4824lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4825{
4826 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004827 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4828 struct lpfc_controller_attribute *cntl_attr;
4829 struct lpfc_mbx_get_port_name *get_port_name;
4830 void *virtaddr = NULL;
4831 uint32_t alloclen, reqlen;
4832 uint32_t shdr_status, shdr_add_status;
4833 union lpfc_sli4_cfg_shdr *shdr;
4834 char cport_name = 0;
4835 int rc;
4836
4837 /* We assume nothing at this point */
4838 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4839 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4840
4841 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4842 if (!mboxq)
4843 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004844 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004845 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4846 lpfc_sli4_read_config(phba);
4847 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4848 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004849
4850 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4851 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4852 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4853 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4854 LPFC_SLI4_MBX_NEMBED);
4855 if (alloclen < reqlen) {
4856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4857 "3084 Allocated DMA memory size (%d) is "
4858 "less than the requested DMA memory size "
4859 "(%d)\n", alloclen, reqlen);
4860 rc = -ENOMEM;
4861 goto out_free_mboxq;
4862 }
4863 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4864 virtaddr = mboxq->sge_array->addr[0];
4865 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4866 shdr = &mbx_cntl_attr->cfg_shdr;
4867 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4868 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4869 if (shdr_status || shdr_add_status || rc) {
4870 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4871 "3085 Mailbox x%x (x%x/x%x) failed, "
4872 "rc:x%x, status:x%x, add_status:x%x\n",
4873 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4874 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4875 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4876 rc, shdr_status, shdr_add_status);
4877 rc = -ENXIO;
4878 goto out_free_mboxq;
4879 }
4880 cntl_attr = &mbx_cntl_attr->cntl_attr;
4881 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4882 phba->sli4_hba.lnk_info.lnk_tp =
4883 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4884 phba->sli4_hba.lnk_info.lnk_no =
4885 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4886 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4887 "3086 lnk_type:%d, lnk_numb:%d\n",
4888 phba->sli4_hba.lnk_info.lnk_tp,
4889 phba->sli4_hba.lnk_info.lnk_no);
4890
4891retrieve_ppname:
4892 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4893 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4894 sizeof(struct lpfc_mbx_get_port_name) -
4895 sizeof(struct lpfc_sli4_cfg_mhdr),
4896 LPFC_SLI4_MBX_EMBED);
4897 get_port_name = &mboxq->u.mqe.un.get_port_name;
4898 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4899 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4900 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4901 phba->sli4_hba.lnk_info.lnk_tp);
4902 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4903 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4904 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4905 if (shdr_status || shdr_add_status || rc) {
4906 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4907 "3087 Mailbox x%x (x%x/x%x) failed: "
4908 "rc:x%x, status:x%x, add_status:x%x\n",
4909 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4910 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4911 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4912 rc, shdr_status, shdr_add_status);
4913 rc = -ENXIO;
4914 goto out_free_mboxq;
4915 }
4916 switch (phba->sli4_hba.lnk_info.lnk_no) {
4917 case LPFC_LINK_NUMBER_0:
4918 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4919 &get_port_name->u.response);
4920 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4921 break;
4922 case LPFC_LINK_NUMBER_1:
4923 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4924 &get_port_name->u.response);
4925 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4926 break;
4927 case LPFC_LINK_NUMBER_2:
4928 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4929 &get_port_name->u.response);
4930 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4931 break;
4932 case LPFC_LINK_NUMBER_3:
4933 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4934 &get_port_name->u.response);
4935 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4936 break;
4937 default:
4938 break;
4939 }
4940
4941 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4942 phba->Port[0] = cport_name;
4943 phba->Port[1] = '\0';
4944 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4945 "3091 SLI get port name: %s\n", phba->Port);
4946 }
4947
4948out_free_mboxq:
4949 if (rc != MBX_TIMEOUT) {
4950 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4951 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4952 else
4953 mempool_free(mboxq, phba->mbox_mem_pool);
4954 }
4955 return rc;
4956}
4957
4958/**
James Smartda0436e2009-05-22 14:51:39 -04004959 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4960 * @phba: pointer to lpfc hba data structure.
4961 *
4962 * This routine is called to explicitly arm the SLI4 device's completion and
4963 * event queues
4964 **/
4965static void
4966lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4967{
James Smart962bc512013-01-03 15:44:00 -05004968 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04004969
4970 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4971 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004972 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004973 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04004974 do {
James Smart2e90f4b2011-12-13 13:22:37 -05004975 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4976 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04004977 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05004978 }
James Smart1ba981f2014-02-20 09:56:45 -05004979
4980 if (phba->cfg_EnableXLane)
4981 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
4982
James Smart67d12732012-08-03 12:36:13 -04004983 if (phba->sli4_hba.hba_eq) {
4984 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05004985 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04004986 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05004987 LPFC_QUEUE_REARM);
4988 }
James Smart1ba981f2014-02-20 09:56:45 -05004989
4990 if (phba->cfg_fof)
4991 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004992}
4993
4994/**
James Smart6d368e52011-05-24 11:44:12 -04004995 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4996 * @phba: Pointer to HBA context object.
4997 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004998 * @extnt_count: buffer to hold port available extent count.
4999 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005000 *
James Smartb76f2dc2011-07-22 18:37:42 -04005001 * This function calls the port and retrievs the number of available
5002 * extents and their size for a particular extent type.
5003 *
5004 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005005 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005006int
James Smart6d368e52011-05-24 11:44:12 -04005007lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5008 uint16_t *extnt_count, uint16_t *extnt_size)
5009{
5010 int rc = 0;
5011 uint32_t length;
5012 uint32_t mbox_tmo;
5013 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5014 LPFC_MBOXQ_t *mbox;
5015
5016 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5017 if (!mbox)
5018 return -ENOMEM;
5019
5020 /* Find out how many extents are available for this resource type */
5021 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5022 sizeof(struct lpfc_sli4_cfg_mhdr));
5023 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5024 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5025 length, LPFC_SLI4_MBX_EMBED);
5026
5027 /* Send an extents count of 0 - the GET doesn't use it. */
5028 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5029 LPFC_SLI4_MBX_EMBED);
5030 if (unlikely(rc)) {
5031 rc = -EIO;
5032 goto err_exit;
5033 }
5034
5035 if (!phba->sli4_hba.intr_enable)
5036 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5037 else {
James Smarta183a152011-10-10 21:32:43 -04005038 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005039 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5040 }
5041 if (unlikely(rc)) {
5042 rc = -EIO;
5043 goto err_exit;
5044 }
5045
5046 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5047 if (bf_get(lpfc_mbox_hdr_status,
5048 &rsrc_info->header.cfg_shdr.response)) {
5049 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5050 "2930 Failed to get resource extents "
5051 "Status 0x%x Add'l Status 0x%x\n",
5052 bf_get(lpfc_mbox_hdr_status,
5053 &rsrc_info->header.cfg_shdr.response),
5054 bf_get(lpfc_mbox_hdr_add_status,
5055 &rsrc_info->header.cfg_shdr.response));
5056 rc = -EIO;
5057 goto err_exit;
5058 }
5059
5060 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5061 &rsrc_info->u.rsp);
5062 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5063 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005064
5065 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5066 "3162 Retrieved extents type-%d from port: count:%d, "
5067 "size:%d\n", type, *extnt_count, *extnt_size);
5068
5069err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005070 mempool_free(mbox, phba->mbox_mem_pool);
5071 return rc;
5072}
5073
5074/**
5075 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5076 * @phba: Pointer to HBA context object.
5077 * @type: The extent type to check.
5078 *
5079 * This function reads the current available extents from the port and checks
5080 * if the extent count or extent size has changed since the last access.
5081 * Callers use this routine post port reset to understand if there is a
5082 * extent reprovisioning requirement.
5083 *
5084 * Returns:
5085 * -Error: error indicates problem.
5086 * 1: Extent count or size has changed.
5087 * 0: No changes.
5088 **/
5089static int
5090lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5091{
5092 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5093 uint16_t size_diff, rsrc_ext_size;
5094 int rc = 0;
5095 struct lpfc_rsrc_blks *rsrc_entry;
5096 struct list_head *rsrc_blk_list = NULL;
5097
5098 size_diff = 0;
5099 curr_ext_cnt = 0;
5100 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5101 &rsrc_ext_cnt,
5102 &rsrc_ext_size);
5103 if (unlikely(rc))
5104 return -EIO;
5105
5106 switch (type) {
5107 case LPFC_RSC_TYPE_FCOE_RPI:
5108 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5109 break;
5110 case LPFC_RSC_TYPE_FCOE_VPI:
5111 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5112 break;
5113 case LPFC_RSC_TYPE_FCOE_XRI:
5114 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5115 break;
5116 case LPFC_RSC_TYPE_FCOE_VFI:
5117 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5118 break;
5119 default:
5120 break;
5121 }
5122
5123 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5124 curr_ext_cnt++;
5125 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5126 size_diff++;
5127 }
5128
5129 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5130 rc = 1;
5131
5132 return rc;
5133}
5134
5135/**
5136 * lpfc_sli4_cfg_post_extnts -
5137 * @phba: Pointer to HBA context object.
5138 * @extnt_cnt - number of available extents.
5139 * @type - the extent type (rpi, xri, vfi, vpi).
5140 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5141 * @mbox - pointer to the caller's allocated mailbox structure.
5142 *
5143 * This function executes the extents allocation request. It also
5144 * takes care of the amount of memory needed to allocate or get the
5145 * allocated extents. It is the caller's responsibility to evaluate
5146 * the response.
5147 *
5148 * Returns:
5149 * -Error: Error value describes the condition found.
5150 * 0: if successful
5151 **/
5152static int
James Smart8a9d2e82012-05-09 21:16:12 -04005153lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005154 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5155{
5156 int rc = 0;
5157 uint32_t req_len;
5158 uint32_t emb_len;
5159 uint32_t alloc_len, mbox_tmo;
5160
5161 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005162 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005163
5164 /*
5165 * Calculate the size of an embedded mailbox. The uint32_t
5166 * accounts for extents-specific word.
5167 */
5168 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5169 sizeof(uint32_t);
5170
5171 /*
5172 * Presume the allocation and response will fit into an embedded
5173 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5174 */
5175 *emb = LPFC_SLI4_MBX_EMBED;
5176 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005177 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005178 sizeof(union lpfc_sli4_cfg_shdr) +
5179 sizeof(uint32_t);
5180 *emb = LPFC_SLI4_MBX_NEMBED;
5181 }
5182
5183 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5184 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5185 req_len, *emb);
5186 if (alloc_len < req_len) {
5187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005188 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005189 "less than the requested DMA memory "
5190 "size (x%x)\n", alloc_len, req_len);
5191 return -ENOMEM;
5192 }
James Smart8a9d2e82012-05-09 21:16:12 -04005193 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005194 if (unlikely(rc))
5195 return -EIO;
5196
5197 if (!phba->sli4_hba.intr_enable)
5198 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5199 else {
James Smarta183a152011-10-10 21:32:43 -04005200 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005201 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5202 }
5203
5204 if (unlikely(rc))
5205 rc = -EIO;
5206 return rc;
5207}
5208
5209/**
5210 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5211 * @phba: Pointer to HBA context object.
5212 * @type: The resource extent type to allocate.
5213 *
5214 * This function allocates the number of elements for the specified
5215 * resource type.
5216 **/
5217static int
5218lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5219{
5220 bool emb = false;
5221 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5222 uint16_t rsrc_id, rsrc_start, j, k;
5223 uint16_t *ids;
5224 int i, rc;
5225 unsigned long longs;
5226 unsigned long *bmask;
5227 struct lpfc_rsrc_blks *rsrc_blks;
5228 LPFC_MBOXQ_t *mbox;
5229 uint32_t length;
5230 struct lpfc_id_range *id_array = NULL;
5231 void *virtaddr = NULL;
5232 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5233 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5234 struct list_head *ext_blk_list;
5235
5236 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5237 &rsrc_cnt,
5238 &rsrc_size);
5239 if (unlikely(rc))
5240 return -EIO;
5241
5242 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5243 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5244 "3009 No available Resource Extents "
5245 "for resource type 0x%x: Count: 0x%x, "
5246 "Size 0x%x\n", type, rsrc_cnt,
5247 rsrc_size);
5248 return -ENOMEM;
5249 }
5250
James Smart8a9d2e82012-05-09 21:16:12 -04005251 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5252 "2903 Post resource extents type-0x%x: "
5253 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005254
5255 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5256 if (!mbox)
5257 return -ENOMEM;
5258
James Smart8a9d2e82012-05-09 21:16:12 -04005259 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005260 if (unlikely(rc)) {
5261 rc = -EIO;
5262 goto err_exit;
5263 }
5264
5265 /*
5266 * Figure out where the response is located. Then get local pointers
5267 * to the response data. The port does not guarantee to respond to
5268 * all extents counts request so update the local variable with the
5269 * allocated count from the port.
5270 */
5271 if (emb == LPFC_SLI4_MBX_EMBED) {
5272 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5273 id_array = &rsrc_ext->u.rsp.id[0];
5274 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5275 } else {
5276 virtaddr = mbox->sge_array->addr[0];
5277 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5278 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5279 id_array = &n_rsrc->id;
5280 }
5281
5282 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5283 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5284
5285 /*
5286 * Based on the resource size and count, correct the base and max
5287 * resource values.
5288 */
5289 length = sizeof(struct lpfc_rsrc_blks);
5290 switch (type) {
5291 case LPFC_RSC_TYPE_FCOE_RPI:
5292 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5293 sizeof(unsigned long),
5294 GFP_KERNEL);
5295 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5296 rc = -ENOMEM;
5297 goto err_exit;
5298 }
5299 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5300 sizeof(uint16_t),
5301 GFP_KERNEL);
5302 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5303 kfree(phba->sli4_hba.rpi_bmask);
5304 rc = -ENOMEM;
5305 goto err_exit;
5306 }
5307
5308 /*
5309 * The next_rpi was initialized with the maximum available
5310 * count but the port may allocate a smaller number. Catch
5311 * that case and update the next_rpi.
5312 */
5313 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5314
5315 /* Initialize local ptrs for common extent processing later. */
5316 bmask = phba->sli4_hba.rpi_bmask;
5317 ids = phba->sli4_hba.rpi_ids;
5318 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5319 break;
5320 case LPFC_RSC_TYPE_FCOE_VPI:
5321 phba->vpi_bmask = kzalloc(longs *
5322 sizeof(unsigned long),
5323 GFP_KERNEL);
5324 if (unlikely(!phba->vpi_bmask)) {
5325 rc = -ENOMEM;
5326 goto err_exit;
5327 }
5328 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5329 sizeof(uint16_t),
5330 GFP_KERNEL);
5331 if (unlikely(!phba->vpi_ids)) {
5332 kfree(phba->vpi_bmask);
5333 rc = -ENOMEM;
5334 goto err_exit;
5335 }
5336
5337 /* Initialize local ptrs for common extent processing later. */
5338 bmask = phba->vpi_bmask;
5339 ids = phba->vpi_ids;
5340 ext_blk_list = &phba->lpfc_vpi_blk_list;
5341 break;
5342 case LPFC_RSC_TYPE_FCOE_XRI:
5343 phba->sli4_hba.xri_bmask = kzalloc(longs *
5344 sizeof(unsigned long),
5345 GFP_KERNEL);
5346 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5347 rc = -ENOMEM;
5348 goto err_exit;
5349 }
James Smart8a9d2e82012-05-09 21:16:12 -04005350 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005351 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5352 sizeof(uint16_t),
5353 GFP_KERNEL);
5354 if (unlikely(!phba->sli4_hba.xri_ids)) {
5355 kfree(phba->sli4_hba.xri_bmask);
5356 rc = -ENOMEM;
5357 goto err_exit;
5358 }
5359
5360 /* Initialize local ptrs for common extent processing later. */
5361 bmask = phba->sli4_hba.xri_bmask;
5362 ids = phba->sli4_hba.xri_ids;
5363 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5364 break;
5365 case LPFC_RSC_TYPE_FCOE_VFI:
5366 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5367 sizeof(unsigned long),
5368 GFP_KERNEL);
5369 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5370 rc = -ENOMEM;
5371 goto err_exit;
5372 }
5373 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5374 sizeof(uint16_t),
5375 GFP_KERNEL);
5376 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5377 kfree(phba->sli4_hba.vfi_bmask);
5378 rc = -ENOMEM;
5379 goto err_exit;
5380 }
5381
5382 /* Initialize local ptrs for common extent processing later. */
5383 bmask = phba->sli4_hba.vfi_bmask;
5384 ids = phba->sli4_hba.vfi_ids;
5385 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5386 break;
5387 default:
5388 /* Unsupported Opcode. Fail call. */
5389 id_array = NULL;
5390 bmask = NULL;
5391 ids = NULL;
5392 ext_blk_list = NULL;
5393 goto err_exit;
5394 }
5395
5396 /*
5397 * Complete initializing the extent configuration with the
5398 * allocated ids assigned to this function. The bitmask serves
5399 * as an index into the array and manages the available ids. The
5400 * array just stores the ids communicated to the port via the wqes.
5401 */
5402 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5403 if ((i % 2) == 0)
5404 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5405 &id_array[k]);
5406 else
5407 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5408 &id_array[k]);
5409
5410 rsrc_blks = kzalloc(length, GFP_KERNEL);
5411 if (unlikely(!rsrc_blks)) {
5412 rc = -ENOMEM;
5413 kfree(bmask);
5414 kfree(ids);
5415 goto err_exit;
5416 }
5417 rsrc_blks->rsrc_start = rsrc_id;
5418 rsrc_blks->rsrc_size = rsrc_size;
5419 list_add_tail(&rsrc_blks->list, ext_blk_list);
5420 rsrc_start = rsrc_id;
5421 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5422 phba->sli4_hba.scsi_xri_start = rsrc_start +
5423 lpfc_sli4_get_els_iocb_cnt(phba);
5424
5425 while (rsrc_id < (rsrc_start + rsrc_size)) {
5426 ids[j] = rsrc_id;
5427 rsrc_id++;
5428 j++;
5429 }
5430 /* Entire word processed. Get next word.*/
5431 if ((i % 2) == 1)
5432 k++;
5433 }
5434 err_exit:
5435 lpfc_sli4_mbox_cmd_free(phba, mbox);
5436 return rc;
5437}
5438
5439/**
5440 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5441 * @phba: Pointer to HBA context object.
5442 * @type: the extent's type.
5443 *
5444 * This function deallocates all extents of a particular resource type.
5445 * SLI4 does not allow for deallocating a particular extent range. It
5446 * is the caller's responsibility to release all kernel memory resources.
5447 **/
5448static int
5449lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5450{
5451 int rc;
5452 uint32_t length, mbox_tmo = 0;
5453 LPFC_MBOXQ_t *mbox;
5454 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5455 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5456
5457 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5458 if (!mbox)
5459 return -ENOMEM;
5460
5461 /*
5462 * This function sends an embedded mailbox because it only sends the
5463 * the resource type. All extents of this type are released by the
5464 * port.
5465 */
5466 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5467 sizeof(struct lpfc_sli4_cfg_mhdr));
5468 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5469 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5470 length, LPFC_SLI4_MBX_EMBED);
5471
5472 /* Send an extents count of 0 - the dealloc doesn't use it. */
5473 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5474 LPFC_SLI4_MBX_EMBED);
5475 if (unlikely(rc)) {
5476 rc = -EIO;
5477 goto out_free_mbox;
5478 }
5479 if (!phba->sli4_hba.intr_enable)
5480 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5481 else {
James Smarta183a152011-10-10 21:32:43 -04005482 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005483 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5484 }
5485 if (unlikely(rc)) {
5486 rc = -EIO;
5487 goto out_free_mbox;
5488 }
5489
5490 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5491 if (bf_get(lpfc_mbox_hdr_status,
5492 &dealloc_rsrc->header.cfg_shdr.response)) {
5493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5494 "2919 Failed to release resource extents "
5495 "for type %d - Status 0x%x Add'l Status 0x%x. "
5496 "Resource memory not released.\n",
5497 type,
5498 bf_get(lpfc_mbox_hdr_status,
5499 &dealloc_rsrc->header.cfg_shdr.response),
5500 bf_get(lpfc_mbox_hdr_add_status,
5501 &dealloc_rsrc->header.cfg_shdr.response));
5502 rc = -EIO;
5503 goto out_free_mbox;
5504 }
5505
5506 /* Release kernel memory resources for the specific type. */
5507 switch (type) {
5508 case LPFC_RSC_TYPE_FCOE_VPI:
5509 kfree(phba->vpi_bmask);
5510 kfree(phba->vpi_ids);
5511 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5512 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5513 &phba->lpfc_vpi_blk_list, list) {
5514 list_del_init(&rsrc_blk->list);
5515 kfree(rsrc_blk);
5516 }
James Smart16a3a202013-04-17 20:14:38 -04005517 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005518 break;
5519 case LPFC_RSC_TYPE_FCOE_XRI:
5520 kfree(phba->sli4_hba.xri_bmask);
5521 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005522 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5523 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5524 list_del_init(&rsrc_blk->list);
5525 kfree(rsrc_blk);
5526 }
5527 break;
5528 case LPFC_RSC_TYPE_FCOE_VFI:
5529 kfree(phba->sli4_hba.vfi_bmask);
5530 kfree(phba->sli4_hba.vfi_ids);
5531 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5532 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5533 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5534 list_del_init(&rsrc_blk->list);
5535 kfree(rsrc_blk);
5536 }
5537 break;
5538 case LPFC_RSC_TYPE_FCOE_RPI:
5539 /* RPI bitmask and physical id array are cleaned up earlier. */
5540 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5541 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5542 list_del_init(&rsrc_blk->list);
5543 kfree(rsrc_blk);
5544 }
5545 break;
5546 default:
5547 break;
5548 }
5549
5550 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5551
5552 out_free_mbox:
5553 mempool_free(mbox, phba->mbox_mem_pool);
5554 return rc;
5555}
5556
5557/**
5558 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5559 * @phba: Pointer to HBA context object.
5560 *
5561 * This function allocates all SLI4 resource identifiers.
5562 **/
5563int
5564lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5565{
5566 int i, rc, error = 0;
5567 uint16_t count, base;
5568 unsigned long longs;
5569
James Smartff78d8f2011-12-13 13:21:35 -05005570 if (!phba->sli4_hba.rpi_hdrs_in_use)
5571 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005572 if (phba->sli4_hba.extents_in_use) {
5573 /*
5574 * The port supports resource extents. The XRI, VPI, VFI, RPI
5575 * resource extent count must be read and allocated before
5576 * provisioning the resource id arrays.
5577 */
5578 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5579 LPFC_IDX_RSRC_RDY) {
5580 /*
5581 * Extent-based resources are set - the driver could
5582 * be in a port reset. Figure out if any corrective
5583 * actions need to be taken.
5584 */
5585 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5586 LPFC_RSC_TYPE_FCOE_VFI);
5587 if (rc != 0)
5588 error++;
5589 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5590 LPFC_RSC_TYPE_FCOE_VPI);
5591 if (rc != 0)
5592 error++;
5593 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5594 LPFC_RSC_TYPE_FCOE_XRI);
5595 if (rc != 0)
5596 error++;
5597 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5598 LPFC_RSC_TYPE_FCOE_RPI);
5599 if (rc != 0)
5600 error++;
5601
5602 /*
5603 * It's possible that the number of resources
5604 * provided to this port instance changed between
5605 * resets. Detect this condition and reallocate
5606 * resources. Otherwise, there is no action.
5607 */
5608 if (error) {
5609 lpfc_printf_log(phba, KERN_INFO,
5610 LOG_MBOX | LOG_INIT,
5611 "2931 Detected extent resource "
5612 "change. Reallocating all "
5613 "extents.\n");
5614 rc = lpfc_sli4_dealloc_extent(phba,
5615 LPFC_RSC_TYPE_FCOE_VFI);
5616 rc = lpfc_sli4_dealloc_extent(phba,
5617 LPFC_RSC_TYPE_FCOE_VPI);
5618 rc = lpfc_sli4_dealloc_extent(phba,
5619 LPFC_RSC_TYPE_FCOE_XRI);
5620 rc = lpfc_sli4_dealloc_extent(phba,
5621 LPFC_RSC_TYPE_FCOE_RPI);
5622 } else
5623 return 0;
5624 }
5625
5626 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5627 if (unlikely(rc))
5628 goto err_exit;
5629
5630 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5631 if (unlikely(rc))
5632 goto err_exit;
5633
5634 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5635 if (unlikely(rc))
5636 goto err_exit;
5637
5638 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5639 if (unlikely(rc))
5640 goto err_exit;
5641 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5642 LPFC_IDX_RSRC_RDY);
5643 return rc;
5644 } else {
5645 /*
5646 * The port does not support resource extents. The XRI, VPI,
5647 * VFI, RPI resource ids were determined from READ_CONFIG.
5648 * Just allocate the bitmasks and provision the resource id
5649 * arrays. If a port reset is active, the resources don't
5650 * need any action - just exit.
5651 */
5652 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005653 LPFC_IDX_RSRC_RDY) {
5654 lpfc_sli4_dealloc_resource_identifiers(phba);
5655 lpfc_sli4_remove_rpis(phba);
5656 }
James Smart6d368e52011-05-24 11:44:12 -04005657 /* RPIs. */
5658 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005659 if (count <= 0) {
5660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5661 "3279 Invalid provisioning of "
5662 "rpi:%d\n", count);
5663 rc = -EINVAL;
5664 goto err_exit;
5665 }
James Smart6d368e52011-05-24 11:44:12 -04005666 base = phba->sli4_hba.max_cfg_param.rpi_base;
5667 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5668 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5669 sizeof(unsigned long),
5670 GFP_KERNEL);
5671 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5672 rc = -ENOMEM;
5673 goto err_exit;
5674 }
5675 phba->sli4_hba.rpi_ids = kzalloc(count *
5676 sizeof(uint16_t),
5677 GFP_KERNEL);
5678 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5679 rc = -ENOMEM;
5680 goto free_rpi_bmask;
5681 }
5682
5683 for (i = 0; i < count; i++)
5684 phba->sli4_hba.rpi_ids[i] = base + i;
5685
5686 /* VPIs. */
5687 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005688 if (count <= 0) {
5689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5690 "3280 Invalid provisioning of "
5691 "vpi:%d\n", count);
5692 rc = -EINVAL;
5693 goto free_rpi_ids;
5694 }
James Smart6d368e52011-05-24 11:44:12 -04005695 base = phba->sli4_hba.max_cfg_param.vpi_base;
5696 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5697 phba->vpi_bmask = kzalloc(longs *
5698 sizeof(unsigned long),
5699 GFP_KERNEL);
5700 if (unlikely(!phba->vpi_bmask)) {
5701 rc = -ENOMEM;
5702 goto free_rpi_ids;
5703 }
5704 phba->vpi_ids = kzalloc(count *
5705 sizeof(uint16_t),
5706 GFP_KERNEL);
5707 if (unlikely(!phba->vpi_ids)) {
5708 rc = -ENOMEM;
5709 goto free_vpi_bmask;
5710 }
5711
5712 for (i = 0; i < count; i++)
5713 phba->vpi_ids[i] = base + i;
5714
5715 /* XRIs. */
5716 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005717 if (count <= 0) {
5718 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5719 "3281 Invalid provisioning of "
5720 "xri:%d\n", count);
5721 rc = -EINVAL;
5722 goto free_vpi_ids;
5723 }
James Smart6d368e52011-05-24 11:44:12 -04005724 base = phba->sli4_hba.max_cfg_param.xri_base;
5725 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5726 phba->sli4_hba.xri_bmask = kzalloc(longs *
5727 sizeof(unsigned long),
5728 GFP_KERNEL);
5729 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5730 rc = -ENOMEM;
5731 goto free_vpi_ids;
5732 }
James Smart41899be2012-03-01 22:34:19 -05005733 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005734 phba->sli4_hba.xri_ids = kzalloc(count *
5735 sizeof(uint16_t),
5736 GFP_KERNEL);
5737 if (unlikely(!phba->sli4_hba.xri_ids)) {
5738 rc = -ENOMEM;
5739 goto free_xri_bmask;
5740 }
5741
5742 for (i = 0; i < count; i++)
5743 phba->sli4_hba.xri_ids[i] = base + i;
5744
5745 /* VFIs. */
5746 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005747 if (count <= 0) {
5748 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5749 "3282 Invalid provisioning of "
5750 "vfi:%d\n", count);
5751 rc = -EINVAL;
5752 goto free_xri_ids;
5753 }
James Smart6d368e52011-05-24 11:44:12 -04005754 base = phba->sli4_hba.max_cfg_param.vfi_base;
5755 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5756 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5757 sizeof(unsigned long),
5758 GFP_KERNEL);
5759 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5760 rc = -ENOMEM;
5761 goto free_xri_ids;
5762 }
5763 phba->sli4_hba.vfi_ids = kzalloc(count *
5764 sizeof(uint16_t),
5765 GFP_KERNEL);
5766 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5767 rc = -ENOMEM;
5768 goto free_vfi_bmask;
5769 }
5770
5771 for (i = 0; i < count; i++)
5772 phba->sli4_hba.vfi_ids[i] = base + i;
5773
5774 /*
5775 * Mark all resources ready. An HBA reset doesn't need
5776 * to reset the initialization.
5777 */
5778 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5779 LPFC_IDX_RSRC_RDY);
5780 return 0;
5781 }
5782
5783 free_vfi_bmask:
5784 kfree(phba->sli4_hba.vfi_bmask);
5785 free_xri_ids:
5786 kfree(phba->sli4_hba.xri_ids);
5787 free_xri_bmask:
5788 kfree(phba->sli4_hba.xri_bmask);
5789 free_vpi_ids:
5790 kfree(phba->vpi_ids);
5791 free_vpi_bmask:
5792 kfree(phba->vpi_bmask);
5793 free_rpi_ids:
5794 kfree(phba->sli4_hba.rpi_ids);
5795 free_rpi_bmask:
5796 kfree(phba->sli4_hba.rpi_bmask);
5797 err_exit:
5798 return rc;
5799}
5800
5801/**
5802 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5803 * @phba: Pointer to HBA context object.
5804 *
5805 * This function allocates the number of elements for the specified
5806 * resource type.
5807 **/
5808int
5809lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5810{
5811 if (phba->sli4_hba.extents_in_use) {
5812 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5813 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5814 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5815 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5816 } else {
5817 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005818 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005819 kfree(phba->vpi_ids);
5820 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5821 kfree(phba->sli4_hba.xri_bmask);
5822 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005823 kfree(phba->sli4_hba.vfi_bmask);
5824 kfree(phba->sli4_hba.vfi_ids);
5825 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5826 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5827 }
5828
5829 return 0;
5830}
5831
5832/**
James Smartb76f2dc2011-07-22 18:37:42 -04005833 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5834 * @phba: Pointer to HBA context object.
5835 * @type: The resource extent type.
5836 * @extnt_count: buffer to hold port extent count response
5837 * @extnt_size: buffer to hold port extent size response.
5838 *
5839 * This function calls the port to read the host allocated extents
5840 * for a particular type.
5841 **/
5842int
5843lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5844 uint16_t *extnt_cnt, uint16_t *extnt_size)
5845{
5846 bool emb;
5847 int rc = 0;
5848 uint16_t curr_blks = 0;
5849 uint32_t req_len, emb_len;
5850 uint32_t alloc_len, mbox_tmo;
5851 struct list_head *blk_list_head;
5852 struct lpfc_rsrc_blks *rsrc_blk;
5853 LPFC_MBOXQ_t *mbox;
5854 void *virtaddr = NULL;
5855 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5856 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5857 union lpfc_sli4_cfg_shdr *shdr;
5858
5859 switch (type) {
5860 case LPFC_RSC_TYPE_FCOE_VPI:
5861 blk_list_head = &phba->lpfc_vpi_blk_list;
5862 break;
5863 case LPFC_RSC_TYPE_FCOE_XRI:
5864 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5865 break;
5866 case LPFC_RSC_TYPE_FCOE_VFI:
5867 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5868 break;
5869 case LPFC_RSC_TYPE_FCOE_RPI:
5870 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5871 break;
5872 default:
5873 return -EIO;
5874 }
5875
5876 /* Count the number of extents currently allocatd for this type. */
5877 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5878 if (curr_blks == 0) {
5879 /*
5880 * The GET_ALLOCATED mailbox does not return the size,
5881 * just the count. The size should be just the size
5882 * stored in the current allocated block and all sizes
5883 * for an extent type are the same so set the return
5884 * value now.
5885 */
5886 *extnt_size = rsrc_blk->rsrc_size;
5887 }
5888 curr_blks++;
5889 }
5890
5891 /* Calculate the total requested length of the dma memory. */
5892 req_len = curr_blks * sizeof(uint16_t);
5893
5894 /*
5895 * Calculate the size of an embedded mailbox. The uint32_t
5896 * accounts for extents-specific word.
5897 */
5898 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5899 sizeof(uint32_t);
5900
5901 /*
5902 * Presume the allocation and response will fit into an embedded
5903 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5904 */
5905 emb = LPFC_SLI4_MBX_EMBED;
5906 req_len = emb_len;
5907 if (req_len > emb_len) {
5908 req_len = curr_blks * sizeof(uint16_t) +
5909 sizeof(union lpfc_sli4_cfg_shdr) +
5910 sizeof(uint32_t);
5911 emb = LPFC_SLI4_MBX_NEMBED;
5912 }
5913
5914 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5915 if (!mbox)
5916 return -ENOMEM;
5917 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5918
5919 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5920 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5921 req_len, emb);
5922 if (alloc_len < req_len) {
5923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5924 "2983 Allocated DMA memory size (x%x) is "
5925 "less than the requested DMA memory "
5926 "size (x%x)\n", alloc_len, req_len);
5927 rc = -ENOMEM;
5928 goto err_exit;
5929 }
5930 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5931 if (unlikely(rc)) {
5932 rc = -EIO;
5933 goto err_exit;
5934 }
5935
5936 if (!phba->sli4_hba.intr_enable)
5937 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5938 else {
James Smarta183a152011-10-10 21:32:43 -04005939 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005940 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5941 }
5942
5943 if (unlikely(rc)) {
5944 rc = -EIO;
5945 goto err_exit;
5946 }
5947
5948 /*
5949 * Figure out where the response is located. Then get local pointers
5950 * to the response data. The port does not guarantee to respond to
5951 * all extents counts request so update the local variable with the
5952 * allocated count from the port.
5953 */
5954 if (emb == LPFC_SLI4_MBX_EMBED) {
5955 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5956 shdr = &rsrc_ext->header.cfg_shdr;
5957 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5958 } else {
5959 virtaddr = mbox->sge_array->addr[0];
5960 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5961 shdr = &n_rsrc->cfg_shdr;
5962 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5963 }
5964
5965 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5966 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5967 "2984 Failed to read allocated resources "
5968 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5969 type,
5970 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5971 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5972 rc = -EIO;
5973 goto err_exit;
5974 }
5975 err_exit:
5976 lpfc_sli4_mbox_cmd_free(phba, mbox);
5977 return rc;
5978}
5979
5980/**
James Smart8a9d2e82012-05-09 21:16:12 -04005981 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5982 * @phba: pointer to lpfc hba data structure.
5983 *
5984 * This routine walks the list of els buffers that have been allocated and
5985 * repost them to the port by using SGL block post. This is needed after a
5986 * pci_function_reset/warm_start or start. It attempts to construct blocks
5987 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5988 * SGL block post mailbox commands to post them to the port. For single els
5989 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5990 * mailbox command for posting.
5991 *
5992 * Returns: 0 = success, non-zero failure.
5993 **/
5994static int
5995lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5996{
5997 struct lpfc_sglq *sglq_entry = NULL;
5998 struct lpfc_sglq *sglq_entry_next = NULL;
5999 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04006000 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006001 int last_xritag = NO_XRI;
6002 LIST_HEAD(prep_sgl_list);
6003 LIST_HEAD(blck_sgl_list);
6004 LIST_HEAD(allc_sgl_list);
6005 LIST_HEAD(post_sgl_list);
6006 LIST_HEAD(free_sgl_list);
6007
James Smart38c20672013-03-01 16:37:44 -05006008 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006009 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05006010 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006011
James Smart711ea882013-04-17 20:18:29 -04006012 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006013 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6014 &allc_sgl_list, list) {
6015 list_del_init(&sglq_entry->list);
6016 block_cnt++;
6017 if ((last_xritag != NO_XRI) &&
6018 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6019 /* a hole in xri block, form a sgl posting block */
6020 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6021 post_cnt = block_cnt - 1;
6022 /* prepare list for next posting block */
6023 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6024 block_cnt = 1;
6025 } else {
6026 /* prepare list for next posting block */
6027 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6028 /* enough sgls for non-embed sgl mbox command */
6029 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6030 list_splice_init(&prep_sgl_list,
6031 &blck_sgl_list);
6032 post_cnt = block_cnt;
6033 block_cnt = 0;
6034 }
6035 }
6036 num_posted++;
6037
6038 /* keep track of last sgl's xritag */
6039 last_xritag = sglq_entry->sli4_xritag;
6040
6041 /* end of repost sgl list condition for els buffers */
6042 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6043 if (post_cnt == 0) {
6044 list_splice_init(&prep_sgl_list,
6045 &blck_sgl_list);
6046 post_cnt = block_cnt;
6047 } else if (block_cnt == 1) {
6048 status = lpfc_sli4_post_sgl(phba,
6049 sglq_entry->phys, 0,
6050 sglq_entry->sli4_xritag);
6051 if (!status) {
6052 /* successful, put sgl to posted list */
6053 list_add_tail(&sglq_entry->list,
6054 &post_sgl_list);
6055 } else {
6056 /* Failure, put sgl to free list */
6057 lpfc_printf_log(phba, KERN_WARNING,
6058 LOG_SLI,
6059 "3159 Failed to post els "
6060 "sgl, xritag:x%x\n",
6061 sglq_entry->sli4_xritag);
6062 list_add_tail(&sglq_entry->list,
6063 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006064 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006065 }
6066 }
6067 }
6068
6069 /* continue until a nembed page worth of sgls */
6070 if (post_cnt == 0)
6071 continue;
6072
6073 /* post the els buffer list sgls as a block */
6074 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6075 post_cnt);
6076
6077 if (!status) {
6078 /* success, put sgl list to posted sgl list */
6079 list_splice_init(&blck_sgl_list, &post_sgl_list);
6080 } else {
6081 /* Failure, put sgl list to free sgl list */
6082 sglq_entry_first = list_first_entry(&blck_sgl_list,
6083 struct lpfc_sglq,
6084 list);
6085 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6086 "3160 Failed to post els sgl-list, "
6087 "xritag:x%x-x%x\n",
6088 sglq_entry_first->sli4_xritag,
6089 (sglq_entry_first->sli4_xritag +
6090 post_cnt - 1));
6091 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006092 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006093 }
6094
6095 /* don't reset xirtag due to hole in xri block */
6096 if (block_cnt == 0)
6097 last_xritag = NO_XRI;
6098
6099 /* reset els sgl post count for next round of posting */
6100 post_cnt = 0;
6101 }
James Smart711ea882013-04-17 20:18:29 -04006102 /* update the number of XRIs posted for ELS */
6103 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006104
6105 /* free the els sgls failed to post */
6106 lpfc_free_sgl_list(phba, &free_sgl_list);
6107
6108 /* push els sgls posted to the availble list */
6109 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006110 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006111 list_splice_init(&post_sgl_list,
6112 &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05006113 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006114 } else {
6115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6116 "3161 Failure to post els sgl to port.\n");
6117 return -EIO;
6118 }
6119 return 0;
6120}
6121
6122/**
James Smartda0436e2009-05-22 14:51:39 -04006123 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6124 * @phba: Pointer to HBA context object.
6125 *
6126 * This function is the main SLI4 device intialization PCI function. This
6127 * function is called by the HBA intialization code, HBA reset code and
6128 * HBA error attention handler code. Caller is not required to hold any
6129 * locks.
6130 **/
6131int
6132lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6133{
6134 int rc;
6135 LPFC_MBOXQ_t *mboxq;
6136 struct lpfc_mqe *mqe;
6137 uint8_t *vpd;
6138 uint32_t vpd_size;
6139 uint32_t ftr_rsp = 0;
6140 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6141 struct lpfc_vport *vport = phba->pport;
6142 struct lpfc_dmabuf *mp;
6143
6144 /* Perform a PCI function reset to start from clean */
6145 rc = lpfc_pci_function_reset(phba);
6146 if (unlikely(rc))
6147 return -ENODEV;
6148
6149 /* Check the HBA Host Status Register for readyness */
6150 rc = lpfc_sli4_post_status_check(phba);
6151 if (unlikely(rc))
6152 return -ENODEV;
6153 else {
6154 spin_lock_irq(&phba->hbalock);
6155 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6156 spin_unlock_irq(&phba->hbalock);
6157 }
6158
6159 /*
6160 * Allocate a single mailbox container for initializing the
6161 * port.
6162 */
6163 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6164 if (!mboxq)
6165 return -ENOMEM;
6166
James Smartda0436e2009-05-22 14:51:39 -04006167 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006168 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006169 vpd = kzalloc(vpd_size, GFP_KERNEL);
6170 if (!vpd) {
6171 rc = -ENOMEM;
6172 goto out_free_mbox;
6173 }
6174
6175 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006176 if (unlikely(rc)) {
6177 kfree(vpd);
6178 goto out_free_mbox;
6179 }
James Smart572709e2013-07-15 18:32:43 -04006180
James Smartda0436e2009-05-22 14:51:39 -04006181 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006182 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6183 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006184 phba->hba_flag |= HBA_FCOE_MODE;
6185 else
6186 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006187
6188 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6189 LPFC_DCBX_CEE_MODE)
6190 phba->hba_flag |= HBA_FIP_SUPPORT;
6191 else
6192 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6193
James Smart4f2e66c2012-05-09 21:17:07 -04006194 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6195
James Smartc31098c2011-04-16 11:03:33 -04006196 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006197 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6198 "0376 READ_REV Error. SLI Level %d "
6199 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006200 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006201 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006202 kfree(vpd);
6203 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006204 }
James Smartcd1c8302011-10-10 21:33:25 -04006205
6206 /*
James Smartff78d8f2011-12-13 13:21:35 -05006207 * Continue initialization with default values even if driver failed
6208 * to read FCoE param config regions, only read parameters if the
6209 * board is FCoE
6210 */
6211 if (phba->hba_flag & HBA_FCOE_MODE &&
6212 lpfc_sli4_read_fcoe_params(phba))
6213 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6214 "2570 Failed to read FCoE parameters\n");
6215
6216 /*
James Smartcd1c8302011-10-10 21:33:25 -04006217 * Retrieve sli4 device physical port name, failure of doing it
6218 * is considered as non-fatal.
6219 */
6220 rc = lpfc_sli4_retrieve_pport_name(phba);
6221 if (!rc)
6222 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6223 "3080 Successful retrieving SLI4 device "
6224 "physical port name: %s.\n", phba->Port);
6225
James Smartda0436e2009-05-22 14:51:39 -04006226 /*
6227 * Evaluate the read rev and vpd data. Populate the driver
6228 * state with the results. If this routine fails, the failure
6229 * is not fatal as the driver will use generic values.
6230 */
6231 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6232 if (unlikely(!rc)) {
6233 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6234 "0377 Error %d parsing vpd. "
6235 "Using defaults.\n", rc);
6236 rc = 0;
6237 }
James Smart76a95d72010-11-20 23:11:48 -05006238 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006239
James Smartf1126682009-06-10 17:22:44 -04006240 /* Save information as VPD data */
6241 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6242 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6243 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6244 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6245 &mqe->un.read_rev);
6246 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6247 &mqe->un.read_rev);
6248 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6249 &mqe->un.read_rev);
6250 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6251 &mqe->un.read_rev);
6252 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6253 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6254 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6255 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6256 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6257 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6258 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6259 "(%d):0380 READ_REV Status x%x "
6260 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6261 mboxq->vport ? mboxq->vport->vpi : 0,
6262 bf_get(lpfc_mqe_status, mqe),
6263 phba->vpd.rev.opFwName,
6264 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6265 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006266
James Smart572709e2013-07-15 18:32:43 -04006267 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6268 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6269 if (phba->pport->cfg_lun_queue_depth > rc) {
6270 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6271 "3362 LUN queue depth changed from %d to %d\n",
6272 phba->pport->cfg_lun_queue_depth, rc);
6273 phba->pport->cfg_lun_queue_depth = rc;
6274 }
6275
6276
James Smartda0436e2009-05-22 14:51:39 -04006277 /*
6278 * Discover the port's supported feature set and match it against the
6279 * hosts requests.
6280 */
6281 lpfc_request_features(phba, mboxq);
6282 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6283 if (unlikely(rc)) {
6284 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006285 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006286 }
6287
6288 /*
6289 * The port must support FCP initiator mode as this is the
6290 * only mode running in the host.
6291 */
6292 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6293 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6294 "0378 No support for fcpi mode.\n");
6295 ftr_rsp++;
6296 }
James Smartfedd3b72011-02-16 12:39:24 -05006297 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6298 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6299 else
6300 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006301 /*
6302 * If the port cannot support the host's requested features
6303 * then turn off the global config parameters to disable the
6304 * feature in the driver. This is not a fatal error.
6305 */
James Smartbf086112011-08-21 21:48:13 -04006306 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6307 if (phba->cfg_enable_bg) {
6308 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6309 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6310 else
6311 ftr_rsp++;
6312 }
James Smartda0436e2009-05-22 14:51:39 -04006313
6314 if (phba->max_vpi && phba->cfg_enable_npiv &&
6315 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6316 ftr_rsp++;
6317
6318 if (ftr_rsp) {
6319 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6320 "0379 Feature Mismatch Data: x%08x %08x "
6321 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6322 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6323 phba->cfg_enable_npiv, phba->max_vpi);
6324 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6325 phba->cfg_enable_bg = 0;
6326 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6327 phba->cfg_enable_npiv = 0;
6328 }
6329
6330 /* These SLI3 features are assumed in SLI4 */
6331 spin_lock_irq(&phba->hbalock);
6332 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6333 spin_unlock_irq(&phba->hbalock);
6334
James Smart6d368e52011-05-24 11:44:12 -04006335 /*
6336 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6337 * calls depends on these resources to complete port setup.
6338 */
6339 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6340 if (rc) {
6341 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6342 "2920 Failed to alloc Resource IDs "
6343 "rc = x%x\n", rc);
6344 goto out_free_mbox;
6345 }
6346
James Smartda0436e2009-05-22 14:51:39 -04006347 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006348 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6349 if (rc) {
6350 phba->link_state = LPFC_HBA_ERROR;
6351 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006352 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006353 }
6354
James Smartda0436e2009-05-22 14:51:39 -04006355 mboxq->vport = vport;
6356 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6357 mp = (struct lpfc_dmabuf *) mboxq->context1;
6358 if (rc == MBX_SUCCESS) {
6359 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6360 rc = 0;
6361 }
6362
6363 /*
6364 * This memory was allocated by the lpfc_read_sparam routine. Release
6365 * it to the mbuf pool.
6366 */
6367 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6368 kfree(mp);
6369 mboxq->context1 = NULL;
6370 if (unlikely(rc)) {
6371 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6372 "0382 READ_SPARAM command failed "
6373 "status %d, mbxStatus x%x\n",
6374 rc, bf_get(lpfc_mqe_status, mqe));
6375 phba->link_state = LPFC_HBA_ERROR;
6376 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006377 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006378 }
6379
James Smart05580562011-05-24 11:40:48 -04006380 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006381
6382 /* Update the fc_host data structures with new wwn. */
6383 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6384 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6385
James Smart8a9d2e82012-05-09 21:16:12 -04006386 /* update host els and scsi xri-sgl sizes and mappings */
6387 rc = lpfc_sli4_xri_sgl_update(phba);
6388 if (unlikely(rc)) {
6389 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6390 "1400 Failed to update xri-sgl size and "
6391 "mapping: %d\n", rc);
6392 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006393 }
6394
James Smart8a9d2e82012-05-09 21:16:12 -04006395 /* register the els sgl pool to the port */
6396 rc = lpfc_sli4_repost_els_sgl_list(phba);
6397 if (unlikely(rc)) {
6398 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6399 "0582 Error %d during els sgl post "
6400 "operation\n", rc);
6401 rc = -ENODEV;
6402 goto out_free_mbox;
6403 }
6404
6405 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006406 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6407 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006409 "0383 Error %d during scsi sgl post "
6410 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006411 /* Some Scsi buffers were moved to the abort scsi list */
6412 /* A pci function reset will repost them */
6413 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006414 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006415 }
6416
6417 /* Post the rpi header region to the device. */
6418 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6419 if (unlikely(rc)) {
6420 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6421 "0393 Error %d during rpi post operation\n",
6422 rc);
6423 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006424 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006425 }
James Smart97f2ecf2012-03-01 22:35:23 -05006426 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006427
James Smart5350d872011-10-10 21:33:49 -04006428 /* Create all the SLI4 queues */
6429 rc = lpfc_sli4_queue_create(phba);
6430 if (rc) {
6431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6432 "3089 Failed to allocate queues\n");
6433 rc = -ENODEV;
6434 goto out_stop_timers;
6435 }
James Smartda0436e2009-05-22 14:51:39 -04006436 /* Set up all the queues to the device */
6437 rc = lpfc_sli4_queue_setup(phba);
6438 if (unlikely(rc)) {
6439 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6440 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006441 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006442 }
6443
6444 /* Arm the CQs and then EQs on device */
6445 lpfc_sli4_arm_cqeq_intr(phba);
6446
6447 /* Indicate device interrupt mode */
6448 phba->sli4_hba.intr_enable = 1;
6449
6450 /* Allow asynchronous mailbox command to go through */
6451 spin_lock_irq(&phba->hbalock);
6452 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6453 spin_unlock_irq(&phba->hbalock);
6454
6455 /* Post receive buffers to the device */
6456 lpfc_sli4_rb_setup(phba);
6457
James Smartfc2b9892010-02-26 14:15:29 -05006458 /* Reset HBA FCF states after HBA reset */
6459 phba->fcf.fcf_flag = 0;
6460 phba->fcf.current_rec.flag = 0;
6461
James Smartda0436e2009-05-22 14:51:39 -04006462 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006463 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006464 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006465
6466 /* Start heart beat timer */
6467 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006468 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006469 phba->hb_outstanding = 0;
6470 phba->last_completion_time = jiffies;
6471
6472 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006473 mod_timer(&phba->eratt_poll,
6474 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006475
James Smart75baf692010-06-08 18:31:21 -04006476 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6477 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6478 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6479 if (!rc) {
6480 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6481 "2829 This device supports "
6482 "Advanced Error Reporting (AER)\n");
6483 spin_lock_irq(&phba->hbalock);
6484 phba->hba_flag |= HBA_AER_ENABLED;
6485 spin_unlock_irq(&phba->hbalock);
6486 } else {
6487 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6488 "2830 This device does not support "
6489 "Advanced Error Reporting (AER)\n");
6490 phba->cfg_aer_support = 0;
6491 }
James Smart0a96e972011-07-22 18:37:28 -04006492 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006493 }
6494
James Smart76a95d72010-11-20 23:11:48 -05006495 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6496 /*
6497 * The FC Port needs to register FCFI (index 0)
6498 */
6499 lpfc_reg_fcfi(phba, mboxq);
6500 mboxq->vport = phba->pport;
6501 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006502 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006503 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006504 rc = 0;
6505 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6506 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006507
6508 /* Check if the port is configured to be disabled */
6509 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006510 }
James Smart026abb82011-12-13 13:20:45 -05006511
James Smartda0436e2009-05-22 14:51:39 -04006512 /*
6513 * The port is ready, set the host's link state to LINK_DOWN
6514 * in preparation for link interrupts.
6515 */
James Smartda0436e2009-05-22 14:51:39 -04006516 spin_lock_irq(&phba->hbalock);
6517 phba->link_state = LPFC_LINK_DOWN;
6518 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006519 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6520 (phba->hba_flag & LINK_DISABLED)) {
6521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6522 "3103 Adapter Link is disabled.\n");
6523 lpfc_down_link(phba, mboxq);
6524 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6525 if (rc != MBX_SUCCESS) {
6526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6527 "3104 Adapter failed to issue "
6528 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6529 goto out_unset_queue;
6530 }
6531 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006532 /* don't perform init_link on SLI4 FC port loopback test */
6533 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6534 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6535 if (rc)
6536 goto out_unset_queue;
6537 }
James Smart5350d872011-10-10 21:33:49 -04006538 }
6539 mempool_free(mboxq, phba->mbox_mem_pool);
6540 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006541out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006542 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006543 lpfc_sli4_queue_unset(phba);
6544out_destroy_queue:
6545 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006546out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006547 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006548out_free_mbox:
6549 mempool_free(mboxq, phba->mbox_mem_pool);
6550 return rc;
6551}
James Smarte59058c2008-08-24 21:49:00 -04006552
6553/**
James Smart3621a712009-04-06 18:47:14 -04006554 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006555 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006556 *
James Smarte59058c2008-08-24 21:49:00 -04006557 * This is the callback function for mailbox timer. The mailbox
6558 * timer is armed when a new mailbox command is issued and the timer
6559 * is deleted when the mailbox complete. The function is called by
6560 * the kernel timer code when a mailbox does not complete within
6561 * expected time. This function wakes up the worker thread to
6562 * process the mailbox timeout and returns. All the processing is
6563 * done by the worker thread function lpfc_mbox_timeout_handler.
6564 **/
dea31012005-04-17 16:05:31 -05006565void
6566lpfc_mbox_timeout(unsigned long ptr)
6567{
James Smart92d7f7b2007-06-17 19:56:38 -05006568 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006569 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006570 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006571
James Smart2e0fef82007-06-17 19:56:36 -05006572 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006573 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006574 if (!tmo_posted)
6575 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6576 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6577
James Smart5e9d9b82008-06-14 22:52:53 -04006578 if (!tmo_posted)
6579 lpfc_worker_wake_up(phba);
6580 return;
dea31012005-04-17 16:05:31 -05006581}
6582
James Smarte8d3c3b2013-10-10 12:21:30 -04006583/**
6584 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6585 * are pending
6586 * @phba: Pointer to HBA context object.
6587 *
6588 * This function checks if any mailbox completions are present on the mailbox
6589 * completion queue.
6590 **/
6591bool
6592lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6593{
6594
6595 uint32_t idx;
6596 struct lpfc_queue *mcq;
6597 struct lpfc_mcqe *mcqe;
6598 bool pending_completions = false;
6599
6600 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6601 return false;
6602
6603 /* Check for completions on mailbox completion queue */
6604
6605 mcq = phba->sli4_hba.mbx_cq;
6606 idx = mcq->hba_index;
6607 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6608 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6609 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6610 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6611 pending_completions = true;
6612 break;
6613 }
6614 idx = (idx + 1) % mcq->entry_count;
6615 if (mcq->hba_index == idx)
6616 break;
6617 }
6618 return pending_completions;
6619
6620}
6621
6622/**
6623 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6624 * that were missed.
6625 * @phba: Pointer to HBA context object.
6626 *
6627 * For sli4, it is possible to miss an interrupt. As such mbox completions
6628 * maybe missed causing erroneous mailbox timeouts to occur. This function
6629 * checks to see if mbox completions are on the mailbox completion queue
6630 * and will process all the completions associated with the eq for the
6631 * mailbox completion queue.
6632 **/
6633bool
6634lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6635{
6636
6637 uint32_t eqidx;
6638 struct lpfc_queue *fpeq = NULL;
6639 struct lpfc_eqe *eqe;
6640 bool mbox_pending;
6641
6642 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6643 return false;
6644
6645 /* Find the eq associated with the mcq */
6646
6647 if (phba->sli4_hba.hba_eq)
6648 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6649 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6650 phba->sli4_hba.mbx_cq->assoc_qid) {
6651 fpeq = phba->sli4_hba.hba_eq[eqidx];
6652 break;
6653 }
6654 if (!fpeq)
6655 return false;
6656
6657 /* Turn off interrupts from this EQ */
6658
6659 lpfc_sli4_eq_clr_intr(fpeq);
6660
6661 /* Check to see if a mbox completion is pending */
6662
6663 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6664
6665 /*
6666 * If a mbox completion is pending, process all the events on EQ
6667 * associated with the mbox completion queue (this could include
6668 * mailbox commands, async events, els commands, receive queue data
6669 * and fcp commands)
6670 */
6671
6672 if (mbox_pending)
6673 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6674 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6675 fpeq->EQ_processed++;
6676 }
6677
6678 /* Always clear and re-arm the EQ */
6679
6680 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6681
6682 return mbox_pending;
6683
6684}
James Smarte59058c2008-08-24 21:49:00 -04006685
6686/**
James Smart3621a712009-04-06 18:47:14 -04006687 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006688 * @phba: Pointer to HBA context object.
6689 *
6690 * This function is called from worker thread when a mailbox command times out.
6691 * The caller is not required to hold any locks. This function will reset the
6692 * HBA and recover all the pending commands.
6693 **/
dea31012005-04-17 16:05:31 -05006694void
6695lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6696{
James Smart2e0fef82007-06-17 19:56:36 -05006697 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006698 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006699 struct lpfc_sli *psli = &phba->sli;
6700 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006701
James Smarte8d3c3b2013-10-10 12:21:30 -04006702 /* If the mailbox completed, process the completion and return */
6703 if (lpfc_sli4_process_missed_mbox_completions(phba))
6704 return;
6705
James Smarta257bf92009-04-06 18:48:10 -04006706 /* Check the pmbox pointer first. There is a race condition
6707 * between the mbox timeout handler getting executed in the
6708 * worklist and the mailbox actually completing. When this
6709 * race condition occurs, the mbox_active will be NULL.
6710 */
6711 spin_lock_irq(&phba->hbalock);
6712 if (pmbox == NULL) {
6713 lpfc_printf_log(phba, KERN_WARNING,
6714 LOG_MBOX | LOG_SLI,
6715 "0353 Active Mailbox cleared - mailbox timeout "
6716 "exiting\n");
6717 spin_unlock_irq(&phba->hbalock);
6718 return;
6719 }
6720
dea31012005-04-17 16:05:31 -05006721 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006722 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006723 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006724 mb->mbxCommand,
6725 phba->pport->port_state,
6726 phba->sli.sli_flag,
6727 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006728 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006729
James Smart1dcb58e2007-04-25 09:51:30 -04006730 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6731 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006732 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006733 */
James Smart2e0fef82007-06-17 19:56:36 -05006734 spin_lock_irq(&phba->pport->work_port_lock);
6735 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6736 spin_unlock_irq(&phba->pport->work_port_lock);
6737 spin_lock_irq(&phba->hbalock);
6738 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006739 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006740 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006741
6742 pring = &psli->ring[psli->fcp_ring];
6743 lpfc_sli_abort_iocb_ring(phba, pring);
6744
6745 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006746 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006747
6748 /* Reset the HBA device */
6749 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006750}
6751
James Smarte59058c2008-08-24 21:49:00 -04006752/**
James Smart3772a992009-05-22 14:50:54 -04006753 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006754 * @phba: Pointer to HBA context object.
6755 * @pmbox: Pointer to mailbox object.
6756 * @flag: Flag indicating how the mailbox need to be processed.
6757 *
6758 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006759 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6760 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006761 * The mailbox command can be submitted in polling mode, in which case
6762 * this function will wait in a polling loop for the completion of the
6763 * mailbox.
6764 * If the mailbox is submitted in no_wait mode (not polling) the
6765 * function will submit the command and returns immediately without waiting
6766 * for the mailbox completion. The no_wait is supported only when HBA
6767 * is in SLI2/SLI3 mode - interrupts are enabled.
6768 * The SLI interface allows only one mailbox pending at a time. If the
6769 * mailbox is issued in polling mode and there is already a mailbox
6770 * pending, then the function will return an error. If the mailbox is issued
6771 * in NO_WAIT mode and there is a mailbox pending already, the function
6772 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6773 * The sli layer owns the mailbox object until the completion of mailbox
6774 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6775 * return codes the caller owns the mailbox command after the return of
6776 * the function.
6777 **/
James Smart3772a992009-05-22 14:50:54 -04006778static int
6779lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6780 uint32_t flag)
dea31012005-04-17 16:05:31 -05006781{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006782 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006783 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006784 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006785 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006786 int i;
James Smart09372822008-01-11 01:52:54 -05006787 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006788 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006789 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006790 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006791 int processing_queue = 0;
6792
6793 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6794 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006795 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006796 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006797 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6798 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6799 return MBX_SUCCESS;
6800 }
James Smart58da1ff2008-04-07 10:15:56 -04006801 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006802 pmbox = lpfc_mbox_get(phba);
6803 if (!pmbox) {
6804 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6805 return MBX_SUCCESS;
6806 }
6807 }
dea31012005-04-17 16:05:31 -05006808
James Smarted957682007-06-17 19:56:37 -05006809 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006810 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006811 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006813 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006814 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006815 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006816 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006817 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006818 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006819 }
6820 }
6821
Linas Vepstas8d63f372007-02-14 14:28:36 -06006822 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006823 if (unlikely(pci_channel_offline(phba->pcidev))) {
6824 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6825 goto out_not_finished;
6826 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006827
James Smarta257bf92009-04-06 18:48:10 -04006828 /* If HBA has a deferred error attention, fail the iocb. */
6829 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6830 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6831 goto out_not_finished;
6832 }
6833
dea31012005-04-17 16:05:31 -05006834 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006835
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006836 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006837 status = MBX_SUCCESS;
6838
James Smart2e0fef82007-06-17 19:56:36 -05006839 if (phba->link_state == LPFC_HBA_ERROR) {
6840 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006841
6842 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006843 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6844 "(%d):0311 Mailbox command x%x cannot "
6845 "issue Data: x%x x%x\n",
6846 pmbox->vport ? pmbox->vport->vpi : 0,
6847 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006848 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006849 }
6850
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006851 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05006852 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6853 !(hc_copy & HC_MBINT_ENA)) {
6854 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6855 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006856 "(%d):2528 Mailbox command x%x cannot "
6857 "issue Data: x%x x%x\n",
6858 pmbox->vport ? pmbox->vport->vpi : 0,
6859 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006860 goto out_not_finished;
6861 }
James Smart92908312006-03-07 15:04:13 -05006862 }
6863
dea31012005-04-17 16:05:31 -05006864 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6865 /* Polling for a mbox command when another one is already active
6866 * is not allowed in SLI. Also, the driver must have established
6867 * SLI2 mode to queue and process multiple mbox commands.
6868 */
6869
6870 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006871 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006872
6873 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006874 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6875 "(%d):2529 Mailbox command x%x "
6876 "cannot issue Data: x%x x%x\n",
6877 pmbox->vport ? pmbox->vport->vpi : 0,
6878 pmbox->u.mb.mbxCommand,
6879 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006880 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006881 }
6882
James Smart3772a992009-05-22 14:50:54 -04006883 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006884 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006885 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006886 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6887 "(%d):2530 Mailbox command x%x "
6888 "cannot issue Data: x%x x%x\n",
6889 pmbox->vport ? pmbox->vport->vpi : 0,
6890 pmbox->u.mb.mbxCommand,
6891 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006892 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006893 }
6894
dea31012005-04-17 16:05:31 -05006895 /* Another mailbox command is still being processed, queue this
6896 * command to be processed later.
6897 */
6898 lpfc_mbox_put(phba, pmbox);
6899
6900 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006901 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006902 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006903 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006904 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006905 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006906 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006907
6908 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006909 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006910
James Smart858c9f62007-06-17 19:56:39 -05006911 if (pmbox->vport) {
6912 lpfc_debugfs_disc_trc(pmbox->vport,
6913 LPFC_DISC_TRC_MBOX_VPORT,
6914 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006915 (uint32_t)mbx->mbxCommand,
6916 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006917 }
6918 else {
6919 lpfc_debugfs_disc_trc(phba->pport,
6920 LPFC_DISC_TRC_MBOX,
6921 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006922 (uint32_t)mbx->mbxCommand,
6923 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006924 }
6925
James Smart2e0fef82007-06-17 19:56:36 -05006926 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006927 }
6928
dea31012005-04-17 16:05:31 -05006929 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6930
6931 /* If we are not polling, we MUST be in SLI2 mode */
6932 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006933 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006934 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006935 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006936 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006937 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006938 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6939 "(%d):2531 Mailbox command x%x "
6940 "cannot issue Data: x%x x%x\n",
6941 pmbox->vport ? pmbox->vport->vpi : 0,
6942 pmbox->u.mb.mbxCommand,
6943 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006944 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006945 }
6946 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04006947 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6948 1000);
6949 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05006950 }
6951
6952 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006953 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006954 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006955 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006956 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006957 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006958 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006959
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006960 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05006961 if (pmbox->vport) {
6962 lpfc_debugfs_disc_trc(pmbox->vport,
6963 LPFC_DISC_TRC_MBOX_VPORT,
6964 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006965 (uint32_t)mbx->mbxCommand,
6966 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006967 }
6968 else {
6969 lpfc_debugfs_disc_trc(phba->pport,
6970 LPFC_DISC_TRC_MBOX,
6971 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006972 (uint32_t)mbx->mbxCommand,
6973 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006974 }
6975 }
6976
dea31012005-04-17 16:05:31 -05006977 psli->slistat.mbox_cmd++;
6978 evtctr = psli->slistat.mbox_event;
6979
6980 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006981 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05006982
James Smart3772a992009-05-22 14:50:54 -04006983 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006984 /* Populate mbox extension offset word. */
6985 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006986 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04006987 = (uint8_t *)phba->mbox_ext
6988 - (uint8_t *)phba->mbox;
6989 }
6990
6991 /* Copy the mailbox extension data */
6992 if (pmbox->in_ext_byte_len && pmbox->context2) {
6993 lpfc_sli_pcimem_bcopy(pmbox->context2,
6994 (uint8_t *)phba->mbox_ext,
6995 pmbox->in_ext_byte_len);
6996 }
6997 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006998 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006999 } else {
James Smart7a470272010-03-15 11:25:20 -04007000 /* Populate mbox extension offset word. */
7001 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007002 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007003 = MAILBOX_HBA_EXT_OFFSET;
7004
7005 /* Copy the mailbox extension data */
7006 if (pmbox->in_ext_byte_len && pmbox->context2) {
7007 lpfc_memcpy_to_slim(phba->MBslimaddr +
7008 MAILBOX_HBA_EXT_OFFSET,
7009 pmbox->context2, pmbox->in_ext_byte_len);
7010
7011 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007012 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007013 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007014 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007015 }
7016
7017 /* First copy mbox command data to HBA SLIM, skip past first
7018 word */
7019 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007020 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007021 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7022
7023 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007024 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007025 to_slim = phba->MBslimaddr;
7026 writel(ldata, to_slim);
7027 readl(to_slim); /* flush */
7028
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007029 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007030 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007031 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007032 }
7033 }
7034
7035 wmb();
dea31012005-04-17 16:05:31 -05007036
7037 switch (flag) {
7038 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007039 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007040 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007041 /* Interrupt board to do it */
7042 writel(CA_MBATT, phba->CAregaddr);
7043 readl(phba->CAregaddr); /* flush */
7044 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007045 break;
7046
7047 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007048 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007049 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007050 /* Interrupt board to do it */
7051 writel(CA_MBATT, phba->CAregaddr);
7052 readl(phba->CAregaddr); /* flush */
7053
James Smart3772a992009-05-22 14:50:54 -04007054 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007055 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007056 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007057 word0 = le32_to_cpu(word0);
7058 } else {
7059 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007060 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7061 spin_unlock_irqrestore(&phba->hbalock,
7062 drvr_flag);
7063 goto out_not_finished;
7064 }
dea31012005-04-17 16:05:31 -05007065 }
7066
7067 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007068 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7069 spin_unlock_irqrestore(&phba->hbalock,
7070 drvr_flag);
7071 goto out_not_finished;
7072 }
James Smarta183a152011-10-10 21:32:43 -04007073 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7074 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007075 i = 0;
dea31012005-04-17 16:05:31 -05007076 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007077 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7078 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007079 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007080 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007081 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007082 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007083 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007084 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007085 }
7086
7087 /* Check if we took a mbox interrupt while we were
7088 polling */
7089 if (((word0 & OWN_CHIP) != OWN_CHIP)
7090 && (evtctr != psli->slistat.mbox_event))
7091 break;
7092
James Smart09372822008-01-11 01:52:54 -05007093 if (i++ > 10) {
7094 spin_unlock_irqrestore(&phba->hbalock,
7095 drvr_flag);
7096 msleep(1);
7097 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7098 }
dea31012005-04-17 16:05:31 -05007099
James Smart3772a992009-05-22 14:50:54 -04007100 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007101 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007102 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007103 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007104 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007105 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007106 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007107 /* Check real SLIM for any errors */
7108 slimword0 = readl(phba->MBslimaddr);
7109 slimmb = (MAILBOX_t *) & slimword0;
7110 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7111 && slimmb->mbxStatus) {
7112 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007113 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007114 word0 = slimword0;
7115 }
7116 }
7117 } else {
7118 /* First copy command data */
7119 word0 = readl(phba->MBslimaddr);
7120 }
7121 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007122 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7123 spin_unlock_irqrestore(&phba->hbalock,
7124 drvr_flag);
7125 goto out_not_finished;
7126 }
dea31012005-04-17 16:05:31 -05007127 }
7128
James Smart3772a992009-05-22 14:50:54 -04007129 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007130 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007131 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007132 /* Copy the mailbox extension data */
7133 if (pmbox->out_ext_byte_len && pmbox->context2) {
7134 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7135 pmbox->context2,
7136 pmbox->out_ext_byte_len);
7137 }
dea31012005-04-17 16:05:31 -05007138 } else {
7139 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007140 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007141 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007142 /* Copy the mailbox extension data */
7143 if (pmbox->out_ext_byte_len && pmbox->context2) {
7144 lpfc_memcpy_from_slim(pmbox->context2,
7145 phba->MBslimaddr +
7146 MAILBOX_HBA_EXT_OFFSET,
7147 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007148 }
7149 }
7150
7151 writel(HA_MBATT, phba->HAregaddr);
7152 readl(phba->HAregaddr); /* flush */
7153
7154 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007155 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007156 }
7157
James Smart2e0fef82007-06-17 19:56:36 -05007158 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7159 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007160
7161out_not_finished:
7162 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007163 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007164 lpfc_mbox_cmpl_put(phba, pmbox);
7165 }
7166 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007167}
7168
James Smarte59058c2008-08-24 21:49:00 -04007169/**
James Smartf1126682009-06-10 17:22:44 -04007170 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7171 * @phba: Pointer to HBA context object.
7172 *
7173 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7174 * the driver internal pending mailbox queue. It will then try to wait out the
7175 * possible outstanding mailbox command before return.
7176 *
7177 * Returns:
7178 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7179 * the outstanding mailbox command timed out.
7180 **/
7181static int
7182lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7183{
7184 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007185 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007186 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007187
7188 /* Mark the asynchronous mailbox command posting as blocked */
7189 spin_lock_irq(&phba->hbalock);
7190 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007191 /* Determine how long we might wait for the active mailbox
7192 * command to be gracefully completed by firmware.
7193 */
James Smarta183a152011-10-10 21:32:43 -04007194 if (phba->sli.mbox_active)
7195 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7196 phba->sli.mbox_active) *
7197 1000) + jiffies;
7198 spin_unlock_irq(&phba->hbalock);
7199
James Smarte8d3c3b2013-10-10 12:21:30 -04007200 /* Make sure the mailbox is really active */
7201 if (timeout)
7202 lpfc_sli4_process_missed_mbox_completions(phba);
7203
James Smartf1126682009-06-10 17:22:44 -04007204 /* Wait for the outstnading mailbox command to complete */
7205 while (phba->sli.mbox_active) {
7206 /* Check active mailbox complete status every 2ms */
7207 msleep(2);
7208 if (time_after(jiffies, timeout)) {
7209 /* Timeout, marked the outstanding cmd not complete */
7210 rc = 1;
7211 break;
7212 }
7213 }
7214
7215 /* Can not cleanly block async mailbox command, fails it */
7216 if (rc) {
7217 spin_lock_irq(&phba->hbalock);
7218 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7219 spin_unlock_irq(&phba->hbalock);
7220 }
7221 return rc;
7222}
7223
7224/**
7225 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7226 * @phba: Pointer to HBA context object.
7227 *
7228 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7229 * commands from the driver internal pending mailbox queue. It makes sure
7230 * that there is no outstanding mailbox command before resuming posting
7231 * asynchronous mailbox commands. If, for any reason, there is outstanding
7232 * mailbox command, it will try to wait it out before resuming asynchronous
7233 * mailbox command posting.
7234 **/
7235static void
7236lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7237{
7238 struct lpfc_sli *psli = &phba->sli;
7239
7240 spin_lock_irq(&phba->hbalock);
7241 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7242 /* Asynchronous mailbox posting is not blocked, do nothing */
7243 spin_unlock_irq(&phba->hbalock);
7244 return;
7245 }
7246
7247 /* Outstanding synchronous mailbox command is guaranteed to be done,
7248 * successful or timeout, after timing-out the outstanding mailbox
7249 * command shall always be removed, so just unblock posting async
7250 * mailbox command and resume
7251 */
7252 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7253 spin_unlock_irq(&phba->hbalock);
7254
7255 /* wake up worker thread to post asynchronlous mailbox command */
7256 lpfc_worker_wake_up(phba);
7257}
7258
7259/**
James Smart2d843ed2012-09-29 11:29:06 -04007260 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7261 * @phba: Pointer to HBA context object.
7262 * @mboxq: Pointer to mailbox object.
7263 *
7264 * The function waits for the bootstrap mailbox register ready bit from
7265 * port for twice the regular mailbox command timeout value.
7266 *
7267 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7268 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7269 **/
7270static int
7271lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7272{
7273 uint32_t db_ready;
7274 unsigned long timeout;
7275 struct lpfc_register bmbx_reg;
7276
7277 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7278 * 1000) + jiffies;
7279
7280 do {
7281 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7282 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7283 if (!db_ready)
7284 msleep(2);
7285
7286 if (time_after(jiffies, timeout))
7287 return MBXERR_ERROR;
7288 } while (!db_ready);
7289
7290 return 0;
7291}
7292
7293/**
James Smartda0436e2009-05-22 14:51:39 -04007294 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7295 * @phba: Pointer to HBA context object.
7296 * @mboxq: Pointer to mailbox object.
7297 *
7298 * The function posts a mailbox to the port. The mailbox is expected
7299 * to be comletely filled in and ready for the port to operate on it.
7300 * This routine executes a synchronous completion operation on the
7301 * mailbox by polling for its completion.
7302 *
7303 * The caller must not be holding any locks when calling this routine.
7304 *
7305 * Returns:
7306 * MBX_SUCCESS - mailbox posted successfully
7307 * Any of the MBX error values.
7308 **/
7309static int
7310lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7311{
7312 int rc = MBX_SUCCESS;
7313 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007314 uint32_t mcqe_status;
7315 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007316 struct lpfc_sli *psli = &phba->sli;
7317 struct lpfc_mqe *mb = &mboxq->u.mqe;
7318 struct lpfc_bmbx_create *mbox_rgn;
7319 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007320
7321 /*
7322 * Only one mailbox can be active to the bootstrap mailbox region
7323 * at a time and there is no queueing provided.
7324 */
7325 spin_lock_irqsave(&phba->hbalock, iflag);
7326 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7327 spin_unlock_irqrestore(&phba->hbalock, iflag);
7328 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007329 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007330 "cannot issue Data: x%x x%x\n",
7331 mboxq->vport ? mboxq->vport->vpi : 0,
7332 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007333 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7334 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007335 psli->sli_flag, MBX_POLL);
7336 return MBXERR_ERROR;
7337 }
7338 /* The server grabs the token and owns it until release */
7339 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7340 phba->sli.mbox_active = mboxq;
7341 spin_unlock_irqrestore(&phba->hbalock, iflag);
7342
James Smart2d843ed2012-09-29 11:29:06 -04007343 /* wait for bootstrap mbox register for readyness */
7344 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7345 if (rc)
7346 goto exit;
7347
James Smartda0436e2009-05-22 14:51:39 -04007348 /*
7349 * Initialize the bootstrap memory region to avoid stale data areas
7350 * in the mailbox post. Then copy the caller's mailbox contents to
7351 * the bmbx mailbox region.
7352 */
7353 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7354 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7355 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7356 sizeof(struct lpfc_mqe));
7357
7358 /* Post the high mailbox dma address to the port and wait for ready. */
7359 dma_address = &phba->sli4_hba.bmbx.dma_address;
7360 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7361
James Smart2d843ed2012-09-29 11:29:06 -04007362 /* wait for bootstrap mbox register for hi-address write done */
7363 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7364 if (rc)
7365 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007366
7367 /* Post the low mailbox dma address to the port. */
7368 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007369
James Smart2d843ed2012-09-29 11:29:06 -04007370 /* wait for bootstrap mbox register for low address write done */
7371 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7372 if (rc)
7373 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007374
7375 /*
7376 * Read the CQ to ensure the mailbox has completed.
7377 * If so, update the mailbox status so that the upper layers
7378 * can complete the request normally.
7379 */
7380 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7381 sizeof(struct lpfc_mqe));
7382 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7383 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7384 sizeof(struct lpfc_mcqe));
7385 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007386 /*
7387 * When the CQE status indicates a failure and the mailbox status
7388 * indicates success then copy the CQE status into the mailbox status
7389 * (and prefix it with x4000).
7390 */
James Smartda0436e2009-05-22 14:51:39 -04007391 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007392 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7393 bf_set(lpfc_mqe_status, mb,
7394 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007395 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007396 } else
7397 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007398
7399 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007400 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007401 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7402 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007403 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7404 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7405 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007406 bf_get(lpfc_mqe_status, mb),
7407 mb->un.mb_words[0], mb->un.mb_words[1],
7408 mb->un.mb_words[2], mb->un.mb_words[3],
7409 mb->un.mb_words[4], mb->un.mb_words[5],
7410 mb->un.mb_words[6], mb->un.mb_words[7],
7411 mb->un.mb_words[8], mb->un.mb_words[9],
7412 mb->un.mb_words[10], mb->un.mb_words[11],
7413 mb->un.mb_words[12], mboxq->mcqe.word0,
7414 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7415 mboxq->mcqe.trailer);
7416exit:
7417 /* We are holding the token, no needed for lock when release */
7418 spin_lock_irqsave(&phba->hbalock, iflag);
7419 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7420 phba->sli.mbox_active = NULL;
7421 spin_unlock_irqrestore(&phba->hbalock, iflag);
7422 return rc;
7423}
7424
7425/**
7426 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7427 * @phba: Pointer to HBA context object.
7428 * @pmbox: Pointer to mailbox object.
7429 * @flag: Flag indicating how the mailbox need to be processed.
7430 *
7431 * This function is called by discovery code and HBA management code to submit
7432 * a mailbox command to firmware with SLI-4 interface spec.
7433 *
7434 * Return codes the caller owns the mailbox command after the return of the
7435 * function.
7436 **/
7437static int
7438lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7439 uint32_t flag)
7440{
7441 struct lpfc_sli *psli = &phba->sli;
7442 unsigned long iflags;
7443 int rc;
7444
James Smartb76f2dc2011-07-22 18:37:42 -04007445 /* dump from issue mailbox command if setup */
7446 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7447
James Smart8fa38512009-07-19 10:01:03 -04007448 rc = lpfc_mbox_dev_check(phba);
7449 if (unlikely(rc)) {
7450 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007451 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -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 Smart8fa38512009-07-19 10:01:03 -04007457 psli->sli_flag, flag);
7458 goto out_not_finished;
7459 }
7460
James Smartda0436e2009-05-22 14:51:39 -04007461 /* Detect polling mode and jump to a handler */
7462 if (!phba->sli4_hba.intr_enable) {
7463 if (flag == MBX_POLL)
7464 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7465 else
7466 rc = -EIO;
7467 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007468 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007469 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007470 "(x%x/x%x) failure: "
7471 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7472 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007473 mboxq->vport ? mboxq->vport->vpi : 0,
7474 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007475 lpfc_sli_config_mbox_subsys_get(phba,
7476 mboxq),
7477 lpfc_sli_config_mbox_opcode_get(phba,
7478 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007479 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7480 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7481 bf_get(lpfc_mcqe_ext_status,
7482 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007483 psli->sli_flag, flag);
7484 return rc;
7485 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007486 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7487 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007488 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007489 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007490 mboxq->vport ? mboxq->vport->vpi : 0,
7491 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007492 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7493 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007494 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007495 /* Try to block the asynchronous mailbox posting */
7496 rc = lpfc_sli4_async_mbox_block(phba);
7497 if (!rc) {
7498 /* Successfully blocked, now issue sync mbox cmd */
7499 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7500 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007501 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007502 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007503 "(%d):2597 Sync Mailbox command "
7504 "x%x (x%x/x%x) failure: "
7505 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7506 "Data: x%x x%x\n,",
7507 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007508 mboxq->u.mb.mbxCommand,
7509 lpfc_sli_config_mbox_subsys_get(phba,
7510 mboxq),
7511 lpfc_sli_config_mbox_opcode_get(phba,
7512 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007513 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7514 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7515 bf_get(lpfc_mcqe_ext_status,
7516 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007517 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007518 /* Unblock the async mailbox posting afterward */
7519 lpfc_sli4_async_mbox_unblock(phba);
7520 }
7521 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007522 }
7523
7524 /* Now, interrupt mode asynchrous mailbox command */
7525 rc = lpfc_mbox_cmd_check(phba, mboxq);
7526 if (rc) {
7527 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007528 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007529 "cannot issue Data: x%x x%x\n",
7530 mboxq->vport ? mboxq->vport->vpi : 0,
7531 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007532 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7533 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007534 psli->sli_flag, flag);
7535 goto out_not_finished;
7536 }
James Smartda0436e2009-05-22 14:51:39 -04007537
7538 /* Put the mailbox command to the driver internal FIFO */
7539 psli->slistat.mbox_busy++;
7540 spin_lock_irqsave(&phba->hbalock, iflags);
7541 lpfc_mbox_put(phba, mboxq);
7542 spin_unlock_irqrestore(&phba->hbalock, iflags);
7543 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7544 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007545 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007546 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7547 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007548 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7549 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007550 phba->pport->port_state,
7551 psli->sli_flag, MBX_NOWAIT);
7552 /* Wake up worker thread to transport mailbox command from head */
7553 lpfc_worker_wake_up(phba);
7554
7555 return MBX_BUSY;
7556
7557out_not_finished:
7558 return MBX_NOT_FINISHED;
7559}
7560
7561/**
7562 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7563 * @phba: Pointer to HBA context object.
7564 *
7565 * This function is called by worker thread to send a mailbox command to
7566 * SLI4 HBA firmware.
7567 *
7568 **/
7569int
7570lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7571{
7572 struct lpfc_sli *psli = &phba->sli;
7573 LPFC_MBOXQ_t *mboxq;
7574 int rc = MBX_SUCCESS;
7575 unsigned long iflags;
7576 struct lpfc_mqe *mqe;
7577 uint32_t mbx_cmnd;
7578
7579 /* Check interrupt mode before post async mailbox command */
7580 if (unlikely(!phba->sli4_hba.intr_enable))
7581 return MBX_NOT_FINISHED;
7582
7583 /* Check for mailbox command service token */
7584 spin_lock_irqsave(&phba->hbalock, iflags);
7585 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7586 spin_unlock_irqrestore(&phba->hbalock, iflags);
7587 return MBX_NOT_FINISHED;
7588 }
7589 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7590 spin_unlock_irqrestore(&phba->hbalock, iflags);
7591 return MBX_NOT_FINISHED;
7592 }
7593 if (unlikely(phba->sli.mbox_active)) {
7594 spin_unlock_irqrestore(&phba->hbalock, iflags);
7595 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7596 "0384 There is pending active mailbox cmd\n");
7597 return MBX_NOT_FINISHED;
7598 }
7599 /* Take the mailbox command service token */
7600 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7601
7602 /* Get the next mailbox command from head of queue */
7603 mboxq = lpfc_mbox_get(phba);
7604
7605 /* If no more mailbox command waiting for post, we're done */
7606 if (!mboxq) {
7607 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7608 spin_unlock_irqrestore(&phba->hbalock, iflags);
7609 return MBX_SUCCESS;
7610 }
7611 phba->sli.mbox_active = mboxq;
7612 spin_unlock_irqrestore(&phba->hbalock, iflags);
7613
7614 /* Check device readiness for posting mailbox command */
7615 rc = lpfc_mbox_dev_check(phba);
7616 if (unlikely(rc))
7617 /* Driver clean routine will clean up pending mailbox */
7618 goto out_not_finished;
7619
7620 /* Prepare the mbox command to be posted */
7621 mqe = &mboxq->u.mqe;
7622 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7623
7624 /* Start timer for the mbox_tmo and log some mailbox post messages */
7625 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007626 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007627
7628 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007629 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007630 "x%x x%x\n",
7631 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007632 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7633 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007634 phba->pport->port_state, psli->sli_flag);
7635
7636 if (mbx_cmnd != MBX_HEARTBEAT) {
7637 if (mboxq->vport) {
7638 lpfc_debugfs_disc_trc(mboxq->vport,
7639 LPFC_DISC_TRC_MBOX_VPORT,
7640 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7641 mbx_cmnd, mqe->un.mb_words[0],
7642 mqe->un.mb_words[1]);
7643 } else {
7644 lpfc_debugfs_disc_trc(phba->pport,
7645 LPFC_DISC_TRC_MBOX,
7646 "MBOX Send: cmd:x%x mb:x%x x%x",
7647 mbx_cmnd, mqe->un.mb_words[0],
7648 mqe->un.mb_words[1]);
7649 }
7650 }
7651 psli->slistat.mbox_cmd++;
7652
7653 /* Post the mailbox command to the port */
7654 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7655 if (rc != MBX_SUCCESS) {
7656 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007657 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007658 "cannot issue Data: x%x x%x\n",
7659 mboxq->vport ? mboxq->vport->vpi : 0,
7660 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007661 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7662 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007663 psli->sli_flag, MBX_NOWAIT);
7664 goto out_not_finished;
7665 }
7666
7667 return rc;
7668
7669out_not_finished:
7670 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007671 if (phba->sli.mbox_active) {
7672 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7673 __lpfc_mbox_cmpl_put(phba, mboxq);
7674 /* Release the token */
7675 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7676 phba->sli.mbox_active = NULL;
7677 }
James Smartda0436e2009-05-22 14:51:39 -04007678 spin_unlock_irqrestore(&phba->hbalock, iflags);
7679
7680 return MBX_NOT_FINISHED;
7681}
7682
7683/**
7684 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7685 * @phba: Pointer to HBA context object.
7686 * @pmbox: Pointer to mailbox object.
7687 * @flag: Flag indicating how the mailbox need to be processed.
7688 *
7689 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7690 * the API jump table function pointer from the lpfc_hba struct.
7691 *
7692 * Return codes the caller owns the mailbox command after the return of the
7693 * function.
7694 **/
7695int
7696lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7697{
7698 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7699}
7700
7701/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007702 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007703 * @phba: The hba struct for which this call is being executed.
7704 * @dev_grp: The HBA PCI-Device group number.
7705 *
7706 * This routine sets up the mbox interface API function jump table in @phba
7707 * struct.
7708 * Returns: 0 - success, -ENODEV - failure.
7709 **/
7710int
7711lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7712{
7713
7714 switch (dev_grp) {
7715 case LPFC_PCI_DEV_LP:
7716 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7717 phba->lpfc_sli_handle_slow_ring_event =
7718 lpfc_sli_handle_slow_ring_event_s3;
7719 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7720 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7721 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7722 break;
7723 case LPFC_PCI_DEV_OC:
7724 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7725 phba->lpfc_sli_handle_slow_ring_event =
7726 lpfc_sli_handle_slow_ring_event_s4;
7727 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7728 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7729 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7730 break;
7731 default:
7732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7733 "1420 Invalid HBA PCI-device group: 0x%x\n",
7734 dev_grp);
7735 return -ENODEV;
7736 break;
7737 }
7738 return 0;
7739}
7740
7741/**
James Smart3621a712009-04-06 18:47:14 -04007742 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007743 * @phba: Pointer to HBA context object.
7744 * @pring: Pointer to driver SLI ring object.
7745 * @piocb: Pointer to address of newly added command iocb.
7746 *
7747 * This function is called with hbalock held to add a command
7748 * iocb to the txq when SLI layer cannot submit the command iocb
7749 * to the ring.
7750 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007751void
James Smart92d7f7b2007-06-17 19:56:38 -05007752__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007753 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007754{
7755 /* Insert the caller's iocb in the txq tail for later processing. */
7756 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007757}
7758
James Smarte59058c2008-08-24 21:49:00 -04007759/**
James Smart3621a712009-04-06 18:47:14 -04007760 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007761 * @phba: Pointer to HBA context object.
7762 * @pring: Pointer to driver SLI ring object.
7763 * @piocb: Pointer to address of newly added command iocb.
7764 *
7765 * This function is called with hbalock held before a new
7766 * iocb is submitted to the firmware. This function checks
7767 * txq to flush the iocbs in txq to Firmware before
7768 * submitting new iocbs to the Firmware.
7769 * If there are iocbs in the txq which need to be submitted
7770 * to firmware, lpfc_sli_next_iocb returns the first element
7771 * of the txq after dequeuing it from txq.
7772 * If there is no iocb in the txq then the function will return
7773 * *piocb and *piocb is set to NULL. Caller needs to check
7774 * *piocb to find if there are more commands in the txq.
7775 **/
dea31012005-04-17 16:05:31 -05007776static struct lpfc_iocbq *
7777lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007778 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007779{
7780 struct lpfc_iocbq * nextiocb;
7781
7782 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7783 if (!nextiocb) {
7784 nextiocb = *piocb;
7785 *piocb = NULL;
7786 }
7787
7788 return nextiocb;
7789}
7790
James Smarte59058c2008-08-24 21:49:00 -04007791/**
James Smart3772a992009-05-22 14:50:54 -04007792 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007793 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007794 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007795 * @piocb: Pointer to command iocb.
7796 * @flag: Flag indicating if this command can be put into txq.
7797 *
James Smart3772a992009-05-22 14:50:54 -04007798 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7799 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7800 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7801 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7802 * this function allows only iocbs for posting buffers. This function finds
7803 * next available slot in the command ring and posts the command to the
7804 * available slot and writes the port attention register to request HBA start
7805 * processing new iocb. If there is no slot available in the ring and
7806 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7807 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007808 *
James Smart3772a992009-05-22 14:50:54 -04007809 * This function is called with hbalock held. The function will return success
7810 * after it successfully submit the iocb to firmware or after adding to the
7811 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007812 **/
James Smart98c9ea52007-10-27 13:37:33 -04007813static int
James Smart3772a992009-05-22 14:50:54 -04007814__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007815 struct lpfc_iocbq *piocb, uint32_t flag)
7816{
7817 struct lpfc_iocbq *nextiocb;
7818 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007819 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007820
James Smart92d7f7b2007-06-17 19:56:38 -05007821 if (piocb->iocb_cmpl && (!piocb->vport) &&
7822 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7823 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7824 lpfc_printf_log(phba, KERN_ERR,
7825 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007826 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007827 piocb->iocb.ulpCommand);
7828 dump_stack();
7829 return IOCB_ERROR;
7830 }
7831
7832
Linas Vepstas8d63f372007-02-14 14:28:36 -06007833 /* If the PCI channel is in offline state, do not post iocbs. */
7834 if (unlikely(pci_channel_offline(phba->pcidev)))
7835 return IOCB_ERROR;
7836
James Smarta257bf92009-04-06 18:48:10 -04007837 /* If HBA has a deferred error attention, fail the iocb. */
7838 if (unlikely(phba->hba_flag & DEFER_ERATT))
7839 return IOCB_ERROR;
7840
dea31012005-04-17 16:05:31 -05007841 /*
7842 * We should never get an IOCB if we are in a < LINK_DOWN state
7843 */
James Smart2e0fef82007-06-17 19:56:36 -05007844 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007845 return IOCB_ERROR;
7846
7847 /*
7848 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007849 * outstanding event.
dea31012005-04-17 16:05:31 -05007850 */
James Smart0b727fe2007-10-27 13:37:25 -04007851 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007852 goto iocb_busy;
7853
James Smart2e0fef82007-06-17 19:56:36 -05007854 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007855 /*
James Smart2680eea2007-04-25 09:52:55 -04007856 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007857 * can be issued if the link is not up.
7858 */
7859 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007860 case CMD_GEN_REQUEST64_CR:
7861 case CMD_GEN_REQUEST64_CX:
7862 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7863 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007864 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007865 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7866 MENLO_TRANSPORT_TYPE))
7867
7868 goto iocb_busy;
7869 break;
dea31012005-04-17 16:05:31 -05007870 case CMD_QUE_RING_BUF_CN:
7871 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007872 /*
7873 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7874 * completion, iocb_cmpl MUST be 0.
7875 */
7876 if (piocb->iocb_cmpl)
7877 piocb->iocb_cmpl = NULL;
7878 /*FALLTHROUGH*/
7879 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007880 case CMD_CLOSE_XRI_CN:
7881 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007882 break;
7883 default:
7884 goto iocb_busy;
7885 }
7886
7887 /*
7888 * For FCP commands, we must be in a state where we can process link
7889 * attention events.
7890 */
7891 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007892 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007893 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007894 }
dea31012005-04-17 16:05:31 -05007895
dea31012005-04-17 16:05:31 -05007896 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7897 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7898 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7899
7900 if (iocb)
7901 lpfc_sli_update_ring(phba, pring);
7902 else
7903 lpfc_sli_update_full_ring(phba, pring);
7904
7905 if (!piocb)
7906 return IOCB_SUCCESS;
7907
7908 goto out_busy;
7909
7910 iocb_busy:
7911 pring->stats.iocb_cmd_delay++;
7912
7913 out_busy:
7914
7915 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007916 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007917 return IOCB_SUCCESS;
7918 }
7919
7920 return IOCB_BUSY;
7921}
7922
James Smart3772a992009-05-22 14:50:54 -04007923/**
James Smart4f774512009-05-22 14:52:35 -04007924 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7925 * @phba: Pointer to HBA context object.
7926 * @piocb: Pointer to command iocb.
7927 * @sglq: Pointer to the scatter gather queue object.
7928 *
7929 * This routine converts the bpl or bde that is in the IOCB
7930 * to a sgl list for the sli4 hardware. The physical address
7931 * of the bpl/bde is converted back to a virtual address.
7932 * If the IOCB contains a BPL then the list of BDE's is
7933 * converted to sli4_sge's. If the IOCB contains a single
7934 * BDE then it is converted to a single sli_sge.
7935 * The IOCB is still in cpu endianess so the contents of
7936 * the bpl can be used without byte swapping.
7937 *
7938 * Returns valid XRI = Success, NO_XRI = Failure.
7939**/
7940static uint16_t
7941lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7942 struct lpfc_sglq *sglq)
7943{
7944 uint16_t xritag = NO_XRI;
7945 struct ulp_bde64 *bpl = NULL;
7946 struct ulp_bde64 bde;
7947 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007948 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007949 IOCB_t *icmd;
7950 int numBdes = 0;
7951 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007952 uint32_t offset = 0; /* accumulated offset in the sg request list */
7953 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007954
7955 if (!piocbq || !sglq)
7956 return xritag;
7957
7958 sgl = (struct sli4_sge *)sglq->sgl;
7959 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007960 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7961 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007962 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7963 numBdes = icmd->un.genreq64.bdl.bdeSize /
7964 sizeof(struct ulp_bde64);
7965 /* The addrHigh and addrLow fields within the IOCB
7966 * have not been byteswapped yet so there is no
7967 * need to swap them back.
7968 */
James Smart1b511972011-12-13 13:23:09 -05007969 if (piocbq->context3)
7970 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7971 else
7972 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007973
James Smart1b511972011-12-13 13:23:09 -05007974 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007975 if (!bpl)
7976 return xritag;
7977
7978 for (i = 0; i < numBdes; i++) {
7979 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007980 sgl->addr_hi = bpl->addrHigh;
7981 sgl->addr_lo = bpl->addrLow;
7982
James Smart05580562011-05-24 11:40:48 -04007983 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007984 if ((i+1) == numBdes)
7985 bf_set(lpfc_sli4_sge_last, sgl, 1);
7986 else
7987 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007988 /* swap the size field back to the cpu so we
7989 * can assign it to the sgl.
7990 */
7991 bde.tus.w = le32_to_cpu(bpl->tus.w);
7992 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007993 /* The offsets in the sgl need to be accumulated
7994 * separately for the request and reply lists.
7995 * The request is always first, the reply follows.
7996 */
7997 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7998 /* add up the reply sg entries */
7999 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8000 inbound++;
8001 /* first inbound? reset the offset */
8002 if (inbound == 1)
8003 offset = 0;
8004 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008005 bf_set(lpfc_sli4_sge_type, sgl,
8006 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008007 offset += bde.tus.f.bdeSize;
8008 }
James Smart546fc852011-03-11 16:06:29 -05008009 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008010 bpl++;
8011 sgl++;
8012 }
8013 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8014 /* The addrHigh and addrLow fields of the BDE have not
8015 * been byteswapped yet so they need to be swapped
8016 * before putting them in the sgl.
8017 */
8018 sgl->addr_hi =
8019 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8020 sgl->addr_lo =
8021 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008022 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008023 bf_set(lpfc_sli4_sge_last, sgl, 1);
8024 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008025 sgl->sge_len =
8026 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008027 }
8028 return sglq->sli4_xritag;
8029}
8030
8031/**
8032 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
8033 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04008034 *
James Smarta93ff372010-10-22 11:06:08 -04008035 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04008036 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
8037 * held.
James Smart4f774512009-05-22 14:52:35 -04008038 *
8039 * Return: index into SLI4 fast-path FCP queue index.
8040 **/
James Smart2a76a282012-08-03 12:35:54 -04008041static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04008042lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04008043{
James Smart7bb03bb2013-04-17 20:19:16 -04008044 struct lpfc_vector_map_info *cpup;
8045 int chann, cpu;
James Smart4f774512009-05-22 14:52:35 -04008046
James Smart76f96b62013-12-17 20:29:01 -05008047 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
8048 && phba->cfg_fcp_io_channel > 1) {
James Smart7bb03bb2013-04-17 20:19:16 -04008049 cpu = smp_processor_id();
8050 if (cpu < phba->sli4_hba.num_present_cpu) {
8051 cpup = phba->sli4_hba.cpu_map;
8052 cpup += cpu;
8053 return cpup->channel_id;
8054 }
8055 chann = cpu;
8056 }
8057 chann = atomic_add_return(1, &phba->fcp_qidx);
8058 chann = (chann % phba->cfg_fcp_io_channel);
8059 return chann;
James Smart4f774512009-05-22 14:52:35 -04008060}
8061
8062/**
8063 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8064 * @phba: Pointer to HBA context object.
8065 * @piocb: Pointer to command iocb.
8066 * @wqe: Pointer to the work queue entry.
8067 *
8068 * This routine converts the iocb command to its Work Queue Entry
8069 * equivalent. The wqe pointer should not have any fields set when
8070 * this routine is called because it will memcpy over them.
8071 * This routine does not set the CQ_ID or the WQEC bits in the
8072 * wqe.
8073 *
8074 * Returns: 0 = Success, IOCB_ERROR = Failure.
8075 **/
8076static int
8077lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8078 union lpfc_wqe *wqe)
8079{
James Smart5ffc2662009-11-18 15:39:44 -05008080 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008081 uint8_t ct = 0;
8082 uint32_t fip;
8083 uint32_t abort_tag;
8084 uint8_t command_type = ELS_COMMAND_NON_FIP;
8085 uint8_t cmnd;
8086 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008087 uint16_t abrt_iotag;
8088 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008089 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008090 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008091 int numBdes, i;
8092 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008093 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008094 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008095 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008096
James Smart45ed1192009-10-02 15:17:02 -04008097 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008098 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008099 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008100 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008101 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008102 command_type = ELS_COMMAND_FIP;
8103 else
8104 command_type = ELS_COMMAND_NON_FIP;
8105
James Smart4f774512009-05-22 14:52:35 -04008106 /* Some of the fields are in the right position already */
8107 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8108 abort_tag = (uint32_t) iocbq->iotag;
8109 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04008110 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04008111 /* words0-2 bpl convert bde */
8112 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008113 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8114 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008115 bpl = (struct ulp_bde64 *)
8116 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8117 if (!bpl)
8118 return IOCB_ERROR;
8119
8120 /* Should already be byte swapped. */
8121 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8122 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8123 /* swap the size field back to the cpu so we
8124 * can assign it to the sgl.
8125 */
8126 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008127 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8128 total_len = 0;
8129 for (i = 0; i < numBdes; i++) {
8130 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8131 total_len += bde.tus.f.bdeSize;
8132 }
James Smart4f774512009-05-22 14:52:35 -04008133 } else
James Smart5ffc2662009-11-18 15:39:44 -05008134 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008135
8136 iocbq->iocb.ulpIoTag = iocbq->iotag;
8137 cmnd = iocbq->iocb.ulpCommand;
8138
8139 switch (iocbq->iocb.ulpCommand) {
8140 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008141 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8142 ndlp = iocbq->context_un.ndlp;
8143 else
8144 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008145 if (!iocbq->iocb.ulpLe) {
8146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8147 "2007 Only Limited Edition cmd Format"
8148 " supported 0x%x\n",
8149 iocbq->iocb.ulpCommand);
8150 return IOCB_ERROR;
8151 }
James Smartff78d8f2011-12-13 13:21:35 -05008152
James Smart5ffc2662009-11-18 15:39:44 -05008153 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008154 /* Els_reguest64 has a TMO */
8155 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8156 iocbq->iocb.ulpTimeout);
8157 /* Need a VF for word 4 set the vf bit*/
8158 bf_set(els_req64_vf, &wqe->els_req, 0);
8159 /* And a VFID for word 12 */
8160 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008161 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008162 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8163 iocbq->iocb.ulpContext);
8164 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8165 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008166 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008167 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008168 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8169 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008170 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8171 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008172 if_type = bf_get(lpfc_sli_intf_if_type,
8173 &phba->sli4_hba.sli_intf);
8174 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008175 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008176 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008177 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008178 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008179 *pcmd == ELS_CMD_PLOGI)) {
8180 bf_set(els_req64_sp, &wqe->els_req, 1);
8181 bf_set(els_req64_sid, &wqe->els_req,
8182 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008183 if ((*pcmd == ELS_CMD_FLOGI) &&
8184 !(phba->fc_topology ==
8185 LPFC_TOPOLOGY_LOOP))
8186 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008187 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8188 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008189 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008190 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008191 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8192 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8193 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8194 }
James Smartc8685952009-11-18 15:39:16 -05008195 }
James Smart6d368e52011-05-24 11:44:12 -04008196 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8197 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008198 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8199 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8200 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8201 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8202 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8203 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008204 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008205 break;
James Smart5ffc2662009-11-18 15:39:44 -05008206 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008207 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8208 iocbq->iocb.un.ulpWord[3]);
8209 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008210 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008211 /* The entire sequence is transmitted for this IOCB */
8212 xmit_len = total_len;
8213 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008214 if (phba->link_flag & LS_LOOPBACK_MODE)
8215 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008216 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008217 /* word3 iocb=io_tag32 wqe=reserved */
8218 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008219 /* word4 relative_offset memcpy */
8220 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008221 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8222 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8223 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8224 LPFC_WQE_IOD_WRITE);
8225 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8226 LPFC_WQE_LENLOC_WORD12);
8227 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008228 wqe->xmit_sequence.xmit_len = xmit_len;
8229 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008230 break;
James Smart4f774512009-05-22 14:52:35 -04008231 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008232 /* word3 iocb=iotag32 wqe=seq_payload_len */
8233 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008234 /* word4 iocb=rsvd wqe=rsvd */
8235 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8236 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008237 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008238 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008239 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8240 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8241 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8242 LPFC_WQE_LENLOC_WORD3);
8243 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008244 break;
James Smart4f774512009-05-22 14:52:35 -04008245 case CMD_FCP_IWRITE64_CR:
8246 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008247 /* word3 iocb=iotag wqe=payload_offset_len */
8248 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008249 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8250 xmit_len + sizeof(struct fcp_rsp));
8251 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8252 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008253 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8254 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8255 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8256 iocbq->iocb.ulpFCP2Rcvy);
8257 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8258 /* Always open the exchange */
8259 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008260 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8261 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8262 LPFC_WQE_LENLOC_WORD4);
8263 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8264 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008265 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008266 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8267 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8268 if (phba->cfg_XLanePriority) {
8269 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8270 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8271 (phba->cfg_XLanePriority << 1));
8272 }
8273 }
James Smart7851fe22011-07-22 18:36:52 -04008274 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008275 case CMD_FCP_IREAD64_CR:
8276 /* word3 iocb=iotag wqe=payload_offset_len */
8277 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008278 bf_set(payload_offset_len, &wqe->fcp_iread,
8279 xmit_len + sizeof(struct fcp_rsp));
8280 bf_set(cmd_buff_len, &wqe->fcp_iread,
8281 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008282 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8283 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8284 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8285 iocbq->iocb.ulpFCP2Rcvy);
8286 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008287 /* Always open the exchange */
8288 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008289 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8290 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8291 LPFC_WQE_LENLOC_WORD4);
8292 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8293 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008294 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008295 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8296 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8297 if (phba->cfg_XLanePriority) {
8298 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8299 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8300 (phba->cfg_XLanePriority << 1));
8301 }
8302 }
James Smart7851fe22011-07-22 18:36:52 -04008303 break;
James Smartf1126682009-06-10 17:22:44 -04008304 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008305 /* word3 iocb=iotag wqe=payload_offset_len */
8306 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8307 bf_set(payload_offset_len, &wqe->fcp_icmd,
8308 xmit_len + sizeof(struct fcp_rsp));
8309 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8310 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008311 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008312 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008313 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008314 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8315 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8316 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8317 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8318 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8319 LPFC_WQE_LENLOC_NONE);
8320 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart2a94aea2012-09-29 11:30:31 -04008321 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8322 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008323 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8324 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8325 if (phba->cfg_XLanePriority) {
8326 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8327 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8328 (phba->cfg_XLanePriority << 1));
8329 }
8330 }
James Smart7851fe22011-07-22 18:36:52 -04008331 break;
James Smart4f774512009-05-22 14:52:35 -04008332 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008333 /* For this command calculate the xmit length of the
8334 * request bde.
8335 */
8336 xmit_len = 0;
8337 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8338 sizeof(struct ulp_bde64);
8339 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008340 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008341 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8342 break;
James Smart63e801c2010-11-20 23:14:19 -05008343 xmit_len += bde.tus.f.bdeSize;
8344 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008345 /* word3 iocb=IO_TAG wqe=request_payload_len */
8346 wqe->gen_req.request_payload_len = xmit_len;
8347 /* word4 iocb=parameter wqe=relative_offset memcpy */
8348 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008349 /* word6 context tag copied in memcpy */
8350 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8351 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8352 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8353 "2015 Invalid CT %x command 0x%x\n",
8354 ct, iocbq->iocb.ulpCommand);
8355 return IOCB_ERROR;
8356 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008357 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8358 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8359 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8360 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8361 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8362 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8363 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8364 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008365 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008366 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008367 break;
James Smart4f774512009-05-22 14:52:35 -04008368 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008369 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008370 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008371 /* word3 iocb=iotag32 wqe=response_payload_len */
8372 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008373 /* word4 */
8374 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008375 /* word5 iocb=rsvd wge=did */
8376 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008377 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8378
8379 if_type = bf_get(lpfc_sli_intf_if_type,
8380 &phba->sli4_hba.sli_intf);
8381 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8382 if (iocbq->vport->fc_flag & FC_PT2PT) {
8383 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8384 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8385 iocbq->vport->fc_myDID);
8386 if (iocbq->vport->fc_myDID == Fabric_DID) {
8387 bf_set(wqe_els_did,
8388 &wqe->xmit_els_rsp.wqe_dest, 0);
8389 }
8390 }
8391 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008392 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8393 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8394 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8395 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008396 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008397 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008398 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008399 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008400 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8401 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8402 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8403 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8404 LPFC_WQE_LENLOC_WORD3);
8405 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008406 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8407 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008408 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8409 iocbq->context2)->virt);
8410 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008411 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8412 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008413 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008414 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8415 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008416 phba->vpi_ids[phba->pport->vpi]);
8417 }
James Smart4f774512009-05-22 14:52:35 -04008418 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008419 break;
James Smart4f774512009-05-22 14:52:35 -04008420 case CMD_CLOSE_XRI_CN:
8421 case CMD_ABORT_XRI_CN:
8422 case CMD_ABORT_XRI_CX:
8423 /* words 0-2 memcpy should be 0 rserved */
8424 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008425 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8426 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8427 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8428 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8429 } else
8430 fip = 0;
8431
8432 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008433 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008434 * The link is down, or the command was ELS_FIP
8435 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008436 * on the wire.
8437 */
8438 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8439 else
8440 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8441 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008442 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8443 wqe->abort_cmd.rsrvd5 = 0;
8444 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008445 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8446 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008447 /*
8448 * The abort handler will send us CMD_ABORT_XRI_CN or
8449 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8450 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008451 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8452 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8453 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8454 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008455 cmnd = CMD_ABORT_XRI_CX;
8456 command_type = OTHER_COMMAND;
8457 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008458 break;
James Smart6669f9b2009-10-02 15:16:45 -04008459 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008460 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008461 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008462 * we re-construct this WQE here based on information in
8463 * iocbq from scratch.
8464 */
8465 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008466 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008467 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008468 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8469 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008470 LPFC_ABTS_UNSOL_INT) {
8471 /* ABTS sent by initiator to CT exchange, the
8472 * RX_ID field will be filled with the newly
8473 * allocated responder XRI.
8474 */
8475 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8476 iocbq->sli4_xritag);
8477 } else {
8478 /* ABTS sent by responder to CT exchange, the
8479 * RX_ID field will be filled with the responder
8480 * RX_ID from ABTS.
8481 */
8482 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008483 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008484 }
James Smart6669f9b2009-10-02 15:16:45 -04008485 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8486 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008487
8488 /* Use CT=VPI */
8489 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8490 ndlp->nlp_DID);
8491 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8492 iocbq->iocb.ulpContext);
8493 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008494 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008495 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008496 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8497 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8498 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008499 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8500 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008501 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8502 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8503 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8504 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8505 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8506 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8507 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8508 }
8509
James Smart7851fe22011-07-22 18:36:52 -04008510 break;
James Smart4f774512009-05-22 14:52:35 -04008511 case CMD_XRI_ABORTED_CX:
8512 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008513 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8514 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8515 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8516 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8517 default:
8518 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8519 "2014 Invalid command 0x%x\n",
8520 iocbq->iocb.ulpCommand);
8521 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008522 break;
James Smart4f774512009-05-22 14:52:35 -04008523 }
James Smart6d368e52011-05-24 11:44:12 -04008524
James Smart8012cc32012-10-31 14:44:49 -04008525 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8526 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8527 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8528 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8529 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8530 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8531 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8532 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008533 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8534 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8535 wqe->generic.wqe_com.abort_tag = abort_tag;
8536 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8537 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8538 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8539 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008540 return 0;
8541}
8542
8543/**
8544 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8545 * @phba: Pointer to HBA context object.
8546 * @ring_number: SLI ring number to issue iocb on.
8547 * @piocb: Pointer to command iocb.
8548 * @flag: Flag indicating if this command can be put into txq.
8549 *
8550 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8551 * an iocb command to an HBA with SLI-4 interface spec.
8552 *
8553 * This function is called with hbalock held. The function will return success
8554 * after it successfully submit the iocb to firmware or after adding to the
8555 * txq.
8556 **/
8557static int
8558__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8559 struct lpfc_iocbq *piocb, uint32_t flag)
8560{
8561 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008562 union lpfc_wqe wqe;
James Smart1ba981f2014-02-20 09:56:45 -05008563 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008564 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008565
8566 if (piocb->sli4_xritag == NO_XRI) {
8567 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008568 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008569 sglq = NULL;
8570 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008571 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008572 if (!(flag & SLI_IOCB_RET_IOCB)) {
8573 __lpfc_sli_ringtx_put(phba,
8574 pring, piocb);
8575 return IOCB_SUCCESS;
8576 } else {
8577 return IOCB_BUSY;
8578 }
8579 } else {
James Smart6d368e52011-05-24 11:44:12 -04008580 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008581 if (!sglq) {
8582 if (!(flag & SLI_IOCB_RET_IOCB)) {
8583 __lpfc_sli_ringtx_put(phba,
8584 pring,
8585 piocb);
8586 return IOCB_SUCCESS;
8587 } else
8588 return IOCB_BUSY;
8589 }
8590 }
James Smart4f774512009-05-22 14:52:35 -04008591 }
8592 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008593 /* These IO's already have an XRI and a mapped sgl. */
8594 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008595 } else {
James Smart6d368e52011-05-24 11:44:12 -04008596 /*
8597 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008598 * sglq is on the active list
8599 */
James Smartedccdc12013-01-03 15:43:45 -05008600 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008601 if (!sglq)
8602 return IOCB_ERROR;
8603 }
8604
8605 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008606 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008607 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008608 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008609 return IOCB_ERROR;
8610 }
8611
8612 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8613 return IOCB_ERROR;
8614
James Smart341af102010-01-26 23:07:37 -05008615 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008616 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8617 if (!phba->cfg_EnableXLane || (!(piocb->iocb_flag &
8618 LPFC_IO_OAS))) {
8619 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8620 } else {
8621 wq = phba->sli4_hba.oas_wq;
8622 }
8623 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart4f774512009-05-22 14:52:35 -04008624 return IOCB_ERROR;
8625 } else {
James Smartea714f32013-04-17 20:18:47 -04008626 if (unlikely(!phba->sli4_hba.els_wq))
8627 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04008628 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8629 return IOCB_ERROR;
8630 }
8631 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8632
8633 return 0;
8634}
8635
8636/**
James Smart3772a992009-05-22 14:50:54 -04008637 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8638 *
8639 * This routine wraps the actual lockless version for issusing IOCB function
8640 * pointer from the lpfc_hba struct.
8641 *
8642 * Return codes:
8643 * IOCB_ERROR - Error
8644 * IOCB_SUCCESS - Success
8645 * IOCB_BUSY - Busy
8646 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008647int
James Smart3772a992009-05-22 14:50:54 -04008648__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8649 struct lpfc_iocbq *piocb, uint32_t flag)
8650{
8651 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8652}
8653
8654/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008655 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008656 * @phba: The hba struct for which this call is being executed.
8657 * @dev_grp: The HBA PCI-Device group number.
8658 *
8659 * This routine sets up the SLI interface API function jump table in @phba
8660 * struct.
8661 * Returns: 0 - success, -ENODEV - failure.
8662 **/
8663int
8664lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8665{
8666
8667 switch (dev_grp) {
8668 case LPFC_PCI_DEV_LP:
8669 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8670 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8671 break;
James Smart4f774512009-05-22 14:52:35 -04008672 case LPFC_PCI_DEV_OC:
8673 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8674 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8675 break;
James Smart3772a992009-05-22 14:50:54 -04008676 default:
8677 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8678 "1419 Invalid HBA PCI-device group: 0x%x\n",
8679 dev_grp);
8680 return -ENODEV;
8681 break;
8682 }
8683 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8684 return 0;
8685}
James Smart92d7f7b2007-06-17 19:56:38 -05008686
James Smarte59058c2008-08-24 21:49:00 -04008687/**
James Smart3621a712009-04-06 18:47:14 -04008688 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008689 * @phba: Pointer to HBA context object.
8690 * @pring: Pointer to driver SLI ring object.
8691 * @piocb: Pointer to command iocb.
8692 * @flag: Flag indicating if this command can be put into txq.
8693 *
8694 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8695 * function. This function gets the hbalock and calls
8696 * __lpfc_sli_issue_iocb function and will return the error returned
8697 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8698 * functions which do not hold hbalock.
8699 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008700int
James Smart3772a992009-05-22 14:50:54 -04008701lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008702 struct lpfc_iocbq *piocb, uint32_t flag)
8703{
James Smartba20c852012-08-03 12:36:52 -04008704 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008705 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008706 struct lpfc_queue *fpeq;
8707 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008708 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008709 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008710
James Smart7e56aa22012-08-03 12:35:34 -04008711 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008712 if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart1ba981f2014-02-20 09:56:45 -05008713 if (!phba->cfg_EnableXLane || (!(piocb->iocb_flag &
8714 LPFC_IO_OAS))) {
8715 if (unlikely(!phba->sli4_hba.fcp_wq))
8716 return IOCB_ERROR;
8717 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8718 piocb->fcp_wqidx = idx;
8719 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
8720 } else {
8721 if (unlikely(!phba->sli4_hba.oas_wq))
8722 return IOCB_ERROR;
8723 idx = 0;
8724 piocb->fcp_wqidx = 0;
8725 ring_number = LPFC_FCP_OAS_RING;
8726 }
James Smartba20c852012-08-03 12:36:52 -04008727 pring = &phba->sli.ring[ring_number];
8728 spin_lock_irqsave(&pring->ring_lock, iflags);
8729 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8730 flag);
8731 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8732
8733 if (lpfc_fcp_look_ahead) {
8734 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8735
8736 if (atomic_dec_and_test(&fcp_eq_hdl->
8737 fcp_eq_in_use)) {
8738
8739 /* Get associated EQ with this index */
8740 fpeq = phba->sli4_hba.hba_eq[idx];
8741
8742 /* Turn off interrupts from this EQ */
8743 lpfc_sli4_eq_clr_intr(fpeq);
8744
8745 /*
8746 * Process all the events on FCP EQ
8747 */
8748 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8749 lpfc_sli4_hba_handle_eqe(phba,
8750 eqe, idx);
8751 fpeq->EQ_processed++;
8752 }
8753
8754 /* Always clear and re-arm the EQ */
8755 lpfc_sli4_eq_release(fpeq,
8756 LPFC_QUEUE_REARM);
8757 }
8758 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8759 }
8760 } else {
8761 pring = &phba->sli.ring[ring_number];
8762 spin_lock_irqsave(&pring->ring_lock, iflags);
8763 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8764 flag);
8765 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8766
James Smart2a76a282012-08-03 12:35:54 -04008767 }
James Smart7e56aa22012-08-03 12:35:34 -04008768 } else {
8769 /* For now, SLI2/3 will still use hbalock */
8770 spin_lock_irqsave(&phba->hbalock, iflags);
8771 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8772 spin_unlock_irqrestore(&phba->hbalock, iflags);
8773 }
James Smart92d7f7b2007-06-17 19:56:38 -05008774 return rc;
8775}
8776
James Smarte59058c2008-08-24 21:49:00 -04008777/**
James Smart3621a712009-04-06 18:47:14 -04008778 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008779 * @phba: Pointer to HBA context object.
8780 *
8781 * This function is called while driver attaches with the
8782 * HBA to setup the extra ring. The extra ring is used
8783 * only when driver needs to support target mode functionality
8784 * or IP over FC functionalities.
8785 *
8786 * This function is called with no lock held.
8787 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008788static int
8789lpfc_extra_ring_setup( struct lpfc_hba *phba)
8790{
8791 struct lpfc_sli *psli;
8792 struct lpfc_sli_ring *pring;
8793
8794 psli = &phba->sli;
8795
8796 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008797
8798 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008799 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008800 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8801 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8802 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8803 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008804
James Smarta4bc3372006-12-02 13:34:16 -05008805 /* and give them to the extra ring */
8806 pring = &psli->ring[psli->extra_ring];
8807
James Smart7e56aa22012-08-03 12:35:34 -04008808 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8809 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8810 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8811 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008812
8813 /* Setup default profile for this ring */
8814 pring->iotag_max = 4096;
8815 pring->num_mask = 1;
8816 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008817 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8818 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008819 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8820 return 0;
8821}
8822
James Smartcb69f7d2011-12-13 13:21:57 -05008823/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8824 * @phba: Pointer to HBA context object.
8825 * @iocbq: Pointer to iocb object.
8826 *
8827 * The async_event handler calls this routine when it receives
8828 * an ASYNC_STATUS_CN event from the port. The port generates
8829 * this event when an Abort Sequence request to an rport fails
8830 * twice in succession. The abort could be originated by the
8831 * driver or by the port. The ABTS could have been for an ELS
8832 * or FCP IO. The port only generates this event when an ABTS
8833 * fails to complete after one retry.
8834 */
8835static void
8836lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8837 struct lpfc_iocbq *iocbq)
8838{
8839 struct lpfc_nodelist *ndlp = NULL;
8840 uint16_t rpi = 0, vpi = 0;
8841 struct lpfc_vport *vport = NULL;
8842
8843 /* The rpi in the ulpContext is vport-sensitive. */
8844 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8845 rpi = iocbq->iocb.ulpContext;
8846
8847 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8848 "3092 Port generated ABTS async event "
8849 "on vpi %d rpi %d status 0x%x\n",
8850 vpi, rpi, iocbq->iocb.ulpStatus);
8851
8852 vport = lpfc_find_vport_by_vpid(phba, vpi);
8853 if (!vport)
8854 goto err_exit;
8855 ndlp = lpfc_findnode_rpi(vport, rpi);
8856 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8857 goto err_exit;
8858
8859 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8860 lpfc_sli_abts_recover_port(vport, ndlp);
8861 return;
8862
8863 err_exit:
8864 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8865 "3095 Event Context not found, no "
8866 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8867 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8868 vpi, rpi);
8869}
8870
8871/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8872 * @phba: pointer to HBA context object.
8873 * @ndlp: nodelist pointer for the impacted rport.
8874 * @axri: pointer to the wcqe containing the failed exchange.
8875 *
8876 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8877 * port. The port generates this event when an abort exchange request to an
8878 * rport fails twice in succession with no reply. The abort could be originated
8879 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8880 */
8881void
8882lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8883 struct lpfc_nodelist *ndlp,
8884 struct sli4_wcqe_xri_aborted *axri)
8885{
8886 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008887 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008888
James Smart6b5151f2012-01-18 16:24:06 -05008889 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008890 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8891 "3115 Node Context not found, driver "
8892 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008893 return;
8894 }
8895
James Smartcb69f7d2011-12-13 13:21:57 -05008896 vport = ndlp->vport;
8897 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8898 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008899 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04008900 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05008901 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008902 bf_get(lpfc_wcqe_xa_status, axri),
8903 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008904
James Smart5c1db2a2012-03-01 22:34:36 -05008905 /*
8906 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8907 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8908 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8909 */
James Smarte3d2b802012-08-14 14:25:43 -04008910 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05008911 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8912 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008913 lpfc_sli_abts_recover_port(vport, ndlp);
8914}
8915
James Smarte59058c2008-08-24 21:49:00 -04008916/**
James Smart3621a712009-04-06 18:47:14 -04008917 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008918 * @phba: Pointer to HBA context object.
8919 * @pring: Pointer to driver SLI ring object.
8920 * @iocbq: Pointer to iocb object.
8921 *
8922 * This function is called by the slow ring event handler
8923 * function when there is an ASYNC event iocb in the ring.
8924 * This function is called with no lock held.
8925 * Currently this function handles only temperature related
8926 * ASYNC events. The function decodes the temperature sensor
8927 * event message and posts events for the management applications.
8928 **/
James Smart98c9ea52007-10-27 13:37:33 -04008929static void
James Smart57127f12007-10-27 13:37:05 -04008930lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8931 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8932{
8933 IOCB_t *icmd;
8934 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008935 struct temp_event temp_event_data;
8936 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008937 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008938
8939 icmd = &iocbq->iocb;
8940 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008941
James Smartcb69f7d2011-12-13 13:21:57 -05008942 switch (evt_code) {
8943 case ASYNC_TEMP_WARN:
8944 case ASYNC_TEMP_SAFE:
8945 temp_event_data.data = (uint32_t) icmd->ulpContext;
8946 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8947 if (evt_code == ASYNC_TEMP_WARN) {
8948 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8949 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8950 "0347 Adapter is very hot, please take "
8951 "corrective action. temperature : %d Celsius\n",
8952 (uint32_t) icmd->ulpContext);
8953 } else {
8954 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8955 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8956 "0340 Adapter temperature is OK now. "
8957 "temperature : %d Celsius\n",
8958 (uint32_t) icmd->ulpContext);
8959 }
8960
8961 /* Send temperature change event to applications */
8962 shost = lpfc_shost_from_vport(phba->pport);
8963 fc_host_post_vendor_event(shost, fc_get_event_number(),
8964 sizeof(temp_event_data), (char *) &temp_event_data,
8965 LPFC_NL_VENDOR_ID);
8966 break;
8967 case ASYNC_STATUS_CN:
8968 lpfc_sli_abts_err_handler(phba, iocbq);
8969 break;
8970 default:
James Smarta257bf92009-04-06 18:48:10 -04008971 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008973 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008974 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008975 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8976 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8977 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8978 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008979 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008980 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8981 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8982 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8983 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8984
James Smartcb69f7d2011-12-13 13:21:57 -05008985 break;
James Smart57127f12007-10-27 13:37:05 -04008986 }
James Smart57127f12007-10-27 13:37:05 -04008987}
8988
8989
James Smarte59058c2008-08-24 21:49:00 -04008990/**
James Smart3621a712009-04-06 18:47:14 -04008991 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008992 * @phba: Pointer to HBA context object.
8993 *
8994 * lpfc_sli_setup sets up rings of the SLI interface with
8995 * number of iocbs per ring and iotags. This function is
8996 * called while driver attach to the HBA and before the
8997 * interrupts are enabled. So there is no need for locking.
8998 *
8999 * This function always returns 0.
9000 **/
dea31012005-04-17 16:05:31 -05009001int
9002lpfc_sli_setup(struct lpfc_hba *phba)
9003{
James Smarted957682007-06-17 19:56:37 -05009004 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009005 struct lpfc_sli *psli = &phba->sli;
9006 struct lpfc_sli_ring *pring;
9007
James Smart2a76a282012-08-03 12:35:54 -04009008 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9009 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009010 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009011 psli->sli_flag = 0;
9012 psli->fcp_ring = LPFC_FCP_RING;
9013 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009014 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009015
James Bottomley604a3e32005-10-29 10:28:33 -05009016 psli->iocbq_lookup = NULL;
9017 psli->iocbq_lookup_len = 0;
9018 psli->last_iotag = 0;
9019
dea31012005-04-17 16:05:31 -05009020 for (i = 0; i < psli->num_rings; i++) {
9021 pring = &psli->ring[i];
9022 switch (i) {
9023 case LPFC_FCP_RING: /* ring 0 - FCP */
9024 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009025 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9026 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9027 pring->sli.sli3.numCiocb +=
9028 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9029 pring->sli.sli3.numRiocb +=
9030 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9031 pring->sli.sli3.numCiocb +=
9032 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9033 pring->sli.sli3.numRiocb +=
9034 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9035 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009036 SLI3_IOCB_CMD_SIZE :
9037 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009038 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009039 SLI3_IOCB_RSP_SIZE :
9040 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009041 pring->iotag_ctr = 0;
9042 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009043 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009044 pring->fast_iotag = pring->iotag_max;
9045 pring->num_mask = 0;
9046 break;
James Smarta4bc3372006-12-02 13:34:16 -05009047 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009048 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009049 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9050 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9051 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009052 SLI3_IOCB_CMD_SIZE :
9053 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009054 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009055 SLI3_IOCB_RSP_SIZE :
9056 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009057 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009058 pring->num_mask = 0;
9059 break;
9060 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9061 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009062 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9063 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9064 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009065 SLI3_IOCB_CMD_SIZE :
9066 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009067 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009068 SLI3_IOCB_RSP_SIZE :
9069 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009070 pring->fast_iotag = 0;
9071 pring->iotag_ctr = 0;
9072 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009073 pring->lpfc_sli_rcv_async_status =
9074 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009075 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009076 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009077 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9078 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009079 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009080 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009081 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009082 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9083 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009084 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009085 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009086 pring->prt[2].profile = 0; /* Mask 2 */
9087 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009088 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009089 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009090 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009091 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009092 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009093 pring->prt[3].profile = 0; /* Mask 3 */
9094 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009095 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009096 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009097 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009098 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009099 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009100 break;
9101 }
James Smart7e56aa22012-08-03 12:35:34 -04009102 totiocbsize += (pring->sli.sli3.numCiocb *
9103 pring->sli.sli3.sizeCiocb) +
9104 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009105 }
James Smarted957682007-06-17 19:56:37 -05009106 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009107 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009108 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9109 "SLI2 SLIM Data: x%x x%lx\n",
9110 phba->brd_no, totiocbsize,
9111 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009112 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009113 if (phba->cfg_multi_ring_support == 2)
9114 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009115
9116 return 0;
9117}
9118
James Smarte59058c2008-08-24 21:49:00 -04009119/**
James Smart3621a712009-04-06 18:47:14 -04009120 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009121 * @phba: Pointer to HBA context object.
9122 *
9123 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9124 * ring. This function also initializes ring indices of each ring.
9125 * This function is called during the initialization of the SLI
9126 * interface of an HBA.
9127 * This function is called with no lock held and always returns
9128 * 1.
9129 **/
dea31012005-04-17 16:05:31 -05009130int
James Smart2e0fef82007-06-17 19:56:36 -05009131lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009132{
9133 struct lpfc_sli *psli;
9134 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009135 int i;
dea31012005-04-17 16:05:31 -05009136
9137 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009138 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009139 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009140 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009141 /* Initialize list headers for txq and txcmplq as double linked lists */
9142 for (i = 0; i < psli->num_rings; i++) {
9143 pring = &psli->ring[i];
9144 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009145 pring->sli.sli3.next_cmdidx = 0;
9146 pring->sli.sli3.local_getidx = 0;
9147 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009148 INIT_LIST_HEAD(&pring->txq);
9149 INIT_LIST_HEAD(&pring->txcmplq);
9150 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009151 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009152 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009153 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009154 }
James Smart2e0fef82007-06-17 19:56:36 -05009155 spin_unlock_irq(&phba->hbalock);
9156 return 1;
dea31012005-04-17 16:05:31 -05009157}
9158
James Smarte59058c2008-08-24 21:49:00 -04009159/**
James Smart04c68492009-05-22 14:52:52 -04009160 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9161 * @phba: Pointer to HBA context object.
9162 *
9163 * This routine flushes the mailbox command subsystem. It will unconditionally
9164 * flush all the mailbox commands in the three possible stages in the mailbox
9165 * command sub-system: pending mailbox command queue; the outstanding mailbox
9166 * command; and completed mailbox command queue. It is caller's responsibility
9167 * to make sure that the driver is in the proper state to flush the mailbox
9168 * command sub-system. Namely, the posting of mailbox commands into the
9169 * pending mailbox command queue from the various clients must be stopped;
9170 * either the HBA is in a state that it will never works on the outstanding
9171 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9172 * mailbox command has been completed.
9173 **/
9174static void
9175lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9176{
9177 LIST_HEAD(completions);
9178 struct lpfc_sli *psli = &phba->sli;
9179 LPFC_MBOXQ_t *pmb;
9180 unsigned long iflag;
9181
9182 /* Flush all the mailbox commands in the mbox system */
9183 spin_lock_irqsave(&phba->hbalock, iflag);
9184 /* The pending mailbox command queue */
9185 list_splice_init(&phba->sli.mboxq, &completions);
9186 /* The outstanding active mailbox command */
9187 if (psli->mbox_active) {
9188 list_add_tail(&psli->mbox_active->list, &completions);
9189 psli->mbox_active = NULL;
9190 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9191 }
9192 /* The completed mailbox command queue */
9193 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9194 spin_unlock_irqrestore(&phba->hbalock, iflag);
9195
9196 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9197 while (!list_empty(&completions)) {
9198 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9199 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9200 if (pmb->mbox_cmpl)
9201 pmb->mbox_cmpl(phba, pmb);
9202 }
9203}
9204
9205/**
James Smart3621a712009-04-06 18:47:14 -04009206 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009207 * @vport: Pointer to virtual port object.
9208 *
9209 * lpfc_sli_host_down is called to clean up the resources
9210 * associated with a vport before destroying virtual
9211 * port data structures.
9212 * This function does following operations:
9213 * - Free discovery resources associated with this virtual
9214 * port.
9215 * - Free iocbs associated with this virtual port in
9216 * the txq.
9217 * - Send abort for all iocb commands associated with this
9218 * vport in txcmplq.
9219 *
9220 * This function is called with no lock held and always returns 1.
9221 **/
dea31012005-04-17 16:05:31 -05009222int
James Smart92d7f7b2007-06-17 19:56:38 -05009223lpfc_sli_host_down(struct lpfc_vport *vport)
9224{
James Smart858c9f62007-06-17 19:56:39 -05009225 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009226 struct lpfc_hba *phba = vport->phba;
9227 struct lpfc_sli *psli = &phba->sli;
9228 struct lpfc_sli_ring *pring;
9229 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009230 int i;
9231 unsigned long flags = 0;
9232 uint16_t prev_pring_flag;
9233
9234 lpfc_cleanup_discovery_resources(vport);
9235
9236 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009237 for (i = 0; i < psli->num_rings; i++) {
9238 pring = &psli->ring[i];
9239 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009240 /* Only slow rings */
9241 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009242 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009243 /* Set the lpfc data pending flag */
9244 set_bit(LPFC_DATA_READY, &phba->data_flags);
9245 }
James Smart92d7f7b2007-06-17 19:56:38 -05009246 /*
9247 * Error everything on the txq since these iocbs have not been
9248 * given to the FW yet.
9249 */
James Smart92d7f7b2007-06-17 19:56:38 -05009250 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9251 if (iocb->vport != vport)
9252 continue;
James Smart858c9f62007-06-17 19:56:39 -05009253 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009254 }
9255
9256 /* Next issue ABTS for everything on the txcmplq */
9257 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9258 list) {
9259 if (iocb->vport != vport)
9260 continue;
9261 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9262 }
9263
9264 pring->flag = prev_pring_flag;
9265 }
9266
9267 spin_unlock_irqrestore(&phba->hbalock, flags);
9268
James Smarta257bf92009-04-06 18:48:10 -04009269 /* Cancel all the IOCBs from the completions list */
9270 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9271 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009272 return 1;
9273}
9274
James Smarte59058c2008-08-24 21:49:00 -04009275/**
James Smart3621a712009-04-06 18:47:14 -04009276 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009277 * @phba: Pointer to HBA context object.
9278 *
9279 * This function cleans up all iocb, buffers, mailbox commands
9280 * while shutting down the HBA. This function is called with no
9281 * lock held and always returns 1.
9282 * This function does the following to cleanup driver resources:
9283 * - Free discovery resources for each virtual port
9284 * - Cleanup any pending fabric iocbs
9285 * - Iterate through the iocb txq and free each entry
9286 * in the list.
9287 * - Free up any buffer posted to the HBA
9288 * - Free mailbox commands in the mailbox queue.
9289 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009290int
James Smart2e0fef82007-06-17 19:56:36 -05009291lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009292{
James Smart2534ba72007-04-25 09:52:20 -04009293 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009294 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009295 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009296 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009297 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009298 int i;
9299
9300 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009301 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009302
dea31012005-04-17 16:05:31 -05009303 lpfc_hba_down_prep(phba);
9304
James Smart92d7f7b2007-06-17 19:56:38 -05009305 lpfc_fabric_abort_hba(phba);
9306
James Smart2e0fef82007-06-17 19:56:36 -05009307 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009308 for (i = 0; i < psli->num_rings; i++) {
9309 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009310 /* Only slow rings */
9311 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009312 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009313 /* Set the lpfc data pending flag */
9314 set_bit(LPFC_DATA_READY, &phba->data_flags);
9315 }
dea31012005-04-17 16:05:31 -05009316
9317 /*
9318 * Error everything on the txq since these iocbs have not been
9319 * given to the FW yet.
9320 */
James Smart2534ba72007-04-25 09:52:20 -04009321 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009322 }
James Smart2e0fef82007-06-17 19:56:36 -05009323 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009324
James Smarta257bf92009-04-06 18:48:10 -04009325 /* Cancel all the IOCBs from the completions list */
9326 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9327 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009328
James Smart0ff10d42008-01-11 01:52:36 -05009329 spin_lock_irqsave(&phba->hbalock, flags);
9330 list_splice_init(&phba->elsbuf, &completions);
9331 phba->elsbuf_cnt = 0;
9332 phba->elsbuf_prev_cnt = 0;
9333 spin_unlock_irqrestore(&phba->hbalock, flags);
9334
9335 while (!list_empty(&completions)) {
9336 list_remove_head(&completions, buf_ptr,
9337 struct lpfc_dmabuf, list);
9338 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9339 kfree(buf_ptr);
9340 }
9341
dea31012005-04-17 16:05:31 -05009342 /* Return any active mbox cmds */
9343 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009344
James Smartda0436e2009-05-22 14:51:39 -04009345 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009346 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009347 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009348
James Smartda0436e2009-05-22 14:51:39 -04009349 return 1;
9350}
James Smart92d7f7b2007-06-17 19:56:38 -05009351
James Smartda0436e2009-05-22 14:51:39 -04009352/**
James Smart3621a712009-04-06 18:47:14 -04009353 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009354 * @srcp: Source memory pointer.
9355 * @destp: Destination memory pointer.
9356 * @cnt: Number of words required to be copied.
9357 *
9358 * This function is used for copying data between driver memory
9359 * and the SLI memory. This function also changes the endianness
9360 * of each word if native endianness is different from SLI
9361 * endianness. This function can be called with or without
9362 * lock.
9363 **/
dea31012005-04-17 16:05:31 -05009364void
9365lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9366{
9367 uint32_t *src = srcp;
9368 uint32_t *dest = destp;
9369 uint32_t ldata;
9370 int i;
9371
9372 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9373 ldata = *src;
9374 ldata = le32_to_cpu(ldata);
9375 *dest = ldata;
9376 src++;
9377 dest++;
9378 }
9379}
9380
James Smarte59058c2008-08-24 21:49:00 -04009381
9382/**
James Smarta0c87cb2009-07-19 10:01:10 -04009383 * lpfc_sli_bemem_bcopy - SLI memory copy function
9384 * @srcp: Source memory pointer.
9385 * @destp: Destination memory pointer.
9386 * @cnt: Number of words required to be copied.
9387 *
9388 * This function is used for copying data between a data structure
9389 * with big endian representation to local endianness.
9390 * This function can be called with or without lock.
9391 **/
9392void
9393lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9394{
9395 uint32_t *src = srcp;
9396 uint32_t *dest = destp;
9397 uint32_t ldata;
9398 int i;
9399
9400 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9401 ldata = *src;
9402 ldata = be32_to_cpu(ldata);
9403 *dest = ldata;
9404 src++;
9405 dest++;
9406 }
9407}
9408
9409/**
James Smart3621a712009-04-06 18:47:14 -04009410 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009411 * @phba: Pointer to HBA context object.
9412 * @pring: Pointer to driver SLI ring object.
9413 * @mp: Pointer to driver buffer object.
9414 *
9415 * This function is called with no lock held.
9416 * It always return zero after adding the buffer to the postbufq
9417 * buffer list.
9418 **/
dea31012005-04-17 16:05:31 -05009419int
James Smart2e0fef82007-06-17 19:56:36 -05009420lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9421 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009422{
9423 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9424 later */
James Smart2e0fef82007-06-17 19:56:36 -05009425 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009426 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009427 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009428 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009429 return 0;
9430}
9431
James Smarte59058c2008-08-24 21:49:00 -04009432/**
James Smart3621a712009-04-06 18:47:14 -04009433 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009434 * @phba: Pointer to HBA context object.
9435 *
9436 * When HBQ is enabled, buffers are searched based on tags. This function
9437 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9438 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9439 * does not conflict with tags of buffer posted for unsolicited events.
9440 * The function returns the allocated tag. The function is called with
9441 * no locks held.
9442 **/
James Smart76bb24e2007-10-27 13:38:00 -04009443uint32_t
9444lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9445{
9446 spin_lock_irq(&phba->hbalock);
9447 phba->buffer_tag_count++;
9448 /*
9449 * Always set the QUE_BUFTAG_BIT to distiguish between
9450 * a tag assigned by HBQ.
9451 */
9452 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9453 spin_unlock_irq(&phba->hbalock);
9454 return phba->buffer_tag_count;
9455}
9456
James Smarte59058c2008-08-24 21:49:00 -04009457/**
James Smart3621a712009-04-06 18:47:14 -04009458 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009459 * @phba: Pointer to HBA context object.
9460 * @pring: Pointer to driver SLI ring object.
9461 * @tag: Buffer tag.
9462 *
9463 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9464 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9465 * iocb is posted to the response ring with the tag of the buffer.
9466 * This function searches the pring->postbufq list using the tag
9467 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9468 * iocb. If the buffer is found then lpfc_dmabuf object of the
9469 * buffer is returned to the caller else NULL is returned.
9470 * This function is called with no lock held.
9471 **/
James Smart76bb24e2007-10-27 13:38:00 -04009472struct lpfc_dmabuf *
9473lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9474 uint32_t tag)
9475{
9476 struct lpfc_dmabuf *mp, *next_mp;
9477 struct list_head *slp = &pring->postbufq;
9478
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009479 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009480 spin_lock_irq(&phba->hbalock);
9481 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9482 if (mp->buffer_tag == tag) {
9483 list_del_init(&mp->list);
9484 pring->postbufq_cnt--;
9485 spin_unlock_irq(&phba->hbalock);
9486 return mp;
9487 }
9488 }
9489
9490 spin_unlock_irq(&phba->hbalock);
9491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009492 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009493 "ring %d Data x%lx x%p x%p x%x\n",
9494 pring->ringno, (unsigned long) tag,
9495 slp->next, slp->prev, pring->postbufq_cnt);
9496
9497 return NULL;
9498}
dea31012005-04-17 16:05:31 -05009499
James Smarte59058c2008-08-24 21:49:00 -04009500/**
James Smart3621a712009-04-06 18:47:14 -04009501 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009502 * @phba: Pointer to HBA context object.
9503 * @pring: Pointer to driver SLI ring object.
9504 * @phys: DMA address of the buffer.
9505 *
9506 * This function searches the buffer list using the dma_address
9507 * of unsolicited event to find the driver's lpfc_dmabuf object
9508 * corresponding to the dma_address. The function returns the
9509 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9510 * This function is called by the ct and els unsolicited event
9511 * handlers to get the buffer associated with the unsolicited
9512 * event.
9513 *
9514 * This function is called with no lock held.
9515 **/
dea31012005-04-17 16:05:31 -05009516struct lpfc_dmabuf *
9517lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9518 dma_addr_t phys)
9519{
9520 struct lpfc_dmabuf *mp, *next_mp;
9521 struct list_head *slp = &pring->postbufq;
9522
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009523 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009524 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009525 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9526 if (mp->phys == phys) {
9527 list_del_init(&mp->list);
9528 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009529 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009530 return mp;
9531 }
9532 }
9533
James Smart2e0fef82007-06-17 19:56:36 -05009534 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009536 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009537 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009538 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009539 slp->next, slp->prev, pring->postbufq_cnt);
9540 return NULL;
9541}
9542
James Smarte59058c2008-08-24 21:49:00 -04009543/**
James Smart3621a712009-04-06 18:47:14 -04009544 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009545 * @phba: Pointer to HBA context object.
9546 * @cmdiocb: Pointer to driver command iocb object.
9547 * @rspiocb: Pointer to driver response iocb object.
9548 *
9549 * This function is the completion handler for the abort iocbs for
9550 * ELS commands. This function is called from the ELS ring event
9551 * handler with no lock held. This function frees memory resources
9552 * associated with the abort iocb.
9553 **/
dea31012005-04-17 16:05:31 -05009554static void
James Smart2e0fef82007-06-17 19:56:36 -05009555lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9556 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009557{
James Smart2e0fef82007-06-17 19:56:36 -05009558 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009559 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009560 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009561
9562 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009563
9564 /*
9565 * Assume that the port already completed and returned, or
9566 * will return the iocb. Just Log the message.
9567 */
James Smart2680eea2007-04-25 09:52:55 -04009568 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9569 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9570
James Smart2e0fef82007-06-17 19:56:36 -05009571 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009572 if (phba->sli_rev < LPFC_SLI_REV4) {
9573 if (abort_iotag != 0 &&
9574 abort_iotag <= phba->sli.last_iotag)
9575 abort_iocb =
9576 phba->sli.iocbq_lookup[abort_iotag];
9577 } else
9578 /* For sli4 the abort_tag is the XRI,
9579 * so the abort routine puts the iotag of the iocb
9580 * being aborted in the context field of the abort
9581 * IOCB.
9582 */
9583 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009584
James Smart2a9bf3d2010-06-07 15:24:45 -04009585 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9586 "0327 Cannot abort els iocb %p "
9587 "with tag %x context %x, abort status %x, "
9588 "abort code %x\n",
9589 abort_iocb, abort_iotag, abort_context,
9590 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009591
James Smartff78d8f2011-12-13 13:21:35 -05009592 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009593 }
James Bottomley604a3e32005-10-29 10:28:33 -05009594 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009595 return;
9596}
9597
James Smarte59058c2008-08-24 21:49:00 -04009598/**
James Smart3621a712009-04-06 18:47:14 -04009599 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009600 * @phba: Pointer to HBA context object.
9601 * @cmdiocb: Pointer to driver command iocb object.
9602 * @rspiocb: Pointer to driver response iocb object.
9603 *
9604 * The function is called from SLI ring event handler with no
9605 * lock held. This function is the completion handler for ELS commands
9606 * which are aborted. The function frees memory resources used for
9607 * the aborted ELS commands.
9608 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009609static void
9610lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9611 struct lpfc_iocbq *rspiocb)
9612{
9613 IOCB_t *irsp = &rspiocb->iocb;
9614
9615 /* ELS cmd tag <ulpIoTag> completes */
9616 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009617 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009618 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009619 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009620 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009621 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9622 lpfc_ct_free_iocb(phba, cmdiocb);
9623 else
9624 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009625 return;
9626}
9627
James Smarte59058c2008-08-24 21:49:00 -04009628/**
James Smart5af5eee2010-10-22 11:06:38 -04009629 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009630 * @phba: Pointer to HBA context object.
9631 * @pring: Pointer to driver SLI ring object.
9632 * @cmdiocb: Pointer to driver command iocb object.
9633 *
James Smart5af5eee2010-10-22 11:06:38 -04009634 * This function issues an abort iocb for the provided command iocb down to
9635 * the port. Other than the case the outstanding command iocb is an abort
9636 * request, this function issues abort out unconditionally. This function is
9637 * called with hbalock held. The function returns 0 when it fails due to
9638 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009639 **/
James Smart5af5eee2010-10-22 11:06:38 -04009640static int
9641lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009642 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009643{
James Smart2e0fef82007-06-17 19:56:36 -05009644 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009645 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009646 IOCB_t *icmd = NULL;
9647 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009648 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009649 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009650
James Smart92d7f7b2007-06-17 19:56:38 -05009651 /*
9652 * There are certain command types we don't want to abort. And we
9653 * don't want to abort commands that are already in the process of
9654 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009655 */
9656 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009657 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009658 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9659 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009660 return 0;
9661
dea31012005-04-17 16:05:31 -05009662 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009663 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009664 if (abtsiocbp == NULL)
9665 return 0;
dea31012005-04-17 16:05:31 -05009666
James Smart07951072007-04-25 09:51:38 -04009667 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009668 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009669 */
9670 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9671
dea31012005-04-17 16:05:31 -05009672 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009673 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9674 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009675 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009676 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009677 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9678 }
James Smartda0436e2009-05-22 14:51:39 -04009679 else
9680 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009681 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009682 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009683
James Smart5ffc2662009-11-18 15:39:44 -05009684 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9685 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009686 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9687 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009688
James Smart2e0fef82007-06-17 19:56:36 -05009689 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009690 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9691 else
9692 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9693
9694 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009695
James Smarte8b62012007-08-02 11:10:09 -04009696 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9697 "0339 Abort xri x%x, original iotag x%x, "
9698 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009699 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009700 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009701 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009702
9703 if (phba->sli_rev == LPFC_SLI_REV4) {
9704 /* Note: both hbalock and ring_lock need to be set here */
9705 spin_lock_irqsave(&pring->ring_lock, iflags);
9706 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9707 abtsiocbp, 0);
9708 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9709 } else {
9710 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9711 abtsiocbp, 0);
9712 }
James Smart07951072007-04-25 09:51:38 -04009713
James Smartd7c255b2008-08-24 21:50:00 -04009714 if (retval)
9715 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009716
9717 /*
9718 * Caller to this routine should check for IOCB_ERROR
9719 * and handle it properly. This routine no longer removes
9720 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9721 */
9722 return retval;
9723}
9724
9725/**
9726 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9727 * @phba: Pointer to HBA context object.
9728 * @pring: Pointer to driver SLI ring object.
9729 * @cmdiocb: Pointer to driver command iocb object.
9730 *
9731 * This function issues an abort iocb for the provided command iocb. In case
9732 * of unloading, the abort iocb will not be issued to commands on the ELS
9733 * ring. Instead, the callback function shall be changed to those commands
9734 * so that nothing happens when them finishes. This function is called with
9735 * hbalock held. The function returns 0 when the command iocb is an abort
9736 * request.
9737 **/
9738int
9739lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9740 struct lpfc_iocbq *cmdiocb)
9741{
9742 struct lpfc_vport *vport = cmdiocb->vport;
9743 int retval = IOCB_ERROR;
9744 IOCB_t *icmd = NULL;
9745
9746 /*
9747 * There are certain command types we don't want to abort. And we
9748 * don't want to abort commands that are already in the process of
9749 * being aborted.
9750 */
9751 icmd = &cmdiocb->iocb;
9752 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9753 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9754 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9755 return 0;
9756
9757 /*
9758 * If we're unloading, don't abort iocb on the ELS ring, but change
9759 * the callback so that nothing happens when it finishes.
9760 */
9761 if ((vport->load_flag & FC_UNLOADING) &&
9762 (pring->ringno == LPFC_ELS_RING)) {
9763 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9764 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9765 else
9766 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9767 goto abort_iotag_exit;
9768 }
9769
9770 /* Now, we try to issue the abort to the cmdiocb out */
9771 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9772
James Smart07951072007-04-25 09:51:38 -04009773abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009774 /*
9775 * Caller to this routine should check for IOCB_ERROR
9776 * and handle it properly. This routine no longer removes
9777 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009778 */
James Smart2e0fef82007-06-17 19:56:36 -05009779 return retval;
dea31012005-04-17 16:05:31 -05009780}
9781
James Smarte59058c2008-08-24 21:49:00 -04009782/**
James Smart5af5eee2010-10-22 11:06:38 -04009783 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9784 * @phba: Pointer to HBA context object.
9785 * @pring: Pointer to driver SLI ring object.
9786 *
9787 * This function aborts all iocbs in the given ring and frees all the iocb
9788 * objects in txq. This function issues abort iocbs unconditionally for all
9789 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9790 * to complete before the return of this function. The caller is not required
9791 * to hold any locks.
9792 **/
9793static void
9794lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9795{
9796 LIST_HEAD(completions);
9797 struct lpfc_iocbq *iocb, *next_iocb;
9798
9799 if (pring->ringno == LPFC_ELS_RING)
9800 lpfc_fabric_abort_hba(phba);
9801
9802 spin_lock_irq(&phba->hbalock);
9803
9804 /* Take off all the iocbs on txq for cancelling */
9805 list_splice_init(&pring->txq, &completions);
9806 pring->txq_cnt = 0;
9807
9808 /* Next issue ABTS for everything on the txcmplq */
9809 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9810 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9811
9812 spin_unlock_irq(&phba->hbalock);
9813
9814 /* Cancel all the IOCBs from the completions list */
9815 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9816 IOERR_SLI_ABORTED);
9817}
9818
9819/**
9820 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9821 * @phba: pointer to lpfc HBA data structure.
9822 *
9823 * This routine will abort all pending and outstanding iocbs to an HBA.
9824 **/
9825void
9826lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9827{
9828 struct lpfc_sli *psli = &phba->sli;
9829 struct lpfc_sli_ring *pring;
9830 int i;
9831
9832 for (i = 0; i < psli->num_rings; i++) {
9833 pring = &psli->ring[i];
9834 lpfc_sli_iocb_ring_abort(phba, pring);
9835 }
9836}
9837
9838/**
James Smart3621a712009-04-06 18:47:14 -04009839 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009840 * @iocbq: Pointer to driver iocb object.
9841 * @vport: Pointer to driver virtual port object.
9842 * @tgt_id: SCSI ID of the target.
9843 * @lun_id: LUN ID of the scsi device.
9844 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9845 *
James Smart3621a712009-04-06 18:47:14 -04009846 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009847 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9848 * 0 if the filtering criteria is met for the given iocb and will return
9849 * 1 if the filtering criteria is not met.
9850 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9851 * given iocb is for the SCSI device specified by vport, tgt_id and
9852 * lun_id parameter.
9853 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9854 * given iocb is for the SCSI target specified by vport and tgt_id
9855 * parameters.
9856 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9857 * given iocb is for the SCSI host associated with the given vport.
9858 * This function is called with no locks held.
9859 **/
dea31012005-04-17 16:05:31 -05009860static int
James Smart51ef4c22007-08-02 11:10:31 -04009861lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9862 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009863 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009864{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009865 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009866 int rc = 1;
9867
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009868 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9869 return rc;
9870
James Smart51ef4c22007-08-02 11:10:31 -04009871 if (iocbq->vport != vport)
9872 return rc;
9873
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009874 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009875
James Smart495a7142008-06-14 22:52:59 -04009876 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009877 return rc;
9878
9879 switch (ctx_cmd) {
9880 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009881 if ((lpfc_cmd->rdata->pnode) &&
9882 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9883 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009884 rc = 0;
9885 break;
9886 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009887 if ((lpfc_cmd->rdata->pnode) &&
9888 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009889 rc = 0;
9890 break;
dea31012005-04-17 16:05:31 -05009891 case LPFC_CTX_HOST:
9892 rc = 0;
9893 break;
9894 default:
9895 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009896 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009897 break;
9898 }
9899
9900 return rc;
9901}
9902
James Smarte59058c2008-08-24 21:49:00 -04009903/**
James Smart3621a712009-04-06 18:47:14 -04009904 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009905 * @vport: Pointer to virtual port.
9906 * @tgt_id: SCSI ID of the target.
9907 * @lun_id: LUN ID of the scsi device.
9908 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9909 *
9910 * This function returns number of FCP commands pending for the vport.
9911 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9912 * commands pending on the vport associated with SCSI device specified
9913 * by tgt_id and lun_id parameters.
9914 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9915 * commands pending on the vport associated with SCSI target specified
9916 * by tgt_id parameter.
9917 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9918 * commands pending on the vport.
9919 * This function returns the number of iocbs which satisfy the filter.
9920 * This function is called without any lock held.
9921 **/
dea31012005-04-17 16:05:31 -05009922int
James Smart51ef4c22007-08-02 11:10:31 -04009923lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9924 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009925{
James Smart51ef4c22007-08-02 11:10:31 -04009926 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009927 struct lpfc_iocbq *iocbq;
9928 int sum, i;
dea31012005-04-17 16:05:31 -05009929
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009930 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9931 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009932
James Smart51ef4c22007-08-02 11:10:31 -04009933 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9934 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009935 sum++;
dea31012005-04-17 16:05:31 -05009936 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009937
dea31012005-04-17 16:05:31 -05009938 return sum;
9939}
9940
James Smarte59058c2008-08-24 21:49:00 -04009941/**
James Smart3621a712009-04-06 18:47:14 -04009942 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009943 * @phba: Pointer to HBA context object
9944 * @cmdiocb: Pointer to command iocb object.
9945 * @rspiocb: Pointer to response iocb object.
9946 *
9947 * This function is called when an aborted FCP iocb completes. This
9948 * function is called by the ring event handler with no lock held.
9949 * This function frees the iocb.
9950 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009951void
James Smart2e0fef82007-06-17 19:56:36 -05009952lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9953 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009954{
James Smartcb69f7d2011-12-13 13:21:57 -05009955 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -04009956 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -05009957 "original iotag x%x, abort cmd iotag x%x "
9958 "status 0x%x, reason 0x%x\n",
9959 cmdiocb->iocb.un.acxri.abortContextTag,
9960 cmdiocb->iocb.un.acxri.abortIoTag,
9961 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9962 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009963 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009964 return;
9965}
9966
James Smarte59058c2008-08-24 21:49:00 -04009967/**
James Smart3621a712009-04-06 18:47:14 -04009968 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009969 * @vport: Pointer to virtual port.
9970 * @pring: Pointer to driver SLI ring object.
9971 * @tgt_id: SCSI ID of the target.
9972 * @lun_id: LUN ID of the scsi device.
9973 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9974 *
9975 * This function sends an abort command for every SCSI command
9976 * associated with the given virtual port pending on the ring
9977 * filtered by lpfc_sli_validate_fcp_iocb function.
9978 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9979 * FCP iocbs associated with lun specified by tgt_id and lun_id
9980 * parameters
9981 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9982 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9983 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9984 * FCP iocbs associated with virtual port.
9985 * This function returns number of iocbs it failed to abort.
9986 * This function is called with no locks held.
9987 **/
dea31012005-04-17 16:05:31 -05009988int
James Smart51ef4c22007-08-02 11:10:31 -04009989lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9990 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009991{
James Smart51ef4c22007-08-02 11:10:31 -04009992 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009993 struct lpfc_iocbq *iocbq;
9994 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009995 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009996 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009997 int i;
dea31012005-04-17 16:05:31 -05009998
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009999 for (i = 1; i <= phba->sli.last_iotag; i++) {
10000 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010001
James Smart51ef4c22007-08-02 11:10:31 -040010002 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010003 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010004 continue;
10005
James Smartafbd8d82013-09-06 12:22:13 -040010006 /*
10007 * If the iocbq is already being aborted, don't take a second
10008 * action, but do count it.
10009 */
10010 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10011 continue;
10012
dea31012005-04-17 16:05:31 -050010013 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010014 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010015 if (abtsiocb == NULL) {
10016 errcnt++;
10017 continue;
10018 }
dea31012005-04-17 16:05:31 -050010019
James Smartafbd8d82013-09-06 12:22:13 -040010020 /* indicate the IO is being aborted by the driver. */
10021 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10022
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010023 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010024 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10025 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010026 if (phba->sli_rev == LPFC_SLI_REV4)
10027 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10028 else
10029 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010030 abtsiocb->iocb.ulpLe = 1;
10031 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010032 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010033
James Smart5ffc2662009-11-18 15:39:44 -050010034 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10035 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010036 if (iocbq->iocb_flag & LPFC_IO_FCP)
10037 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -050010038
James Smart2e0fef82007-06-17 19:56:36 -050010039 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010040 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10041 else
10042 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10043
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010044 /* Setup callback routine and issue the command. */
10045 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010046 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10047 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010048 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010049 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010050 errcnt++;
10051 continue;
10052 }
10053 }
10054
10055 return errcnt;
10056}
10057
James Smarte59058c2008-08-24 21:49:00 -040010058/**
James Smart3621a712009-04-06 18:47:14 -040010059 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010060 * @phba: Pointer to HBA context object.
10061 * @cmdiocbq: Pointer to command iocb.
10062 * @rspiocbq: Pointer to response iocb.
10063 *
10064 * This function is the completion handler for iocbs issued using
10065 * lpfc_sli_issue_iocb_wait function. This function is called by the
10066 * ring event handler function without any lock held. This function
10067 * can be called from both worker thread context and interrupt
10068 * context. This function also can be called from other thread which
10069 * cleans up the SLI layer objects.
10070 * This function copy the contents of the response iocb to the
10071 * response iocb memory object provided by the caller of
10072 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10073 * sleeps for the iocb completion.
10074 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010075static void
10076lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10077 struct lpfc_iocbq *cmdiocbq,
10078 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010079{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010080 wait_queue_head_t *pdone_q;
10081 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010082 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010083
James Smart2e0fef82007-06-17 19:56:36 -050010084 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010085 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10086
10087 /*
10088 * A time out has occurred for the iocb. If a time out
10089 * completion handler has been supplied, call it. Otherwise,
10090 * just free the iocbq.
10091 */
10092
10093 spin_unlock_irqrestore(&phba->hbalock, iflags);
10094 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10095 cmdiocbq->wait_iocb_cmpl = NULL;
10096 if (cmdiocbq->iocb_cmpl)
10097 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10098 else
10099 lpfc_sli_release_iocbq(phba, cmdiocbq);
10100 return;
10101 }
10102
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010103 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10104 if (cmdiocbq->context2 && rspiocbq)
10105 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10106 &rspiocbq->iocb, sizeof(IOCB_t));
10107
James Smart0f65ff62010-02-26 14:14:23 -050010108 /* Set the exchange busy flag for task management commands */
10109 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10110 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10111 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10112 cur_iocbq);
10113 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10114 }
10115
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010116 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010117 if (pdone_q)
10118 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010119 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010120 return;
10121}
10122
James Smarte59058c2008-08-24 21:49:00 -040010123/**
James Smartd11e31d2009-06-10 17:23:06 -040010124 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10125 * @phba: Pointer to HBA context object..
10126 * @piocbq: Pointer to command iocb.
10127 * @flag: Flag to test.
10128 *
10129 * This routine grabs the hbalock and then test the iocb_flag to
10130 * see if the passed in flag is set.
10131 * Returns:
10132 * 1 if flag is set.
10133 * 0 if flag is not set.
10134 **/
10135static int
10136lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10137 struct lpfc_iocbq *piocbq, uint32_t flag)
10138{
10139 unsigned long iflags;
10140 int ret;
10141
10142 spin_lock_irqsave(&phba->hbalock, iflags);
10143 ret = piocbq->iocb_flag & flag;
10144 spin_unlock_irqrestore(&phba->hbalock, iflags);
10145 return ret;
10146
10147}
10148
10149/**
James Smart3621a712009-04-06 18:47:14 -040010150 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010151 * @phba: Pointer to HBA context object..
10152 * @pring: Pointer to sli ring.
10153 * @piocb: Pointer to command iocb.
10154 * @prspiocbq: Pointer to response iocb.
10155 * @timeout: Timeout in number of seconds.
10156 *
10157 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010158 * iocb to complete. The iocb_cmpl field of the shall be used
10159 * to handle iocbs which time out. If the field is NULL, the
10160 * function shall free the iocbq structure. If more clean up is
10161 * needed, the caller is expected to provide a completion function
10162 * that will provide the needed clean up. If the iocb command is
10163 * not completed within timeout seconds, the function will either
10164 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10165 * completion function set in the iocb_cmpl field and then return
10166 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10167 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010168 * The function waits for the iocb completion using an
10169 * non-interruptible wait.
10170 * This function will sleep while waiting for iocb completion.
10171 * So, this function should not be called from any context which
10172 * does not allow sleeping. Due to the same reason, this function
10173 * cannot be called with interrupt disabled.
10174 * This function assumes that the iocb completions occur while
10175 * this function sleep. So, this function cannot be called from
10176 * the thread which process iocb completion for this ring.
10177 * This function clears the iocb_flag of the iocb object before
10178 * issuing the iocb and the iocb completion handler sets this
10179 * flag and wakes this thread when the iocb completes.
10180 * The contents of the response iocb will be copied to prspiocbq
10181 * by the completion handler when the command completes.
10182 * This function returns IOCB_SUCCESS when success.
10183 * This function is called with no lock held.
10184 **/
dea31012005-04-17 16:05:31 -050010185int
James Smart2e0fef82007-06-17 19:56:36 -050010186lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010187 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010188 struct lpfc_iocbq *piocb,
10189 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010190 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010191{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010192 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010193 long timeleft, timeout_req = 0;
10194 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010195 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010196 struct lpfc_iocbq *iocb;
10197 int txq_cnt = 0;
10198 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010199 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010200 unsigned long iflags;
10201 bool iocb_completed = true;
10202
dea31012005-04-17 16:05:31 -050010203 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010204 * If the caller has provided a response iocbq buffer, then context2
10205 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010206 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010207 if (prspiocbq) {
10208 if (piocb->context2)
10209 return IOCB_ERROR;
10210 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010211 }
10212
James Smart5a0916b2013-07-15 18:31:42 -040010213 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010214 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10215 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010216 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010217
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010218 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010219 if (lpfc_readl(phba->HCregaddr, &creg_val))
10220 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010221 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10222 writel(creg_val, phba->HCregaddr);
10223 readl(phba->HCregaddr); /* flush */
10224 }
10225
James Smart2a9bf3d2010-06-07 15:24:45 -040010226 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10227 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010228 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010229 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010230 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010231 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010232 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010233 spin_lock_irqsave(&phba->hbalock, iflags);
10234 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010235
James Smart5a0916b2013-07-15 18:31:42 -040010236 /*
10237 * IOCB timed out. Inform the wake iocb wait
10238 * completion function and set local status
10239 */
10240
10241 iocb_completed = false;
10242 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10243 }
10244 spin_unlock_irqrestore(&phba->hbalock, iflags);
10245 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010246 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010247 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010248 /* Note: we are not indicating if the IOCB has a success
10249 * status or not - that's for the caller to check.
10250 * IOCB_SUCCESS means just that the command was sent and
10251 * completed. Not that it completed successfully.
10252 * */
James Smart7054a602007-04-25 09:52:34 -040010253 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010255 "0338 IOCB wait timeout error - no "
10256 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010257 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010258 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010260 "0330 IOCB wake NOT set, "
10261 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010262 timeout, (timeleft / jiffies));
10263 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010264 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010265 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010266 if (phba->cfg_log_verbose & LOG_SLI) {
10267 list_for_each_entry(iocb, &pring->txq, list) {
10268 txq_cnt++;
10269 }
10270 list_for_each_entry(iocb, &pring->txcmplq, list) {
10271 txcmplq_cnt++;
10272 }
10273 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10274 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10275 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10276 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010277 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010278 } else {
10279 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010280 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010281 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010282 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010283 }
10284
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010285 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010286 if (lpfc_readl(phba->HCregaddr, &creg_val))
10287 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010288 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10289 writel(creg_val, phba->HCregaddr);
10290 readl(phba->HCregaddr); /* flush */
10291 }
10292
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010293 if (prspiocbq)
10294 piocb->context2 = NULL;
10295
10296 piocb->context_un.wait_queue = NULL;
10297 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010298 return retval;
10299}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010300
James Smarte59058c2008-08-24 21:49:00 -040010301/**
James Smart3621a712009-04-06 18:47:14 -040010302 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010303 * @phba: Pointer to HBA context object.
10304 * @pmboxq: Pointer to driver mailbox object.
10305 * @timeout: Timeout in number of seconds.
10306 *
10307 * This function issues the mailbox to firmware and waits for the
10308 * mailbox command to complete. If the mailbox command is not
10309 * completed within timeout seconds, it returns MBX_TIMEOUT.
10310 * The function waits for the mailbox completion using an
10311 * interruptible wait. If the thread is woken up due to a
10312 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10313 * should not free the mailbox resources, if this function returns
10314 * MBX_TIMEOUT.
10315 * This function will sleep while waiting for mailbox completion.
10316 * So, this function should not be called from any context which
10317 * does not allow sleeping. Due to the same reason, this function
10318 * cannot be called with interrupt disabled.
10319 * This function assumes that the mailbox completion occurs while
10320 * this function sleep. So, this function cannot be called from
10321 * the worker thread which processes mailbox completion.
10322 * This function is called in the context of HBA management
10323 * applications.
10324 * This function returns MBX_SUCCESS when successful.
10325 * This function is called with no lock held.
10326 **/
dea31012005-04-17 16:05:31 -050010327int
James Smart2e0fef82007-06-17 19:56:36 -050010328lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010329 uint32_t timeout)
10330{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010331 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a22013-05-31 17:05:27 -040010332 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010333 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010334 unsigned long flag;
dea31012005-04-17 16:05:31 -050010335
James Smartb230b8a22013-05-31 17:05:27 -040010336 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010337 if (pmboxq->context1)
James Smartb230b8a22013-05-31 17:05:27 -040010338 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010339
James Smart495a7142008-06-14 22:52:59 -040010340 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010341 /* setup wake call as IOCB callback */
10342 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10343 /* setup context field to pass wait_queue pointer to wake function */
10344 pmboxq->context1 = &done_q;
10345
dea31012005-04-17 16:05:31 -050010346 /* now issue the command */
10347 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010348 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010349 wait_event_interruptible_timeout(done_q,
10350 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010351 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010352
James Smart858c9f62007-06-17 19:56:39 -050010353 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040010354 /* restore the possible extended buffer for free resource */
10355 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010356 /*
10357 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10358 * else do not free the resources.
10359 */
James Smartd7c47992010-06-08 18:31:54 -040010360 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010361 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010362 } else {
James Smart7054a602007-04-25 09:52:34 -040010363 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010364 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10365 }
10366 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040010367 } else {
10368 /* restore the possible extended buffer for free resource */
10369 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010370 }
10371
dea31012005-04-17 16:05:31 -050010372 return retval;
10373}
10374
James Smarte59058c2008-08-24 21:49:00 -040010375/**
James Smart3772a992009-05-22 14:50:54 -040010376 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010377 * @phba: Pointer to HBA context.
10378 *
James Smart3772a992009-05-22 14:50:54 -040010379 * This function is called to shutdown the driver's mailbox sub-system.
10380 * It first marks the mailbox sub-system is in a block state to prevent
10381 * the asynchronous mailbox command from issued off the pending mailbox
10382 * command queue. If the mailbox command sub-system shutdown is due to
10383 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10384 * the mailbox sub-system flush routine to forcefully bring down the
10385 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10386 * as with offline or HBA function reset), this routine will wait for the
10387 * outstanding mailbox command to complete before invoking the mailbox
10388 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010389 **/
James Smart3772a992009-05-22 14:50:54 -040010390void
James Smart618a5232012-06-12 13:54:36 -040010391lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010392{
James Smart3772a992009-05-22 14:50:54 -040010393 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010394 unsigned long timeout;
10395
James Smart618a5232012-06-12 13:54:36 -040010396 if (mbx_action == LPFC_MBX_NO_WAIT) {
10397 /* delay 100ms for port state */
10398 msleep(100);
10399 lpfc_sli_mbox_sys_flush(phba);
10400 return;
10401 }
James Smarta183a152011-10-10 21:32:43 -040010402 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010403
James Smart3772a992009-05-22 14:50:54 -040010404 spin_lock_irq(&phba->hbalock);
10405 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010406
10407 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010408 /* Determine how long we might wait for the active mailbox
10409 * command to be gracefully completed by firmware.
10410 */
James Smarta183a152011-10-10 21:32:43 -040010411 if (phba->sli.mbox_active)
10412 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10413 phba->sli.mbox_active) *
10414 1000) + jiffies;
10415 spin_unlock_irq(&phba->hbalock);
10416
James Smart3772a992009-05-22 14:50:54 -040010417 while (phba->sli.mbox_active) {
10418 /* Check active mailbox complete status every 2ms */
10419 msleep(2);
10420 if (time_after(jiffies, timeout))
10421 /* Timeout, let the mailbox flush routine to
10422 * forcefully release active mailbox command
10423 */
10424 break;
10425 }
James Smartd7069f02012-03-01 22:36:29 -050010426 } else
10427 spin_unlock_irq(&phba->hbalock);
10428
James Smart3772a992009-05-22 14:50:54 -040010429 lpfc_sli_mbox_sys_flush(phba);
10430}
10431
10432/**
10433 * lpfc_sli_eratt_read - read sli-3 error attention events
10434 * @phba: Pointer to HBA context.
10435 *
10436 * This function is called to read the SLI3 device error attention registers
10437 * for possible error attention events. The caller must hold the hostlock
10438 * with spin_lock_irq().
10439 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010440 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010441 * Register and returns 0 otherwise.
10442 **/
10443static int
10444lpfc_sli_eratt_read(struct lpfc_hba *phba)
10445{
James Smarted957682007-06-17 19:56:37 -050010446 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010447
James Smart3772a992009-05-22 14:50:54 -040010448 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010449 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10450 goto unplug_err;
10451
James Smart3772a992009-05-22 14:50:54 -040010452 if (ha_copy & HA_ERATT) {
10453 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010454 if (lpfc_sli_read_hs(phba))
10455 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010456
James Smart3772a992009-05-22 14:50:54 -040010457 /* Check if there is a deferred error condition is active */
10458 if ((HS_FFER1 & phba->work_hs) &&
10459 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010460 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010461 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010462 /* Clear all interrupt enable conditions */
10463 writel(0, phba->HCregaddr);
10464 readl(phba->HCregaddr);
10465 }
10466
10467 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010468 phba->work_ha |= HA_ERATT;
10469 /* Indicate polling handles this ERATT */
10470 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010471 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010472 }
James Smart3772a992009-05-22 14:50:54 -040010473 return 0;
James Smart9940b972011-03-11 16:06:12 -050010474
10475unplug_err:
10476 /* Set the driver HS work bitmap */
10477 phba->work_hs |= UNPLUG_ERR;
10478 /* Set the driver HA work bitmap */
10479 phba->work_ha |= HA_ERATT;
10480 /* Indicate polling handles this ERATT */
10481 phba->hba_flag |= HBA_ERATT_HANDLED;
10482 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010483}
10484
James Smarte59058c2008-08-24 21:49:00 -040010485/**
James Smartda0436e2009-05-22 14:51:39 -040010486 * lpfc_sli4_eratt_read - read sli-4 error attention events
10487 * @phba: Pointer to HBA context.
10488 *
10489 * This function is called to read the SLI4 device error attention registers
10490 * for possible error attention events. The caller must hold the hostlock
10491 * with spin_lock_irq().
10492 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010493 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010494 * Register and returns 0 otherwise.
10495 **/
10496static int
10497lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10498{
10499 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010500 uint32_t if_type, portsmphr;
10501 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010502
James Smart2fcee4b2010-12-15 17:57:46 -050010503 /*
10504 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010505 * registers for error attention. This can be changed later.
10506 */
James Smart2fcee4b2010-12-15 17:57:46 -050010507 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10508 switch (if_type) {
10509 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010510 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10511 &uerr_sta_lo) ||
10512 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10513 &uerr_sta_hi)) {
10514 phba->work_hs |= UNPLUG_ERR;
10515 phba->work_ha |= HA_ERATT;
10516 phba->hba_flag |= HBA_ERATT_HANDLED;
10517 return 1;
10518 }
James Smart2fcee4b2010-12-15 17:57:46 -050010519 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10520 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10522 "1423 HBA Unrecoverable error: "
10523 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10524 "ue_mask_lo_reg=0x%x, "
10525 "ue_mask_hi_reg=0x%x\n",
10526 uerr_sta_lo, uerr_sta_hi,
10527 phba->sli4_hba.ue_mask_lo,
10528 phba->sli4_hba.ue_mask_hi);
10529 phba->work_status[0] = uerr_sta_lo;
10530 phba->work_status[1] = uerr_sta_hi;
10531 phba->work_ha |= HA_ERATT;
10532 phba->hba_flag |= HBA_ERATT_HANDLED;
10533 return 1;
10534 }
10535 break;
10536 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010537 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10538 &portstat_reg.word0) ||
10539 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10540 &portsmphr)){
10541 phba->work_hs |= UNPLUG_ERR;
10542 phba->work_ha |= HA_ERATT;
10543 phba->hba_flag |= HBA_ERATT_HANDLED;
10544 return 1;
10545 }
James Smart2fcee4b2010-12-15 17:57:46 -050010546 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10547 phba->work_status[0] =
10548 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10549 phba->work_status[1] =
10550 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010552 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010553 "port status reg 0x%x, "
10554 "port smphr reg 0x%x, "
10555 "error 1=0x%x, error 2=0x%x\n",
10556 portstat_reg.word0,
10557 portsmphr,
10558 phba->work_status[0],
10559 phba->work_status[1]);
10560 phba->work_ha |= HA_ERATT;
10561 phba->hba_flag |= HBA_ERATT_HANDLED;
10562 return 1;
10563 }
10564 break;
10565 case LPFC_SLI_INTF_IF_TYPE_1:
10566 default:
James Smarta747c9c2009-11-18 15:41:10 -050010567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010568 "2886 HBA Error Attention on unsupported "
10569 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010570 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010571 }
James Smart2fcee4b2010-12-15 17:57:46 -050010572
James Smartda0436e2009-05-22 14:51:39 -040010573 return 0;
10574}
10575
10576/**
James Smart3621a712009-04-06 18:47:14 -040010577 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010578 * @phba: Pointer to HBA context.
10579 *
James Smart3772a992009-05-22 14:50:54 -040010580 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010581 * error attention register bit for error attention events.
10582 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010583 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010584 * Register and returns 0 otherwise.
10585 **/
10586int
10587lpfc_sli_check_eratt(struct lpfc_hba *phba)
10588{
10589 uint32_t ha_copy;
10590
10591 /* If somebody is waiting to handle an eratt, don't process it
10592 * here. The brdkill function will do this.
10593 */
10594 if (phba->link_flag & LS_IGNORE_ERATT)
10595 return 0;
10596
10597 /* Check if interrupt handler handles this ERATT */
10598 spin_lock_irq(&phba->hbalock);
10599 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10600 /* Interrupt handler has handled ERATT */
10601 spin_unlock_irq(&phba->hbalock);
10602 return 0;
10603 }
10604
James Smarta257bf92009-04-06 18:48:10 -040010605 /*
10606 * If there is deferred error attention, do not check for error
10607 * attention
10608 */
10609 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10610 spin_unlock_irq(&phba->hbalock);
10611 return 0;
10612 }
10613
James Smart3772a992009-05-22 14:50:54 -040010614 /* If PCI channel is offline, don't process it */
10615 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010616 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010617 return 0;
10618 }
10619
10620 switch (phba->sli_rev) {
10621 case LPFC_SLI_REV2:
10622 case LPFC_SLI_REV3:
10623 /* Read chip Host Attention (HA) register */
10624 ha_copy = lpfc_sli_eratt_read(phba);
10625 break;
James Smartda0436e2009-05-22 14:51:39 -040010626 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010627 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010628 ha_copy = lpfc_sli4_eratt_read(phba);
10629 break;
James Smart3772a992009-05-22 14:50:54 -040010630 default:
10631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10632 "0299 Invalid SLI revision (%d)\n",
10633 phba->sli_rev);
10634 ha_copy = 0;
10635 break;
James Smart93996272008-08-24 21:50:30 -040010636 }
10637 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010638
10639 return ha_copy;
10640}
10641
10642/**
10643 * lpfc_intr_state_check - Check device state for interrupt handling
10644 * @phba: Pointer to HBA context.
10645 *
10646 * This inline routine checks whether a device or its PCI slot is in a state
10647 * that the interrupt should be handled.
10648 *
10649 * This function returns 0 if the device or the PCI slot is in a state that
10650 * interrupt should be handled, otherwise -EIO.
10651 */
10652static inline int
10653lpfc_intr_state_check(struct lpfc_hba *phba)
10654{
10655 /* If the pci channel is offline, ignore all the interrupts */
10656 if (unlikely(pci_channel_offline(phba->pcidev)))
10657 return -EIO;
10658
10659 /* Update device level interrupt statistics */
10660 phba->sli.slistat.sli_intr++;
10661
10662 /* Ignore all interrupts during initialization. */
10663 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10664 return -EIO;
10665
James Smart93996272008-08-24 21:50:30 -040010666 return 0;
10667}
10668
10669/**
James Smart3772a992009-05-22 14:50:54 -040010670 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010671 * @irq: Interrupt number.
10672 * @dev_id: The device context pointer.
10673 *
James Smart93996272008-08-24 21:50:30 -040010674 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010675 * service routine when device with SLI-3 interface spec is enabled with
10676 * MSI-X multi-message interrupt mode and there are slow-path events in
10677 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10678 * interrupt mode, this function is called as part of the device-level
10679 * interrupt handler. When the PCI slot is in error recovery or the HBA
10680 * is undergoing initialization, the interrupt handler will not process
10681 * the interrupt. The link attention and ELS ring attention events are
10682 * handled by the worker thread. The interrupt handler signals the worker
10683 * thread and returns for these events. This function is called without
10684 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010685 * structures.
10686 *
10687 * This function returns IRQ_HANDLED when interrupt is handled else it
10688 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010689 **/
dea31012005-04-17 16:05:31 -050010690irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010691lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010692{
James Smart2e0fef82007-06-17 19:56:36 -050010693 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010694 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010695 uint32_t work_ha_copy;
10696 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010697 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010698 uint32_t control;
10699
James Smart92d7f7b2007-06-17 19:56:38 -050010700 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010701 struct lpfc_vport *vport;
10702 struct lpfc_nodelist *ndlp;
10703 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010704 LPFC_MBOXQ_t *pmb;
10705 int rc;
10706
dea31012005-04-17 16:05:31 -050010707 /*
10708 * Get the driver's phba structure from the dev_id and
10709 * assume the HBA is not interrupting.
10710 */
James Smart93996272008-08-24 21:50:30 -040010711 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010712
10713 if (unlikely(!phba))
10714 return IRQ_NONE;
10715
dea31012005-04-17 16:05:31 -050010716 /*
James Smart93996272008-08-24 21:50:30 -040010717 * Stuff needs to be attented to when this function is invoked as an
10718 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010719 */
James Smart93996272008-08-24 21:50:30 -040010720 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010721 /* Check device state for handling interrupt */
10722 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010723 return IRQ_NONE;
10724 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010725 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010726 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10727 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010728 /* If somebody is waiting to handle an eratt don't process it
10729 * here. The brdkill function will do this.
10730 */
10731 if (phba->link_flag & LS_IGNORE_ERATT)
10732 ha_copy &= ~HA_ERATT;
10733 /* Check the need for handling ERATT in interrupt handler */
10734 if (ha_copy & HA_ERATT) {
10735 if (phba->hba_flag & HBA_ERATT_HANDLED)
10736 /* ERATT polling has handled ERATT */
10737 ha_copy &= ~HA_ERATT;
10738 else
10739 /* Indicate interrupt handler handles ERATT */
10740 phba->hba_flag |= HBA_ERATT_HANDLED;
10741 }
James Smarta257bf92009-04-06 18:48:10 -040010742
10743 /*
10744 * If there is deferred error attention, do not check for any
10745 * interrupt.
10746 */
10747 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010748 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010749 return IRQ_NONE;
10750 }
10751
James Smart93996272008-08-24 21:50:30 -040010752 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010753 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10754 goto unplug_error;
10755
James Smarta747c9c2009-11-18 15:41:10 -050010756 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10757 HC_LAINT_ENA | HC_ERINT_ENA),
10758 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010759 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10760 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010761 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010762 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010763 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010764 } else
10765 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010766
dea31012005-04-17 16:05:31 -050010767 work_ha_copy = ha_copy & phba->work_ha_mask;
10768
James Smart93996272008-08-24 21:50:30 -040010769 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010770 if (work_ha_copy & HA_LATT) {
10771 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10772 /*
10773 * Turn off Link Attention interrupts
10774 * until CLEAR_LA done
10775 */
James Smart5b75da22008-12-04 22:39:35 -050010776 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010777 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010778 if (lpfc_readl(phba->HCregaddr, &control))
10779 goto unplug_error;
dea31012005-04-17 16:05:31 -050010780 control &= ~HC_LAINT_ENA;
10781 writel(control, phba->HCregaddr);
10782 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010783 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010784 }
10785 else
10786 work_ha_copy &= ~HA_LATT;
10787 }
10788
James Smart93996272008-08-24 21:50:30 -040010789 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010790 /*
10791 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10792 * the only slow ring.
10793 */
10794 status = (work_ha_copy &
10795 (HA_RXMASK << (4*LPFC_ELS_RING)));
10796 status >>= (4*LPFC_ELS_RING);
10797 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010798 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010799 if (lpfc_readl(phba->HCregaddr, &control))
10800 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010801
10802 lpfc_debugfs_slow_ring_trc(phba,
10803 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10804 control, status,
10805 (uint32_t)phba->sli.slistat.sli_intr);
10806
James Smart858c9f62007-06-17 19:56:39 -050010807 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010808 lpfc_debugfs_slow_ring_trc(phba,
10809 "ISR Disable ring:"
10810 "pwork:x%x hawork:x%x wait:x%x",
10811 phba->work_ha, work_ha_copy,
10812 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010813 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010814
James Smart858c9f62007-06-17 19:56:39 -050010815 control &=
10816 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010817 writel(control, phba->HCregaddr);
10818 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010819 }
James Smarta58cbd52007-08-02 11:09:43 -040010820 else {
10821 lpfc_debugfs_slow_ring_trc(phba,
10822 "ISR slow ring: pwork:"
10823 "x%x hawork:x%x wait:x%x",
10824 phba->work_ha, work_ha_copy,
10825 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010826 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010827 }
James Smart5b75da22008-12-04 22:39:35 -050010828 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010829 }
10830 }
James Smart5b75da22008-12-04 22:39:35 -050010831 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010832 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010833 if (lpfc_sli_read_hs(phba))
10834 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010835 /*
10836 * Check if there is a deferred error condition
10837 * is active
10838 */
10839 if ((HS_FFER1 & phba->work_hs) &&
10840 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010841 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10842 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010843 phba->hba_flag |= DEFER_ERATT;
10844 /* Clear all interrupt enable conditions */
10845 writel(0, phba->HCregaddr);
10846 readl(phba->HCregaddr);
10847 }
10848 }
10849
James Smart93996272008-08-24 21:50:30 -040010850 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010851 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010852 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010853 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010854 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010855
10856 /* First check out the status word */
10857 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10858 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010859 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010860 /*
10861 * Stray Mailbox Interrupt, mbxCommand <cmd>
10862 * mbxStatus <status>
10863 */
James Smart09372822008-01-11 01:52:54 -050010864 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010865 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010866 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010867 "Interrupt mbxCommand x%x "
10868 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010869 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010870 pmbox->mbxCommand,
10871 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010872 /* clear mailbox attention bit */
10873 work_ha_copy &= ~HA_MBATT;
10874 } else {
James Smart97eab632008-04-07 10:16:05 -040010875 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010876 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010877 phba->last_completion_time = jiffies;
10878 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010879 if (pmb->mbox_cmpl) {
10880 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10881 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010882 if (pmb->out_ext_byte_len &&
10883 pmb->context2)
10884 lpfc_sli_pcimem_bcopy(
10885 phba->mbox_ext,
10886 pmb->context2,
10887 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010888 }
James Smart09372822008-01-11 01:52:54 -050010889 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10890 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10891
10892 lpfc_debugfs_disc_trc(vport,
10893 LPFC_DISC_TRC_MBOX_VPORT,
10894 "MBOX dflt rpi: : "
10895 "status:x%x rpi:x%x",
10896 (uint32_t)pmbox->mbxStatus,
10897 pmbox->un.varWords[0], 0);
10898
10899 if (!pmbox->mbxStatus) {
10900 mp = (struct lpfc_dmabuf *)
10901 (pmb->context1);
10902 ndlp = (struct lpfc_nodelist *)
10903 pmb->context2;
10904
10905 /* Reg_LOGIN of dflt RPI was
10906 * successful. new lets get
10907 * rid of the RPI using the
10908 * same mbox buffer.
10909 */
10910 lpfc_unreg_login(phba,
10911 vport->vpi,
10912 pmbox->un.varWords[0],
10913 pmb);
10914 pmb->mbox_cmpl =
10915 lpfc_mbx_cmpl_dflt_rpi;
10916 pmb->context1 = mp;
10917 pmb->context2 = ndlp;
10918 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010919 rc = lpfc_sli_issue_mbox(phba,
10920 pmb,
10921 MBX_NOWAIT);
10922 if (rc != MBX_BUSY)
10923 lpfc_printf_log(phba,
10924 KERN_ERR,
10925 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010926 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010927 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010928 if (rc != MBX_NOT_FINISHED)
10929 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010930 }
10931 }
James Smart5b75da22008-12-04 22:39:35 -050010932 spin_lock_irqsave(
10933 &phba->pport->work_port_lock,
10934 iflag);
James Smart09372822008-01-11 01:52:54 -050010935 phba->pport->work_port_events &=
10936 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010937 spin_unlock_irqrestore(
10938 &phba->pport->work_port_lock,
10939 iflag);
James Smart09372822008-01-11 01:52:54 -050010940 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010941 }
James Smart97eab632008-04-07 10:16:05 -040010942 } else
James Smart5b75da22008-12-04 22:39:35 -050010943 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010944
James Smart92d7f7b2007-06-17 19:56:38 -050010945 if ((work_ha_copy & HA_MBATT) &&
10946 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010947send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010948 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010949 do {
10950 rc = lpfc_sli_issue_mbox(phba, NULL,
10951 MBX_NOWAIT);
10952 } while (rc == MBX_NOT_FINISHED);
10953 if (rc != MBX_SUCCESS)
10954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10955 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010956 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010957 }
10958
James Smart5b75da22008-12-04 22:39:35 -050010959 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010960 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010961 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010962 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010963 }
James Smart93996272008-08-24 21:50:30 -040010964 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010965unplug_error:
10966 spin_unlock_irqrestore(&phba->hbalock, iflag);
10967 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010968
James Smart3772a992009-05-22 14:50:54 -040010969} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010970
10971/**
James Smart3772a992009-05-22 14:50:54 -040010972 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010973 * @irq: Interrupt number.
10974 * @dev_id: The device context pointer.
10975 *
10976 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010977 * service routine when device with SLI-3 interface spec is enabled with
10978 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10979 * ring event in the HBA. However, when the device is enabled with either
10980 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10981 * device-level interrupt handler. When the PCI slot is in error recovery
10982 * or the HBA is undergoing initialization, the interrupt handler will not
10983 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10984 * the intrrupt context. This function is called without any lock held.
10985 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010986 *
10987 * This function returns IRQ_HANDLED when interrupt is handled else it
10988 * returns IRQ_NONE.
10989 **/
10990irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010991lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010992{
10993 struct lpfc_hba *phba;
10994 uint32_t ha_copy;
10995 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010996 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010997
10998 /* Get the driver's phba structure from the dev_id and
10999 * assume the HBA is not interrupting.
11000 */
11001 phba = (struct lpfc_hba *) dev_id;
11002
11003 if (unlikely(!phba))
11004 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011005
11006 /*
James Smart93996272008-08-24 21:50:30 -040011007 * Stuff needs to be attented to when this function is invoked as an
11008 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011009 */
James Smart93996272008-08-24 21:50:30 -040011010 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011011 /* Check device state for handling interrupt */
11012 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011013 return IRQ_NONE;
11014 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011015 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11016 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011017 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011018 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011019 /*
11020 * If there is deferred error attention, do not check for
11021 * any interrupt.
11022 */
11023 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011024 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011025 return IRQ_NONE;
11026 }
James Smart93996272008-08-24 21:50:30 -040011027 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11028 phba->HAregaddr);
11029 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011030 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011031 } else
11032 ha_copy = phba->ha_copy;
11033
11034 /*
11035 * Process all events on FCP ring. Take the optimized path for FCP IO.
11036 */
11037 ha_copy &= ~(phba->work_ha_mask);
11038
11039 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011040 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011041 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011042 lpfc_sli_handle_fast_ring_event(phba,
11043 &phba->sli.ring[LPFC_FCP_RING],
11044 status);
James Smarta4bc3372006-12-02 13:34:16 -050011045
11046 if (phba->cfg_multi_ring_support == 2) {
11047 /*
James Smart93996272008-08-24 21:50:30 -040011048 * Process all events on extra ring. Take the optimized path
11049 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011050 */
James Smart93996272008-08-24 21:50:30 -040011051 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011052 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011053 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011054 lpfc_sli_handle_fast_ring_event(phba,
11055 &phba->sli.ring[LPFC_EXTRA_RING],
11056 status);
11057 }
11058 }
dea31012005-04-17 16:05:31 -050011059 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011060} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011061
James Smart93996272008-08-24 21:50:30 -040011062/**
James Smart3772a992009-05-22 14:50:54 -040011063 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011064 * @irq: Interrupt number.
11065 * @dev_id: The device context pointer.
11066 *
James Smart3772a992009-05-22 14:50:54 -040011067 * This function is the HBA device-level interrupt handler to device with
11068 * SLI-3 interface spec, called from the PCI layer when either MSI or
11069 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11070 * requires driver attention. This function invokes the slow-path interrupt
11071 * attention handling function and fast-path interrupt attention handling
11072 * function in turn to process the relevant HBA attention events. This
11073 * function is called without any lock held. It gets the hbalock to access
11074 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011075 *
11076 * This function returns IRQ_HANDLED when interrupt is handled, else it
11077 * returns IRQ_NONE.
11078 **/
11079irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011080lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011081{
11082 struct lpfc_hba *phba;
11083 irqreturn_t sp_irq_rc, fp_irq_rc;
11084 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011085 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011086
11087 /*
11088 * Get the driver's phba structure from the dev_id and
11089 * assume the HBA is not interrupting.
11090 */
11091 phba = (struct lpfc_hba *) dev_id;
11092
11093 if (unlikely(!phba))
11094 return IRQ_NONE;
11095
James Smart3772a992009-05-22 14:50:54 -040011096 /* Check device state for handling interrupt */
11097 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011098 return IRQ_NONE;
11099
11100 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011101 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11102 spin_unlock(&phba->hbalock);
11103 return IRQ_HANDLED;
11104 }
11105
James Smart93996272008-08-24 21:50:30 -040011106 if (unlikely(!phba->ha_copy)) {
11107 spin_unlock(&phba->hbalock);
11108 return IRQ_NONE;
11109 } else if (phba->ha_copy & HA_ERATT) {
11110 if (phba->hba_flag & HBA_ERATT_HANDLED)
11111 /* ERATT polling has handled ERATT */
11112 phba->ha_copy &= ~HA_ERATT;
11113 else
11114 /* Indicate interrupt handler handles ERATT */
11115 phba->hba_flag |= HBA_ERATT_HANDLED;
11116 }
11117
James Smarta257bf92009-04-06 18:48:10 -040011118 /*
11119 * If there is deferred error attention, do not check for any interrupt.
11120 */
11121 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011122 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011123 return IRQ_NONE;
11124 }
11125
James Smart93996272008-08-24 21:50:30 -040011126 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011127 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11128 spin_unlock(&phba->hbalock);
11129 return IRQ_HANDLED;
11130 }
James Smarta747c9c2009-11-18 15:41:10 -050011131 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11132 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11133 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011134 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011135 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011136 readl(phba->HAregaddr); /* flush */
11137 spin_unlock(&phba->hbalock);
11138
11139 /*
11140 * Invokes slow-path host attention interrupt handling as appropriate.
11141 */
11142
11143 /* status of events with mailbox and link attention */
11144 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11145
11146 /* status of events with ELS ring */
11147 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11148 status2 >>= (4*LPFC_ELS_RING);
11149
11150 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011151 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011152 else
11153 sp_irq_rc = IRQ_NONE;
11154
11155 /*
11156 * Invoke fast-path host attention interrupt handling as appropriate.
11157 */
11158
11159 /* status of events with FCP ring */
11160 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11161 status1 >>= (4*LPFC_FCP_RING);
11162
11163 /* status of events with extra ring */
11164 if (phba->cfg_multi_ring_support == 2) {
11165 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11166 status2 >>= (4*LPFC_EXTRA_RING);
11167 } else
11168 status2 = 0;
11169
11170 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011171 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011172 else
11173 fp_irq_rc = IRQ_NONE;
11174
11175 /* Return device-level interrupt handling status */
11176 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011177} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011178
11179/**
11180 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11181 * @phba: pointer to lpfc hba data structure.
11182 *
11183 * This routine is invoked by the worker thread to process all the pending
11184 * SLI4 FCP abort XRI events.
11185 **/
11186void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11187{
11188 struct lpfc_cq_event *cq_event;
11189
11190 /* First, declare the fcp xri abort event has been handled */
11191 spin_lock_irq(&phba->hbalock);
11192 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11193 spin_unlock_irq(&phba->hbalock);
11194 /* Now, handle all the fcp xri abort events */
11195 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11196 /* Get the first event from the head of the event queue */
11197 spin_lock_irq(&phba->hbalock);
11198 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11199 cq_event, struct lpfc_cq_event, list);
11200 spin_unlock_irq(&phba->hbalock);
11201 /* Notify aborted XRI for FCP work queue */
11202 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11203 /* Free the event processed back to the free pool */
11204 lpfc_sli4_cq_event_release(phba, cq_event);
11205 }
11206}
11207
11208/**
11209 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11210 * @phba: pointer to lpfc hba data structure.
11211 *
11212 * This routine is invoked by the worker thread to process all the pending
11213 * SLI4 els abort xri events.
11214 **/
11215void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11216{
11217 struct lpfc_cq_event *cq_event;
11218
11219 /* First, declare the els xri abort event has been handled */
11220 spin_lock_irq(&phba->hbalock);
11221 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11222 spin_unlock_irq(&phba->hbalock);
11223 /* Now, handle all the els xri abort events */
11224 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11225 /* Get the first event from the head of the event queue */
11226 spin_lock_irq(&phba->hbalock);
11227 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11228 cq_event, struct lpfc_cq_event, list);
11229 spin_unlock_irq(&phba->hbalock);
11230 /* Notify aborted XRI for ELS work queue */
11231 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11232 /* Free the event processed back to the free pool */
11233 lpfc_sli4_cq_event_release(phba, cq_event);
11234 }
11235}
11236
James Smart341af102010-01-26 23:07:37 -050011237/**
11238 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11239 * @phba: pointer to lpfc hba data structure
11240 * @pIocbIn: pointer to the rspiocbq
11241 * @pIocbOut: pointer to the cmdiocbq
11242 * @wcqe: pointer to the complete wcqe
11243 *
11244 * This routine transfers the fields of a command iocbq to a response iocbq
11245 * by copying all the IOCB fields from command iocbq and transferring the
11246 * completion status information from the complete wcqe.
11247 **/
James Smart4f774512009-05-22 14:52:35 -040011248static void
James Smart341af102010-01-26 23:07:37 -050011249lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11250 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011251 struct lpfc_iocbq *pIocbOut,
11252 struct lpfc_wcqe_complete *wcqe)
11253{
James Smartaf227412013-10-10 12:23:10 -040011254 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011255 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011256 uint32_t status, max_response;
11257 struct lpfc_dmabuf *dmabuf;
11258 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011259 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11260
11261 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11262 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011263 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011264 status = bf_get(lpfc_wcqe_c_status, wcqe);
11265 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011266 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11267 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11268 pIocbIn->iocb.un.fcpi.fcpi_parm =
11269 pIocbOut->iocb.un.fcpi.fcpi_parm -
11270 wcqe->total_data_placed;
11271 else
11272 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011273 else {
James Smart4f774512009-05-22 14:52:35 -040011274 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011275 switch (pIocbOut->iocb.ulpCommand) {
11276 case CMD_ELS_REQUEST64_CR:
11277 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11278 bpl = (struct ulp_bde64 *)dmabuf->virt;
11279 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11280 max_response = bde.tus.f.bdeSize;
11281 break;
11282 case CMD_GEN_REQUEST64_CR:
11283 max_response = 0;
11284 if (!pIocbOut->context3)
11285 break;
11286 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11287 sizeof(struct ulp_bde64);
11288 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11289 bpl = (struct ulp_bde64 *)dmabuf->virt;
11290 for (i = 0; i < numBdes; i++) {
11291 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11292 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11293 max_response += bde.tus.f.bdeSize;
11294 }
11295 break;
11296 default:
11297 max_response = wcqe->total_data_placed;
11298 break;
11299 }
11300 if (max_response < wcqe->total_data_placed)
11301 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11302 else
11303 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11304 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011305 }
James Smart341af102010-01-26 23:07:37 -050011306
James Smartacd68592012-01-18 16:25:09 -050011307 /* Convert BG errors for completion status */
11308 if (status == CQE_STATUS_DI_ERROR) {
11309 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11310
11311 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11312 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11313 else
11314 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11315
11316 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11317 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11318 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11319 BGS_GUARD_ERR_MASK;
11320 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11321 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11322 BGS_APPTAG_ERR_MASK;
11323 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11324 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11325 BGS_REFTAG_ERR_MASK;
11326
11327 /* Check to see if there was any good data before the error */
11328 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11329 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11330 BGS_HI_WATER_MARK_PRESENT_MASK;
11331 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11332 wcqe->total_data_placed;
11333 }
11334
11335 /*
11336 * Set ALL the error bits to indicate we don't know what
11337 * type of error it is.
11338 */
11339 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11340 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11341 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11342 BGS_GUARD_ERR_MASK);
11343 }
11344
James Smart341af102010-01-26 23:07:37 -050011345 /* Pick up HBA exchange busy condition */
11346 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11347 spin_lock_irqsave(&phba->hbalock, iflags);
11348 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11349 spin_unlock_irqrestore(&phba->hbalock, iflags);
11350 }
James Smart4f774512009-05-22 14:52:35 -040011351}
11352
11353/**
James Smart45ed1192009-10-02 15:17:02 -040011354 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11355 * @phba: Pointer to HBA context object.
11356 * @wcqe: Pointer to work-queue completion queue entry.
11357 *
11358 * This routine handles an ELS work-queue completion event and construct
11359 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11360 * discovery engine to handle.
11361 *
11362 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11363 **/
11364static struct lpfc_iocbq *
11365lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11366 struct lpfc_iocbq *irspiocbq)
11367{
11368 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11369 struct lpfc_iocbq *cmdiocbq;
11370 struct lpfc_wcqe_complete *wcqe;
11371 unsigned long iflags;
11372
11373 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011374 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011375 pring->stats.iocb_event++;
11376 /* Look up the ELS command IOCB and create pseudo response IOCB */
11377 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11378 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011379 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011380
11381 if (unlikely(!cmdiocbq)) {
11382 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11383 "0386 ELS complete with no corresponding "
11384 "cmdiocb: iotag (%d)\n",
11385 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11386 lpfc_sli_release_iocbq(phba, irspiocbq);
11387 return NULL;
11388 }
11389
11390 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011391 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011392
11393 return irspiocbq;
11394}
11395
11396/**
James Smart04c68492009-05-22 14:52:52 -040011397 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11398 * @phba: Pointer to HBA context object.
11399 * @cqe: Pointer to mailbox completion queue entry.
11400 *
11401 * This routine process a mailbox completion queue entry with asynchrous
11402 * event.
11403 *
11404 * Return: true if work posted to worker thread, otherwise false.
11405 **/
11406static bool
11407lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11408{
11409 struct lpfc_cq_event *cq_event;
11410 unsigned long iflags;
11411
11412 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11413 "0392 Async Event: word0:x%x, word1:x%x, "
11414 "word2:x%x, word3:x%x\n", mcqe->word0,
11415 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11416
11417 /* Allocate a new internal CQ_EVENT entry */
11418 cq_event = lpfc_sli4_cq_event_alloc(phba);
11419 if (!cq_event) {
11420 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11421 "0394 Failed to allocate CQ_EVENT entry\n");
11422 return false;
11423 }
11424
11425 /* Move the CQE into an asynchronous event entry */
11426 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11427 spin_lock_irqsave(&phba->hbalock, iflags);
11428 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11429 /* Set the async event flag */
11430 phba->hba_flag |= ASYNC_EVENT;
11431 spin_unlock_irqrestore(&phba->hbalock, iflags);
11432
11433 return true;
11434}
11435
11436/**
11437 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11438 * @phba: Pointer to HBA context object.
11439 * @cqe: Pointer to mailbox completion queue entry.
11440 *
11441 * This routine process a mailbox completion queue entry with mailbox
11442 * completion event.
11443 *
11444 * Return: true if work posted to worker thread, otherwise false.
11445 **/
11446static bool
11447lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11448{
11449 uint32_t mcqe_status;
11450 MAILBOX_t *mbox, *pmbox;
11451 struct lpfc_mqe *mqe;
11452 struct lpfc_vport *vport;
11453 struct lpfc_nodelist *ndlp;
11454 struct lpfc_dmabuf *mp;
11455 unsigned long iflags;
11456 LPFC_MBOXQ_t *pmb;
11457 bool workposted = false;
11458 int rc;
11459
11460 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11461 if (!bf_get(lpfc_trailer_completed, mcqe))
11462 goto out_no_mqe_complete;
11463
11464 /* Get the reference to the active mbox command */
11465 spin_lock_irqsave(&phba->hbalock, iflags);
11466 pmb = phba->sli.mbox_active;
11467 if (unlikely(!pmb)) {
11468 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11469 "1832 No pending MBOX command to handle\n");
11470 spin_unlock_irqrestore(&phba->hbalock, iflags);
11471 goto out_no_mqe_complete;
11472 }
11473 spin_unlock_irqrestore(&phba->hbalock, iflags);
11474 mqe = &pmb->u.mqe;
11475 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11476 mbox = phba->mbox;
11477 vport = pmb->vport;
11478
11479 /* Reset heartbeat timer */
11480 phba->last_completion_time = jiffies;
11481 del_timer(&phba->sli.mbox_tmo);
11482
11483 /* Move mbox data to caller's mailbox region, do endian swapping */
11484 if (pmb->mbox_cmpl && mbox)
11485 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011486
James Smart73d91e52011-10-10 21:32:10 -040011487 /*
11488 * For mcqe errors, conditionally move a modified error code to
11489 * the mbox so that the error will not be missed.
11490 */
11491 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11492 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11493 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11494 bf_set(lpfc_mqe_status, mqe,
11495 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11496 }
James Smart04c68492009-05-22 14:52:52 -040011497 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11498 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11499 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11500 "MBOX dflt rpi: status:x%x rpi:x%x",
11501 mcqe_status,
11502 pmbox->un.varWords[0], 0);
11503 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11504 mp = (struct lpfc_dmabuf *)(pmb->context1);
11505 ndlp = (struct lpfc_nodelist *)pmb->context2;
11506 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11507 * RID of the PPI using the same mbox buffer.
11508 */
11509 lpfc_unreg_login(phba, vport->vpi,
11510 pmbox->un.varWords[0], pmb);
11511 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11512 pmb->context1 = mp;
11513 pmb->context2 = ndlp;
11514 pmb->vport = vport;
11515 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11516 if (rc != MBX_BUSY)
11517 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11518 LOG_SLI, "0385 rc should "
11519 "have been MBX_BUSY\n");
11520 if (rc != MBX_NOT_FINISHED)
11521 goto send_current_mbox;
11522 }
11523 }
11524 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11525 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11526 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11527
11528 /* There is mailbox completion work to do */
11529 spin_lock_irqsave(&phba->hbalock, iflags);
11530 __lpfc_mbox_cmpl_put(phba, pmb);
11531 phba->work_ha |= HA_MBATT;
11532 spin_unlock_irqrestore(&phba->hbalock, iflags);
11533 workposted = true;
11534
11535send_current_mbox:
11536 spin_lock_irqsave(&phba->hbalock, iflags);
11537 /* Release the mailbox command posting token */
11538 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11539 /* Setting active mailbox pointer need to be in sync to flag clear */
11540 phba->sli.mbox_active = NULL;
11541 spin_unlock_irqrestore(&phba->hbalock, iflags);
11542 /* Wake up worker thread to post the next pending mailbox command */
11543 lpfc_worker_wake_up(phba);
11544out_no_mqe_complete:
11545 if (bf_get(lpfc_trailer_consumed, mcqe))
11546 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11547 return workposted;
11548}
11549
11550/**
11551 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11552 * @phba: Pointer to HBA context object.
11553 * @cqe: Pointer to mailbox completion queue entry.
11554 *
11555 * This routine process a mailbox completion queue entry, it invokes the
11556 * proper mailbox complete handling or asynchrous event handling routine
11557 * according to the MCQE's async bit.
11558 *
11559 * Return: true if work posted to worker thread, otherwise false.
11560 **/
11561static bool
11562lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11563{
11564 struct lpfc_mcqe mcqe;
11565 bool workposted;
11566
11567 /* Copy the mailbox MCQE and convert endian order as needed */
11568 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11569
11570 /* Invoke the proper event handling routine */
11571 if (!bf_get(lpfc_trailer_async, &mcqe))
11572 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11573 else
11574 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11575 return workposted;
11576}
11577
11578/**
James Smart4f774512009-05-22 14:52:35 -040011579 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11580 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011581 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011582 * @wcqe: Pointer to work-queue completion queue entry.
11583 *
11584 * This routine handles an ELS work-queue completion event.
11585 *
11586 * Return: true if work posted to worker thread, otherwise false.
11587 **/
11588static bool
James Smart2a76a282012-08-03 12:35:54 -040011589lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011590 struct lpfc_wcqe_complete *wcqe)
11591{
James Smart4f774512009-05-22 14:52:35 -040011592 struct lpfc_iocbq *irspiocbq;
11593 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011594 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011595 int txq_cnt = 0;
11596 int txcmplq_cnt = 0;
11597 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040011598
James Smart45ed1192009-10-02 15:17:02 -040011599 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011600 irspiocbq = lpfc_sli_get_iocbq(phba);
11601 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011602 if (!list_empty(&pring->txq))
11603 txq_cnt++;
11604 if (!list_empty(&pring->txcmplq))
11605 txcmplq_cnt++;
11606 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11607 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040011608 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011609 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11610 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050011611 txq_cnt, phba->iocb_cnt,
11612 fcp_txcmplq_cnt,
11613 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011614 return false;
James Smart4f774512009-05-22 14:52:35 -040011615 }
James Smart4f774512009-05-22 14:52:35 -040011616
James Smart45ed1192009-10-02 15:17:02 -040011617 /* Save off the slow-path queue event for work thread to process */
11618 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011619 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011620 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011621 &phba->sli4_hba.sp_queue_event);
11622 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011623 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011624
James Smart45ed1192009-10-02 15:17:02 -040011625 return true;
James Smart4f774512009-05-22 14:52:35 -040011626}
11627
11628/**
11629 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11630 * @phba: Pointer to HBA context object.
11631 * @wcqe: Pointer to work-queue completion queue entry.
11632 *
11633 * This routine handles slow-path WQ entry comsumed event by invoking the
11634 * proper WQ release routine to the slow-path WQ.
11635 **/
11636static void
11637lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11638 struct lpfc_wcqe_release *wcqe)
11639{
James Smart2e90f4b2011-12-13 13:22:37 -050011640 /* sanity check on queue memory */
11641 if (unlikely(!phba->sli4_hba.els_wq))
11642 return;
James Smart4f774512009-05-22 14:52:35 -040011643 /* Check for the slow-path ELS work queue */
11644 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11645 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11646 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11647 else
11648 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11649 "2579 Slow-path wqe consume event carries "
11650 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11651 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11652 phba->sli4_hba.els_wq->queue_id);
11653}
11654
11655/**
11656 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11657 * @phba: Pointer to HBA context object.
11658 * @cq: Pointer to a WQ completion queue.
11659 * @wcqe: Pointer to work-queue completion queue entry.
11660 *
11661 * This routine handles an XRI abort event.
11662 *
11663 * Return: true if work posted to worker thread, otherwise false.
11664 **/
11665static bool
11666lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11667 struct lpfc_queue *cq,
11668 struct sli4_wcqe_xri_aborted *wcqe)
11669{
11670 bool workposted = false;
11671 struct lpfc_cq_event *cq_event;
11672 unsigned long iflags;
11673
11674 /* Allocate a new internal CQ_EVENT entry */
11675 cq_event = lpfc_sli4_cq_event_alloc(phba);
11676 if (!cq_event) {
11677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11678 "0602 Failed to allocate CQ_EVENT entry\n");
11679 return false;
11680 }
11681
11682 /* Move the CQE into the proper xri abort event list */
11683 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11684 switch (cq->subtype) {
11685 case LPFC_FCP:
11686 spin_lock_irqsave(&phba->hbalock, iflags);
11687 list_add_tail(&cq_event->list,
11688 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11689 /* Set the fcp xri abort event flag */
11690 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11691 spin_unlock_irqrestore(&phba->hbalock, iflags);
11692 workposted = true;
11693 break;
11694 case LPFC_ELS:
11695 spin_lock_irqsave(&phba->hbalock, iflags);
11696 list_add_tail(&cq_event->list,
11697 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11698 /* Set the els xri abort event flag */
11699 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11700 spin_unlock_irqrestore(&phba->hbalock, iflags);
11701 workposted = true;
11702 break;
11703 default:
11704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11705 "0603 Invalid work queue CQE subtype (x%x)\n",
11706 cq->subtype);
11707 workposted = false;
11708 break;
11709 }
11710 return workposted;
11711}
11712
11713/**
James Smart4d9ab992009-10-02 15:16:39 -040011714 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011715 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011716 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011717 *
James Smart4d9ab992009-10-02 15:16:39 -040011718 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011719 *
11720 * Return: true if work posted to worker thread, otherwise false.
11721 **/
11722static bool
James Smart4d9ab992009-10-02 15:16:39 -040011723lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11724{
11725 bool workposted = false;
11726 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11727 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11728 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011729 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011730 unsigned long iflags;
11731
James Smart2e90f4b2011-12-13 13:22:37 -050011732 /* sanity check on queue memory */
11733 if (unlikely(!hrq) || unlikely(!drq))
11734 return workposted;
11735
James Smart7851fe22011-07-22 18:36:52 -040011736 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11737 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11738 else
11739 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11740 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011741 goto out;
11742
11743 status = bf_get(lpfc_rcqe_status, rcqe);
11744 switch (status) {
11745 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11747 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011748 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011749 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011750 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011751 spin_lock_irqsave(&phba->hbalock, iflags);
11752 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11753 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011754 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011755 spin_unlock_irqrestore(&phba->hbalock, iflags);
11756 goto out;
11757 }
James Smartb84daac2012-08-03 12:35:13 -040011758 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011759 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11760 /* save off the frame for the word thread to process */
11761 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011762 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011763 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011764 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011765 spin_unlock_irqrestore(&phba->hbalock, iflags);
11766 workposted = true;
11767 break;
11768 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11769 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011770 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011771 /* Post more buffers if possible */
11772 spin_lock_irqsave(&phba->hbalock, iflags);
11773 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11774 spin_unlock_irqrestore(&phba->hbalock, iflags);
11775 workposted = true;
11776 break;
11777 }
11778out:
11779 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011780}
11781
11782/**
11783 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11784 * @phba: Pointer to HBA context object.
11785 * @cq: Pointer to the completion queue.
11786 * @wcqe: Pointer to a completion queue entry.
11787 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011788 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011789 * entry.
11790 *
11791 * Return: true if work posted to worker thread, otherwise false.
11792 **/
11793static bool
11794lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011795 struct lpfc_cqe *cqe)
11796{
James Smart45ed1192009-10-02 15:17:02 -040011797 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011798 bool workposted = false;
11799
11800 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011801 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011802
11803 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011804 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011805 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011806 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011807 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011808 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011809 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011810 break;
11811 case CQE_CODE_RELEASE_WQE:
11812 /* Process the WQ release event */
11813 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011814 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011815 break;
11816 case CQE_CODE_XRI_ABORTED:
11817 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011818 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011819 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011820 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011821 break;
James Smart4d9ab992009-10-02 15:16:39 -040011822 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011823 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011824 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011825 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011826 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011827 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011828 break;
James Smart4f774512009-05-22 14:52:35 -040011829 default:
11830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11831 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011832 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011833 break;
11834 }
11835 return workposted;
11836}
11837
11838/**
James Smart4f774512009-05-22 14:52:35 -040011839 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11840 * @phba: Pointer to HBA context object.
11841 * @eqe: Pointer to fast-path event queue entry.
11842 *
11843 * This routine process a event queue entry from the slow-path event queue.
11844 * It will check the MajorCode and MinorCode to determine this is for a
11845 * completion event on a completion queue, if not, an error shall be logged
11846 * and just return. Otherwise, it will get to the corresponding completion
11847 * queue and process all the entries on that completion queue, rearm the
11848 * completion queue, and then return.
11849 *
11850 **/
11851static void
James Smart67d12732012-08-03 12:36:13 -040011852lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11853 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040011854{
James Smart67d12732012-08-03 12:36:13 -040011855 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040011856 struct lpfc_cqe *cqe;
11857 bool workposted = false;
11858 int ecount = 0;
11859 uint16_t cqid;
11860
James Smart4f774512009-05-22 14:52:35 -040011861 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011862 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011863
James Smart4f774512009-05-22 14:52:35 -040011864 list_for_each_entry(childq, &speq->child_list, list) {
11865 if (childq->queue_id == cqid) {
11866 cq = childq;
11867 break;
11868 }
11869 }
11870 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011871 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11872 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11873 "0365 Slow-path CQ identifier "
11874 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011875 return;
11876 }
11877
11878 /* Process all the entries to the CQ */
11879 switch (cq->type) {
11880 case LPFC_MCQ:
11881 while ((cqe = lpfc_sli4_cq_get(cq))) {
11882 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011883 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011884 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011885 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011886 }
11887 break;
11888 case LPFC_WCQ:
11889 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011890 if (cq->subtype == LPFC_FCP)
11891 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11892 cqe);
11893 else
11894 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11895 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011896 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011897 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11898 }
James Smartb84daac2012-08-03 12:35:13 -040011899
11900 /* Track the max number of CQEs processed in 1 EQ */
11901 if (ecount > cq->CQ_max_cqe)
11902 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011903 break;
11904 default:
11905 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11906 "0370 Invalid completion queue type (%d)\n",
11907 cq->type);
11908 return;
11909 }
11910
11911 /* Catch the no cq entry condition, log an error */
11912 if (unlikely(ecount == 0))
11913 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11914 "0371 No entry from the CQ: identifier "
11915 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11916
11917 /* In any case, flash and re-arm the RCQ */
11918 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11919
11920 /* wake up worker thread if there are works to be done */
11921 if (workposted)
11922 lpfc_worker_wake_up(phba);
11923}
11924
11925/**
11926 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011927 * @phba: Pointer to HBA context object.
11928 * @cq: Pointer to associated CQ
11929 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011930 *
11931 * This routine process a fast-path work queue completion entry from fast-path
11932 * event queue for FCP command response completion.
11933 **/
11934static void
James Smart2a76a282012-08-03 12:35:54 -040011935lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011936 struct lpfc_wcqe_complete *wcqe)
11937{
James Smart2a76a282012-08-03 12:35:54 -040011938 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011939 struct lpfc_iocbq *cmdiocbq;
11940 struct lpfc_iocbq irspiocbq;
11941 unsigned long iflags;
11942
James Smart4f774512009-05-22 14:52:35 -040011943 /* Check for response status */
11944 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11945 /* If resource errors reported from HBA, reduce queue
11946 * depth of the SCSI device.
11947 */
James Smarte3d2b802012-08-14 14:25:43 -040011948 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11949 IOSTAT_LOCAL_REJECT)) &&
11950 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11951 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040011952 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040011953
James Smart4f774512009-05-22 14:52:35 -040011954 /* Log the error status */
11955 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11956 "0373 FCP complete error: status=x%x, "
11957 "hw_status=x%x, total_data_specified=%d, "
11958 "parameter=x%x, word3=x%x\n",
11959 bf_get(lpfc_wcqe_c_status, wcqe),
11960 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11961 wcqe->total_data_placed, wcqe->parameter,
11962 wcqe->word3);
11963 }
11964
11965 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011966 spin_lock_irqsave(&pring->ring_lock, iflags);
11967 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011968 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11969 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011970 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011971 if (unlikely(!cmdiocbq)) {
11972 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11973 "0374 FCP complete with no corresponding "
11974 "cmdiocb: iotag (%d)\n",
11975 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11976 return;
11977 }
11978 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11979 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11980 "0375 FCP cmdiocb not callback function "
11981 "iotag: (%d)\n",
11982 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11983 return;
11984 }
11985
11986 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011987 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011988
James Smart0f65ff62010-02-26 14:14:23 -050011989 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11990 spin_lock_irqsave(&phba->hbalock, iflags);
11991 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11992 spin_unlock_irqrestore(&phba->hbalock, iflags);
11993 }
11994
James Smart4f774512009-05-22 14:52:35 -040011995 /* Pass the cmd_iocb and the rsp state to the upper layer */
11996 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11997}
11998
11999/**
12000 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12001 * @phba: Pointer to HBA context object.
12002 * @cq: Pointer to completion queue.
12003 * @wcqe: Pointer to work-queue completion queue entry.
12004 *
12005 * This routine handles an fast-path WQ entry comsumed event by invoking the
12006 * proper WQ release routine to the slow-path WQ.
12007 **/
12008static void
12009lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12010 struct lpfc_wcqe_release *wcqe)
12011{
12012 struct lpfc_queue *childwq;
12013 bool wqid_matched = false;
12014 uint16_t fcp_wqid;
12015
12016 /* Check for fast-path FCP work queue release */
12017 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12018 list_for_each_entry(childwq, &cq->child_list, list) {
12019 if (childwq->queue_id == fcp_wqid) {
12020 lpfc_sli4_wq_release(childwq,
12021 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12022 wqid_matched = true;
12023 break;
12024 }
12025 }
12026 /* Report warning log message if no match found */
12027 if (wqid_matched != true)
12028 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12029 "2580 Fast-path wqe consume event carries "
12030 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12031}
12032
12033/**
12034 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12035 * @cq: Pointer to the completion queue.
12036 * @eqe: Pointer to fast-path completion queue entry.
12037 *
12038 * This routine process a fast-path work queue completion entry from fast-path
12039 * event queue for FCP command response completion.
12040 **/
12041static int
12042lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12043 struct lpfc_cqe *cqe)
12044{
12045 struct lpfc_wcqe_release wcqe;
12046 bool workposted = false;
12047
12048 /* Copy the work queue CQE and convert endian order if needed */
12049 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12050
12051 /* Check and process for different type of WCQE and dispatch */
12052 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12053 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012054 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012055 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012056 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012057 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012058 (struct lpfc_wcqe_complete *)&wcqe);
12059 break;
12060 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012061 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012062 /* Process the WQ release event */
12063 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12064 (struct lpfc_wcqe_release *)&wcqe);
12065 break;
12066 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012067 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012068 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012069 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012070 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12071 (struct sli4_wcqe_xri_aborted *)&wcqe);
12072 break;
12073 default:
12074 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12075 "0144 Not a valid WCQE code: x%x\n",
12076 bf_get(lpfc_wcqe_c_code, &wcqe));
12077 break;
12078 }
12079 return workposted;
12080}
12081
12082/**
James Smart67d12732012-08-03 12:36:13 -040012083 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012084 * @phba: Pointer to HBA context object.
12085 * @eqe: Pointer to fast-path event queue entry.
12086 *
12087 * This routine process a event queue entry from the fast-path event queue.
12088 * It will check the MajorCode and MinorCode to determine this is for a
12089 * completion event on a completion queue, if not, an error shall be logged
12090 * and just return. Otherwise, it will get to the corresponding completion
12091 * queue and process all the entries on the completion queue, rearm the
12092 * completion queue, and then return.
12093 **/
12094static void
James Smart67d12732012-08-03 12:36:13 -040012095lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12096 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012097{
12098 struct lpfc_queue *cq;
12099 struct lpfc_cqe *cqe;
12100 bool workposted = false;
12101 uint16_t cqid;
12102 int ecount = 0;
12103
James Smartcb5172e2010-03-15 11:25:07 -040012104 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012105 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012106 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012107 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012108 bf_get_le32(lpfc_eqe_major_code, eqe),
12109 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012110 return;
12111 }
12112
James Smart67d12732012-08-03 12:36:13 -040012113 /* Get the reference to the corresponding CQ */
12114 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12115
12116 /* Check if this is a Slow path event */
12117 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12118 lpfc_sli4_sp_handle_eqe(phba, eqe,
12119 phba->sli4_hba.hba_eq[qidx]);
12120 return;
12121 }
12122
James Smart2e90f4b2011-12-13 13:22:37 -050012123 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12124 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12125 "3146 Fast-path completion queues "
12126 "does not exist\n");
12127 return;
12128 }
James Smart67d12732012-08-03 12:36:13 -040012129 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012130 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012131 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12132 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12133 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012134 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012135 return;
12136 }
12137
James Smart4f774512009-05-22 14:52:35 -040012138 if (unlikely(cqid != cq->queue_id)) {
12139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12140 "0368 Miss-matched fast-path completion "
12141 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12142 cqid, cq->queue_id);
12143 return;
12144 }
12145
12146 /* Process all the entries to the CQ */
12147 while ((cqe = lpfc_sli4_cq_get(cq))) {
12148 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012149 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012150 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12151 }
12152
James Smartb84daac2012-08-03 12:35:13 -040012153 /* Track the max number of CQEs processed in 1 EQ */
12154 if (ecount > cq->CQ_max_cqe)
12155 cq->CQ_max_cqe = ecount;
12156
James Smart4f774512009-05-22 14:52:35 -040012157 /* Catch the no cq entry condition */
12158 if (unlikely(ecount == 0))
12159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12160 "0369 No entry from fast-path completion "
12161 "queue fcpcqid=%d\n", cq->queue_id);
12162
12163 /* In any case, flash and re-arm the CQ */
12164 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12165
12166 /* wake up worker thread if there are works to be done */
12167 if (workposted)
12168 lpfc_worker_wake_up(phba);
12169}
12170
12171static void
12172lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12173{
12174 struct lpfc_eqe *eqe;
12175
12176 /* walk all the EQ entries and drop on the floor */
12177 while ((eqe = lpfc_sli4_eq_get(eq)))
12178 ;
12179
12180 /* Clear and re-arm the EQ */
12181 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12182}
12183
James Smart1ba981f2014-02-20 09:56:45 -050012184
12185/**
12186 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12187 * entry
12188 * @phba: Pointer to HBA context object.
12189 * @eqe: Pointer to fast-path event queue entry.
12190 *
12191 * This routine process a event queue entry from the Flash Optimized Fabric
12192 * event queue. It will check the MajorCode and MinorCode to determine this
12193 * is for a completion event on a completion queue, if not, an error shall be
12194 * logged and just return. Otherwise, it will get to the corresponding
12195 * completion queue and process all the entries on the completion queue, rearm
12196 * the completion queue, and then return.
12197 **/
12198static void
12199lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12200{
12201 struct lpfc_queue *cq;
12202 struct lpfc_cqe *cqe;
12203 bool workposted = false;
12204 uint16_t cqid;
12205 int ecount = 0;
12206
12207 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12209 "9147 Not a valid completion "
12210 "event: majorcode=x%x, minorcode=x%x\n",
12211 bf_get_le32(lpfc_eqe_major_code, eqe),
12212 bf_get_le32(lpfc_eqe_minor_code, eqe));
12213 return;
12214 }
12215
12216 /* Get the reference to the corresponding CQ */
12217 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12218
12219 /* Next check for OAS */
12220 cq = phba->sli4_hba.oas_cq;
12221 if (unlikely(!cq)) {
12222 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12224 "9148 OAS completion queue "
12225 "does not exist\n");
12226 return;
12227 }
12228
12229 if (unlikely(cqid != cq->queue_id)) {
12230 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12231 "9149 Miss-matched fast-path compl "
12232 "queue id: eqcqid=%d, fcpcqid=%d\n",
12233 cqid, cq->queue_id);
12234 return;
12235 }
12236
12237 /* Process all the entries to the OAS CQ */
12238 while ((cqe = lpfc_sli4_cq_get(cq))) {
12239 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12240 if (!(++ecount % cq->entry_repost))
12241 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12242 }
12243
12244 /* Track the max number of CQEs processed in 1 EQ */
12245 if (ecount > cq->CQ_max_cqe)
12246 cq->CQ_max_cqe = ecount;
12247
12248 /* Catch the no cq entry condition */
12249 if (unlikely(ecount == 0))
12250 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12251 "9153 No entry from fast-path completion "
12252 "queue fcpcqid=%d\n", cq->queue_id);
12253
12254 /* In any case, flash and re-arm the CQ */
12255 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12256
12257 /* wake up worker thread if there are works to be done */
12258 if (workposted)
12259 lpfc_worker_wake_up(phba);
12260}
12261
12262/**
12263 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12264 * @irq: Interrupt number.
12265 * @dev_id: The device context pointer.
12266 *
12267 * This function is directly called from the PCI layer as an interrupt
12268 * service routine when device with SLI-4 interface spec is enabled with
12269 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12270 * IOCB ring event in the HBA. However, when the device is enabled with either
12271 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12272 * device-level interrupt handler. When the PCI slot is in error recovery
12273 * or the HBA is undergoing initialization, the interrupt handler will not
12274 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12275 * the intrrupt context. This function is called without any lock held.
12276 * It gets the hbalock to access and update SLI data structures. Note that,
12277 * the EQ to CQ are one-to-one map such that the EQ index is
12278 * equal to that of CQ index.
12279 *
12280 * This function returns IRQ_HANDLED when interrupt is handled else it
12281 * returns IRQ_NONE.
12282 **/
12283irqreturn_t
12284lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12285{
12286 struct lpfc_hba *phba;
12287 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12288 struct lpfc_queue *eq;
12289 struct lpfc_eqe *eqe;
12290 unsigned long iflag;
12291 int ecount = 0;
12292 uint32_t eqidx;
12293
12294 /* Get the driver's phba structure from the dev_id */
12295 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12296 phba = fcp_eq_hdl->phba;
12297 eqidx = fcp_eq_hdl->idx;
12298
12299 if (unlikely(!phba))
12300 return IRQ_NONE;
12301
12302 /* Get to the EQ struct associated with this vector */
12303 eq = phba->sli4_hba.fof_eq;
12304 if (unlikely(!eq))
12305 return IRQ_NONE;
12306
12307 /* Check device state for handling interrupt */
12308 if (unlikely(lpfc_intr_state_check(phba))) {
12309 eq->EQ_badstate++;
12310 /* Check again for link_state with lock held */
12311 spin_lock_irqsave(&phba->hbalock, iflag);
12312 if (phba->link_state < LPFC_LINK_DOWN)
12313 /* Flush, clear interrupt, and rearm the EQ */
12314 lpfc_sli4_eq_flush(phba, eq);
12315 spin_unlock_irqrestore(&phba->hbalock, iflag);
12316 return IRQ_NONE;
12317 }
12318
12319 /*
12320 * Process all the event on FCP fast-path EQ
12321 */
12322 while ((eqe = lpfc_sli4_eq_get(eq))) {
12323 lpfc_sli4_fof_handle_eqe(phba, eqe);
12324 if (!(++ecount % eq->entry_repost))
12325 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12326 eq->EQ_processed++;
12327 }
12328
12329 /* Track the max number of EQEs processed in 1 intr */
12330 if (ecount > eq->EQ_max_eqe)
12331 eq->EQ_max_eqe = ecount;
12332
12333
12334 if (unlikely(ecount == 0)) {
12335 eq->EQ_no_entry++;
12336
12337 if (phba->intr_type == MSIX)
12338 /* MSI-X treated interrupt served as no EQ share INT */
12339 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12340 "9145 MSI-X interrupt with no EQE\n");
12341 else {
12342 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12343 "9146 ISR interrupt with no EQE\n");
12344 /* Non MSI-X treated on interrupt as EQ share INT */
12345 return IRQ_NONE;
12346 }
12347 }
12348 /* Always clear and re-arm the fast-path EQ */
12349 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12350 return IRQ_HANDLED;
12351}
12352
James Smart4f774512009-05-22 14:52:35 -040012353/**
James Smart67d12732012-08-03 12:36:13 -040012354 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012355 * @irq: Interrupt number.
12356 * @dev_id: The device context pointer.
12357 *
12358 * This function is directly called from the PCI layer as an interrupt
12359 * service routine when device with SLI-4 interface spec is enabled with
12360 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12361 * ring event in the HBA. However, when the device is enabled with either
12362 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12363 * device-level interrupt handler. When the PCI slot is in error recovery
12364 * or the HBA is undergoing initialization, the interrupt handler will not
12365 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12366 * the intrrupt context. This function is called without any lock held.
12367 * It gets the hbalock to access and update SLI data structures. Note that,
12368 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12369 * equal to that of FCP CQ index.
12370 *
James Smart67d12732012-08-03 12:36:13 -040012371 * The link attention and ELS ring attention events are handled
12372 * by the worker thread. The interrupt handler signals the worker thread
12373 * and returns for these events. This function is called without any lock
12374 * held. It gets the hbalock to access and update SLI data structures.
12375 *
James Smart4f774512009-05-22 14:52:35 -040012376 * This function returns IRQ_HANDLED when interrupt is handled else it
12377 * returns IRQ_NONE.
12378 **/
12379irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012380lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012381{
12382 struct lpfc_hba *phba;
12383 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12384 struct lpfc_queue *fpeq;
12385 struct lpfc_eqe *eqe;
12386 unsigned long iflag;
12387 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012388 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012389
12390 /* Get the driver's phba structure from the dev_id */
12391 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12392 phba = fcp_eq_hdl->phba;
12393 fcp_eqidx = fcp_eq_hdl->idx;
12394
12395 if (unlikely(!phba))
12396 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012397 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012398 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012399
12400 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012401 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012402 if (unlikely(!fpeq))
12403 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012404
James Smartba20c852012-08-03 12:36:52 -040012405 if (lpfc_fcp_look_ahead) {
12406 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12407 lpfc_sli4_eq_clr_intr(fpeq);
12408 else {
12409 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12410 return IRQ_NONE;
12411 }
12412 }
12413
James Smart4f774512009-05-22 14:52:35 -040012414 /* Check device state for handling interrupt */
12415 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012416 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012417 /* Check again for link_state with lock held */
12418 spin_lock_irqsave(&phba->hbalock, iflag);
12419 if (phba->link_state < LPFC_LINK_DOWN)
12420 /* Flush, clear interrupt, and rearm the EQ */
12421 lpfc_sli4_eq_flush(phba, fpeq);
12422 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012423 if (lpfc_fcp_look_ahead)
12424 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012425 return IRQ_NONE;
12426 }
12427
12428 /*
12429 * Process all the event on FCP fast-path EQ
12430 */
12431 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smart67d12732012-08-03 12:36:13 -040012432 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012433 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012434 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012435 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012436 }
12437
James Smartb84daac2012-08-03 12:35:13 -040012438 /* Track the max number of EQEs processed in 1 intr */
12439 if (ecount > fpeq->EQ_max_eqe)
12440 fpeq->EQ_max_eqe = ecount;
12441
James Smart4f774512009-05-22 14:52:35 -040012442 /* Always clear and re-arm the fast-path EQ */
12443 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12444
12445 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012446 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012447
12448 if (lpfc_fcp_look_ahead) {
12449 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12450 return IRQ_NONE;
12451 }
12452
James Smart4f774512009-05-22 14:52:35 -040012453 if (phba->intr_type == MSIX)
12454 /* MSI-X treated interrupt served as no EQ share INT */
12455 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12456 "0358 MSI-X interrupt with no EQE\n");
12457 else
12458 /* Non MSI-X treated on interrupt as EQ share INT */
12459 return IRQ_NONE;
12460 }
12461
James Smartba20c852012-08-03 12:36:52 -040012462 if (lpfc_fcp_look_ahead)
12463 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012464 return IRQ_HANDLED;
12465} /* lpfc_sli4_fp_intr_handler */
12466
12467/**
12468 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12469 * @irq: Interrupt number.
12470 * @dev_id: The device context pointer.
12471 *
12472 * This function is the device-level interrupt handler to device with SLI-4
12473 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12474 * interrupt mode is enabled and there is an event in the HBA which requires
12475 * driver attention. This function invokes the slow-path interrupt attention
12476 * handling function and fast-path interrupt attention handling function in
12477 * turn to process the relevant HBA attention events. This function is called
12478 * without any lock held. It gets the hbalock to access and update SLI data
12479 * structures.
12480 *
12481 * This function returns IRQ_HANDLED when interrupt is handled, else it
12482 * returns IRQ_NONE.
12483 **/
12484irqreturn_t
12485lpfc_sli4_intr_handler(int irq, void *dev_id)
12486{
12487 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012488 irqreturn_t hba_irq_rc;
12489 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012490 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012491
12492 /* Get the driver's phba structure from the dev_id */
12493 phba = (struct lpfc_hba *)dev_id;
12494
12495 if (unlikely(!phba))
12496 return IRQ_NONE;
12497
12498 /*
James Smart4f774512009-05-22 14:52:35 -040012499 * Invoke fast-path host attention interrupt handling as appropriate.
12500 */
James Smart67d12732012-08-03 12:36:13 -040012501 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12502 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012503 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012504 if (hba_irq_rc == IRQ_HANDLED)
12505 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012506 }
12507
James Smart1ba981f2014-02-20 09:56:45 -050012508 if (phba->cfg_fof) {
12509 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12510 &phba->sli4_hba.fcp_eq_hdl[0]);
12511 if (hba_irq_rc == IRQ_HANDLED)
12512 hba_handled |= true;
12513 }
12514
James Smart67d12732012-08-03 12:36:13 -040012515 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012516} /* lpfc_sli4_intr_handler */
12517
12518/**
12519 * lpfc_sli4_queue_free - free a queue structure and associated memory
12520 * @queue: The queue structure to free.
12521 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012522 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012523 * the host resident queue. This function must be called after destroying the
12524 * queue on the HBA.
12525 **/
12526void
12527lpfc_sli4_queue_free(struct lpfc_queue *queue)
12528{
12529 struct lpfc_dmabuf *dmabuf;
12530
12531 if (!queue)
12532 return;
12533
12534 while (!list_empty(&queue->page_list)) {
12535 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12536 list);
James Smart49198b32010-04-06 15:04:33 -040012537 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012538 dmabuf->virt, dmabuf->phys);
12539 kfree(dmabuf);
12540 }
12541 kfree(queue);
12542 return;
12543}
12544
12545/**
12546 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12547 * @phba: The HBA that this queue is being created on.
12548 * @entry_size: The size of each queue entry for this queue.
12549 * @entry count: The number of entries that this queue will handle.
12550 *
12551 * This function allocates a queue structure and the DMAable memory used for
12552 * the host resident queue. This function must be called before creating the
12553 * queue on the HBA.
12554 **/
12555struct lpfc_queue *
12556lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12557 uint32_t entry_count)
12558{
12559 struct lpfc_queue *queue;
12560 struct lpfc_dmabuf *dmabuf;
12561 int x, total_qe_count;
12562 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012563 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012564
James Smartcb5172e2010-03-15 11:25:07 -040012565 if (!phba->sli4_hba.pc_sli4_params.supported)
12566 hw_page_size = SLI4_PAGE_SIZE;
12567
James Smart4f774512009-05-22 14:52:35 -040012568 queue = kzalloc(sizeof(struct lpfc_queue) +
12569 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12570 if (!queue)
12571 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012572 queue->page_count = (ALIGN(entry_size * entry_count,
12573 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012574 INIT_LIST_HEAD(&queue->list);
12575 INIT_LIST_HEAD(&queue->page_list);
12576 INIT_LIST_HEAD(&queue->child_list);
12577 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12578 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12579 if (!dmabuf)
12580 goto out_fail;
12581 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012582 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012583 GFP_KERNEL);
12584 if (!dmabuf->virt) {
12585 kfree(dmabuf);
12586 goto out_fail;
12587 }
James Smartcb5172e2010-03-15 11:25:07 -040012588 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012589 dmabuf->buffer_tag = x;
12590 list_add_tail(&dmabuf->list, &queue->page_list);
12591 /* initialize queue's entry array */
12592 dma_pointer = dmabuf->virt;
12593 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012594 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012595 total_qe_count++, dma_pointer += entry_size) {
12596 queue->qe[total_qe_count].address = dma_pointer;
12597 }
12598 }
12599 queue->entry_size = entry_size;
12600 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012601
12602 /*
12603 * entry_repost is calculated based on the number of entries in the
12604 * queue. This works out except for RQs. If buffers are NOT initially
12605 * posted for every RQE, entry_repost should be adjusted accordingly.
12606 */
12607 queue->entry_repost = (entry_count >> 3);
12608 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12609 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012610 queue->phba = phba;
12611
12612 return queue;
12613out_fail:
12614 lpfc_sli4_queue_free(queue);
12615 return NULL;
12616}
12617
12618/**
James Smart962bc512013-01-03 15:44:00 -050012619 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12620 * @phba: HBA structure that indicates port to create a queue on.
12621 * @pci_barset: PCI BAR set flag.
12622 *
12623 * This function shall perform iomap of the specified PCI BAR address to host
12624 * memory address if not already done so and return it. The returned host
12625 * memory address can be NULL.
12626 */
12627static void __iomem *
12628lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12629{
12630 struct pci_dev *pdev;
James Smart962bc512013-01-03 15:44:00 -050012631
12632 if (!phba->pcidev)
12633 return NULL;
12634 else
12635 pdev = phba->pcidev;
12636
12637 switch (pci_barset) {
12638 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050012639 return phba->pci_bar0_memmap_p;
12640 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050012641 return phba->pci_bar2_memmap_p;
12642 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050012643 return phba->pci_bar4_memmap_p;
12644 default:
12645 break;
12646 }
12647 return NULL;
12648}
12649
12650/**
James Smart173edbb2012-06-12 13:54:50 -040012651 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12652 * @phba: HBA structure that indicates port to create a queue on.
12653 * @startq: The starting FCP EQ to modify
12654 *
12655 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12656 *
12657 * The @phba struct is used to send mailbox command to HBA. The @startq
12658 * is used to get the starting FCP EQ to change.
12659 * This function is asynchronous and will wait for the mailbox
12660 * command to finish before continuing.
12661 *
12662 * On success this function will return a zero. If unable to allocate enough
12663 * memory this function will return -ENOMEM. If the queue create mailbox command
12664 * fails this function will return -ENXIO.
12665 **/
12666uint32_t
12667lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12668{
12669 struct lpfc_mbx_modify_eq_delay *eq_delay;
12670 LPFC_MBOXQ_t *mbox;
12671 struct lpfc_queue *eq;
12672 int cnt, rc, length, status = 0;
12673 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040012674 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040012675 int fcp_eqidx;
12676 union lpfc_sli4_cfg_shdr *shdr;
12677 uint16_t dmult;
12678
James Smart67d12732012-08-03 12:36:13 -040012679 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040012680 return 0;
12681
12682 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12683 if (!mbox)
12684 return -ENOMEM;
12685 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12686 sizeof(struct lpfc_sli4_cfg_mhdr));
12687 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12688 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12689 length, LPFC_SLI4_MBX_EMBED);
12690 eq_delay = &mbox->u.mqe.un.eq_delay;
12691
12692 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012693 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12694 if (result > LPFC_DMULT_CONST)
12695 dmult = 0;
12696 else
12697 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040012698
12699 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040012700 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040012701 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040012702 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040012703 if (!eq)
12704 continue;
12705 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12706 eq_delay->u.request.eq[cnt].phase = 0;
12707 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12708 cnt++;
12709 if (cnt >= LPFC_MAX_EQ_DELAY)
12710 break;
12711 }
12712 eq_delay->u.request.num_eq = cnt;
12713
12714 mbox->vport = phba->pport;
12715 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12716 mbox->context1 = NULL;
12717 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12718 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12719 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12720 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12721 if (shdr_status || shdr_add_status || rc) {
12722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12723 "2512 MODIFY_EQ_DELAY mailbox failed with "
12724 "status x%x add_status x%x, mbx status x%x\n",
12725 shdr_status, shdr_add_status, rc);
12726 status = -ENXIO;
12727 }
12728 mempool_free(mbox, phba->mbox_mem_pool);
12729 return status;
12730}
12731
12732/**
James Smart4f774512009-05-22 14:52:35 -040012733 * lpfc_eq_create - Create an Event Queue on the HBA
12734 * @phba: HBA structure that indicates port to create a queue on.
12735 * @eq: The queue structure to use to create the event queue.
12736 * @imax: The maximum interrupt per second limit.
12737 *
12738 * This function creates an event queue, as detailed in @eq, on a port,
12739 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12740 *
12741 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12742 * is used to get the entry count and entry size that are necessary to
12743 * determine the number of pages to allocate and use for this queue. This
12744 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12745 * event queue. This function is asynchronous and will wait for the mailbox
12746 * command to finish before continuing.
12747 *
12748 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012749 * memory this function will return -ENOMEM. If the queue create mailbox command
12750 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012751 **/
12752uint32_t
James Smartee020062012-09-29 11:28:52 -040012753lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040012754{
12755 struct lpfc_mbx_eq_create *eq_create;
12756 LPFC_MBOXQ_t *mbox;
12757 int rc, length, status = 0;
12758 struct lpfc_dmabuf *dmabuf;
12759 uint32_t shdr_status, shdr_add_status;
12760 union lpfc_sli4_cfg_shdr *shdr;
12761 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012762 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12763
James Smart2e90f4b2011-12-13 13:22:37 -050012764 /* sanity check on queue memory */
12765 if (!eq)
12766 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012767 if (!phba->sli4_hba.pc_sli4_params.supported)
12768 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012769
12770 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12771 if (!mbox)
12772 return -ENOMEM;
12773 length = (sizeof(struct lpfc_mbx_eq_create) -
12774 sizeof(struct lpfc_sli4_cfg_mhdr));
12775 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12776 LPFC_MBOX_OPCODE_EQ_CREATE,
12777 length, LPFC_SLI4_MBX_EMBED);
12778 eq_create = &mbox->u.mqe.un.eq_create;
12779 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12780 eq->page_count);
12781 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12782 LPFC_EQE_SIZE);
12783 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12784 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012785 if (imax > LPFC_DMULT_CONST)
12786 dmult = 0;
12787 else
12788 dmult = LPFC_DMULT_CONST/imax - 1;
James Smart4f774512009-05-22 14:52:35 -040012789 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12790 dmult);
12791 switch (eq->entry_count) {
12792 default:
12793 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12794 "0360 Unsupported EQ count. (%d)\n",
12795 eq->entry_count);
12796 if (eq->entry_count < 256)
12797 return -EINVAL;
12798 /* otherwise default to smallest count (drop through) */
12799 case 256:
12800 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12801 LPFC_EQ_CNT_256);
12802 break;
12803 case 512:
12804 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12805 LPFC_EQ_CNT_512);
12806 break;
12807 case 1024:
12808 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12809 LPFC_EQ_CNT_1024);
12810 break;
12811 case 2048:
12812 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12813 LPFC_EQ_CNT_2048);
12814 break;
12815 case 4096:
12816 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12817 LPFC_EQ_CNT_4096);
12818 break;
12819 }
12820 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012821 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012822 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12823 putPaddrLow(dmabuf->phys);
12824 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12825 putPaddrHigh(dmabuf->phys);
12826 }
12827 mbox->vport = phba->pport;
12828 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12829 mbox->context1 = NULL;
12830 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12831 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12832 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12833 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12834 if (shdr_status || shdr_add_status || rc) {
12835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12836 "2500 EQ_CREATE mailbox failed with "
12837 "status x%x add_status x%x, mbx status x%x\n",
12838 shdr_status, shdr_add_status, rc);
12839 status = -ENXIO;
12840 }
12841 eq->type = LPFC_EQ;
12842 eq->subtype = LPFC_NONE;
12843 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12844 if (eq->queue_id == 0xFFFF)
12845 status = -ENXIO;
12846 eq->host_index = 0;
12847 eq->hba_index = 0;
12848
James Smart8fa38512009-07-19 10:01:03 -040012849 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012850 return status;
12851}
12852
12853/**
12854 * lpfc_cq_create - Create a Completion Queue on the HBA
12855 * @phba: HBA structure that indicates port to create a queue on.
12856 * @cq: The queue structure to use to create the completion queue.
12857 * @eq: The event queue to bind this completion queue to.
12858 *
12859 * This function creates a completion queue, as detailed in @wq, on a port,
12860 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12861 *
12862 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12863 * is used to get the entry count and entry size that are necessary to
12864 * determine the number of pages to allocate and use for this queue. The @eq
12865 * is used to indicate which event queue to bind this completion queue to. This
12866 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12867 * completion queue. This function is asynchronous and will wait for the mailbox
12868 * command to finish before continuing.
12869 *
12870 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012871 * memory this function will return -ENOMEM. If the queue create mailbox command
12872 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012873 **/
12874uint32_t
12875lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12876 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12877{
12878 struct lpfc_mbx_cq_create *cq_create;
12879 struct lpfc_dmabuf *dmabuf;
12880 LPFC_MBOXQ_t *mbox;
12881 int rc, length, status = 0;
12882 uint32_t shdr_status, shdr_add_status;
12883 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012884 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12885
James Smart2e90f4b2011-12-13 13:22:37 -050012886 /* sanity check on queue memory */
12887 if (!cq || !eq)
12888 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012889 if (!phba->sli4_hba.pc_sli4_params.supported)
12890 hw_page_size = SLI4_PAGE_SIZE;
12891
James Smart4f774512009-05-22 14:52:35 -040012892 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12893 if (!mbox)
12894 return -ENOMEM;
12895 length = (sizeof(struct lpfc_mbx_cq_create) -
12896 sizeof(struct lpfc_sli4_cfg_mhdr));
12897 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12898 LPFC_MBOX_OPCODE_CQ_CREATE,
12899 length, LPFC_SLI4_MBX_EMBED);
12900 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012901 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012902 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12903 cq->page_count);
12904 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12905 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012906 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12907 phba->sli4_hba.pc_sli4_params.cqv);
12908 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012909 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12910 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012911 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12912 eq->queue_id);
12913 } else {
12914 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12915 eq->queue_id);
12916 }
James Smart4f774512009-05-22 14:52:35 -040012917 switch (cq->entry_count) {
12918 default:
12919 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12920 "0361 Unsupported CQ count. (%d)\n",
12921 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012922 if (cq->entry_count < 256) {
12923 status = -EINVAL;
12924 goto out;
12925 }
James Smart4f774512009-05-22 14:52:35 -040012926 /* otherwise default to smallest count (drop through) */
12927 case 256:
12928 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12929 LPFC_CQ_CNT_256);
12930 break;
12931 case 512:
12932 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12933 LPFC_CQ_CNT_512);
12934 break;
12935 case 1024:
12936 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12937 LPFC_CQ_CNT_1024);
12938 break;
12939 }
12940 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012941 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012942 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12943 putPaddrLow(dmabuf->phys);
12944 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12945 putPaddrHigh(dmabuf->phys);
12946 }
12947 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12948
12949 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012950 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12951 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12952 if (shdr_status || shdr_add_status || rc) {
12953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12954 "2501 CQ_CREATE mailbox failed with "
12955 "status x%x add_status x%x, mbx status x%x\n",
12956 shdr_status, shdr_add_status, rc);
12957 status = -ENXIO;
12958 goto out;
12959 }
12960 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12961 if (cq->queue_id == 0xFFFF) {
12962 status = -ENXIO;
12963 goto out;
12964 }
12965 /* link the cq onto the parent eq child list */
12966 list_add_tail(&cq->list, &eq->child_list);
12967 /* Set up completion queue's type and subtype */
12968 cq->type = type;
12969 cq->subtype = subtype;
12970 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012971 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012972 cq->host_index = 0;
12973 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012974
James Smart8fa38512009-07-19 10:01:03 -040012975out:
12976 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012977 return status;
12978}
12979
12980/**
James Smartb19a0612010-04-06 14:48:51 -040012981 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012982 * @phba: HBA structure that indicates port to create a queue on.
12983 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012984 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12985 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012986 *
James Smartb19a0612010-04-06 14:48:51 -040012987 * This function provides failback (fb) functionality when the
12988 * mq_create_ext fails on older FW generations. It's purpose is identical
12989 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012990 *
James Smartb19a0612010-04-06 14:48:51 -040012991 * This routine cannot fail as all attributes were previously accessed and
12992 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012993 **/
James Smartb19a0612010-04-06 14:48:51 -040012994static void
12995lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12996 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012997{
12998 struct lpfc_mbx_mq_create *mq_create;
12999 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013000 int length;
James Smart04c68492009-05-22 14:52:52 -040013001
James Smart04c68492009-05-22 14:52:52 -040013002 length = (sizeof(struct lpfc_mbx_mq_create) -
13003 sizeof(struct lpfc_sli4_cfg_mhdr));
13004 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13005 LPFC_MBOX_OPCODE_MQ_CREATE,
13006 length, LPFC_SLI4_MBX_EMBED);
13007 mq_create = &mbox->u.mqe.un.mq_create;
13008 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013009 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013010 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013011 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013012 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13013 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013014 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013015 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13016 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013017 break;
13018 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013019 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13020 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013021 break;
13022 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013023 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13024 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013025 break;
13026 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013027 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13028 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013029 break;
13030 }
13031 list_for_each_entry(dmabuf, &mq->page_list, list) {
13032 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013033 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013034 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013035 putPaddrHigh(dmabuf->phys);
13036 }
13037}
13038
13039/**
13040 * lpfc_mq_create - Create a mailbox Queue on the HBA
13041 * @phba: HBA structure that indicates port to create a queue on.
13042 * @mq: The queue structure to use to create the mailbox queue.
13043 * @cq: The completion queue to associate with this cq.
13044 * @subtype: The queue's subtype.
13045 *
13046 * This function creates a mailbox queue, as detailed in @mq, on a port,
13047 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13048 *
13049 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13050 * is used to get the entry count and entry size that are necessary to
13051 * determine the number of pages to allocate and use for this queue. This
13052 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13053 * mailbox queue. This function is asynchronous and will wait for the mailbox
13054 * command to finish before continuing.
13055 *
13056 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013057 * memory this function will return -ENOMEM. If the queue create mailbox command
13058 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013059 **/
13060int32_t
13061lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13062 struct lpfc_queue *cq, uint32_t subtype)
13063{
13064 struct lpfc_mbx_mq_create *mq_create;
13065 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13066 struct lpfc_dmabuf *dmabuf;
13067 LPFC_MBOXQ_t *mbox;
13068 int rc, length, status = 0;
13069 uint32_t shdr_status, shdr_add_status;
13070 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013071 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013072
James Smart2e90f4b2011-12-13 13:22:37 -050013073 /* sanity check on queue memory */
13074 if (!mq || !cq)
13075 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013076 if (!phba->sli4_hba.pc_sli4_params.supported)
13077 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013078
13079 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13080 if (!mbox)
13081 return -ENOMEM;
13082 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13083 sizeof(struct lpfc_sli4_cfg_mhdr));
13084 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13085 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13086 length, LPFC_SLI4_MBX_EMBED);
13087
13088 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013089 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013090 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13091 &mq_create_ext->u.request, mq->page_count);
13092 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13093 &mq_create_ext->u.request, 1);
13094 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013095 &mq_create_ext->u.request, 1);
13096 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13097 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013098 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13099 &mq_create_ext->u.request, 1);
13100 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13101 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013102 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013103 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13104 phba->sli4_hba.pc_sli4_params.mqv);
13105 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13106 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13107 cq->queue_id);
13108 else
13109 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13110 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013111 switch (mq->entry_count) {
13112 default:
13113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13114 "0362 Unsupported MQ count. (%d)\n",
13115 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013116 if (mq->entry_count < 16) {
13117 status = -EINVAL;
13118 goto out;
13119 }
James Smartb19a0612010-04-06 14:48:51 -040013120 /* otherwise default to smallest count (drop through) */
13121 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013122 bf_set(lpfc_mq_context_ring_size,
13123 &mq_create_ext->u.request.context,
13124 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013125 break;
13126 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013127 bf_set(lpfc_mq_context_ring_size,
13128 &mq_create_ext->u.request.context,
13129 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013130 break;
13131 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013132 bf_set(lpfc_mq_context_ring_size,
13133 &mq_create_ext->u.request.context,
13134 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013135 break;
13136 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013137 bf_set(lpfc_mq_context_ring_size,
13138 &mq_create_ext->u.request.context,
13139 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013140 break;
13141 }
13142 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013143 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013144 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13145 putPaddrLow(dmabuf->phys);
13146 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013147 putPaddrHigh(dmabuf->phys);
13148 }
13149 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013150 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13151 &mq_create_ext->u.response);
13152 if (rc != MBX_SUCCESS) {
13153 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13154 "2795 MQ_CREATE_EXT failed with "
13155 "status x%x. Failback to MQ_CREATE.\n",
13156 rc);
13157 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13158 mq_create = &mbox->u.mqe.un.mq_create;
13159 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13160 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13161 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13162 &mq_create->u.response);
13163 }
13164
James Smart04c68492009-05-22 14:52:52 -040013165 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013166 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13167 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13168 if (shdr_status || shdr_add_status || rc) {
13169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13170 "2502 MQ_CREATE mailbox failed with "
13171 "status x%x add_status x%x, mbx status x%x\n",
13172 shdr_status, shdr_add_status, rc);
13173 status = -ENXIO;
13174 goto out;
13175 }
James Smart04c68492009-05-22 14:52:52 -040013176 if (mq->queue_id == 0xFFFF) {
13177 status = -ENXIO;
13178 goto out;
13179 }
13180 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013181 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013182 mq->subtype = subtype;
13183 mq->host_index = 0;
13184 mq->hba_index = 0;
13185
13186 /* link the mq onto the parent cq child list */
13187 list_add_tail(&mq->list, &cq->child_list);
13188out:
James Smart8fa38512009-07-19 10:01:03 -040013189 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013190 return status;
13191}
13192
13193/**
James Smart4f774512009-05-22 14:52:35 -040013194 * lpfc_wq_create - Create a Work Queue on the HBA
13195 * @phba: HBA structure that indicates port to create a queue on.
13196 * @wq: The queue structure to use to create the work queue.
13197 * @cq: The completion queue to bind this work queue to.
13198 * @subtype: The subtype of the work queue indicating its functionality.
13199 *
13200 * This function creates a work queue, as detailed in @wq, on a port, described
13201 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13202 *
13203 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13204 * is used to get the entry count and entry size that are necessary to
13205 * determine the number of pages to allocate and use for this queue. The @cq
13206 * is used to indicate which completion queue to bind this work queue to. This
13207 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13208 * work queue. This function is asynchronous and will wait for the mailbox
13209 * command to finish before continuing.
13210 *
13211 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013212 * memory this function will return -ENOMEM. If the queue create mailbox command
13213 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013214 **/
13215uint32_t
13216lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13217 struct lpfc_queue *cq, uint32_t subtype)
13218{
13219 struct lpfc_mbx_wq_create *wq_create;
13220 struct lpfc_dmabuf *dmabuf;
13221 LPFC_MBOXQ_t *mbox;
13222 int rc, length, status = 0;
13223 uint32_t shdr_status, shdr_add_status;
13224 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013225 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013226 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013227 void __iomem *bar_memmap_p;
13228 uint32_t db_offset;
13229 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013230
James Smart2e90f4b2011-12-13 13:22:37 -050013231 /* sanity check on queue memory */
13232 if (!wq || !cq)
13233 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013234 if (!phba->sli4_hba.pc_sli4_params.supported)
13235 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013236
13237 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13238 if (!mbox)
13239 return -ENOMEM;
13240 length = (sizeof(struct lpfc_mbx_wq_create) -
13241 sizeof(struct lpfc_sli4_cfg_mhdr));
13242 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13243 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13244 length, LPFC_SLI4_MBX_EMBED);
13245 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013246 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013247 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13248 wq->page_count);
13249 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13250 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013251
13252 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013253 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13254 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013255
James Smart0c651872013-07-15 18:33:23 -040013256 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13257 case LPFC_Q_CREATE_VERSION_0:
13258 switch (wq->entry_size) {
13259 default:
13260 case 64:
13261 /* Nothing to do, version 0 ONLY supports 64 byte */
13262 page = wq_create->u.request.page;
13263 break;
13264 case 128:
13265 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13266 LPFC_WQ_SZ128_SUPPORT)) {
13267 status = -ERANGE;
13268 goto out;
13269 }
13270 /* If we get here the HBA MUST also support V1 and
13271 * we MUST use it
13272 */
13273 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13274 LPFC_Q_CREATE_VERSION_1);
13275
13276 bf_set(lpfc_mbx_wq_create_wqe_count,
13277 &wq_create->u.request_1, wq->entry_count);
13278 bf_set(lpfc_mbx_wq_create_wqe_size,
13279 &wq_create->u.request_1,
13280 LPFC_WQ_WQE_SIZE_128);
13281 bf_set(lpfc_mbx_wq_create_page_size,
13282 &wq_create->u.request_1,
13283 (PAGE_SIZE/SLI4_PAGE_SIZE));
13284 page = wq_create->u.request_1.page;
13285 break;
13286 }
13287 break;
13288 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013289 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13290 wq->entry_count);
13291 switch (wq->entry_size) {
13292 default:
13293 case 64:
13294 bf_set(lpfc_mbx_wq_create_wqe_size,
13295 &wq_create->u.request_1,
13296 LPFC_WQ_WQE_SIZE_64);
13297 break;
13298 case 128:
James Smart0c651872013-07-15 18:33:23 -040013299 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13300 LPFC_WQ_SZ128_SUPPORT)) {
13301 status = -ERANGE;
13302 goto out;
13303 }
James Smart5a6f1332011-03-11 16:05:35 -050013304 bf_set(lpfc_mbx_wq_create_wqe_size,
13305 &wq_create->u.request_1,
13306 LPFC_WQ_WQE_SIZE_128);
13307 break;
13308 }
13309 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13310 (PAGE_SIZE/SLI4_PAGE_SIZE));
13311 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013312 break;
13313 default:
13314 status = -ERANGE;
13315 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013316 }
James Smart0c651872013-07-15 18:33:23 -040013317
James Smart4f774512009-05-22 14:52:35 -040013318 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013319 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013320 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13321 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013322 }
James Smart962bc512013-01-03 15:44:00 -050013323
13324 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13325 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13326
James Smart4f774512009-05-22 14:52:35 -040013327 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13328 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013329 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13330 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13331 if (shdr_status || shdr_add_status || rc) {
13332 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13333 "2503 WQ_CREATE mailbox failed with "
13334 "status x%x add_status x%x, mbx status x%x\n",
13335 shdr_status, shdr_add_status, rc);
13336 status = -ENXIO;
13337 goto out;
13338 }
13339 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13340 if (wq->queue_id == 0xFFFF) {
13341 status = -ENXIO;
13342 goto out;
13343 }
James Smart962bc512013-01-03 15:44:00 -050013344 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13345 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13346 &wq_create->u.response);
13347 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13348 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13350 "3265 WQ[%d] doorbell format not "
13351 "supported: x%x\n", wq->queue_id,
13352 wq->db_format);
13353 status = -EINVAL;
13354 goto out;
13355 }
13356 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13357 &wq_create->u.response);
13358 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13359 if (!bar_memmap_p) {
13360 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13361 "3263 WQ[%d] failed to memmap pci "
13362 "barset:x%x\n", wq->queue_id,
13363 pci_barset);
13364 status = -ENOMEM;
13365 goto out;
13366 }
13367 db_offset = wq_create->u.response.doorbell_offset;
13368 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13369 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13371 "3252 WQ[%d] doorbell offset not "
13372 "supported: x%x\n", wq->queue_id,
13373 db_offset);
13374 status = -EINVAL;
13375 goto out;
13376 }
13377 wq->db_regaddr = bar_memmap_p + db_offset;
13378 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013379 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13380 "format:x%x\n", wq->queue_id, pci_barset,
13381 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013382 } else {
13383 wq->db_format = LPFC_DB_LIST_FORMAT;
13384 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13385 }
James Smart4f774512009-05-22 14:52:35 -040013386 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013387 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013388 wq->subtype = subtype;
13389 wq->host_index = 0;
13390 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013391 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013392
13393 /* link the wq onto the parent cq child list */
13394 list_add_tail(&wq->list, &cq->child_list);
13395out:
James Smart8fa38512009-07-19 10:01:03 -040013396 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013397 return status;
13398}
13399
13400/**
James Smart73d91e52011-10-10 21:32:10 -040013401 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13402 * @phba: HBA structure that indicates port to create a queue on.
13403 * @rq: The queue structure to use for the receive queue.
13404 * @qno: The associated HBQ number
13405 *
13406 *
13407 * For SLI4 we need to adjust the RQ repost value based on
13408 * the number of buffers that are initially posted to the RQ.
13409 */
13410void
13411lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13412{
13413 uint32_t cnt;
13414
James Smart2e90f4b2011-12-13 13:22:37 -050013415 /* sanity check on queue memory */
13416 if (!rq)
13417 return;
James Smart73d91e52011-10-10 21:32:10 -040013418 cnt = lpfc_hbq_defs[qno]->entry_count;
13419
13420 /* Recalc repost for RQs based on buffers initially posted */
13421 cnt = (cnt >> 3);
13422 if (cnt < LPFC_QUEUE_MIN_REPOST)
13423 cnt = LPFC_QUEUE_MIN_REPOST;
13424
13425 rq->entry_repost = cnt;
13426}
13427
13428/**
James Smart4f774512009-05-22 14:52:35 -040013429 * lpfc_rq_create - Create a Receive Queue on the HBA
13430 * @phba: HBA structure that indicates port to create a queue on.
13431 * @hrq: The queue structure to use to create the header receive queue.
13432 * @drq: The queue structure to use to create the data receive queue.
13433 * @cq: The completion queue to bind this work queue to.
13434 *
13435 * This function creates a receive buffer queue pair , as detailed in @hrq and
13436 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13437 * to the HBA.
13438 *
13439 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13440 * struct is used to get the entry count that is necessary to determine the
13441 * number of pages to use for this queue. The @cq is used to indicate which
13442 * completion queue to bind received buffers that are posted to these queues to.
13443 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13444 * receive queue pair. This function is asynchronous and will wait for the
13445 * mailbox command to finish before continuing.
13446 *
13447 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013448 * memory this function will return -ENOMEM. If the queue create mailbox command
13449 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013450 **/
13451uint32_t
13452lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13453 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13454{
13455 struct lpfc_mbx_rq_create *rq_create;
13456 struct lpfc_dmabuf *dmabuf;
13457 LPFC_MBOXQ_t *mbox;
13458 int rc, length, status = 0;
13459 uint32_t shdr_status, shdr_add_status;
13460 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013461 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013462 void __iomem *bar_memmap_p;
13463 uint32_t db_offset;
13464 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013465
James Smart2e90f4b2011-12-13 13:22:37 -050013466 /* sanity check on queue memory */
13467 if (!hrq || !drq || !cq)
13468 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013469 if (!phba->sli4_hba.pc_sli4_params.supported)
13470 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013471
13472 if (hrq->entry_count != drq->entry_count)
13473 return -EINVAL;
13474 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13475 if (!mbox)
13476 return -ENOMEM;
13477 length = (sizeof(struct lpfc_mbx_rq_create) -
13478 sizeof(struct lpfc_sli4_cfg_mhdr));
13479 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13480 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13481 length, LPFC_SLI4_MBX_EMBED);
13482 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013483 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13484 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13485 phba->sli4_hba.pc_sli4_params.rqv);
13486 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13487 bf_set(lpfc_rq_context_rqe_count_1,
13488 &rq_create->u.request.context,
13489 hrq->entry_count);
13490 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013491 bf_set(lpfc_rq_context_rqe_size,
13492 &rq_create->u.request.context,
13493 LPFC_RQE_SIZE_8);
13494 bf_set(lpfc_rq_context_page_size,
13495 &rq_create->u.request.context,
13496 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013497 } else {
13498 switch (hrq->entry_count) {
13499 default:
13500 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13501 "2535 Unsupported RQ count. (%d)\n",
13502 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013503 if (hrq->entry_count < 512) {
13504 status = -EINVAL;
13505 goto out;
13506 }
James Smart5a6f1332011-03-11 16:05:35 -050013507 /* otherwise default to smallest count (drop through) */
13508 case 512:
13509 bf_set(lpfc_rq_context_rqe_count,
13510 &rq_create->u.request.context,
13511 LPFC_RQ_RING_SIZE_512);
13512 break;
13513 case 1024:
13514 bf_set(lpfc_rq_context_rqe_count,
13515 &rq_create->u.request.context,
13516 LPFC_RQ_RING_SIZE_1024);
13517 break;
13518 case 2048:
13519 bf_set(lpfc_rq_context_rqe_count,
13520 &rq_create->u.request.context,
13521 LPFC_RQ_RING_SIZE_2048);
13522 break;
13523 case 4096:
13524 bf_set(lpfc_rq_context_rqe_count,
13525 &rq_create->u.request.context,
13526 LPFC_RQ_RING_SIZE_4096);
13527 break;
13528 }
13529 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13530 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013531 }
13532 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13533 cq->queue_id);
13534 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13535 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013536 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013537 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013538 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13539 putPaddrLow(dmabuf->phys);
13540 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13541 putPaddrHigh(dmabuf->phys);
13542 }
James Smart962bc512013-01-03 15:44:00 -050013543 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13544 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13545
James Smart4f774512009-05-22 14:52:35 -040013546 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13547 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013548 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13549 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13550 if (shdr_status || shdr_add_status || rc) {
13551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13552 "2504 RQ_CREATE mailbox failed with "
13553 "status x%x add_status x%x, mbx status x%x\n",
13554 shdr_status, shdr_add_status, rc);
13555 status = -ENXIO;
13556 goto out;
13557 }
13558 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13559 if (hrq->queue_id == 0xFFFF) {
13560 status = -ENXIO;
13561 goto out;
13562 }
James Smart962bc512013-01-03 15:44:00 -050013563
13564 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13565 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13566 &rq_create->u.response);
13567 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13568 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13570 "3262 RQ [%d] doorbell format not "
13571 "supported: x%x\n", hrq->queue_id,
13572 hrq->db_format);
13573 status = -EINVAL;
13574 goto out;
13575 }
13576
13577 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13578 &rq_create->u.response);
13579 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13580 if (!bar_memmap_p) {
13581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13582 "3269 RQ[%d] failed to memmap pci "
13583 "barset:x%x\n", hrq->queue_id,
13584 pci_barset);
13585 status = -ENOMEM;
13586 goto out;
13587 }
13588
13589 db_offset = rq_create->u.response.doorbell_offset;
13590 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13591 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13593 "3270 RQ[%d] doorbell offset not "
13594 "supported: x%x\n", hrq->queue_id,
13595 db_offset);
13596 status = -EINVAL;
13597 goto out;
13598 }
13599 hrq->db_regaddr = bar_memmap_p + db_offset;
13600 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013601 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13602 "format:x%x\n", hrq->queue_id, pci_barset,
13603 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013604 } else {
13605 hrq->db_format = LPFC_DB_RING_FORMAT;
13606 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13607 }
James Smart4f774512009-05-22 14:52:35 -040013608 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013609 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013610 hrq->subtype = subtype;
13611 hrq->host_index = 0;
13612 hrq->hba_index = 0;
13613
13614 /* now create the data queue */
13615 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13616 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13617 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050013618 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13619 phba->sli4_hba.pc_sli4_params.rqv);
13620 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13621 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040013622 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050013623 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013624 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13625 LPFC_RQE_SIZE_8);
13626 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13627 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013628 } else {
13629 switch (drq->entry_count) {
13630 default:
13631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13632 "2536 Unsupported RQ count. (%d)\n",
13633 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013634 if (drq->entry_count < 512) {
13635 status = -EINVAL;
13636 goto out;
13637 }
James Smart5a6f1332011-03-11 16:05:35 -050013638 /* otherwise default to smallest count (drop through) */
13639 case 512:
13640 bf_set(lpfc_rq_context_rqe_count,
13641 &rq_create->u.request.context,
13642 LPFC_RQ_RING_SIZE_512);
13643 break;
13644 case 1024:
13645 bf_set(lpfc_rq_context_rqe_count,
13646 &rq_create->u.request.context,
13647 LPFC_RQ_RING_SIZE_1024);
13648 break;
13649 case 2048:
13650 bf_set(lpfc_rq_context_rqe_count,
13651 &rq_create->u.request.context,
13652 LPFC_RQ_RING_SIZE_2048);
13653 break;
13654 case 4096:
13655 bf_set(lpfc_rq_context_rqe_count,
13656 &rq_create->u.request.context,
13657 LPFC_RQ_RING_SIZE_4096);
13658 break;
13659 }
13660 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13661 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013662 }
13663 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13664 cq->queue_id);
13665 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13666 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013667 list_for_each_entry(dmabuf, &drq->page_list, list) {
13668 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13669 putPaddrLow(dmabuf->phys);
13670 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13671 putPaddrHigh(dmabuf->phys);
13672 }
James Smart962bc512013-01-03 15:44:00 -050013673 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13674 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040013675 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13676 /* The IOCTL status is embedded in the mailbox subheader. */
13677 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13678 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13679 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13680 if (shdr_status || shdr_add_status || rc) {
13681 status = -ENXIO;
13682 goto out;
13683 }
13684 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13685 if (drq->queue_id == 0xFFFF) {
13686 status = -ENXIO;
13687 goto out;
13688 }
13689 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013690 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013691 drq->subtype = subtype;
13692 drq->host_index = 0;
13693 drq->hba_index = 0;
13694
13695 /* link the header and data RQs onto the parent cq child list */
13696 list_add_tail(&hrq->list, &cq->child_list);
13697 list_add_tail(&drq->list, &cq->child_list);
13698
13699out:
James Smart8fa38512009-07-19 10:01:03 -040013700 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013701 return status;
13702}
13703
13704/**
13705 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13706 * @eq: The queue structure associated with the queue to destroy.
13707 *
13708 * This function destroys a queue, as detailed in @eq by sending an mailbox
13709 * command, specific to the type of queue, to the HBA.
13710 *
13711 * The @eq struct is used to get the queue ID of the queue to destroy.
13712 *
13713 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013714 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013715 **/
13716uint32_t
13717lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13718{
13719 LPFC_MBOXQ_t *mbox;
13720 int rc, length, status = 0;
13721 uint32_t shdr_status, shdr_add_status;
13722 union lpfc_sli4_cfg_shdr *shdr;
13723
James Smart2e90f4b2011-12-13 13:22:37 -050013724 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013725 if (!eq)
13726 return -ENODEV;
13727 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13728 if (!mbox)
13729 return -ENOMEM;
13730 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13731 sizeof(struct lpfc_sli4_cfg_mhdr));
13732 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13733 LPFC_MBOX_OPCODE_EQ_DESTROY,
13734 length, LPFC_SLI4_MBX_EMBED);
13735 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13736 eq->queue_id);
13737 mbox->vport = eq->phba->pport;
13738 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13739
13740 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13741 /* The IOCTL status is embedded in the mailbox subheader. */
13742 shdr = (union lpfc_sli4_cfg_shdr *)
13743 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13744 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13745 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13746 if (shdr_status || shdr_add_status || rc) {
13747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13748 "2505 EQ_DESTROY mailbox failed with "
13749 "status x%x add_status x%x, mbx status x%x\n",
13750 shdr_status, shdr_add_status, rc);
13751 status = -ENXIO;
13752 }
13753
13754 /* Remove eq from any list */
13755 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013756 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013757 return status;
13758}
13759
13760/**
13761 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13762 * @cq: The queue structure associated with the queue to destroy.
13763 *
13764 * This function destroys a queue, as detailed in @cq by sending an mailbox
13765 * command, specific to the type of queue, to the HBA.
13766 *
13767 * The @cq struct is used to get the queue ID of the queue to destroy.
13768 *
13769 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013770 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013771 **/
13772uint32_t
13773lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13774{
13775 LPFC_MBOXQ_t *mbox;
13776 int rc, length, status = 0;
13777 uint32_t shdr_status, shdr_add_status;
13778 union lpfc_sli4_cfg_shdr *shdr;
13779
James Smart2e90f4b2011-12-13 13:22:37 -050013780 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013781 if (!cq)
13782 return -ENODEV;
13783 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13784 if (!mbox)
13785 return -ENOMEM;
13786 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13787 sizeof(struct lpfc_sli4_cfg_mhdr));
13788 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13789 LPFC_MBOX_OPCODE_CQ_DESTROY,
13790 length, LPFC_SLI4_MBX_EMBED);
13791 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13792 cq->queue_id);
13793 mbox->vport = cq->phba->pport;
13794 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13795 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13796 /* The IOCTL status is embedded in the mailbox subheader. */
13797 shdr = (union lpfc_sli4_cfg_shdr *)
13798 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13799 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13800 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13801 if (shdr_status || shdr_add_status || rc) {
13802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13803 "2506 CQ_DESTROY mailbox failed with "
13804 "status x%x add_status x%x, mbx status x%x\n",
13805 shdr_status, shdr_add_status, rc);
13806 status = -ENXIO;
13807 }
13808 /* Remove cq from any list */
13809 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013810 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013811 return status;
13812}
13813
13814/**
James Smart04c68492009-05-22 14:52:52 -040013815 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13816 * @qm: The queue structure associated with the queue to destroy.
13817 *
13818 * This function destroys a queue, as detailed in @mq by sending an mailbox
13819 * command, specific to the type of queue, to the HBA.
13820 *
13821 * The @mq struct is used to get the queue ID of the queue to destroy.
13822 *
13823 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013824 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013825 **/
13826uint32_t
13827lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13828{
13829 LPFC_MBOXQ_t *mbox;
13830 int rc, length, status = 0;
13831 uint32_t shdr_status, shdr_add_status;
13832 union lpfc_sli4_cfg_shdr *shdr;
13833
James Smart2e90f4b2011-12-13 13:22:37 -050013834 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013835 if (!mq)
13836 return -ENODEV;
13837 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13838 if (!mbox)
13839 return -ENOMEM;
13840 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13841 sizeof(struct lpfc_sli4_cfg_mhdr));
13842 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13843 LPFC_MBOX_OPCODE_MQ_DESTROY,
13844 length, LPFC_SLI4_MBX_EMBED);
13845 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13846 mq->queue_id);
13847 mbox->vport = mq->phba->pport;
13848 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13849 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13850 /* The IOCTL status is embedded in the mailbox subheader. */
13851 shdr = (union lpfc_sli4_cfg_shdr *)
13852 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13853 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13854 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13855 if (shdr_status || shdr_add_status || rc) {
13856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13857 "2507 MQ_DESTROY mailbox failed with "
13858 "status x%x add_status x%x, mbx status x%x\n",
13859 shdr_status, shdr_add_status, rc);
13860 status = -ENXIO;
13861 }
13862 /* Remove mq from any list */
13863 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013864 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013865 return status;
13866}
13867
13868/**
James Smart4f774512009-05-22 14:52:35 -040013869 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13870 * @wq: The queue structure associated with the queue to destroy.
13871 *
13872 * This function destroys a queue, as detailed in @wq by sending an mailbox
13873 * command, specific to the type of queue, to the HBA.
13874 *
13875 * The @wq struct is used to get the queue ID of the queue to destroy.
13876 *
13877 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013878 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013879 **/
13880uint32_t
13881lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13882{
13883 LPFC_MBOXQ_t *mbox;
13884 int rc, length, status = 0;
13885 uint32_t shdr_status, shdr_add_status;
13886 union lpfc_sli4_cfg_shdr *shdr;
13887
James Smart2e90f4b2011-12-13 13:22:37 -050013888 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013889 if (!wq)
13890 return -ENODEV;
13891 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13892 if (!mbox)
13893 return -ENOMEM;
13894 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13895 sizeof(struct lpfc_sli4_cfg_mhdr));
13896 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13897 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13898 length, LPFC_SLI4_MBX_EMBED);
13899 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13900 wq->queue_id);
13901 mbox->vport = wq->phba->pport;
13902 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13903 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13904 shdr = (union lpfc_sli4_cfg_shdr *)
13905 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13906 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13907 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13908 if (shdr_status || shdr_add_status || rc) {
13909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13910 "2508 WQ_DESTROY mailbox failed with "
13911 "status x%x add_status x%x, mbx status x%x\n",
13912 shdr_status, shdr_add_status, rc);
13913 status = -ENXIO;
13914 }
13915 /* Remove wq from any list */
13916 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013917 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013918 return status;
13919}
13920
13921/**
13922 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13923 * @rq: The queue structure associated with the queue to destroy.
13924 *
13925 * This function destroys a queue, as detailed in @rq by sending an mailbox
13926 * command, specific to the type of queue, to the HBA.
13927 *
13928 * The @rq struct is used to get the queue ID of the queue to destroy.
13929 *
13930 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013931 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013932 **/
13933uint32_t
13934lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13935 struct lpfc_queue *drq)
13936{
13937 LPFC_MBOXQ_t *mbox;
13938 int rc, length, status = 0;
13939 uint32_t shdr_status, shdr_add_status;
13940 union lpfc_sli4_cfg_shdr *shdr;
13941
James Smart2e90f4b2011-12-13 13:22:37 -050013942 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013943 if (!hrq || !drq)
13944 return -ENODEV;
13945 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13946 if (!mbox)
13947 return -ENOMEM;
13948 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013949 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013950 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13951 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13952 length, LPFC_SLI4_MBX_EMBED);
13953 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13954 hrq->queue_id);
13955 mbox->vport = hrq->phba->pport;
13956 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13957 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13958 /* The IOCTL status is embedded in the mailbox subheader. */
13959 shdr = (union lpfc_sli4_cfg_shdr *)
13960 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13961 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13962 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13963 if (shdr_status || shdr_add_status || rc) {
13964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13965 "2509 RQ_DESTROY mailbox failed with "
13966 "status x%x add_status x%x, mbx status x%x\n",
13967 shdr_status, shdr_add_status, rc);
13968 if (rc != MBX_TIMEOUT)
13969 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13970 return -ENXIO;
13971 }
13972 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13973 drq->queue_id);
13974 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13975 shdr = (union lpfc_sli4_cfg_shdr *)
13976 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13977 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13978 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13979 if (shdr_status || shdr_add_status || rc) {
13980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13981 "2510 RQ_DESTROY mailbox failed with "
13982 "status x%x add_status x%x, mbx status x%x\n",
13983 shdr_status, shdr_add_status, rc);
13984 status = -ENXIO;
13985 }
13986 list_del_init(&hrq->list);
13987 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013988 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013989 return status;
13990}
13991
13992/**
13993 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13994 * @phba: The virtual port for which this call being executed.
13995 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13996 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13997 * @xritag: the xritag that ties this io to the SGL pages.
13998 *
13999 * This routine will post the sgl pages for the IO that has the xritag
14000 * that is in the iocbq structure. The xritag is assigned during iocbq
14001 * creation and persists for as long as the driver is loaded.
14002 * if the caller has fewer than 256 scatter gather segments to map then
14003 * pdma_phys_addr1 should be 0.
14004 * If the caller needs to map more than 256 scatter gather segment then
14005 * pdma_phys_addr1 should be a valid physical address.
14006 * physical address for SGLs must be 64 byte aligned.
14007 * If you are going to map 2 SGL's then the first one must have 256 entries
14008 * the second sgl can have between 1 and 256 entries.
14009 *
14010 * Return codes:
14011 * 0 - Success
14012 * -ENXIO, -ENOMEM - Failure
14013 **/
14014int
14015lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14016 dma_addr_t pdma_phys_addr0,
14017 dma_addr_t pdma_phys_addr1,
14018 uint16_t xritag)
14019{
14020 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14021 LPFC_MBOXQ_t *mbox;
14022 int rc;
14023 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014024 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014025 union lpfc_sli4_cfg_shdr *shdr;
14026
14027 if (xritag == NO_XRI) {
14028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14029 "0364 Invalid param:\n");
14030 return -EINVAL;
14031 }
14032
14033 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14034 if (!mbox)
14035 return -ENOMEM;
14036
14037 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14038 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14039 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014040 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014041
14042 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14043 &mbox->u.mqe.un.post_sgl_pages;
14044 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14045 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14046
14047 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14048 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14049 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14050 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14051
14052 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14053 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14054 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14055 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14056 if (!phba->sli4_hba.intr_enable)
14057 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014058 else {
James Smarta183a152011-10-10 21:32:43 -040014059 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014060 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14061 }
James Smart4f774512009-05-22 14:52:35 -040014062 /* The IOCTL status is embedded in the mailbox subheader. */
14063 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14064 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14065 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14066 if (rc != MBX_TIMEOUT)
14067 mempool_free(mbox, phba->mbox_mem_pool);
14068 if (shdr_status || shdr_add_status || rc) {
14069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14070 "2511 POST_SGL mailbox failed with "
14071 "status x%x add_status x%x, mbx status x%x\n",
14072 shdr_status, shdr_add_status, rc);
14073 rc = -ENXIO;
14074 }
14075 return 0;
14076}
James Smart4f774512009-05-22 14:52:35 -040014077
14078/**
James Smart88a2cfb2011-07-22 18:36:33 -040014079 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014080 * @phba: pointer to lpfc hba data structure.
14081 *
14082 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014083 * HBA consistent with the SLI-4 interface spec. This routine
14084 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14085 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014086 *
James Smart88a2cfb2011-07-22 18:36:33 -040014087 * Returns
14088 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14089 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14090 **/
James Smart6d368e52011-05-24 11:44:12 -040014091uint16_t
14092lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14093{
14094 unsigned long xri;
14095
14096 /*
14097 * Fetch the next logical xri. Because this index is logical,
14098 * the driver starts at 0 each time.
14099 */
14100 spin_lock_irq(&phba->hbalock);
14101 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14102 phba->sli4_hba.max_cfg_param.max_xri, 0);
14103 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14104 spin_unlock_irq(&phba->hbalock);
14105 return NO_XRI;
14106 } else {
14107 set_bit(xri, phba->sli4_hba.xri_bmask);
14108 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014109 }
James Smart6d368e52011-05-24 11:44:12 -040014110 spin_unlock_irq(&phba->hbalock);
14111 return xri;
14112}
14113
14114/**
14115 * lpfc_sli4_free_xri - Release an xri for reuse.
14116 * @phba: pointer to lpfc hba data structure.
14117 *
14118 * This routine is invoked to release an xri to the pool of
14119 * available rpis maintained by the driver.
14120 **/
14121void
14122__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14123{
14124 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014125 phba->sli4_hba.max_cfg_param.xri_used--;
14126 }
14127}
14128
14129/**
14130 * lpfc_sli4_free_xri - Release an xri for reuse.
14131 * @phba: pointer to lpfc hba data structure.
14132 *
14133 * This routine is invoked to release an xri to the pool of
14134 * available rpis maintained by the driver.
14135 **/
14136void
14137lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14138{
14139 spin_lock_irq(&phba->hbalock);
14140 __lpfc_sli4_free_xri(phba, xri);
14141 spin_unlock_irq(&phba->hbalock);
14142}
14143
14144/**
James Smart4f774512009-05-22 14:52:35 -040014145 * lpfc_sli4_next_xritag - Get an xritag for the io
14146 * @phba: Pointer to HBA context object.
14147 *
14148 * This function gets an xritag for the iocb. If there is no unused xritag
14149 * it will return 0xffff.
14150 * The function returns the allocated xritag if successful, else returns zero.
14151 * Zero is not a valid xritag.
14152 * The caller is not required to hold any lock.
14153 **/
14154uint16_t
14155lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14156{
James Smart6d368e52011-05-24 11:44:12 -040014157 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014158
James Smart6d368e52011-05-24 11:44:12 -040014159 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014160 if (xri_index == NO_XRI)
14161 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14162 "2004 Failed to allocate XRI.last XRITAG is %d"
14163 " Max XRI is %d, Used XRI is %d\n",
14164 xri_index,
14165 phba->sli4_hba.max_cfg_param.max_xri,
14166 phba->sli4_hba.max_cfg_param.xri_used);
14167 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014168}
14169
14170/**
James Smart6d368e52011-05-24 11:44:12 -040014171 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014172 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014173 * @post_sgl_list: pointer to els sgl entry list.
14174 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014175 *
14176 * This routine is invoked to post a block of driver's sgl pages to the
14177 * HBA using non-embedded mailbox command. No Lock is held. This routine
14178 * is only called when the driver is loading and after all IO has been
14179 * stopped.
14180 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014181static int
14182lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14183 struct list_head *post_sgl_list,
14184 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014185{
James Smart8a9d2e82012-05-09 21:16:12 -040014186 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014187 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14188 struct sgl_page_pairs *sgl_pg_pairs;
14189 void *viraddr;
14190 LPFC_MBOXQ_t *mbox;
14191 uint32_t reqlen, alloclen, pg_pairs;
14192 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014193 uint16_t xritag_start = 0;
14194 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014195 uint32_t shdr_status, shdr_add_status;
14196 union lpfc_sli4_cfg_shdr *shdr;
14197
James Smart8a9d2e82012-05-09 21:16:12 -040014198 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014199 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014200 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014201 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14202 "2559 Block sgl registration required DMA "
14203 "size (%d) great than a page\n", reqlen);
14204 return -ENOMEM;
14205 }
14206 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014207 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014208 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014209
14210 /* Allocate DMA memory and set up the non-embedded mailbox command */
14211 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14212 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14213 LPFC_SLI4_MBX_NEMBED);
14214
14215 if (alloclen < reqlen) {
14216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14217 "0285 Allocated DMA memory size (%d) is "
14218 "less than the requested DMA memory "
14219 "size (%d)\n", alloclen, reqlen);
14220 lpfc_sli4_mbox_cmd_free(phba, mbox);
14221 return -ENOMEM;
14222 }
James Smart4f774512009-05-22 14:52:35 -040014223 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014224 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014225 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14226 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14227
James Smart8a9d2e82012-05-09 21:16:12 -040014228 pg_pairs = 0;
14229 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014230 /* Set up the sge entry */
14231 sgl_pg_pairs->sgl_pg0_addr_lo =
14232 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14233 sgl_pg_pairs->sgl_pg0_addr_hi =
14234 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14235 sgl_pg_pairs->sgl_pg1_addr_lo =
14236 cpu_to_le32(putPaddrLow(0));
14237 sgl_pg_pairs->sgl_pg1_addr_hi =
14238 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014239
James Smart4f774512009-05-22 14:52:35 -040014240 /* Keep the first xritag on the list */
14241 if (pg_pairs == 0)
14242 xritag_start = sglq_entry->sli4_xritag;
14243 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014244 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014245 }
James Smart6d368e52011-05-24 11:44:12 -040014246
14247 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014248 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014249 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014250 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014251 if (!phba->sli4_hba.intr_enable)
14252 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14253 else {
James Smarta183a152011-10-10 21:32:43 -040014254 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014255 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14256 }
14257 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14258 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14259 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14260 if (rc != MBX_TIMEOUT)
14261 lpfc_sli4_mbox_cmd_free(phba, mbox);
14262 if (shdr_status || shdr_add_status || rc) {
14263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14264 "2513 POST_SGL_BLOCK mailbox command failed "
14265 "status x%x add_status x%x mbx status x%x\n",
14266 shdr_status, shdr_add_status, rc);
14267 rc = -ENXIO;
14268 }
14269 return rc;
14270}
14271
14272/**
14273 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14274 * @phba: pointer to lpfc hba data structure.
14275 * @sblist: pointer to scsi buffer list.
14276 * @count: number of scsi buffers on the list.
14277 *
14278 * This routine is invoked to post a block of @count scsi sgl pages from a
14279 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14280 * No Lock is held.
14281 *
14282 **/
14283int
James Smart8a9d2e82012-05-09 21:16:12 -040014284lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14285 struct list_head *sblist,
14286 int count)
James Smart4f774512009-05-22 14:52:35 -040014287{
14288 struct lpfc_scsi_buf *psb;
14289 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14290 struct sgl_page_pairs *sgl_pg_pairs;
14291 void *viraddr;
14292 LPFC_MBOXQ_t *mbox;
14293 uint32_t reqlen, alloclen, pg_pairs;
14294 uint32_t mbox_tmo;
14295 uint16_t xritag_start = 0;
14296 int rc = 0;
14297 uint32_t shdr_status, shdr_add_status;
14298 dma_addr_t pdma_phys_bpl1;
14299 union lpfc_sli4_cfg_shdr *shdr;
14300
14301 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014302 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014303 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014304 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014305 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14306 "0217 Block sgl registration required DMA "
14307 "size (%d) great than a page\n", reqlen);
14308 return -ENOMEM;
14309 }
14310 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14311 if (!mbox) {
14312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14313 "0283 Failed to allocate mbox cmd memory\n");
14314 return -ENOMEM;
14315 }
14316
14317 /* Allocate DMA memory and set up the non-embedded mailbox command */
14318 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14319 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14320 LPFC_SLI4_MBX_NEMBED);
14321
14322 if (alloclen < reqlen) {
14323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14324 "2561 Allocated DMA memory size (%d) is "
14325 "less than the requested DMA memory "
14326 "size (%d)\n", alloclen, reqlen);
14327 lpfc_sli4_mbox_cmd_free(phba, mbox);
14328 return -ENOMEM;
14329 }
James Smart6d368e52011-05-24 11:44:12 -040014330
James Smart4f774512009-05-22 14:52:35 -040014331 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014332 viraddr = mbox->sge_array->addr[0];
14333
14334 /* Set up the SGL pages in the non-embedded DMA pages */
14335 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14336 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14337
14338 pg_pairs = 0;
14339 list_for_each_entry(psb, sblist, list) {
14340 /* Set up the sge entry */
14341 sgl_pg_pairs->sgl_pg0_addr_lo =
14342 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14343 sgl_pg_pairs->sgl_pg0_addr_hi =
14344 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14345 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14346 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14347 else
14348 pdma_phys_bpl1 = 0;
14349 sgl_pg_pairs->sgl_pg1_addr_lo =
14350 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14351 sgl_pg_pairs->sgl_pg1_addr_hi =
14352 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14353 /* Keep the first xritag on the list */
14354 if (pg_pairs == 0)
14355 xritag_start = psb->cur_iocbq.sli4_xritag;
14356 sgl_pg_pairs++;
14357 pg_pairs++;
14358 }
14359 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14360 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14361 /* Perform endian conversion if necessary */
14362 sgl->word0 = cpu_to_le32(sgl->word0);
14363
14364 if (!phba->sli4_hba.intr_enable)
14365 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14366 else {
James Smarta183a152011-10-10 21:32:43 -040014367 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014368 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14369 }
14370 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14371 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14372 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14373 if (rc != MBX_TIMEOUT)
14374 lpfc_sli4_mbox_cmd_free(phba, mbox);
14375 if (shdr_status || shdr_add_status || rc) {
14376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14377 "2564 POST_SGL_BLOCK mailbox command failed "
14378 "status x%x add_status x%x mbx status x%x\n",
14379 shdr_status, shdr_add_status, rc);
14380 rc = -ENXIO;
14381 }
14382 return rc;
14383}
14384
14385/**
14386 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14387 * @phba: pointer to lpfc_hba struct that the frame was received on
14388 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14389 *
14390 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14391 * valid type of frame that the LPFC driver will handle. This function will
14392 * return a zero if the frame is a valid frame or a non zero value when the
14393 * frame does not pass the check.
14394 **/
14395static int
14396lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14397{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014398 /* make rctl_names static to save stack space */
14399 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014400 char *type_names[] = FC_TYPE_NAMES_INIT;
14401 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014402 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014403
14404 switch (fc_hdr->fh_r_ctl) {
14405 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14406 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14407 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14408 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14409 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14410 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14411 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14412 case FC_RCTL_DD_CMD_STATUS: /* command status */
14413 case FC_RCTL_ELS_REQ: /* extended link services request */
14414 case FC_RCTL_ELS_REP: /* extended link services reply */
14415 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14416 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14417 case FC_RCTL_BA_NOP: /* basic link service NOP */
14418 case FC_RCTL_BA_ABTS: /* basic link service abort */
14419 case FC_RCTL_BA_RMC: /* remove connection */
14420 case FC_RCTL_BA_ACC: /* basic accept */
14421 case FC_RCTL_BA_RJT: /* basic reject */
14422 case FC_RCTL_BA_PRMT:
14423 case FC_RCTL_ACK_1: /* acknowledge_1 */
14424 case FC_RCTL_ACK_0: /* acknowledge_0 */
14425 case FC_RCTL_P_RJT: /* port reject */
14426 case FC_RCTL_F_RJT: /* fabric reject */
14427 case FC_RCTL_P_BSY: /* port busy */
14428 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14429 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14430 case FC_RCTL_LCR: /* link credit reset */
14431 case FC_RCTL_END: /* end */
14432 break;
14433 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14434 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14435 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14436 return lpfc_fc_frame_check(phba, fc_hdr);
14437 default:
14438 goto drop;
14439 }
14440 switch (fc_hdr->fh_type) {
14441 case FC_TYPE_BLS:
14442 case FC_TYPE_ELS:
14443 case FC_TYPE_FCP:
14444 case FC_TYPE_CT:
14445 break;
14446 case FC_TYPE_IP:
14447 case FC_TYPE_ILS:
14448 default:
14449 goto drop;
14450 }
James Smart546fc852011-03-11 16:06:29 -050014451
James Smart4f774512009-05-22 14:52:35 -040014452 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014453 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14454 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14455 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14456 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014457 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14458 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014459 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14460 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014461 return 0;
14462drop:
14463 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14464 "2539 Dropped frame rctl:%s type:%s\n",
14465 rctl_names[fc_hdr->fh_r_ctl],
14466 type_names[fc_hdr->fh_type]);
14467 return 1;
14468}
14469
14470/**
14471 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14472 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14473 *
14474 * This function processes the FC header to retrieve the VFI from the VF
14475 * header, if one exists. This function will return the VFI if one exists
14476 * or 0 if no VSAN Header exists.
14477 **/
14478static uint32_t
14479lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14480{
14481 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14482
14483 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14484 return 0;
14485 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14486}
14487
14488/**
14489 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14490 * @phba: Pointer to the HBA structure to search for the vport on
14491 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14492 * @fcfi: The FC Fabric ID that the frame came from
14493 *
14494 * This function searches the @phba for a vport that matches the content of the
14495 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14496 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14497 * returns the matching vport pointer or NULL if unable to match frame to a
14498 * vport.
14499 **/
14500static struct lpfc_vport *
14501lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14502 uint16_t fcfi)
14503{
14504 struct lpfc_vport **vports;
14505 struct lpfc_vport *vport = NULL;
14506 int i;
14507 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14508 fc_hdr->fh_d_id[1] << 8 |
14509 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014510
James Smartbf086112011-08-21 21:48:13 -040014511 if (did == Fabric_DID)
14512 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014513 if ((phba->pport->fc_flag & FC_PT2PT) &&
14514 !(phba->link_state == LPFC_HBA_READY))
14515 return phba->pport;
14516
James Smart4f774512009-05-22 14:52:35 -040014517 vports = lpfc_create_vport_work_array(phba);
14518 if (vports != NULL)
14519 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14520 if (phba->fcf.fcfi == fcfi &&
14521 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14522 vports[i]->fc_myDID == did) {
14523 vport = vports[i];
14524 break;
14525 }
14526 }
14527 lpfc_destroy_vport_work_array(phba, vports);
14528 return vport;
14529}
14530
14531/**
James Smart45ed1192009-10-02 15:17:02 -040014532 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14533 * @vport: The vport to work on.
14534 *
14535 * This function updates the receive sequence time stamp for this vport. The
14536 * receive sequence time stamp indicates the time that the last frame of the
14537 * the sequence that has been idle for the longest amount of time was received.
14538 * the driver uses this time stamp to indicate if any received sequences have
14539 * timed out.
14540 **/
14541void
14542lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14543{
14544 struct lpfc_dmabuf *h_buf;
14545 struct hbq_dmabuf *dmabuf = NULL;
14546
14547 /* get the oldest sequence on the rcv list */
14548 h_buf = list_get_first(&vport->rcv_buffer_list,
14549 struct lpfc_dmabuf, list);
14550 if (!h_buf)
14551 return;
14552 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14553 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14554}
14555
14556/**
14557 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14558 * @vport: The vport that the received sequences were sent to.
14559 *
14560 * This function cleans up all outstanding received sequences. This is called
14561 * by the driver when a link event or user action invalidates all the received
14562 * sequences.
14563 **/
14564void
14565lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14566{
14567 struct lpfc_dmabuf *h_buf, *hnext;
14568 struct lpfc_dmabuf *d_buf, *dnext;
14569 struct hbq_dmabuf *dmabuf = NULL;
14570
14571 /* start with the oldest sequence on the rcv list */
14572 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14573 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14574 list_del_init(&dmabuf->hbuf.list);
14575 list_for_each_entry_safe(d_buf, dnext,
14576 &dmabuf->dbuf.list, list) {
14577 list_del_init(&d_buf->list);
14578 lpfc_in_buf_free(vport->phba, d_buf);
14579 }
14580 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14581 }
14582}
14583
14584/**
14585 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14586 * @vport: The vport that the received sequences were sent to.
14587 *
14588 * This function determines whether any received sequences have timed out by
14589 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14590 * indicates that there is at least one timed out sequence this routine will
14591 * go through the received sequences one at a time from most inactive to most
14592 * active to determine which ones need to be cleaned up. Once it has determined
14593 * that a sequence needs to be cleaned up it will simply free up the resources
14594 * without sending an abort.
14595 **/
14596void
14597lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14598{
14599 struct lpfc_dmabuf *h_buf, *hnext;
14600 struct lpfc_dmabuf *d_buf, *dnext;
14601 struct hbq_dmabuf *dmabuf = NULL;
14602 unsigned long timeout;
14603 int abort_count = 0;
14604
14605 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14606 vport->rcv_buffer_time_stamp);
14607 if (list_empty(&vport->rcv_buffer_list) ||
14608 time_before(jiffies, timeout))
14609 return;
14610 /* start with the oldest sequence on the rcv list */
14611 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14612 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14613 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14614 dmabuf->time_stamp);
14615 if (time_before(jiffies, timeout))
14616 break;
14617 abort_count++;
14618 list_del_init(&dmabuf->hbuf.list);
14619 list_for_each_entry_safe(d_buf, dnext,
14620 &dmabuf->dbuf.list, list) {
14621 list_del_init(&d_buf->list);
14622 lpfc_in_buf_free(vport->phba, d_buf);
14623 }
14624 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14625 }
14626 if (abort_count)
14627 lpfc_update_rcv_time_stamp(vport);
14628}
14629
14630/**
James Smart4f774512009-05-22 14:52:35 -040014631 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14632 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14633 *
14634 * This function searches through the existing incomplete sequences that have
14635 * been sent to this @vport. If the frame matches one of the incomplete
14636 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14637 * make up that sequence. If no sequence is found that matches this frame then
14638 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14639 * This function returns a pointer to the first dmabuf in the sequence list that
14640 * the frame was linked to.
14641 **/
14642static struct hbq_dmabuf *
14643lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14644{
14645 struct fc_frame_header *new_hdr;
14646 struct fc_frame_header *temp_hdr;
14647 struct lpfc_dmabuf *d_buf;
14648 struct lpfc_dmabuf *h_buf;
14649 struct hbq_dmabuf *seq_dmabuf = NULL;
14650 struct hbq_dmabuf *temp_dmabuf = NULL;
14651
James Smart4d9ab992009-10-02 15:16:39 -040014652 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014653 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014654 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14655 /* Use the hdr_buf to find the sequence that this frame belongs to */
14656 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14657 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14658 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14659 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14660 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14661 continue;
14662 /* found a pending sequence that matches this frame */
14663 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14664 break;
14665 }
14666 if (!seq_dmabuf) {
14667 /*
14668 * This indicates first frame received for this sequence.
14669 * Queue the buffer on the vport's rcv_buffer_list.
14670 */
14671 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040014672 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014673 return dmabuf;
14674 }
14675 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050014676 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14677 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040014678 list_del_init(&seq_dmabuf->hbuf.list);
14679 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14680 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014681 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014682 return dmabuf;
14683 }
James Smart45ed1192009-10-02 15:17:02 -040014684 /* move this sequence to the tail to indicate a young sequence */
14685 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14686 seq_dmabuf->time_stamp = jiffies;
14687 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014688 if (list_empty(&seq_dmabuf->dbuf.list)) {
14689 temp_hdr = dmabuf->hbuf.virt;
14690 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14691 return seq_dmabuf;
14692 }
James Smart4f774512009-05-22 14:52:35 -040014693 /* find the correct place in the sequence to insert this frame */
14694 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14695 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14696 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14697 /*
14698 * If the frame's sequence count is greater than the frame on
14699 * the list then insert the frame right after this frame
14700 */
James Smarteeead812009-12-21 17:01:23 -050014701 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14702 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014703 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14704 return seq_dmabuf;
14705 }
14706 }
14707 return NULL;
14708}
14709
14710/**
James Smart6669f9b2009-10-02 15:16:45 -040014711 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14712 * @vport: pointer to a vitural port
14713 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14714 *
14715 * This function tries to abort from the partially assembed sequence, described
14716 * by the information from basic abbort @dmabuf. It checks to see whether such
14717 * partially assembled sequence held by the driver. If so, it shall free up all
14718 * the frames from the partially assembled sequence.
14719 *
14720 * Return
14721 * true -- if there is matching partially assembled sequence present and all
14722 * the frames freed with the sequence;
14723 * false -- if there is no matching partially assembled sequence present so
14724 * nothing got aborted in the lower layer driver
14725 **/
14726static bool
14727lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14728 struct hbq_dmabuf *dmabuf)
14729{
14730 struct fc_frame_header *new_hdr;
14731 struct fc_frame_header *temp_hdr;
14732 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14733 struct hbq_dmabuf *seq_dmabuf = NULL;
14734
14735 /* Use the hdr_buf to find the sequence that matches this frame */
14736 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14737 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14738 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14739 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14740 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14741 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14742 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14743 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14744 continue;
14745 /* found a pending sequence that matches this frame */
14746 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14747 break;
14748 }
14749
14750 /* Free up all the frames from the partially assembled sequence */
14751 if (seq_dmabuf) {
14752 list_for_each_entry_safe(d_buf, n_buf,
14753 &seq_dmabuf->dbuf.list, list) {
14754 list_del_init(&d_buf->list);
14755 lpfc_in_buf_free(vport->phba, d_buf);
14756 }
14757 return true;
14758 }
14759 return false;
14760}
14761
14762/**
James Smart6dd9e312013-01-03 15:43:37 -050014763 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14764 * @vport: pointer to a vitural port
14765 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14766 *
14767 * This function tries to abort from the assembed sequence from upper level
14768 * protocol, described by the information from basic abbort @dmabuf. It
14769 * checks to see whether such pending context exists at upper level protocol.
14770 * If so, it shall clean up the pending context.
14771 *
14772 * Return
14773 * true -- if there is matching pending context of the sequence cleaned
14774 * at ulp;
14775 * false -- if there is no matching pending context of the sequence present
14776 * at ulp.
14777 **/
14778static bool
14779lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14780{
14781 struct lpfc_hba *phba = vport->phba;
14782 int handled;
14783
14784 /* Accepting abort at ulp with SLI4 only */
14785 if (phba->sli_rev < LPFC_SLI_REV4)
14786 return false;
14787
14788 /* Register all caring upper level protocols to attend abort */
14789 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14790 if (handled)
14791 return true;
14792
14793 return false;
14794}
14795
14796/**
James Smart546fc852011-03-11 16:06:29 -050014797 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014798 * @phba: Pointer to HBA context object.
14799 * @cmd_iocbq: pointer to the command iocbq structure.
14800 * @rsp_iocbq: pointer to the response iocbq structure.
14801 *
James Smart546fc852011-03-11 16:06:29 -050014802 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014803 * event. It properly releases the memory allocated to the sequence abort
14804 * accept iocb.
14805 **/
14806static void
James Smart546fc852011-03-11 16:06:29 -050014807lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014808 struct lpfc_iocbq *cmd_iocbq,
14809 struct lpfc_iocbq *rsp_iocbq)
14810{
James Smart6dd9e312013-01-03 15:43:37 -050014811 struct lpfc_nodelist *ndlp;
14812
14813 if (cmd_iocbq) {
14814 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14815 lpfc_nlp_put(ndlp);
14816 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014817 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050014818 }
James Smart6b5151f2012-01-18 16:24:06 -050014819
14820 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14821 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14822 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14823 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14824 rsp_iocbq->iocb.ulpStatus,
14825 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014826}
14827
14828/**
James Smart6d368e52011-05-24 11:44:12 -040014829 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14830 * @phba: Pointer to HBA context object.
14831 * @xri: xri id in transaction.
14832 *
14833 * This function validates the xri maps to the known range of XRIs allocated an
14834 * used by the driver.
14835 **/
James Smart7851fe22011-07-22 18:36:52 -040014836uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014837lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14838 uint16_t xri)
14839{
14840 int i;
14841
14842 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14843 if (xri == phba->sli4_hba.xri_ids[i])
14844 return i;
14845 }
14846 return NO_XRI;
14847}
14848
James Smart6d368e52011-05-24 11:44:12 -040014849/**
James Smart546fc852011-03-11 16:06:29 -050014850 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014851 * @phba: Pointer to HBA context object.
14852 * @fc_hdr: pointer to a FC frame header.
14853 *
James Smart546fc852011-03-11 16:06:29 -050014854 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014855 * event after aborting the sequence handling.
14856 **/
14857static void
James Smart6dd9e312013-01-03 15:43:37 -050014858lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14859 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040014860{
James Smart6dd9e312013-01-03 15:43:37 -050014861 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040014862 struct lpfc_iocbq *ctiocb = NULL;
14863 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014864 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014865 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014866 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014867 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014868
14869 if (!lpfc_is_link_up(phba))
14870 return;
14871
14872 sid = sli4_sid_from_fc_hdr(fc_hdr);
14873 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014874 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014875
James Smart6dd9e312013-01-03 15:43:37 -050014876 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040014877 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050014878 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14879 if (!ndlp) {
14880 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14881 "1268 Failed to allocate ndlp for "
14882 "oxid:x%x SID:x%x\n", oxid, sid);
14883 return;
14884 }
14885 lpfc_nlp_init(vport, ndlp, sid);
14886 /* Put ndlp onto pport node list */
14887 lpfc_enqueue_node(vport, ndlp);
14888 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14889 /* re-setup ndlp without removing from node list */
14890 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14891 if (!ndlp) {
14892 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14893 "3275 Failed to active ndlp found "
14894 "for oxid:x%x SID:x%x\n", oxid, sid);
14895 return;
14896 }
James Smart6669f9b2009-10-02 15:16:45 -040014897 }
14898
James Smart546fc852011-03-11 16:06:29 -050014899 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014900 ctiocb = lpfc_sli_get_iocbq(phba);
14901 if (!ctiocb)
14902 return;
14903
James Smart5ffc2662009-11-18 15:39:44 -050014904 /* Extract the F_CTL field from FC_HDR */
14905 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14906
James Smart6669f9b2009-10-02 15:16:45 -040014907 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014908 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014909 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014910 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14911 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14912 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14913
14914 /* Fill in the rest of iocb fields */
14915 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14916 icmd->ulpBdeCount = 0;
14917 icmd->ulpLe = 1;
14918 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014919 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050014920 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014921
James Smart6669f9b2009-10-02 15:16:45 -040014922 ctiocb->iocb_cmpl = NULL;
14923 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014924 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014925 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014926 ctiocb->sli4_xritag = NO_XRI;
14927
James Smartee0f4fe2012-05-09 21:19:14 -040014928 if (fctl & FC_FC_EX_CTX)
14929 /* Exchange responder sent the abort so we
14930 * own the oxid.
14931 */
14932 xri = oxid;
14933 else
14934 xri = rxid;
14935 lxri = lpfc_sli4_xri_inrange(phba, xri);
14936 if (lxri != NO_XRI)
14937 lpfc_set_rrq_active(phba, ndlp, lxri,
14938 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050014939 /* For BA_ABTS from exchange responder, if the logical xri with
14940 * the oxid maps to the FCP XRI range, the port no longer has
14941 * that exchange context, send a BLS_RJT. Override the IOCB for
14942 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050014943 */
James Smart6dd9e312013-01-03 15:43:37 -050014944 if ((fctl & FC_FC_EX_CTX) &&
14945 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14946 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14947 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14948 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14949 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14950 }
14951
14952 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14953 * the driver no longer has that exchange, send a BLS_RJT. Override
14954 * the IOCB for a BA_RJT.
14955 */
14956 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050014957 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14958 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14959 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14960 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14961 }
James Smart6669f9b2009-10-02 15:16:45 -040014962
James Smart5ffc2662009-11-18 15:39:44 -050014963 if (fctl & FC_FC_EX_CTX) {
14964 /* ABTS sent by responder to CT exchange, construction
14965 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14966 * field and RX_ID from ABTS for RX_ID field.
14967 */
James Smart546fc852011-03-11 16:06:29 -050014968 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014969 } else {
14970 /* ABTS sent by initiator to CT exchange, construction
14971 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014972 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014973 */
James Smart546fc852011-03-11 16:06:29 -050014974 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014975 }
James Smartf09c3ac2012-03-01 22:33:29 -050014976 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014977 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014978
James Smart546fc852011-03-11 16:06:29 -050014979 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050014980 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14981 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14982 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050014983
14984 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14985 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050014986 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14987 "2925 Failed to issue CT ABTS RSP x%x on "
14988 "xri x%x, Data x%x\n",
14989 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14990 phba->link_state);
14991 lpfc_nlp_put(ndlp);
14992 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050014993 lpfc_sli_release_iocbq(phba, ctiocb);
14994 }
James Smart6669f9b2009-10-02 15:16:45 -040014995}
14996
14997/**
14998 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14999 * @vport: Pointer to the vport on which this sequence was received
15000 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15001 *
15002 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15003 * receive sequence is only partially assembed by the driver, it shall abort
15004 * the partially assembled frames for the sequence. Otherwise, if the
15005 * unsolicited receive sequence has been completely assembled and passed to
15006 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15007 * unsolicited sequence has been aborted. After that, it will issue a basic
15008 * accept to accept the abort.
15009 **/
15010void
15011lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15012 struct hbq_dmabuf *dmabuf)
15013{
15014 struct lpfc_hba *phba = vport->phba;
15015 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015016 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015017 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015018
James Smart6669f9b2009-10-02 15:16:45 -040015019 /* Make a copy of fc_hdr before the dmabuf being released */
15020 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015021 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015022
James Smart5ffc2662009-11-18 15:39:44 -050015023 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015024 /* ABTS by responder to exchange, no cleanup needed */
15025 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015026 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015027 /* ABTS by initiator to exchange, need to do cleanup */
15028 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15029 if (aborted == false)
15030 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015031 }
James Smart6dd9e312013-01-03 15:43:37 -050015032 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15033
15034 /* Respond with BA_ACC or BA_RJT accordingly */
15035 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015036}
15037
15038/**
James Smart4f774512009-05-22 14:52:35 -040015039 * lpfc_seq_complete - Indicates if a sequence is complete
15040 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15041 *
15042 * This function checks the sequence, starting with the frame described by
15043 * @dmabuf, to see if all the frames associated with this sequence are present.
15044 * the frames associated with this sequence are linked to the @dmabuf using the
15045 * dbuf list. This function looks for two major things. 1) That the first frame
15046 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15047 * set. 3) That there are no holes in the sequence count. The function will
15048 * return 1 when the sequence is complete, otherwise it will return 0.
15049 **/
15050static int
15051lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15052{
15053 struct fc_frame_header *hdr;
15054 struct lpfc_dmabuf *d_buf;
15055 struct hbq_dmabuf *seq_dmabuf;
15056 uint32_t fctl;
15057 int seq_count = 0;
15058
15059 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15060 /* make sure first fame of sequence has a sequence count of zero */
15061 if (hdr->fh_seq_cnt != seq_count)
15062 return 0;
15063 fctl = (hdr->fh_f_ctl[0] << 16 |
15064 hdr->fh_f_ctl[1] << 8 |
15065 hdr->fh_f_ctl[2]);
15066 /* If last frame of sequence we can return success. */
15067 if (fctl & FC_FC_END_SEQ)
15068 return 1;
15069 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15070 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15071 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15072 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015073 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015074 return 0;
15075 fctl = (hdr->fh_f_ctl[0] << 16 |
15076 hdr->fh_f_ctl[1] << 8 |
15077 hdr->fh_f_ctl[2]);
15078 /* If last frame of sequence we can return success. */
15079 if (fctl & FC_FC_END_SEQ)
15080 return 1;
15081 }
15082 return 0;
15083}
15084
15085/**
15086 * lpfc_prep_seq - Prep sequence for ULP processing
15087 * @vport: Pointer to the vport on which this sequence was received
15088 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15089 *
15090 * This function takes a sequence, described by a list of frames, and creates
15091 * a list of iocbq structures to describe the sequence. This iocbq list will be
15092 * used to issue to the generic unsolicited sequence handler. This routine
15093 * returns a pointer to the first iocbq in the list. If the function is unable
15094 * to allocate an iocbq then it throw out the received frames that were not
15095 * able to be described and return a pointer to the first iocbq. If unable to
15096 * allocate any iocbqs (including the first) this function will return NULL.
15097 **/
15098static struct lpfc_iocbq *
15099lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15100{
James Smart7851fe22011-07-22 18:36:52 -040015101 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015102 struct lpfc_dmabuf *d_buf, *n_buf;
15103 struct lpfc_iocbq *first_iocbq, *iocbq;
15104 struct fc_frame_header *fc_hdr;
15105 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015106 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015107 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015108
15109 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15110 /* remove from receive buffer list */
15111 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015112 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015113 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015114 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015115 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015116 /* Get an iocbq struct to fill in. */
15117 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15118 if (first_iocbq) {
15119 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015120 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015121 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015122
15123 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15124 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15125 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15126 first_iocbq->iocb.un.rcvels.parmRo =
15127 sli4_did_from_fc_hdr(fc_hdr);
15128 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15129 } else
15130 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015131 first_iocbq->iocb.ulpContext = NO_XRI;
15132 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15133 be16_to_cpu(fc_hdr->fh_ox_id);
15134 /* iocbq is prepped for internal consumption. Physical vpi. */
15135 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15136 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015137 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015138 tot_len = bf_get(lpfc_rcqe_length,
15139 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15140
James Smart4f774512009-05-22 14:52:35 -040015141 first_iocbq->context2 = &seq_dmabuf->dbuf;
15142 first_iocbq->context3 = NULL;
15143 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015144 if (tot_len > LPFC_DATA_BUF_SIZE)
15145 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015146 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015147 else
15148 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15149
James Smart4f774512009-05-22 14:52:35 -040015150 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015151
James Smart7851fe22011-07-22 18:36:52 -040015152 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015153 }
15154 iocbq = first_iocbq;
15155 /*
15156 * Each IOCBq can have two Buffers assigned, so go through the list
15157 * of buffers for this sequence and save two buffers in each IOCBq
15158 */
15159 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15160 if (!iocbq) {
15161 lpfc_in_buf_free(vport->phba, d_buf);
15162 continue;
15163 }
15164 if (!iocbq->context3) {
15165 iocbq->context3 = d_buf;
15166 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015167 /* We need to get the size out of the right CQE */
15168 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15169 len = bf_get(lpfc_rcqe_length,
15170 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015171 pbde = (struct ulp_bde64 *)
15172 &iocbq->iocb.unsli3.sli3Words[4];
15173 if (len > LPFC_DATA_BUF_SIZE)
15174 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15175 else
15176 pbde->tus.f.bdeSize = len;
15177
James Smart7851fe22011-07-22 18:36:52 -040015178 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15179 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015180 } else {
15181 iocbq = lpfc_sli_get_iocbq(vport->phba);
15182 if (!iocbq) {
15183 if (first_iocbq) {
15184 first_iocbq->iocb.ulpStatus =
15185 IOSTAT_FCP_RSP_ERROR;
15186 first_iocbq->iocb.un.ulpWord[4] =
15187 IOERR_NO_RESOURCES;
15188 }
15189 lpfc_in_buf_free(vport->phba, d_buf);
15190 continue;
15191 }
James Smart7851fe22011-07-22 18:36:52 -040015192 /* We need to get the size out of the right CQE */
15193 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15194 len = bf_get(lpfc_rcqe_length,
15195 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015196 iocbq->context2 = d_buf;
15197 iocbq->context3 = NULL;
15198 iocbq->iocb.ulpBdeCount = 1;
15199 if (len > LPFC_DATA_BUF_SIZE)
15200 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15201 LPFC_DATA_BUF_SIZE;
15202 else
15203 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15204
James Smart7851fe22011-07-22 18:36:52 -040015205 tot_len += len;
15206 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15207
James Smart4f774512009-05-22 14:52:35 -040015208 iocbq->iocb.un.rcvels.remoteID = sid;
15209 list_add_tail(&iocbq->list, &first_iocbq->list);
15210 }
15211 }
15212 return first_iocbq;
15213}
15214
James Smart6669f9b2009-10-02 15:16:45 -040015215static void
15216lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15217 struct hbq_dmabuf *seq_dmabuf)
15218{
15219 struct fc_frame_header *fc_hdr;
15220 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15221 struct lpfc_hba *phba = vport->phba;
15222
15223 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15224 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15225 if (!iocbq) {
15226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15227 "2707 Ring %d handler: Failed to allocate "
15228 "iocb Rctl x%x Type x%x received\n",
15229 LPFC_ELS_RING,
15230 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15231 return;
15232 }
15233 if (!lpfc_complete_unsol_iocb(phba,
15234 &phba->sli.ring[LPFC_ELS_RING],
15235 iocbq, fc_hdr->fh_r_ctl,
15236 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015238 "2540 Ring %d handler: unexpected Rctl "
15239 "x%x Type x%x received\n",
15240 LPFC_ELS_RING,
15241 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15242
15243 /* Free iocb created in lpfc_prep_seq */
15244 list_for_each_entry_safe(curr_iocb, next_iocb,
15245 &iocbq->list, list) {
15246 list_del_init(&curr_iocb->list);
15247 lpfc_sli_release_iocbq(phba, curr_iocb);
15248 }
15249 lpfc_sli_release_iocbq(phba, iocbq);
15250}
15251
James Smart4f774512009-05-22 14:52:35 -040015252/**
15253 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15254 * @phba: Pointer to HBA context object.
15255 *
15256 * This function is called with no lock held. This function processes all
15257 * the received buffers and gives it to upper layers when a received buffer
15258 * indicates that it is the final frame in the sequence. The interrupt
15259 * service routine processes received buffers at interrupt contexts and adds
15260 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15261 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15262 * appropriate receive function when the final frame in a sequence is received.
15263 **/
James Smart4d9ab992009-10-02 15:16:39 -040015264void
15265lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15266 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015267{
James Smart4d9ab992009-10-02 15:16:39 -040015268 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015269 struct fc_frame_header *fc_hdr;
15270 struct lpfc_vport *vport;
15271 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015272 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015273
James Smart4f774512009-05-22 14:52:35 -040015274 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015275 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15276 /* check to see if this a valid type of frame */
15277 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15278 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15279 return;
15280 }
James Smart7851fe22011-07-22 18:36:52 -040015281 if ((bf_get(lpfc_cqe_code,
15282 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15283 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15284 &dmabuf->cq_event.cqe.rcqe_cmpl);
15285 else
15286 fcfi = bf_get(lpfc_rcqe_fcf_id,
15287 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015288
James Smart4d9ab992009-10-02 15:16:39 -040015289 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015290 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015291 /* throw out the frame */
15292 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15293 return;
15294 }
James Smart939723a2012-05-09 21:19:03 -040015295
15296 /* d_id this frame is directed to */
15297 did = sli4_did_from_fc_hdr(fc_hdr);
15298
15299 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15300 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15301 (did != Fabric_DID)) {
15302 /*
15303 * Throw out the frame if we are not pt2pt.
15304 * The pt2pt protocol allows for discovery frames
15305 * to be received without a registered VPI.
15306 */
15307 if (!(vport->fc_flag & FC_PT2PT) ||
15308 (phba->link_state == LPFC_HBA_READY)) {
15309 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15310 return;
15311 }
15312 }
15313
James Smart6669f9b2009-10-02 15:16:45 -040015314 /* Handle the basic abort sequence (BA_ABTS) event */
15315 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15316 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15317 return;
15318 }
15319
James Smart4d9ab992009-10-02 15:16:39 -040015320 /* Link this frame */
15321 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15322 if (!seq_dmabuf) {
15323 /* unable to add frame to vport - throw it out */
15324 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15325 return;
15326 }
15327 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015328 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015329 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015330
James Smart6669f9b2009-10-02 15:16:45 -040015331 /* Send the complete sequence to the upper layer protocol */
15332 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015333}
James Smart6fb120a2009-05-22 14:52:59 -040015334
15335/**
15336 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15337 * @phba: pointer to lpfc hba data structure.
15338 *
15339 * This routine is invoked to post rpi header templates to the
15340 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015341 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15342 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015343 *
15344 * This routine does not require any locks. It's usage is expected
15345 * to be driver load or reset recovery when the driver is
15346 * sequential.
15347 *
15348 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015349 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015350 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015351 * When this error occurs, the driver is not guaranteed
15352 * to have any rpi regions posted to the device and
15353 * must either attempt to repost the regions or take a
15354 * fatal error.
15355 **/
15356int
15357lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15358{
15359 struct lpfc_rpi_hdr *rpi_page;
15360 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015361 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015362
James Smart6d368e52011-05-24 11:44:12 -040015363 /* SLI4 ports that support extents do not require RPI headers. */
15364 if (!phba->sli4_hba.rpi_hdrs_in_use)
15365 goto exit;
15366 if (phba->sli4_hba.extents_in_use)
15367 return -EIO;
15368
James Smart6fb120a2009-05-22 14:52:59 -040015369 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015370 /*
15371 * Assign the rpi headers a physical rpi only if the driver
15372 * has not initialized those resources. A port reset only
15373 * needs the headers posted.
15374 */
15375 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15376 LPFC_RPI_RSRC_RDY)
15377 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15378
James Smart6fb120a2009-05-22 14:52:59 -040015379 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15380 if (rc != MBX_SUCCESS) {
15381 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15382 "2008 Error %d posting all rpi "
15383 "headers\n", rc);
15384 rc = -EIO;
15385 break;
15386 }
15387 }
15388
James Smart6d368e52011-05-24 11:44:12 -040015389 exit:
15390 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15391 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015392 return rc;
15393}
15394
15395/**
15396 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15397 * @phba: pointer to lpfc hba data structure.
15398 * @rpi_page: pointer to the rpi memory region.
15399 *
15400 * This routine is invoked to post a single rpi header to the
15401 * HBA consistent with the SLI-4 interface spec. This memory region
15402 * maps up to 64 rpi context regions.
15403 *
15404 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015405 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015406 * -ENOMEM - No available memory
15407 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015408 **/
15409int
15410lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15411{
15412 LPFC_MBOXQ_t *mboxq;
15413 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15414 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015415 uint32_t shdr_status, shdr_add_status;
15416 union lpfc_sli4_cfg_shdr *shdr;
15417
James Smart6d368e52011-05-24 11:44:12 -040015418 /* SLI4 ports that support extents do not require RPI headers. */
15419 if (!phba->sli4_hba.rpi_hdrs_in_use)
15420 return rc;
15421 if (phba->sli4_hba.extents_in_use)
15422 return -EIO;
15423
James Smart6fb120a2009-05-22 14:52:59 -040015424 /* The port is notified of the header region via a mailbox command. */
15425 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15426 if (!mboxq) {
15427 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15428 "2001 Unable to allocate memory for issuing "
15429 "SLI_CONFIG_SPECIAL mailbox command\n");
15430 return -ENOMEM;
15431 }
15432
15433 /* Post all rpi memory regions to the port. */
15434 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015435 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15436 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15437 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015438 sizeof(struct lpfc_sli4_cfg_mhdr),
15439 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015440
15441
15442 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015443 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15444 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015445 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15446 hdr_tmpl, rpi_page->page_count);
15447
James Smart6fb120a2009-05-22 14:52:59 -040015448 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15449 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015450 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015451 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15452 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15453 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15454 if (rc != MBX_TIMEOUT)
15455 mempool_free(mboxq, phba->mbox_mem_pool);
15456 if (shdr_status || shdr_add_status || rc) {
15457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15458 "2514 POST_RPI_HDR mailbox failed with "
15459 "status x%x add_status x%x, mbx status x%x\n",
15460 shdr_status, shdr_add_status, rc);
15461 rc = -ENXIO;
15462 }
15463 return rc;
15464}
15465
15466/**
15467 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15468 * @phba: pointer to lpfc hba data structure.
15469 *
15470 * This routine is invoked to post rpi header templates to the
15471 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015472 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15473 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015474 *
15475 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015476 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015477 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15478 **/
15479int
15480lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15481{
James Smart6d368e52011-05-24 11:44:12 -040015482 unsigned long rpi;
15483 uint16_t max_rpi, rpi_limit;
15484 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015485 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015486 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015487
15488 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040015489 rpi_limit = phba->sli4_hba.next_rpi;
15490
15491 /*
James Smart6d368e52011-05-24 11:44:12 -040015492 * Fetch the next logical rpi. Because this index is logical,
15493 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015494 */
James Smart4902b382013-10-10 12:20:35 -040015495 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015496 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15497 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015498 rpi = LPFC_RPI_ALLOC_ERROR;
15499 else {
15500 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15501 phba->sli4_hba.max_cfg_param.rpi_used++;
15502 phba->sli4_hba.rpi_count++;
15503 }
15504
15505 /*
15506 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015507 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015508 */
15509 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15510 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015511 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015512 return rpi;
15513 }
15514
15515 /*
James Smart6d368e52011-05-24 11:44:12 -040015516 * RPI header postings are not required for SLI4 ports capable of
15517 * extents.
15518 */
15519 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015520 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015521 return rpi;
15522 }
15523
15524 /*
James Smart6fb120a2009-05-22 14:52:59 -040015525 * If the driver is running low on rpi resources, allocate another
15526 * page now. Note that the next_rpi value is used because
15527 * it represents how many are actually in use whereas max_rpi notes
15528 * how many are supported max by the device.
15529 */
James Smart6d368e52011-05-24 11:44:12 -040015530 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015531 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015532 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15533 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15534 if (!rpi_hdr) {
15535 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15536 "2002 Error Could not grow rpi "
15537 "count\n");
15538 } else {
James Smart6d368e52011-05-24 11:44:12 -040015539 lrpi = rpi_hdr->start_rpi;
15540 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015541 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15542 }
15543 }
15544
15545 return rpi;
15546}
15547
15548/**
15549 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15550 * @phba: pointer to lpfc hba data structure.
15551 *
15552 * This routine is invoked to release an rpi to the pool of
15553 * available rpis maintained by the driver.
15554 **/
15555void
James Smartd7c47992010-06-08 18:31:54 -040015556__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15557{
15558 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15559 phba->sli4_hba.rpi_count--;
15560 phba->sli4_hba.max_cfg_param.rpi_used--;
15561 }
15562}
15563
15564/**
15565 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15566 * @phba: pointer to lpfc hba data structure.
15567 *
15568 * This routine is invoked to release an rpi to the pool of
15569 * available rpis maintained by the driver.
15570 **/
15571void
James Smart6fb120a2009-05-22 14:52:59 -040015572lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15573{
15574 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015575 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015576 spin_unlock_irq(&phba->hbalock);
15577}
15578
15579/**
15580 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15581 * @phba: pointer to lpfc hba data structure.
15582 *
15583 * This routine is invoked to remove the memory region that
15584 * provided rpi via a bitmask.
15585 **/
15586void
15587lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15588{
15589 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040015590 kfree(phba->sli4_hba.rpi_ids);
15591 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040015592}
15593
15594/**
15595 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15596 * @phba: pointer to lpfc hba data structure.
15597 *
15598 * This routine is invoked to remove the memory region that
15599 * provided rpi via a bitmask.
15600 **/
15601int
James Smart6b5151f2012-01-18 16:24:06 -050015602lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15603 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040015604{
15605 LPFC_MBOXQ_t *mboxq;
15606 struct lpfc_hba *phba = ndlp->phba;
15607 int rc;
15608
15609 /* The port is notified of the header region via a mailbox command. */
15610 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15611 if (!mboxq)
15612 return -ENOMEM;
15613
15614 /* Post all rpi memory regions to the port. */
15615 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050015616 if (cmpl) {
15617 mboxq->mbox_cmpl = cmpl;
15618 mboxq->context1 = arg;
15619 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050015620 } else
15621 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050015622 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040015623 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15624 if (rc == MBX_NOT_FINISHED) {
15625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15626 "2010 Resume RPI Mailbox failed "
15627 "status %d, mbxStatus x%x\n", rc,
15628 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15629 mempool_free(mboxq, phba->mbox_mem_pool);
15630 return -EIO;
15631 }
15632 return 0;
15633}
15634
15635/**
15636 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050015637 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040015638 *
James Smart76a95d72010-11-20 23:11:48 -050015639 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040015640 *
15641 * Returns:
15642 * 0 success
15643 * -Evalue otherwise
15644 **/
15645int
James Smart76a95d72010-11-20 23:11:48 -050015646lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040015647{
15648 LPFC_MBOXQ_t *mboxq;
15649 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040015650 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040015651 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050015652 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040015653 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15654 if (!mboxq)
15655 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050015656 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040015657 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040015658 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040015659 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050015660 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040015661 "2022 INIT VPI Mailbox failed "
15662 "status %d, mbxStatus x%x\n", rc,
15663 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040015664 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040015665 }
James Smart6a9c52c2009-10-02 15:16:51 -040015666 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050015667 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040015668
15669 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040015670}
15671
15672/**
15673 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15674 * @phba: pointer to lpfc hba data structure.
15675 * @mboxq: Pointer to mailbox object.
15676 *
15677 * This routine is invoked to manually add a single FCF record. The caller
15678 * must pass a completely initialized FCF_Record. This routine takes
15679 * care of the nonembedded mailbox operations.
15680 **/
15681static void
15682lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15683{
15684 void *virt_addr;
15685 union lpfc_sli4_cfg_shdr *shdr;
15686 uint32_t shdr_status, shdr_add_status;
15687
15688 virt_addr = mboxq->sge_array->addr[0];
15689 /* The IOCTL status is embedded in the mailbox subheader. */
15690 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15691 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15692 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15693
15694 if ((shdr_status || shdr_add_status) &&
15695 (shdr_status != STATUS_FCF_IN_USE))
15696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15697 "2558 ADD_FCF_RECORD mailbox failed with "
15698 "status x%x add_status x%x\n",
15699 shdr_status, shdr_add_status);
15700
15701 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15702}
15703
15704/**
15705 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15706 * @phba: pointer to lpfc hba data structure.
15707 * @fcf_record: pointer to the initialized fcf record to add.
15708 *
15709 * This routine is invoked to manually add a single FCF record. The caller
15710 * must pass a completely initialized FCF_Record. This routine takes
15711 * care of the nonembedded mailbox operations.
15712 **/
15713int
15714lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15715{
15716 int rc = 0;
15717 LPFC_MBOXQ_t *mboxq;
15718 uint8_t *bytep;
15719 void *virt_addr;
15720 dma_addr_t phys_addr;
15721 struct lpfc_mbx_sge sge;
15722 uint32_t alloc_len, req_len;
15723 uint32_t fcfindex;
15724
15725 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15726 if (!mboxq) {
15727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15728 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15729 return -ENOMEM;
15730 }
15731
15732 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15733 sizeof(uint32_t);
15734
15735 /* Allocate DMA memory and set up the non-embedded mailbox command */
15736 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15737 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15738 req_len, LPFC_SLI4_MBX_NEMBED);
15739 if (alloc_len < req_len) {
15740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15741 "2523 Allocated DMA memory size (x%x) is "
15742 "less than the requested DMA memory "
15743 "size (x%x)\n", alloc_len, req_len);
15744 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15745 return -ENOMEM;
15746 }
15747
15748 /*
15749 * Get the first SGE entry from the non-embedded DMA memory. This
15750 * routine only uses a single SGE.
15751 */
15752 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15753 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040015754 virt_addr = mboxq->sge_array->addr[0];
15755 /*
15756 * Configure the FCF record for FCFI 0. This is the driver's
15757 * hardcoded default and gets used in nonFIP mode.
15758 */
15759 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15760 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15761 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15762
15763 /*
15764 * Copy the fcf_index and the FCF Record Data. The data starts after
15765 * the FCoE header plus word10. The data copy needs to be endian
15766 * correct.
15767 */
15768 bytep += sizeof(uint32_t);
15769 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15770 mboxq->vport = phba->pport;
15771 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15772 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15773 if (rc == MBX_NOT_FINISHED) {
15774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15775 "2515 ADD_FCF_RECORD mailbox failed with "
15776 "status 0x%x\n", rc);
15777 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15778 rc = -EIO;
15779 } else
15780 rc = 0;
15781
15782 return rc;
15783}
15784
15785/**
15786 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15787 * @phba: pointer to lpfc hba data structure.
15788 * @fcf_record: pointer to the fcf record to write the default data.
15789 * @fcf_index: FCF table entry index.
15790 *
15791 * This routine is invoked to build the driver's default FCF record. The
15792 * values used are hardcoded. This routine handles memory initialization.
15793 *
15794 **/
15795void
15796lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15797 struct fcf_record *fcf_record,
15798 uint16_t fcf_index)
15799{
15800 memset(fcf_record, 0, sizeof(struct fcf_record));
15801 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15802 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15803 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15804 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15805 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15806 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15807 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15808 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15809 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15810 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15811 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15812 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15813 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015814 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015815 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15816 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15817 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15818 /* Set the VLAN bit map */
15819 if (phba->valid_vlan) {
15820 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15821 = 1 << (phba->vlan_id % 8);
15822 }
15823}
15824
15825/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015826 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015827 * @phba: pointer to lpfc hba data structure.
15828 * @fcf_index: FCF table entry offset.
15829 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015830 * This routine is invoked to scan the entire FCF table by reading FCF
15831 * record and processing it one at a time starting from the @fcf_index
15832 * for initial FCF discovery or fast FCF failover rediscovery.
15833 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015834 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015835 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015836 **/
15837int
James Smart0c9ab6f2010-02-26 14:15:57 -050015838lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015839{
15840 int rc = 0, error;
15841 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015842
James Smart32b97932009-07-19 10:01:21 -040015843 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015844 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015845 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15846 if (!mboxq) {
15847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15848 "2000 Failed to allocate mbox for "
15849 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015850 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015851 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015852 }
James Smartecfd03c2010-02-12 14:41:27 -050015853 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015854 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015855 if (rc) {
15856 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015857 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015858 }
James Smartecfd03c2010-02-12 14:41:27 -050015859 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015860 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015861 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015862
15863 spin_lock_irq(&phba->hbalock);
15864 phba->hba_flag |= FCF_TS_INPROG;
15865 spin_unlock_irq(&phba->hbalock);
15866
James Smart6fb120a2009-05-22 14:52:59 -040015867 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015868 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015869 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015870 else {
James Smart38b92ef2010-08-04 16:11:39 -040015871 /* Reset eligible FCF count for new scan */
15872 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015873 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015874 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015875 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015876fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015877 if (error) {
15878 if (mboxq)
15879 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015880 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015881 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015882 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015883 spin_unlock_irq(&phba->hbalock);
15884 }
James Smart6fb120a2009-05-22 14:52:59 -040015885 return error;
15886}
James Smarta0c87cb2009-07-19 10:01:10 -040015887
15888/**
James Smarta93ff372010-10-22 11:06:08 -040015889 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015890 * @phba: pointer to lpfc hba data structure.
15891 * @fcf_index: FCF table entry offset.
15892 *
15893 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015894 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015895 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015896 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015897 * otherwise.
15898 **/
15899int
15900lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15901{
15902 int rc = 0, error;
15903 LPFC_MBOXQ_t *mboxq;
15904
15905 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15906 if (!mboxq) {
15907 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15908 "2763 Failed to allocate mbox for "
15909 "READ_FCF cmd\n");
15910 error = -ENOMEM;
15911 goto fail_fcf_read;
15912 }
15913 /* Construct the read FCF record mailbox command */
15914 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15915 if (rc) {
15916 error = -EINVAL;
15917 goto fail_fcf_read;
15918 }
15919 /* Issue the mailbox command asynchronously */
15920 mboxq->vport = phba->pport;
15921 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15922 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15923 if (rc == MBX_NOT_FINISHED)
15924 error = -EIO;
15925 else
15926 error = 0;
15927
15928fail_fcf_read:
15929 if (error && mboxq)
15930 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15931 return error;
15932}
15933
15934/**
15935 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15936 * @phba: pointer to lpfc hba data structure.
15937 * @fcf_index: FCF table entry offset.
15938 *
15939 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015940 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015941 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015942 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015943 * otherwise.
15944 **/
15945int
15946lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15947{
15948 int rc = 0, error;
15949 LPFC_MBOXQ_t *mboxq;
15950
15951 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15952 if (!mboxq) {
15953 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15954 "2758 Failed to allocate mbox for "
15955 "READ_FCF cmd\n");
15956 error = -ENOMEM;
15957 goto fail_fcf_read;
15958 }
15959 /* Construct the read FCF record mailbox command */
15960 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15961 if (rc) {
15962 error = -EINVAL;
15963 goto fail_fcf_read;
15964 }
15965 /* Issue the mailbox command asynchronously */
15966 mboxq->vport = phba->pport;
15967 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15968 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15969 if (rc == MBX_NOT_FINISHED)
15970 error = -EIO;
15971 else
15972 error = 0;
15973
15974fail_fcf_read:
15975 if (error && mboxq)
15976 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15977 return error;
15978}
15979
15980/**
James Smart7d791df2011-07-22 18:37:52 -040015981 * lpfc_check_next_fcf_pri
15982 * phba pointer to the lpfc_hba struct for this port.
15983 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15984 * routine when the rr_bmask is empty. The FCF indecies are put into the
15985 * rr_bmask based on their priority level. Starting from the highest priority
15986 * to the lowest. The most likely FCF candidate will be in the highest
15987 * priority group. When this routine is called it searches the fcf_pri list for
15988 * next lowest priority group and repopulates the rr_bmask with only those
15989 * fcf_indexes.
15990 * returns:
15991 * 1=success 0=failure
15992 **/
15993int
15994lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15995{
15996 uint16_t next_fcf_pri;
15997 uint16_t last_index;
15998 struct lpfc_fcf_pri *fcf_pri;
15999 int rc;
16000 int ret = 0;
16001
16002 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16003 LPFC_SLI4_FCF_TBL_INDX_MAX);
16004 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16005 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016006
16007 /* Verify the priority list has 2 or more entries */
16008 spin_lock_irq(&phba->hbalock);
16009 if (list_empty(&phba->fcf.fcf_pri_list) ||
16010 list_is_singular(&phba->fcf.fcf_pri_list)) {
16011 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016012 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16013 "3061 Last IDX %d\n", last_index);
16014 return 0; /* Empty rr list */
16015 }
James Smart25626692013-03-01 16:36:54 -050016016 spin_unlock_irq(&phba->hbalock);
16017
James Smart7d791df2011-07-22 18:37:52 -040016018 next_fcf_pri = 0;
16019 /*
16020 * Clear the rr_bmask and set all of the bits that are at this
16021 * priority.
16022 */
16023 memset(phba->fcf.fcf_rr_bmask, 0,
16024 sizeof(*phba->fcf.fcf_rr_bmask));
16025 spin_lock_irq(&phba->hbalock);
16026 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16027 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16028 continue;
16029 /*
16030 * the 1st priority that has not FLOGI failed
16031 * will be the highest.
16032 */
16033 if (!next_fcf_pri)
16034 next_fcf_pri = fcf_pri->fcf_rec.priority;
16035 spin_unlock_irq(&phba->hbalock);
16036 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16037 rc = lpfc_sli4_fcf_rr_index_set(phba,
16038 fcf_pri->fcf_rec.fcf_index);
16039 if (rc)
16040 return 0;
16041 }
16042 spin_lock_irq(&phba->hbalock);
16043 }
16044 /*
16045 * if next_fcf_pri was not set above and the list is not empty then
16046 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016047 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016048 */
16049 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16050 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16051 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16052 /*
16053 * the 1st priority that has not FLOGI failed
16054 * will be the highest.
16055 */
16056 if (!next_fcf_pri)
16057 next_fcf_pri = fcf_pri->fcf_rec.priority;
16058 spin_unlock_irq(&phba->hbalock);
16059 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16060 rc = lpfc_sli4_fcf_rr_index_set(phba,
16061 fcf_pri->fcf_rec.fcf_index);
16062 if (rc)
16063 return 0;
16064 }
16065 spin_lock_irq(&phba->hbalock);
16066 }
16067 } else
16068 ret = 1;
16069 spin_unlock_irq(&phba->hbalock);
16070
16071 return ret;
16072}
16073/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016074 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16075 * @phba: pointer to lpfc hba data structure.
16076 *
16077 * This routine is to get the next eligible FCF record index in a round
16078 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016079 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016080 * shall be returned, otherwise, the next eligible FCF record's index
16081 * shall be returned.
16082 **/
16083uint16_t
16084lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16085{
16086 uint16_t next_fcf_index;
16087
James Smart421c6622013-01-03 15:44:16 -050016088initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016089 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016090 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16091
James Smart7d791df2011-07-22 18:37:52 -040016092next_priority:
James Smart421c6622013-01-03 15:44:16 -050016093 /* Determine the next fcf index to check */
16094 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016095 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16096 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016097 next_fcf_index);
16098
James Smart0c9ab6f2010-02-26 14:15:57 -050016099 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016100 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16101 /*
16102 * If we have wrapped then we need to clear the bits that
16103 * have been tested so that we can detect when we should
16104 * change the priority level.
16105 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016106 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16107 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016108 }
16109
James Smart0c9ab6f2010-02-26 14:15:57 -050016110
James Smart3804dc82010-07-14 15:31:37 -040016111 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016112 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16113 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16114 /*
16115 * If next fcf index is not found check if there are lower
16116 * Priority level fcf's in the fcf_priority list.
16117 * Set up the rr_bmask with all of the avaiable fcf bits
16118 * at that level and continue the selection process.
16119 */
16120 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016121 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016122 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16123 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016124 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16125 return LPFC_FCOE_FCF_NEXT_NONE;
16126 else {
16127 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16128 "3063 Only FCF available idx %d, flag %x\n",
16129 next_fcf_index,
16130 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16131 return next_fcf_index;
16132 }
James Smart3804dc82010-07-14 15:31:37 -040016133 }
16134
James Smart7d791df2011-07-22 18:37:52 -040016135 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16136 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
16137 LPFC_FCF_FLOGI_FAILED)
16138 goto next_priority;
16139
James Smart3804dc82010-07-14 15:31:37 -040016140 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016141 "2845 Get next roundrobin failover FCF (x%x)\n",
16142 next_fcf_index);
16143
James Smart0c9ab6f2010-02-26 14:15:57 -050016144 return next_fcf_index;
16145}
16146
16147/**
16148 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16149 * @phba: pointer to lpfc hba data structure.
16150 *
16151 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016152 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016153 * does not go beyond the range of the driver allocated bmask dimension
16154 * before setting the bit.
16155 *
16156 * Returns 0 if the index bit successfully set, otherwise, it returns
16157 * -EINVAL.
16158 **/
16159int
16160lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16161{
16162 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16163 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016164 "2610 FCF (x%x) reached driver's book "
16165 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016166 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16167 return -EINVAL;
16168 }
16169 /* Set the eligible FCF record index bmask */
16170 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16171
James Smart3804dc82010-07-14 15:31:37 -040016172 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016173 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016174 "bmask\n", fcf_index);
16175
James Smart0c9ab6f2010-02-26 14:15:57 -050016176 return 0;
16177}
16178
16179/**
James Smart3804dc82010-07-14 15:31:37 -040016180 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016181 * @phba: pointer to lpfc hba data structure.
16182 *
16183 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016184 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016185 * does not go beyond the range of the driver allocated bmask dimension
16186 * before clearing the bit.
16187 **/
16188void
16189lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16190{
James Smart9a803a72013-09-06 12:17:56 -040016191 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016192 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16193 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016194 "2762 FCF (x%x) reached driver's book "
16195 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016196 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16197 return;
16198 }
16199 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016200 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016201 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16202 list) {
James Smart7d791df2011-07-22 18:37:52 -040016203 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16204 list_del_init(&fcf_pri->list);
16205 break;
16206 }
16207 }
16208 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016209 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016210
16211 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016212 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016213 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016214}
16215
16216/**
James Smartecfd03c2010-02-12 14:41:27 -050016217 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16218 * @phba: pointer to lpfc hba data structure.
16219 *
16220 * This routine is the completion routine for the rediscover FCF table mailbox
16221 * command. If the mailbox command returned failure, it will try to stop the
16222 * FCF rediscover wait timer.
16223 **/
16224void
16225lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16226{
16227 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16228 uint32_t shdr_status, shdr_add_status;
16229
16230 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16231
16232 shdr_status = bf_get(lpfc_mbox_hdr_status,
16233 &redisc_fcf->header.cfg_shdr.response);
16234 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16235 &redisc_fcf->header.cfg_shdr.response);
16236 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016237 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016238 "2746 Requesting for FCF rediscovery failed "
16239 "status x%x add_status x%x\n",
16240 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016241 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016242 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016243 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016244 spin_unlock_irq(&phba->hbalock);
16245 /*
16246 * CVL event triggered FCF rediscover request failed,
16247 * last resort to re-try current registered FCF entry.
16248 */
16249 lpfc_retry_pport_discovery(phba);
16250 } else {
16251 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016252 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016253 spin_unlock_irq(&phba->hbalock);
16254 /*
16255 * DEAD FCF event triggered FCF rediscover request
16256 * failed, last resort to fail over as a link down
16257 * to FCF registration.
16258 */
16259 lpfc_sli4_fcf_dead_failthrough(phba);
16260 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016261 } else {
16262 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016263 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016264 /*
16265 * Start FCF rediscovery wait timer for pending FCF
16266 * before rescan FCF record table.
16267 */
16268 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016269 }
James Smartecfd03c2010-02-12 14:41:27 -050016270
16271 mempool_free(mbox, phba->mbox_mem_pool);
16272}
16273
16274/**
James Smart3804dc82010-07-14 15:31:37 -040016275 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016276 * @phba: pointer to lpfc hba data structure.
16277 *
16278 * This routine is invoked to request for rediscovery of the entire FCF table
16279 * by the port.
16280 **/
16281int
16282lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16283{
16284 LPFC_MBOXQ_t *mbox;
16285 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16286 int rc, length;
16287
James Smart0c9ab6f2010-02-26 14:15:57 -050016288 /* Cancel retry delay timers to all vports before FCF rediscover */
16289 lpfc_cancel_all_vport_retry_delay_timer(phba);
16290
James Smartecfd03c2010-02-12 14:41:27 -050016291 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16292 if (!mbox) {
16293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16294 "2745 Failed to allocate mbox for "
16295 "requesting FCF rediscover.\n");
16296 return -ENOMEM;
16297 }
16298
16299 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16300 sizeof(struct lpfc_sli4_cfg_mhdr));
16301 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16302 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16303 length, LPFC_SLI4_MBX_EMBED);
16304
16305 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16306 /* Set count to 0 for invalidating the entire FCF database */
16307 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16308
16309 /* Issue the mailbox command asynchronously */
16310 mbox->vport = phba->pport;
16311 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16312 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16313
16314 if (rc == MBX_NOT_FINISHED) {
16315 mempool_free(mbox, phba->mbox_mem_pool);
16316 return -EIO;
16317 }
16318 return 0;
16319}
16320
16321/**
James Smartfc2b9892010-02-26 14:15:29 -050016322 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16323 * @phba: pointer to lpfc hba data structure.
16324 *
16325 * This function is the failover routine as a last resort to the FCF DEAD
16326 * event when driver failed to perform fast FCF failover.
16327 **/
16328void
16329lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16330{
16331 uint32_t link_state;
16332
16333 /*
16334 * Last resort as FCF DEAD event failover will treat this as
16335 * a link down, but save the link state because we don't want
16336 * it to be changed to Link Down unless it is already down.
16337 */
16338 link_state = phba->link_state;
16339 lpfc_linkdown(phba);
16340 phba->link_state = link_state;
16341
16342 /* Unregister FCF if no devices connected to it */
16343 lpfc_unregister_unused_fcf(phba);
16344}
16345
16346/**
James Smart026abb82011-12-13 13:20:45 -050016347 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016348 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016349 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016350 *
James Smart026abb82011-12-13 13:20:45 -050016351 * This function gets SLI3 port configure region 23 data through memory dump
16352 * mailbox command. When it successfully retrieves data, the size of the data
16353 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016354 **/
James Smart026abb82011-12-13 13:20:45 -050016355static uint32_t
16356lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016357{
16358 LPFC_MBOXQ_t *pmb = NULL;
16359 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016360 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016361 int rc;
16362
James Smart026abb82011-12-13 13:20:45 -050016363 if (!rgn23_data)
16364 return 0;
16365
James Smarta0c87cb2009-07-19 10:01:10 -040016366 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16367 if (!pmb) {
16368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016369 "2600 failed to allocate mailbox memory\n");
16370 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016371 }
16372 mb = &pmb->u.mb;
16373
James Smarta0c87cb2009-07-19 10:01:10 -040016374 do {
16375 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16376 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16377
16378 if (rc != MBX_SUCCESS) {
16379 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016380 "2601 failed to read config "
16381 "region 23, rc 0x%x Status 0x%x\n",
16382 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016383 mb->un.varDmp.word_cnt = 0;
16384 }
16385 /*
16386 * dump mem may return a zero when finished or we got a
16387 * mailbox error, either way we are done.
16388 */
16389 if (mb->un.varDmp.word_cnt == 0)
16390 break;
16391 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16392 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16393
16394 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016395 rgn23_data + offset,
16396 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016397 offset += mb->un.varDmp.word_cnt;
16398 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16399
James Smart026abb82011-12-13 13:20:45 -050016400 mempool_free(pmb, phba->mbox_mem_pool);
16401 return offset;
16402}
16403
16404/**
16405 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16406 * @phba: pointer to lpfc hba data structure.
16407 * @rgn23_data: pointer to configure region 23 data.
16408 *
16409 * This function gets SLI4 port configure region 23 data through memory dump
16410 * mailbox command. When it successfully retrieves data, the size of the data
16411 * will be returned, otherwise, 0 will be returned.
16412 **/
16413static uint32_t
16414lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16415{
16416 LPFC_MBOXQ_t *mboxq = NULL;
16417 struct lpfc_dmabuf *mp = NULL;
16418 struct lpfc_mqe *mqe;
16419 uint32_t data_length = 0;
16420 int rc;
16421
16422 if (!rgn23_data)
16423 return 0;
16424
16425 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16426 if (!mboxq) {
16427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16428 "3105 failed to allocate mailbox memory\n");
16429 return 0;
16430 }
16431
16432 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16433 goto out;
16434 mqe = &mboxq->u.mqe;
16435 mp = (struct lpfc_dmabuf *) mboxq->context1;
16436 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16437 if (rc)
16438 goto out;
16439 data_length = mqe->un.mb_words[5];
16440 if (data_length == 0)
16441 goto out;
16442 if (data_length > DMP_RGN23_SIZE) {
16443 data_length = 0;
16444 goto out;
16445 }
16446 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16447out:
16448 mempool_free(mboxq, phba->mbox_mem_pool);
16449 if (mp) {
16450 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16451 kfree(mp);
16452 }
16453 return data_length;
16454}
16455
16456/**
16457 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16458 * @phba: pointer to lpfc hba data structure.
16459 *
16460 * This function read region 23 and parse TLV for port status to
16461 * decide if the user disaled the port. If the TLV indicates the
16462 * port is disabled, the hba_flag is set accordingly.
16463 **/
16464void
16465lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16466{
16467 uint8_t *rgn23_data = NULL;
16468 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16469 uint32_t offset = 0;
16470
16471 /* Get adapter Region 23 data */
16472 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16473 if (!rgn23_data)
16474 goto out;
16475
16476 if (phba->sli_rev < LPFC_SLI_REV4)
16477 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16478 else {
16479 if_type = bf_get(lpfc_sli_intf_if_type,
16480 &phba->sli4_hba.sli_intf);
16481 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16482 goto out;
16483 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16484 }
James Smarta0c87cb2009-07-19 10:01:10 -040016485
16486 if (!data_size)
16487 goto out;
16488
16489 /* Check the region signature first */
16490 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16492 "2619 Config region 23 has bad signature\n");
16493 goto out;
16494 }
16495 offset += 4;
16496
16497 /* Check the data structure version */
16498 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16500 "2620 Config region 23 has bad version\n");
16501 goto out;
16502 }
16503 offset += 4;
16504
16505 /* Parse TLV entries in the region */
16506 while (offset < data_size) {
16507 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16508 break;
16509 /*
16510 * If the TLV is not driver specific TLV or driver id is
16511 * not linux driver id, skip the record.
16512 */
16513 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16514 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16515 (rgn23_data[offset + 3] != 0)) {
16516 offset += rgn23_data[offset + 1] * 4 + 4;
16517 continue;
16518 }
16519
16520 /* Driver found a driver specific TLV in the config region */
16521 sub_tlv_len = rgn23_data[offset + 1] * 4;
16522 offset += 4;
16523 tlv_offset = 0;
16524
16525 /*
16526 * Search for configured port state sub-TLV.
16527 */
16528 while ((offset < data_size) &&
16529 (tlv_offset < sub_tlv_len)) {
16530 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16531 offset += 4;
16532 tlv_offset += 4;
16533 break;
16534 }
16535 if (rgn23_data[offset] != PORT_STE_TYPE) {
16536 offset += rgn23_data[offset + 1] * 4 + 4;
16537 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16538 continue;
16539 }
16540
16541 /* This HBA contains PORT_STE configured */
16542 if (!rgn23_data[offset + 2])
16543 phba->hba_flag |= LINK_DISABLED;
16544
16545 goto out;
16546 }
16547 }
James Smart026abb82011-12-13 13:20:45 -050016548
James Smarta0c87cb2009-07-19 10:01:10 -040016549out:
James Smarta0c87cb2009-07-19 10:01:10 -040016550 kfree(rgn23_data);
16551 return;
16552}
James Smart695a8142010-01-26 23:08:03 -050016553
16554/**
James Smart52d52442011-05-24 11:42:45 -040016555 * lpfc_wr_object - write an object to the firmware
16556 * @phba: HBA structure that indicates port to create a queue on.
16557 * @dmabuf_list: list of dmabufs to write to the port.
16558 * @size: the total byte value of the objects to write to the port.
16559 * @offset: the current offset to be used to start the transfer.
16560 *
16561 * This routine will create a wr_object mailbox command to send to the port.
16562 * the mailbox command will be constructed using the dma buffers described in
16563 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16564 * BDEs that the imbedded mailbox can support. The @offset variable will be
16565 * used to indicate the starting offset of the transfer and will also return
16566 * the offset after the write object mailbox has completed. @size is used to
16567 * determine the end of the object and whether the eof bit should be set.
16568 *
16569 * Return 0 is successful and offset will contain the the new offset to use
16570 * for the next write.
16571 * Return negative value for error cases.
16572 **/
16573int
16574lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16575 uint32_t size, uint32_t *offset)
16576{
16577 struct lpfc_mbx_wr_object *wr_object;
16578 LPFC_MBOXQ_t *mbox;
16579 int rc = 0, i = 0;
16580 uint32_t shdr_status, shdr_add_status;
16581 uint32_t mbox_tmo;
16582 union lpfc_sli4_cfg_shdr *shdr;
16583 struct lpfc_dmabuf *dmabuf;
16584 uint32_t written = 0;
16585
16586 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16587 if (!mbox)
16588 return -ENOMEM;
16589
16590 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16591 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16592 sizeof(struct lpfc_mbx_wr_object) -
16593 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16594
16595 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16596 wr_object->u.request.write_offset = *offset;
16597 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16598 wr_object->u.request.object_name[0] =
16599 cpu_to_le32(wr_object->u.request.object_name[0]);
16600 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16601 list_for_each_entry(dmabuf, dmabuf_list, list) {
16602 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16603 break;
16604 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16605 wr_object->u.request.bde[i].addrHigh =
16606 putPaddrHigh(dmabuf->phys);
16607 if (written + SLI4_PAGE_SIZE >= size) {
16608 wr_object->u.request.bde[i].tus.f.bdeSize =
16609 (size - written);
16610 written += (size - written);
16611 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16612 } else {
16613 wr_object->u.request.bde[i].tus.f.bdeSize =
16614 SLI4_PAGE_SIZE;
16615 written += SLI4_PAGE_SIZE;
16616 }
16617 i++;
16618 }
16619 wr_object->u.request.bde_count = i;
16620 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16621 if (!phba->sli4_hba.intr_enable)
16622 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16623 else {
James Smarta183a152011-10-10 21:32:43 -040016624 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040016625 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16626 }
16627 /* The IOCTL status is embedded in the mailbox subheader. */
16628 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16629 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16630 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16631 if (rc != MBX_TIMEOUT)
16632 mempool_free(mbox, phba->mbox_mem_pool);
16633 if (shdr_status || shdr_add_status || rc) {
16634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16635 "3025 Write Object mailbox failed with "
16636 "status x%x add_status x%x, mbx status x%x\n",
16637 shdr_status, shdr_add_status, rc);
16638 rc = -ENXIO;
16639 } else
16640 *offset += wr_object->u.response.actual_write_length;
16641 return rc;
16642}
16643
16644/**
James Smart695a8142010-01-26 23:08:03 -050016645 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16646 * @vport: pointer to vport data structure.
16647 *
16648 * This function iterate through the mailboxq and clean up all REG_LOGIN
16649 * and REG_VPI mailbox commands associated with the vport. This function
16650 * is called when driver want to restart discovery of the vport due to
16651 * a Clear Virtual Link event.
16652 **/
16653void
16654lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16655{
16656 struct lpfc_hba *phba = vport->phba;
16657 LPFC_MBOXQ_t *mb, *nextmb;
16658 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040016659 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040016660 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040016661 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040016662 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050016663 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050016664
James Smartd439d282010-09-29 11:18:45 -040016665 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050016666 spin_lock_irq(&phba->hbalock);
16667 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16668 if (mb->vport != vport)
16669 continue;
16670
16671 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16672 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16673 continue;
16674
James Smartd439d282010-09-29 11:18:45 -040016675 list_del(&mb->list);
16676 list_add_tail(&mb->list, &mbox_cmd_list);
16677 }
16678 /* Clean up active mailbox command with the vport */
16679 mb = phba->sli.mbox_active;
16680 if (mb && (mb->vport == vport)) {
16681 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16682 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16683 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16684 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16685 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16686 /* Put reference count for delayed processing */
16687 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16688 /* Unregister the RPI when mailbox complete */
16689 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16690 }
16691 }
James Smart63e801c2010-11-20 23:14:19 -050016692 /* Cleanup any mailbox completions which are not yet processed */
16693 do {
16694 restart_loop = 0;
16695 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16696 /*
16697 * If this mailox is already processed or it is
16698 * for another vport ignore it.
16699 */
16700 if ((mb->vport != vport) ||
16701 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16702 continue;
16703
16704 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16705 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16706 continue;
16707
16708 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16709 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16710 ndlp = (struct lpfc_nodelist *)mb->context2;
16711 /* Unregister the RPI when mailbox complete */
16712 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16713 restart_loop = 1;
16714 spin_unlock_irq(&phba->hbalock);
16715 spin_lock(shost->host_lock);
16716 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16717 spin_unlock(shost->host_lock);
16718 spin_lock_irq(&phba->hbalock);
16719 break;
16720 }
16721 }
16722 } while (restart_loop);
16723
James Smartd439d282010-09-29 11:18:45 -040016724 spin_unlock_irq(&phba->hbalock);
16725
16726 /* Release the cleaned-up mailbox commands */
16727 while (!list_empty(&mbox_cmd_list)) {
16728 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050016729 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16730 mp = (struct lpfc_dmabuf *) (mb->context1);
16731 if (mp) {
16732 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16733 kfree(mp);
16734 }
James Smart78730cf2010-04-06 15:06:30 -040016735 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040016736 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040016737 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016738 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040016739 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016740 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040016741 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040016742 }
James Smart695a8142010-01-26 23:08:03 -050016743 }
James Smart695a8142010-01-26 23:08:03 -050016744 mempool_free(mb, phba->mbox_mem_pool);
16745 }
James Smartd439d282010-09-29 11:18:45 -040016746
16747 /* Release the ndlp with the cleaned-up active mailbox command */
16748 if (act_mbx_ndlp) {
16749 spin_lock(shost->host_lock);
16750 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16751 spin_unlock(shost->host_lock);
16752 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050016753 }
James Smart695a8142010-01-26 23:08:03 -050016754}
16755
James Smart2a9bf3d2010-06-07 15:24:45 -040016756/**
16757 * lpfc_drain_txq - Drain the txq
16758 * @phba: Pointer to HBA context object.
16759 *
16760 * This function attempt to submit IOCBs on the txq
16761 * to the adapter. For SLI4 adapters, the txq contains
16762 * ELS IOCBs that have been deferred because the there
16763 * are no SGLs. This congestion can occur with large
16764 * vport counts during node discovery.
16765 **/
16766
16767uint32_t
16768lpfc_drain_txq(struct lpfc_hba *phba)
16769{
16770 LIST_HEAD(completions);
16771 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16772 struct lpfc_iocbq *piocbq = 0;
16773 unsigned long iflags = 0;
16774 char *fail_msg = NULL;
16775 struct lpfc_sglq *sglq;
16776 union lpfc_wqe wqe;
James Smart0e9bb8d2013-03-01 16:35:12 -050016777 int txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040016778
James Smart398d81c2013-05-31 17:04:19 -040016779 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050016780 list_for_each_entry(piocbq, &pring->txq, list) {
16781 txq_cnt++;
16782 }
16783
16784 if (txq_cnt > pring->txq_max)
16785 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016786
James Smart398d81c2013-05-31 17:04:19 -040016787 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016788
James Smart0e9bb8d2013-03-01 16:35:12 -050016789 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040016790 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016791
James Smart19ca7602010-11-20 23:11:55 -050016792 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016793 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040016794 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040016795 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16796 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050016797 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040016798 break;
16799 }
James Smart19ca7602010-11-20 23:11:55 -050016800 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016801 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016802 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040016803 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016804 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016805 }
James Smart0e9bb8d2013-03-01 16:35:12 -050016806 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040016807
16808 /* The xri and iocb resources secured,
16809 * attempt to issue request
16810 */
James Smart6d368e52011-05-24 11:44:12 -040016811 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016812 piocbq->sli4_xritag = sglq->sli4_xritag;
16813 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16814 fail_msg = "to convert bpl to sgl";
16815 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16816 fail_msg = "to convert iocb to wqe";
16817 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16818 fail_msg = " - Wq is full";
16819 else
16820 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16821
16822 if (fail_msg) {
16823 /* Failed means we can't issue and need to cancel */
16824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16825 "2822 IOCB failed %s iotag 0x%x "
16826 "xri 0x%x\n",
16827 fail_msg,
16828 piocbq->iotag, piocbq->sli4_xritag);
16829 list_add_tail(&piocbq->list, &completions);
16830 }
James Smart398d81c2013-05-31 17:04:19 -040016831 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016832 }
16833
James Smart2a9bf3d2010-06-07 15:24:45 -040016834 /* Cancel all the IOCBs that cannot be issued */
16835 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16836 IOERR_SLI_ABORTED);
16837
James Smart0e9bb8d2013-03-01 16:35:12 -050016838 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016839}