blob: 3850949c8a793ee66c76b6f3529801d3ed072b3a [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 Smart6d368e52011-05-24 11:44:12 -0400638 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 Smart6d368e52011-05-24 11:44:12 -0400816 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500817 return 1;
818 else
819 return 0;
820}
821
822/**
823 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
824 * @phba: Pointer to HBA context object.
825 * @ndlp: nodelist pointer for this target.
826 * @xritag: xri used in this exchange.
827 * @rxid: Remote Exchange ID.
828 * @send_rrq: Flag used to determine if we should send rrq els cmd.
829 *
830 * This function takes the hbalock.
831 * The active bit is always set in the active rrq xri_bitmap even
832 * if there is no slot avaiable for the other rrq information.
833 *
834 * returns 0 rrq actived for this xri
835 * < 0 No memory or invalid ndlp.
836 **/
837int
838lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500839 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500840{
James Smart19ca7602010-11-20 23:11:55 -0500841 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500842 struct lpfc_node_rrq *rrq;
843 int empty;
844
845 if (!ndlp)
846 return -EINVAL;
847
848 if (!phba->cfg_enable_rrq)
849 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500850
851 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500852 if (phba->pport->load_flag & FC_UNLOADING) {
853 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
854 goto out;
855 }
856
857 /*
858 * set the active bit even if there is no mem available.
859 */
860 if (NLP_CHK_FREE_REQ(ndlp))
861 goto out;
862
863 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
864 goto out;
865
866 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
867 goto out;
868
James Smart19ca7602010-11-20 23:11:55 -0500869 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500870 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
871 if (!rrq) {
872 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
873 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
874 " DID:0x%x Send:%d\n",
875 xritag, rxid, ndlp->nlp_DID, send_rrq);
876 return -EINVAL;
877 }
James Smarte5771b42013-03-01 16:37:14 -0500878 if (phba->cfg_enable_rrq == 1)
879 rrq->send_rrq = send_rrq;
880 else
881 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500882 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400883 rrq->rrq_stop_time = jiffies +
884 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500885 rrq->ndlp = ndlp;
886 rrq->nlp_DID = ndlp->nlp_DID;
887 rrq->vport = ndlp->vport;
888 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500889 spin_lock_irqsave(&phba->hbalock, iflags);
890 empty = list_empty(&phba->active_rrq_list);
891 list_add_tail(&rrq->list, &phba->active_rrq_list);
892 phba->hba_flag |= HBA_RRQ_ACTIVE;
893 if (empty)
894 lpfc_worker_wake_up(phba);
895 spin_unlock_irqrestore(&phba->hbalock, iflags);
896 return 0;
897out:
898 spin_unlock_irqrestore(&phba->hbalock, iflags);
899 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
900 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
901 " DID:0x%x Send:%d\n",
902 xritag, rxid, ndlp->nlp_DID, send_rrq);
903 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500904}
905
906/**
James Smartda0436e2009-05-22 14:51:39 -0400907 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
908 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500909 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400910 *
911 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400912 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400913 * list is not empty then it is successful, it returns pointer to the newly
914 * allocated sglq object else it returns NULL.
915 **/
916static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500917__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400918{
919 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
920 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500921 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500922 struct lpfc_scsi_buf *lpfc_cmd;
923 struct lpfc_nodelist *ndlp;
924 int found = 0;
925
926 if (piocbq->iocb_flag & LPFC_IO_FCP) {
927 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
928 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500929 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
930 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500931 ndlp = piocbq->context_un.ndlp;
James Smartd5ce53b2013-04-17 20:17:26 -0400932 else if (piocbq->iocb_flag & LPFC_IO_LIBDFC)
James Smart93d1379e2012-05-09 21:19:34 -0400933 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500934 else
935 ndlp = piocbq->context1;
936
James Smartda0436e2009-05-22 14:51:39 -0400937 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500938 start_sglq = sglq;
939 while (!found) {
940 if (!sglq)
941 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400942 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500943 /* This xri has an rrq outstanding for this DID.
944 * put it back in the list and get another xri.
945 */
946 list_add_tail(&sglq->list, lpfc_sgl_list);
947 sglq = NULL;
948 list_remove_head(lpfc_sgl_list, sglq,
949 struct lpfc_sglq, list);
950 if (sglq == start_sglq) {
951 sglq = NULL;
952 break;
953 } else
954 continue;
955 }
956 sglq->ndlp = ndlp;
957 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400958 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500959 sglq->state = SGL_ALLOCATED;
960 }
James Smartda0436e2009-05-22 14:51:39 -0400961 return sglq;
962}
963
964/**
James Smart3621a712009-04-06 18:47:14 -0400965 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400966 * @phba: Pointer to HBA context object.
967 *
968 * This function is called with no lock held. This function
969 * allocates a new driver iocb object from the iocb pool. If the
970 * allocation is successful, it returns pointer to the newly
971 * allocated iocb object else it returns NULL.
972 **/
James Smart2e0fef82007-06-17 19:56:36 -0500973struct lpfc_iocbq *
974lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500975{
James Smart2e0fef82007-06-17 19:56:36 -0500976 struct lpfc_iocbq * iocbq = NULL;
977 unsigned long iflags;
978
979 spin_lock_irqsave(&phba->hbalock, iflags);
980 iocbq = __lpfc_sli_get_iocbq(phba);
981 spin_unlock_irqrestore(&phba->hbalock, iflags);
982 return iocbq;
983}
984
James Smarte59058c2008-08-24 21:49:00 -0400985/**
James Smart4f774512009-05-22 14:52:35 -0400986 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
987 * @phba: Pointer to HBA context object.
988 * @iocbq: Pointer to driver iocb object.
989 *
990 * This function is called with hbalock held to release driver
991 * iocb object to the iocb pool. The iotag in the iocb object
992 * does not change for each use of the iocb object. This function
993 * clears all other fields of the iocb object when it is freed.
994 * The sqlq structure that holds the xritag and phys and virtual
995 * mappings for the scatter gather list is retrieved from the
996 * active array of sglq. The get of the sglq pointer also clears
997 * the entry in the array. If the status of the IO indiactes that
998 * this IO was aborted then the sglq entry it put on the
999 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1000 * IO has good status or fails for any other reason then the sglq
1001 * entry is added to the free list (lpfc_sgl_list).
1002 **/
1003static void
1004__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1005{
1006 struct lpfc_sglq *sglq;
1007 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001008 unsigned long iflag = 0;
1009 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001010
1011 if (iocbq->sli4_xritag == NO_XRI)
1012 sglq = NULL;
1013 else
James Smart6d368e52011-05-24 11:44:12 -04001014 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1015
James Smart0e9bb8d2013-03-01 16:35:12 -05001016
James Smart4f774512009-05-22 14:52:35 -04001017 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001018 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1019 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001020 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1021 iflag);
1022 list_add(&sglq->list,
1023 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1024 spin_unlock_irqrestore(
1025 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001026 } else {
1027 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001028 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001029 list_add_tail(&sglq->list,
1030 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -04001031
1032 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001033 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001034 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001035 }
James Smart4f774512009-05-22 14:52:35 -04001036 }
1037
1038
1039 /*
1040 * Clean all volatile data fields, preserve iotag and node struct.
1041 */
1042 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001043 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001044 iocbq->sli4_xritag = NO_XRI;
1045 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1046}
1047
James Smart2a9bf3d2010-06-07 15:24:45 -04001048
James Smart4f774512009-05-22 14:52:35 -04001049/**
James Smart3772a992009-05-22 14:50:54 -04001050 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1051 * @phba: Pointer to HBA context object.
1052 * @iocbq: Pointer to driver iocb object.
1053 *
1054 * This function is called with hbalock held to release driver
1055 * iocb object to the iocb pool. The iotag in the iocb object
1056 * does not change for each use of the iocb object. This function
1057 * clears all other fields of the iocb object when it is freed.
1058 **/
1059static void
1060__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1061{
1062 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1063
James Smart0e9bb8d2013-03-01 16:35:12 -05001064
1065 /*
James Smart3772a992009-05-22 14:50:54 -04001066 * Clean all volatile data fields, preserve iotag and node struct.
1067 */
1068 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1069 iocbq->sli4_xritag = NO_XRI;
1070 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1071}
1072
1073/**
James Smart3621a712009-04-06 18:47:14 -04001074 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001075 * @phba: Pointer to HBA context object.
1076 * @iocbq: Pointer to driver iocb object.
1077 *
1078 * This function is called with hbalock held to release driver
1079 * iocb object to the iocb pool. The iotag in the iocb object
1080 * does not change for each use of the iocb object. This function
1081 * clears all other fields of the iocb object when it is freed.
1082 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001083static void
James Smart2e0fef82007-06-17 19:56:36 -05001084__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1085{
James Smart3772a992009-05-22 14:50:54 -04001086 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001087 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001088}
1089
James Smarte59058c2008-08-24 21:49:00 -04001090/**
James Smart3621a712009-04-06 18:47:14 -04001091 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001092 * @phba: Pointer to HBA context object.
1093 * @iocbq: Pointer to driver iocb object.
1094 *
1095 * This function is called with no lock held to release the iocb to
1096 * iocb pool.
1097 **/
James Smart2e0fef82007-06-17 19:56:36 -05001098void
1099lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1100{
1101 unsigned long iflags;
1102
1103 /*
1104 * Clean all volatile data fields, preserve iotag and node struct.
1105 */
1106 spin_lock_irqsave(&phba->hbalock, iflags);
1107 __lpfc_sli_release_iocbq(phba, iocbq);
1108 spin_unlock_irqrestore(&phba->hbalock, iflags);
1109}
1110
James Smarte59058c2008-08-24 21:49:00 -04001111/**
James Smarta257bf92009-04-06 18:48:10 -04001112 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1113 * @phba: Pointer to HBA context object.
1114 * @iocblist: List of IOCBs.
1115 * @ulpstatus: ULP status in IOCB command field.
1116 * @ulpWord4: ULP word-4 in IOCB command field.
1117 *
1118 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1119 * on the list by invoking the complete callback function associated with the
1120 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1121 * fields.
1122 **/
1123void
1124lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1125 uint32_t ulpstatus, uint32_t ulpWord4)
1126{
1127 struct lpfc_iocbq *piocb;
1128
1129 while (!list_empty(iocblist)) {
1130 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001131 if (!piocb->iocb_cmpl)
1132 lpfc_sli_release_iocbq(phba, piocb);
1133 else {
1134 piocb->iocb.ulpStatus = ulpstatus;
1135 piocb->iocb.un.ulpWord[4] = ulpWord4;
1136 (piocb->iocb_cmpl) (phba, piocb, piocb);
1137 }
1138 }
1139 return;
1140}
1141
1142/**
James Smart3621a712009-04-06 18:47:14 -04001143 * lpfc_sli_iocb_cmd_type - Get the iocb type
1144 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001145 *
1146 * This function is called by ring event handler function to get the iocb type.
1147 * This function translates the iocb command to an iocb command type used to
1148 * decide the final disposition of each completed IOCB.
1149 * The function returns
1150 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1151 * LPFC_SOL_IOCB if it is a solicited iocb completion
1152 * LPFC_ABORT_IOCB if it is an abort iocb
1153 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1154 *
1155 * The caller is not required to hold any lock.
1156 **/
dea31012005-04-17 16:05:31 -05001157static lpfc_iocb_type
1158lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1159{
1160 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1161
1162 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1163 return 0;
1164
1165 switch (iocb_cmnd) {
1166 case CMD_XMIT_SEQUENCE_CR:
1167 case CMD_XMIT_SEQUENCE_CX:
1168 case CMD_XMIT_BCAST_CN:
1169 case CMD_XMIT_BCAST_CX:
1170 case CMD_ELS_REQUEST_CR:
1171 case CMD_ELS_REQUEST_CX:
1172 case CMD_CREATE_XRI_CR:
1173 case CMD_CREATE_XRI_CX:
1174 case CMD_GET_RPI_CN:
1175 case CMD_XMIT_ELS_RSP_CX:
1176 case CMD_GET_RPI_CR:
1177 case CMD_FCP_IWRITE_CR:
1178 case CMD_FCP_IWRITE_CX:
1179 case CMD_FCP_IREAD_CR:
1180 case CMD_FCP_IREAD_CX:
1181 case CMD_FCP_ICMND_CR:
1182 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001183 case CMD_FCP_TSEND_CX:
1184 case CMD_FCP_TRSP_CX:
1185 case CMD_FCP_TRECEIVE_CX:
1186 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001187 case CMD_ADAPTER_MSG:
1188 case CMD_ADAPTER_DUMP:
1189 case CMD_XMIT_SEQUENCE64_CR:
1190 case CMD_XMIT_SEQUENCE64_CX:
1191 case CMD_XMIT_BCAST64_CN:
1192 case CMD_XMIT_BCAST64_CX:
1193 case CMD_ELS_REQUEST64_CR:
1194 case CMD_ELS_REQUEST64_CX:
1195 case CMD_FCP_IWRITE64_CR:
1196 case CMD_FCP_IWRITE64_CX:
1197 case CMD_FCP_IREAD64_CR:
1198 case CMD_FCP_IREAD64_CX:
1199 case CMD_FCP_ICMND64_CR:
1200 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001201 case CMD_FCP_TSEND64_CX:
1202 case CMD_FCP_TRSP64_CX:
1203 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001204 case CMD_GEN_REQUEST64_CR:
1205 case CMD_GEN_REQUEST64_CX:
1206 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001207 case DSSCMD_IWRITE64_CR:
1208 case DSSCMD_IWRITE64_CX:
1209 case DSSCMD_IREAD64_CR:
1210 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001211 type = LPFC_SOL_IOCB;
1212 break;
1213 case CMD_ABORT_XRI_CN:
1214 case CMD_ABORT_XRI_CX:
1215 case CMD_CLOSE_XRI_CN:
1216 case CMD_CLOSE_XRI_CX:
1217 case CMD_XRI_ABORTED_CX:
1218 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001219 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001220 type = LPFC_ABORT_IOCB;
1221 break;
1222 case CMD_RCV_SEQUENCE_CX:
1223 case CMD_RCV_ELS_REQ_CX:
1224 case CMD_RCV_SEQUENCE64_CX:
1225 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001226 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001227 case CMD_IOCB_RCV_SEQ64_CX:
1228 case CMD_IOCB_RCV_ELS64_CX:
1229 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001230 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001231 type = LPFC_UNSOL_IOCB;
1232 break;
James Smart3163f722008-02-08 18:50:25 -05001233 case CMD_IOCB_XMIT_MSEQ64_CR:
1234 case CMD_IOCB_XMIT_MSEQ64_CX:
1235 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1236 case CMD_IOCB_RCV_ELS_LIST64_CX:
1237 case CMD_IOCB_CLOSE_EXTENDED_CN:
1238 case CMD_IOCB_ABORT_EXTENDED_CN:
1239 case CMD_IOCB_RET_HBQE64_CN:
1240 case CMD_IOCB_FCP_IBIDIR64_CR:
1241 case CMD_IOCB_FCP_IBIDIR64_CX:
1242 case CMD_IOCB_FCP_ITASKMGT64_CX:
1243 case CMD_IOCB_LOGENTRY_CN:
1244 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1245 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001246 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001247 type = LPFC_UNKNOWN_IOCB;
1248 break;
dea31012005-04-17 16:05:31 -05001249 default:
1250 type = LPFC_UNKNOWN_IOCB;
1251 break;
1252 }
1253
1254 return type;
1255}
1256
James Smarte59058c2008-08-24 21:49:00 -04001257/**
James Smart3621a712009-04-06 18:47:14 -04001258 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001259 * @phba: Pointer to HBA context object.
1260 *
1261 * This function is called from SLI initialization code
1262 * to configure every ring of the HBA's SLI interface. The
1263 * caller is not required to hold any lock. This function issues
1264 * a config_ring mailbox command for each ring.
1265 * This function returns zero if successful else returns a negative
1266 * error code.
1267 **/
dea31012005-04-17 16:05:31 -05001268static int
James Smarted957682007-06-17 19:56:37 -05001269lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001270{
1271 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001272 LPFC_MBOXQ_t *pmb;
1273 MAILBOX_t *pmbox;
1274 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001275
James Smarted957682007-06-17 19:56:37 -05001276 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1277 if (!pmb)
1278 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001279 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001280 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001281 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001282 lpfc_config_ring(phba, i, pmb);
1283 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1284 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001286 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001287 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1288 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001289 rc, pmbox->mbxCommand,
1290 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001291 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001292 ret = -ENXIO;
1293 break;
dea31012005-04-17 16:05:31 -05001294 }
1295 }
James Smarted957682007-06-17 19:56:37 -05001296 mempool_free(pmb, phba->mbox_mem_pool);
1297 return ret;
dea31012005-04-17 16:05:31 -05001298}
1299
James Smarte59058c2008-08-24 21:49:00 -04001300/**
James Smart3621a712009-04-06 18:47:14 -04001301 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001302 * @phba: Pointer to HBA context object.
1303 * @pring: Pointer to driver SLI ring object.
1304 * @piocb: Pointer to the driver iocb object.
1305 *
1306 * This function is called with hbalock held. The function adds the
1307 * new iocb to txcmplq of the given ring. This function always returns
1308 * 0. If this function is called for ELS ring, this function checks if
1309 * there is a vport associated with the ELS command. This function also
1310 * starts els_tmofunc timer if this is an ELS command.
1311 **/
dea31012005-04-17 16:05:31 -05001312static int
James Smart2e0fef82007-06-17 19:56:36 -05001313lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1314 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001315{
dea31012005-04-17 16:05:31 -05001316 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001317 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001318
James Smart92d7f7b2007-06-17 19:56:38 -05001319 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1320 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1321 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1322 if (!piocb->vport)
1323 BUG();
1324 else
1325 mod_timer(&piocb->vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001326 jiffies +
1327 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
James Smart92d7f7b2007-06-17 19:56:38 -05001328 }
1329
dea31012005-04-17 16:05:31 -05001330
James Smart2e0fef82007-06-17 19:56:36 -05001331 return 0;
dea31012005-04-17 16:05:31 -05001332}
1333
James Smarte59058c2008-08-24 21:49:00 -04001334/**
James Smart3621a712009-04-06 18:47:14 -04001335 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001336 * @phba: Pointer to HBA context object.
1337 * @pring: Pointer to driver SLI ring object.
1338 *
1339 * This function is called with hbalock held to get next
1340 * iocb in txq of the given ring. If there is any iocb in
1341 * the txq, the function returns first iocb in the list after
1342 * removing the iocb from the list, else it returns NULL.
1343 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001344struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001345lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001346{
dea31012005-04-17 16:05:31 -05001347 struct lpfc_iocbq *cmd_iocb;
1348
James Smart858c9f62007-06-17 19:56:39 -05001349 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001350 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001351}
1352
James Smarte59058c2008-08-24 21:49:00 -04001353/**
James Smart3621a712009-04-06 18:47:14 -04001354 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001355 * @phba: Pointer to HBA context object.
1356 * @pring: Pointer to driver SLI ring object.
1357 *
1358 * This function is called with hbalock held and the caller must post the
1359 * iocb without releasing the lock. If the caller releases the lock,
1360 * iocb slot returned by the function is not guaranteed to be available.
1361 * The function returns pointer to the next available iocb slot if there
1362 * is available slot in the ring, else it returns NULL.
1363 * If the get index of the ring is ahead of the put index, the function
1364 * will post an error attention event to the worker thread to take the
1365 * HBA to offline state.
1366 **/
dea31012005-04-17 16:05:31 -05001367static IOCB_t *
1368lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1369{
James Smart34b02dc2008-08-24 21:49:55 -04001370 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001371 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1372 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1373 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1374 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001375
James Smart7e56aa22012-08-03 12:35:34 -04001376 if (unlikely(pring->sli.sli3.local_getidx ==
1377 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001378
James Smart7e56aa22012-08-03 12:35:34 -04001379 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001380
James Smart7e56aa22012-08-03 12:35:34 -04001381 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001383 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001384 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001385 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001386 pring->sli.sli3.local_getidx,
1387 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001388
James Smart2e0fef82007-06-17 19:56:36 -05001389 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001390 /*
1391 * All error attention handlers are posted to
1392 * worker thread
1393 */
1394 phba->work_ha |= HA_ERATT;
1395 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001396
James Smart5e9d9b82008-06-14 22:52:53 -04001397 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001398
1399 return NULL;
1400 }
1401
James Smart7e56aa22012-08-03 12:35:34 -04001402 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001403 return NULL;
1404 }
1405
James Smarted957682007-06-17 19:56:37 -05001406 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001407}
1408
James Smarte59058c2008-08-24 21:49:00 -04001409/**
James Smart3621a712009-04-06 18:47:14 -04001410 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001411 * @phba: Pointer to HBA context object.
1412 * @iocbq: Pointer to driver iocb object.
1413 *
1414 * This function gets an iotag for the iocb. If there is no unused iotag and
1415 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1416 * array and assigns a new iotag.
1417 * The function returns the allocated iotag if successful, else returns zero.
1418 * Zero is not a valid iotag.
1419 * The caller is not required to hold any lock.
1420 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001421uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001422lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001423{
James Smart2e0fef82007-06-17 19:56:36 -05001424 struct lpfc_iocbq **new_arr;
1425 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001426 size_t new_len;
1427 struct lpfc_sli *psli = &phba->sli;
1428 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001429
James Smart2e0fef82007-06-17 19:56:36 -05001430 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001431 iotag = psli->last_iotag;
1432 if(++iotag < psli->iocbq_lookup_len) {
1433 psli->last_iotag = iotag;
1434 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001435 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001436 iocbq->iotag = iotag;
1437 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001438 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001439 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1440 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001441 spin_unlock_irq(&phba->hbalock);
1442 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001443 GFP_KERNEL);
1444 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001445 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001446 old_arr = psli->iocbq_lookup;
1447 if (new_len <= psli->iocbq_lookup_len) {
1448 /* highly unprobable case */
1449 kfree(new_arr);
1450 iotag = psli->last_iotag;
1451 if(++iotag < psli->iocbq_lookup_len) {
1452 psli->last_iotag = iotag;
1453 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001454 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001455 iocbq->iotag = iotag;
1456 return iotag;
1457 }
James Smart2e0fef82007-06-17 19:56:36 -05001458 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001459 return 0;
1460 }
1461 if (psli->iocbq_lookup)
1462 memcpy(new_arr, old_arr,
1463 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001464 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001465 psli->iocbq_lookup = new_arr;
1466 psli->iocbq_lookup_len = new_len;
1467 psli->last_iotag = iotag;
1468 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001469 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001470 iocbq->iotag = iotag;
1471 kfree(old_arr);
1472 return iotag;
1473 }
James Smart8f6d98d2006-08-01 07:34:00 -04001474 } else
James Smart2e0fef82007-06-17 19:56:36 -05001475 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001476
James Smartbc739052010-08-04 16:11:18 -04001477 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001478 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1479 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001480
James Bottomley604a3e32005-10-29 10:28:33 -05001481 return 0;
dea31012005-04-17 16:05:31 -05001482}
1483
James Smarte59058c2008-08-24 21:49:00 -04001484/**
James Smart3621a712009-04-06 18:47:14 -04001485 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001486 * @phba: Pointer to HBA context object.
1487 * @pring: Pointer to driver SLI ring object.
1488 * @iocb: Pointer to iocb slot in the ring.
1489 * @nextiocb: Pointer to driver iocb object which need to be
1490 * posted to firmware.
1491 *
1492 * This function is called with hbalock held to post a new iocb to
1493 * the firmware. This function copies the new iocb to ring iocb slot and
1494 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1495 * a completion call back for this iocb else the function will free the
1496 * iocb object.
1497 **/
dea31012005-04-17 16:05:31 -05001498static void
1499lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1500 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1501{
1502 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001503 * Set up an iotag
dea31012005-04-17 16:05:31 -05001504 */
James Bottomley604a3e32005-10-29 10:28:33 -05001505 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001506
James Smarte2a0a9d2008-12-04 22:40:02 -05001507
James Smarta58cbd52007-08-02 11:09:43 -04001508 if (pring->ringno == LPFC_ELS_RING) {
1509 lpfc_debugfs_slow_ring_trc(phba,
1510 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1511 *(((uint32_t *) &nextiocb->iocb) + 4),
1512 *(((uint32_t *) &nextiocb->iocb) + 6),
1513 *(((uint32_t *) &nextiocb->iocb) + 7));
1514 }
1515
dea31012005-04-17 16:05:31 -05001516 /*
1517 * Issue iocb command to adapter
1518 */
James Smart92d7f7b2007-06-17 19:56:38 -05001519 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001520 wmb();
1521 pring->stats.iocb_cmd++;
1522
1523 /*
1524 * If there is no completion routine to call, we can release the
1525 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1526 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1527 */
1528 if (nextiocb->iocb_cmpl)
1529 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001530 else
James Smart2e0fef82007-06-17 19:56:36 -05001531 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001532
1533 /*
1534 * Let the HBA know what IOCB slot will be the next one the
1535 * driver will put a command into.
1536 */
James Smart7e56aa22012-08-03 12:35:34 -04001537 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1538 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001539}
1540
James Smarte59058c2008-08-24 21:49:00 -04001541/**
James Smart3621a712009-04-06 18:47:14 -04001542 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001543 * @phba: Pointer to HBA context object.
1544 * @pring: Pointer to driver SLI ring object.
1545 *
1546 * The caller is not required to hold any lock for calling this function.
1547 * This function updates the chip attention bits for the ring to inform firmware
1548 * that there are pending work to be done for this ring and requests an
1549 * interrupt when there is space available in the ring. This function is
1550 * called when the driver is unable to post more iocbs to the ring due
1551 * to unavailability of space in the ring.
1552 **/
dea31012005-04-17 16:05:31 -05001553static void
James Smart2e0fef82007-06-17 19:56:36 -05001554lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001555{
1556 int ringno = pring->ringno;
1557
1558 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1559
1560 wmb();
1561
1562 /*
1563 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1564 * The HBA will tell us when an IOCB entry is available.
1565 */
1566 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1567 readl(phba->CAregaddr); /* flush */
1568
1569 pring->stats.iocb_cmd_full++;
1570}
1571
James Smarte59058c2008-08-24 21:49:00 -04001572/**
James Smart3621a712009-04-06 18:47:14 -04001573 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001574 * @phba: Pointer to HBA context object.
1575 * @pring: Pointer to driver SLI ring object.
1576 *
1577 * This function updates the chip attention register bit for the
1578 * given ring to inform HBA that there is more work to be done
1579 * in this ring. The caller is not required to hold any lock.
1580 **/
dea31012005-04-17 16:05:31 -05001581static void
James Smart2e0fef82007-06-17 19:56:36 -05001582lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001583{
1584 int ringno = pring->ringno;
1585
1586 /*
1587 * Tell the HBA that there is work to do in this ring.
1588 */
James Smart34b02dc2008-08-24 21:49:55 -04001589 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1590 wmb();
1591 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1592 readl(phba->CAregaddr); /* flush */
1593 }
dea31012005-04-17 16:05:31 -05001594}
1595
James Smarte59058c2008-08-24 21:49:00 -04001596/**
James Smart3621a712009-04-06 18:47:14 -04001597 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001598 * @phba: Pointer to HBA context object.
1599 * @pring: Pointer to driver SLI ring object.
1600 *
1601 * This function is called with hbalock held to post pending iocbs
1602 * in the txq to the firmware. This function is called when driver
1603 * detects space available in the ring.
1604 **/
dea31012005-04-17 16:05:31 -05001605static void
James Smart2e0fef82007-06-17 19:56:36 -05001606lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001607{
1608 IOCB_t *iocb;
1609 struct lpfc_iocbq *nextiocb;
1610
1611 /*
1612 * Check to see if:
1613 * (a) there is anything on the txq to send
1614 * (b) link is up
1615 * (c) link attention events can be processed (fcp ring only)
1616 * (d) IOCB processing is not blocked by the outstanding mbox command.
1617 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001618
1619 if (lpfc_is_link_up(phba) &&
1620 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001621 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001622 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001623
1624 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1625 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1626 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1627
1628 if (iocb)
1629 lpfc_sli_update_ring(phba, pring);
1630 else
1631 lpfc_sli_update_full_ring(phba, pring);
1632 }
1633
1634 return;
1635}
1636
James Smarte59058c2008-08-24 21:49:00 -04001637/**
James Smart3621a712009-04-06 18:47:14 -04001638 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001639 * @phba: Pointer to HBA context object.
1640 * @hbqno: HBQ number.
1641 *
1642 * This function is called with hbalock held to get the next
1643 * available slot for the given HBQ. If there is free slot
1644 * available for the HBQ it will return pointer to the next available
1645 * HBQ entry else it will return NULL.
1646 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001647static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001648lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1649{
1650 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1651
1652 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1653 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1654 hbqp->next_hbqPutIdx = 0;
1655
1656 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001657 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001658 uint32_t getidx = le32_to_cpu(raw_index);
1659
1660 hbqp->local_hbqGetIdx = getidx;
1661
1662 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1663 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001664 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001665 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001666 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001667 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001668 hbqp->entry_count);
1669
1670 phba->link_state = LPFC_HBA_ERROR;
1671 return NULL;
1672 }
1673
1674 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1675 return NULL;
1676 }
1677
James Smart51ef4c22007-08-02 11:10:31 -04001678 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1679 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001680}
1681
James Smarte59058c2008-08-24 21:49:00 -04001682/**
James Smart3621a712009-04-06 18:47:14 -04001683 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001684 * @phba: Pointer to HBA context object.
1685 *
1686 * This function is called with no lock held to free all the
1687 * hbq buffers while uninitializing the SLI interface. It also
1688 * frees the HBQ buffers returned by the firmware but not yet
1689 * processed by the upper layers.
1690 **/
James Smarted957682007-06-17 19:56:37 -05001691void
1692lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1693{
James Smart92d7f7b2007-06-17 19:56:38 -05001694 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1695 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001696 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001697 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001698 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001699
James Smart51ef4c22007-08-02 11:10:31 -04001700 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001701 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001702 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001703 for (i = 0; i < hbq_count; ++i) {
1704 list_for_each_entry_safe(dmabuf, next_dmabuf,
1705 &phba->hbqs[i].hbq_buffer_list, list) {
1706 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1707 list_del(&hbq_buf->dbuf.list);
1708 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1709 }
James Smarta8adb832007-10-27 13:37:53 -04001710 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001711 }
James Smart3163f722008-02-08 18:50:25 -05001712 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001713 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1714 list) {
James Smart3163f722008-02-08 18:50:25 -05001715 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1716 list_del(&hbq_buf->dbuf.list);
1717 if (hbq_buf->tag == -1) {
1718 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1719 (phba, hbq_buf);
1720 } else {
1721 hbqno = hbq_buf->tag >> 16;
1722 if (hbqno >= LPFC_MAX_HBQS)
1723 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1724 (phba, hbq_buf);
1725 else
1726 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1727 hbq_buf);
1728 }
1729 }
1730
1731 /* Mark the HBQs not in use */
1732 phba->hbq_in_use = 0;
1733 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001734}
1735
James Smarte59058c2008-08-24 21:49:00 -04001736/**
James Smart3621a712009-04-06 18:47:14 -04001737 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001738 * @phba: Pointer to HBA context object.
1739 * @hbqno: HBQ number.
1740 * @hbq_buf: Pointer to HBQ buffer.
1741 *
1742 * This function is called with the hbalock held to post a
1743 * hbq buffer to the firmware. If the function finds an empty
1744 * slot in the HBQ, it will post the buffer. The function will return
1745 * pointer to the hbq entry if it successfully post the buffer
1746 * else it will return NULL.
1747 **/
James Smart3772a992009-05-22 14:50:54 -04001748static int
James Smarted957682007-06-17 19:56:37 -05001749lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001750 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001751{
James Smart3772a992009-05-22 14:50:54 -04001752 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1753}
1754
1755/**
1756 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1757 * @phba: Pointer to HBA context object.
1758 * @hbqno: HBQ number.
1759 * @hbq_buf: Pointer to HBQ buffer.
1760 *
1761 * This function is called with the hbalock held to post a hbq buffer to the
1762 * firmware. If the function finds an empty slot in the HBQ, it will post the
1763 * buffer and place it on the hbq_buffer_list. The function will return zero if
1764 * it successfully post the buffer else it will return an error.
1765 **/
1766static int
1767lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1768 struct hbq_dmabuf *hbq_buf)
1769{
James Smarted957682007-06-17 19:56:37 -05001770 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001771 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001772
1773 /* Get next HBQ entry slot to use */
1774 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1775 if (hbqe) {
1776 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1777
James Smart92d7f7b2007-06-17 19:56:38 -05001778 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1779 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001780 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001781 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001782 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1783 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1784 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001785 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1786 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001787 /* flush */
James Smarted957682007-06-17 19:56:37 -05001788 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001789 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001790 return 0;
1791 } else
1792 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001793}
1794
James Smart4f774512009-05-22 14:52:35 -04001795/**
1796 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1797 * @phba: Pointer to HBA context object.
1798 * @hbqno: HBQ number.
1799 * @hbq_buf: Pointer to HBQ buffer.
1800 *
1801 * This function is called with the hbalock held to post an RQE to the SLI4
1802 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1803 * the hbq_buffer_list and return zero, otherwise it will return an error.
1804 **/
1805static int
1806lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1807 struct hbq_dmabuf *hbq_buf)
1808{
1809 int rc;
1810 struct lpfc_rqe hrqe;
1811 struct lpfc_rqe drqe;
1812
1813 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1814 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1815 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1816 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1817 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1818 &hrqe, &drqe);
1819 if (rc < 0)
1820 return rc;
1821 hbq_buf->tag = rc;
1822 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1823 return 0;
1824}
1825
James Smarte59058c2008-08-24 21:49:00 -04001826/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001827static struct lpfc_hbq_init lpfc_els_hbq = {
1828 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001829 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001830 .mask_count = 0,
1831 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001832 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001833 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001834 .init_count = 40,
1835 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001836};
James Smarted957682007-06-17 19:56:37 -05001837
James Smarte59058c2008-08-24 21:49:00 -04001838/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001839static struct lpfc_hbq_init lpfc_extra_hbq = {
1840 .rn = 1,
1841 .entry_count = 200,
1842 .mask_count = 0,
1843 .profile = 0,
1844 .ring_mask = (1 << LPFC_EXTRA_RING),
1845 .buffer_count = 0,
1846 .init_count = 0,
1847 .add_count = 5,
1848};
1849
James Smarte59058c2008-08-24 21:49:00 -04001850/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001851struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001852 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001853 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001854};
1855
James Smarte59058c2008-08-24 21:49:00 -04001856/**
James Smart3621a712009-04-06 18:47:14 -04001857 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001858 * @phba: Pointer to HBA context object.
1859 * @hbqno: HBQ number.
1860 * @count: Number of HBQ buffers to be posted.
1861 *
James Smartd7c255b2008-08-24 21:50:00 -04001862 * This function is called with no lock held to post more hbq buffers to the
1863 * given HBQ. The function returns the number of HBQ buffers successfully
1864 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001865 **/
James Smart311464e2007-08-02 11:10:37 -04001866static int
James Smart92d7f7b2007-06-17 19:56:38 -05001867lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1868{
James Smartd7c255b2008-08-24 21:50:00 -04001869 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001870 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001871 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001872 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001873 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001874 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001875
James Smartd7c255b2008-08-24 21:50:00 -04001876 if ((phba->hbqs[hbqno].buffer_count + count) >
1877 lpfc_hbq_defs[hbqno]->entry_count)
1878 count = lpfc_hbq_defs[hbqno]->entry_count -
1879 phba->hbqs[hbqno].buffer_count;
1880 if (!count)
1881 return 0;
1882 /* Allocate HBQ entries */
1883 for (i = 0; i < count; i++) {
1884 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1885 if (!hbq_buffer)
1886 break;
1887 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1888 }
James Smart3163f722008-02-08 18:50:25 -05001889 /* Check whether HBQ is still in use */
1890 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001891 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001892 goto err;
1893 while (!list_empty(&hbq_buf_list)) {
1894 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1895 dbuf.list);
1896 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1897 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001898 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001899 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001900 posted++;
1901 } else
James Smart51ef4c22007-08-02 11:10:31 -04001902 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001903 }
James Smart3163f722008-02-08 18:50:25 -05001904 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001905 return posted;
1906err:
1907 spin_unlock_irqrestore(&phba->hbalock, flags);
1908 while (!list_empty(&hbq_buf_list)) {
1909 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1910 dbuf.list);
1911 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1912 }
James Smart92d7f7b2007-06-17 19:56:38 -05001913 return 0;
James Smarted957682007-06-17 19:56:37 -05001914}
1915
James Smarte59058c2008-08-24 21:49:00 -04001916/**
James Smart3621a712009-04-06 18:47:14 -04001917 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001918 * @phba: Pointer to HBA context object.
1919 * @qno: HBQ number.
1920 *
1921 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001922 * is called with no lock held. The function returns the number of HBQ entries
1923 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001924 **/
James Smarted957682007-06-17 19:56:37 -05001925int
James Smart92d7f7b2007-06-17 19:56:38 -05001926lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001927{
James Smartdef9c7a2009-12-21 17:02:28 -05001928 if (phba->sli_rev == LPFC_SLI_REV4)
1929 return 0;
1930 else
1931 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1932 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001933}
1934
James Smarte59058c2008-08-24 21:49:00 -04001935/**
James Smart3621a712009-04-06 18:47:14 -04001936 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001937 * @phba: Pointer to HBA context object.
1938 * @qno: HBQ queue number.
1939 *
1940 * This function is called from SLI initialization code path with
1941 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001942 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001943 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001944static int
James Smart92d7f7b2007-06-17 19:56:38 -05001945lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001946{
James Smartdef9c7a2009-12-21 17:02:28 -05001947 if (phba->sli_rev == LPFC_SLI_REV4)
1948 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001949 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001950 else
1951 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1952 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001953}
1954
James Smarte59058c2008-08-24 21:49:00 -04001955/**
James Smart3772a992009-05-22 14:50:54 -04001956 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1957 * @phba: Pointer to HBA context object.
1958 * @hbqno: HBQ number.
1959 *
1960 * This function removes the first hbq buffer on an hbq list and returns a
1961 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1962 **/
1963static struct hbq_dmabuf *
1964lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1965{
1966 struct lpfc_dmabuf *d_buf;
1967
1968 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1969 if (!d_buf)
1970 return NULL;
1971 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1972}
1973
1974/**
James Smart3621a712009-04-06 18:47:14 -04001975 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001976 * @phba: Pointer to HBA context object.
1977 * @tag: Tag of the hbq buffer.
1978 *
1979 * This function is called with hbalock held. This function searches
1980 * for the hbq buffer associated with the given tag in the hbq buffer
1981 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1982 * it returns NULL.
1983 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001984static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001985lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1986{
James Smart92d7f7b2007-06-17 19:56:38 -05001987 struct lpfc_dmabuf *d_buf;
1988 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001989 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001990
James Smart51ef4c22007-08-02 11:10:31 -04001991 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001992 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001993 return NULL;
1994
James Smart3772a992009-05-22 14:50:54 -04001995 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001996 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001997 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001998 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001999 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002000 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002001 }
2002 }
James Smart3772a992009-05-22 14:50:54 -04002003 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002004 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002005 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002006 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002007 return NULL;
James Smarted957682007-06-17 19:56:37 -05002008}
2009
James Smarte59058c2008-08-24 21:49:00 -04002010/**
James Smart3621a712009-04-06 18:47:14 -04002011 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002012 * @phba: Pointer to HBA context object.
2013 * @hbq_buffer: Pointer to HBQ buffer.
2014 *
2015 * This function is called with hbalock. This function gives back
2016 * the hbq buffer to firmware. If the HBQ does not have space to
2017 * post the buffer, it will free the buffer.
2018 **/
James Smarted957682007-06-17 19:56:37 -05002019void
James Smart51ef4c22007-08-02 11:10:31 -04002020lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002021{
2022 uint32_t hbqno;
2023
James Smart51ef4c22007-08-02 11:10:31 -04002024 if (hbq_buffer) {
2025 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002026 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002027 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002028 }
2029}
2030
James Smarte59058c2008-08-24 21:49:00 -04002031/**
James Smart3621a712009-04-06 18:47:14 -04002032 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002033 * @mbxCommand: mailbox command code.
2034 *
2035 * This function is called by the mailbox event handler function to verify
2036 * that the completed mailbox command is a legitimate mailbox command. If the
2037 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2038 * and the mailbox event handler will take the HBA offline.
2039 **/
dea31012005-04-17 16:05:31 -05002040static int
2041lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2042{
2043 uint8_t ret;
2044
2045 switch (mbxCommand) {
2046 case MBX_LOAD_SM:
2047 case MBX_READ_NV:
2048 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002049 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002050 case MBX_RUN_BIU_DIAG:
2051 case MBX_INIT_LINK:
2052 case MBX_DOWN_LINK:
2053 case MBX_CONFIG_LINK:
2054 case MBX_CONFIG_RING:
2055 case MBX_RESET_RING:
2056 case MBX_READ_CONFIG:
2057 case MBX_READ_RCONFIG:
2058 case MBX_READ_SPARM:
2059 case MBX_READ_STATUS:
2060 case MBX_READ_RPI:
2061 case MBX_READ_XRI:
2062 case MBX_READ_REV:
2063 case MBX_READ_LNK_STAT:
2064 case MBX_REG_LOGIN:
2065 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002066 case MBX_CLEAR_LA:
2067 case MBX_DUMP_MEMORY:
2068 case MBX_DUMP_CONTEXT:
2069 case MBX_RUN_DIAGS:
2070 case MBX_RESTART:
2071 case MBX_UPDATE_CFG:
2072 case MBX_DOWN_LOAD:
2073 case MBX_DEL_LD_ENTRY:
2074 case MBX_RUN_PROGRAM:
2075 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002076 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002077 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002078 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002079 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002080 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002081 case MBX_LOAD_AREA:
2082 case MBX_RUN_BIU_DIAG64:
2083 case MBX_CONFIG_PORT:
2084 case MBX_READ_SPARM64:
2085 case MBX_READ_RPI64:
2086 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002087 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002088 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002089 case MBX_SET_DEBUG:
2090 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002091 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002092 case MBX_REG_VPI:
2093 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002094 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002095 case MBX_PORT_CAPABILITIES:
2096 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002097 case MBX_SLI4_CONFIG:
2098 case MBX_SLI4_REQ_FTRS:
2099 case MBX_REG_FCFI:
2100 case MBX_UNREG_FCFI:
2101 case MBX_REG_VFI:
2102 case MBX_UNREG_VFI:
2103 case MBX_INIT_VPI:
2104 case MBX_INIT_VFI:
2105 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002106 case MBX_READ_EVENT_LOG_STATUS:
2107 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002108 case MBX_SECURITY_MGMT:
2109 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002110 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002111 ret = mbxCommand;
2112 break;
2113 default:
2114 ret = MBX_SHUTDOWN;
2115 break;
2116 }
James Smart2e0fef82007-06-17 19:56:36 -05002117 return ret;
dea31012005-04-17 16:05:31 -05002118}
James Smarte59058c2008-08-24 21:49:00 -04002119
2120/**
James Smart3621a712009-04-06 18:47:14 -04002121 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002122 * @phba: Pointer to HBA context object.
2123 * @pmboxq: Pointer to mailbox command.
2124 *
2125 * This is completion handler function for mailbox commands issued from
2126 * lpfc_sli_issue_mbox_wait function. This function is called by the
2127 * mailbox event handler function with no lock held. This function
2128 * will wake up thread waiting on the wait queue pointed by context1
2129 * of the mailbox.
2130 **/
James Smart04c68492009-05-22 14:52:52 -04002131void
James Smart2e0fef82007-06-17 19:56:36 -05002132lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002133{
2134 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002135 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002136
2137 /*
2138 * If pdone_q is empty, the driver thread gave up waiting and
2139 * continued running.
2140 */
James Smart7054a602007-04-25 09:52:34 -04002141 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002142 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002143 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2144 if (pdone_q)
2145 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002146 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002147 return;
2148}
2149
James Smarte59058c2008-08-24 21:49:00 -04002150
2151/**
James Smart3621a712009-04-06 18:47:14 -04002152 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002153 * @phba: Pointer to HBA context object.
2154 * @pmb: Pointer to mailbox object.
2155 *
2156 * This function is the default mailbox completion handler. It
2157 * frees the memory resources associated with the completed mailbox
2158 * command. If the completed command is a REG_LOGIN mailbox command,
2159 * this function will issue a UREG_LOGIN to re-claim the RPI.
2160 **/
dea31012005-04-17 16:05:31 -05002161void
James Smart2e0fef82007-06-17 19:56:36 -05002162lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002163{
James Smartd439d282010-09-29 11:18:45 -04002164 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002165 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002166 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002167 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002168 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002169 int rc;
2170
dea31012005-04-17 16:05:31 -05002171 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002172
dea31012005-04-17 16:05:31 -05002173 if (mp) {
2174 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2175 kfree(mp);
2176 }
James Smart7054a602007-04-25 09:52:34 -04002177
2178 /*
2179 * If a REG_LOGIN succeeded after node is destroyed or node
2180 * is in re-discovery driver need to cleanup the RPI.
2181 */
James Smart2e0fef82007-06-17 19:56:36 -05002182 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002183 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2184 !pmb->u.mb.mbxStatus) {
2185 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002186 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002187 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002188 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002189 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2190 if (rc != MBX_NOT_FINISHED)
2191 return;
2192 }
2193
James Smart695a8142010-01-26 23:08:03 -05002194 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2195 !(phba->pport->load_flag & FC_UNLOADING) &&
2196 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002197 shost = lpfc_shost_from_vport(vport);
2198 spin_lock_irq(shost->host_lock);
2199 vport->vpi_state |= LPFC_VPI_REGISTERED;
2200 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2201 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002202 }
2203
James Smartd439d282010-09-29 11:18:45 -04002204 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2205 ndlp = (struct lpfc_nodelist *)pmb->context2;
2206 lpfc_nlp_put(ndlp);
2207 pmb->context2 = NULL;
2208 }
2209
James Smartdcf2a4e2010-09-29 11:18:53 -04002210 /* Check security permission status on INIT_LINK mailbox command */
2211 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2212 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2213 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2214 "2860 SLI authentication is required "
2215 "for INIT_LINK but has not done yet\n");
2216
James Smart04c68492009-05-22 14:52:52 -04002217 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2218 lpfc_sli4_mbox_cmd_free(phba, pmb);
2219 else
2220 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002221}
2222
James Smarte59058c2008-08-24 21:49:00 -04002223/**
James Smart3621a712009-04-06 18:47:14 -04002224 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002225 * @phba: Pointer to HBA context object.
2226 *
2227 * This function is called with no lock held. This function processes all
2228 * the completed mailbox commands and gives it to upper layers. The interrupt
2229 * service routine processes mailbox completion interrupt and adds completed
2230 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2231 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2232 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2233 * function returns the mailbox commands to the upper layer by calling the
2234 * completion handler function of each mailbox.
2235 **/
dea31012005-04-17 16:05:31 -05002236int
James Smart2e0fef82007-06-17 19:56:36 -05002237lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002238{
James Smart92d7f7b2007-06-17 19:56:38 -05002239 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002240 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002241 int rc;
2242 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002243
2244 phba->sli.slistat.mbox_event++;
2245
James Smart92d7f7b2007-06-17 19:56:38 -05002246 /* Get all completed mailboxe buffers into the cmplq */
2247 spin_lock_irq(&phba->hbalock);
2248 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2249 spin_unlock_irq(&phba->hbalock);
2250
dea31012005-04-17 16:05:31 -05002251 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002252 do {
2253 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2254 if (pmb == NULL)
2255 break;
2256
James Smart04c68492009-05-22 14:52:52 -04002257 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002258
James Smart858c9f62007-06-17 19:56:39 -05002259 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2260 if (pmb->vport) {
2261 lpfc_debugfs_disc_trc(pmb->vport,
2262 LPFC_DISC_TRC_MBOX_VPORT,
2263 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2264 (uint32_t)pmbox->mbxCommand,
2265 pmbox->un.varWords[0],
2266 pmbox->un.varWords[1]);
2267 }
2268 else {
2269 lpfc_debugfs_disc_trc(phba->pport,
2270 LPFC_DISC_TRC_MBOX,
2271 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2272 (uint32_t)pmbox->mbxCommand,
2273 pmbox->un.varWords[0],
2274 pmbox->un.varWords[1]);
2275 }
2276 }
2277
dea31012005-04-17 16:05:31 -05002278 /*
2279 * It is a fatal error if unknown mbox command completion.
2280 */
2281 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2282 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002283 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002284 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002285 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002286 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002287 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002288 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002289 lpfc_sli_config_mbox_subsys_get(phba,
2290 pmb),
2291 lpfc_sli_config_mbox_opcode_get(phba,
2292 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002293 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002294 phba->work_hs = HS_FFER3;
2295 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002296 continue;
dea31012005-04-17 16:05:31 -05002297 }
2298
dea31012005-04-17 16:05:31 -05002299 if (pmbox->mbxStatus) {
2300 phba->sli.slistat.mbox_stat_err++;
2301 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2302 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002303 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002304 LOG_MBOX | LOG_SLI,
2305 "(%d):0305 Mbox cmd cmpl "
2306 "error - RETRYing Data: x%x "
2307 "(x%x/x%x) x%x x%x x%x\n",
2308 pmb->vport ? pmb->vport->vpi : 0,
2309 pmbox->mbxCommand,
2310 lpfc_sli_config_mbox_subsys_get(phba,
2311 pmb),
2312 lpfc_sli_config_mbox_opcode_get(phba,
2313 pmb),
2314 pmbox->mbxStatus,
2315 pmbox->un.varWords[0],
2316 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002317 pmbox->mbxStatus = 0;
2318 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002319 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002320 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002321 continue;
dea31012005-04-17 16:05:31 -05002322 }
2323 }
2324
2325 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002326 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002327 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002328 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2329 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002330 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002331 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002332 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2333 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002334 pmb->mbox_cmpl,
2335 *((uint32_t *) pmbox),
2336 pmbox->un.varWords[0],
2337 pmbox->un.varWords[1],
2338 pmbox->un.varWords[2],
2339 pmbox->un.varWords[3],
2340 pmbox->un.varWords[4],
2341 pmbox->un.varWords[5],
2342 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002343 pmbox->un.varWords[7],
2344 pmbox->un.varWords[8],
2345 pmbox->un.varWords[9],
2346 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002347
James Smart92d7f7b2007-06-17 19:56:38 -05002348 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002349 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002350 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002351 return 0;
dea31012005-04-17 16:05:31 -05002352}
James Smart92d7f7b2007-06-17 19:56:38 -05002353
James Smarte59058c2008-08-24 21:49:00 -04002354/**
James Smart3621a712009-04-06 18:47:14 -04002355 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002356 * @phba: Pointer to HBA context object.
2357 * @pring: Pointer to driver SLI ring object.
2358 * @tag: buffer tag.
2359 *
2360 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2361 * is set in the tag the buffer is posted for a particular exchange,
2362 * the function will return the buffer without replacing the buffer.
2363 * If the buffer is for unsolicited ELS or CT traffic, this function
2364 * returns the buffer and also posts another buffer to the firmware.
2365 **/
James Smart76bb24e2007-10-27 13:38:00 -04002366static struct lpfc_dmabuf *
2367lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002368 struct lpfc_sli_ring *pring,
2369 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002370{
James Smart9f1e1b52008-12-04 22:39:40 -05002371 struct hbq_dmabuf *hbq_entry;
2372
James Smart76bb24e2007-10-27 13:38:00 -04002373 if (tag & QUE_BUFTAG_BIT)
2374 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002375 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2376 if (!hbq_entry)
2377 return NULL;
2378 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002379}
James Smart57127f12007-10-27 13:37:05 -04002380
James Smart3772a992009-05-22 14:50:54 -04002381/**
2382 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2383 * @phba: Pointer to HBA context object.
2384 * @pring: Pointer to driver SLI ring object.
2385 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2386 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2387 * @fch_type: the type for the first frame of the sequence.
2388 *
2389 * This function is called with no lock held. This function uses the r_ctl and
2390 * type of the received sequence to find the correct callback function to call
2391 * to process the sequence.
2392 **/
2393static int
2394lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2395 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2396 uint32_t fch_type)
2397{
2398 int i;
2399
2400 /* unSolicited Responses */
2401 if (pring->prt[0].profile) {
2402 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2403 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2404 saveq);
2405 return 1;
2406 }
2407 /* We must search, based on rctl / type
2408 for the right routine */
2409 for (i = 0; i < pring->num_mask; i++) {
2410 if ((pring->prt[i].rctl == fch_r_ctl) &&
2411 (pring->prt[i].type == fch_type)) {
2412 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2413 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2414 (phba, pring, saveq);
2415 return 1;
2416 }
2417 }
2418 return 0;
2419}
James Smarte59058c2008-08-24 21:49:00 -04002420
2421/**
James Smart3621a712009-04-06 18:47:14 -04002422 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002423 * @phba: Pointer to HBA context object.
2424 * @pring: Pointer to driver SLI ring object.
2425 * @saveq: Pointer to the unsolicited iocb.
2426 *
2427 * This function is called with no lock held by the ring event handler
2428 * when there is an unsolicited iocb posted to the response ring by the
2429 * firmware. This function gets the buffer associated with the iocbs
2430 * and calls the event handler for the ring. This function handles both
2431 * qring buffers and hbq buffers.
2432 * When the function returns 1 the caller can free the iocb object otherwise
2433 * upper layer functions will free the iocb objects.
2434 **/
dea31012005-04-17 16:05:31 -05002435static int
2436lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2437 struct lpfc_iocbq *saveq)
2438{
2439 IOCB_t * irsp;
2440 WORD5 * w5p;
2441 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002442 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002443 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002444 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002445
2446 match = 0;
2447 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002448
2449 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2450 if (pring->lpfc_sli_rcv_async_status)
2451 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2452 else
2453 lpfc_printf_log(phba,
2454 KERN_WARNING,
2455 LOG_SLI,
2456 "0316 Ring %d handler: unexpected "
2457 "ASYNC_STATUS iocb received evt_code "
2458 "0x%x\n",
2459 pring->ringno,
2460 irsp->un.asyncstat.evt_code);
2461 return 1;
2462 }
2463
James Smart3163f722008-02-08 18:50:25 -05002464 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2465 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2466 if (irsp->ulpBdeCount > 0) {
2467 dmzbuf = lpfc_sli_get_buff(phba, pring,
2468 irsp->un.ulpWord[3]);
2469 lpfc_in_buf_free(phba, dmzbuf);
2470 }
2471
2472 if (irsp->ulpBdeCount > 1) {
2473 dmzbuf = lpfc_sli_get_buff(phba, pring,
2474 irsp->unsli3.sli3Words[3]);
2475 lpfc_in_buf_free(phba, dmzbuf);
2476 }
2477
2478 if (irsp->ulpBdeCount > 2) {
2479 dmzbuf = lpfc_sli_get_buff(phba, pring,
2480 irsp->unsli3.sli3Words[7]);
2481 lpfc_in_buf_free(phba, dmzbuf);
2482 }
2483
2484 return 1;
2485 }
2486
James Smart92d7f7b2007-06-17 19:56:38 -05002487 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002488 if (irsp->ulpBdeCount != 0) {
2489 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002490 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002491 if (!saveq->context2)
2492 lpfc_printf_log(phba,
2493 KERN_ERR,
2494 LOG_SLI,
2495 "0341 Ring %d Cannot find buffer for "
2496 "an unsolicited iocb. tag 0x%x\n",
2497 pring->ringno,
2498 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002499 }
2500 if (irsp->ulpBdeCount == 2) {
2501 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002502 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002503 if (!saveq->context3)
2504 lpfc_printf_log(phba,
2505 KERN_ERR,
2506 LOG_SLI,
2507 "0342 Ring %d Cannot find buffer for an"
2508 " unsolicited iocb. tag 0x%x\n",
2509 pring->ringno,
2510 irsp->unsli3.sli3Words[7]);
2511 }
2512 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002513 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002514 if (irsp->ulpBdeCount != 0) {
2515 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2516 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002517 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002518 lpfc_printf_log(phba,
2519 KERN_ERR,
2520 LOG_SLI,
2521 "0343 Ring %d Cannot find "
2522 "buffer for an unsolicited iocb"
2523 ". tag 0x%x\n", pring->ringno,
2524 irsp->un.ulpWord[3]);
2525 }
2526 if (irsp->ulpBdeCount == 2) {
2527 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2528 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002529 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002530 lpfc_printf_log(phba,
2531 KERN_ERR,
2532 LOG_SLI,
2533 "0344 Ring %d Cannot find "
2534 "buffer for an unsolicited "
2535 "iocb. tag 0x%x\n",
2536 pring->ringno,
2537 irsp->unsli3.sli3Words[7]);
2538 }
2539 }
James Smart92d7f7b2007-06-17 19:56:38 -05002540 }
James Smart9c2face2008-01-11 01:53:18 -05002541 if (irsp->ulpBdeCount != 0 &&
2542 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2543 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2544 int found = 0;
2545
2546 /* search continue save q for same XRI */
2547 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002548 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2549 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002550 list_add_tail(&saveq->list, &iocbq->list);
2551 found = 1;
2552 break;
2553 }
2554 }
2555 if (!found)
2556 list_add_tail(&saveq->clist,
2557 &pring->iocb_continue_saveq);
2558 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2559 list_del_init(&iocbq->clist);
2560 saveq = iocbq;
2561 irsp = &(saveq->iocb);
2562 } else
2563 return 0;
2564 }
2565 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2566 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2567 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002568 Rctl = FC_RCTL_ELS_REQ;
2569 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002570 } else {
2571 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2572 Rctl = w5p->hcsw.Rctl;
2573 Type = w5p->hcsw.Type;
2574
2575 /* Firmware Workaround */
2576 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2577 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2578 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002579 Rctl = FC_RCTL_ELS_REQ;
2580 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002581 w5p->hcsw.Rctl = Rctl;
2582 w5p->hcsw.Type = Type;
2583 }
2584 }
James Smart92d7f7b2007-06-17 19:56:38 -05002585
James Smart3772a992009-05-22 14:50:54 -04002586 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002587 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002588 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002589 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002590 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002591
James Smart92d7f7b2007-06-17 19:56:38 -05002592 return 1;
dea31012005-04-17 16:05:31 -05002593}
2594
James Smarte59058c2008-08-24 21:49:00 -04002595/**
James Smart3621a712009-04-06 18:47:14 -04002596 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002597 * @phba: Pointer to HBA context object.
2598 * @pring: Pointer to driver SLI ring object.
2599 * @prspiocb: Pointer to response iocb object.
2600 *
2601 * This function looks up the iocb_lookup table to get the command iocb
2602 * corresponding to the given response iocb using the iotag of the
2603 * response iocb. This function is called with the hbalock held.
2604 * This function returns the command iocb object if it finds the command
2605 * iocb else returns NULL.
2606 **/
dea31012005-04-17 16:05:31 -05002607static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002608lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2609 struct lpfc_sli_ring *pring,
2610 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002611{
dea31012005-04-17 16:05:31 -05002612 struct lpfc_iocbq *cmd_iocb = NULL;
2613 uint16_t iotag;
2614
James Bottomley604a3e32005-10-29 10:28:33 -05002615 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002616
James Bottomley604a3e32005-10-29 10:28:33 -05002617 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2618 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002619 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002620 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002621 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002622 }
James Bottomley604a3e32005-10-29 10:28:33 -05002623 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002624 }
2625
dea31012005-04-17 16:05:31 -05002626 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002627 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002628 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002629 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002630 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002631 return NULL;
2632}
2633
James Smarte59058c2008-08-24 21:49:00 -04002634/**
James Smart3772a992009-05-22 14:50:54 -04002635 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2636 * @phba: Pointer to HBA context object.
2637 * @pring: Pointer to driver SLI ring object.
2638 * @iotag: IOCB tag.
2639 *
2640 * This function looks up the iocb_lookup table to get the command iocb
2641 * corresponding to the given iotag. This function is called with the
2642 * hbalock held.
2643 * This function returns the command iocb object if it finds the command
2644 * iocb else returns NULL.
2645 **/
2646static struct lpfc_iocbq *
2647lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2648 struct lpfc_sli_ring *pring, uint16_t iotag)
2649{
2650 struct lpfc_iocbq *cmd_iocb;
2651
2652 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2653 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002654 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2655 /* remove from txcmpl queue list */
2656 list_del_init(&cmd_iocb->list);
2657 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002658 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002659 }
James Smart3772a992009-05-22 14:50:54 -04002660 }
James Smart3772a992009-05-22 14:50:54 -04002661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2662 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2663 iotag, phba->sli.last_iotag);
2664 return NULL;
2665}
2666
2667/**
James Smart3621a712009-04-06 18:47:14 -04002668 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002669 * @phba: Pointer to HBA context object.
2670 * @pring: Pointer to driver SLI ring object.
2671 * @saveq: Pointer to the response iocb to be processed.
2672 *
2673 * This function is called by the ring event handler for non-fcp
2674 * rings when there is a new response iocb in the response ring.
2675 * The caller is not required to hold any locks. This function
2676 * gets the command iocb associated with the response iocb and
2677 * calls the completion handler for the command iocb. If there
2678 * is no completion handler, the function will free the resources
2679 * associated with command iocb. If the response iocb is for
2680 * an already aborted command iocb, the status of the completion
2681 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2682 * This function always returns 1.
2683 **/
dea31012005-04-17 16:05:31 -05002684static int
James Smart2e0fef82007-06-17 19:56:36 -05002685lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002686 struct lpfc_iocbq *saveq)
2687{
James Smart2e0fef82007-06-17 19:56:36 -05002688 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002689 int rc = 1;
2690 unsigned long iflag;
2691
2692 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002693 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002694 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002695 spin_unlock_irqrestore(&phba->hbalock, iflag);
2696
dea31012005-04-17 16:05:31 -05002697 if (cmdiocbp) {
2698 if (cmdiocbp->iocb_cmpl) {
2699 /*
James Smartea2151b2008-09-07 11:52:10 -04002700 * If an ELS command failed send an event to mgmt
2701 * application.
2702 */
2703 if (saveq->iocb.ulpStatus &&
2704 (pring->ringno == LPFC_ELS_RING) &&
2705 (cmdiocbp->iocb.ulpCommand ==
2706 CMD_ELS_REQUEST64_CR))
2707 lpfc_send_els_failure_event(phba,
2708 cmdiocbp, saveq);
2709
2710 /*
dea31012005-04-17 16:05:31 -05002711 * Post all ELS completions to the worker thread.
2712 * All other are passed to the completion callback.
2713 */
2714 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002715 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2716 (cmdiocbp->iocb_flag &
2717 LPFC_DRIVER_ABORTED)) {
2718 spin_lock_irqsave(&phba->hbalock,
2719 iflag);
James Smart07951072007-04-25 09:51:38 -04002720 cmdiocbp->iocb_flag &=
2721 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002722 spin_unlock_irqrestore(&phba->hbalock,
2723 iflag);
James Smart07951072007-04-25 09:51:38 -04002724 saveq->iocb.ulpStatus =
2725 IOSTAT_LOCAL_REJECT;
2726 saveq->iocb.un.ulpWord[4] =
2727 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002728
2729 /* Firmware could still be in progress
2730 * of DMAing payload, so don't free data
2731 * buffer till after a hbeat.
2732 */
James Smart341af102010-01-26 23:07:37 -05002733 spin_lock_irqsave(&phba->hbalock,
2734 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002735 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002736 spin_unlock_irqrestore(&phba->hbalock,
2737 iflag);
2738 }
James Smart0f65ff62010-02-26 14:14:23 -05002739 if (phba->sli_rev == LPFC_SLI_REV4) {
2740 if (saveq->iocb_flag &
2741 LPFC_EXCHANGE_BUSY) {
2742 /* Set cmdiocb flag for the
2743 * exchange busy so sgl (xri)
2744 * will not be released until
2745 * the abort xri is received
2746 * from hba.
2747 */
2748 spin_lock_irqsave(
2749 &phba->hbalock, iflag);
2750 cmdiocbp->iocb_flag |=
2751 LPFC_EXCHANGE_BUSY;
2752 spin_unlock_irqrestore(
2753 &phba->hbalock, iflag);
2754 }
2755 if (cmdiocbp->iocb_flag &
2756 LPFC_DRIVER_ABORTED) {
2757 /*
2758 * Clear LPFC_DRIVER_ABORTED
2759 * bit in case it was driver
2760 * initiated abort.
2761 */
2762 spin_lock_irqsave(
2763 &phba->hbalock, iflag);
2764 cmdiocbp->iocb_flag &=
2765 ~LPFC_DRIVER_ABORTED;
2766 spin_unlock_irqrestore(
2767 &phba->hbalock, iflag);
2768 cmdiocbp->iocb.ulpStatus =
2769 IOSTAT_LOCAL_REJECT;
2770 cmdiocbp->iocb.un.ulpWord[4] =
2771 IOERR_ABORT_REQUESTED;
2772 /*
2773 * For SLI4, irsiocb contains
2774 * NO_XRI in sli_xritag, it
2775 * shall not affect releasing
2776 * sgl (xri) process.
2777 */
2778 saveq->iocb.ulpStatus =
2779 IOSTAT_LOCAL_REJECT;
2780 saveq->iocb.un.ulpWord[4] =
2781 IOERR_SLI_ABORTED;
2782 spin_lock_irqsave(
2783 &phba->hbalock, iflag);
2784 saveq->iocb_flag |=
2785 LPFC_DELAY_MEM_FREE;
2786 spin_unlock_irqrestore(
2787 &phba->hbalock, iflag);
2788 }
James Smart07951072007-04-25 09:51:38 -04002789 }
dea31012005-04-17 16:05:31 -05002790 }
James Smart2e0fef82007-06-17 19:56:36 -05002791 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002792 } else
2793 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002794 } else {
2795 /*
2796 * Unknown initiating command based on the response iotag.
2797 * This could be the case on the ELS ring because of
2798 * lpfc_els_abort().
2799 */
2800 if (pring->ringno != LPFC_ELS_RING) {
2801 /*
2802 * Ring <ringno> handler: unexpected completion IoTag
2803 * <IoTag>
2804 */
James Smarta257bf92009-04-06 18:48:10 -04002805 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002806 "0322 Ring %d handler: "
2807 "unexpected completion IoTag x%x "
2808 "Data: x%x x%x x%x x%x\n",
2809 pring->ringno,
2810 saveq->iocb.ulpIoTag,
2811 saveq->iocb.ulpStatus,
2812 saveq->iocb.un.ulpWord[4],
2813 saveq->iocb.ulpCommand,
2814 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002815 }
2816 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002817
dea31012005-04-17 16:05:31 -05002818 return rc;
2819}
2820
James Smarte59058c2008-08-24 21:49:00 -04002821/**
James Smart3621a712009-04-06 18:47:14 -04002822 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002823 * @phba: Pointer to HBA context object.
2824 * @pring: Pointer to driver SLI ring object.
2825 *
2826 * This function is called from the iocb ring event handlers when
2827 * put pointer is ahead of the get pointer for a ring. This function signal
2828 * an error attention condition to the worker thread and the worker
2829 * thread will transition the HBA to offline state.
2830 **/
James Smart2e0fef82007-06-17 19:56:36 -05002831static void
2832lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002833{
James Smart34b02dc2008-08-24 21:49:55 -04002834 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002835 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002836 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002837 * rsp ring <portRspMax>
2838 */
2839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002840 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002841 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002842 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002843 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002844
James Smart2e0fef82007-06-17 19:56:36 -05002845 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002846
2847 /*
2848 * All error attention handlers are posted to
2849 * worker thread
2850 */
2851 phba->work_ha |= HA_ERATT;
2852 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002853
James Smart5e9d9b82008-06-14 22:52:53 -04002854 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002855
2856 return;
2857}
2858
James Smarte59058c2008-08-24 21:49:00 -04002859/**
James Smart3621a712009-04-06 18:47:14 -04002860 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002861 * @ptr: Pointer to address of HBA context object.
2862 *
2863 * This function is invoked by the Error Attention polling timer when the
2864 * timer times out. It will check the SLI Error Attention register for
2865 * possible attention events. If so, it will post an Error Attention event
2866 * and wake up worker thread to process it. Otherwise, it will set up the
2867 * Error Attention polling timer for the next poll.
2868 **/
2869void lpfc_poll_eratt(unsigned long ptr)
2870{
2871 struct lpfc_hba *phba;
James Smartaa6fbb72012-08-03 12:36:03 -04002872 uint32_t eratt = 0, rem;
2873 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002874
2875 phba = (struct lpfc_hba *)ptr;
2876
James Smartaa6fbb72012-08-03 12:36:03 -04002877 /* Here we will also keep track of interrupts per sec of the hba */
2878 sli_intr = phba->sli.slistat.sli_intr;
2879
2880 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2881 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2882 sli_intr);
2883 else
2884 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2885
2886 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2887 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2888 phba->sli.slistat.sli_ips = cnt;
2889
2890 phba->sli.slistat.sli_prev_intr = sli_intr;
2891
James Smart93996272008-08-24 21:50:30 -04002892 /* Check chip HA register for error event */
2893 eratt = lpfc_sli_check_eratt(phba);
2894
2895 if (eratt)
2896 /* Tell the worker thread there is work to do */
2897 lpfc_worker_wake_up(phba);
2898 else
2899 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002900 mod_timer(&phba->eratt_poll,
2901 jiffies +
2902 msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smart93996272008-08-24 21:50:30 -04002903 return;
2904}
2905
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002906
James Smarte59058c2008-08-24 21:49:00 -04002907/**
James Smart3621a712009-04-06 18:47:14 -04002908 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002909 * @phba: Pointer to HBA context object.
2910 * @pring: Pointer to driver SLI ring object.
2911 * @mask: Host attention register mask for this ring.
2912 *
2913 * This function is called from the interrupt context when there is a ring
2914 * event for the fcp ring. The caller does not hold any lock.
2915 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002916 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002917 * LE bit set. The function will call the completion handler of the command iocb
2918 * if the response iocb indicates a completion for a command iocb or it is
2919 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2920 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002921 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002922 * to check it explicitly.
2923 */
2924int
James Smart2e0fef82007-06-17 19:56:36 -05002925lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2926 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002927{
James Smart34b02dc2008-08-24 21:49:55 -04002928 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002929 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002930 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002931 struct lpfc_iocbq *cmdiocbq = NULL;
2932 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002933 uint32_t status;
2934 uint32_t portRspPut, portRspMax;
2935 int rc = 1;
2936 lpfc_iocb_type type;
2937 unsigned long iflag;
2938 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002939
James Smart2e0fef82007-06-17 19:56:36 -05002940 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002941 pring->stats.iocb_event++;
2942
dea31012005-04-17 16:05:31 -05002943 /*
2944 * The next available response entry should never exceed the maximum
2945 * entries. If it does, treat it as an adapter hardware error.
2946 */
James Smart7e56aa22012-08-03 12:35:34 -04002947 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002948 portRspPut = le32_to_cpu(pgp->rspPutInx);
2949 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002950 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002951 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002952 return 1;
2953 }
James Smart45ed1192009-10-02 15:17:02 -04002954 if (phba->fcp_ring_in_use) {
2955 spin_unlock_irqrestore(&phba->hbalock, iflag);
2956 return 1;
2957 } else
2958 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002959
2960 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002961 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002962 /*
2963 * Fetch an entry off the ring and copy it into a local data
2964 * structure. The copy involves a byte-swap since the
2965 * network byte order and pci byte orders are different.
2966 */
James Smarted957682007-06-17 19:56:37 -05002967 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002968 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002969
James Smart7e56aa22012-08-03 12:35:34 -04002970 if (++pring->sli.sli3.rspidx >= portRspMax)
2971 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002972
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002973 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2974 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002975 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002976 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002977 irsp = &rspiocbq.iocb;
2978
dea31012005-04-17 16:05:31 -05002979 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2980 pring->stats.iocb_rsp++;
2981 rsp_cmpl++;
2982
2983 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002984 /*
2985 * If resource errors reported from HBA, reduce
2986 * queuedepths of the SCSI device.
2987 */
2988 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04002989 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
2990 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002991 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002992 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002993 spin_lock_irqsave(&phba->hbalock, iflag);
2994 }
2995
dea31012005-04-17 16:05:31 -05002996 /* Rsp ring <ringno> error: IOCB */
2997 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002998 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002999 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003000 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003001 irsp->un.ulpWord[0],
3002 irsp->un.ulpWord[1],
3003 irsp->un.ulpWord[2],
3004 irsp->un.ulpWord[3],
3005 irsp->un.ulpWord[4],
3006 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003007 *(uint32_t *)&irsp->un1,
3008 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003009 }
3010
3011 switch (type) {
3012 case LPFC_ABORT_IOCB:
3013 case LPFC_SOL_IOCB:
3014 /*
3015 * Idle exchange closed via ABTS from port. No iocb
3016 * resources need to be recovered.
3017 */
3018 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003019 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003020 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003021 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003022 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003023 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003024 break;
3025 }
3026
James Bottomley604a3e32005-10-29 10:28:33 -05003027 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3028 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003029 if (unlikely(!cmdiocbq))
3030 break;
3031 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3032 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3033 if (cmdiocbq->iocb_cmpl) {
3034 spin_unlock_irqrestore(&phba->hbalock, iflag);
3035 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3036 &rspiocbq);
3037 spin_lock_irqsave(&phba->hbalock, iflag);
3038 }
dea31012005-04-17 16:05:31 -05003039 break;
James Smarta4bc3372006-12-02 13:34:16 -05003040 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003041 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003042 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003043 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003044 break;
dea31012005-04-17 16:05:31 -05003045 default:
3046 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3047 char adaptermsg[LPFC_MAX_ADPTMSG];
3048 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3049 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3050 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003051 dev_warn(&((phba->pcidev)->dev),
3052 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003053 phba->brd_no, adaptermsg);
3054 } else {
3055 /* Unknown IOCB command */
3056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003057 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003058 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003059 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003060 irsp->ulpStatus,
3061 irsp->ulpIoTag,
3062 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003063 }
3064 break;
3065 }
3066
3067 /*
3068 * The response IOCB has been processed. Update the ring
3069 * pointer in SLIM. If the port response put pointer has not
3070 * been updated, sync the pgp->rspPutInx and fetch the new port
3071 * response put pointer.
3072 */
James Smart7e56aa22012-08-03 12:35:34 -04003073 writel(pring->sli.sli3.rspidx,
3074 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003075
James Smart7e56aa22012-08-03 12:35:34 -04003076 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003077 portRspPut = le32_to_cpu(pgp->rspPutInx);
3078 }
3079
3080 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3081 pring->stats.iocb_rsp_full++;
3082 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3083 writel(status, phba->CAregaddr);
3084 readl(phba->CAregaddr);
3085 }
3086 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3087 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3088 pring->stats.iocb_cmd_empty++;
3089
3090 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003091 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003092 lpfc_sli_resume_iocb(phba, pring);
3093
3094 if ((pring->lpfc_sli_cmd_available))
3095 (pring->lpfc_sli_cmd_available) (phba, pring);
3096
3097 }
3098
James Smart45ed1192009-10-02 15:17:02 -04003099 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003100 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003101 return rc;
3102}
3103
James Smarte59058c2008-08-24 21:49:00 -04003104/**
James Smart3772a992009-05-22 14:50:54 -04003105 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3106 * @phba: Pointer to HBA context object.
3107 * @pring: Pointer to driver SLI ring object.
3108 * @rspiocbp: Pointer to driver response IOCB object.
3109 *
3110 * This function is called from the worker thread when there is a slow-path
3111 * response IOCB to process. This function chains all the response iocbs until
3112 * seeing the iocb with the LE bit set. The function will call
3113 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3114 * completion of a command iocb. The function will call the
3115 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3116 * The function frees the resources or calls the completion handler if this
3117 * iocb is an abort completion. The function returns NULL when the response
3118 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3119 * this function shall chain the iocb on to the iocb_continueq and return the
3120 * response iocb passed in.
3121 **/
3122static struct lpfc_iocbq *
3123lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3124 struct lpfc_iocbq *rspiocbp)
3125{
3126 struct lpfc_iocbq *saveq;
3127 struct lpfc_iocbq *cmdiocbp;
3128 struct lpfc_iocbq *next_iocb;
3129 IOCB_t *irsp = NULL;
3130 uint32_t free_saveq;
3131 uint8_t iocb_cmd_type;
3132 lpfc_iocb_type type;
3133 unsigned long iflag;
3134 int rc;
3135
3136 spin_lock_irqsave(&phba->hbalock, iflag);
3137 /* First add the response iocb to the countinueq list */
3138 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3139 pring->iocb_continueq_cnt++;
3140
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003141 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003142 irsp = &rspiocbp->iocb;
3143 if (irsp->ulpLe) {
3144 /*
3145 * By default, the driver expects to free all resources
3146 * associated with this iocb completion.
3147 */
3148 free_saveq = 1;
3149 saveq = list_get_first(&pring->iocb_continueq,
3150 struct lpfc_iocbq, list);
3151 irsp = &(saveq->iocb);
3152 list_del_init(&pring->iocb_continueq);
3153 pring->iocb_continueq_cnt = 0;
3154
3155 pring->stats.iocb_rsp++;
3156
3157 /*
3158 * If resource errors reported from HBA, reduce
3159 * queuedepths of the SCSI device.
3160 */
3161 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003162 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3163 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003164 spin_unlock_irqrestore(&phba->hbalock, iflag);
3165 phba->lpfc_rampdown_queue_depth(phba);
3166 spin_lock_irqsave(&phba->hbalock, iflag);
3167 }
3168
3169 if (irsp->ulpStatus) {
3170 /* Rsp ring <ringno> error: IOCB */
3171 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3172 "0328 Rsp Ring %d error: "
3173 "IOCB Data: "
3174 "x%x x%x x%x x%x "
3175 "x%x x%x x%x x%x "
3176 "x%x x%x x%x x%x "
3177 "x%x x%x x%x x%x\n",
3178 pring->ringno,
3179 irsp->un.ulpWord[0],
3180 irsp->un.ulpWord[1],
3181 irsp->un.ulpWord[2],
3182 irsp->un.ulpWord[3],
3183 irsp->un.ulpWord[4],
3184 irsp->un.ulpWord[5],
3185 *(((uint32_t *) irsp) + 6),
3186 *(((uint32_t *) irsp) + 7),
3187 *(((uint32_t *) irsp) + 8),
3188 *(((uint32_t *) irsp) + 9),
3189 *(((uint32_t *) irsp) + 10),
3190 *(((uint32_t *) irsp) + 11),
3191 *(((uint32_t *) irsp) + 12),
3192 *(((uint32_t *) irsp) + 13),
3193 *(((uint32_t *) irsp) + 14),
3194 *(((uint32_t *) irsp) + 15));
3195 }
3196
3197 /*
3198 * Fetch the IOCB command type and call the correct completion
3199 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3200 * get freed back to the lpfc_iocb_list by the discovery
3201 * kernel thread.
3202 */
3203 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3204 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3205 switch (type) {
3206 case LPFC_SOL_IOCB:
3207 spin_unlock_irqrestore(&phba->hbalock, iflag);
3208 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3209 spin_lock_irqsave(&phba->hbalock, iflag);
3210 break;
3211
3212 case LPFC_UNSOL_IOCB:
3213 spin_unlock_irqrestore(&phba->hbalock, iflag);
3214 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3215 spin_lock_irqsave(&phba->hbalock, iflag);
3216 if (!rc)
3217 free_saveq = 0;
3218 break;
3219
3220 case LPFC_ABORT_IOCB:
3221 cmdiocbp = NULL;
3222 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3223 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3224 saveq);
3225 if (cmdiocbp) {
3226 /* Call the specified completion routine */
3227 if (cmdiocbp->iocb_cmpl) {
3228 spin_unlock_irqrestore(&phba->hbalock,
3229 iflag);
3230 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3231 saveq);
3232 spin_lock_irqsave(&phba->hbalock,
3233 iflag);
3234 } else
3235 __lpfc_sli_release_iocbq(phba,
3236 cmdiocbp);
3237 }
3238 break;
3239
3240 case LPFC_UNKNOWN_IOCB:
3241 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3242 char adaptermsg[LPFC_MAX_ADPTMSG];
3243 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3244 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3245 MAX_MSG_DATA);
3246 dev_warn(&((phba->pcidev)->dev),
3247 "lpfc%d: %s\n",
3248 phba->brd_no, adaptermsg);
3249 } else {
3250 /* Unknown IOCB command */
3251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3252 "0335 Unknown IOCB "
3253 "command Data: x%x "
3254 "x%x x%x x%x\n",
3255 irsp->ulpCommand,
3256 irsp->ulpStatus,
3257 irsp->ulpIoTag,
3258 irsp->ulpContext);
3259 }
3260 break;
3261 }
3262
3263 if (free_saveq) {
3264 list_for_each_entry_safe(rspiocbp, next_iocb,
3265 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003266 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003267 __lpfc_sli_release_iocbq(phba, rspiocbp);
3268 }
3269 __lpfc_sli_release_iocbq(phba, saveq);
3270 }
3271 rspiocbp = NULL;
3272 }
3273 spin_unlock_irqrestore(&phba->hbalock, iflag);
3274 return rspiocbp;
3275}
3276
3277/**
3278 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003279 * @phba: Pointer to HBA context object.
3280 * @pring: Pointer to driver SLI ring object.
3281 * @mask: Host attention register mask for this ring.
3282 *
James Smart3772a992009-05-22 14:50:54 -04003283 * This routine wraps the actual slow_ring event process routine from the
3284 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003285 **/
James Smart3772a992009-05-22 14:50:54 -04003286void
James Smart2e0fef82007-06-17 19:56:36 -05003287lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3288 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003289{
James Smart3772a992009-05-22 14:50:54 -04003290 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3291}
3292
3293/**
3294 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3295 * @phba: Pointer to HBA context object.
3296 * @pring: Pointer to driver SLI ring object.
3297 * @mask: Host attention register mask for this ring.
3298 *
3299 * This function is called from the worker thread when there is a ring event
3300 * for non-fcp rings. The caller does not hold any lock. The function will
3301 * remove each response iocb in the response ring and calls the handle
3302 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3303 **/
3304static void
3305lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3306 struct lpfc_sli_ring *pring, uint32_t mask)
3307{
James Smart34b02dc2008-08-24 21:49:55 -04003308 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003309 IOCB_t *entry;
3310 IOCB_t *irsp = NULL;
3311 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003312 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003313 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003314 uint32_t status;
dea31012005-04-17 16:05:31 -05003315
James Smart34b02dc2008-08-24 21:49:55 -04003316 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003317 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003318 pring->stats.iocb_event++;
3319
dea31012005-04-17 16:05:31 -05003320 /*
3321 * The next available response entry should never exceed the maximum
3322 * entries. If it does, treat it as an adapter hardware error.
3323 */
James Smart7e56aa22012-08-03 12:35:34 -04003324 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003325 portRspPut = le32_to_cpu(pgp->rspPutInx);
3326 if (portRspPut >= portRspMax) {
3327 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003328 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003329 * rsp ring <portRspMax>
3330 */
James Smarted957682007-06-17 19:56:37 -05003331 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003332 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003333 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003334 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003335
James Smart2e0fef82007-06-17 19:56:36 -05003336 phba->link_state = LPFC_HBA_ERROR;
3337 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003338
3339 phba->work_hs = HS_FFER3;
3340 lpfc_handle_eratt(phba);
3341
James Smart3772a992009-05-22 14:50:54 -04003342 return;
dea31012005-04-17 16:05:31 -05003343 }
3344
3345 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003346 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003347 /*
3348 * Build a completion list and call the appropriate handler.
3349 * The process is to get the next available response iocb, get
3350 * a free iocb from the list, copy the response data into the
3351 * free iocb, insert to the continuation list, and update the
3352 * next response index to slim. This process makes response
3353 * iocb's in the ring available to DMA as fast as possible but
3354 * pays a penalty for a copy operation. Since the iocb is
3355 * only 32 bytes, this penalty is considered small relative to
3356 * the PCI reads for register values and a slim write. When
3357 * the ulpLe field is set, the entire Command has been
3358 * received.
3359 */
James Smarted957682007-06-17 19:56:37 -05003360 entry = lpfc_resp_iocb(phba, pring);
3361
James Smart858c9f62007-06-17 19:56:39 -05003362 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003363 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003364 if (rspiocbp == NULL) {
3365 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003366 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003367 break;
3368 }
3369
James Smarted957682007-06-17 19:56:37 -05003370 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3371 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003372 irsp = &rspiocbp->iocb;
3373
James Smart7e56aa22012-08-03 12:35:34 -04003374 if (++pring->sli.sli3.rspidx >= portRspMax)
3375 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003376
James Smarta58cbd52007-08-02 11:09:43 -04003377 if (pring->ringno == LPFC_ELS_RING) {
3378 lpfc_debugfs_slow_ring_trc(phba,
3379 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3380 *(((uint32_t *) irsp) + 4),
3381 *(((uint32_t *) irsp) + 6),
3382 *(((uint32_t *) irsp) + 7));
3383 }
3384
James Smart7e56aa22012-08-03 12:35:34 -04003385 writel(pring->sli.sli3.rspidx,
3386 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003387
James Smart3772a992009-05-22 14:50:54 -04003388 spin_unlock_irqrestore(&phba->hbalock, iflag);
3389 /* Handle the response IOCB */
3390 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3391 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003392
3393 /*
3394 * If the port response put pointer has not been updated, sync
3395 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3396 * response put pointer.
3397 */
James Smart7e56aa22012-08-03 12:35:34 -04003398 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003399 portRspPut = le32_to_cpu(pgp->rspPutInx);
3400 }
James Smart7e56aa22012-08-03 12:35:34 -04003401 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003402
James Smart92d7f7b2007-06-17 19:56:38 -05003403 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003404 /* At least one response entry has been freed */
3405 pring->stats.iocb_rsp_full++;
3406 /* SET RxRE_RSP in Chip Att register */
3407 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3408 writel(status, phba->CAregaddr);
3409 readl(phba->CAregaddr); /* flush */
3410 }
3411 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3412 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3413 pring->stats.iocb_cmd_empty++;
3414
3415 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003416 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003417 lpfc_sli_resume_iocb(phba, pring);
3418
3419 if ((pring->lpfc_sli_cmd_available))
3420 (pring->lpfc_sli_cmd_available) (phba, pring);
3421
3422 }
3423
James Smart2e0fef82007-06-17 19:56:36 -05003424 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003425 return;
dea31012005-04-17 16:05:31 -05003426}
3427
James Smarte59058c2008-08-24 21:49:00 -04003428/**
James Smart4f774512009-05-22 14:52:35 -04003429 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3430 * @phba: Pointer to HBA context object.
3431 * @pring: Pointer to driver SLI ring object.
3432 * @mask: Host attention register mask for this ring.
3433 *
3434 * This function is called from the worker thread when there is a pending
3435 * ELS response iocb on the driver internal slow-path response iocb worker
3436 * queue. The caller does not hold any lock. The function will remove each
3437 * response iocb from the response worker queue and calls the handle
3438 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3439 **/
3440static void
3441lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3442 struct lpfc_sli_ring *pring, uint32_t mask)
3443{
3444 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003445 struct hbq_dmabuf *dmabuf;
3446 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003447 unsigned long iflag;
3448
James Smart45ed1192009-10-02 15:17:02 -04003449 spin_lock_irqsave(&phba->hbalock, iflag);
3450 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3451 spin_unlock_irqrestore(&phba->hbalock, iflag);
3452 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003453 /* Get the response iocb from the head of work queue */
3454 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003455 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003456 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003457 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003458
3459 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3460 case CQE_CODE_COMPL_WQE:
3461 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3462 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003463 /* Translate ELS WCQE to response IOCBQ */
3464 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3465 irspiocbq);
3466 if (irspiocbq)
3467 lpfc_sli_sp_handle_rspiocb(phba, pring,
3468 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003469 break;
3470 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003471 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003472 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3473 cq_event);
3474 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3475 break;
3476 default:
3477 break;
3478 }
James Smart4f774512009-05-22 14:52:35 -04003479 }
3480}
3481
3482/**
James Smart3621a712009-04-06 18:47:14 -04003483 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003484 * @phba: Pointer to HBA context object.
3485 * @pring: Pointer to driver SLI ring object.
3486 *
3487 * This function aborts all iocbs in the given ring and frees all the iocb
3488 * objects in txq. This function issues an abort iocb for all the iocb commands
3489 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3490 * the return of this function. The caller is not required to hold any locks.
3491 **/
James Smart2e0fef82007-06-17 19:56:36 -05003492void
dea31012005-04-17 16:05:31 -05003493lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3494{
James Smart2534ba72007-04-25 09:52:20 -04003495 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003496 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003497
James Smart92d7f7b2007-06-17 19:56:38 -05003498 if (pring->ringno == LPFC_ELS_RING) {
3499 lpfc_fabric_abort_hba(phba);
3500 }
3501
dea31012005-04-17 16:05:31 -05003502 /* Error everything on txq and txcmplq
3503 * First do the txq.
3504 */
James Smart2e0fef82007-06-17 19:56:36 -05003505 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003506 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003507
3508 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003509 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3510 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3511
James Smart2e0fef82007-06-17 19:56:36 -05003512 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003513
James Smarta257bf92009-04-06 18:48:10 -04003514 /* Cancel all the IOCBs from the completions list */
3515 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3516 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003517}
3518
James Smarte59058c2008-08-24 21:49:00 -04003519/**
James Smart3621a712009-04-06 18:47:14 -04003520 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003521 * @phba: Pointer to HBA context object.
3522 *
3523 * This function flushes all iocbs in the fcp ring and frees all the iocb
3524 * objects in txq and txcmplq. This function will not issue abort iocbs
3525 * for all the iocb commands in txcmplq, they will just be returned with
3526 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3527 * slot has been permanently disabled.
3528 **/
3529void
3530lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3531{
3532 LIST_HEAD(txq);
3533 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003534 struct lpfc_sli *psli = &phba->sli;
3535 struct lpfc_sli_ring *pring;
3536
3537 /* Currently, only one fcp ring */
3538 pring = &psli->ring[psli->fcp_ring];
3539
3540 spin_lock_irq(&phba->hbalock);
3541 /* Retrieve everything on txq */
3542 list_splice_init(&pring->txq, &txq);
James Smarta8e497d2008-08-24 21:50:11 -04003543
3544 /* Retrieve everything on the txcmplq */
3545 list_splice_init(&pring->txcmplq, &txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04003546
3547 /* Indicate the I/O queues are flushed */
3548 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003549 spin_unlock_irq(&phba->hbalock);
3550
3551 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003552 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3553 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003554
3555 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003556 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3557 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003558}
3559
3560/**
James Smart3772a992009-05-22 14:50:54 -04003561 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003562 * @phba: Pointer to HBA context object.
3563 * @mask: Bit mask to be checked.
3564 *
3565 * This function reads the host status register and compares
3566 * with the provided bit mask to check if HBA completed
3567 * the restart. This function will wait in a loop for the
3568 * HBA to complete restart. If the HBA does not restart within
3569 * 15 iterations, the function will reset the HBA again. The
3570 * function returns 1 when HBA fail to restart otherwise returns
3571 * zero.
3572 **/
James Smart3772a992009-05-22 14:50:54 -04003573static int
3574lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003575{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003576 uint32_t status;
3577 int i = 0;
3578 int retval = 0;
dea31012005-04-17 16:05:31 -05003579
Jamie Wellnitz41415862006-02-28 19:25:27 -05003580 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003581 if (lpfc_readl(phba->HSregaddr, &status))
3582 return 1;
dea31012005-04-17 16:05:31 -05003583
Jamie Wellnitz41415862006-02-28 19:25:27 -05003584 /*
3585 * Check status register every 100ms for 5 retries, then every
3586 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3587 * every 2.5 sec for 4.
3588 * Break our of the loop if errors occurred during init.
3589 */
3590 while (((status & mask) != mask) &&
3591 !(status & HS_FFERM) &&
3592 i++ < 20) {
dea31012005-04-17 16:05:31 -05003593
Jamie Wellnitz41415862006-02-28 19:25:27 -05003594 if (i <= 5)
3595 msleep(10);
3596 else if (i <= 10)
3597 msleep(500);
3598 else
3599 msleep(2500);
dea31012005-04-17 16:05:31 -05003600
Jamie Wellnitz41415862006-02-28 19:25:27 -05003601 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003602 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003603 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003604 lpfc_sli_brdrestart(phba);
3605 }
3606 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003607 if (lpfc_readl(phba->HSregaddr, &status)) {
3608 retval = 1;
3609 break;
3610 }
dea31012005-04-17 16:05:31 -05003611 }
dea31012005-04-17 16:05:31 -05003612
Jamie Wellnitz41415862006-02-28 19:25:27 -05003613 /* Check to see if any errors occurred during init */
3614 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3616 "2751 Adapter failed to restart, "
3617 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3618 status,
3619 readl(phba->MBslimaddr + 0xa8),
3620 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003621 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003622 retval = 1;
3623 }
dea31012005-04-17 16:05:31 -05003624
Jamie Wellnitz41415862006-02-28 19:25:27 -05003625 return retval;
dea31012005-04-17 16:05:31 -05003626}
3627
James Smartda0436e2009-05-22 14:51:39 -04003628/**
3629 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3630 * @phba: Pointer to HBA context object.
3631 * @mask: Bit mask to be checked.
3632 *
3633 * This function checks the host status register to check if HBA is
3634 * ready. This function will wait in a loop for the HBA to be ready
3635 * If the HBA is not ready , the function will will reset the HBA PCI
3636 * function again. The function returns 1 when HBA fail to be ready
3637 * otherwise returns zero.
3638 **/
3639static int
3640lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3641{
3642 uint32_t status;
3643 int retval = 0;
3644
3645 /* Read the HBA Host Status Register */
3646 status = lpfc_sli4_post_status_check(phba);
3647
3648 if (status) {
3649 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3650 lpfc_sli_brdrestart(phba);
3651 status = lpfc_sli4_post_status_check(phba);
3652 }
3653
3654 /* Check to see if any errors occurred during init */
3655 if (status) {
3656 phba->link_state = LPFC_HBA_ERROR;
3657 retval = 1;
3658 } else
3659 phba->sli4_hba.intr_enable = 0;
3660
3661 return retval;
3662}
3663
3664/**
3665 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3666 * @phba: Pointer to HBA context object.
3667 * @mask: Bit mask to be checked.
3668 *
3669 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3670 * from the API jump table function pointer from the lpfc_hba struct.
3671 **/
3672int
3673lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3674{
3675 return phba->lpfc_sli_brdready(phba, mask);
3676}
3677
James Smart92908312006-03-07 15:04:13 -05003678#define BARRIER_TEST_PATTERN (0xdeadbeef)
3679
James Smarte59058c2008-08-24 21:49:00 -04003680/**
James Smart3621a712009-04-06 18:47:14 -04003681 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003682 * @phba: Pointer to HBA context object.
3683 *
James Smart1b511972011-12-13 13:23:09 -05003684 * This function is called before resetting an HBA. This function is called
3685 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003686 **/
James Smart2e0fef82007-06-17 19:56:36 -05003687void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003688{
James Smart65a29c12006-07-06 15:50:50 -04003689 uint32_t __iomem *resp_buf;
3690 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003691 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003692 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003693 int i;
3694 uint8_t hdrtype;
3695
3696 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3697 if (hdrtype != 0x80 ||
3698 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3699 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3700 return;
3701
3702 /*
3703 * Tell the other part of the chip to suspend temporarily all
3704 * its DMA activity.
3705 */
James Smart65a29c12006-07-06 15:50:50 -04003706 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003707
3708 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003709 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3710 return;
James Smart92908312006-03-07 15:04:13 -05003711 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3712 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003713 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003714
James Smart9940b972011-03-11 16:06:12 -05003715 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3716 return;
3717 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003718 /* Clear Chip error bit */
3719 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003720 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003721 }
3722
3723 mbox = 0;
3724 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3725 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3726
3727 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003728 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003729 writel(mbox, mbox_buf);
3730
James Smart9940b972011-03-11 16:06:12 -05003731 for (i = 0; i < 50; i++) {
3732 if (lpfc_readl((resp_buf + 1), &resp_data))
3733 return;
3734 if (resp_data != ~(BARRIER_TEST_PATTERN))
3735 mdelay(1);
3736 else
3737 break;
3738 }
3739 resp_data = 0;
3740 if (lpfc_readl((resp_buf + 1), &resp_data))
3741 return;
3742 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003743 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003744 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003745 goto restore_hc;
3746 else
3747 goto clear_errat;
3748 }
3749
3750 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003751 resp_data = 0;
3752 for (i = 0; i < 500; i++) {
3753 if (lpfc_readl(resp_buf, &resp_data))
3754 return;
3755 if (resp_data != mbox)
3756 mdelay(1);
3757 else
3758 break;
3759 }
James Smart92908312006-03-07 15:04:13 -05003760
3761clear_errat:
3762
James Smart9940b972011-03-11 16:06:12 -05003763 while (++i < 500) {
3764 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3765 return;
3766 if (!(ha_copy & HA_ERATT))
3767 mdelay(1);
3768 else
3769 break;
3770 }
James Smart92908312006-03-07 15:04:13 -05003771
3772 if (readl(phba->HAregaddr) & HA_ERATT) {
3773 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003774 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003775 }
3776
3777restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003778 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003779 writel(hc_copy, phba->HCregaddr);
3780 readl(phba->HCregaddr); /* flush */
3781}
3782
James Smarte59058c2008-08-24 21:49:00 -04003783/**
James Smart3621a712009-04-06 18:47:14 -04003784 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003785 * @phba: Pointer to HBA context object.
3786 *
3787 * This function issues a kill_board mailbox command and waits for
3788 * the error attention interrupt. This function is called for stopping
3789 * the firmware processing. The caller is not required to hold any
3790 * locks. This function calls lpfc_hba_down_post function to free
3791 * any pending commands after the kill. The function will return 1 when it
3792 * fails to kill the board else will return 0.
3793 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003794int
James Smart2e0fef82007-06-17 19:56:36 -05003795lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003796{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003797 struct lpfc_sli *psli;
3798 LPFC_MBOXQ_t *pmb;
3799 uint32_t status;
3800 uint32_t ha_copy;
3801 int retval;
3802 int i = 0;
3803
3804 psli = &phba->sli;
3805
3806 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003807 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003808 "0329 Kill HBA Data: x%x x%x\n",
3809 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003810
James Smart98c9ea52007-10-27 13:37:33 -04003811 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3812 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003813 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003814
3815 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003816 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003817 if (lpfc_readl(phba->HCregaddr, &status)) {
3818 spin_unlock_irq(&phba->hbalock);
3819 mempool_free(pmb, phba->mbox_mem_pool);
3820 return 1;
3821 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003822 status &= ~HC_ERINT_ENA;
3823 writel(status, phba->HCregaddr);
3824 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003825 phba->link_flag |= LS_IGNORE_ERATT;
3826 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003827
3828 lpfc_kill_board(phba, pmb);
3829 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3830 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3831
3832 if (retval != MBX_SUCCESS) {
3833 if (retval != MBX_BUSY)
3834 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003835 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3836 "2752 KILL_BOARD command failed retval %d\n",
3837 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003838 spin_lock_irq(&phba->hbalock);
3839 phba->link_flag &= ~LS_IGNORE_ERATT;
3840 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003841 return 1;
3842 }
3843
James Smartf4b4c682009-05-22 14:53:12 -04003844 spin_lock_irq(&phba->hbalock);
3845 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3846 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003847
Jamie Wellnitz41415862006-02-28 19:25:27 -05003848 mempool_free(pmb, phba->mbox_mem_pool);
3849
3850 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3851 * attention every 100ms for 3 seconds. If we don't get ERATT after
3852 * 3 seconds we still set HBA_ERROR state because the status of the
3853 * board is now undefined.
3854 */
James Smart9940b972011-03-11 16:06:12 -05003855 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3856 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003857 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3858 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003859 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3860 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003861 }
3862
3863 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003864 if (ha_copy & HA_ERATT) {
3865 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003866 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003867 }
James Smart2e0fef82007-06-17 19:56:36 -05003868 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003869 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003870 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003871 phba->link_flag &= ~LS_IGNORE_ERATT;
3872 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003873
Jamie Wellnitz41415862006-02-28 19:25:27 -05003874 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003875 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003876
James Smart2e0fef82007-06-17 19:56:36 -05003877 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003878}
3879
James Smarte59058c2008-08-24 21:49:00 -04003880/**
James Smart3772a992009-05-22 14:50:54 -04003881 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003882 * @phba: Pointer to HBA context object.
3883 *
3884 * This function resets the HBA by writing HC_INITFF to the control
3885 * register. After the HBA resets, this function resets all the iocb ring
3886 * indices. This function disables PCI layer parity checking during
3887 * the reset.
3888 * This function returns 0 always.
3889 * The caller is not required to hold any locks.
3890 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003891int
James Smart2e0fef82007-06-17 19:56:36 -05003892lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003893{
3894 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003895 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003896 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003897 int i;
dea31012005-04-17 16:05:31 -05003898
Jamie Wellnitz41415862006-02-28 19:25:27 -05003899 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003900
Jamie Wellnitz41415862006-02-28 19:25:27 -05003901 /* Reset HBA */
3902 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003903 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003904 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003905
3906 /* perform board reset */
3907 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003908 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003909 phba->pport->fc_myDID = 0;
3910 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003911
Jamie Wellnitz41415862006-02-28 19:25:27 -05003912 /* Turn off parity checking and serr during the physical reset */
3913 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3914 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3915 (cfg_value &
3916 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3917
James Smart3772a992009-05-22 14:50:54 -04003918 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3919
Jamie Wellnitz41415862006-02-28 19:25:27 -05003920 /* Now toggle INITFF bit in the Host Control Register */
3921 writel(HC_INITFF, phba->HCregaddr);
3922 mdelay(1);
3923 readl(phba->HCregaddr); /* flush */
3924 writel(0, phba->HCregaddr);
3925 readl(phba->HCregaddr); /* flush */
3926
3927 /* Restore PCI cmd register */
3928 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003929
3930 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003931 for (i = 0; i < psli->num_rings; i++) {
3932 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003933 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003934 pring->sli.sli3.rspidx = 0;
3935 pring->sli.sli3.next_cmdidx = 0;
3936 pring->sli.sli3.local_getidx = 0;
3937 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003938 pring->missbufcnt = 0;
3939 }
dea31012005-04-17 16:05:31 -05003940
James Smart2e0fef82007-06-17 19:56:36 -05003941 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003942 return 0;
3943}
3944
James Smarte59058c2008-08-24 21:49:00 -04003945/**
James Smartda0436e2009-05-22 14:51:39 -04003946 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3947 * @phba: Pointer to HBA context object.
3948 *
3949 * This function resets a SLI4 HBA. This function disables PCI layer parity
3950 * checking during resets the device. The caller is not required to hold
3951 * any locks.
3952 *
3953 * This function returns 0 always.
3954 **/
3955int
3956lpfc_sli4_brdreset(struct lpfc_hba *phba)
3957{
3958 struct lpfc_sli *psli = &phba->sli;
3959 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003960 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003961
3962 /* Reset HBA */
3963 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3964 "0295 Reset HBA Data: x%x x%x\n",
3965 phba->pport->port_state, psli->sli_flag);
3966
3967 /* perform board reset */
3968 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003969 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003970 phba->pport->fc_myDID = 0;
3971 phba->pport->fc_prevDID = 0;
3972
James Smartda0436e2009-05-22 14:51:39 -04003973 spin_lock_irq(&phba->hbalock);
3974 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3975 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003976 spin_unlock_irq(&phba->hbalock);
3977
3978 /* Now physically reset the device */
3979 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3980 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003981
3982 /* Turn off parity checking and serr during the physical reset */
3983 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3984 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3985 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3986
James Smart88318812012-09-29 11:29:29 -04003987 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04003988 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04003989 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04003990
James Smartbe858b62010-12-15 17:57:20 -05003991 /* Restore PCI cmd register */
3992 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3993
James Smart27b01b82012-05-09 21:19:44 -04003994 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003995}
3996
3997/**
3998 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003999 * @phba: Pointer to HBA context object.
4000 *
4001 * This function is called in the SLI initialization code path to
4002 * restart the HBA. The caller is not required to hold any lock.
4003 * This function writes MBX_RESTART mailbox command to the SLIM and
4004 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4005 * function to free any pending commands. The function enables
4006 * POST only during the first initialization. The function returns zero.
4007 * The function does not guarantee completion of MBX_RESTART mailbox
4008 * command before the return of this function.
4009 **/
James Smartda0436e2009-05-22 14:51:39 -04004010static int
4011lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004012{
4013 MAILBOX_t *mb;
4014 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004015 volatile uint32_t word0;
4016 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004017 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004018
James Smart2e0fef82007-06-17 19:56:36 -05004019 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004020
James Smart0d878412009-10-02 15:16:56 -04004021 /* Take PCIe device Advanced Error Reporting (AER) state */
4022 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4023
Jamie Wellnitz41415862006-02-28 19:25:27 -05004024 psli = &phba->sli;
4025
4026 /* Restart HBA */
4027 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004028 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004029 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004030
4031 word0 = 0;
4032 mb = (MAILBOX_t *) &word0;
4033 mb->mbxCommand = MBX_RESTART;
4034 mb->mbxHc = 1;
4035
James Smart92908312006-03-07 15:04:13 -05004036 lpfc_reset_barrier(phba);
4037
Jamie Wellnitz41415862006-02-28 19:25:27 -05004038 to_slim = phba->MBslimaddr;
4039 writel(*(uint32_t *) mb, to_slim);
4040 readl(to_slim); /* flush */
4041
4042 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004043 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004044 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004045 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004046 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004047 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004048 writel(*(uint32_t *) mb, to_slim);
4049 readl(to_slim); /* flush */
4050
4051 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004052 phba->pport->stopped = 0;
4053 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004054 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004055 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004056
James Smart64ba8812006-08-02 15:24:34 -04004057 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4058 psli->stats_start = get_seconds();
4059
James Smarteaf15d52008-12-04 22:39:29 -05004060 /* Give the INITFF and Post time to settle. */
4061 mdelay(100);
dea31012005-04-17 16:05:31 -05004062
James Smart0d878412009-10-02 15:16:56 -04004063 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4064 if (hba_aer_enabled)
4065 pci_disable_pcie_error_reporting(phba->pcidev);
4066
Jamie Wellnitz41415862006-02-28 19:25:27 -05004067 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004068
4069 return 0;
4070}
4071
James Smarte59058c2008-08-24 21:49:00 -04004072/**
James Smartda0436e2009-05-22 14:51:39 -04004073 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4074 * @phba: Pointer to HBA context object.
4075 *
4076 * This function is called in the SLI initialization code path to restart
4077 * a SLI4 HBA. The caller is not required to hold any lock.
4078 * At the end of the function, it calls lpfc_hba_down_post function to
4079 * free any pending commands.
4080 **/
4081static int
4082lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4083{
4084 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004085 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004086 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004087
4088 /* Restart HBA */
4089 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4090 "0296 Restart HBA Data: x%x x%x\n",
4091 phba->pport->port_state, psli->sli_flag);
4092
James Smart75baf692010-06-08 18:31:21 -04004093 /* Take PCIe device Advanced Error Reporting (AER) state */
4094 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4095
James Smart27b01b82012-05-09 21:19:44 -04004096 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004097
4098 spin_lock_irq(&phba->hbalock);
4099 phba->pport->stopped = 0;
4100 phba->link_state = LPFC_INIT_START;
4101 phba->hba_flag = 0;
4102 spin_unlock_irq(&phba->hbalock);
4103
4104 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4105 psli->stats_start = get_seconds();
4106
James Smart75baf692010-06-08 18:31:21 -04004107 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4108 if (hba_aer_enabled)
4109 pci_disable_pcie_error_reporting(phba->pcidev);
4110
James Smartda0436e2009-05-22 14:51:39 -04004111 lpfc_hba_down_post(phba);
4112
James Smart27b01b82012-05-09 21:19:44 -04004113 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004114}
4115
4116/**
4117 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4118 * @phba: Pointer to HBA context object.
4119 *
4120 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4121 * API jump table function pointer from the lpfc_hba struct.
4122**/
4123int
4124lpfc_sli_brdrestart(struct lpfc_hba *phba)
4125{
4126 return phba->lpfc_sli_brdrestart(phba);
4127}
4128
4129/**
James Smart3621a712009-04-06 18:47:14 -04004130 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004131 * @phba: Pointer to HBA context object.
4132 *
4133 * This function is called after a HBA restart to wait for successful
4134 * restart of the HBA. Successful restart of the HBA is indicated by
4135 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4136 * iteration, the function will restart the HBA again. The function returns
4137 * zero if HBA successfully restarted else returns negative error code.
4138 **/
dea31012005-04-17 16:05:31 -05004139static int
4140lpfc_sli_chipset_init(struct lpfc_hba *phba)
4141{
4142 uint32_t status, i = 0;
4143
4144 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004145 if (lpfc_readl(phba->HSregaddr, &status))
4146 return -EIO;
dea31012005-04-17 16:05:31 -05004147
4148 /* Check status register to see what current state is */
4149 i = 0;
4150 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4151
James Smartdcf2a4e2010-09-29 11:18:53 -04004152 /* Check every 10ms for 10 retries, then every 100ms for 90
4153 * retries, then every 1 sec for 50 retires for a total of
4154 * ~60 seconds before reset the board again and check every
4155 * 1 sec for 50 retries. The up to 60 seconds before the
4156 * board ready is required by the Falcon FIPS zeroization
4157 * complete, and any reset the board in between shall cause
4158 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004159 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004160 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004161 /* Adapter failed to init, timeout, status reg
4162 <status> */
James Smarted957682007-06-17 19:56:37 -05004163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004164 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004165 "timeout, status reg x%x, "
4166 "FW Data: A8 x%x AC x%x\n", status,
4167 readl(phba->MBslimaddr + 0xa8),
4168 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004169 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004170 return -ETIMEDOUT;
4171 }
4172
4173 /* Check to see if any errors occurred during init */
4174 if (status & HS_FFERM) {
4175 /* ERROR: During chipset initialization */
4176 /* Adapter failed to init, chipset, status reg
4177 <status> */
James Smarted957682007-06-17 19:56:37 -05004178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004179 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004180 "chipset, status reg x%x, "
4181 "FW Data: A8 x%x AC x%x\n", status,
4182 readl(phba->MBslimaddr + 0xa8),
4183 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004184 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004185 return -EIO;
4186 }
4187
James Smartdcf2a4e2010-09-29 11:18:53 -04004188 if (i <= 10)
dea31012005-04-17 16:05:31 -05004189 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004190 else if (i <= 100)
4191 msleep(100);
4192 else
4193 msleep(1000);
dea31012005-04-17 16:05:31 -05004194
James Smartdcf2a4e2010-09-29 11:18:53 -04004195 if (i == 150) {
4196 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004197 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004198 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004199 }
4200 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004201 if (lpfc_readl(phba->HSregaddr, &status))
4202 return -EIO;
dea31012005-04-17 16:05:31 -05004203 }
4204
4205 /* Check to see if any errors occurred during init */
4206 if (status & HS_FFERM) {
4207 /* ERROR: During chipset initialization */
4208 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004210 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004211 "status reg x%x, "
4212 "FW Data: A8 x%x AC x%x\n", status,
4213 readl(phba->MBslimaddr + 0xa8),
4214 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004215 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004216 return -EIO;
4217 }
4218
4219 /* Clear all interrupt enable conditions */
4220 writel(0, phba->HCregaddr);
4221 readl(phba->HCregaddr); /* flush */
4222
4223 /* setup host attn register */
4224 writel(0xffffffff, phba->HAregaddr);
4225 readl(phba->HAregaddr); /* flush */
4226 return 0;
4227}
4228
James Smarte59058c2008-08-24 21:49:00 -04004229/**
James Smart3621a712009-04-06 18:47:14 -04004230 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004231 *
4232 * This function calculates and returns the number of HBQs required to be
4233 * configured.
4234 **/
James Smart78b2d852007-08-02 11:10:21 -04004235int
James Smarted957682007-06-17 19:56:37 -05004236lpfc_sli_hbq_count(void)
4237{
James Smart92d7f7b2007-06-17 19:56:38 -05004238 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004239}
4240
James Smarte59058c2008-08-24 21:49:00 -04004241/**
James Smart3621a712009-04-06 18:47:14 -04004242 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004243 *
4244 * This function adds the number of hbq entries in every HBQ to get
4245 * the total number of hbq entries required for the HBA and returns
4246 * the total count.
4247 **/
James Smarted957682007-06-17 19:56:37 -05004248static int
4249lpfc_sli_hbq_entry_count(void)
4250{
4251 int hbq_count = lpfc_sli_hbq_count();
4252 int count = 0;
4253 int i;
4254
4255 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004256 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004257 return count;
4258}
4259
James Smarte59058c2008-08-24 21:49:00 -04004260/**
James Smart3621a712009-04-06 18:47:14 -04004261 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004262 *
4263 * This function calculates amount of memory required for all hbq entries
4264 * to be configured and returns the total memory required.
4265 **/
dea31012005-04-17 16:05:31 -05004266int
James Smarted957682007-06-17 19:56:37 -05004267lpfc_sli_hbq_size(void)
4268{
4269 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4270}
4271
James Smarte59058c2008-08-24 21:49:00 -04004272/**
James Smart3621a712009-04-06 18:47:14 -04004273 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004274 * @phba: Pointer to HBA context object.
4275 *
4276 * This function is called during the SLI initialization to configure
4277 * all the HBQs and post buffers to the HBQ. The caller is not
4278 * required to hold any locks. This function will return zero if successful
4279 * else it will return negative error code.
4280 **/
James Smarted957682007-06-17 19:56:37 -05004281static int
4282lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4283{
4284 int hbq_count = lpfc_sli_hbq_count();
4285 LPFC_MBOXQ_t *pmb;
4286 MAILBOX_t *pmbox;
4287 uint32_t hbqno;
4288 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004289
James Smart92d7f7b2007-06-17 19:56:38 -05004290 /* Get a Mailbox buffer to setup mailbox
4291 * commands for HBA initialization
4292 */
James Smarted957682007-06-17 19:56:37 -05004293 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4294
4295 if (!pmb)
4296 return -ENOMEM;
4297
James Smart04c68492009-05-22 14:52:52 -04004298 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004299
4300 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4301 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004302 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004303
4304 hbq_entry_index = 0;
4305 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4306 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4307 phba->hbqs[hbqno].hbqPutIdx = 0;
4308 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4309 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004310 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004311 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4312 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004313 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4314
4315 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4316 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4317 mbxStatus <status>, ring <num> */
4318
4319 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004320 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004321 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004322 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004323 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004324 pmbox->mbxStatus, hbqno);
4325
4326 phba->link_state = LPFC_HBA_ERROR;
4327 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004328 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004329 }
4330 }
4331 phba->hbq_count = hbq_count;
4332
James Smarted957682007-06-17 19:56:37 -05004333 mempool_free(pmb, phba->mbox_mem_pool);
4334
James Smart92d7f7b2007-06-17 19:56:38 -05004335 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004336 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4337 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004338 return 0;
4339}
4340
James Smarte59058c2008-08-24 21:49:00 -04004341/**
James Smart4f774512009-05-22 14:52:35 -04004342 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4343 * @phba: Pointer to HBA context object.
4344 *
4345 * This function is called during the SLI initialization to configure
4346 * all the HBQs and post buffers to the HBQ. The caller is not
4347 * required to hold any locks. This function will return zero if successful
4348 * else it will return negative error code.
4349 **/
4350static int
4351lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4352{
4353 phba->hbq_in_use = 1;
4354 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4355 phba->hbq_count = 1;
4356 /* Initially populate or replenish the HBQs */
4357 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4358 return 0;
4359}
4360
4361/**
James Smart3621a712009-04-06 18:47:14 -04004362 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004363 * @phba: Pointer to HBA context object.
4364 * @sli_mode: sli mode - 2/3
4365 *
4366 * This function is called by the sli intialization code path
4367 * to issue config_port mailbox command. This function restarts the
4368 * HBA firmware and issues a config_port mailbox command to configure
4369 * the SLI interface in the sli mode specified by sli_mode
4370 * variable. The caller is not required to hold any locks.
4371 * The function returns 0 if successful, else returns negative error
4372 * code.
4373 **/
James Smart93996272008-08-24 21:50:30 -04004374int
4375lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004376{
4377 LPFC_MBOXQ_t *pmb;
4378 uint32_t resetcount = 0, rc = 0, done = 0;
4379
4380 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4381 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004382 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004383 return -ENOMEM;
4384 }
4385
James Smarted957682007-06-17 19:56:37 -05004386 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004387 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004388 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004389 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004390 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004391 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004392 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004393 rc = lpfc_sli_chipset_init(phba);
4394 if (rc)
4395 break;
4396
James Smart2e0fef82007-06-17 19:56:36 -05004397 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004398 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004399 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004400 resetcount++;
4401
James Smarted957682007-06-17 19:56:37 -05004402 /* Call pre CONFIG_PORT mailbox command initialization. A
4403 * value of 0 means the call was successful. Any other
4404 * nonzero value is a failure, but if ERESTART is returned,
4405 * the driver may reset the HBA and try again.
4406 */
dea31012005-04-17 16:05:31 -05004407 rc = lpfc_config_port_prep(phba);
4408 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004409 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004410 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004411 } else if (rc)
dea31012005-04-17 16:05:31 -05004412 break;
James Smart6d368e52011-05-24 11:44:12 -04004413
James Smart2e0fef82007-06-17 19:56:36 -05004414 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004415 lpfc_config_port(phba, pmb);
4416 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004417 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4418 LPFC_SLI3_HBQ_ENABLED |
4419 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004420 LPFC_SLI3_BG_ENABLED |
4421 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004422 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004424 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004425 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004426 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004427 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004428 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004429 spin_unlock_irq(&phba->hbalock);
4430 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004431 } else {
4432 /* Allow asynchronous mailbox command to go through */
4433 spin_lock_irq(&phba->hbalock);
4434 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4435 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004436 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004437
4438 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4439 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4440 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4441 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004442 }
dea31012005-04-17 16:05:31 -05004443 }
James Smarted957682007-06-17 19:56:37 -05004444 if (!done) {
4445 rc = -EINVAL;
4446 goto do_prep_failed;
4447 }
James Smart04c68492009-05-22 14:52:52 -04004448 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4449 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004450 rc = -ENXIO;
4451 goto do_prep_failed;
4452 }
James Smart04c68492009-05-22 14:52:52 -04004453 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004454 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004455 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4456 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4457 phba->max_vpi : phba->max_vports;
4458
James Smart34b02dc2008-08-24 21:49:55 -04004459 } else
4460 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004461 phba->fips_level = 0;
4462 phba->fips_spec_rev = 0;
4463 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004464 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004465 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4466 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4468 "2850 Security Crypto Active. FIPS x%d "
4469 "(Spec Rev: x%d)",
4470 phba->fips_level, phba->fips_spec_rev);
4471 }
4472 if (pmb->u.mb.un.varCfgPort.sec_err) {
4473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4474 "2856 Config Port Security Crypto "
4475 "Error: x%x ",
4476 pmb->u.mb.un.varCfgPort.sec_err);
4477 }
James Smart04c68492009-05-22 14:52:52 -04004478 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004479 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004480 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004481 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004482
4483 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4484 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004485
4486 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004487 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004488 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4489 else
4490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4491 "0443 Adapter did not grant "
4492 "BlockGuard\n");
4493 }
James Smart34b02dc2008-08-24 21:49:55 -04004494 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004495 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004496 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004497 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004498 }
James Smart92d7f7b2007-06-17 19:56:38 -05004499do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004500 mempool_free(pmb, phba->mbox_mem_pool);
4501 return rc;
4502}
4503
James Smarte59058c2008-08-24 21:49:00 -04004504
4505/**
James Smart3621a712009-04-06 18:47:14 -04004506 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004507 * @phba: Pointer to HBA context object.
4508 *
4509 * This function is the main SLI intialization function. This function
4510 * is called by the HBA intialization code, HBA reset code and HBA
4511 * error attention handler code. Caller is not required to hold any
4512 * locks. This function issues config_port mailbox command to configure
4513 * the SLI, setup iocb rings and HBQ rings. In the end the function
4514 * calls the config_port_post function to issue init_link mailbox
4515 * command and to start the discovery. The function will return zero
4516 * if successful, else it will return negative error code.
4517 **/
James Smarted957682007-06-17 19:56:37 -05004518int
4519lpfc_sli_hba_setup(struct lpfc_hba *phba)
4520{
4521 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004522 int mode = 3, i;
4523 int longs;
James Smarted957682007-06-17 19:56:37 -05004524
4525 switch (lpfc_sli_mode) {
4526 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004527 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004529 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004530 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004531 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004532 break;
4533 }
James Smarted957682007-06-17 19:56:37 -05004534 mode = 2;
4535 break;
4536 case 0:
4537 case 3:
4538 break;
4539 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004541 "1819 Unrecognized lpfc_sli_mode "
4542 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004543
4544 break;
4545 }
4546
James Smart93996272008-08-24 21:50:30 -04004547 rc = lpfc_sli_config_port(phba, mode);
4548
James Smarted957682007-06-17 19:56:37 -05004549 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004551 "1820 Unable to select SLI-3. "
4552 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004553 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004554 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004555 if (rc)
dea31012005-04-17 16:05:31 -05004556 goto lpfc_sli_hba_setup_error;
4557
James Smart0d878412009-10-02 15:16:56 -04004558 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4559 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4560 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4561 if (!rc) {
4562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4563 "2709 This device supports "
4564 "Advanced Error Reporting (AER)\n");
4565 spin_lock_irq(&phba->hbalock);
4566 phba->hba_flag |= HBA_AER_ENABLED;
4567 spin_unlock_irq(&phba->hbalock);
4568 } else {
4569 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4570 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004571 "Advanced Error Reporting (AER): %d\n",
4572 rc);
James Smart0d878412009-10-02 15:16:56 -04004573 phba->cfg_aer_support = 0;
4574 }
4575 }
4576
James Smarted957682007-06-17 19:56:37 -05004577 if (phba->sli_rev == 3) {
4578 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4579 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004580 } else {
4581 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4582 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004583 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004584 }
4585
4586 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004587 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4588 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004589 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004590
4591 if (rc)
4592 goto lpfc_sli_hba_setup_error;
4593
James Smart6d368e52011-05-24 11:44:12 -04004594 /* Initialize VPIs. */
4595 if (phba->sli_rev == LPFC_SLI_REV3) {
4596 /*
4597 * The VPI bitmask and physical ID array are allocated
4598 * and initialized once only - at driver load. A port
4599 * reset doesn't need to reinitialize this memory.
4600 */
4601 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4602 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4603 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4604 GFP_KERNEL);
4605 if (!phba->vpi_bmask) {
4606 rc = -ENOMEM;
4607 goto lpfc_sli_hba_setup_error;
4608 }
4609
4610 phba->vpi_ids = kzalloc(
4611 (phba->max_vpi+1) * sizeof(uint16_t),
4612 GFP_KERNEL);
4613 if (!phba->vpi_ids) {
4614 kfree(phba->vpi_bmask);
4615 rc = -ENOMEM;
4616 goto lpfc_sli_hba_setup_error;
4617 }
4618 for (i = 0; i < phba->max_vpi; i++)
4619 phba->vpi_ids[i] = i;
4620 }
4621 }
4622
James Smart93996272008-08-24 21:50:30 -04004623 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004624 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4625 rc = lpfc_sli_hbq_setup(phba);
4626 if (rc)
4627 goto lpfc_sli_hba_setup_error;
4628 }
James Smart04c68492009-05-22 14:52:52 -04004629 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004630 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004631 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004632
4633 rc = lpfc_config_port_post(phba);
4634 if (rc)
4635 goto lpfc_sli_hba_setup_error;
4636
James Smarted957682007-06-17 19:56:37 -05004637 return rc;
4638
James Smart92d7f7b2007-06-17 19:56:38 -05004639lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004640 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004642 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004643 return rc;
4644}
4645
James Smartda0436e2009-05-22 14:51:39 -04004646/**
4647 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4648 * @phba: Pointer to HBA context object.
4649 * @mboxq: mailbox pointer.
4650 * This function issue a dump mailbox command to read config region
4651 * 23 and parse the records in the region and populate driver
4652 * data structure.
4653 **/
4654static int
James Smartff78d8f2011-12-13 13:21:35 -05004655lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004656{
James Smartff78d8f2011-12-13 13:21:35 -05004657 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004658 struct lpfc_dmabuf *mp;
4659 struct lpfc_mqe *mqe;
4660 uint32_t data_length;
4661 int rc;
4662
4663 /* Program the default value of vlan_id and fc_map */
4664 phba->valid_vlan = 0;
4665 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4666 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4667 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4668
James Smartff78d8f2011-12-13 13:21:35 -05004669 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4670 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004671 return -ENOMEM;
4672
James Smartff78d8f2011-12-13 13:21:35 -05004673 mqe = &mboxq->u.mqe;
4674 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4675 rc = -ENOMEM;
4676 goto out_free_mboxq;
4677 }
4678
James Smartda0436e2009-05-22 14:51:39 -04004679 mp = (struct lpfc_dmabuf *) mboxq->context1;
4680 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4681
4682 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4683 "(%d):2571 Mailbox cmd x%x Status x%x "
4684 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4685 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4686 "CQ: x%x x%x x%x x%x\n",
4687 mboxq->vport ? mboxq->vport->vpi : 0,
4688 bf_get(lpfc_mqe_command, mqe),
4689 bf_get(lpfc_mqe_status, mqe),
4690 mqe->un.mb_words[0], mqe->un.mb_words[1],
4691 mqe->un.mb_words[2], mqe->un.mb_words[3],
4692 mqe->un.mb_words[4], mqe->un.mb_words[5],
4693 mqe->un.mb_words[6], mqe->un.mb_words[7],
4694 mqe->un.mb_words[8], mqe->un.mb_words[9],
4695 mqe->un.mb_words[10], mqe->un.mb_words[11],
4696 mqe->un.mb_words[12], mqe->un.mb_words[13],
4697 mqe->un.mb_words[14], mqe->un.mb_words[15],
4698 mqe->un.mb_words[16], mqe->un.mb_words[50],
4699 mboxq->mcqe.word0,
4700 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4701 mboxq->mcqe.trailer);
4702
4703 if (rc) {
4704 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4705 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004706 rc = -EIO;
4707 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004708 }
4709 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004710 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004711 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4712 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004713 rc = -EIO;
4714 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004715 }
James Smartda0436e2009-05-22 14:51:39 -04004716
4717 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4718 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4719 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004720 rc = 0;
4721
4722out_free_mboxq:
4723 mempool_free(mboxq, phba->mbox_mem_pool);
4724 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004725}
4726
4727/**
4728 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4729 * @phba: pointer to lpfc hba data structure.
4730 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4731 * @vpd: pointer to the memory to hold resulting port vpd data.
4732 * @vpd_size: On input, the number of bytes allocated to @vpd.
4733 * On output, the number of data bytes in @vpd.
4734 *
4735 * This routine executes a READ_REV SLI4 mailbox command. In
4736 * addition, this routine gets the port vpd data.
4737 *
4738 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004739 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004740 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004741 **/
4742static int
4743lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4744 uint8_t *vpd, uint32_t *vpd_size)
4745{
4746 int rc = 0;
4747 uint32_t dma_size;
4748 struct lpfc_dmabuf *dmabuf;
4749 struct lpfc_mqe *mqe;
4750
4751 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4752 if (!dmabuf)
4753 return -ENOMEM;
4754
4755 /*
4756 * Get a DMA buffer for the vpd data resulting from the READ_REV
4757 * mailbox command.
4758 */
4759 dma_size = *vpd_size;
4760 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4761 dma_size,
4762 &dmabuf->phys,
4763 GFP_KERNEL);
4764 if (!dmabuf->virt) {
4765 kfree(dmabuf);
4766 return -ENOMEM;
4767 }
4768 memset(dmabuf->virt, 0, dma_size);
4769
4770 /*
4771 * The SLI4 implementation of READ_REV conflicts at word1,
4772 * bits 31:16 and SLI4 adds vpd functionality not present
4773 * in SLI3. This code corrects the conflicts.
4774 */
4775 lpfc_read_rev(phba, mboxq);
4776 mqe = &mboxq->u.mqe;
4777 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4778 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4779 mqe->un.read_rev.word1 &= 0x0000FFFF;
4780 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4781 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4782
4783 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4784 if (rc) {
4785 dma_free_coherent(&phba->pcidev->dev, dma_size,
4786 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004787 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004788 return -EIO;
4789 }
4790
James Smartda0436e2009-05-22 14:51:39 -04004791 /*
4792 * The available vpd length cannot be bigger than the
4793 * DMA buffer passed to the port. Catch the less than
4794 * case and update the caller's size.
4795 */
4796 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4797 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4798
James Smartd7c47992010-06-08 18:31:54 -04004799 memcpy(vpd, dmabuf->virt, *vpd_size);
4800
James Smartda0436e2009-05-22 14:51:39 -04004801 dma_free_coherent(&phba->pcidev->dev, dma_size,
4802 dmabuf->virt, dmabuf->phys);
4803 kfree(dmabuf);
4804 return 0;
4805}
4806
4807/**
James Smartcd1c8302011-10-10 21:33:25 -04004808 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4809 * @phba: pointer to lpfc hba data structure.
4810 *
4811 * This routine retrieves SLI4 device physical port name this PCI function
4812 * is attached to.
4813 *
4814 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004815 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004816 * otherwise - failed to retrieve physical port name
4817 **/
4818static int
4819lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4820{
4821 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004822 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4823 struct lpfc_controller_attribute *cntl_attr;
4824 struct lpfc_mbx_get_port_name *get_port_name;
4825 void *virtaddr = NULL;
4826 uint32_t alloclen, reqlen;
4827 uint32_t shdr_status, shdr_add_status;
4828 union lpfc_sli4_cfg_shdr *shdr;
4829 char cport_name = 0;
4830 int rc;
4831
4832 /* We assume nothing at this point */
4833 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4834 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4835
4836 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4837 if (!mboxq)
4838 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004839 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004840 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4841 lpfc_sli4_read_config(phba);
4842 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4843 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004844
4845 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4846 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4847 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4848 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4849 LPFC_SLI4_MBX_NEMBED);
4850 if (alloclen < reqlen) {
4851 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4852 "3084 Allocated DMA memory size (%d) is "
4853 "less than the requested DMA memory size "
4854 "(%d)\n", alloclen, reqlen);
4855 rc = -ENOMEM;
4856 goto out_free_mboxq;
4857 }
4858 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4859 virtaddr = mboxq->sge_array->addr[0];
4860 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4861 shdr = &mbx_cntl_attr->cfg_shdr;
4862 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4863 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4864 if (shdr_status || shdr_add_status || rc) {
4865 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4866 "3085 Mailbox x%x (x%x/x%x) failed, "
4867 "rc:x%x, status:x%x, add_status:x%x\n",
4868 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4869 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4870 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4871 rc, shdr_status, shdr_add_status);
4872 rc = -ENXIO;
4873 goto out_free_mboxq;
4874 }
4875 cntl_attr = &mbx_cntl_attr->cntl_attr;
4876 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4877 phba->sli4_hba.lnk_info.lnk_tp =
4878 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4879 phba->sli4_hba.lnk_info.lnk_no =
4880 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4881 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4882 "3086 lnk_type:%d, lnk_numb:%d\n",
4883 phba->sli4_hba.lnk_info.lnk_tp,
4884 phba->sli4_hba.lnk_info.lnk_no);
4885
4886retrieve_ppname:
4887 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4888 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4889 sizeof(struct lpfc_mbx_get_port_name) -
4890 sizeof(struct lpfc_sli4_cfg_mhdr),
4891 LPFC_SLI4_MBX_EMBED);
4892 get_port_name = &mboxq->u.mqe.un.get_port_name;
4893 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4894 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4895 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4896 phba->sli4_hba.lnk_info.lnk_tp);
4897 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4898 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4899 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4900 if (shdr_status || shdr_add_status || rc) {
4901 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4902 "3087 Mailbox x%x (x%x/x%x) failed: "
4903 "rc:x%x, status:x%x, add_status:x%x\n",
4904 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4905 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4906 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4907 rc, shdr_status, shdr_add_status);
4908 rc = -ENXIO;
4909 goto out_free_mboxq;
4910 }
4911 switch (phba->sli4_hba.lnk_info.lnk_no) {
4912 case LPFC_LINK_NUMBER_0:
4913 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4914 &get_port_name->u.response);
4915 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4916 break;
4917 case LPFC_LINK_NUMBER_1:
4918 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4919 &get_port_name->u.response);
4920 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4921 break;
4922 case LPFC_LINK_NUMBER_2:
4923 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4924 &get_port_name->u.response);
4925 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4926 break;
4927 case LPFC_LINK_NUMBER_3:
4928 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4929 &get_port_name->u.response);
4930 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4931 break;
4932 default:
4933 break;
4934 }
4935
4936 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4937 phba->Port[0] = cport_name;
4938 phba->Port[1] = '\0';
4939 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4940 "3091 SLI get port name: %s\n", phba->Port);
4941 }
4942
4943out_free_mboxq:
4944 if (rc != MBX_TIMEOUT) {
4945 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4946 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4947 else
4948 mempool_free(mboxq, phba->mbox_mem_pool);
4949 }
4950 return rc;
4951}
4952
4953/**
James Smartda0436e2009-05-22 14:51:39 -04004954 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4955 * @phba: pointer to lpfc hba data structure.
4956 *
4957 * This routine is called to explicitly arm the SLI4 device's completion and
4958 * event queues
4959 **/
4960static void
4961lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4962{
James Smart962bc512013-01-03 15:44:00 -05004963 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04004964
4965 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4966 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004967 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004968 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04004969 do {
James Smart2e90f4b2011-12-13 13:22:37 -05004970 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4971 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04004972 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05004973 }
James Smart67d12732012-08-03 12:36:13 -04004974 if (phba->sli4_hba.hba_eq) {
4975 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05004976 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04004977 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05004978 LPFC_QUEUE_REARM);
4979 }
James Smartda0436e2009-05-22 14:51:39 -04004980}
4981
4982/**
James Smart6d368e52011-05-24 11:44:12 -04004983 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4984 * @phba: Pointer to HBA context object.
4985 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004986 * @extnt_count: buffer to hold port available extent count.
4987 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004988 *
James Smartb76f2dc2011-07-22 18:37:42 -04004989 * This function calls the port and retrievs the number of available
4990 * extents and their size for a particular extent type.
4991 *
4992 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004993 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004994int
James Smart6d368e52011-05-24 11:44:12 -04004995lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4996 uint16_t *extnt_count, uint16_t *extnt_size)
4997{
4998 int rc = 0;
4999 uint32_t length;
5000 uint32_t mbox_tmo;
5001 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5002 LPFC_MBOXQ_t *mbox;
5003
5004 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5005 if (!mbox)
5006 return -ENOMEM;
5007
5008 /* Find out how many extents are available for this resource type */
5009 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5010 sizeof(struct lpfc_sli4_cfg_mhdr));
5011 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5012 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5013 length, LPFC_SLI4_MBX_EMBED);
5014
5015 /* Send an extents count of 0 - the GET doesn't use it. */
5016 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5017 LPFC_SLI4_MBX_EMBED);
5018 if (unlikely(rc)) {
5019 rc = -EIO;
5020 goto err_exit;
5021 }
5022
5023 if (!phba->sli4_hba.intr_enable)
5024 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5025 else {
James Smarta183a152011-10-10 21:32:43 -04005026 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005027 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5028 }
5029 if (unlikely(rc)) {
5030 rc = -EIO;
5031 goto err_exit;
5032 }
5033
5034 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5035 if (bf_get(lpfc_mbox_hdr_status,
5036 &rsrc_info->header.cfg_shdr.response)) {
5037 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5038 "2930 Failed to get resource extents "
5039 "Status 0x%x Add'l Status 0x%x\n",
5040 bf_get(lpfc_mbox_hdr_status,
5041 &rsrc_info->header.cfg_shdr.response),
5042 bf_get(lpfc_mbox_hdr_add_status,
5043 &rsrc_info->header.cfg_shdr.response));
5044 rc = -EIO;
5045 goto err_exit;
5046 }
5047
5048 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5049 &rsrc_info->u.rsp);
5050 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5051 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005052
5053 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5054 "3162 Retrieved extents type-%d from port: count:%d, "
5055 "size:%d\n", type, *extnt_count, *extnt_size);
5056
5057err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005058 mempool_free(mbox, phba->mbox_mem_pool);
5059 return rc;
5060}
5061
5062/**
5063 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5064 * @phba: Pointer to HBA context object.
5065 * @type: The extent type to check.
5066 *
5067 * This function reads the current available extents from the port and checks
5068 * if the extent count or extent size has changed since the last access.
5069 * Callers use this routine post port reset to understand if there is a
5070 * extent reprovisioning requirement.
5071 *
5072 * Returns:
5073 * -Error: error indicates problem.
5074 * 1: Extent count or size has changed.
5075 * 0: No changes.
5076 **/
5077static int
5078lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5079{
5080 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5081 uint16_t size_diff, rsrc_ext_size;
5082 int rc = 0;
5083 struct lpfc_rsrc_blks *rsrc_entry;
5084 struct list_head *rsrc_blk_list = NULL;
5085
5086 size_diff = 0;
5087 curr_ext_cnt = 0;
5088 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5089 &rsrc_ext_cnt,
5090 &rsrc_ext_size);
5091 if (unlikely(rc))
5092 return -EIO;
5093
5094 switch (type) {
5095 case LPFC_RSC_TYPE_FCOE_RPI:
5096 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5097 break;
5098 case LPFC_RSC_TYPE_FCOE_VPI:
5099 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5100 break;
5101 case LPFC_RSC_TYPE_FCOE_XRI:
5102 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5103 break;
5104 case LPFC_RSC_TYPE_FCOE_VFI:
5105 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5106 break;
5107 default:
5108 break;
5109 }
5110
5111 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5112 curr_ext_cnt++;
5113 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5114 size_diff++;
5115 }
5116
5117 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5118 rc = 1;
5119
5120 return rc;
5121}
5122
5123/**
5124 * lpfc_sli4_cfg_post_extnts -
5125 * @phba: Pointer to HBA context object.
5126 * @extnt_cnt - number of available extents.
5127 * @type - the extent type (rpi, xri, vfi, vpi).
5128 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5129 * @mbox - pointer to the caller's allocated mailbox structure.
5130 *
5131 * This function executes the extents allocation request. It also
5132 * takes care of the amount of memory needed to allocate or get the
5133 * allocated extents. It is the caller's responsibility to evaluate
5134 * the response.
5135 *
5136 * Returns:
5137 * -Error: Error value describes the condition found.
5138 * 0: if successful
5139 **/
5140static int
James Smart8a9d2e82012-05-09 21:16:12 -04005141lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005142 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5143{
5144 int rc = 0;
5145 uint32_t req_len;
5146 uint32_t emb_len;
5147 uint32_t alloc_len, mbox_tmo;
5148
5149 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005150 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005151
5152 /*
5153 * Calculate the size of an embedded mailbox. The uint32_t
5154 * accounts for extents-specific word.
5155 */
5156 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5157 sizeof(uint32_t);
5158
5159 /*
5160 * Presume the allocation and response will fit into an embedded
5161 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5162 */
5163 *emb = LPFC_SLI4_MBX_EMBED;
5164 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005165 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005166 sizeof(union lpfc_sli4_cfg_shdr) +
5167 sizeof(uint32_t);
5168 *emb = LPFC_SLI4_MBX_NEMBED;
5169 }
5170
5171 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5172 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5173 req_len, *emb);
5174 if (alloc_len < req_len) {
5175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005176 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005177 "less than the requested DMA memory "
5178 "size (x%x)\n", alloc_len, req_len);
5179 return -ENOMEM;
5180 }
James Smart8a9d2e82012-05-09 21:16:12 -04005181 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005182 if (unlikely(rc))
5183 return -EIO;
5184
5185 if (!phba->sli4_hba.intr_enable)
5186 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5187 else {
James Smarta183a152011-10-10 21:32:43 -04005188 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005189 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5190 }
5191
5192 if (unlikely(rc))
5193 rc = -EIO;
5194 return rc;
5195}
5196
5197/**
5198 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5199 * @phba: Pointer to HBA context object.
5200 * @type: The resource extent type to allocate.
5201 *
5202 * This function allocates the number of elements for the specified
5203 * resource type.
5204 **/
5205static int
5206lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5207{
5208 bool emb = false;
5209 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5210 uint16_t rsrc_id, rsrc_start, j, k;
5211 uint16_t *ids;
5212 int i, rc;
5213 unsigned long longs;
5214 unsigned long *bmask;
5215 struct lpfc_rsrc_blks *rsrc_blks;
5216 LPFC_MBOXQ_t *mbox;
5217 uint32_t length;
5218 struct lpfc_id_range *id_array = NULL;
5219 void *virtaddr = NULL;
5220 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5221 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5222 struct list_head *ext_blk_list;
5223
5224 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5225 &rsrc_cnt,
5226 &rsrc_size);
5227 if (unlikely(rc))
5228 return -EIO;
5229
5230 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5231 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5232 "3009 No available Resource Extents "
5233 "for resource type 0x%x: Count: 0x%x, "
5234 "Size 0x%x\n", type, rsrc_cnt,
5235 rsrc_size);
5236 return -ENOMEM;
5237 }
5238
James Smart8a9d2e82012-05-09 21:16:12 -04005239 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5240 "2903 Post resource extents type-0x%x: "
5241 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005242
5243 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5244 if (!mbox)
5245 return -ENOMEM;
5246
James Smart8a9d2e82012-05-09 21:16:12 -04005247 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005248 if (unlikely(rc)) {
5249 rc = -EIO;
5250 goto err_exit;
5251 }
5252
5253 /*
5254 * Figure out where the response is located. Then get local pointers
5255 * to the response data. The port does not guarantee to respond to
5256 * all extents counts request so update the local variable with the
5257 * allocated count from the port.
5258 */
5259 if (emb == LPFC_SLI4_MBX_EMBED) {
5260 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5261 id_array = &rsrc_ext->u.rsp.id[0];
5262 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5263 } else {
5264 virtaddr = mbox->sge_array->addr[0];
5265 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5266 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5267 id_array = &n_rsrc->id;
5268 }
5269
5270 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5271 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5272
5273 /*
5274 * Based on the resource size and count, correct the base and max
5275 * resource values.
5276 */
5277 length = sizeof(struct lpfc_rsrc_blks);
5278 switch (type) {
5279 case LPFC_RSC_TYPE_FCOE_RPI:
5280 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5281 sizeof(unsigned long),
5282 GFP_KERNEL);
5283 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5284 rc = -ENOMEM;
5285 goto err_exit;
5286 }
5287 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5288 sizeof(uint16_t),
5289 GFP_KERNEL);
5290 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5291 kfree(phba->sli4_hba.rpi_bmask);
5292 rc = -ENOMEM;
5293 goto err_exit;
5294 }
5295
5296 /*
5297 * The next_rpi was initialized with the maximum available
5298 * count but the port may allocate a smaller number. Catch
5299 * that case and update the next_rpi.
5300 */
5301 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5302
5303 /* Initialize local ptrs for common extent processing later. */
5304 bmask = phba->sli4_hba.rpi_bmask;
5305 ids = phba->sli4_hba.rpi_ids;
5306 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5307 break;
5308 case LPFC_RSC_TYPE_FCOE_VPI:
5309 phba->vpi_bmask = kzalloc(longs *
5310 sizeof(unsigned long),
5311 GFP_KERNEL);
5312 if (unlikely(!phba->vpi_bmask)) {
5313 rc = -ENOMEM;
5314 goto err_exit;
5315 }
5316 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5317 sizeof(uint16_t),
5318 GFP_KERNEL);
5319 if (unlikely(!phba->vpi_ids)) {
5320 kfree(phba->vpi_bmask);
5321 rc = -ENOMEM;
5322 goto err_exit;
5323 }
5324
5325 /* Initialize local ptrs for common extent processing later. */
5326 bmask = phba->vpi_bmask;
5327 ids = phba->vpi_ids;
5328 ext_blk_list = &phba->lpfc_vpi_blk_list;
5329 break;
5330 case LPFC_RSC_TYPE_FCOE_XRI:
5331 phba->sli4_hba.xri_bmask = kzalloc(longs *
5332 sizeof(unsigned long),
5333 GFP_KERNEL);
5334 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5335 rc = -ENOMEM;
5336 goto err_exit;
5337 }
James Smart8a9d2e82012-05-09 21:16:12 -04005338 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005339 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5340 sizeof(uint16_t),
5341 GFP_KERNEL);
5342 if (unlikely(!phba->sli4_hba.xri_ids)) {
5343 kfree(phba->sli4_hba.xri_bmask);
5344 rc = -ENOMEM;
5345 goto err_exit;
5346 }
5347
5348 /* Initialize local ptrs for common extent processing later. */
5349 bmask = phba->sli4_hba.xri_bmask;
5350 ids = phba->sli4_hba.xri_ids;
5351 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5352 break;
5353 case LPFC_RSC_TYPE_FCOE_VFI:
5354 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5355 sizeof(unsigned long),
5356 GFP_KERNEL);
5357 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5358 rc = -ENOMEM;
5359 goto err_exit;
5360 }
5361 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5362 sizeof(uint16_t),
5363 GFP_KERNEL);
5364 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5365 kfree(phba->sli4_hba.vfi_bmask);
5366 rc = -ENOMEM;
5367 goto err_exit;
5368 }
5369
5370 /* Initialize local ptrs for common extent processing later. */
5371 bmask = phba->sli4_hba.vfi_bmask;
5372 ids = phba->sli4_hba.vfi_ids;
5373 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5374 break;
5375 default:
5376 /* Unsupported Opcode. Fail call. */
5377 id_array = NULL;
5378 bmask = NULL;
5379 ids = NULL;
5380 ext_blk_list = NULL;
5381 goto err_exit;
5382 }
5383
5384 /*
5385 * Complete initializing the extent configuration with the
5386 * allocated ids assigned to this function. The bitmask serves
5387 * as an index into the array and manages the available ids. The
5388 * array just stores the ids communicated to the port via the wqes.
5389 */
5390 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5391 if ((i % 2) == 0)
5392 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5393 &id_array[k]);
5394 else
5395 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5396 &id_array[k]);
5397
5398 rsrc_blks = kzalloc(length, GFP_KERNEL);
5399 if (unlikely(!rsrc_blks)) {
5400 rc = -ENOMEM;
5401 kfree(bmask);
5402 kfree(ids);
5403 goto err_exit;
5404 }
5405 rsrc_blks->rsrc_start = rsrc_id;
5406 rsrc_blks->rsrc_size = rsrc_size;
5407 list_add_tail(&rsrc_blks->list, ext_blk_list);
5408 rsrc_start = rsrc_id;
5409 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5410 phba->sli4_hba.scsi_xri_start = rsrc_start +
5411 lpfc_sli4_get_els_iocb_cnt(phba);
5412
5413 while (rsrc_id < (rsrc_start + rsrc_size)) {
5414 ids[j] = rsrc_id;
5415 rsrc_id++;
5416 j++;
5417 }
5418 /* Entire word processed. Get next word.*/
5419 if ((i % 2) == 1)
5420 k++;
5421 }
5422 err_exit:
5423 lpfc_sli4_mbox_cmd_free(phba, mbox);
5424 return rc;
5425}
5426
5427/**
5428 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5429 * @phba: Pointer to HBA context object.
5430 * @type: the extent's type.
5431 *
5432 * This function deallocates all extents of a particular resource type.
5433 * SLI4 does not allow for deallocating a particular extent range. It
5434 * is the caller's responsibility to release all kernel memory resources.
5435 **/
5436static int
5437lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5438{
5439 int rc;
5440 uint32_t length, mbox_tmo = 0;
5441 LPFC_MBOXQ_t *mbox;
5442 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5443 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5444
5445 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5446 if (!mbox)
5447 return -ENOMEM;
5448
5449 /*
5450 * This function sends an embedded mailbox because it only sends the
5451 * the resource type. All extents of this type are released by the
5452 * port.
5453 */
5454 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5455 sizeof(struct lpfc_sli4_cfg_mhdr));
5456 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5457 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5458 length, LPFC_SLI4_MBX_EMBED);
5459
5460 /* Send an extents count of 0 - the dealloc doesn't use it. */
5461 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5462 LPFC_SLI4_MBX_EMBED);
5463 if (unlikely(rc)) {
5464 rc = -EIO;
5465 goto out_free_mbox;
5466 }
5467 if (!phba->sli4_hba.intr_enable)
5468 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5469 else {
James Smarta183a152011-10-10 21:32:43 -04005470 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005471 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5472 }
5473 if (unlikely(rc)) {
5474 rc = -EIO;
5475 goto out_free_mbox;
5476 }
5477
5478 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5479 if (bf_get(lpfc_mbox_hdr_status,
5480 &dealloc_rsrc->header.cfg_shdr.response)) {
5481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5482 "2919 Failed to release resource extents "
5483 "for type %d - Status 0x%x Add'l Status 0x%x. "
5484 "Resource memory not released.\n",
5485 type,
5486 bf_get(lpfc_mbox_hdr_status,
5487 &dealloc_rsrc->header.cfg_shdr.response),
5488 bf_get(lpfc_mbox_hdr_add_status,
5489 &dealloc_rsrc->header.cfg_shdr.response));
5490 rc = -EIO;
5491 goto out_free_mbox;
5492 }
5493
5494 /* Release kernel memory resources for the specific type. */
5495 switch (type) {
5496 case LPFC_RSC_TYPE_FCOE_VPI:
5497 kfree(phba->vpi_bmask);
5498 kfree(phba->vpi_ids);
5499 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5500 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5501 &phba->lpfc_vpi_blk_list, list) {
5502 list_del_init(&rsrc_blk->list);
5503 kfree(rsrc_blk);
5504 }
James Smart16a3a202013-04-17 20:14:38 -04005505 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005506 break;
5507 case LPFC_RSC_TYPE_FCOE_XRI:
5508 kfree(phba->sli4_hba.xri_bmask);
5509 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005510 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5511 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5512 list_del_init(&rsrc_blk->list);
5513 kfree(rsrc_blk);
5514 }
5515 break;
5516 case LPFC_RSC_TYPE_FCOE_VFI:
5517 kfree(phba->sli4_hba.vfi_bmask);
5518 kfree(phba->sli4_hba.vfi_ids);
5519 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5520 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5521 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5522 list_del_init(&rsrc_blk->list);
5523 kfree(rsrc_blk);
5524 }
5525 break;
5526 case LPFC_RSC_TYPE_FCOE_RPI:
5527 /* RPI bitmask and physical id array are cleaned up earlier. */
5528 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5529 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5530 list_del_init(&rsrc_blk->list);
5531 kfree(rsrc_blk);
5532 }
5533 break;
5534 default:
5535 break;
5536 }
5537
5538 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5539
5540 out_free_mbox:
5541 mempool_free(mbox, phba->mbox_mem_pool);
5542 return rc;
5543}
5544
5545/**
5546 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5547 * @phba: Pointer to HBA context object.
5548 *
5549 * This function allocates all SLI4 resource identifiers.
5550 **/
5551int
5552lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5553{
5554 int i, rc, error = 0;
5555 uint16_t count, base;
5556 unsigned long longs;
5557
James Smartff78d8f2011-12-13 13:21:35 -05005558 if (!phba->sli4_hba.rpi_hdrs_in_use)
5559 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005560 if (phba->sli4_hba.extents_in_use) {
5561 /*
5562 * The port supports resource extents. The XRI, VPI, VFI, RPI
5563 * resource extent count must be read and allocated before
5564 * provisioning the resource id arrays.
5565 */
5566 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5567 LPFC_IDX_RSRC_RDY) {
5568 /*
5569 * Extent-based resources are set - the driver could
5570 * be in a port reset. Figure out if any corrective
5571 * actions need to be taken.
5572 */
5573 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5574 LPFC_RSC_TYPE_FCOE_VFI);
5575 if (rc != 0)
5576 error++;
5577 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5578 LPFC_RSC_TYPE_FCOE_VPI);
5579 if (rc != 0)
5580 error++;
5581 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5582 LPFC_RSC_TYPE_FCOE_XRI);
5583 if (rc != 0)
5584 error++;
5585 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5586 LPFC_RSC_TYPE_FCOE_RPI);
5587 if (rc != 0)
5588 error++;
5589
5590 /*
5591 * It's possible that the number of resources
5592 * provided to this port instance changed between
5593 * resets. Detect this condition and reallocate
5594 * resources. Otherwise, there is no action.
5595 */
5596 if (error) {
5597 lpfc_printf_log(phba, KERN_INFO,
5598 LOG_MBOX | LOG_INIT,
5599 "2931 Detected extent resource "
5600 "change. Reallocating all "
5601 "extents.\n");
5602 rc = lpfc_sli4_dealloc_extent(phba,
5603 LPFC_RSC_TYPE_FCOE_VFI);
5604 rc = lpfc_sli4_dealloc_extent(phba,
5605 LPFC_RSC_TYPE_FCOE_VPI);
5606 rc = lpfc_sli4_dealloc_extent(phba,
5607 LPFC_RSC_TYPE_FCOE_XRI);
5608 rc = lpfc_sli4_dealloc_extent(phba,
5609 LPFC_RSC_TYPE_FCOE_RPI);
5610 } else
5611 return 0;
5612 }
5613
5614 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5615 if (unlikely(rc))
5616 goto err_exit;
5617
5618 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5619 if (unlikely(rc))
5620 goto err_exit;
5621
5622 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5623 if (unlikely(rc))
5624 goto err_exit;
5625
5626 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5627 if (unlikely(rc))
5628 goto err_exit;
5629 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5630 LPFC_IDX_RSRC_RDY);
5631 return rc;
5632 } else {
5633 /*
5634 * The port does not support resource extents. The XRI, VPI,
5635 * VFI, RPI resource ids were determined from READ_CONFIG.
5636 * Just allocate the bitmasks and provision the resource id
5637 * arrays. If a port reset is active, the resources don't
5638 * need any action - just exit.
5639 */
5640 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005641 LPFC_IDX_RSRC_RDY) {
5642 lpfc_sli4_dealloc_resource_identifiers(phba);
5643 lpfc_sli4_remove_rpis(phba);
5644 }
James Smart6d368e52011-05-24 11:44:12 -04005645 /* RPIs. */
5646 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005647 if (count <= 0) {
5648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5649 "3279 Invalid provisioning of "
5650 "rpi:%d\n", count);
5651 rc = -EINVAL;
5652 goto err_exit;
5653 }
James Smart6d368e52011-05-24 11:44:12 -04005654 base = phba->sli4_hba.max_cfg_param.rpi_base;
5655 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5656 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5657 sizeof(unsigned long),
5658 GFP_KERNEL);
5659 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5660 rc = -ENOMEM;
5661 goto err_exit;
5662 }
5663 phba->sli4_hba.rpi_ids = kzalloc(count *
5664 sizeof(uint16_t),
5665 GFP_KERNEL);
5666 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5667 rc = -ENOMEM;
5668 goto free_rpi_bmask;
5669 }
5670
5671 for (i = 0; i < count; i++)
5672 phba->sli4_hba.rpi_ids[i] = base + i;
5673
5674 /* VPIs. */
5675 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005676 if (count <= 0) {
5677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5678 "3280 Invalid provisioning of "
5679 "vpi:%d\n", count);
5680 rc = -EINVAL;
5681 goto free_rpi_ids;
5682 }
James Smart6d368e52011-05-24 11:44:12 -04005683 base = phba->sli4_hba.max_cfg_param.vpi_base;
5684 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5685 phba->vpi_bmask = kzalloc(longs *
5686 sizeof(unsigned long),
5687 GFP_KERNEL);
5688 if (unlikely(!phba->vpi_bmask)) {
5689 rc = -ENOMEM;
5690 goto free_rpi_ids;
5691 }
5692 phba->vpi_ids = kzalloc(count *
5693 sizeof(uint16_t),
5694 GFP_KERNEL);
5695 if (unlikely(!phba->vpi_ids)) {
5696 rc = -ENOMEM;
5697 goto free_vpi_bmask;
5698 }
5699
5700 for (i = 0; i < count; i++)
5701 phba->vpi_ids[i] = base + i;
5702
5703 /* XRIs. */
5704 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005705 if (count <= 0) {
5706 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5707 "3281 Invalid provisioning of "
5708 "xri:%d\n", count);
5709 rc = -EINVAL;
5710 goto free_vpi_ids;
5711 }
James Smart6d368e52011-05-24 11:44:12 -04005712 base = phba->sli4_hba.max_cfg_param.xri_base;
5713 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5714 phba->sli4_hba.xri_bmask = kzalloc(longs *
5715 sizeof(unsigned long),
5716 GFP_KERNEL);
5717 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5718 rc = -ENOMEM;
5719 goto free_vpi_ids;
5720 }
James Smart41899be2012-03-01 22:34:19 -05005721 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005722 phba->sli4_hba.xri_ids = kzalloc(count *
5723 sizeof(uint16_t),
5724 GFP_KERNEL);
5725 if (unlikely(!phba->sli4_hba.xri_ids)) {
5726 rc = -ENOMEM;
5727 goto free_xri_bmask;
5728 }
5729
5730 for (i = 0; i < count; i++)
5731 phba->sli4_hba.xri_ids[i] = base + i;
5732
5733 /* VFIs. */
5734 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005735 if (count <= 0) {
5736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5737 "3282 Invalid provisioning of "
5738 "vfi:%d\n", count);
5739 rc = -EINVAL;
5740 goto free_xri_ids;
5741 }
James Smart6d368e52011-05-24 11:44:12 -04005742 base = phba->sli4_hba.max_cfg_param.vfi_base;
5743 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5744 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5745 sizeof(unsigned long),
5746 GFP_KERNEL);
5747 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5748 rc = -ENOMEM;
5749 goto free_xri_ids;
5750 }
5751 phba->sli4_hba.vfi_ids = kzalloc(count *
5752 sizeof(uint16_t),
5753 GFP_KERNEL);
5754 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5755 rc = -ENOMEM;
5756 goto free_vfi_bmask;
5757 }
5758
5759 for (i = 0; i < count; i++)
5760 phba->sli4_hba.vfi_ids[i] = base + i;
5761
5762 /*
5763 * Mark all resources ready. An HBA reset doesn't need
5764 * to reset the initialization.
5765 */
5766 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5767 LPFC_IDX_RSRC_RDY);
5768 return 0;
5769 }
5770
5771 free_vfi_bmask:
5772 kfree(phba->sli4_hba.vfi_bmask);
5773 free_xri_ids:
5774 kfree(phba->sli4_hba.xri_ids);
5775 free_xri_bmask:
5776 kfree(phba->sli4_hba.xri_bmask);
5777 free_vpi_ids:
5778 kfree(phba->vpi_ids);
5779 free_vpi_bmask:
5780 kfree(phba->vpi_bmask);
5781 free_rpi_ids:
5782 kfree(phba->sli4_hba.rpi_ids);
5783 free_rpi_bmask:
5784 kfree(phba->sli4_hba.rpi_bmask);
5785 err_exit:
5786 return rc;
5787}
5788
5789/**
5790 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5791 * @phba: Pointer to HBA context object.
5792 *
5793 * This function allocates the number of elements for the specified
5794 * resource type.
5795 **/
5796int
5797lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5798{
5799 if (phba->sli4_hba.extents_in_use) {
5800 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5801 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5802 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5803 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5804 } else {
5805 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005806 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005807 kfree(phba->vpi_ids);
5808 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5809 kfree(phba->sli4_hba.xri_bmask);
5810 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005811 kfree(phba->sli4_hba.vfi_bmask);
5812 kfree(phba->sli4_hba.vfi_ids);
5813 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5814 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5815 }
5816
5817 return 0;
5818}
5819
5820/**
James Smartb76f2dc2011-07-22 18:37:42 -04005821 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5822 * @phba: Pointer to HBA context object.
5823 * @type: The resource extent type.
5824 * @extnt_count: buffer to hold port extent count response
5825 * @extnt_size: buffer to hold port extent size response.
5826 *
5827 * This function calls the port to read the host allocated extents
5828 * for a particular type.
5829 **/
5830int
5831lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5832 uint16_t *extnt_cnt, uint16_t *extnt_size)
5833{
5834 bool emb;
5835 int rc = 0;
5836 uint16_t curr_blks = 0;
5837 uint32_t req_len, emb_len;
5838 uint32_t alloc_len, mbox_tmo;
5839 struct list_head *blk_list_head;
5840 struct lpfc_rsrc_blks *rsrc_blk;
5841 LPFC_MBOXQ_t *mbox;
5842 void *virtaddr = NULL;
5843 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5844 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5845 union lpfc_sli4_cfg_shdr *shdr;
5846
5847 switch (type) {
5848 case LPFC_RSC_TYPE_FCOE_VPI:
5849 blk_list_head = &phba->lpfc_vpi_blk_list;
5850 break;
5851 case LPFC_RSC_TYPE_FCOE_XRI:
5852 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5853 break;
5854 case LPFC_RSC_TYPE_FCOE_VFI:
5855 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5856 break;
5857 case LPFC_RSC_TYPE_FCOE_RPI:
5858 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5859 break;
5860 default:
5861 return -EIO;
5862 }
5863
5864 /* Count the number of extents currently allocatd for this type. */
5865 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5866 if (curr_blks == 0) {
5867 /*
5868 * The GET_ALLOCATED mailbox does not return the size,
5869 * just the count. The size should be just the size
5870 * stored in the current allocated block and all sizes
5871 * for an extent type are the same so set the return
5872 * value now.
5873 */
5874 *extnt_size = rsrc_blk->rsrc_size;
5875 }
5876 curr_blks++;
5877 }
5878
5879 /* Calculate the total requested length of the dma memory. */
5880 req_len = curr_blks * sizeof(uint16_t);
5881
5882 /*
5883 * Calculate the size of an embedded mailbox. The uint32_t
5884 * accounts for extents-specific word.
5885 */
5886 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5887 sizeof(uint32_t);
5888
5889 /*
5890 * Presume the allocation and response will fit into an embedded
5891 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5892 */
5893 emb = LPFC_SLI4_MBX_EMBED;
5894 req_len = emb_len;
5895 if (req_len > emb_len) {
5896 req_len = curr_blks * sizeof(uint16_t) +
5897 sizeof(union lpfc_sli4_cfg_shdr) +
5898 sizeof(uint32_t);
5899 emb = LPFC_SLI4_MBX_NEMBED;
5900 }
5901
5902 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5903 if (!mbox)
5904 return -ENOMEM;
5905 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5906
5907 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5908 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5909 req_len, emb);
5910 if (alloc_len < req_len) {
5911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5912 "2983 Allocated DMA memory size (x%x) is "
5913 "less than the requested DMA memory "
5914 "size (x%x)\n", alloc_len, req_len);
5915 rc = -ENOMEM;
5916 goto err_exit;
5917 }
5918 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5919 if (unlikely(rc)) {
5920 rc = -EIO;
5921 goto err_exit;
5922 }
5923
5924 if (!phba->sli4_hba.intr_enable)
5925 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5926 else {
James Smarta183a152011-10-10 21:32:43 -04005927 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005928 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5929 }
5930
5931 if (unlikely(rc)) {
5932 rc = -EIO;
5933 goto err_exit;
5934 }
5935
5936 /*
5937 * Figure out where the response is located. Then get local pointers
5938 * to the response data. The port does not guarantee to respond to
5939 * all extents counts request so update the local variable with the
5940 * allocated count from the port.
5941 */
5942 if (emb == LPFC_SLI4_MBX_EMBED) {
5943 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5944 shdr = &rsrc_ext->header.cfg_shdr;
5945 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5946 } else {
5947 virtaddr = mbox->sge_array->addr[0];
5948 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5949 shdr = &n_rsrc->cfg_shdr;
5950 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5951 }
5952
5953 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5955 "2984 Failed to read allocated resources "
5956 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5957 type,
5958 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5959 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5960 rc = -EIO;
5961 goto err_exit;
5962 }
5963 err_exit:
5964 lpfc_sli4_mbox_cmd_free(phba, mbox);
5965 return rc;
5966}
5967
5968/**
James Smart8a9d2e82012-05-09 21:16:12 -04005969 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5970 * @phba: pointer to lpfc hba data structure.
5971 *
5972 * This routine walks the list of els buffers that have been allocated and
5973 * repost them to the port by using SGL block post. This is needed after a
5974 * pci_function_reset/warm_start or start. It attempts to construct blocks
5975 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5976 * SGL block post mailbox commands to post them to the port. For single els
5977 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5978 * mailbox command for posting.
5979 *
5980 * Returns: 0 = success, non-zero failure.
5981 **/
5982static int
5983lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5984{
5985 struct lpfc_sglq *sglq_entry = NULL;
5986 struct lpfc_sglq *sglq_entry_next = NULL;
5987 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04005988 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04005989 int last_xritag = NO_XRI;
5990 LIST_HEAD(prep_sgl_list);
5991 LIST_HEAD(blck_sgl_list);
5992 LIST_HEAD(allc_sgl_list);
5993 LIST_HEAD(post_sgl_list);
5994 LIST_HEAD(free_sgl_list);
5995
James Smart38c20672013-03-01 16:37:44 -05005996 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04005997 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05005998 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04005999
James Smart711ea882013-04-17 20:18:29 -04006000 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006001 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6002 &allc_sgl_list, list) {
6003 list_del_init(&sglq_entry->list);
6004 block_cnt++;
6005 if ((last_xritag != NO_XRI) &&
6006 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6007 /* a hole in xri block, form a sgl posting block */
6008 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6009 post_cnt = block_cnt - 1;
6010 /* prepare list for next posting block */
6011 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6012 block_cnt = 1;
6013 } else {
6014 /* prepare list for next posting block */
6015 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6016 /* enough sgls for non-embed sgl mbox command */
6017 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6018 list_splice_init(&prep_sgl_list,
6019 &blck_sgl_list);
6020 post_cnt = block_cnt;
6021 block_cnt = 0;
6022 }
6023 }
6024 num_posted++;
6025
6026 /* keep track of last sgl's xritag */
6027 last_xritag = sglq_entry->sli4_xritag;
6028
6029 /* end of repost sgl list condition for els buffers */
6030 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6031 if (post_cnt == 0) {
6032 list_splice_init(&prep_sgl_list,
6033 &blck_sgl_list);
6034 post_cnt = block_cnt;
6035 } else if (block_cnt == 1) {
6036 status = lpfc_sli4_post_sgl(phba,
6037 sglq_entry->phys, 0,
6038 sglq_entry->sli4_xritag);
6039 if (!status) {
6040 /* successful, put sgl to posted list */
6041 list_add_tail(&sglq_entry->list,
6042 &post_sgl_list);
6043 } else {
6044 /* Failure, put sgl to free list */
6045 lpfc_printf_log(phba, KERN_WARNING,
6046 LOG_SLI,
6047 "3159 Failed to post els "
6048 "sgl, xritag:x%x\n",
6049 sglq_entry->sli4_xritag);
6050 list_add_tail(&sglq_entry->list,
6051 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006052 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006053 }
6054 }
6055 }
6056
6057 /* continue until a nembed page worth of sgls */
6058 if (post_cnt == 0)
6059 continue;
6060
6061 /* post the els buffer list sgls as a block */
6062 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6063 post_cnt);
6064
6065 if (!status) {
6066 /* success, put sgl list to posted sgl list */
6067 list_splice_init(&blck_sgl_list, &post_sgl_list);
6068 } else {
6069 /* Failure, put sgl list to free sgl list */
6070 sglq_entry_first = list_first_entry(&blck_sgl_list,
6071 struct lpfc_sglq,
6072 list);
6073 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6074 "3160 Failed to post els sgl-list, "
6075 "xritag:x%x-x%x\n",
6076 sglq_entry_first->sli4_xritag,
6077 (sglq_entry_first->sli4_xritag +
6078 post_cnt - 1));
6079 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006080 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006081 }
6082
6083 /* don't reset xirtag due to hole in xri block */
6084 if (block_cnt == 0)
6085 last_xritag = NO_XRI;
6086
6087 /* reset els sgl post count for next round of posting */
6088 post_cnt = 0;
6089 }
James Smart711ea882013-04-17 20:18:29 -04006090 /* update the number of XRIs posted for ELS */
6091 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006092
6093 /* free the els sgls failed to post */
6094 lpfc_free_sgl_list(phba, &free_sgl_list);
6095
6096 /* push els sgls posted to the availble list */
6097 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006098 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006099 list_splice_init(&post_sgl_list,
6100 &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05006101 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006102 } else {
6103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6104 "3161 Failure to post els sgl to port.\n");
6105 return -EIO;
6106 }
6107 return 0;
6108}
6109
6110/**
James Smartda0436e2009-05-22 14:51:39 -04006111 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6112 * @phba: Pointer to HBA context object.
6113 *
6114 * This function is the main SLI4 device intialization PCI function. This
6115 * function is called by the HBA intialization code, HBA reset code and
6116 * HBA error attention handler code. Caller is not required to hold any
6117 * locks.
6118 **/
6119int
6120lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6121{
6122 int rc;
6123 LPFC_MBOXQ_t *mboxq;
6124 struct lpfc_mqe *mqe;
6125 uint8_t *vpd;
6126 uint32_t vpd_size;
6127 uint32_t ftr_rsp = 0;
6128 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6129 struct lpfc_vport *vport = phba->pport;
6130 struct lpfc_dmabuf *mp;
6131
6132 /* Perform a PCI function reset to start from clean */
6133 rc = lpfc_pci_function_reset(phba);
6134 if (unlikely(rc))
6135 return -ENODEV;
6136
6137 /* Check the HBA Host Status Register for readyness */
6138 rc = lpfc_sli4_post_status_check(phba);
6139 if (unlikely(rc))
6140 return -ENODEV;
6141 else {
6142 spin_lock_irq(&phba->hbalock);
6143 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6144 spin_unlock_irq(&phba->hbalock);
6145 }
6146
6147 /*
6148 * Allocate a single mailbox container for initializing the
6149 * port.
6150 */
6151 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6152 if (!mboxq)
6153 return -ENOMEM;
6154
James Smartda0436e2009-05-22 14:51:39 -04006155 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006156 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006157 vpd = kzalloc(vpd_size, GFP_KERNEL);
6158 if (!vpd) {
6159 rc = -ENOMEM;
6160 goto out_free_mbox;
6161 }
6162
6163 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006164 if (unlikely(rc)) {
6165 kfree(vpd);
6166 goto out_free_mbox;
6167 }
James Smart572709e2013-07-15 18:32:43 -04006168
James Smartda0436e2009-05-22 14:51:39 -04006169 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006170 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6171 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006172 phba->hba_flag |= HBA_FCOE_MODE;
6173 else
6174 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006175
6176 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6177 LPFC_DCBX_CEE_MODE)
6178 phba->hba_flag |= HBA_FIP_SUPPORT;
6179 else
6180 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6181
James Smart4f2e66c2012-05-09 21:17:07 -04006182 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6183
James Smartc31098c2011-04-16 11:03:33 -04006184 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006185 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6186 "0376 READ_REV Error. SLI Level %d "
6187 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006188 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006189 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006190 kfree(vpd);
6191 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006192 }
James Smartcd1c8302011-10-10 21:33:25 -04006193
6194 /*
James Smartff78d8f2011-12-13 13:21:35 -05006195 * Continue initialization with default values even if driver failed
6196 * to read FCoE param config regions, only read parameters if the
6197 * board is FCoE
6198 */
6199 if (phba->hba_flag & HBA_FCOE_MODE &&
6200 lpfc_sli4_read_fcoe_params(phba))
6201 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6202 "2570 Failed to read FCoE parameters\n");
6203
6204 /*
James Smartcd1c8302011-10-10 21:33:25 -04006205 * Retrieve sli4 device physical port name, failure of doing it
6206 * is considered as non-fatal.
6207 */
6208 rc = lpfc_sli4_retrieve_pport_name(phba);
6209 if (!rc)
6210 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6211 "3080 Successful retrieving SLI4 device "
6212 "physical port name: %s.\n", phba->Port);
6213
James Smartda0436e2009-05-22 14:51:39 -04006214 /*
6215 * Evaluate the read rev and vpd data. Populate the driver
6216 * state with the results. If this routine fails, the failure
6217 * is not fatal as the driver will use generic values.
6218 */
6219 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6220 if (unlikely(!rc)) {
6221 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6222 "0377 Error %d parsing vpd. "
6223 "Using defaults.\n", rc);
6224 rc = 0;
6225 }
James Smart76a95d72010-11-20 23:11:48 -05006226 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006227
James Smartf1126682009-06-10 17:22:44 -04006228 /* Save information as VPD data */
6229 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6230 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6231 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6232 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6233 &mqe->un.read_rev);
6234 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6235 &mqe->un.read_rev);
6236 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6237 &mqe->un.read_rev);
6238 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6239 &mqe->un.read_rev);
6240 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6241 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6242 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6243 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6244 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6245 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6246 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6247 "(%d):0380 READ_REV Status x%x "
6248 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6249 mboxq->vport ? mboxq->vport->vpi : 0,
6250 bf_get(lpfc_mqe_status, mqe),
6251 phba->vpd.rev.opFwName,
6252 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6253 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006254
James Smart572709e2013-07-15 18:32:43 -04006255 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6256 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6257 if (phba->pport->cfg_lun_queue_depth > rc) {
6258 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6259 "3362 LUN queue depth changed from %d to %d\n",
6260 phba->pport->cfg_lun_queue_depth, rc);
6261 phba->pport->cfg_lun_queue_depth = rc;
6262 }
6263
6264
James Smartda0436e2009-05-22 14:51:39 -04006265 /*
6266 * Discover the port's supported feature set and match it against the
6267 * hosts requests.
6268 */
6269 lpfc_request_features(phba, mboxq);
6270 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6271 if (unlikely(rc)) {
6272 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006273 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006274 }
6275
6276 /*
6277 * The port must support FCP initiator mode as this is the
6278 * only mode running in the host.
6279 */
6280 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6281 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6282 "0378 No support for fcpi mode.\n");
6283 ftr_rsp++;
6284 }
James Smartfedd3b72011-02-16 12:39:24 -05006285 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6286 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6287 else
6288 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006289 /*
6290 * If the port cannot support the host's requested features
6291 * then turn off the global config parameters to disable the
6292 * feature in the driver. This is not a fatal error.
6293 */
James Smartbf086112011-08-21 21:48:13 -04006294 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6295 if (phba->cfg_enable_bg) {
6296 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6297 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6298 else
6299 ftr_rsp++;
6300 }
James Smartda0436e2009-05-22 14:51:39 -04006301
6302 if (phba->max_vpi && phba->cfg_enable_npiv &&
6303 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6304 ftr_rsp++;
6305
6306 if (ftr_rsp) {
6307 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6308 "0379 Feature Mismatch Data: x%08x %08x "
6309 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6310 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6311 phba->cfg_enable_npiv, phba->max_vpi);
6312 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6313 phba->cfg_enable_bg = 0;
6314 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6315 phba->cfg_enable_npiv = 0;
6316 }
6317
6318 /* These SLI3 features are assumed in SLI4 */
6319 spin_lock_irq(&phba->hbalock);
6320 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6321 spin_unlock_irq(&phba->hbalock);
6322
James Smart6d368e52011-05-24 11:44:12 -04006323 /*
6324 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6325 * calls depends on these resources to complete port setup.
6326 */
6327 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6328 if (rc) {
6329 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6330 "2920 Failed to alloc Resource IDs "
6331 "rc = x%x\n", rc);
6332 goto out_free_mbox;
6333 }
6334
James Smartda0436e2009-05-22 14:51:39 -04006335 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006336 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6337 if (rc) {
6338 phba->link_state = LPFC_HBA_ERROR;
6339 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006340 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006341 }
6342
James Smartda0436e2009-05-22 14:51:39 -04006343 mboxq->vport = vport;
6344 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6345 mp = (struct lpfc_dmabuf *) mboxq->context1;
6346 if (rc == MBX_SUCCESS) {
6347 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6348 rc = 0;
6349 }
6350
6351 /*
6352 * This memory was allocated by the lpfc_read_sparam routine. Release
6353 * it to the mbuf pool.
6354 */
6355 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6356 kfree(mp);
6357 mboxq->context1 = NULL;
6358 if (unlikely(rc)) {
6359 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6360 "0382 READ_SPARAM command failed "
6361 "status %d, mbxStatus x%x\n",
6362 rc, bf_get(lpfc_mqe_status, mqe));
6363 phba->link_state = LPFC_HBA_ERROR;
6364 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006365 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006366 }
6367
James Smart05580562011-05-24 11:40:48 -04006368 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006369
6370 /* Update the fc_host data structures with new wwn. */
6371 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6372 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6373
James Smart8a9d2e82012-05-09 21:16:12 -04006374 /* update host els and scsi xri-sgl sizes and mappings */
6375 rc = lpfc_sli4_xri_sgl_update(phba);
6376 if (unlikely(rc)) {
6377 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6378 "1400 Failed to update xri-sgl size and "
6379 "mapping: %d\n", rc);
6380 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006381 }
6382
James Smart8a9d2e82012-05-09 21:16:12 -04006383 /* register the els sgl pool to the port */
6384 rc = lpfc_sli4_repost_els_sgl_list(phba);
6385 if (unlikely(rc)) {
6386 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6387 "0582 Error %d during els sgl post "
6388 "operation\n", rc);
6389 rc = -ENODEV;
6390 goto out_free_mbox;
6391 }
6392
6393 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006394 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6395 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006396 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006397 "0383 Error %d during scsi sgl post "
6398 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006399 /* Some Scsi buffers were moved to the abort scsi list */
6400 /* A pci function reset will repost them */
6401 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006402 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006403 }
6404
6405 /* Post the rpi header region to the device. */
6406 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6407 if (unlikely(rc)) {
6408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6409 "0393 Error %d during rpi post operation\n",
6410 rc);
6411 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006412 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006413 }
James Smart97f2ecf2012-03-01 22:35:23 -05006414 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006415
James Smart5350d872011-10-10 21:33:49 -04006416 /* Create all the SLI4 queues */
6417 rc = lpfc_sli4_queue_create(phba);
6418 if (rc) {
6419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6420 "3089 Failed to allocate queues\n");
6421 rc = -ENODEV;
6422 goto out_stop_timers;
6423 }
James Smartda0436e2009-05-22 14:51:39 -04006424 /* Set up all the queues to the device */
6425 rc = lpfc_sli4_queue_setup(phba);
6426 if (unlikely(rc)) {
6427 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6428 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006429 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006430 }
6431
6432 /* Arm the CQs and then EQs on device */
6433 lpfc_sli4_arm_cqeq_intr(phba);
6434
6435 /* Indicate device interrupt mode */
6436 phba->sli4_hba.intr_enable = 1;
6437
6438 /* Allow asynchronous mailbox command to go through */
6439 spin_lock_irq(&phba->hbalock);
6440 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6441 spin_unlock_irq(&phba->hbalock);
6442
6443 /* Post receive buffers to the device */
6444 lpfc_sli4_rb_setup(phba);
6445
James Smartfc2b9892010-02-26 14:15:29 -05006446 /* Reset HBA FCF states after HBA reset */
6447 phba->fcf.fcf_flag = 0;
6448 phba->fcf.current_rec.flag = 0;
6449
James Smartda0436e2009-05-22 14:51:39 -04006450 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006451 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006452 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006453
6454 /* Start heart beat timer */
6455 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006456 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006457 phba->hb_outstanding = 0;
6458 phba->last_completion_time = jiffies;
6459
6460 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006461 mod_timer(&phba->eratt_poll,
6462 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006463
James Smart75baf692010-06-08 18:31:21 -04006464 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6465 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6466 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6467 if (!rc) {
6468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6469 "2829 This device supports "
6470 "Advanced Error Reporting (AER)\n");
6471 spin_lock_irq(&phba->hbalock);
6472 phba->hba_flag |= HBA_AER_ENABLED;
6473 spin_unlock_irq(&phba->hbalock);
6474 } else {
6475 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6476 "2830 This device does not support "
6477 "Advanced Error Reporting (AER)\n");
6478 phba->cfg_aer_support = 0;
6479 }
James Smart0a96e972011-07-22 18:37:28 -04006480 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006481 }
6482
James Smart76a95d72010-11-20 23:11:48 -05006483 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6484 /*
6485 * The FC Port needs to register FCFI (index 0)
6486 */
6487 lpfc_reg_fcfi(phba, mboxq);
6488 mboxq->vport = phba->pport;
6489 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006490 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006491 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006492 rc = 0;
6493 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6494 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006495
6496 /* Check if the port is configured to be disabled */
6497 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006498 }
James Smart026abb82011-12-13 13:20:45 -05006499
James Smartda0436e2009-05-22 14:51:39 -04006500 /*
6501 * The port is ready, set the host's link state to LINK_DOWN
6502 * in preparation for link interrupts.
6503 */
James Smartda0436e2009-05-22 14:51:39 -04006504 spin_lock_irq(&phba->hbalock);
6505 phba->link_state = LPFC_LINK_DOWN;
6506 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006507 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6508 (phba->hba_flag & LINK_DISABLED)) {
6509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6510 "3103 Adapter Link is disabled.\n");
6511 lpfc_down_link(phba, mboxq);
6512 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6513 if (rc != MBX_SUCCESS) {
6514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6515 "3104 Adapter failed to issue "
6516 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6517 goto out_unset_queue;
6518 }
6519 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006520 /* don't perform init_link on SLI4 FC port loopback test */
6521 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6522 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6523 if (rc)
6524 goto out_unset_queue;
6525 }
James Smart5350d872011-10-10 21:33:49 -04006526 }
6527 mempool_free(mboxq, phba->mbox_mem_pool);
6528 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006529out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006530 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006531 lpfc_sli4_queue_unset(phba);
6532out_destroy_queue:
6533 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006534out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006535 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006536out_free_mbox:
6537 mempool_free(mboxq, phba->mbox_mem_pool);
6538 return rc;
6539}
James Smarte59058c2008-08-24 21:49:00 -04006540
6541/**
James Smart3621a712009-04-06 18:47:14 -04006542 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006543 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006544 *
James Smarte59058c2008-08-24 21:49:00 -04006545 * This is the callback function for mailbox timer. The mailbox
6546 * timer is armed when a new mailbox command is issued and the timer
6547 * is deleted when the mailbox complete. The function is called by
6548 * the kernel timer code when a mailbox does not complete within
6549 * expected time. This function wakes up the worker thread to
6550 * process the mailbox timeout and returns. All the processing is
6551 * done by the worker thread function lpfc_mbox_timeout_handler.
6552 **/
dea31012005-04-17 16:05:31 -05006553void
6554lpfc_mbox_timeout(unsigned long ptr)
6555{
James Smart92d7f7b2007-06-17 19:56:38 -05006556 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006557 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006558 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006559
James Smart2e0fef82007-06-17 19:56:36 -05006560 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006561 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006562 if (!tmo_posted)
6563 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6564 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6565
James Smart5e9d9b82008-06-14 22:52:53 -04006566 if (!tmo_posted)
6567 lpfc_worker_wake_up(phba);
6568 return;
dea31012005-04-17 16:05:31 -05006569}
6570
James Smarte8d3c3b2013-10-10 12:21:30 -04006571/**
6572 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6573 * are pending
6574 * @phba: Pointer to HBA context object.
6575 *
6576 * This function checks if any mailbox completions are present on the mailbox
6577 * completion queue.
6578 **/
6579bool
6580lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6581{
6582
6583 uint32_t idx;
6584 struct lpfc_queue *mcq;
6585 struct lpfc_mcqe *mcqe;
6586 bool pending_completions = false;
6587
6588 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6589 return false;
6590
6591 /* Check for completions on mailbox completion queue */
6592
6593 mcq = phba->sli4_hba.mbx_cq;
6594 idx = mcq->hba_index;
6595 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6596 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6597 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6598 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6599 pending_completions = true;
6600 break;
6601 }
6602 idx = (idx + 1) % mcq->entry_count;
6603 if (mcq->hba_index == idx)
6604 break;
6605 }
6606 return pending_completions;
6607
6608}
6609
6610/**
6611 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6612 * that were missed.
6613 * @phba: Pointer to HBA context object.
6614 *
6615 * For sli4, it is possible to miss an interrupt. As such mbox completions
6616 * maybe missed causing erroneous mailbox timeouts to occur. This function
6617 * checks to see if mbox completions are on the mailbox completion queue
6618 * and will process all the completions associated with the eq for the
6619 * mailbox completion queue.
6620 **/
6621bool
6622lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6623{
6624
6625 uint32_t eqidx;
6626 struct lpfc_queue *fpeq = NULL;
6627 struct lpfc_eqe *eqe;
6628 bool mbox_pending;
6629
6630 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6631 return false;
6632
6633 /* Find the eq associated with the mcq */
6634
6635 if (phba->sli4_hba.hba_eq)
6636 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6637 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6638 phba->sli4_hba.mbx_cq->assoc_qid) {
6639 fpeq = phba->sli4_hba.hba_eq[eqidx];
6640 break;
6641 }
6642 if (!fpeq)
6643 return false;
6644
6645 /* Turn off interrupts from this EQ */
6646
6647 lpfc_sli4_eq_clr_intr(fpeq);
6648
6649 /* Check to see if a mbox completion is pending */
6650
6651 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6652
6653 /*
6654 * If a mbox completion is pending, process all the events on EQ
6655 * associated with the mbox completion queue (this could include
6656 * mailbox commands, async events, els commands, receive queue data
6657 * and fcp commands)
6658 */
6659
6660 if (mbox_pending)
6661 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6662 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6663 fpeq->EQ_processed++;
6664 }
6665
6666 /* Always clear and re-arm the EQ */
6667
6668 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6669
6670 return mbox_pending;
6671
6672}
James Smarte59058c2008-08-24 21:49:00 -04006673
6674/**
James Smart3621a712009-04-06 18:47:14 -04006675 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006676 * @phba: Pointer to HBA context object.
6677 *
6678 * This function is called from worker thread when a mailbox command times out.
6679 * The caller is not required to hold any locks. This function will reset the
6680 * HBA and recover all the pending commands.
6681 **/
dea31012005-04-17 16:05:31 -05006682void
6683lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6684{
James Smart2e0fef82007-06-17 19:56:36 -05006685 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006686 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006687 struct lpfc_sli *psli = &phba->sli;
6688 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006689
James Smarte8d3c3b2013-10-10 12:21:30 -04006690 /* If the mailbox completed, process the completion and return */
6691 if (lpfc_sli4_process_missed_mbox_completions(phba))
6692 return;
6693
James Smarta257bf92009-04-06 18:48:10 -04006694 /* Check the pmbox pointer first. There is a race condition
6695 * between the mbox timeout handler getting executed in the
6696 * worklist and the mailbox actually completing. When this
6697 * race condition occurs, the mbox_active will be NULL.
6698 */
6699 spin_lock_irq(&phba->hbalock);
6700 if (pmbox == NULL) {
6701 lpfc_printf_log(phba, KERN_WARNING,
6702 LOG_MBOX | LOG_SLI,
6703 "0353 Active Mailbox cleared - mailbox timeout "
6704 "exiting\n");
6705 spin_unlock_irq(&phba->hbalock);
6706 return;
6707 }
6708
dea31012005-04-17 16:05:31 -05006709 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006711 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006712 mb->mbxCommand,
6713 phba->pport->port_state,
6714 phba->sli.sli_flag,
6715 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006716 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006717
James Smart1dcb58e2007-04-25 09:51:30 -04006718 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6719 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006720 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006721 */
James Smart2e0fef82007-06-17 19:56:36 -05006722 spin_lock_irq(&phba->pport->work_port_lock);
6723 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6724 spin_unlock_irq(&phba->pport->work_port_lock);
6725 spin_lock_irq(&phba->hbalock);
6726 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006727 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006728 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006729
6730 pring = &psli->ring[psli->fcp_ring];
6731 lpfc_sli_abort_iocb_ring(phba, pring);
6732
6733 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006734 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006735
6736 /* Reset the HBA device */
6737 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006738}
6739
James Smarte59058c2008-08-24 21:49:00 -04006740/**
James Smart3772a992009-05-22 14:50:54 -04006741 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006742 * @phba: Pointer to HBA context object.
6743 * @pmbox: Pointer to mailbox object.
6744 * @flag: Flag indicating how the mailbox need to be processed.
6745 *
6746 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006747 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6748 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006749 * The mailbox command can be submitted in polling mode, in which case
6750 * this function will wait in a polling loop for the completion of the
6751 * mailbox.
6752 * If the mailbox is submitted in no_wait mode (not polling) the
6753 * function will submit the command and returns immediately without waiting
6754 * for the mailbox completion. The no_wait is supported only when HBA
6755 * is in SLI2/SLI3 mode - interrupts are enabled.
6756 * The SLI interface allows only one mailbox pending at a time. If the
6757 * mailbox is issued in polling mode and there is already a mailbox
6758 * pending, then the function will return an error. If the mailbox is issued
6759 * in NO_WAIT mode and there is a mailbox pending already, the function
6760 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6761 * The sli layer owns the mailbox object until the completion of mailbox
6762 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6763 * return codes the caller owns the mailbox command after the return of
6764 * the function.
6765 **/
James Smart3772a992009-05-22 14:50:54 -04006766static int
6767lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6768 uint32_t flag)
dea31012005-04-17 16:05:31 -05006769{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006770 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006771 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006772 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006773 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006774 int i;
James Smart09372822008-01-11 01:52:54 -05006775 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006776 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006777 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006778 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006779 int processing_queue = 0;
6780
6781 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6782 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006783 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006784 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006785 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6786 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6787 return MBX_SUCCESS;
6788 }
James Smart58da1ff2008-04-07 10:15:56 -04006789 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006790 pmbox = lpfc_mbox_get(phba);
6791 if (!pmbox) {
6792 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6793 return MBX_SUCCESS;
6794 }
6795 }
dea31012005-04-17 16:05:31 -05006796
James Smarted957682007-06-17 19:56:37 -05006797 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006798 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006799 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006800 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006801 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006802 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006803 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006804 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006805 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006806 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006807 }
6808 }
6809
Linas Vepstas8d63f372007-02-14 14:28:36 -06006810 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006811 if (unlikely(pci_channel_offline(phba->pcidev))) {
6812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6813 goto out_not_finished;
6814 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006815
James Smarta257bf92009-04-06 18:48:10 -04006816 /* If HBA has a deferred error attention, fail the iocb. */
6817 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6818 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6819 goto out_not_finished;
6820 }
6821
dea31012005-04-17 16:05:31 -05006822 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006823
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006824 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006825 status = MBX_SUCCESS;
6826
James Smart2e0fef82007-06-17 19:56:36 -05006827 if (phba->link_state == LPFC_HBA_ERROR) {
6828 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006829
6830 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006831 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6832 "(%d):0311 Mailbox command x%x cannot "
6833 "issue Data: x%x x%x\n",
6834 pmbox->vport ? pmbox->vport->vpi : 0,
6835 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006836 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006837 }
6838
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006839 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05006840 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6841 !(hc_copy & HC_MBINT_ENA)) {
6842 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6843 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006844 "(%d):2528 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 Smart9940b972011-03-11 16:06:12 -05006848 goto out_not_finished;
6849 }
James Smart92908312006-03-07 15:04:13 -05006850 }
6851
dea31012005-04-17 16:05:31 -05006852 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6853 /* Polling for a mbox command when another one is already active
6854 * is not allowed in SLI. Also, the driver must have established
6855 * SLI2 mode to queue and process multiple mbox commands.
6856 */
6857
6858 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006859 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006860
6861 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006862 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6863 "(%d):2529 Mailbox command x%x "
6864 "cannot issue Data: x%x x%x\n",
6865 pmbox->vport ? pmbox->vport->vpi : 0,
6866 pmbox->u.mb.mbxCommand,
6867 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006868 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006869 }
6870
James Smart3772a992009-05-22 14:50:54 -04006871 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006872 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006873 /* 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):2530 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
dea31012005-04-17 16:05:31 -05006883 /* Another mailbox command is still being processed, queue this
6884 * command to be processed later.
6885 */
6886 lpfc_mbox_put(phba, pmbox);
6887
6888 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006889 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006890 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006891 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006892 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006893 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006894 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006895
6896 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006897 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006898
James Smart858c9f62007-06-17 19:56:39 -05006899 if (pmbox->vport) {
6900 lpfc_debugfs_disc_trc(pmbox->vport,
6901 LPFC_DISC_TRC_MBOX_VPORT,
6902 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006903 (uint32_t)mbx->mbxCommand,
6904 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006905 }
6906 else {
6907 lpfc_debugfs_disc_trc(phba->pport,
6908 LPFC_DISC_TRC_MBOX,
6909 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006910 (uint32_t)mbx->mbxCommand,
6911 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006912 }
6913
James Smart2e0fef82007-06-17 19:56:36 -05006914 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006915 }
6916
dea31012005-04-17 16:05:31 -05006917 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6918
6919 /* If we are not polling, we MUST be in SLI2 mode */
6920 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006921 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006922 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006923 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006924 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006925 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006926 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6927 "(%d):2531 Mailbox command x%x "
6928 "cannot issue Data: x%x x%x\n",
6929 pmbox->vport ? pmbox->vport->vpi : 0,
6930 pmbox->u.mb.mbxCommand,
6931 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006932 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006933 }
6934 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04006935 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6936 1000);
6937 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05006938 }
6939
6940 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006941 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006942 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006943 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006944 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006945 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006946 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006947
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006948 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05006949 if (pmbox->vport) {
6950 lpfc_debugfs_disc_trc(pmbox->vport,
6951 LPFC_DISC_TRC_MBOX_VPORT,
6952 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006953 (uint32_t)mbx->mbxCommand,
6954 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006955 }
6956 else {
6957 lpfc_debugfs_disc_trc(phba->pport,
6958 LPFC_DISC_TRC_MBOX,
6959 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006960 (uint32_t)mbx->mbxCommand,
6961 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006962 }
6963 }
6964
dea31012005-04-17 16:05:31 -05006965 psli->slistat.mbox_cmd++;
6966 evtctr = psli->slistat.mbox_event;
6967
6968 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006969 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05006970
James Smart3772a992009-05-22 14:50:54 -04006971 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006972 /* Populate mbox extension offset word. */
6973 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006974 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04006975 = (uint8_t *)phba->mbox_ext
6976 - (uint8_t *)phba->mbox;
6977 }
6978
6979 /* Copy the mailbox extension data */
6980 if (pmbox->in_ext_byte_len && pmbox->context2) {
6981 lpfc_sli_pcimem_bcopy(pmbox->context2,
6982 (uint8_t *)phba->mbox_ext,
6983 pmbox->in_ext_byte_len);
6984 }
6985 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006986 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006987 } else {
James Smart7a470272010-03-15 11:25:20 -04006988 /* Populate mbox extension offset word. */
6989 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006990 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04006991 = MAILBOX_HBA_EXT_OFFSET;
6992
6993 /* Copy the mailbox extension data */
6994 if (pmbox->in_ext_byte_len && pmbox->context2) {
6995 lpfc_memcpy_to_slim(phba->MBslimaddr +
6996 MAILBOX_HBA_EXT_OFFSET,
6997 pmbox->context2, pmbox->in_ext_byte_len);
6998
6999 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007000 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007001 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007002 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007003 }
7004
7005 /* First copy mbox command data to HBA SLIM, skip past first
7006 word */
7007 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007008 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007009 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7010
7011 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007012 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007013 to_slim = phba->MBslimaddr;
7014 writel(ldata, to_slim);
7015 readl(to_slim); /* flush */
7016
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007017 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007018 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007019 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007020 }
7021 }
7022
7023 wmb();
dea31012005-04-17 16:05:31 -05007024
7025 switch (flag) {
7026 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007027 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007028 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007029 /* Interrupt board to do it */
7030 writel(CA_MBATT, phba->CAregaddr);
7031 readl(phba->CAregaddr); /* flush */
7032 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007033 break;
7034
7035 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007036 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007037 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007038 /* Interrupt board to do it */
7039 writel(CA_MBATT, phba->CAregaddr);
7040 readl(phba->CAregaddr); /* flush */
7041
James Smart3772a992009-05-22 14:50:54 -04007042 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007043 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007044 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007045 word0 = le32_to_cpu(word0);
7046 } else {
7047 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007048 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7049 spin_unlock_irqrestore(&phba->hbalock,
7050 drvr_flag);
7051 goto out_not_finished;
7052 }
dea31012005-04-17 16:05:31 -05007053 }
7054
7055 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007056 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7057 spin_unlock_irqrestore(&phba->hbalock,
7058 drvr_flag);
7059 goto out_not_finished;
7060 }
James Smarta183a152011-10-10 21:32:43 -04007061 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7062 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007063 i = 0;
dea31012005-04-17 16:05:31 -05007064 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007065 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7066 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007067 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007068 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007069 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007070 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007071 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007072 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007073 }
7074
7075 /* Check if we took a mbox interrupt while we were
7076 polling */
7077 if (((word0 & OWN_CHIP) != OWN_CHIP)
7078 && (evtctr != psli->slistat.mbox_event))
7079 break;
7080
James Smart09372822008-01-11 01:52:54 -05007081 if (i++ > 10) {
7082 spin_unlock_irqrestore(&phba->hbalock,
7083 drvr_flag);
7084 msleep(1);
7085 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7086 }
dea31012005-04-17 16:05:31 -05007087
James Smart3772a992009-05-22 14:50:54 -04007088 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007089 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007090 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007091 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007092 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007093 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007094 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007095 /* Check real SLIM for any errors */
7096 slimword0 = readl(phba->MBslimaddr);
7097 slimmb = (MAILBOX_t *) & slimword0;
7098 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7099 && slimmb->mbxStatus) {
7100 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007101 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007102 word0 = slimword0;
7103 }
7104 }
7105 } else {
7106 /* First copy command data */
7107 word0 = readl(phba->MBslimaddr);
7108 }
7109 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007110 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7111 spin_unlock_irqrestore(&phba->hbalock,
7112 drvr_flag);
7113 goto out_not_finished;
7114 }
dea31012005-04-17 16:05:31 -05007115 }
7116
James Smart3772a992009-05-22 14:50:54 -04007117 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007118 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007119 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007120 /* Copy the mailbox extension data */
7121 if (pmbox->out_ext_byte_len && pmbox->context2) {
7122 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7123 pmbox->context2,
7124 pmbox->out_ext_byte_len);
7125 }
dea31012005-04-17 16:05:31 -05007126 } else {
7127 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007128 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007129 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007130 /* Copy the mailbox extension data */
7131 if (pmbox->out_ext_byte_len && pmbox->context2) {
7132 lpfc_memcpy_from_slim(pmbox->context2,
7133 phba->MBslimaddr +
7134 MAILBOX_HBA_EXT_OFFSET,
7135 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007136 }
7137 }
7138
7139 writel(HA_MBATT, phba->HAregaddr);
7140 readl(phba->HAregaddr); /* flush */
7141
7142 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007143 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007144 }
7145
James Smart2e0fef82007-06-17 19:56:36 -05007146 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7147 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007148
7149out_not_finished:
7150 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007151 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007152 lpfc_mbox_cmpl_put(phba, pmbox);
7153 }
7154 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007155}
7156
James Smarte59058c2008-08-24 21:49:00 -04007157/**
James Smartf1126682009-06-10 17:22:44 -04007158 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7159 * @phba: Pointer to HBA context object.
7160 *
7161 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7162 * the driver internal pending mailbox queue. It will then try to wait out the
7163 * possible outstanding mailbox command before return.
7164 *
7165 * Returns:
7166 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7167 * the outstanding mailbox command timed out.
7168 **/
7169static int
7170lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7171{
7172 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007173 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007174 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007175
7176 /* Mark the asynchronous mailbox command posting as blocked */
7177 spin_lock_irq(&phba->hbalock);
7178 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007179 /* Determine how long we might wait for the active mailbox
7180 * command to be gracefully completed by firmware.
7181 */
James Smarta183a152011-10-10 21:32:43 -04007182 if (phba->sli.mbox_active)
7183 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7184 phba->sli.mbox_active) *
7185 1000) + jiffies;
7186 spin_unlock_irq(&phba->hbalock);
7187
James Smarte8d3c3b2013-10-10 12:21:30 -04007188 /* Make sure the mailbox is really active */
7189 if (timeout)
7190 lpfc_sli4_process_missed_mbox_completions(phba);
7191
James Smartf1126682009-06-10 17:22:44 -04007192 /* Wait for the outstnading mailbox command to complete */
7193 while (phba->sli.mbox_active) {
7194 /* Check active mailbox complete status every 2ms */
7195 msleep(2);
7196 if (time_after(jiffies, timeout)) {
7197 /* Timeout, marked the outstanding cmd not complete */
7198 rc = 1;
7199 break;
7200 }
7201 }
7202
7203 /* Can not cleanly block async mailbox command, fails it */
7204 if (rc) {
7205 spin_lock_irq(&phba->hbalock);
7206 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7207 spin_unlock_irq(&phba->hbalock);
7208 }
7209 return rc;
7210}
7211
7212/**
7213 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7214 * @phba: Pointer to HBA context object.
7215 *
7216 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7217 * commands from the driver internal pending mailbox queue. It makes sure
7218 * that there is no outstanding mailbox command before resuming posting
7219 * asynchronous mailbox commands. If, for any reason, there is outstanding
7220 * mailbox command, it will try to wait it out before resuming asynchronous
7221 * mailbox command posting.
7222 **/
7223static void
7224lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7225{
7226 struct lpfc_sli *psli = &phba->sli;
7227
7228 spin_lock_irq(&phba->hbalock);
7229 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7230 /* Asynchronous mailbox posting is not blocked, do nothing */
7231 spin_unlock_irq(&phba->hbalock);
7232 return;
7233 }
7234
7235 /* Outstanding synchronous mailbox command is guaranteed to be done,
7236 * successful or timeout, after timing-out the outstanding mailbox
7237 * command shall always be removed, so just unblock posting async
7238 * mailbox command and resume
7239 */
7240 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7241 spin_unlock_irq(&phba->hbalock);
7242
7243 /* wake up worker thread to post asynchronlous mailbox command */
7244 lpfc_worker_wake_up(phba);
7245}
7246
7247/**
James Smart2d843ed2012-09-29 11:29:06 -04007248 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7249 * @phba: Pointer to HBA context object.
7250 * @mboxq: Pointer to mailbox object.
7251 *
7252 * The function waits for the bootstrap mailbox register ready bit from
7253 * port for twice the regular mailbox command timeout value.
7254 *
7255 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7256 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7257 **/
7258static int
7259lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7260{
7261 uint32_t db_ready;
7262 unsigned long timeout;
7263 struct lpfc_register bmbx_reg;
7264
7265 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7266 * 1000) + jiffies;
7267
7268 do {
7269 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7270 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7271 if (!db_ready)
7272 msleep(2);
7273
7274 if (time_after(jiffies, timeout))
7275 return MBXERR_ERROR;
7276 } while (!db_ready);
7277
7278 return 0;
7279}
7280
7281/**
James Smartda0436e2009-05-22 14:51:39 -04007282 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7283 * @phba: Pointer to HBA context object.
7284 * @mboxq: Pointer to mailbox object.
7285 *
7286 * The function posts a mailbox to the port. The mailbox is expected
7287 * to be comletely filled in and ready for the port to operate on it.
7288 * This routine executes a synchronous completion operation on the
7289 * mailbox by polling for its completion.
7290 *
7291 * The caller must not be holding any locks when calling this routine.
7292 *
7293 * Returns:
7294 * MBX_SUCCESS - mailbox posted successfully
7295 * Any of the MBX error values.
7296 **/
7297static int
7298lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7299{
7300 int rc = MBX_SUCCESS;
7301 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007302 uint32_t mcqe_status;
7303 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007304 struct lpfc_sli *psli = &phba->sli;
7305 struct lpfc_mqe *mb = &mboxq->u.mqe;
7306 struct lpfc_bmbx_create *mbox_rgn;
7307 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007308
7309 /*
7310 * Only one mailbox can be active to the bootstrap mailbox region
7311 * at a time and there is no queueing provided.
7312 */
7313 spin_lock_irqsave(&phba->hbalock, iflag);
7314 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7315 spin_unlock_irqrestore(&phba->hbalock, iflag);
7316 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007317 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007318 "cannot issue Data: x%x x%x\n",
7319 mboxq->vport ? mboxq->vport->vpi : 0,
7320 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007321 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7322 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007323 psli->sli_flag, MBX_POLL);
7324 return MBXERR_ERROR;
7325 }
7326 /* The server grabs the token and owns it until release */
7327 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7328 phba->sli.mbox_active = mboxq;
7329 spin_unlock_irqrestore(&phba->hbalock, iflag);
7330
James Smart2d843ed2012-09-29 11:29:06 -04007331 /* wait for bootstrap mbox register for readyness */
7332 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7333 if (rc)
7334 goto exit;
7335
James Smartda0436e2009-05-22 14:51:39 -04007336 /*
7337 * Initialize the bootstrap memory region to avoid stale data areas
7338 * in the mailbox post. Then copy the caller's mailbox contents to
7339 * the bmbx mailbox region.
7340 */
7341 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7342 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7343 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7344 sizeof(struct lpfc_mqe));
7345
7346 /* Post the high mailbox dma address to the port and wait for ready. */
7347 dma_address = &phba->sli4_hba.bmbx.dma_address;
7348 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7349
James Smart2d843ed2012-09-29 11:29:06 -04007350 /* wait for bootstrap mbox register for hi-address write done */
7351 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7352 if (rc)
7353 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007354
7355 /* Post the low mailbox dma address to the port. */
7356 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007357
James Smart2d843ed2012-09-29 11:29:06 -04007358 /* wait for bootstrap mbox register for low address write done */
7359 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7360 if (rc)
7361 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007362
7363 /*
7364 * Read the CQ to ensure the mailbox has completed.
7365 * If so, update the mailbox status so that the upper layers
7366 * can complete the request normally.
7367 */
7368 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7369 sizeof(struct lpfc_mqe));
7370 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7371 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7372 sizeof(struct lpfc_mcqe));
7373 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007374 /*
7375 * When the CQE status indicates a failure and the mailbox status
7376 * indicates success then copy the CQE status into the mailbox status
7377 * (and prefix it with x4000).
7378 */
James Smartda0436e2009-05-22 14:51:39 -04007379 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007380 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7381 bf_set(lpfc_mqe_status, mb,
7382 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007383 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007384 } else
7385 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007386
7387 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007388 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007389 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7390 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007391 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7392 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7393 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007394 bf_get(lpfc_mqe_status, mb),
7395 mb->un.mb_words[0], mb->un.mb_words[1],
7396 mb->un.mb_words[2], mb->un.mb_words[3],
7397 mb->un.mb_words[4], mb->un.mb_words[5],
7398 mb->un.mb_words[6], mb->un.mb_words[7],
7399 mb->un.mb_words[8], mb->un.mb_words[9],
7400 mb->un.mb_words[10], mb->un.mb_words[11],
7401 mb->un.mb_words[12], mboxq->mcqe.word0,
7402 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7403 mboxq->mcqe.trailer);
7404exit:
7405 /* We are holding the token, no needed for lock when release */
7406 spin_lock_irqsave(&phba->hbalock, iflag);
7407 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7408 phba->sli.mbox_active = NULL;
7409 spin_unlock_irqrestore(&phba->hbalock, iflag);
7410 return rc;
7411}
7412
7413/**
7414 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7415 * @phba: Pointer to HBA context object.
7416 * @pmbox: Pointer to mailbox object.
7417 * @flag: Flag indicating how the mailbox need to be processed.
7418 *
7419 * This function is called by discovery code and HBA management code to submit
7420 * a mailbox command to firmware with SLI-4 interface spec.
7421 *
7422 * Return codes the caller owns the mailbox command after the return of the
7423 * function.
7424 **/
7425static int
7426lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7427 uint32_t flag)
7428{
7429 struct lpfc_sli *psli = &phba->sli;
7430 unsigned long iflags;
7431 int rc;
7432
James Smartb76f2dc2011-07-22 18:37:42 -04007433 /* dump from issue mailbox command if setup */
7434 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7435
James Smart8fa38512009-07-19 10:01:03 -04007436 rc = lpfc_mbox_dev_check(phba);
7437 if (unlikely(rc)) {
7438 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007439 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007440 "cannot issue Data: x%x x%x\n",
7441 mboxq->vport ? mboxq->vport->vpi : 0,
7442 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007443 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7444 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007445 psli->sli_flag, flag);
7446 goto out_not_finished;
7447 }
7448
James Smartda0436e2009-05-22 14:51:39 -04007449 /* Detect polling mode and jump to a handler */
7450 if (!phba->sli4_hba.intr_enable) {
7451 if (flag == MBX_POLL)
7452 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7453 else
7454 rc = -EIO;
7455 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007456 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007457 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007458 "(x%x/x%x) failure: "
7459 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7460 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007461 mboxq->vport ? mboxq->vport->vpi : 0,
7462 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007463 lpfc_sli_config_mbox_subsys_get(phba,
7464 mboxq),
7465 lpfc_sli_config_mbox_opcode_get(phba,
7466 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007467 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7468 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7469 bf_get(lpfc_mcqe_ext_status,
7470 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007471 psli->sli_flag, flag);
7472 return rc;
7473 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007474 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7475 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007476 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007477 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007478 mboxq->vport ? mboxq->vport->vpi : 0,
7479 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007480 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7481 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007482 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007483 /* Try to block the asynchronous mailbox posting */
7484 rc = lpfc_sli4_async_mbox_block(phba);
7485 if (!rc) {
7486 /* Successfully blocked, now issue sync mbox cmd */
7487 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7488 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007489 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007490 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007491 "(%d):2597 Sync Mailbox command "
7492 "x%x (x%x/x%x) failure: "
7493 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7494 "Data: x%x x%x\n,",
7495 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007496 mboxq->u.mb.mbxCommand,
7497 lpfc_sli_config_mbox_subsys_get(phba,
7498 mboxq),
7499 lpfc_sli_config_mbox_opcode_get(phba,
7500 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007501 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7502 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7503 bf_get(lpfc_mcqe_ext_status,
7504 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007505 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007506 /* Unblock the async mailbox posting afterward */
7507 lpfc_sli4_async_mbox_unblock(phba);
7508 }
7509 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007510 }
7511
7512 /* Now, interrupt mode asynchrous mailbox command */
7513 rc = lpfc_mbox_cmd_check(phba, mboxq);
7514 if (rc) {
7515 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007516 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007517 "cannot issue Data: x%x x%x\n",
7518 mboxq->vport ? mboxq->vport->vpi : 0,
7519 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007520 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7521 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007522 psli->sli_flag, flag);
7523 goto out_not_finished;
7524 }
James Smartda0436e2009-05-22 14:51:39 -04007525
7526 /* Put the mailbox command to the driver internal FIFO */
7527 psli->slistat.mbox_busy++;
7528 spin_lock_irqsave(&phba->hbalock, iflags);
7529 lpfc_mbox_put(phba, mboxq);
7530 spin_unlock_irqrestore(&phba->hbalock, iflags);
7531 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7532 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007533 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007534 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7535 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007536 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7537 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007538 phba->pport->port_state,
7539 psli->sli_flag, MBX_NOWAIT);
7540 /* Wake up worker thread to transport mailbox command from head */
7541 lpfc_worker_wake_up(phba);
7542
7543 return MBX_BUSY;
7544
7545out_not_finished:
7546 return MBX_NOT_FINISHED;
7547}
7548
7549/**
7550 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7551 * @phba: Pointer to HBA context object.
7552 *
7553 * This function is called by worker thread to send a mailbox command to
7554 * SLI4 HBA firmware.
7555 *
7556 **/
7557int
7558lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7559{
7560 struct lpfc_sli *psli = &phba->sli;
7561 LPFC_MBOXQ_t *mboxq;
7562 int rc = MBX_SUCCESS;
7563 unsigned long iflags;
7564 struct lpfc_mqe *mqe;
7565 uint32_t mbx_cmnd;
7566
7567 /* Check interrupt mode before post async mailbox command */
7568 if (unlikely(!phba->sli4_hba.intr_enable))
7569 return MBX_NOT_FINISHED;
7570
7571 /* Check for mailbox command service token */
7572 spin_lock_irqsave(&phba->hbalock, iflags);
7573 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7574 spin_unlock_irqrestore(&phba->hbalock, iflags);
7575 return MBX_NOT_FINISHED;
7576 }
7577 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7578 spin_unlock_irqrestore(&phba->hbalock, iflags);
7579 return MBX_NOT_FINISHED;
7580 }
7581 if (unlikely(phba->sli.mbox_active)) {
7582 spin_unlock_irqrestore(&phba->hbalock, iflags);
7583 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7584 "0384 There is pending active mailbox cmd\n");
7585 return MBX_NOT_FINISHED;
7586 }
7587 /* Take the mailbox command service token */
7588 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7589
7590 /* Get the next mailbox command from head of queue */
7591 mboxq = lpfc_mbox_get(phba);
7592
7593 /* If no more mailbox command waiting for post, we're done */
7594 if (!mboxq) {
7595 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7596 spin_unlock_irqrestore(&phba->hbalock, iflags);
7597 return MBX_SUCCESS;
7598 }
7599 phba->sli.mbox_active = mboxq;
7600 spin_unlock_irqrestore(&phba->hbalock, iflags);
7601
7602 /* Check device readiness for posting mailbox command */
7603 rc = lpfc_mbox_dev_check(phba);
7604 if (unlikely(rc))
7605 /* Driver clean routine will clean up pending mailbox */
7606 goto out_not_finished;
7607
7608 /* Prepare the mbox command to be posted */
7609 mqe = &mboxq->u.mqe;
7610 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7611
7612 /* Start timer for the mbox_tmo and log some mailbox post messages */
7613 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007614 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007615
7616 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007617 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007618 "x%x x%x\n",
7619 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007620 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7621 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007622 phba->pport->port_state, psli->sli_flag);
7623
7624 if (mbx_cmnd != MBX_HEARTBEAT) {
7625 if (mboxq->vport) {
7626 lpfc_debugfs_disc_trc(mboxq->vport,
7627 LPFC_DISC_TRC_MBOX_VPORT,
7628 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7629 mbx_cmnd, mqe->un.mb_words[0],
7630 mqe->un.mb_words[1]);
7631 } else {
7632 lpfc_debugfs_disc_trc(phba->pport,
7633 LPFC_DISC_TRC_MBOX,
7634 "MBOX Send: cmd:x%x mb:x%x x%x",
7635 mbx_cmnd, mqe->un.mb_words[0],
7636 mqe->un.mb_words[1]);
7637 }
7638 }
7639 psli->slistat.mbox_cmd++;
7640
7641 /* Post the mailbox command to the port */
7642 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7643 if (rc != MBX_SUCCESS) {
7644 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007645 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007646 "cannot issue Data: x%x x%x\n",
7647 mboxq->vport ? mboxq->vport->vpi : 0,
7648 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007649 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7650 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007651 psli->sli_flag, MBX_NOWAIT);
7652 goto out_not_finished;
7653 }
7654
7655 return rc;
7656
7657out_not_finished:
7658 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007659 if (phba->sli.mbox_active) {
7660 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7661 __lpfc_mbox_cmpl_put(phba, mboxq);
7662 /* Release the token */
7663 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7664 phba->sli.mbox_active = NULL;
7665 }
James Smartda0436e2009-05-22 14:51:39 -04007666 spin_unlock_irqrestore(&phba->hbalock, iflags);
7667
7668 return MBX_NOT_FINISHED;
7669}
7670
7671/**
7672 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7673 * @phba: Pointer to HBA context object.
7674 * @pmbox: Pointer to mailbox object.
7675 * @flag: Flag indicating how the mailbox need to be processed.
7676 *
7677 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7678 * the API jump table function pointer from the lpfc_hba struct.
7679 *
7680 * Return codes the caller owns the mailbox command after the return of the
7681 * function.
7682 **/
7683int
7684lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7685{
7686 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7687}
7688
7689/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007690 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007691 * @phba: The hba struct for which this call is being executed.
7692 * @dev_grp: The HBA PCI-Device group number.
7693 *
7694 * This routine sets up the mbox interface API function jump table in @phba
7695 * struct.
7696 * Returns: 0 - success, -ENODEV - failure.
7697 **/
7698int
7699lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7700{
7701
7702 switch (dev_grp) {
7703 case LPFC_PCI_DEV_LP:
7704 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7705 phba->lpfc_sli_handle_slow_ring_event =
7706 lpfc_sli_handle_slow_ring_event_s3;
7707 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7708 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7709 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7710 break;
7711 case LPFC_PCI_DEV_OC:
7712 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7713 phba->lpfc_sli_handle_slow_ring_event =
7714 lpfc_sli_handle_slow_ring_event_s4;
7715 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7716 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7717 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7718 break;
7719 default:
7720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7721 "1420 Invalid HBA PCI-device group: 0x%x\n",
7722 dev_grp);
7723 return -ENODEV;
7724 break;
7725 }
7726 return 0;
7727}
7728
7729/**
James Smart3621a712009-04-06 18:47:14 -04007730 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007731 * @phba: Pointer to HBA context object.
7732 * @pring: Pointer to driver SLI ring object.
7733 * @piocb: Pointer to address of newly added command iocb.
7734 *
7735 * This function is called with hbalock held to add a command
7736 * iocb to the txq when SLI layer cannot submit the command iocb
7737 * to the ring.
7738 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007739void
James Smart92d7f7b2007-06-17 19:56:38 -05007740__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007741 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007742{
7743 /* Insert the caller's iocb in the txq tail for later processing. */
7744 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007745}
7746
James Smarte59058c2008-08-24 21:49:00 -04007747/**
James Smart3621a712009-04-06 18:47:14 -04007748 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007749 * @phba: Pointer to HBA context object.
7750 * @pring: Pointer to driver SLI ring object.
7751 * @piocb: Pointer to address of newly added command iocb.
7752 *
7753 * This function is called with hbalock held before a new
7754 * iocb is submitted to the firmware. This function checks
7755 * txq to flush the iocbs in txq to Firmware before
7756 * submitting new iocbs to the Firmware.
7757 * If there are iocbs in the txq which need to be submitted
7758 * to firmware, lpfc_sli_next_iocb returns the first element
7759 * of the txq after dequeuing it from txq.
7760 * If there is no iocb in the txq then the function will return
7761 * *piocb and *piocb is set to NULL. Caller needs to check
7762 * *piocb to find if there are more commands in the txq.
7763 **/
dea31012005-04-17 16:05:31 -05007764static struct lpfc_iocbq *
7765lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007766 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007767{
7768 struct lpfc_iocbq * nextiocb;
7769
7770 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7771 if (!nextiocb) {
7772 nextiocb = *piocb;
7773 *piocb = NULL;
7774 }
7775
7776 return nextiocb;
7777}
7778
James Smarte59058c2008-08-24 21:49:00 -04007779/**
James Smart3772a992009-05-22 14:50:54 -04007780 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007781 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007782 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007783 * @piocb: Pointer to command iocb.
7784 * @flag: Flag indicating if this command can be put into txq.
7785 *
James Smart3772a992009-05-22 14:50:54 -04007786 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7787 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7788 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7789 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7790 * this function allows only iocbs for posting buffers. This function finds
7791 * next available slot in the command ring and posts the command to the
7792 * available slot and writes the port attention register to request HBA start
7793 * processing new iocb. If there is no slot available in the ring and
7794 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7795 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007796 *
James Smart3772a992009-05-22 14:50:54 -04007797 * This function is called with hbalock held. The function will return success
7798 * after it successfully submit the iocb to firmware or after adding to the
7799 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007800 **/
James Smart98c9ea52007-10-27 13:37:33 -04007801static int
James Smart3772a992009-05-22 14:50:54 -04007802__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007803 struct lpfc_iocbq *piocb, uint32_t flag)
7804{
7805 struct lpfc_iocbq *nextiocb;
7806 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007807 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007808
James Smart92d7f7b2007-06-17 19:56:38 -05007809 if (piocb->iocb_cmpl && (!piocb->vport) &&
7810 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7811 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7812 lpfc_printf_log(phba, KERN_ERR,
7813 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007814 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007815 piocb->iocb.ulpCommand);
7816 dump_stack();
7817 return IOCB_ERROR;
7818 }
7819
7820
Linas Vepstas8d63f372007-02-14 14:28:36 -06007821 /* If the PCI channel is in offline state, do not post iocbs. */
7822 if (unlikely(pci_channel_offline(phba->pcidev)))
7823 return IOCB_ERROR;
7824
James Smarta257bf92009-04-06 18:48:10 -04007825 /* If HBA has a deferred error attention, fail the iocb. */
7826 if (unlikely(phba->hba_flag & DEFER_ERATT))
7827 return IOCB_ERROR;
7828
dea31012005-04-17 16:05:31 -05007829 /*
7830 * We should never get an IOCB if we are in a < LINK_DOWN state
7831 */
James Smart2e0fef82007-06-17 19:56:36 -05007832 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007833 return IOCB_ERROR;
7834
7835 /*
7836 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007837 * outstanding event.
dea31012005-04-17 16:05:31 -05007838 */
James Smart0b727fe2007-10-27 13:37:25 -04007839 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007840 goto iocb_busy;
7841
James Smart2e0fef82007-06-17 19:56:36 -05007842 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007843 /*
James Smart2680eea2007-04-25 09:52:55 -04007844 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007845 * can be issued if the link is not up.
7846 */
7847 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007848 case CMD_GEN_REQUEST64_CR:
7849 case CMD_GEN_REQUEST64_CX:
7850 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7851 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007852 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007853 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7854 MENLO_TRANSPORT_TYPE))
7855
7856 goto iocb_busy;
7857 break;
dea31012005-04-17 16:05:31 -05007858 case CMD_QUE_RING_BUF_CN:
7859 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007860 /*
7861 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7862 * completion, iocb_cmpl MUST be 0.
7863 */
7864 if (piocb->iocb_cmpl)
7865 piocb->iocb_cmpl = NULL;
7866 /*FALLTHROUGH*/
7867 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007868 case CMD_CLOSE_XRI_CN:
7869 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007870 break;
7871 default:
7872 goto iocb_busy;
7873 }
7874
7875 /*
7876 * For FCP commands, we must be in a state where we can process link
7877 * attention events.
7878 */
7879 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007880 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007881 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007882 }
dea31012005-04-17 16:05:31 -05007883
dea31012005-04-17 16:05:31 -05007884 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7885 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7886 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7887
7888 if (iocb)
7889 lpfc_sli_update_ring(phba, pring);
7890 else
7891 lpfc_sli_update_full_ring(phba, pring);
7892
7893 if (!piocb)
7894 return IOCB_SUCCESS;
7895
7896 goto out_busy;
7897
7898 iocb_busy:
7899 pring->stats.iocb_cmd_delay++;
7900
7901 out_busy:
7902
7903 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007904 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007905 return IOCB_SUCCESS;
7906 }
7907
7908 return IOCB_BUSY;
7909}
7910
James Smart3772a992009-05-22 14:50:54 -04007911/**
James Smart4f774512009-05-22 14:52:35 -04007912 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7913 * @phba: Pointer to HBA context object.
7914 * @piocb: Pointer to command iocb.
7915 * @sglq: Pointer to the scatter gather queue object.
7916 *
7917 * This routine converts the bpl or bde that is in the IOCB
7918 * to a sgl list for the sli4 hardware. The physical address
7919 * of the bpl/bde is converted back to a virtual address.
7920 * If the IOCB contains a BPL then the list of BDE's is
7921 * converted to sli4_sge's. If the IOCB contains a single
7922 * BDE then it is converted to a single sli_sge.
7923 * The IOCB is still in cpu endianess so the contents of
7924 * the bpl can be used without byte swapping.
7925 *
7926 * Returns valid XRI = Success, NO_XRI = Failure.
7927**/
7928static uint16_t
7929lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7930 struct lpfc_sglq *sglq)
7931{
7932 uint16_t xritag = NO_XRI;
7933 struct ulp_bde64 *bpl = NULL;
7934 struct ulp_bde64 bde;
7935 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007936 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007937 IOCB_t *icmd;
7938 int numBdes = 0;
7939 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007940 uint32_t offset = 0; /* accumulated offset in the sg request list */
7941 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007942
7943 if (!piocbq || !sglq)
7944 return xritag;
7945
7946 sgl = (struct sli4_sge *)sglq->sgl;
7947 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007948 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7949 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007950 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7951 numBdes = icmd->un.genreq64.bdl.bdeSize /
7952 sizeof(struct ulp_bde64);
7953 /* The addrHigh and addrLow fields within the IOCB
7954 * have not been byteswapped yet so there is no
7955 * need to swap them back.
7956 */
James Smart1b511972011-12-13 13:23:09 -05007957 if (piocbq->context3)
7958 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7959 else
7960 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007961
James Smart1b511972011-12-13 13:23:09 -05007962 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007963 if (!bpl)
7964 return xritag;
7965
7966 for (i = 0; i < numBdes; i++) {
7967 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007968 sgl->addr_hi = bpl->addrHigh;
7969 sgl->addr_lo = bpl->addrLow;
7970
James Smart05580562011-05-24 11:40:48 -04007971 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007972 if ((i+1) == numBdes)
7973 bf_set(lpfc_sli4_sge_last, sgl, 1);
7974 else
7975 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007976 /* swap the size field back to the cpu so we
7977 * can assign it to the sgl.
7978 */
7979 bde.tus.w = le32_to_cpu(bpl->tus.w);
7980 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007981 /* The offsets in the sgl need to be accumulated
7982 * separately for the request and reply lists.
7983 * The request is always first, the reply follows.
7984 */
7985 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7986 /* add up the reply sg entries */
7987 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7988 inbound++;
7989 /* first inbound? reset the offset */
7990 if (inbound == 1)
7991 offset = 0;
7992 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007993 bf_set(lpfc_sli4_sge_type, sgl,
7994 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007995 offset += bde.tus.f.bdeSize;
7996 }
James Smart546fc852011-03-11 16:06:29 -05007997 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007998 bpl++;
7999 sgl++;
8000 }
8001 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8002 /* The addrHigh and addrLow fields of the BDE have not
8003 * been byteswapped yet so they need to be swapped
8004 * before putting them in the sgl.
8005 */
8006 sgl->addr_hi =
8007 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8008 sgl->addr_lo =
8009 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008010 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008011 bf_set(lpfc_sli4_sge_last, sgl, 1);
8012 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008013 sgl->sge_len =
8014 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008015 }
8016 return sglq->sli4_xritag;
8017}
8018
8019/**
8020 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
8021 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04008022 *
James Smarta93ff372010-10-22 11:06:08 -04008023 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04008024 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
8025 * held.
James Smart4f774512009-05-22 14:52:35 -04008026 *
8027 * Return: index into SLI4 fast-path FCP queue index.
8028 **/
James Smart2a76a282012-08-03 12:35:54 -04008029static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04008030lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04008031{
James Smart7bb03bb2013-04-17 20:19:16 -04008032 struct lpfc_vector_map_info *cpup;
8033 int chann, cpu;
James Smart4f774512009-05-22 14:52:35 -04008034
James Smart7bb03bb2013-04-17 20:19:16 -04008035 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU) {
8036 cpu = smp_processor_id();
8037 if (cpu < phba->sli4_hba.num_present_cpu) {
8038 cpup = phba->sli4_hba.cpu_map;
8039 cpup += cpu;
8040 return cpup->channel_id;
8041 }
8042 chann = cpu;
8043 }
8044 chann = atomic_add_return(1, &phba->fcp_qidx);
8045 chann = (chann % phba->cfg_fcp_io_channel);
8046 return chann;
James Smart4f774512009-05-22 14:52:35 -04008047}
8048
8049/**
8050 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8051 * @phba: Pointer to HBA context object.
8052 * @piocb: Pointer to command iocb.
8053 * @wqe: Pointer to the work queue entry.
8054 *
8055 * This routine converts the iocb command to its Work Queue Entry
8056 * equivalent. The wqe pointer should not have any fields set when
8057 * this routine is called because it will memcpy over them.
8058 * This routine does not set the CQ_ID or the WQEC bits in the
8059 * wqe.
8060 *
8061 * Returns: 0 = Success, IOCB_ERROR = Failure.
8062 **/
8063static int
8064lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8065 union lpfc_wqe *wqe)
8066{
James Smart5ffc2662009-11-18 15:39:44 -05008067 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008068 uint8_t ct = 0;
8069 uint32_t fip;
8070 uint32_t abort_tag;
8071 uint8_t command_type = ELS_COMMAND_NON_FIP;
8072 uint8_t cmnd;
8073 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008074 uint16_t abrt_iotag;
8075 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008076 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008077 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008078 int numBdes, i;
8079 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008080 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008081 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008082 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008083
James Smart45ed1192009-10-02 15:17:02 -04008084 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008085 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008086 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008087 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008088 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008089 command_type = ELS_COMMAND_FIP;
8090 else
8091 command_type = ELS_COMMAND_NON_FIP;
8092
James Smart4f774512009-05-22 14:52:35 -04008093 /* Some of the fields are in the right position already */
8094 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8095 abort_tag = (uint32_t) iocbq->iotag;
8096 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04008097 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04008098 /* words0-2 bpl convert bde */
8099 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008100 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8101 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008102 bpl = (struct ulp_bde64 *)
8103 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8104 if (!bpl)
8105 return IOCB_ERROR;
8106
8107 /* Should already be byte swapped. */
8108 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8109 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8110 /* swap the size field back to the cpu so we
8111 * can assign it to the sgl.
8112 */
8113 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008114 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8115 total_len = 0;
8116 for (i = 0; i < numBdes; i++) {
8117 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8118 total_len += bde.tus.f.bdeSize;
8119 }
James Smart4f774512009-05-22 14:52:35 -04008120 } else
James Smart5ffc2662009-11-18 15:39:44 -05008121 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008122
8123 iocbq->iocb.ulpIoTag = iocbq->iotag;
8124 cmnd = iocbq->iocb.ulpCommand;
8125
8126 switch (iocbq->iocb.ulpCommand) {
8127 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008128 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8129 ndlp = iocbq->context_un.ndlp;
8130 else
8131 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008132 if (!iocbq->iocb.ulpLe) {
8133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8134 "2007 Only Limited Edition cmd Format"
8135 " supported 0x%x\n",
8136 iocbq->iocb.ulpCommand);
8137 return IOCB_ERROR;
8138 }
James Smartff78d8f2011-12-13 13:21:35 -05008139
James Smart5ffc2662009-11-18 15:39:44 -05008140 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008141 /* Els_reguest64 has a TMO */
8142 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8143 iocbq->iocb.ulpTimeout);
8144 /* Need a VF for word 4 set the vf bit*/
8145 bf_set(els_req64_vf, &wqe->els_req, 0);
8146 /* And a VFID for word 12 */
8147 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008148 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008149 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8150 iocbq->iocb.ulpContext);
8151 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8152 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008153 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008154 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008155 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8156 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008157 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8158 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008159 if_type = bf_get(lpfc_sli_intf_if_type,
8160 &phba->sli4_hba.sli_intf);
8161 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008162 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008163 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008164 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008165 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008166 *pcmd == ELS_CMD_PLOGI)) {
8167 bf_set(els_req64_sp, &wqe->els_req, 1);
8168 bf_set(els_req64_sid, &wqe->els_req,
8169 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008170 if ((*pcmd == ELS_CMD_FLOGI) &&
8171 !(phba->fc_topology ==
8172 LPFC_TOPOLOGY_LOOP))
8173 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008174 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8175 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008176 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008177 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008178 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8179 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8180 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8181 }
James Smartc8685952009-11-18 15:39:16 -05008182 }
James Smart6d368e52011-05-24 11:44:12 -04008183 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8184 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008185 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8186 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8187 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8188 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8189 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8190 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008191 break;
James Smart5ffc2662009-11-18 15:39:44 -05008192 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008193 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8194 iocbq->iocb.un.ulpWord[3]);
8195 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008196 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008197 /* The entire sequence is transmitted for this IOCB */
8198 xmit_len = total_len;
8199 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008200 if (phba->link_flag & LS_LOOPBACK_MODE)
8201 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008202 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008203 /* word3 iocb=io_tag32 wqe=reserved */
8204 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008205 /* word4 relative_offset memcpy */
8206 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008207 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8208 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8209 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8210 LPFC_WQE_IOD_WRITE);
8211 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8212 LPFC_WQE_LENLOC_WORD12);
8213 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008214 wqe->xmit_sequence.xmit_len = xmit_len;
8215 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008216 break;
James Smart4f774512009-05-22 14:52:35 -04008217 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008218 /* word3 iocb=iotag32 wqe=seq_payload_len */
8219 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008220 /* word4 iocb=rsvd wqe=rsvd */
8221 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8222 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008223 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008224 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008225 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8226 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8227 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8228 LPFC_WQE_LENLOC_WORD3);
8229 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008230 break;
James Smart4f774512009-05-22 14:52:35 -04008231 case CMD_FCP_IWRITE64_CR:
8232 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008233 /* word3 iocb=iotag wqe=payload_offset_len */
8234 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008235 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8236 xmit_len + sizeof(struct fcp_rsp));
8237 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8238 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008239 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8240 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8241 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8242 iocbq->iocb.ulpFCP2Rcvy);
8243 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8244 /* Always open the exchange */
8245 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008246 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8247 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8248 LPFC_WQE_LENLOC_WORD4);
8249 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8250 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008251 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008252 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008253 case CMD_FCP_IREAD64_CR:
8254 /* word3 iocb=iotag wqe=payload_offset_len */
8255 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008256 bf_set(payload_offset_len, &wqe->fcp_iread,
8257 xmit_len + sizeof(struct fcp_rsp));
8258 bf_set(cmd_buff_len, &wqe->fcp_iread,
8259 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008260 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8261 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8262 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8263 iocbq->iocb.ulpFCP2Rcvy);
8264 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008265 /* Always open the exchange */
8266 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008267 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8268 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8269 LPFC_WQE_LENLOC_WORD4);
8270 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8271 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008272 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008273 break;
James Smartf1126682009-06-10 17:22:44 -04008274 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008275 /* word3 iocb=iotag wqe=payload_offset_len */
8276 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8277 bf_set(payload_offset_len, &wqe->fcp_icmd,
8278 xmit_len + sizeof(struct fcp_rsp));
8279 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8280 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008281 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008282 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008283 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008284 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8285 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8286 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8287 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8288 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8289 LPFC_WQE_LENLOC_NONE);
8290 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart2a94aea2012-09-29 11:30:31 -04008291 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8292 iocbq->iocb.ulpFCP2Rcvy);
James Smart7851fe22011-07-22 18:36:52 -04008293 break;
James Smart4f774512009-05-22 14:52:35 -04008294 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008295 /* For this command calculate the xmit length of the
8296 * request bde.
8297 */
8298 xmit_len = 0;
8299 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8300 sizeof(struct ulp_bde64);
8301 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008302 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008303 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8304 break;
James Smart63e801c2010-11-20 23:14:19 -05008305 xmit_len += bde.tus.f.bdeSize;
8306 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008307 /* word3 iocb=IO_TAG wqe=request_payload_len */
8308 wqe->gen_req.request_payload_len = xmit_len;
8309 /* word4 iocb=parameter wqe=relative_offset memcpy */
8310 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008311 /* word6 context tag copied in memcpy */
8312 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8313 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8314 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8315 "2015 Invalid CT %x command 0x%x\n",
8316 ct, iocbq->iocb.ulpCommand);
8317 return IOCB_ERROR;
8318 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008319 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8320 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8321 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8322 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8323 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8324 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8325 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8326 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008327 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008328 break;
James Smart4f774512009-05-22 14:52:35 -04008329 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008330 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008331 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008332 /* word3 iocb=iotag32 wqe=response_payload_len */
8333 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008334 /* word4 */
8335 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008336 /* word5 iocb=rsvd wge=did */
8337 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008338 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8339
8340 if_type = bf_get(lpfc_sli_intf_if_type,
8341 &phba->sli4_hba.sli_intf);
8342 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8343 if (iocbq->vport->fc_flag & FC_PT2PT) {
8344 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8345 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8346 iocbq->vport->fc_myDID);
8347 if (iocbq->vport->fc_myDID == Fabric_DID) {
8348 bf_set(wqe_els_did,
8349 &wqe->xmit_els_rsp.wqe_dest, 0);
8350 }
8351 }
8352 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008353 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8354 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8355 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8356 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008357 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008358 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008359 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008360 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008361 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8362 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8363 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8364 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8365 LPFC_WQE_LENLOC_WORD3);
8366 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008367 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8368 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008369 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8370 iocbq->context2)->virt);
8371 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008372 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8373 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008374 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008375 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8376 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008377 phba->vpi_ids[phba->pport->vpi]);
8378 }
James Smart4f774512009-05-22 14:52:35 -04008379 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008380 break;
James Smart4f774512009-05-22 14:52:35 -04008381 case CMD_CLOSE_XRI_CN:
8382 case CMD_ABORT_XRI_CN:
8383 case CMD_ABORT_XRI_CX:
8384 /* words 0-2 memcpy should be 0 rserved */
8385 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008386 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8387 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8388 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8389 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8390 } else
8391 fip = 0;
8392
8393 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008394 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008395 * The link is down, or the command was ELS_FIP
8396 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008397 * on the wire.
8398 */
8399 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8400 else
8401 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8402 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008403 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8404 wqe->abort_cmd.rsrvd5 = 0;
8405 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008406 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8407 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008408 /*
8409 * The abort handler will send us CMD_ABORT_XRI_CN or
8410 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8411 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008412 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8413 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8414 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8415 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008416 cmnd = CMD_ABORT_XRI_CX;
8417 command_type = OTHER_COMMAND;
8418 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008419 break;
James Smart6669f9b2009-10-02 15:16:45 -04008420 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008421 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008422 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008423 * we re-construct this WQE here based on information in
8424 * iocbq from scratch.
8425 */
8426 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008427 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008428 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008429 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8430 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008431 LPFC_ABTS_UNSOL_INT) {
8432 /* ABTS sent by initiator to CT exchange, the
8433 * RX_ID field will be filled with the newly
8434 * allocated responder XRI.
8435 */
8436 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8437 iocbq->sli4_xritag);
8438 } else {
8439 /* ABTS sent by responder to CT exchange, the
8440 * RX_ID field will be filled with the responder
8441 * RX_ID from ABTS.
8442 */
8443 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008444 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008445 }
James Smart6669f9b2009-10-02 15:16:45 -04008446 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8447 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008448
8449 /* Use CT=VPI */
8450 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8451 ndlp->nlp_DID);
8452 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8453 iocbq->iocb.ulpContext);
8454 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008455 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008456 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008457 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8458 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8459 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008460 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8461 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008462 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8463 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8464 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8465 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8466 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8467 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8468 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8469 }
8470
James Smart7851fe22011-07-22 18:36:52 -04008471 break;
James Smart4f774512009-05-22 14:52:35 -04008472 case CMD_XRI_ABORTED_CX:
8473 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008474 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8475 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8476 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8477 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8478 default:
8479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8480 "2014 Invalid command 0x%x\n",
8481 iocbq->iocb.ulpCommand);
8482 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008483 break;
James Smart4f774512009-05-22 14:52:35 -04008484 }
James Smart6d368e52011-05-24 11:44:12 -04008485
James Smart8012cc32012-10-31 14:44:49 -04008486 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8487 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8488 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8489 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8490 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8491 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8492 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8493 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008494 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8495 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8496 wqe->generic.wqe_com.abort_tag = abort_tag;
8497 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8498 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8499 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8500 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008501 return 0;
8502}
8503
8504/**
8505 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8506 * @phba: Pointer to HBA context object.
8507 * @ring_number: SLI ring number to issue iocb on.
8508 * @piocb: Pointer to command iocb.
8509 * @flag: Flag indicating if this command can be put into txq.
8510 *
8511 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8512 * an iocb command to an HBA with SLI-4 interface spec.
8513 *
8514 * This function is called with hbalock held. The function will return success
8515 * after it successfully submit the iocb to firmware or after adding to the
8516 * txq.
8517 **/
8518static int
8519__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8520 struct lpfc_iocbq *piocb, uint32_t flag)
8521{
8522 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008523 union lpfc_wqe wqe;
8524 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008525
8526 if (piocb->sli4_xritag == NO_XRI) {
8527 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008528 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008529 sglq = NULL;
8530 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008531 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008532 if (!(flag & SLI_IOCB_RET_IOCB)) {
8533 __lpfc_sli_ringtx_put(phba,
8534 pring, piocb);
8535 return IOCB_SUCCESS;
8536 } else {
8537 return IOCB_BUSY;
8538 }
8539 } else {
James Smart6d368e52011-05-24 11:44:12 -04008540 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008541 if (!sglq) {
8542 if (!(flag & SLI_IOCB_RET_IOCB)) {
8543 __lpfc_sli_ringtx_put(phba,
8544 pring,
8545 piocb);
8546 return IOCB_SUCCESS;
8547 } else
8548 return IOCB_BUSY;
8549 }
8550 }
James Smart4f774512009-05-22 14:52:35 -04008551 }
8552 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008553 /* These IO's already have an XRI and a mapped sgl. */
8554 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008555 } else {
James Smart6d368e52011-05-24 11:44:12 -04008556 /*
8557 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008558 * sglq is on the active list
8559 */
James Smartedccdc12013-01-03 15:43:45 -05008560 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008561 if (!sglq)
8562 return IOCB_ERROR;
8563 }
8564
8565 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008566 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008567 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008568 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008569 return IOCB_ERROR;
8570 }
8571
8572 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8573 return IOCB_ERROR;
8574
James Smart341af102010-01-26 23:07:37 -05008575 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8576 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartea714f32013-04-17 20:18:47 -04008577 if (unlikely(!phba->sli4_hba.fcp_wq))
8578 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008579 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8580 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008581 return IOCB_ERROR;
8582 } else {
James Smartea714f32013-04-17 20:18:47 -04008583 if (unlikely(!phba->sli4_hba.els_wq))
8584 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04008585 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8586 return IOCB_ERROR;
8587 }
8588 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8589
8590 return 0;
8591}
8592
8593/**
James Smart3772a992009-05-22 14:50:54 -04008594 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8595 *
8596 * This routine wraps the actual lockless version for issusing IOCB function
8597 * pointer from the lpfc_hba struct.
8598 *
8599 * Return codes:
8600 * IOCB_ERROR - Error
8601 * IOCB_SUCCESS - Success
8602 * IOCB_BUSY - Busy
8603 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008604int
James Smart3772a992009-05-22 14:50:54 -04008605__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8606 struct lpfc_iocbq *piocb, uint32_t flag)
8607{
8608 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8609}
8610
8611/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008612 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008613 * @phba: The hba struct for which this call is being executed.
8614 * @dev_grp: The HBA PCI-Device group number.
8615 *
8616 * This routine sets up the SLI interface API function jump table in @phba
8617 * struct.
8618 * Returns: 0 - success, -ENODEV - failure.
8619 **/
8620int
8621lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8622{
8623
8624 switch (dev_grp) {
8625 case LPFC_PCI_DEV_LP:
8626 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8627 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8628 break;
James Smart4f774512009-05-22 14:52:35 -04008629 case LPFC_PCI_DEV_OC:
8630 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8631 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8632 break;
James Smart3772a992009-05-22 14:50:54 -04008633 default:
8634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8635 "1419 Invalid HBA PCI-device group: 0x%x\n",
8636 dev_grp);
8637 return -ENODEV;
8638 break;
8639 }
8640 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8641 return 0;
8642}
James Smart92d7f7b2007-06-17 19:56:38 -05008643
James Smarte59058c2008-08-24 21:49:00 -04008644/**
James Smart3621a712009-04-06 18:47:14 -04008645 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008646 * @phba: Pointer to HBA context object.
8647 * @pring: Pointer to driver SLI ring object.
8648 * @piocb: Pointer to command iocb.
8649 * @flag: Flag indicating if this command can be put into txq.
8650 *
8651 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8652 * function. This function gets the hbalock and calls
8653 * __lpfc_sli_issue_iocb function and will return the error returned
8654 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8655 * functions which do not hold hbalock.
8656 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008657int
James Smart3772a992009-05-22 14:50:54 -04008658lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008659 struct lpfc_iocbq *piocb, uint32_t flag)
8660{
James Smartba20c852012-08-03 12:36:52 -04008661 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008662 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008663 struct lpfc_queue *fpeq;
8664 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008665 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008666 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008667
James Smart7e56aa22012-08-03 12:35:34 -04008668 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008669 if (piocb->iocb_flag & LPFC_IO_FCP) {
8670 if (unlikely(!phba->sli4_hba.fcp_wq))
8671 return IOCB_ERROR;
8672 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8673 piocb->fcp_wqidx = idx;
8674 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008675
James Smartba20c852012-08-03 12:36:52 -04008676 pring = &phba->sli.ring[ring_number];
8677 spin_lock_irqsave(&pring->ring_lock, iflags);
8678 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8679 flag);
8680 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8681
8682 if (lpfc_fcp_look_ahead) {
8683 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8684
8685 if (atomic_dec_and_test(&fcp_eq_hdl->
8686 fcp_eq_in_use)) {
8687
8688 /* Get associated EQ with this index */
8689 fpeq = phba->sli4_hba.hba_eq[idx];
8690
8691 /* Turn off interrupts from this EQ */
8692 lpfc_sli4_eq_clr_intr(fpeq);
8693
8694 /*
8695 * Process all the events on FCP EQ
8696 */
8697 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8698 lpfc_sli4_hba_handle_eqe(phba,
8699 eqe, idx);
8700 fpeq->EQ_processed++;
8701 }
8702
8703 /* Always clear and re-arm the EQ */
8704 lpfc_sli4_eq_release(fpeq,
8705 LPFC_QUEUE_REARM);
8706 }
8707 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8708 }
8709 } else {
8710 pring = &phba->sli.ring[ring_number];
8711 spin_lock_irqsave(&pring->ring_lock, iflags);
8712 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8713 flag);
8714 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8715
James Smart2a76a282012-08-03 12:35:54 -04008716 }
James Smart7e56aa22012-08-03 12:35:34 -04008717 } else {
8718 /* For now, SLI2/3 will still use hbalock */
8719 spin_lock_irqsave(&phba->hbalock, iflags);
8720 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8721 spin_unlock_irqrestore(&phba->hbalock, iflags);
8722 }
James Smart92d7f7b2007-06-17 19:56:38 -05008723 return rc;
8724}
8725
James Smarte59058c2008-08-24 21:49:00 -04008726/**
James Smart3621a712009-04-06 18:47:14 -04008727 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008728 * @phba: Pointer to HBA context object.
8729 *
8730 * This function is called while driver attaches with the
8731 * HBA to setup the extra ring. The extra ring is used
8732 * only when driver needs to support target mode functionality
8733 * or IP over FC functionalities.
8734 *
8735 * This function is called with no lock held.
8736 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008737static int
8738lpfc_extra_ring_setup( struct lpfc_hba *phba)
8739{
8740 struct lpfc_sli *psli;
8741 struct lpfc_sli_ring *pring;
8742
8743 psli = &phba->sli;
8744
8745 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008746
8747 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008748 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008749 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8750 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8751 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8752 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008753
James Smarta4bc3372006-12-02 13:34:16 -05008754 /* and give them to the extra ring */
8755 pring = &psli->ring[psli->extra_ring];
8756
James Smart7e56aa22012-08-03 12:35:34 -04008757 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8758 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8759 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8760 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008761
8762 /* Setup default profile for this ring */
8763 pring->iotag_max = 4096;
8764 pring->num_mask = 1;
8765 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008766 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8767 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008768 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8769 return 0;
8770}
8771
James Smartcb69f7d2011-12-13 13:21:57 -05008772/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8773 * @phba: Pointer to HBA context object.
8774 * @iocbq: Pointer to iocb object.
8775 *
8776 * The async_event handler calls this routine when it receives
8777 * an ASYNC_STATUS_CN event from the port. The port generates
8778 * this event when an Abort Sequence request to an rport fails
8779 * twice in succession. The abort could be originated by the
8780 * driver or by the port. The ABTS could have been for an ELS
8781 * or FCP IO. The port only generates this event when an ABTS
8782 * fails to complete after one retry.
8783 */
8784static void
8785lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8786 struct lpfc_iocbq *iocbq)
8787{
8788 struct lpfc_nodelist *ndlp = NULL;
8789 uint16_t rpi = 0, vpi = 0;
8790 struct lpfc_vport *vport = NULL;
8791
8792 /* The rpi in the ulpContext is vport-sensitive. */
8793 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8794 rpi = iocbq->iocb.ulpContext;
8795
8796 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8797 "3092 Port generated ABTS async event "
8798 "on vpi %d rpi %d status 0x%x\n",
8799 vpi, rpi, iocbq->iocb.ulpStatus);
8800
8801 vport = lpfc_find_vport_by_vpid(phba, vpi);
8802 if (!vport)
8803 goto err_exit;
8804 ndlp = lpfc_findnode_rpi(vport, rpi);
8805 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8806 goto err_exit;
8807
8808 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8809 lpfc_sli_abts_recover_port(vport, ndlp);
8810 return;
8811
8812 err_exit:
8813 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8814 "3095 Event Context not found, no "
8815 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8816 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8817 vpi, rpi);
8818}
8819
8820/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8821 * @phba: pointer to HBA context object.
8822 * @ndlp: nodelist pointer for the impacted rport.
8823 * @axri: pointer to the wcqe containing the failed exchange.
8824 *
8825 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8826 * port. The port generates this event when an abort exchange request to an
8827 * rport fails twice in succession with no reply. The abort could be originated
8828 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8829 */
8830void
8831lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8832 struct lpfc_nodelist *ndlp,
8833 struct sli4_wcqe_xri_aborted *axri)
8834{
8835 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008836 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008837
James Smart6b5151f2012-01-18 16:24:06 -05008838 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008839 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8840 "3115 Node Context not found, driver "
8841 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008842 return;
8843 }
8844
James Smartcb69f7d2011-12-13 13:21:57 -05008845 vport = ndlp->vport;
8846 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8847 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008848 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04008849 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05008850 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008851 bf_get(lpfc_wcqe_xa_status, axri),
8852 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008853
James Smart5c1db2a2012-03-01 22:34:36 -05008854 /*
8855 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8856 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8857 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8858 */
James Smarte3d2b802012-08-14 14:25:43 -04008859 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05008860 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8861 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008862 lpfc_sli_abts_recover_port(vport, ndlp);
8863}
8864
James Smarte59058c2008-08-24 21:49:00 -04008865/**
James Smart3621a712009-04-06 18:47:14 -04008866 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008867 * @phba: Pointer to HBA context object.
8868 * @pring: Pointer to driver SLI ring object.
8869 * @iocbq: Pointer to iocb object.
8870 *
8871 * This function is called by the slow ring event handler
8872 * function when there is an ASYNC event iocb in the ring.
8873 * This function is called with no lock held.
8874 * Currently this function handles only temperature related
8875 * ASYNC events. The function decodes the temperature sensor
8876 * event message and posts events for the management applications.
8877 **/
James Smart98c9ea52007-10-27 13:37:33 -04008878static void
James Smart57127f12007-10-27 13:37:05 -04008879lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8880 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8881{
8882 IOCB_t *icmd;
8883 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008884 struct temp_event temp_event_data;
8885 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008886 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008887
8888 icmd = &iocbq->iocb;
8889 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008890
James Smartcb69f7d2011-12-13 13:21:57 -05008891 switch (evt_code) {
8892 case ASYNC_TEMP_WARN:
8893 case ASYNC_TEMP_SAFE:
8894 temp_event_data.data = (uint32_t) icmd->ulpContext;
8895 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8896 if (evt_code == ASYNC_TEMP_WARN) {
8897 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8898 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8899 "0347 Adapter is very hot, please take "
8900 "corrective action. temperature : %d Celsius\n",
8901 (uint32_t) icmd->ulpContext);
8902 } else {
8903 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8904 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8905 "0340 Adapter temperature is OK now. "
8906 "temperature : %d Celsius\n",
8907 (uint32_t) icmd->ulpContext);
8908 }
8909
8910 /* Send temperature change event to applications */
8911 shost = lpfc_shost_from_vport(phba->pport);
8912 fc_host_post_vendor_event(shost, fc_get_event_number(),
8913 sizeof(temp_event_data), (char *) &temp_event_data,
8914 LPFC_NL_VENDOR_ID);
8915 break;
8916 case ASYNC_STATUS_CN:
8917 lpfc_sli_abts_err_handler(phba, iocbq);
8918 break;
8919 default:
James Smarta257bf92009-04-06 18:48:10 -04008920 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008921 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008922 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008923 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008924 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8925 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8926 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8927 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008928 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008929 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8930 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8931 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8932 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8933
James Smartcb69f7d2011-12-13 13:21:57 -05008934 break;
James Smart57127f12007-10-27 13:37:05 -04008935 }
James Smart57127f12007-10-27 13:37:05 -04008936}
8937
8938
James Smarte59058c2008-08-24 21:49:00 -04008939/**
James Smart3621a712009-04-06 18:47:14 -04008940 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008941 * @phba: Pointer to HBA context object.
8942 *
8943 * lpfc_sli_setup sets up rings of the SLI interface with
8944 * number of iocbs per ring and iotags. This function is
8945 * called while driver attach to the HBA and before the
8946 * interrupts are enabled. So there is no need for locking.
8947 *
8948 * This function always returns 0.
8949 **/
dea31012005-04-17 16:05:31 -05008950int
8951lpfc_sli_setup(struct lpfc_hba *phba)
8952{
James Smarted957682007-06-17 19:56:37 -05008953 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008954 struct lpfc_sli *psli = &phba->sli;
8955 struct lpfc_sli_ring *pring;
8956
James Smart2a76a282012-08-03 12:35:54 -04008957 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8958 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04008959 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05008960 psli->sli_flag = 0;
8961 psli->fcp_ring = LPFC_FCP_RING;
8962 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008963 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008964
James Bottomley604a3e32005-10-29 10:28:33 -05008965 psli->iocbq_lookup = NULL;
8966 psli->iocbq_lookup_len = 0;
8967 psli->last_iotag = 0;
8968
dea31012005-04-17 16:05:31 -05008969 for (i = 0; i < psli->num_rings; i++) {
8970 pring = &psli->ring[i];
8971 switch (i) {
8972 case LPFC_FCP_RING: /* ring 0 - FCP */
8973 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008974 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8975 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8976 pring->sli.sli3.numCiocb +=
8977 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8978 pring->sli.sli3.numRiocb +=
8979 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8980 pring->sli.sli3.numCiocb +=
8981 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8982 pring->sli.sli3.numRiocb +=
8983 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8984 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008985 SLI3_IOCB_CMD_SIZE :
8986 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008987 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008988 SLI3_IOCB_RSP_SIZE :
8989 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008990 pring->iotag_ctr = 0;
8991 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008992 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008993 pring->fast_iotag = pring->iotag_max;
8994 pring->num_mask = 0;
8995 break;
James Smarta4bc3372006-12-02 13:34:16 -05008996 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008997 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008998 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8999 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9000 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009001 SLI3_IOCB_CMD_SIZE :
9002 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009003 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009004 SLI3_IOCB_RSP_SIZE :
9005 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009006 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009007 pring->num_mask = 0;
9008 break;
9009 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9010 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009011 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9012 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9013 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009014 SLI3_IOCB_CMD_SIZE :
9015 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009016 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009017 SLI3_IOCB_RSP_SIZE :
9018 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009019 pring->fast_iotag = 0;
9020 pring->iotag_ctr = 0;
9021 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009022 pring->lpfc_sli_rcv_async_status =
9023 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009024 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009025 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009026 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9027 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009028 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009029 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009030 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009031 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9032 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009033 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009034 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009035 pring->prt[2].profile = 0; /* Mask 2 */
9036 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009037 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009038 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009039 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009040 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009041 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009042 pring->prt[3].profile = 0; /* Mask 3 */
9043 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009044 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009045 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009046 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009047 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009048 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009049 break;
9050 }
James Smart7e56aa22012-08-03 12:35:34 -04009051 totiocbsize += (pring->sli.sli3.numCiocb *
9052 pring->sli.sli3.sizeCiocb) +
9053 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009054 }
James Smarted957682007-06-17 19:56:37 -05009055 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009056 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009057 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9058 "SLI2 SLIM Data: x%x x%lx\n",
9059 phba->brd_no, totiocbsize,
9060 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009061 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009062 if (phba->cfg_multi_ring_support == 2)
9063 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009064
9065 return 0;
9066}
9067
James Smarte59058c2008-08-24 21:49:00 -04009068/**
James Smart3621a712009-04-06 18:47:14 -04009069 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009070 * @phba: Pointer to HBA context object.
9071 *
9072 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9073 * ring. This function also initializes ring indices of each ring.
9074 * This function is called during the initialization of the SLI
9075 * interface of an HBA.
9076 * This function is called with no lock held and always returns
9077 * 1.
9078 **/
dea31012005-04-17 16:05:31 -05009079int
James Smart2e0fef82007-06-17 19:56:36 -05009080lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009081{
9082 struct lpfc_sli *psli;
9083 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009084 int i;
dea31012005-04-17 16:05:31 -05009085
9086 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009087 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009088 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009089 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009090 /* Initialize list headers for txq and txcmplq as double linked lists */
9091 for (i = 0; i < psli->num_rings; i++) {
9092 pring = &psli->ring[i];
9093 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009094 pring->sli.sli3.next_cmdidx = 0;
9095 pring->sli.sli3.local_getidx = 0;
9096 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009097 INIT_LIST_HEAD(&pring->txq);
9098 INIT_LIST_HEAD(&pring->txcmplq);
9099 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009100 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009101 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009102 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009103 }
James Smart2e0fef82007-06-17 19:56:36 -05009104 spin_unlock_irq(&phba->hbalock);
9105 return 1;
dea31012005-04-17 16:05:31 -05009106}
9107
James Smarte59058c2008-08-24 21:49:00 -04009108/**
James Smart04c68492009-05-22 14:52:52 -04009109 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9110 * @phba: Pointer to HBA context object.
9111 *
9112 * This routine flushes the mailbox command subsystem. It will unconditionally
9113 * flush all the mailbox commands in the three possible stages in the mailbox
9114 * command sub-system: pending mailbox command queue; the outstanding mailbox
9115 * command; and completed mailbox command queue. It is caller's responsibility
9116 * to make sure that the driver is in the proper state to flush the mailbox
9117 * command sub-system. Namely, the posting of mailbox commands into the
9118 * pending mailbox command queue from the various clients must be stopped;
9119 * either the HBA is in a state that it will never works on the outstanding
9120 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9121 * mailbox command has been completed.
9122 **/
9123static void
9124lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9125{
9126 LIST_HEAD(completions);
9127 struct lpfc_sli *psli = &phba->sli;
9128 LPFC_MBOXQ_t *pmb;
9129 unsigned long iflag;
9130
9131 /* Flush all the mailbox commands in the mbox system */
9132 spin_lock_irqsave(&phba->hbalock, iflag);
9133 /* The pending mailbox command queue */
9134 list_splice_init(&phba->sli.mboxq, &completions);
9135 /* The outstanding active mailbox command */
9136 if (psli->mbox_active) {
9137 list_add_tail(&psli->mbox_active->list, &completions);
9138 psli->mbox_active = NULL;
9139 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9140 }
9141 /* The completed mailbox command queue */
9142 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9143 spin_unlock_irqrestore(&phba->hbalock, iflag);
9144
9145 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9146 while (!list_empty(&completions)) {
9147 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9148 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9149 if (pmb->mbox_cmpl)
9150 pmb->mbox_cmpl(phba, pmb);
9151 }
9152}
9153
9154/**
James Smart3621a712009-04-06 18:47:14 -04009155 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009156 * @vport: Pointer to virtual port object.
9157 *
9158 * lpfc_sli_host_down is called to clean up the resources
9159 * associated with a vport before destroying virtual
9160 * port data structures.
9161 * This function does following operations:
9162 * - Free discovery resources associated with this virtual
9163 * port.
9164 * - Free iocbs associated with this virtual port in
9165 * the txq.
9166 * - Send abort for all iocb commands associated with this
9167 * vport in txcmplq.
9168 *
9169 * This function is called with no lock held and always returns 1.
9170 **/
dea31012005-04-17 16:05:31 -05009171int
James Smart92d7f7b2007-06-17 19:56:38 -05009172lpfc_sli_host_down(struct lpfc_vport *vport)
9173{
James Smart858c9f62007-06-17 19:56:39 -05009174 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009175 struct lpfc_hba *phba = vport->phba;
9176 struct lpfc_sli *psli = &phba->sli;
9177 struct lpfc_sli_ring *pring;
9178 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009179 int i;
9180 unsigned long flags = 0;
9181 uint16_t prev_pring_flag;
9182
9183 lpfc_cleanup_discovery_resources(vport);
9184
9185 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009186 for (i = 0; i < psli->num_rings; i++) {
9187 pring = &psli->ring[i];
9188 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009189 /* Only slow rings */
9190 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009191 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009192 /* Set the lpfc data pending flag */
9193 set_bit(LPFC_DATA_READY, &phba->data_flags);
9194 }
James Smart92d7f7b2007-06-17 19:56:38 -05009195 /*
9196 * Error everything on the txq since these iocbs have not been
9197 * given to the FW yet.
9198 */
James Smart92d7f7b2007-06-17 19:56:38 -05009199 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9200 if (iocb->vport != vport)
9201 continue;
James Smart858c9f62007-06-17 19:56:39 -05009202 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009203 }
9204
9205 /* Next issue ABTS for everything on the txcmplq */
9206 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9207 list) {
9208 if (iocb->vport != vport)
9209 continue;
9210 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9211 }
9212
9213 pring->flag = prev_pring_flag;
9214 }
9215
9216 spin_unlock_irqrestore(&phba->hbalock, flags);
9217
James Smarta257bf92009-04-06 18:48:10 -04009218 /* Cancel all the IOCBs from the completions list */
9219 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9220 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009221 return 1;
9222}
9223
James Smarte59058c2008-08-24 21:49:00 -04009224/**
James Smart3621a712009-04-06 18:47:14 -04009225 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009226 * @phba: Pointer to HBA context object.
9227 *
9228 * This function cleans up all iocb, buffers, mailbox commands
9229 * while shutting down the HBA. This function is called with no
9230 * lock held and always returns 1.
9231 * This function does the following to cleanup driver resources:
9232 * - Free discovery resources for each virtual port
9233 * - Cleanup any pending fabric iocbs
9234 * - Iterate through the iocb txq and free each entry
9235 * in the list.
9236 * - Free up any buffer posted to the HBA
9237 * - Free mailbox commands in the mailbox queue.
9238 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009239int
James Smart2e0fef82007-06-17 19:56:36 -05009240lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009241{
James Smart2534ba72007-04-25 09:52:20 -04009242 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009243 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009244 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009245 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009246 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009247 int i;
9248
9249 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009250 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009251
dea31012005-04-17 16:05:31 -05009252 lpfc_hba_down_prep(phba);
9253
James Smart92d7f7b2007-06-17 19:56:38 -05009254 lpfc_fabric_abort_hba(phba);
9255
James Smart2e0fef82007-06-17 19:56:36 -05009256 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009257 for (i = 0; i < psli->num_rings; i++) {
9258 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009259 /* Only slow rings */
9260 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009261 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009262 /* Set the lpfc data pending flag */
9263 set_bit(LPFC_DATA_READY, &phba->data_flags);
9264 }
dea31012005-04-17 16:05:31 -05009265
9266 /*
9267 * Error everything on the txq since these iocbs have not been
9268 * given to the FW yet.
9269 */
James Smart2534ba72007-04-25 09:52:20 -04009270 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009271 }
James Smart2e0fef82007-06-17 19:56:36 -05009272 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009273
James Smarta257bf92009-04-06 18:48:10 -04009274 /* Cancel all the IOCBs from the completions list */
9275 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9276 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009277
James Smart0ff10d42008-01-11 01:52:36 -05009278 spin_lock_irqsave(&phba->hbalock, flags);
9279 list_splice_init(&phba->elsbuf, &completions);
9280 phba->elsbuf_cnt = 0;
9281 phba->elsbuf_prev_cnt = 0;
9282 spin_unlock_irqrestore(&phba->hbalock, flags);
9283
9284 while (!list_empty(&completions)) {
9285 list_remove_head(&completions, buf_ptr,
9286 struct lpfc_dmabuf, list);
9287 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9288 kfree(buf_ptr);
9289 }
9290
dea31012005-04-17 16:05:31 -05009291 /* Return any active mbox cmds */
9292 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009293
James Smartda0436e2009-05-22 14:51:39 -04009294 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009295 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009296 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009297
James Smartda0436e2009-05-22 14:51:39 -04009298 return 1;
9299}
James Smart92d7f7b2007-06-17 19:56:38 -05009300
James Smartda0436e2009-05-22 14:51:39 -04009301/**
James Smart3621a712009-04-06 18:47:14 -04009302 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009303 * @srcp: Source memory pointer.
9304 * @destp: Destination memory pointer.
9305 * @cnt: Number of words required to be copied.
9306 *
9307 * This function is used for copying data between driver memory
9308 * and the SLI memory. This function also changes the endianness
9309 * of each word if native endianness is different from SLI
9310 * endianness. This function can be called with or without
9311 * lock.
9312 **/
dea31012005-04-17 16:05:31 -05009313void
9314lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9315{
9316 uint32_t *src = srcp;
9317 uint32_t *dest = destp;
9318 uint32_t ldata;
9319 int i;
9320
9321 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9322 ldata = *src;
9323 ldata = le32_to_cpu(ldata);
9324 *dest = ldata;
9325 src++;
9326 dest++;
9327 }
9328}
9329
James Smarte59058c2008-08-24 21:49:00 -04009330
9331/**
James Smarta0c87cb2009-07-19 10:01:10 -04009332 * lpfc_sli_bemem_bcopy - SLI memory copy function
9333 * @srcp: Source memory pointer.
9334 * @destp: Destination memory pointer.
9335 * @cnt: Number of words required to be copied.
9336 *
9337 * This function is used for copying data between a data structure
9338 * with big endian representation to local endianness.
9339 * This function can be called with or without lock.
9340 **/
9341void
9342lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9343{
9344 uint32_t *src = srcp;
9345 uint32_t *dest = destp;
9346 uint32_t ldata;
9347 int i;
9348
9349 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9350 ldata = *src;
9351 ldata = be32_to_cpu(ldata);
9352 *dest = ldata;
9353 src++;
9354 dest++;
9355 }
9356}
9357
9358/**
James Smart3621a712009-04-06 18:47:14 -04009359 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009360 * @phba: Pointer to HBA context object.
9361 * @pring: Pointer to driver SLI ring object.
9362 * @mp: Pointer to driver buffer object.
9363 *
9364 * This function is called with no lock held.
9365 * It always return zero after adding the buffer to the postbufq
9366 * buffer list.
9367 **/
dea31012005-04-17 16:05:31 -05009368int
James Smart2e0fef82007-06-17 19:56:36 -05009369lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9370 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009371{
9372 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9373 later */
James Smart2e0fef82007-06-17 19:56:36 -05009374 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009375 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009376 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009377 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009378 return 0;
9379}
9380
James Smarte59058c2008-08-24 21:49:00 -04009381/**
James Smart3621a712009-04-06 18:47:14 -04009382 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009383 * @phba: Pointer to HBA context object.
9384 *
9385 * When HBQ is enabled, buffers are searched based on tags. This function
9386 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9387 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9388 * does not conflict with tags of buffer posted for unsolicited events.
9389 * The function returns the allocated tag. The function is called with
9390 * no locks held.
9391 **/
James Smart76bb24e2007-10-27 13:38:00 -04009392uint32_t
9393lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9394{
9395 spin_lock_irq(&phba->hbalock);
9396 phba->buffer_tag_count++;
9397 /*
9398 * Always set the QUE_BUFTAG_BIT to distiguish between
9399 * a tag assigned by HBQ.
9400 */
9401 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9402 spin_unlock_irq(&phba->hbalock);
9403 return phba->buffer_tag_count;
9404}
9405
James Smarte59058c2008-08-24 21:49:00 -04009406/**
James Smart3621a712009-04-06 18:47:14 -04009407 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009408 * @phba: Pointer to HBA context object.
9409 * @pring: Pointer to driver SLI ring object.
9410 * @tag: Buffer tag.
9411 *
9412 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9413 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9414 * iocb is posted to the response ring with the tag of the buffer.
9415 * This function searches the pring->postbufq list using the tag
9416 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9417 * iocb. If the buffer is found then lpfc_dmabuf object of the
9418 * buffer is returned to the caller else NULL is returned.
9419 * This function is called with no lock held.
9420 **/
James Smart76bb24e2007-10-27 13:38:00 -04009421struct lpfc_dmabuf *
9422lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9423 uint32_t tag)
9424{
9425 struct lpfc_dmabuf *mp, *next_mp;
9426 struct list_head *slp = &pring->postbufq;
9427
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009428 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009429 spin_lock_irq(&phba->hbalock);
9430 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9431 if (mp->buffer_tag == tag) {
9432 list_del_init(&mp->list);
9433 pring->postbufq_cnt--;
9434 spin_unlock_irq(&phba->hbalock);
9435 return mp;
9436 }
9437 }
9438
9439 spin_unlock_irq(&phba->hbalock);
9440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009441 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009442 "ring %d Data x%lx x%p x%p x%x\n",
9443 pring->ringno, (unsigned long) tag,
9444 slp->next, slp->prev, pring->postbufq_cnt);
9445
9446 return NULL;
9447}
dea31012005-04-17 16:05:31 -05009448
James Smarte59058c2008-08-24 21:49:00 -04009449/**
James Smart3621a712009-04-06 18:47:14 -04009450 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009451 * @phba: Pointer to HBA context object.
9452 * @pring: Pointer to driver SLI ring object.
9453 * @phys: DMA address of the buffer.
9454 *
9455 * This function searches the buffer list using the dma_address
9456 * of unsolicited event to find the driver's lpfc_dmabuf object
9457 * corresponding to the dma_address. The function returns the
9458 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9459 * This function is called by the ct and els unsolicited event
9460 * handlers to get the buffer associated with the unsolicited
9461 * event.
9462 *
9463 * This function is called with no lock held.
9464 **/
dea31012005-04-17 16:05:31 -05009465struct lpfc_dmabuf *
9466lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9467 dma_addr_t phys)
9468{
9469 struct lpfc_dmabuf *mp, *next_mp;
9470 struct list_head *slp = &pring->postbufq;
9471
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009472 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009473 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009474 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9475 if (mp->phys == phys) {
9476 list_del_init(&mp->list);
9477 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009478 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009479 return mp;
9480 }
9481 }
9482
James Smart2e0fef82007-06-17 19:56:36 -05009483 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009485 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009486 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009487 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009488 slp->next, slp->prev, pring->postbufq_cnt);
9489 return NULL;
9490}
9491
James Smarte59058c2008-08-24 21:49:00 -04009492/**
James Smart3621a712009-04-06 18:47:14 -04009493 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009494 * @phba: Pointer to HBA context object.
9495 * @cmdiocb: Pointer to driver command iocb object.
9496 * @rspiocb: Pointer to driver response iocb object.
9497 *
9498 * This function is the completion handler for the abort iocbs for
9499 * ELS commands. This function is called from the ELS ring event
9500 * handler with no lock held. This function frees memory resources
9501 * associated with the abort iocb.
9502 **/
dea31012005-04-17 16:05:31 -05009503static void
James Smart2e0fef82007-06-17 19:56:36 -05009504lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9505 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009506{
James Smart2e0fef82007-06-17 19:56:36 -05009507 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009508 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009509 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009510
9511 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009512
9513 /*
9514 * Assume that the port already completed and returned, or
9515 * will return the iocb. Just Log the message.
9516 */
James Smart2680eea2007-04-25 09:52:55 -04009517 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9518 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9519
James Smart2e0fef82007-06-17 19:56:36 -05009520 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009521 if (phba->sli_rev < LPFC_SLI_REV4) {
9522 if (abort_iotag != 0 &&
9523 abort_iotag <= phba->sli.last_iotag)
9524 abort_iocb =
9525 phba->sli.iocbq_lookup[abort_iotag];
9526 } else
9527 /* For sli4 the abort_tag is the XRI,
9528 * so the abort routine puts the iotag of the iocb
9529 * being aborted in the context field of the abort
9530 * IOCB.
9531 */
9532 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009533
James Smart2a9bf3d2010-06-07 15:24:45 -04009534 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9535 "0327 Cannot abort els iocb %p "
9536 "with tag %x context %x, abort status %x, "
9537 "abort code %x\n",
9538 abort_iocb, abort_iotag, abort_context,
9539 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009540
James Smartff78d8f2011-12-13 13:21:35 -05009541 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009542 }
James Bottomley604a3e32005-10-29 10:28:33 -05009543 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009544 return;
9545}
9546
James Smarte59058c2008-08-24 21:49:00 -04009547/**
James Smart3621a712009-04-06 18:47:14 -04009548 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009549 * @phba: Pointer to HBA context object.
9550 * @cmdiocb: Pointer to driver command iocb object.
9551 * @rspiocb: Pointer to driver response iocb object.
9552 *
9553 * The function is called from SLI ring event handler with no
9554 * lock held. This function is the completion handler for ELS commands
9555 * which are aborted. The function frees memory resources used for
9556 * the aborted ELS commands.
9557 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009558static void
9559lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9560 struct lpfc_iocbq *rspiocb)
9561{
9562 IOCB_t *irsp = &rspiocb->iocb;
9563
9564 /* ELS cmd tag <ulpIoTag> completes */
9565 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009566 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009567 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009568 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009569 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009570 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9571 lpfc_ct_free_iocb(phba, cmdiocb);
9572 else
9573 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009574 return;
9575}
9576
James Smarte59058c2008-08-24 21:49:00 -04009577/**
James Smart5af5eee2010-10-22 11:06:38 -04009578 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009579 * @phba: Pointer to HBA context object.
9580 * @pring: Pointer to driver SLI ring object.
9581 * @cmdiocb: Pointer to driver command iocb object.
9582 *
James Smart5af5eee2010-10-22 11:06:38 -04009583 * This function issues an abort iocb for the provided command iocb down to
9584 * the port. Other than the case the outstanding command iocb is an abort
9585 * request, this function issues abort out unconditionally. This function is
9586 * called with hbalock held. The function returns 0 when it fails due to
9587 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009588 **/
James Smart5af5eee2010-10-22 11:06:38 -04009589static int
9590lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009591 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009592{
James Smart2e0fef82007-06-17 19:56:36 -05009593 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009594 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009595 IOCB_t *icmd = NULL;
9596 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009597 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009598 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009599
James Smart92d7f7b2007-06-17 19:56:38 -05009600 /*
9601 * There are certain command types we don't want to abort. And we
9602 * don't want to abort commands that are already in the process of
9603 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009604 */
9605 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009606 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009607 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9608 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009609 return 0;
9610
dea31012005-04-17 16:05:31 -05009611 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009612 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009613 if (abtsiocbp == NULL)
9614 return 0;
dea31012005-04-17 16:05:31 -05009615
James Smart07951072007-04-25 09:51:38 -04009616 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009617 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009618 */
9619 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9620
dea31012005-04-17 16:05:31 -05009621 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009622 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9623 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009624 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009625 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009626 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9627 }
James Smartda0436e2009-05-22 14:51:39 -04009628 else
9629 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009630 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009631 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009632
James Smart5ffc2662009-11-18 15:39:44 -05009633 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9634 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009635 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9636 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009637
James Smart2e0fef82007-06-17 19:56:36 -05009638 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009639 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9640 else
9641 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9642
9643 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009644
James Smarte8b62012007-08-02 11:10:09 -04009645 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9646 "0339 Abort xri x%x, original iotag x%x, "
9647 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009648 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009649 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009650 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009651
9652 if (phba->sli_rev == LPFC_SLI_REV4) {
9653 /* Note: both hbalock and ring_lock need to be set here */
9654 spin_lock_irqsave(&pring->ring_lock, iflags);
9655 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9656 abtsiocbp, 0);
9657 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9658 } else {
9659 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9660 abtsiocbp, 0);
9661 }
James Smart07951072007-04-25 09:51:38 -04009662
James Smartd7c255b2008-08-24 21:50:00 -04009663 if (retval)
9664 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009665
9666 /*
9667 * Caller to this routine should check for IOCB_ERROR
9668 * and handle it properly. This routine no longer removes
9669 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9670 */
9671 return retval;
9672}
9673
9674/**
9675 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9676 * @phba: Pointer to HBA context object.
9677 * @pring: Pointer to driver SLI ring object.
9678 * @cmdiocb: Pointer to driver command iocb object.
9679 *
9680 * This function issues an abort iocb for the provided command iocb. In case
9681 * of unloading, the abort iocb will not be issued to commands on the ELS
9682 * ring. Instead, the callback function shall be changed to those commands
9683 * so that nothing happens when them finishes. This function is called with
9684 * hbalock held. The function returns 0 when the command iocb is an abort
9685 * request.
9686 **/
9687int
9688lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9689 struct lpfc_iocbq *cmdiocb)
9690{
9691 struct lpfc_vport *vport = cmdiocb->vport;
9692 int retval = IOCB_ERROR;
9693 IOCB_t *icmd = NULL;
9694
9695 /*
9696 * There are certain command types we don't want to abort. And we
9697 * don't want to abort commands that are already in the process of
9698 * being aborted.
9699 */
9700 icmd = &cmdiocb->iocb;
9701 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9702 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9703 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9704 return 0;
9705
9706 /*
9707 * If we're unloading, don't abort iocb on the ELS ring, but change
9708 * the callback so that nothing happens when it finishes.
9709 */
9710 if ((vport->load_flag & FC_UNLOADING) &&
9711 (pring->ringno == LPFC_ELS_RING)) {
9712 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9713 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9714 else
9715 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9716 goto abort_iotag_exit;
9717 }
9718
9719 /* Now, we try to issue the abort to the cmdiocb out */
9720 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9721
James Smart07951072007-04-25 09:51:38 -04009722abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009723 /*
9724 * Caller to this routine should check for IOCB_ERROR
9725 * and handle it properly. This routine no longer removes
9726 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009727 */
James Smart2e0fef82007-06-17 19:56:36 -05009728 return retval;
dea31012005-04-17 16:05:31 -05009729}
9730
James Smarte59058c2008-08-24 21:49:00 -04009731/**
James Smart5af5eee2010-10-22 11:06:38 -04009732 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9733 * @phba: Pointer to HBA context object.
9734 * @pring: Pointer to driver SLI ring object.
9735 *
9736 * This function aborts all iocbs in the given ring and frees all the iocb
9737 * objects in txq. This function issues abort iocbs unconditionally for all
9738 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9739 * to complete before the return of this function. The caller is not required
9740 * to hold any locks.
9741 **/
9742static void
9743lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9744{
9745 LIST_HEAD(completions);
9746 struct lpfc_iocbq *iocb, *next_iocb;
9747
9748 if (pring->ringno == LPFC_ELS_RING)
9749 lpfc_fabric_abort_hba(phba);
9750
9751 spin_lock_irq(&phba->hbalock);
9752
9753 /* Take off all the iocbs on txq for cancelling */
9754 list_splice_init(&pring->txq, &completions);
9755 pring->txq_cnt = 0;
9756
9757 /* Next issue ABTS for everything on the txcmplq */
9758 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9759 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9760
9761 spin_unlock_irq(&phba->hbalock);
9762
9763 /* Cancel all the IOCBs from the completions list */
9764 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9765 IOERR_SLI_ABORTED);
9766}
9767
9768/**
9769 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9770 * @phba: pointer to lpfc HBA data structure.
9771 *
9772 * This routine will abort all pending and outstanding iocbs to an HBA.
9773 **/
9774void
9775lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9776{
9777 struct lpfc_sli *psli = &phba->sli;
9778 struct lpfc_sli_ring *pring;
9779 int i;
9780
9781 for (i = 0; i < psli->num_rings; i++) {
9782 pring = &psli->ring[i];
9783 lpfc_sli_iocb_ring_abort(phba, pring);
9784 }
9785}
9786
9787/**
James Smart3621a712009-04-06 18:47:14 -04009788 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009789 * @iocbq: Pointer to driver iocb object.
9790 * @vport: Pointer to driver virtual port object.
9791 * @tgt_id: SCSI ID of the target.
9792 * @lun_id: LUN ID of the scsi device.
9793 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9794 *
James Smart3621a712009-04-06 18:47:14 -04009795 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009796 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9797 * 0 if the filtering criteria is met for the given iocb and will return
9798 * 1 if the filtering criteria is not met.
9799 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9800 * given iocb is for the SCSI device specified by vport, tgt_id and
9801 * lun_id parameter.
9802 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9803 * given iocb is for the SCSI target specified by vport and tgt_id
9804 * parameters.
9805 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9806 * given iocb is for the SCSI host associated with the given vport.
9807 * This function is called with no locks held.
9808 **/
dea31012005-04-17 16:05:31 -05009809static int
James Smart51ef4c22007-08-02 11:10:31 -04009810lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9811 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009812 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009813{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009814 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009815 int rc = 1;
9816
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009817 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9818 return rc;
9819
James Smart51ef4c22007-08-02 11:10:31 -04009820 if (iocbq->vport != vport)
9821 return rc;
9822
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009823 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009824
James Smart495a7142008-06-14 22:52:59 -04009825 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009826 return rc;
9827
9828 switch (ctx_cmd) {
9829 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009830 if ((lpfc_cmd->rdata->pnode) &&
9831 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9832 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009833 rc = 0;
9834 break;
9835 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009836 if ((lpfc_cmd->rdata->pnode) &&
9837 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009838 rc = 0;
9839 break;
dea31012005-04-17 16:05:31 -05009840 case LPFC_CTX_HOST:
9841 rc = 0;
9842 break;
9843 default:
9844 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009845 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009846 break;
9847 }
9848
9849 return rc;
9850}
9851
James Smarte59058c2008-08-24 21:49:00 -04009852/**
James Smart3621a712009-04-06 18:47:14 -04009853 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009854 * @vport: Pointer to virtual port.
9855 * @tgt_id: SCSI ID of the target.
9856 * @lun_id: LUN ID of the scsi device.
9857 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9858 *
9859 * This function returns number of FCP commands pending for the vport.
9860 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9861 * commands pending on the vport associated with SCSI device specified
9862 * by tgt_id and lun_id parameters.
9863 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9864 * commands pending on the vport associated with SCSI target specified
9865 * by tgt_id parameter.
9866 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9867 * commands pending on the vport.
9868 * This function returns the number of iocbs which satisfy the filter.
9869 * This function is called without any lock held.
9870 **/
dea31012005-04-17 16:05:31 -05009871int
James Smart51ef4c22007-08-02 11:10:31 -04009872lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9873 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009874{
James Smart51ef4c22007-08-02 11:10:31 -04009875 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009876 struct lpfc_iocbq *iocbq;
9877 int sum, i;
dea31012005-04-17 16:05:31 -05009878
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009879 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9880 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009881
James Smart51ef4c22007-08-02 11:10:31 -04009882 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9883 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009884 sum++;
dea31012005-04-17 16:05:31 -05009885 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009886
dea31012005-04-17 16:05:31 -05009887 return sum;
9888}
9889
James Smarte59058c2008-08-24 21:49:00 -04009890/**
James Smart3621a712009-04-06 18:47:14 -04009891 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009892 * @phba: Pointer to HBA context object
9893 * @cmdiocb: Pointer to command iocb object.
9894 * @rspiocb: Pointer to response iocb object.
9895 *
9896 * This function is called when an aborted FCP iocb completes. This
9897 * function is called by the ring event handler with no lock held.
9898 * This function frees the iocb.
9899 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009900void
James Smart2e0fef82007-06-17 19:56:36 -05009901lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9902 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009903{
James Smartcb69f7d2011-12-13 13:21:57 -05009904 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -04009905 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -05009906 "original iotag x%x, abort cmd iotag x%x "
9907 "status 0x%x, reason 0x%x\n",
9908 cmdiocb->iocb.un.acxri.abortContextTag,
9909 cmdiocb->iocb.un.acxri.abortIoTag,
9910 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9911 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009912 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009913 return;
9914}
9915
James Smarte59058c2008-08-24 21:49:00 -04009916/**
James Smart3621a712009-04-06 18:47:14 -04009917 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009918 * @vport: Pointer to virtual port.
9919 * @pring: Pointer to driver SLI ring object.
9920 * @tgt_id: SCSI ID of the target.
9921 * @lun_id: LUN ID of the scsi device.
9922 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9923 *
9924 * This function sends an abort command for every SCSI command
9925 * associated with the given virtual port pending on the ring
9926 * filtered by lpfc_sli_validate_fcp_iocb function.
9927 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9928 * FCP iocbs associated with lun specified by tgt_id and lun_id
9929 * parameters
9930 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9931 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9932 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9933 * FCP iocbs associated with virtual port.
9934 * This function returns number of iocbs it failed to abort.
9935 * This function is called with no locks held.
9936 **/
dea31012005-04-17 16:05:31 -05009937int
James Smart51ef4c22007-08-02 11:10:31 -04009938lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9939 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009940{
James Smart51ef4c22007-08-02 11:10:31 -04009941 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009942 struct lpfc_iocbq *iocbq;
9943 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009944 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009945 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009946 int i;
dea31012005-04-17 16:05:31 -05009947
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009948 for (i = 1; i <= phba->sli.last_iotag; i++) {
9949 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009950
James Smart51ef4c22007-08-02 11:10:31 -04009951 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009952 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009953 continue;
9954
James Smartafbd8d82013-09-06 12:22:13 -04009955 /*
9956 * If the iocbq is already being aborted, don't take a second
9957 * action, but do count it.
9958 */
9959 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
9960 continue;
9961
dea31012005-04-17 16:05:31 -05009962 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009963 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009964 if (abtsiocb == NULL) {
9965 errcnt++;
9966 continue;
9967 }
dea31012005-04-17 16:05:31 -05009968
James Smartafbd8d82013-09-06 12:22:13 -04009969 /* indicate the IO is being aborted by the driver. */
9970 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
9971
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009972 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009973 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9974 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009975 if (phba->sli_rev == LPFC_SLI_REV4)
9976 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9977 else
9978 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009979 abtsiocb->iocb.ulpLe = 1;
9980 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -04009981 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -05009982
James Smart5ffc2662009-11-18 15:39:44 -05009983 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9984 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009985 if (iocbq->iocb_flag & LPFC_IO_FCP)
9986 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009987
James Smart2e0fef82007-06-17 19:56:36 -05009988 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009989 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9990 else
9991 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9992
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009993 /* Setup callback routine and issue the command. */
9994 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009995 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9996 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009997 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009998 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009999 errcnt++;
10000 continue;
10001 }
10002 }
10003
10004 return errcnt;
10005}
10006
James Smarte59058c2008-08-24 21:49:00 -040010007/**
James Smart3621a712009-04-06 18:47:14 -040010008 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010009 * @phba: Pointer to HBA context object.
10010 * @cmdiocbq: Pointer to command iocb.
10011 * @rspiocbq: Pointer to response iocb.
10012 *
10013 * This function is the completion handler for iocbs issued using
10014 * lpfc_sli_issue_iocb_wait function. This function is called by the
10015 * ring event handler function without any lock held. This function
10016 * can be called from both worker thread context and interrupt
10017 * context. This function also can be called from other thread which
10018 * cleans up the SLI layer objects.
10019 * This function copy the contents of the response iocb to the
10020 * response iocb memory object provided by the caller of
10021 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10022 * sleeps for the iocb completion.
10023 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010024static void
10025lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10026 struct lpfc_iocbq *cmdiocbq,
10027 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010028{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010029 wait_queue_head_t *pdone_q;
10030 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010031 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010032
James Smart2e0fef82007-06-17 19:56:36 -050010033 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010034 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10035
10036 /*
10037 * A time out has occurred for the iocb. If a time out
10038 * completion handler has been supplied, call it. Otherwise,
10039 * just free the iocbq.
10040 */
10041
10042 spin_unlock_irqrestore(&phba->hbalock, iflags);
10043 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10044 cmdiocbq->wait_iocb_cmpl = NULL;
10045 if (cmdiocbq->iocb_cmpl)
10046 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10047 else
10048 lpfc_sli_release_iocbq(phba, cmdiocbq);
10049 return;
10050 }
10051
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010052 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10053 if (cmdiocbq->context2 && rspiocbq)
10054 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10055 &rspiocbq->iocb, sizeof(IOCB_t));
10056
James Smart0f65ff62010-02-26 14:14:23 -050010057 /* Set the exchange busy flag for task management commands */
10058 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10059 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10060 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10061 cur_iocbq);
10062 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10063 }
10064
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010065 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010066 if (pdone_q)
10067 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010068 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010069 return;
10070}
10071
James Smarte59058c2008-08-24 21:49:00 -040010072/**
James Smartd11e31d2009-06-10 17:23:06 -040010073 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10074 * @phba: Pointer to HBA context object..
10075 * @piocbq: Pointer to command iocb.
10076 * @flag: Flag to test.
10077 *
10078 * This routine grabs the hbalock and then test the iocb_flag to
10079 * see if the passed in flag is set.
10080 * Returns:
10081 * 1 if flag is set.
10082 * 0 if flag is not set.
10083 **/
10084static int
10085lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10086 struct lpfc_iocbq *piocbq, uint32_t flag)
10087{
10088 unsigned long iflags;
10089 int ret;
10090
10091 spin_lock_irqsave(&phba->hbalock, iflags);
10092 ret = piocbq->iocb_flag & flag;
10093 spin_unlock_irqrestore(&phba->hbalock, iflags);
10094 return ret;
10095
10096}
10097
10098/**
James Smart3621a712009-04-06 18:47:14 -040010099 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010100 * @phba: Pointer to HBA context object..
10101 * @pring: Pointer to sli ring.
10102 * @piocb: Pointer to command iocb.
10103 * @prspiocbq: Pointer to response iocb.
10104 * @timeout: Timeout in number of seconds.
10105 *
10106 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010107 * iocb to complete. The iocb_cmpl field of the shall be used
10108 * to handle iocbs which time out. If the field is NULL, the
10109 * function shall free the iocbq structure. If more clean up is
10110 * needed, the caller is expected to provide a completion function
10111 * that will provide the needed clean up. If the iocb command is
10112 * not completed within timeout seconds, the function will either
10113 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10114 * completion function set in the iocb_cmpl field and then return
10115 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10116 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010117 * The function waits for the iocb completion using an
10118 * non-interruptible wait.
10119 * This function will sleep while waiting for iocb completion.
10120 * So, this function should not be called from any context which
10121 * does not allow sleeping. Due to the same reason, this function
10122 * cannot be called with interrupt disabled.
10123 * This function assumes that the iocb completions occur while
10124 * this function sleep. So, this function cannot be called from
10125 * the thread which process iocb completion for this ring.
10126 * This function clears the iocb_flag of the iocb object before
10127 * issuing the iocb and the iocb completion handler sets this
10128 * flag and wakes this thread when the iocb completes.
10129 * The contents of the response iocb will be copied to prspiocbq
10130 * by the completion handler when the command completes.
10131 * This function returns IOCB_SUCCESS when success.
10132 * This function is called with no lock held.
10133 **/
dea31012005-04-17 16:05:31 -050010134int
James Smart2e0fef82007-06-17 19:56:36 -050010135lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010136 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010137 struct lpfc_iocbq *piocb,
10138 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010139 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010140{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010141 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010142 long timeleft, timeout_req = 0;
10143 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010144 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010145 struct lpfc_iocbq *iocb;
10146 int txq_cnt = 0;
10147 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010148 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010149 unsigned long iflags;
10150 bool iocb_completed = true;
10151
dea31012005-04-17 16:05:31 -050010152 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010153 * If the caller has provided a response iocbq buffer, then context2
10154 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010155 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010156 if (prspiocbq) {
10157 if (piocb->context2)
10158 return IOCB_ERROR;
10159 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010160 }
10161
James Smart5a0916b2013-07-15 18:31:42 -040010162 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010163 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10164 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010165 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010166
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010167 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010168 if (lpfc_readl(phba->HCregaddr, &creg_val))
10169 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010170 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10171 writel(creg_val, phba->HCregaddr);
10172 readl(phba->HCregaddr); /* flush */
10173 }
10174
James Smart2a9bf3d2010-06-07 15:24:45 -040010175 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10176 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010177 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010178 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010179 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010180 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010181 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010182 spin_lock_irqsave(&phba->hbalock, iflags);
10183 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010184
James Smart5a0916b2013-07-15 18:31:42 -040010185 /*
10186 * IOCB timed out. Inform the wake iocb wait
10187 * completion function and set local status
10188 */
10189
10190 iocb_completed = false;
10191 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10192 }
10193 spin_unlock_irqrestore(&phba->hbalock, iflags);
10194 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010195 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010196 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -040010197 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010198 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010199 "0338 IOCB wait timeout error - no "
10200 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010201 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010202 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010204 "0330 IOCB wake NOT set, "
10205 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010206 timeout, (timeleft / jiffies));
10207 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010208 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010209 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010210 if (phba->cfg_log_verbose & LOG_SLI) {
10211 list_for_each_entry(iocb, &pring->txq, list) {
10212 txq_cnt++;
10213 }
10214 list_for_each_entry(iocb, &pring->txcmplq, list) {
10215 txcmplq_cnt++;
10216 }
10217 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10218 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10219 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10220 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010221 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010222 } else {
10223 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010224 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010225 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010226 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010227 }
10228
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010229 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010230 if (lpfc_readl(phba->HCregaddr, &creg_val))
10231 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010232 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10233 writel(creg_val, phba->HCregaddr);
10234 readl(phba->HCregaddr); /* flush */
10235 }
10236
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010237 if (prspiocbq)
10238 piocb->context2 = NULL;
10239
10240 piocb->context_un.wait_queue = NULL;
10241 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010242 return retval;
10243}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010244
James Smarte59058c2008-08-24 21:49:00 -040010245/**
James Smart3621a712009-04-06 18:47:14 -040010246 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010247 * @phba: Pointer to HBA context object.
10248 * @pmboxq: Pointer to driver mailbox object.
10249 * @timeout: Timeout in number of seconds.
10250 *
10251 * This function issues the mailbox to firmware and waits for the
10252 * mailbox command to complete. If the mailbox command is not
10253 * completed within timeout seconds, it returns MBX_TIMEOUT.
10254 * The function waits for the mailbox completion using an
10255 * interruptible wait. If the thread is woken up due to a
10256 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10257 * should not free the mailbox resources, if this function returns
10258 * MBX_TIMEOUT.
10259 * This function will sleep while waiting for mailbox completion.
10260 * So, this function should not be called from any context which
10261 * does not allow sleeping. Due to the same reason, this function
10262 * cannot be called with interrupt disabled.
10263 * This function assumes that the mailbox completion occurs while
10264 * this function sleep. So, this function cannot be called from
10265 * the worker thread which processes mailbox completion.
10266 * This function is called in the context of HBA management
10267 * applications.
10268 * This function returns MBX_SUCCESS when successful.
10269 * This function is called with no lock held.
10270 **/
dea31012005-04-17 16:05:31 -050010271int
James Smart2e0fef82007-06-17 19:56:36 -050010272lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010273 uint32_t timeout)
10274{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010275 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010276 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010277 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010278 unsigned long flag;
dea31012005-04-17 16:05:31 -050010279
James Smartb230b8a2013-05-31 17:05:27 -040010280 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010281 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010282 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010283
James Smart495a7142008-06-14 22:52:59 -040010284 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010285 /* setup wake call as IOCB callback */
10286 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10287 /* setup context field to pass wait_queue pointer to wake function */
10288 pmboxq->context1 = &done_q;
10289
dea31012005-04-17 16:05:31 -050010290 /* now issue the command */
10291 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010292 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010293 wait_event_interruptible_timeout(done_q,
10294 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010295 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010296
James Smart858c9f62007-06-17 19:56:39 -050010297 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010298 /* restore the possible extended buffer for free resource */
10299 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010300 /*
10301 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10302 * else do not free the resources.
10303 */
James Smartd7c47992010-06-08 18:31:54 -040010304 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010305 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010306 } else {
James Smart7054a602007-04-25 09:52:34 -040010307 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010308 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10309 }
10310 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010311 } else {
10312 /* restore the possible extended buffer for free resource */
10313 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010314 }
10315
dea31012005-04-17 16:05:31 -050010316 return retval;
10317}
10318
James Smarte59058c2008-08-24 21:49:00 -040010319/**
James Smart3772a992009-05-22 14:50:54 -040010320 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010321 * @phba: Pointer to HBA context.
10322 *
James Smart3772a992009-05-22 14:50:54 -040010323 * This function is called to shutdown the driver's mailbox sub-system.
10324 * It first marks the mailbox sub-system is in a block state to prevent
10325 * the asynchronous mailbox command from issued off the pending mailbox
10326 * command queue. If the mailbox command sub-system shutdown is due to
10327 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10328 * the mailbox sub-system flush routine to forcefully bring down the
10329 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10330 * as with offline or HBA function reset), this routine will wait for the
10331 * outstanding mailbox command to complete before invoking the mailbox
10332 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010333 **/
James Smart3772a992009-05-22 14:50:54 -040010334void
James Smart618a5232012-06-12 13:54:36 -040010335lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010336{
James Smart3772a992009-05-22 14:50:54 -040010337 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010338 unsigned long timeout;
10339
James Smart618a5232012-06-12 13:54:36 -040010340 if (mbx_action == LPFC_MBX_NO_WAIT) {
10341 /* delay 100ms for port state */
10342 msleep(100);
10343 lpfc_sli_mbox_sys_flush(phba);
10344 return;
10345 }
James Smarta183a152011-10-10 21:32:43 -040010346 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010347
James Smart3772a992009-05-22 14:50:54 -040010348 spin_lock_irq(&phba->hbalock);
10349 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010350
10351 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010352 /* Determine how long we might wait for the active mailbox
10353 * command to be gracefully completed by firmware.
10354 */
James Smarta183a152011-10-10 21:32:43 -040010355 if (phba->sli.mbox_active)
10356 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10357 phba->sli.mbox_active) *
10358 1000) + jiffies;
10359 spin_unlock_irq(&phba->hbalock);
10360
James Smart3772a992009-05-22 14:50:54 -040010361 while (phba->sli.mbox_active) {
10362 /* Check active mailbox complete status every 2ms */
10363 msleep(2);
10364 if (time_after(jiffies, timeout))
10365 /* Timeout, let the mailbox flush routine to
10366 * forcefully release active mailbox command
10367 */
10368 break;
10369 }
James Smartd7069f02012-03-01 22:36:29 -050010370 } else
10371 spin_unlock_irq(&phba->hbalock);
10372
James Smart3772a992009-05-22 14:50:54 -040010373 lpfc_sli_mbox_sys_flush(phba);
10374}
10375
10376/**
10377 * lpfc_sli_eratt_read - read sli-3 error attention events
10378 * @phba: Pointer to HBA context.
10379 *
10380 * This function is called to read the SLI3 device error attention registers
10381 * for possible error attention events. The caller must hold the hostlock
10382 * with spin_lock_irq().
10383 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010384 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010385 * Register and returns 0 otherwise.
10386 **/
10387static int
10388lpfc_sli_eratt_read(struct lpfc_hba *phba)
10389{
James Smarted957682007-06-17 19:56:37 -050010390 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010391
James Smart3772a992009-05-22 14:50:54 -040010392 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010393 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10394 goto unplug_err;
10395
James Smart3772a992009-05-22 14:50:54 -040010396 if (ha_copy & HA_ERATT) {
10397 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010398 if (lpfc_sli_read_hs(phba))
10399 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010400
James Smart3772a992009-05-22 14:50:54 -040010401 /* Check if there is a deferred error condition is active */
10402 if ((HS_FFER1 & phba->work_hs) &&
10403 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010404 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010405 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010406 /* Clear all interrupt enable conditions */
10407 writel(0, phba->HCregaddr);
10408 readl(phba->HCregaddr);
10409 }
10410
10411 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010412 phba->work_ha |= HA_ERATT;
10413 /* Indicate polling handles this ERATT */
10414 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010415 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010416 }
James Smart3772a992009-05-22 14:50:54 -040010417 return 0;
James Smart9940b972011-03-11 16:06:12 -050010418
10419unplug_err:
10420 /* Set the driver HS work bitmap */
10421 phba->work_hs |= UNPLUG_ERR;
10422 /* Set the driver HA work bitmap */
10423 phba->work_ha |= HA_ERATT;
10424 /* Indicate polling handles this ERATT */
10425 phba->hba_flag |= HBA_ERATT_HANDLED;
10426 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010427}
10428
James Smarte59058c2008-08-24 21:49:00 -040010429/**
James Smartda0436e2009-05-22 14:51:39 -040010430 * lpfc_sli4_eratt_read - read sli-4 error attention events
10431 * @phba: Pointer to HBA context.
10432 *
10433 * This function is called to read the SLI4 device error attention registers
10434 * for possible error attention events. The caller must hold the hostlock
10435 * with spin_lock_irq().
10436 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010437 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010438 * Register and returns 0 otherwise.
10439 **/
10440static int
10441lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10442{
10443 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010444 uint32_t if_type, portsmphr;
10445 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010446
James Smart2fcee4b2010-12-15 17:57:46 -050010447 /*
10448 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010449 * registers for error attention. This can be changed later.
10450 */
James Smart2fcee4b2010-12-15 17:57:46 -050010451 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10452 switch (if_type) {
10453 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010454 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10455 &uerr_sta_lo) ||
10456 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10457 &uerr_sta_hi)) {
10458 phba->work_hs |= UNPLUG_ERR;
10459 phba->work_ha |= HA_ERATT;
10460 phba->hba_flag |= HBA_ERATT_HANDLED;
10461 return 1;
10462 }
James Smart2fcee4b2010-12-15 17:57:46 -050010463 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10464 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10466 "1423 HBA Unrecoverable error: "
10467 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10468 "ue_mask_lo_reg=0x%x, "
10469 "ue_mask_hi_reg=0x%x\n",
10470 uerr_sta_lo, uerr_sta_hi,
10471 phba->sli4_hba.ue_mask_lo,
10472 phba->sli4_hba.ue_mask_hi);
10473 phba->work_status[0] = uerr_sta_lo;
10474 phba->work_status[1] = uerr_sta_hi;
10475 phba->work_ha |= HA_ERATT;
10476 phba->hba_flag |= HBA_ERATT_HANDLED;
10477 return 1;
10478 }
10479 break;
10480 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010481 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10482 &portstat_reg.word0) ||
10483 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10484 &portsmphr)){
10485 phba->work_hs |= UNPLUG_ERR;
10486 phba->work_ha |= HA_ERATT;
10487 phba->hba_flag |= HBA_ERATT_HANDLED;
10488 return 1;
10489 }
James Smart2fcee4b2010-12-15 17:57:46 -050010490 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10491 phba->work_status[0] =
10492 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10493 phba->work_status[1] =
10494 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010496 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010497 "port status reg 0x%x, "
10498 "port smphr reg 0x%x, "
10499 "error 1=0x%x, error 2=0x%x\n",
10500 portstat_reg.word0,
10501 portsmphr,
10502 phba->work_status[0],
10503 phba->work_status[1]);
10504 phba->work_ha |= HA_ERATT;
10505 phba->hba_flag |= HBA_ERATT_HANDLED;
10506 return 1;
10507 }
10508 break;
10509 case LPFC_SLI_INTF_IF_TYPE_1:
10510 default:
James Smarta747c9c2009-11-18 15:41:10 -050010511 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010512 "2886 HBA Error Attention on unsupported "
10513 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010514 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010515 }
James Smart2fcee4b2010-12-15 17:57:46 -050010516
James Smartda0436e2009-05-22 14:51:39 -040010517 return 0;
10518}
10519
10520/**
James Smart3621a712009-04-06 18:47:14 -040010521 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010522 * @phba: Pointer to HBA context.
10523 *
James Smart3772a992009-05-22 14:50:54 -040010524 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010525 * error attention register bit for error attention events.
10526 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010527 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010528 * Register and returns 0 otherwise.
10529 **/
10530int
10531lpfc_sli_check_eratt(struct lpfc_hba *phba)
10532{
10533 uint32_t ha_copy;
10534
10535 /* If somebody is waiting to handle an eratt, don't process it
10536 * here. The brdkill function will do this.
10537 */
10538 if (phba->link_flag & LS_IGNORE_ERATT)
10539 return 0;
10540
10541 /* Check if interrupt handler handles this ERATT */
10542 spin_lock_irq(&phba->hbalock);
10543 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10544 /* Interrupt handler has handled ERATT */
10545 spin_unlock_irq(&phba->hbalock);
10546 return 0;
10547 }
10548
James Smarta257bf92009-04-06 18:48:10 -040010549 /*
10550 * If there is deferred error attention, do not check for error
10551 * attention
10552 */
10553 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10554 spin_unlock_irq(&phba->hbalock);
10555 return 0;
10556 }
10557
James Smart3772a992009-05-22 14:50:54 -040010558 /* If PCI channel is offline, don't process it */
10559 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010560 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010561 return 0;
10562 }
10563
10564 switch (phba->sli_rev) {
10565 case LPFC_SLI_REV2:
10566 case LPFC_SLI_REV3:
10567 /* Read chip Host Attention (HA) register */
10568 ha_copy = lpfc_sli_eratt_read(phba);
10569 break;
James Smartda0436e2009-05-22 14:51:39 -040010570 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010571 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010572 ha_copy = lpfc_sli4_eratt_read(phba);
10573 break;
James Smart3772a992009-05-22 14:50:54 -040010574 default:
10575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10576 "0299 Invalid SLI revision (%d)\n",
10577 phba->sli_rev);
10578 ha_copy = 0;
10579 break;
James Smart93996272008-08-24 21:50:30 -040010580 }
10581 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010582
10583 return ha_copy;
10584}
10585
10586/**
10587 * lpfc_intr_state_check - Check device state for interrupt handling
10588 * @phba: Pointer to HBA context.
10589 *
10590 * This inline routine checks whether a device or its PCI slot is in a state
10591 * that the interrupt should be handled.
10592 *
10593 * This function returns 0 if the device or the PCI slot is in a state that
10594 * interrupt should be handled, otherwise -EIO.
10595 */
10596static inline int
10597lpfc_intr_state_check(struct lpfc_hba *phba)
10598{
10599 /* If the pci channel is offline, ignore all the interrupts */
10600 if (unlikely(pci_channel_offline(phba->pcidev)))
10601 return -EIO;
10602
10603 /* Update device level interrupt statistics */
10604 phba->sli.slistat.sli_intr++;
10605
10606 /* Ignore all interrupts during initialization. */
10607 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10608 return -EIO;
10609
James Smart93996272008-08-24 21:50:30 -040010610 return 0;
10611}
10612
10613/**
James Smart3772a992009-05-22 14:50:54 -040010614 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010615 * @irq: Interrupt number.
10616 * @dev_id: The device context pointer.
10617 *
James Smart93996272008-08-24 21:50:30 -040010618 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010619 * service routine when device with SLI-3 interface spec is enabled with
10620 * MSI-X multi-message interrupt mode and there are slow-path events in
10621 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10622 * interrupt mode, this function is called as part of the device-level
10623 * interrupt handler. When the PCI slot is in error recovery or the HBA
10624 * is undergoing initialization, the interrupt handler will not process
10625 * the interrupt. The link attention and ELS ring attention events are
10626 * handled by the worker thread. The interrupt handler signals the worker
10627 * thread and returns for these events. This function is called without
10628 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010629 * structures.
10630 *
10631 * This function returns IRQ_HANDLED when interrupt is handled else it
10632 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010633 **/
dea31012005-04-17 16:05:31 -050010634irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010635lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010636{
James Smart2e0fef82007-06-17 19:56:36 -050010637 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010638 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010639 uint32_t work_ha_copy;
10640 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010641 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010642 uint32_t control;
10643
James Smart92d7f7b2007-06-17 19:56:38 -050010644 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010645 struct lpfc_vport *vport;
10646 struct lpfc_nodelist *ndlp;
10647 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010648 LPFC_MBOXQ_t *pmb;
10649 int rc;
10650
dea31012005-04-17 16:05:31 -050010651 /*
10652 * Get the driver's phba structure from the dev_id and
10653 * assume the HBA is not interrupting.
10654 */
James Smart93996272008-08-24 21:50:30 -040010655 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010656
10657 if (unlikely(!phba))
10658 return IRQ_NONE;
10659
dea31012005-04-17 16:05:31 -050010660 /*
James Smart93996272008-08-24 21:50:30 -040010661 * Stuff needs to be attented to when this function is invoked as an
10662 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010663 */
James Smart93996272008-08-24 21:50:30 -040010664 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010665 /* Check device state for handling interrupt */
10666 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010667 return IRQ_NONE;
10668 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010669 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010670 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10671 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010672 /* If somebody is waiting to handle an eratt don't process it
10673 * here. The brdkill function will do this.
10674 */
10675 if (phba->link_flag & LS_IGNORE_ERATT)
10676 ha_copy &= ~HA_ERATT;
10677 /* Check the need for handling ERATT in interrupt handler */
10678 if (ha_copy & HA_ERATT) {
10679 if (phba->hba_flag & HBA_ERATT_HANDLED)
10680 /* ERATT polling has handled ERATT */
10681 ha_copy &= ~HA_ERATT;
10682 else
10683 /* Indicate interrupt handler handles ERATT */
10684 phba->hba_flag |= HBA_ERATT_HANDLED;
10685 }
James Smarta257bf92009-04-06 18:48:10 -040010686
10687 /*
10688 * If there is deferred error attention, do not check for any
10689 * interrupt.
10690 */
10691 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010692 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010693 return IRQ_NONE;
10694 }
10695
James Smart93996272008-08-24 21:50:30 -040010696 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010697 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10698 goto unplug_error;
10699
James Smarta747c9c2009-11-18 15:41:10 -050010700 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10701 HC_LAINT_ENA | HC_ERINT_ENA),
10702 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010703 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10704 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010705 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010706 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010707 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010708 } else
10709 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010710
dea31012005-04-17 16:05:31 -050010711 work_ha_copy = ha_copy & phba->work_ha_mask;
10712
James Smart93996272008-08-24 21:50:30 -040010713 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010714 if (work_ha_copy & HA_LATT) {
10715 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10716 /*
10717 * Turn off Link Attention interrupts
10718 * until CLEAR_LA done
10719 */
James Smart5b75da22008-12-04 22:39:35 -050010720 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010721 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010722 if (lpfc_readl(phba->HCregaddr, &control))
10723 goto unplug_error;
dea31012005-04-17 16:05:31 -050010724 control &= ~HC_LAINT_ENA;
10725 writel(control, phba->HCregaddr);
10726 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010727 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010728 }
10729 else
10730 work_ha_copy &= ~HA_LATT;
10731 }
10732
James Smart93996272008-08-24 21:50:30 -040010733 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010734 /*
10735 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10736 * the only slow ring.
10737 */
10738 status = (work_ha_copy &
10739 (HA_RXMASK << (4*LPFC_ELS_RING)));
10740 status >>= (4*LPFC_ELS_RING);
10741 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010742 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010743 if (lpfc_readl(phba->HCregaddr, &control))
10744 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010745
10746 lpfc_debugfs_slow_ring_trc(phba,
10747 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10748 control, status,
10749 (uint32_t)phba->sli.slistat.sli_intr);
10750
James Smart858c9f62007-06-17 19:56:39 -050010751 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010752 lpfc_debugfs_slow_ring_trc(phba,
10753 "ISR Disable ring:"
10754 "pwork:x%x hawork:x%x wait:x%x",
10755 phba->work_ha, work_ha_copy,
10756 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010757 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010758
James Smart858c9f62007-06-17 19:56:39 -050010759 control &=
10760 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010761 writel(control, phba->HCregaddr);
10762 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010763 }
James Smarta58cbd52007-08-02 11:09:43 -040010764 else {
10765 lpfc_debugfs_slow_ring_trc(phba,
10766 "ISR slow ring: pwork:"
10767 "x%x hawork:x%x wait:x%x",
10768 phba->work_ha, work_ha_copy,
10769 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010770 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010771 }
James Smart5b75da22008-12-04 22:39:35 -050010772 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010773 }
10774 }
James Smart5b75da22008-12-04 22:39:35 -050010775 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010776 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010777 if (lpfc_sli_read_hs(phba))
10778 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010779 /*
10780 * Check if there is a deferred error condition
10781 * is active
10782 */
10783 if ((HS_FFER1 & phba->work_hs) &&
10784 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010785 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10786 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010787 phba->hba_flag |= DEFER_ERATT;
10788 /* Clear all interrupt enable conditions */
10789 writel(0, phba->HCregaddr);
10790 readl(phba->HCregaddr);
10791 }
10792 }
10793
James Smart93996272008-08-24 21:50:30 -040010794 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010795 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010796 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010797 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010798 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010799
10800 /* First check out the status word */
10801 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10802 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010803 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010804 /*
10805 * Stray Mailbox Interrupt, mbxCommand <cmd>
10806 * mbxStatus <status>
10807 */
James Smart09372822008-01-11 01:52:54 -050010808 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010809 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010810 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010811 "Interrupt mbxCommand x%x "
10812 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010813 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010814 pmbox->mbxCommand,
10815 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010816 /* clear mailbox attention bit */
10817 work_ha_copy &= ~HA_MBATT;
10818 } else {
James Smart97eab632008-04-07 10:16:05 -040010819 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010820 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010821 phba->last_completion_time = jiffies;
10822 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010823 if (pmb->mbox_cmpl) {
10824 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10825 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010826 if (pmb->out_ext_byte_len &&
10827 pmb->context2)
10828 lpfc_sli_pcimem_bcopy(
10829 phba->mbox_ext,
10830 pmb->context2,
10831 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010832 }
James Smart09372822008-01-11 01:52:54 -050010833 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10834 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10835
10836 lpfc_debugfs_disc_trc(vport,
10837 LPFC_DISC_TRC_MBOX_VPORT,
10838 "MBOX dflt rpi: : "
10839 "status:x%x rpi:x%x",
10840 (uint32_t)pmbox->mbxStatus,
10841 pmbox->un.varWords[0], 0);
10842
10843 if (!pmbox->mbxStatus) {
10844 mp = (struct lpfc_dmabuf *)
10845 (pmb->context1);
10846 ndlp = (struct lpfc_nodelist *)
10847 pmb->context2;
10848
10849 /* Reg_LOGIN of dflt RPI was
10850 * successful. new lets get
10851 * rid of the RPI using the
10852 * same mbox buffer.
10853 */
10854 lpfc_unreg_login(phba,
10855 vport->vpi,
10856 pmbox->un.varWords[0],
10857 pmb);
10858 pmb->mbox_cmpl =
10859 lpfc_mbx_cmpl_dflt_rpi;
10860 pmb->context1 = mp;
10861 pmb->context2 = ndlp;
10862 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010863 rc = lpfc_sli_issue_mbox(phba,
10864 pmb,
10865 MBX_NOWAIT);
10866 if (rc != MBX_BUSY)
10867 lpfc_printf_log(phba,
10868 KERN_ERR,
10869 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010870 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010871 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010872 if (rc != MBX_NOT_FINISHED)
10873 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010874 }
10875 }
James Smart5b75da22008-12-04 22:39:35 -050010876 spin_lock_irqsave(
10877 &phba->pport->work_port_lock,
10878 iflag);
James Smart09372822008-01-11 01:52:54 -050010879 phba->pport->work_port_events &=
10880 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010881 spin_unlock_irqrestore(
10882 &phba->pport->work_port_lock,
10883 iflag);
James Smart09372822008-01-11 01:52:54 -050010884 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010885 }
James Smart97eab632008-04-07 10:16:05 -040010886 } else
James Smart5b75da22008-12-04 22:39:35 -050010887 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010888
James Smart92d7f7b2007-06-17 19:56:38 -050010889 if ((work_ha_copy & HA_MBATT) &&
10890 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010891send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010892 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010893 do {
10894 rc = lpfc_sli_issue_mbox(phba, NULL,
10895 MBX_NOWAIT);
10896 } while (rc == MBX_NOT_FINISHED);
10897 if (rc != MBX_SUCCESS)
10898 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10899 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010900 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010901 }
10902
James Smart5b75da22008-12-04 22:39:35 -050010903 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010904 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010905 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010906 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010907 }
James Smart93996272008-08-24 21:50:30 -040010908 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010909unplug_error:
10910 spin_unlock_irqrestore(&phba->hbalock, iflag);
10911 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010912
James Smart3772a992009-05-22 14:50:54 -040010913} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010914
10915/**
James Smart3772a992009-05-22 14:50:54 -040010916 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010917 * @irq: Interrupt number.
10918 * @dev_id: The device context pointer.
10919 *
10920 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010921 * service routine when device with SLI-3 interface spec is enabled with
10922 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10923 * ring event in the HBA. However, when the device is enabled with either
10924 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10925 * device-level interrupt handler. When the PCI slot is in error recovery
10926 * or the HBA is undergoing initialization, the interrupt handler will not
10927 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10928 * the intrrupt context. This function is called without any lock held.
10929 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010930 *
10931 * This function returns IRQ_HANDLED when interrupt is handled else it
10932 * returns IRQ_NONE.
10933 **/
10934irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010935lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010936{
10937 struct lpfc_hba *phba;
10938 uint32_t ha_copy;
10939 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010940 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010941
10942 /* Get the driver's phba structure from the dev_id and
10943 * assume the HBA is not interrupting.
10944 */
10945 phba = (struct lpfc_hba *) dev_id;
10946
10947 if (unlikely(!phba))
10948 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010949
10950 /*
James Smart93996272008-08-24 21:50:30 -040010951 * Stuff needs to be attented to when this function is invoked as an
10952 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010953 */
James Smart93996272008-08-24 21:50:30 -040010954 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010955 /* Check device state for handling interrupt */
10956 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010957 return IRQ_NONE;
10958 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010959 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10960 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010961 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010962 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010963 /*
10964 * If there is deferred error attention, do not check for
10965 * any interrupt.
10966 */
10967 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010968 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010969 return IRQ_NONE;
10970 }
James Smart93996272008-08-24 21:50:30 -040010971 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10972 phba->HAregaddr);
10973 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010974 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010975 } else
10976 ha_copy = phba->ha_copy;
10977
10978 /*
10979 * Process all events on FCP ring. Take the optimized path for FCP IO.
10980 */
10981 ha_copy &= ~(phba->work_ha_mask);
10982
10983 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010984 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010985 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010986 lpfc_sli_handle_fast_ring_event(phba,
10987 &phba->sli.ring[LPFC_FCP_RING],
10988 status);
James Smarta4bc3372006-12-02 13:34:16 -050010989
10990 if (phba->cfg_multi_ring_support == 2) {
10991 /*
James Smart93996272008-08-24 21:50:30 -040010992 * Process all events on extra ring. Take the optimized path
10993 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010994 */
James Smart93996272008-08-24 21:50:30 -040010995 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010996 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010997 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010998 lpfc_sli_handle_fast_ring_event(phba,
10999 &phba->sli.ring[LPFC_EXTRA_RING],
11000 status);
11001 }
11002 }
dea31012005-04-17 16:05:31 -050011003 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011004} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011005
James Smart93996272008-08-24 21:50:30 -040011006/**
James Smart3772a992009-05-22 14:50:54 -040011007 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011008 * @irq: Interrupt number.
11009 * @dev_id: The device context pointer.
11010 *
James Smart3772a992009-05-22 14:50:54 -040011011 * This function is the HBA device-level interrupt handler to device with
11012 * SLI-3 interface spec, called from the PCI layer when either MSI or
11013 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11014 * requires driver attention. This function invokes the slow-path interrupt
11015 * attention handling function and fast-path interrupt attention handling
11016 * function in turn to process the relevant HBA attention events. This
11017 * function is called without any lock held. It gets the hbalock to access
11018 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011019 *
11020 * This function returns IRQ_HANDLED when interrupt is handled, else it
11021 * returns IRQ_NONE.
11022 **/
11023irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011024lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011025{
11026 struct lpfc_hba *phba;
11027 irqreturn_t sp_irq_rc, fp_irq_rc;
11028 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011029 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011030
11031 /*
11032 * Get the driver's phba structure from the dev_id and
11033 * assume the HBA is not interrupting.
11034 */
11035 phba = (struct lpfc_hba *) dev_id;
11036
11037 if (unlikely(!phba))
11038 return IRQ_NONE;
11039
James Smart3772a992009-05-22 14:50:54 -040011040 /* Check device state for handling interrupt */
11041 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011042 return IRQ_NONE;
11043
11044 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011045 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11046 spin_unlock(&phba->hbalock);
11047 return IRQ_HANDLED;
11048 }
11049
James Smart93996272008-08-24 21:50:30 -040011050 if (unlikely(!phba->ha_copy)) {
11051 spin_unlock(&phba->hbalock);
11052 return IRQ_NONE;
11053 } else if (phba->ha_copy & HA_ERATT) {
11054 if (phba->hba_flag & HBA_ERATT_HANDLED)
11055 /* ERATT polling has handled ERATT */
11056 phba->ha_copy &= ~HA_ERATT;
11057 else
11058 /* Indicate interrupt handler handles ERATT */
11059 phba->hba_flag |= HBA_ERATT_HANDLED;
11060 }
11061
James Smarta257bf92009-04-06 18:48:10 -040011062 /*
11063 * If there is deferred error attention, do not check for any interrupt.
11064 */
11065 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011066 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011067 return IRQ_NONE;
11068 }
11069
James Smart93996272008-08-24 21:50:30 -040011070 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011071 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11072 spin_unlock(&phba->hbalock);
11073 return IRQ_HANDLED;
11074 }
James Smarta747c9c2009-11-18 15:41:10 -050011075 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11076 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11077 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011078 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011079 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011080 readl(phba->HAregaddr); /* flush */
11081 spin_unlock(&phba->hbalock);
11082
11083 /*
11084 * Invokes slow-path host attention interrupt handling as appropriate.
11085 */
11086
11087 /* status of events with mailbox and link attention */
11088 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11089
11090 /* status of events with ELS ring */
11091 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11092 status2 >>= (4*LPFC_ELS_RING);
11093
11094 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011095 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011096 else
11097 sp_irq_rc = IRQ_NONE;
11098
11099 /*
11100 * Invoke fast-path host attention interrupt handling as appropriate.
11101 */
11102
11103 /* status of events with FCP ring */
11104 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11105 status1 >>= (4*LPFC_FCP_RING);
11106
11107 /* status of events with extra ring */
11108 if (phba->cfg_multi_ring_support == 2) {
11109 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11110 status2 >>= (4*LPFC_EXTRA_RING);
11111 } else
11112 status2 = 0;
11113
11114 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011115 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011116 else
11117 fp_irq_rc = IRQ_NONE;
11118
11119 /* Return device-level interrupt handling status */
11120 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011121} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011122
11123/**
11124 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11125 * @phba: pointer to lpfc hba data structure.
11126 *
11127 * This routine is invoked by the worker thread to process all the pending
11128 * SLI4 FCP abort XRI events.
11129 **/
11130void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11131{
11132 struct lpfc_cq_event *cq_event;
11133
11134 /* First, declare the fcp xri abort event has been handled */
11135 spin_lock_irq(&phba->hbalock);
11136 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11137 spin_unlock_irq(&phba->hbalock);
11138 /* Now, handle all the fcp xri abort events */
11139 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11140 /* Get the first event from the head of the event queue */
11141 spin_lock_irq(&phba->hbalock);
11142 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11143 cq_event, struct lpfc_cq_event, list);
11144 spin_unlock_irq(&phba->hbalock);
11145 /* Notify aborted XRI for FCP work queue */
11146 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11147 /* Free the event processed back to the free pool */
11148 lpfc_sli4_cq_event_release(phba, cq_event);
11149 }
11150}
11151
11152/**
11153 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11154 * @phba: pointer to lpfc hba data structure.
11155 *
11156 * This routine is invoked by the worker thread to process all the pending
11157 * SLI4 els abort xri events.
11158 **/
11159void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11160{
11161 struct lpfc_cq_event *cq_event;
11162
11163 /* First, declare the els xri abort event has been handled */
11164 spin_lock_irq(&phba->hbalock);
11165 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11166 spin_unlock_irq(&phba->hbalock);
11167 /* Now, handle all the els xri abort events */
11168 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11169 /* Get the first event from the head of the event queue */
11170 spin_lock_irq(&phba->hbalock);
11171 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11172 cq_event, struct lpfc_cq_event, list);
11173 spin_unlock_irq(&phba->hbalock);
11174 /* Notify aborted XRI for ELS work queue */
11175 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11176 /* Free the event processed back to the free pool */
11177 lpfc_sli4_cq_event_release(phba, cq_event);
11178 }
11179}
11180
James Smart341af102010-01-26 23:07:37 -050011181/**
11182 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11183 * @phba: pointer to lpfc hba data structure
11184 * @pIocbIn: pointer to the rspiocbq
11185 * @pIocbOut: pointer to the cmdiocbq
11186 * @wcqe: pointer to the complete wcqe
11187 *
11188 * This routine transfers the fields of a command iocbq to a response iocbq
11189 * by copying all the IOCB fields from command iocbq and transferring the
11190 * completion status information from the complete wcqe.
11191 **/
James Smart4f774512009-05-22 14:52:35 -040011192static void
James Smart341af102010-01-26 23:07:37 -050011193lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11194 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011195 struct lpfc_iocbq *pIocbOut,
11196 struct lpfc_wcqe_complete *wcqe)
11197{
James Smart341af102010-01-26 23:07:37 -050011198 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050011199 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040011200 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11201
11202 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11203 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011204 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011205 status = bf_get(lpfc_wcqe_c_status, wcqe);
11206 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011207 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11208 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11209 pIocbIn->iocb.un.fcpi.fcpi_parm =
11210 pIocbOut->iocb.un.fcpi.fcpi_parm -
11211 wcqe->total_data_placed;
11212 else
11213 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011214 else {
James Smart4f774512009-05-22 14:52:35 -040011215 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011216 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
11217 }
James Smart341af102010-01-26 23:07:37 -050011218
James Smartacd68592012-01-18 16:25:09 -050011219 /* Convert BG errors for completion status */
11220 if (status == CQE_STATUS_DI_ERROR) {
11221 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11222
11223 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11224 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11225 else
11226 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11227
11228 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11229 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11230 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11231 BGS_GUARD_ERR_MASK;
11232 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11233 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11234 BGS_APPTAG_ERR_MASK;
11235 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11236 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11237 BGS_REFTAG_ERR_MASK;
11238
11239 /* Check to see if there was any good data before the error */
11240 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11241 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11242 BGS_HI_WATER_MARK_PRESENT_MASK;
11243 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11244 wcqe->total_data_placed;
11245 }
11246
11247 /*
11248 * Set ALL the error bits to indicate we don't know what
11249 * type of error it is.
11250 */
11251 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11252 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11253 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11254 BGS_GUARD_ERR_MASK);
11255 }
11256
James Smart341af102010-01-26 23:07:37 -050011257 /* Pick up HBA exchange busy condition */
11258 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11259 spin_lock_irqsave(&phba->hbalock, iflags);
11260 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11261 spin_unlock_irqrestore(&phba->hbalock, iflags);
11262 }
James Smart4f774512009-05-22 14:52:35 -040011263}
11264
11265/**
James Smart45ed1192009-10-02 15:17:02 -040011266 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11267 * @phba: Pointer to HBA context object.
11268 * @wcqe: Pointer to work-queue completion queue entry.
11269 *
11270 * This routine handles an ELS work-queue completion event and construct
11271 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11272 * discovery engine to handle.
11273 *
11274 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11275 **/
11276static struct lpfc_iocbq *
11277lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11278 struct lpfc_iocbq *irspiocbq)
11279{
11280 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11281 struct lpfc_iocbq *cmdiocbq;
11282 struct lpfc_wcqe_complete *wcqe;
11283 unsigned long iflags;
11284
11285 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011286 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011287 pring->stats.iocb_event++;
11288 /* Look up the ELS command IOCB and create pseudo response IOCB */
11289 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11290 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011291 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011292
11293 if (unlikely(!cmdiocbq)) {
11294 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11295 "0386 ELS complete with no corresponding "
11296 "cmdiocb: iotag (%d)\n",
11297 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11298 lpfc_sli_release_iocbq(phba, irspiocbq);
11299 return NULL;
11300 }
11301
11302 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011303 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011304
11305 return irspiocbq;
11306}
11307
11308/**
James Smart04c68492009-05-22 14:52:52 -040011309 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11310 * @phba: Pointer to HBA context object.
11311 * @cqe: Pointer to mailbox completion queue entry.
11312 *
11313 * This routine process a mailbox completion queue entry with asynchrous
11314 * event.
11315 *
11316 * Return: true if work posted to worker thread, otherwise false.
11317 **/
11318static bool
11319lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11320{
11321 struct lpfc_cq_event *cq_event;
11322 unsigned long iflags;
11323
11324 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11325 "0392 Async Event: word0:x%x, word1:x%x, "
11326 "word2:x%x, word3:x%x\n", mcqe->word0,
11327 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11328
11329 /* Allocate a new internal CQ_EVENT entry */
11330 cq_event = lpfc_sli4_cq_event_alloc(phba);
11331 if (!cq_event) {
11332 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11333 "0394 Failed to allocate CQ_EVENT entry\n");
11334 return false;
11335 }
11336
11337 /* Move the CQE into an asynchronous event entry */
11338 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11339 spin_lock_irqsave(&phba->hbalock, iflags);
11340 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11341 /* Set the async event flag */
11342 phba->hba_flag |= ASYNC_EVENT;
11343 spin_unlock_irqrestore(&phba->hbalock, iflags);
11344
11345 return true;
11346}
11347
11348/**
11349 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11350 * @phba: Pointer to HBA context object.
11351 * @cqe: Pointer to mailbox completion queue entry.
11352 *
11353 * This routine process a mailbox completion queue entry with mailbox
11354 * completion event.
11355 *
11356 * Return: true if work posted to worker thread, otherwise false.
11357 **/
11358static bool
11359lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11360{
11361 uint32_t mcqe_status;
11362 MAILBOX_t *mbox, *pmbox;
11363 struct lpfc_mqe *mqe;
11364 struct lpfc_vport *vport;
11365 struct lpfc_nodelist *ndlp;
11366 struct lpfc_dmabuf *mp;
11367 unsigned long iflags;
11368 LPFC_MBOXQ_t *pmb;
11369 bool workposted = false;
11370 int rc;
11371
11372 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11373 if (!bf_get(lpfc_trailer_completed, mcqe))
11374 goto out_no_mqe_complete;
11375
11376 /* Get the reference to the active mbox command */
11377 spin_lock_irqsave(&phba->hbalock, iflags);
11378 pmb = phba->sli.mbox_active;
11379 if (unlikely(!pmb)) {
11380 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11381 "1832 No pending MBOX command to handle\n");
11382 spin_unlock_irqrestore(&phba->hbalock, iflags);
11383 goto out_no_mqe_complete;
11384 }
11385 spin_unlock_irqrestore(&phba->hbalock, iflags);
11386 mqe = &pmb->u.mqe;
11387 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11388 mbox = phba->mbox;
11389 vport = pmb->vport;
11390
11391 /* Reset heartbeat timer */
11392 phba->last_completion_time = jiffies;
11393 del_timer(&phba->sli.mbox_tmo);
11394
11395 /* Move mbox data to caller's mailbox region, do endian swapping */
11396 if (pmb->mbox_cmpl && mbox)
11397 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011398
James Smart73d91e52011-10-10 21:32:10 -040011399 /*
11400 * For mcqe errors, conditionally move a modified error code to
11401 * the mbox so that the error will not be missed.
11402 */
11403 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11404 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11405 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11406 bf_set(lpfc_mqe_status, mqe,
11407 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11408 }
James Smart04c68492009-05-22 14:52:52 -040011409 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11410 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11411 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11412 "MBOX dflt rpi: status:x%x rpi:x%x",
11413 mcqe_status,
11414 pmbox->un.varWords[0], 0);
11415 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11416 mp = (struct lpfc_dmabuf *)(pmb->context1);
11417 ndlp = (struct lpfc_nodelist *)pmb->context2;
11418 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11419 * RID of the PPI using the same mbox buffer.
11420 */
11421 lpfc_unreg_login(phba, vport->vpi,
11422 pmbox->un.varWords[0], pmb);
11423 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11424 pmb->context1 = mp;
11425 pmb->context2 = ndlp;
11426 pmb->vport = vport;
11427 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11428 if (rc != MBX_BUSY)
11429 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11430 LOG_SLI, "0385 rc should "
11431 "have been MBX_BUSY\n");
11432 if (rc != MBX_NOT_FINISHED)
11433 goto send_current_mbox;
11434 }
11435 }
11436 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11437 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11438 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11439
11440 /* There is mailbox completion work to do */
11441 spin_lock_irqsave(&phba->hbalock, iflags);
11442 __lpfc_mbox_cmpl_put(phba, pmb);
11443 phba->work_ha |= HA_MBATT;
11444 spin_unlock_irqrestore(&phba->hbalock, iflags);
11445 workposted = true;
11446
11447send_current_mbox:
11448 spin_lock_irqsave(&phba->hbalock, iflags);
11449 /* Release the mailbox command posting token */
11450 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11451 /* Setting active mailbox pointer need to be in sync to flag clear */
11452 phba->sli.mbox_active = NULL;
11453 spin_unlock_irqrestore(&phba->hbalock, iflags);
11454 /* Wake up worker thread to post the next pending mailbox command */
11455 lpfc_worker_wake_up(phba);
11456out_no_mqe_complete:
11457 if (bf_get(lpfc_trailer_consumed, mcqe))
11458 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11459 return workposted;
11460}
11461
11462/**
11463 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11464 * @phba: Pointer to HBA context object.
11465 * @cqe: Pointer to mailbox completion queue entry.
11466 *
11467 * This routine process a mailbox completion queue entry, it invokes the
11468 * proper mailbox complete handling or asynchrous event handling routine
11469 * according to the MCQE's async bit.
11470 *
11471 * Return: true if work posted to worker thread, otherwise false.
11472 **/
11473static bool
11474lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11475{
11476 struct lpfc_mcqe mcqe;
11477 bool workposted;
11478
11479 /* Copy the mailbox MCQE and convert endian order as needed */
11480 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11481
11482 /* Invoke the proper event handling routine */
11483 if (!bf_get(lpfc_trailer_async, &mcqe))
11484 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11485 else
11486 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11487 return workposted;
11488}
11489
11490/**
James Smart4f774512009-05-22 14:52:35 -040011491 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11492 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011493 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011494 * @wcqe: Pointer to work-queue completion queue entry.
11495 *
11496 * This routine handles an ELS work-queue completion event.
11497 *
11498 * Return: true if work posted to worker thread, otherwise false.
11499 **/
11500static bool
James Smart2a76a282012-08-03 12:35:54 -040011501lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011502 struct lpfc_wcqe_complete *wcqe)
11503{
James Smart4f774512009-05-22 14:52:35 -040011504 struct lpfc_iocbq *irspiocbq;
11505 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011506 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011507 int txq_cnt = 0;
11508 int txcmplq_cnt = 0;
11509 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040011510
James Smart45ed1192009-10-02 15:17:02 -040011511 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011512 irspiocbq = lpfc_sli_get_iocbq(phba);
11513 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011514 if (!list_empty(&pring->txq))
11515 txq_cnt++;
11516 if (!list_empty(&pring->txcmplq))
11517 txcmplq_cnt++;
11518 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11519 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040011520 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011521 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11522 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050011523 txq_cnt, phba->iocb_cnt,
11524 fcp_txcmplq_cnt,
11525 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011526 return false;
James Smart4f774512009-05-22 14:52:35 -040011527 }
James Smart4f774512009-05-22 14:52:35 -040011528
James Smart45ed1192009-10-02 15:17:02 -040011529 /* Save off the slow-path queue event for work thread to process */
11530 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011531 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011532 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011533 &phba->sli4_hba.sp_queue_event);
11534 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011535 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011536
James Smart45ed1192009-10-02 15:17:02 -040011537 return true;
James Smart4f774512009-05-22 14:52:35 -040011538}
11539
11540/**
11541 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11542 * @phba: Pointer to HBA context object.
11543 * @wcqe: Pointer to work-queue completion queue entry.
11544 *
11545 * This routine handles slow-path WQ entry comsumed event by invoking the
11546 * proper WQ release routine to the slow-path WQ.
11547 **/
11548static void
11549lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11550 struct lpfc_wcqe_release *wcqe)
11551{
James Smart2e90f4b2011-12-13 13:22:37 -050011552 /* sanity check on queue memory */
11553 if (unlikely(!phba->sli4_hba.els_wq))
11554 return;
James Smart4f774512009-05-22 14:52:35 -040011555 /* Check for the slow-path ELS work queue */
11556 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11557 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11558 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11559 else
11560 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11561 "2579 Slow-path wqe consume event carries "
11562 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11563 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11564 phba->sli4_hba.els_wq->queue_id);
11565}
11566
11567/**
11568 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11569 * @phba: Pointer to HBA context object.
11570 * @cq: Pointer to a WQ completion queue.
11571 * @wcqe: Pointer to work-queue completion queue entry.
11572 *
11573 * This routine handles an XRI abort event.
11574 *
11575 * Return: true if work posted to worker thread, otherwise false.
11576 **/
11577static bool
11578lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11579 struct lpfc_queue *cq,
11580 struct sli4_wcqe_xri_aborted *wcqe)
11581{
11582 bool workposted = false;
11583 struct lpfc_cq_event *cq_event;
11584 unsigned long iflags;
11585
11586 /* Allocate a new internal CQ_EVENT entry */
11587 cq_event = lpfc_sli4_cq_event_alloc(phba);
11588 if (!cq_event) {
11589 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11590 "0602 Failed to allocate CQ_EVENT entry\n");
11591 return false;
11592 }
11593
11594 /* Move the CQE into the proper xri abort event list */
11595 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11596 switch (cq->subtype) {
11597 case LPFC_FCP:
11598 spin_lock_irqsave(&phba->hbalock, iflags);
11599 list_add_tail(&cq_event->list,
11600 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11601 /* Set the fcp xri abort event flag */
11602 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11603 spin_unlock_irqrestore(&phba->hbalock, iflags);
11604 workposted = true;
11605 break;
11606 case LPFC_ELS:
11607 spin_lock_irqsave(&phba->hbalock, iflags);
11608 list_add_tail(&cq_event->list,
11609 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11610 /* Set the els xri abort event flag */
11611 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11612 spin_unlock_irqrestore(&phba->hbalock, iflags);
11613 workposted = true;
11614 break;
11615 default:
11616 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11617 "0603 Invalid work queue CQE subtype (x%x)\n",
11618 cq->subtype);
11619 workposted = false;
11620 break;
11621 }
11622 return workposted;
11623}
11624
11625/**
James Smart4d9ab992009-10-02 15:16:39 -040011626 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011627 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011628 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011629 *
James Smart4d9ab992009-10-02 15:16:39 -040011630 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011631 *
11632 * Return: true if work posted to worker thread, otherwise false.
11633 **/
11634static bool
James Smart4d9ab992009-10-02 15:16:39 -040011635lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11636{
11637 bool workposted = false;
11638 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11639 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11640 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011641 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011642 unsigned long iflags;
11643
James Smart2e90f4b2011-12-13 13:22:37 -050011644 /* sanity check on queue memory */
11645 if (unlikely(!hrq) || unlikely(!drq))
11646 return workposted;
11647
James Smart7851fe22011-07-22 18:36:52 -040011648 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11649 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11650 else
11651 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11652 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011653 goto out;
11654
11655 status = bf_get(lpfc_rcqe_status, rcqe);
11656 switch (status) {
11657 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11659 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011660 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011661 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011662 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011663 spin_lock_irqsave(&phba->hbalock, iflags);
11664 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11665 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011666 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011667 spin_unlock_irqrestore(&phba->hbalock, iflags);
11668 goto out;
11669 }
James Smartb84daac2012-08-03 12:35:13 -040011670 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011671 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11672 /* save off the frame for the word thread to process */
11673 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011674 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011675 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011676 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011677 spin_unlock_irqrestore(&phba->hbalock, iflags);
11678 workposted = true;
11679 break;
11680 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11681 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011682 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011683 /* Post more buffers if possible */
11684 spin_lock_irqsave(&phba->hbalock, iflags);
11685 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11686 spin_unlock_irqrestore(&phba->hbalock, iflags);
11687 workposted = true;
11688 break;
11689 }
11690out:
11691 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011692}
11693
11694/**
11695 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11696 * @phba: Pointer to HBA context object.
11697 * @cq: Pointer to the completion queue.
11698 * @wcqe: Pointer to a completion queue entry.
11699 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011700 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011701 * entry.
11702 *
11703 * Return: true if work posted to worker thread, otherwise false.
11704 **/
11705static bool
11706lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011707 struct lpfc_cqe *cqe)
11708{
James Smart45ed1192009-10-02 15:17:02 -040011709 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011710 bool workposted = false;
11711
11712 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011713 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011714
11715 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011716 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011717 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011718 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011719 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011720 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011721 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011722 break;
11723 case CQE_CODE_RELEASE_WQE:
11724 /* Process the WQ release event */
11725 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011726 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011727 break;
11728 case CQE_CODE_XRI_ABORTED:
11729 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011730 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011731 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011732 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011733 break;
James Smart4d9ab992009-10-02 15:16:39 -040011734 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011735 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011736 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011737 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011738 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011739 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011740 break;
James Smart4f774512009-05-22 14:52:35 -040011741 default:
11742 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11743 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011744 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011745 break;
11746 }
11747 return workposted;
11748}
11749
11750/**
James Smart4f774512009-05-22 14:52:35 -040011751 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11752 * @phba: Pointer to HBA context object.
11753 * @eqe: Pointer to fast-path event queue entry.
11754 *
11755 * This routine process a event queue entry from the slow-path event queue.
11756 * It will check the MajorCode and MinorCode to determine this is for a
11757 * completion event on a completion queue, if not, an error shall be logged
11758 * and just return. Otherwise, it will get to the corresponding completion
11759 * queue and process all the entries on that completion queue, rearm the
11760 * completion queue, and then return.
11761 *
11762 **/
11763static void
James Smart67d12732012-08-03 12:36:13 -040011764lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11765 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040011766{
James Smart67d12732012-08-03 12:36:13 -040011767 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040011768 struct lpfc_cqe *cqe;
11769 bool workposted = false;
11770 int ecount = 0;
11771 uint16_t cqid;
11772
James Smart4f774512009-05-22 14:52:35 -040011773 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011774 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011775
James Smart4f774512009-05-22 14:52:35 -040011776 list_for_each_entry(childq, &speq->child_list, list) {
11777 if (childq->queue_id == cqid) {
11778 cq = childq;
11779 break;
11780 }
11781 }
11782 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011783 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11784 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11785 "0365 Slow-path CQ identifier "
11786 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011787 return;
11788 }
11789
11790 /* Process all the entries to the CQ */
11791 switch (cq->type) {
11792 case LPFC_MCQ:
11793 while ((cqe = lpfc_sli4_cq_get(cq))) {
11794 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011795 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011796 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011797 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011798 }
11799 break;
11800 case LPFC_WCQ:
11801 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011802 if (cq->subtype == LPFC_FCP)
11803 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11804 cqe);
11805 else
11806 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11807 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011808 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011809 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11810 }
James Smartb84daac2012-08-03 12:35:13 -040011811
11812 /* Track the max number of CQEs processed in 1 EQ */
11813 if (ecount > cq->CQ_max_cqe)
11814 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011815 break;
11816 default:
11817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11818 "0370 Invalid completion queue type (%d)\n",
11819 cq->type);
11820 return;
11821 }
11822
11823 /* Catch the no cq entry condition, log an error */
11824 if (unlikely(ecount == 0))
11825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11826 "0371 No entry from the CQ: identifier "
11827 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11828
11829 /* In any case, flash and re-arm the RCQ */
11830 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11831
11832 /* wake up worker thread if there are works to be done */
11833 if (workposted)
11834 lpfc_worker_wake_up(phba);
11835}
11836
11837/**
11838 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011839 * @phba: Pointer to HBA context object.
11840 * @cq: Pointer to associated CQ
11841 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011842 *
11843 * This routine process a fast-path work queue completion entry from fast-path
11844 * event queue for FCP command response completion.
11845 **/
11846static void
James Smart2a76a282012-08-03 12:35:54 -040011847lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011848 struct lpfc_wcqe_complete *wcqe)
11849{
James Smart2a76a282012-08-03 12:35:54 -040011850 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011851 struct lpfc_iocbq *cmdiocbq;
11852 struct lpfc_iocbq irspiocbq;
11853 unsigned long iflags;
11854
James Smart4f774512009-05-22 14:52:35 -040011855 /* Check for response status */
11856 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11857 /* If resource errors reported from HBA, reduce queue
11858 * depth of the SCSI device.
11859 */
James Smarte3d2b802012-08-14 14:25:43 -040011860 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11861 IOSTAT_LOCAL_REJECT)) &&
11862 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11863 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040011864 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040011865
James Smart4f774512009-05-22 14:52:35 -040011866 /* Log the error status */
11867 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11868 "0373 FCP complete error: status=x%x, "
11869 "hw_status=x%x, total_data_specified=%d, "
11870 "parameter=x%x, word3=x%x\n",
11871 bf_get(lpfc_wcqe_c_status, wcqe),
11872 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11873 wcqe->total_data_placed, wcqe->parameter,
11874 wcqe->word3);
11875 }
11876
11877 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011878 spin_lock_irqsave(&pring->ring_lock, iflags);
11879 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011880 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11881 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011882 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011883 if (unlikely(!cmdiocbq)) {
11884 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11885 "0374 FCP complete with no corresponding "
11886 "cmdiocb: iotag (%d)\n",
11887 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11888 return;
11889 }
11890 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11891 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11892 "0375 FCP cmdiocb not callback function "
11893 "iotag: (%d)\n",
11894 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11895 return;
11896 }
11897
11898 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011899 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011900
James Smart0f65ff62010-02-26 14:14:23 -050011901 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11902 spin_lock_irqsave(&phba->hbalock, iflags);
11903 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11904 spin_unlock_irqrestore(&phba->hbalock, iflags);
11905 }
11906
James Smart4f774512009-05-22 14:52:35 -040011907 /* Pass the cmd_iocb and the rsp state to the upper layer */
11908 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11909}
11910
11911/**
11912 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11913 * @phba: Pointer to HBA context object.
11914 * @cq: Pointer to completion queue.
11915 * @wcqe: Pointer to work-queue completion queue entry.
11916 *
11917 * This routine handles an fast-path WQ entry comsumed event by invoking the
11918 * proper WQ release routine to the slow-path WQ.
11919 **/
11920static void
11921lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11922 struct lpfc_wcqe_release *wcqe)
11923{
11924 struct lpfc_queue *childwq;
11925 bool wqid_matched = false;
11926 uint16_t fcp_wqid;
11927
11928 /* Check for fast-path FCP work queue release */
11929 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11930 list_for_each_entry(childwq, &cq->child_list, list) {
11931 if (childwq->queue_id == fcp_wqid) {
11932 lpfc_sli4_wq_release(childwq,
11933 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11934 wqid_matched = true;
11935 break;
11936 }
11937 }
11938 /* Report warning log message if no match found */
11939 if (wqid_matched != true)
11940 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11941 "2580 Fast-path wqe consume event carries "
11942 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11943}
11944
11945/**
11946 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11947 * @cq: Pointer to the completion queue.
11948 * @eqe: Pointer to fast-path completion queue entry.
11949 *
11950 * This routine process a fast-path work queue completion entry from fast-path
11951 * event queue for FCP command response completion.
11952 **/
11953static int
11954lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11955 struct lpfc_cqe *cqe)
11956{
11957 struct lpfc_wcqe_release wcqe;
11958 bool workposted = false;
11959
11960 /* Copy the work queue CQE and convert endian order if needed */
11961 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11962
11963 /* Check and process for different type of WCQE and dispatch */
11964 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11965 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011966 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011967 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011968 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011969 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040011970 (struct lpfc_wcqe_complete *)&wcqe);
11971 break;
11972 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011973 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011974 /* Process the WQ release event */
11975 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11976 (struct lpfc_wcqe_release *)&wcqe);
11977 break;
11978 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011979 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011980 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011981 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011982 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11983 (struct sli4_wcqe_xri_aborted *)&wcqe);
11984 break;
11985 default:
11986 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11987 "0144 Not a valid WCQE code: x%x\n",
11988 bf_get(lpfc_wcqe_c_code, &wcqe));
11989 break;
11990 }
11991 return workposted;
11992}
11993
11994/**
James Smart67d12732012-08-03 12:36:13 -040011995 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040011996 * @phba: Pointer to HBA context object.
11997 * @eqe: Pointer to fast-path event queue entry.
11998 *
11999 * This routine process a event queue entry from the fast-path event queue.
12000 * It will check the MajorCode and MinorCode to determine this is for a
12001 * completion event on a completion queue, if not, an error shall be logged
12002 * and just return. Otherwise, it will get to the corresponding completion
12003 * queue and process all the entries on the completion queue, rearm the
12004 * completion queue, and then return.
12005 **/
12006static void
James Smart67d12732012-08-03 12:36:13 -040012007lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12008 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012009{
12010 struct lpfc_queue *cq;
12011 struct lpfc_cqe *cqe;
12012 bool workposted = false;
12013 uint16_t cqid;
12014 int ecount = 0;
12015
James Smartcb5172e2010-03-15 11:25:07 -040012016 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012018 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012019 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012020 bf_get_le32(lpfc_eqe_major_code, eqe),
12021 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012022 return;
12023 }
12024
James Smart67d12732012-08-03 12:36:13 -040012025 /* Get the reference to the corresponding CQ */
12026 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12027
12028 /* Check if this is a Slow path event */
12029 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12030 lpfc_sli4_sp_handle_eqe(phba, eqe,
12031 phba->sli4_hba.hba_eq[qidx]);
12032 return;
12033 }
12034
James Smart2e90f4b2011-12-13 13:22:37 -050012035 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12036 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12037 "3146 Fast-path completion queues "
12038 "does not exist\n");
12039 return;
12040 }
James Smart67d12732012-08-03 12:36:13 -040012041 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012042 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012043 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12044 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12045 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012046 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012047 return;
12048 }
12049
James Smart4f774512009-05-22 14:52:35 -040012050 if (unlikely(cqid != cq->queue_id)) {
12051 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12052 "0368 Miss-matched fast-path completion "
12053 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12054 cqid, cq->queue_id);
12055 return;
12056 }
12057
12058 /* Process all the entries to the CQ */
12059 while ((cqe = lpfc_sli4_cq_get(cq))) {
12060 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012061 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012062 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12063 }
12064
James Smartb84daac2012-08-03 12:35:13 -040012065 /* Track the max number of CQEs processed in 1 EQ */
12066 if (ecount > cq->CQ_max_cqe)
12067 cq->CQ_max_cqe = ecount;
12068
James Smart4f774512009-05-22 14:52:35 -040012069 /* Catch the no cq entry condition */
12070 if (unlikely(ecount == 0))
12071 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12072 "0369 No entry from fast-path completion "
12073 "queue fcpcqid=%d\n", cq->queue_id);
12074
12075 /* In any case, flash and re-arm the CQ */
12076 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12077
12078 /* wake up worker thread if there are works to be done */
12079 if (workposted)
12080 lpfc_worker_wake_up(phba);
12081}
12082
12083static void
12084lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12085{
12086 struct lpfc_eqe *eqe;
12087
12088 /* walk all the EQ entries and drop on the floor */
12089 while ((eqe = lpfc_sli4_eq_get(eq)))
12090 ;
12091
12092 /* Clear and re-arm the EQ */
12093 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12094}
12095
12096/**
James Smart67d12732012-08-03 12:36:13 -040012097 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012098 * @irq: Interrupt number.
12099 * @dev_id: The device context pointer.
12100 *
12101 * This function is directly called from the PCI layer as an interrupt
12102 * service routine when device with SLI-4 interface spec is enabled with
12103 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12104 * ring event in the HBA. However, when the device is enabled with either
12105 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12106 * device-level interrupt handler. When the PCI slot is in error recovery
12107 * or the HBA is undergoing initialization, the interrupt handler will not
12108 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12109 * the intrrupt context. This function is called without any lock held.
12110 * It gets the hbalock to access and update SLI data structures. Note that,
12111 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12112 * equal to that of FCP CQ index.
12113 *
James Smart67d12732012-08-03 12:36:13 -040012114 * The link attention and ELS ring attention events are handled
12115 * by the worker thread. The interrupt handler signals the worker thread
12116 * and returns for these events. This function is called without any lock
12117 * held. It gets the hbalock to access and update SLI data structures.
12118 *
James Smart4f774512009-05-22 14:52:35 -040012119 * This function returns IRQ_HANDLED when interrupt is handled else it
12120 * returns IRQ_NONE.
12121 **/
12122irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012123lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012124{
12125 struct lpfc_hba *phba;
12126 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12127 struct lpfc_queue *fpeq;
12128 struct lpfc_eqe *eqe;
12129 unsigned long iflag;
12130 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012131 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012132
12133 /* Get the driver's phba structure from the dev_id */
12134 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12135 phba = fcp_eq_hdl->phba;
12136 fcp_eqidx = fcp_eq_hdl->idx;
12137
12138 if (unlikely(!phba))
12139 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012140 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012141 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012142
12143 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012144 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012145 if (unlikely(!fpeq))
12146 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012147
James Smartba20c852012-08-03 12:36:52 -040012148 if (lpfc_fcp_look_ahead) {
12149 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12150 lpfc_sli4_eq_clr_intr(fpeq);
12151 else {
12152 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12153 return IRQ_NONE;
12154 }
12155 }
12156
James Smart4f774512009-05-22 14:52:35 -040012157 /* Check device state for handling interrupt */
12158 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012159 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012160 /* Check again for link_state with lock held */
12161 spin_lock_irqsave(&phba->hbalock, iflag);
12162 if (phba->link_state < LPFC_LINK_DOWN)
12163 /* Flush, clear interrupt, and rearm the EQ */
12164 lpfc_sli4_eq_flush(phba, fpeq);
12165 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012166 if (lpfc_fcp_look_ahead)
12167 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012168 return IRQ_NONE;
12169 }
12170
12171 /*
12172 * Process all the event on FCP fast-path EQ
12173 */
12174 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smart67d12732012-08-03 12:36:13 -040012175 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012176 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012177 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012178 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012179 }
12180
James Smartb84daac2012-08-03 12:35:13 -040012181 /* Track the max number of EQEs processed in 1 intr */
12182 if (ecount > fpeq->EQ_max_eqe)
12183 fpeq->EQ_max_eqe = ecount;
12184
James Smart4f774512009-05-22 14:52:35 -040012185 /* Always clear and re-arm the fast-path EQ */
12186 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12187
12188 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012189 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012190
12191 if (lpfc_fcp_look_ahead) {
12192 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12193 return IRQ_NONE;
12194 }
12195
James Smart4f774512009-05-22 14:52:35 -040012196 if (phba->intr_type == MSIX)
12197 /* MSI-X treated interrupt served as no EQ share INT */
12198 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12199 "0358 MSI-X interrupt with no EQE\n");
12200 else
12201 /* Non MSI-X treated on interrupt as EQ share INT */
12202 return IRQ_NONE;
12203 }
12204
James Smartba20c852012-08-03 12:36:52 -040012205 if (lpfc_fcp_look_ahead)
12206 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012207 return IRQ_HANDLED;
12208} /* lpfc_sli4_fp_intr_handler */
12209
12210/**
12211 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12212 * @irq: Interrupt number.
12213 * @dev_id: The device context pointer.
12214 *
12215 * This function is the device-level interrupt handler to device with SLI-4
12216 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12217 * interrupt mode is enabled and there is an event in the HBA which requires
12218 * driver attention. This function invokes the slow-path interrupt attention
12219 * handling function and fast-path interrupt attention handling function in
12220 * turn to process the relevant HBA attention events. This function is called
12221 * without any lock held. It gets the hbalock to access and update SLI data
12222 * structures.
12223 *
12224 * This function returns IRQ_HANDLED when interrupt is handled, else it
12225 * returns IRQ_NONE.
12226 **/
12227irqreturn_t
12228lpfc_sli4_intr_handler(int irq, void *dev_id)
12229{
12230 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012231 irqreturn_t hba_irq_rc;
12232 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012233 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012234
12235 /* Get the driver's phba structure from the dev_id */
12236 phba = (struct lpfc_hba *)dev_id;
12237
12238 if (unlikely(!phba))
12239 return IRQ_NONE;
12240
12241 /*
James Smart4f774512009-05-22 14:52:35 -040012242 * Invoke fast-path host attention interrupt handling as appropriate.
12243 */
James Smart67d12732012-08-03 12:36:13 -040012244 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12245 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012246 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012247 if (hba_irq_rc == IRQ_HANDLED)
12248 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012249 }
12250
James Smart67d12732012-08-03 12:36:13 -040012251 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012252} /* lpfc_sli4_intr_handler */
12253
12254/**
12255 * lpfc_sli4_queue_free - free a queue structure and associated memory
12256 * @queue: The queue structure to free.
12257 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012258 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012259 * the host resident queue. This function must be called after destroying the
12260 * queue on the HBA.
12261 **/
12262void
12263lpfc_sli4_queue_free(struct lpfc_queue *queue)
12264{
12265 struct lpfc_dmabuf *dmabuf;
12266
12267 if (!queue)
12268 return;
12269
12270 while (!list_empty(&queue->page_list)) {
12271 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12272 list);
James Smart49198b32010-04-06 15:04:33 -040012273 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012274 dmabuf->virt, dmabuf->phys);
12275 kfree(dmabuf);
12276 }
12277 kfree(queue);
12278 return;
12279}
12280
12281/**
12282 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12283 * @phba: The HBA that this queue is being created on.
12284 * @entry_size: The size of each queue entry for this queue.
12285 * @entry count: The number of entries that this queue will handle.
12286 *
12287 * This function allocates a queue structure and the DMAable memory used for
12288 * the host resident queue. This function must be called before creating the
12289 * queue on the HBA.
12290 **/
12291struct lpfc_queue *
12292lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12293 uint32_t entry_count)
12294{
12295 struct lpfc_queue *queue;
12296 struct lpfc_dmabuf *dmabuf;
12297 int x, total_qe_count;
12298 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012299 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012300
James Smartcb5172e2010-03-15 11:25:07 -040012301 if (!phba->sli4_hba.pc_sli4_params.supported)
12302 hw_page_size = SLI4_PAGE_SIZE;
12303
James Smart4f774512009-05-22 14:52:35 -040012304 queue = kzalloc(sizeof(struct lpfc_queue) +
12305 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12306 if (!queue)
12307 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012308 queue->page_count = (ALIGN(entry_size * entry_count,
12309 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012310 INIT_LIST_HEAD(&queue->list);
12311 INIT_LIST_HEAD(&queue->page_list);
12312 INIT_LIST_HEAD(&queue->child_list);
12313 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12314 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12315 if (!dmabuf)
12316 goto out_fail;
12317 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012318 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012319 GFP_KERNEL);
12320 if (!dmabuf->virt) {
12321 kfree(dmabuf);
12322 goto out_fail;
12323 }
James Smartcb5172e2010-03-15 11:25:07 -040012324 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012325 dmabuf->buffer_tag = x;
12326 list_add_tail(&dmabuf->list, &queue->page_list);
12327 /* initialize queue's entry array */
12328 dma_pointer = dmabuf->virt;
12329 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012330 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012331 total_qe_count++, dma_pointer += entry_size) {
12332 queue->qe[total_qe_count].address = dma_pointer;
12333 }
12334 }
12335 queue->entry_size = entry_size;
12336 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012337
12338 /*
12339 * entry_repost is calculated based on the number of entries in the
12340 * queue. This works out except for RQs. If buffers are NOT initially
12341 * posted for every RQE, entry_repost should be adjusted accordingly.
12342 */
12343 queue->entry_repost = (entry_count >> 3);
12344 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12345 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012346 queue->phba = phba;
12347
12348 return queue;
12349out_fail:
12350 lpfc_sli4_queue_free(queue);
12351 return NULL;
12352}
12353
12354/**
James Smart962bc512013-01-03 15:44:00 -050012355 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12356 * @phba: HBA structure that indicates port to create a queue on.
12357 * @pci_barset: PCI BAR set flag.
12358 *
12359 * This function shall perform iomap of the specified PCI BAR address to host
12360 * memory address if not already done so and return it. The returned host
12361 * memory address can be NULL.
12362 */
12363static void __iomem *
12364lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12365{
12366 struct pci_dev *pdev;
James Smart962bc512013-01-03 15:44:00 -050012367
12368 if (!phba->pcidev)
12369 return NULL;
12370 else
12371 pdev = phba->pcidev;
12372
12373 switch (pci_barset) {
12374 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050012375 return phba->pci_bar0_memmap_p;
12376 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050012377 return phba->pci_bar2_memmap_p;
12378 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050012379 return phba->pci_bar4_memmap_p;
12380 default:
12381 break;
12382 }
12383 return NULL;
12384}
12385
12386/**
James Smart173edbb2012-06-12 13:54:50 -040012387 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12388 * @phba: HBA structure that indicates port to create a queue on.
12389 * @startq: The starting FCP EQ to modify
12390 *
12391 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12392 *
12393 * The @phba struct is used to send mailbox command to HBA. The @startq
12394 * is used to get the starting FCP EQ to change.
12395 * This function is asynchronous and will wait for the mailbox
12396 * command to finish before continuing.
12397 *
12398 * On success this function will return a zero. If unable to allocate enough
12399 * memory this function will return -ENOMEM. If the queue create mailbox command
12400 * fails this function will return -ENXIO.
12401 **/
12402uint32_t
12403lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12404{
12405 struct lpfc_mbx_modify_eq_delay *eq_delay;
12406 LPFC_MBOXQ_t *mbox;
12407 struct lpfc_queue *eq;
12408 int cnt, rc, length, status = 0;
12409 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040012410 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040012411 int fcp_eqidx;
12412 union lpfc_sli4_cfg_shdr *shdr;
12413 uint16_t dmult;
12414
James Smart67d12732012-08-03 12:36:13 -040012415 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040012416 return 0;
12417
12418 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12419 if (!mbox)
12420 return -ENOMEM;
12421 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12422 sizeof(struct lpfc_sli4_cfg_mhdr));
12423 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12424 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12425 length, LPFC_SLI4_MBX_EMBED);
12426 eq_delay = &mbox->u.mqe.un.eq_delay;
12427
12428 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012429 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12430 if (result > LPFC_DMULT_CONST)
12431 dmult = 0;
12432 else
12433 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040012434
12435 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040012436 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040012437 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040012438 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040012439 if (!eq)
12440 continue;
12441 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12442 eq_delay->u.request.eq[cnt].phase = 0;
12443 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12444 cnt++;
12445 if (cnt >= LPFC_MAX_EQ_DELAY)
12446 break;
12447 }
12448 eq_delay->u.request.num_eq = cnt;
12449
12450 mbox->vport = phba->pport;
12451 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12452 mbox->context1 = NULL;
12453 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12454 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12455 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12456 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12457 if (shdr_status || shdr_add_status || rc) {
12458 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12459 "2512 MODIFY_EQ_DELAY mailbox failed with "
12460 "status x%x add_status x%x, mbx status x%x\n",
12461 shdr_status, shdr_add_status, rc);
12462 status = -ENXIO;
12463 }
12464 mempool_free(mbox, phba->mbox_mem_pool);
12465 return status;
12466}
12467
12468/**
James Smart4f774512009-05-22 14:52:35 -040012469 * lpfc_eq_create - Create an Event Queue on the HBA
12470 * @phba: HBA structure that indicates port to create a queue on.
12471 * @eq: The queue structure to use to create the event queue.
12472 * @imax: The maximum interrupt per second limit.
12473 *
12474 * This function creates an event queue, as detailed in @eq, on a port,
12475 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12476 *
12477 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12478 * is used to get the entry count and entry size that are necessary to
12479 * determine the number of pages to allocate and use for this queue. This
12480 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12481 * event queue. This function is asynchronous and will wait for the mailbox
12482 * command to finish before continuing.
12483 *
12484 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012485 * memory this function will return -ENOMEM. If the queue create mailbox command
12486 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012487 **/
12488uint32_t
James Smartee020062012-09-29 11:28:52 -040012489lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040012490{
12491 struct lpfc_mbx_eq_create *eq_create;
12492 LPFC_MBOXQ_t *mbox;
12493 int rc, length, status = 0;
12494 struct lpfc_dmabuf *dmabuf;
12495 uint32_t shdr_status, shdr_add_status;
12496 union lpfc_sli4_cfg_shdr *shdr;
12497 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012498 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12499
James Smart2e90f4b2011-12-13 13:22:37 -050012500 /* sanity check on queue memory */
12501 if (!eq)
12502 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012503 if (!phba->sli4_hba.pc_sli4_params.supported)
12504 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012505
12506 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12507 if (!mbox)
12508 return -ENOMEM;
12509 length = (sizeof(struct lpfc_mbx_eq_create) -
12510 sizeof(struct lpfc_sli4_cfg_mhdr));
12511 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12512 LPFC_MBOX_OPCODE_EQ_CREATE,
12513 length, LPFC_SLI4_MBX_EMBED);
12514 eq_create = &mbox->u.mqe.un.eq_create;
12515 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12516 eq->page_count);
12517 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12518 LPFC_EQE_SIZE);
12519 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12520 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012521 if (imax > LPFC_DMULT_CONST)
12522 dmult = 0;
12523 else
12524 dmult = LPFC_DMULT_CONST/imax - 1;
James Smart4f774512009-05-22 14:52:35 -040012525 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12526 dmult);
12527 switch (eq->entry_count) {
12528 default:
12529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12530 "0360 Unsupported EQ count. (%d)\n",
12531 eq->entry_count);
12532 if (eq->entry_count < 256)
12533 return -EINVAL;
12534 /* otherwise default to smallest count (drop through) */
12535 case 256:
12536 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12537 LPFC_EQ_CNT_256);
12538 break;
12539 case 512:
12540 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12541 LPFC_EQ_CNT_512);
12542 break;
12543 case 1024:
12544 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12545 LPFC_EQ_CNT_1024);
12546 break;
12547 case 2048:
12548 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12549 LPFC_EQ_CNT_2048);
12550 break;
12551 case 4096:
12552 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12553 LPFC_EQ_CNT_4096);
12554 break;
12555 }
12556 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012557 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012558 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12559 putPaddrLow(dmabuf->phys);
12560 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12561 putPaddrHigh(dmabuf->phys);
12562 }
12563 mbox->vport = phba->pport;
12564 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12565 mbox->context1 = NULL;
12566 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12567 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12568 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12569 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12570 if (shdr_status || shdr_add_status || rc) {
12571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12572 "2500 EQ_CREATE mailbox failed with "
12573 "status x%x add_status x%x, mbx status x%x\n",
12574 shdr_status, shdr_add_status, rc);
12575 status = -ENXIO;
12576 }
12577 eq->type = LPFC_EQ;
12578 eq->subtype = LPFC_NONE;
12579 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12580 if (eq->queue_id == 0xFFFF)
12581 status = -ENXIO;
12582 eq->host_index = 0;
12583 eq->hba_index = 0;
12584
James Smart8fa38512009-07-19 10:01:03 -040012585 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012586 return status;
12587}
12588
12589/**
12590 * lpfc_cq_create - Create a Completion Queue on the HBA
12591 * @phba: HBA structure that indicates port to create a queue on.
12592 * @cq: The queue structure to use to create the completion queue.
12593 * @eq: The event queue to bind this completion queue to.
12594 *
12595 * This function creates a completion queue, as detailed in @wq, on a port,
12596 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12597 *
12598 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12599 * is used to get the entry count and entry size that are necessary to
12600 * determine the number of pages to allocate and use for this queue. The @eq
12601 * is used to indicate which event queue to bind this completion queue to. This
12602 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12603 * completion queue. This function is asynchronous and will wait for the mailbox
12604 * command to finish before continuing.
12605 *
12606 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012607 * memory this function will return -ENOMEM. If the queue create mailbox command
12608 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012609 **/
12610uint32_t
12611lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12612 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12613{
12614 struct lpfc_mbx_cq_create *cq_create;
12615 struct lpfc_dmabuf *dmabuf;
12616 LPFC_MBOXQ_t *mbox;
12617 int rc, length, status = 0;
12618 uint32_t shdr_status, shdr_add_status;
12619 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012620 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12621
James Smart2e90f4b2011-12-13 13:22:37 -050012622 /* sanity check on queue memory */
12623 if (!cq || !eq)
12624 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012625 if (!phba->sli4_hba.pc_sli4_params.supported)
12626 hw_page_size = SLI4_PAGE_SIZE;
12627
James Smart4f774512009-05-22 14:52:35 -040012628 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12629 if (!mbox)
12630 return -ENOMEM;
12631 length = (sizeof(struct lpfc_mbx_cq_create) -
12632 sizeof(struct lpfc_sli4_cfg_mhdr));
12633 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12634 LPFC_MBOX_OPCODE_CQ_CREATE,
12635 length, LPFC_SLI4_MBX_EMBED);
12636 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012637 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012638 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12639 cq->page_count);
12640 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12641 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012642 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12643 phba->sli4_hba.pc_sli4_params.cqv);
12644 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012645 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12646 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012647 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12648 eq->queue_id);
12649 } else {
12650 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12651 eq->queue_id);
12652 }
James Smart4f774512009-05-22 14:52:35 -040012653 switch (cq->entry_count) {
12654 default:
12655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12656 "0361 Unsupported CQ count. (%d)\n",
12657 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012658 if (cq->entry_count < 256) {
12659 status = -EINVAL;
12660 goto out;
12661 }
James Smart4f774512009-05-22 14:52:35 -040012662 /* otherwise default to smallest count (drop through) */
12663 case 256:
12664 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12665 LPFC_CQ_CNT_256);
12666 break;
12667 case 512:
12668 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12669 LPFC_CQ_CNT_512);
12670 break;
12671 case 1024:
12672 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12673 LPFC_CQ_CNT_1024);
12674 break;
12675 }
12676 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012677 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012678 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12679 putPaddrLow(dmabuf->phys);
12680 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12681 putPaddrHigh(dmabuf->phys);
12682 }
12683 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12684
12685 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012686 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12687 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12688 if (shdr_status || shdr_add_status || rc) {
12689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12690 "2501 CQ_CREATE mailbox failed with "
12691 "status x%x add_status x%x, mbx status x%x\n",
12692 shdr_status, shdr_add_status, rc);
12693 status = -ENXIO;
12694 goto out;
12695 }
12696 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12697 if (cq->queue_id == 0xFFFF) {
12698 status = -ENXIO;
12699 goto out;
12700 }
12701 /* link the cq onto the parent eq child list */
12702 list_add_tail(&cq->list, &eq->child_list);
12703 /* Set up completion queue's type and subtype */
12704 cq->type = type;
12705 cq->subtype = subtype;
12706 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012707 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012708 cq->host_index = 0;
12709 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012710
James Smart8fa38512009-07-19 10:01:03 -040012711out:
12712 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012713 return status;
12714}
12715
12716/**
James Smartb19a0612010-04-06 14:48:51 -040012717 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012718 * @phba: HBA structure that indicates port to create a queue on.
12719 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012720 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12721 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012722 *
James Smartb19a0612010-04-06 14:48:51 -040012723 * This function provides failback (fb) functionality when the
12724 * mq_create_ext fails on older FW generations. It's purpose is identical
12725 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012726 *
James Smartb19a0612010-04-06 14:48:51 -040012727 * This routine cannot fail as all attributes were previously accessed and
12728 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012729 **/
James Smartb19a0612010-04-06 14:48:51 -040012730static void
12731lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12732 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012733{
12734 struct lpfc_mbx_mq_create *mq_create;
12735 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012736 int length;
James Smart04c68492009-05-22 14:52:52 -040012737
James Smart04c68492009-05-22 14:52:52 -040012738 length = (sizeof(struct lpfc_mbx_mq_create) -
12739 sizeof(struct lpfc_sli4_cfg_mhdr));
12740 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12741 LPFC_MBOX_OPCODE_MQ_CREATE,
12742 length, LPFC_SLI4_MBX_EMBED);
12743 mq_create = &mbox->u.mqe.un.mq_create;
12744 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012745 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012746 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012747 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012748 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12749 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012750 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012751 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12752 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012753 break;
12754 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012755 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12756 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012757 break;
12758 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012759 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12760 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012761 break;
12762 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012763 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12764 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012765 break;
12766 }
12767 list_for_each_entry(dmabuf, &mq->page_list, list) {
12768 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012769 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012770 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012771 putPaddrHigh(dmabuf->phys);
12772 }
12773}
12774
12775/**
12776 * lpfc_mq_create - Create a mailbox Queue on the HBA
12777 * @phba: HBA structure that indicates port to create a queue on.
12778 * @mq: The queue structure to use to create the mailbox queue.
12779 * @cq: The completion queue to associate with this cq.
12780 * @subtype: The queue's subtype.
12781 *
12782 * This function creates a mailbox queue, as detailed in @mq, on a port,
12783 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12784 *
12785 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12786 * is used to get the entry count and entry size that are necessary to
12787 * determine the number of pages to allocate and use for this queue. This
12788 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12789 * mailbox queue. This function is asynchronous and will wait for the mailbox
12790 * command to finish before continuing.
12791 *
12792 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012793 * memory this function will return -ENOMEM. If the queue create mailbox command
12794 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012795 **/
12796int32_t
12797lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12798 struct lpfc_queue *cq, uint32_t subtype)
12799{
12800 struct lpfc_mbx_mq_create *mq_create;
12801 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12802 struct lpfc_dmabuf *dmabuf;
12803 LPFC_MBOXQ_t *mbox;
12804 int rc, length, status = 0;
12805 uint32_t shdr_status, shdr_add_status;
12806 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012807 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012808
James Smart2e90f4b2011-12-13 13:22:37 -050012809 /* sanity check on queue memory */
12810 if (!mq || !cq)
12811 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012812 if (!phba->sli4_hba.pc_sli4_params.supported)
12813 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012814
12815 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12816 if (!mbox)
12817 return -ENOMEM;
12818 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12819 sizeof(struct lpfc_sli4_cfg_mhdr));
12820 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12821 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12822 length, LPFC_SLI4_MBX_EMBED);
12823
12824 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012825 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012826 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12827 &mq_create_ext->u.request, mq->page_count);
12828 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12829 &mq_create_ext->u.request, 1);
12830 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012831 &mq_create_ext->u.request, 1);
12832 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12833 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012834 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12835 &mq_create_ext->u.request, 1);
12836 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12837 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012838 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012839 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12840 phba->sli4_hba.pc_sli4_params.mqv);
12841 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12842 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12843 cq->queue_id);
12844 else
12845 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12846 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012847 switch (mq->entry_count) {
12848 default:
12849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12850 "0362 Unsupported MQ count. (%d)\n",
12851 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012852 if (mq->entry_count < 16) {
12853 status = -EINVAL;
12854 goto out;
12855 }
James Smartb19a0612010-04-06 14:48:51 -040012856 /* otherwise default to smallest count (drop through) */
12857 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012858 bf_set(lpfc_mq_context_ring_size,
12859 &mq_create_ext->u.request.context,
12860 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012861 break;
12862 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012863 bf_set(lpfc_mq_context_ring_size,
12864 &mq_create_ext->u.request.context,
12865 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012866 break;
12867 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012868 bf_set(lpfc_mq_context_ring_size,
12869 &mq_create_ext->u.request.context,
12870 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012871 break;
12872 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012873 bf_set(lpfc_mq_context_ring_size,
12874 &mq_create_ext->u.request.context,
12875 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012876 break;
12877 }
12878 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012879 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012880 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12881 putPaddrLow(dmabuf->phys);
12882 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012883 putPaddrHigh(dmabuf->phys);
12884 }
12885 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012886 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12887 &mq_create_ext->u.response);
12888 if (rc != MBX_SUCCESS) {
12889 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12890 "2795 MQ_CREATE_EXT failed with "
12891 "status x%x. Failback to MQ_CREATE.\n",
12892 rc);
12893 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12894 mq_create = &mbox->u.mqe.un.mq_create;
12895 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12896 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12897 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12898 &mq_create->u.response);
12899 }
12900
James Smart04c68492009-05-22 14:52:52 -040012901 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012902 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12903 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12904 if (shdr_status || shdr_add_status || rc) {
12905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12906 "2502 MQ_CREATE mailbox failed with "
12907 "status x%x add_status x%x, mbx status x%x\n",
12908 shdr_status, shdr_add_status, rc);
12909 status = -ENXIO;
12910 goto out;
12911 }
James Smart04c68492009-05-22 14:52:52 -040012912 if (mq->queue_id == 0xFFFF) {
12913 status = -ENXIO;
12914 goto out;
12915 }
12916 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012917 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012918 mq->subtype = subtype;
12919 mq->host_index = 0;
12920 mq->hba_index = 0;
12921
12922 /* link the mq onto the parent cq child list */
12923 list_add_tail(&mq->list, &cq->child_list);
12924out:
James Smart8fa38512009-07-19 10:01:03 -040012925 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012926 return status;
12927}
12928
12929/**
James Smart4f774512009-05-22 14:52:35 -040012930 * lpfc_wq_create - Create a Work Queue on the HBA
12931 * @phba: HBA structure that indicates port to create a queue on.
12932 * @wq: The queue structure to use to create the work queue.
12933 * @cq: The completion queue to bind this work queue to.
12934 * @subtype: The subtype of the work queue indicating its functionality.
12935 *
12936 * This function creates a work queue, as detailed in @wq, on a port, described
12937 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12938 *
12939 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12940 * is used to get the entry count and entry size that are necessary to
12941 * determine the number of pages to allocate and use for this queue. The @cq
12942 * is used to indicate which completion queue to bind this work queue to. This
12943 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12944 * work queue. This function is asynchronous and will wait for the mailbox
12945 * command to finish before continuing.
12946 *
12947 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012948 * memory this function will return -ENOMEM. If the queue create mailbox command
12949 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012950 **/
12951uint32_t
12952lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12953 struct lpfc_queue *cq, uint32_t subtype)
12954{
12955 struct lpfc_mbx_wq_create *wq_create;
12956 struct lpfc_dmabuf *dmabuf;
12957 LPFC_MBOXQ_t *mbox;
12958 int rc, length, status = 0;
12959 uint32_t shdr_status, shdr_add_status;
12960 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012961 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012962 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050012963 void __iomem *bar_memmap_p;
12964 uint32_t db_offset;
12965 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040012966
James Smart2e90f4b2011-12-13 13:22:37 -050012967 /* sanity check on queue memory */
12968 if (!wq || !cq)
12969 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012970 if (!phba->sli4_hba.pc_sli4_params.supported)
12971 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012972
12973 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12974 if (!mbox)
12975 return -ENOMEM;
12976 length = (sizeof(struct lpfc_mbx_wq_create) -
12977 sizeof(struct lpfc_sli4_cfg_mhdr));
12978 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12979 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12980 length, LPFC_SLI4_MBX_EMBED);
12981 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012982 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012983 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12984 wq->page_count);
12985 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12986 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040012987
12988 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050012989 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12990 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050012991
James Smart0c651872013-07-15 18:33:23 -040012992 switch (phba->sli4_hba.pc_sli4_params.wqv) {
12993 case LPFC_Q_CREATE_VERSION_0:
12994 switch (wq->entry_size) {
12995 default:
12996 case 64:
12997 /* Nothing to do, version 0 ONLY supports 64 byte */
12998 page = wq_create->u.request.page;
12999 break;
13000 case 128:
13001 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13002 LPFC_WQ_SZ128_SUPPORT)) {
13003 status = -ERANGE;
13004 goto out;
13005 }
13006 /* If we get here the HBA MUST also support V1 and
13007 * we MUST use it
13008 */
13009 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13010 LPFC_Q_CREATE_VERSION_1);
13011
13012 bf_set(lpfc_mbx_wq_create_wqe_count,
13013 &wq_create->u.request_1, wq->entry_count);
13014 bf_set(lpfc_mbx_wq_create_wqe_size,
13015 &wq_create->u.request_1,
13016 LPFC_WQ_WQE_SIZE_128);
13017 bf_set(lpfc_mbx_wq_create_page_size,
13018 &wq_create->u.request_1,
13019 (PAGE_SIZE/SLI4_PAGE_SIZE));
13020 page = wq_create->u.request_1.page;
13021 break;
13022 }
13023 break;
13024 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013025 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13026 wq->entry_count);
13027 switch (wq->entry_size) {
13028 default:
13029 case 64:
13030 bf_set(lpfc_mbx_wq_create_wqe_size,
13031 &wq_create->u.request_1,
13032 LPFC_WQ_WQE_SIZE_64);
13033 break;
13034 case 128:
James Smart0c651872013-07-15 18:33:23 -040013035 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13036 LPFC_WQ_SZ128_SUPPORT)) {
13037 status = -ERANGE;
13038 goto out;
13039 }
James Smart5a6f1332011-03-11 16:05:35 -050013040 bf_set(lpfc_mbx_wq_create_wqe_size,
13041 &wq_create->u.request_1,
13042 LPFC_WQ_WQE_SIZE_128);
13043 break;
13044 }
13045 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13046 (PAGE_SIZE/SLI4_PAGE_SIZE));
13047 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013048 break;
13049 default:
13050 status = -ERANGE;
13051 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013052 }
James Smart0c651872013-07-15 18:33:23 -040013053
James Smart4f774512009-05-22 14:52:35 -040013054 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013055 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013056 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13057 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013058 }
James Smart962bc512013-01-03 15:44:00 -050013059
13060 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13061 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13062
James Smart4f774512009-05-22 14:52:35 -040013063 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13064 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013065 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13066 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13067 if (shdr_status || shdr_add_status || rc) {
13068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13069 "2503 WQ_CREATE mailbox failed with "
13070 "status x%x add_status x%x, mbx status x%x\n",
13071 shdr_status, shdr_add_status, rc);
13072 status = -ENXIO;
13073 goto out;
13074 }
13075 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13076 if (wq->queue_id == 0xFFFF) {
13077 status = -ENXIO;
13078 goto out;
13079 }
James Smart962bc512013-01-03 15:44:00 -050013080 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13081 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13082 &wq_create->u.response);
13083 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13084 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13086 "3265 WQ[%d] doorbell format not "
13087 "supported: x%x\n", wq->queue_id,
13088 wq->db_format);
13089 status = -EINVAL;
13090 goto out;
13091 }
13092 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13093 &wq_create->u.response);
13094 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13095 if (!bar_memmap_p) {
13096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13097 "3263 WQ[%d] failed to memmap pci "
13098 "barset:x%x\n", wq->queue_id,
13099 pci_barset);
13100 status = -ENOMEM;
13101 goto out;
13102 }
13103 db_offset = wq_create->u.response.doorbell_offset;
13104 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13105 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13107 "3252 WQ[%d] doorbell offset not "
13108 "supported: x%x\n", wq->queue_id,
13109 db_offset);
13110 status = -EINVAL;
13111 goto out;
13112 }
13113 wq->db_regaddr = bar_memmap_p + db_offset;
13114 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013115 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13116 "format:x%x\n", wq->queue_id, pci_barset,
13117 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013118 } else {
13119 wq->db_format = LPFC_DB_LIST_FORMAT;
13120 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13121 }
James Smart4f774512009-05-22 14:52:35 -040013122 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013123 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013124 wq->subtype = subtype;
13125 wq->host_index = 0;
13126 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013127 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013128
13129 /* link the wq onto the parent cq child list */
13130 list_add_tail(&wq->list, &cq->child_list);
13131out:
James Smart8fa38512009-07-19 10:01:03 -040013132 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013133 return status;
13134}
13135
13136/**
James Smart73d91e52011-10-10 21:32:10 -040013137 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13138 * @phba: HBA structure that indicates port to create a queue on.
13139 * @rq: The queue structure to use for the receive queue.
13140 * @qno: The associated HBQ number
13141 *
13142 *
13143 * For SLI4 we need to adjust the RQ repost value based on
13144 * the number of buffers that are initially posted to the RQ.
13145 */
13146void
13147lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13148{
13149 uint32_t cnt;
13150
James Smart2e90f4b2011-12-13 13:22:37 -050013151 /* sanity check on queue memory */
13152 if (!rq)
13153 return;
James Smart73d91e52011-10-10 21:32:10 -040013154 cnt = lpfc_hbq_defs[qno]->entry_count;
13155
13156 /* Recalc repost for RQs based on buffers initially posted */
13157 cnt = (cnt >> 3);
13158 if (cnt < LPFC_QUEUE_MIN_REPOST)
13159 cnt = LPFC_QUEUE_MIN_REPOST;
13160
13161 rq->entry_repost = cnt;
13162}
13163
13164/**
James Smart4f774512009-05-22 14:52:35 -040013165 * lpfc_rq_create - Create a Receive Queue on the HBA
13166 * @phba: HBA structure that indicates port to create a queue on.
13167 * @hrq: The queue structure to use to create the header receive queue.
13168 * @drq: The queue structure to use to create the data receive queue.
13169 * @cq: The completion queue to bind this work queue to.
13170 *
13171 * This function creates a receive buffer queue pair , as detailed in @hrq and
13172 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13173 * to the HBA.
13174 *
13175 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13176 * struct is used to get the entry count that is necessary to determine the
13177 * number of pages to use for this queue. The @cq is used to indicate which
13178 * completion queue to bind received buffers that are posted to these queues to.
13179 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13180 * receive queue pair. This function is asynchronous and will wait for the
13181 * mailbox command to finish before continuing.
13182 *
13183 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013184 * memory this function will return -ENOMEM. If the queue create mailbox command
13185 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013186 **/
13187uint32_t
13188lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13189 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13190{
13191 struct lpfc_mbx_rq_create *rq_create;
13192 struct lpfc_dmabuf *dmabuf;
13193 LPFC_MBOXQ_t *mbox;
13194 int rc, length, status = 0;
13195 uint32_t shdr_status, shdr_add_status;
13196 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013197 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013198 void __iomem *bar_memmap_p;
13199 uint32_t db_offset;
13200 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013201
James Smart2e90f4b2011-12-13 13:22:37 -050013202 /* sanity check on queue memory */
13203 if (!hrq || !drq || !cq)
13204 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013205 if (!phba->sli4_hba.pc_sli4_params.supported)
13206 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013207
13208 if (hrq->entry_count != drq->entry_count)
13209 return -EINVAL;
13210 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13211 if (!mbox)
13212 return -ENOMEM;
13213 length = (sizeof(struct lpfc_mbx_rq_create) -
13214 sizeof(struct lpfc_sli4_cfg_mhdr));
13215 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13216 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13217 length, LPFC_SLI4_MBX_EMBED);
13218 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013219 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13220 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13221 phba->sli4_hba.pc_sli4_params.rqv);
13222 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13223 bf_set(lpfc_rq_context_rqe_count_1,
13224 &rq_create->u.request.context,
13225 hrq->entry_count);
13226 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013227 bf_set(lpfc_rq_context_rqe_size,
13228 &rq_create->u.request.context,
13229 LPFC_RQE_SIZE_8);
13230 bf_set(lpfc_rq_context_page_size,
13231 &rq_create->u.request.context,
13232 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013233 } else {
13234 switch (hrq->entry_count) {
13235 default:
13236 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13237 "2535 Unsupported RQ count. (%d)\n",
13238 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013239 if (hrq->entry_count < 512) {
13240 status = -EINVAL;
13241 goto out;
13242 }
James Smart5a6f1332011-03-11 16:05:35 -050013243 /* otherwise default to smallest count (drop through) */
13244 case 512:
13245 bf_set(lpfc_rq_context_rqe_count,
13246 &rq_create->u.request.context,
13247 LPFC_RQ_RING_SIZE_512);
13248 break;
13249 case 1024:
13250 bf_set(lpfc_rq_context_rqe_count,
13251 &rq_create->u.request.context,
13252 LPFC_RQ_RING_SIZE_1024);
13253 break;
13254 case 2048:
13255 bf_set(lpfc_rq_context_rqe_count,
13256 &rq_create->u.request.context,
13257 LPFC_RQ_RING_SIZE_2048);
13258 break;
13259 case 4096:
13260 bf_set(lpfc_rq_context_rqe_count,
13261 &rq_create->u.request.context,
13262 LPFC_RQ_RING_SIZE_4096);
13263 break;
13264 }
13265 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13266 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013267 }
13268 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13269 cq->queue_id);
13270 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13271 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013272 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013273 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013274 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13275 putPaddrLow(dmabuf->phys);
13276 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13277 putPaddrHigh(dmabuf->phys);
13278 }
James Smart962bc512013-01-03 15:44:00 -050013279 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13280 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13281
James Smart4f774512009-05-22 14:52:35 -040013282 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13283 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013284 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13285 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13286 if (shdr_status || shdr_add_status || rc) {
13287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13288 "2504 RQ_CREATE mailbox failed with "
13289 "status x%x add_status x%x, mbx status x%x\n",
13290 shdr_status, shdr_add_status, rc);
13291 status = -ENXIO;
13292 goto out;
13293 }
13294 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13295 if (hrq->queue_id == 0xFFFF) {
13296 status = -ENXIO;
13297 goto out;
13298 }
James Smart962bc512013-01-03 15:44:00 -050013299
13300 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13301 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13302 &rq_create->u.response);
13303 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13304 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13306 "3262 RQ [%d] doorbell format not "
13307 "supported: x%x\n", hrq->queue_id,
13308 hrq->db_format);
13309 status = -EINVAL;
13310 goto out;
13311 }
13312
13313 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13314 &rq_create->u.response);
13315 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13316 if (!bar_memmap_p) {
13317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13318 "3269 RQ[%d] failed to memmap pci "
13319 "barset:x%x\n", hrq->queue_id,
13320 pci_barset);
13321 status = -ENOMEM;
13322 goto out;
13323 }
13324
13325 db_offset = rq_create->u.response.doorbell_offset;
13326 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13327 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13329 "3270 RQ[%d] doorbell offset not "
13330 "supported: x%x\n", hrq->queue_id,
13331 db_offset);
13332 status = -EINVAL;
13333 goto out;
13334 }
13335 hrq->db_regaddr = bar_memmap_p + db_offset;
13336 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013337 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13338 "format:x%x\n", hrq->queue_id, pci_barset,
13339 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013340 } else {
13341 hrq->db_format = LPFC_DB_RING_FORMAT;
13342 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13343 }
James Smart4f774512009-05-22 14:52:35 -040013344 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013345 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013346 hrq->subtype = subtype;
13347 hrq->host_index = 0;
13348 hrq->hba_index = 0;
13349
13350 /* now create the data queue */
13351 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13352 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13353 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050013354 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13355 phba->sli4_hba.pc_sli4_params.rqv);
13356 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13357 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040013358 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050013359 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013360 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13361 LPFC_RQE_SIZE_8);
13362 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13363 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013364 } else {
13365 switch (drq->entry_count) {
13366 default:
13367 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13368 "2536 Unsupported RQ count. (%d)\n",
13369 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013370 if (drq->entry_count < 512) {
13371 status = -EINVAL;
13372 goto out;
13373 }
James Smart5a6f1332011-03-11 16:05:35 -050013374 /* otherwise default to smallest count (drop through) */
13375 case 512:
13376 bf_set(lpfc_rq_context_rqe_count,
13377 &rq_create->u.request.context,
13378 LPFC_RQ_RING_SIZE_512);
13379 break;
13380 case 1024:
13381 bf_set(lpfc_rq_context_rqe_count,
13382 &rq_create->u.request.context,
13383 LPFC_RQ_RING_SIZE_1024);
13384 break;
13385 case 2048:
13386 bf_set(lpfc_rq_context_rqe_count,
13387 &rq_create->u.request.context,
13388 LPFC_RQ_RING_SIZE_2048);
13389 break;
13390 case 4096:
13391 bf_set(lpfc_rq_context_rqe_count,
13392 &rq_create->u.request.context,
13393 LPFC_RQ_RING_SIZE_4096);
13394 break;
13395 }
13396 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13397 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013398 }
13399 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13400 cq->queue_id);
13401 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13402 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013403 list_for_each_entry(dmabuf, &drq->page_list, list) {
13404 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13405 putPaddrLow(dmabuf->phys);
13406 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13407 putPaddrHigh(dmabuf->phys);
13408 }
James Smart962bc512013-01-03 15:44:00 -050013409 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13410 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040013411 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13412 /* The IOCTL status is embedded in the mailbox subheader. */
13413 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13414 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13415 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13416 if (shdr_status || shdr_add_status || rc) {
13417 status = -ENXIO;
13418 goto out;
13419 }
13420 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13421 if (drq->queue_id == 0xFFFF) {
13422 status = -ENXIO;
13423 goto out;
13424 }
13425 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013426 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013427 drq->subtype = subtype;
13428 drq->host_index = 0;
13429 drq->hba_index = 0;
13430
13431 /* link the header and data RQs onto the parent cq child list */
13432 list_add_tail(&hrq->list, &cq->child_list);
13433 list_add_tail(&drq->list, &cq->child_list);
13434
13435out:
James Smart8fa38512009-07-19 10:01:03 -040013436 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013437 return status;
13438}
13439
13440/**
13441 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13442 * @eq: The queue structure associated with the queue to destroy.
13443 *
13444 * This function destroys a queue, as detailed in @eq by sending an mailbox
13445 * command, specific to the type of queue, to the HBA.
13446 *
13447 * The @eq struct is used to get the queue ID of the queue to destroy.
13448 *
13449 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013450 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013451 **/
13452uint32_t
13453lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13454{
13455 LPFC_MBOXQ_t *mbox;
13456 int rc, length, status = 0;
13457 uint32_t shdr_status, shdr_add_status;
13458 union lpfc_sli4_cfg_shdr *shdr;
13459
James Smart2e90f4b2011-12-13 13:22:37 -050013460 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013461 if (!eq)
13462 return -ENODEV;
13463 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13464 if (!mbox)
13465 return -ENOMEM;
13466 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13467 sizeof(struct lpfc_sli4_cfg_mhdr));
13468 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13469 LPFC_MBOX_OPCODE_EQ_DESTROY,
13470 length, LPFC_SLI4_MBX_EMBED);
13471 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13472 eq->queue_id);
13473 mbox->vport = eq->phba->pport;
13474 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13475
13476 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13477 /* The IOCTL status is embedded in the mailbox subheader. */
13478 shdr = (union lpfc_sli4_cfg_shdr *)
13479 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13480 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13481 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13482 if (shdr_status || shdr_add_status || rc) {
13483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13484 "2505 EQ_DESTROY mailbox failed with "
13485 "status x%x add_status x%x, mbx status x%x\n",
13486 shdr_status, shdr_add_status, rc);
13487 status = -ENXIO;
13488 }
13489
13490 /* Remove eq from any list */
13491 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013492 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013493 return status;
13494}
13495
13496/**
13497 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13498 * @cq: The queue structure associated with the queue to destroy.
13499 *
13500 * This function destroys a queue, as detailed in @cq by sending an mailbox
13501 * command, specific to the type of queue, to the HBA.
13502 *
13503 * The @cq struct is used to get the queue ID of the queue to destroy.
13504 *
13505 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013506 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013507 **/
13508uint32_t
13509lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13510{
13511 LPFC_MBOXQ_t *mbox;
13512 int rc, length, status = 0;
13513 uint32_t shdr_status, shdr_add_status;
13514 union lpfc_sli4_cfg_shdr *shdr;
13515
James Smart2e90f4b2011-12-13 13:22:37 -050013516 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013517 if (!cq)
13518 return -ENODEV;
13519 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13520 if (!mbox)
13521 return -ENOMEM;
13522 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13523 sizeof(struct lpfc_sli4_cfg_mhdr));
13524 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13525 LPFC_MBOX_OPCODE_CQ_DESTROY,
13526 length, LPFC_SLI4_MBX_EMBED);
13527 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13528 cq->queue_id);
13529 mbox->vport = cq->phba->pport;
13530 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13531 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13532 /* The IOCTL status is embedded in the mailbox subheader. */
13533 shdr = (union lpfc_sli4_cfg_shdr *)
13534 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13535 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13536 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13537 if (shdr_status || shdr_add_status || rc) {
13538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13539 "2506 CQ_DESTROY mailbox failed with "
13540 "status x%x add_status x%x, mbx status x%x\n",
13541 shdr_status, shdr_add_status, rc);
13542 status = -ENXIO;
13543 }
13544 /* Remove cq from any list */
13545 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013546 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013547 return status;
13548}
13549
13550/**
James Smart04c68492009-05-22 14:52:52 -040013551 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13552 * @qm: The queue structure associated with the queue to destroy.
13553 *
13554 * This function destroys a queue, as detailed in @mq by sending an mailbox
13555 * command, specific to the type of queue, to the HBA.
13556 *
13557 * The @mq struct is used to get the queue ID of the queue to destroy.
13558 *
13559 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013560 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013561 **/
13562uint32_t
13563lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13564{
13565 LPFC_MBOXQ_t *mbox;
13566 int rc, length, status = 0;
13567 uint32_t shdr_status, shdr_add_status;
13568 union lpfc_sli4_cfg_shdr *shdr;
13569
James Smart2e90f4b2011-12-13 13:22:37 -050013570 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013571 if (!mq)
13572 return -ENODEV;
13573 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13574 if (!mbox)
13575 return -ENOMEM;
13576 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13577 sizeof(struct lpfc_sli4_cfg_mhdr));
13578 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13579 LPFC_MBOX_OPCODE_MQ_DESTROY,
13580 length, LPFC_SLI4_MBX_EMBED);
13581 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13582 mq->queue_id);
13583 mbox->vport = mq->phba->pport;
13584 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13585 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13586 /* The IOCTL status is embedded in the mailbox subheader. */
13587 shdr = (union lpfc_sli4_cfg_shdr *)
13588 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13589 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13590 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13591 if (shdr_status || shdr_add_status || rc) {
13592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13593 "2507 MQ_DESTROY mailbox failed with "
13594 "status x%x add_status x%x, mbx status x%x\n",
13595 shdr_status, shdr_add_status, rc);
13596 status = -ENXIO;
13597 }
13598 /* Remove mq from any list */
13599 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013600 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013601 return status;
13602}
13603
13604/**
James Smart4f774512009-05-22 14:52:35 -040013605 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13606 * @wq: The queue structure associated with the queue to destroy.
13607 *
13608 * This function destroys a queue, as detailed in @wq by sending an mailbox
13609 * command, specific to the type of queue, to the HBA.
13610 *
13611 * The @wq struct is used to get the queue ID of the queue to destroy.
13612 *
13613 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013614 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013615 **/
13616uint32_t
13617lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13618{
13619 LPFC_MBOXQ_t *mbox;
13620 int rc, length, status = 0;
13621 uint32_t shdr_status, shdr_add_status;
13622 union lpfc_sli4_cfg_shdr *shdr;
13623
James Smart2e90f4b2011-12-13 13:22:37 -050013624 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013625 if (!wq)
13626 return -ENODEV;
13627 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13628 if (!mbox)
13629 return -ENOMEM;
13630 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13631 sizeof(struct lpfc_sli4_cfg_mhdr));
13632 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13633 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13634 length, LPFC_SLI4_MBX_EMBED);
13635 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13636 wq->queue_id);
13637 mbox->vport = wq->phba->pport;
13638 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13639 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13640 shdr = (union lpfc_sli4_cfg_shdr *)
13641 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13642 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13643 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13644 if (shdr_status || shdr_add_status || rc) {
13645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13646 "2508 WQ_DESTROY mailbox failed with "
13647 "status x%x add_status x%x, mbx status x%x\n",
13648 shdr_status, shdr_add_status, rc);
13649 status = -ENXIO;
13650 }
13651 /* Remove wq from any list */
13652 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013653 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013654 return status;
13655}
13656
13657/**
13658 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13659 * @rq: The queue structure associated with the queue to destroy.
13660 *
13661 * This function destroys a queue, as detailed in @rq by sending an mailbox
13662 * command, specific to the type of queue, to the HBA.
13663 *
13664 * The @rq struct is used to get the queue ID of the queue to destroy.
13665 *
13666 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013667 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013668 **/
13669uint32_t
13670lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13671 struct lpfc_queue *drq)
13672{
13673 LPFC_MBOXQ_t *mbox;
13674 int rc, length, status = 0;
13675 uint32_t shdr_status, shdr_add_status;
13676 union lpfc_sli4_cfg_shdr *shdr;
13677
James Smart2e90f4b2011-12-13 13:22:37 -050013678 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013679 if (!hrq || !drq)
13680 return -ENODEV;
13681 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13682 if (!mbox)
13683 return -ENOMEM;
13684 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013685 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013686 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13687 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13688 length, LPFC_SLI4_MBX_EMBED);
13689 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13690 hrq->queue_id);
13691 mbox->vport = hrq->phba->pport;
13692 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13693 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13694 /* The IOCTL status is embedded in the mailbox subheader. */
13695 shdr = (union lpfc_sli4_cfg_shdr *)
13696 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13697 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13698 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13699 if (shdr_status || shdr_add_status || rc) {
13700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13701 "2509 RQ_DESTROY mailbox failed with "
13702 "status x%x add_status x%x, mbx status x%x\n",
13703 shdr_status, shdr_add_status, rc);
13704 if (rc != MBX_TIMEOUT)
13705 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13706 return -ENXIO;
13707 }
13708 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13709 drq->queue_id);
13710 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13711 shdr = (union lpfc_sli4_cfg_shdr *)
13712 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13713 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13714 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13715 if (shdr_status || shdr_add_status || rc) {
13716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13717 "2510 RQ_DESTROY mailbox failed with "
13718 "status x%x add_status x%x, mbx status x%x\n",
13719 shdr_status, shdr_add_status, rc);
13720 status = -ENXIO;
13721 }
13722 list_del_init(&hrq->list);
13723 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013724 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013725 return status;
13726}
13727
13728/**
13729 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13730 * @phba: The virtual port for which this call being executed.
13731 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13732 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13733 * @xritag: the xritag that ties this io to the SGL pages.
13734 *
13735 * This routine will post the sgl pages for the IO that has the xritag
13736 * that is in the iocbq structure. The xritag is assigned during iocbq
13737 * creation and persists for as long as the driver is loaded.
13738 * if the caller has fewer than 256 scatter gather segments to map then
13739 * pdma_phys_addr1 should be 0.
13740 * If the caller needs to map more than 256 scatter gather segment then
13741 * pdma_phys_addr1 should be a valid physical address.
13742 * physical address for SGLs must be 64 byte aligned.
13743 * If you are going to map 2 SGL's then the first one must have 256 entries
13744 * the second sgl can have between 1 and 256 entries.
13745 *
13746 * Return codes:
13747 * 0 - Success
13748 * -ENXIO, -ENOMEM - Failure
13749 **/
13750int
13751lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13752 dma_addr_t pdma_phys_addr0,
13753 dma_addr_t pdma_phys_addr1,
13754 uint16_t xritag)
13755{
13756 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13757 LPFC_MBOXQ_t *mbox;
13758 int rc;
13759 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013760 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013761 union lpfc_sli4_cfg_shdr *shdr;
13762
13763 if (xritag == NO_XRI) {
13764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13765 "0364 Invalid param:\n");
13766 return -EINVAL;
13767 }
13768
13769 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13770 if (!mbox)
13771 return -ENOMEM;
13772
13773 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13774 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13775 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013776 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013777
13778 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13779 &mbox->u.mqe.un.post_sgl_pages;
13780 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13781 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13782
13783 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13784 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13785 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13786 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13787
13788 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13789 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13790 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13791 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13792 if (!phba->sli4_hba.intr_enable)
13793 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013794 else {
James Smarta183a152011-10-10 21:32:43 -040013795 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013796 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13797 }
James Smart4f774512009-05-22 14:52:35 -040013798 /* The IOCTL status is embedded in the mailbox subheader. */
13799 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13800 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13801 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13802 if (rc != MBX_TIMEOUT)
13803 mempool_free(mbox, phba->mbox_mem_pool);
13804 if (shdr_status || shdr_add_status || rc) {
13805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13806 "2511 POST_SGL mailbox failed with "
13807 "status x%x add_status x%x, mbx status x%x\n",
13808 shdr_status, shdr_add_status, rc);
13809 rc = -ENXIO;
13810 }
13811 return 0;
13812}
James Smart4f774512009-05-22 14:52:35 -040013813
13814/**
James Smart88a2cfb2011-07-22 18:36:33 -040013815 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013816 * @phba: pointer to lpfc hba data structure.
13817 *
13818 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013819 * HBA consistent with the SLI-4 interface spec. This routine
13820 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13821 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013822 *
James Smart88a2cfb2011-07-22 18:36:33 -040013823 * Returns
13824 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13825 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13826 **/
James Smart6d368e52011-05-24 11:44:12 -040013827uint16_t
13828lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13829{
13830 unsigned long xri;
13831
13832 /*
13833 * Fetch the next logical xri. Because this index is logical,
13834 * the driver starts at 0 each time.
13835 */
13836 spin_lock_irq(&phba->hbalock);
13837 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13838 phba->sli4_hba.max_cfg_param.max_xri, 0);
13839 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13840 spin_unlock_irq(&phba->hbalock);
13841 return NO_XRI;
13842 } else {
13843 set_bit(xri, phba->sli4_hba.xri_bmask);
13844 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013845 }
James Smart6d368e52011-05-24 11:44:12 -040013846 spin_unlock_irq(&phba->hbalock);
13847 return xri;
13848}
13849
13850/**
13851 * lpfc_sli4_free_xri - Release an xri for reuse.
13852 * @phba: pointer to lpfc hba data structure.
13853 *
13854 * This routine is invoked to release an xri to the pool of
13855 * available rpis maintained by the driver.
13856 **/
13857void
13858__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13859{
13860 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013861 phba->sli4_hba.max_cfg_param.xri_used--;
13862 }
13863}
13864
13865/**
13866 * lpfc_sli4_free_xri - Release an xri for reuse.
13867 * @phba: pointer to lpfc hba data structure.
13868 *
13869 * This routine is invoked to release an xri to the pool of
13870 * available rpis maintained by the driver.
13871 **/
13872void
13873lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13874{
13875 spin_lock_irq(&phba->hbalock);
13876 __lpfc_sli4_free_xri(phba, xri);
13877 spin_unlock_irq(&phba->hbalock);
13878}
13879
13880/**
James Smart4f774512009-05-22 14:52:35 -040013881 * lpfc_sli4_next_xritag - Get an xritag for the io
13882 * @phba: Pointer to HBA context object.
13883 *
13884 * This function gets an xritag for the iocb. If there is no unused xritag
13885 * it will return 0xffff.
13886 * The function returns the allocated xritag if successful, else returns zero.
13887 * Zero is not a valid xritag.
13888 * The caller is not required to hold any lock.
13889 **/
13890uint16_t
13891lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13892{
James Smart6d368e52011-05-24 11:44:12 -040013893 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013894
James Smart6d368e52011-05-24 11:44:12 -040013895 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013896 if (xri_index == NO_XRI)
13897 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13898 "2004 Failed to allocate XRI.last XRITAG is %d"
13899 " Max XRI is %d, Used XRI is %d\n",
13900 xri_index,
13901 phba->sli4_hba.max_cfg_param.max_xri,
13902 phba->sli4_hba.max_cfg_param.xri_used);
13903 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013904}
13905
13906/**
James Smart6d368e52011-05-24 11:44:12 -040013907 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013908 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013909 * @post_sgl_list: pointer to els sgl entry list.
13910 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013911 *
13912 * This routine is invoked to post a block of driver's sgl pages to the
13913 * HBA using non-embedded mailbox command. No Lock is held. This routine
13914 * is only called when the driver is loading and after all IO has been
13915 * stopped.
13916 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013917static int
13918lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13919 struct list_head *post_sgl_list,
13920 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013921{
James Smart8a9d2e82012-05-09 21:16:12 -040013922 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013923 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13924 struct sgl_page_pairs *sgl_pg_pairs;
13925 void *viraddr;
13926 LPFC_MBOXQ_t *mbox;
13927 uint32_t reqlen, alloclen, pg_pairs;
13928 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013929 uint16_t xritag_start = 0;
13930 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013931 uint32_t shdr_status, shdr_add_status;
13932 union lpfc_sli4_cfg_shdr *shdr;
13933
James Smart8a9d2e82012-05-09 21:16:12 -040013934 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013935 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013936 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013937 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13938 "2559 Block sgl registration required DMA "
13939 "size (%d) great than a page\n", reqlen);
13940 return -ENOMEM;
13941 }
13942 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013943 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013944 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013945
13946 /* Allocate DMA memory and set up the non-embedded mailbox command */
13947 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13948 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13949 LPFC_SLI4_MBX_NEMBED);
13950
13951 if (alloclen < reqlen) {
13952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13953 "0285 Allocated DMA memory size (%d) is "
13954 "less than the requested DMA memory "
13955 "size (%d)\n", alloclen, reqlen);
13956 lpfc_sli4_mbox_cmd_free(phba, mbox);
13957 return -ENOMEM;
13958 }
James Smart4f774512009-05-22 14:52:35 -040013959 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013960 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013961 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13962 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13963
James Smart8a9d2e82012-05-09 21:16:12 -040013964 pg_pairs = 0;
13965 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013966 /* Set up the sge entry */
13967 sgl_pg_pairs->sgl_pg0_addr_lo =
13968 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13969 sgl_pg_pairs->sgl_pg0_addr_hi =
13970 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13971 sgl_pg_pairs->sgl_pg1_addr_lo =
13972 cpu_to_le32(putPaddrLow(0));
13973 sgl_pg_pairs->sgl_pg1_addr_hi =
13974 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013975
James Smart4f774512009-05-22 14:52:35 -040013976 /* Keep the first xritag on the list */
13977 if (pg_pairs == 0)
13978 xritag_start = sglq_entry->sli4_xritag;
13979 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013980 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013981 }
James Smart6d368e52011-05-24 11:44:12 -040013982
13983 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013984 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013985 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013986 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013987 if (!phba->sli4_hba.intr_enable)
13988 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13989 else {
James Smarta183a152011-10-10 21:32:43 -040013990 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013991 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13992 }
13993 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13994 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13995 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13996 if (rc != MBX_TIMEOUT)
13997 lpfc_sli4_mbox_cmd_free(phba, mbox);
13998 if (shdr_status || shdr_add_status || rc) {
13999 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14000 "2513 POST_SGL_BLOCK mailbox command failed "
14001 "status x%x add_status x%x mbx status x%x\n",
14002 shdr_status, shdr_add_status, rc);
14003 rc = -ENXIO;
14004 }
14005 return rc;
14006}
14007
14008/**
14009 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14010 * @phba: pointer to lpfc hba data structure.
14011 * @sblist: pointer to scsi buffer list.
14012 * @count: number of scsi buffers on the list.
14013 *
14014 * This routine is invoked to post a block of @count scsi sgl pages from a
14015 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14016 * No Lock is held.
14017 *
14018 **/
14019int
James Smart8a9d2e82012-05-09 21:16:12 -040014020lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14021 struct list_head *sblist,
14022 int count)
James Smart4f774512009-05-22 14:52:35 -040014023{
14024 struct lpfc_scsi_buf *psb;
14025 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14026 struct sgl_page_pairs *sgl_pg_pairs;
14027 void *viraddr;
14028 LPFC_MBOXQ_t *mbox;
14029 uint32_t reqlen, alloclen, pg_pairs;
14030 uint32_t mbox_tmo;
14031 uint16_t xritag_start = 0;
14032 int rc = 0;
14033 uint32_t shdr_status, shdr_add_status;
14034 dma_addr_t pdma_phys_bpl1;
14035 union lpfc_sli4_cfg_shdr *shdr;
14036
14037 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014038 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014039 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014040 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014041 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14042 "0217 Block sgl registration required DMA "
14043 "size (%d) great than a page\n", reqlen);
14044 return -ENOMEM;
14045 }
14046 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14047 if (!mbox) {
14048 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14049 "0283 Failed to allocate mbox cmd memory\n");
14050 return -ENOMEM;
14051 }
14052
14053 /* Allocate DMA memory and set up the non-embedded mailbox command */
14054 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14055 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14056 LPFC_SLI4_MBX_NEMBED);
14057
14058 if (alloclen < reqlen) {
14059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14060 "2561 Allocated DMA memory size (%d) is "
14061 "less than the requested DMA memory "
14062 "size (%d)\n", alloclen, reqlen);
14063 lpfc_sli4_mbox_cmd_free(phba, mbox);
14064 return -ENOMEM;
14065 }
James Smart6d368e52011-05-24 11:44:12 -040014066
James Smart4f774512009-05-22 14:52:35 -040014067 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014068 viraddr = mbox->sge_array->addr[0];
14069
14070 /* Set up the SGL pages in the non-embedded DMA pages */
14071 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14072 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14073
14074 pg_pairs = 0;
14075 list_for_each_entry(psb, sblist, list) {
14076 /* Set up the sge entry */
14077 sgl_pg_pairs->sgl_pg0_addr_lo =
14078 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14079 sgl_pg_pairs->sgl_pg0_addr_hi =
14080 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14081 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14082 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14083 else
14084 pdma_phys_bpl1 = 0;
14085 sgl_pg_pairs->sgl_pg1_addr_lo =
14086 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14087 sgl_pg_pairs->sgl_pg1_addr_hi =
14088 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14089 /* Keep the first xritag on the list */
14090 if (pg_pairs == 0)
14091 xritag_start = psb->cur_iocbq.sli4_xritag;
14092 sgl_pg_pairs++;
14093 pg_pairs++;
14094 }
14095 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14096 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14097 /* Perform endian conversion if necessary */
14098 sgl->word0 = cpu_to_le32(sgl->word0);
14099
14100 if (!phba->sli4_hba.intr_enable)
14101 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14102 else {
James Smarta183a152011-10-10 21:32:43 -040014103 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014104 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14105 }
14106 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14107 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14108 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14109 if (rc != MBX_TIMEOUT)
14110 lpfc_sli4_mbox_cmd_free(phba, mbox);
14111 if (shdr_status || shdr_add_status || rc) {
14112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14113 "2564 POST_SGL_BLOCK mailbox command failed "
14114 "status x%x add_status x%x mbx status x%x\n",
14115 shdr_status, shdr_add_status, rc);
14116 rc = -ENXIO;
14117 }
14118 return rc;
14119}
14120
14121/**
14122 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14123 * @phba: pointer to lpfc_hba struct that the frame was received on
14124 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14125 *
14126 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14127 * valid type of frame that the LPFC driver will handle. This function will
14128 * return a zero if the frame is a valid frame or a non zero value when the
14129 * frame does not pass the check.
14130 **/
14131static int
14132lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14133{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014134 /* make rctl_names static to save stack space */
14135 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014136 char *type_names[] = FC_TYPE_NAMES_INIT;
14137 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014138 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014139
14140 switch (fc_hdr->fh_r_ctl) {
14141 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14142 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14143 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14144 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14145 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14146 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14147 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14148 case FC_RCTL_DD_CMD_STATUS: /* command status */
14149 case FC_RCTL_ELS_REQ: /* extended link services request */
14150 case FC_RCTL_ELS_REP: /* extended link services reply */
14151 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14152 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14153 case FC_RCTL_BA_NOP: /* basic link service NOP */
14154 case FC_RCTL_BA_ABTS: /* basic link service abort */
14155 case FC_RCTL_BA_RMC: /* remove connection */
14156 case FC_RCTL_BA_ACC: /* basic accept */
14157 case FC_RCTL_BA_RJT: /* basic reject */
14158 case FC_RCTL_BA_PRMT:
14159 case FC_RCTL_ACK_1: /* acknowledge_1 */
14160 case FC_RCTL_ACK_0: /* acknowledge_0 */
14161 case FC_RCTL_P_RJT: /* port reject */
14162 case FC_RCTL_F_RJT: /* fabric reject */
14163 case FC_RCTL_P_BSY: /* port busy */
14164 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14165 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14166 case FC_RCTL_LCR: /* link credit reset */
14167 case FC_RCTL_END: /* end */
14168 break;
14169 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14170 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14171 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14172 return lpfc_fc_frame_check(phba, fc_hdr);
14173 default:
14174 goto drop;
14175 }
14176 switch (fc_hdr->fh_type) {
14177 case FC_TYPE_BLS:
14178 case FC_TYPE_ELS:
14179 case FC_TYPE_FCP:
14180 case FC_TYPE_CT:
14181 break;
14182 case FC_TYPE_IP:
14183 case FC_TYPE_ILS:
14184 default:
14185 goto drop;
14186 }
James Smart546fc852011-03-11 16:06:29 -050014187
James Smart4f774512009-05-22 14:52:35 -040014188 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014189 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14190 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14191 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14192 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014193 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14194 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014195 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14196 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014197 return 0;
14198drop:
14199 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14200 "2539 Dropped frame rctl:%s type:%s\n",
14201 rctl_names[fc_hdr->fh_r_ctl],
14202 type_names[fc_hdr->fh_type]);
14203 return 1;
14204}
14205
14206/**
14207 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14208 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14209 *
14210 * This function processes the FC header to retrieve the VFI from the VF
14211 * header, if one exists. This function will return the VFI if one exists
14212 * or 0 if no VSAN Header exists.
14213 **/
14214static uint32_t
14215lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14216{
14217 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14218
14219 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14220 return 0;
14221 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14222}
14223
14224/**
14225 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14226 * @phba: Pointer to the HBA structure to search for the vport on
14227 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14228 * @fcfi: The FC Fabric ID that the frame came from
14229 *
14230 * This function searches the @phba for a vport that matches the content of the
14231 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14232 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14233 * returns the matching vport pointer or NULL if unable to match frame to a
14234 * vport.
14235 **/
14236static struct lpfc_vport *
14237lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14238 uint16_t fcfi)
14239{
14240 struct lpfc_vport **vports;
14241 struct lpfc_vport *vport = NULL;
14242 int i;
14243 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14244 fc_hdr->fh_d_id[1] << 8 |
14245 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014246
James Smartbf086112011-08-21 21:48:13 -040014247 if (did == Fabric_DID)
14248 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014249 if ((phba->pport->fc_flag & FC_PT2PT) &&
14250 !(phba->link_state == LPFC_HBA_READY))
14251 return phba->pport;
14252
James Smart4f774512009-05-22 14:52:35 -040014253 vports = lpfc_create_vport_work_array(phba);
14254 if (vports != NULL)
14255 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14256 if (phba->fcf.fcfi == fcfi &&
14257 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14258 vports[i]->fc_myDID == did) {
14259 vport = vports[i];
14260 break;
14261 }
14262 }
14263 lpfc_destroy_vport_work_array(phba, vports);
14264 return vport;
14265}
14266
14267/**
James Smart45ed1192009-10-02 15:17:02 -040014268 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14269 * @vport: The vport to work on.
14270 *
14271 * This function updates the receive sequence time stamp for this vport. The
14272 * receive sequence time stamp indicates the time that the last frame of the
14273 * the sequence that has been idle for the longest amount of time was received.
14274 * the driver uses this time stamp to indicate if any received sequences have
14275 * timed out.
14276 **/
14277void
14278lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14279{
14280 struct lpfc_dmabuf *h_buf;
14281 struct hbq_dmabuf *dmabuf = NULL;
14282
14283 /* get the oldest sequence on the rcv list */
14284 h_buf = list_get_first(&vport->rcv_buffer_list,
14285 struct lpfc_dmabuf, list);
14286 if (!h_buf)
14287 return;
14288 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14289 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14290}
14291
14292/**
14293 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14294 * @vport: The vport that the received sequences were sent to.
14295 *
14296 * This function cleans up all outstanding received sequences. This is called
14297 * by the driver when a link event or user action invalidates all the received
14298 * sequences.
14299 **/
14300void
14301lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14302{
14303 struct lpfc_dmabuf *h_buf, *hnext;
14304 struct lpfc_dmabuf *d_buf, *dnext;
14305 struct hbq_dmabuf *dmabuf = NULL;
14306
14307 /* start with the oldest sequence on the rcv list */
14308 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14309 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14310 list_del_init(&dmabuf->hbuf.list);
14311 list_for_each_entry_safe(d_buf, dnext,
14312 &dmabuf->dbuf.list, list) {
14313 list_del_init(&d_buf->list);
14314 lpfc_in_buf_free(vport->phba, d_buf);
14315 }
14316 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14317 }
14318}
14319
14320/**
14321 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14322 * @vport: The vport that the received sequences were sent to.
14323 *
14324 * This function determines whether any received sequences have timed out by
14325 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14326 * indicates that there is at least one timed out sequence this routine will
14327 * go through the received sequences one at a time from most inactive to most
14328 * active to determine which ones need to be cleaned up. Once it has determined
14329 * that a sequence needs to be cleaned up it will simply free up the resources
14330 * without sending an abort.
14331 **/
14332void
14333lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14334{
14335 struct lpfc_dmabuf *h_buf, *hnext;
14336 struct lpfc_dmabuf *d_buf, *dnext;
14337 struct hbq_dmabuf *dmabuf = NULL;
14338 unsigned long timeout;
14339 int abort_count = 0;
14340
14341 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14342 vport->rcv_buffer_time_stamp);
14343 if (list_empty(&vport->rcv_buffer_list) ||
14344 time_before(jiffies, timeout))
14345 return;
14346 /* start with the oldest sequence on the rcv list */
14347 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14348 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14349 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14350 dmabuf->time_stamp);
14351 if (time_before(jiffies, timeout))
14352 break;
14353 abort_count++;
14354 list_del_init(&dmabuf->hbuf.list);
14355 list_for_each_entry_safe(d_buf, dnext,
14356 &dmabuf->dbuf.list, list) {
14357 list_del_init(&d_buf->list);
14358 lpfc_in_buf_free(vport->phba, d_buf);
14359 }
14360 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14361 }
14362 if (abort_count)
14363 lpfc_update_rcv_time_stamp(vport);
14364}
14365
14366/**
James Smart4f774512009-05-22 14:52:35 -040014367 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14368 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14369 *
14370 * This function searches through the existing incomplete sequences that have
14371 * been sent to this @vport. If the frame matches one of the incomplete
14372 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14373 * make up that sequence. If no sequence is found that matches this frame then
14374 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14375 * This function returns a pointer to the first dmabuf in the sequence list that
14376 * the frame was linked to.
14377 **/
14378static struct hbq_dmabuf *
14379lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14380{
14381 struct fc_frame_header *new_hdr;
14382 struct fc_frame_header *temp_hdr;
14383 struct lpfc_dmabuf *d_buf;
14384 struct lpfc_dmabuf *h_buf;
14385 struct hbq_dmabuf *seq_dmabuf = NULL;
14386 struct hbq_dmabuf *temp_dmabuf = NULL;
14387
James Smart4d9ab992009-10-02 15:16:39 -040014388 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014389 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014390 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14391 /* Use the hdr_buf to find the sequence that this frame belongs to */
14392 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14393 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14394 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14395 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14396 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14397 continue;
14398 /* found a pending sequence that matches this frame */
14399 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14400 break;
14401 }
14402 if (!seq_dmabuf) {
14403 /*
14404 * This indicates first frame received for this sequence.
14405 * Queue the buffer on the vport's rcv_buffer_list.
14406 */
14407 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040014408 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014409 return dmabuf;
14410 }
14411 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050014412 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14413 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040014414 list_del_init(&seq_dmabuf->hbuf.list);
14415 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14416 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014417 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014418 return dmabuf;
14419 }
James Smart45ed1192009-10-02 15:17:02 -040014420 /* move this sequence to the tail to indicate a young sequence */
14421 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14422 seq_dmabuf->time_stamp = jiffies;
14423 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014424 if (list_empty(&seq_dmabuf->dbuf.list)) {
14425 temp_hdr = dmabuf->hbuf.virt;
14426 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14427 return seq_dmabuf;
14428 }
James Smart4f774512009-05-22 14:52:35 -040014429 /* find the correct place in the sequence to insert this frame */
14430 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14431 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14432 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14433 /*
14434 * If the frame's sequence count is greater than the frame on
14435 * the list then insert the frame right after this frame
14436 */
James Smarteeead812009-12-21 17:01:23 -050014437 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14438 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014439 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14440 return seq_dmabuf;
14441 }
14442 }
14443 return NULL;
14444}
14445
14446/**
James Smart6669f9b2009-10-02 15:16:45 -040014447 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14448 * @vport: pointer to a vitural port
14449 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14450 *
14451 * This function tries to abort from the partially assembed sequence, described
14452 * by the information from basic abbort @dmabuf. It checks to see whether such
14453 * partially assembled sequence held by the driver. If so, it shall free up all
14454 * the frames from the partially assembled sequence.
14455 *
14456 * Return
14457 * true -- if there is matching partially assembled sequence present and all
14458 * the frames freed with the sequence;
14459 * false -- if there is no matching partially assembled sequence present so
14460 * nothing got aborted in the lower layer driver
14461 **/
14462static bool
14463lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14464 struct hbq_dmabuf *dmabuf)
14465{
14466 struct fc_frame_header *new_hdr;
14467 struct fc_frame_header *temp_hdr;
14468 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14469 struct hbq_dmabuf *seq_dmabuf = NULL;
14470
14471 /* Use the hdr_buf to find the sequence that matches this frame */
14472 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14473 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14474 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14475 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14476 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14477 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14478 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14479 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14480 continue;
14481 /* found a pending sequence that matches this frame */
14482 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14483 break;
14484 }
14485
14486 /* Free up all the frames from the partially assembled sequence */
14487 if (seq_dmabuf) {
14488 list_for_each_entry_safe(d_buf, n_buf,
14489 &seq_dmabuf->dbuf.list, list) {
14490 list_del_init(&d_buf->list);
14491 lpfc_in_buf_free(vport->phba, d_buf);
14492 }
14493 return true;
14494 }
14495 return false;
14496}
14497
14498/**
James Smart6dd9e312013-01-03 15:43:37 -050014499 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14500 * @vport: pointer to a vitural port
14501 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14502 *
14503 * This function tries to abort from the assembed sequence from upper level
14504 * protocol, described by the information from basic abbort @dmabuf. It
14505 * checks to see whether such pending context exists at upper level protocol.
14506 * If so, it shall clean up the pending context.
14507 *
14508 * Return
14509 * true -- if there is matching pending context of the sequence cleaned
14510 * at ulp;
14511 * false -- if there is no matching pending context of the sequence present
14512 * at ulp.
14513 **/
14514static bool
14515lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14516{
14517 struct lpfc_hba *phba = vport->phba;
14518 int handled;
14519
14520 /* Accepting abort at ulp with SLI4 only */
14521 if (phba->sli_rev < LPFC_SLI_REV4)
14522 return false;
14523
14524 /* Register all caring upper level protocols to attend abort */
14525 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14526 if (handled)
14527 return true;
14528
14529 return false;
14530}
14531
14532/**
James Smart546fc852011-03-11 16:06:29 -050014533 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014534 * @phba: Pointer to HBA context object.
14535 * @cmd_iocbq: pointer to the command iocbq structure.
14536 * @rsp_iocbq: pointer to the response iocbq structure.
14537 *
James Smart546fc852011-03-11 16:06:29 -050014538 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014539 * event. It properly releases the memory allocated to the sequence abort
14540 * accept iocb.
14541 **/
14542static void
James Smart546fc852011-03-11 16:06:29 -050014543lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014544 struct lpfc_iocbq *cmd_iocbq,
14545 struct lpfc_iocbq *rsp_iocbq)
14546{
James Smart6dd9e312013-01-03 15:43:37 -050014547 struct lpfc_nodelist *ndlp;
14548
14549 if (cmd_iocbq) {
14550 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14551 lpfc_nlp_put(ndlp);
14552 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014553 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050014554 }
James Smart6b5151f2012-01-18 16:24:06 -050014555
14556 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14557 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14559 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14560 rsp_iocbq->iocb.ulpStatus,
14561 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014562}
14563
14564/**
James Smart6d368e52011-05-24 11:44:12 -040014565 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14566 * @phba: Pointer to HBA context object.
14567 * @xri: xri id in transaction.
14568 *
14569 * This function validates the xri maps to the known range of XRIs allocated an
14570 * used by the driver.
14571 **/
James Smart7851fe22011-07-22 18:36:52 -040014572uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014573lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14574 uint16_t xri)
14575{
14576 int i;
14577
14578 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14579 if (xri == phba->sli4_hba.xri_ids[i])
14580 return i;
14581 }
14582 return NO_XRI;
14583}
14584
James Smart6d368e52011-05-24 11:44:12 -040014585/**
James Smart546fc852011-03-11 16:06:29 -050014586 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014587 * @phba: Pointer to HBA context object.
14588 * @fc_hdr: pointer to a FC frame header.
14589 *
James Smart546fc852011-03-11 16:06:29 -050014590 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014591 * event after aborting the sequence handling.
14592 **/
14593static void
James Smart6dd9e312013-01-03 15:43:37 -050014594lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14595 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040014596{
James Smart6dd9e312013-01-03 15:43:37 -050014597 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040014598 struct lpfc_iocbq *ctiocb = NULL;
14599 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014600 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014601 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014602 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014603 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014604
14605 if (!lpfc_is_link_up(phba))
14606 return;
14607
14608 sid = sli4_sid_from_fc_hdr(fc_hdr);
14609 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014610 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014611
James Smart6dd9e312013-01-03 15:43:37 -050014612 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040014613 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050014614 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14615 if (!ndlp) {
14616 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14617 "1268 Failed to allocate ndlp for "
14618 "oxid:x%x SID:x%x\n", oxid, sid);
14619 return;
14620 }
14621 lpfc_nlp_init(vport, ndlp, sid);
14622 /* Put ndlp onto pport node list */
14623 lpfc_enqueue_node(vport, ndlp);
14624 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14625 /* re-setup ndlp without removing from node list */
14626 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14627 if (!ndlp) {
14628 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14629 "3275 Failed to active ndlp found "
14630 "for oxid:x%x SID:x%x\n", oxid, sid);
14631 return;
14632 }
James Smart6669f9b2009-10-02 15:16:45 -040014633 }
14634
James Smart546fc852011-03-11 16:06:29 -050014635 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014636 ctiocb = lpfc_sli_get_iocbq(phba);
14637 if (!ctiocb)
14638 return;
14639
James Smart5ffc2662009-11-18 15:39:44 -050014640 /* Extract the F_CTL field from FC_HDR */
14641 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14642
James Smart6669f9b2009-10-02 15:16:45 -040014643 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014644 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014645 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014646 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14647 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14648 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14649
14650 /* Fill in the rest of iocb fields */
14651 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14652 icmd->ulpBdeCount = 0;
14653 icmd->ulpLe = 1;
14654 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014655 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050014656 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014657
James Smart6669f9b2009-10-02 15:16:45 -040014658 ctiocb->iocb_cmpl = NULL;
14659 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014660 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014661 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014662 ctiocb->sli4_xritag = NO_XRI;
14663
James Smartee0f4fe2012-05-09 21:19:14 -040014664 if (fctl & FC_FC_EX_CTX)
14665 /* Exchange responder sent the abort so we
14666 * own the oxid.
14667 */
14668 xri = oxid;
14669 else
14670 xri = rxid;
14671 lxri = lpfc_sli4_xri_inrange(phba, xri);
14672 if (lxri != NO_XRI)
14673 lpfc_set_rrq_active(phba, ndlp, lxri,
14674 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050014675 /* For BA_ABTS from exchange responder, if the logical xri with
14676 * the oxid maps to the FCP XRI range, the port no longer has
14677 * that exchange context, send a BLS_RJT. Override the IOCB for
14678 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050014679 */
James Smart6dd9e312013-01-03 15:43:37 -050014680 if ((fctl & FC_FC_EX_CTX) &&
14681 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14682 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14683 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14684 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14685 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14686 }
14687
14688 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14689 * the driver no longer has that exchange, send a BLS_RJT. Override
14690 * the IOCB for a BA_RJT.
14691 */
14692 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050014693 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14694 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14695 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14696 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14697 }
James Smart6669f9b2009-10-02 15:16:45 -040014698
James Smart5ffc2662009-11-18 15:39:44 -050014699 if (fctl & FC_FC_EX_CTX) {
14700 /* ABTS sent by responder to CT exchange, construction
14701 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14702 * field and RX_ID from ABTS for RX_ID field.
14703 */
James Smart546fc852011-03-11 16:06:29 -050014704 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014705 } else {
14706 /* ABTS sent by initiator to CT exchange, construction
14707 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014708 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014709 */
James Smart546fc852011-03-11 16:06:29 -050014710 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014711 }
James Smartf09c3ac2012-03-01 22:33:29 -050014712 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014713 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014714
James Smart546fc852011-03-11 16:06:29 -050014715 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050014716 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14717 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14718 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050014719
14720 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14721 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050014722 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14723 "2925 Failed to issue CT ABTS RSP x%x on "
14724 "xri x%x, Data x%x\n",
14725 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14726 phba->link_state);
14727 lpfc_nlp_put(ndlp);
14728 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050014729 lpfc_sli_release_iocbq(phba, ctiocb);
14730 }
James Smart6669f9b2009-10-02 15:16:45 -040014731}
14732
14733/**
14734 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14735 * @vport: Pointer to the vport on which this sequence was received
14736 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14737 *
14738 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14739 * receive sequence is only partially assembed by the driver, it shall abort
14740 * the partially assembled frames for the sequence. Otherwise, if the
14741 * unsolicited receive sequence has been completely assembled and passed to
14742 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14743 * unsolicited sequence has been aborted. After that, it will issue a basic
14744 * accept to accept the abort.
14745 **/
14746void
14747lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14748 struct hbq_dmabuf *dmabuf)
14749{
14750 struct lpfc_hba *phba = vport->phba;
14751 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014752 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050014753 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040014754
James Smart6669f9b2009-10-02 15:16:45 -040014755 /* Make a copy of fc_hdr before the dmabuf being released */
14756 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014757 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014758
James Smart5ffc2662009-11-18 15:39:44 -050014759 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050014760 /* ABTS by responder to exchange, no cleanup needed */
14761 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050014762 } else {
James Smart6dd9e312013-01-03 15:43:37 -050014763 /* ABTS by initiator to exchange, need to do cleanup */
14764 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14765 if (aborted == false)
14766 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050014767 }
James Smart6dd9e312013-01-03 15:43:37 -050014768 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14769
14770 /* Respond with BA_ACC or BA_RJT accordingly */
14771 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040014772}
14773
14774/**
James Smart4f774512009-05-22 14:52:35 -040014775 * lpfc_seq_complete - Indicates if a sequence is complete
14776 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14777 *
14778 * This function checks the sequence, starting with the frame described by
14779 * @dmabuf, to see if all the frames associated with this sequence are present.
14780 * the frames associated with this sequence are linked to the @dmabuf using the
14781 * dbuf list. This function looks for two major things. 1) That the first frame
14782 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14783 * set. 3) That there are no holes in the sequence count. The function will
14784 * return 1 when the sequence is complete, otherwise it will return 0.
14785 **/
14786static int
14787lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14788{
14789 struct fc_frame_header *hdr;
14790 struct lpfc_dmabuf *d_buf;
14791 struct hbq_dmabuf *seq_dmabuf;
14792 uint32_t fctl;
14793 int seq_count = 0;
14794
14795 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14796 /* make sure first fame of sequence has a sequence count of zero */
14797 if (hdr->fh_seq_cnt != seq_count)
14798 return 0;
14799 fctl = (hdr->fh_f_ctl[0] << 16 |
14800 hdr->fh_f_ctl[1] << 8 |
14801 hdr->fh_f_ctl[2]);
14802 /* If last frame of sequence we can return success. */
14803 if (fctl & FC_FC_END_SEQ)
14804 return 1;
14805 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14806 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14807 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14808 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014809 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014810 return 0;
14811 fctl = (hdr->fh_f_ctl[0] << 16 |
14812 hdr->fh_f_ctl[1] << 8 |
14813 hdr->fh_f_ctl[2]);
14814 /* If last frame of sequence we can return success. */
14815 if (fctl & FC_FC_END_SEQ)
14816 return 1;
14817 }
14818 return 0;
14819}
14820
14821/**
14822 * lpfc_prep_seq - Prep sequence for ULP processing
14823 * @vport: Pointer to the vport on which this sequence was received
14824 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14825 *
14826 * This function takes a sequence, described by a list of frames, and creates
14827 * a list of iocbq structures to describe the sequence. This iocbq list will be
14828 * used to issue to the generic unsolicited sequence handler. This routine
14829 * returns a pointer to the first iocbq in the list. If the function is unable
14830 * to allocate an iocbq then it throw out the received frames that were not
14831 * able to be described and return a pointer to the first iocbq. If unable to
14832 * allocate any iocbqs (including the first) this function will return NULL.
14833 **/
14834static struct lpfc_iocbq *
14835lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14836{
James Smart7851fe22011-07-22 18:36:52 -040014837 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014838 struct lpfc_dmabuf *d_buf, *n_buf;
14839 struct lpfc_iocbq *first_iocbq, *iocbq;
14840 struct fc_frame_header *fc_hdr;
14841 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014842 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014843 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014844
14845 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14846 /* remove from receive buffer list */
14847 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014848 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014849 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014850 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014851 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014852 /* Get an iocbq struct to fill in. */
14853 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14854 if (first_iocbq) {
14855 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014856 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014857 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014858
14859 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14860 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14861 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14862 first_iocbq->iocb.un.rcvels.parmRo =
14863 sli4_did_from_fc_hdr(fc_hdr);
14864 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14865 } else
14866 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014867 first_iocbq->iocb.ulpContext = NO_XRI;
14868 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14869 be16_to_cpu(fc_hdr->fh_ox_id);
14870 /* iocbq is prepped for internal consumption. Physical vpi. */
14871 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14872 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014873 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040014874 tot_len = bf_get(lpfc_rcqe_length,
14875 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14876
James Smart4f774512009-05-22 14:52:35 -040014877 first_iocbq->context2 = &seq_dmabuf->dbuf;
14878 first_iocbq->context3 = NULL;
14879 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040014880 if (tot_len > LPFC_DATA_BUF_SIZE)
14881 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040014882 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040014883 else
14884 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
14885
James Smart4f774512009-05-22 14:52:35 -040014886 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040014887
James Smart7851fe22011-07-22 18:36:52 -040014888 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014889 }
14890 iocbq = first_iocbq;
14891 /*
14892 * Each IOCBq can have two Buffers assigned, so go through the list
14893 * of buffers for this sequence and save two buffers in each IOCBq
14894 */
14895 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14896 if (!iocbq) {
14897 lpfc_in_buf_free(vport->phba, d_buf);
14898 continue;
14899 }
14900 if (!iocbq->context3) {
14901 iocbq->context3 = d_buf;
14902 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040014903 /* We need to get the size out of the right CQE */
14904 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14905 len = bf_get(lpfc_rcqe_length,
14906 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040014907 pbde = (struct ulp_bde64 *)
14908 &iocbq->iocb.unsli3.sli3Words[4];
14909 if (len > LPFC_DATA_BUF_SIZE)
14910 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14911 else
14912 pbde->tus.f.bdeSize = len;
14913
James Smart7851fe22011-07-22 18:36:52 -040014914 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14915 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014916 } else {
14917 iocbq = lpfc_sli_get_iocbq(vport->phba);
14918 if (!iocbq) {
14919 if (first_iocbq) {
14920 first_iocbq->iocb.ulpStatus =
14921 IOSTAT_FCP_RSP_ERROR;
14922 first_iocbq->iocb.un.ulpWord[4] =
14923 IOERR_NO_RESOURCES;
14924 }
14925 lpfc_in_buf_free(vport->phba, d_buf);
14926 continue;
14927 }
James Smart7851fe22011-07-22 18:36:52 -040014928 /* We need to get the size out of the right CQE */
14929 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14930 len = bf_get(lpfc_rcqe_length,
14931 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040014932 iocbq->context2 = d_buf;
14933 iocbq->context3 = NULL;
14934 iocbq->iocb.ulpBdeCount = 1;
14935 if (len > LPFC_DATA_BUF_SIZE)
14936 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14937 LPFC_DATA_BUF_SIZE;
14938 else
14939 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
14940
James Smart7851fe22011-07-22 18:36:52 -040014941 tot_len += len;
14942 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14943
James Smart4f774512009-05-22 14:52:35 -040014944 iocbq->iocb.un.rcvels.remoteID = sid;
14945 list_add_tail(&iocbq->list, &first_iocbq->list);
14946 }
14947 }
14948 return first_iocbq;
14949}
14950
James Smart6669f9b2009-10-02 15:16:45 -040014951static void
14952lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14953 struct hbq_dmabuf *seq_dmabuf)
14954{
14955 struct fc_frame_header *fc_hdr;
14956 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14957 struct lpfc_hba *phba = vport->phba;
14958
14959 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14960 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14961 if (!iocbq) {
14962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14963 "2707 Ring %d handler: Failed to allocate "
14964 "iocb Rctl x%x Type x%x received\n",
14965 LPFC_ELS_RING,
14966 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14967 return;
14968 }
14969 if (!lpfc_complete_unsol_iocb(phba,
14970 &phba->sli.ring[LPFC_ELS_RING],
14971 iocbq, fc_hdr->fh_r_ctl,
14972 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014973 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014974 "2540 Ring %d handler: unexpected Rctl "
14975 "x%x Type x%x received\n",
14976 LPFC_ELS_RING,
14977 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14978
14979 /* Free iocb created in lpfc_prep_seq */
14980 list_for_each_entry_safe(curr_iocb, next_iocb,
14981 &iocbq->list, list) {
14982 list_del_init(&curr_iocb->list);
14983 lpfc_sli_release_iocbq(phba, curr_iocb);
14984 }
14985 lpfc_sli_release_iocbq(phba, iocbq);
14986}
14987
James Smart4f774512009-05-22 14:52:35 -040014988/**
14989 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14990 * @phba: Pointer to HBA context object.
14991 *
14992 * This function is called with no lock held. This function processes all
14993 * the received buffers and gives it to upper layers when a received buffer
14994 * indicates that it is the final frame in the sequence. The interrupt
14995 * service routine processes received buffers at interrupt contexts and adds
14996 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14997 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14998 * appropriate receive function when the final frame in a sequence is received.
14999 **/
James Smart4d9ab992009-10-02 15:16:39 -040015000void
15001lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15002 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015003{
James Smart4d9ab992009-10-02 15:16:39 -040015004 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015005 struct fc_frame_header *fc_hdr;
15006 struct lpfc_vport *vport;
15007 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015008 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015009
James Smart4f774512009-05-22 14:52:35 -040015010 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015011 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15012 /* check to see if this a valid type of frame */
15013 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15014 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15015 return;
15016 }
James Smart7851fe22011-07-22 18:36:52 -040015017 if ((bf_get(lpfc_cqe_code,
15018 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15019 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15020 &dmabuf->cq_event.cqe.rcqe_cmpl);
15021 else
15022 fcfi = bf_get(lpfc_rcqe_fcf_id,
15023 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015024
James Smart4d9ab992009-10-02 15:16:39 -040015025 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015026 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015027 /* throw out the frame */
15028 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15029 return;
15030 }
James Smart939723a2012-05-09 21:19:03 -040015031
15032 /* d_id this frame is directed to */
15033 did = sli4_did_from_fc_hdr(fc_hdr);
15034
15035 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15036 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15037 (did != Fabric_DID)) {
15038 /*
15039 * Throw out the frame if we are not pt2pt.
15040 * The pt2pt protocol allows for discovery frames
15041 * to be received without a registered VPI.
15042 */
15043 if (!(vport->fc_flag & FC_PT2PT) ||
15044 (phba->link_state == LPFC_HBA_READY)) {
15045 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15046 return;
15047 }
15048 }
15049
James Smart6669f9b2009-10-02 15:16:45 -040015050 /* Handle the basic abort sequence (BA_ABTS) event */
15051 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15052 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15053 return;
15054 }
15055
James Smart4d9ab992009-10-02 15:16:39 -040015056 /* Link this frame */
15057 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15058 if (!seq_dmabuf) {
15059 /* unable to add frame to vport - throw it out */
15060 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15061 return;
15062 }
15063 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015064 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015065 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015066
James Smart6669f9b2009-10-02 15:16:45 -040015067 /* Send the complete sequence to the upper layer protocol */
15068 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015069}
James Smart6fb120a2009-05-22 14:52:59 -040015070
15071/**
15072 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15073 * @phba: pointer to lpfc hba data structure.
15074 *
15075 * This routine is invoked to post rpi header templates to the
15076 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015077 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15078 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015079 *
15080 * This routine does not require any locks. It's usage is expected
15081 * to be driver load or reset recovery when the driver is
15082 * sequential.
15083 *
15084 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015085 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015086 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015087 * When this error occurs, the driver is not guaranteed
15088 * to have any rpi regions posted to the device and
15089 * must either attempt to repost the regions or take a
15090 * fatal error.
15091 **/
15092int
15093lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15094{
15095 struct lpfc_rpi_hdr *rpi_page;
15096 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015097 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015098
James Smart6d368e52011-05-24 11:44:12 -040015099 /* SLI4 ports that support extents do not require RPI headers. */
15100 if (!phba->sli4_hba.rpi_hdrs_in_use)
15101 goto exit;
15102 if (phba->sli4_hba.extents_in_use)
15103 return -EIO;
15104
James Smart6fb120a2009-05-22 14:52:59 -040015105 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015106 /*
15107 * Assign the rpi headers a physical rpi only if the driver
15108 * has not initialized those resources. A port reset only
15109 * needs the headers posted.
15110 */
15111 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15112 LPFC_RPI_RSRC_RDY)
15113 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15114
James Smart6fb120a2009-05-22 14:52:59 -040015115 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15116 if (rc != MBX_SUCCESS) {
15117 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15118 "2008 Error %d posting all rpi "
15119 "headers\n", rc);
15120 rc = -EIO;
15121 break;
15122 }
15123 }
15124
James Smart6d368e52011-05-24 11:44:12 -040015125 exit:
15126 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15127 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015128 return rc;
15129}
15130
15131/**
15132 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15133 * @phba: pointer to lpfc hba data structure.
15134 * @rpi_page: pointer to the rpi memory region.
15135 *
15136 * This routine is invoked to post a single rpi header to the
15137 * HBA consistent with the SLI-4 interface spec. This memory region
15138 * maps up to 64 rpi context regions.
15139 *
15140 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015141 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015142 * -ENOMEM - No available memory
15143 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015144 **/
15145int
15146lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15147{
15148 LPFC_MBOXQ_t *mboxq;
15149 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15150 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015151 uint32_t shdr_status, shdr_add_status;
15152 union lpfc_sli4_cfg_shdr *shdr;
15153
James Smart6d368e52011-05-24 11:44:12 -040015154 /* SLI4 ports that support extents do not require RPI headers. */
15155 if (!phba->sli4_hba.rpi_hdrs_in_use)
15156 return rc;
15157 if (phba->sli4_hba.extents_in_use)
15158 return -EIO;
15159
James Smart6fb120a2009-05-22 14:52:59 -040015160 /* The port is notified of the header region via a mailbox command. */
15161 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15162 if (!mboxq) {
15163 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15164 "2001 Unable to allocate memory for issuing "
15165 "SLI_CONFIG_SPECIAL mailbox command\n");
15166 return -ENOMEM;
15167 }
15168
15169 /* Post all rpi memory regions to the port. */
15170 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015171 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15172 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15173 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015174 sizeof(struct lpfc_sli4_cfg_mhdr),
15175 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015176
15177
15178 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015179 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15180 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015181 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15182 hdr_tmpl, rpi_page->page_count);
15183
James Smart6fb120a2009-05-22 14:52:59 -040015184 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15185 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015186 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015187 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15188 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15189 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15190 if (rc != MBX_TIMEOUT)
15191 mempool_free(mboxq, phba->mbox_mem_pool);
15192 if (shdr_status || shdr_add_status || rc) {
15193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15194 "2514 POST_RPI_HDR mailbox failed with "
15195 "status x%x add_status x%x, mbx status x%x\n",
15196 shdr_status, shdr_add_status, rc);
15197 rc = -ENXIO;
15198 }
15199 return rc;
15200}
15201
15202/**
15203 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15204 * @phba: pointer to lpfc hba data structure.
15205 *
15206 * This routine is invoked to post rpi header templates to the
15207 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015208 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15209 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015210 *
15211 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015212 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015213 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15214 **/
15215int
15216lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15217{
James Smart6d368e52011-05-24 11:44:12 -040015218 unsigned long rpi;
15219 uint16_t max_rpi, rpi_limit;
15220 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015221 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015222 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015223
15224 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040015225 rpi_limit = phba->sli4_hba.next_rpi;
15226
15227 /*
James Smart6d368e52011-05-24 11:44:12 -040015228 * Fetch the next logical rpi. Because this index is logical,
15229 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015230 */
James Smart4902b382013-10-10 12:20:35 -040015231 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015232 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15233 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015234 rpi = LPFC_RPI_ALLOC_ERROR;
15235 else {
15236 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15237 phba->sli4_hba.max_cfg_param.rpi_used++;
15238 phba->sli4_hba.rpi_count++;
15239 }
15240
15241 /*
15242 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015243 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015244 */
15245 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15246 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015247 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015248 return rpi;
15249 }
15250
15251 /*
James Smart6d368e52011-05-24 11:44:12 -040015252 * RPI header postings are not required for SLI4 ports capable of
15253 * extents.
15254 */
15255 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015256 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015257 return rpi;
15258 }
15259
15260 /*
James Smart6fb120a2009-05-22 14:52:59 -040015261 * If the driver is running low on rpi resources, allocate another
15262 * page now. Note that the next_rpi value is used because
15263 * it represents how many are actually in use whereas max_rpi notes
15264 * how many are supported max by the device.
15265 */
James Smart6d368e52011-05-24 11:44:12 -040015266 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015267 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015268 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15269 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15270 if (!rpi_hdr) {
15271 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15272 "2002 Error Could not grow rpi "
15273 "count\n");
15274 } else {
James Smart6d368e52011-05-24 11:44:12 -040015275 lrpi = rpi_hdr->start_rpi;
15276 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015277 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15278 }
15279 }
15280
15281 return rpi;
15282}
15283
15284/**
15285 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15286 * @phba: pointer to lpfc hba data structure.
15287 *
15288 * This routine is invoked to release an rpi to the pool of
15289 * available rpis maintained by the driver.
15290 **/
15291void
James Smartd7c47992010-06-08 18:31:54 -040015292__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15293{
15294 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15295 phba->sli4_hba.rpi_count--;
15296 phba->sli4_hba.max_cfg_param.rpi_used--;
15297 }
15298}
15299
15300/**
15301 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15302 * @phba: pointer to lpfc hba data structure.
15303 *
15304 * This routine is invoked to release an rpi to the pool of
15305 * available rpis maintained by the driver.
15306 **/
15307void
James Smart6fb120a2009-05-22 14:52:59 -040015308lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15309{
15310 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015311 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015312 spin_unlock_irq(&phba->hbalock);
15313}
15314
15315/**
15316 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15317 * @phba: pointer to lpfc hba data structure.
15318 *
15319 * This routine is invoked to remove the memory region that
15320 * provided rpi via a bitmask.
15321 **/
15322void
15323lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15324{
15325 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040015326 kfree(phba->sli4_hba.rpi_ids);
15327 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040015328}
15329
15330/**
15331 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15332 * @phba: pointer to lpfc hba data structure.
15333 *
15334 * This routine is invoked to remove the memory region that
15335 * provided rpi via a bitmask.
15336 **/
15337int
James Smart6b5151f2012-01-18 16:24:06 -050015338lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15339 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040015340{
15341 LPFC_MBOXQ_t *mboxq;
15342 struct lpfc_hba *phba = ndlp->phba;
15343 int rc;
15344
15345 /* The port is notified of the header region via a mailbox command. */
15346 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15347 if (!mboxq)
15348 return -ENOMEM;
15349
15350 /* Post all rpi memory regions to the port. */
15351 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050015352 if (cmpl) {
15353 mboxq->mbox_cmpl = cmpl;
15354 mboxq->context1 = arg;
15355 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050015356 } else
15357 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050015358 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040015359 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15360 if (rc == MBX_NOT_FINISHED) {
15361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15362 "2010 Resume RPI Mailbox failed "
15363 "status %d, mbxStatus x%x\n", rc,
15364 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15365 mempool_free(mboxq, phba->mbox_mem_pool);
15366 return -EIO;
15367 }
15368 return 0;
15369}
15370
15371/**
15372 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050015373 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040015374 *
James Smart76a95d72010-11-20 23:11:48 -050015375 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040015376 *
15377 * Returns:
15378 * 0 success
15379 * -Evalue otherwise
15380 **/
15381int
James Smart76a95d72010-11-20 23:11:48 -050015382lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040015383{
15384 LPFC_MBOXQ_t *mboxq;
15385 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040015386 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040015387 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050015388 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040015389 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15390 if (!mboxq)
15391 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050015392 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040015393 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040015394 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040015395 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050015396 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040015397 "2022 INIT VPI Mailbox failed "
15398 "status %d, mbxStatus x%x\n", rc,
15399 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040015400 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040015401 }
James Smart6a9c52c2009-10-02 15:16:51 -040015402 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050015403 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040015404
15405 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040015406}
15407
15408/**
15409 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15410 * @phba: pointer to lpfc hba data structure.
15411 * @mboxq: Pointer to mailbox object.
15412 *
15413 * This routine is invoked to manually add a single FCF record. The caller
15414 * must pass a completely initialized FCF_Record. This routine takes
15415 * care of the nonembedded mailbox operations.
15416 **/
15417static void
15418lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15419{
15420 void *virt_addr;
15421 union lpfc_sli4_cfg_shdr *shdr;
15422 uint32_t shdr_status, shdr_add_status;
15423
15424 virt_addr = mboxq->sge_array->addr[0];
15425 /* The IOCTL status is embedded in the mailbox subheader. */
15426 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15427 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15428 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15429
15430 if ((shdr_status || shdr_add_status) &&
15431 (shdr_status != STATUS_FCF_IN_USE))
15432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15433 "2558 ADD_FCF_RECORD mailbox failed with "
15434 "status x%x add_status x%x\n",
15435 shdr_status, shdr_add_status);
15436
15437 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15438}
15439
15440/**
15441 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15442 * @phba: pointer to lpfc hba data structure.
15443 * @fcf_record: pointer to the initialized fcf record to add.
15444 *
15445 * This routine is invoked to manually add a single FCF record. The caller
15446 * must pass a completely initialized FCF_Record. This routine takes
15447 * care of the nonembedded mailbox operations.
15448 **/
15449int
15450lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15451{
15452 int rc = 0;
15453 LPFC_MBOXQ_t *mboxq;
15454 uint8_t *bytep;
15455 void *virt_addr;
15456 dma_addr_t phys_addr;
15457 struct lpfc_mbx_sge sge;
15458 uint32_t alloc_len, req_len;
15459 uint32_t fcfindex;
15460
15461 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15462 if (!mboxq) {
15463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15464 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15465 return -ENOMEM;
15466 }
15467
15468 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15469 sizeof(uint32_t);
15470
15471 /* Allocate DMA memory and set up the non-embedded mailbox command */
15472 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15473 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15474 req_len, LPFC_SLI4_MBX_NEMBED);
15475 if (alloc_len < req_len) {
15476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15477 "2523 Allocated DMA memory size (x%x) is "
15478 "less than the requested DMA memory "
15479 "size (x%x)\n", alloc_len, req_len);
15480 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15481 return -ENOMEM;
15482 }
15483
15484 /*
15485 * Get the first SGE entry from the non-embedded DMA memory. This
15486 * routine only uses a single SGE.
15487 */
15488 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15489 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040015490 virt_addr = mboxq->sge_array->addr[0];
15491 /*
15492 * Configure the FCF record for FCFI 0. This is the driver's
15493 * hardcoded default and gets used in nonFIP mode.
15494 */
15495 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15496 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15497 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15498
15499 /*
15500 * Copy the fcf_index and the FCF Record Data. The data starts after
15501 * the FCoE header plus word10. The data copy needs to be endian
15502 * correct.
15503 */
15504 bytep += sizeof(uint32_t);
15505 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15506 mboxq->vport = phba->pport;
15507 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15508 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15509 if (rc == MBX_NOT_FINISHED) {
15510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15511 "2515 ADD_FCF_RECORD mailbox failed with "
15512 "status 0x%x\n", rc);
15513 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15514 rc = -EIO;
15515 } else
15516 rc = 0;
15517
15518 return rc;
15519}
15520
15521/**
15522 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15523 * @phba: pointer to lpfc hba data structure.
15524 * @fcf_record: pointer to the fcf record to write the default data.
15525 * @fcf_index: FCF table entry index.
15526 *
15527 * This routine is invoked to build the driver's default FCF record. The
15528 * values used are hardcoded. This routine handles memory initialization.
15529 *
15530 **/
15531void
15532lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15533 struct fcf_record *fcf_record,
15534 uint16_t fcf_index)
15535{
15536 memset(fcf_record, 0, sizeof(struct fcf_record));
15537 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15538 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15539 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15540 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15541 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15542 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15543 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15544 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15545 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15546 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15547 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15548 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15549 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015550 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015551 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15552 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15553 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15554 /* Set the VLAN bit map */
15555 if (phba->valid_vlan) {
15556 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15557 = 1 << (phba->vlan_id % 8);
15558 }
15559}
15560
15561/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015562 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015563 * @phba: pointer to lpfc hba data structure.
15564 * @fcf_index: FCF table entry offset.
15565 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015566 * This routine is invoked to scan the entire FCF table by reading FCF
15567 * record and processing it one at a time starting from the @fcf_index
15568 * for initial FCF discovery or fast FCF failover rediscovery.
15569 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015570 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015571 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015572 **/
15573int
James Smart0c9ab6f2010-02-26 14:15:57 -050015574lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015575{
15576 int rc = 0, error;
15577 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015578
James Smart32b97932009-07-19 10:01:21 -040015579 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015580 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015581 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15582 if (!mboxq) {
15583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15584 "2000 Failed to allocate mbox for "
15585 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015586 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015587 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015588 }
James Smartecfd03c2010-02-12 14:41:27 -050015589 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015590 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015591 if (rc) {
15592 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015593 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015594 }
James Smartecfd03c2010-02-12 14:41:27 -050015595 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015596 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015597 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015598
15599 spin_lock_irq(&phba->hbalock);
15600 phba->hba_flag |= FCF_TS_INPROG;
15601 spin_unlock_irq(&phba->hbalock);
15602
James Smart6fb120a2009-05-22 14:52:59 -040015603 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015604 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015605 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015606 else {
James Smart38b92ef2010-08-04 16:11:39 -040015607 /* Reset eligible FCF count for new scan */
15608 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015609 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015610 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015611 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015612fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015613 if (error) {
15614 if (mboxq)
15615 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015616 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015617 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015618 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015619 spin_unlock_irq(&phba->hbalock);
15620 }
James Smart6fb120a2009-05-22 14:52:59 -040015621 return error;
15622}
James Smarta0c87cb2009-07-19 10:01:10 -040015623
15624/**
James Smarta93ff372010-10-22 11:06:08 -040015625 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015626 * @phba: pointer to lpfc hba data structure.
15627 * @fcf_index: FCF table entry offset.
15628 *
15629 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015630 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015631 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015632 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015633 * otherwise.
15634 **/
15635int
15636lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15637{
15638 int rc = 0, error;
15639 LPFC_MBOXQ_t *mboxq;
15640
15641 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15642 if (!mboxq) {
15643 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15644 "2763 Failed to allocate mbox for "
15645 "READ_FCF cmd\n");
15646 error = -ENOMEM;
15647 goto fail_fcf_read;
15648 }
15649 /* Construct the read FCF record mailbox command */
15650 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15651 if (rc) {
15652 error = -EINVAL;
15653 goto fail_fcf_read;
15654 }
15655 /* Issue the mailbox command asynchronously */
15656 mboxq->vport = phba->pport;
15657 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15658 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15659 if (rc == MBX_NOT_FINISHED)
15660 error = -EIO;
15661 else
15662 error = 0;
15663
15664fail_fcf_read:
15665 if (error && mboxq)
15666 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15667 return error;
15668}
15669
15670/**
15671 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15672 * @phba: pointer to lpfc hba data structure.
15673 * @fcf_index: FCF table entry offset.
15674 *
15675 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015676 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015677 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015678 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015679 * otherwise.
15680 **/
15681int
15682lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15683{
15684 int rc = 0, error;
15685 LPFC_MBOXQ_t *mboxq;
15686
15687 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15688 if (!mboxq) {
15689 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15690 "2758 Failed to allocate mbox for "
15691 "READ_FCF cmd\n");
15692 error = -ENOMEM;
15693 goto fail_fcf_read;
15694 }
15695 /* Construct the read FCF record mailbox command */
15696 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15697 if (rc) {
15698 error = -EINVAL;
15699 goto fail_fcf_read;
15700 }
15701 /* Issue the mailbox command asynchronously */
15702 mboxq->vport = phba->pport;
15703 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15704 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15705 if (rc == MBX_NOT_FINISHED)
15706 error = -EIO;
15707 else
15708 error = 0;
15709
15710fail_fcf_read:
15711 if (error && mboxq)
15712 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15713 return error;
15714}
15715
15716/**
James Smart7d791df2011-07-22 18:37:52 -040015717 * lpfc_check_next_fcf_pri
15718 * phba pointer to the lpfc_hba struct for this port.
15719 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15720 * routine when the rr_bmask is empty. The FCF indecies are put into the
15721 * rr_bmask based on their priority level. Starting from the highest priority
15722 * to the lowest. The most likely FCF candidate will be in the highest
15723 * priority group. When this routine is called it searches the fcf_pri list for
15724 * next lowest priority group and repopulates the rr_bmask with only those
15725 * fcf_indexes.
15726 * returns:
15727 * 1=success 0=failure
15728 **/
15729int
15730lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15731{
15732 uint16_t next_fcf_pri;
15733 uint16_t last_index;
15734 struct lpfc_fcf_pri *fcf_pri;
15735 int rc;
15736 int ret = 0;
15737
15738 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15739 LPFC_SLI4_FCF_TBL_INDX_MAX);
15740 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15741 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050015742
15743 /* Verify the priority list has 2 or more entries */
15744 spin_lock_irq(&phba->hbalock);
15745 if (list_empty(&phba->fcf.fcf_pri_list) ||
15746 list_is_singular(&phba->fcf.fcf_pri_list)) {
15747 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040015748 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15749 "3061 Last IDX %d\n", last_index);
15750 return 0; /* Empty rr list */
15751 }
James Smart25626692013-03-01 16:36:54 -050015752 spin_unlock_irq(&phba->hbalock);
15753
James Smart7d791df2011-07-22 18:37:52 -040015754 next_fcf_pri = 0;
15755 /*
15756 * Clear the rr_bmask and set all of the bits that are at this
15757 * priority.
15758 */
15759 memset(phba->fcf.fcf_rr_bmask, 0,
15760 sizeof(*phba->fcf.fcf_rr_bmask));
15761 spin_lock_irq(&phba->hbalock);
15762 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15763 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15764 continue;
15765 /*
15766 * the 1st priority that has not FLOGI failed
15767 * will be the highest.
15768 */
15769 if (!next_fcf_pri)
15770 next_fcf_pri = fcf_pri->fcf_rec.priority;
15771 spin_unlock_irq(&phba->hbalock);
15772 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15773 rc = lpfc_sli4_fcf_rr_index_set(phba,
15774 fcf_pri->fcf_rec.fcf_index);
15775 if (rc)
15776 return 0;
15777 }
15778 spin_lock_irq(&phba->hbalock);
15779 }
15780 /*
15781 * if next_fcf_pri was not set above and the list is not empty then
15782 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070015783 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040015784 */
15785 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15786 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15787 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15788 /*
15789 * the 1st priority that has not FLOGI failed
15790 * will be the highest.
15791 */
15792 if (!next_fcf_pri)
15793 next_fcf_pri = fcf_pri->fcf_rec.priority;
15794 spin_unlock_irq(&phba->hbalock);
15795 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15796 rc = lpfc_sli4_fcf_rr_index_set(phba,
15797 fcf_pri->fcf_rec.fcf_index);
15798 if (rc)
15799 return 0;
15800 }
15801 spin_lock_irq(&phba->hbalock);
15802 }
15803 } else
15804 ret = 1;
15805 spin_unlock_irq(&phba->hbalock);
15806
15807 return ret;
15808}
15809/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015810 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15811 * @phba: pointer to lpfc hba data structure.
15812 *
15813 * This routine is to get the next eligible FCF record index in a round
15814 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015815 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015816 * shall be returned, otherwise, the next eligible FCF record's index
15817 * shall be returned.
15818 **/
15819uint16_t
15820lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15821{
15822 uint16_t next_fcf_index;
15823
James Smart421c6622013-01-03 15:44:16 -050015824initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040015825 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050015826 next_fcf_index = phba->fcf.current_rec.fcf_indx;
15827
James Smart7d791df2011-07-22 18:37:52 -040015828next_priority:
James Smart421c6622013-01-03 15:44:16 -050015829 /* Determine the next fcf index to check */
15830 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015831 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15832 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015833 next_fcf_index);
15834
James Smart0c9ab6f2010-02-26 14:15:57 -050015835 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015836 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15837 /*
15838 * If we have wrapped then we need to clear the bits that
15839 * have been tested so that we can detect when we should
15840 * change the priority level.
15841 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015842 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15843 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015844 }
15845
James Smart0c9ab6f2010-02-26 14:15:57 -050015846
James Smart3804dc82010-07-14 15:31:37 -040015847 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015848 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15849 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15850 /*
15851 * If next fcf index is not found check if there are lower
15852 * Priority level fcf's in the fcf_priority list.
15853 * Set up the rr_bmask with all of the avaiable fcf bits
15854 * at that level and continue the selection process.
15855 */
15856 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050015857 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040015858 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15859 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015860 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15861 return LPFC_FCOE_FCF_NEXT_NONE;
15862 else {
15863 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15864 "3063 Only FCF available idx %d, flag %x\n",
15865 next_fcf_index,
15866 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15867 return next_fcf_index;
15868 }
James Smart3804dc82010-07-14 15:31:37 -040015869 }
15870
James Smart7d791df2011-07-22 18:37:52 -040015871 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15872 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15873 LPFC_FCF_FLOGI_FAILED)
15874 goto next_priority;
15875
James Smart3804dc82010-07-14 15:31:37 -040015876 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015877 "2845 Get next roundrobin failover FCF (x%x)\n",
15878 next_fcf_index);
15879
James Smart0c9ab6f2010-02-26 14:15:57 -050015880 return next_fcf_index;
15881}
15882
15883/**
15884 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15885 * @phba: pointer to lpfc hba data structure.
15886 *
15887 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015888 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015889 * does not go beyond the range of the driver allocated bmask dimension
15890 * before setting the bit.
15891 *
15892 * Returns 0 if the index bit successfully set, otherwise, it returns
15893 * -EINVAL.
15894 **/
15895int
15896lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15897{
15898 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15899 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015900 "2610 FCF (x%x) reached driver's book "
15901 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015902 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15903 return -EINVAL;
15904 }
15905 /* Set the eligible FCF record index bmask */
15906 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15907
James Smart3804dc82010-07-14 15:31:37 -040015908 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015909 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015910 "bmask\n", fcf_index);
15911
James Smart0c9ab6f2010-02-26 14:15:57 -050015912 return 0;
15913}
15914
15915/**
James Smart3804dc82010-07-14 15:31:37 -040015916 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015917 * @phba: pointer to lpfc hba data structure.
15918 *
15919 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015920 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015921 * does not go beyond the range of the driver allocated bmask dimension
15922 * before clearing the bit.
15923 **/
15924void
15925lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15926{
James Smart9a803a72013-09-06 12:17:56 -040015927 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050015928 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15929 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015930 "2762 FCF (x%x) reached driver's book "
15931 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015932 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15933 return;
15934 }
15935 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015936 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040015937 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
15938 list) {
James Smart7d791df2011-07-22 18:37:52 -040015939 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15940 list_del_init(&fcf_pri->list);
15941 break;
15942 }
15943 }
15944 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015945 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015946
15947 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015948 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015949 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015950}
15951
15952/**
James Smartecfd03c2010-02-12 14:41:27 -050015953 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15954 * @phba: pointer to lpfc hba data structure.
15955 *
15956 * This routine is the completion routine for the rediscover FCF table mailbox
15957 * command. If the mailbox command returned failure, it will try to stop the
15958 * FCF rediscover wait timer.
15959 **/
15960void
15961lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15962{
15963 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15964 uint32_t shdr_status, shdr_add_status;
15965
15966 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15967
15968 shdr_status = bf_get(lpfc_mbox_hdr_status,
15969 &redisc_fcf->header.cfg_shdr.response);
15970 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15971 &redisc_fcf->header.cfg_shdr.response);
15972 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015973 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015974 "2746 Requesting for FCF rediscovery failed "
15975 "status x%x add_status x%x\n",
15976 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015977 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015978 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015979 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015980 spin_unlock_irq(&phba->hbalock);
15981 /*
15982 * CVL event triggered FCF rediscover request failed,
15983 * last resort to re-try current registered FCF entry.
15984 */
15985 lpfc_retry_pport_discovery(phba);
15986 } else {
15987 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015988 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015989 spin_unlock_irq(&phba->hbalock);
15990 /*
15991 * DEAD FCF event triggered FCF rediscover request
15992 * failed, last resort to fail over as a link down
15993 * to FCF registration.
15994 */
15995 lpfc_sli4_fcf_dead_failthrough(phba);
15996 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015997 } else {
15998 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015999 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016000 /*
16001 * Start FCF rediscovery wait timer for pending FCF
16002 * before rescan FCF record table.
16003 */
16004 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016005 }
James Smartecfd03c2010-02-12 14:41:27 -050016006
16007 mempool_free(mbox, phba->mbox_mem_pool);
16008}
16009
16010/**
James Smart3804dc82010-07-14 15:31:37 -040016011 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016012 * @phba: pointer to lpfc hba data structure.
16013 *
16014 * This routine is invoked to request for rediscovery of the entire FCF table
16015 * by the port.
16016 **/
16017int
16018lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16019{
16020 LPFC_MBOXQ_t *mbox;
16021 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16022 int rc, length;
16023
James Smart0c9ab6f2010-02-26 14:15:57 -050016024 /* Cancel retry delay timers to all vports before FCF rediscover */
16025 lpfc_cancel_all_vport_retry_delay_timer(phba);
16026
James Smartecfd03c2010-02-12 14:41:27 -050016027 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16028 if (!mbox) {
16029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16030 "2745 Failed to allocate mbox for "
16031 "requesting FCF rediscover.\n");
16032 return -ENOMEM;
16033 }
16034
16035 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16036 sizeof(struct lpfc_sli4_cfg_mhdr));
16037 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16038 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16039 length, LPFC_SLI4_MBX_EMBED);
16040
16041 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16042 /* Set count to 0 for invalidating the entire FCF database */
16043 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16044
16045 /* Issue the mailbox command asynchronously */
16046 mbox->vport = phba->pport;
16047 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16048 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16049
16050 if (rc == MBX_NOT_FINISHED) {
16051 mempool_free(mbox, phba->mbox_mem_pool);
16052 return -EIO;
16053 }
16054 return 0;
16055}
16056
16057/**
James Smartfc2b9892010-02-26 14:15:29 -050016058 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16059 * @phba: pointer to lpfc hba data structure.
16060 *
16061 * This function is the failover routine as a last resort to the FCF DEAD
16062 * event when driver failed to perform fast FCF failover.
16063 **/
16064void
16065lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16066{
16067 uint32_t link_state;
16068
16069 /*
16070 * Last resort as FCF DEAD event failover will treat this as
16071 * a link down, but save the link state because we don't want
16072 * it to be changed to Link Down unless it is already down.
16073 */
16074 link_state = phba->link_state;
16075 lpfc_linkdown(phba);
16076 phba->link_state = link_state;
16077
16078 /* Unregister FCF if no devices connected to it */
16079 lpfc_unregister_unused_fcf(phba);
16080}
16081
16082/**
James Smart026abb82011-12-13 13:20:45 -050016083 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016084 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016085 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016086 *
James Smart026abb82011-12-13 13:20:45 -050016087 * This function gets SLI3 port configure region 23 data through memory dump
16088 * mailbox command. When it successfully retrieves data, the size of the data
16089 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016090 **/
James Smart026abb82011-12-13 13:20:45 -050016091static uint32_t
16092lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016093{
16094 LPFC_MBOXQ_t *pmb = NULL;
16095 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016096 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016097 int rc;
16098
James Smart026abb82011-12-13 13:20:45 -050016099 if (!rgn23_data)
16100 return 0;
16101
James Smarta0c87cb2009-07-19 10:01:10 -040016102 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16103 if (!pmb) {
16104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016105 "2600 failed to allocate mailbox memory\n");
16106 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016107 }
16108 mb = &pmb->u.mb;
16109
James Smarta0c87cb2009-07-19 10:01:10 -040016110 do {
16111 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16112 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16113
16114 if (rc != MBX_SUCCESS) {
16115 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016116 "2601 failed to read config "
16117 "region 23, rc 0x%x Status 0x%x\n",
16118 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016119 mb->un.varDmp.word_cnt = 0;
16120 }
16121 /*
16122 * dump mem may return a zero when finished or we got a
16123 * mailbox error, either way we are done.
16124 */
16125 if (mb->un.varDmp.word_cnt == 0)
16126 break;
16127 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16128 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16129
16130 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016131 rgn23_data + offset,
16132 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016133 offset += mb->un.varDmp.word_cnt;
16134 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16135
James Smart026abb82011-12-13 13:20:45 -050016136 mempool_free(pmb, phba->mbox_mem_pool);
16137 return offset;
16138}
16139
16140/**
16141 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16142 * @phba: pointer to lpfc hba data structure.
16143 * @rgn23_data: pointer to configure region 23 data.
16144 *
16145 * This function gets SLI4 port configure region 23 data through memory dump
16146 * mailbox command. When it successfully retrieves data, the size of the data
16147 * will be returned, otherwise, 0 will be returned.
16148 **/
16149static uint32_t
16150lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16151{
16152 LPFC_MBOXQ_t *mboxq = NULL;
16153 struct lpfc_dmabuf *mp = NULL;
16154 struct lpfc_mqe *mqe;
16155 uint32_t data_length = 0;
16156 int rc;
16157
16158 if (!rgn23_data)
16159 return 0;
16160
16161 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16162 if (!mboxq) {
16163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16164 "3105 failed to allocate mailbox memory\n");
16165 return 0;
16166 }
16167
16168 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16169 goto out;
16170 mqe = &mboxq->u.mqe;
16171 mp = (struct lpfc_dmabuf *) mboxq->context1;
16172 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16173 if (rc)
16174 goto out;
16175 data_length = mqe->un.mb_words[5];
16176 if (data_length == 0)
16177 goto out;
16178 if (data_length > DMP_RGN23_SIZE) {
16179 data_length = 0;
16180 goto out;
16181 }
16182 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16183out:
16184 mempool_free(mboxq, phba->mbox_mem_pool);
16185 if (mp) {
16186 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16187 kfree(mp);
16188 }
16189 return data_length;
16190}
16191
16192/**
16193 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16194 * @phba: pointer to lpfc hba data structure.
16195 *
16196 * This function read region 23 and parse TLV for port status to
16197 * decide if the user disaled the port. If the TLV indicates the
16198 * port is disabled, the hba_flag is set accordingly.
16199 **/
16200void
16201lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16202{
16203 uint8_t *rgn23_data = NULL;
16204 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16205 uint32_t offset = 0;
16206
16207 /* Get adapter Region 23 data */
16208 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16209 if (!rgn23_data)
16210 goto out;
16211
16212 if (phba->sli_rev < LPFC_SLI_REV4)
16213 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16214 else {
16215 if_type = bf_get(lpfc_sli_intf_if_type,
16216 &phba->sli4_hba.sli_intf);
16217 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16218 goto out;
16219 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16220 }
James Smarta0c87cb2009-07-19 10:01:10 -040016221
16222 if (!data_size)
16223 goto out;
16224
16225 /* Check the region signature first */
16226 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16228 "2619 Config region 23 has bad signature\n");
16229 goto out;
16230 }
16231 offset += 4;
16232
16233 /* Check the data structure version */
16234 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16236 "2620 Config region 23 has bad version\n");
16237 goto out;
16238 }
16239 offset += 4;
16240
16241 /* Parse TLV entries in the region */
16242 while (offset < data_size) {
16243 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16244 break;
16245 /*
16246 * If the TLV is not driver specific TLV or driver id is
16247 * not linux driver id, skip the record.
16248 */
16249 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16250 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16251 (rgn23_data[offset + 3] != 0)) {
16252 offset += rgn23_data[offset + 1] * 4 + 4;
16253 continue;
16254 }
16255
16256 /* Driver found a driver specific TLV in the config region */
16257 sub_tlv_len = rgn23_data[offset + 1] * 4;
16258 offset += 4;
16259 tlv_offset = 0;
16260
16261 /*
16262 * Search for configured port state sub-TLV.
16263 */
16264 while ((offset < data_size) &&
16265 (tlv_offset < sub_tlv_len)) {
16266 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16267 offset += 4;
16268 tlv_offset += 4;
16269 break;
16270 }
16271 if (rgn23_data[offset] != PORT_STE_TYPE) {
16272 offset += rgn23_data[offset + 1] * 4 + 4;
16273 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16274 continue;
16275 }
16276
16277 /* This HBA contains PORT_STE configured */
16278 if (!rgn23_data[offset + 2])
16279 phba->hba_flag |= LINK_DISABLED;
16280
16281 goto out;
16282 }
16283 }
James Smart026abb82011-12-13 13:20:45 -050016284
James Smarta0c87cb2009-07-19 10:01:10 -040016285out:
James Smarta0c87cb2009-07-19 10:01:10 -040016286 kfree(rgn23_data);
16287 return;
16288}
James Smart695a8142010-01-26 23:08:03 -050016289
16290/**
James Smart52d52442011-05-24 11:42:45 -040016291 * lpfc_wr_object - write an object to the firmware
16292 * @phba: HBA structure that indicates port to create a queue on.
16293 * @dmabuf_list: list of dmabufs to write to the port.
16294 * @size: the total byte value of the objects to write to the port.
16295 * @offset: the current offset to be used to start the transfer.
16296 *
16297 * This routine will create a wr_object mailbox command to send to the port.
16298 * the mailbox command will be constructed using the dma buffers described in
16299 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16300 * BDEs that the imbedded mailbox can support. The @offset variable will be
16301 * used to indicate the starting offset of the transfer and will also return
16302 * the offset after the write object mailbox has completed. @size is used to
16303 * determine the end of the object and whether the eof bit should be set.
16304 *
16305 * Return 0 is successful and offset will contain the the new offset to use
16306 * for the next write.
16307 * Return negative value for error cases.
16308 **/
16309int
16310lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16311 uint32_t size, uint32_t *offset)
16312{
16313 struct lpfc_mbx_wr_object *wr_object;
16314 LPFC_MBOXQ_t *mbox;
16315 int rc = 0, i = 0;
16316 uint32_t shdr_status, shdr_add_status;
16317 uint32_t mbox_tmo;
16318 union lpfc_sli4_cfg_shdr *shdr;
16319 struct lpfc_dmabuf *dmabuf;
16320 uint32_t written = 0;
16321
16322 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16323 if (!mbox)
16324 return -ENOMEM;
16325
16326 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16327 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16328 sizeof(struct lpfc_mbx_wr_object) -
16329 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16330
16331 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16332 wr_object->u.request.write_offset = *offset;
16333 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16334 wr_object->u.request.object_name[0] =
16335 cpu_to_le32(wr_object->u.request.object_name[0]);
16336 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16337 list_for_each_entry(dmabuf, dmabuf_list, list) {
16338 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16339 break;
16340 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16341 wr_object->u.request.bde[i].addrHigh =
16342 putPaddrHigh(dmabuf->phys);
16343 if (written + SLI4_PAGE_SIZE >= size) {
16344 wr_object->u.request.bde[i].tus.f.bdeSize =
16345 (size - written);
16346 written += (size - written);
16347 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16348 } else {
16349 wr_object->u.request.bde[i].tus.f.bdeSize =
16350 SLI4_PAGE_SIZE;
16351 written += SLI4_PAGE_SIZE;
16352 }
16353 i++;
16354 }
16355 wr_object->u.request.bde_count = i;
16356 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16357 if (!phba->sli4_hba.intr_enable)
16358 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16359 else {
James Smarta183a152011-10-10 21:32:43 -040016360 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040016361 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16362 }
16363 /* The IOCTL status is embedded in the mailbox subheader. */
16364 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16365 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16366 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16367 if (rc != MBX_TIMEOUT)
16368 mempool_free(mbox, phba->mbox_mem_pool);
16369 if (shdr_status || shdr_add_status || rc) {
16370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16371 "3025 Write Object mailbox failed with "
16372 "status x%x add_status x%x, mbx status x%x\n",
16373 shdr_status, shdr_add_status, rc);
16374 rc = -ENXIO;
16375 } else
16376 *offset += wr_object->u.response.actual_write_length;
16377 return rc;
16378}
16379
16380/**
James Smart695a8142010-01-26 23:08:03 -050016381 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16382 * @vport: pointer to vport data structure.
16383 *
16384 * This function iterate through the mailboxq and clean up all REG_LOGIN
16385 * and REG_VPI mailbox commands associated with the vport. This function
16386 * is called when driver want to restart discovery of the vport due to
16387 * a Clear Virtual Link event.
16388 **/
16389void
16390lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16391{
16392 struct lpfc_hba *phba = vport->phba;
16393 LPFC_MBOXQ_t *mb, *nextmb;
16394 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040016395 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040016396 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040016397 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040016398 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050016399 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050016400
James Smartd439d282010-09-29 11:18:45 -040016401 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050016402 spin_lock_irq(&phba->hbalock);
16403 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16404 if (mb->vport != vport)
16405 continue;
16406
16407 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16408 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16409 continue;
16410
James Smartd439d282010-09-29 11:18:45 -040016411 list_del(&mb->list);
16412 list_add_tail(&mb->list, &mbox_cmd_list);
16413 }
16414 /* Clean up active mailbox command with the vport */
16415 mb = phba->sli.mbox_active;
16416 if (mb && (mb->vport == vport)) {
16417 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16418 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16419 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16420 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16421 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16422 /* Put reference count for delayed processing */
16423 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16424 /* Unregister the RPI when mailbox complete */
16425 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16426 }
16427 }
James Smart63e801c2010-11-20 23:14:19 -050016428 /* Cleanup any mailbox completions which are not yet processed */
16429 do {
16430 restart_loop = 0;
16431 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16432 /*
16433 * If this mailox is already processed or it is
16434 * for another vport ignore it.
16435 */
16436 if ((mb->vport != vport) ||
16437 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16438 continue;
16439
16440 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16441 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16442 continue;
16443
16444 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16445 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16446 ndlp = (struct lpfc_nodelist *)mb->context2;
16447 /* Unregister the RPI when mailbox complete */
16448 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16449 restart_loop = 1;
16450 spin_unlock_irq(&phba->hbalock);
16451 spin_lock(shost->host_lock);
16452 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16453 spin_unlock(shost->host_lock);
16454 spin_lock_irq(&phba->hbalock);
16455 break;
16456 }
16457 }
16458 } while (restart_loop);
16459
James Smartd439d282010-09-29 11:18:45 -040016460 spin_unlock_irq(&phba->hbalock);
16461
16462 /* Release the cleaned-up mailbox commands */
16463 while (!list_empty(&mbox_cmd_list)) {
16464 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050016465 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16466 mp = (struct lpfc_dmabuf *) (mb->context1);
16467 if (mp) {
16468 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16469 kfree(mp);
16470 }
James Smart78730cf2010-04-06 15:06:30 -040016471 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040016472 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040016473 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016474 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040016475 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016476 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040016477 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040016478 }
James Smart695a8142010-01-26 23:08:03 -050016479 }
James Smart695a8142010-01-26 23:08:03 -050016480 mempool_free(mb, phba->mbox_mem_pool);
16481 }
James Smartd439d282010-09-29 11:18:45 -040016482
16483 /* Release the ndlp with the cleaned-up active mailbox command */
16484 if (act_mbx_ndlp) {
16485 spin_lock(shost->host_lock);
16486 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16487 spin_unlock(shost->host_lock);
16488 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050016489 }
James Smart695a8142010-01-26 23:08:03 -050016490}
16491
James Smart2a9bf3d2010-06-07 15:24:45 -040016492/**
16493 * lpfc_drain_txq - Drain the txq
16494 * @phba: Pointer to HBA context object.
16495 *
16496 * This function attempt to submit IOCBs on the txq
16497 * to the adapter. For SLI4 adapters, the txq contains
16498 * ELS IOCBs that have been deferred because the there
16499 * are no SGLs. This congestion can occur with large
16500 * vport counts during node discovery.
16501 **/
16502
16503uint32_t
16504lpfc_drain_txq(struct lpfc_hba *phba)
16505{
16506 LIST_HEAD(completions);
16507 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16508 struct lpfc_iocbq *piocbq = 0;
16509 unsigned long iflags = 0;
16510 char *fail_msg = NULL;
16511 struct lpfc_sglq *sglq;
16512 union lpfc_wqe wqe;
James Smart0e9bb8d2013-03-01 16:35:12 -050016513 int txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040016514
James Smart398d81c2013-05-31 17:04:19 -040016515 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050016516 list_for_each_entry(piocbq, &pring->txq, list) {
16517 txq_cnt++;
16518 }
16519
16520 if (txq_cnt > pring->txq_max)
16521 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016522
James Smart398d81c2013-05-31 17:04:19 -040016523 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016524
James Smart0e9bb8d2013-03-01 16:35:12 -050016525 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040016526 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016527
James Smart19ca7602010-11-20 23:11:55 -050016528 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016529 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040016530 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040016531 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16532 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050016533 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040016534 break;
16535 }
James Smart19ca7602010-11-20 23:11:55 -050016536 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016537 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016538 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040016539 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016540 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016541 }
James Smart0e9bb8d2013-03-01 16:35:12 -050016542 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040016543
16544 /* The xri and iocb resources secured,
16545 * attempt to issue request
16546 */
James Smart6d368e52011-05-24 11:44:12 -040016547 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016548 piocbq->sli4_xritag = sglq->sli4_xritag;
16549 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16550 fail_msg = "to convert bpl to sgl";
16551 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16552 fail_msg = "to convert iocb to wqe";
16553 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16554 fail_msg = " - Wq is full";
16555 else
16556 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16557
16558 if (fail_msg) {
16559 /* Failed means we can't issue and need to cancel */
16560 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16561 "2822 IOCB failed %s iotag 0x%x "
16562 "xri 0x%x\n",
16563 fail_msg,
16564 piocbq->iotag, piocbq->sli4_xritag);
16565 list_add_tail(&piocbq->list, &completions);
16566 }
James Smart398d81c2013-05-31 17:04:19 -040016567 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040016568 }
16569
James Smart2a9bf3d2010-06-07 15:24:45 -040016570 /* Cancel all the IOCBs that cannot be issued */
16571 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16572 IOERR_SLI_ABORTED);
16573
James Smart0e9bb8d2013-03-01 16:35:12 -050016574 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016575}